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