Fix open_mmap_trace missing support
[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 <babeltrace/types.h>
25 #include <stdint.h>
26
27 static
28 struct definition *_integer_definition_new(struct declaration *declaration,
29 struct definition_scope *parent_scope,
30 GQuark field_name, int index,
31 const char *root_name);
32 static
33 void _integer_definition_free(struct definition *definition);
34
35 static
36 void _integer_declaration_free(struct declaration *declaration)
37 {
38 struct declaration_integer *integer_declaration =
39 container_of(declaration, struct declaration_integer, p);
40 g_free(integer_declaration);
41 }
42
43 struct declaration_integer *
44 integer_declaration_new(size_t len, int byte_order,
45 int signedness, size_t alignment, int base,
46 enum ctf_string_encoding encoding,
47 struct ctf_clock *clock)
48 {
49 struct declaration_integer *integer_declaration;
50
51 integer_declaration = g_new(struct declaration_integer, 1);
52 integer_declaration->p.id = CTF_TYPE_INTEGER;
53 integer_declaration->p.alignment = alignment;
54 integer_declaration->p.declaration_free = _integer_declaration_free;
55 integer_declaration->p.definition_free = _integer_definition_free;
56 integer_declaration->p.definition_new = _integer_definition_new;
57 integer_declaration->p.ref = 1;
58 integer_declaration->len = len;
59 integer_declaration->byte_order = byte_order;
60 integer_declaration->signedness = signedness;
61 integer_declaration->base = base;
62 integer_declaration->encoding = encoding;
63 integer_declaration->clock = clock;
64 return integer_declaration;
65 }
66
67 static
68 struct definition *
69 _integer_definition_new(struct declaration *declaration,
70 struct definition_scope *parent_scope,
71 GQuark field_name, int index,
72 const char *root_name)
73 {
74 struct declaration_integer *integer_declaration =
75 container_of(declaration, struct declaration_integer, p);
76 struct definition_integer *integer;
77 int ret;
78
79 integer = g_new(struct definition_integer, 1);
80 declaration_ref(&integer_declaration->p);
81 integer->p.declaration = declaration;
82 integer->declaration = integer_declaration;
83 integer->p.ref = 1;
84 /*
85 * Use INT_MAX order to ensure that all fields of the parent
86 * scope are seen as being prior to this scope.
87 */
88 integer->p.index = root_name ? INT_MAX : index;
89 integer->p.name = field_name;
90 integer->p.path = new_definition_path(parent_scope, field_name,
91 root_name);
92 integer->p.scope = NULL;
93 integer->value._unsigned = 0;
94 ret = register_field_definition(field_name, &integer->p,
95 parent_scope);
96 assert(!ret);
97 return &integer->p;
98 }
99
100 static
101 void _integer_definition_free(struct definition *definition)
102 {
103 struct definition_integer *integer =
104 container_of(definition, struct definition_integer, p);
105
106 declaration_unref(integer->p.declaration);
107 g_free(integer);
108 }
109
110 uint64_t get_unsigned_int(struct definition *field)
111 {
112 struct definition_integer *integer_definition;
113 const struct declaration_integer *integer_declaration;
114
115 integer_definition = container_of(field, struct definition_integer, p);
116 integer_declaration = integer_definition->declaration;
117
118 if (!integer_declaration->signedness) {
119 return integer_definition->value._unsigned;
120 }
121 fprintf(stderr, "[warning] Extracting unsigned value in a signed int\n");
122 return (uint64_t)integer_definition->value._signed;
123 }
124
125 int64_t get_signed_int(struct definition *field)
126 {
127 struct definition_integer *integer_definition;
128 const struct declaration_integer *integer_declaration;
129
130 integer_definition = container_of(field, struct definition_integer, p);
131 integer_declaration = integer_definition->declaration;
132
133 if (integer_declaration->signedness) {
134 return integer_definition->value._signed;
135 }
136 fprintf(stderr, "[warning] Extracting signed value in an unsigned int\n");
137 return (int64_t)integer_definition->value._unsigned;
138 }
This page took 0.031597 seconds and 4 git commands to generate.