cleanup
[babeltrace.git] / formats / ctf / types / integer.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
de0ba614 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
6dc2ca62
MD
17 */
18
4c8bfb7e
MD
19#include <babeltrace/ctf/types.h>
20#include <babeltrace/bitfield.h>
6dc2ca62
MD
21#include <stdint.h>
22#include <glib.h>
23#include <endian.h>
24
5385cf15
MD
25/*
26 * The aligned read/write functions are expected to be faster than the
27 * bitfield variants. They will be enabled eventually as an
28 * optimisation.
29 */
30
7fe00194 31static
d11e9c49
MD
32void _aligned_integer_read(struct stream_pos *ppos,
33 struct definition *definition)
6dc2ca62 34{
d11e9c49
MD
35 struct definition_integer *integer_definition =
36 container_of(definition, struct definition_integer, p);
37 const struct declaration_integer *integer_declaration =
38 integer_definition->declaration;
46322b33 39 struct ctf_stream_pos *pos = ctf_pos(ppos);
f6625916 40 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 41
46322b33 42 ctf_align_pos(pos, integer_declaration->p.alignment);
bed864a7 43 assert(!(pos->offset % CHAR_BIT));
6dc2ca62 44
d11e9c49
MD
45 if (!integer_declaration->signedness) {
46 switch (integer_declaration->len) {
47 case 8:
48 {
49 uint8_t v;
50
5385cf15 51 v = *(const uint8_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
52 integer_definition->value._unsigned = v;
53 break;
54 }
55 case 16:
56 {
57 uint16_t v;
58
5385cf15 59 v = *(const uint16_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
60 integer_definition->value._unsigned =
61 rbo ? GUINT16_SWAP_LE_BE(v) : v;
62 break;
63 }
64 case 32:
65 {
66 uint32_t v;
67
5385cf15 68 v = *(const uint32_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
69 integer_definition->value._unsigned =
70 rbo ? GUINT32_SWAP_LE_BE(v) : v;
71 break;
72 }
73 case 64:
74 {
75 uint64_t v;
76
5385cf15 77 v = *(const uint64_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
78 integer_definition->value._unsigned =
79 rbo ? GUINT64_SWAP_LE_BE(v) : v;
80 break;
81 }
82 default:
83 assert(0);
84 }
85 } else {
86 switch (integer_declaration->len) {
87 case 8:
88 {
89 int8_t v;
90
5385cf15 91 v = *(const int8_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
92 integer_definition->value._signed = v;
93 break;
94 }
95 case 16:
96 {
97 int16_t v;
98
5385cf15 99 v = *(const int16_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
100 integer_definition->value._signed =
101 rbo ? GUINT16_SWAP_LE_BE(v) : v;
102 break;
103 }
104 case 32:
105 {
106 int32_t v;
107
5385cf15 108 v = *(const int32_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
109 integer_definition->value._signed =
110 rbo ? GUINT32_SWAP_LE_BE(v) : v;
111 break;
112 }
113 case 64:
114 {
115 int64_t v;
116
5385cf15 117 v = *(const int64_t *) ctf_get_pos_addr(pos);
d11e9c49
MD
118 integer_definition->value._signed =
119 rbo ? GUINT64_SWAP_LE_BE(v) : v;
120 break;
121 }
122 default:
123 assert(0);
124 }
6dc2ca62 125 }
d11e9c49 126 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62
MD
127}
128
7fe00194 129static
d11e9c49
MD
130void _aligned_integer_write(struct stream_pos *ppos,
131 struct definition *definition)
6dc2ca62 132{
d11e9c49
MD
133 struct definition_integer *integer_definition =
134 container_of(definition, struct definition_integer, p);
135 const struct declaration_integer *integer_declaration =
136 integer_definition->declaration;
46322b33 137 struct ctf_stream_pos *pos = ctf_pos(ppos);
f6625916 138 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 139
46322b33 140 ctf_align_pos(pos, integer_declaration->p.alignment);
bed864a7 141 assert(!(pos->offset % CHAR_BIT));
6dc2ca62 142
bed864a7 143 if (pos->dummy)
6dc2ca62 144 goto end;
d11e9c49
MD
145 if (!integer_declaration->signedness) {
146 uint64_t v = integer_definition->value._unsigned;
147
148 switch (integer_declaration->len) {
149 case 8: *(uint8_t *) ctf_get_pos_addr(pos) = (uint8_t) v;
150 break;
151 case 16:
152 *(uint16_t *) ctf_get_pos_addr(pos) = rbo ?
153 GUINT16_SWAP_LE_BE((uint16_t) v) :
154 (uint16_t) v;
155 break;
156 case 32:
157 *(uint32_t *) ctf_get_pos_addr(pos) = rbo ?
158 GUINT32_SWAP_LE_BE((uint32_t) v) :
159 (uint32_t) v;
160 break;
161 case 64:
162 *(uint64_t *) ctf_get_pos_addr(pos) = rbo ?
163 GUINT64_SWAP_LE_BE(v) : v;
164 break;
165 default:
166 assert(0);
167 }
168 } else {
169 int64_t v = integer_definition->value._signed;
170
171 switch (integer_declaration->len) {
172 case 8: *(int8_t *) ctf_get_pos_addr(pos) = (int8_t) v;
173 break;
174 case 16:
175 *(int16_t *) ctf_get_pos_addr(pos) = rbo ?
176 GUINT16_SWAP_LE_BE((int16_t) v) :
177 (int16_t) v;
178 break;
179 case 32:
180 *(int32_t *) ctf_get_pos_addr(pos) = rbo ?
181 GUINT32_SWAP_LE_BE((int32_t) v) :
182 (int32_t) v;
183 break;
184 case 64:
185 *(int64_t *) ctf_get_pos_addr(pos) = rbo ?
186 GUINT64_SWAP_LE_BE(v) : v;
187 break;
188 default:
189 assert(0);
190 }
6dc2ca62
MD
191 }
192end:
46322b33 193 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62
MD
194}
195
d11e9c49 196void ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
6dc2ca62 197{
d11e9c49
MD
198 struct definition_integer *integer_definition =
199 container_of(definition, struct definition_integer, p);
200 const struct declaration_integer *integer_declaration =
201 integer_definition->declaration;
46322b33 202 struct ctf_stream_pos *pos = ctf_pos(ppos);
6dc2ca62 203
5385cf15
MD
204 if (!(integer_declaration->p.alignment % CHAR_BIT)
205 && !(integer_declaration->len % CHAR_BIT)) {
206 _aligned_integer_read(ppos, definition);
207 return;
208 }
209
46322b33 210 ctf_align_pos(pos, integer_declaration->p.alignment);
d11e9c49
MD
211 if (!integer_declaration->signedness) {
212 if (integer_declaration->byte_order == LITTLE_ENDIAN)
213 bt_bitfield_read_le(pos->base, unsigned long,
214 pos->offset, integer_declaration->len,
215 &integer_definition->value._unsigned);
216 else
217 bt_bitfield_read_be(pos->base, unsigned long,
218 pos->offset, integer_declaration->len,
219 &integer_definition->value._unsigned);
220 } else {
221 if (integer_declaration->byte_order == LITTLE_ENDIAN)
222 bt_bitfield_read_le(pos->base, unsigned long,
223 pos->offset, integer_declaration->len,
224 &integer_definition->value._signed);
225 else
226 bt_bitfield_read_be(pos->base, unsigned long,
227 pos->offset, integer_declaration->len,
228 &integer_definition->value._signed);
6dc2ca62 229 }
46322b33 230 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62 231}
7fe00194 232
d11e9c49 233void ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
7fe00194 234{
d11e9c49
MD
235 struct definition_integer *integer_definition =
236 container_of(definition, struct definition_integer, p);
237 const struct declaration_integer *integer_declaration =
238 integer_definition->declaration;
46322b33
MD
239 struct ctf_stream_pos *pos = ctf_pos(ppos);
240
5385cf15
MD
241 if (!(integer_declaration->p.alignment % CHAR_BIT)
242 && !(integer_declaration->len % CHAR_BIT)) {
243 _aligned_integer_write(ppos, definition);
244 return;
245 }
246
46322b33 247 ctf_align_pos(pos, integer_declaration->p.alignment);
7fe00194
MD
248 if (pos->dummy)
249 goto end;
d11e9c49
MD
250 if (!integer_declaration->signedness) {
251 if (integer_declaration->byte_order == LITTLE_ENDIAN)
252 bt_bitfield_write_le(pos->base, unsigned long,
253 pos->offset, integer_declaration->len,
254 integer_definition->value._unsigned);
255 else
256 bt_bitfield_write_be(pos->base, unsigned long,
257 pos->offset, integer_declaration->len,
258 integer_definition->value._unsigned);
259 } else {
260 if (integer_declaration->byte_order == LITTLE_ENDIAN)
261 bt_bitfield_write_le(pos->base, unsigned long,
262 pos->offset, integer_declaration->len,
263 integer_definition->value._signed);
264 else
265 bt_bitfield_write_be(pos->base, unsigned long,
266 pos->offset, integer_declaration->len,
267 integer_definition->value._signed);
268 }
7fe00194 269end:
46322b33 270 ctf_move_pos(pos, integer_declaration->len);
7fe00194 271}
This page took 0.059084 seconds and 4 git commands to generate.