blob: 854b57bd7d9d346276fe83599f5cbd306c6e0fe4 [file] [log] [blame]
Dave Hansen534acc02009-07-29 15:04:18 -07001/*
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
23#include <linux/flex_array.h>
24#include <linux/slab.h>
25#include <linux/stddef.h>
David Rientjes78c377d2011-01-12 16:59:55 -080026#include <linux/module.h>
Dave Hansen534acc02009-07-29 15:04:18 -070027
28struct flex_array_part {
29 char elements[FLEX_ARRAY_PART_SIZE];
30};
31
Dave Hansen534acc02009-07-29 15:04:18 -070032/*
33 * If a user requests an allocation which is small
34 * enough, we may simply use the space in the
35 * flex_array->parts[] array to store the user
36 * data.
37 */
38static inline int elements_fit_in_base(struct flex_array *fa)
39{
40 int data_size = fa->element_size * fa->total_nr_elements;
David Rientjes45b588d2009-09-21 17:04:33 -070041 if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
Dave Hansen534acc02009-07-29 15:04:18 -070042 return 1;
43 return 0;
44}
45
46/**
47 * flex_array_alloc - allocate a new flexible array
48 * @element_size: the size of individual elements in the array
49 * @total: total number of elements that this should hold
David Rientjesfc0d8d92009-09-21 17:04:33 -070050 * @flags: page allocation flags to use for base array
Dave Hansen534acc02009-07-29 15:04:18 -070051 *
52 * Note: all locking must be provided by the caller.
53 *
54 * @total is used to size internal structures. If the user ever
55 * accesses any array indexes >=@total, it will produce errors.
56 *
57 * The maximum number of elements is defined as: the number of
58 * elements that can be stored in a page times the number of
59 * page pointers that we can fit in the base structure or (using
60 * integer math):
61 *
62 * (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
63 *
64 * Here's a table showing example capacities. Note that the maximum
65 * index that the get/put() functions is just nr_objects-1. This
66 * basically means that you get 4MB of storage on 32-bit and 2MB on
67 * 64-bit.
68 *
69 *
70 * Element size | Objects | Objects |
71 * PAGE_SIZE=4k | 32-bit | 64-bit |
72 * ---------------------------------|
73 * 1 bytes | 4186112 | 2093056 |
74 * 2 bytes | 2093056 | 1046528 |
75 * 3 bytes | 1395030 | 697515 |
76 * 4 bytes | 1046528 | 523264 |
77 * 32 bytes | 130816 | 65408 |
78 * 33 bytes | 126728 | 63364 |
79 * 2048 bytes | 2044 | 1022 |
80 * 2049 bytes | 1022 | 511 |
81 * void * | 1046528 | 261632 |
82 *
83 * Since 64-bit pointers are twice the size, we lose half the
84 * capacity in the base structure. Also note that no effort is made
85 * to efficiently pack objects across page boundaries.
86 */
David Rientjesb62e4082009-08-26 14:29:22 -070087struct flex_array *flex_array_alloc(int element_size, unsigned int total,
88 gfp_t flags)
Dave Hansen534acc02009-07-29 15:04:18 -070089{
90 struct flex_array *ret;
David Rientjes45b588d2009-09-21 17:04:33 -070091 int max_size = FLEX_ARRAY_NR_BASE_PTRS *
92 FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
Dave Hansen534acc02009-07-29 15:04:18 -070093
94 /* max_size will end up 0 if element_size > PAGE_SIZE */
95 if (total > max_size)
96 return NULL;
97 ret = kzalloc(sizeof(struct flex_array), flags);
98 if (!ret)
99 return NULL;
100 ret->element_size = element_size;
101 ret->total_nr_elements = total;
David Rientjes19da3dd2009-09-21 17:04:31 -0700102 if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
Changli Gaoe59464c2010-04-23 13:17:45 -0400103 memset(&ret->parts[0], FLEX_ARRAY_FREE,
David Rientjes45b588d2009-09-21 17:04:33 -0700104 FLEX_ARRAY_BASE_BYTES_LEFT);
Dave Hansen534acc02009-07-29 15:04:18 -0700105 return ret;
106}
David Rientjes78c377d2011-01-12 16:59:55 -0800107EXPORT_SYMBOL(flex_array_alloc);
Dave Hansen534acc02009-07-29 15:04:18 -0700108
David Rientjesb62e4082009-08-26 14:29:22 -0700109static int fa_element_to_part_nr(struct flex_array *fa,
110 unsigned int element_nr)
Dave Hansen534acc02009-07-29 15:04:18 -0700111{
David Rientjes45b588d2009-09-21 17:04:33 -0700112 return element_nr / FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size);
Dave Hansen534acc02009-07-29 15:04:18 -0700113}
114
115/**
116 * flex_array_free_parts - just free the second-level pages
David Rientjesfc0d8d92009-09-21 17:04:33 -0700117 * @fa: the flex array from which to free parts
Dave Hansen534acc02009-07-29 15:04:18 -0700118 *
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 */
122void flex_array_free_parts(struct flex_array *fa)
123{
124 int part_nr;
Dave Hansen534acc02009-07-29 15:04:18 -0700125
126 if (elements_fit_in_base(fa))
127 return;
David Rientjes45b588d2009-09-21 17:04:33 -0700128 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
Dave Hansen534acc02009-07-29 15:04:18 -0700129 kfree(fa->parts[part_nr]);
130}
David Rientjes78c377d2011-01-12 16:59:55 -0800131EXPORT_SYMBOL(flex_array_free_parts);
Dave Hansen534acc02009-07-29 15:04:18 -0700132
133void flex_array_free(struct flex_array *fa)
134{
135 flex_array_free_parts(fa);
136 kfree(fa);
137}
David Rientjes78c377d2011-01-12 16:59:55 -0800138EXPORT_SYMBOL(flex_array_free);
Dave Hansen534acc02009-07-29 15:04:18 -0700139
David Rientjesb62e4082009-08-26 14:29:22 -0700140static unsigned int index_inside_part(struct flex_array *fa,
141 unsigned int element_nr)
Dave Hansen534acc02009-07-29 15:04:18 -0700142{
David Rientjesb62e4082009-08-26 14:29:22 -0700143 unsigned int part_offset;
Dave Hansen534acc02009-07-29 15:04:18 -0700144
David Rientjes45b588d2009-09-21 17:04:33 -0700145 part_offset = element_nr %
146 FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size);
Dave Hansen534acc02009-07-29 15:04:18 -0700147 return part_offset * fa->element_size;
148}
149
150static struct flex_array_part *
151__fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
152{
153 struct flex_array_part *part = fa->parts[part_nr];
154 if (!part) {
David Rientjes19da3dd2009-09-21 17:04:31 -0700155 part = kmalloc(sizeof(struct flex_array_part), flags);
Dave Hansen534acc02009-07-29 15:04:18 -0700156 if (!part)
157 return NULL;
David Rientjes19da3dd2009-09-21 17:04:31 -0700158 if (!(flags & __GFP_ZERO))
159 memset(part, FLEX_ARRAY_FREE,
160 sizeof(struct flex_array_part));
Dave Hansen534acc02009-07-29 15:04:18 -0700161 fa->parts[part_nr] = part;
162 }
163 return part;
164}
165
166/**
167 * flex_array_put - copy data into the array at @element_nr
David Rientjesfc0d8d92009-09-21 17:04:33 -0700168 * @fa: the flex array to copy data into
Dave Hansen534acc02009-07-29 15:04:18 -0700169 * @element_nr: index of the position in which to insert
170 * the new element.
David Rientjesfc0d8d92009-09-21 17:04:33 -0700171 * @src: address of data to copy into the array
172 * @flags: page allocation flags to use for array expansion
173 *
Dave Hansen534acc02009-07-29 15:04:18 -0700174 *
175 * Note that this *copies* the contents of @src into
176 * the array. If you are trying to store an array of
177 * pointers, make sure to pass in &ptr instead of ptr.
Eric Parisea98eed2010-08-09 17:20:56 -0700178 * You may instead wish to use the flex_array_put_ptr()
179 * helper function.
Dave Hansen534acc02009-07-29 15:04:18 -0700180 *
181 * Locking must be provided by the caller.
182 */
David Rientjesb62e4082009-08-26 14:29:22 -0700183int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
184 gfp_t flags)
Dave Hansen534acc02009-07-29 15:04:18 -0700185{
186 int part_nr = fa_element_to_part_nr(fa, element_nr);
187 struct flex_array_part *part;
188 void *dst;
189
190 if (element_nr >= fa->total_nr_elements)
191 return -ENOSPC;
192 if (elements_fit_in_base(fa))
193 part = (struct flex_array_part *)&fa->parts[0];
David Rientjesa30b5952009-08-26 14:29:20 -0700194 else {
Dave Hansen534acc02009-07-29 15:04:18 -0700195 part = __fa_get_part(fa, part_nr, flags);
David Rientjesa30b5952009-08-26 14:29:20 -0700196 if (!part)
197 return -ENOMEM;
198 }
Dave Hansen534acc02009-07-29 15:04:18 -0700199 dst = &part->elements[index_inside_part(fa, element_nr)];
200 memcpy(dst, src, fa->element_size);
201 return 0;
202}
David Rientjes78c377d2011-01-12 16:59:55 -0800203EXPORT_SYMBOL(flex_array_put);
Dave Hansen534acc02009-07-29 15:04:18 -0700204
205/**
David Rientjese6de3982009-09-21 17:04:30 -0700206 * flex_array_clear - clear element in array at @element_nr
David Rientjesfc0d8d92009-09-21 17:04:33 -0700207 * @fa: the flex array of the element.
David Rientjese6de3982009-09-21 17:04:30 -0700208 * @element_nr: index of the position to clear.
209 *
210 * Locking must be provided by the caller.
211 */
212int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
213{
214 int part_nr = fa_element_to_part_nr(fa, element_nr);
215 struct flex_array_part *part;
216 void *dst;
217
218 if (element_nr >= fa->total_nr_elements)
219 return -ENOSPC;
220 if (elements_fit_in_base(fa))
221 part = (struct flex_array_part *)&fa->parts[0];
222 else {
223 part = fa->parts[part_nr];
224 if (!part)
225 return -EINVAL;
226 }
227 dst = &part->elements[index_inside_part(fa, element_nr)];
David Rientjes19da3dd2009-09-21 17:04:31 -0700228 memset(dst, FLEX_ARRAY_FREE, fa->element_size);
David Rientjese6de3982009-09-21 17:04:30 -0700229 return 0;
230}
David Rientjes78c377d2011-01-12 16:59:55 -0800231EXPORT_SYMBOL(flex_array_clear);
David Rientjese6de3982009-09-21 17:04:30 -0700232
233/**
Dave Hansen534acc02009-07-29 15:04:18 -0700234 * flex_array_prealloc - guarantee that array space exists
Eric Paris5d30b102011-04-28 15:55:52 -0400235 * @fa: the flex array for which to preallocate parts
236 * @start: index of first array element for which space is allocated
237 * @nr_elements: number of elements for which space is allocated
238 * @flags: page allocation flags
Dave Hansen534acc02009-07-29 15:04:18 -0700239 *
240 * This will guarantee that no future calls to flex_array_put()
241 * will allocate memory. It can be used if you are expecting to
242 * be holding a lock or in some atomic context while writing
243 * data into the array.
244 *
245 * Locking must be provided by the caller.
246 */
David Rientjesb62e4082009-08-26 14:29:22 -0700247int flex_array_prealloc(struct flex_array *fa, unsigned int start,
Eric Paris5d30b102011-04-28 15:55:52 -0400248 unsigned int nr_elements, gfp_t flags)
Dave Hansen534acc02009-07-29 15:04:18 -0700249{
250 int start_part;
251 int end_part;
252 int part_nr;
Eric Paris5d30b102011-04-28 15:55:52 -0400253 unsigned int end;
Dave Hansen534acc02009-07-29 15:04:18 -0700254 struct flex_array_part *part;
255
Eric Parisbf69d412011-04-28 15:55:52 -0400256 if (!start && !nr_elements)
257 return 0;
258 if (start >= fa->total_nr_elements)
259 return -ENOSPC;
260 if (!nr_elements)
261 return 0;
262
Eric Paris5d30b102011-04-28 15:55:52 -0400263 end = start + nr_elements - 1;
264
Eric Parisbf69d412011-04-28 15:55:52 -0400265 if (end >= fa->total_nr_elements)
Dave Hansen534acc02009-07-29 15:04:18 -0700266 return -ENOSPC;
267 if (elements_fit_in_base(fa))
268 return 0;
269 start_part = fa_element_to_part_nr(fa, start);
270 end_part = fa_element_to_part_nr(fa, end);
271 for (part_nr = start_part; part_nr <= end_part; part_nr++) {
272 part = __fa_get_part(fa, part_nr, flags);
273 if (!part)
274 return -ENOMEM;
275 }
276 return 0;
277}
David Rientjes78c377d2011-01-12 16:59:55 -0800278EXPORT_SYMBOL(flex_array_prealloc);
Dave Hansen534acc02009-07-29 15:04:18 -0700279
280/**
281 * flex_array_get - pull data back out of the array
David Rientjesfc0d8d92009-09-21 17:04:33 -0700282 * @fa: the flex array from which to extract data
Dave Hansen534acc02009-07-29 15:04:18 -0700283 * @element_nr: index of the element to fetch from the array
284 *
285 * Returns a pointer to the data at index @element_nr. Note
286 * that this is a copy of the data that was passed in. If you
Eric Parisea98eed2010-08-09 17:20:56 -0700287 * are using this to store pointers, you'll get back &ptr. You
288 * may instead wish to use the flex_array_get_ptr helper.
Dave Hansen534acc02009-07-29 15:04:18 -0700289 *
290 * Locking must be provided by the caller.
291 */
David Rientjesb62e4082009-08-26 14:29:22 -0700292void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
Dave Hansen534acc02009-07-29 15:04:18 -0700293{
294 int part_nr = fa_element_to_part_nr(fa, element_nr);
295 struct flex_array_part *part;
Dave Hansen534acc02009-07-29 15:04:18 -0700296
297 if (element_nr >= fa->total_nr_elements)
298 return NULL;
Dave Hansen534acc02009-07-29 15:04:18 -0700299 if (elements_fit_in_base(fa))
300 part = (struct flex_array_part *)&fa->parts[0];
David Rientjesa30b5952009-08-26 14:29:20 -0700301 else {
Dave Hansen534acc02009-07-29 15:04:18 -0700302 part = fa->parts[part_nr];
David Rientjesa30b5952009-08-26 14:29:20 -0700303 if (!part)
304 return NULL;
305 }
Dave Hansen534acc02009-07-29 15:04:18 -0700306 return &part->elements[index_inside_part(fa, element_nr)];
307}
David Rientjes78c377d2011-01-12 16:59:55 -0800308EXPORT_SYMBOL(flex_array_get);
David Rientjes4af5a2f2009-09-21 17:04:31 -0700309
Eric Parisea98eed2010-08-09 17:20:56 -0700310/**
311 * flex_array_get_ptr - pull a ptr back out of the array
312 * @fa: the flex array from which to extract data
313 * @element_nr: index of the element to fetch from the array
314 *
315 * Returns the pointer placed in the flex array at element_nr using
316 * flex_array_put_ptr(). This function should not be called if the
317 * element in question was not set using the _put_ptr() helper.
318 */
319void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
320{
321 void **tmp;
322
323 tmp = flex_array_get(fa, element_nr);
324 if (!tmp)
325 return NULL;
326
327 return *tmp;
328}
David Rientjes78c377d2011-01-12 16:59:55 -0800329EXPORT_SYMBOL(flex_array_get_ptr);
Eric Parisea98eed2010-08-09 17:20:56 -0700330
David Rientjes4af5a2f2009-09-21 17:04:31 -0700331static int part_is_free(struct flex_array_part *part)
332{
333 int i;
334
335 for (i = 0; i < sizeof(struct flex_array_part); i++)
336 if (part->elements[i] != FLEX_ARRAY_FREE)
337 return 0;
338 return 1;
339}
340
341/**
342 * flex_array_shrink - free unused second-level pages
David Rientjesfc0d8d92009-09-21 17:04:33 -0700343 * @fa: the flex array to shrink
David Rientjes4af5a2f2009-09-21 17:04:31 -0700344 *
345 * Frees all second-level pages that consist solely of unused
346 * elements. Returns the number of pages freed.
347 *
348 * Locking must be provided by the caller.
349 */
350int flex_array_shrink(struct flex_array *fa)
351{
352 struct flex_array_part *part;
David Rientjes4af5a2f2009-09-21 17:04:31 -0700353 int part_nr;
354 int ret = 0;
355
Eric Parisbf69d412011-04-28 15:55:52 -0400356 if (!fa->total_nr_elements)
357 return 0;
David Rientjes4af5a2f2009-09-21 17:04:31 -0700358 if (elements_fit_in_base(fa))
359 return ret;
David Rientjes45b588d2009-09-21 17:04:33 -0700360 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
David Rientjes4af5a2f2009-09-21 17:04:31 -0700361 part = fa->parts[part_nr];
362 if (!part)
363 continue;
364 if (part_is_free(part)) {
365 fa->parts[part_nr] = NULL;
366 kfree(part);
367 ret++;
368 }
369 }
370 return ret;
371}
David Rientjes78c377d2011-01-12 16:59:55 -0800372EXPORT_SYMBOL(flex_array_shrink);