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