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/format.h>
23 #include <babeltrace/align.h>
26 /* Preallocate this many fields for structures */
27 #define DEFAULT_NR_STRUCT_FIELDS 8
30 * Always update stream_pos with move_pos and init_pos.
33 unsigned char *base
; /* Base address */
34 size_t offset
; /* Offset from base, in bits */
35 int dummy
; /* Dummy position, for length calculation */
39 void init_pos(struct stream_pos
*pos
, unsigned char *base
)
41 pos
->base
= base
; /* initial base, page-aligned */
47 * move_pos - move position of a relative bit offset
49 * TODO: allow larger files by updating base too.
52 void move_pos(struct stream_pos
*pos
, size_t offset
)
54 pos
->offset
= pos
->offset
+ offset
;
58 * align_pos - align position on a bit offset (> 0)
60 * TODO: allow larger files by updating base too.
63 void align_pos(struct stream_pos
*pos
, size_t offset
)
65 pos
->offset
+= offset_align(pos
->offset
, offset
);
69 void copy_pos(struct stream_pos
*dest
, struct stream_pos
*src
)
71 memcpy(dest
, src
, sizeof(struct stream_pos
));
75 unsigned char *get_pos_addr(struct stream_pos
*pos
)
77 /* Only makes sense to get the address after aligning on CHAR_BIT */
78 assert(!(pos
->alignment
% CHAR_BIT
));
79 return pos
->base
+ (pos
->offset
/ CHAR_BIT
);
83 GQuark name
; /* type name */
84 size_t alignment
; /* type alignment, in bits */
86 * Type copy function. Knows how to find the child type_class from the
89 size_t (*copy
)(struct stream_pos
*dest
, const struct format
*fdest
,
90 struct stream_pos
*src
, const struct format
*fsrc
,
91 const struct type_class
*type_class
);
92 void (*free
)(struct type_class
*type_class
);
96 * Because we address in bits, bitfields end up being exactly the same as
97 * integers, except that their read/write functions must be able to deal with
98 * read/write non aligned on CHAR_BIT.
100 struct type_class_integer
{
102 size_t len
; /* length, in bits. */
103 int byte_order
; /* byte order */
107 struct type_class_float
{
109 struct int_class
*sign
;
110 struct int_class
*mantissa
;
111 struct int_class
*exp
;
113 /* TODO: we might want to express more info about NaN, +inf and -inf */
117 GHashTable
*value_to_quark
; /* Tuples (value, GQuark) */
118 GHashTable
*quark_to_value
; /* Tuples (GQuark, value) */
121 struct type_class_enum
{
122 struct type_class_int p
; /* inherit from integer */
123 struct enum_table table
;
126 struct type_class_string
{
132 struct type_class
*type_class
;
135 struct type_class_struct
{
137 GHashTable
*fields_by_name
; /* Tuples (field name, field index) */
138 GArray
*fields
; /* Array of fields */
141 struct type_class_array
{
144 struct type_class
*elem
;
147 struct type_class_sequence
{
149 struct type_class_integer
*len_class
;
150 struct type_class
*elem
;
153 struct type_class
*ctf_lookup_type(GQuark qname
);
154 int ctf_register_type(struct type_class
*type_class
);
156 /* Nameless types can be created by passing a NULL name */
158 struct type_class_integer
*integer_type_new(const char *name
,
159 size_t len
, int byte_order
,
162 void integer_type_free(struct type_class_integer
*int_class
);
165 * mantissa_len is the length of the number of bytes represented by the mantissa
166 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
168 struct type_class_float
*float_type_new(const char *name
,
170 size_t exp_len
, int byte_order
,
172 void float_type_free(struct type_class_float
*float_class
);
175 * A GQuark can be translated to/from strings with g_quark_from_string() and
176 * g_quark_to_string().
178 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
);
179 GQuark
enum_int_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
);
180 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
181 size_t len
, int byte_order
, GQuark q
);
182 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
183 size_t len
, int byte_order
, GQuark q
);
184 void enum_signed_insert(struct type_class_enum
*enum_class
,
185 int64_t v
, GQuark q
);
186 void enum_unsigned_insert(struct type_class_enum
*enum_class
,
187 uint64_t v
, GQuark q
);
189 struct type_class_enum
*enum_type_new(const char *name
,
190 size_t len
, int byte_order
,
193 void enum_type_free(struct type_class_enum
*enum_class
);
195 struct type_class_struct
*struct_type_new(const char *name
);
196 void struct_type_free(struct type_class_struct
*struct_class
);
197 void struct_type_add_field(struct type_class_struct
*struct_class
,
199 struct type_class
*type_class
);
201 * Returns the index of a field within a structure.
204 struct_type_lookup_field_index(struct type_class_struct
*struct_class
,
207 * field returned only valid as long as the field structure is not appended to.
210 struct_type_get_field_from_index(struct type_class_struct
*struct_class
,
211 unsigned long index
);
214 * elem_class passed as parameter now belongs to the array. No need to free it
217 struct type_class_array
*array_type_new(const char *name
,
219 struct type_class
*elem_class
);
220 void array_type_free(struct type_class_array
*array_class
);
223 * int_class and elem_class passed as parameter now belongs to the sequence. No
224 * need to free them explicitely.
226 struct type_class_sequence
*sequence_type_new(const char *name
,
227 struct type_class_integer
*len_class
,
228 struct type_class
*elem_class
);
229 void sequence_type_free(struct type_class_sequence
*sequence_class
);
231 #endif /* _BABELTRACE_TYPES_H */
This page took 0.051231 seconds and 4 git commands to generate.