1 #ifndef _BABELTRACE_TYPES_H
2 #define _BABELTRACE_TYPES_H
9 * Copyright 2010 - 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 unsigned 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
, unsigned 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 unsigned 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
);
90 GQuark name
; /* type name */
91 size_t alignment
; /* type alignment, in bits */
92 int ref
; /* number of references to the type */
94 * Type copy function. Knows how to find the child type_class from the
97 void (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
98 struct stream_pos
*src
, const struct format
*fsrc
,
99 const struct type_class
*type_class
);
100 void (*free
)(struct type_class
*type_class
);
104 * Because we address in bits, bitfields end up being exactly the same as
105 * integers, except that their read/write functions must be able to deal with
106 * read/write non aligned on CHAR_BIT.
108 struct type_class_integer
{
110 size_t len
; /* length, in bits. */
111 int byte_order
; /* byte order */
115 struct type_class_float
{
117 struct type_class_integer
*sign
;
118 struct type_class_integer
*mantissa
;
119 struct type_class_integer
*exp
;
121 /* TODO: we might want to express more info about NaN, +inf and -inf */
125 * enum_val_equal assumes that signed and unsigned memory layout overlap.
131 } start
; /* lowest range value */
135 } end
; /* highest range value */
138 struct enum_range_to_quark
{
139 struct cds_list_head node
;
140 struct enum_range range
;
145 * We optimize the common case (range of size 1: single value) by creating a
146 * hash table mapping values to quark sets. We then lookup the ranges to
147 * complete the quark set.
149 * TODO: The proper structure to hold the range to quark set mapping would be an
150 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
151 * time. Using a simple O(n) list search for now for implementation speed and
152 * given that we can expect to have a _relatively_ small number of enumeration
153 * ranges. This might become untrue if we are fed with symbol tables often
154 * required to lookup function names from instruction pointer value.
157 GHashTable
*value_to_quark_set
; /* (value, GQuark GArray) */
158 struct cds_list_head range_to_quark
; /* (range, GQuark) */
159 GHashTable
*quark_to_range_set
; /* (GQuark, range GArray) */
162 struct type_class_enum
{
163 struct type_class_integer p
; /* inherit from integer */
164 struct enum_table table
;
167 struct type_class_string
{
173 struct type_class
*type_class
;
176 struct type_class_struct
{
178 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
179 GArray
*fields
; /* Array of fields */
182 struct type_class_array
{
185 struct type_class
*elem
;
188 struct type_class_sequence
{
190 struct type_class_integer
*len_class
;
191 struct type_class
*elem
;
194 struct type_class
*lookup_type(GQuark qname
);
195 int register_type(struct type_class
*type_class
);
197 void type_ref(struct type_class
*type_class
);
198 void type_unref(struct type_class
*type_class
);
200 /* Nameless types can be created by passing a NULL name */
202 struct type_class_integer
*integer_type_new(const char *name
,
203 size_t len
, int byte_order
,
206 void integer_type_free(struct type_class_integer
*int_class
);
209 * mantissa_len is the length of the number of bytes represented by the mantissa
210 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
212 struct type_class_float
*float_type_new(const char *name
,
214 size_t exp_len
, int byte_order
,
216 void float_type_free(struct type_class_float
*float_class
);
219 * A GQuark can be translated to/from strings with g_quark_from_string() and
220 * g_quark_to_string().
222 GArray
*enum_uint_to_quark_set(const struct type_class_enum
*enum_class
,
226 * Returns a GArray or NULL.
227 * Caller must release the GArray with g_array_unref().
229 GArray
*enum_int_to_quark_set(const struct type_class_enum
*enum_class
,
231 GArray
*enum_quark_to_range_set(const struct type_class_enum
*enum_class
,
233 void enum_signed_insert(struct type_class_enum
*enum_class
,
234 int64_t start
, int64_t end
, GQuark q
);
235 void enum_unsigned_insert(struct type_class_enum
*enum_class
,
236 uint64_t start
, uint64_t end
, GQuark q
);
238 struct type_class_enum
*enum_type_new(const char *name
,
239 size_t len
, int byte_order
,
242 void enum_type_free(struct type_class_enum
*enum_class
);
244 struct type_class_struct
*struct_type_new(const char *name
);
245 void struct_type_free(struct type_class_struct
*struct_class
);
246 void struct_type_add_field(struct type_class_struct
*struct_class
,
247 const char *field_name
,
248 struct type_class
*type_class
);
250 * Returns the index of a field within a structure.
253 struct_type_lookup_field_index(struct type_class_struct
*struct_class
,
256 * field returned only valid as long as the field structure is not appended to.
259 struct_type_get_field_from_index(struct type_class_struct
*struct_class
,
260 unsigned long index
);
263 * elem_class passed as parameter now belongs to the array. No need to free it
266 struct type_class_array
*array_type_new(const char *name
,
268 struct type_class
*elem_class
);
269 void array_type_free(struct type_class_array
*array_class
);
272 * int_class and elem_class passed as parameter now belongs to the sequence. No
273 * need to free them explicitely.
275 struct type_class_sequence
*sequence_type_new(const char *name
,
276 struct type_class_integer
*len_class
,
277 struct type_class
*elem_class
);
278 void sequence_type_free(struct type_class_sequence
*sequence_class
);
280 #endif /* _BABELTRACE_TYPES_H */