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