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