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