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.
21 #include <babeltrace/ctf/types.h>
22 #include <babeltrace/bitfield.h>
25 #include <babeltrace/endian.h>
28 * The aligned read/write functions are expected to be faster than the
29 * bitfield variants. They will be enabled eventually as an
34 int _aligned_integer_read(struct stream_pos
*ppos
,
35 struct definition
*definition
)
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 */
44 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
46 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
49 assert(!(pos
->offset
% CHAR_BIT
));
50 if (!integer_declaration
->signedness
) {
51 switch (integer_declaration
->len
) {
56 v
= *(const uint8_t *) ctf_get_pos_addr(pos
);
57 integer_definition
->value
._unsigned
= v
;
64 v
= *(const uint16_t *) ctf_get_pos_addr(pos
);
65 integer_definition
->value
._unsigned
=
66 rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
73 v
= *(const uint32_t *) ctf_get_pos_addr(pos
);
74 integer_definition
->value
._unsigned
=
75 rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
82 v
= *(const uint64_t *) ctf_get_pos_addr(pos
);
83 integer_definition
->value
._unsigned
=
84 rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
91 switch (integer_declaration
->len
) {
96 v
= *(const int8_t *) ctf_get_pos_addr(pos
);
97 integer_definition
->value
._signed
= v
;
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
;
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
;
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
;
131 ctf_move_pos(pos
, integer_declaration
->len
);
136 int _aligned_integer_write(struct stream_pos
*ppos
,
137 struct definition
*definition
)
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 */
146 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
148 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
151 assert(!(pos
->offset
% CHAR_BIT
));
154 if (!integer_declaration
->signedness
) {
155 uint64_t v
= integer_definition
->value
._unsigned
;
157 switch (integer_declaration
->len
) {
158 case 8: *(uint8_t *) ctf_get_pos_addr(pos
) = (uint8_t) v
;
161 *(uint16_t *) ctf_get_pos_addr(pos
) = rbo
?
162 GUINT16_SWAP_LE_BE((uint16_t) v
) :
166 *(uint32_t *) ctf_get_pos_addr(pos
) = rbo
?
167 GUINT32_SWAP_LE_BE((uint32_t) v
) :
171 *(uint64_t *) ctf_get_pos_addr(pos
) = rbo
?
172 GUINT64_SWAP_LE_BE(v
) : v
;
178 int64_t v
= integer_definition
->value
._signed
;
180 switch (integer_declaration
->len
) {
181 case 8: *(int8_t *) ctf_get_pos_addr(pos
) = (int8_t) v
;
184 *(int16_t *) ctf_get_pos_addr(pos
) = rbo
?
185 (int16_t) GUINT16_SWAP_LE_BE((int16_t) v
) :
189 *(int32_t *) ctf_get_pos_addr(pos
) = rbo
?
190 (int32_t) GUINT32_SWAP_LE_BE((int32_t) v
) :
194 *(int64_t *) ctf_get_pos_addr(pos
) = rbo
?
195 GUINT64_SWAP_LE_BE(v
) : v
;
202 ctf_move_pos(pos
, integer_declaration
->len
);
206 int ctf_integer_read(struct stream_pos
*ppos
, struct definition
*definition
)
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
);
214 if (!(integer_declaration
->p
.alignment
% CHAR_BIT
)
215 && !(integer_declaration
->len
% CHAR_BIT
)) {
216 return _aligned_integer_read(ppos
, definition
);
219 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
221 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
224 if (!integer_declaration
->signedness
) {
225 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
226 bt_bitfield_read_le(mmap_align_addr(pos
->base_mma
) +
227 pos
->mmap_base_offset
, unsigned long,
228 pos
->offset
, integer_declaration
->len
,
229 &integer_definition
->value
._unsigned
);
231 bt_bitfield_read_be(mmap_align_addr(pos
->base_mma
) +
232 pos
->mmap_base_offset
, unsigned long,
233 pos
->offset
, integer_declaration
->len
,
234 &integer_definition
->value
._unsigned
);
236 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
237 bt_bitfield_read_le(mmap_align_addr(pos
->base_mma
) +
238 pos
->mmap_base_offset
, unsigned long,
239 pos
->offset
, integer_declaration
->len
,
240 &integer_definition
->value
._signed
);
242 bt_bitfield_read_be(mmap_align_addr(pos
->base_mma
) +
243 pos
->mmap_base_offset
, unsigned long,
244 pos
->offset
, integer_declaration
->len
,
245 &integer_definition
->value
._signed
);
247 ctf_move_pos(pos
, integer_declaration
->len
);
251 int ctf_integer_write(struct stream_pos
*ppos
, struct definition
*definition
)
253 struct definition_integer
*integer_definition
=
254 container_of(definition
, struct definition_integer
, p
);
255 const struct declaration_integer
*integer_declaration
=
256 integer_definition
->declaration
;
257 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
259 if (!(integer_declaration
->p
.alignment
% CHAR_BIT
)
260 && !(integer_declaration
->len
% CHAR_BIT
)) {
261 return _aligned_integer_write(ppos
, definition
);
264 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
266 if (!ctf_pos_access_ok(pos
, integer_declaration
->len
))
271 if (!integer_declaration
->signedness
) {
272 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
273 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
274 pos
->mmap_base_offset
, unsigned long,
275 pos
->offset
, integer_declaration
->len
,
276 integer_definition
->value
._unsigned
);
278 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
279 pos
->mmap_base_offset
, unsigned long,
280 pos
->offset
, integer_declaration
->len
,
281 integer_definition
->value
._unsigned
);
283 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
284 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
285 pos
->mmap_base_offset
, unsigned long,
286 pos
->offset
, integer_declaration
->len
,
287 integer_definition
->value
._signed
);
289 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
290 pos
->mmap_base_offset
, unsigned long,
291 pos
->offset
, integer_declaration
->len
,
292 integer_definition
->value
._signed
);
295 ctf_move_pos(pos
, integer_declaration
->len
);
This page took 0.037417 seconds and 4 git commands to generate.