Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / include / linux / flex_array.h
blobb94fa61b51fb30d290b357e18dce124068679afb
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FLEX_ARRAY_H
3 #define _FLEX_ARRAY_H
5 #include <linux/types.h>
6 #include <linux/reciprocal_div.h>
7 #include <asm/page.h>
9 #define FLEX_ARRAY_PART_SIZE PAGE_SIZE
10 #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
12 struct flex_array_part;
15 * This is meant to replace cases where an array-like
16 * structure has gotten too big to fit into kmalloc()
17 * and the developer is getting tempted to use
18 * vmalloc().
21 struct flex_array {
22 union {
23 struct {
24 int element_size;
25 int total_nr_elements;
26 int elems_per_part;
27 struct reciprocal_value reciprocal_elems;
28 struct flex_array_part *parts[];
31 * This little trick makes sure that
32 * sizeof(flex_array) == PAGE_SIZE
34 char padding[FLEX_ARRAY_BASE_SIZE];
38 /* Number of bytes left in base struct flex_array, excluding metadata */
39 #define FLEX_ARRAY_BASE_BYTES_LEFT \
40 (FLEX_ARRAY_BASE_SIZE - offsetof(struct flex_array, parts))
42 /* Number of pointers in base to struct flex_array_part pages */
43 #define FLEX_ARRAY_NR_BASE_PTRS \
44 (FLEX_ARRAY_BASE_BYTES_LEFT / sizeof(struct flex_array_part *))
46 /* Number of elements of size that fit in struct flex_array_part */
47 #define FLEX_ARRAY_ELEMENTS_PER_PART(size) \
48 (FLEX_ARRAY_PART_SIZE / size)
51 * Defines a statically allocated flex array and ensures its parameters are
52 * valid.
54 #define DEFINE_FLEX_ARRAY(__arrayname, __element_size, __total) \
55 struct flex_array __arrayname = { { { \
56 .element_size = (__element_size), \
57 .total_nr_elements = (__total), \
58 } } }; \
59 static inline void __arrayname##_invalid_parameter(void) \
60 { \
61 BUILD_BUG_ON((__total) > FLEX_ARRAY_NR_BASE_PTRS * \
62 FLEX_ARRAY_ELEMENTS_PER_PART(__element_size)); \
65 /**
66 * flex_array_alloc() - Creates a flexible array.
67 * @element_size: individual object size.
68 * @total: maximum number of objects which can be stored.
69 * @flags: GFP flags
71 * Return: Returns an object of structure flex_array.
73 struct flex_array *flex_array_alloc(int element_size, unsigned int total,
74 gfp_t flags);
76 /**
77 * flex_array_prealloc() - Ensures that memory for the elements indexed in the
78 * range defined by start and nr_elements has been allocated.
79 * @fa: array to allocate memory to.
80 * @start: start address
81 * @nr_elements: number of elements to be allocated.
82 * @flags: GFP flags
85 int flex_array_prealloc(struct flex_array *fa, unsigned int start,
86 unsigned int nr_elements, gfp_t flags);
88 /**
89 * flex_array_free() - Removes all elements of a flexible array.
90 * @fa: array to be freed.
92 void flex_array_free(struct flex_array *fa);
94 /**
95 * flex_array_free_parts() - Removes all elements of a flexible array, but
96 * leaves the array itself in place.
97 * @fa: array to be emptied.
99 void flex_array_free_parts(struct flex_array *fa);
102 * flex_array_put() - Stores data into a flexible array.
103 * @fa: array where element is to be stored.
104 * @element_nr: position to copy, must be less than the maximum specified when
105 * the array was created.
106 * @src: data source to be copied into the array.
107 * @flags: GFP flags
109 * Return: Returns zero on success, a negative error code otherwise.
111 int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
112 gfp_t flags);
115 * flex_array_clear() - Clears an individual element in the array, sets the
116 * given element to FLEX_ARRAY_FREE.
117 * @element_nr: element position to clear.
118 * @fa: array to which element to be cleared belongs.
120 * Return: Returns zero on success, -EINVAL otherwise.
122 int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
125 * flex_array_get() - Retrieves data into a flexible array.
127 * @element_nr: Element position to retrieve data from.
128 * @fa: array from which data is to be retrieved.
130 * Return: Returns a pointer to the data element, or NULL if that
131 * particular element has never been allocated.
133 void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
136 * flex_array_shrink() - Reduces the allocated size of an array.
137 * @fa: array to shrink.
139 * Return: Returns number of pages of memory actually freed.
142 int flex_array_shrink(struct flex_array *fa);
144 #define flex_array_put_ptr(fa, nr, src, gfp) \
145 flex_array_put(fa, nr, (void *)&(src), gfp)
147 void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
149 #endif /* _FLEX_ARRAY_H */