Fix: lttng-destroy: missing newline on session destruction message
[lttng-tools.git] / src / common / dynamic-array.h
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 #ifndef LTTNG_DYNAMIC_ARRAY_H
9 #define LTTNG_DYNAMIC_ARRAY_H
10
11 #include <common/dynamic-buffer.h>
12 #include <assert.h>
13
14 typedef void (*lttng_dynamic_array_element_destructor)(void *element);
15 typedef void (*lttng_dynamic_pointer_array_destructor)(void *ptr);
16
17 struct lttng_dynamic_array {
18 struct lttng_dynamic_buffer buffer;
19 size_t element_size;
20 size_t size;
21 lttng_dynamic_array_element_destructor destructor;
22 };
23
24 struct lttng_dynamic_pointer_array {
25 struct lttng_dynamic_array array;
26 };
27
28 /*
29 * Initialize a resizable array of fixed-size elements. This performs no
30 * allocation and can't fail.
31 */
32 LTTNG_HIDDEN
33 void lttng_dynamic_array_init(struct lttng_dynamic_array *array,
34 size_t element_size,
35 lttng_dynamic_array_element_destructor destructor);
36
37 /*
38 * Returns the number of elements in the dynamic array.
39 */
40 static inline
41 size_t lttng_dynamic_array_get_count(
42 const struct lttng_dynamic_array *array)
43 {
44 return array->size;
45 }
46
47 /*
48 * Returns a pointer to the element. Mutating operations on the array invalidate
49 * the returned pointer.
50 */
51 static inline
52 void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array *array,
53 size_t element_index)
54 {
55 assert(element_index < array->size);
56 return array->buffer.data + (element_index * array->element_size);
57 }
58
59 /*
60 * Add an element to the end of a dynamic array. The array's element count is
61 * increased by one and its underlying capacity is adjusted automatically.
62 *
63 * element is a pointer to the element to add (copy) to the array.
64 */
65 LTTNG_HIDDEN
66 int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array,
67 const void *element);
68
69 /*
70 * Remove an element from the dynamic array. The array's element count is
71 * decreased by one and the following elements are shifted to take its place
72 * (when applicable).
73 */
74 LTTNG_HIDDEN
75 int lttng_dynamic_array_remove_element(struct lttng_dynamic_array *array,
76 size_t element_index);
77
78 /* Release any memory used by the dynamic array. */
79 LTTNG_HIDDEN
80 void lttng_dynamic_array_reset(struct lttng_dynamic_array *array);
81
82 /* Remove all elements from the dynamic array. */
83 LTTNG_HIDDEN
84 void lttng_dynamic_array_clear(struct lttng_dynamic_array *array);
85
86 /*
87 * Specialization of lttng_dynamic_array for pointers. This utility
88 * is built under the assumption that pointer sizes are equal
89 * for all data types on supported architectures. Revisit this in the event
90 * of a port to an Harvard architecture.
91 */
92
93 /*
94 * Initialize a resizable array of fixed-size elements. This performs no
95 * allocation and can't fail.
96 */
97 LTTNG_HIDDEN
98 void lttng_dynamic_pointer_array_init(
99 struct lttng_dynamic_pointer_array *array,
100 lttng_dynamic_pointer_array_destructor destructor);
101
102 /*
103 * Returns the number of pointers in the dynamic pointer array.
104 */
105 static inline
106 size_t lttng_dynamic_pointer_array_get_count(
107 const struct lttng_dynamic_pointer_array *array)
108 {
109 return lttng_dynamic_array_get_count(&array->array);
110 }
111
112 /*
113 * Returns the pointer at index `index`.
114 */
115 static inline
116 void *lttng_dynamic_pointer_array_get_pointer(
117 const struct lttng_dynamic_pointer_array *array, size_t index)
118 {
119 void **element = lttng_dynamic_array_get_element(&array->array, index);
120
121 return *element;
122 }
123
124 /*
125 * Add a pointer to the end of a dynamic pointer array. The array's element
126 * count is increased by one and its underlying capacity is adjusted
127 * automatically.
128 */
129 static inline
130 int lttng_dynamic_pointer_array_add_pointer(
131 struct lttng_dynamic_pointer_array *array, void *pointer)
132 {
133 return lttng_dynamic_array_add_element(&array->array, &pointer);
134 }
135
136 /*
137 * Remove a pointer from a dynamic pointer array. The array's element
138 * count is decreased by one and the following pointers are shifted to
139 * take the place of the removed pointer (if applicable).
140 */
141 LTTNG_HIDDEN
142 int lttng_dynamic_pointer_array_remove_pointer(
143 struct lttng_dynamic_pointer_array *array, size_t index);
144
145 /* Release any memory used by the dynamic array. */
146 LTTNG_HIDDEN
147 void lttng_dynamic_pointer_array_reset(
148 struct lttng_dynamic_pointer_array *array);
149
150 /* Remove all elements from the dynamic pointer array. */
151 LTTNG_HIDDEN
152 void lttng_dynamic_pointer_array_clear(
153 struct lttng_dynamic_pointer_array *array);
154
155 #endif /* LTTNG_DYNAMIC_ARRAY_H */
This page took 0.034234 seconds and 4 git commands to generate.