9e6df588f9764537a9969a0937274b941fa70aa8
[babeltrace.git] / types / integer.c
1 /*
2 * integer.c
3 *
4 * BabelTrace - Integer Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/align.h>
23 #include <babeltrace/format.h>
24 #include <stdint.h>
25
26 static
27 struct definition *_integer_definition_new(struct declaration *declaration,
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index,
30 const char *root_name);
31 static
32 void _integer_definition_free(struct definition *definition);
33
34 static
35 void _integer_declaration_free(struct declaration *declaration)
36 {
37 struct declaration_integer *integer_declaration =
38 container_of(declaration, struct declaration_integer, p);
39 g_free(integer_declaration);
40 }
41
42 struct declaration_integer *
43 integer_declaration_new(size_t len, int byte_order,
44 int signedness, size_t alignment, int base,
45 enum ctf_string_encoding encoding)
46 {
47 struct declaration_integer *integer_declaration;
48
49 integer_declaration = g_new(struct declaration_integer, 1);
50 integer_declaration->p.id = CTF_TYPE_INTEGER;
51 integer_declaration->p.alignment = alignment;
52 integer_declaration->p.declaration_free = _integer_declaration_free;
53 integer_declaration->p.definition_free = _integer_definition_free;
54 integer_declaration->p.definition_new = _integer_definition_new;
55 integer_declaration->p.ref = 1;
56 integer_declaration->len = len;
57 integer_declaration->byte_order = byte_order;
58 integer_declaration->signedness = signedness;
59 integer_declaration->base = base;
60 integer_declaration->encoding = encoding;
61 return integer_declaration;
62 }
63
64 static
65 struct definition *
66 _integer_definition_new(struct declaration *declaration,
67 struct definition_scope *parent_scope,
68 GQuark field_name, int index,
69 const char *root_name)
70 {
71 struct declaration_integer *integer_declaration =
72 container_of(declaration, struct declaration_integer, p);
73 struct definition_integer *integer;
74 int ret;
75
76 integer = g_new(struct definition_integer, 1);
77 declaration_ref(&integer_declaration->p);
78 integer->p.declaration = declaration;
79 integer->declaration = integer_declaration;
80 integer->p.ref = 1;
81 /*
82 * Use INT_MAX order to ensure that all fields of the parent
83 * scope are seen as being prior to this scope.
84 */
85 integer->p.index = root_name ? INT_MAX : index;
86 integer->p.name = field_name;
87 integer->p.path = new_definition_path(parent_scope, field_name,
88 root_name);
89 integer->p.scope = NULL;
90 integer->value._unsigned = 0;
91 ret = register_field_definition(field_name, &integer->p,
92 parent_scope);
93 assert(!ret);
94 return &integer->p;
95 }
96
97 static
98 void _integer_definition_free(struct definition *definition)
99 {
100 struct definition_integer *integer =
101 container_of(definition, struct definition_integer, p);
102
103 declaration_unref(integer->p.declaration);
104 g_free(integer);
105 }
106
107 uint64_t get_unsigned_int(struct definition *field)
108 {
109 struct definition_integer *integer_definition;
110 const struct declaration_integer *integer_declaration;
111
112 integer_definition = container_of(field, struct definition_integer, p);
113 integer_declaration = integer_definition->declaration;
114
115 if (!integer_declaration->signedness) {
116 return integer_definition->value._unsigned;
117 }
118 fprintf(stderr, "[warning] Extracting unsigned value in a signed int\n");
119 return (uint64_t)integer_definition->value._signed;
120 }
121
122 int64_t get_signed_int(struct definition *field)
123 {
124 struct definition_integer *integer_definition;
125 const struct declaration_integer *integer_declaration;
126
127 integer_definition = container_of(field, struct definition_integer, p);
128 integer_declaration = integer_definition->declaration;
129
130 if (integer_declaration->signedness) {
131 return integer_definition->value._signed;
132 }
133 fprintf(stderr, "[warning] Extracting signed value in an unsigned int\n");
134 return (int64_t)integer_definition->value._unsigned;
135 }
This page took 0.04934 seconds and 3 git commands to generate.