Fix: common: make lttng_trace_chunk_remove_subdirectory_recursive static
[lttng-tools.git] / src / common / dynamic-array.c
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.h>
9
10 LTTNG_HIDDEN
11 void lttng_dynamic_array_init(struct lttng_dynamic_array *array,
12 size_t element_size,
13 lttng_dynamic_array_element_destructor destructor)
14 {
15 lttng_dynamic_buffer_init(&array->buffer);
16 array->element_size = element_size;
17 array->size = 0;
18 array->destructor = destructor;
19 }
20
21 LTTNG_HIDDEN
22 int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array,
23 const void *element)
24 {
25 int ret;
26
27 if (!array || !element) {
28 ret = -1;
29 goto end;
30 }
31
32 ret = lttng_dynamic_buffer_append(&array->buffer, element,
33 array->element_size);
34 if (ret) {
35 goto end;
36 }
37 array->size++;
38 end:
39 return ret;
40 }
41
42 LTTNG_HIDDEN
43 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array,
44 size_t element_index)
45 {
46 void *element = lttng_dynamic_array_get_element(array,
47 element_index);
48
49 if (array->destructor) {
50 array->destructor(element);
51 }
52 if (element_index != lttng_dynamic_array_get_count(array) - 1) {
53 void *next_element = lttng_dynamic_array_get_element(array,
54 element_index + 1);
55
56 memmove(element, next_element,
57 (array->size - element_index - 1) * array->element_size);
58 }
59 array->size--;
60 return lttng_dynamic_buffer_set_size(&array->buffer,
61 array->buffer.size - array->element_size);
62 }
63
64 LTTNG_HIDDEN
65 void lttng_dynamic_array_reset(struct lttng_dynamic_array *array)
66 {
67 if (array->destructor) {
68 size_t i;
69
70 for (i = 0; i < lttng_dynamic_array_get_count(array); i++) {
71 array->destructor(lttng_dynamic_array_get_element(array,
72 i));
73 }
74 }
75
76 lttng_dynamic_buffer_reset(&array->buffer);
77 array->size = 0;
78 }
79
80 LTTNG_HIDDEN
81 void lttng_dynamic_array_clear(struct lttng_dynamic_array *array)
82 {
83 if (array->destructor) {
84 size_t i;
85
86 for (i = 0; i < lttng_dynamic_array_get_count(array); i++) {
87 array->destructor(lttng_dynamic_array_get_element(array,
88 i));
89 }
90 }
91
92 (void) lttng_dynamic_buffer_set_size(&array->buffer, 0);
93 array->size = 0;
94 }
95
96 LTTNG_HIDDEN
97 void lttng_dynamic_pointer_array_init(
98 struct lttng_dynamic_pointer_array *array,
99 lttng_dynamic_pointer_array_destructor destructor)
100 {
101 lttng_dynamic_array_init(&array->array, sizeof(void *), destructor);
102 }
103
104 LTTNG_HIDDEN
105 int lttng_dynamic_pointer_array_remove_pointer(
106 struct lttng_dynamic_pointer_array *array, size_t index)
107 {
108 int ret;
109 const lttng_dynamic_array_element_destructor destructor =
110 array->array.destructor;
111
112 /*
113 * Prevent the destructor from being used by the underlying
114 * dynamic array.
115 */
116 array->array.destructor = NULL;
117 if (destructor) {
118 destructor(lttng_dynamic_pointer_array_get_pointer(array,
119 index));
120 }
121 ret = lttng_dynamic_array_remove_element(&array->array, index);
122 array->array.destructor = destructor;
123 return ret;
124 }
125
126 /* Release any memory used by the dynamic array. */
127 LTTNG_HIDDEN
128 void lttng_dynamic_pointer_array_reset(
129 struct lttng_dynamic_pointer_array *array)
130 {
131 if (array->array.destructor) {
132 size_t i, count = lttng_dynamic_pointer_array_get_count(array);
133
134 for (i = 0; i < count; i++) {
135 void *ptr = lttng_dynamic_pointer_array_get_pointer(
136 array, i);
137 array->array.destructor(ptr);
138 }
139 /*
140 * Prevent the destructor from being used by the underlying
141 * dynamic array.
142 */
143 array->array.destructor = NULL;
144 }
145 lttng_dynamic_array_reset(&array->array);
146 }
147
148 LTTNG_HIDDEN
149 void lttng_dynamic_pointer_array_clear(
150 struct lttng_dynamic_pointer_array *array)
151 {
152 const lttng_dynamic_array_element_destructor destructor =
153 array->array.destructor;
154
155 /*
156 * Prevent the destructor from being used by the underlying
157 * dynamic array.
158 */
159 array->array.destructor = NULL;
160 if (destructor) {
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);
166 destructor(ptr);
167 }
168 }
169 lttng_dynamic_array_clear(&array->array);
170 array->array.destructor = destructor;
171 }
This page took 0.090222 seconds and 4 git commands to generate.