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 struct lttng_dynamic_array
{
25 struct lttng_dynamic_buffer buffer
;
30 struct lttng_dynamic_pointer_array
{
31 struct lttng_dynamic_array array
;
34 typedef void (*lttng_dynamic_array_element_destructor
)(void *element
);
35 typedef void (*lttng_dynamic_pointer_array_destructor
)(void *ptr
);
38 * Initialize a resizable array of fixed-size elements. This performs no
39 * allocation and can't fail.
42 void lttng_dynamic_array_init(struct lttng_dynamic_array
*array
,
46 * Returns the number of elements in the dynamic array.
49 size_t lttng_dynamic_array_get_count(
50 const struct lttng_dynamic_array
*array
)
56 * Returns a pointer to the element. Mutating operations on the array invalidate
57 * the returned pointer.
60 void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array
*array
,
63 assert(element_index
< array
->size
);
64 return array
->buffer
.data
+ (element_index
* array
->element_size
);
68 * Add an element to the end of a dynamic array. The array's element count is
69 * increased by one and its underlying capacity is adjusted automatically.
71 * element is a pointer to the element to add (copy) to the array.
74 int lttng_dynamic_array_add_element(struct lttng_dynamic_array
*array
,
77 /* Release any memory used by the dynamic array. */
79 void lttng_dynamic_array_reset(struct lttng_dynamic_array
*array
,
80 lttng_dynamic_array_element_destructor destructor
);
84 * Specialization of lttng_dynamic_array for pointers. This utility
85 * is built under the assumption that pointer sizes are equal
86 * for all data types on supported architectures. Revisit this in the event
87 * of a port to an Harvard architecture.
91 * Initialize a resizable array of fixed-size elements. This performs no
92 * allocation and can't fail.
95 void lttng_dynamic_pointer_array_init(
96 struct lttng_dynamic_pointer_array
*array
);
99 * Returns the number of pointers in the dynamic pointer array.
102 size_t lttng_dynamic_pointer_array_get_count(
103 const struct lttng_dynamic_pointer_array
*array
)
105 return lttng_dynamic_array_get_count(&array
->array
);
109 * Returns a pointer to the element. Mutating operations on the array invalidate
110 * the returned pointer.
113 void *lttng_dynamic_pointer_array_get_pointer(
114 const struct lttng_dynamic_pointer_array
*array
, size_t index
)
116 void **element
= lttng_dynamic_array_get_element(&array
->array
, index
);
122 * Add a pointer to the end of a dynamic pointer array. The array's element
123 * count is increased by one and its underlying capacity is adjusted
127 int lttng_dynamic_pointer_array_add_pointer(
128 struct lttng_dynamic_pointer_array
*array
, void *pointer
)
130 return lttng_dynamic_array_add_element(&array
->array
, &pointer
);
133 /* Release any memory used by the dynamic array. */
135 void lttng_dynamic_pointer_array_reset(
136 struct lttng_dynamic_pointer_array
*array
,
137 lttng_dynamic_pointer_array_destructor destructor
);
139 #endif /* LTTNG_DYNAMIC_ARRAY_H */