namespace the variant functions
[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 *
64fa3fec
MD
9 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
10 *
11 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d79865b9 12 *
ccd7e1c8
MD
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
d79865b9 19 *
ccd7e1c8
MD
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
c462e188
MD
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
d79865b9
MD
30 */
31
bed864a7 32#include <babeltrace/align.h>
d65d8abb 33#include <babeltrace/list.h>
9843982d 34#include <babeltrace/ctf/events.h>
4c8bfb7e
MD
35#include <stdbool.h>
36#include <stdint.h>
37#include <limits.h>
bed864a7 38#include <string.h>
4c8bfb7e 39#include <glib.h>
8eab883c 40#include <assert.h>
bed864a7 41
11796b96
MD
42/* Preallocate this many fields for structures */
43#define DEFAULT_NR_STRUCT_FIELDS 8
44
9e88d150 45struct ctf_stream_definition;
dd2544fd 46struct stream_pos;
4c8bfb7e 47struct format;
e1151715 48struct definition;
56e60373 49struct ctf_clock;
4c8bfb7e 50
64893f33 51/* type scope */
f6625916 52struct declaration_scope {
c13cbf74 53 /* Hash table mapping type name GQuark to "struct declaration" */
1ee8e81d 54 /* Used for both typedef and typealias. */
f6625916
MD
55 GHashTable *typedef_declarations;
56 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
57 GHashTable *struct_declarations;
58 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
59 GHashTable *variant_declarations;
c13cbf74 60 /* Hash table mapping enum name GQuark to "struct type_enum" */
f6625916
MD
61 GHashTable *enum_declarations;
62 struct declaration_scope *parent_scope;
64893f33
MD
63};
64
e1151715
MD
65/* definition scope */
66struct definition_scope {
67 /* Hash table mapping field name GQuark to "struct definition" */
68 GHashTable *definitions;
69 struct definition_scope *parent_scope;
05c749e5
MD
70 /*
71 * Complete "path" leading to this definition scope.
9e29e16e 72 * Includes dynamic scope name '.' field name '.' field name '.' ....
05c749e5 73 * Array of GQuark elements (which are each separated by dots).
9e29e16e
MD
74 * The dynamic scope name can contain dots, and is encoded into
75 * a single GQuark. Thus, scope_path[0] returns the GQuark
76 * identifying the dynamic scope.
05c749e5
MD
77 */
78 GArray *scope_path; /* array of GQuark */
6b71274a
MD
79};
80
f6625916 81struct declaration {
05628561 82 enum ctf_type_id id;
fc93b2bd 83 size_t alignment; /* type alignment, in bits */
e19c3d69 84 int ref; /* number of references to the type */
c054553d 85 /*
f6625916 86 * declaration_free called with declaration ref is decremented to 0.
c054553d 87 */
f6625916 88 void (*declaration_free)(struct declaration *declaration);
e1151715 89 struct definition *
f6625916 90 (*definition_new)(struct declaration *declaration,
05c749e5 91 struct definition_scope *parent_scope,
98df1c9f
MD
92 GQuark field_name, int index,
93 const char *root_name);
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 */
b1a2f580 103 GQuark name; /* Field name in its container (or 0 if unset) */
e1151715 104 int ref; /* number of references to the definition */
31262354 105 GQuark path;
a35173fe 106 struct definition_scope *scope;
fc93b2bd
MD
107};
108
c5e74408
MD
109typedef int (*rw_dispatch)(struct stream_pos *pos,
110 struct definition *definition);
d11e9c49
MD
111
112/* Parent of per-plugin positions */
113struct stream_pos {
114 /* read/write dispatch table. Specific to plugin used for stream. */
115 rw_dispatch *rw_table; /* rw dispatch table */
31262354 116 int (*event_cb)(struct stream_pos *pos,
9e88d150 117 struct ctf_stream_definition *stream);
d11e9c49
MD
118};
119
120static inline
c5e74408 121int generic_rw(struct stream_pos *pos, struct definition *definition)
d11e9c49
MD
122{
123 enum ctf_type_id dispatch_id = definition->declaration->id;
124 rw_dispatch call;
125
126 assert(pos->rw_table[dispatch_id] != NULL);
127 call = pos->rw_table[dispatch_id];
c5e74408 128 return call(pos, definition);
d11e9c49
MD
129}
130
bed864a7
MD
131/*
132 * Because we address in bits, bitfields end up being exactly the same as
133 * integers, except that their read/write functions must be able to deal with
134 * read/write non aligned on CHAR_BIT.
135 */
f6625916
MD
136struct declaration_integer {
137 struct declaration p;
7fe00194
MD
138 size_t len; /* length, in bits. */
139 int byte_order; /* byte order */
140 int signedness;
164078da 141 int base; /* Base for pretty-printing: 2, 8, 10, 16 */
81dee1bb 142 enum ctf_string_encoding encoding;
56e60373 143 struct ctf_clock *clock;
fc93b2bd
MD
144};
145
e1151715
MD
146struct definition_integer {
147 struct definition p;
f6625916 148 struct declaration_integer *declaration;
c054553d
MD
149 /* Last values read */
150 union {
151 uint64_t _unsigned;
152 int64_t _signed;
153 } value;
154};
155
f6625916
MD
156struct declaration_float {
157 struct declaration p;
158 struct declaration_integer *sign;
159 struct declaration_integer *mantissa;
160 struct declaration_integer *exp;
fc93b2bd 161 int byte_order;
0a46062b 162 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
163};
164
e1151715
MD
165struct definition_float {
166 struct definition p;
f6625916 167 struct declaration_float *declaration;
d11e9c49
MD
168 struct definition_integer *sign;
169 struct definition_integer *mantissa;
170 struct definition_integer *exp;
c054553d 171 /* Last values read */
f72803ca 172 double value;
c054553d
MD
173};
174
d65d8abb
MD
175/*
176 * enum_val_equal assumes that signed and unsigned memory layout overlap.
177 */
178struct enum_range {
179 union {
180 int64_t _signed;
181 uint64_t _unsigned;
182 } start; /* lowest range value */
183 union {
184 int64_t _signed;
185 uint64_t _unsigned;
186 } end; /* highest range value */
187};
188
189struct enum_range_to_quark {
3122e6f0 190 struct bt_list_head node;
d65d8abb
MD
191 struct enum_range range;
192 GQuark quark;
193};
194
195/*
196 * We optimize the common case (range of size 1: single value) by creating a
197 * hash table mapping values to quark sets. We then lookup the ranges to
198 * complete the quark set.
199 *
200 * TODO: The proper structure to hold the range to quark set mapping would be an
201 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
202 * time. Using a simple O(n) list search for now for implementation speed and
203 * given that we can expect to have a _relatively_ small number of enumeration
204 * ranges. This might become untrue if we are fed with symbol tables often
205 * required to lookup function names from instruction pointer value.
206 */
448d3cc7 207struct enum_table {
d65d8abb 208 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
3122e6f0 209 struct bt_list_head range_to_quark; /* (range, GQuark) */
d65d8abb 210 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
211};
212
f6625916
MD
213struct declaration_enum {
214 struct declaration p;
215 struct declaration_integer *integer_declaration;
448d3cc7 216 struct enum_table table;
fc93b2bd
MD
217};
218
e1151715
MD
219struct definition_enum {
220 struct definition p;
221 struct definition_integer *integer;
f6625916 222 struct declaration_enum *declaration;
c054553d
MD
223 /* Last GQuark values read. Keeping a reference on the GQuark array. */
224 GArray *value;
225};
226
f6625916
MD
227struct declaration_string {
228 struct declaration p;
ab4cf058 229 enum ctf_string_encoding encoding;
c054553d
MD
230};
231
e1151715
MD
232struct definition_string {
233 struct definition p;
f6625916 234 struct declaration_string *declaration;
e1151715 235 char *value; /* freed at definition_string teardown */
d11e9c49 236 size_t len, alloc_len;
11796b96
MD
237};
238
f6625916 239struct declaration_field {
e19c3d69 240 GQuark name;
f6625916 241 struct declaration *declaration;
c054553d
MD
242};
243
f6625916
MD
244struct declaration_struct {
245 struct declaration p;
e19c3d69 246 GHashTable *fields_by_name; /* Tuples (field name, field index) */
f6625916
MD
247 struct declaration_scope *scope;
248 GArray *fields; /* Array of declaration_field */
e19c3d69
MD
249};
250
e1151715
MD
251struct definition_struct {
252 struct definition p;
f6625916 253 struct declaration_struct *declaration;
b1a2f580 254 GPtrArray *fields; /* Array of pointers to struct definition */
c054553d
MD
255};
256
d60cb676 257struct declaration_untagged_variant {
f6625916 258 struct declaration p;
c054553d 259 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
f6625916
MD
260 struct declaration_scope *scope;
261 GArray *fields; /* Array of declaration_field */
c054553d
MD
262};
263
d60cb676
MD
264struct declaration_variant {
265 struct declaration p;
266 struct declaration_untagged_variant *untagged_variant;
267 GArray *tag_name; /* Array of GQuark */
268};
269
270/* A variant needs to be tagged to be defined. */
e1151715
MD
271struct definition_variant {
272 struct definition p;
f6625916 273 struct declaration_variant *declaration;
e1151715 274 struct definition *enum_tag;
b1a2f580
MD
275 GPtrArray *fields; /* Array of pointers to struct definition */
276 struct definition *current_field; /* Last field read */
11796b96
MD
277};
278
f6625916
MD
279struct declaration_array {
280 struct declaration p;
11796b96 281 size_t len;
f6625916
MD
282 struct declaration *elem;
283 struct declaration_scope *scope;
11796b96
MD
284};
285
e1151715
MD
286struct definition_array {
287 struct definition p;
f6625916 288 struct declaration_array *declaration;
b1a2f580 289 GPtrArray *elems; /* Array of pointers to struct definition */
81dee1bb 290 GString *string; /* String for encoded integer children */
c054553d
MD
291};
292
f6625916
MD
293struct declaration_sequence {
294 struct declaration p;
98df1c9f 295 GArray *length_name; /* Array of GQuark */
f6625916
MD
296 struct declaration *elem;
297 struct declaration_scope *scope;
e19c3d69
MD
298};
299
e1151715
MD
300struct definition_sequence {
301 struct definition p;
f6625916 302 struct declaration_sequence *declaration;
98df1c9f 303 struct definition_integer *length;
b1a2f580 304 GPtrArray *elems; /* Array of pointers to struct definition */
81dee1bb 305 GString *string; /* String for encoded integer children */
c054553d
MD
306};
307
becd02a1 308int bt_register_declaration(GQuark declaration_name,
f6625916
MD
309 struct declaration *declaration,
310 struct declaration_scope *scope);
becd02a1 311struct declaration *bt_lookup_declaration(GQuark declaration_name,
78af2bcd 312 struct declaration_scope *scope);
c13cbf74
MD
313
314/*
315 * Type scopes also contain a separate registry for struct, variant and
e1151715 316 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
317 * that a named variant can be declared without specifying its target
318 * "choice" tag field immediately.
319 */
c8c98132 320int bt_register_struct_declaration(GQuark struct_name,
f6625916
MD
321 struct declaration_struct *struct_declaration,
322 struct declaration_scope *scope);
323struct declaration_struct *
c8c98132 324 bt_lookup_struct_declaration(GQuark struct_name,
f6625916 325 struct declaration_scope *scope);
becd02a1 326int bt_register_variant_declaration(GQuark variant_name,
a0720417 327 struct declaration_untagged_variant *untagged_variant_declaration,
f6625916 328 struct declaration_scope *scope);
becd02a1 329struct declaration_untagged_variant *bt_lookup_variant_declaration(GQuark variant_name,
f6625916 330 struct declaration_scope *scope);
becd02a1 331int bt_register_enum_declaration(GQuark enum_name,
f6625916
MD
332 struct declaration_enum *enum_declaration,
333 struct declaration_scope *scope);
334struct declaration_enum *
becd02a1 335 bt_lookup_enum_declaration(GQuark enum_name,
f6625916
MD
336 struct declaration_scope *scope);
337
338struct declaration_scope *
becd02a1
JD
339 bt_new_declaration_scope(struct declaration_scope *parent_scope);
340void bt_free_declaration_scope(struct declaration_scope *scope);
c054553d 341
c13cbf74 342/*
e1151715
MD
343 * field_definition is for field definitions. They are registered into
344 * definition scopes.
c13cbf74 345 */
e1151715 346struct definition *
a35173fe
MD
347 lookup_path_definition(GArray *cur_path, /* array of GQuark */
348 GArray *lookup_path, /* array of GQuark */
349 struct definition_scope *scope);
e1151715
MD
350int register_field_definition(GQuark field_name,
351 struct definition *definition,
352 struct definition_scope *scope);
353struct definition_scope *
05c749e5 354 new_definition_scope(struct definition_scope *parent_scope,
98df1c9f 355 GQuark field_name, const char *root_name);
e1151715 356void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 357
98df1c9f
MD
358GQuark new_definition_path(struct definition_scope *parent_scope,
359 GQuark field_name, const char *root_name);
31262354
MD
360
361static inline
362int compare_definition_path(struct definition *definition, GQuark path)
363{
364 return definition->path == path;
365}
366
e6b4b4f4
JD
367void bt_declaration_ref(struct declaration *declaration);
368void bt_declaration_unref(struct declaration *declaration);
64893f33 369
13fad8b6
JD
370void bt_definition_ref(struct definition *definition);
371void bt_definition_unref(struct definition *definition);
698f0fe4 372
becd02a1 373struct declaration_integer *bt_integer_declaration_new(size_t len, int byte_order,
164078da 374 int signedness, size_t alignment,
56e60373
MD
375 int base, enum ctf_string_encoding encoding,
376 struct ctf_clock *clock);
fe2b0e24
JD
377uint64_t bt_get_unsigned_int(const struct definition *field);
378int64_t bt_get_signed_int(const struct definition *field);
379int bt_get_int_signedness(const struct definition *field);
380int bt_get_int_byte_order(const struct definition *field);
381int bt_get_int_base(const struct definition *field);
382size_t bt_get_int_len(const struct definition *field); /* in bits */
383enum ctf_string_encoding bt_get_int_encoding(const struct definition *field);
0a46062b 384
11d43b90
MD
385/*
386 * mantissa_len is the length of the number of bytes represented by the mantissa
387 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
388 */
becd02a1 389struct declaration_float *bt_float_declaration_new(size_t mantissa_len,
e19c3d69
MD
390 size_t exp_len, int byte_order,
391 size_t alignment);
0a46062b 392
448d3cc7
MD
393/*
394 * A GQuark can be translated to/from strings with g_quark_from_string() and
395 * g_quark_to_string().
396 */
47e0f2e2
MD
397
398/*
399 * Returns a GArray of GQuark or NULL.
400 * Caller must release the GArray with g_array_unref().
401 */
2399b6f4 402GArray *bt_enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
f6625916 403 uint64_t v);
d65d8abb
MD
404
405/*
47e0f2e2 406 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
407 * Caller must release the GArray with g_array_unref().
408 */
2399b6f4 409GArray *bt_enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
bcdf4cf2 410 int64_t v);
47e0f2e2
MD
411
412/*
413 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
414 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
415 * release it).
47e0f2e2 416 */
2399b6f4 417GArray *bt_enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
f6625916 418 GQuark q);
2399b6f4 419void bt_enum_signed_insert(struct declaration_enum *enum_declaration,
d65d8abb 420 int64_t start, int64_t end, GQuark q);
2399b6f4 421void bt_enum_unsigned_insert(struct declaration_enum *enum_declaration,
d65d8abb 422 uint64_t start, uint64_t end, GQuark q);
2399b6f4 423size_t bt_enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
f6625916
MD
424
425struct declaration_enum *
2399b6f4 426 bt_enum_declaration_new(struct declaration_integer *integer_declaration);
f6625916 427
e397791f 428struct declaration_string *
ebdb2383
JD
429 bt_string_declaration_new(enum ctf_string_encoding encoding);
430char *bt_get_string(const struct definition *field);
431enum ctf_string_encoding bt_get_string_encoding(const struct definition *field);
e397791f 432
f6625916 433struct declaration_struct *
c8c98132 434 bt_struct_declaration_new(struct declaration_scope *parent_scope,
b7e35bad 435 uint64_t min_align);
c8c98132 436void bt_struct_declaration_add_field(struct declaration_struct *struct_declaration,
f6625916
MD
437 const char *field_name,
438 struct declaration *field_declaration);
11796b96
MD
439/*
440 * Returns the index of a field within a structure.
441 */
c8c98132 442int bt_struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
f6625916 443 GQuark field_name);
11796b96
MD
444/*
445 * field returned only valid as long as the field structure is not appended to.
446 */
f6625916 447struct declaration_field *
c8c98132 448bt_struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 449 int index);
b1a2f580 450struct definition *
c8c98132 451bt_struct_definition_get_field_from_index(struct definition_struct *struct_definition,
0f980a35 452 int index);
c8c98132
JD
453int bt_struct_rw(struct stream_pos *pos, struct definition *definition);
454uint64_t bt_struct_declaration_len(struct declaration_struct *struct_declaration);
11796b96 455
c054553d
MD
456/*
457 * The tag enumeration is validated to ensure that it contains only mappings
458 * from numeric values to a single tag. Overlapping tag value ranges are
459 * therefore forbidden.
460 */
becd02a1 461struct declaration_untagged_variant *bt_untagged_bt_variant_declaration_new(
1934b94f 462 struct declaration_scope *parent_scope);
becd02a1 463struct declaration_variant *bt_variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
d60cb676
MD
464 const char *tag);
465
becd02a1 466void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
d60cb676
MD
467 const char *field_name,
468 struct declaration *field_declaration);
f6625916 469struct declaration_field *
becd02a1 470 bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
1934b94f 471 GQuark tag);
c054553d
MD
472/*
473 * Returns 0 on success, -EPERM on error.
474 */
e1151715
MD
475int variant_definition_set_tag(struct definition_variant *variant,
476 struct definition *enum_tag);
c054553d
MD
477/*
478 * Returns the field selected by the current tag value.
479 * field returned only valid as long as the variant structure is not appended
480 * to.
481 */
ebae302b
JD
482struct definition *bt_variant_get_current_field(struct definition_variant *variant);
483int bt_variant_rw(struct stream_pos *pos, struct definition *definition);
c054553d 484
d06d03db 485/*
f6625916
MD
486 * elem_declaration passed as parameter now belongs to the array. No
487 * need to free it explicitly. "len" is the number of elements in the
488 * array.
d06d03db 489 */
f6625916 490struct declaration_array *
dcaa2e7d 491 bt_array_declaration_new(size_t len, struct declaration *elem_declaration,
1934b94f 492 struct declaration_scope *parent_scope);
dcaa2e7d
JD
493uint64_t bt_array_len(struct definition_array *array);
494struct definition *bt_array_index(struct definition_array *array, uint64_t i);
495int bt_array_rw(struct stream_pos *pos, struct definition *definition);
496GString *bt_get_char_array(const struct definition *field);
497int bt_get_array_len(const struct definition *field);
11796b96 498
d06d03db 499/*
f6625916
MD
500 * int_declaration and elem_declaration passed as parameter now belong
501 * to the sequence. No need to free them explicitly.
d06d03db 502 */
f6625916 503struct declaration_sequence *
9ffd39fc 504 bt_sequence_declaration_new(const char *length_name,
1934b94f
MD
505 struct declaration *elem_declaration,
506 struct declaration_scope *parent_scope);
9ffd39fc
JD
507uint64_t bt_sequence_len(struct definition_sequence *sequence);
508struct definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i);
509int bt_sequence_rw(struct stream_pos *pos, struct definition *definition);
11796b96 510
d60cb676
MD
511/*
512 * in: path (dot separated), out: q (GArray of GQuark)
513 */
dd0365d9 514void bt_append_scope_path(const char *path, GArray *q);
d60cb676 515
a35173fe
MD
516/*
517 * Lookup helpers.
518 */
04ae3991 519struct definition *lookup_definition(const struct definition *definition,
620961f6 520 const char *field_name);
04ae3991 521struct definition_integer *lookup_integer(const struct definition *definition,
a35173fe
MD
522 const char *field_name,
523 int signedness);
9e3274b0 524struct definition_enum *bt_lookup_enum(const struct definition *definition,
a35173fe
MD
525 const char *field_name,
526 int signedness);
ebae302b 527struct definition *bt_lookup_variant(const struct definition *definition,
a35173fe
MD
528 const char *field_name);
529
300d317a
JD
530static inline
531const char *rem_(const char *str)
532{
533 if (str[0] == '_')
534 return &str[1];
535 else
536 return str;
537}
538
31262354 539#endif /* _BABELTRACE_TYPES_H */
This page took 0.057374 seconds and 4 git commands to generate.