Use inheritance for trace descriptor and positions
[babeltrace.git] / formats / ctf / types / integer.c
1 /*
2 * Common Trace Format
3 *
4 * Integers read/write functions.
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/ctf/types.h>
20 #include <babeltrace/bitfield.h>
21 #include <stdint.h>
22 #include <glib.h>
23 #include <endian.h>
24
25 static
26 uint64_t _aligned_uint_read(struct stream_pos *ppos,
27 const struct declaration_integer *integer_declaration)
28 {
29 struct ctf_stream_pos *pos = ctf_pos(ppos);
30 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
31
32 ctf_align_pos(pos, integer_declaration->p.alignment);
33 assert(!(pos->offset % CHAR_BIT));
34 switch (integer_declaration->len) {
35 case 8:
36 {
37 uint8_t v;
38
39 v = *(const uint8_t *)pos->base;
40 ctf_move_pos(pos, integer_declaration->len);
41 return v;
42 }
43 case 16:
44 {
45 uint16_t v;
46
47 v = *(const uint16_t *)pos->base;
48 ctf_move_pos(pos, integer_declaration->len);
49 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
50 }
51 case 32:
52 {
53 uint32_t v;
54
55 v = *(const uint32_t *)pos->base;
56 ctf_move_pos(pos, integer_declaration->len);
57 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
58 }
59 case 64:
60 {
61 uint64_t v;
62
63 v = *(const uint64_t *)pos->base;
64 ctf_move_pos(pos, integer_declaration->len);
65 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
66 }
67 default:
68 assert(0);
69 }
70 }
71
72 static
73 int64_t _aligned_int_read(struct stream_pos *ppos,
74 const struct declaration_integer *integer_declaration)
75 {
76 struct ctf_stream_pos *pos = ctf_pos(ppos);
77 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
78
79 ctf_align_pos(pos, integer_declaration->p.alignment);
80 assert(!(pos->offset % CHAR_BIT));
81 switch (integer_declaration->len) {
82 case 8:
83 {
84 int8_t v;
85
86 v = *(const int8_t *)pos->base;
87 ctf_move_pos(pos, integer_declaration->len);
88 return v;
89 }
90 case 16:
91 {
92 int16_t v;
93
94 v = *(const int16_t *)pos->base;
95 ctf_move_pos(pos, integer_declaration->len);
96 return rbo ? GUINT16_SWAP_LE_BE(v) : v;
97 }
98 case 32:
99 {
100 int32_t v;
101
102 v = *(const int32_t *)pos->base;
103 ctf_move_pos(pos, integer_declaration->len);
104 return rbo ? GUINT32_SWAP_LE_BE(v) : v;
105 }
106 case 64:
107 {
108 int64_t v;
109
110 v = *(const int64_t *)pos->base;
111 ctf_move_pos(pos, integer_declaration->len);
112 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
113 }
114 default:
115 assert(0);
116 }
117 }
118
119 static
120 void _aligned_uint_write(struct stream_pos *ppos,
121 const struct declaration_integer *integer_declaration,
122 uint64_t v)
123 {
124 struct ctf_stream_pos *pos = ctf_pos(ppos);
125 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
126
127 ctf_align_pos(pos, integer_declaration->p.alignment);
128 assert(!(pos->offset % CHAR_BIT));
129 if (pos->dummy)
130 goto end;
131
132 switch (integer_declaration->len) {
133 case 8: *(uint8_t *) ctf_get_pos_addr(pos) = (uint8_t) v;
134 break;
135 case 16:
136 *(uint16_t *) ctf_get_pos_addr(pos) = rbo ?
137 GUINT16_SWAP_LE_BE((uint16_t) v) :
138 (uint16_t) v;
139 break;
140 case 32:
141 *(uint32_t *) ctf_get_pos_addr(pos) = rbo ?
142 GUINT32_SWAP_LE_BE((uint32_t) v) :
143 (uint32_t) v;
144 break;
145 case 64:
146 *(uint64_t *) ctf_get_pos_addr(pos) = rbo ?
147 GUINT64_SWAP_LE_BE(v) : v;
148 break;
149 default:
150 assert(0);
151 }
152 end:
153 ctf_move_pos(pos, integer_declaration->len);
154 }
155
156 static
157 void _aligned_int_write(struct stream_pos *ppos,
158 const struct declaration_integer *integer_declaration,
159 int64_t v)
160 {
161 struct ctf_stream_pos *pos = ctf_pos(ppos);
162 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
163
164 ctf_align_pos(pos, integer_declaration->p.alignment);
165 assert(!(pos->offset % CHAR_BIT));
166 if (pos->dummy)
167 goto end;
168
169 switch (integer_declaration->len) {
170 case 8: *(int8_t *) ctf_get_pos_addr(pos) = (int8_t) v;
171 break;
172 case 16:
173 *(int16_t *) ctf_get_pos_addr(pos) = rbo ?
174 GUINT16_SWAP_LE_BE((int16_t) v) :
175 (int16_t) v;
176 break;
177 case 32:
178 *(int32_t *) ctf_get_pos_addr(pos) = rbo ?
179 GUINT32_SWAP_LE_BE((int32_t) v) :
180 (int32_t) v;
181 break;
182 case 64:
183 *(int64_t *) ctf_get_pos_addr(pos) = rbo ?
184 GUINT64_SWAP_LE_BE(v) : v;
185 break;
186 default:
187 assert(0);
188 }
189 end:
190 ctf_move_pos(pos, integer_declaration->len);
191 return;
192 }
193
194 uint64_t ctf_uint_read(struct stream_pos *ppos,
195 const struct declaration_integer *integer_declaration)
196 {
197 struct ctf_stream_pos *pos = ctf_pos(ppos);
198 uint64_t v = 0;
199
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);
204 else
205 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
206 integer_declaration->len, &v);
207 ctf_move_pos(pos, integer_declaration->len);
208 return v;
209 }
210
211 int64_t ctf_int_read(struct stream_pos *ppos,
212 const struct declaration_integer *integer_declaration)
213 {
214 struct ctf_stream_pos *pos = ctf_pos(ppos);
215 int64_t v = 0;
216
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);
221 else
222 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
223 integer_declaration->len, &v);
224 ctf_move_pos(pos, integer_declaration->len);
225 return v;
226 }
227
228 void ctf_uint_write(struct stream_pos *ppos,
229 const struct declaration_integer *integer_declaration,
230 uint64_t v)
231 {
232 struct ctf_stream_pos *pos = ctf_pos(ppos);
233
234 ctf_align_pos(pos, integer_declaration->p.alignment);
235 if (pos->dummy)
236 goto end;
237 if (integer_declaration->byte_order == LITTLE_ENDIAN)
238 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
239 integer_declaration->len, v);
240 else
241 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
242 integer_declaration->len, v);
243 end:
244 ctf_move_pos(pos, integer_declaration->len);
245 }
246
247 void ctf_int_write(struct stream_pos *ppos,
248 const struct declaration_integer *integer_declaration,
249 int64_t v)
250 {
251 struct ctf_stream_pos *pos = ctf_pos(ppos);
252
253 ctf_align_pos(pos, integer_declaration->p.alignment);
254 if (pos->dummy)
255 goto end;
256 if (integer_declaration->byte_order == LITTLE_ENDIAN)
257 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
258 integer_declaration->len, v);
259 else
260 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
261 integer_declaration->len, v);
262 end:
263 ctf_move_pos(pos, integer_declaration->len);
264 }
This page took 0.033791 seconds and 4 git commands to generate.