blob: e557edbad21bd1caf2f6aa6a3ad1e29418d0eb0a [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001#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
13struct flex_array {
14 union {
15 struct {
16 int element_size;
17 int total_nr_elements;
18 int elems_per_part;
19 u32 reciprocal_elems;
20 struct flex_array_part *parts[];
21 };
22 char padding[FLEX_ARRAY_BASE_SIZE];
23 };
24};
25
26#define FLEX_ARRAY_BASE_BYTES_LEFT \
27 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
28
29#define FLEX_ARRAY_NR_BASE_PTRS \
30 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
31
32#define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
33 (FLEX_ARRAY_PART_SIZE / size)
34
35#define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
36 struct flex_array __arrayname = { { { \
37 .element_size = (__element_size), \
38 .total_nr_elements = (__total), \
39 } } }; \
40 static inline void __arrayname##_invalid_parameter(void) \
41 { \
42 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
43 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
44 }
45
46struct flex_array *flex_array_alloc(int element_size, unsigned int total,
47 gfp_t flags);
48int flex_array_prealloc(struct flex_array *fa, unsigned int start,
49 unsigned int nr_elements, gfp_t flags);
50void flex_array_free(struct flex_array *fa);
51void flex_array_free_parts(struct flex_array *fa);
52int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
53 gfp_t flags);
54int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
55void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
56int flex_array_shrink(struct flex_array *fa);
57
58#define flex_array_put_ptr(fa, nr, src, gfp) \
59 flex_array_put(fa, nr, (void *)&(src), gfp)
60
61void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
62
63#endif