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/types.h>
12 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
13 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
15 struct flex_array_part;
18 * This is meant to replace cases where an array-like
19 * structure has gotten too big to fit into kmalloc()
20 * and the developer is getting tempted to use
28 int total_nr_elements;
31 struct flex_array_part *parts[];
34 * This little trick makes sure that
35 * sizeof(flex_array) == PAGE_SIZE
37 char padding[FLEX_ARRAY_BASE_SIZE];
41 /* Number of bytes left in base struct flex_array, excluding metadata */
42 #define FLEX_ARRAY_BASE_BYTES_LEFT \
43 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
45 /* Number of pointers in base to struct flex_array_part pages */
46 #define FLEX_ARRAY_NR_BASE_PTRS \
47 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
49 /* Number of elements of size that fit in struct flex_array_part */
50 #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
51 (FLEX_ARRAY_PART_SIZE / size)
54 * Defines a statically allocated flex array and ensures its parameters are
57 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
58 struct flex_array __arrayname = { { { \
59 .element_size = (__element_size), \
60 .total_nr_elements = (__total), \
62 static inline void __arrayname##_invalid_parameter(void) \
64 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
65 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
68 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
70 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
71 unsigned int nr_elements, gfp_t flags);
72 void flex_array_free(struct flex_array *fa);
73 void flex_array_free_parts(struct flex_array *fa);
74 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
76 int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
77 void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
78 int flex_array_shrink(struct flex_array *fa);
80 #define flex_array_put_ptr(fa, nr, src, gfp) \
81 flex_array_put(fa, nr, (void *)&(src), gfp)
83 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
85 #endif /* Linux version < 3.0.0 */
86 #endif /* __LINUX_FLEX_ARRAY_WRAPPER_H */