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