flex_array.c 9.1 KB

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