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