2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/dynamic-array.hpp>
10 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
12 lttng_dynamic_array_element_destructor destructor
)
14 lttng_dynamic_buffer_init(&array
->buffer
);
15 array
->element_size
= element_size
;
17 array
->destructor
= destructor
;
20 int lttng_dynamic_array_set_count(struct lttng_dynamic_array
*array
, size_t new_element_count
)
29 if (array
->destructor
) {
32 for (i
= new_element_count
; i
< array
->size
; i
++) {
33 void *element
= lttng_dynamic_array_get_element(array
, i
);
35 array
->destructor(element
);
39 array
->size
= new_element_count
;
40 ret
= lttng_dynamic_buffer_set_size(&array
->buffer
,
41 new_element_count
* array
->element_size
);
46 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
, const void *element
)
50 if (!array
|| !element
) {
55 ret
= lttng_dynamic_buffer_append(&array
->buffer
, element
, array
->element_size
);
64 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
, size_t element_index
)
66 void *element
= lttng_dynamic_array_get_element(array
, element_index
);
68 if (array
->destructor
) {
69 array
->destructor(element
);
71 if (element_index
!= lttng_dynamic_array_get_count(array
) - 1) {
72 void *next_element
= lttng_dynamic_array_get_element(array
, element_index
+ 1);
76 (array
->size
- element_index
- 1) * array
->element_size
);
79 return lttng_dynamic_buffer_set_size(&array
->buffer
,
80 array
->buffer
.size
- array
->element_size
);
83 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
)
85 if (array
->destructor
) {
88 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
89 array
->destructor(lttng_dynamic_array_get_element(array
, i
));
93 lttng_dynamic_buffer_reset(&array
->buffer
);
97 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
)
99 if (array
->destructor
) {
102 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
103 array
->destructor(lttng_dynamic_array_get_element(array
, i
));
107 (void) lttng_dynamic_buffer_set_size(&array
->buffer
, 0);
111 void lttng_dynamic_pointer_array_init(struct lttng_dynamic_pointer_array
*array
,
112 lttng_dynamic_pointer_array_destructor destructor
)
114 lttng_dynamic_array_init(&array
->array
, sizeof(void *), destructor
);
117 int lttng_dynamic_pointer_array_remove_pointer(struct lttng_dynamic_pointer_array
*array
,
121 const lttng_dynamic_array_element_destructor destructor
= array
->array
.destructor
;
124 * Prevent the destructor from being used by the underlying
127 array
->array
.destructor
= nullptr;
129 destructor(lttng_dynamic_pointer_array_get_pointer(array
, index
));
131 ret
= lttng_dynamic_array_remove_element(&array
->array
, index
);
132 array
->array
.destructor
= destructor
;
136 /* Release any memory used by the dynamic array. */
137 void lttng_dynamic_pointer_array_reset(struct lttng_dynamic_pointer_array
*array
)
139 if (array
->array
.destructor
) {
140 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
142 for (i
= 0; i
< count
; i
++) {
143 void *ptr
= lttng_dynamic_pointer_array_get_pointer(array
, i
);
144 array
->array
.destructor(ptr
);
147 * Prevent the destructor from being used by the underlying
150 array
->array
.destructor
= nullptr;
152 lttng_dynamic_array_reset(&array
->array
);
155 void lttng_dynamic_pointer_array_clear(struct lttng_dynamic_pointer_array
*array
)
157 const lttng_dynamic_array_element_destructor destructor
= array
->array
.destructor
;
160 * Prevent the destructor from being used by the underlying
163 array
->array
.destructor
= nullptr;
165 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
167 for (i
= 0; i
< count
; i
++) {
168 void *ptr
= lttng_dynamic_pointer_array_get_pointer(array
, i
);
172 lttng_dynamic_array_clear(&array
->array
);
173 array
->array
.destructor
= destructor
;