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_add_element(struct lttng_dynamic_array
*array
,
27 if (!array
|| !element
) {
32 ret
= lttng_dynamic_buffer_append(&array
->buffer
, element
,
43 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
46 void *element
= lttng_dynamic_array_get_element(array
,
49 if (array
->destructor
) {
50 array
->destructor(element
);
52 if (element_index
!= lttng_dynamic_array_get_count(array
) - 1) {
53 void *next_element
= lttng_dynamic_array_get_element(array
,
56 memmove(element
, next_element
,
57 (array
->size
- element_index
- 1) * array
->element_size
);
60 return lttng_dynamic_buffer_set_size(&array
->buffer
,
61 array
->buffer
.size
- array
->element_size
);
65 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
)
67 if (array
->destructor
) {
70 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
71 array
->destructor(lttng_dynamic_array_get_element(array
,
76 lttng_dynamic_buffer_reset(&array
->buffer
);
81 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
)
83 if (array
->destructor
) {
86 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
87 array
->destructor(lttng_dynamic_array_get_element(array
,
92 (void) lttng_dynamic_buffer_set_size(&array
->buffer
, 0);
97 void lttng_dynamic_pointer_array_init(
98 struct lttng_dynamic_pointer_array
*array
,
99 lttng_dynamic_pointer_array_destructor destructor
)
101 lttng_dynamic_array_init(&array
->array
, sizeof(void *), destructor
);
105 int lttng_dynamic_pointer_array_remove_pointer(
106 struct lttng_dynamic_pointer_array
*array
, size_t index
)
109 const lttng_dynamic_array_element_destructor destructor
=
110 array
->array
.destructor
;
113 * Prevent the destructor from being used by the underlying
116 array
->array
.destructor
= NULL
;
118 destructor(lttng_dynamic_pointer_array_get_pointer(array
,
121 ret
= lttng_dynamic_array_remove_element(&array
->array
, index
);
122 array
->array
.destructor
= destructor
;
126 /* Release any memory used by the dynamic array. */
128 void lttng_dynamic_pointer_array_reset(
129 struct lttng_dynamic_pointer_array
*array
)
131 if (array
->array
.destructor
) {
132 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
134 for (i
= 0; i
< count
; i
++) {
135 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
137 array
->array
.destructor(ptr
);
140 * Prevent the destructor from being used by the underlying
143 array
->array
.destructor
= NULL
;
145 lttng_dynamic_array_reset(&array
->array
);
149 void lttng_dynamic_pointer_array_clear(
150 struct lttng_dynamic_pointer_array
*array
)
152 const lttng_dynamic_array_element_destructor destructor
=
153 array
->array
.destructor
;
156 * Prevent the destructor from being used by the underlying
159 array
->array
.destructor
= NULL
;
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(
169 lttng_dynamic_array_clear(&array
->array
);
170 array
->array
.destructor
= destructor
;