flex_array.c 9.8 KB

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