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