Sort includes in C++ files
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta-update-alignments.cpp
CommitLineData
e6938018
PP
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
e6938018 7#include <glib.h>
c802cacb 8#include <inttypes.h>
e6938018
PP
9#include <stdint.h>
10#include <string.h>
c802cacb
SM
11
12#include <babeltrace2/babeltrace.h>
13
14#include "common/assert.h"
15#include "common/macros.h"
e6938018 16
087cd0f5 17#include "ctf-meta-visitors.hpp"
e6938018 18
4164020e 19static inline int set_alignments(struct ctf_field_class *fc)
e6938018 20{
4164020e
SM
21 int ret = 0;
22 uint64_t i;
23
24 if (!fc) {
25 goto end;
26 }
27
28 switch (fc->type) {
29 case CTF_FIELD_CLASS_TYPE_STRUCT:
30 {
31 struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc);
32
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);
36
37 ret = set_alignments(named_fc->fc);
38 if (ret) {
39 goto end;
40 }
41
42 if (named_fc->fc->alignment > fc->alignment) {
43 fc->alignment = named_fc->fc->alignment;
44 }
45 }
46
47 break;
48 }
49 case CTF_FIELD_CLASS_TYPE_VARIANT:
50 {
51 struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
52
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);
56
57 ret = set_alignments(named_fc->fc);
58 if (ret) {
59 goto end;
60 }
61 }
62
63 break;
64 }
65 case CTF_FIELD_CLASS_TYPE_ARRAY:
66 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
67 {
68 struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
69
70 ret = set_alignments(array_fc->elem_fc);
71 if (ret) {
72 goto end;
73 }
74
75 /*
76 * Use the alignment of the array/sequence field class's
77 * element FC as its own alignment.
78 *
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]:
83 *
84 * > Arrays are always aligned on their element
85 * > alignment requirement.
86 *
87 * For example:
88 *
89 * struct {
90 * integer { size = 8; } a;
91 * integer { size = 8; align = 16; } b[0];
92 * integer { size = 8; } c;
93 * };
94 *
95 * When using this to decode the bytes 1, 2, and 3, then
96 * the decoded values are:
97 *
98 * `a`: 1
99 * `b`: []
100 * `c`: 3
101 *
102 * [1]: https://diamon.org/ctf/#spec4.2.3
103 */
104 array_fc->base.alignment = array_fc->elem_fc->alignment;
105 break;
106 }
107 default:
108 break;
109 }
e6938018
PP
110
111end:
4164020e 112 return ret;
e6938018
PP
113}
114
4164020e 115int ctf_trace_class_update_alignments(struct ctf_trace_class *ctf_tc)
e6938018 116{
4164020e
SM
117 int ret = 0;
118 uint64_t i;
119
120 if (!ctf_tc->is_translated) {
121 ret = set_alignments(ctf_tc->packet_header_fc);
122 if (ret) {
123 goto end;
124 }
125 }
126
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];
129 uint64_t j;
130
131 if (!sc->is_translated) {
132 ret = set_alignments(sc->packet_context_fc);
133 if (ret) {
134 goto end;
135 }
136
137 ret = set_alignments(sc->event_header_fc);
138 if (ret) {
139 goto end;
140 }
141
142 ret = set_alignments(sc->event_common_context_fc);
143 if (ret) {
144 goto end;
145 }
146 }
147
148 for (j = 0; j < sc->event_classes->len; j++) {
149 struct ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[j];
150
151 if (ec->is_translated) {
152 continue;
153 }
154
155 ret = set_alignments(ec->spec_context_fc);
156 if (ret) {
157 goto end;
158 }
159
160 ret = set_alignments(ec->payload_fc);
161 if (ret) {
162 goto end;
163 }
164 }
165 }
e6938018
PP
166
167end:
4164020e 168 return ret;
e6938018 169}
This page took 0.051935 seconds and 4 git commands to generate.