Commit | Line | Data |
---|---|---|
fc93b2bd | 1 | /* |
ccd7e1c8 | 2 | * integer.c |
fc93b2bd | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Integer Type Converter |
fc93b2bd | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - 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 | ||
c054553d MD |
24 | static |
25 | struct type *_integer_type_new(struct type_class *type_class, | |
26 | struct declaration_scope *parent_scope); | |
27 | static | |
28 | void _integer_type_free(struct type *type); | |
29 | ||
4c8bfb7e MD |
30 | void integer_copy(struct stream_pos *dest, const struct format *fdest, |
31 | struct stream_pos *src, const struct format *fsrc, | |
c054553d | 32 | struct type *type) |
fc93b2bd | 33 | { |
c054553d MD |
34 | struct type_integer *integer = container_of(type, struct type_integer, |
35 | p); | |
36 | struct type_class_integer *int_class = integer->_class; | |
fc93b2bd MD |
37 | |
38 | if (!int_class->signedness) { | |
39 | uint64_t v; | |
40 | ||
bed864a7 | 41 | v = fsrc->uint_read(src, int_class); |
4c8bfb7e | 42 | fdest->uint_write(dest, int_class, v); |
fc93b2bd MD |
43 | } else { |
44 | int64_t v; | |
45 | ||
bed864a7 | 46 | v = fsrc->int_read(src, int_class); |
4c8bfb7e | 47 | fdest->int_write(dest, int_class, v); |
fc93b2bd MD |
48 | } |
49 | } | |
698f0fe4 | 50 | |
c054553d MD |
51 | static |
52 | void _integer_type_class_free(struct type_class *type_class) | |
90b676d7 | 53 | { |
c054553d | 54 | struct type_class_integer *integer_class = |
90b676d7 | 55 | container_of(type_class, struct type_class_integer, p); |
c054553d | 56 | g_free(integer_class); |
90b676d7 MD |
57 | } |
58 | ||
c054553d MD |
59 | struct type_class_integer * |
60 | integer_type_class_new(const char *name, size_t len, int byte_order, | |
61 | int signedness, size_t alignment) | |
698f0fe4 | 62 | { |
0a46062b | 63 | struct type_class_integer *int_class; |
698f0fe4 MD |
64 | int ret; |
65 | ||
698f0fe4 MD |
66 | int_class = g_new(struct type_class_integer, 1); |
67 | int_class->p.name = g_quark_from_string(name); | |
68 | int_class->p.alignment = alignment; | |
69 | int_class->p.copy = integer_copy; | |
c054553d MD |
70 | int_class->p.class_free = _integer_type_class_free; |
71 | int_class->p.type_free = _integer_type_free; | |
72 | int_class->p.type_new = _integer_type_new; | |
4c8bfb7e | 73 | int_class->p.ref = 1; |
698f0fe4 MD |
74 | int_class->len = len; |
75 | int_class->byte_order = byte_order; | |
76 | int_class->signedness = signedness; | |
0a46062b | 77 | if (int_class->p.name) { |
be85c1c7 | 78 | ret = register_type(&int_class->p); |
0a46062b MD |
79 | if (ret) { |
80 | g_free(int_class); | |
81 | return NULL; | |
82 | } | |
83 | } | |
84 | return int_class; | |
698f0fe4 | 85 | } |
c054553d MD |
86 | |
87 | static | |
88 | struct type_integer *_integer_type_new(struct type_class *type_class, | |
89 | struct declaration_scope *parent_scope) | |
90 | { | |
91 | struct type_class_integer *integer_class = | |
92 | container_of(type_class, struct type_class_integer, p); | |
93 | struct type_integer *integer; | |
94 | ||
95 | integer = g_new(struct type_integer, 1); | |
96 | type_class_ref(&integer_class->p); | |
97 | integer->p._class = integer_class; | |
98 | integer->p.ref = 1; | |
99 | integer->value._unsigned = 0; | |
100 | return &integer->p; | |
101 | } | |
102 | ||
103 | static | |
104 | void _integer_type_free(struct type *type) | |
105 | { | |
106 | struct type_integer *integer = | |
107 | container_of(type, struct type_integer, p); | |
108 | ||
109 | type_class_unref(integer->p._class); | |
110 | g_free(integer); | |
111 | } |