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