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