Update sequence (type specifier -> field ref), fix definition lookup
[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
764af3f4 34struct ctf_stream;
dd2544fd 35struct stream_pos;
4c8bfb7e 36struct format;
e1151715 37struct definition;
4c8bfb7e 38
64893f33 39/* type scope */
f6625916 40struct declaration_scope {
c13cbf74 41 /* Hash table mapping type name GQuark to "struct declaration" */
1ee8e81d 42 /* Used for both typedef and typealias. */
f6625916
MD
43 GHashTable *typedef_declarations;
44 /* Hash table mapping struct name GQuark to "struct declaration_struct" */
45 GHashTable *struct_declarations;
46 /* Hash table mapping variant name GQuark to "struct declaration_variant" */
47 GHashTable *variant_declarations;
c13cbf74 48 /* Hash table mapping enum name GQuark to "struct type_enum" */
f6625916
MD
49 GHashTable *enum_declarations;
50 struct declaration_scope *parent_scope;
64893f33
MD
51};
52
e1151715
MD
53/* definition scope */
54struct definition_scope {
55 /* Hash table mapping field name GQuark to "struct definition" */
56 GHashTable *definitions;
57 struct definition_scope *parent_scope;
05c749e5
MD
58 /*
59 * Complete "path" leading to this definition scope.
9e29e16e 60 * Includes dynamic scope name '.' field name '.' field name '.' ....
05c749e5 61 * Array of GQuark elements (which are each separated by dots).
9e29e16e
MD
62 * The dynamic scope name can contain dots, and is encoded into
63 * a single GQuark. Thus, scope_path[0] returns the GQuark
64 * identifying the dynamic scope.
05c749e5
MD
65 */
66 GArray *scope_path; /* array of GQuark */
6b71274a
MD
67};
68
05628561
MD
69enum ctf_type_id {
70 CTF_TYPE_UNKNOWN = 0,
71 CTF_TYPE_INTEGER,
72 CTF_TYPE_FLOAT,
73 CTF_TYPE_ENUM,
74 CTF_TYPE_STRING,
75 CTF_TYPE_STRUCT,
d60cb676 76 CTF_TYPE_UNTAGGED_VARIANT,
05628561
MD
77 CTF_TYPE_VARIANT,
78 CTF_TYPE_ARRAY,
79 CTF_TYPE_SEQUENCE,
80 NR_CTF_TYPES,
81};
82
f6625916 83struct declaration {
05628561 84 enum ctf_type_id id;
fc93b2bd 85 size_t alignment; /* type alignment, in bits */
e19c3d69 86 int ref; /* number of references to the type */
c054553d 87 /*
f6625916 88 * declaration_free called with declaration ref is decremented to 0.
c054553d 89 */
f6625916 90 void (*declaration_free)(struct declaration *declaration);
e1151715 91 struct definition *
f6625916 92 (*definition_new)(struct declaration *declaration,
05c749e5 93 struct definition_scope *parent_scope,
98df1c9f
MD
94 GQuark field_name, int index,
95 const char *root_name);
c054553d 96 /*
e1151715 97 * definition_free called with definition ref is decremented to 0.
e19c3d69 98 */
e1151715 99 void (*definition_free)(struct definition *definition);
c054553d
MD
100};
101
e1151715 102struct definition {
f6625916 103 struct declaration *declaration;
05c749e5 104 int index; /* Position of the definition in its container */
b1a2f580 105 GQuark name; /* Field name in its container (or 0 if unset) */
e1151715 106 int ref; /* number of references to the definition */
31262354 107 GQuark path;
fc93b2bd
MD
108};
109
c5e74408
MD
110typedef int (*rw_dispatch)(struct stream_pos *pos,
111 struct definition *definition);
d11e9c49
MD
112
113/* Parent of per-plugin positions */
114struct stream_pos {
115 /* read/write dispatch table. Specific to plugin used for stream. */
116 rw_dispatch *rw_table; /* rw dispatch table */
31262354 117 int (*event_cb)(struct stream_pos *pos,
764af3f4 118 struct ctf_stream *stream);
d11e9c49
MD
119};
120
121static inline
c5e74408 122int generic_rw(struct stream_pos *pos, struct definition *definition)
d11e9c49
MD
123{
124 enum ctf_type_id dispatch_id = definition->declaration->id;
125 rw_dispatch call;
126
127 assert(pos->rw_table[dispatch_id] != NULL);
128 call = pos->rw_table[dispatch_id];
c5e74408 129 return call(pos, definition);
d11e9c49
MD
130}
131
bed864a7
MD
132/*
133 * Because we address in bits, bitfields end up being exactly the same as
134 * integers, except that their read/write functions must be able to deal with
135 * read/write non aligned on CHAR_BIT.
136 */
f6625916
MD
137struct declaration_integer {
138 struct declaration p;
7fe00194
MD
139 size_t len; /* length, in bits. */
140 int byte_order; /* byte order */
141 int signedness;
164078da 142 int base; /* Base for pretty-printing: 2, 8, 10, 16 */
fc93b2bd
MD
143};
144
e1151715
MD
145struct definition_integer {
146 struct definition p;
f6625916 147 struct declaration_integer *declaration;
c054553d
MD
148 /* Last values read */
149 union {
150 uint64_t _unsigned;
151 int64_t _signed;
152 } value;
153};
154
f6625916
MD
155struct declaration_float {
156 struct declaration p;
157 struct declaration_integer *sign;
158 struct declaration_integer *mantissa;
159 struct declaration_integer *exp;
fc93b2bd 160 int byte_order;
0a46062b 161 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
162};
163
e1151715
MD
164struct definition_float {
165 struct definition p;
f6625916 166 struct declaration_float *declaration;
d11e9c49
MD
167 struct definition_integer *sign;
168 struct definition_integer *mantissa;
169 struct definition_integer *exp;
98df1c9f 170 struct definition_scope *scope;
c054553d
MD
171 /* Last values read */
172 long double value;
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 {
190 struct cds_list_head node;
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
MD
208 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
209 struct cds_list_head range_to_quark; /* (range, GQuark) */
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;
98df1c9f 223 struct definition_scope *scope;
c054553d
MD
224 /* Last GQuark values read. Keeping a reference on the GQuark array. */
225 GArray *value;
226};
227
ab4cf058
MD
228enum ctf_string_encoding {
229 CTF_STRING_UTF8 = 0,
230 CTF_STRING_ASCII,
231 CTF_STRING_UNKNOWN,
232};
233
f6625916
MD
234struct declaration_string {
235 struct declaration p;
ab4cf058 236 enum ctf_string_encoding encoding;
c054553d
MD
237};
238
e1151715
MD
239struct definition_string {
240 struct definition p;
f6625916 241 struct declaration_string *declaration;
e1151715 242 char *value; /* freed at definition_string teardown */
d11e9c49 243 size_t len, alloc_len;
11796b96
MD
244};
245
f6625916 246struct declaration_field {
e19c3d69 247 GQuark name;
f6625916 248 struct declaration *declaration;
c054553d
MD
249};
250
f6625916
MD
251struct declaration_struct {
252 struct declaration p;
e19c3d69 253 GHashTable *fields_by_name; /* Tuples (field name, field index) */
f6625916
MD
254 struct declaration_scope *scope;
255 GArray *fields; /* Array of declaration_field */
e19c3d69
MD
256};
257
e1151715
MD
258struct definition_struct {
259 struct definition p;
f6625916 260 struct declaration_struct *declaration;
e1151715 261 struct definition_scope *scope;
b1a2f580 262 GPtrArray *fields; /* Array of pointers to struct definition */
c054553d
MD
263};
264
d60cb676 265struct declaration_untagged_variant {
f6625916 266 struct declaration p;
c054553d 267 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
f6625916
MD
268 struct declaration_scope *scope;
269 GArray *fields; /* Array of declaration_field */
c054553d
MD
270};
271
d60cb676
MD
272struct declaration_variant {
273 struct declaration p;
274 struct declaration_untagged_variant *untagged_variant;
275 GArray *tag_name; /* Array of GQuark */
276};
277
278/* A variant needs to be tagged to be defined. */
e1151715
MD
279struct definition_variant {
280 struct definition p;
f6625916 281 struct declaration_variant *declaration;
e1151715
MD
282 struct definition_scope *scope;
283 struct definition *enum_tag;
b1a2f580
MD
284 GPtrArray *fields; /* Array of pointers to struct definition */
285 struct definition *current_field; /* Last field read */
11796b96
MD
286};
287
f6625916
MD
288struct declaration_array {
289 struct declaration p;
11796b96 290 size_t len;
f6625916
MD
291 struct declaration *elem;
292 struct declaration_scope *scope;
11796b96
MD
293};
294
e1151715
MD
295struct definition_array {
296 struct definition p;
f6625916 297 struct declaration_array *declaration;
e1151715 298 struct definition_scope *scope;
b1a2f580 299 GPtrArray *elems; /* Array of pointers to struct definition */
c054553d
MD
300};
301
f6625916
MD
302struct declaration_sequence {
303 struct declaration p;
98df1c9f 304 GArray *length_name; /* Array of GQuark */
f6625916
MD
305 struct declaration *elem;
306 struct declaration_scope *scope;
e19c3d69
MD
307};
308
e1151715
MD
309struct definition_sequence {
310 struct definition p;
f6625916 311 struct declaration_sequence *declaration;
e1151715 312 struct definition_scope *scope;
98df1c9f 313 struct definition_integer *length;
b1a2f580 314 GPtrArray *elems; /* Array of pointers to struct definition */
c054553d
MD
315};
316
f6625916
MD
317int register_declaration(GQuark declaration_name,
318 struct declaration *declaration,
319 struct declaration_scope *scope);
320struct declaration *lookup_declaration(GQuark declaration_name,
78af2bcd 321 struct declaration_scope *scope);
c13cbf74
MD
322
323/*
324 * Type scopes also contain a separate registry for struct, variant and
e1151715 325 * enum types. Those register types rather than type definitions, so
c13cbf74
MD
326 * that a named variant can be declared without specifying its target
327 * "choice" tag field immediately.
328 */
f6625916
MD
329int register_struct_declaration(GQuark struct_name,
330 struct declaration_struct *struct_declaration,
331 struct declaration_scope *scope);
332struct declaration_struct *
333 lookup_struct_declaration(GQuark struct_name,
334 struct declaration_scope *scope);
335int register_variant_declaration(GQuark variant_name,
a0720417 336 struct declaration_untagged_variant *untagged_variant_declaration,
f6625916 337 struct declaration_scope *scope);
a0720417 338struct declaration_untagged_variant *lookup_variant_declaration(GQuark variant_name,
f6625916
MD
339 struct declaration_scope *scope);
340int register_enum_declaration(GQuark enum_name,
341 struct declaration_enum *enum_declaration,
342 struct declaration_scope *scope);
343struct declaration_enum *
344 lookup_enum_declaration(GQuark enum_name,
345 struct declaration_scope *scope);
346
347struct declaration_scope *
348 new_declaration_scope(struct declaration_scope *parent_scope);
349void free_declaration_scope(struct declaration_scope *scope);
c054553d 350
c13cbf74 351/*
e1151715
MD
352 * field_definition is for field definitions. They are registered into
353 * definition scopes.
c13cbf74 354 */
e1151715 355struct definition *
05c749e5
MD
356 lookup_definition(GArray *cur_path, /* array of GQuark */
357 GArray *lookup_path, /* array of GQuark */
358 struct definition_scope *scope);
e1151715
MD
359int register_field_definition(GQuark field_name,
360 struct definition *definition,
361 struct definition_scope *scope);
362struct definition_scope *
05c749e5 363 new_definition_scope(struct definition_scope *parent_scope,
98df1c9f 364 GQuark field_name, const char *root_name);
e1151715 365void free_definition_scope(struct definition_scope *scope);
4c8bfb7e 366
98df1c9f
MD
367GQuark new_definition_path(struct definition_scope *parent_scope,
368 GQuark field_name, const char *root_name);
31262354
MD
369
370static inline
371int compare_definition_path(struct definition *definition, GQuark path)
372{
373 return definition->path == path;
374}
375
f6625916
MD
376void declaration_ref(struct declaration *declaration);
377void declaration_unref(struct declaration *declaration);
64893f33 378
e1151715
MD
379void definition_ref(struct definition *definition);
380void definition_unref(struct definition *definition);
698f0fe4 381
add40b62 382struct declaration_integer *integer_declaration_new(size_t len, int byte_order,
164078da
MD
383 int signedness, size_t alignment,
384 int base);
0a46062b 385
11d43b90
MD
386/*
387 * mantissa_len is the length of the number of bytes represented by the mantissa
388 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
389 */
add40b62 390struct declaration_float *float_declaration_new(size_t mantissa_len,
e19c3d69
MD
391 size_t exp_len, int byte_order,
392 size_t alignment);
0a46062b 393
448d3cc7
MD
394/*
395 * A GQuark can be translated to/from strings with g_quark_from_string() and
396 * g_quark_to_string().
397 */
47e0f2e2
MD
398
399/*
400 * Returns a GArray of GQuark or NULL.
401 * Caller must release the GArray with g_array_unref().
402 */
f6625916
MD
403GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
404 uint64_t v);
d65d8abb
MD
405
406/*
47e0f2e2 407 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
408 * Caller must release the GArray with g_array_unref().
409 */
f6625916
MD
410GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
411 uint64_t v);
47e0f2e2
MD
412
413/*
414 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
415 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
416 * release it).
47e0f2e2 417 */
f6625916
MD
418GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
419 GQuark q);
420void enum_signed_insert(struct declaration_enum *enum_declaration,
d65d8abb 421 int64_t start, int64_t end, GQuark q);
f6625916 422void enum_unsigned_insert(struct declaration_enum *enum_declaration,
d65d8abb 423 uint64_t start, uint64_t end, GQuark q);
f6625916
MD
424size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration);
425
426struct declaration_enum *
ab4cf058 427 enum_declaration_new(struct declaration_integer *integer_declaration);
f6625916 428
e397791f
MD
429struct declaration_string *
430 string_declaration_new(enum ctf_string_encoding encoding);
431
f6625916 432struct declaration_struct *
b7e35bad
MD
433 struct_declaration_new(struct declaration_scope *parent_scope,
434 uint64_t min_align);
f6625916
MD
435void struct_declaration_add_field(struct declaration_struct *struct_declaration,
436 const char *field_name,
437 struct declaration *field_declaration);
11796b96
MD
438/*
439 * Returns the index of a field within a structure.
440 */
0f980a35 441int struct_declaration_lookup_field_index(struct declaration_struct *struct_declaration,
f6625916 442 GQuark field_name);
11796b96
MD
443/*
444 * field returned only valid as long as the field structure is not appended to.
445 */
f6625916
MD
446struct declaration_field *
447struct_declaration_get_field_from_index(struct declaration_struct *struct_declaration,
0f980a35 448 int index);
b1a2f580 449struct definition *
0f980a35
MD
450struct_definition_get_field_from_index(struct definition_struct *struct_definition,
451 int index);
c5e74408 452int struct_rw(struct stream_pos *pos, struct definition *definition);
fd3382e8 453uint64_t struct_declaration_len(struct declaration_struct *struct_declaration);
11796b96 454
c054553d
MD
455/*
456 * The tag enumeration is validated to ensure that it contains only mappings
457 * from numeric values to a single tag. Overlapping tag value ranges are
458 * therefore forbidden.
459 */
ab4cf058 460struct declaration_untagged_variant *untagged_variant_declaration_new(
1934b94f 461 struct declaration_scope *parent_scope);
d60cb676
MD
462struct declaration_variant *variant_declaration_new(struct declaration_untagged_variant *untagged_variant,
463 const char *tag);
464
465void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
466 const char *field_name,
467 struct declaration *field_declaration);
f6625916 468struct declaration_field *
d60cb676 469 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration,
1934b94f 470 GQuark tag);
c054553d
MD
471/*
472 * Returns 0 on success, -EPERM on error.
473 */
e1151715
MD
474int variant_definition_set_tag(struct definition_variant *variant,
475 struct definition *enum_tag);
c054553d
MD
476/*
477 * Returns the field selected by the current tag value.
478 * field returned only valid as long as the variant structure is not appended
479 * to.
480 */
b1a2f580 481struct definition *variant_get_current_field(struct definition_variant *variant);
c5e74408 482int variant_rw(struct stream_pos *pos, struct definition *definition);
c054553d 483
d06d03db 484/*
f6625916
MD
485 * elem_declaration passed as parameter now belongs to the array. No
486 * need to free it explicitly. "len" is the number of elements in the
487 * array.
d06d03db 488 */
f6625916 489struct declaration_array *
ab4cf058 490 array_declaration_new(size_t len, struct declaration *elem_declaration,
1934b94f 491 struct declaration_scope *parent_scope);
3838df27 492uint64_t array_len(struct definition_array *array);
0f980a35 493struct definition *array_index(struct definition_array *array, uint64_t i);
c5e74408 494int array_rw(struct stream_pos *pos, struct definition *definition);
11796b96 495
d06d03db 496/*
f6625916
MD
497 * int_declaration and elem_declaration passed as parameter now belong
498 * to the sequence. No need to free them explicitly.
d06d03db 499 */
f6625916 500struct declaration_sequence *
98df1c9f 501 sequence_declaration_new(const char *length_name,
1934b94f
MD
502 struct declaration *elem_declaration,
503 struct declaration_scope *parent_scope);
3838df27 504uint64_t sequence_len(struct definition_sequence *sequence);
0f980a35 505struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i);
c5e74408 506int sequence_rw(struct stream_pos *pos, struct definition *definition);
11796b96 507
d60cb676
MD
508/*
509 * in: path (dot separated), out: q (GArray of GQuark)
510 */
511void append_scope_path(const char *path, GArray *q);
512
31262354 513#endif /* _BABELTRACE_TYPES_H */
This page took 0.050494 seconds and 4 git commands to generate.