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