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