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