Packet metadata read should substract header size
[babeltrace.git] / types / float.c
1 /*
2 * float.c
3 *
4 * BabelTrace - Float Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21 #include <endian.h>
22
23 static
24 struct definition *_float_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index,
27 const char *root_name);
28 static
29 void _float_definition_free(struct definition *definition);
30
31 static
32 void _float_declaration_free(struct declaration *declaration)
33 {
34 struct declaration_float *float_declaration =
35 container_of(declaration, struct declaration_float, p);
36
37 declaration_unref(&float_declaration->exp->p);
38 declaration_unref(&float_declaration->mantissa->p);
39 declaration_unref(&float_declaration->sign->p);
40 g_free(float_declaration);
41 }
42
43 struct declaration_float *
44 float_declaration_new(size_t mantissa_len,
45 size_t exp_len, int byte_order, size_t alignment)
46 {
47 struct declaration_float *float_declaration;
48 struct declaration *declaration;
49
50 float_declaration = g_new(struct declaration_float, 1);
51 declaration = &float_declaration->p;
52 declaration->id = CTF_TYPE_FLOAT;
53 declaration->alignment = alignment;
54 declaration->declaration_free = _float_declaration_free;
55 declaration->definition_new = _float_definition_new;
56 declaration->definition_free = _float_definition_free;
57 declaration->ref = 1;
58 float_declaration->byte_order = byte_order;
59
60 float_declaration->sign = integer_declaration_new(1,
61 byte_order, false, 1, 2,
62 CTF_STRING_NONE);
63 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
64 byte_order, false, 1, 10,
65 CTF_STRING_NONE);
66 float_declaration->exp = integer_declaration_new(exp_len,
67 byte_order, true, 1, 10,
68 CTF_STRING_NONE);
69 return float_declaration;
70 }
71
72 static
73 struct definition *
74 _float_definition_new(struct declaration *declaration,
75 struct definition_scope *parent_scope,
76 GQuark field_name, int index,
77 const char *root_name)
78 {
79 struct declaration_float *float_declaration =
80 container_of(declaration, struct declaration_float, p);
81 struct definition_float *_float;
82 struct definition *tmp;
83 int ret;
84
85 _float = g_new(struct definition_float, 1);
86 declaration_ref(&float_declaration->p);
87 _float->p.declaration = declaration;
88 _float->declaration = float_declaration;
89 _float->p.scope = new_definition_scope(parent_scope, field_name, root_name);
90 _float->p.path = new_definition_path(parent_scope, field_name, root_name);
91 if (float_declaration->byte_order == LITTLE_ENDIAN) {
92 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
93 _float->p.scope, g_quark_from_static_string("mantissa"), 0, NULL);
94 _float->mantissa = container_of(tmp, struct definition_integer, p);
95 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
96 _float->p.scope, g_quark_from_static_string("exp"), 1, NULL);
97 _float->exp = container_of(tmp, struct definition_integer, p);
98 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
99 _float->p.scope, g_quark_from_static_string("sign"), 2, NULL);
100 _float->sign = container_of(tmp, struct definition_integer, p);
101 } else {
102 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
103 _float->p.scope, g_quark_from_static_string("sign"), 0, NULL);
104 _float->sign = 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->mantissa->p.definition_new(&float_declaration->mantissa->p,
109 _float->p.scope, g_quark_from_static_string("mantissa"), 2, NULL);
110 _float->mantissa = container_of(tmp, struct definition_integer, p);
111 }
112 _float->p.ref = 1;
113 /*
114 * Use INT_MAX order to ensure that all fields of the parent
115 * scope are seen as being prior to this scope.
116 */
117 _float->p.index = root_name ? INT_MAX : index;
118 _float->p.name = field_name;
119 _float->value = 0.0;
120 ret = register_field_definition(field_name, &_float->p,
121 parent_scope);
122 assert(!ret);
123 return &_float->p;
124 }
125
126 static
127 void _float_definition_free(struct definition *definition)
128 {
129 struct definition_float *_float =
130 container_of(definition, struct definition_float, p);
131
132 definition_unref(&_float->sign->p);
133 definition_unref(&_float->exp->p);
134 definition_unref(&_float->mantissa->p);
135 free_definition_scope(_float->p.scope);
136 declaration_unref(_float->p.declaration);
137 g_free(_float);
138 }
This page took 0.031598 seconds and 4 git commands to generate.