2 * SPDX-License-Identifier: MIT
4 * Copyright 2020 Philippe Proulx <pproulx@efficios.com>
9 #include "ctf-meta-visitors.hpp"
11 static inline int set_alignments(struct ctf_field_class
*fc
)
21 case CTF_FIELD_CLASS_TYPE_STRUCT
:
23 struct ctf_field_class_struct
*struct_fc
= ctf_field_class_as_struct(fc
);
25 for (i
= 0; i
< struct_fc
->members
->len
; i
++) {
26 struct ctf_named_field_class
*named_fc
=
27 ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
29 ret
= set_alignments(named_fc
->fc
);
34 if (named_fc
->fc
->alignment
> fc
->alignment
) {
35 fc
->alignment
= named_fc
->fc
->alignment
;
41 case CTF_FIELD_CLASS_TYPE_VARIANT
:
43 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(fc
);
45 for (i
= 0; i
< var_fc
->options
->len
; i
++) {
46 struct ctf_named_field_class
*named_fc
=
47 ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
49 ret
= set_alignments(named_fc
->fc
);
57 case CTF_FIELD_CLASS_TYPE_ARRAY
:
58 case CTF_FIELD_CLASS_TYPE_SEQUENCE
:
60 struct ctf_field_class_array_base
*array_fc
= ctf_field_class_as_array_base(fc
);
62 ret
= set_alignments(array_fc
->elem_fc
);
68 * Use the alignment of the array/sequence field class's
69 * element FC as its own alignment.
71 * This is especially important when the array/sequence
72 * field's effective length is zero: as per CTF 1.8, the
73 * stream data decoding process still needs to align the
74 * cursor using the element's alignment [1]:
76 * > Arrays are always aligned on their element
77 * > alignment requirement.
82 * integer { size = 8; } a;
83 * integer { size = 8; align = 16; } b[0];
84 * integer { size = 8; } c;
87 * When using this to decode the bytes 1, 2, and 3, then
88 * the decoded values are:
94 * [1]: https://diamon.org/ctf/#spec4.2.3
96 array_fc
->base
.alignment
= array_fc
->elem_fc
->alignment
;
107 int ctf_trace_class_update_alignments(struct ctf_trace_class
*ctf_tc
)
112 if (!ctf_tc
->is_translated
) {
113 ret
= set_alignments(ctf_tc
->packet_header_fc
);
119 for (i
= 0; i
< ctf_tc
->stream_classes
->len
; i
++) {
120 ctf_stream_class
*sc
= (ctf_stream_class
*) ctf_tc
->stream_classes
->pdata
[i
];
123 if (!sc
->is_translated
) {
124 ret
= set_alignments(sc
->packet_context_fc
);
129 ret
= set_alignments(sc
->event_header_fc
);
134 ret
= set_alignments(sc
->event_common_context_fc
);
140 for (j
= 0; j
< sc
->event_classes
->len
; j
++) {
141 struct ctf_event_class
*ec
= (ctf_event_class
*) sc
->event_classes
->pdata
[j
];
143 if (ec
->is_translated
) {
147 ret
= set_alignments(ec
->spec_context_fc
);
152 ret
= set_alignments(ec
->payload_fc
);