4 * Integers read/write functions.
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/ctf/types.h>
20 #include <babeltrace/bitfield.h>
26 uint64_t _aligned_uint_read(struct stream_pos
*ppos
,
27 const struct declaration_integer
*integer_declaration
)
29 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
30 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
32 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
33 assert(!(pos
->offset
% CHAR_BIT
));
34 switch (integer_declaration
->len
) {
39 v
= *(const uint8_t *)pos
->base
;
40 ctf_move_pos(pos
, integer_declaration
->len
);
47 v
= *(const uint16_t *)pos
->base
;
48 ctf_move_pos(pos
, integer_declaration
->len
);
49 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
55 v
= *(const uint32_t *)pos
->base
;
56 ctf_move_pos(pos
, integer_declaration
->len
);
57 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
63 v
= *(const uint64_t *)pos
->base
;
64 ctf_move_pos(pos
, integer_declaration
->len
);
65 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
73 int64_t _aligned_int_read(struct stream_pos
*ppos
,
74 const struct declaration_integer
*integer_declaration
)
76 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
77 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
79 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
80 assert(!(pos
->offset
% CHAR_BIT
));
81 switch (integer_declaration
->len
) {
86 v
= *(const int8_t *)pos
->base
;
87 ctf_move_pos(pos
, integer_declaration
->len
);
94 v
= *(const int16_t *)pos
->base
;
95 ctf_move_pos(pos
, integer_declaration
->len
);
96 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
102 v
= *(const int32_t *)pos
->base
;
103 ctf_move_pos(pos
, integer_declaration
->len
);
104 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
110 v
= *(const int64_t *)pos
->base
;
111 ctf_move_pos(pos
, integer_declaration
->len
);
112 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
120 void _aligned_uint_write(struct stream_pos
*ppos
,
121 const struct declaration_integer
*integer_declaration
,
124 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
125 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
127 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
128 assert(!(pos
->offset
% CHAR_BIT
));
132 switch (integer_declaration
->len
) {
133 case 8: *(uint8_t *) ctf_get_pos_addr(pos
) = (uint8_t) v
;
136 *(uint16_t *) ctf_get_pos_addr(pos
) = rbo
?
137 GUINT16_SWAP_LE_BE((uint16_t) v
) :
141 *(uint32_t *) ctf_get_pos_addr(pos
) = rbo
?
142 GUINT32_SWAP_LE_BE((uint32_t) v
) :
146 *(uint64_t *) ctf_get_pos_addr(pos
) = rbo
?
147 GUINT64_SWAP_LE_BE(v
) : v
;
153 ctf_move_pos(pos
, integer_declaration
->len
);
157 void _aligned_int_write(struct stream_pos
*ppos
,
158 const struct declaration_integer
*integer_declaration
,
161 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
162 int rbo
= (integer_declaration
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
164 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
165 assert(!(pos
->offset
% CHAR_BIT
));
169 switch (integer_declaration
->len
) {
170 case 8: *(int8_t *) ctf_get_pos_addr(pos
) = (int8_t) v
;
173 *(int16_t *) ctf_get_pos_addr(pos
) = rbo
?
174 GUINT16_SWAP_LE_BE((int16_t) v
) :
178 *(int32_t *) ctf_get_pos_addr(pos
) = rbo
?
179 GUINT32_SWAP_LE_BE((int32_t) v
) :
183 *(int64_t *) ctf_get_pos_addr(pos
) = rbo
?
184 GUINT64_SWAP_LE_BE(v
) : v
;
190 ctf_move_pos(pos
, integer_declaration
->len
);
194 uint64_t ctf_uint_read(struct stream_pos
*ppos
,
195 const struct declaration_integer
*integer_declaration
)
197 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
200 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
201 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
202 bt_bitfield_read_le(pos
->base
, unsigned long, pos
->offset
,
203 integer_declaration
->len
, &v
);
205 bt_bitfield_read_be(pos
->base
, unsigned long, pos
->offset
,
206 integer_declaration
->len
, &v
);
207 ctf_move_pos(pos
, integer_declaration
->len
);
211 int64_t ctf_int_read(struct stream_pos
*ppos
,
212 const struct declaration_integer
*integer_declaration
)
214 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
217 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
218 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
219 bt_bitfield_read_le(pos
->base
, unsigned long, pos
->offset
,
220 integer_declaration
->len
, &v
);
222 bt_bitfield_read_be(pos
->base
, unsigned long, pos
->offset
,
223 integer_declaration
->len
, &v
);
224 ctf_move_pos(pos
, integer_declaration
->len
);
228 void ctf_uint_write(struct stream_pos
*ppos
,
229 const struct declaration_integer
*integer_declaration
,
232 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
234 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
237 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
238 bt_bitfield_write_le(pos
->base
, unsigned long, pos
->offset
,
239 integer_declaration
->len
, v
);
241 bt_bitfield_write_be(pos
->base
, unsigned long, pos
->offset
,
242 integer_declaration
->len
, v
);
244 ctf_move_pos(pos
, integer_declaration
->len
);
247 void ctf_int_write(struct stream_pos
*ppos
,
248 const struct declaration_integer
*integer_declaration
,
251 struct ctf_stream_pos
*pos
= ctf_pos(ppos
);
253 ctf_align_pos(pos
, integer_declaration
->p
.alignment
);
256 if (integer_declaration
->byte_order
== LITTLE_ENDIAN
)
257 bt_bitfield_write_le(pos
->base
, unsigned long, pos
->offset
,
258 integer_declaration
->len
, v
);
260 bt_bitfield_write_be(pos
->base
, unsigned long, pos
->offset
,
261 integer_declaration
->len
, v
);
263 ctf_move_pos(pos
, integer_declaration
->len
);
This page took 0.034899 seconds and 5 git commands to generate.