Rename: type_class, type -> type, declaration
[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 *
c054553d 9 * Copyright 2010, 2011 - 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 {
47e0f2e2 38 char *base; /* Base address */
bed864a7
MD
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41};
42
43static inline
47e0f2e2 44void init_pos(struct stream_pos *pos, char *base)
bed864a7
MD
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
47e0f2e2 80char *get_pos_addr(struct stream_pos *pos)
bed864a7
MD
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 87struct format;
e19c3d69 88struct declaration;
4c8bfb7e 89
6b71274a
MD
90/* Type declaration scope */
91struct declaration_scope {
e19c3d69
MD
92 /* Hash table mapping type name GQuark to struct type */
93 GHashTable *types;
6b71274a
MD
94 struct declaration_scope *parent_scope;
95};
96
e19c3d69 97struct type {
fc93b2bd
MD
98 GQuark name; /* type name */
99 size_t alignment; /* type alignment, in bits */
e19c3d69 100 int ref; /* number of references to the type */
c054553d
MD
101 /*
102 * type_free called with type ref is decremented to 0.
103 */
104 void (*type_free)(struct type *type);
e19c3d69
MD
105 struct declaration *
106 (*declaration_new)(struct type *type,
107 struct declaration_scope *parent_scope);
c054553d 108 /*
e19c3d69
MD
109 * declaration_free called with declaration ref is decremented to 0.
110 */
111 void (*declaration_free)(struct declaration *declaration);
112 /*
113 * Declaration copy function. Knows how to find the child declaration
114 * from the parent declaration.
fc93b2bd 115 */
4c8bfb7e
MD
116 void (*copy)(struct stream_pos *dest, const struct format *fdest,
117 struct stream_pos *src, const struct format *fsrc,
e19c3d69 118 struct declaration *declaration);
c054553d
MD
119};
120
e19c3d69
MD
121struct declaration {
122 struct type *type;
123 int ref; /* number of references to the declaration */
fc93b2bd
MD
124};
125
bed864a7
MD
126/*
127 * Because we address in bits, bitfields end up being exactly the same as
128 * integers, except that their read/write functions must be able to deal with
129 * read/write non aligned on CHAR_BIT.
130 */
e19c3d69
MD
131struct type_integer {
132 struct type p;
7fe00194
MD
133 size_t len; /* length, in bits. */
134 int byte_order; /* byte order */
135 int signedness;
fc93b2bd
MD
136};
137
e19c3d69
MD
138struct declaration_integer {
139 struct declaration p;
140 struct type_integer *type;
c054553d
MD
141 /* Last values read */
142 union {
143 uint64_t _unsigned;
144 int64_t _signed;
145 } value;
146};
147
e19c3d69
MD
148struct type_float {
149 struct type p;
150 struct type_integer *sign;
151 struct type_integer *mantissa;
152 struct type_integer *exp;
fc93b2bd 153 int byte_order;
0a46062b 154 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
155};
156
e19c3d69
MD
157struct declaration_float {
158 struct declaration p;
159 struct type_float *type;
c054553d
MD
160 /* Last values read */
161 long double value;
162};
163
d65d8abb
MD
164/*
165 * enum_val_equal assumes that signed and unsigned memory layout overlap.
166 */
167struct enum_range {
168 union {
169 int64_t _signed;
170 uint64_t _unsigned;
171 } start; /* lowest range value */
172 union {
173 int64_t _signed;
174 uint64_t _unsigned;
175 } end; /* highest range value */
176};
177
178struct enum_range_to_quark {
179 struct cds_list_head node;
180 struct enum_range range;
181 GQuark quark;
182};
183
184/*
185 * We optimize the common case (range of size 1: single value) by creating a
186 * hash table mapping values to quark sets. We then lookup the ranges to
187 * complete the quark set.
188 *
189 * TODO: The proper structure to hold the range to quark set mapping would be an
190 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
191 * time. Using a simple O(n) list search for now for implementation speed and
192 * given that we can expect to have a _relatively_ small number of enumeration
193 * ranges. This might become untrue if we are fed with symbol tables often
194 * required to lookup function names from instruction pointer value.
195 */
448d3cc7 196struct enum_table {
d65d8abb
MD
197 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
198 struct cds_list_head range_to_quark; /* (range, GQuark) */
199 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
200};
201
e19c3d69
MD
202struct type_enum {
203 struct type p;
204 struct type_integer *integer_type;
448d3cc7 205 struct enum_table table;
fc93b2bd
MD
206};
207
e19c3d69
MD
208struct declaration_enum {
209 struct declaration p;
210 struct declaration_integer *integer;
211 struct type_enum *type;
c054553d
MD
212 /* Last GQuark values read. Keeping a reference on the GQuark array. */
213 GArray *value;
214};
215
c054553d
MD
216struct type_string {
217 struct type p;
c054553d
MD
218};
219
e19c3d69
MD
220struct declaration_string {
221 struct declaration p;
222 struct type_string *type;
223 char *value; /* freed at declaration_string teardown */
11796b96
MD
224};
225
e19c3d69
MD
226struct type_field {
227 GQuark name;
c054553d
MD
228 struct type *type;
229};
230
e19c3d69
MD
231struct field {
232 struct declaration *type;
c054553d
MD
233};
234
235struct type_struct {
236 struct type p;
e19c3d69
MD
237 GHashTable *fields_by_name; /* Tuples (field name, field index) */
238 GArray *fields; /* Array of type_field */
239};
240
241struct declaration_struct {
242 struct declaration p;
243 struct type_struct *type;
c054553d
MD
244 struct declaration_scope *scope;
245 GArray *fields; /* Array of struct field */
246};
247
e19c3d69
MD
248struct type_variant {
249 struct type p;
c054553d 250 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
e19c3d69 251 GArray *fields; /* Array of type_field */
c054553d
MD
252};
253
e19c3d69
MD
254struct declaration_variant {
255 struct declaration p;
256 struct type_variant *type;
c054553d 257 struct declaration_scope *scope;
e19c3d69 258 struct declaration *tag;
c054553d
MD
259 GArray *fields; /* Array of struct field */
260 struct field *current_field; /* Last field read */
11796b96
MD
261};
262
e19c3d69
MD
263struct type_array {
264 struct type p;
11796b96 265 size_t len;
e19c3d69 266 struct type *elem;
11796b96
MD
267};
268
e19c3d69
MD
269struct declaration_array {
270 struct declaration p;
271 struct type_array *type;
c054553d
MD
272 struct declaration_scope *scope;
273 struct field current_element; /* struct field */
274};
275
c054553d
MD
276struct type_sequence {
277 struct type p;
e19c3d69
MD
278 struct type_integer *len_type;
279 struct type *elem;
280};
281
282struct declaration_sequence {
283 struct declaration p;
284 struct type_sequence *type;
c054553d 285 struct declaration_scope *scope;
e19c3d69 286 struct declaration_integer *len;
c054553d
MD
287 struct field current_element; /* struct field */
288};
289
e19c3d69
MD
290struct type *lookup_type(GQuark qname, struct declaration_scope *scope);
291int register_type(struct type *type, struct declaration_scope *scope);
c054553d 292
e19c3d69
MD
293void type_ref(struct type *type);
294void type_unref(struct type *type);
c054553d
MD
295
296struct declaration_scope *
e19c3d69 297 new_declaration_scope(struct declaration_scope *parent_scope);
c054553d 298void free_declaration_scope(struct declaration_scope *scope);
4c8bfb7e 299
e19c3d69
MD
300void declaration_ref(struct declaration *declaration);
301void declaration_unref(struct declaration *declaration);
698f0fe4 302
0a46062b
MD
303/* Nameless types can be created by passing a NULL name */
304
e19c3d69
MD
305struct type_integer *integer_type_new(const char *name,
306 size_t len, int byte_order,
307 int signedness, size_t alignment);
0a46062b 308
11d43b90
MD
309/*
310 * mantissa_len is the length of the number of bytes represented by the mantissa
311 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
312 */
e19c3d69
MD
313struct type_float *float_type_new(const char *name,
314 size_t mantissa_len,
315 size_t exp_len, int byte_order,
316 size_t alignment);
0a46062b 317
448d3cc7
MD
318/*
319 * A GQuark can be translated to/from strings with g_quark_from_string() and
320 * g_quark_to_string().
321 */
47e0f2e2
MD
322
323/*
324 * Returns a GArray of GQuark or NULL.
325 * Caller must release the GArray with g_array_unref().
326 */
e19c3d69 327GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
d65d8abb
MD
328
329/*
47e0f2e2 330 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
331 * Caller must release the GArray with g_array_unref().
332 */
e19c3d69 333GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
47e0f2e2
MD
334
335/*
336 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
337 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
338 * release it).
47e0f2e2 339 */
e19c3d69
MD
340GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
341void enum_signed_insert(struct type_enum *enum_type,
d65d8abb 342 int64_t start, int64_t end, GQuark q);
e19c3d69 343void enum_unsigned_insert(struct type_enum *enum_type,
d65d8abb 344 uint64_t start, uint64_t end, GQuark q);
e19c3d69 345size_t enum_get_nr_enumerators(struct type_enum *enum_type);
448d3cc7 346
e19c3d69
MD
347struct type_enum *enum_type_new(const char *name,
348 struct type_integer *integer_type);
448d3cc7 349
e19c3d69
MD
350struct type_struct *struct_type_new(const char *name);
351void struct_type_add_field(struct type_struct *struct_type,
352 const char *field_name, struct type *field_type);
11796b96
MD
353/*
354 * Returns the index of a field within a structure.
355 */
e19c3d69
MD
356unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
357 GQuark field_name);
11796b96
MD
358/*
359 * field returned only valid as long as the field structure is not appended to.
360 */
e19c3d69
MD
361struct type_field *
362struct_type_get_field_from_index(struct type_struct *struct_type,
11796b96 363 unsigned long index);
e19c3d69
MD
364struct field *
365struct_get_field_from_index(struct declaration_struct *struct_declaration,
366 unsigned long index);
11796b96 367
c054553d
MD
368/*
369 * The tag enumeration is validated to ensure that it contains only mappings
370 * from numeric values to a single tag. Overlapping tag value ranges are
371 * therefore forbidden.
372 */
e19c3d69
MD
373struct type_variant *variant_type_new(const char *name);
374void variant_type_add_field(struct type_variant *variant_type,
375 const char *tag_name, struct type *tag_type);
376struct type_field *
377variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
c054553d
MD
378/*
379 * Returns 0 on success, -EPERM on error.
380 */
e19c3d69
MD
381int variant_declaration_set_tag(struct declaration_variant *variant,
382 struct declaration *enum_tag);
c054553d
MD
383/*
384 * Returns the field selected by the current tag value.
385 * field returned only valid as long as the variant structure is not appended
386 * to.
387 */
388struct field *
e19c3d69 389variant_get_current_field(struct declaration_variant *variant);
c054553d 390
d06d03db 391/*
e19c3d69 392 * elem_type passed as parameter now belongs to the array. No need to free it
c054553d 393 * explicitly. "len" is the number of elements in the array.
d06d03db 394 */
e19c3d69
MD
395struct type_array *array_type_new(const char *name,
396 size_t len, struct type *elem_type);
11796b96 397
d06d03db 398/*
e19c3d69 399 * int_type and elem_type passed as parameter now belong to the sequence. No
c054553d 400 * need to free them explicitly.
d06d03db 401 */
e19c3d69
MD
402struct type_sequence *sequence_type_new(const char *name,
403 struct type_integer *len_type,
404 struct type *elem_type);
11796b96 405
d79865b9 406#endif /* _BABELTRACE_TYPES_H */
This page took 0.04105 seconds and 4 git commands to generate.