tap-driver.sh: flush stdout after each test result
[babeltrace.git] / plugins / ctf / common / metadata / ctf-meta.h
CommitLineData
44c440bc
PP
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
3fadfbc0
MJ
18#include <babeltrace2/babeltrace.h>
19#include <babeltrace2/common-internal.h>
20#include <babeltrace2/assert-internal.h>
44c440bc
PP
21#include <glib.h>
22#include <stdint.h>
23#include <string.h>
24
864cad70
PP
25enum 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,
44c440bc
PP
34};
35
5cd6d0e5
PP
36enum 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,
44c440bc
PP
49};
50
51enum ctf_byte_order {
52 CTF_BYTE_ORDER_DEFAULT,
53 CTF_BYTE_ORDER_LITTLE,
54 CTF_BYTE_ORDER_BIG,
55};
56
57enum ctf_encoding {
58 CTF_ENCODING_NONE,
59 CTF_ENCODING_UTF8,
60};
61
83ebb7f1
PP
62enum 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
0f2d58c9
PP
71struct 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
5cd6d0e5 86struct ctf_field_class {
864cad70 87 enum ctf_field_class_type type;
44c440bc
PP
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. */
b19ff26f 93 bt_field_class *ir_fc;
44c440bc
PP
94};
95
5cd6d0e5
PP
96struct ctf_field_class_bit_array {
97 struct ctf_field_class base;
44c440bc
PP
98 enum ctf_byte_order byte_order;
99 unsigned int size;
100};
101
5cd6d0e5
PP
102struct ctf_field_class_int {
103 struct ctf_field_class_bit_array base;
104 enum ctf_field_class_meaning meaning;
44c440bc 105 bool is_signed;
4cdfc5e8 106 bt_field_class_integer_preferred_display_base disp_base;
44c440bc
PP
107 enum ctf_encoding encoding;
108 int64_t storing_index;
109
0f2d58c9
PP
110 /* Weak */
111 struct ctf_clock_class *mapped_clock_class;
44c440bc
PP
112};
113
114struct 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
5cd6d0e5 126struct ctf_field_class_enum_mapping {
44c440bc
PP
127 GString *label;
128 struct ctf_range range;
129};
130
5cd6d0e5
PP
131struct ctf_field_class_enum {
132 struct ctf_field_class_int base;
44c440bc 133
5cd6d0e5 134 /* Array of `struct ctf_field_class_enum_mapping` */
44c440bc
PP
135 GArray *mappings;
136};
137
5cd6d0e5
PP
138struct ctf_field_class_float {
139 struct ctf_field_class_bit_array base;
44c440bc
PP
140};
141
5cd6d0e5
PP
142struct ctf_field_class_string {
143 struct ctf_field_class base;
44c440bc
PP
144 enum ctf_encoding encoding;
145};
146
5cd6d0e5 147struct ctf_named_field_class {
44c440bc
PP
148 GString *name;
149
150 /* Owned by this */
5cd6d0e5 151 struct ctf_field_class *fc;
44c440bc
PP
152};
153
5cd6d0e5
PP
154struct ctf_field_class_struct {
155 struct ctf_field_class base;
44c440bc 156
5cd6d0e5 157 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
158 GArray *members;
159};
160
161struct ctf_field_path {
83ebb7f1 162 enum ctf_scope root;
44c440bc
PP
163
164 /* Array of `int64_t` */
165 GArray *path;
166};
167
5cd6d0e5 168struct ctf_field_class_variant_range {
44c440bc
PP
169 struct ctf_range range;
170 uint64_t option_index;
171};
172
5cd6d0e5
PP
173struct ctf_field_class_variant {
174 struct ctf_field_class base;
44c440bc
PP
175 GString *tag_ref;
176 struct ctf_field_path tag_path;
177 uint64_t stored_tag_index;
178
5cd6d0e5 179 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
180 GArray *options;
181
5cd6d0e5 182 /* Array of `struct ctf_field_class_variant_range` */
44c440bc
PP
183 GArray *ranges;
184
185 /* Weak */
5cd6d0e5 186 struct ctf_field_class_enum *tag_fc;
44c440bc
PP
187};
188
5cd6d0e5
PP
189struct ctf_field_class_array_base {
190 struct ctf_field_class base;
191 struct ctf_field_class *elem_fc;
44c440bc
PP
192 bool is_text;
193};
194
5cd6d0e5
PP
195struct ctf_field_class_array {
196 struct ctf_field_class_array_base base;
197 enum ctf_field_class_meaning meaning;
44c440bc
PP
198 uint64_t length;
199};
200
5cd6d0e5
PP
201struct ctf_field_class_sequence {
202 struct ctf_field_class_array_base base;
44c440bc
PP
203 GString *length_ref;
204 struct ctf_field_path length_path;
205 uint64_t stored_length_index;
206
207 /* Weak */
5cd6d0e5 208 struct ctf_field_class_int *length_fc;
44c440bc
PP
209};
210
211struct ctf_event_class {
212 GString *name;
213 uint64_t id;
214 GString *emf_uri;
4cdfc5e8 215 bt_event_class_log_level log_level;
44c440bc
PP
216 bool is_translated;
217
218 /* Owned by this */
5cd6d0e5 219 struct ctf_field_class *spec_context_fc;
44c440bc
PP
220
221 /* Owned by this */
5cd6d0e5 222 struct ctf_field_class *payload_fc;
44c440bc
PP
223
224 /* Weak, set during translation */
b19ff26f 225 bt_event_class *ir_ec;
44c440bc
PP
226};
227
228struct ctf_stream_class {
229 uint64_t id;
230 bool is_translated;
afd45274
PP
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;
44c440bc
PP
237
238 /* Owned by this */
5cd6d0e5 239 struct ctf_field_class *packet_context_fc;
44c440bc
PP
240
241 /* Owned by this */
5cd6d0e5 242 struct ctf_field_class *event_header_fc;
44c440bc
PP
243
244 /* Owned by this */
5cd6d0e5 245 struct ctf_field_class *event_common_context_fc;
44c440bc
PP
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
0f2d58c9
PP
256 /* Weak */
257 struct ctf_clock_class *default_clock_class;
44c440bc
PP
258
259 /* Weak, set during translation */
b19ff26f 260 bt_stream_class *ir_sc;
44c440bc
PP
261};
262
263enum ctf_trace_class_env_entry_type {
264 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT,
265 CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR,
266};
267
268struct 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
278struct ctf_trace_class {
44c440bc
PP
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 */
5cd6d0e5 286 struct ctf_field_class *packet_header_fc;
44c440bc
PP
287
288 uint64_t stored_value_count;
289
0f2d58c9 290 /* Array of `struct ctf_clock_class *` (owned by this) */
44c440bc
PP
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 */
b19ff26f 302 bt_trace_class *ir_tc;
44c440bc
PP
303};
304
305static inline
5cd6d0e5 306void ctf_field_class_destroy(struct ctf_field_class *fc);
44c440bc
PP
307
308static inline
864cad70
PP
309void _ctf_field_class_init(struct ctf_field_class *fc,
310 enum ctf_field_class_type type, unsigned int alignment)
44c440bc 311{
5cd6d0e5 312 BT_ASSERT(fc);
864cad70 313 fc->type = type;
5cd6d0e5
PP
314 fc->alignment = alignment;
315 fc->in_ir = false;
44c440bc
PP
316}
317
318static inline
5cd6d0e5 319void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
864cad70 320 enum ctf_field_class_type type)
44c440bc 321{
864cad70 322 _ctf_field_class_init((void *) fc, type, 1);
44c440bc
PP
323}
324
325static inline
5cd6d0e5 326void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
864cad70 327 enum ctf_field_class_type type)
44c440bc 328{
864cad70 329 _ctf_field_class_bit_array_init((void *) fc, type);
5cd6d0e5
PP
330 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
331 fc->storing_index = -1;
44c440bc
PP
332}
333
334static inline
335void 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
342static inline
343void 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
352static inline
5cd6d0e5 353void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
44c440bc 354{
5cd6d0e5
PP
355 BT_ASSERT(named_fc);
356 named_fc->name = g_string_new(NULL);
357 BT_ASSERT(named_fc->name);
44c440bc
PP
358}
359
360static inline
5cd6d0e5 361void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
44c440bc 362{
5cd6d0e5 363 BT_ASSERT(named_fc);
44c440bc 364
5cd6d0e5
PP
365 if (named_fc->name) {
366 g_string_free(named_fc->name, TRUE);
44c440bc
PP
367 }
368
5cd6d0e5 369 ctf_field_class_destroy(named_fc->fc);
44c440bc
PP
370}
371
372static inline
5cd6d0e5
PP
373void _ctf_field_class_enum_mapping_init(
374 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
375{
376 BT_ASSERT(mapping);
377 mapping->label = g_string_new(NULL);
378 BT_ASSERT(mapping->label);
379}
380
381static inline
5cd6d0e5
PP
382void _ctf_field_class_enum_mapping_fini(
383 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
384{
385 BT_ASSERT(mapping);
386
387 if (mapping->label) {
388 g_string_free(mapping->label, TRUE);
389 }
390}
391
392static inline
5cd6d0e5 393struct ctf_field_class_int *ctf_field_class_int_create(void)
44c440bc 394{
5cd6d0e5 395 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
44c440bc 396
5cd6d0e5 397 BT_ASSERT(fc);
864cad70 398 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
5cd6d0e5 399 return fc;
44c440bc
PP
400}
401
402static inline
5cd6d0e5 403struct ctf_field_class_float *ctf_field_class_float_create(void)
44c440bc 404{
5cd6d0e5
PP
405 struct ctf_field_class_float *fc =
406 g_new0(struct ctf_field_class_float, 1);
44c440bc 407
5cd6d0e5 408 BT_ASSERT(fc);
864cad70 409 _ctf_field_class_bit_array_init((void *) fc, CTF_FIELD_CLASS_TYPE_FLOAT);
5cd6d0e5 410 return fc;
44c440bc
PP
411}
412
413static inline
5cd6d0e5 414struct ctf_field_class_string *ctf_field_class_string_create(void)
44c440bc 415{
5cd6d0e5
PP
416 struct ctf_field_class_string *fc =
417 g_new0(struct ctf_field_class_string, 1);
44c440bc 418
5cd6d0e5 419 BT_ASSERT(fc);
864cad70 420 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRING, 8);
5cd6d0e5 421 return fc;
44c440bc
PP
422}
423
424static inline
5cd6d0e5 425struct ctf_field_class_enum *ctf_field_class_enum_create(void)
44c440bc 426{
5cd6d0e5 427 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
44c440bc 428
5cd6d0e5 429 BT_ASSERT(fc);
864cad70 430 _ctf_field_class_int_init((void *) fc, CTF_FIELD_CLASS_TYPE_ENUM);
5cd6d0e5
PP
431 fc->mappings = g_array_new(FALSE, TRUE,
432 sizeof(struct ctf_field_class_enum_mapping));
433 BT_ASSERT(fc->mappings);
434 return fc;
44c440bc
PP
435}
436
437static inline
5cd6d0e5 438struct ctf_field_class_struct *ctf_field_class_struct_create(void)
44c440bc 439{
5cd6d0e5
PP
440 struct ctf_field_class_struct *fc =
441 g_new0(struct ctf_field_class_struct, 1);
44c440bc 442
5cd6d0e5 443 BT_ASSERT(fc);
864cad70 444 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
5cd6d0e5
PP
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;
44c440bc
PP
450}
451
452static inline
5cd6d0e5 453struct ctf_field_class_variant *ctf_field_class_variant_create(void)
44c440bc 454{
5cd6d0e5
PP
455 struct ctf_field_class_variant *fc =
456 g_new0(struct ctf_field_class_variant, 1);
44c440bc 457
5cd6d0e5 458 BT_ASSERT(fc);
864cad70 459 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
5cd6d0e5
PP
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;
44c440bc
PP
471}
472
473static inline
5cd6d0e5 474struct ctf_field_class_array *ctf_field_class_array_create(void)
44c440bc 475{
5cd6d0e5
PP
476 struct ctf_field_class_array *fc =
477 g_new0(struct ctf_field_class_array, 1);
44c440bc 478
5cd6d0e5 479 BT_ASSERT(fc);
864cad70 480 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
5cd6d0e5
PP
481 fc->base.base.is_compound = true;
482 return fc;
44c440bc
PP
483}
484
485static inline
5cd6d0e5 486struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
44c440bc 487{
5cd6d0e5
PP
488 struct ctf_field_class_sequence *fc =
489 g_new0(struct ctf_field_class_sequence, 1);
44c440bc 490
5cd6d0e5 491 BT_ASSERT(fc);
864cad70 492 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
5cd6d0e5
PP
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;
44c440bc
PP
498}
499
500static inline
5cd6d0e5 501void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
44c440bc 502{
5cd6d0e5 503 BT_ASSERT(fc);
5cd6d0e5 504 g_free(fc);
44c440bc
PP
505}
506
507static inline
5cd6d0e5 508void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
44c440bc 509{
5cd6d0e5 510 BT_ASSERT(fc);
44c440bc 511
5cd6d0e5 512 if (fc->mappings) {
44c440bc
PP
513 uint64_t i;
514
5cd6d0e5
PP
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);
44c440bc 519
5cd6d0e5 520 _ctf_field_class_enum_mapping_fini(mapping);
44c440bc
PP
521 }
522
5cd6d0e5 523 g_array_free(fc->mappings, TRUE);
44c440bc
PP
524 }
525
5cd6d0e5 526 g_free(fc);
44c440bc
PP
527}
528
529static inline
5cd6d0e5 530void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
44c440bc 531{
5cd6d0e5
PP
532 BT_ASSERT(fc);
533 g_free(fc);
44c440bc
PP
534}
535
536static inline
5cd6d0e5 537void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
44c440bc 538{
5cd6d0e5
PP
539 BT_ASSERT(fc);
540 g_free(fc);
44c440bc
PP
541}
542
543static inline
5cd6d0e5 544void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
44c440bc 545{
5cd6d0e5 546 BT_ASSERT(fc);
44c440bc 547
5cd6d0e5 548 if (fc->members) {
44c440bc
PP
549 uint64_t i;
550
5cd6d0e5
PP
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);
44c440bc 555
5cd6d0e5 556 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
557 }
558
5cd6d0e5 559 g_array_free(fc->members, TRUE);
44c440bc
PP
560 }
561
5cd6d0e5 562 g_free(fc);
44c440bc
PP
563}
564
565static inline
5cd6d0e5 566void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
44c440bc 567{
5cd6d0e5
PP
568 BT_ASSERT(fc);
569 ctf_field_class_destroy(fc->elem_fc);
44c440bc
PP
570}
571
572static inline
5cd6d0e5 573void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
44c440bc 574{
5cd6d0e5
PP
575 BT_ASSERT(fc);
576 _ctf_field_class_array_base_fini((void *) fc);
577 g_free(fc);
44c440bc
PP
578}
579
580static inline
5cd6d0e5 581void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
44c440bc 582{
5cd6d0e5
PP
583 BT_ASSERT(fc);
584 _ctf_field_class_array_base_fini((void *) fc);
44c440bc 585
5cd6d0e5
PP
586 if (fc->length_ref) {
587 g_string_free(fc->length_ref, TRUE);
44c440bc
PP
588 }
589
5cd6d0e5
PP
590 ctf_field_path_fini(&fc->length_path);
591 g_free(fc);
44c440bc
PP
592}
593
594static inline
5cd6d0e5 595void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
44c440bc 596{
5cd6d0e5 597 BT_ASSERT(fc);
44c440bc 598
5cd6d0e5 599 if (fc->options) {
44c440bc
PP
600 uint64_t i;
601
5cd6d0e5
PP
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);
44c440bc 606
5cd6d0e5 607 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
608 }
609
5cd6d0e5 610 g_array_free(fc->options, TRUE);
44c440bc
PP
611 }
612
5cd6d0e5
PP
613 if (fc->ranges) {
614 g_array_free(fc->ranges, TRUE);
44c440bc
PP
615 }
616
5cd6d0e5
PP
617 if (fc->tag_ref) {
618 g_string_free(fc->tag_ref, TRUE);
44c440bc
PP
619 }
620
5cd6d0e5
PP
621 ctf_field_path_fini(&fc->tag_path);
622 g_free(fc);
44c440bc
PP
623}
624
625static inline
5cd6d0e5 626void ctf_field_class_destroy(struct ctf_field_class *fc)
44c440bc 627{
5cd6d0e5 628 if (!fc) {
44c440bc
PP
629 return;
630 }
631
864cad70
PP
632 switch (fc->type) {
633 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 634 _ctf_field_class_int_destroy((void *) fc);
44c440bc 635 break;
864cad70 636 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 637 _ctf_field_class_enum_destroy((void *) fc);
44c440bc 638 break;
864cad70 639 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 640 _ctf_field_class_float_destroy((void *) fc);
44c440bc 641 break;
864cad70 642 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 643 _ctf_field_class_string_destroy((void *) fc);
44c440bc 644 break;
864cad70 645 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 646 _ctf_field_class_struct_destroy((void *) fc);
44c440bc 647 break;
864cad70 648 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 649 _ctf_field_class_array_destroy((void *) fc);
44c440bc 650 break;
864cad70 651 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 652 _ctf_field_class_sequence_destroy((void *) fc);
44c440bc 653 break;
864cad70 654 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 655 _ctf_field_class_variant_destroy((void *) fc);
44c440bc
PP
656 break;
657 default:
658 abort();
659 }
660}
661
662static inline
5cd6d0e5 663void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum *fc,
44c440bc
PP
664 const char *label, uint64_t u_lower, uint64_t u_upper)
665{
5cd6d0e5 666 struct ctf_field_class_enum_mapping *mapping;
44c440bc 667
5cd6d0e5 668 BT_ASSERT(fc);
44c440bc 669 BT_ASSERT(label);
5cd6d0e5 670 g_array_set_size(fc->mappings, fc->mappings->len + 1);
44c440bc 671
5cd6d0e5
PP
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);
44c440bc
PP
675 g_string_assign(mapping->label, label);
676 mapping->range.lower.u = u_lower;
677 mapping->range.upper.u = u_upper;
678}
679
680static inline
5cd6d0e5
PP
681struct ctf_field_class_enum_mapping *ctf_field_class_enum_borrow_mapping_by_index(
682 struct ctf_field_class_enum *fc, uint64_t index)
44c440bc 683{
5cd6d0e5
PP
684 BT_ASSERT(fc);
685 BT_ASSERT(index < fc->mappings->len);
686 return &g_array_index(fc->mappings, struct ctf_field_class_enum_mapping,
44c440bc
PP
687 index);
688}
689
690static inline
5cd6d0e5
PP
691struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_index(
692 struct ctf_field_class_struct *fc, uint64_t index)
44c440bc 693{
5cd6d0e5
PP
694 BT_ASSERT(fc);
695 BT_ASSERT(index < fc->members->len);
696 return &g_array_index(fc->members, struct ctf_named_field_class,
44c440bc
PP
697 index);
698}
699
700static inline
5cd6d0e5
PP
701struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_name(
702 struct ctf_field_class_struct *fc, const char *name)
44c440bc
PP
703{
704 uint64_t i;
5cd6d0e5 705 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 706
5cd6d0e5 707 BT_ASSERT(fc);
44c440bc
PP
708 BT_ASSERT(name);
709
5cd6d0e5
PP
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);
44c440bc 713
5cd6d0e5
PP
714 if (strcmp(name, named_fc->name->str) == 0) {
715 ret_named_fc = named_fc;
44c440bc
PP
716 goto end;
717 }
718 }
719
720end:
5cd6d0e5 721 return ret_named_fc;
44c440bc
PP
722}
723
724static inline
5cd6d0e5
PP
725struct ctf_field_class *ctf_field_class_struct_borrow_member_field_class_by_name(
726 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 727{
5cd6d0e5
PP
728 struct ctf_named_field_class *named_fc = NULL;
729 struct ctf_field_class *fc = NULL;
44c440bc 730
5cd6d0e5 731 if (!struct_fc) {
44c440bc
PP
732 goto end;
733 }
734
5cd6d0e5
PP
735 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
736 if (!named_fc) {
44c440bc
PP
737 goto end;
738 }
739
5cd6d0e5 740 fc = named_fc->fc;
44c440bc
PP
741
742end:
5cd6d0e5 743 return fc;
44c440bc
PP
744}
745
746static inline
5cd6d0e5
PP
747struct ctf_field_class_int *
748ctf_field_class_struct_borrow_member_int_field_class_by_name(
749 struct ctf_field_class_struct *struct_fc, const char *name)
44c440bc 750{
5cd6d0e5 751 struct ctf_field_class_int *int_fc = NULL;
44c440bc 752
5cd6d0e5
PP
753 int_fc = (void *)
754 ctf_field_class_struct_borrow_member_field_class_by_name(
755 struct_fc, name);
756 if (!int_fc) {
44c440bc
PP
757 goto end;
758 }
759
864cad70
PP
760 if (int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_INT &&
761 int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_ENUM) {
5cd6d0e5 762 int_fc = NULL;
44c440bc
PP
763 goto end;
764 }
765
766end:
5cd6d0e5 767 return int_fc;
44c440bc
PP
768}
769
770
771static inline
5cd6d0e5
PP
772void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
773 const char *name, struct ctf_field_class *member_fc)
44c440bc 774{
5cd6d0e5 775 struct ctf_named_field_class *named_fc;
44c440bc 776
5cd6d0e5 777 BT_ASSERT(fc);
44c440bc 778 BT_ASSERT(name);
5cd6d0e5 779 g_array_set_size(fc->members, fc->members->len + 1);
44c440bc 780
5cd6d0e5
PP
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;
44c440bc 786
5cd6d0e5
PP
787 if (member_fc->alignment > fc->base.alignment) {
788 fc->base.alignment = member_fc->alignment;
44c440bc
PP
789 }
790}
791
792static inline
5cd6d0e5
PP
793struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_index(
794 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 795{
5cd6d0e5
PP
796 BT_ASSERT(fc);
797 BT_ASSERT(index < fc->options->len);
798 return &g_array_index(fc->options, struct ctf_named_field_class,
44c440bc
PP
799 index);
800}
801
802static inline
5cd6d0e5
PP
803struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_name(
804 struct ctf_field_class_variant *fc, const char *name)
44c440bc
PP
805{
806 uint64_t i;
5cd6d0e5 807 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 808
5cd6d0e5 809 BT_ASSERT(fc);
44c440bc
PP
810 BT_ASSERT(name);
811
5cd6d0e5
PP
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);
44c440bc 815
5cd6d0e5
PP
816 if (strcmp(name, named_fc->name->str) == 0) {
817 ret_named_fc = named_fc;
44c440bc
PP
818 goto end;
819 }
820 }
821
822end:
5cd6d0e5 823 return ret_named_fc;
44c440bc
PP
824}
825
826static inline
5cd6d0e5
PP
827struct ctf_field_class_variant_range *
828ctf_field_class_variant_borrow_range_by_index(
829 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 830{
5cd6d0e5
PP
831 BT_ASSERT(fc);
832 BT_ASSERT(index < fc->ranges->len);
833 return &g_array_index(fc->ranges, struct ctf_field_class_variant_range,
44c440bc
PP
834 index);
835}
836
837static inline
5cd6d0e5
PP
838void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
839 const char *name, struct ctf_field_class *option_fc)
44c440bc 840{
5cd6d0e5 841 struct ctf_named_field_class *named_fc;
44c440bc 842
5cd6d0e5 843 BT_ASSERT(fc);
44c440bc 844 BT_ASSERT(name);
5cd6d0e5 845 g_array_set_size(fc->options, fc->options->len + 1);
44c440bc 846
5cd6d0e5
PP
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;
44c440bc
PP
852}
853
854static inline
5cd6d0e5
PP
855void ctf_field_class_variant_set_tag_field_class(
856 struct ctf_field_class_variant *fc,
857 struct ctf_field_class_enum *tag_fc)
44c440bc
PP
858{
859 uint64_t option_i;
860
5cd6d0e5
PP
861 BT_ASSERT(fc);
862 BT_ASSERT(tag_fc);
863 fc->tag_fc = tag_fc;
44c440bc 864
5cd6d0e5 865 for (option_i = 0; option_i < fc->options->len; option_i++) {
44c440bc 866 uint64_t mapping_i;
5cd6d0e5
PP
867 struct ctf_named_field_class *named_fc =
868 ctf_field_class_variant_borrow_option_by_index(
869 fc, option_i);
44c440bc 870
5cd6d0e5 871 for (mapping_i = 0; mapping_i < tag_fc->mappings->len;
44c440bc 872 mapping_i++) {
5cd6d0e5
PP
873 struct ctf_field_class_enum_mapping *mapping =
874 ctf_field_class_enum_borrow_mapping_by_index(
875 tag_fc, mapping_i);
44c440bc 876
5cd6d0e5 877 if (strcmp(named_fc->name->str,
44c440bc 878 mapping->label->str) == 0) {
5cd6d0e5 879 struct ctf_field_class_variant_range range;
44c440bc
PP
880
881 range.range = mapping->range;
882 range.option_index = option_i;
5cd6d0e5 883 g_array_append_val(fc->ranges, range);
44c440bc
PP
884 }
885 }
886 }
887}
888
889static inline
5cd6d0e5
PP
890struct ctf_field_class *ctf_field_class_compound_borrow_field_class_by_index(
891 struct ctf_field_class *comp_fc, uint64_t index)
44c440bc 892{
5cd6d0e5 893 struct ctf_field_class *fc = NULL;
44c440bc 894
864cad70
PP
895 switch (comp_fc->type) {
896 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 897 {
5cd6d0e5
PP
898 struct ctf_named_field_class *named_fc =
899 ctf_field_class_struct_borrow_member_by_index(
900 (void *) comp_fc, index);
44c440bc 901
5cd6d0e5
PP
902 BT_ASSERT(named_fc);
903 fc = named_fc->fc;
44c440bc
PP
904 break;
905 }
864cad70 906 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 907 {
5cd6d0e5
PP
908 struct ctf_named_field_class *named_fc =
909 ctf_field_class_variant_borrow_option_by_index(
910 (void *) comp_fc, index);
44c440bc 911
5cd6d0e5
PP
912 BT_ASSERT(named_fc);
913 fc = named_fc->fc;
44c440bc
PP
914 break;
915 }
864cad70
PP
916 case CTF_FIELD_CLASS_TYPE_ARRAY:
917 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc 918 {
5cd6d0e5 919 struct ctf_field_class_array_base *array_fc = (void *) comp_fc;
44c440bc 920
5cd6d0e5 921 fc = array_fc->elem_fc;
44c440bc
PP
922 break;
923 }
924 default:
925 break;
926 }
927
5cd6d0e5 928 return fc;
44c440bc
PP
929}
930
931static inline
5cd6d0e5 932uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
44c440bc
PP
933{
934 uint64_t field_count;
935
864cad70
PP
936 switch (fc->type) {
937 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 938 {
5cd6d0e5 939 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 940
5cd6d0e5 941 field_count = struct_fc->members->len;
44c440bc
PP
942 break;
943 }
864cad70 944 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 945 {
5cd6d0e5 946 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 947
5cd6d0e5 948 field_count = var_fc->options->len;
44c440bc
PP
949 break;
950 }
864cad70
PP
951 case CTF_FIELD_CLASS_TYPE_ARRAY:
952 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc
PP
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
966static inline
5cd6d0e5
PP
967int64_t ctf_field_class_compound_get_field_class_index_from_name(
968 struct ctf_field_class *fc, const char *name)
44c440bc
PP
969{
970 int64_t ret_index = -1;
971 uint64_t i;
972
864cad70
PP
973 switch (fc->type) {
974 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 975 {
5cd6d0e5 976 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 977
5cd6d0e5
PP
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);
44c440bc 982
5cd6d0e5 983 if (strcmp(name, named_fc->name->str) == 0) {
44c440bc
PP
984 ret_index = (int64_t) i;
985 goto end;
986 }
987 }
988
989 break;
990 }
864cad70 991 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 992 {
5cd6d0e5 993 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 994
5cd6d0e5
PP
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);
44c440bc 999
5cd6d0e5 1000 if (strcmp(name, named_fc->name->str) == 0) {
44c440bc
PP
1001 ret_index = (int64_t) i;
1002 goto end;
1003 }
1004 }
1005
1006 break;
1007 }
1008 default:
1009 break;
1010 }
1011
1012end:
1013 return ret_index;
1014}
1015
1016static inline
1017void 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
1023static inline
1024int64_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
1032static inline
1033void ctf_field_path_clear(struct ctf_field_path *fp)
1034{
1035 BT_ASSERT(fp);
1036 g_array_set_size(fp->path, 0);
1037}
1038
83ebb7f1
PP
1039static inline
1040const 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
44c440bc
PP
1060static inline
1061GString *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
83ebb7f1 1072 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
44c440bc
PP
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
1081end:
1082 return str;
1083}
1084
1085static inline
5cd6d0e5 1086struct ctf_field_class *ctf_field_path_borrow_field_class(
44c440bc
PP
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;
5cd6d0e5 1093 struct ctf_field_class *fc;
44c440bc
PP
1094
1095 switch (field_path->root) {
83ebb7f1 1096 case CTF_SCOPE_PACKET_HEADER:
5cd6d0e5 1097 fc = tc->packet_header_fc;
44c440bc 1098 break;
83ebb7f1 1099 case CTF_SCOPE_PACKET_CONTEXT:
5cd6d0e5 1100 fc = sc->packet_context_fc;
44c440bc 1101 break;
83ebb7f1 1102 case CTF_SCOPE_EVENT_HEADER:
5cd6d0e5 1103 fc = sc->event_header_fc;
44c440bc 1104 break;
83ebb7f1 1105 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
5cd6d0e5 1106 fc = sc->event_common_context_fc;
44c440bc 1107 break;
83ebb7f1 1108 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
5cd6d0e5 1109 fc = ec->spec_context_fc;
44c440bc 1110 break;
83ebb7f1 1111 case CTF_SCOPE_EVENT_PAYLOAD:
5cd6d0e5 1112 fc = ec->payload_fc;
44c440bc
PP
1113 break;
1114 default:
1115 abort();
1116 }
1117
5cd6d0e5 1118 BT_ASSERT(fc);
44c440bc
PP
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);
5cd6d0e5
PP
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;
44c440bc
PP
1128 }
1129
5cd6d0e5
PP
1130 BT_ASSERT(fc);
1131 return fc;
44c440bc
PP
1132}
1133
1134static inline
5cd6d0e5 1135struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
44c440bc
PP
1136
1137static inline
5cd6d0e5
PP
1138void 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)
44c440bc 1141{
5cd6d0e5
PP
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;
44c440bc
PP
1146}
1147
1148static inline
5cd6d0e5
PP
1149void ctf_field_class_int_copy_content(
1150 struct ctf_field_class_int *dst_fc,
1151 struct ctf_field_class_int *src_fc)
44c440bc 1152{
5cd6d0e5
PP
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;
398454ed 1158 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
5cd6d0e5 1159 dst_fc->storing_index = src_fc->storing_index;
44c440bc
PP
1160}
1161
1162static inline
5cd6d0e5
PP
1163struct ctf_field_class_int *_ctf_field_class_int_copy(
1164 struct ctf_field_class_int *fc)
44c440bc 1165{
5cd6d0e5 1166 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
44c440bc 1167
5cd6d0e5
PP
1168 BT_ASSERT(copy_fc);
1169 ctf_field_class_int_copy_content(copy_fc, fc);
1170 return copy_fc;
44c440bc
PP
1171}
1172
1173static inline
5cd6d0e5
PP
1174struct ctf_field_class_enum *_ctf_field_class_enum_copy(
1175 struct ctf_field_class_enum *fc)
44c440bc 1176{
5cd6d0e5 1177 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
44c440bc
PP
1178 uint64_t i;
1179
5cd6d0e5
PP
1180 BT_ASSERT(copy_fc);
1181 ctf_field_class_int_copy_content((void *) copy_fc, (void *) fc);
44c440bc 1182
5cd6d0e5
PP
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);
44c440bc 1187
5cd6d0e5 1188 ctf_field_class_enum_append_mapping(copy_fc, mapping->label->str,
44c440bc
PP
1189 mapping->range.lower.u, mapping->range.upper.u);
1190 }
1191
5cd6d0e5 1192 return copy_fc;
44c440bc
PP
1193}
1194
1195static inline
5cd6d0e5
PP
1196struct ctf_field_class_float *_ctf_field_class_float_copy(
1197 struct ctf_field_class_float *fc)
44c440bc 1198{
5cd6d0e5 1199 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
44c440bc 1200
5cd6d0e5
PP
1201 BT_ASSERT(copy_fc);
1202 ctf_field_class_bit_array_copy_content((void *) copy_fc, (void *) fc);
1203 return copy_fc;
44c440bc
PP
1204}
1205
1206static inline
5cd6d0e5
PP
1207struct ctf_field_class_string *_ctf_field_class_string_copy(
1208 struct ctf_field_class_string *fc)
44c440bc 1209{
5cd6d0e5 1210 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
44c440bc 1211
5cd6d0e5
PP
1212 BT_ASSERT(copy_fc);
1213 return copy_fc;
44c440bc
PP
1214}
1215
1216static inline
5cd6d0e5
PP
1217struct ctf_field_class_struct *_ctf_field_class_struct_copy(
1218 struct ctf_field_class_struct *fc)
44c440bc 1219{
5cd6d0e5 1220 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
44c440bc
PP
1221 uint64_t i;
1222
5cd6d0e5 1223 BT_ASSERT(copy_fc);
44c440bc 1224
5cd6d0e5
PP
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);
44c440bc 1229
5cd6d0e5
PP
1230 ctf_field_class_struct_append_member(copy_fc,
1231 named_fc->name->str,
1232 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1233 }
1234
5cd6d0e5 1235 return copy_fc;
44c440bc
PP
1236}
1237
1238static inline
1239void 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
1257static inline
5cd6d0e5
PP
1258struct ctf_field_class_variant *_ctf_field_class_variant_copy(
1259 struct ctf_field_class_variant *fc)
44c440bc 1260{
5cd6d0e5
PP
1261 struct ctf_field_class_variant *copy_fc =
1262 ctf_field_class_variant_create();
44c440bc
PP
1263 uint64_t i;
1264
5cd6d0e5 1265 BT_ASSERT(copy_fc);
44c440bc 1266
5cd6d0e5
PP
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);
44c440bc 1271
5cd6d0e5
PP
1272 ctf_field_class_variant_append_option(copy_fc,
1273 named_fc->name->str,
1274 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1275 }
1276
5cd6d0e5
PP
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);
44c440bc 1281
5cd6d0e5 1282 g_array_append_val(copy_fc->ranges, *range);
44c440bc
PP
1283 }
1284
5cd6d0e5
PP
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;
44c440bc
PP
1289}
1290
1291static inline
5cd6d0e5
PP
1292void 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)
44c440bc 1295{
5cd6d0e5
PP
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;
44c440bc
PP
1300}
1301
1302static inline
5cd6d0e5
PP
1303struct ctf_field_class_array *_ctf_field_class_array_copy(
1304 struct ctf_field_class_array *fc)
44c440bc 1305{
5cd6d0e5 1306 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
44c440bc 1307
5cd6d0e5
PP
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;
44c440bc
PP
1312}
1313
1314static inline
5cd6d0e5
PP
1315struct ctf_field_class_sequence *_ctf_field_class_sequence_copy(
1316 struct ctf_field_class_sequence *fc)
44c440bc 1317{
5cd6d0e5
PP
1318 struct ctf_field_class_sequence *copy_fc =
1319 ctf_field_class_sequence_create();
44c440bc 1320
5cd6d0e5
PP
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;
44c440bc
PP
1327}
1328
1329static inline
5cd6d0e5 1330struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
44c440bc 1331{
5cd6d0e5 1332 struct ctf_field_class *copy_fc = NULL;
44c440bc 1333
5cd6d0e5 1334 if (!fc) {
44c440bc
PP
1335 goto end;
1336 }
1337
1338 /*
1339 * Translation should not have happened yet.
1340 */
5cd6d0e5 1341 BT_ASSERT(!fc->ir_fc);
44c440bc 1342
864cad70
PP
1343 switch (fc->type) {
1344 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 1345 copy_fc = (void *) _ctf_field_class_int_copy((void *) fc);
44c440bc 1346 break;
864cad70 1347 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 1348 copy_fc = (void *) _ctf_field_class_enum_copy((void *) fc);
44c440bc 1349 break;
864cad70 1350 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 1351 copy_fc = (void *) _ctf_field_class_float_copy((void *) fc);
44c440bc 1352 break;
864cad70 1353 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 1354 copy_fc = (void *) _ctf_field_class_string_copy((void *) fc);
44c440bc 1355 break;
864cad70 1356 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 1357 copy_fc = (void *) _ctf_field_class_struct_copy((void *) fc);
44c440bc 1358 break;
864cad70 1359 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 1360 copy_fc = (void *) _ctf_field_class_array_copy((void *) fc);
44c440bc 1361 break;
864cad70 1362 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 1363 copy_fc = (void *) _ctf_field_class_sequence_copy((void *) fc);
44c440bc 1364 break;
864cad70 1365 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 1366 copy_fc = (void *) _ctf_field_class_variant_copy((void *) fc);
44c440bc
PP
1367 break;
1368 default:
1369 abort();
1370 }
1371
864cad70 1372 copy_fc->type = fc->type;
5cd6d0e5
PP
1373 copy_fc->alignment = fc->alignment;
1374 copy_fc->in_ir = fc->in_ir;
44c440bc
PP
1375
1376end:
5cd6d0e5 1377 return copy_fc;
44c440bc
PP
1378}
1379
1380static inline
1381struct 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
1394static inline
1395void 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
5cd6d0e5
PP
1409 ctf_field_class_destroy(ec->spec_context_fc);
1410 ctf_field_class_destroy(ec->payload_fc);
44c440bc
PP
1411 g_free(ec);
1412}
1413
1414static inline
1415struct 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
1429static inline
1430void 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
5cd6d0e5
PP
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);
44c440bc
PP
1447 g_free(sc);
1448}
1449
1450static inline
1451void 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
1459static inline
1460struct ctf_event_class *ctf_stream_class_borrow_event_class_by_id(
864cad70 1461 struct ctf_stream_class *sc, uint64_t type)
44c440bc
PP
1462{
1463 BT_ASSERT(sc);
1464 return g_hash_table_lookup(sc->event_classes_by_id,
864cad70 1465 GUINT_TO_POINTER((guint) type));
44c440bc
PP
1466}
1467
1468static inline
1469void _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
1478static inline
1479void _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
0f2d58c9
PP
1492static inline
1493struct 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
1505static inline
1506void 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
44c440bc
PP
1524static inline
1525struct 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);
44c440bc 1530 tc->default_byte_order = -1;
44c440bc 1531 tc->clock_classes = g_ptr_array_new_with_free_func(
0f2d58c9 1532 (GDestroyNotify) ctf_clock_class_destroy);
44c440bc
PP
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
1542static inline
1543void ctf_trace_class_destroy(struct ctf_trace_class *tc)
1544{
1545 if (!tc) {
1546 return;
1547 }
1548
5cd6d0e5 1549 ctf_field_class_destroy(tc->packet_header_fc);
44c440bc
PP
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
1576static inline
1577void 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
1600static inline
1601struct 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
1618end:
1619 return ret_sc;
1620}
1621
1622static inline
0f2d58c9 1623struct ctf_clock_class *ctf_trace_class_borrow_clock_class_by_name(
44c440bc
PP
1624 struct ctf_trace_class *tc, const char *name)
1625{
1626 uint64_t i;
0f2d58c9 1627 struct ctf_clock_class *ret_cc = NULL;
44c440bc
PP
1628
1629 BT_ASSERT(tc);
1630 BT_ASSERT(name);
1631
1632 for (i = 0; i < tc->clock_classes->len; i++) {
0f2d58c9 1633 struct ctf_clock_class *cc = tc->clock_classes->pdata[i];
44c440bc 1634
0f2d58c9
PP
1635 BT_ASSERT(cc->name);
1636 if (strcmp(cc->name->str, name) == 0) {
44c440bc
PP
1637 ret_cc = cc;
1638 goto end;
1639 }
1640 }
1641
1642end:
1643 return ret_cc;
1644}
1645
1646static inline
1647struct 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
1656static inline
1657struct 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
1676end:
1677 return ret_entry;
1678}
1679
1680#endif /* _CTF_META_H */
This page took 0.107261 seconds and 4 git commands to generate.