1 #ifndef _BABELTRACE_TYPES_H
2 #define _BABELTRACE_TYPES_H
9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
22 #include <babeltrace/align.h>
23 #include <babeltrace/list.h>
31 /* Preallocate this many fields for structures */
32 #define DEFAULT_NR_STRUCT_FIELDS 8
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
;
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
;
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.
65 GArray
*scope_path
; /* array of GQuark */
75 CTF_TYPE_UNTAGGED_VARIANT
,
84 size_t alignment
; /* type alignment, in bits */
85 int ref
; /* number of references to the type */
87 * declaration_free called with declaration ref is decremented to 0.
89 void (*declaration_free
)(struct declaration
*declaration
);
91 (*definition_new
)(struct declaration
*declaration
,
92 struct definition_scope
*parent_scope
,
93 GQuark field_name
, int index
);
95 * definition_free called with definition ref is decremented to 0.
97 void (*definition_free
)(struct definition
*definition
);
101 struct declaration
*declaration
;
102 int index
; /* Position of the definition in its container */
103 GQuark name
; /* Field name in its container (or 0 if unset) */
104 int ref
; /* number of references to the definition */
107 typedef int (*rw_dispatch
)(struct stream_pos
*pos
,
108 struct definition
*definition
);
110 /* Parent of per-plugin positions */
112 /* read/write dispatch table. Specific to plugin used for stream. */
113 rw_dispatch
*rw_table
; /* rw dispatch table */
117 int generic_rw(struct stream_pos
*pos
, struct definition
*definition
)
119 enum ctf_type_id dispatch_id
= definition
->declaration
->id
;
122 assert(pos
->rw_table
[dispatch_id
] != NULL
);
123 call
= pos
->rw_table
[dispatch_id
];
124 return call(pos
, definition
);
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.
132 struct declaration_integer
{
133 struct declaration p
;
134 size_t len
; /* length, in bits. */
135 int byte_order
; /* byte order */
139 struct definition_integer
{
141 struct declaration_integer
*declaration
;
142 /* Last values read */
149 struct declaration_float
{
150 struct declaration p
;
151 struct declaration_integer
*sign
;
152 struct declaration_integer
*mantissa
;
153 struct declaration_integer
*exp
;
155 /* TODO: we might want to express more info about NaN, +inf and -inf */
158 struct definition_float
{
160 struct declaration_float
*declaration
;
161 struct definition_integer
*sign
;
162 struct definition_integer
*mantissa
;
163 struct definition_integer
*exp
;
164 /* Last values read */
169 * enum_val_equal assumes that signed and unsigned memory layout overlap.
175 } start
; /* lowest range value */
179 } end
; /* highest range value */
182 struct enum_range_to_quark
{
183 struct cds_list_head node
;
184 struct enum_range range
;
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.
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.
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) */
206 struct declaration_enum
{
207 struct declaration p
;
208 struct declaration_integer
*integer_declaration
;
209 struct enum_table table
;
212 struct definition_enum
{
214 struct definition_integer
*integer
;
215 struct declaration_enum
*declaration
;
216 /* Last GQuark values read. Keeping a reference on the GQuark array. */
220 enum ctf_string_encoding
{
226 struct declaration_string
{
227 struct declaration p
;
228 enum ctf_string_encoding encoding
;
231 struct definition_string
{
233 struct declaration_string
*declaration
;
234 char *value
; /* freed at definition_string teardown */
235 size_t len
, alloc_len
;
238 struct declaration_field
{
240 struct declaration
*declaration
;
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 */
250 struct definition_struct
{
252 struct declaration_struct
*declaration
;
253 struct definition_scope
*scope
;
254 GPtrArray
*fields
; /* Array of pointers to struct definition */
257 struct declaration_untagged_variant
{
258 struct declaration p
;
259 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
260 struct declaration_scope
*scope
;
261 GArray
*fields
; /* Array of declaration_field */
264 struct declaration_variant
{
265 struct declaration p
;
266 struct declaration_untagged_variant
*untagged_variant
;
267 GArray
*tag_name
; /* Array of GQuark */
270 /* A variant needs to be tagged to be defined. */
271 struct definition_variant
{
273 struct declaration_variant
*declaration
;
274 struct definition_scope
*scope
;
275 struct definition
*enum_tag
;
276 GPtrArray
*fields
; /* Array of pointers to struct definition */
277 struct definition
*current_field
; /* Last field read */
280 struct declaration_array
{
281 struct declaration p
;
283 struct declaration
*elem
;
284 struct declaration_scope
*scope
;
287 struct definition_array
{
289 struct declaration_array
*declaration
;
290 struct definition_scope
*scope
;
291 GPtrArray
*elems
; /* Array of pointers to struct definition */
294 struct declaration_sequence
{
295 struct declaration p
;
296 struct declaration_integer
*len_declaration
;
297 struct declaration
*elem
;
298 struct declaration_scope
*scope
;
301 struct definition_sequence
{
303 struct declaration_sequence
*declaration
;
304 struct definition_scope
*scope
;
305 struct definition_integer
*len
;
306 GPtrArray
*elems
; /* Array of pointers to struct definition */
309 int register_declaration(GQuark declaration_name
,
310 struct declaration
*declaration
,
311 struct declaration_scope
*scope
);
312 struct declaration
*lookup_declaration(GQuark declaration_name
,
313 struct declaration_scope
*scope
);
316 * Type scopes also contain a separate registry for struct, variant and
317 * enum types. Those register types rather than type definitions, so
318 * that a named variant can be declared without specifying its target
319 * "choice" tag field immediately.
321 int register_struct_declaration(GQuark struct_name
,
322 struct declaration_struct
*struct_declaration
,
323 struct declaration_scope
*scope
);
324 struct declaration_struct
*
325 lookup_struct_declaration(GQuark struct_name
,
326 struct declaration_scope
*scope
);
327 int register_variant_declaration(GQuark variant_name
,
328 struct declaration_untagged_variant
*untagged_variant_declaration
,
329 struct declaration_scope
*scope
);
330 struct declaration_untagged_variant
*lookup_variant_declaration(GQuark variant_name
,
331 struct declaration_scope
*scope
);
332 int register_enum_declaration(GQuark enum_name
,
333 struct declaration_enum
*enum_declaration
,
334 struct declaration_scope
*scope
);
335 struct declaration_enum
*
336 lookup_enum_declaration(GQuark enum_name
,
337 struct declaration_scope
*scope
);
339 struct declaration_scope
*
340 new_declaration_scope(struct declaration_scope
*parent_scope
);
341 void free_declaration_scope(struct declaration_scope
*scope
);
344 * field_definition is for field definitions. They are registered into
348 lookup_definition(GArray
*cur_path
, /* array of GQuark */
349 GArray
*lookup_path
, /* array of GQuark */
350 struct definition_scope
*scope
);
351 int register_field_definition(GQuark field_name
,
352 struct definition
*definition
,
353 struct definition_scope
*scope
);
354 struct definition_scope
*
355 new_definition_scope(struct definition_scope
*parent_scope
,
357 void set_dynamic_definition_scope(struct definition
*definition
,
358 struct definition_scope
*scope
,
359 const char *root_name
);
360 void free_definition_scope(struct definition_scope
*scope
);
362 void declaration_ref(struct declaration
*declaration
);
363 void declaration_unref(struct declaration
*declaration
);
365 void definition_ref(struct definition
*definition
);
366 void definition_unref(struct definition
*definition
);
368 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
369 int signedness
, size_t alignment
);
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.
375 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
376 size_t exp_len
, int byte_order
,
380 * A GQuark can be translated to/from strings with g_quark_from_string() and
381 * g_quark_to_string().
385 * Returns a GArray of GQuark or NULL.
386 * Caller must release the GArray with g_array_unref().
388 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
392 * Returns a GArray of GQuark or NULL.
393 * Caller must release the GArray with g_array_unref().
395 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
399 * Returns a GArray of struct enum_range or NULL.
400 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
403 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
405 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
406 int64_t start
, int64_t end
, GQuark q
);
407 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
408 uint64_t start
, uint64_t end
, GQuark q
);
409 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
);
411 struct declaration_enum
*
412 enum_declaration_new(struct declaration_integer
*integer_declaration
);
414 struct declaration_string
*
415 string_declaration_new(enum ctf_string_encoding encoding
);
417 struct declaration_struct
*
418 struct_declaration_new(struct declaration_scope
*parent_scope
);
419 void struct_declaration_add_field(struct declaration_struct
*struct_declaration
,
420 const char *field_name
,
421 struct declaration
*field_declaration
);
423 * Returns the index of a field within a structure.
425 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
428 * field returned only valid as long as the field structure is not appended to.
430 struct declaration_field
*
431 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
434 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
436 int struct_rw(struct stream_pos
*pos
, struct definition
*definition
);
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.
443 struct declaration_untagged_variant
*untagged_variant_declaration_new(
444 struct declaration_scope
*parent_scope
);
445 struct declaration_variant
*variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
,
448 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
449 const char *field_name
,
450 struct declaration
*field_declaration
);
451 struct declaration_field
*
452 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
,
455 * Returns 0 on success, -EPERM on error.
457 int variant_definition_set_tag(struct definition_variant
*variant
,
458 struct definition
*enum_tag
);
460 * Returns the field selected by the current tag value.
461 * field returned only valid as long as the variant structure is not appended
464 struct definition
*variant_get_current_field(struct definition_variant
*variant
);
465 int variant_rw(struct stream_pos
*pos
, struct definition
*definition
);
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
472 struct declaration_array
*
473 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
474 struct declaration_scope
*parent_scope
);
475 uint64_t array_len(struct definition_array
*array
);
476 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
477 int array_rw(struct stream_pos
*pos
, struct definition
*definition
);
480 * int_declaration and elem_declaration passed as parameter now belong
481 * to the sequence. No need to free them explicitly.
483 struct declaration_sequence
*
484 sequence_declaration_new(struct declaration_integer
*len_declaration
,
485 struct declaration
*elem_declaration
,
486 struct declaration_scope
*parent_scope
);
487 uint64_t sequence_len(struct definition_sequence
*sequence
);
488 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
489 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
);
492 * in: path (dot separated), out: q (GArray of GQuark)
494 void append_scope_path(const char *path
, GArray
*q
);
496 #endif /* _BABELTRACE_declarationS_H */