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
35 * Always update stream_pos with move_pos and init_pos.
38 char *base
; /* Base address */
39 size_t offset
; /* Offset from base, in bits */
40 int dummy
; /* Dummy position, for length calculation */
44 void init_pos(struct stream_pos
*pos
, char *base
)
46 pos
->base
= base
; /* initial base, page-aligned */
52 * move_pos - move position of a relative bit offset
54 * TODO: allow larger files by updating base too.
57 void move_pos(struct stream_pos
*pos
, size_t offset
)
59 pos
->offset
= pos
->offset
+ offset
;
63 * align_pos - align position on a bit offset (> 0)
65 * TODO: allow larger files by updating base too.
68 void align_pos(struct stream_pos
*pos
, size_t offset
)
70 pos
->offset
+= offset_align(pos
->offset
, offset
);
74 void copy_pos(struct stream_pos
*dest
, struct stream_pos
*src
)
76 memcpy(dest
, src
, sizeof(struct stream_pos
));
80 char *get_pos_addr(struct stream_pos
*pos
)
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
83 assert(!(pos
->offset
% CHAR_BIT
));
84 return pos
->base
+ (pos
->offset
/ CHAR_BIT
);
92 /* Hash table mapping type name GQuark to "struct declaration" */
93 GHashTable
*type_definitions
;
94 /* Hash table mapping struct name GQuark to "struct type_struct" */
95 GHashTable
*struct_types
;
96 /* Hash table mapping variant name GQuark to "struct type_variant" */
97 GHashTable
*variant_types
;
98 /* Hash table mapping enum name GQuark to "struct type_enum" */
99 GHashTable
*enum_types
;
100 struct type_scope
*parent_scope
;
103 /* definition scope */
104 struct definition_scope
{
105 /* Hash table mapping field name GQuark to "struct definition" */
106 GHashTable
*definitions
;
107 struct definition_scope
*parent_scope
;
111 CTF_TYPE_UNKNOWN
= 0,
125 GQuark name
; /* type name */
126 size_t alignment
; /* type alignment, in bits */
127 int ref
; /* number of references to the type */
129 * type_free called with type ref is decremented to 0.
131 void (*type_free
)(struct type
*type
);
133 (*definition_new
)(struct type
*type
,
134 struct definition_scope
*parent_scope
);
136 * definition_free called with definition ref is decremented to 0.
138 void (*definition_free
)(struct definition
*definition
);
140 * Definition copy function. Knows how to find the child
141 * definition from the parent definition.
143 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
144 struct stream_pos
*src
, const struct format
*fsrc
,
145 struct definition
*definition
);
150 int ref
; /* number of references to the definition */
154 * Because we address in bits, bitfields end up being exactly the same as
155 * integers, except that their read/write functions must be able to deal with
156 * read/write non aligned on CHAR_BIT.
158 struct type_integer
{
160 size_t len
; /* length, in bits. */
161 int byte_order
; /* byte order */
165 struct definition_integer
{
167 struct type_integer
*type
;
168 /* Last values read */
177 struct type_integer
*sign
;
178 struct type_integer
*mantissa
;
179 struct type_integer
*exp
;
181 /* TODO: we might want to express more info about NaN, +inf and -inf */
184 struct definition_float
{
186 struct type_float
*type
;
187 /* Last values read */
192 * enum_val_equal assumes that signed and unsigned memory layout overlap.
198 } start
; /* lowest range value */
202 } end
; /* highest range value */
205 struct enum_range_to_quark
{
206 struct cds_list_head node
;
207 struct enum_range range
;
212 * We optimize the common case (range of size 1: single value) by creating a
213 * hash table mapping values to quark sets. We then lookup the ranges to
214 * complete the quark set.
216 * TODO: The proper structure to hold the range to quark set mapping would be an
217 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
218 * time. Using a simple O(n) list search for now for implementation speed and
219 * given that we can expect to have a _relatively_ small number of enumeration
220 * ranges. This might become untrue if we are fed with symbol tables often
221 * required to lookup function names from instruction pointer value.
224 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
225 struct cds_list_head range_to_quark
; /* (range, GQuark) */
226 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
231 struct type_integer
*integer_type
;
232 struct enum_table table
;
235 struct definition_enum
{
237 struct definition_integer
*integer
;
238 struct type_enum
*type
;
239 /* Last GQuark values read. Keeping a reference on the GQuark array. */
247 struct definition_string
{
249 struct type_string
*type
;
250 char *value
; /* freed at definition_string teardown */
260 struct definition
*definition
;
265 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
266 struct type_scope
*scope
;
267 GArray
*fields
; /* Array of type_field */
270 struct definition_struct
{
272 struct type_struct
*type
;
273 struct definition_scope
*scope
;
274 GArray
*fields
; /* Array of struct field */
277 struct type_variant
{
279 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
280 struct type_scope
*scope
;
281 GArray
*fields
; /* Array of type_field */
282 GQuark tag_name
; /* TODO */
283 /* Tag name must be nonzero and must exist when defining the variant */
286 struct definition_variant
{
288 struct type_variant
*type
;
289 struct definition_scope
*scope
;
290 struct definition
*enum_tag
;
291 GArray
*fields
; /* Array of struct field */
292 struct field
*current_field
; /* Last field read */
299 struct type_scope
*scope
;
302 struct definition_array
{
304 struct type_array
*type
;
305 struct definition_scope
*scope
;
306 struct field current_element
; /* struct field */
309 struct type_sequence
{
311 struct type_integer
*len_type
;
313 struct type_scope
*scope
;
316 struct definition_sequence
{
318 struct type_sequence
*type
;
319 struct definition_scope
*scope
;
320 struct definition_integer
*len
;
321 struct field current_element
; /* struct field */
324 int register_type(GQuark type_name
, struct definition
*definition
,
325 struct type_scope
*scope
);
326 struct definition
*lookup_type(GQuark type_name
,
327 struct type_scope
*scope
);
330 * Type scopes also contain a separate registry for struct, variant and
331 * enum types. Those register types rather than type definitions, so
332 * that a named variant can be declared without specifying its target
333 * "choice" tag field immediately.
335 int register_struct_type(GQuark struct_name
, struct type_struct
*struct_type
,
336 struct type_scope
*scope
);
337 struct type_struct
*lookup_struct_type(GQuark struct_name
,
338 struct type_scope
*scope
);
339 int register_variant_type(GQuark variant_name
,
340 struct type_variant
*variant_type
,
341 struct type_scope
*scope
);
342 struct type_variant
*lookup_variant_type(GQuark variant_name
,
343 struct type_scope
*scope
);
344 int register_enum_type(GQuark enum_name
, struct type_enum
*enum_type
,
345 struct type_scope
*scope
);
346 struct type_enum
*lookup_enum_type(GQuark enum_name
,
347 struct type_scope
*scope
);
349 struct type_scope
*new_type_scope(struct type_scope
*parent_scope
);
350 void free_type_scope(struct type_scope
*scope
);
353 * field_definition is for field definitions. They are registered into
357 lookup_field_definition(GQuark field_name
,
358 struct definition_scope
*scope
);
359 int register_field_definition(GQuark field_name
,
360 struct definition
*definition
,
361 struct definition_scope
*scope
);
362 struct definition_scope
*
363 new_definition_scope(struct definition_scope
*parent_scope
);
364 void free_definition_scope(struct definition_scope
*scope
);
366 void type_ref(struct type
*type
);
367 void type_unref(struct type
*type
);
369 void definition_ref(struct definition
*definition
);
370 void definition_unref(struct definition
*definition
);
372 /* Nameless types can be created by passing a NULL name */
374 struct type_integer
*integer_type_new(const char *name
,
375 size_t len
, int byte_order
,
376 int signedness
, size_t alignment
);
379 * mantissa_len is the length of the number of bytes represented by the mantissa
380 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
382 struct type_float
*float_type_new(const char *name
,
384 size_t exp_len
, int byte_order
,
388 * A GQuark can be translated to/from strings with g_quark_from_string() and
389 * g_quark_to_string().
393 * Returns a GArray of GQuark or NULL.
394 * Caller must release the GArray with g_array_unref().
396 GArray
*enum_uint_to_quark_set(const struct type_enum
*enum_type
, uint64_t v
);
399 * Returns a GArray of GQuark or NULL.
400 * Caller must release the GArray with g_array_unref().
402 GArray
*enum_int_to_quark_set(const struct type_enum
*enum_type
, uint64_t v
);
405 * Returns a GArray of struct enum_range or NULL.
406 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
409 GArray
*enum_quark_to_range_set(const struct type_enum
*enum_type
, GQuark q
);
410 void enum_signed_insert(struct type_enum
*enum_type
,
411 int64_t start
, int64_t end
, GQuark q
);
412 void enum_unsigned_insert(struct type_enum
*enum_type
,
413 uint64_t start
, uint64_t end
, GQuark q
);
414 size_t enum_get_nr_enumerators(struct type_enum
*enum_type
);
416 struct type_enum
*enum_type_new(const char *name
,
417 struct type_integer
*integer_type
);
419 struct type_struct
*struct_type_new(const char *name
,
420 struct type_scope
*parent_scope
);
421 void struct_type_add_field(struct type_struct
*struct_type
,
422 const char *field_name
, struct type
*field_type
);
424 * Returns the index of a field within a structure.
426 unsigned long struct_type_lookup_field_index(struct type_struct
*struct_type
,
429 * field returned only valid as long as the field structure is not appended to.
432 struct_type_get_field_from_index(struct type_struct
*struct_type
,
433 unsigned long index
);
435 struct_get_field_from_index(struct definition_struct
*struct_definition
,
436 unsigned long index
);
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 type_variant
*variant_type_new(const char *name
,
444 struct type_scope
*parent_scope
);
445 void variant_type_add_field(struct type_variant
*variant_type
,
446 const char *tag_name
, struct type
*tag_type
);
448 variant_type_get_field_from_tag(struct type_variant
*variant_type
, GQuark tag
);
450 * Returns 0 on success, -EPERM on error.
452 int variant_definition_set_tag(struct definition_variant
*variant
,
453 struct definition
*enum_tag
);
455 * Returns the field selected by the current tag value.
456 * field returned only valid as long as the variant structure is not appended
460 variant_get_current_field(struct definition_variant
*variant
);
463 * elem_type passed as parameter now belongs to the array. No need to free it
464 * explicitly. "len" is the number of elements in the array.
466 struct type_array
*array_type_new(const char *name
,
467 size_t len
, struct type
*elem_type
,
468 struct type_scope
*parent_scope
);
471 * int_type and elem_type passed as parameter now belong to the sequence. No
472 * need to free them explicitly.
474 struct type_sequence
*sequence_type_new(const char *name
,
475 struct type_integer
*len_type
,
476 struct type
*elem_type
,
477 struct type_scope
*parent_scope
);
479 #endif /* _BABELTRACE_TYPES_H */