1 #ifndef __LINUX_FLEX_ARRAY_WRAPPER_H
2 #define __LINUX_FLEX_ARRAY_WRAPPER_H
4 #include <linux/version.h>
5 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
6 #include_next <linux/flex_array.h>
9 #include <linux/reciprocal_div.h>
10 #include <linux/types.h>
13 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
14 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
16 struct flex_array_part;
19 * This is meant to replace cases where an array-like
20 * structure has gotten too big to fit into kmalloc()
21 * and the developer is getting tempted to use
29 int total_nr_elements;
31 struct reciprocal_value reciprocal_elems;
32 struct flex_array_part *parts[];
35 * This little trick makes sure that
36 * sizeof(flex_array) == PAGE_SIZE
38 char padding[FLEX_ARRAY_BASE_SIZE];
42 /* Number of bytes left in base struct flex_array, excluding metadata */
43 #define FLEX_ARRAY_BASE_BYTES_LEFT \
44 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
46 /* Number of pointers in base to struct flex_array_part pages */
47 #define FLEX_ARRAY_NR_BASE_PTRS \
48 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
50 /* Number of elements of size that fit in struct flex_array_part */
51 #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
52 (FLEX_ARRAY_PART_SIZE / size)
55 * Defines a statically allocated flex array and ensures its parameters are
58 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
59 struct flex_array __arrayname = { { { \
60 .element_size = (__element_size), \
61 .total_nr_elements = (__total), \
63 static inline void __arrayname##_invalid_parameter(void) \
65 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
66 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
69 #define flex_array_alloc rpl_flex_array_alloc
70 struct flex_array *rpl_flex_array_alloc(int element_size, unsigned int total,
73 #define flex_array_prealloc rpl_flex_array_prealloc
74 int rpl_flex_array_prealloc(struct flex_array *fa, unsigned int start,
75 unsigned int nr_elements, gfp_t flags);
77 #define flex_array_free rpl_flex_array_free
78 void rpl_flex_array_free(struct flex_array *fa);
80 #define flex_array_free_parts rpl_flex_array_free_parts
81 void rpl_flex_array_free_parts(struct flex_array *fa);
83 #define flex_array_put rpl_flex_array_put
84 int rpl_flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
87 #define flex_array_clear rpl_flex_array_clear
88 int rpl_flex_array_clear(struct flex_array *fa, unsigned int element_nr);
90 #define flex_array_get rpl_flex_array_get
91 void *rpl_flex_array_get(struct flex_array *fa, unsigned int element_nr);
93 #define flex_array_shrink rpl_flex_array_shrink
94 int rpl_flex_array_shrink(struct flex_array *fa);
96 #define flex_array_put_ptr rpl_flex_array_put_ptr
97 #define rpl_flex_array_put_ptr(fa, nr, src, gfp) \
98 flex_array_put(fa, nr, (void *)&(src), gfp)
100 #define flex_array_get_ptr rpl_flex_array_get_ptr
101 void *rpl_flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
103 #endif /* Linux version < 3.0.0 */
104 #endif /* __LINUX_FLEX_ARRAY_WRAPPER_H */