Commit | Line | Data |
---|---|---|
2c5ff4e4 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
2c5ff4e4 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
2c5ff4e4 | 5 | * |
2c5ff4e4 JG |
6 | */ |
7 | ||
8 | #ifndef LTTNG_DYNAMIC_ARRAY_H | |
9 | #define LTTNG_DYNAMIC_ARRAY_H | |
10 | ||
11 | #include <common/dynamic-buffer.h> | |
2c5ff4e4 | 12 | |
93bed9fe JG |
13 | typedef void (*lttng_dynamic_array_element_destructor)(void *element); |
14 | typedef void (*lttng_dynamic_pointer_array_destructor)(void *ptr); | |
15 | ||
2c5ff4e4 JG |
16 | struct lttng_dynamic_array { |
17 | struct lttng_dynamic_buffer buffer; | |
18 | size_t element_size; | |
19 | size_t size; | |
93bed9fe | 20 | lttng_dynamic_array_element_destructor destructor; |
2c5ff4e4 JG |
21 | }; |
22 | ||
23 | struct lttng_dynamic_pointer_array { | |
24 | struct lttng_dynamic_array array; | |
25 | }; | |
26 | ||
2c5ff4e4 JG |
27 | /* |
28 | * Initialize a resizable array of fixed-size elements. This performs no | |
29 | * allocation and can't fail. | |
30 | */ | |
31 | LTTNG_HIDDEN | |
32 | void lttng_dynamic_array_init(struct lttng_dynamic_array *array, | |
93bed9fe JG |
33 | size_t element_size, |
34 | lttng_dynamic_array_element_destructor destructor); | |
2c5ff4e4 JG |
35 | |
36 | /* | |
37 | * Returns the number of elements in the dynamic array. | |
38 | */ | |
39 | static inline | |
40 | size_t lttng_dynamic_array_get_count( | |
41 | const struct lttng_dynamic_array *array) | |
42 | { | |
43 | return array->size; | |
44 | } | |
45 | ||
46 | /* | |
47 | * Returns a pointer to the element. Mutating operations on the array invalidate | |
48 | * the returned pointer. | |
49 | */ | |
50 | static inline | |
51 | void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array *array, | |
52 | size_t element_index) | |
53 | { | |
a0377dfe | 54 | LTTNG_ASSERT(element_index < array->size); |
2c5ff4e4 JG |
55 | return array->buffer.data + (element_index * array->element_size); |
56 | } | |
57 | ||
5fe3e097 JG |
58 | /* |
59 | * Set the array's element count to new_element_count. Any added element will | |
60 | * be zeroed. | |
61 | * | |
62 | * Be careful to expand the array's element count _before_ calling out external | |
63 | * APIs (e.g. read(3)) which may populate the buffer as setting the element | |
64 | * count after will zero-out the result of the operation. | |
65 | * | |
66 | * Shrinking an array does not zero the old content. If the buffer may contain | |
67 | * sensititve information, it must be cleared manually _before_ changing the | |
68 | * size. | |
69 | * | |
70 | * NOTE: It is striclty _invalid_ to access memory after _size_, regardless | |
71 | * of prior calls to set_capacity(). | |
72 | */ | |
73 | LTTNG_HIDDEN | |
74 | int lttng_dynamic_array_set_count(struct lttng_dynamic_array *array, | |
75 | size_t new_element_count); | |
76 | ||
2c5ff4e4 JG |
77 | /* |
78 | * Add an element to the end of a dynamic array. The array's element count is | |
79 | * increased by one and its underlying capacity is adjusted automatically. | |
80 | * | |
81 | * element is a pointer to the element to add (copy) to the array. | |
82 | */ | |
83 | LTTNG_HIDDEN | |
84 | int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, | |
85 | const void *element); | |
86 | ||
93bed9fe JG |
87 | /* |
88 | * Remove an element from the dynamic array. The array's element count is | |
89 | * decreased by one and the following elements are shifted to take its place | |
90 | * (when applicable). | |
91 | */ | |
92 | LTTNG_HIDDEN | |
93 | int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array, | |
94 | size_t element_index); | |
95 | ||
2c5ff4e4 JG |
96 | /* Release any memory used by the dynamic array. */ |
97 | LTTNG_HIDDEN | |
93bed9fe | 98 | void lttng_dynamic_array_reset(struct lttng_dynamic_array *array); |
2c5ff4e4 | 99 | |
74465ffb MD |
100 | /* Remove all elements from the dynamic array. */ |
101 | LTTNG_HIDDEN | |
102 | void lttng_dynamic_array_clear(struct lttng_dynamic_array *array); | |
2c5ff4e4 JG |
103 | |
104 | /* | |
105 | * Specialization of lttng_dynamic_array for pointers. This utility | |
106 | * is built under the assumption that pointer sizes are equal | |
107 | * for all data types on supported architectures. Revisit this in the event | |
108 | * of a port to an Harvard architecture. | |
109 | */ | |
110 | ||
111 | /* | |
112 | * Initialize a resizable array of fixed-size elements. This performs no | |
113 | * allocation and can't fail. | |
114 | */ | |
115 | LTTNG_HIDDEN | |
116 | void lttng_dynamic_pointer_array_init( | |
93bed9fe JG |
117 | struct lttng_dynamic_pointer_array *array, |
118 | lttng_dynamic_pointer_array_destructor destructor); | |
2c5ff4e4 JG |
119 | |
120 | /* | |
121 | * Returns the number of pointers in the dynamic pointer array. | |
122 | */ | |
123 | static inline | |
124 | size_t lttng_dynamic_pointer_array_get_count( | |
125 | const struct lttng_dynamic_pointer_array *array) | |
126 | { | |
127 | return lttng_dynamic_array_get_count(&array->array); | |
128 | } | |
129 | ||
130 | /* | |
14c4262b | 131 | * Returns the pointer at index `index`. |
2c5ff4e4 JG |
132 | */ |
133 | static inline | |
134 | void *lttng_dynamic_pointer_array_get_pointer( | |
135 | const struct lttng_dynamic_pointer_array *array, size_t index) | |
136 | { | |
137 | void **element = lttng_dynamic_array_get_element(&array->array, index); | |
138 | ||
139 | return *element; | |
140 | } | |
141 | ||
4624dad0 SM |
142 | /* |
143 | * Returns the pointer at index `index`, sets the array slot to NULL. Does not | |
144 | * run the destructor. | |
145 | */ | |
146 | ||
147 | static inline | |
148 | void *lttng_dynamic_pointer_array_steal_pointer( | |
149 | struct lttng_dynamic_pointer_array *array, size_t index) | |
150 | { | |
151 | void **p_element = lttng_dynamic_array_get_element(&array->array, index); | |
152 | void *element = *p_element; | |
153 | ||
154 | *p_element = NULL; | |
155 | ||
156 | return element; | |
157 | } | |
158 | ||
2c5ff4e4 JG |
159 | /* |
160 | * Add a pointer to the end of a dynamic pointer array. The array's element | |
161 | * count is increased by one and its underlying capacity is adjusted | |
162 | * automatically. | |
163 | */ | |
164 | static inline | |
165 | int lttng_dynamic_pointer_array_add_pointer( | |
166 | struct lttng_dynamic_pointer_array *array, void *pointer) | |
167 | { | |
168 | return lttng_dynamic_array_add_element(&array->array, &pointer); | |
169 | } | |
170 | ||
93bed9fe JG |
171 | /* |
172 | * Remove a pointer from a dynamic pointer array. The array's element | |
173 | * count is decreased by one and the following pointers are shifted to | |
174 | * take the place of the removed pointer (if applicable). | |
175 | */ | |
0186592a | 176 | LTTNG_HIDDEN |
93bed9fe | 177 | int lttng_dynamic_pointer_array_remove_pointer( |
0186592a | 178 | struct lttng_dynamic_pointer_array *array, size_t index); |
93bed9fe | 179 | |
2c5ff4e4 JG |
180 | /* Release any memory used by the dynamic array. */ |
181 | LTTNG_HIDDEN | |
182 | void lttng_dynamic_pointer_array_reset( | |
93bed9fe | 183 | struct lttng_dynamic_pointer_array *array); |
2c5ff4e4 | 184 | |
74465ffb MD |
185 | /* Remove all elements from the dynamic pointer array. */ |
186 | LTTNG_HIDDEN | |
187 | void lttng_dynamic_pointer_array_clear( | |
188 | struct lttng_dynamic_pointer_array *array); | |
189 | ||
2c5ff4e4 | 190 | #endif /* LTTNG_DYNAMIC_ARRAY_H */ |