Add the trace chunk and trace chunk registry interfaces
[lttng-tools.git] / src / common / dynamic-array.h
CommitLineData
2c5ff4e4
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#ifndef LTTNG_DYNAMIC_ARRAY_H
19#define LTTNG_DYNAMIC_ARRAY_H
20
21#include <common/dynamic-buffer.h>
22#include <assert.h>
23
24struct lttng_dynamic_array {
25 struct lttng_dynamic_buffer buffer;
26 size_t element_size;
27 size_t size;
28};
29
30struct lttng_dynamic_pointer_array {
31 struct lttng_dynamic_array array;
32};
33
34typedef void (*lttng_dynamic_array_element_destructor)(void *element);
35typedef void (*lttng_dynamic_pointer_array_destructor)(void *ptr);
36
37/*
38 * Initialize a resizable array of fixed-size elements. This performs no
39 * allocation and can't fail.
40 */
41LTTNG_HIDDEN
42void lttng_dynamic_array_init(struct lttng_dynamic_array *array,
43 size_t element_size);
44
45/*
46 * Returns the number of elements in the dynamic array.
47 */
48static inline
49size_t lttng_dynamic_array_get_count(
50 const struct lttng_dynamic_array *array)
51{
52 return array->size;
53}
54
55/*
56 * Returns a pointer to the element. Mutating operations on the array invalidate
57 * the returned pointer.
58 */
59static inline
60void *lttng_dynamic_array_get_element(const struct lttng_dynamic_array *array,
61 size_t element_index)
62{
63 assert(element_index < array->size);
64 return array->buffer.data + (element_index * array->element_size);
65}
66
67/*
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.
70 *
71 * element is a pointer to the element to add (copy) to the array.
72 */
73LTTNG_HIDDEN
74int lttng_dynamic_array_add_element(struct lttng_dynamic_array *array,
75 const void *element);
76
77/* Release any memory used by the dynamic array. */
78LTTNG_HIDDEN
79void lttng_dynamic_array_reset(struct lttng_dynamic_array *array,
80 lttng_dynamic_array_element_destructor destructor);
81
82
83/*
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.
88 */
89
90/*
91 * Initialize a resizable array of fixed-size elements. This performs no
92 * allocation and can't fail.
93 */
94LTTNG_HIDDEN
95void lttng_dynamic_pointer_array_init(
96 struct lttng_dynamic_pointer_array *array);
97
98/*
99 * Returns the number of pointers in the dynamic pointer array.
100 */
101static inline
102size_t lttng_dynamic_pointer_array_get_count(
103 const struct lttng_dynamic_pointer_array *array)
104{
105 return lttng_dynamic_array_get_count(&array->array);
106}
107
108/*
109 * Returns a pointer to the element. Mutating operations on the array invalidate
110 * the returned pointer.
111 */
112static inline
113void *lttng_dynamic_pointer_array_get_pointer(
114 const struct lttng_dynamic_pointer_array *array, size_t index)
115{
116 void **element = lttng_dynamic_array_get_element(&array->array, index);
117
118 return *element;
119}
120
121/*
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
124 * automatically.
125 */
126static inline
127int lttng_dynamic_pointer_array_add_pointer(
128 struct lttng_dynamic_pointer_array *array, void *pointer)
129{
130 return lttng_dynamic_array_add_element(&array->array, &pointer);
131}
132
133/* Release any memory used by the dynamic array. */
134LTTNG_HIDDEN
135void lttng_dynamic_pointer_array_reset(
136 struct lttng_dynamic_pointer_array *array,
137 lttng_dynamic_pointer_array_destructor destructor);
138
139#endif /* LTTNG_DYNAMIC_ARRAY_H */
This page took 0.027463 seconds and 4 git commands to generate.