| 1 | /* |
| 2 | * integer.c |
| 3 | * |
| 4 | * BabelTrace - Integer 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/align.h> |
| 21 | #include <babeltrace/format.h> |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | void integer_copy(struct stream_pos *dest, const struct format *fdest, |
| 25 | struct stream_pos *src, const struct format *fsrc, |
| 26 | const struct type_class *type_class) |
| 27 | { |
| 28 | struct type_class_integer *int_class = |
| 29 | container_of(type_class, struct type_class_integer, p); |
| 30 | |
| 31 | if (!int_class->signedness) { |
| 32 | uint64_t v; |
| 33 | |
| 34 | v = fsrc->uint_read(src, int_class); |
| 35 | fdest->uint_write(dest, int_class, v); |
| 36 | } else { |
| 37 | int64_t v; |
| 38 | |
| 39 | v = fsrc->int_read(src, int_class); |
| 40 | fdest->int_write(dest, int_class, v); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void integer_type_free(struct type_class_integer *int_class) |
| 45 | { |
| 46 | g_free(int_class); |
| 47 | } |
| 48 | |
| 49 | static void _integer_type_free(struct type_class *type_class) |
| 50 | { |
| 51 | struct type_class_integer *int_class = |
| 52 | container_of(type_class, struct type_class_integer, p); |
| 53 | integer_type_free(int_class); |
| 54 | } |
| 55 | |
| 56 | struct type_class_integer *integer_type_new(const char *name, |
| 57 | size_t len, int byte_order, |
| 58 | int signedness, |
| 59 | size_t alignment) |
| 60 | { |
| 61 | struct type_class_integer *int_class; |
| 62 | int ret; |
| 63 | |
| 64 | int_class = g_new(struct type_class_integer, 1); |
| 65 | int_class->p.name = g_quark_from_string(name); |
| 66 | int_class->p.alignment = alignment; |
| 67 | int_class->p.copy = integer_copy; |
| 68 | int_class->p.free = _integer_type_free; |
| 69 | int_class->p.ref = 1; |
| 70 | int_class->len = len; |
| 71 | int_class->byte_order = byte_order; |
| 72 | int_class->signedness = signedness; |
| 73 | if (int_class->p.name) { |
| 74 | ret = register_type(&int_class->p); |
| 75 | if (ret) { |
| 76 | g_free(int_class); |
| 77 | return NULL; |
| 78 | } |
| 79 | } |
| 80 | return int_class; |
| 81 | } |