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