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
);
99 * Definition copy function. Knows how to find the child
100 * definition from the parent definition.
102 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
103 struct stream_pos
*src
, const struct format
*fsrc
,
104 struct definition
*definition
);
108 struct declaration
*declaration
;
109 int index
; /* Position of the definition in its container */
110 int ref
; /* number of references to the definition */
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.
118 struct declaration_integer
{
119 struct declaration p
;
120 size_t len
; /* length, in bits. */
121 int byte_order
; /* byte order */
125 struct definition_integer
{
127 struct declaration_integer
*declaration
;
128 /* Last values read */
135 struct declaration_float
{
136 struct declaration p
;
137 struct declaration_integer
*sign
;
138 struct declaration_integer
*mantissa
;
139 struct declaration_integer
*exp
;
141 /* TODO: we might want to express more info about NaN, +inf and -inf */
144 struct definition_float
{
146 struct declaration_float
*declaration
;
147 /* Last values read */
152 * enum_val_equal assumes that signed and unsigned memory layout overlap.
158 } start
; /* lowest range value */
162 } end
; /* highest range value */
165 struct enum_range_to_quark
{
166 struct cds_list_head node
;
167 struct enum_range range
;
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.
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.
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) */
189 struct declaration_enum
{
190 struct declaration p
;
191 struct declaration_integer
*integer_declaration
;
192 struct enum_table table
;
195 struct definition_enum
{
197 struct definition_integer
*integer
;
198 struct declaration_enum
*declaration
;
199 /* Last GQuark values read. Keeping a reference on the GQuark array. */
203 enum ctf_string_encoding
{
209 struct declaration_string
{
210 struct declaration p
;
211 enum ctf_string_encoding encoding
;
214 struct definition_string
{
216 struct declaration_string
*declaration
;
217 char *value
; /* freed at definition_string teardown */
220 struct declaration_field
{
222 struct declaration
*declaration
;
227 struct definition
*definition
;
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 */
237 struct definition_struct
{
239 struct declaration_struct
*declaration
;
240 struct definition_scope
*scope
;
241 GArray
*fields
; /* Array of struct field */
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 */
251 struct declaration_variant
{
252 struct declaration p
;
253 struct declaration_untagged_variant
*untagged_variant
;
254 GArray
*tag_name
; /* Array of GQuark */
257 /* A variant needs to be tagged to be defined. */
258 struct definition_variant
{
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 */
267 struct declaration_array
{
268 struct declaration p
;
270 struct declaration
*elem
;
271 struct declaration_scope
*scope
;
274 struct definition_array
{
276 struct declaration_array
*declaration
;
277 struct definition_scope
*scope
;
278 GArray
*elems
; /* struct field */
281 struct declaration_sequence
{
282 struct declaration p
;
283 struct declaration_integer
*len_declaration
;
284 struct declaration
*elem
;
285 struct declaration_scope
*scope
;
288 struct definition_sequence
{
290 struct declaration_sequence
*declaration
;
291 struct definition_scope
*scope
;
292 struct definition_integer
*len
;
293 GArray
*elems
; /* struct field */
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
);
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.
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
);
326 struct declaration_scope
*
327 new_declaration_scope(struct declaration_scope
*parent_scope
);
328 void free_declaration_scope(struct declaration_scope
*scope
);
331 * field_definition is for field definitions. They are registered into
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
,
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
);
349 void declaration_ref(struct declaration
*declaration
);
350 void declaration_unref(struct declaration
*declaration
);
352 void definition_ref(struct definition
*definition
);
353 void definition_unref(struct definition
*definition
);
355 struct declaration_integer
*integer_declaration_new(size_t len
, int byte_order
,
356 int signedness
, size_t alignment
);
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.
362 struct declaration_float
*float_declaration_new(size_t mantissa_len
,
363 size_t exp_len
, int byte_order
,
367 * A GQuark can be translated to/from strings with g_quark_from_string() and
368 * g_quark_to_string().
372 * Returns a GArray of GQuark or NULL.
373 * Caller must release the GArray with g_array_unref().
375 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
379 * Returns a GArray of GQuark or NULL.
380 * Caller must release the GArray with g_array_unref().
382 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
386 * Returns a GArray of struct enum_range or NULL.
387 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
390 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
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
);
398 struct declaration_enum
*
399 enum_declaration_new(struct declaration_integer
*integer_declaration
);
401 struct declaration_string
*
402 string_declaration_new(enum ctf_string_encoding encoding
);
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
);
410 * Returns the index of a field within a structure.
412 int struct_declaration_lookup_field_index(struct declaration_struct
*struct_declaration
,
415 * field returned only valid as long as the field structure is not appended to.
417 struct declaration_field
*
418 struct_declaration_get_field_from_index(struct declaration_struct
*struct_declaration
,
421 struct_definition_get_field_from_index(struct definition_struct
*struct_definition
,
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.
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
,
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
,
441 * Returns 0 on success, -EPERM on error.
443 int variant_definition_set_tag(struct definition_variant
*variant
,
444 struct definition
*enum_tag
);
446 * Returns the field selected by the current tag value.
447 * field returned only valid as long as the variant structure is not appended
450 struct field
*variant_get_current_field(struct definition_variant
*variant
);
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
457 struct declaration_array
*
458 array_declaration_new(size_t len
, struct declaration
*elem_declaration
,
459 struct declaration_scope
*parent_scope
);
460 struct definition
*array_index(struct definition_array
*array
, uint64_t i
);
463 * int_declaration and elem_declaration passed as parameter now belong
464 * to the sequence. No need to free them explicitly.
466 struct declaration_sequence
*
467 sequence_declaration_new(struct declaration_integer
*len_declaration
,
468 struct declaration
*elem_declaration
,
469 struct declaration_scope
*parent_scope
);
470 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
);
473 * in: path (dot separated), out: q (GArray of GQuark)
475 void append_scope_path(const char *path
, GArray
*q
);
477 #endif /* _BABELTRACE_declarationS_H */