| 1 | /* |
| 2 | * BabelTrace - Float Type Converter |
| 3 | * |
| 4 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/compiler.h> |
| 22 | |
| 23 | size_t float_copy(unsigned char *dest, const struct format *fdest, |
| 24 | const unsigned char *src, const struct format *fsrc, |
| 25 | const struct type_class *type_class) |
| 26 | { |
| 27 | struct type_class_float *float_class = |
| 28 | container_of(type_class, struct type_class_float, p); |
| 29 | |
| 30 | if (fsrc->float_copy == fdest->float_copy) { |
| 31 | fsrc->float_copy(dest, float_class, src, float_class); |
| 32 | return float_class->mantissa_len + float_class->exp_len; |
| 33 | } else { |
| 34 | double v; |
| 35 | |
| 36 | v = fsrc->double_read(src, fsrc); |
| 37 | return fdest->double_write(dest, fdest, v); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | struct type_class_float *float_type_new(const char *name, |
| 42 | size_t mantissa_len, |
| 43 | size_t exp_len, int byte_order, |
| 44 | size_t alignment) |
| 45 | { |
| 46 | struct type_class_float *float_class; |
| 47 | int ret; |
| 48 | |
| 49 | /* |
| 50 | * Freed when type is unregistered. |
| 51 | */ |
| 52 | float_class = g_new(struct type_class_float, 1); |
| 53 | float_class->p.name = g_quark_from_string(name); |
| 54 | float_class->p.alignment = alignment; |
| 55 | float_class->mantissa_len = mantissa_len; |
| 56 | float_class->exp_len = exp_len; |
| 57 | float_class->byte_order = byte_order; |
| 58 | if (float_class->p.name) { |
| 59 | ret = ctf_register_type(&float_class->p); |
| 60 | if (ret) { |
| 61 | g_free(float_class); |
| 62 | return NULL; |
| 63 | } |
| 64 | } |
| 65 | return float_class; |
| 66 | } |
| 67 | |
| 68 | void float_type_free(struct type_class_float *float_class) |
| 69 | { |
| 70 | if (!float_class->name) |
| 71 | g_free(float_class); |
| 72 | } |