Implement clock mapping within integer type
[babeltrace.git] / types / integer.c
CommitLineData
fc93b2bd 1/*
ccd7e1c8 2 * integer.c
fc93b2bd 3 *
ccd7e1c8 4 * BabelTrace - Integer Type Converter
fc93b2bd 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 9 *
ccd7e1c8
MD
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:
fc93b2bd 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
fc93b2bd
MD
19 */
20
21#include <babeltrace/compiler.h>
698f0fe4 22#include <babeltrace/align.h>
4c8bfb7e 23#include <babeltrace/format.h>
fc93b2bd
MD
24#include <stdint.h>
25
c054553d 26static
f6625916 27struct definition *_integer_definition_new(struct declaration *declaration,
05c749e5 28 struct definition_scope *parent_scope,
98df1c9f
MD
29 GQuark field_name, int index,
30 const char *root_name);
c054553d 31static
e1151715 32void _integer_definition_free(struct definition *definition);
c054553d 33
c054553d 34static
f6625916 35void _integer_declaration_free(struct declaration *declaration)
90b676d7 36{
f6625916
MD
37 struct declaration_integer *integer_declaration =
38 container_of(declaration, struct declaration_integer, p);
39 g_free(integer_declaration);
90b676d7
MD
40}
41
f6625916 42struct declaration_integer *
add40b62 43 integer_declaration_new(size_t len, int byte_order,
81dee1bb 44 int signedness, size_t alignment, int base,
56e60373
MD
45 enum ctf_string_encoding encoding,
46 struct ctf_clock *clock)
698f0fe4 47{
f6625916 48 struct declaration_integer *integer_declaration;
698f0fe4 49
f6625916
MD
50 integer_declaration = g_new(struct declaration_integer, 1);
51 integer_declaration->p.id = CTF_TYPE_INTEGER;
f6625916 52 integer_declaration->p.alignment = alignment;
f6625916
MD
53 integer_declaration->p.declaration_free = _integer_declaration_free;
54 integer_declaration->p.definition_free = _integer_definition_free;
55 integer_declaration->p.definition_new = _integer_definition_new;
56 integer_declaration->p.ref = 1;
57 integer_declaration->len = len;
58 integer_declaration->byte_order = byte_order;
59 integer_declaration->signedness = signedness;
164078da 60 integer_declaration->base = base;
81dee1bb 61 integer_declaration->encoding = encoding;
56e60373 62 integer_declaration->clock = clock;
f6625916 63 return integer_declaration;
698f0fe4 64}
c054553d
MD
65
66static
e1151715 67struct definition *
f6625916 68 _integer_definition_new(struct declaration *declaration,
05c749e5 69 struct definition_scope *parent_scope,
98df1c9f
MD
70 GQuark field_name, int index,
71 const char *root_name)
c054553d 72{
f6625916
MD
73 struct declaration_integer *integer_declaration =
74 container_of(declaration, struct declaration_integer, p);
e1151715 75 struct definition_integer *integer;
98df1c9f 76 int ret;
c054553d 77
e1151715 78 integer = g_new(struct definition_integer, 1);
f6625916
MD
79 declaration_ref(&integer_declaration->p);
80 integer->p.declaration = declaration;
81 integer->declaration = integer_declaration;
c054553d 82 integer->p.ref = 1;
98df1c9f
MD
83 /*
84 * Use INT_MAX order to ensure that all fields of the parent
85 * scope are seen as being prior to this scope.
86 */
87 integer->p.index = root_name ? INT_MAX : index;
b1a2f580 88 integer->p.name = field_name;
98df1c9f
MD
89 integer->p.path = new_definition_path(parent_scope, field_name,
90 root_name);
a35173fe 91 integer->p.scope = NULL;
c054553d 92 integer->value._unsigned = 0;
98df1c9f
MD
93 ret = register_field_definition(field_name, &integer->p,
94 parent_scope);
95 assert(!ret);
c054553d
MD
96 return &integer->p;
97}
98
99static
e1151715 100void _integer_definition_free(struct definition *definition)
c054553d 101{
e1151715
MD
102 struct definition_integer *integer =
103 container_of(definition, struct definition_integer, p);
c054553d 104
f6625916 105 declaration_unref(integer->p.declaration);
c054553d
MD
106 g_free(integer);
107}
343c801f
JD
108
109uint64_t get_unsigned_int(struct definition *field)
110{
111 struct definition_integer *integer_definition;
112 const struct declaration_integer *integer_declaration;
113
114 integer_definition = container_of(field, struct definition_integer, p);
115 integer_declaration = integer_definition->declaration;
116
117 if (!integer_declaration->signedness) {
118 return integer_definition->value._unsigned;
119 }
120 fprintf(stderr, "[warning] Extracting unsigned value in a signed int\n");
121 return (uint64_t)integer_definition->value._signed;
122}
123
124int64_t get_signed_int(struct definition *field)
125{
126 struct definition_integer *integer_definition;
127 const struct declaration_integer *integer_declaration;
128
129 integer_definition = container_of(field, struct definition_integer, p);
130 integer_declaration = integer_definition->declaration;
131
132 if (integer_declaration->signedness) {
133 return integer_definition->value._signed;
134 }
135 fprintf(stderr, "[warning] Extracting signed value in an unsigned int\n");
136 return (int64_t)integer_definition->value._unsigned;
137}
This page took 0.045692 seconds and 4 git commands to generate.