aboutsummaryrefslogtreecommitdiffstats
path: root/lib/flex_array.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/flex_array.c')
-rw-r--r--lib/flex_array.c37
1 files changed, 35 insertions, 2 deletions
diff --git a/lib/flex_array.c b/lib/flex_array.c
index 66eef2e4483e..c0ea40ba2082 100644
--- a/lib/flex_array.c
+++ b/lib/flex_array.c
@@ -23,6 +23,7 @@
23#include <linux/flex_array.h> 23#include <linux/flex_array.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/stddef.h> 25#include <linux/stddef.h>
26#include <linux/module.h>
26 27
27struct flex_array_part { 28struct flex_array_part {
28 char elements[FLEX_ARRAY_PART_SIZE]; 29 char elements[FLEX_ARRAY_PART_SIZE];
@@ -99,10 +100,11 @@ struct flex_array *flex_array_alloc(int element_size, unsigned int total,
99 ret->element_size = element_size; 100 ret->element_size = element_size;
100 ret->total_nr_elements = total; 101 ret->total_nr_elements = total;
101 if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO)) 102 if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
102 memset(ret->parts[0], FLEX_ARRAY_FREE, 103 memset(&ret->parts[0], FLEX_ARRAY_FREE,
103 FLEX_ARRAY_BASE_BYTES_LEFT); 104 FLEX_ARRAY_BASE_BYTES_LEFT);
104 return ret; 105 return ret;
105} 106}
107EXPORT_SYMBOL(flex_array_alloc);
106 108
107static int fa_element_to_part_nr(struct flex_array *fa, 109static int fa_element_to_part_nr(struct flex_array *fa,
108 unsigned int element_nr) 110 unsigned int element_nr)
@@ -126,12 +128,14 @@ void flex_array_free_parts(struct flex_array *fa)
126 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) 128 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
127 kfree(fa->parts[part_nr]); 129 kfree(fa->parts[part_nr]);
128} 130}
131EXPORT_SYMBOL(flex_array_free_parts);
129 132
130void flex_array_free(struct flex_array *fa) 133void flex_array_free(struct flex_array *fa)
131{ 134{
132 flex_array_free_parts(fa); 135 flex_array_free_parts(fa);
133 kfree(fa); 136 kfree(fa);
134} 137}
138EXPORT_SYMBOL(flex_array_free);
135 139
136static unsigned int index_inside_part(struct flex_array *fa, 140static unsigned int index_inside_part(struct flex_array *fa,
137 unsigned int element_nr) 141 unsigned int element_nr)
@@ -171,6 +175,8 @@ __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
171 * Note that this *copies* the contents of @src into 175 * Note that this *copies* the contents of @src into
172 * the array. If you are trying to store an array of 176 * the array. If you are trying to store an array of
173 * pointers, make sure to pass in &ptr instead of ptr. 177 * pointers, make sure to pass in &ptr instead of ptr.
178 * You may instead wish to use the flex_array_put_ptr()
179 * helper function.
174 * 180 *
175 * Locking must be provided by the caller. 181 * Locking must be provided by the caller.
176 */ 182 */
@@ -194,6 +200,7 @@ int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
194 memcpy(dst, src, fa->element_size); 200 memcpy(dst, src, fa->element_size);
195 return 0; 201 return 0;
196} 202}
203EXPORT_SYMBOL(flex_array_put);
197 204
198/** 205/**
199 * flex_array_clear - clear element in array at @element_nr 206 * flex_array_clear - clear element in array at @element_nr
@@ -221,6 +228,7 @@ int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
221 memset(dst, FLEX_ARRAY_FREE, fa->element_size); 228 memset(dst, FLEX_ARRAY_FREE, fa->element_size);
222 return 0; 229 return 0;
223} 230}
231EXPORT_SYMBOL(flex_array_clear);
224 232
225/** 233/**
226 * flex_array_prealloc - guarantee that array space exists 234 * flex_array_prealloc - guarantee that array space exists
@@ -257,6 +265,7 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
257 } 265 }
258 return 0; 266 return 0;
259} 267}
268EXPORT_SYMBOL(flex_array_prealloc);
260 269
261/** 270/**
262 * flex_array_get - pull data back out of the array 271 * flex_array_get - pull data back out of the array
@@ -265,7 +274,8 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
265 * 274 *
266 * Returns a pointer to the data at index @element_nr. Note 275 * Returns a pointer to the data at index @element_nr. Note
267 * that this is a copy of the data that was passed in. If you 276 * that this is a copy of the data that was passed in. If you
268 * are using this to store pointers, you'll get back &ptr. 277 * are using this to store pointers, you'll get back &ptr. You
278 * may instead wish to use the flex_array_get_ptr helper.
269 * 279 *
270 * Locking must be provided by the caller. 280 * Locking must be provided by the caller.
271 */ 281 */
@@ -285,6 +295,28 @@ void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
285 } 295 }
286 return &part->elements[index_inside_part(fa, element_nr)]; 296 return &part->elements[index_inside_part(fa, element_nr)];
287} 297}
298EXPORT_SYMBOL(flex_array_get);
299
300/**
301 * flex_array_get_ptr - pull a ptr back out of the array
302 * @fa: the flex array from which to extract data
303 * @element_nr: index of the element to fetch from the array
304 *
305 * Returns the pointer placed in the flex array at element_nr using
306 * flex_array_put_ptr(). This function should not be called if the
307 * element in question was not set using the _put_ptr() helper.
308 */
309void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
310{
311 void **tmp;
312
313 tmp = flex_array_get(fa, element_nr);
314 if (!tmp)
315 return NULL;
316
317 return *tmp;
318}
319EXPORT_SYMBOL(flex_array_get_ptr);
288 320
289static int part_is_free(struct flex_array_part *part) 321static int part_is_free(struct flex_array_part *part)
290{ 322{
@@ -325,3 +357,4 @@ int flex_array_shrink(struct flex_array *fa)
325 } 357 }
326 return ret; 358 return ret;
327} 359}
360EXPORT_SYMBOL(flex_array_shrink);