Fix: erroneous specifier used with ERR_FMT macro
[lttng-tools.git] / src / common / dynamic-array.cpp
1 /*
2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/dynamic-array.hpp>
9
10 void lttng_dynamic_array_init(struct lttng_dynamic_array *array,
11 size_t element_size,
12 lttng_dynamic_array_element_destructor destructor)
13 {
14 lttng_dynamic_buffer_init(&array->buffer);
15 array->element_size = element_size;
16 array->size = 0;
17 array->destructor = destructor;
18 }
19
20 int lttng_dynamic_array_set_count(struct lttng_dynamic_array *array, size_t new_element_count)
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++) {
33 void *element = lttng_dynamic_array_get_element(array, i);
34
35 array->destructor(element);
36 }
37 }
38
39 array->size = new_element_count;
40 ret = lttng_dynamic_buffer_set_size(&array->buffer,
41 new_element_count * array->element_size);
42 end:
43 return ret;
44 }
45
46 int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, const void *element)
47 {
48 int ret;
49
50 if (!array || !element) {
51 ret = -1;
52 goto end;
53 }
54
55 ret = lttng_dynamic_buffer_append(&array->buffer, element, array->element_size);
56 if (ret) {
57 goto end;
58 }
59 array->size++;
60 end:
61 return ret;
62 }
63
64 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array, size_t element_index)
65 {
66 void *element = lttng_dynamic_array_get_element(array, element_index);
67
68 if (array->destructor) {
69 array->destructor(element);
70 }
71 if (element_index != lttng_dynamic_array_get_count(array) - 1) {
72 void *next_element = lttng_dynamic_array_get_element(array, element_index + 1);
73
74 memmove(element,
75 next_element,
76 (array->size - element_index - 1) * array->element_size);
77 }
78 array->size--;
79 return lttng_dynamic_buffer_set_size(&array->buffer,
80 array->buffer.size - array->element_size);
81 }
82
83 void lttng_dynamic_array_reset(struct lttng_dynamic_array *array)
84 {
85 if (array->destructor) {
86 size_t i;
87
88 for (i = 0; i < lttng_dynamic_array_get_count(array); i++) {
89 array->destructor(lttng_dynamic_array_get_element(array, i));
90 }
91 }
92
93 lttng_dynamic_buffer_reset(&array->buffer);
94 array->size = 0;
95 }
96
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++) {
103 array->destructor(lttng_dynamic_array_get_element(array, i));
104 }
105 }
106
107 (void) lttng_dynamic_buffer_set_size(&array->buffer, 0);
108 array->size = 0;
109 }
110
111 void lttng_dynamic_pointer_array_init(struct lttng_dynamic_pointer_array *array,
112 lttng_dynamic_pointer_array_destructor destructor)
113 {
114 lttng_dynamic_array_init(&array->array, sizeof(void *), destructor);
115 }
116
117 int lttng_dynamic_pointer_array_remove_pointer(struct lttng_dynamic_pointer_array *array,
118 size_t index)
119 {
120 int ret;
121 const lttng_dynamic_array_element_destructor destructor = array->array.destructor;
122
123 /*
124 * Prevent the destructor from being used by the underlying
125 * dynamic array.
126 */
127 array->array.destructor = nullptr;
128 if (destructor) {
129 destructor(lttng_dynamic_pointer_array_get_pointer(array, index));
130 }
131 ret = lttng_dynamic_array_remove_element(&array->array, index);
132 array->array.destructor = destructor;
133 return ret;
134 }
135
136 /* Release any memory used by the dynamic array. */
137 void lttng_dynamic_pointer_array_reset(struct lttng_dynamic_pointer_array *array)
138 {
139 if (array->array.destructor) {
140 size_t i, count = lttng_dynamic_pointer_array_get_count(array);
141
142 for (i = 0; i < count; i++) {
143 void *ptr = lttng_dynamic_pointer_array_get_pointer(array, i);
144 array->array.destructor(ptr);
145 }
146 /*
147 * Prevent the destructor from being used by the underlying
148 * dynamic array.
149 */
150 array->array.destructor = nullptr;
151 }
152 lttng_dynamic_array_reset(&array->array);
153 }
154
155 void lttng_dynamic_pointer_array_clear(struct lttng_dynamic_pointer_array *array)
156 {
157 const lttng_dynamic_array_element_destructor destructor = array->array.destructor;
158
159 /*
160 * Prevent the destructor from being used by the underlying
161 * dynamic array.
162 */
163 array->array.destructor = nullptr;
164 if (destructor) {
165 size_t i, count = lttng_dynamic_pointer_array_get_count(array);
166
167 for (i = 0; i < count; i++) {
168 void *ptr = lttng_dynamic_pointer_array_get_pointer(array, i);
169 destructor(ptr);
170 }
171 }
172 lttng_dynamic_array_clear(&array->array);
173 array->array.destructor = destructor;
174 }
This page took 0.089001 seconds and 4 git commands to generate.