Create copy of integer declaration before applying base-16 for pointers
[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
MD
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
c054553d 27static
e1151715 28void _float_definition_free(struct definition *definition);
c054553d 29
c054553d 30static
f6625916 31void _float_declaration_free(struct declaration *declaration)
90b676d7 32{
f6625916
MD
33 struct declaration_float *float_declaration =
34 container_of(declaration, struct declaration_float, p);
c054553d 35
f6625916
MD
36 declaration_unref(&float_declaration->exp->p);
37 declaration_unref(&float_declaration->mantissa->p);
38 declaration_unref(&float_declaration->sign->p);
39 g_free(float_declaration);
90b676d7
MD
40}
41
f6625916 42struct declaration_float *
add40b62 43 float_declaration_new(size_t mantissa_len,
e19c3d69 44 size_t exp_len, int byte_order, size_t alignment)
0a46062b 45{
f6625916
MD
46 struct declaration_float *float_declaration;
47 struct declaration *declaration;
0a46062b 48
f6625916
MD
49 float_declaration = g_new(struct declaration_float, 1);
50 declaration = &float_declaration->p;
51 declaration->id = CTF_TYPE_FLOAT;
f6625916 52 declaration->alignment = alignment;
f6625916
MD
53 declaration->declaration_free = _float_declaration_free;
54 declaration->definition_new = _float_definition_new;
55 declaration->definition_free = _float_definition_free;
56 declaration->ref = 1;
57 float_declaration->byte_order = byte_order;
e19c3d69 58
add40b62 59 float_declaration->sign = integer_declaration_new(1,
164078da 60 byte_order, false, 1, 2);
add40b62 61 float_declaration->mantissa = integer_declaration_new(mantissa_len - 1,
164078da 62 byte_order, false, 1, 10);
add40b62 63 float_declaration->exp = integer_declaration_new(exp_len,
164078da 64 byte_order, true, 1, 10);
f6625916 65 return float_declaration;
0a46062b 66}
c054553d
MD
67
68static
e1151715 69struct definition *
f6625916 70 _float_definition_new(struct declaration *declaration,
05c749e5
MD
71 struct definition_scope *parent_scope,
72 GQuark field_name, int index)
c054553d 73{
f6625916
MD
74 struct declaration_float *float_declaration =
75 container_of(declaration, struct declaration_float, p);
e1151715 76 struct definition_float *_float;
d11e9c49 77 struct definition *tmp;
c054553d 78
e1151715 79 _float = g_new(struct definition_float, 1);
f6625916 80 declaration_ref(&float_declaration->p);
05c749e5
MD
81 _float->p.declaration = declaration;
82 _float->declaration = float_declaration;
d11e9c49
MD
83 if (float_declaration->byte_order == LITTLE_ENDIAN) {
84 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
85 parent_scope, g_quark_from_static_string("mantissa"), 0);
86 _float->mantissa = container_of(tmp, struct definition_integer, p);
87 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
88 parent_scope, g_quark_from_static_string("exp"), 1);
89 _float->exp = container_of(tmp, struct definition_integer, p);
90 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
91 parent_scope, g_quark_from_static_string("sign"), 2);
92 _float->sign = container_of(tmp, struct definition_integer, p);
93 } else {
94 tmp = float_declaration->sign->p.definition_new(&float_declaration->sign->p,
95 parent_scope, g_quark_from_static_string("sign"), 0);
96 _float->sign = container_of(tmp, struct definition_integer, p);
97 tmp = float_declaration->exp->p.definition_new(&float_declaration->exp->p,
98 parent_scope, g_quark_from_static_string("exp"), 1);
99 _float->exp = container_of(tmp, struct definition_integer, p);
100 tmp = float_declaration->mantissa->p.definition_new(&float_declaration->mantissa->p,
101 parent_scope, g_quark_from_static_string("mantissa"), 2);
102 _float->mantissa = container_of(tmp, struct definition_integer, p);
103 }
c054553d 104 _float->p.ref = 1;
05c749e5 105 _float->p.index = index;
b1a2f580 106 _float->p.name = field_name;
31262354 107 _float->p.path = new_definition_path(parent_scope, field_name);
c054553d
MD
108 _float->value = 0.0;
109 return &_float->p;
110}
111
112static
e1151715 113void _float_definition_free(struct definition *definition)
c054553d 114{
e1151715
MD
115 struct definition_float *_float =
116 container_of(definition, struct definition_float, p);
c054553d 117
d11e9c49
MD
118 definition_unref(&_float->sign->p);
119 definition_unref(&_float->exp->p);
120 definition_unref(&_float->mantissa->p);
f6625916 121 declaration_unref(_float->p.declaration);
c054553d
MD
122 g_free(_float);
123}
This page took 0.028405 seconds and 4 git commands to generate.