diff options
| author | David Rientjes <rientjes@google.com> | 2009-09-21 20:04:30 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-09-22 10:17:47 -0400 |
| commit | e6de3988aa52debb25a427d085061f3bf1181d54 (patch) | |
| tree | 4a5dd6acac3e73049918c646bd977894335e5560 /lib | |
| parent | 2f30b1f9e1b612cdd1a17daeecf514229e8d6a5f (diff) | |
flex_array: add flex_array_clear function
Add a new function to the flex_array API:
int flex_array_clear(struct flex_array *fa,
unsigned int element_nr)
This function will zero the element at element_nr in the flex_array.
Although this is equivalent to using flex_array_put() and passing a
pointer to zero'd memory, flex_array_clear() does not require such a
pointer to memory that would most likely need to be allocated on the
caller's stack which could be significantly large depending on
element_size.
Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Dave Hansen <dave@linux.vnet.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/flex_array.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/flex_array.c b/lib/flex_array.c index 7baed2fc3bc8..b68f99be4080 100644 --- a/lib/flex_array.c +++ b/lib/flex_array.c | |||
| @@ -207,6 +207,32 @@ int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, | |||
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | /** | 209 | /** |
| 210 | * flex_array_clear - clear element in array at @element_nr | ||
| 211 | * @element_nr: index of the position to clear. | ||
| 212 | * | ||
| 213 | * Locking must be provided by the caller. | ||
| 214 | */ | ||
| 215 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr) | ||
| 216 | { | ||
| 217 | int part_nr = fa_element_to_part_nr(fa, element_nr); | ||
| 218 | struct flex_array_part *part; | ||
| 219 | void *dst; | ||
| 220 | |||
| 221 | if (element_nr >= fa->total_nr_elements) | ||
| 222 | return -ENOSPC; | ||
| 223 | if (elements_fit_in_base(fa)) | ||
| 224 | part = (struct flex_array_part *)&fa->parts[0]; | ||
| 225 | else { | ||
| 226 | part = fa->parts[part_nr]; | ||
| 227 | if (!part) | ||
| 228 | return -EINVAL; | ||
| 229 | } | ||
| 230 | dst = &part->elements[index_inside_part(fa, element_nr)]; | ||
| 231 | memset(dst, 0, fa->element_size); | ||
| 232 | return 0; | ||
| 233 | } | ||
| 234 | |||
| 235 | /** | ||
| 210 | * flex_array_prealloc - guarantee that array space exists | 236 | * flex_array_prealloc - guarantee that array space exists |
| 211 | * @start: index of first array element for which space is allocated | 237 | * @start: index of first array element for which space is allocated |
| 212 | * @end: index of last (inclusive) element for which space is allocated | 238 | * @end: index of last (inclusive) element for which space is allocated |
