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