flex_array.c 8.8 KB

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