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