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