Commit | Line | Data |
---|---|---|
fc93b2bd | 1 | /* |
ccd7e1c8 | 2 | * integer.c |
fc93b2bd | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Integer Type Converter |
fc93b2bd | 5 | * |
ccd7e1c8 | 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fc93b2bd | 7 | * |
ccd7e1c8 MD |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
fc93b2bd | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
fc93b2bd MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
698f0fe4 | 20 | #include <babeltrace/align.h> |
4c8bfb7e | 21 | #include <babeltrace/format.h> |
fc93b2bd MD |
22 | #include <stdint.h> |
23 | ||
4c8bfb7e MD |
24 | void integer_copy(struct stream_pos *dest, const struct format *fdest, |
25 | struct stream_pos *src, const struct format *fsrc, | |
26 | const struct type_class *type_class) | |
fc93b2bd MD |
27 | { |
28 | struct type_class_integer *int_class = | |
29 | container_of(type_class, struct type_class_integer, p); | |
30 | ||
31 | if (!int_class->signedness) { | |
32 | uint64_t v; | |
33 | ||
bed864a7 | 34 | v = fsrc->uint_read(src, int_class); |
4c8bfb7e | 35 | fdest->uint_write(dest, int_class, v); |
fc93b2bd MD |
36 | } else { |
37 | int64_t v; | |
38 | ||
bed864a7 | 39 | v = fsrc->int_read(src, int_class); |
4c8bfb7e | 40 | fdest->int_write(dest, int_class, v); |
fc93b2bd MD |
41 | } |
42 | } | |
698f0fe4 | 43 | |
90b676d7 MD |
44 | void integer_type_free(struct type_class_integer *int_class) |
45 | { | |
46 | g_free(int_class); | |
47 | } | |
48 | ||
49 | static void _integer_type_free(struct type_class *type_class) | |
50 | { | |
51 | struct type_class_integer *int_class = | |
52 | container_of(type_class, struct type_class_integer, p); | |
53 | integer_type_free(int_class); | |
54 | } | |
55 | ||
0a46062b | 56 | struct type_class_integer *integer_type_new(const char *name, |
0a46062b MD |
57 | size_t len, int byte_order, |
58 | int signedness, | |
59 | size_t alignment) | |
698f0fe4 | 60 | { |
0a46062b | 61 | struct type_class_integer *int_class; |
698f0fe4 MD |
62 | int ret; |
63 | ||
698f0fe4 MD |
64 | int_class = g_new(struct type_class_integer, 1); |
65 | int_class->p.name = g_quark_from_string(name); | |
66 | int_class->p.alignment = alignment; | |
67 | int_class->p.copy = integer_copy; | |
90b676d7 | 68 | int_class->p.free = _integer_type_free; |
4c8bfb7e | 69 | int_class->p.ref = 1; |
698f0fe4 MD |
70 | int_class->len = len; |
71 | int_class->byte_order = byte_order; | |
72 | int_class->signedness = signedness; | |
0a46062b | 73 | if (int_class->p.name) { |
be85c1c7 | 74 | ret = register_type(&int_class->p); |
0a46062b MD |
75 | if (ret) { |
76 | g_free(int_class); | |
77 | return NULL; | |
78 | } | |
79 | } | |
80 | return int_class; | |
698f0fe4 | 81 | } |