flex_array.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, int total, gfp_t flags)
  96. {
  97. struct flex_array *ret;
  98. int max_size = nr_base_part_ptrs() * __elements_per_part(element_size);
  99. /* max_size will end up 0 if element_size > PAGE_SIZE */
  100. if (total > max_size)
  101. return NULL;
  102. ret = kzalloc(sizeof(struct flex_array), flags);
  103. if (!ret)
  104. return NULL;
  105. ret->element_size = element_size;
  106. ret->total_nr_elements = total;
  107. return ret;
  108. }
  109. static int fa_element_to_part_nr(struct flex_array *fa, int element_nr)
  110. {
  111. return element_nr / __elements_per_part(fa->element_size);
  112. }
  113. /**
  114. * flex_array_free_parts - just free the second-level pages
  115. * @src: address of data to copy into the array
  116. * @element_nr: index of the position in which to insert
  117. * the new element.
  118. *
  119. * This is to be used in cases where the base 'struct flex_array'
  120. * has been statically allocated and should not be free.
  121. */
  122. void flex_array_free_parts(struct flex_array *fa)
  123. {
  124. int part_nr;
  125. int max_part = nr_base_part_ptrs();
  126. if (elements_fit_in_base(fa))
  127. return;
  128. for (part_nr = 0; part_nr < max_part; part_nr++)
  129. kfree(fa->parts[part_nr]);
  130. }
  131. void flex_array_free(struct flex_array *fa)
  132. {
  133. flex_array_free_parts(fa);
  134. kfree(fa);
  135. }
  136. static int fa_index_inside_part(struct flex_array *fa, int element_nr)
  137. {
  138. return element_nr % __elements_per_part(fa->element_size);
  139. }
  140. static int index_inside_part(struct flex_array *fa, int element_nr)
  141. {
  142. int part_offset = fa_index_inside_part(fa, element_nr);
  143. return part_offset * fa->element_size;
  144. }
  145. static struct flex_array_part *
  146. __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
  147. {
  148. struct flex_array_part *part = fa->parts[part_nr];
  149. if (!part) {
  150. /*
  151. * This leaves the part pages uninitialized
  152. * and with potentially random data, just
  153. * as if the user had kmalloc()'d the whole.
  154. * __GFP_ZERO can be used to zero it.
  155. */
  156. part = kmalloc(FLEX_ARRAY_PART_SIZE, flags);
  157. if (!part)
  158. return NULL;
  159. fa->parts[part_nr] = part;
  160. }
  161. return part;
  162. }
  163. /**
  164. * flex_array_put - copy data into the array at @element_nr
  165. * @src: address of data to copy into the array
  166. * @element_nr: index of the position in which to insert
  167. * the new element.
  168. *
  169. * Note that this *copies* the contents of @src into
  170. * the array. If you are trying to store an array of
  171. * pointers, make sure to pass in &ptr instead of ptr.
  172. *
  173. * Locking must be provided by the caller.
  174. */
  175. int flex_array_put(struct flex_array *fa, int element_nr, void *src, gfp_t flags)
  176. {
  177. int part_nr = fa_element_to_part_nr(fa, element_nr);
  178. struct flex_array_part *part;
  179. void *dst;
  180. if (element_nr >= fa->total_nr_elements)
  181. return -ENOSPC;
  182. if (elements_fit_in_base(fa))
  183. part = (struct flex_array_part *)&fa->parts[0];
  184. else
  185. part = __fa_get_part(fa, part_nr, flags);
  186. if (!part)
  187. return -ENOMEM;
  188. dst = &part->elements[index_inside_part(fa, element_nr)];
  189. memcpy(dst, src, fa->element_size);
  190. return 0;
  191. }
  192. /**
  193. * flex_array_prealloc - guarantee that array space exists
  194. * @start: index of first array element for which space is allocated
  195. * @end: index of last (inclusive) element for which space is allocated
  196. *
  197. * This will guarantee that no future calls to flex_array_put()
  198. * will allocate memory. It can be used if you are expecting to
  199. * be holding a lock or in some atomic context while writing
  200. * data into the array.
  201. *
  202. * Locking must be provided by the caller.
  203. */
  204. int flex_array_prealloc(struct flex_array *fa, int start, int end, gfp_t flags)
  205. {
  206. int start_part;
  207. int end_part;
  208. int part_nr;
  209. struct flex_array_part *part;
  210. if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
  211. return -ENOSPC;
  212. if (elements_fit_in_base(fa))
  213. return 0;
  214. start_part = fa_element_to_part_nr(fa, start);
  215. end_part = fa_element_to_part_nr(fa, end);
  216. for (part_nr = start_part; part_nr <= end_part; part_nr++) {
  217. part = __fa_get_part(fa, part_nr, flags);
  218. if (!part)
  219. return -ENOMEM;
  220. }
  221. return 0;
  222. }
  223. /**
  224. * flex_array_get - pull data back out of the array
  225. * @element_nr: index of the element to fetch from the array
  226. *
  227. * Returns a pointer to the data at index @element_nr. Note
  228. * that this is a copy of the data that was passed in. If you
  229. * are using this to store pointers, you'll get back &ptr.
  230. *
  231. * Locking must be provided by the caller.
  232. */
  233. void *flex_array_get(struct flex_array *fa, int element_nr)
  234. {
  235. int part_nr = fa_element_to_part_nr(fa, element_nr);
  236. struct flex_array_part *part;
  237. int index;
  238. if (element_nr >= fa->total_nr_elements)
  239. return NULL;
  240. if (!fa->parts[part_nr])
  241. return NULL;
  242. if (elements_fit_in_base(fa))
  243. part = (struct flex_array_part *)&fa->parts[0];
  244. else
  245. part = fa->parts[part_nr];
  246. index = index_inside_part(fa, element_nr);
  247. return &part->elements[index_inside_part(fa, element_nr)];
  248. }