Commit ea98eed9bcb62d1319db8b1210712c6a110a886c

Authored by Eric Paris
Committed by Linus Torvalds
1 parent 559b140a36

flex_array: add helpers to get and put to make pointers easy to use

Getting and putting arrays of pointers with flex arrays is a PITA.  You
have to remember to pass &ptr to the _put and you have to do weird and
wacky casting to get the ptr back from the _get.  Add two functions
flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.

[akpm@linux-foundation.org: simplification suggested by Joe]
Signed-off-by: Eric Paris <eparis@redhat.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Dave Hansen <dave@linux.vnet.ibm.com>
Cc: Joe Perches <joe@perches.com>
Cc: James Morris <jmorris@namei.org>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 29 additions and 1 deletions Side-by-side Diff

include/linux/flex_array.h
... ... @@ -70,5 +70,10 @@
70 70 void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
71 71 int flex_array_shrink(struct flex_array *fa);
72 72  
  73 +#define flex_array_put_ptr(fa, nr, src, gfp) \
  74 + flex_array_put(fa, nr, &(void *)(src), gfp)
  75 +
  76 +void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
  77 +
73 78 #endif /* _FLEX_ARRAY_H */
... ... @@ -171,6 +171,8 @@
171 171 * Note that this *copies* the contents of @src into
172 172 * the array. If you are trying to store an array of
173 173 * pointers, make sure to pass in &ptr instead of ptr.
  174 + * You may instead wish to use the flex_array_put_ptr()
  175 + * helper function.
174 176 *
175 177 * Locking must be provided by the caller.
176 178 */
... ... @@ -265,7 +267,8 @@
265 267 *
266 268 * Returns a pointer to the data at index @element_nr. Note
267 269 * 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.
  270 + * are using this to store pointers, you'll get back &ptr. You
  271 + * may instead wish to use the flex_array_get_ptr helper.
269 272 *
270 273 * Locking must be provided by the caller.
271 274 */
... ... @@ -284,6 +287,26 @@
284 287 return NULL;
285 288 }
286 289 return &part->elements[index_inside_part(fa, element_nr)];
  290 +}
  291 +
  292 +/**
  293 + * flex_array_get_ptr - pull a ptr back out of the array
  294 + * @fa: the flex array from which to extract data
  295 + * @element_nr: index of the element to fetch from the array
  296 + *
  297 + * Returns the pointer placed in the flex array at element_nr using
  298 + * flex_array_put_ptr(). This function should not be called if the
  299 + * element in question was not set using the _put_ptr() helper.
  300 + */
  301 +void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
  302 +{
  303 + void **tmp;
  304 +
  305 + tmp = flex_array_get(fa, element_nr);
  306 + if (!tmp)
  307 + return NULL;
  308 +
  309 + return *tmp;
287 310 }
288 311  
289 312 static int part_is_free(struct flex_array_part *part)