Fix: --clock-force-correlate to handle trace collections gathered from various nodes
[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.
fc93b2bd
MD
19 */
20
21#include <babeltrace/compiler.h>
698f0fe4 22#include <babeltrace/align.h>
4c8bfb7e 23#include <babeltrace/format.h>
3122e6f0 24#include <babeltrace/types.h>
fc93b2bd
MD
25#include <stdint.h>
26
c054553d 27static
f6625916 28struct definition *_integer_definition_new(struct declaration *declaration,
05c749e5 29 struct definition_scope *parent_scope,
98df1c9f
MD
30 GQuark field_name, int index,
31 const char *root_name);
c054553d 32static
e1151715 33void _integer_definition_free(struct definition *definition);
c054553d 34
c054553d 35static
f6625916 36void _integer_declaration_free(struct declaration *declaration)
90b676d7 37{
f6625916
MD
38 struct declaration_integer *integer_declaration =
39 container_of(declaration, struct declaration_integer, p);
40 g_free(integer_declaration);
90b676d7
MD
41}
42
f6625916 43struct declaration_integer *
add40b62 44 integer_declaration_new(size_t len, int byte_order,
81dee1bb 45 int signedness, size_t alignment, int base,
56e60373
MD
46 enum ctf_string_encoding encoding,
47 struct ctf_clock *clock)
698f0fe4 48{
f6625916 49 struct declaration_integer *integer_declaration;
698f0fe4 50
f6625916
MD
51 integer_declaration = g_new(struct declaration_integer, 1);
52 integer_declaration->p.id = CTF_TYPE_INTEGER;
f6625916 53 integer_declaration->p.alignment = alignment;
f6625916
MD
54 integer_declaration->p.declaration_free = _integer_declaration_free;
55 integer_declaration->p.definition_free = _integer_definition_free;
56 integer_declaration->p.definition_new = _integer_definition_new;
57 integer_declaration->p.ref = 1;
58 integer_declaration->len = len;
59 integer_declaration->byte_order = byte_order;
60 integer_declaration->signedness = signedness;
164078da 61 integer_declaration->base = base;
81dee1bb 62 integer_declaration->encoding = encoding;
56e60373 63 integer_declaration->clock = clock;
f6625916 64 return integer_declaration;
698f0fe4 65}
c054553d
MD
66
67static
e1151715 68struct definition *
f6625916 69 _integer_definition_new(struct declaration *declaration,
05c749e5 70 struct definition_scope *parent_scope,
98df1c9f
MD
71 GQuark field_name, int index,
72 const char *root_name)
c054553d 73{
f6625916
MD
74 struct declaration_integer *integer_declaration =
75 container_of(declaration, struct declaration_integer, p);
e1151715 76 struct definition_integer *integer;
98df1c9f 77 int ret;
c054553d 78
e1151715 79 integer = g_new(struct definition_integer, 1);
f6625916
MD
80 declaration_ref(&integer_declaration->p);
81 integer->p.declaration = declaration;
82 integer->declaration = integer_declaration;
c054553d 83 integer->p.ref = 1;
98df1c9f
MD
84 /*
85 * Use INT_MAX order to ensure that all fields of the parent
86 * scope are seen as being prior to this scope.
87 */
88 integer->p.index = root_name ? INT_MAX : index;
b1a2f580 89 integer->p.name = field_name;
98df1c9f
MD
90 integer->p.path = new_definition_path(parent_scope, field_name,
91 root_name);
a35173fe 92 integer->p.scope = NULL;
c054553d 93 integer->value._unsigned = 0;
98df1c9f
MD
94 ret = register_field_definition(field_name, &integer->p,
95 parent_scope);
96 assert(!ret);
c054553d
MD
97 return &integer->p;
98}
99
100static
e1151715 101void _integer_definition_free(struct definition *definition)
c054553d 102{
e1151715
MD
103 struct definition_integer *integer =
104 container_of(definition, struct definition_integer, p);
c054553d 105
f6625916 106 declaration_unref(integer->p.declaration);
c054553d
MD
107 g_free(integer);
108}
343c801f 109
8673030f
JD
110enum ctf_string_encoding get_int_encoding(const struct definition *field)
111{
112 struct definition_integer *integer_definition;
113 const struct declaration_integer *integer_declaration;
114
115 integer_definition = container_of(field, struct definition_integer, p);
116 integer_declaration = integer_definition->declaration;
117
118 return integer_declaration->encoding;
119}
120
121int get_int_base(const struct definition *field)
122{
123 struct definition_integer *integer_definition;
124 const struct declaration_integer *integer_declaration;
125
126 integer_definition = container_of(field, struct definition_integer, p);
127 integer_declaration = integer_definition->declaration;
128
129 return integer_declaration->base;
130}
131
fef0e521
MD
132size_t get_int_len(const struct definition *field)
133{
134 struct definition_integer *integer_definition;
135 const struct declaration_integer *integer_declaration;
136
137 integer_definition = container_of(field, struct definition_integer, p);
138 integer_declaration = integer_definition->declaration;
139
140 return integer_declaration->len;
141}
142
8673030f
JD
143int get_int_byte_order(const struct definition *field)
144{
145 struct definition_integer *integer_definition;
146 const struct declaration_integer *integer_declaration;
147
148 integer_definition = container_of(field, struct definition_integer, p);
149 integer_declaration = integer_definition->declaration;
150
151 return integer_declaration->byte_order;
152}
153
154int get_int_signedness(const struct definition *field)
155{
156 struct definition_integer *integer_definition;
157 const struct declaration_integer *integer_declaration;
158
159 integer_definition = container_of(field, struct definition_integer, p);
160 integer_declaration = integer_definition->declaration;
161
162 return integer_declaration->signedness;
163}
164
da320b83 165uint64_t get_unsigned_int(const struct definition *field)
343c801f
JD
166{
167 struct definition_integer *integer_definition;
168 const struct declaration_integer *integer_declaration;
169
170 integer_definition = container_of(field, struct definition_integer, p);
171 integer_declaration = integer_definition->declaration;
172
173 if (!integer_declaration->signedness) {
174 return integer_definition->value._unsigned;
175 }
1f7b01c2 176 fprintf(stderr, "[warning] Extracting unsigned value from a signed int (%s)\n",
5c6cab21 177 g_quark_to_string(field->name));
343c801f
JD
178 return (uint64_t)integer_definition->value._signed;
179}
180
da320b83 181int64_t get_signed_int(const struct definition *field)
343c801f
JD
182{
183 struct definition_integer *integer_definition;
184 const struct declaration_integer *integer_declaration;
185
186 integer_definition = container_of(field, struct definition_integer, p);
187 integer_declaration = integer_definition->declaration;
188
189 if (integer_declaration->signedness) {
190 return integer_definition->value._signed;
191 }
1f7b01c2 192 fprintf(stderr, "[warning] Extracting signed value from an unsigned int (%s)\n",
5c6cab21 193 g_quark_to_string(field->name));
343c801f
JD
194 return (int64_t)integer_definition->value._unsigned;
195}
This page took 0.056704 seconds and 4 git commands to generate.