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