Add single-stream tests
[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
dd2544fd 34struct stream_pos;
4c8bfb7e 35struct format;
e1151715 36struct definition;
4c8bfb7e 37
64893f33 38/* type scope */
f6625916 39struct declaration_scope {
c13cbf74 40 /* Hash table mapping type name GQuark to "struct declaration" */
1ee8e81d 41 /* Used for both typedef and typealias. */
f6625916
MD
42 GHashTable *typedef_declarations;
43 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
44 GHashTable *struct_declarations;
45 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
46 GHashTable *variant_declarations;
c13cbf74 47 /* Hash table mapping enum name GQuark to "struct type_enum" */
f6625916
MD
48 GHashTable *enum_declarations;
49 struct declaration_scope *parent_scope;
64893f33
MD
50};
51
e1151715
MD
52/* definition scope */
53struct definition_scope {
54 /* Hash table mapping field name GQuark to "struct definition" */
55 GHashTable *definitions;
56 struct definition_scope *parent_scope;
05c749e5
MD
57 /*
58 * Complete "path" leading to this definition scope.
9e29e16e 59 * Includes dynamic scope name '.' field name '.' field name '.' ....
05c749e5 60 * Array of GQuark elements (which are each separated by dots).
9e29e16e
MD
61 * The dynamic scope name can contain dots, and is encoded into
62 * a single GQuark. Thus, scope_path[0] returns the GQuark
63 * identifying the dynamic scope.
05c749e5
MD
64 */
65 GArray *scope_path; /* array of GQuark */
6b71274a
MD
66};
67
05628561
MD
68enum ctf_type_id {
69 CTF_TYPE_UNKNOWN = 0,
70 CTF_TYPE_INTEGER,
71 CTF_TYPE_FLOAT,
72 CTF_TYPE_ENUM,
73 CTF_TYPE_STRING,
74 CTF_TYPE_STRUCT,
d60cb676 75 CTF_TYPE_UNTAGGED_VARIANT,
05628561
MD
76 CTF_TYPE_VARIANT,
77 CTF_TYPE_ARRAY,
78 CTF_TYPE_SEQUENCE,
79 NR_CTF_TYPES,
80};
81
f6625916 82struct declaration {
05628561 83 enum ctf_type_id id;
fc93b2bd 84 size_t alignment; /* type alignment, in bits */
e19c3d69 85 int ref; /* number of references to the type */
c054553d 86 /*
f6625916 87 * declaration_free called with declaration ref is decremented to 0.
c054553d 88 */
f6625916 89 void (*declaration_free)(struct declaration *declaration);
e1151715 90 struct definition *
f6625916 91 (*definition_new)(struct declaration *declaration,
05c749e5
MD
92 struct definition_scope *parent_scope,
93 GQuark field_name, int index);
c054553d 94 /*
e1151715 95 * definition_free called with definition ref is decremented to 0.
e19c3d69 96 */
e1151715 97 void (*definition_free)(struct definition *definition);
e19c3d69 98 /*
e1151715
MD
99 * Definition copy function. Knows how to find the child
100 * definition from the parent definition.
fc93b2bd 101 */
4c8bfb7e
MD
102 void (*copy)(struct stream_pos *dest, const struct format *fdest,
103 struct stream_pos *src, const struct format *fsrc,
e1151715 104 struct definition *definition);
c054553d
MD
105};
106
e1151715 107struct definition {
f6625916 108 struct declaration *declaration;
05c749e5 109 int index; /* Position of the definition in its container */
e1151715 110 int ref; /* number of references to the definition */
fc93b2bd
MD
111};
112
bed864a7
MD
113/*
114 * Because we address in bits, bitfields end up being exactly the same as
115 * integers, except that their read/write functions must be able to deal with
116 * read/write non aligned on CHAR_BIT.
117 */
f6625916
MD
118struct declaration_integer {
119 struct declaration p;
7fe00194
MD
120 size_t len; /* length, in bits. */
121 int byte_order; /* byte order */
122 int signedness;
fc93b2bd
MD
123};
124
e1151715
MD
125struct definition_integer {
126 struct definition p;
f6625916 127 struct declaration_integer *declaration;
c054553d
MD
128 /* Last values read */
129 union {
130 uint64_t _unsigned;
131 int64_t _signed;
132 } value;
133};
134
f6625916
MD
135struct declaration_float {
136 struct declaration p;
137 struct declaration_integer *sign;
138 struct declaration_integer *mantissa;
139 struct declaration_integer *exp;
fc93b2bd 140 int byte_order;
0a46062b 141 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
142};
143
e1151715
MD
144struct definition_float {
145 struct definition p;
f6625916 146 struct declaration_float *declaration;
c054553d
MD
147 /* Last values read */
148 long double value;
149};
150
d65d8abb
MD
151/*
152 * enum_val_equal assumes that signed and unsigned memory layout overlap.
153 */
154struct enum_range {
155 union {
156 int64_t _signed;
157 uint64_t _unsigned;
158 } start; /* lowest range value */
159 union {
160 int64_t _signed;
161 uint64_t _unsigned;
162 } end; /* highest range value */
163};
164
165struct enum_range_to_quark {
166 struct cds_list_head node;
167 struct enum_range range;
168 GQuark quark;
169};
170
171/*
172 * We optimize the common case (range of size 1: single value) by creating a
173 * hash table mapping values to quark sets. We then lookup the ranges to
174 * complete the quark set.
175 *
176 * TODO: The proper structure to hold the range to quark set mapping would be an
177 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
178 * time. Using a simple O(n) list search for now for implementation speed and
179 * given that we can expect to have a _relatively_ small number of enumeration
180 * ranges. This might become untrue if we are fed with symbol tables often
181 * required to lookup function names from instruction pointer value.
182 */
448d3cc7 183struct enum_table {
d65d8abb
MD
184 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
185 struct cds_list_head range_to_quark; /* (range, GQuark) */
186 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
187};
188
f6625916
MD
189struct declaration_enum {
190 struct declaration p;
191 struct declaration_integer *integer_declaration;
448d3cc7 192 struct enum_table table;
fc93b2bd
MD
193};
194
e1151715
MD
195struct definition_enum {
196 struct definition p;
197 struct definition_integer *integer;
f6625916 198 struct declaration_enum *declaration;
c054553d
MD
199 /* Last GQuark values read. Keeping a reference on the GQuark array. */
200 GArray *value;
201};
202
ab4cf058
MD
203enum ctf_string_encoding {
204 CTF_STRING_UTF8 = 0,
205 CTF_STRING_ASCII,
206 CTF_STRING_UNKNOWN,
207};
208
f6625916
MD
209struct declaration_string {
210 struct declaration p;
ab4cf058 211 enum ctf_string_encoding encoding;
c054553d
MD
212};
213
e1151715
MD
214struct definition_string {
215 struct definition p;
f6625916 216 struct declaration_string *declaration;
e1151715 217 char *value; /* freed at definition_string teardown */
11796b96
MD
218};
219
f6625916 220struct declaration_field {
e19c3d69 221 GQuark name;
f6625916 222 struct declaration *declaration;
c054553d
MD
223};
224
e19c3d69 225struct field {
ac88af75 226 GQuark name;
e1151715 227 struct definition *definition;
c054553d
MD
228};
229
f6625916
MD
230struct declaration_struct {
231 struct declaration p;
e19c3d69 232 GHashTable *fields_by_name; /* Tuples (field name, field index) */
f6625916
MD
233 struct declaration_scope *scope;
234 GArray *fields; /* Array of declaration_field */
e19c3d69
MD
235};
236
e1151715
MD
237struct definition_struct {
238 struct definition p;
f6625916 239 struct declaration_struct *declaration;
e1151715 240 struct definition_scope *scope;
c054553d
MD
241 GArray *fields; /* Array of struct field */
242};
243
d60cb676 244struct declaration_untagged_variant {
f6625916 245 struct declaration p;
c054553d 246 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
f6625916
MD
247 struct declaration_scope *scope;
248 GArray *fields; /* Array of declaration_field */
c054553d
MD
249};
250
d60cb676
MD
251struct declaration_variant {
252 struct declaration p;
253 struct declaration_untagged_variant *untagged_variant;
254 GArray *tag_name; /* Array of GQuark */
255};
256
257/* A variant needs to be tagged to be defined. */
e1151715
MD
258struct definition_variant {
259 struct definition p;
f6625916 260 struct declaration_variant *declaration;
e1151715
MD
261 struct definition_scope *scope;
262 struct definition *enum_tag;
c054553d
MD
263 GArray *fields; /* Array of struct field */
264 struct field *current_field; /* Last field read */
11796b96
MD
265};
266
f6625916
MD
267struct declaration_array {
268 struct declaration p;
11796b96 269 size_t len;
f6625916
MD
270 struct declaration *elem;
271 struct declaration_scope *scope;
11796b96
MD
272};
273
e1151715
MD
274struct definition_array {
275 struct definition p;
f6625916 276 struct declaration_array *declaration;
e1151715 277 struct definition_scope *scope;
0f980a35 278 GArray *elems; /* struct field */
c054553d
MD
279};
280
f6625916
MD
281struct declaration_sequence {
282 struct declaration p;
283 struct declaration_integer *len_declaration;
284 struct declaration *elem;
285 struct declaration_scope *scope;
e19c3d69
MD
286};
287
e1151715
MD
288struct definition_sequence {
289 struct definition p;
f6625916 290 struct declaration_sequence *declaration;
e1151715
MD
291 struct definition_scope *scope;
292 struct definition_integer *len;
0f980a35 293 GArray *elems; /* struct field */
c054553d
MD
294};
295
f6625916
MD
296int register_declaration(GQuark declaration_name,
297 struct declaration *declaration,
298 struct declaration_scope *scope);
299struct declaration *lookup_declaration(GQuark declaration_name,
78af2bcd 300 struct declaration_scope *scope);
c13cbf74
MD
301
302/*
303 * Type scopes also contain a separate registry for struct, variant and
e1151715 304 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
305 * that a named variant can be declared without specifying its target
306 * "choice" tag field immediately.
307 */
f6625916
MD
308int register_struct_declaration(GQuark struct_name,
309 struct declaration_struct *struct_declaration,
310 struct declaration_scope *scope);
311struct declaration_struct *
312 lookup_struct_declaration(GQuark struct_name,
313 struct declaration_scope *scope);
314int register_variant_declaration(GQuark variant_name,
a0720417 315 struct declaration_untagged_variant *untagged_variant_declaration,
f6625916 316 struct declaration_scope *scope);
a0720417 317struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name,
f6625916
MD
318 struct declaration_scope *scope);
319int register_enum_declaration(GQuark enum_name,
320 struct declaration_enum *enum_declaration,
321 struct declaration_scope *scope);
322struct declaration_enum *
323 lookup_enum_declaration(GQuark enum_name,
324 struct declaration_scope *scope);
325
326struct declaration_scope *
327 new_declaration_scope(struct declaration_scope *parent_scope);
328void free_declaration_scope(struct declaration_scope *scope);
c054553d 329
c13cbf74 330/*
e1151715
MD
331 * field_definition is for field definitions. They are registered into
332 * definition scopes.
c13cbf74 333 */
e1151715 334struct definition *
05c749e5
MD
335 lookup_definition(GArray *cur_path, /* array of GQuark */
336 GArray *lookup_path, /* array of GQuark */
337 struct definition_scope *scope);
e1151715
MD
338int register_field_definition(GQuark field_name,
339 struct definition *definition,
340 struct definition_scope *scope);
341struct definition_scope *
05c749e5
MD
342 new_definition_scope(struct definition_scope *parent_scope,
343 GQuark field_name);
6a36ddca
MD
344void set_dynamic_definition_scope(struct definition *definition,
345 struct definition_scope *scope,
d00d17d1 346 const char *root_name);
e1151715 347void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 348
f6625916
MD
349void declaration_ref(struct declaration *declaration);
350void declaration_unref(struct declaration *declaration);
64893f33 351
e1151715
MD
352void definition_ref(struct definition *definition);
353void definition_unref(struct definition *definition);
698f0fe4 354
add40b62 355struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
e19c3d69 356 int signedness, size_t alignment);
0a46062b 357
11d43b90
MD
358/*
359 * mantissa_len is the length of the number of bytes represented by the mantissa
360 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
361 */
add40b62 362struct declaration_float *float_declaration_new(size_t mantissa_len,
e19c3d69
MD
363 size_t exp_len, int byte_order,
364 size_t alignment);
0a46062b 365
448d3cc7
MD
366/*
367 * A GQuark can be translated to/from strings with g_quark_from_string() and
368 * g_quark_to_string().
369 */
47e0f2e2
MD
370
371/*
372 * Returns a GArray of GQuark or NULL.
373 * Caller must release the GArray with g_array_unref().
374 */
f6625916
MD
375GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
376 uint64_t v);
d65d8abb
MD
377
378/*
47e0f2e2 379 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
380 * Caller must release the GArray with g_array_unref().
381 */
f6625916
MD
382GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
383 uint64_t v);
47e0f2e2
MD
384
385/*
386 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
387 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
388 * release it).
47e0f2e2 389 */
f6625916
MD
390GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
391 GQuark q);
392void enum_signed_insert(struct declaration_enum *enum_declaration,
d65d8abb 393 int64_t start, int64_t end, GQuark q);
f6625916 394void enum_unsigned_insert(struct declaration_enum *enum_declaration,
d65d8abb 395 uint64_t start, uint64_t end, GQuark q);
f6625916
MD
396size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
397
398struct declaration_enum *
ab4cf058 399 enum_declaration_new(struct declaration_integer *integer_declaration);
f6625916 400
e397791f
MD
401struct declaration_string *
402 string_declaration_new(enum ctf_string_encoding encoding);
403
f6625916 404struct declaration_struct *
ab4cf058 405 struct_declaration_new(struct declaration_scope *parent_scope);
f6625916
MD
406void struct_declaration_add_field(struct declaration_struct *struct_declaration,
407 const char *field_name,
408 struct declaration *field_declaration);
11796b96
MD
409/*
410 * Returns the index of a field within a structure.
411 */
0f980a35 412int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
f6625916 413 GQuark field_name);
11796b96
MD
414/*
415 * field returned only valid as long as the field structure is not appended to.
416 */
f6625916
MD
417struct declaration_field *
418struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 419 int index);
e19c3d69 420struct field *
0f980a35
MD
421struct_definition_get_field_from_index(struct definition_struct *struct_definition,
422 int index);
11796b96 423
c054553d
MD
424/*
425 * The tag enumeration is validated to ensure that it contains only mappings
426 * from numeric values to a single tag. Overlapping tag value ranges are
427 * therefore forbidden.
428 */
ab4cf058 429struct declaration_untagged_variant *untagged_variant_declaration_new(
1934b94f 430 struct declaration_scope *parent_scope);
d60cb676
MD
431struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
432 const char *tag);
433
434void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
435 const char *field_name,
436 struct declaration *field_declaration);
f6625916 437struct declaration_field *
d60cb676 438 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
1934b94f 439 GQuark tag);
c054553d
MD
440/*
441 * Returns 0 on success, -EPERM on error.
442 */
e1151715
MD
443int variant_definition_set_tag(struct definition_variant *variant,
444 struct definition *enum_tag);
c054553d
MD
445/*
446 * Returns the field selected by the current tag value.
447 * field returned only valid as long as the variant structure is not appended
448 * to.
449 */
f6625916 450struct field *variant_get_current_field(struct definition_variant *variant);
c054553d 451
d06d03db 452/*
f6625916
MD
453 * elem_declaration passed as parameter now belongs to the array. No
454 * need to free it explicitly. "len" is the number of elements in the
455 * array.
d06d03db 456 */
f6625916 457struct declaration_array *
ab4cf058 458 array_declaration_new(size_t len, struct declaration *elem_declaration,
1934b94f 459 struct declaration_scope *parent_scope);
3838df27 460uint64_t array_len(struct definition_array *array);
0f980a35 461struct definition *array_index(struct definition_array *array, uint64_t i);
11796b96 462
d06d03db 463/*
f6625916
MD
464 * int_declaration and elem_declaration passed as parameter now belong
465 * to the sequence. No need to free them explicitly.
d06d03db 466 */
f6625916 467struct declaration_sequence *
ab4cf058 468 sequence_declaration_new(struct declaration_integer *len_declaration,
1934b94f
MD
469 struct declaration *elem_declaration,
470 struct declaration_scope *parent_scope);
3838df27 471uint64_t sequence_len(struct definition_sequence *sequence);
0f980a35 472struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i);
11796b96 473
d60cb676
MD
474/*
475 * in: path (dot separated), out: q (GArray of GQuark)
476 */
477void append_scope_path(const char *path, GArray *q);
478
f6625916 479#endif /* _BABELTRACE_declarationS_H */
This page took 0.04813 seconds and 4 git commands to generate.