Commit | Line | Data |
---|---|---|
698f0fe4 MD |
1 | /* |
2 | * BabelTrace - Bitfield 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 <stdint.h> | |
23 | ||
24 | /* | |
25 | * Shortcut to integer copy if we copy bit-aligned data with 0 start_offset. | |
26 | * This skips the bitfield overhead when dealing with enumerations (which use | |
27 | * the bitfield copy functions). | |
28 | */ | |
29 | extern | |
30 | size_t integer_copy(unsigned char *dest, const struct format *fdest, | |
31 | const unsigned char *src, const struct format *fsrc, | |
32 | const struct type_class *type_class); | |
33 | ||
34 | size_t bitfield_copy(unsigned char *dest, const struct format *fdest, | |
35 | const unsigned char *src, const struct format *fsrc, | |
36 | const struct type_class *type_class) | |
37 | { | |
38 | struct type_class_bitfield *bitfield_class = | |
39 | container_of(type_class, struct type_class_bitfield, p); | |
40 | struct type_class_integer *int_class = &bitfield_class->p; | |
41 | ||
42 | if (!(int_class->p.alignment % CHAR_BIT) | |
43 | && !(int_class->len % CHAR_BIT) | |
3c1bd98a MD |
44 | && !(bitfield_class->start_offset % CHAR_BIT)) { |
45 | size_t offset = bitfield_class->start_offset / CHAR_BIT; | |
46 | dest += offset; | |
47 | src += offset; | |
698f0fe4 | 48 | return integer_copy(dest, fdest, src, fsrc, type_class); |
3c1bd98a | 49 | } |
698f0fe4 MD |
50 | |
51 | if (!int_class->signedness) { | |
52 | uint64_t v; | |
53 | ||
54 | v = fsrc->bitfield_unsigned_read(src, | |
55 | bitfield_class->start_offset, | |
56 | int_class->len, | |
57 | int_class->byte_order); | |
58 | return fdest->bitfield_unsigned_write(dest, | |
59 | bitfield_class->start_offset, | |
60 | int_class->len, int_class->byte_order, | |
61 | v); | |
62 | } else { | |
63 | int64_t v; | |
64 | ||
65 | v = fsrc->bitfield_signed_read(src, | |
66 | bitfield_class->start_offset, | |
67 | int_class->len, | |
68 | int_class->byte_order); | |
69 | return fdest->bitfield_signed_write(dest, | |
70 | bitfield_class->start_offset, | |
71 | int_class->len, int_class->byte_order, | |
72 | v); | |
73 | } | |
74 | } | |
75 | ||
0a46062b MD |
76 | struct type_class_bitfield *bitfield_type_new(const char *name, |
77 | size_t start_offset, | |
78 | size_t len, int byte_order, | |
79 | int signedness, | |
80 | size_t alignment) | |
698f0fe4 | 81 | { |
0a46062b | 82 | struct type_class_bitfield *bitfield_class; |
698f0fe4 MD |
83 | struct type_class_integer *int_class; |
84 | int ret; | |
85 | ||
86 | /* | |
87 | * Freed when type is unregistered. | |
88 | */ | |
89 | bitfield_class = g_new(struct type_class_bitfield, 1); | |
90 | int_class = &bitfield_class->p; | |
91 | int_class->p.name = g_quark_from_string(name); | |
0a46062b | 92 | int_class->p.alignment = alignment; |
698f0fe4 MD |
93 | int_class->len = len; |
94 | int_class->byte_order = byte_order; | |
95 | int_class->signedness = signedness; | |
96 | bitfield_class->start_offset = start_offset; | |
0a46062b MD |
97 | if (int_class->p.name) { |
98 | ret = ctf_register_type(&int_class->p); | |
99 | if (ret) { | |
100 | g_free(bitfield_class); | |
101 | return NULL; | |
102 | } | |
103 | } | |
104 | return bitfield_class; | |
698f0fe4 MD |
105 | } |
106 | ||
0a46062b MD |
107 | void bitfield_type_free(struct type_class_bitfield *bitfield_class) |
108 | { | |
109 | if (!bitfield_class->name) | |
110 | g_free(bitfield_class); | |
111 | } |