41a3116120984f32fe74011476b11c00924aa85d
[babeltrace.git] / types / integer.c
1 /*
2 * integer.c
3 *
4 * BabelTrace - Integer Type Converter
5 *
6 * Copyright 2010 - 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/types.h>
22 #include <stdint.h>
23
24 size_t integer_copy(unsigned char *dest, const struct format *fdest,
25 const unsigned char *src, const struct format *fsrc,
26 const struct type_class *type_class)
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
34 v = fsrc->uint_read(src, int_class);
35 return fdest->uint_write(dest, int_class, v);
36 } else {
37 int64_t v;
38
39 v = fsrc->int_read(src, int_class);
40 return fdest->int_write(dest, int_class, v);
41 }
42 }
43
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
56 struct type_class_integer *integer_type_new(const char *name,
57 size_t len, int byte_order,
58 int signedness,
59 size_t alignment)
60 {
61 struct type_class_integer *int_class;
62 int ret;
63
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;
68 int_class->p.free = _integer_type_free;
69 int_class->len = len;
70 int_class->byte_order = byte_order;
71 int_class->signedness = signedness;
72 if (int_class->p.name) {
73 ret = ctf_register_type(&int_class->p);
74 if (ret) {
75 g_free(int_class);
76 return NULL;
77 }
78 }
79 return int_class;
80 }
This page took 0.030487 seconds and 3 git commands to generate.