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