blob: b6efb0c64408b35b7874726ca22c72f2e5fe21f3 [file] [log] [blame]
Dave Hansen534acc02009-07-29 15:04:18 -07001#ifndef _FLEX_ARRAY_H
2#define _FLEX_ARRAY_H
3
4#include <linux/types.h>
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005#include <linux/reciprocal_div.h>
Dave Hansen534acc02009-07-29 15:04:18 -07006#include <asm/page.h>
7
8#define FLEX_ARRAY_PART_SIZE PAGE_SIZE
9#define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
10
11struct flex_array_part;
12
13/*
14 * This is meant to replace cases where an array-like
15 * structure has gotten too big to fit into kmalloc()
16 * and the developer is getting tempted to use
17 * vmalloc().
18 */
19
20struct flex_array {
21 union {
22 struct {
23 int element_size;
24 int total_nr_elements;
Jesse Gross704f15d2011-05-26 16:25:02 -070025 int elems_per_part;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010026 struct reciprocal_value reciprocal_elems;
David Rientjes8e7ee2702009-08-26 14:29:21 -070027 struct flex_array_part *parts[];
Dave Hansen534acc02009-07-29 15:04:18 -070028 };
29 /*
30 * This little trick makes sure that
31 * sizeof(flex_array) == PAGE_SIZE
32 */
33 char padding[FLEX_ARRAY_BASE_SIZE];
34 };
35};
36
David Rientjes45b588d2009-09-21 17:04:33 -070037/* Number of bytes left in base struct flex_array, excluding metadata */
38#define FLEX_ARRAY_BASE_BYTES_LEFT \
39 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
40
41/* Number of pointers in base to struct flex_array_part pages */
42#define FLEX_ARRAY_NR_BASE_PTRS \
43 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
44
45/* Number of elements of size that fit in struct flex_array_part */
46#define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
47 (FLEX_ARRAY_PART_SIZE / size)
48
49/*
50 * Defines a statically allocated flex array and ensures its parameters are
51 * valid.
52 */
53#define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
54 struct flex_array __arrayname = { { { \
55 .element_size = (__element_size), \
56 .total_nr_elements = (__total), \
57 } } }; \
58 static inline void __arrayname##_invalid_parameter(void) \
59 { \
60 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
61 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
62 }
Dave Hansen534acc02009-07-29 15:04:18 -070063
David Rientjesb62e4082009-08-26 14:29:22 -070064struct flex_array *flex_array_alloc(int element_size, unsigned int total,
65 gfp_t flags);
66int flex_array_prealloc(struct flex_array *fa, unsigned int start,
Eric Paris5d30b102011-04-28 15:55:52 -040067 unsigned int nr_elements, gfp_t flags);
Dave Hansen534acc02009-07-29 15:04:18 -070068void flex_array_free(struct flex_array *fa);
69void flex_array_free_parts(struct flex_array *fa);
David Rientjesb62e4082009-08-26 14:29:22 -070070int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
Dave Hansen534acc02009-07-29 15:04:18 -070071 gfp_t flags);
David Rientjese6de3982009-09-21 17:04:30 -070072int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
David Rientjesb62e4082009-08-26 14:29:22 -070073void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
David Rientjes4af5a2f2009-09-21 17:04:31 -070074int flex_array_shrink(struct flex_array *fa);
Dave Hansen534acc02009-07-29 15:04:18 -070075
Eric Parisea98eed2010-08-09 17:20:56 -070076#define flex_array_put_ptr(fa, nr, src, gfp) \
Eric Parisc41ab6a2010-11-29 15:47:09 -050077 flex_array_put(fa, nr, (void *)&(src), gfp)
Eric Parisea98eed2010-08-09 17:20:56 -070078
79void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
80
Dave Hansen534acc02009-07-29 15:04:18 -070081#endif /* _FLEX_ARRAY_H */