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