Finish float
[babeltrace.git] / include / babeltrace / types.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
fc93b2bd 7 * Type Header
d79865b9
MD
8 *
9 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
fc93b2bd 26#include <babeltrace/format.h>
bed864a7
MD
27#include <babeltrace/align.h>
28#include <string.h>
29
30/*
31 * Always update stream_pos with move_pos and init_pos.
32 */
33struct stream_pos {
34 unsigned char *base; /* Base address */
35 size_t offset; /* Offset from base, in bits */
36 int dummy; /* Dummy position, for length calculation */
37};
38
39static inline
40void init_pos(struct stream_pos *pos, unsigned char *base)
41{
42 pos->base = base; /* initial base, page-aligned */
43 pos->offset = 0;
44 pos->dummy = false;
45}
46
47/*
48 * move_pos - move position of a relative bit offset
49 *
50 * TODO: allow larger files by updating base too.
51 */
52static inline
53void move_pos(struct stream_pos *pos, size_t offset)
54{
55 pos->offset = pos->offset + offset;
56}
57
58/*
59 * align_pos - align position on a bit offset (> 0)
60 *
61 * TODO: allow larger files by updating base too.
62 */
63static inline
64void align_pos(struct stream_pos *pos, size_t offset)
65{
66 pos->offset += offset_align(pos->offset, offset);
67}
68
69static inline
70void copy_pos(struct stream_pos *dest, struct stream_pos *src)
71{
72 memcpy(dest, src, sizeof(struct stream_pos));
73}
74
75static inline
76unsigned char *get_pos_addr(struct stream_pos *pos)
77{
78 /* Only makes sense to get the address after aligning on CHAR_BIT */
79 assert(!(pos->alignment % CHAR_BIT));
80 return pos->base + (pos->offset / CHAR_BIT);
81}
fc93b2bd
MD
82
83struct type_class {
84 GQuark name; /* type name */
85 size_t alignment; /* type alignment, in bits */
86 /*
87 * Type copy function. Knows how to find the child type_class from the
88 * parent type_class.
89 */
bed864a7
MD
90 size_t (*copy)(struct stream_pos *dest, const struct format *fdest,
91 struct stream_pos *src, const struct format *fsrc,
fc93b2bd 92 const struct type_class *type_class);
90b676d7 93 void (*free)(struct type_class *type_class);
fc93b2bd
MD
94};
95
96struct type_class_integer {
97 struct type_class p;
98 size_t len; /* length, in bits. */
99 int byte_order; /* byte order */
100 int signedness;
101};
102
bed864a7
MD
103/*
104 * Because we address in bits, bitfields end up being exactly the same as
105 * integers, except that their read/write functions must be able to deal with
106 * read/write non aligned on CHAR_BIT.
107 */
fc93b2bd
MD
108struct type_class_bitfield {
109 struct type_class_integer p;
fc93b2bd
MD
110};
111
112struct type_class_float {
113 struct type_class p;
11d43b90 114 struct bitfield_class *sign;
bed864a7
MD
115 struct bitfield_class *mantissa;
116 struct bitfield_class *exp;
fc93b2bd 117 int byte_order;
0a46062b 118 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
119};
120
448d3cc7
MD
121struct enum_table {
122 GHashTable *value_to_quark; /* Tuples (value, GQuark) */
123 GHashTable *quark_to_value; /* Tuples (GQuark, value) */
124};
125
fc93b2bd 126struct type_class_enum {
448d3cc7
MD
127 struct type_class_bitfield p; /* inherit from bitfield */
128 struct enum_table table;
fc93b2bd
MD
129};
130
bed864a7
MD
131struct type_class_string {
132 struct type_class p;
133};
134
fc93b2bd
MD
135struct type_class_struct {
136 struct type_class p;
137 /* TODO */
138};
139
698f0fe4
MD
140struct type_class *ctf_lookup_type(GQuark qname);
141int ctf_register_type(struct type_class *type_class);
142
0a46062b
MD
143/* Nameless types can be created by passing a NULL name */
144
145struct type_class_integer *integer_type_new(const char *name,
0a46062b 146 size_t len, int byte_order,
11d43b90
MD
147 int signedness,
148 size_t alignment);
0a46062b
MD
149void integer_type_free(struct type_class_integer *int_class);
150
151struct type_class_bitfield *bitfield_type_new(const char *name,
0a46062b 152 size_t len, int byte_order,
11d43b90
MD
153 int signedness,
154 size_t alignment);
0a46062b
MD
155void bitfield_type_free(struct type_class_bitfield *bitfield_class);
156
11d43b90
MD
157/*
158 * mantissa_len is the length of the number of bytes represented by the mantissa
159 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
160 */
0a46062b
MD
161struct type_class_float *float_type_new(const char *name,
162 size_t mantissa_len,
163 size_t exp_len, int byte_order,
164 size_t alignment);
165void float_type_free(struct type_class_float *float_class);
166
448d3cc7
MD
167/*
168 * A GQuark can be translated to/from strings with g_quark_from_string() and
169 * g_quark_to_string().
170 */
171GQuark enum_uint_to_quark(const struct type_class_enum *enum_class, uint64_t v);
172GQuark enum_int_to_quark(const struct type_class_enum *enum_class, uint64_t v);
173uint64_t enum_quark_to_uint(const struct type_class_enum *enum_class,
174 size_t len, int byte_order, GQuark q);
175int64_t enum_quark_to_int(const struct type_class_enum *enum_class,
176 size_t len, int byte_order, GQuark q);
177void enum_signed_insert(struct type_class_enum *enum_class,
178 int64_t v, GQuark q);
179void enum_unsigned_insert(struct type_class_enum *enum_class,
180 uint64_t v, GQuark q);
181
182struct type_class_enum *enum_type_new(const char *name,
448d3cc7
MD
183 size_t len, int byte_order,
184 int signedness,
185 size_t alignment);
186void enum_type_free(struct type_class_enum *enum_class);
187
d79865b9 188#endif /* _BABELTRACE_TYPES_H */
This page took 0.02973 seconds and 4 git commands to generate.