datapath: Fix coding style issues.
[sliver-openvswitch.git] / datapath / linux / compat / flex_array.c
1 #include <linux/version.h>
2
3 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
4
5 /*
6  * Flexible array managed in PAGE_SIZE parts
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Copyright IBM Corporation, 2009
23  *
24  * Author: Dave Hansen <dave@linux.vnet.ibm.com>
25  */
26
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>
32
33 struct flex_array_part {
34         char elements[FLEX_ARRAY_PART_SIZE];
35 };
36
37 /*
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
41  * data.
42  */
43 static inline int elements_fit_in_base(struct flex_array *fa)
44 {
45         int data_size = fa->element_size * fa->total_nr_elements;
46         if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
47                 return 1;
48         return 0;
49 }
50
51 /**
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
56  *
57  * Note: all locking must be provided by the caller.
58  *
59  * @total is used to size internal structures.  If the user ever
60  * accesses any array indexes >=@total, it will produce errors.
61  *
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
65  * integer math):
66  *
67  *      (PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *)
68  *
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
72  * 64-bit.
73  *
74  *
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 |
87  *
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.
91  */
92 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
93                                         gfp_t flags)
94 {
95         struct flex_array *ret;
96         int elems_per_part = 0;
97         int reciprocal_elems = 0;
98         int max_size = 0;
99
100         if (element_size) {
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;
104         }
105
106         /* max_size will end up 0 if element_size > PAGE_SIZE */
107         if (total > max_size)
108                 return NULL;
109         ret = kzalloc(sizeof(struct flex_array), flags);
110         if (!ret)
111                 return NULL;
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);
119         return ret;
120 }
121
122 static int fa_element_to_part_nr(struct flex_array *fa,
123                                         unsigned int element_nr)
124 {
125         return reciprocal_divide(element_nr, fa->reciprocal_elems);
126 }
127
128 /**
129  * flex_array_free_parts - just free the second-level pages
130  * @fa:         the flex array from which to free parts
131  *
132  * This is to be used in cases where the base 'struct flex_array'
133  * has been statically allocated and should not be free.
134  */
135 void flex_array_free_parts(struct flex_array *fa)
136 {
137         int part_nr;
138
139         if (elements_fit_in_base(fa))
140                 return;
141         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
142                 kfree(fa->parts[part_nr]);
143 }
144
145 void flex_array_free(struct flex_array *fa)
146 {
147         flex_array_free_parts(fa);
148         kfree(fa);
149 }
150
151 static unsigned int index_inside_part(struct flex_array *fa,
152                                         unsigned int element_nr,
153                                         unsigned int part_nr)
154 {
155         unsigned int part_offset;
156
157         part_offset = element_nr - part_nr * fa->elems_per_part;
158         return part_offset * fa->element_size;
159 }
160
161 static struct flex_array_part *
162 __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
163 {
164         struct flex_array_part *part = fa->parts[part_nr];
165         if (!part) {
166                 part = kmalloc(sizeof(struct flex_array_part), flags);
167                 if (!part)
168                         return NULL;
169                 if (!(flags & __GFP_ZERO))
170                         memset(part, FLEX_ARRAY_FREE,
171                                 sizeof(struct flex_array_part));
172                 fa->parts[part_nr] = part;
173         }
174         return part;
175 }
176
177 /**
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
181  *              the new element.
182  * @src:        address of data to copy into the array
183  * @flags:      page allocation flags to use for array expansion
184  *
185  *
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()
190  * helper function.
191  *
192  * Locking must be provided by the caller.
193  */
194 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
195                         gfp_t flags)
196 {
197         int part_nr = 0;
198         struct flex_array_part *part;
199         void *dst;
200
201         if (element_nr >= fa->total_nr_elements)
202                 return -ENOSPC;
203         if (!fa->element_size)
204                 return 0;
205         if (elements_fit_in_base(fa))
206                 part = (struct flex_array_part *)&fa->parts[0];
207         else {
208                 part_nr = fa_element_to_part_nr(fa, element_nr);
209                 part = __fa_get_part(fa, part_nr, flags);
210                 if (!part)
211                         return -ENOMEM;
212         }
213         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
214         memcpy(dst, src, fa->element_size);
215         return 0;
216 }
217
218 /**
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.
222  *
223  * Locking must be provided by the caller.
224  */
225 int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
226 {
227         int part_nr = 0;
228         struct flex_array_part *part;
229         void *dst;
230
231         if (element_nr >= fa->total_nr_elements)
232                 return -ENOSPC;
233         if (!fa->element_size)
234                 return 0;
235         if (elements_fit_in_base(fa))
236                 part = (struct flex_array_part *)&fa->parts[0];
237         else {
238                 part_nr = fa_element_to_part_nr(fa, element_nr);
239                 part = fa->parts[part_nr];
240                 if (!part)
241                         return -EINVAL;
242         }
243         dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
244         memset(dst, FLEX_ARRAY_FREE, fa->element_size);
245         return 0;
246 }
247
248 /**
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
252  *                      allocated
253  * @nr_elements:        number of elements for which space is allocated
254  * @flags:              page allocation flags
255  *
256  * This will guarantee that no future calls to flex_array_put()
257  * will allocate memory.  It can be used if you are expecting to
258  * be holding a lock or in some atomic context while writing
259  * data into the array.
260  *
261  * Locking must be provided by the caller.
262  */
263 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
264                         unsigned int nr_elements, gfp_t flags)
265 {
266         int start_part;
267         int end_part;
268         int part_nr;
269         unsigned int end;
270         struct flex_array_part *part;
271
272         if (!start && !nr_elements)
273                 return 0;
274         if (start >= fa->total_nr_elements)
275                 return -ENOSPC;
276         if (!nr_elements)
277                 return 0;
278
279         end = start + nr_elements - 1;
280
281         if (end >= fa->total_nr_elements)
282                 return -ENOSPC;
283         if (!fa->element_size)
284                 return 0;
285         if (elements_fit_in_base(fa))
286                 return 0;
287         start_part = fa_element_to_part_nr(fa, start);
288         end_part = fa_element_to_part_nr(fa, end);
289         for (part_nr = start_part; part_nr <= end_part; part_nr++) {
290                 part = __fa_get_part(fa, part_nr, flags);
291                 if (!part)
292                         return -ENOMEM;
293         }
294         return 0;
295 }
296
297 /**
298  * flex_array_get - pull data back out of the array
299  * @fa:         the flex array from which to extract data
300  * @element_nr: index of the element to fetch from the array
301  *
302  * Returns a pointer to the data at index @element_nr.  Note
303  * that this is a copy of the data that was passed in.  If you
304  * are using this to store pointers, you'll get back &ptr.  You
305  * may instead wish to use the flex_array_get_ptr helper.
306  *
307  * Locking must be provided by the caller.
308  */
309 void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
310 {
311         int part_nr = 0;
312         struct flex_array_part *part;
313
314         if (!fa->element_size)
315                 return NULL;
316         if (element_nr >= fa->total_nr_elements)
317                 return NULL;
318         if (elements_fit_in_base(fa))
319                 part = (struct flex_array_part *)&fa->parts[0];
320         else {
321                 part_nr = fa_element_to_part_nr(fa, element_nr);
322                 part = fa->parts[part_nr];
323                 if (!part)
324                         return NULL;
325         }
326         return &part->elements[index_inside_part(fa, element_nr, part_nr)];
327 }
328
329 /**
330  * flex_array_get_ptr - pull a ptr back out of the array
331  * @fa:         the flex array from which to extract data
332  * @element_nr: index of the element to fetch from the array
333  *
334  * Returns the pointer placed in the flex array at element_nr using
335  * flex_array_put_ptr().  This function should not be called if the
336  * element in question was not set using the _put_ptr() helper.
337  */
338 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
339 {
340         void **tmp;
341
342         tmp = flex_array_get(fa, element_nr);
343         if (!tmp)
344                 return NULL;
345
346         return *tmp;
347 }
348
349 static int part_is_free(struct flex_array_part *part)
350 {
351         int i;
352
353         for (i = 0; i < sizeof(struct flex_array_part); i++)
354                 if (part->elements[i] != FLEX_ARRAY_FREE)
355                         return 0;
356         return 1;
357 }
358
359 /**
360  * flex_array_shrink - free unused second-level pages
361  * @fa:         the flex array to shrink
362  *
363  * Frees all second-level pages that consist solely of unused
364  * elements.  Returns the number of pages freed.
365  *
366  * Locking must be provided by the caller.
367  */
368 int flex_array_shrink(struct flex_array *fa)
369 {
370         struct flex_array_part *part;
371         int part_nr;
372         int ret = 0;
373
374         if (!fa->total_nr_elements || !fa->element_size)
375                 return 0;
376         if (elements_fit_in_base(fa))
377                 return ret;
378         for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
379                 part = fa->parts[part_nr];
380                 if (!part)
381                         continue;
382                 if (part_is_free(part)) {
383                         fa->parts[part_nr] = NULL;
384                         kfree(part);
385                         ret++;
386                 }
387         }
388         return ret;
389 }
390
391 #endif /* Linux version < 3.0.0 */