flex_array.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. *
  162. * Locking must be provided by the caller.
  163. */
  164. int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
  165. gfp_t flags)
  166. {
  167. int part_nr = fa_element_to_part_nr(fa, element_nr);
  168. struct flex_array_part *part;
  169. void *dst;
  170. if (element_nr >= fa->total_nr_elements)
  171. return -ENOSPC;
  172. if (elements_fit_in_base(fa))
  173. part = (struct flex_array_part *)&fa->parts[0];
  174. else {
  175. part = __fa_get_part(fa, part_nr, flags);
  176. if (!part)
  177. return -ENOMEM;
  178. }
  179. dst = &part->elements[index_inside_part(fa, element_nr)];
  180. memcpy(dst, src, fa->element_size);
  181. return 0;
  182. }
  183. /**
  184. * flex_array_clear - clear element in array at @element_nr
  185. * @fa: the flex array of the element.
  186. * @element_nr: index of the position to clear.
  187. *
  188. * Locking must be provided by the caller.
  189. */
  190. int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
  191. {
  192. int part_nr = fa_element_to_part_nr(fa, element_nr);
  193. struct flex_array_part *part;
  194. void *dst;
  195. if (element_nr >= fa->total_nr_elements)
  196. return -ENOSPC;
  197. if (elements_fit_in_base(fa))
  198. part = (struct flex_array_part *)&fa->parts[0];
  199. else {
  200. part = fa->parts[part_nr];
  201. if (!part)
  202. return -EINVAL;
  203. }
  204. dst = &part->elements[index_inside_part(fa, element_nr)];
  205. memset(dst, FLEX_ARRAY_FREE, fa->element_size);
  206. return 0;
  207. }
  208. /**
  209. * flex_array_prealloc - guarantee that array space exists
  210. * @fa: the flex array for which to preallocate parts
  211. * @start: index of first array element for which space is allocated
  212. * @end: index of last (inclusive) element for which space is allocated
  213. * @flags: page allocation flags
  214. *
  215. * This will guarantee that no future calls to flex_array_put()
  216. * will allocate memory. It can be used if you are expecting to
  217. * be holding a lock or in some atomic context while writing
  218. * data into the array.
  219. *
  220. * Locking must be provided by the caller.
  221. */
  222. int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  223. unsigned int end, gfp_t flags)
  224. {
  225. int start_part;
  226. int end_part;
  227. int part_nr;
  228. struct flex_array_part *part;
  229. if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
  230. return -ENOSPC;
  231. if (elements_fit_in_base(fa))
  232. return 0;
  233. start_part = fa_element_to_part_nr(fa, start);
  234. end_part = fa_element_to_part_nr(fa, end);
  235. for (part_nr = start_part; part_nr <= end_part; part_nr++) {
  236. part = __fa_get_part(fa, part_nr, flags);
  237. if (!part)
  238. return -ENOMEM;
  239. }
  240. return 0;
  241. }
  242. /**
  243. * flex_array_get - pull data back out of the array
  244. * @fa: the flex array from which to extract data
  245. * @element_nr: index of the element to fetch from the array
  246. *
  247. * Returns a pointer to the data at index @element_nr. Note
  248. * that this is a copy of the data that was passed in. If you
  249. * are using this to store pointers, you'll get back &ptr.
  250. *
  251. * Locking must be provided by the caller.
  252. */
  253. void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
  254. {
  255. int part_nr = fa_element_to_part_nr(fa, element_nr);
  256. struct flex_array_part *part;
  257. if (element_nr >= fa->total_nr_elements)
  258. return NULL;
  259. if (elements_fit_in_base(fa))
  260. part = (struct flex_array_part *)&fa->parts[0];
  261. else {
  262. part = fa->parts[part_nr];
  263. if (!part)
  264. return NULL;
  265. }
  266. return &part->elements[index_inside_part(fa, element_nr)];
  267. }
  268. static int part_is_free(struct flex_array_part *part)
  269. {
  270. int i;
  271. for (i = 0; i < sizeof(struct flex_array_part); i++)
  272. if (part->elements[i] != FLEX_ARRAY_FREE)
  273. return 0;
  274. return 1;
  275. }
  276. /**
  277. * flex_array_shrink - free unused second-level pages
  278. * @fa: the flex array to shrink
  279. *
  280. * Frees all second-level pages that consist solely of unused
  281. * elements. Returns the number of pages freed.
  282. *
  283. * Locking must be provided by the caller.
  284. */
  285. int flex_array_shrink(struct flex_array *fa)
  286. {
  287. struct flex_array_part *part;
  288. int part_nr;
  289. int ret = 0;
  290. if (elements_fit_in_base(fa))
  291. return ret;
  292. for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
  293. part = fa->parts[part_nr];
  294. if (!part)
  295. continue;
  296. if (part_is_free(part)) {
  297. fa->parts[part_nr] = NULL;
  298. kfree(part);
  299. ret++;
  300. }
  301. }
  302. return ret;
  303. }