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