flex_array.c 11 KB

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