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 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
71 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
72 unsigned int nr_elements, gfp_t flags);
73 void flex_array_free(struct flex_array *fa);
74 void flex_array_free_parts(struct flex_array *fa);
75 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
77 int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
78 void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
79 int flex_array_shrink(struct flex_array *fa);
81 #define flex_array_put_ptr(fa, nr, src, gfp) \
82 flex_array_put(fa, nr, (void *)&(src), gfp)
84 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
86 #endif /* Linux version < 3.0.0 */
87 #endif /* __LINUX_FLEX_ARRAY_WRAPPER_H */