1 #include <linux/version.h>
3 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
6 * Flexible array managed in PAGE_SIZE parts
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Copyright IBM Corporation, 2009
24 * Author: Dave Hansen <dave@linux.vnet.ibm.com>
27 #include <linux/flex_array.h>
28 #include <linux/slab.h>
29 #include <linux/stddef.h>
30 #include <linux/module.h>
31 #include <linux/reciprocal_div.h>
33 struct flex_array_part {
34 char elements[FLEX_ARRAY_PART_SIZE];
38 * If a user requests an allocation which is small
39 * enough, we may simply use the space in the
40 * flex_array->parts[] array to store the user
43 static inline int elements_fit_in_base(struct flex_array *fa)
45 int data_size = fa->element_size * fa->total_nr_elements;
46 if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
52 * flex_array_alloc - allocate a new flexible array
53 * @element_size: the size of individual elements in the array
54 * @total: total number of elements that this should hold
55 * @flags: page allocation flags to use for base array
57 * Note: all locking must be provided by the caller.
59 * @total is used to size internal structures. If the user ever
60 * accesses any array indexes >=@total, it will produce errors.
62 * The maximum number of elements is defined as: the number of
63 * elements that can be stored in a page times the number of
64 * page pointers that we can fit in the base structure or (using
67 * (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
69 * Here's a table showing example capacities. Note that the maximum
70 * index that the get/put() functions is just nr_objects-1. This
71 * basically means that you get 4MB of storage on 32-bit and 2MB on
75 * Element size | Objects | Objects |
76 * PAGE_SIZE=4k | 32-bit | 64-bit |
77 * ---------------------------------|
78 * 1 bytes | 4177920 | 2088960 |
79 * 2 bytes | 2088960 | 1044480 |
80 * 3 bytes | 1392300 | 696150 |
81 * 4 bytes | 1044480 | 522240 |
82 * 32 bytes | 130560 | 65408 |
83 * 33 bytes | 126480 | 63240 |
84 * 2048 bytes | 2040 | 1020 |
85 * 2049 bytes | 1020 | 510 |
86 * void * | 1044480 | 261120 |
88 * Since 64-bit pointers are twice the size, we lose half the
89 * capacity in the base structure. Also note that no effort is made
90 * to efficiently pack objects across page boundaries.
92 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
95 struct flex_array *ret;
96 int elems_per_part = 0;
97 int reciprocal_elems = 0;
101 elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
102 reciprocal_elems = reciprocal_value(elems_per_part);
103 max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part;
106 /* max_size will end up 0 if element_size > PAGE_SIZE */
107 if (total > max_size)
109 ret = kzalloc(sizeof(struct flex_array), flags);
112 ret->element_size = element_size;
113 ret->total_nr_elements = total;
114 ret->elems_per_part = elems_per_part;
115 ret->reciprocal_elems = reciprocal_elems;
116 if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
117 memset(&ret->parts[0], FLEX_ARRAY_FREE,
118 FLEX_ARRAY_BASE_BYTES_LEFT);
122 static int fa_element_to_part_nr(struct flex_array *fa,
123 unsigned int element_nr)
125 return reciprocal_divide(element_nr, fa->reciprocal_elems);
129 * flex_array_free_parts - just free the second-level pages
130 * @fa: the flex array from which to free parts
132 * This is to be used in cases where the base 'struct flex_array'
133 * has been statically allocated and should not be free.
135 void flex_array_free_parts(struct flex_array *fa)
139 if (elements_fit_in_base(fa))
141 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
142 kfree(fa->parts[part_nr]);
145 void flex_array_free(struct flex_array *fa)
147 flex_array_free_parts(fa);
151 static unsigned int index_inside_part(struct flex_array *fa,
152 unsigned int element_nr,
153 unsigned int part_nr)
155 unsigned int part_offset;
157 part_offset = element_nr - part_nr * fa->elems_per_part;
158 return part_offset * fa->element_size;
161 static struct flex_array_part *
162 __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
164 struct flex_array_part *part = fa->parts[part_nr];
166 part = kmalloc(sizeof(struct flex_array_part), flags);
169 if (!(flags & __GFP_ZERO))
170 memset(part, FLEX_ARRAY_FREE,
171 sizeof(struct flex_array_part));
172 fa->parts[part_nr] = part;
178 * flex_array_put - copy data into the array at @element_nr
179 * @fa: the flex array to copy data into
180 * @element_nr: index of the position in which to insert
182 * @src: address of data to copy into the array
183 * @flags: page allocation flags to use for array expansion
186 * Note that this *copies* the contents of @src into
187 * the array. If you are trying to store an array of
188 * pointers, make sure to pass in &ptr instead of ptr.
189 * You may instead wish to use the flex_array_put_ptr()
192 * Locking must be provided by the caller.
194 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
198 struct flex_array_part *part;
201 if (element_nr >= fa->total_nr_elements)
203 if (!fa->element_size)
205 if (elements_fit_in_base(fa))
206 part = (struct flex_array_part *)&fa->parts[0];
208 part_nr = fa_element_to_part_nr(fa, element_nr);
209 part = __fa_get_part(fa, part_nr, flags);
213 dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
214 memcpy(dst, src, fa->element_size);
219 * flex_array_clear - clear element in array at @element_nr
220 * @fa: the flex array of the element.
221 * @element_nr: index of the position to clear.
223 * Locking must be provided by the caller.
225 int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
228 struct flex_array_part *part;
231 if (element_nr >= fa->total_nr_elements)
233 if (!fa->element_size)
235 if (elements_fit_in_base(fa))
236 part = (struct flex_array_part *)&fa->parts[0];
238 part_nr = fa_element_to_part_nr(fa, element_nr);
239 part = fa->parts[part_nr];
243 dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
244 memset(dst, FLEX_ARRAY_FREE, fa->element_size);
249 * flex_array_prealloc - guarantee that array space exists
250 * @fa: the flex array for which to preallocate parts
251 * @start: index of first array element for which space is allocated
252 * @nr_elements: number of elements for which space is allocated
253 * @flags: page allocation flags
255 * This will guarantee that no future calls to flex_array_put()
256 * will allocate memory. It can be used if you are expecting to
257 * be holding a lock or in some atomic context while writing
258 * data into the array.
260 * Locking must be provided by the caller.
262 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
263 unsigned int nr_elements, gfp_t flags)
269 struct flex_array_part *part;
271 if (!start && !nr_elements)
273 if (start >= fa->total_nr_elements)
278 end = start + nr_elements - 1;
280 if (end >= fa->total_nr_elements)
282 if (!fa->element_size)
284 if (elements_fit_in_base(fa))
286 start_part = fa_element_to_part_nr(fa, start);
287 end_part = fa_element_to_part_nr(fa, end);
288 for (part_nr = start_part; part_nr <= end_part; part_nr++) {
289 part = __fa_get_part(fa, part_nr, flags);
297 * flex_array_get - pull data back out of the array
298 * @fa: the flex array from which to extract data
299 * @element_nr: index of the element to fetch from the array
301 * Returns a pointer to the data at index @element_nr. Note
302 * that this is a copy of the data that was passed in. If you
303 * are using this to store pointers, you'll get back &ptr. You
304 * may instead wish to use the flex_array_get_ptr helper.
306 * Locking must be provided by the caller.
308 void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
311 struct flex_array_part *part;
313 if (!fa->element_size)
315 if (element_nr >= fa->total_nr_elements)
317 if (elements_fit_in_base(fa))
318 part = (struct flex_array_part *)&fa->parts[0];
320 part_nr = fa_element_to_part_nr(fa, element_nr);
321 part = fa->parts[part_nr];
325 return &part->elements[index_inside_part(fa, element_nr, part_nr)];
329 * flex_array_get_ptr - pull a ptr back out of the array
330 * @fa: the flex array from which to extract data
331 * @element_nr: index of the element to fetch from the array
333 * Returns the pointer placed in the flex array at element_nr using
334 * flex_array_put_ptr(). This function should not be called if the
335 * element in question was not set using the _put_ptr() helper.
337 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
341 tmp = flex_array_get(fa, element_nr);
348 static int part_is_free(struct flex_array_part *part)
352 for (i = 0; i < sizeof(struct flex_array_part); i++)
353 if (part->elements[i] != FLEX_ARRAY_FREE)
359 * flex_array_shrink - free unused second-level pages
360 * @fa: the flex array to shrink
362 * Frees all second-level pages that consist solely of unused
363 * elements. Returns the number of pages freed.
365 * Locking must be provided by the caller.
367 int flex_array_shrink(struct flex_array *fa)
369 struct flex_array_part *part;
373 if (!fa->total_nr_elements || !fa->element_size)
375 if (elements_fit_in_base(fa))
377 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
378 part = fa->parts[part_nr];
381 if (part_is_free(part)) {
382 fa->parts[part_nr] = NULL;
390 #endif /* Linux version < 3.0.0 */