2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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
18 #ifndef LTTNG_DYNAMIC_ARRAY_H
19 #define LTTNG_DYNAMIC_ARRAY_H
21 #include <common/dynamic-buffer.h>
24 typedef void (*lttng_dynamic_array_element_destructor
)(void *element
);
25 typedef void (*lttng_dynamic_pointer_array_destructor
)(void *ptr
);
27 struct lttng_dynamic_array
{
28 struct lttng_dynamic_buffer buffer
;
31 lttng_dynamic_array_element_destructor destructor
;
34 struct lttng_dynamic_pointer_array
{
35 struct lttng_dynamic_array array
;
39 * Initialize a resizable array of fixed-size elements. This performs no
40 * allocation and can't fail.
43 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
45 lttng_dynamic_array_element_destructor destructor
);
48 * Returns the number of elements in the dynamic array.
51 size_t lttng_dynamic_array_get_count(
52 const struct lttng_dynamic_array
*array
)
58 * Returns a pointer to the element. Mutating operations on the array invalidate
59 * the returned pointer.
62 void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array
*array
,
65 assert(element_index
< array
->size
);
66 return array
->buffer
.data
+ (element_index
* array
->element_size
);
70 * Add an element to the end of a dynamic array. The array's element count is
71 * increased by one and its underlying capacity is adjusted automatically.
73 * element is a pointer to the element to add (copy) to the array.
76 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
80 * Remove an element from the dynamic array. The array's element count is
81 * decreased by one and the following elements are shifted to take its place
85 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array
*array
,
86 size_t element_index
);
88 /* Release any memory used by the dynamic array. */
90 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
);
92 /* Remove all elements from the dynamic array. */
94 void lttng_dynamic_array_clear(struct lttng_dynamic_array
*array
);
97 * Specialization of lttng_dynamic_array for pointers. This utility
98 * is built under the assumption that pointer sizes are equal
99 * for all data types on supported architectures. Revisit this in the event
100 * of a port to an Harvard architecture.
104 * Initialize a resizable array of fixed-size elements. This performs no
105 * allocation and can't fail.
108 void lttng_dynamic_pointer_array_init(
109 struct lttng_dynamic_pointer_array
*array
,
110 lttng_dynamic_pointer_array_destructor destructor
);
113 * Returns the number of pointers in the dynamic pointer array.
116 size_t lttng_dynamic_pointer_array_get_count(
117 const struct lttng_dynamic_pointer_array
*array
)
119 return lttng_dynamic_array_get_count(&array
->array
);
123 * Returns the pointer at index `index`.
126 void *lttng_dynamic_pointer_array_get_pointer(
127 const struct lttng_dynamic_pointer_array
*array
, size_t index
)
129 void **element
= lttng_dynamic_array_get_element(&array
->array
, index
);
135 * Add a pointer to the end of a dynamic pointer array. The array's element
136 * count is increased by one and its underlying capacity is adjusted
140 int lttng_dynamic_pointer_array_add_pointer(
141 struct lttng_dynamic_pointer_array
*array
, void *pointer
)
143 return lttng_dynamic_array_add_element(&array
->array
, &pointer
);
147 * Remove a pointer from a dynamic pointer array. The array's element
148 * count is decreased by one and the following pointers are shifted to
149 * take the place of the removed pointer (if applicable).
152 int lttng_dynamic_pointer_array_remove_pointer(
153 struct lttng_dynamic_pointer_array
*array
, size_t index
);
155 /* Release any memory used by the dynamic array. */
157 void lttng_dynamic_pointer_array_reset(
158 struct lttng_dynamic_pointer_array
*array
);
160 /* Remove all elements from the dynamic pointer array. */
162 void lttng_dynamic_pointer_array_clear(
163 struct lttng_dynamic_pointer_array
*array
);
165 #endif /* LTTNG_DYNAMIC_ARRAY_H */