lib: remove CTF concepts of packet and event headers
[babeltrace.git] / 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
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
864cad70
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,
44c440bc
PP
34};
35
5cd6d0e5
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,
44c440bc
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
83ebb7f1
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
0f2d58c9
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
5cd6d0e5 86struct ctf_field_class {
864cad70 87 enum ctf_field_class_type type;
44c440bc
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. */
b19ff26f 93 bt_field_class *ir_fc;
44c440bc
PP
94};
95
5cd6d0e5
PP
96struct ctf_field_class_bit_array {
97 struct ctf_field_class base;
44c440bc
PP
98 enum ctf_byte_order byte_order;
99 unsigned int size;
100};
101
5cd6d0e5
PP
102struct ctf_field_class_int {
103 struct ctf_field_class_bit_array base;
104 enum ctf_field_class_meaning meaning;
44c440bc 105 bool is_signed;
4cdfc5e8 106 bt_field_class_integer_preferred_display_base disp_base;
44c440bc
PP
107 enum ctf_encoding encoding;
108 int64_t storing_index;
109
0f2d58c9
PP
110 /* Weak */
111 struct ctf_clock_class *mapped_clock_class;
44c440bc
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
5cd6d0e5 126struct ctf_field_class_enum_mapping {
44c440bc
PP
127 GString *label;
128 struct ctf_range range;
129};
130
5cd6d0e5
PP
131struct ctf_field_class_enum {
132 struct ctf_field_class_int base;
44c440bc 133
5cd6d0e5 134 /* Array of `struct ctf_field_class_enum_mapping` */
44c440bc
PP
135 GArray *mappings;
136};
137
5cd6d0e5
PP
138struct ctf_field_class_float {
139 struct ctf_field_class_bit_array base;
44c440bc
PP
140};
141
5cd6d0e5
PP
142struct ctf_field_class_string {
143 struct ctf_field_class base;
44c440bc
PP
144 enum ctf_encoding encoding;
145};
146
5cd6d0e5 147struct ctf_named_field_class {
44c440bc
PP
148 GString *name;
149
150 /* Owned by this */
5cd6d0e5 151 struct ctf_field_class *fc;
44c440bc
PP
152};
153
5cd6d0e5
PP
154struct ctf_field_class_struct {
155 struct ctf_field_class base;
44c440bc 156
5cd6d0e5 157 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
158 GArray *members;
159};
160
161struct ctf_field_path {
83ebb7f1 162 enum ctf_scope root;
44c440bc
PP
163
164 /* Array of `int64_t` */
165 GArray *path;
166};
167
5cd6d0e5 168struct ctf_field_class_variant_range {
44c440bc
PP
169 struct ctf_range range;
170 uint64_t option_index;
171};
172
5cd6d0e5
PP
173struct ctf_field_class_variant {
174 struct ctf_field_class base;
44c440bc
PP
175 GString *tag_ref;
176 struct ctf_field_path tag_path;
177 uint64_t stored_tag_index;
178
5cd6d0e5 179 /* Array of `struct ctf_named_field_class` */
44c440bc
PP
180 GArray *options;
181
5cd6d0e5 182 /* Array of `struct ctf_field_class_variant_range` */
44c440bc
PP
183 GArray *ranges;
184
185 /* Weak */
5cd6d0e5 186 struct ctf_field_class_enum *tag_fc;
44c440bc
PP
187};
188
5cd6d0e5
PP
189struct ctf_field_class_array_base {
190 struct ctf_field_class base;
191 struct ctf_field_class *elem_fc;
44c440bc
PP
192 bool is_text;
193};
194
5cd6d0e5
PP
195struct ctf_field_class_array {
196 struct ctf_field_class_array_base base;
197 enum ctf_field_class_meaning meaning;
44c440bc
PP
198 uint64_t length;
199};
200
5cd6d0e5
PP
201struct ctf_field_class_sequence {
202 struct ctf_field_class_array_base base;
44c440bc
PP
203 GString *length_ref;
204 struct ctf_field_path length_path;
205 uint64_t stored_length_index;
206
207 /* Weak */
5cd6d0e5 208 struct ctf_field_class_int *length_fc;
44c440bc
PP
209};
210
211struct ctf_event_class {
212 GString *name;
213 uint64_t id;
214 GString *emf_uri;
4cdfc5e8 215 bt_event_class_log_level log_level;
44c440bc
PP
216 bool is_translated;
217
218 /* Owned by this */
5cd6d0e5 219 struct ctf_field_class *spec_context_fc;
44c440bc
PP
220
221 /* Owned by this */
5cd6d0e5 222 struct ctf_field_class *payload_fc;
44c440bc
PP
223
224 /* Weak, set during translation */
b19ff26f 225 bt_event_class *ir_ec;
44c440bc
PP
226};
227
228struct ctf_stream_class {
229 uint64_t id;
230 bool is_translated;
231
232 /* Owned by this */
5cd6d0e5 233 struct ctf_field_class *packet_context_fc;
44c440bc
PP
234
235 /* Owned by this */
5cd6d0e5 236 struct ctf_field_class *event_header_fc;
44c440bc
PP
237
238 /* Owned by this */
5cd6d0e5 239 struct ctf_field_class *event_common_context_fc;
44c440bc
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
0f2d58c9
PP
250 /* Weak */
251 struct ctf_clock_class *default_clock_class;
44c440bc
PP
252
253 /* Weak, set during translation */
b19ff26f 254 bt_stream_class *ir_sc;
44c440bc
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 {
44c440bc
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 */
5cd6d0e5 280 struct ctf_field_class *packet_header_fc;
44c440bc
PP
281
282 uint64_t stored_value_count;
283
0f2d58c9 284 /* Array of `struct ctf_clock_class *` (owned by this) */
44c440bc
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 */
b19ff26f 296 bt_trace_class *ir_tc;
44c440bc
PP
297};
298
299static inline
5cd6d0e5 300void ctf_field_class_destroy(struct ctf_field_class *fc);
44c440bc
PP
301
302static inline
864cad70
PP
303void _ctf_field_class_init(struct ctf_field_class *fc,
304 enum ctf_field_class_type type, unsigned int alignment)
44c440bc 305{
5cd6d0e5 306 BT_ASSERT(fc);
864cad70 307 fc->type = type;
5cd6d0e5
PP
308 fc->alignment = alignment;
309 fc->in_ir = false;
44c440bc
PP
310}
311
312static inline
5cd6d0e5 313void _ctf_field_class_bit_array_init(struct ctf_field_class_bit_array *fc,
864cad70 314 enum ctf_field_class_type type)
44c440bc 315{
864cad70 316 _ctf_field_class_init((void *) fc, type, 1);
44c440bc
PP
317}
318
319static inline
5cd6d0e5 320void _ctf_field_class_int_init(struct ctf_field_class_int *fc,
864cad70 321 enum ctf_field_class_type type)
44c440bc 322{
864cad70 323 _ctf_field_class_bit_array_init((void *) fc, type);
5cd6d0e5
PP
324 fc->meaning = CTF_FIELD_CLASS_MEANING_NONE;
325 fc->storing_index = -1;
44c440bc
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
5cd6d0e5 347void _ctf_named_field_class_init(struct ctf_named_field_class *named_fc)
44c440bc 348{
5cd6d0e5
PP
349 BT_ASSERT(named_fc);
350 named_fc->name = g_string_new(NULL);
351 BT_ASSERT(named_fc->name);
44c440bc
PP
352}
353
354static inline
5cd6d0e5 355void _ctf_named_field_class_fini(struct ctf_named_field_class *named_fc)
44c440bc 356{
5cd6d0e5 357 BT_ASSERT(named_fc);
44c440bc 358
5cd6d0e5
PP
359 if (named_fc->name) {
360 g_string_free(named_fc->name, TRUE);
44c440bc
PP
361 }
362
5cd6d0e5 363 ctf_field_class_destroy(named_fc->fc);
44c440bc
PP
364}
365
366static inline
5cd6d0e5
PP
367void _ctf_field_class_enum_mapping_init(
368 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
369{
370 BT_ASSERT(mapping);
371 mapping->label = g_string_new(NULL);
372 BT_ASSERT(mapping->label);
373}
374
375static inline
5cd6d0e5
PP
376void _ctf_field_class_enum_mapping_fini(
377 struct ctf_field_class_enum_mapping *mapping)
44c440bc
PP
378{
379 BT_ASSERT(mapping);
380
381 if (mapping->label) {
382 g_string_free(mapping->label, TRUE);
383 }
384}
385
386static inline
5cd6d0e5 387struct ctf_field_class_int *ctf_field_class_int_create(void)
44c440bc 388{
5cd6d0e5 389 struct ctf_field_class_int *fc = g_new0(struct ctf_field_class_int, 1);
44c440bc 390
5cd6d0e5 391 BT_ASSERT(fc);
864cad70 392 _ctf_field_class_int_init(fc, CTF_FIELD_CLASS_TYPE_INT);
5cd6d0e5 393 return fc;
44c440bc
PP
394}
395
396static inline
5cd6d0e5 397struct ctf_field_class_float *ctf_field_class_float_create(void)
44c440bc 398{
5cd6d0e5
PP
399 struct ctf_field_class_float *fc =
400 g_new0(struct ctf_field_class_float, 1);
44c440bc 401
5cd6d0e5 402 BT_ASSERT(fc);
864cad70 403 _ctf_field_class_bit_array_init((void *) fc, CTF_FIELD_CLASS_TYPE_FLOAT);
5cd6d0e5 404 return fc;
44c440bc
PP
405}
406
407static inline
5cd6d0e5 408struct ctf_field_class_string *ctf_field_class_string_create(void)
44c440bc 409{
5cd6d0e5
PP
410 struct ctf_field_class_string *fc =
411 g_new0(struct ctf_field_class_string, 1);
44c440bc 412
5cd6d0e5 413 BT_ASSERT(fc);
864cad70 414 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRING, 8);
5cd6d0e5 415 return fc;
44c440bc
PP
416}
417
418static inline
5cd6d0e5 419struct ctf_field_class_enum *ctf_field_class_enum_create(void)
44c440bc 420{
5cd6d0e5 421 struct ctf_field_class_enum *fc = g_new0(struct ctf_field_class_enum, 1);
44c440bc 422
5cd6d0e5 423 BT_ASSERT(fc);
864cad70 424 _ctf_field_class_int_init((void *) fc, CTF_FIELD_CLASS_TYPE_ENUM);
5cd6d0e5
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;
44c440bc
PP
429}
430
431static inline
5cd6d0e5 432struct ctf_field_class_struct *ctf_field_class_struct_create(void)
44c440bc 433{
5cd6d0e5
PP
434 struct ctf_field_class_struct *fc =
435 g_new0(struct ctf_field_class_struct, 1);
44c440bc 436
5cd6d0e5 437 BT_ASSERT(fc);
864cad70 438 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_STRUCT, 1);
5cd6d0e5
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;
44c440bc
PP
444}
445
446static inline
5cd6d0e5 447struct ctf_field_class_variant *ctf_field_class_variant_create(void)
44c440bc 448{
5cd6d0e5
PP
449 struct ctf_field_class_variant *fc =
450 g_new0(struct ctf_field_class_variant, 1);
44c440bc 451
5cd6d0e5 452 BT_ASSERT(fc);
864cad70 453 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_VARIANT, 1);
5cd6d0e5
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;
44c440bc
PP
465}
466
467static inline
5cd6d0e5 468struct ctf_field_class_array *ctf_field_class_array_create(void)
44c440bc 469{
5cd6d0e5
PP
470 struct ctf_field_class_array *fc =
471 g_new0(struct ctf_field_class_array, 1);
44c440bc 472
5cd6d0e5 473 BT_ASSERT(fc);
864cad70 474 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_ARRAY, 1);
5cd6d0e5
PP
475 fc->base.base.is_compound = true;
476 return fc;
44c440bc
PP
477}
478
479static inline
5cd6d0e5 480struct ctf_field_class_sequence *ctf_field_class_sequence_create(void)
44c440bc 481{
5cd6d0e5
PP
482 struct ctf_field_class_sequence *fc =
483 g_new0(struct ctf_field_class_sequence, 1);
44c440bc 484
5cd6d0e5 485 BT_ASSERT(fc);
864cad70 486 _ctf_field_class_init((void *) fc, CTF_FIELD_CLASS_TYPE_SEQUENCE, 1);
5cd6d0e5
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;
44c440bc
PP
492}
493
494static inline
5cd6d0e5 495void _ctf_field_class_int_destroy(struct ctf_field_class_int *fc)
44c440bc 496{
5cd6d0e5 497 BT_ASSERT(fc);
5cd6d0e5 498 g_free(fc);
44c440bc
PP
499}
500
501static inline
5cd6d0e5 502void _ctf_field_class_enum_destroy(struct ctf_field_class_enum *fc)
44c440bc 503{
5cd6d0e5 504 BT_ASSERT(fc);
44c440bc 505
5cd6d0e5 506 if (fc->mappings) {
44c440bc
PP
507 uint64_t i;
508
5cd6d0e5
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);
44c440bc 513
5cd6d0e5 514 _ctf_field_class_enum_mapping_fini(mapping);
44c440bc
PP
515 }
516
5cd6d0e5 517 g_array_free(fc->mappings, TRUE);
44c440bc
PP
518 }
519
5cd6d0e5 520 g_free(fc);
44c440bc
PP
521}
522
523static inline
5cd6d0e5 524void _ctf_field_class_float_destroy(struct ctf_field_class_float *fc)
44c440bc 525{
5cd6d0e5
PP
526 BT_ASSERT(fc);
527 g_free(fc);
44c440bc
PP
528}
529
530static inline
5cd6d0e5 531void _ctf_field_class_string_destroy(struct ctf_field_class_string *fc)
44c440bc 532{
5cd6d0e5
PP
533 BT_ASSERT(fc);
534 g_free(fc);
44c440bc
PP
535}
536
537static inline
5cd6d0e5 538void _ctf_field_class_struct_destroy(struct ctf_field_class_struct *fc)
44c440bc 539{
5cd6d0e5 540 BT_ASSERT(fc);
44c440bc 541
5cd6d0e5 542 if (fc->members) {
44c440bc
PP
543 uint64_t i;
544
5cd6d0e5
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);
44c440bc 549
5cd6d0e5 550 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
551 }
552
5cd6d0e5 553 g_array_free(fc->members, TRUE);
44c440bc
PP
554 }
555
5cd6d0e5 556 g_free(fc);
44c440bc
PP
557}
558
559static inline
5cd6d0e5 560void _ctf_field_class_array_base_fini(struct ctf_field_class_array_base *fc)
44c440bc 561{
5cd6d0e5
PP
562 BT_ASSERT(fc);
563 ctf_field_class_destroy(fc->elem_fc);
44c440bc
PP
564}
565
566static inline
5cd6d0e5 567void _ctf_field_class_array_destroy(struct ctf_field_class_array *fc)
44c440bc 568{
5cd6d0e5
PP
569 BT_ASSERT(fc);
570 _ctf_field_class_array_base_fini((void *) fc);
571 g_free(fc);
44c440bc
PP
572}
573
574static inline
5cd6d0e5 575void _ctf_field_class_sequence_destroy(struct ctf_field_class_sequence *fc)
44c440bc 576{
5cd6d0e5
PP
577 BT_ASSERT(fc);
578 _ctf_field_class_array_base_fini((void *) fc);
44c440bc 579
5cd6d0e5
PP
580 if (fc->length_ref) {
581 g_string_free(fc->length_ref, TRUE);
44c440bc
PP
582 }
583
5cd6d0e5
PP
584 ctf_field_path_fini(&fc->length_path);
585 g_free(fc);
44c440bc
PP
586}
587
588static inline
5cd6d0e5 589void _ctf_field_class_variant_destroy(struct ctf_field_class_variant *fc)
44c440bc 590{
5cd6d0e5 591 BT_ASSERT(fc);
44c440bc 592
5cd6d0e5 593 if (fc->options) {
44c440bc
PP
594 uint64_t i;
595
5cd6d0e5
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);
44c440bc 600
5cd6d0e5 601 _ctf_named_field_class_fini(named_fc);
44c440bc
PP
602 }
603
5cd6d0e5 604 g_array_free(fc->options, TRUE);
44c440bc
PP
605 }
606
5cd6d0e5
PP
607 if (fc->ranges) {
608 g_array_free(fc->ranges, TRUE);
44c440bc
PP
609 }
610
5cd6d0e5
PP
611 if (fc->tag_ref) {
612 g_string_free(fc->tag_ref, TRUE);
44c440bc
PP
613 }
614
5cd6d0e5
PP
615 ctf_field_path_fini(&fc->tag_path);
616 g_free(fc);
44c440bc
PP
617}
618
619static inline
5cd6d0e5 620void ctf_field_class_destroy(struct ctf_field_class *fc)
44c440bc 621{
5cd6d0e5 622 if (!fc) {
44c440bc
PP
623 return;
624 }
625
864cad70
PP
626 switch (fc->type) {
627 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 628 _ctf_field_class_int_destroy((void *) fc);
44c440bc 629 break;
864cad70 630 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 631 _ctf_field_class_enum_destroy((void *) fc);
44c440bc 632 break;
864cad70 633 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 634 _ctf_field_class_float_destroy((void *) fc);
44c440bc 635 break;
864cad70 636 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 637 _ctf_field_class_string_destroy((void *) fc);
44c440bc 638 break;
864cad70 639 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 640 _ctf_field_class_struct_destroy((void *) fc);
44c440bc 641 break;
864cad70 642 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 643 _ctf_field_class_array_destroy((void *) fc);
44c440bc 644 break;
864cad70 645 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 646 _ctf_field_class_sequence_destroy((void *) fc);
44c440bc 647 break;
864cad70 648 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 649 _ctf_field_class_variant_destroy((void *) fc);
44c440bc
PP
650 break;
651 default:
652 abort();
653 }
654}
655
656static inline
5cd6d0e5 657void ctf_field_class_enum_append_mapping(struct ctf_field_class_enum *fc,
44c440bc
PP
658 const char *label, uint64_t u_lower, uint64_t u_upper)
659{
5cd6d0e5 660 struct ctf_field_class_enum_mapping *mapping;
44c440bc 661
5cd6d0e5 662 BT_ASSERT(fc);
44c440bc 663 BT_ASSERT(label);
5cd6d0e5 664 g_array_set_size(fc->mappings, fc->mappings->len + 1);
44c440bc 665
5cd6d0e5
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);
44c440bc
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
5cd6d0e5
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)
44c440bc 677{
5cd6d0e5
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,
44c440bc
PP
681 index);
682}
683
684static inline
5cd6d0e5
PP
685struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_index(
686 struct ctf_field_class_struct *fc, uint64_t index)
44c440bc 687{
5cd6d0e5
PP
688 BT_ASSERT(fc);
689 BT_ASSERT(index < fc->members->len);
690 return &g_array_index(fc->members, struct ctf_named_field_class,
44c440bc
PP
691 index);
692}
693
694static inline
5cd6d0e5
PP
695struct ctf_named_field_class *ctf_field_class_struct_borrow_member_by_name(
696 struct ctf_field_class_struct *fc, const char *name)
44c440bc
PP
697{
698 uint64_t i;
5cd6d0e5 699 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 700
5cd6d0e5 701 BT_ASSERT(fc);
44c440bc
PP
702 BT_ASSERT(name);
703
5cd6d0e5
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);
44c440bc 707
5cd6d0e5
PP
708 if (strcmp(name, named_fc->name->str) == 0) {
709 ret_named_fc = named_fc;
44c440bc
PP
710 goto end;
711 }
712 }
713
714end:
5cd6d0e5 715 return ret_named_fc;
44c440bc
PP
716}
717
718static inline
5cd6d0e5
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)
44c440bc 721{
5cd6d0e5
PP
722 struct ctf_named_field_class *named_fc = NULL;
723 struct ctf_field_class *fc = NULL;
44c440bc 724
5cd6d0e5 725 if (!struct_fc) {
44c440bc
PP
726 goto end;
727 }
728
5cd6d0e5
PP
729 named_fc = ctf_field_class_struct_borrow_member_by_name(struct_fc, name);
730 if (!named_fc) {
44c440bc
PP
731 goto end;
732 }
733
5cd6d0e5 734 fc = named_fc->fc;
44c440bc
PP
735
736end:
5cd6d0e5 737 return fc;
44c440bc
PP
738}
739
740static inline
5cd6d0e5
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)
44c440bc 744{
5cd6d0e5 745 struct ctf_field_class_int *int_fc = NULL;
44c440bc 746
5cd6d0e5
PP
747 int_fc = (void *)
748 ctf_field_class_struct_borrow_member_field_class_by_name(
749 struct_fc, name);
750 if (!int_fc) {
44c440bc
PP
751 goto end;
752 }
753
864cad70
PP
754 if (int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_INT &&
755 int_fc->base.base.type != CTF_FIELD_CLASS_TYPE_ENUM) {
5cd6d0e5 756 int_fc = NULL;
44c440bc
PP
757 goto end;
758 }
759
760end:
5cd6d0e5 761 return int_fc;
44c440bc
PP
762}
763
764
765static inline
5cd6d0e5
PP
766void ctf_field_class_struct_append_member(struct ctf_field_class_struct *fc,
767 const char *name, struct ctf_field_class *member_fc)
44c440bc 768{
5cd6d0e5 769 struct ctf_named_field_class *named_fc;
44c440bc 770
5cd6d0e5 771 BT_ASSERT(fc);
44c440bc 772 BT_ASSERT(name);
5cd6d0e5 773 g_array_set_size(fc->members, fc->members->len + 1);
44c440bc 774
5cd6d0e5
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;
44c440bc 780
5cd6d0e5
PP
781 if (member_fc->alignment > fc->base.alignment) {
782 fc->base.alignment = member_fc->alignment;
44c440bc
PP
783 }
784}
785
786static inline
5cd6d0e5
PP
787struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_index(
788 struct ctf_field_class_variant *fc, uint64_t index)
44c440bc 789{
5cd6d0e5
PP
790 BT_ASSERT(fc);
791 BT_ASSERT(index < fc->options->len);
792 return &g_array_index(fc->options, struct ctf_named_field_class,
44c440bc
PP
793 index);
794}
795
796static inline
5cd6d0e5
PP
797struct ctf_named_field_class *ctf_field_class_variant_borrow_option_by_name(
798 struct ctf_field_class_variant *fc, const char *name)
44c440bc
PP
799{
800 uint64_t i;
5cd6d0e5 801 struct ctf_named_field_class *ret_named_fc = NULL;
44c440bc 802
5cd6d0e5 803 BT_ASSERT(fc);
44c440bc
PP
804 BT_ASSERT(name);
805
5cd6d0e5
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);
44c440bc 809
5cd6d0e5
PP
810 if (strcmp(name, named_fc->name->str) == 0) {
811 ret_named_fc = named_fc;
44c440bc
PP
812 goto end;
813 }
814 }
815
816end:
5cd6d0e5 817 return ret_named_fc;
44c440bc
PP
818}
819
820static inline
5cd6d0e5
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)
44c440bc 824{
5cd6d0e5
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,
44c440bc
PP
828 index);
829}
830
831static inline
5cd6d0e5
PP
832void ctf_field_class_variant_append_option(struct ctf_field_class_variant *fc,
833 const char *name, struct ctf_field_class *option_fc)
44c440bc 834{
5cd6d0e5 835 struct ctf_named_field_class *named_fc;
44c440bc 836
5cd6d0e5 837 BT_ASSERT(fc);
44c440bc 838 BT_ASSERT(name);
5cd6d0e5 839 g_array_set_size(fc->options, fc->options->len + 1);
44c440bc 840
5cd6d0e5
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;
44c440bc
PP
846}
847
848static inline
5cd6d0e5
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)
44c440bc
PP
852{
853 uint64_t option_i;
854
5cd6d0e5
PP
855 BT_ASSERT(fc);
856 BT_ASSERT(tag_fc);
857 fc->tag_fc = tag_fc;
44c440bc 858
5cd6d0e5 859 for (option_i = 0; option_i < fc->options->len; option_i++) {
44c440bc 860 uint64_t mapping_i;
5cd6d0e5
PP
861 struct ctf_named_field_class *named_fc =
862 ctf_field_class_variant_borrow_option_by_index(
863 fc, option_i);
44c440bc 864
5cd6d0e5 865 for (mapping_i = 0; mapping_i < tag_fc->mappings->len;
44c440bc 866 mapping_i++) {
5cd6d0e5
PP
867 struct ctf_field_class_enum_mapping *mapping =
868 ctf_field_class_enum_borrow_mapping_by_index(
869 tag_fc, mapping_i);
44c440bc 870
5cd6d0e5 871 if (strcmp(named_fc->name->str,
44c440bc 872 mapping->label->str) == 0) {
5cd6d0e5 873 struct ctf_field_class_variant_range range;
44c440bc
PP
874
875 range.range = mapping->range;
876 range.option_index = option_i;
5cd6d0e5 877 g_array_append_val(fc->ranges, range);
44c440bc
PP
878 }
879 }
880 }
881}
882
883static inline
5cd6d0e5
PP
884struct ctf_field_class *ctf_field_class_compound_borrow_field_class_by_index(
885 struct ctf_field_class *comp_fc, uint64_t index)
44c440bc 886{
5cd6d0e5 887 struct ctf_field_class *fc = NULL;
44c440bc 888
864cad70
PP
889 switch (comp_fc->type) {
890 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 891 {
5cd6d0e5
PP
892 struct ctf_named_field_class *named_fc =
893 ctf_field_class_struct_borrow_member_by_index(
894 (void *) comp_fc, index);
44c440bc 895
5cd6d0e5
PP
896 BT_ASSERT(named_fc);
897 fc = named_fc->fc;
44c440bc
PP
898 break;
899 }
864cad70 900 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 901 {
5cd6d0e5
PP
902 struct ctf_named_field_class *named_fc =
903 ctf_field_class_variant_borrow_option_by_index(
904 (void *) comp_fc, index);
44c440bc 905
5cd6d0e5
PP
906 BT_ASSERT(named_fc);
907 fc = named_fc->fc;
44c440bc
PP
908 break;
909 }
864cad70
PP
910 case CTF_FIELD_CLASS_TYPE_ARRAY:
911 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc 912 {
5cd6d0e5 913 struct ctf_field_class_array_base *array_fc = (void *) comp_fc;
44c440bc 914
5cd6d0e5 915 fc = array_fc->elem_fc;
44c440bc
PP
916 break;
917 }
918 default:
919 break;
920 }
921
5cd6d0e5 922 return fc;
44c440bc
PP
923}
924
925static inline
5cd6d0e5 926uint64_t ctf_field_class_compound_get_field_class_count(struct ctf_field_class *fc)
44c440bc
PP
927{
928 uint64_t field_count;
929
864cad70
PP
930 switch (fc->type) {
931 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 932 {
5cd6d0e5 933 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 934
5cd6d0e5 935 field_count = struct_fc->members->len;
44c440bc
PP
936 break;
937 }
864cad70 938 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 939 {
5cd6d0e5 940 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 941
5cd6d0e5 942 field_count = var_fc->options->len;
44c440bc
PP
943 break;
944 }
864cad70
PP
945 case CTF_FIELD_CLASS_TYPE_ARRAY:
946 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44c440bc
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
5cd6d0e5
PP
961int64_t ctf_field_class_compound_get_field_class_index_from_name(
962 struct ctf_field_class *fc, const char *name)
44c440bc
PP
963{
964 int64_t ret_index = -1;
965 uint64_t i;
966
864cad70
PP
967 switch (fc->type) {
968 case CTF_FIELD_CLASS_TYPE_STRUCT:
44c440bc 969 {
5cd6d0e5 970 struct ctf_field_class_struct *struct_fc = (void *) fc;
44c440bc 971
5cd6d0e5
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);
44c440bc 976
5cd6d0e5 977 if (strcmp(name, named_fc->name->str) == 0) {
44c440bc
PP
978 ret_index = (int64_t) i;
979 goto end;
980 }
981 }
982
983 break;
984 }
864cad70 985 case CTF_FIELD_CLASS_TYPE_VARIANT:
44c440bc 986 {
5cd6d0e5 987 struct ctf_field_class_variant *var_fc = (void *) fc;
44c440bc 988
5cd6d0e5
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);
44c440bc 993
5cd6d0e5 994 if (strcmp(name, named_fc->name->str) == 0) {
44c440bc
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
83ebb7f1
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
44c440bc
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
83ebb7f1 1066 g_string_append_printf(str, "[%s", ctf_scope_string(path->root));
44c440bc
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
5cd6d0e5 1080struct ctf_field_class *ctf_field_path_borrow_field_class(
44c440bc
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;
5cd6d0e5 1087 struct ctf_field_class *fc;
44c440bc
PP
1088
1089 switch (field_path->root) {
83ebb7f1 1090 case CTF_SCOPE_PACKET_HEADER:
5cd6d0e5 1091 fc = tc->packet_header_fc;
44c440bc 1092 break;
83ebb7f1 1093 case CTF_SCOPE_PACKET_CONTEXT:
5cd6d0e5 1094 fc = sc->packet_context_fc;
44c440bc 1095 break;
83ebb7f1 1096 case CTF_SCOPE_EVENT_HEADER:
5cd6d0e5 1097 fc = sc->event_header_fc;
44c440bc 1098 break;
83ebb7f1 1099 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
5cd6d0e5 1100 fc = sc->event_common_context_fc;
44c440bc 1101 break;
83ebb7f1 1102 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
5cd6d0e5 1103 fc = ec->spec_context_fc;
44c440bc 1104 break;
83ebb7f1 1105 case CTF_SCOPE_EVENT_PAYLOAD:
5cd6d0e5 1106 fc = ec->payload_fc;
44c440bc
PP
1107 break;
1108 default:
1109 abort();
1110 }
1111
5cd6d0e5 1112 BT_ASSERT(fc);
44c440bc
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);
5cd6d0e5
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;
44c440bc
PP
1122 }
1123
5cd6d0e5
PP
1124 BT_ASSERT(fc);
1125 return fc;
44c440bc
PP
1126}
1127
1128static inline
5cd6d0e5 1129struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc);
44c440bc
PP
1130
1131static inline
5cd6d0e5
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)
44c440bc 1135{
5cd6d0e5
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;
44c440bc
PP
1140}
1141
1142static inline
5cd6d0e5
PP
1143void ctf_field_class_int_copy_content(
1144 struct ctf_field_class_int *dst_fc,
1145 struct ctf_field_class_int *src_fc)
44c440bc 1146{
5cd6d0e5
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;
398454ed 1152 dst_fc->mapped_clock_class = src_fc->mapped_clock_class;
5cd6d0e5 1153 dst_fc->storing_index = src_fc->storing_index;
44c440bc
PP
1154}
1155
1156static inline
5cd6d0e5
PP
1157struct ctf_field_class_int *_ctf_field_class_int_copy(
1158 struct ctf_field_class_int *fc)
44c440bc 1159{
5cd6d0e5 1160 struct ctf_field_class_int *copy_fc = ctf_field_class_int_create();
44c440bc 1161
5cd6d0e5
PP
1162 BT_ASSERT(copy_fc);
1163 ctf_field_class_int_copy_content(copy_fc, fc);
1164 return copy_fc;
44c440bc
PP
1165}
1166
1167static inline
5cd6d0e5
PP
1168struct ctf_field_class_enum *_ctf_field_class_enum_copy(
1169 struct ctf_field_class_enum *fc)
44c440bc 1170{
5cd6d0e5 1171 struct ctf_field_class_enum *copy_fc = ctf_field_class_enum_create();
44c440bc
PP
1172 uint64_t i;
1173
5cd6d0e5
PP
1174 BT_ASSERT(copy_fc);
1175 ctf_field_class_int_copy_content((void *) copy_fc, (void *) fc);
44c440bc 1176
5cd6d0e5
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);
44c440bc 1181
5cd6d0e5 1182 ctf_field_class_enum_append_mapping(copy_fc, mapping->label->str,
44c440bc
PP
1183 mapping->range.lower.u, mapping->range.upper.u);
1184 }
1185
5cd6d0e5 1186 return copy_fc;
44c440bc
PP
1187}
1188
1189static inline
5cd6d0e5
PP
1190struct ctf_field_class_float *_ctf_field_class_float_copy(
1191 struct ctf_field_class_float *fc)
44c440bc 1192{
5cd6d0e5 1193 struct ctf_field_class_float *copy_fc = ctf_field_class_float_create();
44c440bc 1194
5cd6d0e5
PP
1195 BT_ASSERT(copy_fc);
1196 ctf_field_class_bit_array_copy_content((void *) copy_fc, (void *) fc);
1197 return copy_fc;
44c440bc
PP
1198}
1199
1200static inline
5cd6d0e5
PP
1201struct ctf_field_class_string *_ctf_field_class_string_copy(
1202 struct ctf_field_class_string *fc)
44c440bc 1203{
5cd6d0e5 1204 struct ctf_field_class_string *copy_fc = ctf_field_class_string_create();
44c440bc 1205
5cd6d0e5
PP
1206 BT_ASSERT(copy_fc);
1207 return copy_fc;
44c440bc
PP
1208}
1209
1210static inline
5cd6d0e5
PP
1211struct ctf_field_class_struct *_ctf_field_class_struct_copy(
1212 struct ctf_field_class_struct *fc)
44c440bc 1213{
5cd6d0e5 1214 struct ctf_field_class_struct *copy_fc = ctf_field_class_struct_create();
44c440bc
PP
1215 uint64_t i;
1216
5cd6d0e5 1217 BT_ASSERT(copy_fc);
44c440bc 1218
5cd6d0e5
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);
44c440bc 1223
5cd6d0e5
PP
1224 ctf_field_class_struct_append_member(copy_fc,
1225 named_fc->name->str,
1226 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1227 }
1228
5cd6d0e5 1229 return copy_fc;
44c440bc
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
5cd6d0e5
PP
1252struct ctf_field_class_variant *_ctf_field_class_variant_copy(
1253 struct ctf_field_class_variant *fc)
44c440bc 1254{
5cd6d0e5
PP
1255 struct ctf_field_class_variant *copy_fc =
1256 ctf_field_class_variant_create();
44c440bc
PP
1257 uint64_t i;
1258
5cd6d0e5 1259 BT_ASSERT(copy_fc);
44c440bc 1260
5cd6d0e5
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);
44c440bc 1265
5cd6d0e5
PP
1266 ctf_field_class_variant_append_option(copy_fc,
1267 named_fc->name->str,
1268 ctf_field_class_copy(named_fc->fc));
44c440bc
PP
1269 }
1270
5cd6d0e5
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);
44c440bc 1275
5cd6d0e5 1276 g_array_append_val(copy_fc->ranges, *range);
44c440bc
PP
1277 }
1278
5cd6d0e5
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;
44c440bc
PP
1283}
1284
1285static inline
5cd6d0e5
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)
44c440bc 1289{
5cd6d0e5
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;
44c440bc
PP
1294}
1295
1296static inline
5cd6d0e5
PP
1297struct ctf_field_class_array *_ctf_field_class_array_copy(
1298 struct ctf_field_class_array *fc)
44c440bc 1299{
5cd6d0e5 1300 struct ctf_field_class_array *copy_fc = ctf_field_class_array_create();
44c440bc 1301
5cd6d0e5
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;
44c440bc
PP
1306}
1307
1308static inline
5cd6d0e5
PP
1309struct ctf_field_class_sequence *_ctf_field_class_sequence_copy(
1310 struct ctf_field_class_sequence *fc)
44c440bc 1311{
5cd6d0e5
PP
1312 struct ctf_field_class_sequence *copy_fc =
1313 ctf_field_class_sequence_create();
44c440bc 1314
5cd6d0e5
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;
44c440bc
PP
1321}
1322
1323static inline
5cd6d0e5 1324struct ctf_field_class *ctf_field_class_copy(struct ctf_field_class *fc)
44c440bc 1325{
5cd6d0e5 1326 struct ctf_field_class *copy_fc = NULL;
44c440bc 1327
5cd6d0e5 1328 if (!fc) {
44c440bc
PP
1329 goto end;
1330 }
1331
1332 /*
1333 * Translation should not have happened yet.
1334 */
5cd6d0e5 1335 BT_ASSERT(!fc->ir_fc);
44c440bc 1336
864cad70
PP
1337 switch (fc->type) {
1338 case CTF_FIELD_CLASS_TYPE_INT:
5cd6d0e5 1339 copy_fc = (void *) _ctf_field_class_int_copy((void *) fc);
44c440bc 1340 break;
864cad70 1341 case CTF_FIELD_CLASS_TYPE_ENUM:
5cd6d0e5 1342 copy_fc = (void *) _ctf_field_class_enum_copy((void *) fc);
44c440bc 1343 break;
864cad70 1344 case CTF_FIELD_CLASS_TYPE_FLOAT:
5cd6d0e5 1345 copy_fc = (void *) _ctf_field_class_float_copy((void *) fc);
44c440bc 1346 break;
864cad70 1347 case CTF_FIELD_CLASS_TYPE_STRING:
5cd6d0e5 1348 copy_fc = (void *) _ctf_field_class_string_copy((void *) fc);
44c440bc 1349 break;
864cad70 1350 case CTF_FIELD_CLASS_TYPE_STRUCT:
5cd6d0e5 1351 copy_fc = (void *) _ctf_field_class_struct_copy((void *) fc);
44c440bc 1352 break;
864cad70 1353 case CTF_FIELD_CLASS_TYPE_ARRAY:
5cd6d0e5 1354 copy_fc = (void *) _ctf_field_class_array_copy((void *) fc);
44c440bc 1355 break;
864cad70 1356 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
5cd6d0e5 1357 copy_fc = (void *) _ctf_field_class_sequence_copy((void *) fc);
44c440bc 1358 break;
864cad70 1359 case CTF_FIELD_CLASS_TYPE_VARIANT:
5cd6d0e5 1360 copy_fc = (void *) _ctf_field_class_variant_copy((void *) fc);
44c440bc
PP
1361 break;
1362 default:
1363 abort();
1364 }
1365
864cad70 1366 copy_fc->type = fc->type;
5cd6d0e5
PP
1367 copy_fc->alignment = fc->alignment;
1368 copy_fc->in_ir = fc->in_ir;
44c440bc
PP
1369
1370end:
5cd6d0e5 1371 return copy_fc;
44c440bc
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
5cd6d0e5
PP
1403 ctf_field_class_destroy(ec->spec_context_fc);
1404 ctf_field_class_destroy(ec->payload_fc);
44c440bc
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
5cd6d0e5
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);
44c440bc
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(
864cad70 1455 struct ctf_stream_class *sc, uint64_t type)
44c440bc
PP
1456{
1457 BT_ASSERT(sc);
1458 return g_hash_table_lookup(sc->event_classes_by_id,
864cad70 1459 GUINT_TO_POINTER((guint) type));
44c440bc
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
0f2d58c9
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
44c440bc
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);
44c440bc 1524 tc->default_byte_order = -1;
44c440bc 1525 tc->clock_classes = g_ptr_array_new_with_free_func(
0f2d58c9 1526 (GDestroyNotify) ctf_clock_class_destroy);
44c440bc
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
5cd6d0e5 1543 ctf_field_class_destroy(tc->packet_header_fc);
44c440bc
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
0f2d58c9 1617struct ctf_clock_class *ctf_trace_class_borrow_clock_class_by_name(
44c440bc
PP
1618 struct ctf_trace_class *tc, const char *name)
1619{
1620 uint64_t i;
0f2d58c9 1621 struct ctf_clock_class *ret_cc = NULL;
44c440bc
PP
1622
1623 BT_ASSERT(tc);
1624 BT_ASSERT(name);
1625
1626 for (i = 0; i < tc->clock_classes->len; i++) {
0f2d58c9 1627 struct ctf_clock_class *cc = tc->clock_classes->pdata[i];
44c440bc 1628
0f2d58c9
PP
1629 BT_ASSERT(cc->name);
1630 if (strcmp(cc->name->str, name) == 0) {
44c440bc
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.130184 seconds and 4 git commands to generate.