flex_array.c 7.6 KB

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