datapath: Backport flex_arrays.
[sliver-openvswitch.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/types.h>
10 #include <asm/page.h>
11
12 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
13 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
14
15 struct flex_array_part;
16
17 /*
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
21  * vmalloc().
22  */
23
24 struct flex_array {
25         union {
26                 struct {
27                         int element_size;
28                         int total_nr_elements;
29                         int elems_per_part;
30                         u32 reciprocal_elems;
31                         struct flex_array_part *parts[];
32                 };
33                 /*
34                  * This little trick makes sure that
35                  * sizeof(flex_array) == PAGE_SIZE
36                  */
37                 char padding[FLEX_ARRAY_BASE_SIZE];
38         };
39 };
40
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))
44
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 *))
48
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)
52
53 /*
54  * Defines a statically allocated flex array and ensures its parameters are
55  * valid.
56  */
57 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total)         \
58         struct flex_array __arrayname = { { {                           \
59                 .element_size = (__element_size),                       \
60                 .total_nr_elements = (__total),                         \
61         } } };                                                          \
62         static inline void __arrayname##_invalid_parameter(void)        \
63         {                                                               \
64                 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS *      \
65                         FLEX_ARRAY_ELEMENTS_PER_PART(__element_size));  \
66         }
67
68 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
69                 gfp_t flags);
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,
75                 gfp_t flags);
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);
79
80 #define flex_array_put_ptr(fa, nr, src, gfp) \
81         flex_array_put(fa, nr, (void *)&(src), gfp)
82
83 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
84
85 #endif /* Linux version < 3.0.0 */
86 #endif /* __LINUX_FLEX_ARRAY_WRAPPER_H */