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