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
,
21 size_t new_element_count
)
30 if (array
->destructor
) {
33 for (i
= new_element_count
; i
< array
->size
; i
++) {
34 void *element
= lttng_dynamic_array_get_element(
37 array
->destructor(element
);
41 array
->size
= new_element_count
;
42 ret
= lttng_dynamic_buffer_set_size(&array
->buffer
,
43 new_element_count
* array
->element_size
);
48 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
53 if (!array
|| !element
) {
58 ret
= lttng_dynamic_buffer_append(&array
->buffer
, element
,
68 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
71 void *element
= lttng_dynamic_array_get_element(array
,
74 if (array
->destructor
) {
75 array
->destructor(element
);
77 if (element_index
!= lttng_dynamic_array_get_count(array
) - 1) {
78 void *next_element
= lttng_dynamic_array_get_element(array
,
81 memmove(element
, next_element
,
82 (array
->size
- element_index
- 1) * array
->element_size
);
85 return lttng_dynamic_buffer_set_size(&array
->buffer
,
86 array
->buffer
.size
- array
->element_size
);
89 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
)
91 if (array
->destructor
) {
94 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
95 array
->destructor(lttng_dynamic_array_get_element(array
,
100 lttng_dynamic_buffer_reset(&array
->buffer
);
104 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
)
106 if (array
->destructor
) {
109 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
110 array
->destructor(lttng_dynamic_array_get_element(array
,
115 (void) lttng_dynamic_buffer_set_size(&array
->buffer
, 0);
119 void lttng_dynamic_pointer_array_init(
120 struct lttng_dynamic_pointer_array
*array
,
121 lttng_dynamic_pointer_array_destructor destructor
)
123 lttng_dynamic_array_init(&array
->array
, sizeof(void *), destructor
);
126 int lttng_dynamic_pointer_array_remove_pointer(
127 struct lttng_dynamic_pointer_array
*array
, size_t index
)
130 const lttng_dynamic_array_element_destructor destructor
=
131 array
->array
.destructor
;
134 * Prevent the destructor from being used by the underlying
137 array
->array
.destructor
= NULL
;
139 destructor(lttng_dynamic_pointer_array_get_pointer(array
,
142 ret
= lttng_dynamic_array_remove_element(&array
->array
, index
);
143 array
->array
.destructor
= destructor
;
147 /* Release any memory used by the dynamic array. */
148 void lttng_dynamic_pointer_array_reset(
149 struct lttng_dynamic_pointer_array
*array
)
151 if (array
->array
.destructor
) {
152 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
154 for (i
= 0; i
< count
; i
++) {
155 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
157 array
->array
.destructor(ptr
);
160 * Prevent the destructor from being used by the underlying
163 array
->array
.destructor
= NULL
;
165 lttng_dynamic_array_reset(&array
->array
);
168 void lttng_dynamic_pointer_array_clear(
169 struct lttng_dynamic_pointer_array
*array
)
171 const lttng_dynamic_array_element_destructor destructor
=
172 array
->array
.destructor
;
175 * Prevent the destructor from being used by the underlying
178 array
->array
.destructor
= NULL
;
180 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
182 for (i
= 0; i
< count
; i
++) {
183 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
188 lttng_dynamic_array_clear(&array
->array
);
189 array
->array
.destructor
= destructor
;