Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 1 | #ifndef _FLEX_ARRAY_H |
| 2 | #define _FLEX_ARRAY_H |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <asm/page.h> |
| 6 | |
| 7 | #define FLEX_ARRAY_PART_SIZE PAGE_SIZE |
| 8 | #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE |
| 9 | |
| 10 | struct flex_array_part; |
| 11 | |
| 12 | /* |
| 13 | * This is meant to replace cases where an array-like |
| 14 | * structure has gotten too big to fit into kmalloc() |
| 15 | * and the developer is getting tempted to use |
| 16 | * vmalloc(). |
| 17 | */ |
| 18 | |
| 19 | struct flex_array { |
| 20 | union { |
| 21 | struct { |
| 22 | int element_size; |
| 23 | int total_nr_elements; |
Jesse Gross | 704f15d | 2011-05-26 16:25:02 -0700 | [diff] [blame] | 24 | int elems_per_part; |
| 25 | u32 reciprocal_elems; |
David Rientjes | 8e7ee270 | 2009-08-26 14:29:21 -0700 | [diff] [blame] | 26 | struct flex_array_part *parts[]; |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 27 | }; |
| 28 | /* |
| 29 | * This little trick makes sure that |
| 30 | * sizeof(flex_array) == PAGE_SIZE |
| 31 | */ |
| 32 | char padding[FLEX_ARRAY_BASE_SIZE]; |
| 33 | }; |
| 34 | }; |
| 35 | |
David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 36 | /* Number of bytes left in base struct flex_array, excluding metadata */ |
| 37 | #define FLEX_ARRAY_BASE_BYTES_LEFT \ |
| 38 | (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts)) |
| 39 | |
| 40 | /* Number of pointers in base to struct flex_array_part pages */ |
| 41 | #define FLEX_ARRAY_NR_BASE_PTRS \ |
| 42 | (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *)) |
| 43 | |
| 44 | /* Number of elements of size that fit in struct flex_array_part */ |
| 45 | #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \ |
| 46 | (FLEX_ARRAY_PART_SIZE / size) |
| 47 | |
| 48 | /* |
| 49 | * Defines a statically allocated flex array and ensures its parameters are |
| 50 | * valid. |
| 51 | */ |
| 52 | #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \ |
| 53 | struct flex_array __arrayname = { { { \ |
| 54 | .element_size = (__element_size), \ |
| 55 | .total_nr_elements = (__total), \ |
| 56 | } } }; \ |
| 57 | static inline void __arrayname##_invalid_parameter(void) \ |
| 58 | { \ |
| 59 | BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \ |
| 60 | FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \ |
| 61 | } |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 62 | |
David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 63 | struct flex_array *flex_array_alloc(int element_size, unsigned int total, |
| 64 | gfp_t flags); |
| 65 | int flex_array_prealloc(struct flex_array *fa, unsigned int start, |
Eric Paris | 5d30b10 | 2011-04-28 15:55:52 -0400 | [diff] [blame] | 66 | unsigned int nr_elements, gfp_t flags); |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 67 | void flex_array_free(struct flex_array *fa); |
| 68 | void flex_array_free_parts(struct flex_array *fa); |
David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 69 | int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 70 | gfp_t flags); |
David Rientjes | e6de398 | 2009-09-21 17:04:30 -0700 | [diff] [blame] | 71 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr); |
David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 72 | void *flex_array_get(struct flex_array *fa, unsigned int element_nr); |
David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 73 | int flex_array_shrink(struct flex_array *fa); |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 74 | |
Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 75 | #define flex_array_put_ptr(fa, nr, src, gfp) \ |
Eric Paris | c41ab6a | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 76 | flex_array_put(fa, nr, (void *)&(src), gfp) |
Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 77 | |
| 78 | void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr); |
| 79 | |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 80 | #endif /* _FLEX_ARRAY_H */ |