Rename "declaration" to "definition"
[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;
e1151715 88struct definition;
4c8bfb7e 89
64893f33
MD
90/* type scope */
91struct type_scope {
c13cbf74 92 /* Hash table mapping type name GQuark to "struct declaration" */
e1151715 93 GHashTable *type_definitions;
c13cbf74
MD
94 /* Hash table mapping struct name GQuark to "struct type_struct" */
95 GHashTable *struct_types;
96 /* Hash table mapping variant name GQuark to "struct type_variant" */
97 GHashTable *variant_types;
98 /* Hash table mapping enum name GQuark to "struct type_enum" */
99 GHashTable *enum_types;
64893f33
MD
100 struct type_scope *parent_scope;
101};
102
e1151715
MD
103/* definition scope */
104struct definition_scope {
105 /* Hash table mapping field name GQuark to "struct definition" */
106 GHashTable *definitions;
107 struct definition_scope *parent_scope;
6b71274a
MD
108};
109
05628561
MD
110enum ctf_type_id {
111 CTF_TYPE_UNKNOWN = 0,
112 CTF_TYPE_INTEGER,
113 CTF_TYPE_FLOAT,
114 CTF_TYPE_ENUM,
115 CTF_TYPE_STRING,
116 CTF_TYPE_STRUCT,
117 CTF_TYPE_VARIANT,
118 CTF_TYPE_ARRAY,
119 CTF_TYPE_SEQUENCE,
120 NR_CTF_TYPES,
121};
122
e19c3d69 123struct type {
05628561 124 enum ctf_type_id id;
fc93b2bd
MD
125 GQuark name; /* type name */
126 size_t alignment; /* type alignment, in bits */
e19c3d69 127 int ref; /* number of references to the type */
c054553d
MD
128 /*
129 * type_free called with type ref is decremented to 0.
130 */
131 void (*type_free)(struct type *type);
e1151715
MD
132 struct definition *
133 (*definition_new)(struct type *type,
134 struct definition_scope *parent_scope);
c054553d 135 /*
e1151715 136 * definition_free called with definition ref is decremented to 0.
e19c3d69 137 */
e1151715 138 void (*definition_free)(struct definition *definition);
e19c3d69 139 /*
e1151715
MD
140 * Definition copy function. Knows how to find the child
141 * definition from the parent definition.
fc93b2bd 142 */
4c8bfb7e
MD
143 void (*copy)(struct stream_pos *dest, const struct format *fdest,
144 struct stream_pos *src, const struct format *fsrc,
e1151715 145 struct definition *definition);
c054553d
MD
146};
147
e1151715 148struct definition {
e19c3d69 149 struct type *type;
e1151715 150 int ref; /* number of references to the definition */
fc93b2bd
MD
151};
152
bed864a7
MD
153/*
154 * Because we address in bits, bitfields end up being exactly the same as
155 * integers, except that their read/write functions must be able to deal with
156 * read/write non aligned on CHAR_BIT.
157 */
e19c3d69
MD
158struct type_integer {
159 struct type p;
7fe00194
MD
160 size_t len; /* length, in bits. */
161 int byte_order; /* byte order */
162 int signedness;
fc93b2bd
MD
163};
164
e1151715
MD
165struct definition_integer {
166 struct definition p;
e19c3d69 167 struct type_integer *type;
c054553d
MD
168 /* Last values read */
169 union {
170 uint64_t _unsigned;
171 int64_t _signed;
172 } value;
173};
174
e19c3d69
MD
175struct type_float {
176 struct type p;
177 struct type_integer *sign;
178 struct type_integer *mantissa;
179 struct type_integer *exp;
fc93b2bd 180 int byte_order;
0a46062b 181 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
182};
183
e1151715
MD
184struct definition_float {
185 struct definition p;
e19c3d69 186 struct type_float *type;
c054553d
MD
187 /* Last values read */
188 long double value;
189};
190
d65d8abb
MD
191/*
192 * enum_val_equal assumes that signed and unsigned memory layout overlap.
193 */
194struct enum_range {
195 union {
196 int64_t _signed;
197 uint64_t _unsigned;
198 } start; /* lowest range value */
199 union {
200 int64_t _signed;
201 uint64_t _unsigned;
202 } end; /* highest range value */
203};
204
205struct enum_range_to_quark {
206 struct cds_list_head node;
207 struct enum_range range;
208 GQuark quark;
209};
210
211/*
212 * We optimize the common case (range of size 1: single value) by creating a
213 * hash table mapping values to quark sets. We then lookup the ranges to
214 * complete the quark set.
215 *
216 * TODO: The proper structure to hold the range to quark set mapping would be an
217 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
218 * time. Using a simple O(n) list search for now for implementation speed and
219 * given that we can expect to have a _relatively_ small number of enumeration
220 * ranges. This might become untrue if we are fed with symbol tables often
221 * required to lookup function names from instruction pointer value.
222 */
448d3cc7 223struct enum_table {
d65d8abb
MD
224 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
225 struct cds_list_head range_to_quark; /* (range, GQuark) */
226 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
227};
228
e19c3d69
MD
229struct type_enum {
230 struct type p;
231 struct type_integer *integer_type;
448d3cc7 232 struct enum_table table;
fc93b2bd
MD
233};
234
e1151715
MD
235struct definition_enum {
236 struct definition p;
237 struct definition_integer *integer;
e19c3d69 238 struct type_enum *type;
c054553d
MD
239 /* Last GQuark values read. Keeping a reference on the GQuark array. */
240 GArray *value;
241};
242
c054553d
MD
243struct type_string {
244 struct type p;
c054553d
MD
245};
246
e1151715
MD
247struct definition_string {
248 struct definition p;
e19c3d69 249 struct type_string *type;
e1151715 250 char *value; /* freed at definition_string teardown */
11796b96
MD
251};
252
e19c3d69
MD
253struct type_field {
254 GQuark name;
c054553d
MD
255 struct type *type;
256};
257
e19c3d69 258struct field {
ac88af75 259 GQuark name;
e1151715 260 struct definition *definition;
c054553d
MD
261};
262
263struct type_struct {
264 struct type p;
e19c3d69 265 GHashTable *fields_by_name; /* Tuples (field name, field index) */
64893f33 266 struct type_scope *scope;
e19c3d69
MD
267 GArray *fields; /* Array of type_field */
268};
269
e1151715
MD
270struct definition_struct {
271 struct definition p;
e19c3d69 272 struct type_struct *type;
e1151715 273 struct definition_scope *scope;
c054553d
MD
274 GArray *fields; /* Array of struct field */
275};
276
e19c3d69
MD
277struct type_variant {
278 struct type p;
c054553d 279 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
64893f33 280 struct type_scope *scope;
e19c3d69 281 GArray *fields; /* Array of type_field */
de47353a
MD
282 GQuark tag_name; /* TODO */
283 /* Tag name must be nonzero and must exist when defining the variant */
c054553d
MD
284};
285
e1151715
MD
286struct definition_variant {
287 struct definition p;
e19c3d69 288 struct type_variant *type;
e1151715
MD
289 struct definition_scope *scope;
290 struct definition *enum_tag;
c054553d
MD
291 GArray *fields; /* Array of struct field */
292 struct field *current_field; /* Last field read */
11796b96
MD
293};
294
e19c3d69
MD
295struct type_array {
296 struct type p;
11796b96 297 size_t len;
e19c3d69 298 struct type *elem;
64893f33 299 struct type_scope *scope;
11796b96
MD
300};
301
e1151715
MD
302struct definition_array {
303 struct definition p;
e19c3d69 304 struct type_array *type;
e1151715 305 struct definition_scope *scope;
c054553d
MD
306 struct field current_element; /* struct field */
307};
308
c054553d
MD
309struct type_sequence {
310 struct type p;
e19c3d69
MD
311 struct type_integer *len_type;
312 struct type *elem;
64893f33 313 struct type_scope *scope;
e19c3d69
MD
314};
315
e1151715
MD
316struct definition_sequence {
317 struct definition p;
e19c3d69 318 struct type_sequence *type;
e1151715
MD
319 struct definition_scope *scope;
320 struct definition_integer *len;
c054553d
MD
321 struct field current_element; /* struct field */
322};
323
e1151715
MD
324int register_type(GQuark type_name, struct definition *definition,
325 struct type_scope *scope);
326struct definition *lookup_type(GQuark type_name,
327 struct type_scope *scope);
c13cbf74
MD
328
329/*
330 * Type scopes also contain a separate registry for struct, variant and
e1151715 331 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
332 * that a named variant can be declared without specifying its target
333 * "choice" tag field immediately.
334 */
335int register_struct_type(GQuark struct_name, struct type_struct *struct_type,
336 struct type_scope *scope);
337struct type_struct *lookup_struct_type(GQuark struct_name,
338 struct type_scope *scope);
339int register_variant_type(GQuark variant_name,
340 struct type_variant *variant_type,
341 struct type_scope *scope);
342struct type_variant *lookup_variant_type(GQuark variant_name,
343 struct type_scope *scope);
344int register_enum_type(GQuark enum_name, struct type_enum *enum_type,
345 struct type_scope *scope);
346struct type_enum *lookup_enum_type(GQuark enum_name,
347 struct type_scope *scope);
348
64893f33
MD
349struct type_scope *new_type_scope(struct type_scope *parent_scope);
350void free_type_scope(struct type_scope *scope);
c054553d 351
c13cbf74 352/*
e1151715
MD
353 * field_definition is for field definitions. They are registered into
354 * definition scopes.
c13cbf74 355 */
e1151715
MD
356struct definition *
357 lookup_field_definition(GQuark field_name,
358 struct definition_scope *scope);
359int register_field_definition(GQuark field_name,
360 struct definition *definition,
361 struct definition_scope *scope);
362struct definition_scope *
363 new_definition_scope(struct definition_scope *parent_scope);
364void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 365
64893f33
MD
366void type_ref(struct type *type);
367void type_unref(struct type *type);
368
e1151715
MD
369void definition_ref(struct definition *definition);
370void definition_unref(struct definition *definition);
698f0fe4 371
0a46062b
MD
372/* Nameless types can be created by passing a NULL name */
373
e19c3d69
MD
374struct type_integer *integer_type_new(const char *name,
375 size_t len, int byte_order,
376 int signedness, size_t alignment);
0a46062b 377
11d43b90
MD
378/*
379 * mantissa_len is the length of the number of bytes represented by the mantissa
380 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
381 */
e19c3d69
MD
382struct type_float *float_type_new(const char *name,
383 size_t mantissa_len,
384 size_t exp_len, int byte_order,
385 size_t alignment);
0a46062b 386
448d3cc7
MD
387/*
388 * A GQuark can be translated to/from strings with g_quark_from_string() and
389 * g_quark_to_string().
390 */
47e0f2e2
MD
391
392/*
393 * Returns a GArray of GQuark or NULL.
394 * Caller must release the GArray with g_array_unref().
395 */
e19c3d69 396GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
d65d8abb
MD
397
398/*
47e0f2e2 399 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
400 * Caller must release the GArray with g_array_unref().
401 */
e19c3d69 402GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
47e0f2e2
MD
403
404/*
405 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
406 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
407 * release it).
47e0f2e2 408 */
e19c3d69
MD
409GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
410void enum_signed_insert(struct type_enum *enum_type,
d65d8abb 411 int64_t start, int64_t end, GQuark q);
e19c3d69 412void enum_unsigned_insert(struct type_enum *enum_type,
d65d8abb 413 uint64_t start, uint64_t end, GQuark q);
e19c3d69 414size_t enum_get_nr_enumerators(struct type_enum *enum_type);
448d3cc7 415
e19c3d69
MD
416struct type_enum *enum_type_new(const char *name,
417 struct type_integer *integer_type);
448d3cc7 418
64893f33
MD
419struct type_struct *struct_type_new(const char *name,
420 struct type_scope *parent_scope);
e19c3d69
MD
421void struct_type_add_field(struct type_struct *struct_type,
422 const char *field_name, struct type *field_type);
11796b96
MD
423/*
424 * Returns the index of a field within a structure.
425 */
e19c3d69
MD
426unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
427 GQuark field_name);
11796b96
MD
428/*
429 * field returned only valid as long as the field structure is not appended to.
430 */
e19c3d69
MD
431struct type_field *
432struct_type_get_field_from_index(struct type_struct *struct_type,
11796b96 433 unsigned long index);
e19c3d69 434struct field *
e1151715 435struct_get_field_from_index(struct definition_struct *struct_definition,
e19c3d69 436 unsigned long index);
11796b96 437
c054553d
MD
438/*
439 * The tag enumeration is validated to ensure that it contains only mappings
440 * from numeric values to a single tag. Overlapping tag value ranges are
441 * therefore forbidden.
442 */
64893f33
MD
443struct type_variant *variant_type_new(const char *name,
444 struct type_scope *parent_scope);
e19c3d69
MD
445void variant_type_add_field(struct type_variant *variant_type,
446 const char *tag_name, struct type *tag_type);
447struct type_field *
448variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
c054553d
MD
449/*
450 * Returns 0 on success, -EPERM on error.
451 */
e1151715
MD
452int variant_definition_set_tag(struct definition_variant *variant,
453 struct definition *enum_tag);
c054553d
MD
454/*
455 * Returns the field selected by the current tag value.
456 * field returned only valid as long as the variant structure is not appended
457 * to.
458 */
459struct field *
e1151715 460variant_get_current_field(struct definition_variant *variant);
c054553d 461
d06d03db 462/*
e19c3d69 463 * elem_type passed as parameter now belongs to the array. No need to free it
c054553d 464 * explicitly. "len" is the number of elements in the array.
d06d03db 465 */
e19c3d69 466struct type_array *array_type_new(const char *name,
64893f33
MD
467 size_t len, struct type *elem_type,
468 struct type_scope *parent_scope);
11796b96 469
d06d03db 470/*
e19c3d69 471 * int_type and elem_type passed as parameter now belong to the sequence. No
c054553d 472 * need to free them explicitly.
d06d03db 473 */
e19c3d69
MD
474struct type_sequence *sequence_type_new(const char *name,
475 struct type_integer *len_type,
64893f33
MD
476 struct type *elem_type,
477 struct type_scope *parent_scope);
11796b96 478
d79865b9 479#endif /* _BABELTRACE_TYPES_H */
This page took 0.044733 seconds and 4 git commands to generate.