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 type */
94 struct type_scope
*parent_scope
;
97 /* declaration scope */
98 struct declaration_scope
{
99 /* Hash table mapping field name GQuark to struct declaration */
100 GHashTable
*declarations
;
101 struct declaration_scope
*parent_scope
;
105 GQuark name
; /* type name */
106 size_t alignment
; /* type alignment, in bits */
107 int ref
; /* number of references to the type */
109 * type_free called with type ref is decremented to 0.
111 void (*type_free
)(struct type
*type
);
113 (*declaration_new
)(struct type
*type
,
114 struct declaration_scope
*parent_scope
);
116 * declaration_free called with declaration ref is decremented to 0.
118 void (*declaration_free
)(struct declaration
*declaration
);
120 * Declaration copy function. Knows how to find the child declaration
121 * from the parent declaration.
123 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
124 struct stream_pos
*src
, const struct format
*fsrc
,
125 struct declaration
*declaration
);
130 int ref
; /* number of references to the declaration */
134 * Because we address in bits, bitfields end up being exactly the same as
135 * integers, except that their read/write functions must be able to deal with
136 * read/write non aligned on CHAR_BIT.
138 struct type_integer
{
140 size_t len
; /* length, in bits. */
141 int byte_order
; /* byte order */
145 struct declaration_integer
{
146 struct declaration p
;
147 struct type_integer
*type
;
148 /* Last values read */
157 struct type_integer
*sign
;
158 struct type_integer
*mantissa
;
159 struct type_integer
*exp
;
161 /* TODO: we might want to express more info about NaN, +inf and -inf */
164 struct declaration_float
{
165 struct declaration p
;
166 struct type_float
*type
;
167 /* Last values read */
172 * enum_val_equal assumes that signed and unsigned memory layout overlap.
178 } start
; /* lowest range value */
182 } end
; /* highest range value */
185 struct enum_range_to_quark
{
186 struct cds_list_head node
;
187 struct enum_range range
;
192 * We optimize the common case (range of size 1: single value) by creating a
193 * hash table mapping values to quark sets. We then lookup the ranges to
194 * complete the quark set.
196 * TODO: The proper structure to hold the range to quark set mapping would be an
197 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
198 * time. Using a simple O(n) list search for now for implementation speed and
199 * given that we can expect to have a _relatively_ small number of enumeration
200 * ranges. This might become untrue if we are fed with symbol tables often
201 * required to lookup function names from instruction pointer value.
204 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
205 struct cds_list_head range_to_quark
; /* (range, GQuark) */
206 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
211 struct type_integer
*integer_type
;
212 struct enum_table table
;
215 struct declaration_enum
{
216 struct declaration p
;
217 struct declaration_integer
*integer
;
218 struct type_enum
*type
;
219 /* Last GQuark values read. Keeping a reference on the GQuark array. */
227 struct declaration_string
{
228 struct declaration p
;
229 struct type_string
*type
;
230 char *value
; /* freed at declaration_string teardown */
240 struct declaration
*declaration
;
245 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
246 struct type_scope
*scope
;
247 GArray
*fields
; /* Array of type_field */
250 struct declaration_struct
{
251 struct declaration p
;
252 struct type_struct
*type
;
253 struct declaration_scope
*scope
;
254 GArray
*fields
; /* Array of struct field */
257 struct type_variant
{
259 GHashTable
*fields_by_tag
; /* Tuples (field tag, field index) */
260 struct type_scope
*scope
;
261 GArray
*fields
; /* Array of type_field */
264 struct declaration_variant
{
265 struct declaration p
;
266 struct type_variant
*type
;
267 struct declaration_scope
*scope
;
268 struct declaration
*enum_tag
;
269 GArray
*fields
; /* Array of struct field */
270 struct field
*current_field
; /* Last field read */
277 struct type_scope
*scope
;
280 struct declaration_array
{
281 struct declaration p
;
282 struct type_array
*type
;
283 struct declaration_scope
*scope
;
284 struct field current_element
; /* struct field */
287 struct type_sequence
{
289 struct type_integer
*len_type
;
291 struct type_scope
*scope
;
294 struct declaration_sequence
{
295 struct declaration p
;
296 struct type_sequence
*type
;
297 struct declaration_scope
*scope
;
298 struct declaration_integer
*len
;
299 struct field current_element
; /* struct field */
302 int register_type(GQuark type_name
, struct type
*type
,
303 struct type_scope
*scope
);
304 struct type
*lookup_type(GQuark type_name
, struct type_scope
*scope
);
305 struct type_scope
*new_type_scope(struct type_scope
*parent_scope
);
306 void free_type_scope(struct type_scope
*scope
);
309 lookup_declaration(GQuark field_name
, struct declaration_scope
*scope
);
310 int register_declaration(GQuark field_name
, struct declaration
*declaration
,
311 struct declaration_scope
*scope
);
312 struct declaration_scope
*
313 new_declaration_scope(struct declaration_scope
*parent_scope
);
314 void free_declaration_scope(struct declaration_scope
*scope
);
316 void type_ref(struct type
*type
);
317 void type_unref(struct type
*type
);
319 void declaration_ref(struct declaration
*declaration
);
320 void declaration_unref(struct declaration
*declaration
);
322 /* Nameless types can be created by passing a NULL name */
324 struct type_integer
*integer_type_new(const char *name
,
325 size_t len
, int byte_order
,
326 int signedness
, size_t alignment
);
329 * mantissa_len is the length of the number of bytes represented by the mantissa
330 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
332 struct type_float
*float_type_new(const char *name
,
334 size_t exp_len
, int byte_order
,
338 * A GQuark can be translated to/from strings with g_quark_from_string() and
339 * g_quark_to_string().
343 * Returns a GArray of GQuark or NULL.
344 * Caller must release the GArray with g_array_unref().
346 GArray
*enum_uint_to_quark_set(const struct type_enum
*enum_type
, uint64_t v
);
349 * Returns a GArray of GQuark or NULL.
350 * Caller must release the GArray with g_array_unref().
352 GArray
*enum_int_to_quark_set(const struct type_enum
*enum_type
, uint64_t v
);
355 * Returns a GArray of struct enum_range or NULL.
356 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
359 GArray
*enum_quark_to_range_set(const struct type_enum
*enum_type
, GQuark q
);
360 void enum_signed_insert(struct type_enum
*enum_type
,
361 int64_t start
, int64_t end
, GQuark q
);
362 void enum_unsigned_insert(struct type_enum
*enum_type
,
363 uint64_t start
, uint64_t end
, GQuark q
);
364 size_t enum_get_nr_enumerators(struct type_enum
*enum_type
);
366 struct type_enum
*enum_type_new(const char *name
,
367 struct type_integer
*integer_type
);
369 struct type_struct
*struct_type_new(const char *name
,
370 struct type_scope
*parent_scope
);
371 void struct_type_add_field(struct type_struct
*struct_type
,
372 const char *field_name
, struct type
*field_type
);
374 * Returns the index of a field within a structure.
376 unsigned long struct_type_lookup_field_index(struct type_struct
*struct_type
,
379 * field returned only valid as long as the field structure is not appended to.
382 struct_type_get_field_from_index(struct type_struct
*struct_type
,
383 unsigned long index
);
385 struct_get_field_from_index(struct declaration_struct
*struct_declaration
,
386 unsigned long index
);
389 * The tag enumeration is validated to ensure that it contains only mappings
390 * from numeric values to a single tag. Overlapping tag value ranges are
391 * therefore forbidden.
393 struct type_variant
*variant_type_new(const char *name
,
394 struct type_scope
*parent_scope
);
395 void variant_type_add_field(struct type_variant
*variant_type
,
396 const char *tag_name
, struct type
*tag_type
);
398 variant_type_get_field_from_tag(struct type_variant
*variant_type
, GQuark tag
);
400 * Returns 0 on success, -EPERM on error.
402 int variant_declaration_set_tag(struct declaration_variant
*variant
,
403 struct declaration
*enum_tag
);
405 * Returns the field selected by the current tag value.
406 * field returned only valid as long as the variant structure is not appended
410 variant_get_current_field(struct declaration_variant
*variant
);
413 * elem_type passed as parameter now belongs to the array. No need to free it
414 * explicitly. "len" is the number of elements in the array.
416 struct type_array
*array_type_new(const char *name
,
417 size_t len
, struct type
*elem_type
,
418 struct type_scope
*parent_scope
);
421 * int_type and elem_type passed as parameter now belong to the sequence. No
422 * need to free them explicitly.
424 struct type_sequence
*sequence_type_new(const char *name
,
425 struct type_integer
*len_type
,
426 struct type
*elem_type
,
427 struct type_scope
*parent_scope
);
429 #endif /* _BABELTRACE_TYPES_H */