ea292900ce80886141bf21a171ec3b76d2573db8
[babeltrace.git] / types / integer.c
1 /*
2 * integer.c
3 *
4 * BabelTrace - Integer Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/align.h>
21 #include <babeltrace/format.h>
22 #include <stdint.h>
23
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
30 void integer_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
32 struct type *type)
33 {
34 struct type_integer *integer = container_of(type, struct type_integer,
35 p);
36 struct type_class_integer *int_class = integer->_class;
37
38 if (!int_class->signedness) {
39 uint64_t v;
40
41 v = fsrc->uint_read(src, int_class);
42 fdest->uint_write(dest, int_class, v);
43 } else {
44 int64_t v;
45
46 v = fsrc->int_read(src, int_class);
47 fdest->int_write(dest, int_class, v);
48 }
49 }
50
51 static
52 void _integer_type_class_free(struct type_class *type_class)
53 {
54 struct type_class_integer *integer_class =
55 container_of(type_class, struct type_class_integer, p);
56 g_free(integer_class);
57 }
58
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)
62 {
63 struct type_class_integer *int_class;
64 int ret;
65
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;
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;
73 int_class->p.ref = 1;
74 int_class->len = len;
75 int_class->byte_order = byte_order;
76 int_class->signedness = signedness;
77 if (int_class->p.name) {
78 ret = register_type(&int_class->p);
79 if (ret) {
80 g_free(int_class);
81 return NULL;
82 }
83 }
84 return int_class;
85 }
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 }
This page took 0.030789 seconds and 3 git commands to generate.