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