flex_array.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Flexible array managed in PAGE_SIZE parts
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2009
  19. *
  20. * Author: Dave Hansen <dave@linux.vnet.ibm.com>
  21. */
  22. #include <linux/flex_array.h>
  23. #include <linux/slab.h>
  24. #include <linux/stddef.h>
  25. #include <linux/module.h>
  26. #include <linux/reciprocal_div.h>
  27. struct flex_array_part {
  28. char elements[FLEX_ARRAY_PART_SIZE];
  29. };
  30. /*
  31. * If a user requests an allocation which is small
  32. * enough, we may simply use the space in the
  33. * flex_array->parts[] array to store the user
  34. * data.
  35. */
  36. static inline int elements_fit_in_base(struct flex_array *fa)
  37. {
  38. int data_size = fa->element_size * fa->total_nr_elements;
  39. if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
  40. return 1;
  41. return 0;
  42. }
  43. /**
  44. * flex_array_alloc - allocate a new flexible array
  45. * @element_size: the size of individual elements in the array
  46. * @total: total number of elements that this should hold
  47. * @flags: page allocation flags to use for base array
  48. *
  49. * Note: all locking must be provided by the caller.
  50. *
  51. * @total is used to size internal structures. If the user ever
  52. * accesses any array indexes >=@total, it will produce errors.
  53. *
  54. * The maximum number of elements is defined as: the number of
  55. * elements that can be stored in a page times the number of
  56. * page pointers that we can fit in the base structure or (using
  57. * integer math):
  58. *
  59. * (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
  60. *
  61. * Here's a table showing example capacities. Note that the maximum
  62. * index that the get/put() functions is just nr_objects-1. This
  63. * basically means that you get 4MB of storage on 32-bit and 2MB on
  64. * 64-bit.
  65. *
  66. *
  67. * Element size | Objects | Objects |
  68. * PAGE_SIZE=4k | 32-bit | 64-bit |
  69. * ---------------------------------|
  70. * 1 bytes | 4177920 | 2088960 |
  71. * 2 bytes | 2088960 | 1044480 |
  72. * 3 bytes | 1392300 | 696150 |
  73. * 4 bytes | 1044480 | 522240 |
  74. * 32 bytes | 130560 | 65408 |
  75. * 33 bytes | 126480 | 63240 |
  76. * 2048 bytes | 2040 | 1020 |
  77. * 2049 bytes | 1020 | 510 |
  78. * void * | 1044480 | 261120 |
  79. *
  80. * Since 64-bit pointers are twice the size, we lose half the
  81. * capacity in the base structure. Also note that no effort is made
  82. * to efficiently pack objects across page boundaries.
  83. */
  84. struct flex_array *flex_array_alloc(int element_size, unsigned int total,
  85. gfp_t flags)
  86. {
  87. struct flex_array *ret;
  88. int elems_per_part = 0;
  89. int reciprocal_elems = 0;
  90. int max_size = 0;
  91. if (element_size) {
  92. elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
  93. reciprocal_elems = reciprocal_value(elems_per_part);
  94. max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part;
  95. }
  96. /* max_size will end up 0 if element_size > PAGE_SIZE */
  97. if (total > max_size)
  98. return NULL;
  99. ret = kzalloc(sizeof(struct flex_array), flags);
  100. if (!ret)
  101. return NULL;
  102. ret->element_size = element_size;
  103. ret->total_nr_elements = total;
  104. ret->elems_per_part = elems_per_part;
  105. ret->reciprocal_elems = reciprocal_elems;
  106. if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
  107. memset(&ret->parts[0], FLEX_ARRAY_FREE,
  108. FLEX_ARRAY_BASE_BYTES_LEFT);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(flex_array_alloc);
  112. static int fa_element_to_part_nr(struct flex_array *fa,
  113. unsigned int element_nr)
  114. {
  115. return reciprocal_divide(element_nr, fa->reciprocal_elems);
  116. }
  117. /**
  118. * flex_array_free_parts - just free the second-level pages
  119. * @fa: the flex array from which to free parts
  120. *
  121. * This is to be used in cases where the base 'struct flex_array'
  122. * has been statically allocated and should not be free.
  123. */
  124. void flex_array_free_parts(struct flex_array *fa)
  125. {
  126. int part_nr;
  127. if (elements_fit_in_base(fa))
  128. return;
  129. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
  130. kfree(fa->parts[part_nr]);
  131. }
  132. EXPORT_SYMBOL(flex_array_free_parts);
  133. void flex_array_free(struct flex_array *fa)
  134. {
  135. flex_array_free_parts(fa);
  136. kfree(fa);
  137. }
  138. EXPORT_SYMBOL(flex_array_free);
  139. static unsigned int index_inside_part(struct flex_array *fa,
  140. unsigned int element_nr,
  141. unsigned int part_nr)
  142. {
  143. unsigned int part_offset;
  144. part_offset = element_nr - part_nr * fa->elems_per_part;
  145. return part_offset * fa->element_size;
  146. }
  147. static struct flex_array_part *
  148. __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
  149. {
  150. struct flex_array_part *part = fa->parts[part_nr];
  151. if (!part) {
  152. part = kmalloc(sizeof(struct flex_array_part), flags);
  153. if (!part)
  154. return NULL;
  155. if (!(flags & __GFP_ZERO))
  156. memset(part, FLEX_ARRAY_FREE,
  157. sizeof(struct flex_array_part));
  158. fa->parts[part_nr] = part;
  159. }
  160. return part;
  161. }
  162. /**
  163. * flex_array_put - copy data into the array at @element_nr
  164. * @fa: the flex array to copy data into
  165. * @element_nr: index of the position in which to insert
  166. * the new element.
  167. * @src: address of data to copy into the array
  168. * @flags: page allocation flags to use for array expansion
  169. *
  170. *
  171. * Note that this *copies* the contents of @src into
  172. * the array. If you are trying to store an array of
  173. * pointers, make sure to pass in &ptr instead of ptr.
  174. * You may instead wish to use the flex_array_put_ptr()
  175. * helper function.
  176. *
  177. * Locking must be provided by the caller.
  178. */
  179. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  180. gfp_t flags)
  181. {
  182. int part_nr = 0;
  183. struct flex_array_part *part;
  184. void *dst;
  185. if (element_nr >= fa->total_nr_elements)
  186. return -ENOSPC;
  187. if (!fa->element_size)
  188. return 0;
  189. if (elements_fit_in_base(fa))
  190. part = (struct flex_array_part *)&fa->parts[0];
  191. else {
  192. part_nr = fa_element_to_part_nr(fa, element_nr);
  193. part = __fa_get_part(fa, part_nr, flags);
  194. if (!part)
  195. return -ENOMEM;
  196. }
  197. dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
  198. memcpy(dst, src, fa->element_size);
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(flex_array_put);
  202. /**
  203. * flex_array_clear - clear element in array at @element_nr
  204. * @fa: the flex array of the element.
  205. * @element_nr: index of the position to clear.
  206. *
  207. * Locking must be provided by the caller.
  208. */
  209. int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
  210. {
  211. int part_nr = 0;
  212. struct flex_array_part *part;
  213. void *dst;
  214. if (element_nr >= fa->total_nr_elements)
  215. return -ENOSPC;
  216. if (!fa->element_size)
  217. return 0;
  218. if (elements_fit_in_base(fa))
  219. part = (struct flex_array_part *)&fa->parts[0];
  220. else {
  221. part_nr = fa_element_to_part_nr(fa, element_nr);
  222. part = fa->parts[part_nr];
  223. if (!part)
  224. return -EINVAL;
  225. }
  226. dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
  227. memset(dst, FLEX_ARRAY_FREE, fa->element_size);
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(flex_array_clear);
  231. /**
  232. * flex_array_prealloc - guarantee that array space exists
  233. * @fa: the flex array for which to preallocate parts
  234. * @start: index of first array element for which space is allocated
  235. * @nr_elements: number of elements for which space is allocated
  236. * @flags: page allocation flags
  237. *
  238. * This will guarantee that no future calls to flex_array_put()
  239. * will allocate memory. It can be used if you are expecting to
  240. * be holding a lock or in some atomic context while writing
  241. * data into the array.
  242. *
  243. * Locking must be provided by the caller.
  244. */
  245. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  246. unsigned int nr_elements, gfp_t flags)
  247. {
  248. int start_part;
  249. int end_part;
  250. int part_nr;
  251. unsigned int end;
  252. struct flex_array_part *part;
  253. if (!start && !nr_elements)
  254. return 0;
  255. if (start >= fa->total_nr_elements)
  256. return -ENOSPC;
  257. if (!nr_elements)
  258. return 0;
  259. end = start + nr_elements - 1;
  260. if (end >= fa->total_nr_elements)
  261. return -ENOSPC;
  262. if (!fa->element_size)
  263. return 0;
  264. if (elements_fit_in_base(fa))
  265. return 0;
  266. start_part = fa_element_to_part_nr(fa, start);
  267. end_part = fa_element_to_part_nr(fa, end);
  268. for (part_nr = start_part; part_nr <= end_part; part_nr++) {
  269. part = __fa_get_part(fa, part_nr, flags);
  270. if (!part)
  271. return -ENOMEM;
  272. }
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(flex_array_prealloc);
  276. /**
  277. * flex_array_get - pull data back out of the array
  278. * @fa: the flex array from which to extract data
  279. * @element_nr: index of the element to fetch from the array
  280. *
  281. * Returns a pointer to the data at index @element_nr. Note
  282. * that this is a copy of the data that was passed in. If you
  283. * are using this to store pointers, you'll get back &ptr. You
  284. * may instead wish to use the flex_array_get_ptr helper.
  285. *
  286. * Locking must be provided by the caller.
  287. */
  288. void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
  289. {
  290. int part_nr = 0;
  291. struct flex_array_part *part;
  292. if (!fa->element_size)
  293. return NULL;
  294. if (element_nr >= fa->total_nr_elements)
  295. return NULL;
  296. if (elements_fit_in_base(fa))
  297. part = (struct flex_array_part *)&fa->parts[0];
  298. else {
  299. part_nr = fa_element_to_part_nr(fa, element_nr);
  300. part = fa->parts[part_nr];
  301. if (!part)
  302. return NULL;
  303. }
  304. return &part->elements[index_inside_part(fa, element_nr, part_nr)];
  305. }
  306. EXPORT_SYMBOL(flex_array_get);
  307. /**
  308. * flex_array_get_ptr - pull a ptr back out of the array
  309. * @fa: the flex array from which to extract data
  310. * @element_nr: index of the element to fetch from the array
  311. *
  312. * Returns the pointer placed in the flex array at element_nr using
  313. * flex_array_put_ptr(). This function should not be called if the
  314. * element in question was not set using the _put_ptr() helper.
  315. */
  316. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
  317. {
  318. void **tmp;
  319. tmp = flex_array_get(fa, element_nr);
  320. if (!tmp)
  321. return NULL;
  322. return *tmp;
  323. }
  324. EXPORT_SYMBOL(flex_array_get_ptr);
  325. static int part_is_free(struct flex_array_part *part)
  326. {
  327. int i;
  328. for (i = 0; i < sizeof(struct flex_array_part); i++)
  329. if (part->elements[i] != FLEX_ARRAY_FREE)
  330. return 0;
  331. return 1;
  332. }
  333. /**
  334. * flex_array_shrink - free unused second-level pages
  335. * @fa: the flex array to shrink
  336. *
  337. * Frees all second-level pages that consist solely of unused
  338. * elements. Returns the number of pages freed.
  339. *
  340. * Locking must be provided by the caller.
  341. */
  342. int flex_array_shrink(struct flex_array *fa)
  343. {
  344. struct flex_array_part *part;
  345. int part_nr;
  346. int ret = 0;
  347. if (!fa->total_nr_elements || !fa->element_size)
  348. return 0;
  349. if (elements_fit_in_base(fa))
  350. return ret;
  351. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
  352. part = fa->parts[part_nr];
  353. if (!part)
  354. continue;
  355. if (part_is_free(part)) {
  356. fa->parts[part_nr] = NULL;
  357. kfree(part);
  358. ret++;
  359. }
  360. }
  361. return ret;
  362. }
  363. EXPORT_SYMBOL(flex_array_shrink);