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