2 * Flexible array managed in PAGE_SIZE parts
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.
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.
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.
18 * Copyright IBM Corporation, 2009
20 * Author: Dave Hansen <dave@linux.vnet.ibm.com>
23 #include <linux/flex_array.h>
24 #include <linux/slab.h>
25 #include <linux/stddef.h>
27 struct flex_array_part
{
28 char elements
[FLEX_ARRAY_PART_SIZE
];
31 static inline int __elements_per_part(int element_size
)
33 return FLEX_ARRAY_PART_SIZE
/ element_size
;
36 static inline int bytes_left_in_base(void)
38 int element_offset
= offsetof(struct flex_array
, parts
);
39 int bytes_left
= FLEX_ARRAY_BASE_SIZE
- element_offset
;
43 static inline int nr_base_part_ptrs(void)
45 return bytes_left_in_base() / sizeof(struct flex_array_part
*);
49 * If a user requests an allocation which is small
50 * enough, we may simply use the space in the
51 * flex_array->parts[] array to store the user
54 static inline int elements_fit_in_base(struct flex_array
*fa
)
56 int data_size
= fa
->element_size
* fa
->total_nr_elements
;
57 if (data_size
<= bytes_left_in_base())
63 * flex_array_alloc - allocate a new flexible array
64 * @element_size: the size of individual elements in the array
65 * @total: total number of elements that this should hold
67 * Note: all locking must be provided by the caller.
69 * @total is used to size internal structures. If the user ever
70 * accesses any array indexes >=@total, it will produce errors.
72 * The maximum number of elements is defined as: the number of
73 * elements that can be stored in a page times the number of
74 * page pointers that we can fit in the base structure or (using
77 * (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
79 * Here's a table showing example capacities. Note that the maximum
80 * index that the get/put() functions is just nr_objects-1. This
81 * basically means that you get 4MB of storage on 32-bit and 2MB on
85 * Element size | Objects | Objects |
86 * PAGE_SIZE=4k | 32-bit | 64-bit |
87 * ---------------------------------|
88 * 1 bytes | 4186112 | 2093056 |
89 * 2 bytes | 2093056 | 1046528 |
90 * 3 bytes | 1395030 | 697515 |
91 * 4 bytes | 1046528 | 523264 |
92 * 32 bytes | 130816 | 65408 |
93 * 33 bytes | 126728 | 63364 |
94 * 2048 bytes | 2044 | 1022 |
95 * 2049 bytes | 1022 | 511 |
96 * void * | 1046528 | 261632 |
98 * Since 64-bit pointers are twice the size, we lose half the
99 * capacity in the base structure. Also note that no effort is made
100 * to efficiently pack objects across page boundaries.
102 struct flex_array
*flex_array_alloc(int element_size
, unsigned int total
,
105 struct flex_array
*ret
;
106 int max_size
= nr_base_part_ptrs() * __elements_per_part(element_size
);
108 /* max_size will end up 0 if element_size > PAGE_SIZE */
109 if (total
> max_size
)
111 ret
= kzalloc(sizeof(struct flex_array
), flags
);
114 ret
->element_size
= element_size
;
115 ret
->total_nr_elements
= total
;
116 if (elements_fit_in_base(ret
) && !(flags
& __GFP_ZERO
))
117 memset(ret
->parts
[0], FLEX_ARRAY_FREE
, bytes_left_in_base());
121 static int fa_element_to_part_nr(struct flex_array
*fa
,
122 unsigned int element_nr
)
124 return element_nr
/ __elements_per_part(fa
->element_size
);
128 * flex_array_free_parts - just free the second-level pages
130 * This is to be used in cases where the base 'struct flex_array'
131 * has been statically allocated and should not be free.
133 void flex_array_free_parts(struct flex_array
*fa
)
136 int max_part
= nr_base_part_ptrs();
138 if (elements_fit_in_base(fa
))
140 for (part_nr
= 0; part_nr
< max_part
; part_nr
++)
141 kfree(fa
->parts
[part_nr
]);
144 void flex_array_free(struct flex_array
*fa
)
146 flex_array_free_parts(fa
);
150 static unsigned int index_inside_part(struct flex_array
*fa
,
151 unsigned int element_nr
)
153 unsigned int part_offset
;
155 part_offset
= element_nr
% __elements_per_part(fa
->element_size
);
156 return part_offset
* fa
->element_size
;
159 static struct flex_array_part
*
160 __fa_get_part(struct flex_array
*fa
, int part_nr
, gfp_t flags
)
162 struct flex_array_part
*part
= fa
->parts
[part_nr
];
164 part
= kmalloc(sizeof(struct flex_array_part
), flags
);
167 if (!(flags
& __GFP_ZERO
))
168 memset(part
, FLEX_ARRAY_FREE
,
169 sizeof(struct flex_array_part
));
170 fa
->parts
[part_nr
] = part
;
176 * flex_array_put - copy data into the array at @element_nr
177 * @src: address of data to copy into the array
178 * @element_nr: index of the position in which to insert
181 * Note that this *copies* the contents of @src into
182 * the array. If you are trying to store an array of
183 * pointers, make sure to pass in &ptr instead of ptr.
185 * Locking must be provided by the caller.
187 int flex_array_put(struct flex_array
*fa
, unsigned int element_nr
, void *src
,
190 int part_nr
= fa_element_to_part_nr(fa
, element_nr
);
191 struct flex_array_part
*part
;
194 if (element_nr
>= fa
->total_nr_elements
)
196 if (elements_fit_in_base(fa
))
197 part
= (struct flex_array_part
*)&fa
->parts
[0];
199 part
= __fa_get_part(fa
, part_nr
, flags
);
203 dst
= &part
->elements
[index_inside_part(fa
, element_nr
)];
204 memcpy(dst
, src
, fa
->element_size
);
209 * flex_array_clear - clear element in array at @element_nr
210 * @element_nr: index of the position to clear.
212 * Locking must be provided by the caller.
214 int flex_array_clear(struct flex_array
*fa
, unsigned int element_nr
)
216 int part_nr
= fa_element_to_part_nr(fa
, element_nr
);
217 struct flex_array_part
*part
;
220 if (element_nr
>= fa
->total_nr_elements
)
222 if (elements_fit_in_base(fa
))
223 part
= (struct flex_array_part
*)&fa
->parts
[0];
225 part
= fa
->parts
[part_nr
];
229 dst
= &part
->elements
[index_inside_part(fa
, element_nr
)];
230 memset(dst
, FLEX_ARRAY_FREE
, fa
->element_size
);
235 * flex_array_prealloc - guarantee that array space exists
236 * @start: index of first array element for which space is allocated
237 * @end: index of last (inclusive) element for which space is allocated
239 * This will guarantee that no future calls to flex_array_put()
240 * will allocate memory. It can be used if you are expecting to
241 * be holding a lock or in some atomic context while writing
242 * data into the array.
244 * Locking must be provided by the caller.
246 int flex_array_prealloc(struct flex_array
*fa
, unsigned int start
,
247 unsigned int end
, gfp_t flags
)
252 struct flex_array_part
*part
;
254 if (start
>= fa
->total_nr_elements
|| end
>= fa
->total_nr_elements
)
256 if (elements_fit_in_base(fa
))
258 start_part
= fa_element_to_part_nr(fa
, start
);
259 end_part
= fa_element_to_part_nr(fa
, end
);
260 for (part_nr
= start_part
; part_nr
<= end_part
; part_nr
++) {
261 part
= __fa_get_part(fa
, part_nr
, flags
);
269 * flex_array_get - pull data back out of the array
270 * @element_nr: index of the element to fetch from the array
272 * Returns a pointer to the data at index @element_nr. Note
273 * that this is a copy of the data that was passed in. If you
274 * are using this to store pointers, you'll get back &ptr.
276 * Locking must be provided by the caller.
278 void *flex_array_get(struct flex_array
*fa
, unsigned int element_nr
)
280 int part_nr
= fa_element_to_part_nr(fa
, element_nr
);
281 struct flex_array_part
*part
;
283 if (element_nr
>= fa
->total_nr_elements
)
285 if (elements_fit_in_base(fa
))
286 part
= (struct flex_array_part
*)&fa
->parts
[0];
288 part
= fa
->parts
[part_nr
];
292 return &part
->elements
[index_inside_part(fa
, element_nr
)];
295 static int part_is_free(struct flex_array_part
*part
)
299 for (i
= 0; i
< sizeof(struct flex_array_part
); i
++)
300 if (part
->elements
[i
] != FLEX_ARRAY_FREE
)
306 * flex_array_shrink - free unused second-level pages
308 * Frees all second-level pages that consist solely of unused
309 * elements. Returns the number of pages freed.
311 * Locking must be provided by the caller.
313 int flex_array_shrink(struct flex_array
*fa
)
315 struct flex_array_part
*part
;
316 int max_part
= nr_base_part_ptrs();
320 if (elements_fit_in_base(fa
))
322 for (part_nr
= 0; part_nr
< max_part
; part_nr
++) {
323 part
= fa
->parts
[part_nr
];
326 if (part_is_free(part
)) {
327 fa
->parts
[part_nr
] = NULL
;