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