d1e1b831c313da2baebd34e2c2015ac46f8c7c65
[cascardo/ovs.git] / datapath / linux / compat / include / linux / flex_array.h
1 #ifndef __LINUX_FLEX_ARRAY_WRAPPER_H
2 #define __LINUX_FLEX_ARRAY_WRAPPER_H
3
4 #include <linux/version.h>
5 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
6 #include_next <linux/flex_array.h>
7 #else
8
9 #include <linux/reciprocal_div.h>
10 #include <linux/types.h>
11 #include <asm/page.h>
12
13 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
14 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
15
16 struct flex_array_part;
17
18 /*
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
22  * vmalloc().
23  */
24
25 struct flex_array {
26         union {
27                 struct {
28                         int element_size;
29                         int total_nr_elements;
30                         int elems_per_part;
31                         struct reciprocal_value reciprocal_elems;
32                         struct flex_array_part *parts[];
33                 };
34                 /*
35                  * This little trick makes sure that
36                  * sizeof(flex_array) == PAGE_SIZE
37                  */
38                 char padding[FLEX_ARRAY_BASE_SIZE];
39         };
40 };
41
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))
45
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 *))
49
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)
53
54 /*
55  * Defines a statically allocated flex array and ensures its parameters are
56  * valid.
57  */
58 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total)         \
59         struct flex_array __arrayname = { { {                           \
60                 .element_size = (__element_size),                       \
61                 .total_nr_elements = (__total),                         \
62         } } };                                                          \
63         static inline void __arrayname##_invalid_parameter(void)        \
64         {                                                               \
65                 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS *      \
66                         FLEX_ARRAY_ELEMENTS_PER_PART(__element_size));  \
67         }
68
69 #define flex_array_alloc rpl_flex_array_alloc
70 struct flex_array *rpl_flex_array_alloc(int element_size, unsigned int total,
71                 gfp_t flags);
72
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);
76
77 #define flex_array_free rpl_flex_array_free
78 void rpl_flex_array_free(struct flex_array *fa);
79
80 #define flex_array_free_parts rpl_flex_array_free_parts
81 void rpl_flex_array_free_parts(struct flex_array *fa);
82
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,
85                 gfp_t flags);
86
87 #define flex_array_clear rpl_flex_array_clear
88 int rpl_flex_array_clear(struct flex_array *fa, unsigned int element_nr);
89
90 #define flex_array_get rpl_flex_array_get
91 void *rpl_flex_array_get(struct flex_array *fa, unsigned int element_nr);
92
93 #define flex_array_shrink rpl_flex_array_shrink
94 int rpl_flex_array_shrink(struct flex_array *fa);
95
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)
99
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);
102
103 #endif /* Linux version < 3.0.0 */
104 #endif /* __LINUX_FLEX_ARRAY_WRAPPER_H */