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