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