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