4 #include <linux/types.h>
5 #include <linux/reciprocal_div.h>
8 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
9 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
11 struct flex_array_part
;
14 * This is meant to replace cases where an array-like
15 * structure has gotten too big to fit into kmalloc()
16 * and the developer is getting tempted to use
24 int total_nr_elements
;
26 struct reciprocal_value reciprocal_elems
;
27 struct flex_array_part
*parts
[];
30 * This little trick makes sure that
31 * sizeof(flex_array) == PAGE_SIZE
33 char padding
[FLEX_ARRAY_BASE_SIZE
];
37 /* Number of bytes left in base struct flex_array, excluding metadata */
38 #define FLEX_ARRAY_BASE_BYTES_LEFT \
39 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
41 /* Number of pointers in base to struct flex_array_part pages */
42 #define FLEX_ARRAY_NR_BASE_PTRS \
43 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
45 /* Number of elements of size that fit in struct flex_array_part */
46 #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
47 (FLEX_ARRAY_PART_SIZE / size)
50 * Defines a statically allocated flex array and ensures its parameters are
53 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
54 struct flex_array __arrayname = { { { \
55 .element_size = (__element_size), \
56 .total_nr_elements = (__total), \
58 static inline void __arrayname##_invalid_parameter(void) \
60 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
61 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
64 struct flex_array
*flex_array_alloc(int element_size
, unsigned int total
,
66 int flex_array_prealloc(struct flex_array
*fa
, unsigned int start
,
67 unsigned int nr_elements
, gfp_t flags
);
68 void flex_array_free(struct flex_array
*fa
);
69 void flex_array_free_parts(struct flex_array
*fa
);
70 int flex_array_put(struct flex_array
*fa
, unsigned int element_nr
, void *src
,
72 int flex_array_clear(struct flex_array
*fa
, unsigned int element_nr
);
73 void *flex_array_get(struct flex_array
*fa
, unsigned int element_nr
);
74 int flex_array_shrink(struct flex_array
*fa
);
76 #define flex_array_put_ptr(fa, nr, src, gfp) \
77 flex_array_put(fa, nr, (void *)&(src), gfp)
79 void *flex_array_get_ptr(struct flex_array
*fa
, unsigned int element_nr
);
81 #endif /* _FLEX_ARRAY_H */