Fix babeltrace-log incorrect timestamp type
[babeltrace.git] / types / float.c
CommitLineData
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>
809cbff5 21#include <endian.h>
0a46062b 22
c054553d 23static
f6625916 24struct definition *_float_definition_new(struct declaration *declaration,
05c749e5 25 struct definition_scope *parent_scope,
98df1c9f
MD
26 GQuark field_name, int index,
27 const char *root_name);
c054553d 28static
e1151715 29void _float_definition_free(struct definition *definition);
c054553d 30
c054553d 31static
f6625916 32void _float_declaration_free(struct declaration *declaration)
90b676d7 33{
f6625916
MD
34 struct declaration_float *float_declaration =
35 container_of(declaration, struct declaration_float, p);
c054553d 36
f6625916
MD
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);
90b676d7
MD
41}
42
f6625916 43struct declaration_float *
add40b62 44 float_declaration_new(size_t mantissa_len,
e19c3d69 45 size_t exp_len, int byte_order, size_t alignment)
0a46062b 46{
f6625916
MD
47 struct declaration_float *float_declaration;
48 struct declaration *declaration;
0a46062b 49
f6625916
MD
50 float_declaration = g_new(struct declaration_float, 1);
51 declaration = &float_declaration->p;
52 declaration->id = CTF_TYPE_FLOAT;
f6625916 53 declaration->alignment = alignment;
f6625916
MD
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;
e19c3d69 59
add40b62 60 float_declaration->sign = integer_declaration_new(1,
81dee1bb
MD
61 byte_order, false, 1, 2,
62 CTF_STRING_NONE);
add40b62 63 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
81dee1bb
MD
64 byte_order, false, 1, 10,
65 CTF_STRING_NONE);
add40b62 66 float_declaration->exp = integer_declaration_new(exp_len,
81dee1bb
MD
67 byte_order, true, 1, 10,
68 CTF_STRING_NONE);
f6625916 69 return float_declaration;
0a46062b 70}
c054553d
MD
71
72static
e1151715 73struct definition *
f6625916 74 _float_definition_new(struct declaration *declaration,
05c749e5 75 struct definition_scope *parent_scope,
98df1c9f
MD
76 GQuark field_name, int index,
77 const char *root_name)
c054553d 78{
f6625916
MD
79 struct declaration_float *float_declaration =
80 container_of(declaration, struct declaration_float, p);
e1151715 81 struct definition_float *_float;
d11e9c49 82 struct definition *tmp;
98df1c9f 83 int ret;
c054553d 84
e1151715 85 _float = g_new(struct definition_float, 1);
f6625916 86 declaration_ref(&float_declaration->p);
05c749e5
MD
87 _float->p.declaration = declaration;
88 _float->declaration = float_declaration;
a35173fe 89 _float->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f 90 _float->p.path = new_definition_path(parent_scope, field_name, root_name);
d11e9c49
MD
91 if (float_declaration->byte_order == LITTLE_ENDIAN) {
92 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
a35173fe 93 _float->p.scope, g_quark_from_static_string("mantissa"), 0, NULL);
d11e9c49
MD
94 _float->mantissa = container_of(tmp, struct definition_integer, p);
95 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
a35173fe 96 _float->p.scope, g_quark_from_static_string("exp"), 1, NULL);
d11e9c49
MD
97 _float->exp = container_of(tmp, struct definition_integer, p);
98 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
a35173fe 99 _float->p.scope, g_quark_from_static_string("sign"), 2, NULL);
d11e9c49
MD
100 _float->sign = container_of(tmp, struct definition_integer, p);
101 } else {
102 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
a35173fe 103 _float->p.scope, g_quark_from_static_string("sign"), 0, NULL);
d11e9c49
MD
104 _float->sign = container_of(tmp, struct definition_integer, p);
105 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
a35173fe 106 _float->p.scope, g_quark_from_static_string("exp"), 1, NULL);
d11e9c49
MD
107 _float->exp = container_of(tmp, struct definition_integer, p);
108 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
a35173fe 109 _float->p.scope, g_quark_from_static_string("mantissa"), 2, NULL);
d11e9c49
MD
110 _float->mantissa = container_of(tmp, struct definition_integer, p);
111 }
c054553d 112 _float->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 118 _float->p.name = field_name;
c054553d 119 _float->value = 0.0;
98df1c9f
MD
120 ret = register_field_definition(field_name, &_float->p,
121 parent_scope);
122 assert(!ret);
c054553d
MD
123 return &_float->p;
124}
125
126static
e1151715 127void _float_definition_free(struct definition *definition)
c054553d 128{
e1151715
MD
129 struct definition_float *_float =
130 container_of(definition, struct definition_float, p);
c054553d 131
d11e9c49
MD
132 definition_unref(&_float->sign->p);
133 definition_unref(&_float->exp->p);
134 definition_unref(&_float->mantissa->p);
a35173fe 135 free_definition_scope(_float->p.scope);
f6625916 136 declaration_unref(_float->p.declaration);
c054553d
MD
137 g_free(_float);
138}
This page took 0.029877 seconds and 4 git commands to generate.