Remove stdbool.h includes from C++ files
[babeltrace.git] / src / plugins / ctf / fs-sink / fs-sink-ctf-meta.hpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2018-2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #ifndef BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H
8 #define BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H
9
10 #include <ctype.h>
11 #include <glib.h>
12 #include <stdint.h>
13 #include <string.h>
14
15 #include <babeltrace2/babeltrace.h>
16
17 #include "common/assert.h"
18 #include "common/common.h"
19 #include "common/uuid.h"
20
21 enum fs_sink_ctf_field_class_type
22 {
23 FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL,
24 FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY,
25 FS_SINK_CTF_FIELD_CLASS_TYPE_INT,
26 FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT,
27 FS_SINK_CTF_FIELD_CLASS_TYPE_STRING,
28 FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT,
29 FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY,
30 FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE,
31 FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION,
32 FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT,
33 };
34
35 struct fs_sink_ctf_field_class
36 {
37 enum fs_sink_ctf_field_class_type type;
38
39 /* Weak */
40 const bt_field_class *ir_fc;
41
42 unsigned int alignment;
43
44 /* Index of the field class within its own parent */
45 uint64_t index_in_parent;
46 };
47
48 struct fs_sink_ctf_field_class_bit_array
49 {
50 struct fs_sink_ctf_field_class base;
51 unsigned int size;
52 };
53
54 struct fs_sink_ctf_field_class_bool
55 {
56 struct fs_sink_ctf_field_class_bit_array base;
57 };
58
59 struct fs_sink_ctf_field_class_int
60 {
61 struct fs_sink_ctf_field_class_bit_array base;
62 bool is_signed;
63 };
64
65 struct fs_sink_ctf_field_class_float
66 {
67 struct fs_sink_ctf_field_class_bit_array base;
68 };
69
70 struct fs_sink_ctf_field_class_string
71 {
72 struct fs_sink_ctf_field_class base;
73 };
74
75 struct fs_sink_ctf_named_field_class
76 {
77 GString *name;
78
79 /* Owned by this */
80 struct fs_sink_ctf_field_class *fc;
81 };
82
83 struct fs_sink_ctf_field_class_struct
84 {
85 struct fs_sink_ctf_field_class base;
86
87 /* Array of `struct fs_sink_ctf_named_field_class` */
88 GArray *members;
89 };
90
91 struct fs_sink_ctf_field_class_option
92 {
93 struct fs_sink_ctf_field_class base;
94 struct fs_sink_ctf_field_class *content_fc;
95 GString *tag_ref;
96 };
97
98 struct fs_sink_ctf_field_class_variant
99 {
100 struct fs_sink_ctf_field_class base;
101 GString *tag_ref;
102 bool tag_is_before;
103
104 /* Array of `struct fs_sink_ctf_named_field_class` */
105 GArray *options;
106 };
107
108 struct fs_sink_ctf_field_class_array_base
109 {
110 struct fs_sink_ctf_field_class base;
111 struct fs_sink_ctf_field_class *elem_fc;
112 };
113
114 struct fs_sink_ctf_field_class_array
115 {
116 struct fs_sink_ctf_field_class_array_base base;
117 uint64_t length;
118 };
119
120 struct fs_sink_ctf_field_class_sequence
121 {
122 struct fs_sink_ctf_field_class_array_base base;
123 GString *length_ref;
124 bool length_is_before;
125 };
126
127 static inline fs_sink_ctf_field_class_bit_array *
128 fs_sink_ctf_field_class_as_bit_array(fs_sink_ctf_field_class *fc)
129 {
130 BT_ASSERT_DBG(!fc || (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY ||
131 fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT ||
132 fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL ||
133 fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT));
134
135 return (fs_sink_ctf_field_class_bit_array *) fc;
136 }
137
138 static inline fs_sink_ctf_field_class_bool *
139 fs_sink_ctf_field_class_as_bool(fs_sink_ctf_field_class *fc)
140 {
141 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL);
142
143 return (fs_sink_ctf_field_class_bool *) fc;
144 }
145
146 static inline fs_sink_ctf_field_class_int *
147 fs_sink_ctf_field_class_as_int(fs_sink_ctf_field_class *fc)
148 {
149 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT);
150
151 return (fs_sink_ctf_field_class_int *) fc;
152 }
153
154 static inline fs_sink_ctf_field_class_float *
155 fs_sink_ctf_field_class_as_float(fs_sink_ctf_field_class *fc)
156 {
157 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT);
158
159 return (fs_sink_ctf_field_class_float *) fc;
160 }
161
162 static inline fs_sink_ctf_field_class_string *
163 fs_sink_ctf_field_class_as_string(fs_sink_ctf_field_class *fc)
164 {
165 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRING);
166
167 return (fs_sink_ctf_field_class_string *) fc;
168 }
169
170 static inline fs_sink_ctf_field_class_struct *
171 fs_sink_ctf_field_class_as_struct(fs_sink_ctf_field_class *fc)
172 {
173 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT);
174
175 return (fs_sink_ctf_field_class_struct *) fc;
176 }
177
178 static inline fs_sink_ctf_field_class_array_base *
179 fs_sink_ctf_field_class_as_array_base(fs_sink_ctf_field_class *fc)
180 {
181 BT_ASSERT_DBG(!fc || (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY ||
182 fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE));
183
184 return (fs_sink_ctf_field_class_array_base *) fc;
185 }
186
187 static inline fs_sink_ctf_field_class_array *
188 fs_sink_ctf_field_class_as_array(fs_sink_ctf_field_class *fc)
189 {
190 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY);
191
192 return (fs_sink_ctf_field_class_array *) fc;
193 }
194
195 static inline fs_sink_ctf_field_class_sequence *
196 fs_sink_ctf_field_class_as_sequence(fs_sink_ctf_field_class *fc)
197 {
198 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE);
199
200 return (fs_sink_ctf_field_class_sequence *) fc;
201 }
202
203 static inline fs_sink_ctf_field_class_option *
204 fs_sink_ctf_field_class_as_option(fs_sink_ctf_field_class *fc)
205 {
206 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION);
207
208 return (fs_sink_ctf_field_class_option *) fc;
209 }
210
211 static inline fs_sink_ctf_field_class_variant *
212 fs_sink_ctf_field_class_as_variant(fs_sink_ctf_field_class *fc)
213 {
214 BT_ASSERT_DBG(!fc || fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT);
215
216 return (fs_sink_ctf_field_class_variant *) fc;
217 }
218
219 struct fs_sink_ctf_stream_class;
220
221 struct fs_sink_ctf_event_class
222 {
223 /* Weak */
224 const bt_event_class *ir_ec;
225
226 /* Weak */
227 struct fs_sink_ctf_stream_class *sc;
228
229 /* Owned by this */
230 struct fs_sink_ctf_field_class *spec_context_fc;
231
232 /* Owned by this */
233 struct fs_sink_ctf_field_class *payload_fc;
234 };
235
236 struct fs_sink_ctf_trace;
237
238 struct fs_sink_ctf_stream_class
239 {
240 /* Weak */
241 struct fs_sink_ctf_trace *trace;
242
243 /* Weak */
244 const bt_stream_class *ir_sc;
245
246 /* Weak */
247 const bt_clock_class *default_clock_class;
248
249 GString *default_clock_class_name;
250 bool has_packets;
251 bool packets_have_ts_begin;
252 bool packets_have_ts_end;
253 bool has_discarded_events;
254 bool discarded_events_has_ts;
255 bool discarded_packets_has_ts;
256
257 /* Owned by this */
258 struct fs_sink_ctf_field_class *packet_context_fc;
259
260 /* Owned by this */
261 struct fs_sink_ctf_field_class *event_common_context_fc;
262
263 /* Array of `struct fs_sink_ctf_event_class *` (owned by this) */
264 GPtrArray *event_classes;
265
266 /*
267 * `const bt_event_class *` (weak) ->
268 * `struct fs_sink_ctf_event_class *` (weak)
269 */
270 GHashTable *event_classes_from_ir;
271 };
272
273 struct fs_sink_ctf_trace
274 {
275 /* Weak */
276 const bt_trace *ir_trace;
277
278 /* Weak */
279 const bt_trace_class *ir_tc;
280
281 bt_uuid_t uuid;
282
283 /* Array of `struct fs_sink_ctf_stream_class *` (owned by this) */
284 GPtrArray *stream_classes;
285 };
286
287 static inline void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc);
288
289 static inline void _fs_sink_ctf_field_class_init(struct fs_sink_ctf_field_class *fc,
290 enum fs_sink_ctf_field_class_type type,
291 const bt_field_class *ir_fc,
292 unsigned int alignment, uint64_t index_in_parent)
293 {
294 BT_ASSERT(fc);
295 fc->type = type;
296 fc->ir_fc = ir_fc;
297 fc->alignment = alignment;
298 fc->index_in_parent = index_in_parent;
299 }
300
301 static inline void _fs_sink_ctf_field_class_bit_array_init(
302 struct fs_sink_ctf_field_class_bit_array *fc, enum fs_sink_ctf_field_class_type type,
303 const bt_field_class *ir_fc, unsigned int size, uint64_t index_in_parent)
304 {
305 _fs_sink_ctf_field_class_init(&fc->base, type, ir_fc, size % 8 == 0 ? 8 : 1, index_in_parent);
306 fc->size = size;
307 }
308
309 static inline void _fs_sink_ctf_field_class_int_init(struct fs_sink_ctf_field_class_int *fc,
310 enum fs_sink_ctf_field_class_type type,
311 const bt_field_class *ir_fc,
312 uint64_t index_in_parent)
313 {
314 bt_field_class_type ir_fc_type = bt_field_class_get_type(ir_fc);
315
316 _fs_sink_ctf_field_class_bit_array_init(
317 &fc->base, type, ir_fc, (unsigned int) bt_field_class_integer_get_field_value_range(ir_fc),
318 index_in_parent);
319 fc->is_signed = bt_field_class_type_is(ir_fc_type, BT_FIELD_CLASS_TYPE_SIGNED_INTEGER);
320 }
321
322 static inline void
323 _fs_sink_ctf_named_field_class_init(struct fs_sink_ctf_named_field_class *named_fc)
324 {
325 BT_ASSERT(named_fc);
326 named_fc->name = g_string_new(NULL);
327 BT_ASSERT(named_fc->name);
328 }
329
330 static inline void
331 _fs_sink_ctf_named_field_class_fini(struct fs_sink_ctf_named_field_class *named_fc)
332 {
333 BT_ASSERT(named_fc);
334
335 if (named_fc->name) {
336 g_string_free(named_fc->name, TRUE);
337 named_fc->name = NULL;
338 }
339
340 fs_sink_ctf_field_class_destroy(named_fc->fc);
341 named_fc->fc = NULL;
342 }
343
344 static inline struct fs_sink_ctf_field_class_bit_array *
345 fs_sink_ctf_field_class_bit_array_create(const bt_field_class *ir_fc, uint64_t index_in_parent)
346 {
347 struct fs_sink_ctf_field_class_bit_array *fc =
348 g_new0(struct fs_sink_ctf_field_class_bit_array, 1);
349
350 BT_ASSERT(fc);
351 _fs_sink_ctf_field_class_bit_array_init(
352 fc, FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY, ir_fc,
353 (unsigned int) bt_field_class_bit_array_get_length(ir_fc), index_in_parent);
354 return fc;
355 }
356
357 static inline struct fs_sink_ctf_field_class_bool *
358 fs_sink_ctf_field_class_bool_create(const bt_field_class *ir_fc, uint64_t index_in_parent)
359 {
360 struct fs_sink_ctf_field_class_bool *fc = g_new0(struct fs_sink_ctf_field_class_bool, 1);
361
362 BT_ASSERT(fc);
363
364 /*
365 * CTF 1.8 has no boolean field class type, so this component
366 * translates it to an 8-bit unsigned integer field class.
367 */
368 _fs_sink_ctf_field_class_bit_array_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL, ir_fc, 8,
369 index_in_parent);
370 return fc;
371 }
372
373 static inline struct fs_sink_ctf_field_class_int *
374 fs_sink_ctf_field_class_int_create(const bt_field_class *ir_fc, uint64_t index_in_parent)
375 {
376 struct fs_sink_ctf_field_class_int *fc = g_new0(struct fs_sink_ctf_field_class_int, 1);
377
378 BT_ASSERT(fc);
379 _fs_sink_ctf_field_class_int_init(fc, FS_SINK_CTF_FIELD_CLASS_TYPE_INT, ir_fc, index_in_parent);
380 return fc;
381 }
382
383 static inline struct fs_sink_ctf_field_class_float *
384 fs_sink_ctf_field_class_float_create(const bt_field_class *ir_fc, uint64_t index_in_parent)
385 {
386 struct fs_sink_ctf_field_class_float *fc = g_new0(struct fs_sink_ctf_field_class_float, 1);
387
388 BT_ASSERT(fc);
389 _fs_sink_ctf_field_class_bit_array_init(
390 &fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT, ir_fc,
391 bt_field_class_get_type(ir_fc) == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL ? 32 : 64,
392 index_in_parent);
393 return fc;
394 }
395
396 static inline struct fs_sink_ctf_field_class_string *
397 fs_sink_ctf_field_class_string_create(const bt_field_class *ir_fc, uint64_t index_in_parent)
398 {
399 struct fs_sink_ctf_field_class_string *fc = g_new0(struct fs_sink_ctf_field_class_string, 1);
400
401 BT_ASSERT(fc);
402 _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, ir_fc, 8,
403 index_in_parent);
404 return fc;
405 }
406
407 static inline struct fs_sink_ctf_field_class_struct *
408 fs_sink_ctf_field_class_struct_create_empty(const bt_field_class *ir_fc, uint64_t index_in_parent)
409 {
410 struct fs_sink_ctf_field_class_struct *fc = g_new0(struct fs_sink_ctf_field_class_struct, 1);
411
412 BT_ASSERT(fc);
413 _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, ir_fc, 1,
414 index_in_parent);
415 fc->members = g_array_new(FALSE, TRUE, sizeof(struct fs_sink_ctf_named_field_class));
416 BT_ASSERT(fc->members);
417 return fc;
418 }
419
420 static inline struct fs_sink_ctf_field_class_option *
421 fs_sink_ctf_field_class_option_create_empty(const bt_field_class *ir_fc, uint64_t index_in_parent)
422 {
423 struct fs_sink_ctf_field_class_option *fc = g_new0(struct fs_sink_ctf_field_class_option, 1);
424
425 BT_ASSERT(fc);
426 _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION, ir_fc, 1,
427 index_in_parent);
428 fc->tag_ref = g_string_new(NULL);
429 BT_ASSERT(fc->tag_ref);
430 return fc;
431 }
432
433 static inline struct fs_sink_ctf_field_class_variant *
434 fs_sink_ctf_field_class_variant_create_empty(const bt_field_class *ir_fc, uint64_t index_in_parent)
435 {
436 struct fs_sink_ctf_field_class_variant *fc = g_new0(struct fs_sink_ctf_field_class_variant, 1);
437
438 BT_ASSERT(fc);
439 _fs_sink_ctf_field_class_init(&fc->base, FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, ir_fc, 1,
440 index_in_parent);
441 fc->options = g_array_new(FALSE, TRUE, sizeof(struct fs_sink_ctf_named_field_class));
442 BT_ASSERT(fc->options);
443 fc->tag_ref = g_string_new(NULL);
444 BT_ASSERT(fc->tag_ref);
445 fc->tag_is_before = bt_field_class_get_type(fc->base.ir_fc) ==
446 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD;
447 return fc;
448 }
449
450 static inline struct fs_sink_ctf_field_class_array *
451 fs_sink_ctf_field_class_array_create_empty(const bt_field_class *ir_fc, uint64_t index_in_parent)
452 {
453 struct fs_sink_ctf_field_class_array *fc = g_new0(struct fs_sink_ctf_field_class_array, 1);
454
455 BT_ASSERT(fc);
456 _fs_sink_ctf_field_class_init(&fc->base.base, FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, ir_fc, 1,
457 index_in_parent);
458 fc->length = bt_field_class_array_static_get_length(ir_fc);
459 return fc;
460 }
461
462 static inline struct fs_sink_ctf_field_class_sequence *
463 fs_sink_ctf_field_class_sequence_create_empty(const bt_field_class *ir_fc, uint64_t index_in_parent)
464 {
465 struct fs_sink_ctf_field_class_sequence *fc =
466 g_new0(struct fs_sink_ctf_field_class_sequence, 1);
467
468 BT_ASSERT(fc);
469 _fs_sink_ctf_field_class_init(&fc->base.base, FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE, ir_fc, 1,
470 index_in_parent);
471 fc->length_ref = g_string_new(NULL);
472 BT_ASSERT(fc->length_ref);
473 fc->length_is_before =
474 bt_field_class_get_type(ir_fc) == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD;
475 return fc;
476 }
477
478 static inline struct fs_sink_ctf_named_field_class *
479 fs_sink_ctf_field_class_struct_borrow_member_by_index(struct fs_sink_ctf_field_class_struct *fc,
480 uint64_t index);
481
482 static inline struct fs_sink_ctf_named_field_class *
483 fs_sink_ctf_field_class_variant_borrow_option_by_index(struct fs_sink_ctf_field_class_variant *fc,
484 uint64_t index);
485
486 static inline void _fs_sink_ctf_field_class_fini(struct fs_sink_ctf_field_class *fc)
487 {
488 BT_ASSERT(fc);
489 }
490
491 static inline void
492 _fs_sink_ctf_field_class_bit_array_destroy(struct fs_sink_ctf_field_class_bit_array *fc)
493 {
494 BT_ASSERT(fc);
495 _fs_sink_ctf_field_class_fini(&fc->base);
496 g_free(fc);
497 }
498
499 static inline void _fs_sink_ctf_field_class_bool_destroy(struct fs_sink_ctf_field_class_bool *fc)
500 {
501 BT_ASSERT(fc);
502 _fs_sink_ctf_field_class_fini(&fc->base.base);
503 g_free(fc);
504 }
505
506 static inline void _fs_sink_ctf_field_class_int_destroy(struct fs_sink_ctf_field_class_int *fc)
507 {
508 BT_ASSERT(fc);
509 _fs_sink_ctf_field_class_fini(&fc->base.base);
510 g_free(fc);
511 }
512
513 static inline void _fs_sink_ctf_field_class_float_destroy(struct fs_sink_ctf_field_class_float *fc)
514 {
515 BT_ASSERT(fc);
516 _fs_sink_ctf_field_class_fini(&fc->base.base);
517 g_free(fc);
518 }
519
520 static inline void
521 _fs_sink_ctf_field_class_string_destroy(struct fs_sink_ctf_field_class_string *fc)
522 {
523 BT_ASSERT(fc);
524 _fs_sink_ctf_field_class_fini(&fc->base);
525 g_free(fc);
526 }
527
528 static inline void
529 _fs_sink_ctf_field_class_struct_destroy(struct fs_sink_ctf_field_class_struct *fc)
530 {
531 BT_ASSERT(fc);
532 _fs_sink_ctf_field_class_fini(&fc->base);
533
534 if (fc->members) {
535 uint64_t i;
536
537 for (i = 0; i < fc->members->len; i++) {
538 struct fs_sink_ctf_named_field_class *named_fc =
539 fs_sink_ctf_field_class_struct_borrow_member_by_index(fc, i);
540
541 _fs_sink_ctf_named_field_class_fini(named_fc);
542 }
543
544 g_array_free(fc->members, TRUE);
545 fc->members = NULL;
546 }
547
548 g_free(fc);
549 }
550
551 static inline void
552 _fs_sink_ctf_field_class_array_base_fini(struct fs_sink_ctf_field_class_array_base *fc)
553 {
554 BT_ASSERT(fc);
555 _fs_sink_ctf_field_class_fini(&fc->base);
556 fs_sink_ctf_field_class_destroy(fc->elem_fc);
557 fc->elem_fc = NULL;
558 }
559
560 static inline void _fs_sink_ctf_field_class_array_destroy(struct fs_sink_ctf_field_class_array *fc)
561 {
562 BT_ASSERT(fc);
563 _fs_sink_ctf_field_class_array_base_fini(&fc->base);
564 g_free(fc);
565 }
566
567 static inline void
568 _fs_sink_ctf_field_class_sequence_destroy(struct fs_sink_ctf_field_class_sequence *fc)
569 {
570 BT_ASSERT(fc);
571 _fs_sink_ctf_field_class_array_base_fini(&fc->base);
572
573 if (fc->length_ref) {
574 g_string_free(fc->length_ref, TRUE);
575 fc->length_ref = NULL;
576 }
577
578 g_free(fc);
579 }
580
581 static inline void
582 _fs_sink_ctf_field_class_option_destroy(struct fs_sink_ctf_field_class_option *fc)
583 {
584 BT_ASSERT(fc);
585 _fs_sink_ctf_field_class_fini(&fc->base);
586 fs_sink_ctf_field_class_destroy(fc->content_fc);
587
588 if (fc->tag_ref) {
589 g_string_free(fc->tag_ref, TRUE);
590 fc->tag_ref = NULL;
591 }
592
593 g_free(fc);
594 }
595
596 static inline void
597 _fs_sink_ctf_field_class_variant_destroy(struct fs_sink_ctf_field_class_variant *fc)
598 {
599 BT_ASSERT(fc);
600 _fs_sink_ctf_field_class_fini(&fc->base);
601
602 if (fc->options) {
603 uint64_t i;
604
605 for (i = 0; i < fc->options->len; i++) {
606 struct fs_sink_ctf_named_field_class *named_fc =
607 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, i);
608
609 _fs_sink_ctf_named_field_class_fini(named_fc);
610 }
611
612 g_array_free(fc->options, TRUE);
613 fc->options = NULL;
614 }
615
616 if (fc->tag_ref) {
617 g_string_free(fc->tag_ref, TRUE);
618 fc->tag_ref = NULL;
619 }
620
621 g_free(fc);
622 }
623
624 static inline void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc)
625 {
626 if (!fc) {
627 return;
628 }
629
630 switch (fc->type) {
631 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL:
632 _fs_sink_ctf_field_class_bool_destroy(fs_sink_ctf_field_class_as_bool(fc));
633 break;
634 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY:
635 _fs_sink_ctf_field_class_bit_array_destroy(fs_sink_ctf_field_class_as_bit_array(fc));
636 break;
637 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT:
638 _fs_sink_ctf_field_class_int_destroy(fs_sink_ctf_field_class_as_int(fc));
639 break;
640 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT:
641 _fs_sink_ctf_field_class_float_destroy(fs_sink_ctf_field_class_as_float(fc));
642 break;
643 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING:
644 _fs_sink_ctf_field_class_string_destroy(fs_sink_ctf_field_class_as_string(fc));
645 break;
646 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT:
647 _fs_sink_ctf_field_class_struct_destroy(fs_sink_ctf_field_class_as_struct(fc));
648 break;
649 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY:
650 _fs_sink_ctf_field_class_array_destroy(fs_sink_ctf_field_class_as_array(fc));
651 break;
652 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE:
653 _fs_sink_ctf_field_class_sequence_destroy(fs_sink_ctf_field_class_as_sequence(fc));
654 break;
655 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION:
656 _fs_sink_ctf_field_class_option_destroy(fs_sink_ctf_field_class_as_option(fc));
657 break;
658 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT:
659 _fs_sink_ctf_field_class_variant_destroy(fs_sink_ctf_field_class_as_variant(fc));
660 break;
661 default:
662 bt_common_abort();
663 }
664 }
665
666 static inline struct fs_sink_ctf_named_field_class *
667 fs_sink_ctf_field_class_struct_borrow_member_by_index(struct fs_sink_ctf_field_class_struct *fc,
668 uint64_t index)
669 {
670 BT_ASSERT_DBG(fc);
671 BT_ASSERT_DBG(index < fc->members->len);
672 return &bt_g_array_index(fc->members, struct fs_sink_ctf_named_field_class, index);
673 }
674
675 static inline struct fs_sink_ctf_named_field_class *
676 fs_sink_ctf_field_class_struct_borrow_member_by_name(struct fs_sink_ctf_field_class_struct *fc,
677 const char *name)
678 {
679 uint64_t i;
680 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
681
682 BT_ASSERT_DBG(fc);
683 BT_ASSERT_DBG(name);
684
685 for (i = 0; i < fc->members->len; i++) {
686 struct fs_sink_ctf_named_field_class *named_fc =
687 fs_sink_ctf_field_class_struct_borrow_member_by_index(fc, i);
688
689 if (strcmp(name, named_fc->name->str) == 0) {
690 ret_named_fc = named_fc;
691 goto end;
692 }
693 }
694
695 end:
696 return ret_named_fc;
697 }
698
699 static inline struct fs_sink_ctf_field_class *
700 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(
701 struct fs_sink_ctf_field_class_struct *struct_fc, const char *name)
702 {
703 struct fs_sink_ctf_named_field_class *named_fc = NULL;
704 struct fs_sink_ctf_field_class *fc = NULL;
705
706 if (!struct_fc) {
707 goto end;
708 }
709
710 named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
711 if (!named_fc) {
712 goto end;
713 }
714
715 fc = named_fc->fc;
716
717 end:
718 return fc;
719 }
720
721 static inline struct fs_sink_ctf_field_class_int *
722 fs_sink_ctf_field_class_struct_borrow_member_int_field_class_by_name(
723 struct fs_sink_ctf_field_class_struct *struct_fc, const char *name)
724 {
725 struct fs_sink_ctf_field_class_int *int_fc = NULL;
726
727 int_fc = fs_sink_ctf_field_class_as_int(
728 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name));
729 if (!int_fc) {
730 goto end;
731 }
732
733 if (int_fc->base.base.type != FS_SINK_CTF_FIELD_CLASS_TYPE_INT) {
734 int_fc = NULL;
735 goto end;
736 }
737
738 end:
739 return int_fc;
740 }
741
742 static inline void
743 fs_sink_ctf_field_class_struct_align_at_least(struct fs_sink_ctf_field_class_struct *fc,
744 unsigned int alignment)
745 {
746 if (alignment > fc->base.alignment) {
747 fc->base.alignment = alignment;
748 }
749 }
750
751 static inline void
752 fs_sink_ctf_field_class_struct_append_member(struct fs_sink_ctf_field_class_struct *fc,
753 const char *name,
754 struct fs_sink_ctf_field_class *member_fc)
755 {
756 struct fs_sink_ctf_named_field_class *named_fc;
757
758 BT_ASSERT(fc);
759 BT_ASSERT(name);
760 g_array_set_size(fc->members, fc->members->len + 1);
761
762 named_fc =
763 &bt_g_array_index(fc->members, struct fs_sink_ctf_named_field_class, fc->members->len - 1);
764 _fs_sink_ctf_named_field_class_init(named_fc);
765 g_string_assign(named_fc->name, name);
766 named_fc->fc = member_fc;
767 fs_sink_ctf_field_class_struct_align_at_least(fc, member_fc->alignment);
768 }
769
770 static inline struct fs_sink_ctf_named_field_class *
771 fs_sink_ctf_field_class_variant_borrow_option_by_index(struct fs_sink_ctf_field_class_variant *fc,
772 uint64_t index)
773 {
774 BT_ASSERT_DBG(fc);
775 BT_ASSERT_DBG(index < fc->options->len);
776 return &bt_g_array_index(fc->options, struct fs_sink_ctf_named_field_class, index);
777 }
778
779 static inline struct fs_sink_ctf_named_field_class *
780 fs_sink_ctf_field_class_variant_borrow_option_by_name(struct fs_sink_ctf_field_class_variant *fc,
781 const char *name)
782 {
783 uint64_t i;
784 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
785
786 BT_ASSERT_DBG(fc);
787 BT_ASSERT_DBG(name);
788
789 for (i = 0; i < fc->options->len; i++) {
790 struct fs_sink_ctf_named_field_class *named_fc =
791 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, i);
792
793 if (strcmp(name, named_fc->name->str) == 0) {
794 ret_named_fc = named_fc;
795 goto end;
796 }
797 }
798
799 end:
800 return ret_named_fc;
801 }
802
803 static inline void
804 fs_sink_ctf_field_class_variant_append_option(struct fs_sink_ctf_field_class_variant *fc,
805 const char *name,
806 struct fs_sink_ctf_field_class *option_fc)
807 {
808 struct fs_sink_ctf_named_field_class *named_fc;
809
810 BT_ASSERT(fc);
811 BT_ASSERT(name);
812 g_array_set_size(fc->options, fc->options->len + 1);
813
814 named_fc =
815 &bt_g_array_index(fc->options, struct fs_sink_ctf_named_field_class, fc->options->len - 1);
816 _fs_sink_ctf_named_field_class_init(named_fc);
817 g_string_assign(named_fc->name, name);
818 named_fc->fc = option_fc;
819 }
820
821 static inline struct fs_sink_ctf_event_class *
822 fs_sink_ctf_event_class_create(struct fs_sink_ctf_stream_class *sc, const bt_event_class *ir_ec)
823 {
824 struct fs_sink_ctf_event_class *ec = g_new0(struct fs_sink_ctf_event_class, 1);
825
826 BT_ASSERT(sc);
827 BT_ASSERT(ir_ec);
828 BT_ASSERT(ec);
829 ec->ir_ec = ir_ec;
830 ec->sc = sc;
831 g_ptr_array_add(sc->event_classes, ec);
832 g_hash_table_insert(sc->event_classes_from_ir, (gpointer) ir_ec, ec);
833 return ec;
834 }
835
836 static inline void fs_sink_ctf_event_class_destroy(struct fs_sink_ctf_event_class *ec)
837 {
838 if (!ec) {
839 return;
840 }
841
842 fs_sink_ctf_field_class_destroy(ec->spec_context_fc);
843 ec->spec_context_fc = NULL;
844 fs_sink_ctf_field_class_destroy(ec->payload_fc);
845 ec->payload_fc = NULL;
846 g_free(ec);
847 }
848
849 static inline struct fs_sink_ctf_stream_class *
850 fs_sink_ctf_stream_class_create(struct fs_sink_ctf_trace *trace, const bt_stream_class *ir_sc)
851 {
852 struct fs_sink_ctf_stream_class *sc = g_new0(struct fs_sink_ctf_stream_class, 1);
853
854 BT_ASSERT(trace);
855 BT_ASSERT(ir_sc);
856 BT_ASSERT(sc);
857 sc->trace = trace;
858 sc->ir_sc = ir_sc;
859 sc->default_clock_class = bt_stream_class_borrow_default_clock_class_const(ir_sc);
860 sc->default_clock_class_name = g_string_new(NULL);
861 BT_ASSERT(sc->default_clock_class_name);
862 sc->event_classes =
863 g_ptr_array_new_with_free_func((GDestroyNotify) fs_sink_ctf_event_class_destroy);
864 BT_ASSERT(sc->event_classes);
865 sc->event_classes_from_ir = g_hash_table_new(g_direct_hash, g_direct_equal);
866 BT_ASSERT(sc->event_classes_from_ir);
867 sc->has_packets = bt_stream_class_supports_packets(ir_sc);
868 sc->packets_have_ts_begin =
869 bt_stream_class_packets_have_beginning_default_clock_snapshot(ir_sc);
870 sc->packets_have_ts_end = bt_stream_class_packets_have_end_default_clock_snapshot(ir_sc);
871 sc->has_discarded_events = bt_stream_class_supports_discarded_events(ir_sc);
872
873 if (sc->has_discarded_events) {
874 sc->discarded_events_has_ts =
875 bt_stream_class_discarded_events_have_default_clock_snapshots(ir_sc);
876 }
877
878 if (bt_stream_class_supports_discarded_packets(ir_sc)) {
879 sc->discarded_packets_has_ts =
880 bt_stream_class_discarded_packets_have_default_clock_snapshots(ir_sc);
881 }
882
883 g_ptr_array_add(trace->stream_classes, sc);
884 return sc;
885 }
886
887 static inline void fs_sink_ctf_stream_class_destroy(struct fs_sink_ctf_stream_class *sc)
888 {
889 if (!sc) {
890 return;
891 }
892
893 if (sc->default_clock_class_name) {
894 g_string_free(sc->default_clock_class_name, TRUE);
895 sc->default_clock_class_name = NULL;
896 }
897
898 if (sc->event_classes) {
899 g_ptr_array_free(sc->event_classes, TRUE);
900 sc->event_classes = NULL;
901 }
902
903 if (sc->event_classes_from_ir) {
904 g_hash_table_destroy(sc->event_classes_from_ir);
905 sc->event_classes_from_ir = NULL;
906 }
907
908 fs_sink_ctf_field_class_destroy(sc->packet_context_fc);
909 sc->packet_context_fc = NULL;
910 fs_sink_ctf_field_class_destroy(sc->event_common_context_fc);
911 sc->event_common_context_fc = NULL;
912 g_free(sc);
913 }
914
915 static inline void fs_sink_ctf_stream_class_append_event_class(struct fs_sink_ctf_stream_class *sc,
916 struct fs_sink_ctf_event_class *ec)
917 {
918 g_ptr_array_add(sc->event_classes, ec);
919 }
920
921 static inline void fs_sink_ctf_trace_destroy(struct fs_sink_ctf_trace *trace)
922 {
923 if (!trace) {
924 return;
925 }
926
927 if (trace->stream_classes) {
928 g_ptr_array_free(trace->stream_classes, TRUE);
929 trace->stream_classes = NULL;
930 }
931
932 g_free(trace);
933 }
934
935 static inline struct fs_sink_ctf_trace *fs_sink_ctf_trace_create(const bt_trace *ir_trace)
936 {
937 struct fs_sink_ctf_trace *trace = g_new0(struct fs_sink_ctf_trace, 1);
938
939 BT_ASSERT(trace);
940
941 bt_uuid_generate(trace->uuid);
942
943 trace->ir_trace = ir_trace;
944 trace->ir_tc = bt_trace_borrow_class_const(ir_trace);
945 trace->stream_classes =
946 g_ptr_array_new_with_free_func((GDestroyNotify) fs_sink_ctf_stream_class_destroy);
947 BT_ASSERT(trace->stream_classes);
948
949 return trace;
950 }
951
952 #endif /* BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H */
This page took 0.058613 seconds and 4 git commands to generate.