Scope path: split dynamic scope path
[babeltrace.git] / types / integer.c
CommitLineData
fc93b2bd 1/*
ccd7e1c8 2 * integer.c
fc93b2bd 3 *
ccd7e1c8 4 * BabelTrace - Integer Type Converter
fc93b2bd 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 7 *
ccd7e1c8
MD
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:
fc93b2bd 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
fc93b2bd
MD
17 */
18
19#include <babeltrace/compiler.h>
698f0fe4 20#include <babeltrace/align.h>
4c8bfb7e 21#include <babeltrace/format.h>
fc93b2bd
MD
22#include <stdint.h>
23
c054553d 24static
f6625916 25struct definition *_integer_definition_new(struct declaration *declaration,
05c749e5
MD
26 struct definition_scope *parent_scope,
27 GQuark field_name, int index);
c054553d 28static
e1151715 29void _integer_definition_free(struct definition *definition);
c054553d 30
4c8bfb7e
MD
31void integer_copy(struct stream_pos *dest, const struct format *fdest,
32 struct stream_pos *src, const struct format *fsrc,
e1151715 33 struct definition *definition)
fc93b2bd 34{
e1151715
MD
35 struct definition_integer *integer =
36 container_of(definition, struct definition_integer, p);
f6625916 37 struct declaration_integer *integer_declaration = integer->declaration;
fc93b2bd 38
f6625916 39 if (!integer_declaration->signedness) {
fc93b2bd
MD
40 uint64_t v;
41
f6625916
MD
42 v = fsrc->uint_read(src, integer_declaration);
43 fdest->uint_write(dest, integer_declaration, v);
fc93b2bd
MD
44 } else {
45 int64_t v;
46
f6625916
MD
47 v = fsrc->int_read(src, integer_declaration);
48 fdest->int_write(dest, integer_declaration, v);
fc93b2bd
MD
49 }
50}
698f0fe4 51
c054553d 52static
f6625916 53void _integer_declaration_free(struct declaration *declaration)
90b676d7 54{
f6625916
MD
55 struct declaration_integer *integer_declaration =
56 container_of(declaration, struct declaration_integer, p);
57 g_free(integer_declaration);
90b676d7
MD
58}
59
f6625916
MD
60struct declaration_integer *
61 integer_declaration_new(const char *name, size_t len, int byte_order,
e19c3d69 62 int signedness, size_t alignment)
698f0fe4 63{
f6625916 64 struct declaration_integer *integer_declaration;
698f0fe4 65
f6625916
MD
66 integer_declaration = g_new(struct declaration_integer, 1);
67 integer_declaration->p.id = CTF_TYPE_INTEGER;
68 integer_declaration->p.name = g_quark_from_string(name);
69 integer_declaration->p.alignment = alignment;
70 integer_declaration->p.copy = integer_copy;
71 integer_declaration->p.declaration_free = _integer_declaration_free;
72 integer_declaration->p.definition_free = _integer_definition_free;
73 integer_declaration->p.definition_new = _integer_definition_new;
74 integer_declaration->p.ref = 1;
75 integer_declaration->len = len;
76 integer_declaration->byte_order = byte_order;
77 integer_declaration->signedness = signedness;
78 return integer_declaration;
698f0fe4 79}
c054553d
MD
80
81static
e1151715 82struct definition *
f6625916 83 _integer_definition_new(struct declaration *declaration,
05c749e5
MD
84 struct definition_scope *parent_scope,
85 GQuark field_name, int index)
c054553d 86{
f6625916
MD
87 struct declaration_integer *integer_declaration =
88 container_of(declaration, struct declaration_integer, p);
e1151715 89 struct definition_integer *integer;
c054553d 90
e1151715 91 integer = g_new(struct definition_integer, 1);
f6625916
MD
92 declaration_ref(&integer_declaration->p);
93 integer->p.declaration = declaration;
94 integer->declaration = integer_declaration;
c054553d 95 integer->p.ref = 1;
05c749e5 96 integer->p.index = index;
c054553d
MD
97 integer->value._unsigned = 0;
98 return &integer->p;
99}
100
101static
e1151715 102void _integer_definition_free(struct definition *definition)
c054553d 103{
e1151715
MD
104 struct definition_integer *integer =
105 container_of(definition, struct definition_integer, p);
c054553d 106
f6625916 107 declaration_unref(integer->p.declaration);
c054553d
MD
108 g_free(integer);
109}
This page took 0.040721 seconds and 4 git commands to generate.