sink.text.details: use write_none_prop_value() for empty array 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_VARIANT,
37 };
38
39 struct fs_sink_ctf_field_class {
40 enum fs_sink_ctf_field_class_type type;
41
42 /* Weak */
43 const bt_field_class *ir_fc;
44
45 unsigned int alignment;
46
47 /* Index of the field class within its own parent */
48 uint64_t index_in_parent;
49 };
50
51 struct fs_sink_ctf_field_class_bit_array {
52 struct fs_sink_ctf_field_class base;
53 unsigned int size;
54 };
55
56 struct fs_sink_ctf_field_class_bool {
57 struct fs_sink_ctf_field_class_bit_array base;
58 };
59
60 struct fs_sink_ctf_field_class_int {
61 struct fs_sink_ctf_field_class_bit_array base;
62 bool is_signed;
63 };
64
65 struct fs_sink_ctf_field_class_float {
66 struct fs_sink_ctf_field_class_bit_array base;
67 };
68
69 struct fs_sink_ctf_field_class_string {
70 struct fs_sink_ctf_field_class base;
71 };
72
73 struct fs_sink_ctf_named_field_class {
74 GString *name;
75
76 /* Owned by this */
77 struct fs_sink_ctf_field_class *fc;
78 };
79
80 struct fs_sink_ctf_field_class_struct {
81 struct fs_sink_ctf_field_class base;
82
83 /* Array of `struct fs_sink_ctf_named_field_class` */
84 GArray *members;
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 = (ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
217 ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
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_bool *fs_sink_ctf_field_class_bool_create(
246 const bt_field_class *ir_fc, uint64_t index_in_parent)
247 {
248 struct fs_sink_ctf_field_class_bool *fc =
249 g_new0(struct fs_sink_ctf_field_class_bool, 1);
250
251 BT_ASSERT(fc);
252
253 /*
254 * CTF 1.8 has no boolean field class type, so this component
255 * translates it to an 8-bit unsigned integer field class.
256 */
257 _fs_sink_ctf_field_class_bit_array_init((void *) fc,
258 FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL, ir_fc,
259 8, index_in_parent);
260 return fc;
261 }
262
263 static inline
264 struct fs_sink_ctf_field_class_int *fs_sink_ctf_field_class_int_create(
265 const bt_field_class *ir_fc, uint64_t index_in_parent)
266 {
267 struct fs_sink_ctf_field_class_int *fc =
268 g_new0(struct fs_sink_ctf_field_class_int, 1);
269
270 BT_ASSERT(fc);
271 _fs_sink_ctf_field_class_int_init(fc, FS_SINK_CTF_FIELD_CLASS_TYPE_INT,
272 ir_fc, index_in_parent);
273 return fc;
274 }
275
276 static inline
277 struct fs_sink_ctf_field_class_float *fs_sink_ctf_field_class_float_create(
278 const bt_field_class *ir_fc, uint64_t index_in_parent)
279 {
280 struct fs_sink_ctf_field_class_float *fc =
281 g_new0(struct fs_sink_ctf_field_class_float, 1);
282
283 BT_ASSERT(fc);
284 _fs_sink_ctf_field_class_bit_array_init((void *) fc,
285 FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT,
286 ir_fc, bt_field_class_real_is_single_precision(ir_fc) ? 32 : 64,
287 index_in_parent);
288 return fc;
289 }
290
291 static inline
292 struct fs_sink_ctf_field_class_string *fs_sink_ctf_field_class_string_create(
293 const bt_field_class *ir_fc, uint64_t index_in_parent)
294 {
295 struct fs_sink_ctf_field_class_string *fc =
296 g_new0(struct fs_sink_ctf_field_class_string, 1);
297
298 BT_ASSERT(fc);
299 _fs_sink_ctf_field_class_init((void *) fc,
300 FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, ir_fc,
301 8, index_in_parent);
302 return fc;
303 }
304
305 static inline
306 struct fs_sink_ctf_field_class_struct *fs_sink_ctf_field_class_struct_create_empty(
307 const bt_field_class *ir_fc, uint64_t index_in_parent)
308 {
309 struct fs_sink_ctf_field_class_struct *fc =
310 g_new0(struct fs_sink_ctf_field_class_struct, 1);
311
312 BT_ASSERT(fc);
313 _fs_sink_ctf_field_class_init((void *) fc,
314 FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, ir_fc, 1, index_in_parent);
315 fc->members = g_array_new(FALSE, TRUE,
316 sizeof(struct fs_sink_ctf_named_field_class));
317 BT_ASSERT(fc->members);
318 return fc;
319 }
320
321 static inline
322 struct fs_sink_ctf_field_class_variant *fs_sink_ctf_field_class_variant_create_empty(
323 const bt_field_class *ir_fc, uint64_t index_in_parent)
324 {
325 struct fs_sink_ctf_field_class_variant *fc =
326 g_new0(struct fs_sink_ctf_field_class_variant, 1);
327
328 BT_ASSERT(fc);
329 _fs_sink_ctf_field_class_init((void *) fc,
330 FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, ir_fc,
331 1, index_in_parent);
332 fc->options = g_array_new(FALSE, TRUE,
333 sizeof(struct fs_sink_ctf_named_field_class));
334 BT_ASSERT(fc->options);
335 fc->tag_ref = g_string_new(NULL);
336 BT_ASSERT(fc->tag_ref);
337 fc->tag_is_before =
338 bt_field_class_get_type(fc->base.ir_fc) ==
339 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR;
340 return fc;
341 }
342
343 static inline
344 struct fs_sink_ctf_field_class_array *fs_sink_ctf_field_class_array_create_empty(
345 const bt_field_class *ir_fc, uint64_t index_in_parent)
346 {
347 struct fs_sink_ctf_field_class_array *fc =
348 g_new0(struct fs_sink_ctf_field_class_array, 1);
349
350 BT_ASSERT(fc);
351 _fs_sink_ctf_field_class_init((void *) fc,
352 FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, ir_fc,
353 1, index_in_parent);
354 fc->length = bt_field_class_array_static_get_length(ir_fc);
355 return fc;
356 }
357
358 static inline
359 struct fs_sink_ctf_field_class_sequence *fs_sink_ctf_field_class_sequence_create_empty(
360 const bt_field_class *ir_fc, uint64_t index_in_parent)
361 {
362 struct fs_sink_ctf_field_class_sequence *fc =
363 g_new0(struct fs_sink_ctf_field_class_sequence, 1);
364
365 BT_ASSERT(fc);
366 _fs_sink_ctf_field_class_init((void *) fc,
367 FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE,
368 ir_fc, 1, index_in_parent);
369 fc->length_ref = g_string_new(NULL);
370 BT_ASSERT(fc->length_ref);
371 fc->length_is_before =
372 bt_field_class_array_dynamic_borrow_length_field_path_const(ir_fc) ==
373 NULL;
374 return fc;
375 }
376
377 static inline
378 struct fs_sink_ctf_named_field_class *
379 fs_sink_ctf_field_class_struct_borrow_member_by_index(
380 struct fs_sink_ctf_field_class_struct *fc, uint64_t index);
381
382 static inline
383 struct fs_sink_ctf_named_field_class *
384 fs_sink_ctf_field_class_variant_borrow_option_by_index(
385 struct fs_sink_ctf_field_class_variant *fc, uint64_t index);
386
387 static inline
388 void _fs_sink_ctf_field_class_fini(struct fs_sink_ctf_field_class *fc)
389 {
390 BT_ASSERT(fc);
391 }
392
393 static inline
394 void _fs_sink_ctf_field_class_bool_destroy(
395 struct fs_sink_ctf_field_class_int *fc)
396 {
397 BT_ASSERT(fc);
398 _fs_sink_ctf_field_class_fini((void *) fc);
399 g_free(fc);
400 }
401
402 static inline
403 void _fs_sink_ctf_field_class_int_destroy(
404 struct fs_sink_ctf_field_class_int *fc)
405 {
406 BT_ASSERT(fc);
407 _fs_sink_ctf_field_class_fini((void *) fc);
408 g_free(fc);
409 }
410
411 static inline
412 void _fs_sink_ctf_field_class_float_destroy(
413 struct fs_sink_ctf_field_class_float *fc)
414 {
415 BT_ASSERT(fc);
416 _fs_sink_ctf_field_class_fini((void *) fc);
417 g_free(fc);
418 }
419
420 static inline
421 void _fs_sink_ctf_field_class_string_destroy(
422 struct fs_sink_ctf_field_class_string *fc)
423 {
424 BT_ASSERT(fc);
425 _fs_sink_ctf_field_class_fini((void *) fc);
426 g_free(fc);
427 }
428
429 static inline
430 void _fs_sink_ctf_field_class_struct_destroy(
431 struct fs_sink_ctf_field_class_struct *fc)
432 {
433 BT_ASSERT(fc);
434 _fs_sink_ctf_field_class_fini((void *) fc);
435
436 if (fc->members) {
437 uint64_t i;
438
439 for (i = 0; i < fc->members->len; i++) {
440 struct fs_sink_ctf_named_field_class *named_fc =
441 fs_sink_ctf_field_class_struct_borrow_member_by_index(
442 fc, i);
443
444 _fs_sink_ctf_named_field_class_fini(named_fc);
445 }
446
447 g_array_free(fc->members, TRUE);
448 fc->members = NULL;
449 }
450
451 g_free(fc);
452 }
453
454 static inline
455 void _fs_sink_ctf_field_class_array_base_fini(
456 struct fs_sink_ctf_field_class_array_base *fc)
457 {
458 BT_ASSERT(fc);
459 _fs_sink_ctf_field_class_fini((void *) fc);
460 fs_sink_ctf_field_class_destroy(fc->elem_fc);
461 fc->elem_fc = NULL;
462 }
463
464 static inline
465 void _fs_sink_ctf_field_class_array_destroy(
466 struct fs_sink_ctf_field_class_array *fc)
467 {
468 BT_ASSERT(fc);
469 _fs_sink_ctf_field_class_array_base_fini((void *) fc);
470 g_free(fc);
471 }
472
473 static inline
474 void _fs_sink_ctf_field_class_sequence_destroy(
475 struct fs_sink_ctf_field_class_sequence *fc)
476 {
477 BT_ASSERT(fc);
478 _fs_sink_ctf_field_class_array_base_fini((void *) fc);
479
480 if (fc->length_ref) {
481 g_string_free(fc->length_ref, TRUE);
482 fc->length_ref = NULL;
483 }
484
485 g_free(fc);
486 }
487
488 static inline
489 void _fs_sink_ctf_field_class_variant_destroy(
490 struct fs_sink_ctf_field_class_variant *fc)
491 {
492 BT_ASSERT(fc);
493 _fs_sink_ctf_field_class_fini((void *) fc);
494
495 if (fc->options) {
496 uint64_t i;
497
498 for (i = 0; i < fc->options->len; i++) {
499 struct fs_sink_ctf_named_field_class *named_fc =
500 fs_sink_ctf_field_class_variant_borrow_option_by_index(
501 fc, i);
502
503 _fs_sink_ctf_named_field_class_fini(named_fc);
504 }
505
506 g_array_free(fc->options, TRUE);
507 fc->options = NULL;
508 }
509
510 if (fc->tag_ref) {
511 g_string_free(fc->tag_ref, TRUE);
512 fc->tag_ref = NULL;
513 }
514
515 g_free(fc);
516 }
517
518 static inline
519 void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc)
520 {
521 if (!fc) {
522 return;
523 }
524
525 switch (fc->type) {
526 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL:
527 _fs_sink_ctf_field_class_bool_destroy((void *) fc);
528 break;
529 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT:
530 _fs_sink_ctf_field_class_int_destroy((void *) fc);
531 break;
532 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT:
533 _fs_sink_ctf_field_class_float_destroy((void *) fc);
534 break;
535 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING:
536 _fs_sink_ctf_field_class_string_destroy((void *) fc);
537 break;
538 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT:
539 _fs_sink_ctf_field_class_struct_destroy((void *) fc);
540 break;
541 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY:
542 _fs_sink_ctf_field_class_array_destroy((void *) fc);
543 break;
544 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE:
545 _fs_sink_ctf_field_class_sequence_destroy((void *) fc);
546 break;
547 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT:
548 _fs_sink_ctf_field_class_variant_destroy((void *) fc);
549 break;
550 default:
551 abort();
552 }
553 }
554
555 static inline
556 struct fs_sink_ctf_named_field_class *
557 fs_sink_ctf_field_class_struct_borrow_member_by_index(
558 struct fs_sink_ctf_field_class_struct *fc, uint64_t index)
559 {
560 BT_ASSERT(fc);
561 BT_ASSERT(index < fc->members->len);
562 return &g_array_index(fc->members, struct fs_sink_ctf_named_field_class,
563 index);
564 }
565
566 static inline
567 struct fs_sink_ctf_named_field_class *
568 fs_sink_ctf_field_class_struct_borrow_member_by_name(
569 struct fs_sink_ctf_field_class_struct *fc, const char *name)
570 {
571 uint64_t i;
572 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
573
574 BT_ASSERT(fc);
575 BT_ASSERT(name);
576
577 for (i = 0; i < fc->members->len; i++) {
578 struct fs_sink_ctf_named_field_class *named_fc =
579 fs_sink_ctf_field_class_struct_borrow_member_by_index(
580 fc, i);
581
582 if (strcmp(name, named_fc->name->str) == 0) {
583 ret_named_fc = named_fc;
584 goto end;
585 }
586 }
587
588 end:
589 return ret_named_fc;
590 }
591
592 static inline
593 struct fs_sink_ctf_field_class *
594 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(
595 struct fs_sink_ctf_field_class_struct *struct_fc, const char *name)
596 {
597 struct fs_sink_ctf_named_field_class *named_fc = NULL;
598 struct fs_sink_ctf_field_class *fc = NULL;
599
600 if (!struct_fc) {
601 goto end;
602 }
603
604 named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_name(
605 struct_fc, name);
606 if (!named_fc) {
607 goto end;
608 }
609
610 fc = named_fc->fc;
611
612 end:
613 return fc;
614 }
615
616 static inline
617 struct fs_sink_ctf_field_class_int *
618 fs_sink_ctf_field_class_struct_borrow_member_int_field_class_by_name(
619 struct fs_sink_ctf_field_class_struct *struct_fc,
620 const char *name)
621 {
622 struct fs_sink_ctf_field_class_int *int_fc = NULL;
623
624 int_fc = (void *)
625 fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name(
626 struct_fc, name);
627 if (!int_fc) {
628 goto end;
629 }
630
631 if (int_fc->base.base.type != FS_SINK_CTF_FIELD_CLASS_TYPE_INT) {
632 int_fc = NULL;
633 goto end;
634 }
635
636 end:
637 return int_fc;
638 }
639
640 static inline
641 void fs_sink_ctf_field_class_struct_align_at_least(
642 struct fs_sink_ctf_field_class_struct *fc,
643 unsigned int alignment)
644 {
645 if (alignment > fc->base.alignment) {
646 fc->base.alignment = alignment;
647 }
648 }
649
650 static inline
651 void fs_sink_ctf_field_class_struct_append_member(
652 struct fs_sink_ctf_field_class_struct *fc,
653 const char *name, struct fs_sink_ctf_field_class *member_fc)
654 {
655 struct fs_sink_ctf_named_field_class *named_fc;
656
657 BT_ASSERT(fc);
658 BT_ASSERT(name);
659 g_array_set_size(fc->members, fc->members->len + 1);
660
661 named_fc = &g_array_index(fc->members,
662 struct fs_sink_ctf_named_field_class, fc->members->len - 1);
663 _fs_sink_ctf_named_field_class_init(named_fc);
664 g_string_assign(named_fc->name, name);
665 named_fc->fc = member_fc;
666 fs_sink_ctf_field_class_struct_align_at_least(fc, member_fc->alignment);
667 }
668
669 static inline
670 struct fs_sink_ctf_named_field_class *
671 fs_sink_ctf_field_class_variant_borrow_option_by_index(
672 struct fs_sink_ctf_field_class_variant *fc, uint64_t index)
673 {
674 BT_ASSERT(fc);
675 BT_ASSERT(index < fc->options->len);
676 return &g_array_index(fc->options, struct fs_sink_ctf_named_field_class,
677 index);
678 }
679
680 static inline
681 struct fs_sink_ctf_named_field_class *
682 fs_sink_ctf_field_class_variant_borrow_option_by_name(
683 struct fs_sink_ctf_field_class_variant *fc, const char *name)
684 {
685 uint64_t i;
686 struct fs_sink_ctf_named_field_class *ret_named_fc = NULL;
687
688 BT_ASSERT(fc);
689 BT_ASSERT(name);
690
691 for (i = 0; i < fc->options->len; i++) {
692 struct fs_sink_ctf_named_field_class *named_fc =
693 fs_sink_ctf_field_class_variant_borrow_option_by_index(
694 fc, i);
695
696 if (strcmp(name, named_fc->name->str) == 0) {
697 ret_named_fc = named_fc;
698 goto end;
699 }
700 }
701
702 end:
703 return ret_named_fc;
704 }
705
706 static inline
707 void fs_sink_ctf_field_class_variant_append_option(
708 struct fs_sink_ctf_field_class_variant *fc,
709 const char *name, struct fs_sink_ctf_field_class *option_fc)
710 {
711 struct fs_sink_ctf_named_field_class *named_fc;
712
713 BT_ASSERT(fc);
714 BT_ASSERT(name);
715 g_array_set_size(fc->options, fc->options->len + 1);
716
717 named_fc = &g_array_index(fc->options,
718 struct fs_sink_ctf_named_field_class, fc->options->len - 1);
719 _fs_sink_ctf_named_field_class_init(named_fc);
720 g_string_assign(named_fc->name, name);
721 named_fc->fc = option_fc;
722 }
723
724 static inline
725 struct fs_sink_ctf_event_class *fs_sink_ctf_event_class_create(
726 struct fs_sink_ctf_stream_class *sc,
727 const bt_event_class *ir_ec)
728 {
729 struct fs_sink_ctf_event_class *ec =
730 g_new0(struct fs_sink_ctf_event_class, 1);
731
732 BT_ASSERT(sc);
733 BT_ASSERT(ir_ec);
734 BT_ASSERT(ec);
735 ec->ir_ec = ir_ec;
736 ec->sc = sc;
737 g_ptr_array_add(sc->event_classes, ec);
738 g_hash_table_insert(sc->event_classes_from_ir, (gpointer) ir_ec, ec);
739 return ec;
740 }
741
742 static inline
743 void fs_sink_ctf_event_class_destroy(struct fs_sink_ctf_event_class *ec)
744 {
745 if (!ec) {
746 return;
747 }
748
749 fs_sink_ctf_field_class_destroy(ec->spec_context_fc);
750 ec->spec_context_fc = NULL;
751 fs_sink_ctf_field_class_destroy(ec->payload_fc);
752 ec->payload_fc = NULL;
753 g_free(ec);
754 }
755
756 static inline
757 struct fs_sink_ctf_stream_class *fs_sink_ctf_stream_class_create(
758 struct fs_sink_ctf_trace *trace,
759 const bt_stream_class *ir_sc)
760 {
761 struct fs_sink_ctf_stream_class *sc =
762 g_new0(struct fs_sink_ctf_stream_class, 1);
763
764 BT_ASSERT(trace);
765 BT_ASSERT(ir_sc);
766 BT_ASSERT(sc);
767 sc->trace = trace;
768 sc->ir_sc = ir_sc;
769 sc->default_clock_class =
770 bt_stream_class_borrow_default_clock_class_const(ir_sc);
771 sc->default_clock_class_name = g_string_new(NULL);
772 BT_ASSERT(sc->default_clock_class_name);
773 sc->event_classes = g_ptr_array_new_with_free_func(
774 (GDestroyNotify) fs_sink_ctf_event_class_destroy);
775 BT_ASSERT(sc->event_classes);
776 sc->event_classes_from_ir = g_hash_table_new(g_direct_hash,
777 g_direct_equal);
778 BT_ASSERT(sc->event_classes_from_ir);
779 sc->has_packets = bt_stream_class_supports_packets(ir_sc);
780 sc->packets_have_ts_begin =
781 bt_stream_class_packets_have_beginning_default_clock_snapshot(
782 ir_sc);
783 sc->packets_have_ts_end =
784 bt_stream_class_packets_have_end_default_clock_snapshot(ir_sc);
785 sc->has_discarded_events =
786 bt_stream_class_supports_discarded_events(ir_sc);
787
788 if (sc->has_discarded_events) {
789 sc->discarded_events_has_ts =
790 bt_stream_class_discarded_events_have_default_clock_snapshots(
791 ir_sc);
792 }
793
794 if (bt_stream_class_supports_discarded_packets(ir_sc)) {
795 sc->discarded_packets_has_ts =
796 bt_stream_class_discarded_packets_have_default_clock_snapshots(
797 ir_sc);
798 }
799
800 g_ptr_array_add(trace->stream_classes, sc);
801 return sc;
802 }
803
804 static inline
805 void fs_sink_ctf_stream_class_destroy(struct fs_sink_ctf_stream_class *sc)
806 {
807 if (!sc) {
808 return;
809 }
810
811 if (sc->default_clock_class_name) {
812 g_string_free(sc->default_clock_class_name, TRUE);
813 sc->default_clock_class_name = NULL;
814 }
815
816 if (sc->event_classes) {
817 g_ptr_array_free(sc->event_classes, TRUE);
818 sc->event_classes = NULL;
819 }
820
821 if (sc->event_classes_from_ir) {
822 g_hash_table_destroy(sc->event_classes_from_ir);
823 sc->event_classes_from_ir = NULL;
824 }
825
826 fs_sink_ctf_field_class_destroy(sc->packet_context_fc);
827 sc->packet_context_fc = NULL;
828 fs_sink_ctf_field_class_destroy(sc->event_common_context_fc);
829 sc->event_common_context_fc = NULL;
830 g_free(sc);
831 }
832
833 static inline
834 void fs_sink_ctf_stream_class_append_event_class(
835 struct fs_sink_ctf_stream_class *sc,
836 struct fs_sink_ctf_event_class *ec)
837 {
838 g_ptr_array_add(sc->event_classes, ec);
839 }
840
841 static inline
842 void fs_sink_ctf_trace_destroy(struct fs_sink_ctf_trace *trace)
843 {
844 if (!trace) {
845 return;
846 }
847
848 if (trace->stream_classes) {
849 g_ptr_array_free(trace->stream_classes, TRUE);
850 trace->stream_classes = NULL;
851 }
852
853 g_free(trace);
854 }
855
856 static inline
857 struct fs_sink_ctf_trace *fs_sink_ctf_trace_create(const bt_trace *ir_trace)
858 {
859 struct fs_sink_ctf_trace *trace =
860 g_new0(struct fs_sink_ctf_trace, 1);
861
862 BT_ASSERT(trace);
863
864 bt_uuid_generate(trace->uuid);
865
866 trace->ir_trace = ir_trace;
867 trace->ir_tc = bt_trace_borrow_class_const(ir_trace);
868 trace->stream_classes = g_ptr_array_new_with_free_func(
869 (GDestroyNotify) fs_sink_ctf_stream_class_destroy);
870 BT_ASSERT(trace->stream_classes);
871
872 return trace;
873 }
874
875 #endif /* BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H */
This page took 0.047966 seconds and 5 git commands to generate.