92c7eb11813026256f7646bfc8a996421c28f902
[babeltrace.git] / types / integer.c
1 /*
2 * integer.c
3 *
4 * BabelTrace - Integer Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
27 */
28
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/align.h>
31 #include <babeltrace/format.h>
32 #include <babeltrace/types.h>
33 #include <stdint.h>
34
35 static
36 struct bt_definition *_integer_definition_new(struct declaration *declaration,
37 struct definition_scope *parent_scope,
38 GQuark field_name, int index,
39 const char *root_name);
40 static
41 void _integer_definition_free(struct bt_definition *definition);
42
43 static
44 void _integer_declaration_free(struct declaration *declaration)
45 {
46 struct declaration_integer *integer_declaration =
47 container_of(declaration, struct declaration_integer, p);
48 g_free(integer_declaration);
49 }
50
51 struct declaration_integer *
52 bt_integer_declaration_new(size_t len, int byte_order,
53 int signedness, size_t alignment, int base,
54 enum ctf_string_encoding encoding,
55 struct ctf_clock *clock)
56 {
57 struct declaration_integer *integer_declaration;
58
59 integer_declaration = g_new(struct declaration_integer, 1);
60 integer_declaration->p.id = CTF_TYPE_INTEGER;
61 integer_declaration->p.alignment = alignment;
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;
69 integer_declaration->base = base;
70 integer_declaration->encoding = encoding;
71 integer_declaration->clock = clock;
72 return integer_declaration;
73 }
74
75 static
76 struct bt_definition *
77 _integer_definition_new(struct declaration *declaration,
78 struct definition_scope *parent_scope,
79 GQuark field_name, int index,
80 const char *root_name)
81 {
82 struct declaration_integer *integer_declaration =
83 container_of(declaration, struct declaration_integer, p);
84 struct definition_integer *integer;
85 int ret;
86
87 integer = g_new(struct definition_integer, 1);
88 bt_declaration_ref(&integer_declaration->p);
89 integer->p.declaration = declaration;
90 integer->declaration = integer_declaration;
91 integer->p.ref = 1;
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;
97 integer->p.name = field_name;
98 integer->p.path = bt_new_definition_path(parent_scope, field_name,
99 root_name);
100 integer->p.scope = NULL;
101 integer->value._unsigned = 0;
102 ret = bt_register_field_definition(field_name, &integer->p,
103 parent_scope);
104 assert(!ret);
105 return &integer->p;
106 }
107
108 static
109 void _integer_definition_free(struct bt_definition *definition)
110 {
111 struct definition_integer *integer =
112 container_of(definition, struct definition_integer, p);
113
114 bt_declaration_unref(integer->p.declaration);
115 g_free(integer);
116 }
117
118 enum ctf_string_encoding bt_get_int_encoding(const struct bt_definition *field)
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
129 int bt_get_int_base(const struct bt_definition *field)
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
140 size_t bt_get_int_len(const struct bt_definition *field)
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
151 int bt_get_int_byte_order(const struct bt_definition *field)
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
162 int bt_get_int_signedness(const struct bt_definition *field)
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
173 uint64_t bt_get_unsigned_int(const struct bt_definition *field)
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 }
184 fprintf(stderr, "[warning] Extracting unsigned value from a signed int (%s)\n",
185 g_quark_to_string(field->name));
186 return (uint64_t)integer_definition->value._signed;
187 }
188
189 int64_t bt_get_signed_int(const struct bt_definition *field)
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 }
200 fprintf(stderr, "[warning] Extracting signed value from an unsigned int (%s)\n",
201 g_quark_to_string(field->name));
202 return (int64_t)integer_definition->value._unsigned;
203 }
This page took 0.032432 seconds and 3 git commands to generate.