2 * SPDX-License-Identifier: MIT
4 * Copyright 2020 Philippe Proulx <pproulx@efficios.com>
12 #include <babeltrace2/babeltrace.h>
14 #include "common/assert.h"
15 #include "common/macros.h"
17 #include "ctf-meta-visitors.hpp"
19 static inline int set_alignments(struct ctf_field_class
*fc
)
29 case CTF_FIELD_CLASS_TYPE_STRUCT
:
31 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
33 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
34 struct ctf_named_field_class
*named_fc
=
35 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
37 ret
= set_alignments(named_fc
->fc
);
42 if (named_fc
->fc
->alignment
> fc
->alignment
) {
43 fc
->alignment
= named_fc
->fc
->alignment
;
49 case CTF_FIELD_CLASS_TYPE_VARIANT
:
51 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
53 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
54 struct ctf_named_field_class
*named_fc
=
55 ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
57 ret
= set_alignments(named_fc
->fc
);
65 case CTF_FIELD_CLASS_TYPE_ARRAY
:
66 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
68 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
70 ret
= set_alignments(array_fc
->elem_fc
);
76 * Use the alignment of the array/sequence field class's
77 * element FC as its own alignment.
79 * This is especially important when the array/sequence
80 * field's effective length is zero: as per CTF 1.8, the
81 * stream data decoding process still needs to align the
82 * cursor using the element's alignment [1]:
84 * > Arrays are always aligned on their element
85 * > alignment requirement.
90 * integer { size = 8; } a;
91 * integer { size = 8; align = 16; } b[0];
92 * integer { size = 8; } c;
95 * When using this to decode the bytes 1, 2, and 3, then
96 * the decoded values are:
102 * [1]: https://diamon.org/ctf/#spec4.2.3
104 array_fc
->base
.alignment
= array_fc
->elem_fc
->alignment
;
115 int ctf_trace_class_update_alignments(struct ctf_trace_class
*ctf_tc
)
120 if (!ctf_tc
->is_translated
) {
121 ret
= set_alignments(ctf_tc
->packet_header_fc
);
127 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
128 ctf_stream_class
*sc
= (ctf_stream_class
*) ctf_tc
->stream_classes
->pdata
[i
];
131 if (!sc
->is_translated
) {
132 ret
= set_alignments(sc
->packet_context_fc
);
137 ret
= set_alignments(sc
->event_header_fc
);
142 ret
= set_alignments(sc
->event_common_context_fc
);
148 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
149 struct ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[j
];
151 if (ec
->is_translated
) {
155 ret
= set_alignments(ec
->spec_context_fc
);
160 ret
= set_alignments(ec
->payload_fc
);