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