blob: b94fa61b51fb30d290b357e18dce124068679afb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dave Hansen534acc02009-07-29 15:04:18 -07002#ifndef _FLEX_ARRAY_H
3#define _FLEX_ARRAY_H
4
5#include <linux/types.h>
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01006#include <linux/reciprocal_div.h>
Dave Hansen534acc02009-07-29 15:04:18 -07007#include <asm/page.h>
8
9#define FLEX_ARRAY_PART_SIZE PAGE_SIZE
10#define FLEX_ARRAY_BASE_SIZE PAGE_SIZE
11
12struct flex_array_part;
13
14/*
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().
19 */
20
21struct flex_array {
22 union {
23 struct {
24 int element_size;
25 int total_nr_elements;
Jesse Gross704f15d2011-05-26 16:25:02 -070026 int elems_per_part;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010027 struct reciprocal_value reciprocal_elems;
David Rientjes8e7ee2702009-08-26 14:29:21 -070028 struct flex_array_part *parts[];
Dave Hansen534acc02009-07-29 15:04:18 -070029 };
30 /*
31 * This little trick makes sure that
32 * sizeof(flex_array) == PAGE_SIZE
33 */
34 char padding[FLEX_ARRAY_BASE_SIZE];
35 };
36};
37
David Rientjes45b588d2009-09-21 17:04:33 -070038/* 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))
41
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 *))
45
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)
49
50/*
51 * Defines a statically allocated flex array and ensures its parameters are
52 * valid.
53 */
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)); \
63 }
Dave Hansen534acc02009-07-29 15:04:18 -070064
sayli karnikb2e33532017-03-30 02:01:16 +053065/**
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
70 *
71 * Return: Returns an object of structure flex_array.
72 */
David Rientjesb62e4082009-08-26 14:29:22 -070073struct flex_array *flex_array_alloc(int element_size, unsigned int total,
74 gfp_t flags);
sayli karnikb2e33532017-03-30 02:01:16 +053075
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
83 *
84 */
David Rientjesb62e4082009-08-26 14:29:22 -070085int flex_array_prealloc(struct flex_array *fa, unsigned int start,
Eric Paris5d30b102011-04-28 15:55:52 -040086 unsigned int nr_elements, gfp_t flags);
sayli karnikb2e33532017-03-30 02:01:16 +053087
88/**
89 * flex_array_free() - Removes all elements of a flexible array.
90 * @fa: array to be freed.
91 */
Dave Hansen534acc02009-07-29 15:04:18 -070092void flex_array_free(struct flex_array *fa);
sayli karnikb2e33532017-03-30 02:01:16 +053093
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.
98 */
Dave Hansen534acc02009-07-29 15:04:18 -070099void flex_array_free_parts(struct flex_array *fa);
sayli karnikb2e33532017-03-30 02:01:16 +0530100
101/**
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
108 *
109 * Return: Returns zero on success, a negative error code otherwise.
110 */
David Rientjesb62e4082009-08-26 14:29:22 -0700111int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
Dave Hansen534acc02009-07-29 15:04:18 -0700112 gfp_t flags);
sayli karnikb2e33532017-03-30 02:01:16 +0530113
114/**
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.
119 *
120 * Return: Returns zero on success, -EINVAL otherwise.
121 */
David Rientjese6de3982009-09-21 17:04:30 -0700122int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
sayli karnikb2e33532017-03-30 02:01:16 +0530123
124/**
125 * flex_array_get() - Retrieves data into a flexible array.
126 *
127 * @element_nr: Element position to retrieve data from.
128 * @fa: array from which data is to be retrieved.
129 *
130 * Return: Returns a pointer to the data element, or NULL if that
131 * particular element has never been allocated.
132 */
David Rientjesb62e4082009-08-26 14:29:22 -0700133void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
sayli karnikb2e33532017-03-30 02:01:16 +0530134
135/**
136 * flex_array_shrink() - Reduces the allocated size of an array.
137 * @fa: array to shrink.
138 *
139 * Return: Returns number of pages of memory actually freed.
140 *
141 */
David Rientjes4af5a2f2009-09-21 17:04:31 -0700142int flex_array_shrink(struct flex_array *fa);
Dave Hansen534acc02009-07-29 15:04:18 -0700143
Eric Parisea98eed2010-08-09 17:20:56 -0700144#define flex_array_put_ptr(fa, nr, src, gfp) \
Eric Parisc41ab6a2010-11-29 15:47:09 -0500145 flex_array_put(fa, nr, (void *)&(src), gfp)
Eric Parisea98eed2010-08-09 17:20:56 -0700146
147void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
148
Dave Hansen534acc02009-07-29 15:04:18 -0700149#endif /* _FLEX_ARRAY_H */