b338a5913971f86f7787c6a9d4d3a2db4b6778e9
[babeltrace.git] / types / integer.c
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>
22 #include <babeltrace/align.h>
23 #include <stdint.h>
24
25 size_t integer_copy(unsigned char *dest, const struct format *fdest,
26 const unsigned char *src, const struct format *fsrc,
27 const struct type_class *type_class)
28 {
29 struct type_class_integer *int_class =
30 container_of(type_class, struct type_class_integer, p);
31
32 if (fsrc->p.alignment)
33 src = PTR_ALIGN(src, fsrc->p.alignment / CHAR_BIT);
34 if (fdest->p.alignment)
35 dest = PTR_ALIGN(dest, fdest->p.alignment / CHAR_BIT);
36
37 if (!int_class->signedness) {
38 uint64_t v;
39
40 v = fsrc->uint_read(src, int_class->len, int_class->byte_order);
41 return fdest->uint_write(dest, int_class->len, int_class->byte_order, v);
42 } else {
43 int64_t v;
44
45 v = fsrc->int_read(src, int_class->len, int_class->byte_order);
46 return fdest->int_write(dest, int_class->len, int_class->byte_order, v);
47 }
48 }
49
50 struct type_class_integer *integer_type_new(const char *name,
51 size_t start_offset,
52 size_t len, int byte_order,
53 int signedness,
54 size_t alignment)
55 {
56 struct type_class_integer *int_class;
57 int ret;
58
59 /*
60 * Freed when type is unregistered.
61 */
62 int_class = g_new(struct type_class_integer, 1);
63 int_class->p.name = g_quark_from_string(name);
64 int_class->p.alignment = alignment;
65 int_class->p.copy = integer_copy;
66 int_class->len = len;
67 int_class->byte_order = byte_order;
68 int_class->signedness = signedness;
69 if (int_class->p.name) {
70 ret = ctf_register_type(&int_class.p);
71 if (ret) {
72 g_free(int_class);
73 return NULL;
74 }
75 }
76 return int_class;
77 }
78
79 void integer_type_free(struct type_class_integer *int_class)
80 {
81 if (!int_class->name)
82 g_free(int_class);
83 }
This page took 0.030182 seconds and 4 git commands to generate.