namespace the sequence functions
[babeltrace.git] / types / integer.c
CommitLineData
fc93b2bd 1/*
ccd7e1c8 2 * integer.c
fc93b2bd 3 *
ccd7e1c8 4 * BabelTrace - Integer Type Converter
fc93b2bd 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 9 *
ccd7e1c8
MD
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:
fc93b2bd 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
19 *
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
26 * SOFTWARE.
fc93b2bd
MD
27 */
28
29#include <babeltrace/compiler.h>
698f0fe4 30#include <babeltrace/align.h>
4c8bfb7e 31#include <babeltrace/format.h>
3122e6f0 32#include <babeltrace/types.h>
fc93b2bd
MD
33#include <stdint.h>
34
c054553d 35static
f6625916 36struct definition *_integer_definition_new(struct declaration *declaration,
05c749e5 37 struct definition_scope *parent_scope,
98df1c9f
MD
38 GQuark field_name, int index,
39 const char *root_name);
c054553d 40static
e1151715 41void _integer_definition_free(struct definition *definition);
c054553d 42
c054553d 43static
f6625916 44void _integer_declaration_free(struct declaration *declaration)
90b676d7 45{
f6625916
MD
46 struct declaration_integer *integer_declaration =
47 container_of(declaration, struct declaration_integer, p);
48 g_free(integer_declaration);
90b676d7
MD
49}
50
f6625916 51struct declaration_integer *
add40b62 52 integer_declaration_new(size_t len, int byte_order,
81dee1bb 53 int signedness, size_t alignment, int base,
56e60373
MD
54 enum ctf_string_encoding encoding,
55 struct ctf_clock *clock)
698f0fe4 56{
f6625916 57 struct declaration_integer *integer_declaration;
698f0fe4 58
f6625916
MD
59 integer_declaration = g_new(struct declaration_integer, 1);
60 integer_declaration->p.id = CTF_TYPE_INTEGER;
f6625916 61 integer_declaration->p.alignment = alignment;
f6625916
MD
62 integer_declaration->p.declaration_free = _integer_declaration_free;
63 integer_declaration->p.definition_free = _integer_definition_free;
64 integer_declaration->p.definition_new = _integer_definition_new;
65 integer_declaration->p.ref = 1;
66 integer_declaration->len = len;
67 integer_declaration->byte_order = byte_order;
68 integer_declaration->signedness = signedness;
164078da 69 integer_declaration->base = base;
81dee1bb 70 integer_declaration->encoding = encoding;
56e60373 71 integer_declaration->clock = clock;
f6625916 72 return integer_declaration;
698f0fe4 73}
c054553d
MD
74
75static
e1151715 76struct definition *
f6625916 77 _integer_definition_new(struct declaration *declaration,
05c749e5 78 struct definition_scope *parent_scope,
98df1c9f
MD
79 GQuark field_name, int index,
80 const char *root_name)
c054553d 81{
f6625916
MD
82 struct declaration_integer *integer_declaration =
83 container_of(declaration, struct declaration_integer, p);
e1151715 84 struct definition_integer *integer;
98df1c9f 85 int ret;
c054553d 86
e1151715 87 integer = g_new(struct definition_integer, 1);
e6b4b4f4 88 bt_declaration_ref(&integer_declaration->p);
f6625916
MD
89 integer->p.declaration = declaration;
90 integer->declaration = integer_declaration;
c054553d 91 integer->p.ref = 1;
98df1c9f
MD
92 /*
93 * Use INT_MAX order to ensure that all fields of the parent
94 * scope are seen as being prior to this scope.
95 */
96 integer->p.index = root_name ? INT_MAX : index;
b1a2f580 97 integer->p.name = field_name;
98df1c9f
MD
98 integer->p.path = new_definition_path(parent_scope, field_name,
99 root_name);
a35173fe 100 integer->p.scope = NULL;
c054553d 101 integer->value._unsigned = 0;
98df1c9f
MD
102 ret = register_field_definition(field_name, &integer->p,
103 parent_scope);
104 assert(!ret);
c054553d
MD
105 return &integer->p;
106}
107
108static
e1151715 109void _integer_definition_free(struct definition *definition)
c054553d 110{
e1151715
MD
111 struct definition_integer *integer =
112 container_of(definition, struct definition_integer, p);
c054553d 113
e6b4b4f4 114 bt_declaration_unref(integer->p.declaration);
c054553d
MD
115 g_free(integer);
116}
343c801f 117
fe2b0e24 118enum ctf_string_encoding bt_get_int_encoding(const struct definition *field)
8673030f
JD
119{
120 struct definition_integer *integer_definition;
121 const struct declaration_integer *integer_declaration;
122
123 integer_definition = container_of(field, struct definition_integer, p);
124 integer_declaration = integer_definition->declaration;
125
126 return integer_declaration->encoding;
127}
128
fe2b0e24 129int bt_get_int_base(const struct definition *field)
8673030f
JD
130{
131 struct definition_integer *integer_definition;
132 const struct declaration_integer *integer_declaration;
133
134 integer_definition = container_of(field, struct definition_integer, p);
135 integer_declaration = integer_definition->declaration;
136
137 return integer_declaration->base;
138}
139
fe2b0e24 140size_t bt_get_int_len(const struct definition *field)
fef0e521
MD
141{
142 struct definition_integer *integer_definition;
143 const struct declaration_integer *integer_declaration;
144
145 integer_definition = container_of(field, struct definition_integer, p);
146 integer_declaration = integer_definition->declaration;
147
148 return integer_declaration->len;
149}
150
fe2b0e24 151int bt_get_int_byte_order(const struct definition *field)
8673030f
JD
152{
153 struct definition_integer *integer_definition;
154 const struct declaration_integer *integer_declaration;
155
156 integer_definition = container_of(field, struct definition_integer, p);
157 integer_declaration = integer_definition->declaration;
158
159 return integer_declaration->byte_order;
160}
161
fe2b0e24 162int bt_get_int_signedness(const struct definition *field)
8673030f
JD
163{
164 struct definition_integer *integer_definition;
165 const struct declaration_integer *integer_declaration;
166
167 integer_definition = container_of(field, struct definition_integer, p);
168 integer_declaration = integer_definition->declaration;
169
170 return integer_declaration->signedness;
171}
172
fe2b0e24 173uint64_t bt_get_unsigned_int(const struct definition *field)
343c801f
JD
174{
175 struct definition_integer *integer_definition;
176 const struct declaration_integer *integer_declaration;
177
178 integer_definition = container_of(field, struct definition_integer, p);
179 integer_declaration = integer_definition->declaration;
180
181 if (!integer_declaration->signedness) {
182 return integer_definition->value._unsigned;
183 }
1f7b01c2 184 fprintf(stderr, "[warning] Extracting unsigned value from a signed int (%s)\n",
5c6cab21 185 g_quark_to_string(field->name));
343c801f
JD
186 return (uint64_t)integer_definition->value._signed;
187}
188
fe2b0e24 189int64_t bt_get_signed_int(const struct definition *field)
343c801f
JD
190{
191 struct definition_integer *integer_definition;
192 const struct declaration_integer *integer_declaration;
193
194 integer_definition = container_of(field, struct definition_integer, p);
195 integer_declaration = integer_definition->declaration;
196
197 if (integer_declaration->signedness) {
198 return integer_definition->value._signed;
199 }
1f7b01c2 200 fprintf(stderr, "[warning] Extracting signed value from an unsigned int (%s)\n",
5c6cab21 201 g_quark_to_string(field->name));
343c801f
JD
202 return (int64_t)integer_definition->value._unsigned;
203}
This page took 0.035345 seconds and 4 git commands to generate.