| 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 | |
| 22 | static |
| 23 | struct type_float *_float_type_new(struct type_class *type_class, |
| 24 | struct declaration_scope *parent_scope); |
| 25 | static |
| 26 | void _float_type_free(struct type *type); |
| 27 | |
| 28 | void float_copy(struct stream_pos *destp, |
| 29 | const struct format *fdest, |
| 30 | struct stream_pos *srcp, |
| 31 | const struct format *fsrc, |
| 32 | struct type *type) |
| 33 | { |
| 34 | struct type_float *_float = container_of(type, struct type_float, p); |
| 35 | struct type_class_float *float_class = _float->_class; |
| 36 | |
| 37 | if (fsrc->float_copy == fdest->float_copy) { |
| 38 | fsrc->float_copy(destp, srcp, float_class); |
| 39 | } else { |
| 40 | double v; |
| 41 | |
| 42 | v = fsrc->double_read(srcp, float_class); |
| 43 | fdest->double_write(destp, float_class, v); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static |
| 48 | void _float_type_class_free(struct type_class *type_class) |
| 49 | { |
| 50 | struct type_class_float *float_class = |
| 51 | container_of(type_class, struct type_class_float, p); |
| 52 | |
| 53 | type_class_unref(&float_class->exp->p); |
| 54 | type_class_unref(&float_class->mantissa->p); |
| 55 | type_class_unref(&float_class->sign->p); |
| 56 | g_free(float_class); |
| 57 | } |
| 58 | |
| 59 | struct type_class_float * |
| 60 | float_type_class_new(const char *name, size_t mantissa_len, |
| 61 | size_t exp_len, int byte_order, size_t alignment) |
| 62 | { |
| 63 | struct type_class_float *float_class; |
| 64 | struct type_class *type_class; |
| 65 | int ret; |
| 66 | |
| 67 | float_class = g_new(struct type_class_float, 1); |
| 68 | type_class = &float_class->p; |
| 69 | type_class->name = g_quark_from_string(name); |
| 70 | type_class->alignment = alignment; |
| 71 | type_class->copy = float_copy; |
| 72 | type_class->class_free = _float_type_class_free; |
| 73 | type_class->type_new = _float_type_new; |
| 74 | type_class->type_free = _float_type_free; |
| 75 | type_class->ref = 1; |
| 76 | float_class->byte_order = byte_order; |
| 77 | |
| 78 | float_class->sign = integer_type_new(NULL, 1, |
| 79 | byte_order, false, 1); |
| 80 | if (!float_class->mantissa) |
| 81 | goto error_sign; |
| 82 | float_class->mantissa = integer_type_new(NULL, mantissa_len - 1, |
| 83 | byte_order, false, 1); |
| 84 | if (!float_class->mantissa) |
| 85 | goto error_mantissa; |
| 86 | float_class->exp = integer_type_new(NULL, exp_len, |
| 87 | byte_order, true, 1); |
| 88 | if (!float_class->exp) |
| 89 | goto error_exp; |
| 90 | |
| 91 | if (float_class->p.name) { |
| 92 | ret = register_type(&float_class->p); |
| 93 | if (ret) |
| 94 | goto error_register; |
| 95 | } |
| 96 | return float_class; |
| 97 | |
| 98 | error_register: |
| 99 | type_class_unref(&float_class->exp->p); |
| 100 | error_exp: |
| 101 | type_class_unref(&float_class->mantissa->p); |
| 102 | error_mantissa: |
| 103 | type_class_unref(&float_class->sign->p); |
| 104 | error_sign: |
| 105 | g_free(float_class); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | struct type_float *_float_type_new(struct type_class *type_class, |
| 111 | struct declaration_scope *parent_scope) |
| 112 | { |
| 113 | struct type_class_float *float_class = |
| 114 | container_of(type_class, struct type_class_float, p); |
| 115 | struct type_float *_float; |
| 116 | |
| 117 | _float = g_new(struct type_float, 1); |
| 118 | type_class_ref(&_float_class->p); |
| 119 | _float->p._class = _float_class; |
| 120 | _float->p.ref = 1; |
| 121 | _float->value = 0.0; |
| 122 | return &_float->p; |
| 123 | } |
| 124 | |
| 125 | static |
| 126 | void _float_type_free(struct type *type) |
| 127 | { |
| 128 | struct type_float *_float = container_of(type, struct type_float, p); |
| 129 | |
| 130 | type_class_unref(_float->p._class); |
| 131 | g_free(_float); |
| 132 | } |