Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed
PP
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
862ca4ed
PP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
a1a6f91b 24#define BT_LOG_TAG "LIB/TRACE-CLASS"
c2d9d9cf 25#include "lib/logging.h"
862ca4ed 26
578e048b 27#include "lib/assert-pre.h"
3c634c85 28#include "lib/assert-post.h"
3fadfbc0
MJ
29#include <babeltrace2/trace-ir/trace-class.h>
30#include <babeltrace2/trace-ir/trace-class-const.h>
3fadfbc0 31#include <babeltrace2/trace-ir/event-class.h>
578e048b
MJ
32#include "ctf-writer/functor.h"
33#include "ctf-writer/clock.h"
34#include "compat/compiler.h"
3fadfbc0
MJ
35#include <babeltrace2/value.h>
36#include <babeltrace2/value-const.h>
578e048b 37#include "lib/value.h"
3fadfbc0 38#include <babeltrace2/types.h>
578e048b
MJ
39#include "compat/endian.h"
40#include "common/assert.h"
41#include "compat/glib.h"
862ca4ed
PP
42#include <inttypes.h>
43#include <stdint.h>
44#include <string.h>
c4f23e30 45#include <stdbool.h>
862ca4ed
PP
46#include <stdlib.h>
47
578e048b
MJ
48#include "clock-class.h"
49#include "event-class.h"
50#include "event.h"
51#include "field-class.h"
52#include "field-wrapper.h"
53#include "resolve-field-path.h"
54#include "stream-class.h"
55#include "stream.h"
56#include "trace.h"
57#include "utils.h"
c6962c96 58#include "lib/value.h"
d24d5663 59#include "lib/func-status.h"
578e048b 60
ad5268b5
FD
61struct bt_trace_class_destruction_listener_elem {
62 bt_trace_class_destruction_listener_func func;
63 void *data;
64};
65
bdb288b3
PP
66#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
67 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
862ca4ed
PP
68
69static
70void destroy_trace_class(struct bt_object *obj)
71{
72 struct bt_trace_class *tc = (void *) obj;
73
74 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
c6962c96
PP
75 BT_OBJECT_PUT_REF_AND_RESET(tc->user_attributes);
76
ad5268b5
FD
77 /*
78 * Call destruction listener functions so that everything else
79 * still exists in the trace class.
80 */
81 if (tc->destruction_listeners) {
82 uint64_t i;
3f7d4d90 83 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
84
85 /*
86 * The trace class' reference count is 0 if we're here. Increment
87 * it to avoid a double-destroy (possibly infinitely recursive).
88 * This could happen for example if a destruction listener did
89 * bt_object_get_ref() (or anything that causes
90 * bt_object_get_ref() to be called) on the trace class (ref.
91 * count goes from 0 to 1), and then bt_object_put_ref(): the
92 * reference count would go from 1 to 0 again and this function
93 * would be called again.
94 */
95 tc->base.ref_count++;
96
ad5268b5
FD
97 /* Call all the trace class destruction listeners */
98 for (i = 0; i < tc->destruction_listeners->len; i++) {
99 struct bt_trace_class_destruction_listener_elem elem =
100 g_array_index(tc->destruction_listeners,
101 struct bt_trace_class_destruction_listener_elem, i);
102
103 if (elem.func) {
104 elem.func(tc, elem.data);
105 }
c47a6bec
SM
106
107 /*
108 * The destruction listener should not have kept a
109 * reference to the trace class.
110 */
3c634c85 111 BT_ASSERT_POST(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
ad5268b5
FD
112 }
113 g_array_free(tc->destruction_listeners, TRUE);
114 tc->destruction_listeners = NULL;
115 }
862ca4ed 116
862ca4ed
PP
117 if (tc->stream_classes) {
118 BT_LOGD_STR("Destroying stream classes.");
119 g_ptr_array_free(tc->stream_classes, TRUE);
120 tc->stream_classes = NULL;
121 }
122
862ca4ed
PP
123 g_free(tc);
124}
125
41693723 126struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
127{
128 struct bt_trace_class *tc = NULL;
862ca4ed 129
41693723 130 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
131 BT_LOGD_STR("Creating default trace class object.");
132 tc = g_new0(struct bt_trace_class, 1);
133 if (!tc) {
870631a2 134 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
135 goto error;
136 }
137
138 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
c6962c96
PP
139 tc->user_attributes = bt_value_map_create();
140 if (!tc->user_attributes) {
141 BT_LIB_LOGE_APPEND_CAUSE(
142 "Failed to create a map value object.");
143 goto error;
144 }
862ca4ed
PP
145
146 tc->stream_classes = g_ptr_array_new_with_free_func(
147 (GDestroyNotify) bt_object_try_spec_release);
148 if (!tc->stream_classes) {
870631a2 149 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
150 goto error;
151 }
152
ad5268b5
FD
153 tc->destruction_listeners = g_array_new(FALSE, TRUE,
154 sizeof(struct bt_trace_class_destruction_listener_elem));
155 if (!tc->destruction_listeners) {
870631a2 156 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
157 goto error;
158 }
159
862ca4ed 160 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
161 BT_LIB_LOGD("Created trace class object: %!+T", tc);
162 goto end;
163
164error:
165 BT_OBJECT_PUT_REF_AND_RESET(tc);
166
167end:
168 return tc;
169}
170
d24d5663 171enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
172 const struct bt_trace_class *_tc,
173 bt_trace_class_destruction_listener_func listener,
2054a0d1 174 void *data, bt_listener_id *listener_id)
ad5268b5
FD
175{
176 struct bt_trace_class *tc = (void *) _tc;
177 uint64_t i;
178 struct bt_trace_class_destruction_listener_elem new_elem = {
179 .func = listener,
180 .data = data,
181 };
182
183 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
184 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
185
186 /* Find the next available spot */
187 for (i = 0; i < tc->destruction_listeners->len; i++) {
188 struct bt_trace_class_destruction_listener_elem elem =
189 g_array_index(tc->destruction_listeners,
190 struct bt_trace_class_destruction_listener_elem, i);
191
192 if (!elem.func) {
193 break;
194 }
195 }
196
197 if (i == tc->destruction_listeners->len) {
198 g_array_append_val(tc->destruction_listeners, new_elem);
199 } else {
200 g_array_insert_val(tc->destruction_listeners, i, new_elem);
201 }
202
203 if (listener_id) {
204 *listener_id = i;
205 }
206
3f7d4d90 207 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 208 "listener-id=%" PRIu64, tc, i);
d24d5663 209 return BT_FUNC_STATUS_OK;
ad5268b5
FD
210}
211
ad5268b5
FD
212static
213bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
214{
215 BT_ASSERT(listener_id < tc->destruction_listeners->len);
216 return (&g_array_index(tc->destruction_listeners,
217 struct bt_trace_class_destruction_listener_elem,
5084732e 218 listener_id))->func;
ad5268b5
FD
219}
220
d24d5663 221enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
2054a0d1 222 const struct bt_trace_class *_tc, bt_listener_id listener_id)
ad5268b5
FD
223{
224 struct bt_trace_class *tc = (void *) _tc;
225 struct bt_trace_class_destruction_listener_elem *elem;
226
227 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
228 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
229 "Trace class has no such trace class destruction listener ID: "
230 "%![tc-]+T, %" PRIu64, tc, listener_id);
231 elem = &g_array_index(tc->destruction_listeners,
232 struct bt_trace_class_destruction_listener_elem,
233 listener_id);
234 BT_ASSERT(elem->func);
235
236 elem->func = NULL;
237 elem->data = NULL;
3f7d4d90 238 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
239 "%![tc-]+T, listener-id=%" PRIu64,
240 tc, listener_id);
d24d5663 241 return BT_FUNC_STATUS_OK;
ad5268b5
FD
242}
243
862ca4ed
PP
244uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
245{
bdb288b3 246 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
247 return (uint64_t) tc->stream_classes->len;
248}
249
250struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
251 struct bt_trace_class *tc, uint64_t index)
252{
bdb288b3
PP
253 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
254 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
862ca4ed
PP
255 return g_ptr_array_index(tc->stream_classes, index);
256}
257
258const struct bt_stream_class *
259bt_trace_class_borrow_stream_class_by_index_const(
260 const struct bt_trace_class *tc, uint64_t index)
261{
262 return bt_trace_class_borrow_stream_class_by_index(
263 (void *) tc, index);
264}
265
266struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
267 struct bt_trace_class *tc, uint64_t id)
268{
269 struct bt_stream_class *stream_class = NULL;
270 uint64_t i;
271
bdb288b3 272 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
273
274 for (i = 0; i < tc->stream_classes->len; i++) {
275 struct bt_stream_class *stream_class_candidate =
276 g_ptr_array_index(tc->stream_classes, i);
277
278 if (stream_class_candidate->id == id) {
279 stream_class = stream_class_candidate;
280 goto end;
281 }
282 }
283
284end:
285 return stream_class;
286}
287
288const struct bt_stream_class *
289bt_trace_class_borrow_stream_class_by_id_const(
290 const struct bt_trace_class *tc, uint64_t id)
291{
292 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
293}
294
862ca4ed
PP
295BT_HIDDEN
296void _bt_trace_class_freeze(const struct bt_trace_class *tc)
297{
862ca4ed
PP
298 BT_ASSERT(tc);
299 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
300 ((struct bt_trace_class *) tc)->frozen = true;
301}
302
303bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
304{
bdb288b3 305 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
306 return (bt_bool) tc->assigns_automatic_stream_class_id;
307}
308
309void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
310 bt_bool value)
311{
312 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
bdb288b3 313 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
862ca4ed 314 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 315 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
316 "assignment property: %!+T", tc);
317}
c5b9b441 318
c6962c96
PP
319const struct bt_value *bt_trace_class_borrow_user_attributes_const(
320 const struct bt_trace_class *trace_class)
321{
322 BT_ASSERT_PRE_DEV_NON_NULL(trace_class, "Trace class");
323 return trace_class->user_attributes;
324}
325
326struct bt_value *bt_trace_class_borrow_user_attributes(
327 struct bt_trace_class *trace_class)
328{
329 return (void *) bt_trace_class_borrow_user_attributes_const(
330 (void *) trace_class);
331}
332
333void bt_trace_class_set_user_attributes(struct bt_trace_class *trace_class,
334 const struct bt_value *user_attributes)
335{
336 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
337 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
338 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
339 "User attributes object is not a map value object.");
340 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class);
6871026b 341 bt_object_put_ref_no_null_check(trace_class->user_attributes);
c6962c96 342 trace_class->user_attributes = (void *) user_attributes;
6871026b 343 bt_object_get_ref_no_null_check(trace_class->user_attributes);
c6962c96
PP
344}
345
c5b9b441
PP
346void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
347{
348 bt_object_get_ref(trace_class);
349}
350
351void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
352{
353 bt_object_put_ref(trace_class);
354}
This page took 0.060435 seconds and 4 git commands to generate.