namespace the declaration functions
[babeltrace.git] / types / float.c
1 /*
2 * float.c
3 *
4 * BabelTrace - Float 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/format.h>
31 #include <babeltrace/types.h>
32 #include <babeltrace/endian.h>
33
34 static
35 struct definition *_float_definition_new(struct declaration *declaration,
36 struct definition_scope *parent_scope,
37 GQuark field_name, int index,
38 const char *root_name);
39 static
40 void _float_definition_free(struct definition *definition);
41
42 static
43 void _float_declaration_free(struct declaration *declaration)
44 {
45 struct declaration_float *float_declaration =
46 container_of(declaration, struct declaration_float, p);
47
48 bt_declaration_unref(&float_declaration->exp->p);
49 bt_declaration_unref(&float_declaration->mantissa->p);
50 bt_declaration_unref(&float_declaration->sign->p);
51 g_free(float_declaration);
52 }
53
54 struct declaration_float *
55 bt_float_declaration_new(size_t mantissa_len,
56 size_t exp_len, int byte_order, size_t alignment)
57 {
58 struct declaration_float *float_declaration;
59 struct declaration *declaration;
60
61 float_declaration = g_new(struct declaration_float, 1);
62 declaration = &float_declaration->p;
63 declaration->id = CTF_TYPE_FLOAT;
64 declaration->alignment = alignment;
65 declaration->declaration_free = _float_declaration_free;
66 declaration->definition_new = _float_definition_new;
67 declaration->definition_free = _float_definition_free;
68 declaration->ref = 1;
69 float_declaration->byte_order = byte_order;
70
71 float_declaration->sign = bt_integer_declaration_new(1,
72 byte_order, false, 1, 2,
73 CTF_STRING_NONE, NULL);
74 float_declaration->mantissa = bt_integer_declaration_new(mantissa_len - 1,
75 byte_order, false, 1, 10,
76 CTF_STRING_NONE, NULL);
77 float_declaration->exp = bt_integer_declaration_new(exp_len,
78 byte_order, true, 1, 10,
79 CTF_STRING_NONE, NULL);
80 return float_declaration;
81 }
82
83 static
84 struct definition *
85 _float_definition_new(struct declaration *declaration,
86 struct definition_scope *parent_scope,
87 GQuark field_name, int index,
88 const char *root_name)
89 {
90 struct declaration_float *float_declaration =
91 container_of(declaration, struct declaration_float, p);
92 struct definition_float *_float;
93 struct definition *tmp;
94
95 _float = g_new(struct definition_float, 1);
96 bt_declaration_ref(&float_declaration->p);
97 _float->p.declaration = declaration;
98 _float->declaration = float_declaration;
99 _float->p.scope = new_definition_scope(parent_scope, field_name, root_name);
100 _float->p.path = new_definition_path(parent_scope, field_name, root_name);
101 if (float_declaration->byte_order == LITTLE_ENDIAN) {
102 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
103 _float->p.scope, g_quark_from_static_string("mantissa"), 0, NULL);
104 _float->mantissa = container_of(tmp, struct definition_integer, p);
105 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
106 _float->p.scope, g_quark_from_static_string("exp"), 1, NULL);
107 _float->exp = container_of(tmp, struct definition_integer, p);
108 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
109 _float->p.scope, g_quark_from_static_string("sign"), 2, NULL);
110 _float->sign = container_of(tmp, struct definition_integer, p);
111 } else {
112 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
113 _float->p.scope, g_quark_from_static_string("sign"), 0, NULL);
114 _float->sign = container_of(tmp, struct definition_integer, p);
115 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
116 _float->p.scope, g_quark_from_static_string("exp"), 1, NULL);
117 _float->exp = container_of(tmp, struct definition_integer, p);
118 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
119 _float->p.scope, g_quark_from_static_string("mantissa"), 2, NULL);
120 _float->mantissa = container_of(tmp, struct definition_integer, p);
121 }
122 _float->p.ref = 1;
123 /*
124 * Use INT_MAX order to ensure that all fields of the parent
125 * scope are seen as being prior to this scope.
126 */
127 _float->p.index = root_name ? INT_MAX : index;
128 _float->p.name = field_name;
129 _float->value = 0.0;
130 if (parent_scope) {
131 int ret;
132
133 ret = register_field_definition(field_name, &_float->p,
134 parent_scope);
135 assert(!ret);
136 }
137 return &_float->p;
138 }
139
140 static
141 void _float_definition_free(struct definition *definition)
142 {
143 struct definition_float *_float =
144 container_of(definition, struct definition_float, p);
145
146 bt_definition_unref(&_float->sign->p);
147 bt_definition_unref(&_float->exp->p);
148 bt_definition_unref(&_float->mantissa->p);
149 free_definition_scope(_float->p.scope);
150 bt_declaration_unref(_float->p.declaration);
151 g_free(_float);
152 }
This page took 0.033347 seconds and 5 git commands to generate.