Commit | Line | Data |
---|---|---|
1545fd17 JG |
1 | /* |
2 | * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this program; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | #include <common/dynamic-array.h> | |
19 | ||
20 | LTTNG_HIDDEN | |
21 | void lttng_dynamic_array_init(struct lttng_dynamic_array *array, | |
fcde531a JG |
22 | size_t element_size, |
23 | lttng_dynamic_array_element_destructor destructor) | |
1545fd17 JG |
24 | { |
25 | lttng_dynamic_buffer_init(&array->buffer); | |
26 | array->element_size = element_size; | |
fcde531a JG |
27 | array->size = 0; |
28 | array->destructor = destructor; | |
1545fd17 JG |
29 | } |
30 | ||
31 | LTTNG_HIDDEN | |
32 | int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array, | |
33 | const void *element) | |
34 | { | |
35 | int ret; | |
36 | ||
37 | if (!array || !element) { | |
38 | ret = -1; | |
39 | goto end; | |
40 | } | |
41 | ||
42 | ret = lttng_dynamic_buffer_append(&array->buffer, element, | |
43 | array->element_size); | |
44 | if (ret) { | |
45 | goto end; | |
46 | } | |
47 | array->size++; | |
48 | end: | |
49 | return ret; | |
50 | } | |
51 | ||
52 | LTTNG_HIDDEN | |
fcde531a JG |
53 | int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array, |
54 | size_t element_index) | |
1545fd17 | 55 | { |
fcde531a JG |
56 | void *element = lttng_dynamic_array_get_element(array, |
57 | element_index); | |
58 | ||
59 | if (array->destructor) { | |
60 | array->destructor(element); | |
61 | } | |
62 | if (element_index != lttng_dynamic_array_get_count(array) - 1) { | |
63 | void *next_element = lttng_dynamic_array_get_element(array, | |
64 | element_index + 1); | |
65 | ||
66 | memmove(element, next_element, | |
67 | (array->size - element_index - 1) * array->element_size); | |
68 | } | |
69 | array->size--; | |
70 | return lttng_dynamic_buffer_set_size(&array->buffer, | |
71 | array->buffer.size - array->element_size); | |
72 | } | |
73 | ||
74 | LTTNG_HIDDEN | |
75 | void lttng_dynamic_array_reset(struct lttng_dynamic_array *array) | |
76 | { | |
77 | if (array->destructor) { | |
1545fd17 JG |
78 | size_t i; |
79 | ||
80 | for (i = 0; i < lttng_dynamic_array_get_count(array); i++) { | |
fcde531a JG |
81 | array->destructor(lttng_dynamic_array_get_element(array, |
82 | i)); | |
1545fd17 JG |
83 | } |
84 | } | |
85 | ||
86 | lttng_dynamic_buffer_reset(&array->buffer); | |
87 | array->size = 0; | |
88 | } | |
89 | ||
90 | LTTNG_HIDDEN | |
91 | void lttng_dynamic_pointer_array_init( | |
fcde531a JG |
92 | struct lttng_dynamic_pointer_array *array, |
93 | lttng_dynamic_pointer_array_destructor destructor) | |
1545fd17 | 94 | { |
fcde531a | 95 | lttng_dynamic_array_init(&array->array, sizeof(void *), destructor); |
1545fd17 JG |
96 | } |
97 | ||
b7912f94 JG |
98 | LTTNG_HIDDEN |
99 | int lttng_dynamic_pointer_array_remove_pointer( | |
100 | struct lttng_dynamic_pointer_array *array, size_t index) | |
101 | { | |
102 | int ret; | |
103 | const lttng_dynamic_array_element_destructor destructor = | |
104 | array->array.destructor; | |
105 | ||
106 | /* | |
107 | * Prevent the destructor from being used by the underlying | |
108 | * dynamic array. | |
109 | */ | |
110 | array->array.destructor = NULL; | |
111 | if (destructor) { | |
112 | destructor(lttng_dynamic_pointer_array_get_pointer(array, | |
113 | index)); | |
114 | } | |
115 | ret = lttng_dynamic_array_remove_element(&array->array, index); | |
116 | array->array.destructor = destructor; | |
117 | return ret; | |
118 | } | |
119 | ||
1545fd17 JG |
120 | /* Release any memory used by the dynamic array. */ |
121 | LTTNG_HIDDEN | |
122 | void lttng_dynamic_pointer_array_reset( | |
fcde531a | 123 | struct lttng_dynamic_pointer_array *array) |
1545fd17 | 124 | { |
fcde531a | 125 | if (array->array.destructor) { |
1545fd17 JG |
126 | size_t i, count = lttng_dynamic_pointer_array_get_count(array); |
127 | ||
128 | for (i = 0; i < count; i++) { | |
129 | void *ptr = lttng_dynamic_pointer_array_get_pointer( | |
130 | array, i); | |
fcde531a | 131 | array->array.destructor(ptr); |
1545fd17 | 132 | } |
b7912f94 JG |
133 | /* |
134 | * Prevent the destructor from being used by the underlying | |
135 | * dynamic array. | |
136 | */ | |
fcde531a | 137 | array->array.destructor = NULL; |
1545fd17 | 138 | } |
fcde531a | 139 | lttng_dynamic_array_reset(&array->array); |
1545fd17 | 140 | } |