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 *
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);
c054553d
MD
98};
99
e1151715 100struct definition {
f6625916 101 struct declaration *declaration;
05c749e5 102 int index; /* Position of the definition in its container */
e1151715 103 int ref; /* number of references to the definition */
fc93b2bd
MD
104};
105
d11e9c49
MD
106typedef void (*rw_dispatch)(struct stream_pos *pos,
107 struct definition *definition);
108
109/* Parent of per-plugin positions */
110struct stream_pos {
111 /* read/write dispatch table. Specific to plugin used for stream. */
112 rw_dispatch *rw_table; /* rw dispatch table */
113};
114
115static inline
116void generic_rw(struct stream_pos *pos, struct definition *definition)
117{
118 enum ctf_type_id dispatch_id = definition->declaration->id;
119 rw_dispatch call;
120
121 assert(pos->rw_table[dispatch_id] != NULL);
122 call = pos->rw_table[dispatch_id];
123 call(pos, definition);
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 */
f6625916
MD
131struct declaration_integer {
132 struct declaration p;
7fe00194
MD
133 size_t len; /* length, in bits. */
134 int byte_order; /* byte order */
135 int signedness;
fc93b2bd
MD
136};
137
e1151715
MD
138struct definition_integer {
139 struct definition p;
f6625916 140 struct declaration_integer *declaration;
c054553d
MD
141 /* Last values read */
142 union {
143 uint64_t _unsigned;
144 int64_t _signed;
145 } value;
146};
147
f6625916
MD
148struct declaration_float {
149 struct declaration p;
150 struct declaration_integer *sign;
151 struct declaration_integer *mantissa;
152 struct declaration_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
e1151715
MD
157struct definition_float {
158 struct definition p;
f6625916 159 struct declaration_float *declaration;
d11e9c49
MD
160 struct definition_integer *sign;
161 struct definition_integer *mantissa;
162 struct definition_integer *exp;
c054553d
MD
163 /* Last values read */
164 long double value;
165};
166
d65d8abb
MD
167/*
168 * enum_val_equal assumes that signed and unsigned memory layout overlap.
169 */
170struct enum_range {
171 union {
172 int64_t _signed;
173 uint64_t _unsigned;
174 } start; /* lowest range value */
175 union {
176 int64_t _signed;
177 uint64_t _unsigned;
178 } end; /* highest range value */
179};
180
181struct enum_range_to_quark {
182 struct cds_list_head node;
183 struct enum_range range;
184 GQuark quark;
185};
186
187/*
188 * We optimize the common case (range of size 1: single value) by creating a
189 * hash table mapping values to quark sets. We then lookup the ranges to
190 * complete the quark set.
191 *
192 * TODO: The proper structure to hold the range to quark set mapping would be an
193 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
194 * time. Using a simple O(n) list search for now for implementation speed and
195 * given that we can expect to have a _relatively_ small number of enumeration
196 * ranges. This might become untrue if we are fed with symbol tables often
197 * required to lookup function names from instruction pointer value.
198 */
448d3cc7 199struct enum_table {
d65d8abb
MD
200 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
201 struct cds_list_head range_to_quark; /* (range, GQuark) */
202 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
203};
204
f6625916
MD
205struct declaration_enum {
206 struct declaration p;
207 struct declaration_integer *integer_declaration;
448d3cc7 208 struct enum_table table;
fc93b2bd
MD
209};
210
e1151715
MD
211struct definition_enum {
212 struct definition p;
213 struct definition_integer *integer;
f6625916 214 struct declaration_enum *declaration;
c054553d
MD
215 /* Last GQuark values read. Keeping a reference on the GQuark array. */
216 GArray *value;
217};
218
ab4cf058
MD
219enum ctf_string_encoding {
220 CTF_STRING_UTF8 = 0,
221 CTF_STRING_ASCII,
222 CTF_STRING_UNKNOWN,
223};
224
f6625916
MD
225struct declaration_string {
226 struct declaration p;
ab4cf058 227 enum ctf_string_encoding encoding;
c054553d
MD
228};
229
e1151715
MD
230struct definition_string {
231 struct definition p;
f6625916 232 struct declaration_string *declaration;
e1151715 233 char *value; /* freed at definition_string teardown */
d11e9c49 234 size_t len, alloc_len;
11796b96
MD
235};
236
f6625916 237struct declaration_field {
e19c3d69 238 GQuark name;
f6625916 239 struct declaration *declaration;
c054553d
MD
240};
241
e19c3d69 242struct field {
ac88af75 243 GQuark name;
e1151715 244 struct definition *definition;
c054553d
MD
245};
246
f6625916
MD
247struct declaration_struct {
248 struct declaration p;
e19c3d69 249 GHashTable *fields_by_name; /* Tuples (field name, field index) */
f6625916
MD
250 struct declaration_scope *scope;
251 GArray *fields; /* Array of declaration_field */
e19c3d69
MD
252};
253
e1151715
MD
254struct definition_struct {
255 struct definition p;
f6625916 256 struct declaration_struct *declaration;
e1151715 257 struct definition_scope *scope;
c054553d
MD
258 GArray *fields; /* Array of struct field */
259};
260
d60cb676 261struct declaration_untagged_variant {
f6625916 262 struct declaration p;
c054553d 263 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
f6625916
MD
264 struct declaration_scope *scope;
265 GArray *fields; /* Array of declaration_field */
c054553d
MD
266};
267
d60cb676
MD
268struct declaration_variant {
269 struct declaration p;
270 struct declaration_untagged_variant *untagged_variant;
271 GArray *tag_name; /* Array of GQuark */
272};
273
274/* A variant needs to be tagged to be defined. */
e1151715
MD
275struct definition_variant {
276 struct definition p;
f6625916 277 struct declaration_variant *declaration;
e1151715
MD
278 struct definition_scope *scope;
279 struct definition *enum_tag;
c054553d
MD
280 GArray *fields; /* Array of struct field */
281 struct field *current_field; /* Last field read */
11796b96
MD
282};
283
f6625916
MD
284struct declaration_array {
285 struct declaration p;
11796b96 286 size_t len;
f6625916
MD
287 struct declaration *elem;
288 struct declaration_scope *scope;
11796b96
MD
289};
290
e1151715
MD
291struct definition_array {
292 struct definition p;
f6625916 293 struct declaration_array *declaration;
e1151715 294 struct definition_scope *scope;
0f980a35 295 GArray *elems; /* struct field */
c054553d
MD
296};
297
f6625916
MD
298struct declaration_sequence {
299 struct declaration p;
300 struct declaration_integer *len_declaration;
301 struct declaration *elem;
302 struct declaration_scope *scope;
e19c3d69
MD
303};
304
e1151715
MD
305struct definition_sequence {
306 struct definition p;
f6625916 307 struct declaration_sequence *declaration;
e1151715
MD
308 struct definition_scope *scope;
309 struct definition_integer *len;
0f980a35 310 GArray *elems; /* struct field */
c054553d
MD
311};
312
f6625916
MD
313int register_declaration(GQuark declaration_name,
314 struct declaration *declaration,
315 struct declaration_scope *scope);
316struct declaration *lookup_declaration(GQuark declaration_name,
78af2bcd 317 struct declaration_scope *scope);
c13cbf74
MD
318
319/*
320 * Type scopes also contain a separate registry for struct, variant and
e1151715 321 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
322 * that a named variant can be declared without specifying its target
323 * "choice" tag field immediately.
324 */
f6625916
MD
325int register_struct_declaration(GQuark struct_name,
326 struct declaration_struct *struct_declaration,
327 struct declaration_scope *scope);
328struct declaration_struct *
329 lookup_struct_declaration(GQuark struct_name,
330 struct declaration_scope *scope);
331int register_variant_declaration(GQuark variant_name,
a0720417 332 struct declaration_untagged_variant *untagged_variant_declaration,
f6625916 333 struct declaration_scope *scope);
a0720417 334struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name,
f6625916
MD
335 struct declaration_scope *scope);
336int register_enum_declaration(GQuark enum_name,
337 struct declaration_enum *enum_declaration,
338 struct declaration_scope *scope);
339struct declaration_enum *
340 lookup_enum_declaration(GQuark enum_name,
341 struct declaration_scope *scope);
342
343struct declaration_scope *
344 new_declaration_scope(struct declaration_scope *parent_scope);
345void free_declaration_scope(struct declaration_scope *scope);
c054553d 346
c13cbf74 347/*
e1151715
MD
348 * field_definition is for field definitions. They are registered into
349 * definition scopes.
c13cbf74 350 */
e1151715 351struct definition *
05c749e5
MD
352 lookup_definition(GArray *cur_path, /* array of GQuark */
353 GArray *lookup_path, /* array of GQuark */
354 struct definition_scope *scope);
e1151715
MD
355int register_field_definition(GQuark field_name,
356 struct definition *definition,
357 struct definition_scope *scope);
358struct definition_scope *
05c749e5
MD
359 new_definition_scope(struct definition_scope *parent_scope,
360 GQuark field_name);
6a36ddca
MD
361void set_dynamic_definition_scope(struct definition *definition,
362 struct definition_scope *scope,
d00d17d1 363 const char *root_name);
e1151715 364void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 365
f6625916
MD
366void declaration_ref(struct declaration *declaration);
367void declaration_unref(struct declaration *declaration);
64893f33 368
e1151715
MD
369void definition_ref(struct definition *definition);
370void definition_unref(struct definition *definition);
698f0fe4 371
add40b62 372struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
e19c3d69 373 int signedness, size_t alignment);
0a46062b 374
11d43b90
MD
375/*
376 * mantissa_len is the length of the number of bytes represented by the mantissa
377 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
378 */
add40b62 379struct declaration_float *float_declaration_new(size_t mantissa_len,
e19c3d69
MD
380 size_t exp_len, int byte_order,
381 size_t alignment);
0a46062b 382
448d3cc7
MD
383/*
384 * A GQuark can be translated to/from strings with g_quark_from_string() and
385 * g_quark_to_string().
386 */
47e0f2e2
MD
387
388/*
389 * Returns a GArray of GQuark or NULL.
390 * Caller must release the GArray with g_array_unref().
391 */
f6625916
MD
392GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
393 uint64_t v);
d65d8abb
MD
394
395/*
47e0f2e2 396 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
397 * Caller must release the GArray with g_array_unref().
398 */
f6625916
MD
399GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
400 uint64_t v);
47e0f2e2
MD
401
402/*
403 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
404 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
405 * release it).
47e0f2e2 406 */
f6625916
MD
407GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
408 GQuark q);
409void enum_signed_insert(struct declaration_enum *enum_declaration,
d65d8abb 410 int64_t start, int64_t end, GQuark q);
f6625916 411void enum_unsigned_insert(struct declaration_enum *enum_declaration,
d65d8abb 412 uint64_t start, uint64_t end, GQuark q);
f6625916
MD
413size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
414
415struct declaration_enum *
ab4cf058 416 enum_declaration_new(struct declaration_integer *integer_declaration);
f6625916 417
e397791f
MD
418struct declaration_string *
419 string_declaration_new(enum ctf_string_encoding encoding);
420
f6625916 421struct declaration_struct *
ab4cf058 422 struct_declaration_new(struct declaration_scope *parent_scope);
f6625916
MD
423void struct_declaration_add_field(struct declaration_struct *struct_declaration,
424 const char *field_name,
425 struct declaration *field_declaration);
11796b96
MD
426/*
427 * Returns the index of a field within a structure.
428 */
0f980a35 429int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
f6625916 430 GQuark field_name);
11796b96
MD
431/*
432 * field returned only valid as long as the field structure is not appended to.
433 */
f6625916
MD
434struct declaration_field *
435struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 436 int index);
e19c3d69 437struct field *
0f980a35
MD
438struct_definition_get_field_from_index(struct definition_struct *struct_definition,
439 int index);
d11e9c49 440void struct_rw(struct stream_pos *pos, struct definition *definition);
11796b96 441
c054553d
MD
442/*
443 * The tag enumeration is validated to ensure that it contains only mappings
444 * from numeric values to a single tag. Overlapping tag value ranges are
445 * therefore forbidden.
446 */
ab4cf058 447struct declaration_untagged_variant *untagged_variant_declaration_new(
1934b94f 448 struct declaration_scope *parent_scope);
d60cb676
MD
449struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
450 const char *tag);
451
452void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
453 const char *field_name,
454 struct declaration *field_declaration);
f6625916 455struct declaration_field *
d60cb676 456 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
1934b94f 457 GQuark tag);
c054553d
MD
458/*
459 * Returns 0 on success, -EPERM on error.
460 */
e1151715
MD
461int variant_definition_set_tag(struct definition_variant *variant,
462 struct definition *enum_tag);
c054553d
MD
463/*
464 * Returns the field selected by the current tag value.
465 * field returned only valid as long as the variant structure is not appended
466 * to.
467 */
f6625916 468struct field *variant_get_current_field(struct definition_variant *variant);
d11e9c49 469void variant_rw(struct stream_pos *pos, struct definition *definition);
c054553d 470
d06d03db 471/*
f6625916
MD
472 * elem_declaration passed as parameter now belongs to the array. No
473 * need to free it explicitly. "len" is the number of elements in the
474 * array.
d06d03db 475 */
f6625916 476struct declaration_array *
ab4cf058 477 array_declaration_new(size_t len, struct declaration *elem_declaration,
1934b94f 478 struct declaration_scope *parent_scope);
3838df27 479uint64_t array_len(struct definition_array *array);
0f980a35 480struct definition *array_index(struct definition_array *array, uint64_t i);
d11e9c49 481void array_rw(struct stream_pos *pos, struct definition *definition);
11796b96 482
d06d03db 483/*
f6625916
MD
484 * int_declaration and elem_declaration passed as parameter now belong
485 * to the sequence. No need to free them explicitly.
d06d03db 486 */
f6625916 487struct declaration_sequence *
ab4cf058 488 sequence_declaration_new(struct declaration_integer *len_declaration,
1934b94f
MD
489 struct declaration *elem_declaration,
490 struct declaration_scope *parent_scope);
3838df27 491uint64_t sequence_len(struct definition_sequence *sequence);
0f980a35 492struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i);
d11e9c49 493void sequence_rw(struct stream_pos *pos, struct definition *definition);
11796b96 494
d60cb676
MD
495/*
496 * in: path (dot separated), out: q (GArray of GQuark)
497 */
498void append_scope_path(const char *path, GArray *q);
499
f6625916 500#endif /* _BABELTRACE_declarationS_H */
This page took 0.04862 seconds and 4 git commands to generate.