2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #ifndef LTTNG_DYNAMIC_ARRAY_H
9 #define LTTNG_DYNAMIC_ARRAY_H
11 #include <common/dynamic-buffer.h>
13 #if defined(__cplusplus)
17 typedef void (*lttng_dynamic_array_element_destructor
)(void *element
);
18 typedef void (*lttng_dynamic_pointer_array_destructor
)(void *ptr
);
20 struct lttng_dynamic_array
{
21 struct lttng_dynamic_buffer buffer
;
24 lttng_dynamic_array_element_destructor destructor
;
27 struct lttng_dynamic_pointer_array
{
28 struct lttng_dynamic_array array
;
32 * Initialize a resizable array of fixed-size elements. This performs no
33 * allocation and can't fail.
35 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
37 lttng_dynamic_array_element_destructor destructor
);
40 * Returns the number of elements in the dynamic array.
43 size_t lttng_dynamic_array_get_count(
44 const struct lttng_dynamic_array
*array
)
50 * Returns a pointer to the element. Mutating operations on the array invalidate
51 * the returned pointer.
54 void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array
*array
,
57 LTTNG_ASSERT(element_index
< array
->size
);
58 return array
->buffer
.data
+ (element_index
* array
->element_size
);
62 * Set the array's element count to new_element_count. Any added element will
65 * Be careful to expand the array's element count _before_ calling out external
66 * APIs (e.g. read(3)) which may populate the buffer as setting the element
67 * count after will zero-out the result of the operation.
69 * Shrinking an array does not zero the old content. If the buffer may contain
70 * sensititve information, it must be cleared manually _before_ changing the
73 * NOTE: It is striclty _invalid_ to access memory after _size_, regardless
74 * of prior calls to set_capacity().
76 int lttng_dynamic_array_set_count(struct lttng_dynamic_array
*array
,
77 size_t new_element_count
);
80 * Add an element to the end of a dynamic array. The array's element count is
81 * increased by one and its underlying capacity is adjusted automatically.
83 * element is a pointer to the element to add (copy) to the array.
85 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
89 * Remove an element from the dynamic array. The array's element count is
90 * decreased by one and the following elements are shifted to take its place
93 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
94 size_t element_index
);
96 /* Release any memory used by the dynamic array. */
97 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
);
99 /* Remove all elements from the dynamic array. */
100 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
);
103 * Specialization of lttng_dynamic_array for pointers. This utility
104 * is built under the assumption that pointer sizes are equal
105 * for all data types on supported architectures. Revisit this in the event
106 * of a port to an Harvard architecture.
110 * Initialize a resizable array of fixed-size elements. This performs no
111 * allocation and can't fail.
113 void lttng_dynamic_pointer_array_init(
114 struct lttng_dynamic_pointer_array
*array
,
115 lttng_dynamic_pointer_array_destructor destructor
);
118 * Returns the number of pointers in the dynamic pointer array.
121 size_t lttng_dynamic_pointer_array_get_count(
122 const struct lttng_dynamic_pointer_array
*array
)
124 return lttng_dynamic_array_get_count(&array
->array
);
128 * Returns the pointer at index `index`.
131 void *lttng_dynamic_pointer_array_get_pointer(
132 const struct lttng_dynamic_pointer_array
*array
, size_t index
)
134 void **element
= (void **) lttng_dynamic_array_get_element(&array
->array
, index
);
140 * Returns the pointer at index `index`, sets the array slot to NULL. Does not
141 * run the destructor.
145 void *lttng_dynamic_pointer_array_steal_pointer(
146 struct lttng_dynamic_pointer_array
*array
, size_t index
)
148 void **p_element
= (void **) lttng_dynamic_array_get_element(&array
->array
, index
);
149 void *element
= *p_element
;
157 * Add a pointer to the end of a dynamic pointer array. The array's element
158 * count is increased by one and its underlying capacity is adjusted
162 int lttng_dynamic_pointer_array_add_pointer(
163 struct lttng_dynamic_pointer_array
*array
, void *pointer
)
165 return lttng_dynamic_array_add_element(&array
->array
, &pointer
);
169 * Remove a pointer from a dynamic pointer array. The array's element
170 * count is decreased by one and the following pointers are shifted to
171 * take the place of the removed pointer (if applicable).
173 int lttng_dynamic_pointer_array_remove_pointer(
174 struct lttng_dynamic_pointer_array
*array
, size_t index
);
176 /* Release any memory used by the dynamic array. */
177 void lttng_dynamic_pointer_array_reset(
178 struct lttng_dynamic_pointer_array
*array
);
180 /* Remove all elements from the dynamic pointer array. */
181 void lttng_dynamic_pointer_array_clear(
182 struct lttng_dynamic_pointer_array
*array
);
184 #if defined(__cplusplus)
188 #endif /* LTTNG_DYNAMIC_ARRAY_H */