consumerd: pass channel instance to stream creation function
[lttng-tools.git] / src / common / dynamic-array.c
CommitLineData
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
10LTTNG_HIDDEN
11void lttng_dynamic_array_init(struct lttng_dynamic_array *array,
93bed9fe
JG
12 size_t element_size,
13 lttng_dynamic_array_element_destructor destructor)
2c5ff4e4
JG
14{
15 lttng_dynamic_buffer_init(&array->buffer);
16 array->element_size = element_size;
93bed9fe
JG
17 array->size = 0;
18 array->destructor = destructor;
2c5ff4e4
JG
19}
20
21LTTNG_HIDDEN
22int 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++;
38end:
39 return ret;
40}
41
42LTTNG_HIDDEN
93bed9fe
JG
43int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array,
44 size_t element_index)
2c5ff4e4 45{
93bed9fe
JG
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
64LTTNG_HIDDEN
65void lttng_dynamic_array_reset(struct lttng_dynamic_array *array)
66{
67 if (array->destructor) {
2c5ff4e4
JG
68 size_t i;
69
70 for (i = 0; i < lttng_dynamic_array_get_count(array); i++) {
93bed9fe
JG
71 array->destructor(lttng_dynamic_array_get_element(array,
72 i));
2c5ff4e4
JG
73 }
74 }
75
76 lttng_dynamic_buffer_reset(&array->buffer);
77 array->size = 0;
78}
79
74465ffb
MD
80LTTNG_HIDDEN
81void 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
9ac61dbd 92 (void) lttng_dynamic_buffer_set_size(&array->buffer, 0);
74465ffb
MD
93 array->size = 0;
94}
95
2c5ff4e4
JG
96LTTNG_HIDDEN
97void lttng_dynamic_pointer_array_init(
93bed9fe
JG
98 struct lttng_dynamic_pointer_array *array,
99 lttng_dynamic_pointer_array_destructor destructor)
2c5ff4e4 100{
93bed9fe 101 lttng_dynamic_array_init(&array->array, sizeof(void *), destructor);
2c5ff4e4
JG
102}
103
0186592a
JG
104LTTNG_HIDDEN
105int 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
2c5ff4e4
JG
126/* Release any memory used by the dynamic array. */
127LTTNG_HIDDEN
128void lttng_dynamic_pointer_array_reset(
93bed9fe 129 struct lttng_dynamic_pointer_array *array)
2c5ff4e4 130{
93bed9fe 131 if (array->array.destructor) {
2c5ff4e4
JG
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);
93bed9fe 137 array->array.destructor(ptr);
2c5ff4e4 138 }
0186592a
JG
139 /*
140 * Prevent the destructor from being used by the underlying
141 * dynamic array.
142 */
93bed9fe 143 array->array.destructor = NULL;
2c5ff4e4 144 }
93bed9fe 145 lttng_dynamic_array_reset(&array->array);
2c5ff4e4 146}
74465ffb
MD
147
148LTTNG_HIDDEN
149void 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.033787 seconds and 4 git commands to generate.