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