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