2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <common/dynamic-array.h>
21 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
23 lttng_dynamic_array_element_destructor destructor
)
25 lttng_dynamic_buffer_init(&array
->buffer
);
26 array
->element_size
= element_size
;
28 array
->destructor
= destructor
;
32 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
37 if (!array
|| !element
) {
42 ret
= lttng_dynamic_buffer_append(&array
->buffer
, element
,
53 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
56 void *element
= lttng_dynamic_array_get_element(array
,
59 if (array
->destructor
) {
60 array
->destructor(element
);
62 if (element_index
!= lttng_dynamic_array_get_count(array
) - 1) {
63 void *next_element
= lttng_dynamic_array_get_element(array
,
66 memmove(element
, next_element
,
67 (array
->size
- element_index
- 1) * array
->element_size
);
70 return lttng_dynamic_buffer_set_size(&array
->buffer
,
71 array
->buffer
.size
- array
->element_size
);
75 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
)
77 if (array
->destructor
) {
80 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
81 array
->destructor(lttng_dynamic_array_get_element(array
,
86 lttng_dynamic_buffer_reset(&array
->buffer
);
91 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
)
93 if (array
->destructor
) {
96 for (i
= 0; i
< lttng_dynamic_array_get_count(array
); i
++) {
97 array
->destructor(lttng_dynamic_array_get_element(array
,
102 lttng_dynamic_buffer_set_size(&array
->buffer
, 0);
107 void lttng_dynamic_pointer_array_init(
108 struct lttng_dynamic_pointer_array
*array
,
109 lttng_dynamic_pointer_array_destructor destructor
)
111 lttng_dynamic_array_init(&array
->array
, sizeof(void *), destructor
);
115 int lttng_dynamic_pointer_array_remove_pointer(
116 struct lttng_dynamic_pointer_array
*array
, size_t index
)
119 const lttng_dynamic_array_element_destructor destructor
=
120 array
->array
.destructor
;
123 * Prevent the destructor from being used by the underlying
126 array
->array
.destructor
= NULL
;
128 destructor(lttng_dynamic_pointer_array_get_pointer(array
,
131 ret
= lttng_dynamic_array_remove_element(&array
->array
, index
);
132 array
->array
.destructor
= destructor
;
136 /* Release any memory used by the dynamic array. */
138 void lttng_dynamic_pointer_array_reset(
139 struct lttng_dynamic_pointer_array
*array
)
141 if (array
->array
.destructor
) {
142 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
144 for (i
= 0; i
< count
; i
++) {
145 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
147 array
->array
.destructor(ptr
);
150 * Prevent the destructor from being used by the underlying
153 array
->array
.destructor
= NULL
;
155 lttng_dynamic_array_reset(&array
->array
);
159 void lttng_dynamic_pointer_array_clear(
160 struct lttng_dynamic_pointer_array
*array
)
162 const lttng_dynamic_array_element_destructor destructor
=
163 array
->array
.destructor
;
166 * Prevent the destructor from being used by the underlying
169 array
->array
.destructor
= NULL
;
171 size_t i
, count
= lttng_dynamic_pointer_array_get_count(array
);
173 for (i
= 0; i
< count
; i
++) {
174 void *ptr
= lttng_dynamic_pointer_array_get_pointer(
179 lttng_dynamic_array_clear(&array
->array
);
180 array
->array
.destructor
= destructor
;