Commit | Line | Data |
---|---|---|
2c5ff4e4 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
2c5ff4e4 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
2c5ff4e4 | 5 | * |
2c5ff4e4 JG |
6 | */ |
7 | ||
8 | #include <common/dynamic-array.h> | |
9 | ||
10 | LTTNG_HIDDEN | |
11 | void lttng_dynamic_array_init(struct lttng_dynamic_array *array, | |
93bed9fe JG |
12 | size_t element_size, |
13 | lttng_dynamic_array_element_destructor destructor) | |
2c5ff4e4 JG |
14 | { |
15 | lttng_dynamic_buffer_init(&array->buffer); | |
16 | array->element_size = element_size; | |
93bed9fe JG |
17 | array->size = 0; |
18 | array->destructor = destructor; | |
2c5ff4e4 JG |
19 | } |
20 | ||
5fe3e097 JG |
21 | LTTNG_HIDDEN |
22 | int lttng_dynamic_array_set_count(struct lttng_dynamic_array *array, | |
23 | size_t new_element_count) | |
24 | { | |
25 | int ret; | |
26 | ||
27 | if (!array) { | |
28 | ret = -1; | |
29 | goto end; | |
30 | } | |
31 | ||
32 | if (array->destructor) { | |
33 | size_t i; | |
34 | ||
35 | for (i = new_element_count; i < array->size; i++) { | |
36 | void *element = lttng_dynamic_array_get_element( | |
37 | array, i); | |
38 | ||
39 | array->destructor(element); | |
40 | } | |
41 | } | |
42 | ||
43 | array->size = new_element_count; | |
44 | ret = lttng_dynamic_buffer_set_size(&array->buffer, | |
45 | new_element_count * array->element_size); | |
46 | end: | |
47 | return ret; | |
48 | } | |
49 | ||
2c5ff4e4 JG |
50 | LTTNG_HIDDEN |
51 | int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, | |
52 | const void *element) | |
53 | { | |
54 | int ret; | |
55 | ||
56 | if (!array || !element) { | |
57 | ret = -1; | |
58 | goto end; | |
59 | } | |
60 | ||
61 | ret = lttng_dynamic_buffer_append(&array->buffer, element, | |
62 | array->element_size); | |
63 | if (ret) { | |
64 | goto end; | |
65 | } | |
66 | array->size++; | |
67 | end: | |
68 | return ret; | |
69 | } | |
70 | ||
71 | LTTNG_HIDDEN | |
93bed9fe JG |
72 | int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array, |
73 | size_t element_index) | |
2c5ff4e4 | 74 | { |
93bed9fe JG |
75 | void *element = lttng_dynamic_array_get_element(array, |
76 | element_index); | |
77 | ||
78 | if (array->destructor) { | |
79 | array->destructor(element); | |
80 | } | |
81 | if (element_index != lttng_dynamic_array_get_count(array) - 1) { | |
82 | void *next_element = lttng_dynamic_array_get_element(array, | |
83 | element_index + 1); | |
84 | ||
85 | memmove(element, next_element, | |
86 | (array->size - element_index - 1) * array->element_size); | |
87 | } | |
88 | array->size--; | |
89 | return lttng_dynamic_buffer_set_size(&array->buffer, | |
90 | array->buffer.size - array->element_size); | |
91 | } | |
92 | ||
93 | LTTNG_HIDDEN | |
94 | void lttng_dynamic_array_reset(struct lttng_dynamic_array *array) | |
95 | { | |
96 | if (array->destructor) { | |
2c5ff4e4 JG |
97 | size_t i; |
98 | ||
99 | for (i = 0; i < lttng_dynamic_array_get_count(array); i++) { | |
93bed9fe JG |
100 | array->destructor(lttng_dynamic_array_get_element(array, |
101 | i)); | |
2c5ff4e4 JG |
102 | } |
103 | } | |
104 | ||
105 | lttng_dynamic_buffer_reset(&array->buffer); | |
106 | array->size = 0; | |
107 | } | |
108 | ||
74465ffb MD |
109 | LTTNG_HIDDEN |
110 | void lttng_dynamic_array_clear(struct lttng_dynamic_array *array) | |
111 | { | |
112 | if (array->destructor) { | |
113 | size_t i; | |
114 | ||
115 | for (i = 0; i < lttng_dynamic_array_get_count(array); i++) { | |
116 | array->destructor(lttng_dynamic_array_get_element(array, | |
117 | i)); | |
118 | } | |
119 | } | |
120 | ||
9ac61dbd | 121 | (void) lttng_dynamic_buffer_set_size(&array->buffer, 0); |
74465ffb MD |
122 | array->size = 0; |
123 | } | |
124 | ||
2c5ff4e4 JG |
125 | LTTNG_HIDDEN |
126 | void lttng_dynamic_pointer_array_init( | |
93bed9fe JG |
127 | struct lttng_dynamic_pointer_array *array, |
128 | lttng_dynamic_pointer_array_destructor destructor) | |
2c5ff4e4 | 129 | { |
93bed9fe | 130 | lttng_dynamic_array_init(&array->array, sizeof(void *), destructor); |
2c5ff4e4 JG |
131 | } |
132 | ||
0186592a JG |
133 | LTTNG_HIDDEN |
134 | int lttng_dynamic_pointer_array_remove_pointer( | |
135 | struct lttng_dynamic_pointer_array *array, size_t index) | |
136 | { | |
137 | int ret; | |
138 | const lttng_dynamic_array_element_destructor destructor = | |
139 | array->array.destructor; | |
140 | ||
141 | /* | |
142 | * Prevent the destructor from being used by the underlying | |
143 | * dynamic array. | |
144 | */ | |
145 | array->array.destructor = NULL; | |
146 | if (destructor) { | |
147 | destructor(lttng_dynamic_pointer_array_get_pointer(array, | |
148 | index)); | |
149 | } | |
150 | ret = lttng_dynamic_array_remove_element(&array->array, index); | |
151 | array->array.destructor = destructor; | |
152 | return ret; | |
153 | } | |
154 | ||
2c5ff4e4 JG |
155 | /* Release any memory used by the dynamic array. */ |
156 | LTTNG_HIDDEN | |
157 | void lttng_dynamic_pointer_array_reset( | |
93bed9fe | 158 | struct lttng_dynamic_pointer_array *array) |
2c5ff4e4 | 159 | { |
93bed9fe | 160 | if (array->array.destructor) { |
2c5ff4e4 JG |
161 | size_t i, count = lttng_dynamic_pointer_array_get_count(array); |
162 | ||
163 | for (i = 0; i < count; i++) { | |
164 | void *ptr = lttng_dynamic_pointer_array_get_pointer( | |
165 | array, i); | |
93bed9fe | 166 | array->array.destructor(ptr); |
2c5ff4e4 | 167 | } |
0186592a JG |
168 | /* |
169 | * Prevent the destructor from being used by the underlying | |
170 | * dynamic array. | |
171 | */ | |
93bed9fe | 172 | array->array.destructor = NULL; |
2c5ff4e4 | 173 | } |
93bed9fe | 174 | lttng_dynamic_array_reset(&array->array); |
2c5ff4e4 | 175 | } |
74465ffb MD |
176 | |
177 | LTTNG_HIDDEN | |
178 | void lttng_dynamic_pointer_array_clear( | |
179 | struct lttng_dynamic_pointer_array *array) | |
180 | { | |
181 | const lttng_dynamic_array_element_destructor destructor = | |
182 | array->array.destructor; | |
183 | ||
184 | /* | |
185 | * Prevent the destructor from being used by the underlying | |
186 | * dynamic array. | |
187 | */ | |
188 | array->array.destructor = NULL; | |
189 | if (destructor) { | |
190 | size_t i, count = lttng_dynamic_pointer_array_get_count(array); | |
191 | ||
192 | for (i = 0; i < count; i++) { | |
193 | void *ptr = lttng_dynamic_pointer_array_get_pointer( | |
194 | array, i); | |
195 | destructor(ptr); | |
196 | } | |
197 | } | |
198 | lttng_dynamic_array_clear(&array->array); | |
199 | array->array.destructor = destructor; | |
200 | } |