Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 2 | #ifndef _FLEX_ARRAY_H |
| 3 | #define _FLEX_ARRAY_H |
| 4 | |
| 5 | #include <linux/types.h> |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 6 | #include <linux/reciprocal_div.h> |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 7 | #include <asm/page.h> |
| 8 | |
| 9 | #define FLEX_ARRAY_PART_SIZE PAGE_SIZE |
| 10 | #define FLEX_ARRAY_BASE_SIZE PAGE_SIZE |
| 11 | |
| 12 | struct 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 | |
| 21 | struct flex_array { |
| 22 | union { |
| 23 | struct { |
| 24 | int element_size; |
| 25 | int total_nr_elements; |
Jesse Gross | 704f15d | 2011-05-26 16:25:02 -0700 | [diff] [blame] | 26 | int elems_per_part; |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 27 | struct reciprocal_value reciprocal_elems; |
David Rientjes | 8e7ee270 | 2009-08-26 14:29:21 -0700 | [diff] [blame] | 28 | struct flex_array_part *parts[]; |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 29 | }; |
| 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 Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 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)) |
| 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 Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 64 | |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 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 |
| 70 | * |
| 71 | * Return: Returns an object of structure flex_array. |
| 72 | */ |
David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 73 | struct flex_array *flex_array_alloc(int element_size, unsigned int total, |
| 74 | gfp_t flags); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 75 | |
| 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 Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 85 | int flex_array_prealloc(struct flex_array *fa, unsigned int start, |
Eric Paris | 5d30b10 | 2011-04-28 15:55:52 -0400 | [diff] [blame] | 86 | unsigned int nr_elements, gfp_t flags); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * flex_array_free() - Removes all elements of a flexible array. |
| 90 | * @fa: array to be freed. |
| 91 | */ |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 92 | void flex_array_free(struct flex_array *fa); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 93 | |
| 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 Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 99 | void flex_array_free_parts(struct flex_array *fa); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 100 | |
| 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 Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 111 | int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 112 | gfp_t flags); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 113 | |
| 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 Rientjes | e6de398 | 2009-09-21 17:04:30 -0700 | [diff] [blame] | 122 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 123 | |
| 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 Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 133 | void *flex_array_get(struct flex_array *fa, unsigned int element_nr); |
sayli karnik | b2e3353 | 2017-03-30 02:01:16 +0530 | [diff] [blame] | 134 | |
| 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 Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 142 | int flex_array_shrink(struct flex_array *fa); |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 143 | |
Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 144 | #define flex_array_put_ptr(fa, nr, src, gfp) \ |
Eric Paris | c41ab6a | 2010-11-29 15:47:09 -0500 | [diff] [blame] | 145 | flex_array_put(fa, nr, (void *)&(src), gfp) |
Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 146 | |
| 147 | void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr); |
| 148 | |
Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 149 | #endif /* _FLEX_ARRAY_H */ |