cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / ctf-meta.hpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #ifndef _CTF_META_H
8 #define _CTF_META_H
9
10 #include <glib.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 #include <babeltrace2/babeltrace.h>
15
16 #include "common/assert.h"
17 #include "common/common.h"
18 #include "common/uuid.h"
19 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
20
21 enum ctf_field_class_type
22 {
23 CTF_FIELD_CLASS_TYPE_INT,
24 CTF_FIELD_CLASS_TYPE_ENUM,
25 CTF_FIELD_CLASS_TYPE_FLOAT,
26 CTF_FIELD_CLASS_TYPE_STRING,
27 CTF_FIELD_CLASS_TYPE_STRUCT,
28 CTF_FIELD_CLASS_TYPE_ARRAY,
29 CTF_FIELD_CLASS_TYPE_SEQUENCE,
30 CTF_FIELD_CLASS_TYPE_VARIANT,
31 };
32
33 inline const char *format_as(ctf_field_class_type type) noexcept
34 {
35 switch (type) {
36 case CTF_FIELD_CLASS_TYPE_INT:
37 return "CTF_FIELD_CLASS_TYPE_INT";
38
39 case CTF_FIELD_CLASS_TYPE_ENUM:
40 return "CTF_FIELD_CLASS_TYPE_ENUM";
41
42 case CTF_FIELD_CLASS_TYPE_FLOAT:
43 return "CTF_FIELD_CLASS_TYPE_FLOAT";
44
45 case CTF_FIELD_CLASS_TYPE_STRING:
46 return "CTF_FIELD_CLASS_TYPE_STRING";
47
48 case CTF_FIELD_CLASS_TYPE_STRUCT:
49 return "CTF_FIELD_CLASS_TYPE_STRUCT";
50
51 case CTF_FIELD_CLASS_TYPE_ARRAY:
52 return "CTF_FIELD_CLASS_TYPE_ARRAY";
53
54 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
55 return "CTF_FIELD_CLASS_TYPE_SEQUENCE";
56
57 case CTF_FIELD_CLASS_TYPE_VARIANT:
58 return "CTF_FIELD_CLASS_TYPE_VARIANT";
59 }
60
61 bt_common_abort();
62 }
63
64 enum ctf_field_class_meaning
65 {
66 CTF_FIELD_CLASS_MEANING_NONE,
67 CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME,
68 CTF_FIELD_CLASS_MEANING_PACKET_END_TIME,
69 CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID,
70 CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID,
71 CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID,
72 CTF_FIELD_CLASS_MEANING_MAGIC,
73 CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT,
74 CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT,
75 CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE,
76 CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE,
77 CTF_FIELD_CLASS_MEANING_UUID,
78 };
79
80 enum ctf_byte_order
81 {
82 CTF_BYTE_ORDER_UNKNOWN,
83 CTF_BYTE_ORDER_DEFAULT,
84 CTF_BYTE_ORDER_LITTLE,
85 CTF_BYTE_ORDER_BIG,
86 };
87
88 enum ctf_encoding
89 {
90 CTF_ENCODING_NONE,
91 CTF_ENCODING_UTF8,
92 };
93
94 enum ctf_scope
95 {
96 CTF_SCOPE_PACKET_UNKNOWN = -1,
97 CTF_SCOPE_PACKET_HEADER = 0,
98 CTF_SCOPE_PACKET_CONTEXT,
99 CTF_SCOPE_EVENT_HEADER,
100 CTF_SCOPE_EVENT_COMMON_CONTEXT,
101 CTF_SCOPE_EVENT_SPECIFIC_CONTEXT,
102 CTF_SCOPE_EVENT_PAYLOAD,
103 };
104
105 inline const char *format_as(const ctf_scope scope) noexcept
106 {
107 switch (scope) {
108 case CTF_SCOPE_PACKET_UNKNOWN:
109 return "PACKET_UNKNOWN";
110
111 case CTF_SCOPE_PACKET_HEADER:
112 return "PACKET_HEADER";
113
114 case CTF_SCOPE_PACKET_CONTEXT:
115 return "PACKET_CONTEXT";
116
117 case CTF_SCOPE_EVENT_HEADER:
118 return "EVENT_HEADER";
119
120 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
121 return "EVENT_COMMON_CONTEXT";
122
123 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
124 return "EVENT_SPECIFIC_CONTEXT";
125
126 case CTF_SCOPE_EVENT_PAYLOAD:
127 return "EVENT_PAYLOAD";
128 }
129
130 bt_common_abort();
131 }
132
133 struct ctf_clock_class
134 {
135 GString *name;
136 GString *description;
137 uint64_t frequency;
138 uint64_t precision;
139 int64_t offset_seconds;
140 uint64_t offset_cycles;
141 bt_uuid_t uuid;
142 bool has_uuid;
143 bool is_absolute;
144
145 /* Weak, set during translation */
146 bt_clock_class *ir_cc;
147 };
148
149 struct ctf_field_class
150 {
151 enum ctf_field_class_type type;
152 unsigned int alignment;
153 bool is_compound;
154 bool in_ir;
155
156 /* Weak, set during translation. NULL if `in_ir` is false below. */
157 bt_field_class *ir_fc;
158 };
159
160 struct ctf_field_class_bit_array
161 {
162 struct ctf_field_class base;
163 enum ctf_byte_order byte_order;
164 unsigned int size;
165 };
166
167 struct ctf_field_class_int
168 {
169 struct ctf_field_class_bit_array base;
170 enum ctf_field_class_meaning meaning;
171 bool is_signed;
172 bt_field_class_integer_preferred_display_base disp_base;
173 enum ctf_encoding encoding;
174 int64_t storing_index;
175
176 /* Weak */
177 struct ctf_clock_class *mapped_clock_class;
178 };
179
180 struct ctf_range
181 {
182 union
183 {
184 uint64_t u;
185 int64_t i;
186 } lower;
187
188 union
189 {
190 uint64_t u;
191 int64_t i;
192 } upper;
193 };
194
195 struct ctf_field_class_enum_mapping
196 {
197 GString *label;
198
199 /* Array of `struct ctf_range` */
200 GArray *ranges;
201 };
202
203 struct ctf_field_class_enum
204 {
205 struct ctf_field_class_int base;
206
207 /* Array of `struct ctf_field_class_enum_mapping` */
208 GArray *mappings;
209 };
210
211 struct ctf_field_class_float
212 {
213 struct ctf_field_class_bit_array base;
214 };
215
216 struct ctf_field_class_string
217 {
218 struct ctf_field_class base;
219 enum ctf_encoding encoding;
220 };
221
222 struct ctf_named_field_class
223 {
224 /* Original name which can include a leading `_` */
225 GString *orig_name;
226
227 /* Name as translated to trace IR (leading `_` removed) */
228 GString *name;
229
230 /* Owned by this */
231 struct ctf_field_class *fc;
232 };
233
234 struct ctf_field_class_struct
235 {
236 struct ctf_field_class base;
237
238 /* Array of `struct ctf_named_field_class` */
239 GArray *members;
240 };
241
242 struct ctf_field_path
243 {
244 enum ctf_scope root;
245
246 /* Array of `int64_t` */
247 GArray *path;
248 };
249
250 struct ctf_field_class_variant_range
251 {
252 struct ctf_range range;
253 uint64_t option_index;
254 };
255
256 struct ctf_field_class_variant
257 {
258 struct ctf_field_class base;
259 GString *tag_ref;
260 struct ctf_field_path tag_path;
261 uint64_t stored_tag_index;
262
263 /* Array of `struct ctf_named_field_class` */
264 GArray *options;
265
266 /* Array of `struct ctf_field_class_variant_range` */
267 GArray *ranges;
268
269 /* Weak */
270 struct ctf_field_class_enum *tag_fc;
271 };
272
273 struct ctf_field_class_array_base
274 {
275 struct ctf_field_class base;
276 struct ctf_field_class *elem_fc;
277 bool is_text;
278 };
279
280 struct ctf_field_class_array
281 {
282 struct ctf_field_class_array_base base;
283 enum ctf_field_class_meaning meaning;
284 uint64_t length;
285 };
286
287 struct ctf_field_class_sequence
288 {
289 struct ctf_field_class_array_base base;
290 GString *length_ref;
291 struct ctf_field_path length_path;
292 uint64_t stored_length_index;
293
294 /* Weak */
295 struct ctf_field_class_int *length_fc;
296 };
297
298 struct ctf_event_class
299 {
300 GString *name;
301 uint64_t id;
302 GString *emf_uri;
303 bt_event_class_log_level log_level;
304 bool is_translated;
305 bool is_log_level_set;
306
307 /* Owned by this */
308 struct ctf_field_class *spec_context_fc;
309
310 /* Owned by this */
311 struct ctf_field_class *payload_fc;
312
313 /* Weak, set during translation */
314 bt_event_class *ir_ec;
315 };
316
317 struct ctf_stream_class
318 {
319 uint64_t id;
320 bool is_translated;
321 bool packets_have_ts_begin;
322 bool packets_have_ts_end;
323 bool has_discarded_events;
324 bool has_discarded_packets;
325 bool discarded_events_have_default_cs;
326 bool discarded_packets_have_default_cs;
327
328 /* Owned by this */
329 struct ctf_field_class *packet_context_fc;
330
331 /* Owned by this */
332 struct ctf_field_class *event_header_fc;
333
334 /* Owned by this */
335 struct ctf_field_class *event_common_context_fc;
336
337 /* Array of `struct ctf_event_class *`, owned by this */
338 GPtrArray *event_classes;
339
340 /*
341 * Hash table mapping event class IDs to `struct ctf_event_class *`,
342 * weak.
343 */
344 GHashTable *event_classes_by_id;
345
346 /* Weak */
347 struct ctf_clock_class *default_clock_class;
348
349 /* Weak, set during translation */
350 bt_stream_class *ir_sc;
351 };
352
353 enum ctf_trace_class_env_entry_type
354 {
355 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
356 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
357 };
358
359 struct ctf_trace_class_env_entry
360 {
361 enum ctf_trace_class_env_entry_type type;
362 GString *name;
363
364 struct
365 {
366 int64_t i;
367 GString *str;
368 } value;
369 };
370
371 struct ctf_trace_class
372 {
373 unsigned int major;
374 unsigned int minor;
375 bt_uuid_t uuid;
376 bool is_uuid_set;
377 enum ctf_byte_order default_byte_order;
378
379 /* Owned by this */
380 struct ctf_field_class *packet_header_fc;
381
382 uint64_t stored_value_count;
383
384 /* Array of `struct ctf_clock_class *` (owned by this) */
385 GPtrArray *clock_classes;
386
387 /* Array of `struct ctf_stream_class *` */
388 GPtrArray *stream_classes;
389
390 /* Array of `struct ctf_trace_class_env_entry` */
391 GArray *env_entries;
392
393 bool is_translated;
394
395 /* Weak, set during translation */
396 bt_trace_class *ir_tc;
397
398 struct
399 {
400 bool lttng_crash;
401 bool lttng_event_after_packet;
402 bool barectf_event_before_packet;
403 } quirks;
404 };
405
406 static inline ctf_field_class_bit_array *ctf_field_class_as_bit_array(ctf_field_class *fc)
407 {
408 BT_ASSERT_DBG(!fc ||
409 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM ||
410 fc->type == CTF_FIELD_CLASS_TYPE_FLOAT));
411 return (ctf_field_class_bit_array *) fc;
412 }
413
414 static inline ctf_field_class_int *ctf_field_class_as_int(ctf_field_class *fc)
415 {
416 BT_ASSERT_DBG(!fc ||
417 (fc->type == CTF_FIELD_CLASS_TYPE_INT || fc->type == CTF_FIELD_CLASS_TYPE_ENUM));
418 return (ctf_field_class_int *) fc;
419 }
420
421 static inline ctf_field_class_enum *ctf_field_class_as_enum(ctf_field_class *fc)
422 {
423 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ENUM);
424 return (ctf_field_class_enum *) fc;
425 }
426
427 static inline ctf_field_class_float *ctf_field_class_as_float(ctf_field_class *fc)
428 {
429 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_FLOAT);
430 return (ctf_field_class_float *) fc;
431 }
432
433 static inline ctf_field_class_string *ctf_field_class_as_string(ctf_field_class *fc)
434 {
435 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRING);
436 return (ctf_field_class_string *) fc;
437 }
438
439 static inline ctf_field_class_struct *ctf_field_class_as_struct(ctf_field_class *fc)
440 {
441 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_STRUCT);
442 return (ctf_field_class_struct *) fc;
443 }
444
445 static inline ctf_field_class_array_base *ctf_field_class_as_array_base(ctf_field_class *fc)
446 {
447 BT_ASSERT_DBG(!fc || (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
448 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE));
449 return (ctf_field_class_array_base *) fc;
450 }
451
452 static inline ctf_field_class_array *ctf_field_class_as_array(ctf_field_class *fc)
453 {
454 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_ARRAY);
455 return (ctf_field_class_array *) fc;
456 }
457
458 static inline ctf_field_class_sequence *ctf_field_class_as_sequence(ctf_field_class *fc)
459 {
460 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE);
461 return (ctf_field_class_sequence *) fc;
462 }
463
464 static inline ctf_field_class_variant *ctf_field_class_as_variant(ctf_field_class *fc)
465 {
466 BT_ASSERT_DBG(!fc || fc->type == CTF_FIELD_CLASS_TYPE_VARIANT);
467 return (ctf_field_class_variant *) fc;
468 }
469
470 static inline void ctf_field_class_destroy(struct ctf_field_class *fc);
471
472 static inline void _ctf_field_class_init(struct ctf_field_class *fc, enum ctf_field_class_type type,
473 unsigned int alignment)
474 {
475 BT_ASSERT(fc);
476 fc->type = type;
477 fc->alignment = alignment;
478 fc->in_ir = false;
479 }
480
481 static inline void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
482 enum ctf_field_class_type type)
483 {
484 _ctf_field_class_init(&fc->base, type, 1);
485 }
486
487 static inline void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
488 enum ctf_field_class_type type)
489 {
490 _ctf_field_class_bit_array_init(&fc->base, type);
491 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
492 fc->storing_index = -1;
493 }
494
495 static inline void ctf_field_path_init(struct ctf_field_path *field_path)
496 {
497 BT_ASSERT(field_path);
498 field_path->path = g_array_new(FALSE, TRUE, sizeof(int64_t));
499 BT_ASSERT(field_path->path);
500 }
501
502 static inline void ctf_field_path_fini(struct ctf_field_path *field_path)
503 {
504 BT_ASSERT(field_path);
505
506 if (field_path->path) {
507 g_array_free(field_path->path, TRUE);
508 }
509 }
510
511 static inline void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
512 {
513 BT_ASSERT(named_fc);
514 named_fc->name = g_string_new(NULL);
515 BT_ASSERT(named_fc->name);
516 named_fc->orig_name = g_string_new(NULL);
517 BT_ASSERT(named_fc->orig_name);
518 }
519
520 static inline void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
521 {
522 BT_ASSERT(named_fc);
523
524 if (named_fc->name) {
525 g_string_free(named_fc->name, TRUE);
526 }
527
528 if (named_fc->orig_name) {
529 g_string_free(named_fc->orig_name, TRUE);
530 }
531
532 ctf_field_class_destroy(named_fc->fc);
533 }
534
535 static inline void _ctf_field_class_enum_mapping_init(struct ctf_field_class_enum_mapping *mapping)
536 {
537 BT_ASSERT(mapping);
538 mapping->label = g_string_new(NULL);
539 BT_ASSERT(mapping->label);
540 mapping->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_range));
541 BT_ASSERT(mapping->ranges);
542 }
543
544 static inline void _ctf_field_class_enum_mapping_fini(struct ctf_field_class_enum_mapping *mapping)
545 {
546 BT_ASSERT(mapping);
547
548 if (mapping->label) {
549 g_string_free(mapping->label, TRUE);
550 }
551
552 if (mapping->ranges) {
553 g_array_free(mapping->ranges, TRUE);
554 }
555 }
556
557 static inline struct ctf_field_class_int *ctf_field_class_int_create(void)
558 {
559 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
560
561 BT_ASSERT(fc);
562 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
563 return fc;
564 }
565
566 static inline struct ctf_field_class_float *ctf_field_class_float_create(void)
567 {
568 struct ctf_field_class_float *fc = g_new0(struct ctf_field_class_float, 1);
569
570 BT_ASSERT(fc);
571 _ctf_field_class_bit_array_init(&fc->base, CTF_FIELD_CLASS_TYPE_FLOAT);
572 return fc;
573 }
574
575 static inline struct ctf_field_class_string *ctf_field_class_string_create(void)
576 {
577 struct ctf_field_class_string *fc = g_new0(struct ctf_field_class_string, 1);
578
579 BT_ASSERT(fc);
580 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRING, 8);
581 return fc;
582 }
583
584 static inline struct ctf_field_class_enum *ctf_field_class_enum_create(void)
585 {
586 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
587
588 BT_ASSERT(fc);
589 _ctf_field_class_int_init(&fc->base, CTF_FIELD_CLASS_TYPE_ENUM);
590 fc->mappings = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_enum_mapping));
591 BT_ASSERT(fc->mappings);
592 return fc;
593 }
594
595 static inline struct ctf_field_class_struct *ctf_field_class_struct_create(void)
596 {
597 struct ctf_field_class_struct *fc = g_new0(struct ctf_field_class_struct, 1);
598
599 BT_ASSERT(fc);
600 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
601 fc->members = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
602 BT_ASSERT(fc->members);
603 fc->base.is_compound = true;
604 return fc;
605 }
606
607 static inline struct ctf_field_class_variant *ctf_field_class_variant_create(void)
608 {
609 struct ctf_field_class_variant *fc = g_new0(struct ctf_field_class_variant, 1);
610
611 BT_ASSERT(fc);
612 _ctf_field_class_init(&fc->base, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
613 fc->options = g_array_new(FALSE, TRUE, sizeof(struct ctf_named_field_class));
614 BT_ASSERT(fc->options);
615 fc->ranges = g_array_new(FALSE, TRUE, sizeof(struct ctf_field_class_variant_range));
616 BT_ASSERT(fc->ranges);
617 fc->tag_ref = g_string_new(NULL);
618 BT_ASSERT(fc->tag_ref);
619 ctf_field_path_init(&fc->tag_path);
620 fc->base.is_compound = true;
621 return fc;
622 }
623
624 static inline struct ctf_field_class_array *ctf_field_class_array_create(void)
625 {
626 struct ctf_field_class_array *fc = g_new0(struct ctf_field_class_array, 1);
627
628 BT_ASSERT(fc);
629 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
630 fc->base.base.is_compound = true;
631 return fc;
632 }
633
634 static inline struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
635 {
636 struct ctf_field_class_sequence *fc = g_new0(struct ctf_field_class_sequence, 1);
637
638 BT_ASSERT(fc);
639 _ctf_field_class_init(&fc->base.base, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
640 fc->length_ref = g_string_new(NULL);
641 BT_ASSERT(fc->length_ref);
642 ctf_field_path_init(&fc->length_path);
643 fc->base.base.is_compound = true;
644 return fc;
645 }
646
647 static inline void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
648 {
649 BT_ASSERT(fc);
650 g_free(fc);
651 }
652
653 static inline void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
654 {
655 BT_ASSERT(fc);
656
657 if (fc->mappings) {
658 uint64_t i;
659
660 for (i = 0; i < fc->mappings->len; i++) {
661 struct ctf_field_class_enum_mapping *mapping =
662 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
663
664 _ctf_field_class_enum_mapping_fini(mapping);
665 }
666
667 g_array_free(fc->mappings, TRUE);
668 }
669
670 g_free(fc);
671 }
672
673 static inline void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
674 {
675 BT_ASSERT(fc);
676 g_free(fc);
677 }
678
679 static inline void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
680 {
681 BT_ASSERT(fc);
682 g_free(fc);
683 }
684
685 static inline void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
686 {
687 BT_ASSERT(fc);
688
689 if (fc->members) {
690 uint64_t i;
691
692 for (i = 0; i < fc->members->len; i++) {
693 struct ctf_named_field_class *named_fc =
694 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
695
696 _ctf_named_field_class_fini(named_fc);
697 }
698
699 g_array_free(fc->members, TRUE);
700 }
701
702 g_free(fc);
703 }
704
705 static inline void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
706 {
707 BT_ASSERT(fc);
708 ctf_field_class_destroy(fc->elem_fc);
709 }
710
711 static inline void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
712 {
713 BT_ASSERT(fc);
714 _ctf_field_class_array_base_fini(&fc->base);
715 g_free(fc);
716 }
717
718 static inline void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
719 {
720 BT_ASSERT(fc);
721 _ctf_field_class_array_base_fini(&fc->base);
722
723 if (fc->length_ref) {
724 g_string_free(fc->length_ref, TRUE);
725 }
726
727 ctf_field_path_fini(&fc->length_path);
728 g_free(fc);
729 }
730
731 static inline void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
732 {
733 BT_ASSERT(fc);
734
735 if (fc->options) {
736 uint64_t i;
737
738 for (i = 0; i < fc->options->len; i++) {
739 struct ctf_named_field_class *named_fc =
740 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
741
742 _ctf_named_field_class_fini(named_fc);
743 }
744
745 g_array_free(fc->options, TRUE);
746 }
747
748 if (fc->ranges) {
749 g_array_free(fc->ranges, TRUE);
750 }
751
752 if (fc->tag_ref) {
753 g_string_free(fc->tag_ref, TRUE);
754 }
755
756 ctf_field_path_fini(&fc->tag_path);
757 g_free(fc);
758 }
759
760 static inline void ctf_field_class_destroy(struct ctf_field_class *fc)
761 {
762 if (!fc) {
763 return;
764 }
765
766 switch (fc->type) {
767 case CTF_FIELD_CLASS_TYPE_INT:
768 _ctf_field_class_int_destroy(ctf_field_class_as_int(fc));
769 break;
770 case CTF_FIELD_CLASS_TYPE_ENUM:
771 _ctf_field_class_enum_destroy(ctf_field_class_as_enum(fc));
772 break;
773 case CTF_FIELD_CLASS_TYPE_FLOAT:
774 _ctf_field_class_float_destroy(ctf_field_class_as_float(fc));
775 break;
776 case CTF_FIELD_CLASS_TYPE_STRING:
777 _ctf_field_class_string_destroy(ctf_field_class_as_string(fc));
778 break;
779 case CTF_FIELD_CLASS_TYPE_STRUCT:
780 _ctf_field_class_struct_destroy(ctf_field_class_as_struct(fc));
781 break;
782 case CTF_FIELD_CLASS_TYPE_ARRAY:
783 _ctf_field_class_array_destroy(ctf_field_class_as_array(fc));
784 break;
785 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
786 _ctf_field_class_sequence_destroy(ctf_field_class_as_sequence(fc));
787 break;
788 case CTF_FIELD_CLASS_TYPE_VARIANT:
789 _ctf_field_class_variant_destroy(ctf_field_class_as_variant(fc));
790 break;
791 default:
792 bt_common_abort();
793 }
794 }
795
796 static inline struct ctf_range *
797 ctf_field_class_enum_mapping_borrow_range_by_index(struct ctf_field_class_enum_mapping *mapping,
798 uint64_t index)
799 {
800 BT_ASSERT_DBG(mapping);
801 BT_ASSERT_DBG(index < mapping->ranges->len);
802 return &bt_g_array_index(mapping->ranges, struct ctf_range, index);
803 }
804
805 static inline struct ctf_field_class_enum_mapping *
806 ctf_field_class_enum_borrow_mapping_by_index(struct ctf_field_class_enum *fc, uint64_t index)
807 {
808 BT_ASSERT_DBG(fc);
809 BT_ASSERT_DBG(index < fc->mappings->len);
810 return &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, index);
811 }
812
813 static inline struct ctf_field_class_enum_mapping *
814 ctf_field_class_enum_borrow_mapping_by_label(struct ctf_field_class_enum *fc, const char *label)
815 {
816 struct ctf_field_class_enum_mapping *ret_mapping = NULL;
817 uint64_t i;
818
819 BT_ASSERT_DBG(fc);
820 BT_ASSERT_DBG(label);
821
822 for (i = 0; i < fc->mappings->len; i++) {
823 struct ctf_field_class_enum_mapping *mapping =
824 ctf_field_class_enum_borrow_mapping_by_index(fc, i);
825
826 if (strcmp(mapping->label->str, label) == 0) {
827 ret_mapping = mapping;
828 goto end;
829 }
830 }
831
832 end:
833 return ret_mapping;
834 }
835
836 static inline void ctf_field_class_enum_map_range(struct ctf_field_class_enum *fc,
837 const char *label, uint64_t u_lower,
838 uint64_t u_upper)
839 {
840 struct ctf_field_class_enum_mapping *mapping = NULL;
841 struct ctf_range range = {
842 .lower =
843 {
844 .u = u_lower,
845 },
846 .upper =
847 {
848 .u = u_upper,
849 },
850 };
851 uint64_t i;
852
853 BT_ASSERT(fc);
854 BT_ASSERT(label);
855
856 for (i = 0; i < fc->mappings->len; i++) {
857 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, i);
858
859 if (strcmp(mapping->label->str, label) == 0) {
860 break;
861 }
862 }
863
864 if (i == fc->mappings->len) {
865 mapping = NULL;
866 }
867
868 if (!mapping) {
869 g_array_set_size(fc->mappings, fc->mappings->len + 1);
870 mapping = ctf_field_class_enum_borrow_mapping_by_index(fc, fc->mappings->len - 1);
871 _ctf_field_class_enum_mapping_init(mapping);
872 g_string_assign(mapping->label, label);
873 }
874
875 g_array_append_val(mapping->ranges, range);
876 }
877
878 static inline struct ctf_named_field_class *
879 ctf_field_class_struct_borrow_member_by_index(struct ctf_field_class_struct *fc, uint64_t index)
880 {
881 BT_ASSERT_DBG(fc);
882 BT_ASSERT_DBG(index < fc->members->len);
883 return &bt_g_array_index(fc->members, struct ctf_named_field_class, index);
884 }
885
886 static inline struct ctf_named_field_class *
887 ctf_field_class_struct_borrow_member_by_name(struct ctf_field_class_struct *fc, const char *name)
888 {
889 uint64_t i;
890 struct ctf_named_field_class *ret_named_fc = NULL;
891
892 BT_ASSERT_DBG(fc);
893 BT_ASSERT_DBG(name);
894
895 for (i = 0; i < fc->members->len; i++) {
896 struct ctf_named_field_class *named_fc =
897 ctf_field_class_struct_borrow_member_by_index(fc, i);
898
899 if (strcmp(name, named_fc->name->str) == 0) {
900 ret_named_fc = named_fc;
901 goto end;
902 }
903 }
904
905 end:
906 return ret_named_fc;
907 }
908
909 static inline struct ctf_field_class *
910 ctf_field_class_struct_borrow_member_field_class_by_name(struct ctf_field_class_struct *struct_fc,
911 const char *name)
912 {
913 struct ctf_named_field_class *named_fc = NULL;
914 struct ctf_field_class *fc = NULL;
915
916 if (!struct_fc) {
917 goto end;
918 }
919
920 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
921 if (!named_fc) {
922 goto end;
923 }
924
925 fc = named_fc->fc;
926
927 end:
928 return fc;
929 }
930
931 static inline struct ctf_field_class_int *
932 ctf_field_class_struct_borrow_member_int_field_class_by_name(
933 struct ctf_field_class_struct *struct_fc, const char *name)
934 {
935 ctf_field_class *member_fc =
936 ctf_field_class_struct_borrow_member_field_class_by_name(struct_fc, name);
937
938 if (!member_fc) {
939 return nullptr;
940 }
941
942 if (member_fc->type != CTF_FIELD_CLASS_TYPE_INT &&
943 member_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
944 return nullptr;
945 }
946
947 return ctf_field_class_as_int(member_fc);
948 }
949
950 static inline void _ctf_named_field_class_unescape_orig_name(struct ctf_named_field_class *named_fc)
951 {
952 const char *name = named_fc->orig_name->str;
953
954 if (name[0] == '_') {
955 name++;
956 }
957
958 g_string_assign(named_fc->name, name);
959 }
960
961 static inline void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
962 const char *orig_name,
963 struct ctf_field_class *member_fc)
964 {
965 struct ctf_named_field_class *named_fc;
966
967 BT_ASSERT(fc);
968 BT_ASSERT(orig_name);
969 g_array_set_size(fc->members, fc->members->len + 1);
970
971 named_fc = &bt_g_array_index(fc->members, struct ctf_named_field_class, fc->members->len - 1);
972 _ctf_named_field_class_init(named_fc);
973 g_string_assign(named_fc->orig_name, orig_name);
974 _ctf_named_field_class_unescape_orig_name(named_fc);
975 named_fc->fc = member_fc;
976
977 if (member_fc->alignment > fc->base.alignment) {
978 fc->base.alignment = member_fc->alignment;
979 }
980 }
981
982 static inline struct ctf_named_field_class *
983 ctf_field_class_variant_borrow_option_by_index(struct ctf_field_class_variant *fc, uint64_t index)
984 {
985 BT_ASSERT_DBG(fc);
986 BT_ASSERT_DBG(index < fc->options->len);
987 return &bt_g_array_index(fc->options, struct ctf_named_field_class, index);
988 }
989
990 static inline struct ctf_named_field_class *
991 ctf_field_class_variant_borrow_option_by_name(struct ctf_field_class_variant *fc, const char *name)
992 {
993 uint64_t i;
994 struct ctf_named_field_class *ret_named_fc = NULL;
995
996 BT_ASSERT_DBG(fc);
997 BT_ASSERT_DBG(name);
998
999 for (i = 0; i < fc->options->len; i++) {
1000 struct ctf_named_field_class *named_fc =
1001 ctf_field_class_variant_borrow_option_by_index(fc, i);
1002
1003 if (strcmp(name, named_fc->name->str) == 0) {
1004 ret_named_fc = named_fc;
1005 goto end;
1006 }
1007 }
1008
1009 end:
1010 return ret_named_fc;
1011 }
1012
1013 static inline struct ctf_field_class_variant_range *
1014 ctf_field_class_variant_borrow_range_by_index(struct ctf_field_class_variant *fc, uint64_t index)
1015 {
1016 BT_ASSERT_DBG(fc);
1017 BT_ASSERT_DBG(index < fc->ranges->len);
1018 return &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, index);
1019 }
1020
1021 static inline void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
1022 const char *orig_name,
1023 struct ctf_field_class *option_fc)
1024 {
1025 struct ctf_named_field_class *named_fc;
1026
1027 BT_ASSERT(fc);
1028 BT_ASSERT(orig_name);
1029 g_array_set_size(fc->options, fc->options->len + 1);
1030
1031 named_fc = &bt_g_array_index(fc->options, struct ctf_named_field_class, fc->options->len - 1);
1032 _ctf_named_field_class_init(named_fc);
1033 g_string_assign(named_fc->orig_name, orig_name);
1034 _ctf_named_field_class_unescape_orig_name(named_fc);
1035 named_fc->fc = option_fc;
1036 }
1037
1038 static inline void ctf_field_class_variant_set_tag_field_class(struct ctf_field_class_variant *fc,
1039 struct ctf_field_class_enum *tag_fc)
1040 {
1041 uint64_t option_i;
1042
1043 BT_ASSERT(fc);
1044 BT_ASSERT(tag_fc);
1045 fc->tag_fc = tag_fc;
1046
1047 for (option_i = 0; option_i < fc->options->len; option_i++) {
1048 uint64_t range_i;
1049 struct ctf_named_field_class *named_fc =
1050 ctf_field_class_variant_borrow_option_by_index(fc, option_i);
1051 struct ctf_field_class_enum_mapping *mapping;
1052
1053 mapping = ctf_field_class_enum_borrow_mapping_by_label(tag_fc, named_fc->orig_name->str);
1054 if (!mapping) {
1055 continue;
1056 }
1057
1058 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1059 struct ctf_range *range =
1060 ctf_field_class_enum_mapping_borrow_range_by_index(mapping, range_i);
1061 struct ctf_field_class_variant_range var_range;
1062
1063 var_range.range = *range;
1064 var_range.option_index = option_i;
1065 g_array_append_val(fc->ranges, var_range);
1066 }
1067 }
1068 }
1069
1070 static inline struct ctf_field_class *
1071 ctf_field_class_compound_borrow_field_class_by_index(struct ctf_field_class *comp_fc,
1072 uint64_t index)
1073 {
1074 struct ctf_field_class *fc = NULL;
1075
1076 switch (comp_fc->type) {
1077 case CTF_FIELD_CLASS_TYPE_STRUCT:
1078 {
1079 struct ctf_named_field_class *named_fc = ctf_field_class_struct_borrow_member_by_index(
1080 (struct ctf_field_class_struct *) comp_fc, index);
1081
1082 BT_ASSERT_DBG(named_fc);
1083 fc = named_fc->fc;
1084 break;
1085 }
1086 case CTF_FIELD_CLASS_TYPE_VARIANT:
1087 {
1088 struct ctf_named_field_class *named_fc = ctf_field_class_variant_borrow_option_by_index(
1089 (struct ctf_field_class_variant *) comp_fc, index);
1090
1091 BT_ASSERT_DBG(named_fc);
1092 fc = named_fc->fc;
1093 break;
1094 }
1095 case CTF_FIELD_CLASS_TYPE_ARRAY:
1096 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1097 {
1098 struct ctf_field_class_array_base *array_fc = (struct ctf_field_class_array_base *) comp_fc;
1099
1100 fc = array_fc->elem_fc;
1101 break;
1102 }
1103 default:
1104 break;
1105 }
1106
1107 return fc;
1108 }
1109
1110 static inline uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
1111 {
1112 uint64_t field_count;
1113
1114 switch (fc->type) {
1115 case CTF_FIELD_CLASS_TYPE_STRUCT:
1116 {
1117 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1118
1119 field_count = struct_fc->members->len;
1120 break;
1121 }
1122 case CTF_FIELD_CLASS_TYPE_VARIANT:
1123 {
1124 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1125
1126 field_count = var_fc->options->len;
1127 break;
1128 }
1129 case CTF_FIELD_CLASS_TYPE_ARRAY:
1130 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1131 /*
1132 * Array and sequence types always contain a single
1133 * member (the element type).
1134 */
1135 field_count = 1;
1136 break;
1137 default:
1138 bt_common_abort();
1139 }
1140
1141 return field_count;
1142 }
1143
1144 static inline int64_t
1145 ctf_field_class_compound_get_field_class_index_from_orig_name(struct ctf_field_class *fc,
1146 const char *orig_name)
1147 {
1148 int64_t ret_index = -1;
1149 uint64_t i;
1150
1151 switch (fc->type) {
1152 case CTF_FIELD_CLASS_TYPE_STRUCT:
1153 {
1154 struct ctf_field_class_struct *struct_fc = (struct ctf_field_class_struct *) fc;
1155
1156 for (i = 0; i < struct_fc->members->len; i++) {
1157 struct ctf_named_field_class *named_fc =
1158 ctf_field_class_struct_borrow_member_by_index(struct_fc, i);
1159
1160 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1161 ret_index = (int64_t) i;
1162 goto end;
1163 }
1164 }
1165
1166 break;
1167 }
1168 case CTF_FIELD_CLASS_TYPE_VARIANT:
1169 {
1170 struct ctf_field_class_variant *var_fc = (struct ctf_field_class_variant *) fc;
1171
1172 for (i = 0; i < var_fc->options->len; i++) {
1173 struct ctf_named_field_class *named_fc =
1174 ctf_field_class_variant_borrow_option_by_index(var_fc, i);
1175
1176 if (strcmp(orig_name, named_fc->orig_name->str) == 0) {
1177 ret_index = (int64_t) i;
1178 goto end;
1179 }
1180 }
1181
1182 break;
1183 }
1184 default:
1185 break;
1186 }
1187
1188 end:
1189 return ret_index;
1190 }
1191
1192 static inline void ctf_field_path_append_index(struct ctf_field_path *fp, int64_t index)
1193 {
1194 BT_ASSERT(fp);
1195 g_array_append_val(fp->path, index);
1196 }
1197
1198 static inline int64_t ctf_field_path_borrow_index_by_index(const ctf_field_path *fp, uint64_t index)
1199 {
1200 BT_ASSERT_DBG(fp);
1201 BT_ASSERT_DBG(index < fp->path->len);
1202 return bt_g_array_index(fp->path, int64_t, index);
1203 }
1204
1205 static inline void ctf_field_path_clear(struct ctf_field_path *fp)
1206 {
1207 BT_ASSERT(fp);
1208 g_array_set_size(fp->path, 0);
1209 }
1210
1211 inline std::string format_as(const ctf_field_path& path)
1212 {
1213 std::string str = fmt::format("[{}", path.root);
1214
1215 for (guint i = 0; i < path.path->len; i++) {
1216 str += fmt::format(", {}", ctf_field_path_borrow_index_by_index(&path, i));
1217 }
1218
1219 str += ']';
1220 return str;
1221 }
1222
1223 static inline struct ctf_field_class *
1224 ctf_field_path_borrow_field_class(struct ctf_field_path *field_path, struct ctf_trace_class *tc,
1225 struct ctf_stream_class *sc, struct ctf_event_class *ec)
1226 {
1227 uint64_t i;
1228 struct ctf_field_class *fc;
1229
1230 switch (field_path->root) {
1231 case CTF_SCOPE_PACKET_HEADER:
1232 fc = tc->packet_header_fc;
1233 break;
1234 case CTF_SCOPE_PACKET_CONTEXT:
1235 fc = sc->packet_context_fc;
1236 break;
1237 case CTF_SCOPE_EVENT_HEADER:
1238 fc = sc->event_header_fc;
1239 break;
1240 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
1241 fc = sc->event_common_context_fc;
1242 break;
1243 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
1244 fc = ec->spec_context_fc;
1245 break;
1246 case CTF_SCOPE_EVENT_PAYLOAD:
1247 fc = ec->payload_fc;
1248 break;
1249 default:
1250 bt_common_abort();
1251 }
1252
1253 BT_ASSERT_DBG(fc);
1254
1255 for (i = 0; i < field_path->path->len; i++) {
1256 int64_t child_index = ctf_field_path_borrow_index_by_index(field_path, i);
1257 struct ctf_field_class *child_fc =
1258 ctf_field_class_compound_borrow_field_class_by_index(fc, child_index);
1259 BT_ASSERT_DBG(child_fc);
1260 fc = child_fc;
1261 }
1262
1263 BT_ASSERT_DBG(fc);
1264 return fc;
1265 }
1266
1267 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
1268
1269 static inline void ctf_field_class_bit_array_copy_content(struct ctf_field_class_bit_array *dst_fc,
1270 struct ctf_field_class_bit_array *src_fc)
1271 {
1272 BT_ASSERT(dst_fc);
1273 BT_ASSERT(src_fc);
1274 dst_fc->byte_order = src_fc->byte_order;
1275 dst_fc->size = src_fc->size;
1276 }
1277
1278 static inline void ctf_field_class_int_copy_content(struct ctf_field_class_int *dst_fc,
1279 struct ctf_field_class_int *src_fc)
1280 {
1281 ctf_field_class_bit_array_copy_content(&dst_fc->base, &src_fc->base);
1282 dst_fc->meaning = src_fc->meaning;
1283 dst_fc->is_signed = src_fc->is_signed;
1284 dst_fc->disp_base = src_fc->disp_base;
1285 dst_fc->encoding = src_fc->encoding;
1286 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
1287 dst_fc->storing_index = src_fc->storing_index;
1288 }
1289
1290 static inline struct ctf_field_class_int *_ctf_field_class_int_copy(struct ctf_field_class_int *fc)
1291 {
1292 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
1293
1294 BT_ASSERT(copy_fc);
1295 ctf_field_class_int_copy_content(copy_fc, fc);
1296 return copy_fc;
1297 }
1298
1299 static inline struct ctf_field_class_enum *
1300 _ctf_field_class_enum_copy(struct ctf_field_class_enum *fc)
1301 {
1302 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
1303 uint64_t i;
1304
1305 BT_ASSERT(copy_fc);
1306 ctf_field_class_int_copy_content(&copy_fc->base, &fc->base);
1307
1308 for (i = 0; i < fc->mappings->len; i++) {
1309 uint64_t range_i;
1310
1311 struct ctf_field_class_enum_mapping *mapping =
1312 &bt_g_array_index(fc->mappings, struct ctf_field_class_enum_mapping, i);
1313
1314 for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
1315 struct ctf_range *range = &bt_g_array_index(mapping->ranges, struct ctf_range, range_i);
1316
1317 ctf_field_class_enum_map_range(copy_fc, mapping->label->str, range->lower.u,
1318 range->upper.u);
1319 }
1320 }
1321
1322 return copy_fc;
1323 }
1324
1325 static inline struct ctf_field_class_float *
1326 _ctf_field_class_float_copy(struct ctf_field_class_float *fc)
1327 {
1328 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
1329
1330 BT_ASSERT(copy_fc);
1331 ctf_field_class_bit_array_copy_content(&copy_fc->base, &fc->base);
1332 return copy_fc;
1333 }
1334
1335 static inline struct ctf_field_class_string *
1336 _ctf_field_class_string_copy(struct ctf_field_class_string *)
1337 {
1338 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
1339
1340 BT_ASSERT(copy_fc);
1341 return copy_fc;
1342 }
1343
1344 static inline struct ctf_field_class_struct *
1345 _ctf_field_class_struct_copy(struct ctf_field_class_struct *fc)
1346 {
1347 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
1348 uint64_t i;
1349
1350 BT_ASSERT(copy_fc);
1351
1352 for (i = 0; i < fc->members->len; i++) {
1353 struct ctf_named_field_class *named_fc =
1354 &bt_g_array_index(fc->members, struct ctf_named_field_class, i);
1355
1356 ctf_field_class_struct_append_member(copy_fc, named_fc->name->str,
1357 ctf_field_class_copy(named_fc->fc));
1358 }
1359
1360 return copy_fc;
1361 }
1362
1363 static inline void ctf_field_path_copy_content(struct ctf_field_path *dst_fp,
1364 struct ctf_field_path *src_fp)
1365 {
1366 uint64_t i;
1367
1368 BT_ASSERT(dst_fp);
1369 BT_ASSERT(src_fp);
1370 dst_fp->root = src_fp->root;
1371 ctf_field_path_clear(dst_fp);
1372
1373 for (i = 0; i < src_fp->path->len; i++) {
1374 int64_t index = ctf_field_path_borrow_index_by_index(src_fp, i);
1375
1376 ctf_field_path_append_index(dst_fp, index);
1377 }
1378 }
1379
1380 static inline struct ctf_field_class_variant *
1381 _ctf_field_class_variant_copy(struct ctf_field_class_variant *fc)
1382 {
1383 struct ctf_field_class_variant *copy_fc = ctf_field_class_variant_create();
1384 uint64_t i;
1385
1386 BT_ASSERT(copy_fc);
1387
1388 for (i = 0; i < fc->options->len; i++) {
1389 struct ctf_named_field_class *named_fc =
1390 &bt_g_array_index(fc->options, struct ctf_named_field_class, i);
1391
1392 ctf_field_class_variant_append_option(copy_fc, named_fc->name->str,
1393 ctf_field_class_copy(named_fc->fc));
1394 }
1395
1396 for (i = 0; i < fc->ranges->len; i++) {
1397 struct ctf_field_class_variant_range *range =
1398 &bt_g_array_index(fc->ranges, struct ctf_field_class_variant_range, i);
1399
1400 g_array_append_val(copy_fc->ranges, *range);
1401 }
1402
1403 ctf_field_path_copy_content(&copy_fc->tag_path, &fc->tag_path);
1404 g_string_assign(copy_fc->tag_ref, fc->tag_ref->str);
1405 copy_fc->stored_tag_index = fc->stored_tag_index;
1406 return copy_fc;
1407 }
1408
1409 static inline void
1410 ctf_field_class_array_base_copy_content(struct ctf_field_class_array_base *dst_fc,
1411 struct ctf_field_class_array_base *src_fc)
1412 {
1413 BT_ASSERT(dst_fc);
1414 BT_ASSERT(src_fc);
1415 dst_fc->elem_fc = ctf_field_class_copy(src_fc->elem_fc);
1416 dst_fc->is_text = src_fc->is_text;
1417 }
1418
1419 static inline struct ctf_field_class_array *
1420 _ctf_field_class_array_copy(struct ctf_field_class_array *fc)
1421 {
1422 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
1423
1424 BT_ASSERT(copy_fc);
1425 ctf_field_class_array_base_copy_content(&copy_fc->base, &fc->base);
1426 copy_fc->length = fc->length;
1427 return copy_fc;
1428 }
1429
1430 static inline struct ctf_field_class_sequence *
1431 _ctf_field_class_sequence_copy(struct ctf_field_class_sequence *fc)
1432 {
1433 struct ctf_field_class_sequence *copy_fc = ctf_field_class_sequence_create();
1434
1435 BT_ASSERT(copy_fc);
1436 ctf_field_class_array_base_copy_content(&copy_fc->base, &fc->base);
1437 ctf_field_path_copy_content(&copy_fc->length_path, &fc->length_path);
1438 g_string_assign(copy_fc->length_ref, fc->length_ref->str);
1439 copy_fc->stored_length_index = fc->stored_length_index;
1440 return copy_fc;
1441 }
1442
1443 static inline struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
1444 {
1445 struct ctf_field_class *copy_fc = NULL;
1446
1447 if (!fc) {
1448 goto end;
1449 }
1450
1451 /*
1452 * Translation should not have happened yet.
1453 */
1454 BT_ASSERT(!fc->ir_fc);
1455
1456 switch (fc->type) {
1457 case CTF_FIELD_CLASS_TYPE_INT:
1458 copy_fc = &_ctf_field_class_int_copy(ctf_field_class_as_int(fc))->base.base;
1459 break;
1460 case CTF_FIELD_CLASS_TYPE_ENUM:
1461 copy_fc = &_ctf_field_class_enum_copy(ctf_field_class_as_enum(fc))->base.base.base;
1462 break;
1463 case CTF_FIELD_CLASS_TYPE_FLOAT:
1464 copy_fc = &_ctf_field_class_float_copy(ctf_field_class_as_float(fc))->base.base;
1465 break;
1466 case CTF_FIELD_CLASS_TYPE_STRING:
1467 copy_fc = &_ctf_field_class_string_copy(ctf_field_class_as_string(fc))->base;
1468 break;
1469 case CTF_FIELD_CLASS_TYPE_STRUCT:
1470 copy_fc = &_ctf_field_class_struct_copy(ctf_field_class_as_struct(fc))->base;
1471 break;
1472 case CTF_FIELD_CLASS_TYPE_ARRAY:
1473 copy_fc = &_ctf_field_class_array_copy(ctf_field_class_as_array(fc))->base.base;
1474 break;
1475 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1476 copy_fc = &_ctf_field_class_sequence_copy(ctf_field_class_as_sequence(fc))->base.base;
1477 break;
1478 case CTF_FIELD_CLASS_TYPE_VARIANT:
1479 copy_fc = &_ctf_field_class_variant_copy(ctf_field_class_as_variant(fc))->base;
1480 break;
1481 default:
1482 bt_common_abort();
1483 }
1484
1485 copy_fc->type = fc->type;
1486 copy_fc->alignment = fc->alignment;
1487 copy_fc->in_ir = fc->in_ir;
1488
1489 end:
1490 return copy_fc;
1491 }
1492
1493 static inline struct ctf_event_class *ctf_event_class_create(void)
1494 {
1495 struct ctf_event_class *ec = g_new0(struct ctf_event_class, 1);
1496
1497 BT_ASSERT(ec);
1498 ec->name = g_string_new(NULL);
1499 BT_ASSERT(ec->name);
1500 ec->emf_uri = g_string_new(NULL);
1501 BT_ASSERT(ec->emf_uri);
1502 ec->is_log_level_set = false;
1503 return ec;
1504 }
1505
1506 static inline void ctf_event_class_set_log_level(struct ctf_event_class *ec,
1507 enum bt_event_class_log_level log_level)
1508 {
1509 BT_ASSERT(ec);
1510 ec->log_level = log_level;
1511 ec->is_log_level_set = true;
1512 }
1513
1514 static inline void ctf_event_class_destroy(struct ctf_event_class *ec)
1515 {
1516 if (!ec) {
1517 return;
1518 }
1519
1520 if (ec->name) {
1521 g_string_free(ec->name, TRUE);
1522 }
1523
1524 if (ec->emf_uri) {
1525 g_string_free(ec->emf_uri, TRUE);
1526 }
1527
1528 ctf_field_class_destroy(ec->spec_context_fc);
1529 ctf_field_class_destroy(ec->payload_fc);
1530 g_free(ec);
1531 }
1532
1533 static inline struct ctf_stream_class *ctf_stream_class_create(void)
1534 {
1535 struct ctf_stream_class *sc = g_new0(struct ctf_stream_class, 1);
1536
1537 BT_ASSERT(sc);
1538 sc->event_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_event_class_destroy);
1539 BT_ASSERT(sc->event_classes);
1540 sc->event_classes_by_id = g_hash_table_new(g_direct_hash, g_direct_equal);
1541 BT_ASSERT(sc->event_classes_by_id);
1542 return sc;
1543 }
1544
1545 static inline void ctf_stream_class_destroy(struct ctf_stream_class *sc)
1546 {
1547 if (!sc) {
1548 return;
1549 }
1550
1551 if (sc->event_classes) {
1552 g_ptr_array_free(sc->event_classes, TRUE);
1553 }
1554
1555 if (sc->event_classes_by_id) {
1556 g_hash_table_destroy(sc->event_classes_by_id);
1557 }
1558
1559 ctf_field_class_destroy(sc->packet_context_fc);
1560 ctf_field_class_destroy(sc->event_header_fc);
1561 ctf_field_class_destroy(sc->event_common_context_fc);
1562 g_free(sc);
1563 }
1564
1565 static inline void ctf_stream_class_append_event_class(struct ctf_stream_class *sc,
1566 struct ctf_event_class *ec)
1567 {
1568 g_ptr_array_add(sc->event_classes, ec);
1569 g_hash_table_insert(sc->event_classes_by_id, GUINT_TO_POINTER((guint) ec->id), ec);
1570 }
1571
1572 static inline struct ctf_event_class *
1573 ctf_stream_class_borrow_event_class_by_id(struct ctf_stream_class *sc, uint64_t type)
1574 {
1575 BT_ASSERT_DBG(sc);
1576 return (struct ctf_event_class *) g_hash_table_lookup(sc->event_classes_by_id,
1577 GUINT_TO_POINTER((guint) type));
1578 }
1579
1580 static inline void _ctf_trace_class_env_entry_init(struct ctf_trace_class_env_entry *entry)
1581 {
1582 BT_ASSERT(entry);
1583 entry->name = g_string_new(NULL);
1584 BT_ASSERT(entry->name);
1585 entry->value.str = g_string_new(NULL);
1586 BT_ASSERT(entry->value.str);
1587 }
1588
1589 static inline void _ctf_trace_class_env_entry_fini(struct ctf_trace_class_env_entry *entry)
1590 {
1591 BT_ASSERT(entry);
1592
1593 if (entry->name) {
1594 g_string_free(entry->name, TRUE);
1595 }
1596
1597 if (entry->value.str) {
1598 g_string_free(entry->value.str, TRUE);
1599 }
1600 }
1601
1602 static inline struct ctf_clock_class *ctf_clock_class_create(void)
1603 {
1604 struct ctf_clock_class *cc = g_new0(struct ctf_clock_class, 1);
1605
1606 BT_ASSERT(cc);
1607 cc->name = g_string_new(NULL);
1608 BT_ASSERT(cc->name);
1609 cc->description = g_string_new(NULL);
1610 BT_ASSERT(cc->description);
1611 return cc;
1612 }
1613
1614 static inline void ctf_clock_class_destroy(struct ctf_clock_class *cc)
1615 {
1616 if (!cc) {
1617 return;
1618 }
1619
1620 if (cc->name) {
1621 g_string_free(cc->name, TRUE);
1622 }
1623
1624 if (cc->description) {
1625 g_string_free(cc->description, TRUE);
1626 }
1627
1628 bt_clock_class_put_ref(cc->ir_cc);
1629 g_free(cc);
1630 }
1631
1632 static inline struct ctf_trace_class *ctf_trace_class_create(void)
1633 {
1634 struct ctf_trace_class *tc = g_new0(struct ctf_trace_class, 1);
1635
1636 BT_ASSERT(tc);
1637 tc->default_byte_order = CTF_BYTE_ORDER_UNKNOWN;
1638 tc->clock_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_clock_class_destroy);
1639 BT_ASSERT(tc->clock_classes);
1640 tc->stream_classes = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_stream_class_destroy);
1641 BT_ASSERT(tc->stream_classes);
1642 tc->env_entries = g_array_new(FALSE, TRUE, sizeof(struct ctf_trace_class_env_entry));
1643 return tc;
1644 }
1645
1646 static inline void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1647 {
1648 if (!tc) {
1649 return;
1650 }
1651
1652 ctf_field_class_destroy(tc->packet_header_fc);
1653
1654 if (tc->clock_classes) {
1655 g_ptr_array_free(tc->clock_classes, TRUE);
1656 }
1657
1658 if (tc->stream_classes) {
1659 g_ptr_array_free(tc->stream_classes, TRUE);
1660 }
1661
1662 if (tc->env_entries) {
1663 uint64_t i;
1664
1665 for (i = 0; i < tc->env_entries->len; i++) {
1666 struct ctf_trace_class_env_entry *entry =
1667 &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, i);
1668
1669 _ctf_trace_class_env_entry_fini(entry);
1670 }
1671
1672 g_array_free(tc->env_entries, TRUE);
1673 }
1674
1675 g_free(tc);
1676 }
1677
1678 static inline void ctf_trace_class_append_env_entry(struct ctf_trace_class *tc, const char *name,
1679 enum ctf_trace_class_env_entry_type type,
1680 const char *str_value, int64_t i_value)
1681 {
1682 struct ctf_trace_class_env_entry *entry;
1683
1684 BT_ASSERT(tc);
1685 BT_ASSERT(name);
1686 g_array_set_size(tc->env_entries, tc->env_entries->len + 1);
1687
1688 entry = &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry,
1689 tc->env_entries->len - 1);
1690 entry->type = type;
1691 _ctf_trace_class_env_entry_init(entry);
1692 g_string_assign(entry->name, name);
1693
1694 if (str_value) {
1695 g_string_assign(entry->value.str, str_value);
1696 }
1697
1698 entry->value.i = i_value;
1699 }
1700
1701 static inline struct ctf_stream_class *
1702 ctf_trace_class_borrow_stream_class_by_id(struct ctf_trace_class *tc, uint64_t id)
1703 {
1704 uint64_t i;
1705 struct ctf_stream_class *ret_sc = NULL;
1706
1707 BT_ASSERT_DBG(tc);
1708
1709 for (i = 0; i < tc->stream_classes->len; i++) {
1710 struct ctf_stream_class *sc = (struct ctf_stream_class *) tc->stream_classes->pdata[i];
1711
1712 if (sc->id == id) {
1713 ret_sc = sc;
1714 goto end;
1715 }
1716 }
1717
1718 end:
1719 return ret_sc;
1720 }
1721
1722 static inline struct ctf_clock_class *
1723 ctf_trace_class_borrow_clock_class_by_name(struct ctf_trace_class *tc, const char *name)
1724 {
1725 uint64_t i;
1726 struct ctf_clock_class *ret_cc = NULL;
1727
1728 BT_ASSERT_DBG(tc);
1729 BT_ASSERT_DBG(name);
1730
1731 for (i = 0; i < tc->clock_classes->len; i++) {
1732 struct ctf_clock_class *cc = (struct ctf_clock_class *) tc->clock_classes->pdata[i];
1733
1734 BT_ASSERT_DBG(cc->name);
1735 if (strcmp(cc->name->str, name) == 0) {
1736 ret_cc = cc;
1737 goto end;
1738 }
1739 }
1740
1741 end:
1742 return ret_cc;
1743 }
1744
1745 static inline struct ctf_trace_class_env_entry *
1746 ctf_trace_class_borrow_env_entry_by_index(struct ctf_trace_class *tc, uint64_t index)
1747 {
1748 BT_ASSERT_DBG(tc);
1749 BT_ASSERT_DBG(index < tc->env_entries->len);
1750 return &bt_g_array_index(tc->env_entries, struct ctf_trace_class_env_entry, index);
1751 }
1752
1753 static inline struct ctf_trace_class_env_entry *
1754 ctf_trace_class_borrow_env_entry_by_name(struct ctf_trace_class *tc, const char *name)
1755 {
1756 struct ctf_trace_class_env_entry *ret_entry = NULL;
1757 uint64_t i;
1758
1759 BT_ASSERT_DBG(tc);
1760 BT_ASSERT_DBG(name);
1761
1762 for (i = 0; i < tc->env_entries->len; i++) {
1763 struct ctf_trace_class_env_entry *env_entry =
1764 ctf_trace_class_borrow_env_entry_by_index(tc, i);
1765
1766 if (strcmp(env_entry->name->str, name) == 0) {
1767 ret_entry = env_entry;
1768 goto end;
1769 }
1770 }
1771
1772 end:
1773 return ret_entry;
1774 }
1775
1776 #endif /* _CTF_META_H */
This page took 0.066645 seconds and 4 git commands to generate.