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