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 typedef void (*lttng_dynamic_array_element_destructor
)(void *element
);
14 typedef void (*lttng_dynamic_pointer_array_destructor
)(void *ptr
);
16 struct lttng_dynamic_array
{
17 struct lttng_dynamic_buffer buffer
;
20 lttng_dynamic_array_element_destructor destructor
;
23 struct lttng_dynamic_pointer_array
{
24 struct lttng_dynamic_array array
;
28 * Initialize a resizable array of fixed-size elements. This performs no
29 * allocation and can't fail.
32 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
34 lttng_dynamic_array_element_destructor destructor
);
37 * Returns the number of elements in the dynamic array.
40 size_t lttng_dynamic_array_get_count(
41 const struct lttng_dynamic_array
*array
)
47 * Returns a pointer to the element. Mutating operations on the array invalidate
48 * the returned pointer.
51 void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array
*array
,
54 LTTNG_ASSERT(element_index
< array
->size
);
55 return array
->buffer
.data
+ (element_index
* array
->element_size
);
59 * Set the array's element count to new_element_count. Any added element will
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.
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
70 * NOTE: It is striclty _invalid_ to access memory after _size_, regardless
71 * of prior calls to set_capacity().
74 int lttng_dynamic_array_set_count(struct lttng_dynamic_array
*array
,
75 size_t new_element_count
);
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.
81 * element is a pointer to the element to add (copy) to the array.
84 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
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
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. */
98 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
);
100 /* Remove all elements from the dynamic array. */
102 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
);
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.
112 * Initialize a resizable array of fixed-size elements. This performs no
113 * allocation and can't fail.
116 void lttng_dynamic_pointer_array_init(
117 struct lttng_dynamic_pointer_array
*array
,
118 lttng_dynamic_pointer_array_destructor destructor
);
121 * Returns the number of pointers in the dynamic pointer array.
124 size_t lttng_dynamic_pointer_array_get_count(
125 const struct lttng_dynamic_pointer_array
*array
)
127 return lttng_dynamic_array_get_count(&array
->array
);
131 * Returns the pointer at index `index`.
134 void *lttng_dynamic_pointer_array_get_pointer(
135 const struct lttng_dynamic_pointer_array
*array
, size_t index
)
137 void **element
= lttng_dynamic_array_get_element(&array
->array
, index
);
143 * Returns the pointer at index `index`, sets the array slot to NULL. Does not
144 * run the destructor.
148 void *lttng_dynamic_pointer_array_steal_pointer(
149 struct lttng_dynamic_pointer_array
*array
, size_t index
)
151 void **p_element
= lttng_dynamic_array_get_element(&array
->array
, index
);
152 void *element
= *p_element
;
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
165 int lttng_dynamic_pointer_array_add_pointer(
166 struct lttng_dynamic_pointer_array
*array
, void *pointer
)
168 return lttng_dynamic_array_add_element(&array
->array
, &pointer
);
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).
177 int lttng_dynamic_pointer_array_remove_pointer(
178 struct lttng_dynamic_pointer_array
*array
, size_t index
);
180 /* Release any memory used by the dynamic array. */
182 void lttng_dynamic_pointer_array_reset(
183 struct lttng_dynamic_pointer_array
*array
);
185 /* Remove all elements from the dynamic pointer array. */
187 void lttng_dynamic_pointer_array_clear(
188 struct lttng_dynamic_pointer_array
*array
);
190 #endif /* LTTNG_DYNAMIC_ARRAY_H */