Parser test should report AST creation errors
[babeltrace.git] / types / float.c
... / ...
CommitLineData
1/*
2 * float.c
3 *
4 * BabelTrace - Float Type Converter
5 *
6 * Copyright 2010 - 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
22void float_copy(struct stream_pos *destp,
23 const struct format *fdest,
24 struct stream_pos *srcp,
25 const struct format *fsrc,
26 const struct type_class *type_class)
27{
28 struct type_class_float *float_class =
29 container_of(type_class, struct type_class_float, p);
30
31 if (fsrc->float_copy == fdest->float_copy) {
32 fsrc->float_copy(destp, srcp, float_class);
33 } else {
34 double v;
35
36 v = fsrc->double_read(srcp, float_class);
37 fdest->double_write(destp, float_class, v);
38 }
39}
40
41void float_type_free(struct type_class_float *float_class)
42{
43 integer_type_free(float_class->exp);
44 integer_type_free(float_class->mantissa);
45 integer_type_free(float_class->sign);
46 g_free(float_class);
47}
48
49static void _float_type_free(struct type_class *type_class)
50{
51 struct type_class_float *float_class =
52 container_of(type_class, struct type_class_float, p);
53 float_type_free(float_class);
54}
55
56struct type_class_float *float_type_new(const char *name,
57 size_t mantissa_len,
58 size_t exp_len, int byte_order,
59 size_t alignment)
60{
61 struct type_class_float *float_class;
62 struct type_class *type_class;
63 int ret;
64
65 float_class = g_new(struct type_class_float, 1);
66 type_class = &float_class->p;
67
68 type_class->name = g_quark_from_string(name);
69 type_class->alignment = alignment;
70 type_class->copy = float_copy;
71 type_class->free = _float_type_free;
72 type_class->ref = 1;
73 float_class->byte_order = byte_order;
74
75 float_class->sign = integer_type_new(NULL, 1,
76 byte_order, false, 1);
77 if (!float_class->mantissa)
78 goto error_sign;
79 float_class->mantissa = integer_type_new(NULL, mantissa_len - 1,
80 byte_order, false, 1);
81 if (!float_class->mantissa)
82 goto error_mantissa;
83 float_class->exp = integer_type_new(NULL, exp_len,
84 byte_order, true, 1);
85 if (!float_class->exp)
86 goto error_exp;
87
88 if (float_class->p.name) {
89 ret = register_type(&float_class->p);
90 if (ret)
91 goto error_register;
92 }
93 return float_class;
94
95error_register:
96 integer_type_free(float_class->exp);
97error_exp:
98 integer_type_free(float_class->mantissa);
99error_mantissa:
100 integer_type_free(float_class->sign);
101error_sign:
102 g_free(float_class);
103 return NULL;
104}
This page took 0.022501 seconds and 4 git commands to generate.