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