1#ifndef _FLEX_ARRAY_H
2#define _FLEX_ARRAY_H
3
4#include <linux/types.h>
5#include <asm/page.h>
6
7#define FLEX_ARRAY_PART_SIZE PAGE_SIZE
8#define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
9
10struct flex_array_part;
11
12
13
14
15
16
17
18
19struct flex_array {
20 union {
21 struct {
22 int element_size;
23 int total_nr_elements;
24 int elems_per_part;
25 u32 reciprocal_elems;
26 struct flex_array_part *parts[];
27 };
28
29
30
31
32 char padding[FLEX_ARRAY_BASE_SIZE];
33 };
34};
35
36
37#define FLEX_ARRAY_BASE_BYTES_LEFT \
38 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
39
40
41#define FLEX_ARRAY_NR_BASE_PTRS \
42 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
43
44
45#define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
46 (FLEX_ARRAY_PART_SIZE / size)
47
48
49
50
51
52#define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
53 struct flex_array __arrayname = { { { \
54 .element_size = (__element_size), \
55 .total_nr_elements = (__total), \
56 } } }; \
57 static inline void __arrayname##_invalid_parameter(void) \
58 { \
59 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
60 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
61 }
62
63struct flex_array *flex_array_alloc(int element_size, unsigned int total,
64 gfp_t flags);
65int flex_array_prealloc(struct flex_array *fa, unsigned int start,
66 unsigned int nr_elements, gfp_t flags);
67void flex_array_free(struct flex_array *fa);
68void flex_array_free_parts(struct flex_array *fa);
69int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
70 gfp_t flags);
71int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
72void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
73int flex_array_shrink(struct flex_array *fa);
74
75#define flex_array_put_ptr(fa, nr, src, gfp) \
76 flex_array_put(fa, nr, (void *)&(src), gfp)
77
78void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
79
80#endif
81