blob: f12401e485fe682c0f859c9a1d9cec99b2ec58b4 [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>
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 * This is meant to replace cases where an array-like
14 * structure has gotten too big to fit into kmalloc()
15 * and the developer is getting tempted to use
16 * vmalloc().
17 */
18
19struct flex_array {
20 union {
21 struct {
22 int element_size;
23 int total_nr_elements;
David Rientjes8e7ee2702009-08-26 14:29:21 -070024 struct flex_array_part *parts[];
Dave Hansen534acc02009-07-29 15:04:18 -070025 };
26 /*
27 * This little trick makes sure that
28 * sizeof(flex_array) == PAGE_SIZE
29 */
30 char padding[FLEX_ARRAY_BASE_SIZE];
31 };
32};
33
34#define FLEX_ARRAY_INIT(size, total) { { {\
35 .element_size = (size), \
36 .total_nr_elements = (total), \
37} } }
38
David Rientjesb62e4082009-08-26 14:29:22 -070039struct flex_array *flex_array_alloc(int element_size, unsigned int total,
40 gfp_t flags);
41int flex_array_prealloc(struct flex_array *fa, unsigned int start,
42 unsigned int end, gfp_t flags);
Dave Hansen534acc02009-07-29 15:04:18 -070043void flex_array_free(struct flex_array *fa);
44void flex_array_free_parts(struct flex_array *fa);
David Rientjesb62e4082009-08-26 14:29:22 -070045int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
Dave Hansen534acc02009-07-29 15:04:18 -070046 gfp_t flags);
David Rientjese6de3982009-09-21 17:04:30 -070047int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
David Rientjesb62e4082009-08-26 14:29:22 -070048void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
David Rientjes4af5a2f2009-09-21 17:04:31 -070049int flex_array_shrink(struct flex_array *fa);
Dave Hansen534acc02009-07-29 15:04:18 -070050
51#endif /* _FLEX_ARRAY_H */