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