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