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