diff options
Diffstat (limited to 'lib/flex_array.c')
| -rw-r--r-- | lib/flex_array.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/lib/flex_array.c b/lib/flex_array.c index 854b57bd7d9d..9b8b89458c4c 100644 --- a/lib/flex_array.c +++ b/lib/flex_array.c | |||
| @@ -24,6 +24,7 @@ | |||
| 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 | #include <linux/module.h> |
| 27 | #include <linux/reciprocal_div.h> | ||
| 27 | 28 | ||
| 28 | struct flex_array_part { | 29 | struct flex_array_part { |
| 29 | char elements[FLEX_ARRAY_PART_SIZE]; | 30 | char elements[FLEX_ARRAY_PART_SIZE]; |
| @@ -70,15 +71,15 @@ static inline int elements_fit_in_base(struct flex_array *fa) | |||
| 70 | * Element size | Objects | Objects | | 71 | * Element size | Objects | Objects | |
| 71 | * PAGE_SIZE=4k | 32-bit | 64-bit | | 72 | * PAGE_SIZE=4k | 32-bit | 64-bit | |
| 72 | * ---------------------------------| | 73 | * ---------------------------------| |
| 73 | * 1 bytes | 4186112 | 2093056 | | 74 | * 1 bytes | 4177920 | 2088960 | |
| 74 | * 2 bytes | 2093056 | 1046528 | | 75 | * 2 bytes | 2088960 | 1044480 | |
| 75 | * 3 bytes | 1395030 | 697515 | | 76 | * 3 bytes | 1392300 | 696150 | |
| 76 | * 4 bytes | 1046528 | 523264 | | 77 | * 4 bytes | 1044480 | 522240 | |
| 77 | * 32 bytes | 130816 | 65408 | | 78 | * 32 bytes | 130560 | 65408 | |
| 78 | * 33 bytes | 126728 | 63364 | | 79 | * 33 bytes | 126480 | 63240 | |
| 79 | * 2048 bytes | 2044 | 1022 | | 80 | * 2048 bytes | 2040 | 1020 | |
| 80 | * 2049 bytes | 1022 | 511 | | 81 | * 2049 bytes | 1020 | 510 | |
| 81 | * void * | 1046528 | 261632 | | 82 | * void * | 1044480 | 261120 | |
| 82 | * | 83 | * |
| 83 | * Since 64-bit pointers are twice the size, we lose half the | 84 | * 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 | * capacity in the base structure. Also note that no effort is made |
| @@ -88,8 +89,15 @@ struct flex_array *flex_array_alloc(int element_size, unsigned int total, | |||
| 88 | gfp_t flags) | 89 | gfp_t flags) |
| 89 | { | 90 | { |
| 90 | struct flex_array *ret; | 91 | struct flex_array *ret; |
| 91 | int max_size = FLEX_ARRAY_NR_BASE_PTRS * | 92 | int elems_per_part = 0; |
| 92 | FLEX_ARRAY_ELEMENTS_PER_PART(element_size); | 93 | int reciprocal_elems = 0; |
| 94 | int max_size = 0; | ||
| 95 | |||
| 96 | if (element_size) { | ||
| 97 | elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size); | ||
| 98 | reciprocal_elems = reciprocal_value(elems_per_part); | ||
| 99 | max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part; | ||
| 100 | } | ||
| 93 | 101 | ||
| 94 | /* max_size will end up 0 if element_size > PAGE_SIZE */ | 102 | /* max_size will end up 0 if element_size > PAGE_SIZE */ |
| 95 | if (total > max_size) | 103 | if (total > max_size) |
| @@ -99,6 +107,8 @@ struct flex_array *flex_array_alloc(int element_size, unsigned int total, | |||
| 99 | return NULL; | 107 | return NULL; |
| 100 | ret->element_size = element_size; | 108 | ret->element_size = element_size; |
| 101 | ret->total_nr_elements = total; | 109 | ret->total_nr_elements = total; |
| 110 | ret->elems_per_part = elems_per_part; | ||
| 111 | ret->reciprocal_elems = reciprocal_elems; | ||
| 102 | if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO)) | 112 | if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO)) |
| 103 | memset(&ret->parts[0], FLEX_ARRAY_FREE, | 113 | memset(&ret->parts[0], FLEX_ARRAY_FREE, |
| 104 | FLEX_ARRAY_BASE_BYTES_LEFT); | 114 | FLEX_ARRAY_BASE_BYTES_LEFT); |
| @@ -109,7 +119,7 @@ EXPORT_SYMBOL(flex_array_alloc); | |||
| 109 | static int fa_element_to_part_nr(struct flex_array *fa, | 119 | static int fa_element_to_part_nr(struct flex_array *fa, |
| 110 | unsigned int element_nr) | 120 | unsigned int element_nr) |
| 111 | { | 121 | { |
| 112 | return element_nr / FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size); | 122 | return reciprocal_divide(element_nr, fa->reciprocal_elems); |
| 113 | } | 123 | } |
| 114 | 124 | ||
| 115 | /** | 125 | /** |
| @@ -138,12 +148,12 @@ void flex_array_free(struct flex_array *fa) | |||
| 138 | EXPORT_SYMBOL(flex_array_free); | 148 | EXPORT_SYMBOL(flex_array_free); |
| 139 | 149 | ||
| 140 | static unsigned int index_inside_part(struct flex_array *fa, | 150 | static unsigned int index_inside_part(struct flex_array *fa, |
| 141 | unsigned int element_nr) | 151 | unsigned int element_nr, |
| 152 | unsigned int part_nr) | ||
| 142 | { | 153 | { |
| 143 | unsigned int part_offset; | 154 | unsigned int part_offset; |
| 144 | 155 | ||
| 145 | part_offset = element_nr % | 156 | part_offset = element_nr - part_nr * fa->elems_per_part; |
| 146 | FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size); | ||
| 147 | return part_offset * fa->element_size; | 157 | return part_offset * fa->element_size; |
| 148 | } | 158 | } |
| 149 | 159 | ||
| @@ -183,20 +193,23 @@ __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags) | |||
| 183 | int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, | 193 | int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, |
| 184 | gfp_t flags) | 194 | gfp_t flags) |
| 185 | { | 195 | { |
| 186 | int part_nr = fa_element_to_part_nr(fa, element_nr); | 196 | int part_nr = 0; |
| 187 | struct flex_array_part *part; | 197 | struct flex_array_part *part; |
| 188 | void *dst; | 198 | void *dst; |
| 189 | 199 | ||
| 190 | if (element_nr >= fa->total_nr_elements) | 200 | if (element_nr >= fa->total_nr_elements) |
| 191 | return -ENOSPC; | 201 | return -ENOSPC; |
| 202 | if (!fa->element_size) | ||
| 203 | return 0; | ||
| 192 | if (elements_fit_in_base(fa)) | 204 | if (elements_fit_in_base(fa)) |
| 193 | part = (struct flex_array_part *)&fa->parts[0]; | 205 | part = (struct flex_array_part *)&fa->parts[0]; |
| 194 | else { | 206 | else { |
| 207 | part_nr = fa_element_to_part_nr(fa, element_nr); | ||
| 195 | part = __fa_get_part(fa, part_nr, flags); | 208 | part = __fa_get_part(fa, part_nr, flags); |
| 196 | if (!part) | 209 | if (!part) |
| 197 | return -ENOMEM; | 210 | return -ENOMEM; |
| 198 | } | 211 | } |
| 199 | dst = &part->elements[index_inside_part(fa, element_nr)]; | 212 | dst = &part->elements[index_inside_part(fa, element_nr, part_nr)]; |
| 200 | memcpy(dst, src, fa->element_size); | 213 | memcpy(dst, src, fa->element_size); |
| 201 | return 0; | 214 | return 0; |
| 202 | } | 215 | } |
| @@ -211,20 +224,23 @@ EXPORT_SYMBOL(flex_array_put); | |||
| 211 | */ | 224 | */ |
| 212 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr) | 225 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr) |
| 213 | { | 226 | { |
| 214 | int part_nr = fa_element_to_part_nr(fa, element_nr); | 227 | int part_nr = 0; |
| 215 | struct flex_array_part *part; | 228 | struct flex_array_part *part; |
| 216 | void *dst; | 229 | void *dst; |
| 217 | 230 | ||
| 218 | if (element_nr >= fa->total_nr_elements) | 231 | if (element_nr >= fa->total_nr_elements) |
| 219 | return -ENOSPC; | 232 | return -ENOSPC; |
| 233 | if (!fa->element_size) | ||
| 234 | return 0; | ||
| 220 | if (elements_fit_in_base(fa)) | 235 | if (elements_fit_in_base(fa)) |
| 221 | part = (struct flex_array_part *)&fa->parts[0]; | 236 | part = (struct flex_array_part *)&fa->parts[0]; |
| 222 | else { | 237 | else { |
| 238 | part_nr = fa_element_to_part_nr(fa, element_nr); | ||
| 223 | part = fa->parts[part_nr]; | 239 | part = fa->parts[part_nr]; |
| 224 | if (!part) | 240 | if (!part) |
| 225 | return -EINVAL; | 241 | return -EINVAL; |
| 226 | } | 242 | } |
| 227 | dst = &part->elements[index_inside_part(fa, element_nr)]; | 243 | dst = &part->elements[index_inside_part(fa, element_nr, part_nr)]; |
| 228 | memset(dst, FLEX_ARRAY_FREE, fa->element_size); | 244 | memset(dst, FLEX_ARRAY_FREE, fa->element_size); |
| 229 | return 0; | 245 | return 0; |
| 230 | } | 246 | } |
| @@ -264,6 +280,8 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start, | |||
| 264 | 280 | ||
| 265 | if (end >= fa->total_nr_elements) | 281 | if (end >= fa->total_nr_elements) |
| 266 | return -ENOSPC; | 282 | return -ENOSPC; |
| 283 | if (!fa->element_size) | ||
| 284 | return 0; | ||
| 267 | if (elements_fit_in_base(fa)) | 285 | if (elements_fit_in_base(fa)) |
| 268 | return 0; | 286 | return 0; |
| 269 | start_part = fa_element_to_part_nr(fa, start); | 287 | start_part = fa_element_to_part_nr(fa, start); |
| @@ -291,19 +309,22 @@ EXPORT_SYMBOL(flex_array_prealloc); | |||
| 291 | */ | 309 | */ |
| 292 | void *flex_array_get(struct flex_array *fa, unsigned int element_nr) | 310 | void *flex_array_get(struct flex_array *fa, unsigned int element_nr) |
| 293 | { | 311 | { |
| 294 | int part_nr = fa_element_to_part_nr(fa, element_nr); | 312 | int part_nr = 0; |
| 295 | struct flex_array_part *part; | 313 | struct flex_array_part *part; |
| 296 | 314 | ||
| 315 | if (!fa->element_size) | ||
| 316 | return NULL; | ||
| 297 | if (element_nr >= fa->total_nr_elements) | 317 | if (element_nr >= fa->total_nr_elements) |
| 298 | return NULL; | 318 | return NULL; |
| 299 | if (elements_fit_in_base(fa)) | 319 | if (elements_fit_in_base(fa)) |
| 300 | part = (struct flex_array_part *)&fa->parts[0]; | 320 | part = (struct flex_array_part *)&fa->parts[0]; |
| 301 | else { | 321 | else { |
| 322 | part_nr = fa_element_to_part_nr(fa, element_nr); | ||
| 302 | part = fa->parts[part_nr]; | 323 | part = fa->parts[part_nr]; |
| 303 | if (!part) | 324 | if (!part) |
| 304 | return NULL; | 325 | return NULL; |
| 305 | } | 326 | } |
| 306 | return &part->elements[index_inside_part(fa, element_nr)]; | 327 | return &part->elements[index_inside_part(fa, element_nr, part_nr)]; |
| 307 | } | 328 | } |
| 308 | EXPORT_SYMBOL(flex_array_get); | 329 | EXPORT_SYMBOL(flex_array_get); |
| 309 | 330 | ||
| @@ -353,7 +374,7 @@ int flex_array_shrink(struct flex_array *fa) | |||
| 353 | int part_nr; | 374 | int part_nr; |
| 354 | int ret = 0; | 375 | int ret = 0; |
| 355 | 376 | ||
| 356 | if (!fa->total_nr_elements) | 377 | if (!fa->total_nr_elements || !fa->element_size) |
| 357 | return 0; | 378 | return 0; |
| 358 | if (elements_fit_in_base(fa)) | 379 | if (elements_fit_in_base(fa)) |
| 359 | return ret; | 380 | return ret; |
