4 * Integers read/write functions.
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf/types.h>
30 #include <babeltrace/bitfield.h>
33 #include <babeltrace/endian.h>
36 * The aligned read/write functions are expected to be faster than the
37 * bitfield variants. They will be enabled eventually as an
42 int _aligned_integer_read(struct bt_stream_pos
*ppos
,
43 struct bt_definition
*definition
)
45 struct definition_integer
*integer_definition
=
46 container_of(definition
, struct definition_integer
, p
);
47 const struct declaration_integer
*integer_declaration
=
48 integer_definition
->declaration
;
49 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
50 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
52 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
54 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
57 assert(!(pos
->offset
% CHAR_BIT
));
58 if (!integer_declaration
->signedness
) {
59 switch (integer_declaration
->len
) {
64 v
= *(const uint8_t *) ctf_get_pos_addr(pos
);
65 integer_definition
->value
._unsigned
= v
;
72 v
= *(const uint16_t *) ctf_get_pos_addr(pos
);
73 integer_definition
->value
._unsigned
=
74 rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
81 v
= *(const uint32_t *) ctf_get_pos_addr(pos
);
82 integer_definition
->value
._unsigned
=
83 rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
90 v
= *(const uint64_t *) ctf_get_pos_addr(pos
);
91 integer_definition
->value
._unsigned
=
92 rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
99 switch (integer_declaration
->len
) {
104 v
= *(const int8_t *) ctf_get_pos_addr(pos
);
105 integer_definition
->value
._signed
= v
;
112 v
= *(const int16_t *) ctf_get_pos_addr(pos
);
113 integer_definition
->value
._signed
=
114 rbo
? (int16_t) GUINT16_SWAP_LE_BE(v
) : v
;
121 v
= *(const int32_t *) ctf_get_pos_addr(pos
);
122 integer_definition
->value
._signed
=
123 rbo
? (int32_t) GUINT32_SWAP_LE_BE(v
) : v
;
130 v
= *(const int64_t *) ctf_get_pos_addr(pos
);
131 integer_definition
->value
._signed
=
132 rbo
? (int64_t) GUINT64_SWAP_LE_BE(v
) : v
;
139 ctf_move_pos(pos
, integer_declaration
->len
);
144 int _aligned_integer_write(struct bt_stream_pos
*ppos
,
145 struct bt_definition
*definition
)
147 struct definition_integer
*integer_definition
=
148 container_of(definition
, struct definition_integer
, p
);
149 const struct declaration_integer
*integer_declaration
=
150 integer_definition
->declaration
;
151 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
152 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
154 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
156 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
159 assert(!(pos
->offset
% CHAR_BIT
));
162 if (!integer_declaration
->signedness
) {
163 uint64_t v
= integer_definition
->value
._unsigned
;
165 switch (integer_declaration
->len
) {
166 case 8: *(uint8_t *) ctf_get_pos_addr(pos
) = (uint8_t) v
;
169 *(uint16_t *) ctf_get_pos_addr(pos
) = rbo
?
170 GUINT16_SWAP_LE_BE((uint16_t) v
) :
174 *(uint32_t *) ctf_get_pos_addr(pos
) = rbo
?
175 GUINT32_SWAP_LE_BE((uint32_t) v
) :
179 *(uint64_t *) ctf_get_pos_addr(pos
) = rbo
?
180 GUINT64_SWAP_LE_BE(v
) : v
;
186 int64_t v
= integer_definition
->value
._signed
;
188 switch (integer_declaration
->len
) {
189 case 8: *(int8_t *) ctf_get_pos_addr(pos
) = (int8_t) v
;
192 *(int16_t *) ctf_get_pos_addr(pos
) = rbo
?
193 (int16_t) GUINT16_SWAP_LE_BE((int16_t) v
) :
197 *(int32_t *) ctf_get_pos_addr(pos
) = rbo
?
198 (int32_t) GUINT32_SWAP_LE_BE((int32_t) v
) :
202 *(int64_t *) ctf_get_pos_addr(pos
) = rbo
?
203 GUINT64_SWAP_LE_BE(v
) : v
;
210 ctf_move_pos(pos
, integer_declaration
->len
);
214 int ctf_integer_read(struct bt_stream_pos
*ppos
, struct bt_definition
*definition
)
216 struct definition_integer
*integer_definition
=
217 container_of(definition
, struct definition_integer
, p
);
218 const struct declaration_integer
*integer_declaration
=
219 integer_definition
->declaration
;
220 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
222 if (!(integer_declaration
->p
.alignment
% CHAR_BIT
)
223 && !(integer_declaration
->len
% CHAR_BIT
)) {
224 return _aligned_integer_read(ppos
, definition
);
227 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
229 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
232 if (!integer_declaration
->signedness
) {
233 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
234 bt_bitfield_read_le(mmap_align_addr(pos
->base_mma
) +
235 pos
->mmap_base_offset
, unsigned long,
236 pos
->offset
, integer_declaration
->len
,
237 &integer_definition
->value
._unsigned
);
239 bt_bitfield_read_be(mmap_align_addr(pos
->base_mma
) +
240 pos
->mmap_base_offset
, unsigned long,
241 pos
->offset
, integer_declaration
->len
,
242 &integer_definition
->value
._unsigned
);
244 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
245 bt_bitfield_read_le(mmap_align_addr(pos
->base_mma
) +
246 pos
->mmap_base_offset
, unsigned long,
247 pos
->offset
, integer_declaration
->len
,
248 &integer_definition
->value
._signed
);
250 bt_bitfield_read_be(mmap_align_addr(pos
->base_mma
) +
251 pos
->mmap_base_offset
, unsigned long,
252 pos
->offset
, integer_declaration
->len
,
253 &integer_definition
->value
._signed
);
255 ctf_move_pos(pos
, integer_declaration
->len
);
259 int ctf_integer_write(struct bt_stream_pos
*ppos
, struct bt_definition
*definition
)
261 struct definition_integer
*integer_definition
=
262 container_of(definition
, struct definition_integer
, p
);
263 const struct declaration_integer
*integer_declaration
=
264 integer_definition
->declaration
;
265 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
267 if (!(integer_declaration
->p
.alignment
% CHAR_BIT
)
268 && !(integer_declaration
->len
% CHAR_BIT
)) {
269 return _aligned_integer_write(ppos
, definition
);
272 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
274 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
279 if (!integer_declaration
->signedness
) {
280 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
281 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
282 pos
->mmap_base_offset
, unsigned long,
283 pos
->offset
, integer_declaration
->len
,
284 integer_definition
->value
._unsigned
);
286 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
287 pos
->mmap_base_offset
, unsigned long,
288 pos
->offset
, integer_declaration
->len
,
289 integer_definition
->value
._unsigned
);
291 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
292 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
293 pos
->mmap_base_offset
, unsigned long,
294 pos
->offset
, integer_declaration
->len
,
295 integer_definition
->value
._signed
);
297 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
298 pos
->mmap_base_offset
, unsigned long,
299 pos
->offset
, integer_declaration
->len
,
300 integer_definition
->value
._signed
);
303 ctf_move_pos(pos
, integer_declaration
->len
);
This page took 0.037263 seconds and 4 git commands to generate.