flex_array.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 = FLEX_ARRAY_NR_BASE_PTRS *
  88. FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
  89. /* max_size will end up 0 if element_size > PAGE_SIZE */
  90. if (total > max_size)
  91. return NULL;
  92. ret = kzalloc(sizeof(struct flex_array), flags);
  93. if (!ret)
  94. return NULL;
  95. ret->element_size = element_size;
  96. ret->total_nr_elements = total;
  97. if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
  98. memset(&ret->parts[0], FLEX_ARRAY_FREE,
  99. FLEX_ARRAY_BASE_BYTES_LEFT);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(flex_array_alloc);
  103. static int fa_element_to_part_nr(struct flex_array *fa,
  104. unsigned int element_nr)
  105. {
  106. return element_nr / FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size);
  107. }
  108. /**
  109. * flex_array_free_parts - just free the second-level pages
  110. * @fa: the flex array from which to free parts
  111. *
  112. * This is to be used in cases where the base 'struct flex_array'
  113. * has been statically allocated and should not be free.
  114. */
  115. void flex_array_free_parts(struct flex_array *fa)
  116. {
  117. int part_nr;
  118. if (elements_fit_in_base(fa))
  119. return;
  120. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
  121. kfree(fa->parts[part_nr]);
  122. }
  123. EXPORT_SYMBOL(flex_array_free_parts);
  124. void flex_array_free(struct flex_array *fa)
  125. {
  126. flex_array_free_parts(fa);
  127. kfree(fa);
  128. }
  129. EXPORT_SYMBOL(flex_array_free);
  130. static unsigned int index_inside_part(struct flex_array *fa,
  131. unsigned int element_nr)
  132. {
  133. unsigned int part_offset;
  134. part_offset = element_nr %
  135. FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size);
  136. return part_offset * fa->element_size;
  137. }
  138. static struct flex_array_part *
  139. __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
  140. {
  141. struct flex_array_part *part = fa->parts[part_nr];
  142. if (!part) {
  143. part = kmalloc(sizeof(struct flex_array_part), flags);
  144. if (!part)
  145. return NULL;
  146. if (!(flags & __GFP_ZERO))
  147. memset(part, FLEX_ARRAY_FREE,
  148. sizeof(struct flex_array_part));
  149. fa->parts[part_nr] = part;
  150. }
  151. return part;
  152. }
  153. /**
  154. * flex_array_put - copy data into the array at @element_nr
  155. * @fa: the flex array to copy data into
  156. * @element_nr: index of the position in which to insert
  157. * the new element.
  158. * @src: address of data to copy into the array
  159. * @flags: page allocation flags to use for array expansion
  160. *
  161. *
  162. * Note that this *copies* the contents of @src into
  163. * the array. If you are trying to store an array of
  164. * pointers, make sure to pass in &ptr instead of ptr.
  165. * You may instead wish to use the flex_array_put_ptr()
  166. * helper function.
  167. *
  168. * Locking must be provided by the caller.
  169. */
  170. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  171. gfp_t flags)
  172. {
  173. int part_nr = fa_element_to_part_nr(fa, element_nr);
  174. struct flex_array_part *part;
  175. void *dst;
  176. if (element_nr >= fa->total_nr_elements)
  177. return -ENOSPC;
  178. if (elements_fit_in_base(fa))
  179. part = (struct flex_array_part *)&fa->parts[0];
  180. else {
  181. part = __fa_get_part(fa, part_nr, flags);
  182. if (!part)
  183. return -ENOMEM;
  184. }
  185. dst = &part->elements[index_inside_part(fa, element_nr)];
  186. memcpy(dst, src, fa->element_size);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(flex_array_put);
  190. /**
  191. * flex_array_clear - clear element in array at @element_nr
  192. * @fa: the flex array of the element.
  193. * @element_nr: index of the position to clear.
  194. *
  195. * Locking must be provided by the caller.
  196. */
  197. int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
  198. {
  199. int part_nr = fa_element_to_part_nr(fa, element_nr);
  200. struct flex_array_part *part;
  201. void *dst;
  202. if (element_nr >= fa->total_nr_elements)
  203. return -ENOSPC;
  204. if (elements_fit_in_base(fa))
  205. part = (struct flex_array_part *)&fa->parts[0];
  206. else {
  207. part = fa->parts[part_nr];
  208. if (!part)
  209. return -EINVAL;
  210. }
  211. dst = &part->elements[index_inside_part(fa, element_nr)];
  212. memset(dst, FLEX_ARRAY_FREE, fa->element_size);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(flex_array_clear);
  216. /**
  217. * flex_array_prealloc - guarantee that array space exists
  218. * @fa: the flex array for which to preallocate parts
  219. * @start: index of first array element for which space is allocated
  220. * @nr_elements: number of elements for which space is allocated
  221. * @flags: page allocation flags
  222. *
  223. * This will guarantee that no future calls to flex_array_put()
  224. * will allocate memory. It can be used if you are expecting to
  225. * be holding a lock or in some atomic context while writing
  226. * data into the array.
  227. *
  228. * Locking must be provided by the caller.
  229. */
  230. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  231. unsigned int nr_elements, gfp_t flags)
  232. {
  233. int start_part;
  234. int end_part;
  235. int part_nr;
  236. unsigned int end;
  237. struct flex_array_part *part;
  238. end = start + nr_elements - 1;
  239. if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
  240. return -ENOSPC;
  241. if (elements_fit_in_base(fa))
  242. return 0;
  243. start_part = fa_element_to_part_nr(fa, start);
  244. end_part = fa_element_to_part_nr(fa, end);
  245. for (part_nr = start_part; part_nr <= end_part; part_nr++) {
  246. part = __fa_get_part(fa, part_nr, flags);
  247. if (!part)
  248. return -ENOMEM;
  249. }
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(flex_array_prealloc);
  253. /**
  254. * flex_array_get - pull data back out of the array
  255. * @fa: the flex array from which to extract data
  256. * @element_nr: index of the element to fetch from the array
  257. *
  258. * Returns a pointer to the data at index @element_nr. Note
  259. * that this is a copy of the data that was passed in. If you
  260. * are using this to store pointers, you'll get back &ptr. You
  261. * may instead wish to use the flex_array_get_ptr helper.
  262. *
  263. * Locking must be provided by the caller.
  264. */
  265. void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
  266. {
  267. int part_nr = fa_element_to_part_nr(fa, element_nr);
  268. struct flex_array_part *part;
  269. if (element_nr >= fa->total_nr_elements)
  270. return NULL;
  271. if (elements_fit_in_base(fa))
  272. part = (struct flex_array_part *)&fa->parts[0];
  273. else {
  274. part = fa->parts[part_nr];
  275. if (!part)
  276. return NULL;
  277. }
  278. return &part->elements[index_inside_part(fa, element_nr)];
  279. }
  280. EXPORT_SYMBOL(flex_array_get);
  281. /**
  282. * flex_array_get_ptr - pull a ptr back out of the array
  283. * @fa: the flex array from which to extract data
  284. * @element_nr: index of the element to fetch from the array
  285. *
  286. * Returns the pointer placed in the flex array at element_nr using
  287. * flex_array_put_ptr(). This function should not be called if the
  288. * element in question was not set using the _put_ptr() helper.
  289. */
  290. void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
  291. {
  292. void **tmp;
  293. tmp = flex_array_get(fa, element_nr);
  294. if (!tmp)
  295. return NULL;
  296. return *tmp;
  297. }
  298. EXPORT_SYMBOL(flex_array_get_ptr);
  299. static int part_is_free(struct flex_array_part *part)
  300. {
  301. int i;
  302. for (i = 0; i < sizeof(struct flex_array_part); i++)
  303. if (part->elements[i] != FLEX_ARRAY_FREE)
  304. return 0;
  305. return 1;
  306. }
  307. /**
  308. * flex_array_shrink - free unused second-level pages
  309. * @fa: the flex array to shrink
  310. *
  311. * Frees all second-level pages that consist solely of unused
  312. * elements. Returns the number of pages freed.
  313. *
  314. * Locking must be provided by the caller.
  315. */
  316. int flex_array_shrink(struct flex_array *fa)
  317. {
  318. struct flex_array_part *part;
  319. int part_nr;
  320. int ret = 0;
  321. if (elements_fit_in_base(fa))
  322. return ret;
  323. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
  324. part = fa->parts[part_nr];
  325. if (!part)
  326. continue;
  327. if (part_is_free(part)) {
  328. fa->parts[part_nr] = NULL;
  329. kfree(part);
  330. ret++;
  331. }
  332. }
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(flex_array_shrink);