697748773f413b0171388b85f0315821bf402ab3
[babeltrace.git] / types / bitfield.c
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 <babeltrace/types.h>
23 #include <stdint.h>
24
25 size_t bitfield_copy(struct stream_pos *dest, const struct format *fdest,
26 struct stream_pos *src, const struct format *fsrc,
27 const struct type_class *type_class)
28 {
29 struct type_class_bitfield *bitfield_class =
30 container_of(type_class, struct type_class_bitfield, p);
31 struct type_class_integer *int_class = &bitfield_class->p;
32
33 if (!int_class->signedness) {
34 uint64_t v;
35
36 v = fsrc->bitfield_unsigned_read(src, bitfield_class);
37 return fdest->bitfield_unsigned_write(dest, bitfield_class, v);
38 } else {
39 int64_t v;
40
41 v = fsrc->bitfield_signed_read(src, bitfield_class);
42 return fdest->bitfield_signed_write(dest, bitfield_class, v);
43 }
44 }
45
46 void bitfield_type_free(struct type_class_bitfield *bitfield_class)
47 {
48 g_free(bitfield_class);
49 }
50
51 static void _bitfield_type_free(struct type_class *type_class)
52 {
53 struct type_class_bitfield *bitfield_class =
54 container_of(type_class, struct type_class_bitfield, p);
55 bitfield_type_free(bitfield_class);
56 }
57
58 struct type_class_bitfield *bitfield_type_new(const char *name,
59 size_t len, int byte_order,
60 int signedness,
61 size_t alignment)
62 {
63 struct type_class_bitfield *bitfield_class;
64 struct type_class_integer *int_class;
65 int ret;
66
67 bitfield_class = g_new(struct type_class_bitfield, 1);
68 int_class = &bitfield_class->p;
69 int_class->p.name = g_quark_from_string(name);
70 int_class->p.alignment = alignment;
71 int_class->p.copy = bitfield_copy;
72 int_class->p.free = _bitfield_type_free;
73 int_class->len = len;
74 int_class->byte_order = byte_order;
75 int_class->signedness = signedness;
76 if (int_class->p.name) {
77 ret = ctf_register_type(&int_class->p);
78 if (ret) {
79 g_free(bitfield_class);
80 return NULL;
81 }
82 }
83 return bitfield_class;
84 }
This page took 0.031671 seconds and 3 git commands to generate.