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