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.h>
11 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
13 lttng_dynamic_array_element_destructor destructor
)
15 lttng_dynamic_buffer_init(&array
->buffer
);
16 array
->element_size
= element_size
;
18 array
->destructor
= destructor
;
22 int lttng_dynamic_array_set_count(struct lttng_dynamic_array
*array
,
23 size_t new_element_count
)
32 if (array
->destructor
) {
35 for (i
= new_element_count
; i
< array
->size
; i
++) {
36 void *element
= lttng_dynamic_array_get_element(
39 array
->destructor(element
);
43 array
->size
= new_element_count
;
44 ret
= lttng_dynamic_buffer_set_size(&array
->buffer
,
45 new_element_count
* array
->element_size
);
51 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
56 if (!array
|| !element
) {
61 ret
= lttng_dynamic_buffer_append(&array
->buffer
, element
,
72 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
75 void *element
= lttng_dynamic_array_get_element(array
,
78 if (array
->destructor
) {
79 array
->destructor(element
);
81 if (element_index
!= lttng_dynamic_array_get_count(array
) - 1) {
82 void *next_element
= lttng_dynamic_array_get_element(array
,
85 memmove(element
, next_element
,
86 (array
->size
- element_index
- 1) * array
->element_size
);
89 return lttng_dynamic_buffer_set_size(&array
->buffer
,
90 array
->buffer
.size
- array
->element_size
);
94 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
)
96 if (array
->destructor
) {
99 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
100 array
->destructor(lttng_dynamic_array_get_element(array
,
105 lttng_dynamic_buffer_reset(&array
->buffer
);
110 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
)
112 if (array
->destructor
) {
115 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
116 array
->destructor(lttng_dynamic_array_get_element(array
,
121 (void) lttng_dynamic_buffer_set_size(&array
->buffer
, 0);
126 void lttng_dynamic_pointer_array_init(
127 struct lttng_dynamic_pointer_array
*array
,
128 lttng_dynamic_pointer_array_destructor destructor
)
130 lttng_dynamic_array_init(&array
->array
, sizeof(void *), destructor
);
134 int lttng_dynamic_pointer_array_remove_pointer(
135 struct lttng_dynamic_pointer_array
*array
, size_t index
)
138 const lttng_dynamic_array_element_destructor destructor
=
139 array
->array
.destructor
;
142 * Prevent the destructor from being used by the underlying
145 array
->array
.destructor
= NULL
;
147 destructor(lttng_dynamic_pointer_array_get_pointer(array
,
150 ret
= lttng_dynamic_array_remove_element(&array
->array
, index
);
151 array
->array
.destructor
= destructor
;
155 /* Release any memory used by the dynamic array. */
157 void lttng_dynamic_pointer_array_reset(
158 struct lttng_dynamic_pointer_array
*array
)
160 if (array
->array
.destructor
) {
161 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
163 for (i
= 0; i
< count
; i
++) {
164 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
166 array
->array
.destructor(ptr
);
169 * Prevent the destructor from being used by the underlying
172 array
->array
.destructor
= NULL
;
174 lttng_dynamic_array_reset(&array
->array
);
178 void lttng_dynamic_pointer_array_clear(
179 struct lttng_dynamic_pointer_array
*array
)
181 const lttng_dynamic_array_element_destructor destructor
=
182 array
->array
.destructor
;
185 * Prevent the destructor from being used by the underlying
188 array
->array
.destructor
= NULL
;
190 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
192 for (i
= 0; i
< count
; i
++) {
193 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
198 lttng_dynamic_array_clear(&array
->array
);
199 array
->array
.destructor
= destructor
;