Commit | Line | Data |
---|---|---|
0a46062b | 1 | /* |
ccd7e1c8 | 2 | * float.c |
0a46062b | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Float Type Converter |
0a46062b | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
0a46062b | 7 | * |
ccd7e1c8 MD |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
0a46062b | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
0a46062b MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
0a46062b | 21 | |
c054553d | 22 | static |
f6625916 | 23 | struct definition *_float_definition_new(struct declaration *declaration, |
05c749e5 MD |
24 | struct definition_scope *parent_scope, |
25 | GQuark field_name, int index); | |
c054553d | 26 | static |
e1151715 | 27 | void _float_definition_free(struct definition *definition); |
c054553d | 28 | |
c054553d | 29 | static |
f6625916 | 30 | void _float_declaration_free(struct declaration *declaration) |
90b676d7 | 31 | { |
f6625916 MD |
32 | struct declaration_float *float_declaration = |
33 | container_of(declaration, struct declaration_float, p); | |
c054553d | 34 | |
f6625916 MD |
35 | declaration_unref(&float_declaration->exp->p); |
36 | declaration_unref(&float_declaration->mantissa->p); | |
37 | declaration_unref(&float_declaration->sign->p); | |
38 | g_free(float_declaration); | |
90b676d7 MD |
39 | } |
40 | ||
f6625916 | 41 | struct declaration_float * |
add40b62 | 42 | float_declaration_new(size_t mantissa_len, |
e19c3d69 | 43 | size_t exp_len, int byte_order, size_t alignment) |
0a46062b | 44 | { |
f6625916 MD |
45 | struct declaration_float *float_declaration; |
46 | struct declaration *declaration; | |
0a46062b | 47 | |
f6625916 MD |
48 | float_declaration = g_new(struct declaration_float, 1); |
49 | declaration = &float_declaration->p; | |
50 | declaration->id = CTF_TYPE_FLOAT; | |
f6625916 | 51 | declaration->alignment = alignment; |
f6625916 MD |
52 | declaration->declaration_free = _float_declaration_free; |
53 | declaration->definition_new = _float_definition_new; | |
54 | declaration->definition_free = _float_definition_free; | |
55 | declaration->ref = 1; | |
56 | float_declaration->byte_order = byte_order; | |
e19c3d69 | 57 | |
add40b62 | 58 | float_declaration->sign = integer_declaration_new(1, |
e19c3d69 | 59 | byte_order, false, 1); |
add40b62 | 60 | float_declaration->mantissa = integer_declaration_new(mantissa_len - 1, |
e19c3d69 | 61 | byte_order, false, 1); |
add40b62 | 62 | float_declaration->exp = integer_declaration_new(exp_len, |
e19c3d69 | 63 | byte_order, true, 1); |
f6625916 | 64 | return float_declaration; |
0a46062b | 65 | } |
c054553d MD |
66 | |
67 | static | |
e1151715 | 68 | struct definition * |
f6625916 | 69 | _float_definition_new(struct declaration *declaration, |
05c749e5 MD |
70 | struct definition_scope *parent_scope, |
71 | GQuark field_name, int index) | |
c054553d | 72 | { |
f6625916 MD |
73 | struct declaration_float *float_declaration = |
74 | container_of(declaration, struct declaration_float, p); | |
e1151715 | 75 | struct definition_float *_float; |
d11e9c49 | 76 | struct definition *tmp; |
c054553d | 77 | |
e1151715 | 78 | _float = g_new(struct definition_float, 1); |
f6625916 | 79 | declaration_ref(&float_declaration->p); |
05c749e5 MD |
80 | _float->p.declaration = declaration; |
81 | _float->declaration = float_declaration; | |
d11e9c49 MD |
82 | if (float_declaration->byte_order == LITTLE_ENDIAN) { |
83 | tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, | |
84 | parent_scope, g_quark_from_static_string("mantissa"), 0); | |
85 | _float->mantissa = container_of(tmp, struct definition_integer, p); | |
86 | tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, | |
87 | parent_scope, g_quark_from_static_string("exp"), 1); | |
88 | _float->exp = container_of(tmp, struct definition_integer, p); | |
89 | tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, | |
90 | parent_scope, g_quark_from_static_string("sign"), 2); | |
91 | _float->sign = container_of(tmp, struct definition_integer, p); | |
92 | } else { | |
93 | tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p, | |
94 | parent_scope, g_quark_from_static_string("sign"), 0); | |
95 | _float->sign = container_of(tmp, struct definition_integer, p); | |
96 | tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p, | |
97 | parent_scope, g_quark_from_static_string("exp"), 1); | |
98 | _float->exp = container_of(tmp, struct definition_integer, p); | |
99 | tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p, | |
100 | parent_scope, g_quark_from_static_string("mantissa"), 2); | |
101 | _float->mantissa = container_of(tmp, struct definition_integer, p); | |
102 | } | |
c054553d | 103 | _float->p.ref = 1; |
05c749e5 | 104 | _float->p.index = index; |
c054553d MD |
105 | _float->value = 0.0; |
106 | return &_float->p; | |
107 | } | |
108 | ||
109 | static | |
e1151715 | 110 | void _float_definition_free(struct definition *definition) |
c054553d | 111 | { |
e1151715 MD |
112 | struct definition_float *_float = |
113 | container_of(definition, struct definition_float, p); | |
c054553d | 114 | |
d11e9c49 MD |
115 | definition_unref(&_float->sign->p); |
116 | definition_unref(&_float->exp->p); | |
117 | definition_unref(&_float->mantissa->p); | |
f6625916 | 118 | declaration_unref(_float->p.declaration); |
c054553d MD |
119 | g_free(_float); |
120 | } |