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