Babeltrace type: build warning cleanup
[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 8 *
ccd7e1c8 9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d79865b9 10 *
ccd7e1c8
MD
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
d79865b9 17 *
ccd7e1c8
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
d79865b9
MD
20 */
21
bed864a7 22#include <babeltrace/align.h>
d65d8abb 23#include <babeltrace/list.h>
4c8bfb7e
MD
24#include <stdbool.h>
25#include <stdint.h>
26#include <limits.h>
bed864a7 27#include <string.h>
4c8bfb7e 28#include <glib.h>
8eab883c 29#include <assert.h>
bed864a7 30
11796b96
MD
31/* Preallocate this many fields for structures */
32#define DEFAULT_NR_STRUCT_FIELDS 8
33
bed864a7
MD
34/*
35 * Always update stream_pos with move_pos and init_pos.
36 */
37struct stream_pos {
38 unsigned char *base; /* Base address */
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41};
42
43static inline
44void init_pos(struct stream_pos *pos, unsigned char *base)
45{
46 pos->base = base; /* initial base, page-aligned */
47 pos->offset = 0;
48 pos->dummy = false;
49}
50
51/*
52 * move_pos - move position of a relative bit offset
53 *
54 * TODO: allow larger files by updating base too.
55 */
56static inline
57void move_pos(struct stream_pos *pos, size_t offset)
58{
59 pos->offset = pos->offset + offset;
60}
61
62/*
63 * align_pos - align position on a bit offset (> 0)
64 *
65 * TODO: allow larger files by updating base too.
66 */
67static inline
68void align_pos(struct stream_pos *pos, size_t offset)
69{
70 pos->offset += offset_align(pos->offset, offset);
71}
72
73static inline
74void copy_pos(struct stream_pos *dest, struct stream_pos *src)
75{
76 memcpy(dest, src, sizeof(struct stream_pos));
77}
78
79static inline
80unsigned char *get_pos_addr(struct stream_pos *pos)
81{
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
4c8bfb7e 83 assert(!(pos->offset % CHAR_BIT));
bed864a7
MD
84 return pos->base + (pos->offset / CHAR_BIT);
85}
fc93b2bd 86
4c8bfb7e
MD
87struct format;
88
fc93b2bd
MD
89struct type_class {
90 GQuark name; /* type name */
91 size_t alignment; /* type alignment, in bits */
4c8bfb7e 92 int ref; /* number of references to the type */
fc93b2bd
MD
93 /*
94 * Type copy function. Knows how to find the child type_class from the
95 * parent type_class.
96 */
4c8bfb7e
MD
97 void (*copy)(struct stream_pos *dest, const struct format *fdest,
98 struct stream_pos *src, const struct format *fsrc,
99 const struct type_class *type_class);
90b676d7 100 void (*free)(struct type_class *type_class);
fc93b2bd
MD
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 */
7fe00194
MD
108struct type_class_integer {
109 struct type_class p;
110 size_t len; /* length, in bits. */
111 int byte_order; /* byte order */
112 int signedness;
fc93b2bd
MD
113};
114
115struct type_class_float {
116 struct type_class p;
4c8bfb7e
MD
117 struct type_class_integer *sign;
118 struct type_class_integer *mantissa;
119 struct type_class_integer *exp;
fc93b2bd 120 int byte_order;
0a46062b 121 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
122};
123
d65d8abb
MD
124/*
125 * enum_val_equal assumes that signed and unsigned memory layout overlap.
126 */
127struct enum_range {
128 union {
129 int64_t _signed;
130 uint64_t _unsigned;
131 } start; /* lowest range value */
132 union {
133 int64_t _signed;
134 uint64_t _unsigned;
135 } end; /* highest range value */
136};
137
138struct enum_range_to_quark {
139 struct cds_list_head node;
140 struct enum_range range;
141 GQuark quark;
142};
143
144/*
145 * We optimize the common case (range of size 1: single value) by creating a
146 * hash table mapping values to quark sets. We then lookup the ranges to
147 * complete the quark set.
148 *
149 * TODO: The proper structure to hold the range to quark set mapping would be an
150 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
151 * time. Using a simple O(n) list search for now for implementation speed and
152 * given that we can expect to have a _relatively_ small number of enumeration
153 * ranges. This might become untrue if we are fed with symbol tables often
154 * required to lookup function names from instruction pointer value.
155 */
448d3cc7 156struct enum_table {
d65d8abb
MD
157 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
158 struct cds_list_head range_to_quark; /* (range, GQuark) */
159 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
160};
161
fc93b2bd 162struct type_class_enum {
4c8bfb7e 163 struct type_class_integer p; /* inherit from integer */
448d3cc7 164 struct enum_table table;
fc93b2bd
MD
165};
166
bed864a7
MD
167struct type_class_string {
168 struct type_class p;
169};
170
11796b96
MD
171struct field {
172 GQuark name;
173 struct type_class *type_class;
174};
175
fc93b2bd
MD
176struct type_class_struct {
177 struct type_class p;
11796b96
MD
178 GHashTable *fields_by_name; /* Tuples (field name, field index) */
179 GArray *fields; /* Array of fields */
180};
181
182struct type_class_array {
183 struct type_class p;
184 size_t len;
185 struct type_class *elem;
186};
187
188struct type_class_sequence {
189 struct type_class p;
2e7d72cf 190 struct type_class_integer *len_class;
11796b96 191 struct type_class *elem;
fc93b2bd
MD
192};
193
4c8bfb7e
MD
194struct type_class *lookup_type(GQuark qname);
195int register_type(struct type_class *type_class);
196
197void type_ref(struct type_class *type_class);
198void type_unref(struct type_class *type_class);
698f0fe4 199
0a46062b
MD
200/* Nameless types can be created by passing a NULL name */
201
202struct type_class_integer *integer_type_new(const char *name,
0a46062b 203 size_t len, int byte_order,
11d43b90
MD
204 int signedness,
205 size_t alignment);
0a46062b
MD
206void integer_type_free(struct type_class_integer *int_class);
207
11d43b90
MD
208/*
209 * mantissa_len is the length of the number of bytes represented by the mantissa
210 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
211 */
0a46062b
MD
212struct type_class_float *float_type_new(const char *name,
213 size_t mantissa_len,
214 size_t exp_len, int byte_order,
215 size_t alignment);
216void float_type_free(struct type_class_float *float_class);
217
448d3cc7
MD
218/*
219 * A GQuark can be translated to/from strings with g_quark_from_string() and
220 * g_quark_to_string().
221 */
d65d8abb
MD
222GArray *enum_uint_to_quark_set(const struct type_class_enum *enum_class,
223 uint64_t v);
224
225/*
226 * Returns a GArray or NULL.
227 * Caller must release the GArray with g_array_unref().
228 */
229GArray *enum_int_to_quark_set(const struct type_class_enum *enum_class,
230 uint64_t v);
231GArray *enum_quark_to_range_set(const struct type_class_enum *enum_class,
232 GQuark q);
448d3cc7 233void enum_signed_insert(struct type_class_enum *enum_class,
d65d8abb 234 int64_t start, int64_t end, GQuark q);
448d3cc7 235void enum_unsigned_insert(struct type_class_enum *enum_class,
d65d8abb 236 uint64_t start, uint64_t end, GQuark q);
448d3cc7
MD
237
238struct type_class_enum *enum_type_new(const char *name,
448d3cc7
MD
239 size_t len, int byte_order,
240 int signedness,
241 size_t alignment);
242void enum_type_free(struct type_class_enum *enum_class);
243
11796b96
MD
244struct type_class_struct *struct_type_new(const char *name);
245void struct_type_free(struct type_class_struct *struct_class);
246void struct_type_add_field(struct type_class_struct *struct_class,
4c8bfb7e 247 const char *field_name,
11796b96
MD
248 struct type_class *type_class);
249/*
250 * Returns the index of a field within a structure.
251 */
252unsigned long
253struct_type_lookup_field_index(struct type_class_struct *struct_class,
254 GQuark field_name);
255/*
256 * field returned only valid as long as the field structure is not appended to.
257 */
258struct field *
259struct_type_get_field_from_index(struct type_class_struct *struct_class,
260 unsigned long index);
261
d06d03db
MD
262/*
263 * elem_class passed as parameter now belongs to the array. No need to free it
264 * explicitely.
265 */
11796b96
MD
266struct type_class_array *array_type_new(const char *name,
267 size_t len,
268 struct type_class *elem_class);
269void array_type_free(struct type_class_array *array_class);
270
d06d03db
MD
271/*
272 * int_class and elem_class passed as parameter now belongs to the sequence. No
273 * need to free them explicitely.
274 */
11796b96 275struct type_class_sequence *sequence_type_new(const char *name,
2e7d72cf 276 struct type_class_integer *len_class,
11796b96 277 struct type_class *elem_class);
d06d03db 278void sequence_type_free(struct type_class_sequence *sequence_class);
11796b96 279
d79865b9 280#endif /* _BABELTRACE_TYPES_H */
This page took 0.033958 seconds and 4 git commands to generate.