Commit | Line | Data |
---|---|---|
0a46062b | 1 | /* |
ccd7e1c8 | 2 | * float.c |
0a46062b | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Float Type Converter |
0a46062b | 5 | * |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
0a46062b | 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: | |
0a46062b | 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. | |
0a46062b MD |
27 | */ |
28 | ||
29 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 30 | #include <babeltrace/format.h> |
3122e6f0 | 31 | #include <babeltrace/types.h> |
dd6f7e0f | 32 | #include <babeltrace/endian.h> |
0a46062b | 33 | |
c054553d | 34 | static |
ecc54f11 | 35 | struct bt_definition *_float_definition_new(struct bt_declaration *declaration, |
05c749e5 | 36 | struct definition_scope *parent_scope, |
98df1c9f MD |
37 | GQuark field_name, int index, |
38 | const char *root_name); | |
c054553d | 39 | static |
0d69b916 | 40 | void _float_definition_free(struct bt_definition *definition); |
c054553d | 41 | |
c054553d | 42 | static |
ecc54f11 | 43 | void _float_declaration_free(struct bt_declaration *declaration) |
90b676d7 | 44 | { |
f6625916 MD |
45 | struct declaration_float *float_declaration = |
46 | container_of(declaration, struct declaration_float, p); | |
c054553d | 47 | |
e6b4b4f4 JD |
48 | bt_declaration_unref(&float_declaration->exp->p); |
49 | bt_declaration_unref(&float_declaration->mantissa->p); | |
50 | bt_declaration_unref(&float_declaration->sign->p); | |
f6625916 | 51 | g_free(float_declaration); |
90b676d7 MD |
52 | } |
53 | ||
f6625916 | 54 | struct declaration_float * |
becd02a1 | 55 | bt_float_declaration_new(size_t mantissa_len, |
e19c3d69 | 56 | size_t exp_len, int byte_order, size_t alignment) |
0a46062b | 57 | { |
f6625916 | 58 | struct declaration_float *float_declaration; |
ecc54f11 | 59 | struct bt_declaration *declaration; |
0a46062b | 60 | |
f6625916 MD |
61 | float_declaration = g_new(struct declaration_float, 1); |
62 | declaration = &float_declaration->p; | |
63 | declaration->id = CTF_TYPE_FLOAT; | |
f6625916 | 64 | declaration->alignment = alignment; |
f6625916 MD |
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; | |
e19c3d69 | 70 | |
becd02a1 | 71 | float_declaration->sign = bt_integer_declaration_new(1, |
81dee1bb | 72 | byte_order, false, 1, 2, |
56e60373 | 73 | CTF_STRING_NONE, NULL); |
becd02a1 | 74 | float_declaration->mantissa = bt_integer_declaration_new(mantissa_len - 1, |
81dee1bb | 75 | byte_order, false, 1, 10, |
56e60373 | 76 | CTF_STRING_NONE, NULL); |
becd02a1 | 77 | float_declaration->exp = bt_integer_declaration_new(exp_len, |
81dee1bb | 78 | byte_order, true, 1, 10, |
56e60373 | 79 | CTF_STRING_NONE, NULL); |
f6625916 | 80 | return float_declaration; |
0a46062b | 81 | } |
c054553d MD |
82 | |
83 | static | |
0d69b916 | 84 | struct bt_definition * |
ecc54f11 | 85 | _float_definition_new(struct bt_declaration *declaration, |
05c749e5 | 86 | struct definition_scope *parent_scope, |
98df1c9f MD |
87 | GQuark field_name, int index, |
88 | const char *root_name) | |
c054553d | 89 | { |
f6625916 MD |
90 | struct declaration_float *float_declaration = |
91 | container_of(declaration, struct declaration_float, p); | |
e1151715 | 92 | struct definition_float *_float; |
0d69b916 | 93 | struct bt_definition *tmp; |
c054553d | 94 | |
e1151715 | 95 | _float = g_new(struct definition_float, 1); |
e6b4b4f4 | 96 | bt_declaration_ref(&float_declaration->p); |
05c749e5 MD |
97 | _float->p.declaration = declaration; |
98 | _float->declaration = float_declaration; | |
2b77e6a6 JD |
99 | _float->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name); |
100 | _float->p.path = bt_new_definition_path(parent_scope, field_name, root_name); | |
d11e9c49 MD |
101 | if (float_declaration->byte_order == LITTLE_ENDIAN) { |
102 | tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, | |
2ea5cc8b | 103 | _float->p.scope, g_quark_from_string("mantissa"), 0, NULL); |
d11e9c49 MD |
104 | _float->mantissa = container_of(tmp, struct definition_integer, p); |
105 | tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, | |
2ea5cc8b | 106 | _float->p.scope, g_quark_from_string("exp"), 1, NULL); |
d11e9c49 MD |
107 | _float->exp = container_of(tmp, struct definition_integer, p); |
108 | tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, | |
2ea5cc8b | 109 | _float->p.scope, g_quark_from_string("sign"), 2, NULL); |
d11e9c49 MD |
110 | _float->sign = container_of(tmp, struct definition_integer, p); |
111 | } else { | |
112 | tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, | |
2ea5cc8b | 113 | _float->p.scope, g_quark_from_string("sign"), 0, NULL); |
d11e9c49 MD |
114 | _float->sign = container_of(tmp, struct definition_integer, p); |
115 | tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, | |
2ea5cc8b | 116 | _float->p.scope, g_quark_from_string("exp"), 1, NULL); |
d11e9c49 MD |
117 | _float->exp = container_of(tmp, struct definition_integer, p); |
118 | tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, | |
2ea5cc8b | 119 | _float->p.scope, g_quark_from_string("mantissa"), 2, NULL); |
d11e9c49 MD |
120 | _float->mantissa = container_of(tmp, struct definition_integer, p); |
121 | } | |
c054553d | 122 | _float->p.ref = 1; |
98df1c9f MD |
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; | |
b1a2f580 | 128 | _float->p.name = field_name; |
c054553d | 129 | _float->value = 0.0; |
f72803ca | 130 | if (parent_scope) { |
08c82b90 MD |
131 | int ret; |
132 | ||
2b77e6a6 | 133 | ret = bt_register_field_definition(field_name, &_float->p, |
f72803ca MD |
134 | parent_scope); |
135 | assert(!ret); | |
136 | } | |
c054553d MD |
137 | return &_float->p; |
138 | } | |
139 | ||
140 | static | |
0d69b916 | 141 | void _float_definition_free(struct bt_definition *definition) |
c054553d | 142 | { |
e1151715 MD |
143 | struct definition_float *_float = |
144 | container_of(definition, struct definition_float, p); | |
c054553d | 145 | |
13fad8b6 JD |
146 | bt_definition_unref(&_float->sign->p); |
147 | bt_definition_unref(&_float->exp->p); | |
148 | bt_definition_unref(&_float->mantissa->p); | |
2b77e6a6 | 149 | bt_free_definition_scope(_float->p.scope); |
e6b4b4f4 | 150 | bt_declaration_unref(_float->p.declaration); |
c054553d MD |
151 | g_free(_float); |
152 | } |