I/O structures accessors generation (work in progress)
[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
e19c3d69 25struct declaration *_integer_declaration_new(struct type *type,
c054553d
MD
26 struct declaration_scope *parent_scope);
27static
e19c3d69 28void _integer_declaration_free(struct declaration *declaration);
c054553d 29
4c8bfb7e
MD
30void integer_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
e19c3d69 32 struct declaration *declaration)
fc93b2bd 33{
e19c3d69 34 struct declaration_integer *integer =
6ee5115e 35 container_of(declaration, struct declaration_integer, p);
e19c3d69 36 struct type_integer *integer_type = integer->type;
fc93b2bd 37
e19c3d69 38 if (!integer_type->signedness) {
fc93b2bd
MD
39 uint64_t v;
40
e19c3d69
MD
41 v = fsrc->uint_read(src, integer_type);
42 fdest->uint_write(dest, integer_type, v);
fc93b2bd
MD
43 } else {
44 int64_t v;
45
e19c3d69
MD
46 v = fsrc->int_read(src, integer_type);
47 fdest->int_write(dest, integer_type, v);
fc93b2bd
MD
48 }
49}
698f0fe4 50
c054553d 51static
e19c3d69 52void _integer_type_free(struct type *type)
90b676d7 53{
e19c3d69
MD
54 struct type_integer *integer_type =
55 container_of(type, struct type_integer, p);
56 g_free(integer_type);
90b676d7
MD
57}
58
e19c3d69
MD
59struct type_integer *
60 integer_type_new(const char *name, size_t len, int byte_order,
61 int signedness, size_t alignment)
698f0fe4 62{
e19c3d69 63 struct type_integer *integer_type;
698f0fe4 64
e19c3d69 65 integer_type = g_new(struct type_integer, 1);
05628561 66 integer_type->p.id = CTF_TYPE_INTEGER;
e19c3d69
MD
67 integer_type->p.name = g_quark_from_string(name);
68 integer_type->p.alignment = alignment;
69 integer_type->p.copy = integer_copy;
70 integer_type->p.type_free = _integer_type_free;
71 integer_type->p.declaration_free = _integer_declaration_free;
72 integer_type->p.declaration_new = _integer_declaration_new;
73 integer_type->p.ref = 1;
74 integer_type->len = len;
75 integer_type->byte_order = byte_order;
76 integer_type->signedness = signedness;
e19c3d69 77 return integer_type;
698f0fe4 78}
c054553d
MD
79
80static
e19c3d69
MD
81struct declaration *
82 _integer_declaration_new(struct type *type,
83 struct declaration_scope *parent_scope)
c054553d 84{
e19c3d69
MD
85 struct type_integer *integer_type =
86 container_of(type, struct type_integer, p);
87 struct declaration_integer *integer;
c054553d 88
e19c3d69
MD
89 integer = g_new(struct declaration_integer, 1);
90 type_ref(&integer_type->p);
6ee5115e
MD
91 integer->p.type = type;
92 integer->type = integer_type;
c054553d
MD
93 integer->p.ref = 1;
94 integer->value._unsigned = 0;
95 return &integer->p;
96}
97
98static
e19c3d69 99void _integer_declaration_free(struct declaration *declaration)
c054553d 100{
e19c3d69
MD
101 struct declaration_integer *integer =
102 container_of(declaration, struct declaration_integer, p);
c054553d 103
e19c3d69 104 type_unref(integer->p.type);
c054553d
MD
105 g_free(integer);
106}
This page took 0.039664 seconds and 4 git commands to generate.