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