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