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