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