lib: split real FC/field into single and double prec FC/field
[babeltrace.git] / src / plugins / ctf / fs-sink / fs-sink-ctf-meta.h
1 #ifndef BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H
2 #define BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H
3
4 /*
5 * Copyright 2018-2019 - 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 <babeltrace2/babeltrace.h>
19 #include "common/common.h"
20 #include "common/assert.h"
21 #include "common/uuid.h"
22 #include <glib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdbool.h>
26 #include <ctype.h>
27
28 enum fs_sink_ctf_field_class_type {
29 FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL,
30 FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY,
31 FS_SINK_CTF_FIELD_CLASS_TYPE_INT,
32 FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT,
33 FS_SINK_CTF_FIELD_CLASS_TYPE_STRING,
34 FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT,
35 FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY,
36 FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE,
37 FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION,
38 FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT,
39 };
40
41 struct fs_sink_ctf_field_class {
42 enum fs_sink_ctf_field_class_type type;
43
44 /* Weak */
45 const bt_field_class *ir_fc;
46
47 unsigned int alignment;
48
49 /* Index of the field class within its own parent */
50 uint64_t index_in_parent;
51 };
52
53 struct fs_sink_ctf_field_class_bit_array {
54 struct fs_sink_ctf_field_class base;
55 unsigned int size;
56 };
57
58 struct fs_sink_ctf_field_class_bool {
59 struct fs_sink_ctf_field_class_bit_array base;
60 };
61
62 struct fs_sink_ctf_field_class_int {
63 struct fs_sink_ctf_field_class_bit_array base;
64 bool is_signed;
65 };
66
67 struct fs_sink_ctf_field_class_float {
68 struct fs_sink_ctf_field_class_bit_array base;
69 };
70
71 struct fs_sink_ctf_field_class_string {
72 struct fs_sink_ctf_field_class base;
73 };
74
75 struct fs_sink_ctf_named_field_class {
76 GString *name;
77
78 /* Owned by this */
79 struct fs_sink_ctf_field_class *fc;
80 };
81
82 struct fs_sink_ctf_field_class_struct {
83 struct fs_sink_ctf_field_class base;
84
85 /* Array of `struct fs_sink_ctf_named_field_class` */
86 GArray *members;
87 };
88
89 struct fs_sink_ctf_field_class_option {
90 struct fs_sink_ctf_field_class base;
91 struct fs_sink_ctf_field_class *content_fc;
92 GString *tag_ref;
93 };
94
95 struct fs_sink_ctf_field_class_variant {
96 struct fs_sink_ctf_field_class base;
97 GString *tag_ref;
98 bool tag_is_before;
99
100 /* Array of `struct fs_sink_ctf_named_field_class` */
101 GArray *options;
102 };
103
104 struct fs_sink_ctf_field_class_array_base {
105 struct fs_sink_ctf_field_class base;
106 struct fs_sink_ctf_field_class *elem_fc;
107 };
108
109 struct fs_sink_ctf_field_class_array {
110 struct fs_sink_ctf_field_class_array_base base;
111 uint64_t length;
112 };
113
114 struct fs_sink_ctf_field_class_sequence {
115 struct fs_sink_ctf_field_class_array_base base;
116 GString *length_ref;
117 bool length_is_before;
118 };
119
120 struct fs_sink_ctf_stream_class;
121
122 struct fs_sink_ctf_event_class {
123 /* Weak */
124 const bt_event_class *ir_ec;
125
126 /* Weak */
127 struct fs_sink_ctf_stream_class *sc;
128
129 /* Owned by this */
130 struct fs_sink_ctf_field_class *spec_context_fc;
131
132 /* Owned by this */
133 struct fs_sink_ctf_field_class *payload_fc;
134 };
135
136 struct fs_sink_ctf_trace;
137
138 struct fs_sink_ctf_stream_class {
139 /* Weak */
140 struct fs_sink_ctf_trace *trace;
141
142 /* Weak */
143 const bt_stream_class *ir_sc;
144
145 /* Weak */
146 const bt_clock_class *default_clock_class;
147
148 GString *default_clock_class_name;
149 bool has_packets;
150 bool packets_have_ts_begin;
151 bool packets_have_ts_end;
152 bool has_discarded_events;
153 bool discarded_events_has_ts;
154 bool discarded_packets_has_ts;
155
156 /* Owned by this */
157 struct fs_sink_ctf_field_class *packet_context_fc;
158
159 /* Owned by this */
160 struct fs_sink_ctf_field_class *event_common_context_fc;
161
162 /* Array of `struct fs_sink_ctf_event_class *` (owned by this) */
163 GPtrArray *event_classes;
164
165 /*
166 * `const bt_event_class *` (weak) ->
167 * `struct fs_sink_ctf_event_class *` (weak)
168 */
169 GHashTable *event_classes_from_ir;
170 };
171
172 struct fs_sink_ctf_trace {
173 /* Weak */
174 const bt_trace *ir_trace;
175
176 /* Weak */
177 const bt_trace_class *ir_tc;
178
179 bt_uuid_t uuid;
180
181 /* Array of `struct fs_sink_ctf_stream_class *` (owned by this) */
182 GPtrArray *stream_classes;
183 };
184
185 static inline
186 void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc);
187
188 static inline
189 void _fs_sink_ctf_field_class_init(struct fs_sink_ctf_field_class *fc,
190 enum fs_sink_ctf_field_class_type type,
191 const bt_field_class *ir_fc, unsigned int alignment,
192 uint64_t index_in_parent)
193 {
194 BT_ASSERT(fc);
195 fc->type = type;
196 fc->ir_fc = ir_fc;
197 fc->alignment = alignment;
198 fc->index_in_parent = index_in_parent;
199 }
200
201 static inline
202 void _fs_sink_ctf_field_class_bit_array_init(
203 struct fs_sink_ctf_field_class_bit_array *fc,
204 enum fs_sink_ctf_field_class_type type,
205 const bt_field_class *ir_fc, unsigned int size,
206 uint64_t index_in_parent)
207 {
208 _fs_sink_ctf_field_class_init((void *) fc, type, ir_fc,
209 size % 8 == 0 ? 8 : 1, index_in_parent);
210 fc->size = size;
211 }
212
213 static inline
214 void _fs_sink_ctf_field_class_int_init(struct fs_sink_ctf_field_class_int *fc,
215 enum fs_sink_ctf_field_class_type type,
216 const bt_field_class *ir_fc, uint64_t index_in_parent)
217 {
218 bt_field_class_type ir_fc_type = bt_field_class_get_type(ir_fc);
219
220 _fs_sink_ctf_field_class_bit_array_init((void *) fc, type, ir_fc,
221 (unsigned int) bt_field_class_integer_get_field_value_range(
222 ir_fc),
223 index_in_parent);
224 fc->is_signed = (ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
225 ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
226 }
227
228 static inline
229 void _fs_sink_ctf_named_field_class_init(
230 struct fs_sink_ctf_named_field_class *named_fc)
231 {
232 BT_ASSERT(named_fc);
233 named_fc->name = g_string_new(NULL);
234 BT_ASSERT(named_fc->name);
235 }
236
237 static inline
238 void _fs_sink_ctf_named_field_class_fini(
239 struct fs_sink_ctf_named_field_class *named_fc)
240 {
241 BT_ASSERT(named_fc);
242
243 if (named_fc->name) {
244 g_string_free(named_fc->name, TRUE);
245 named_fc->name = NULL;
246 }
247
248 fs_sink_ctf_field_class_destroy(named_fc->fc);
249 named_fc->fc = NULL;
250 }
251
252 static inline
253 struct fs_sink_ctf_field_class_bit_array *
254 fs_sink_ctf_field_class_bit_array_create(
255 const bt_field_class *ir_fc, uint64_t index_in_parent)
256 {
257 struct fs_sink_ctf_field_class_bit_array *fc =
258 g_new0(struct fs_sink_ctf_field_class_bit_array, 1);
259
260 BT_ASSERT(fc);
261 _fs_sink_ctf_field_class_bit_array_init((void *) fc,
262 FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY, ir_fc,
263 (unsigned int) bt_field_class_bit_array_get_length(ir_fc),
264 index_in_parent);
265 return fc;
266 }
267
268 static inline
269 struct fs_sink_ctf_field_class_bool *fs_sink_ctf_field_class_bool_create(
270 const bt_field_class *ir_fc, uint64_t index_in_parent)
271 {
272 struct fs_sink_ctf_field_class_bool *fc =
273 g_new0(struct fs_sink_ctf_field_class_bool, 1);
274
275 BT_ASSERT(fc);
276
277 /*
278 * CTF 1.8 has no boolean field class type, so this component
279 * translates it to an 8-bit unsigned integer field class.
280 */
281 _fs_sink_ctf_field_class_bit_array_init((void *) fc,
282 FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL, ir_fc,
283 8, index_in_parent);
284 return fc;
285 }
286
287 static inline
288 struct fs_sink_ctf_field_class_int *fs_sink_ctf_field_class_int_create(
289 const bt_field_class *ir_fc, uint64_t index_in_parent)
290 {
291 struct fs_sink_ctf_field_class_int *fc =
292 g_new0(struct fs_sink_ctf_field_class_int, 1);
293
294 BT_ASSERT(fc);
295 _fs_sink_ctf_field_class_int_init(fc, FS_SINK_CTF_FIELD_CLASS_TYPE_INT,
296 ir_fc, index_in_parent);
297 return fc;
298 }
299
300 static inline
301 struct fs_sink_ctf_field_class_float *fs_sink_ctf_field_class_float_create(
302 const bt_field_class *ir_fc, uint64_t index_in_parent)
303 {
304 struct fs_sink_ctf_field_class_float *fc =
305 g_new0(struct fs_sink_ctf_field_class_float, 1);
306
307 BT_ASSERT(fc);
308 _fs_sink_ctf_field_class_bit_array_init((void *) fc,
309 FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT,
310 ir_fc,
311 bt_field_class_get_type(ir_fc) == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL ? 32 : 64,
312 index_in_parent);
313 return fc;
314 }
315
316 static inline
317 struct fs_sink_ctf_field_class_string *fs_sink_ctf_field_class_string_create(
318 const bt_field_class *ir_fc, uint64_t index_in_parent)
319 {
320 struct fs_sink_ctf_field_class_string *fc =
321 g_new0(struct fs_sink_ctf_field_class_string, 1);
322
323 BT_ASSERT(fc);
324 _fs_sink_ctf_field_class_init((void *) fc,
325 FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, ir_fc,
326 8, index_in_parent);
327 return fc;
328 }
329
330 static inline
331 struct fs_sink_ctf_field_class_struct *fs_sink_ctf_field_class_struct_create_empty(
332 const bt_field_class *ir_fc, uint64_t index_in_parent)
333 {
334 struct fs_sink_ctf_field_class_struct *fc =
335 g_new0(struct fs_sink_ctf_field_class_struct, 1);
336
337 BT_ASSERT(fc);
338 _fs_sink_ctf_field_class_init((void *) fc,
339 FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, ir_fc, 1, index_in_parent);
340 fc->members = g_array_new(FALSE, TRUE,
341 sizeof(struct fs_sink_ctf_named_field_class));
342 BT_ASSERT(fc->members);
343 return fc;
344 }
345
346 static inline
347 struct fs_sink_ctf_field_class_option *fs_sink_ctf_field_class_option_create_empty(
348 const bt_field_class *ir_fc, uint64_t index_in_parent)
349 {
350 struct fs_sink_ctf_field_class_option *fc =
351 g_new0(struct fs_sink_ctf_field_class_option, 1);
352
353 BT_ASSERT(fc);
354 _fs_sink_ctf_field_class_init((void *) fc,
355 FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION, ir_fc,
356 1, index_in_parent);
357 fc->tag_ref = g_string_new(NULL);
358 BT_ASSERT(fc->tag_ref);
359 return fc;
360 }
361
362 static inline
363 struct fs_sink_ctf_field_class_variant *fs_sink_ctf_field_class_variant_create_empty(
364 const bt_field_class *ir_fc, uint64_t index_in_parent)
365 {
366 struct fs_sink_ctf_field_class_variant *fc =
367 g_new0(struct fs_sink_ctf_field_class_variant, 1);
368
369 BT_ASSERT(fc);
370 _fs_sink_ctf_field_class_init((void *) fc,
371 FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, ir_fc,
372 1, index_in_parent);
373 fc->options = g_array_new(FALSE, TRUE,
374 sizeof(struct fs_sink_ctf_named_field_class));
375 BT_ASSERT(fc->options);
376 fc->tag_ref = g_string_new(NULL);
377 BT_ASSERT(fc->tag_ref);
378 fc->tag_is_before =
379 bt_field_class_get_type(fc->base.ir_fc) ==
380 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR;
381 return fc;
382 }
383
384 static inline
385 struct fs_sink_ctf_field_class_array *fs_sink_ctf_field_class_array_create_empty(
386 const bt_field_class *ir_fc, uint64_t index_in_parent)
387 {
388 struct fs_sink_ctf_field_class_array *fc =
389 g_new0(struct fs_sink_ctf_field_class_array, 1);
390
391 BT_ASSERT(fc);
392 _fs_sink_ctf_field_class_init((void *) fc,
393 FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, ir_fc,
394 1, index_in_parent);
395 fc->length = bt_field_class_array_static_get_length(ir_fc);
396 return fc;
397 }
398
399 static inline
400 struct fs_sink_ctf_field_class_sequence *fs_sink_ctf_field_class_sequence_create_empty(
401 const bt_field_class *ir_fc, uint64_t index_in_parent)
402 {
403 struct fs_sink_ctf_field_class_sequence *fc =
404 g_new0(struct fs_sink_ctf_field_class_sequence, 1);
405
406 BT_ASSERT(fc);
407 _fs_sink_ctf_field_class_init((void *) fc,
408 FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE,
409 ir_fc, 1, index_in_parent);
410 fc->length_ref = g_string_new(NULL);
411 BT_ASSERT(fc->length_ref);
412 fc->length_is_before =
413 bt_field_class_array_dynamic_borrow_length_field_path_const(ir_fc) ==
414 NULL;
415 return fc;
416 }
417
418 static inline
419 struct fs_sink_ctf_named_field_class *
420 fs_sink_ctf_field_class_struct_borrow_member_by_index(
421 struct fs_sink_ctf_field_class_struct *fc, uint64_t index);
422
423 static inline
424 struct fs_sink_ctf_named_field_class *
425 fs_sink_ctf_field_class_variant_borrow_option_by_index(
426 struct fs_sink_ctf_field_class_variant *fc, uint64_t index);
427
428 static inline
429 void _fs_sink_ctf_field_class_fini(struct fs_sink_ctf_field_class *fc)
430 {
431 BT_ASSERT(fc);
432 }
433
434 static inline
435 void _fs_sink_ctf_field_class_bit_array_destroy(
436 struct fs_sink_ctf_field_class_int *fc)
437 {
438 BT_ASSERT(fc);
439 _fs_sink_ctf_field_class_fini((void *) fc);
440 g_free(fc);
441 }
442
443 static inline
444 void _fs_sink_ctf_field_class_bool_destroy(
445 struct fs_sink_ctf_field_class_int *fc)
446 {
447 BT_ASSERT(fc);
448 _fs_sink_ctf_field_class_fini((void *) fc);
449 g_free(fc);
450 }
451
452 static inline
453 void _fs_sink_ctf_field_class_int_destroy(
454 struct fs_sink_ctf_field_class_int *fc)
455 {
456 BT_ASSERT(fc);
457 _fs_sink_ctf_field_class_fini((void *) fc);
458 g_free(fc);
459 }
460
461 static inline
462 void _fs_sink_ctf_field_class_float_destroy(
463 struct fs_sink_ctf_field_class_float *fc)
464 {
465 BT_ASSERT(fc);
466 _fs_sink_ctf_field_class_fini((void *) fc);
467 g_free(fc);
468 }
469
470 static inline
471 void _fs_sink_ctf_field_class_string_destroy(
472 struct fs_sink_ctf_field_class_string *fc)
473 {
474 BT_ASSERT(fc);
475 _fs_sink_ctf_field_class_fini((void *) fc);
476 g_free(fc);
477 }
478
479 static inline
480 void _fs_sink_ctf_field_class_struct_destroy(
481 struct fs_sink_ctf_field_class_struct *fc)
482 {
483 BT_ASSERT(fc);
484 _fs_sink_ctf_field_class_fini((void *) fc);
485
486 if (fc->members) {
487 uint64_t i;
488
489 for (i = 0; i < fc->members->len; i++) {
490 struct fs_sink_ctf_named_field_class *named_fc =
491 fs_sink_ctf_field_class_struct_borrow_member_by_index(
492 fc, i);
493
494 _fs_sink_ctf_named_field_class_fini(named_fc);
495 }
496
497 g_array_free(fc->members, TRUE);
498 fc->members = NULL;
499 }
500
501 g_free(fc);
502 }
503
504 static inline
505 void _fs_sink_ctf_field_class_array_base_fini(
506 struct fs_sink_ctf_field_class_array_base *fc)
507 {
508 BT_ASSERT(fc);
509 _fs_sink_ctf_field_class_fini((void *) fc);
510 fs_sink_ctf_field_class_destroy(fc->elem_fc);
511 fc->elem_fc = NULL;
512 }
513
514 static inline
515 void _fs_sink_ctf_field_class_array_destroy(
516 struct fs_sink_ctf_field_class_array *fc)
517 {
518 BT_ASSERT(fc);
519 _fs_sink_ctf_field_class_array_base_fini((void *) fc);
520 g_free(fc);
521 }
522
523 static inline
524 void _fs_sink_ctf_field_class_sequence_destroy(
525 struct fs_sink_ctf_field_class_sequence *fc)
526 {
527 BT_ASSERT(fc);
528 _fs_sink_ctf_field_class_array_base_fini((void *) fc);
529
530 if (fc->length_ref) {
531 g_string_free(fc->length_ref, TRUE);
532 fc->length_ref = NULL;
533 }
534
535 g_free(fc);
536 }
537
538 static inline
539 void _fs_sink_ctf_field_class_option_destroy(
540 struct fs_sink_ctf_field_class_option *fc)
541 {
542 BT_ASSERT(fc);
543 _fs_sink_ctf_field_class_fini((void *) fc);
544 fs_sink_ctf_field_class_destroy(fc->content_fc);
545
546 if (fc->tag_ref) {
547 g_string_free(fc->tag_ref, TRUE);
548 fc->tag_ref = NULL;
549 }
550
551 g_free(fc);
552 }
553
554 static inline
555 void _fs_sink_ctf_field_class_variant_destroy(
556 struct fs_sink_ctf_field_class_variant *fc)
557 {
558 BT_ASSERT(fc);
559 _fs_sink_ctf_field_class_fini((void *) fc);
560
561 if (fc->options) {
562 uint64_t i;
563
564 for (i = 0; i < fc->options->len; i++) {
565 struct fs_sink_ctf_named_field_class *named_fc =
566 fs_sink_ctf_field_class_variant_borrow_option_by_index(
567 fc, i);
568
569 _fs_sink_ctf_named_field_class_fini(named_fc);
570 }
571
572 g_array_free(fc->options, TRUE);
573 fc->options = NULL;
574 }
575
576 if (fc->tag_ref) {
577 g_string_free(fc->tag_ref, TRUE);
578 fc->tag_ref = NULL;
579 }
580
581 g_free(fc);
582 }
583
584 static inline
585 void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc)
586 {
587 if (!fc) {
588 return;
589 }
590
591 switch (fc->type) {
592 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL:
593 _fs_sink_ctf_field_class_bool_destroy((void *) fc);
594 break;
595 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY:
596 _fs_sink_ctf_field_class_bit_array_destroy((void *) fc);
597 break;
598 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT:
599 _fs_sink_ctf_field_class_int_destroy((void *) fc);
600 break;
601 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT:
602 _fs_sink_ctf_field_class_float_destroy((void *) fc);
603 break;
604 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING:
605 _fs_sink_ctf_field_class_string_destroy((void *) fc);
606 break;
607 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT:
608 _fs_sink_ctf_field_class_struct_destroy((void *) fc);
609 break;
610 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY:
611 _fs_sink_ctf_field_class_array_destroy((void *) fc);
612 break;
613 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE:
614 _fs_sink_ctf_field_class_sequence_destroy((void *) fc);
615 break;
616 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION:
617 _fs_sink_ctf_field_class_option_destroy((void *) fc);
618 break;
619 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT:
620 _fs_sink_ctf_field_class_variant_destroy((void *) fc);
621 break;
622 default:
623 abort();
624 }
625 }
626
627 static inline
628 struct fs_sink_ctf_named_field_class *
629 fs_sink_ctf_field_class_struct_borrow_member_by_index(
630 struct fs_sink_ctf_field_class_struct *fc, uint64_t index)
631 {
632 BT_ASSERT(fc);
633 BT_ASSERT(index < fc->members->len);
634 return &g_array_index(fc->members, struct fs_sink_ctf_named_field_class,
635 index);
636 }
637
638 static inline
639 struct fs_sink_ctf_named_field_class *
640 fs_sink_ctf_field_class_struct_borrow_member_by_name(
641 struct fs_sink_ctf_field_class_struct *fc, const char *name)
642 {
643 uint64_t i;
644 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
645
646 BT_ASSERT(fc);
647 BT_ASSERT(name);
648
649 for (i = 0; i < fc->members->len; i++) {
650 struct fs_sink_ctf_named_field_class *named_fc =
651 fs_sink_ctf_field_class_struct_borrow_member_by_index(
652 fc, i);
653
654 if (strcmp(name, named_fc->name->str) == 0) {
655 ret_named_fc = named_fc;
656 goto end;
657 }
658 }
659
660 end:
661 return ret_named_fc;
662 }
663
664 static inline
665 struct fs_sink_ctf_field_class *
666 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(
667 struct fs_sink_ctf_field_class_struct *struct_fc, const char *name)
668 {
669 struct fs_sink_ctf_named_field_class *named_fc = NULL;
670 struct fs_sink_ctf_field_class *fc = NULL;
671
672 if (!struct_fc) {
673 goto end;
674 }
675
676 named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_name(
677 struct_fc, name);
678 if (!named_fc) {
679 goto end;
680 }
681
682 fc = named_fc->fc;
683
684 end:
685 return fc;
686 }
687
688 static inline
689 struct fs_sink_ctf_field_class_int *
690 fs_sink_ctf_field_class_struct_borrow_member_int_field_class_by_name(
691 struct fs_sink_ctf_field_class_struct *struct_fc,
692 const char *name)
693 {
694 struct fs_sink_ctf_field_class_int *int_fc = NULL;
695
696 int_fc = (void *)
697 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(
698 struct_fc, name);
699 if (!int_fc) {
700 goto end;
701 }
702
703 if (int_fc->base.base.type != FS_SINK_CTF_FIELD_CLASS_TYPE_INT) {
704 int_fc = NULL;
705 goto end;
706 }
707
708 end:
709 return int_fc;
710 }
711
712 static inline
713 void fs_sink_ctf_field_class_struct_align_at_least(
714 struct fs_sink_ctf_field_class_struct *fc,
715 unsigned int alignment)
716 {
717 if (alignment > fc->base.alignment) {
718 fc->base.alignment = alignment;
719 }
720 }
721
722 static inline
723 void fs_sink_ctf_field_class_struct_append_member(
724 struct fs_sink_ctf_field_class_struct *fc,
725 const char *name, struct fs_sink_ctf_field_class *member_fc)
726 {
727 struct fs_sink_ctf_named_field_class *named_fc;
728
729 BT_ASSERT(fc);
730 BT_ASSERT(name);
731 g_array_set_size(fc->members, fc->members->len + 1);
732
733 named_fc = &g_array_index(fc->members,
734 struct fs_sink_ctf_named_field_class, fc->members->len - 1);
735 _fs_sink_ctf_named_field_class_init(named_fc);
736 g_string_assign(named_fc->name, name);
737 named_fc->fc = member_fc;
738 fs_sink_ctf_field_class_struct_align_at_least(fc, member_fc->alignment);
739 }
740
741 static inline
742 struct fs_sink_ctf_named_field_class *
743 fs_sink_ctf_field_class_variant_borrow_option_by_index(
744 struct fs_sink_ctf_field_class_variant *fc, uint64_t index)
745 {
746 BT_ASSERT(fc);
747 BT_ASSERT(index < fc->options->len);
748 return &g_array_index(fc->options, struct fs_sink_ctf_named_field_class,
749 index);
750 }
751
752 static inline
753 struct fs_sink_ctf_named_field_class *
754 fs_sink_ctf_field_class_variant_borrow_option_by_name(
755 struct fs_sink_ctf_field_class_variant *fc, const char *name)
756 {
757 uint64_t i;
758 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
759
760 BT_ASSERT(fc);
761 BT_ASSERT(name);
762
763 for (i = 0; i < fc->options->len; i++) {
764 struct fs_sink_ctf_named_field_class *named_fc =
765 fs_sink_ctf_field_class_variant_borrow_option_by_index(
766 fc, i);
767
768 if (strcmp(name, named_fc->name->str) == 0) {
769 ret_named_fc = named_fc;
770 goto end;
771 }
772 }
773
774 end:
775 return ret_named_fc;
776 }
777
778 static inline
779 void fs_sink_ctf_field_class_variant_append_option(
780 struct fs_sink_ctf_field_class_variant *fc,
781 const char *name, struct fs_sink_ctf_field_class *option_fc)
782 {
783 struct fs_sink_ctf_named_field_class *named_fc;
784
785 BT_ASSERT(fc);
786 BT_ASSERT(name);
787 g_array_set_size(fc->options, fc->options->len + 1);
788
789 named_fc = &g_array_index(fc->options,
790 struct fs_sink_ctf_named_field_class, fc->options->len - 1);
791 _fs_sink_ctf_named_field_class_init(named_fc);
792 g_string_assign(named_fc->name, name);
793 named_fc->fc = option_fc;
794 }
795
796 static inline
797 struct fs_sink_ctf_event_class *fs_sink_ctf_event_class_create(
798 struct fs_sink_ctf_stream_class *sc,
799 const bt_event_class *ir_ec)
800 {
801 struct fs_sink_ctf_event_class *ec =
802 g_new0(struct fs_sink_ctf_event_class, 1);
803
804 BT_ASSERT(sc);
805 BT_ASSERT(ir_ec);
806 BT_ASSERT(ec);
807 ec->ir_ec = ir_ec;
808 ec->sc = sc;
809 g_ptr_array_add(sc->event_classes, ec);
810 g_hash_table_insert(sc->event_classes_from_ir, (gpointer) ir_ec, ec);
811 return ec;
812 }
813
814 static inline
815 void fs_sink_ctf_event_class_destroy(struct fs_sink_ctf_event_class *ec)
816 {
817 if (!ec) {
818 return;
819 }
820
821 fs_sink_ctf_field_class_destroy(ec->spec_context_fc);
822 ec->spec_context_fc = NULL;
823 fs_sink_ctf_field_class_destroy(ec->payload_fc);
824 ec->payload_fc = NULL;
825 g_free(ec);
826 }
827
828 static inline
829 struct fs_sink_ctf_stream_class *fs_sink_ctf_stream_class_create(
830 struct fs_sink_ctf_trace *trace,
831 const bt_stream_class *ir_sc)
832 {
833 struct fs_sink_ctf_stream_class *sc =
834 g_new0(struct fs_sink_ctf_stream_class, 1);
835
836 BT_ASSERT(trace);
837 BT_ASSERT(ir_sc);
838 BT_ASSERT(sc);
839 sc->trace = trace;
840 sc->ir_sc = ir_sc;
841 sc->default_clock_class =
842 bt_stream_class_borrow_default_clock_class_const(ir_sc);
843 sc->default_clock_class_name = g_string_new(NULL);
844 BT_ASSERT(sc->default_clock_class_name);
845 sc->event_classes = g_ptr_array_new_with_free_func(
846 (GDestroyNotify) fs_sink_ctf_event_class_destroy);
847 BT_ASSERT(sc->event_classes);
848 sc->event_classes_from_ir = g_hash_table_new(g_direct_hash,
849 g_direct_equal);
850 BT_ASSERT(sc->event_classes_from_ir);
851 sc->has_packets = bt_stream_class_supports_packets(ir_sc);
852 sc->packets_have_ts_begin =
853 bt_stream_class_packets_have_beginning_default_clock_snapshot(
854 ir_sc);
855 sc->packets_have_ts_end =
856 bt_stream_class_packets_have_end_default_clock_snapshot(ir_sc);
857 sc->has_discarded_events =
858 bt_stream_class_supports_discarded_events(ir_sc);
859
860 if (sc->has_discarded_events) {
861 sc->discarded_events_has_ts =
862 bt_stream_class_discarded_events_have_default_clock_snapshots(
863 ir_sc);
864 }
865
866 if (bt_stream_class_supports_discarded_packets(ir_sc)) {
867 sc->discarded_packets_has_ts =
868 bt_stream_class_discarded_packets_have_default_clock_snapshots(
869 ir_sc);
870 }
871
872 g_ptr_array_add(trace->stream_classes, sc);
873 return sc;
874 }
875
876 static inline
877 void fs_sink_ctf_stream_class_destroy(struct fs_sink_ctf_stream_class *sc)
878 {
879 if (!sc) {
880 return;
881 }
882
883 if (sc->default_clock_class_name) {
884 g_string_free(sc->default_clock_class_name, TRUE);
885 sc->default_clock_class_name = NULL;
886 }
887
888 if (sc->event_classes) {
889 g_ptr_array_free(sc->event_classes, TRUE);
890 sc->event_classes = NULL;
891 }
892
893 if (sc->event_classes_from_ir) {
894 g_hash_table_destroy(sc->event_classes_from_ir);
895 sc->event_classes_from_ir = NULL;
896 }
897
898 fs_sink_ctf_field_class_destroy(sc->packet_context_fc);
899 sc->packet_context_fc = NULL;
900 fs_sink_ctf_field_class_destroy(sc->event_common_context_fc);
901 sc->event_common_context_fc = NULL;
902 g_free(sc);
903 }
904
905 static inline
906 void fs_sink_ctf_stream_class_append_event_class(
907 struct fs_sink_ctf_stream_class *sc,
908 struct fs_sink_ctf_event_class *ec)
909 {
910 g_ptr_array_add(sc->event_classes, ec);
911 }
912
913 static inline
914 void fs_sink_ctf_trace_destroy(struct fs_sink_ctf_trace *trace)
915 {
916 if (!trace) {
917 return;
918 }
919
920 if (trace->stream_classes) {
921 g_ptr_array_free(trace->stream_classes, TRUE);
922 trace->stream_classes = NULL;
923 }
924
925 g_free(trace);
926 }
927
928 static inline
929 struct fs_sink_ctf_trace *fs_sink_ctf_trace_create(const bt_trace *ir_trace)
930 {
931 struct fs_sink_ctf_trace *trace =
932 g_new0(struct fs_sink_ctf_trace, 1);
933
934 BT_ASSERT(trace);
935
936 bt_uuid_generate(trace->uuid);
937
938 trace->ir_trace = ir_trace;
939 trace->ir_tc = bt_trace_borrow_class_const(ir_trace);
940 trace->stream_classes = g_ptr_array_new_with_free_func(
941 (GDestroyNotify) fs_sink_ctf_stream_class_destroy);
942 BT_ASSERT(trace->stream_classes);
943
944 return trace;
945 }
946
947 #endif /* BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H */
This page took 0.04823 seconds and 4 git commands to generate.