Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_TYPES_H |
2 | #define _BABELTRACE_TYPES_H | |
3 | ||
4 | /* | |
5 | * BabelTrace | |
6 | * | |
fc93b2bd | 7 | * Type Header |
d79865b9 MD |
8 | * |
9 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
fc93b2bd | 26 | #include <babeltrace/format.h> |
bed864a7 MD |
27 | #include <babeltrace/align.h> |
28 | #include <string.h> | |
29 | ||
11796b96 MD |
30 | /* Preallocate this many fields for structures */ |
31 | #define DEFAULT_NR_STRUCT_FIELDS 8 | |
32 | ||
bed864a7 MD |
33 | /* |
34 | * Always update stream_pos with move_pos and init_pos. | |
35 | */ | |
36 | struct stream_pos { | |
37 | unsigned char *base; /* Base address */ | |
38 | size_t offset; /* Offset from base, in bits */ | |
39 | int dummy; /* Dummy position, for length calculation */ | |
40 | }; | |
41 | ||
42 | static inline | |
43 | void init_pos(struct stream_pos *pos, unsigned char *base) | |
44 | { | |
45 | pos->base = base; /* initial base, page-aligned */ | |
46 | pos->offset = 0; | |
47 | pos->dummy = false; | |
48 | } | |
49 | ||
50 | /* | |
51 | * move_pos - move position of a relative bit offset | |
52 | * | |
53 | * TODO: allow larger files by updating base too. | |
54 | */ | |
55 | static inline | |
56 | void move_pos(struct stream_pos *pos, size_t offset) | |
57 | { | |
58 | pos->offset = pos->offset + offset; | |
59 | } | |
60 | ||
61 | /* | |
62 | * align_pos - align position on a bit offset (> 0) | |
63 | * | |
64 | * TODO: allow larger files by updating base too. | |
65 | */ | |
66 | static inline | |
67 | void align_pos(struct stream_pos *pos, size_t offset) | |
68 | { | |
69 | pos->offset += offset_align(pos->offset, offset); | |
70 | } | |
71 | ||
72 | static inline | |
73 | void copy_pos(struct stream_pos *dest, struct stream_pos *src) | |
74 | { | |
75 | memcpy(dest, src, sizeof(struct stream_pos)); | |
76 | } | |
77 | ||
78 | static inline | |
79 | unsigned char *get_pos_addr(struct stream_pos *pos) | |
80 | { | |
81 | /* Only makes sense to get the address after aligning on CHAR_BIT */ | |
82 | assert(!(pos->alignment % CHAR_BIT)); | |
83 | return pos->base + (pos->offset / CHAR_BIT); | |
84 | } | |
fc93b2bd MD |
85 | |
86 | struct type_class { | |
87 | GQuark name; /* type name */ | |
88 | size_t alignment; /* type alignment, in bits */ | |
89 | /* | |
90 | * Type copy function. Knows how to find the child type_class from the | |
91 | * parent type_class. | |
92 | */ | |
bed864a7 MD |
93 | size_t (*copy)(struct stream_pos *dest, const struct format *fdest, |
94 | struct stream_pos *src, const struct format *fsrc, | |
fc93b2bd | 95 | const struct type_class *type_class); |
90b676d7 | 96 | void (*free)(struct type_class *type_class); |
fc93b2bd MD |
97 | }; |
98 | ||
bed864a7 MD |
99 | /* |
100 | * Because we address in bits, bitfields end up being exactly the same as | |
101 | * integers, except that their read/write functions must be able to deal with | |
102 | * read/write non aligned on CHAR_BIT. | |
103 | */ | |
7fe00194 MD |
104 | struct type_class_integer { |
105 | struct type_class p; | |
106 | size_t len; /* length, in bits. */ | |
107 | int byte_order; /* byte order */ | |
108 | int signedness; | |
fc93b2bd MD |
109 | }; |
110 | ||
111 | struct type_class_float { | |
112 | struct type_class p; | |
7fe00194 MD |
113 | struct int_class *sign; |
114 | struct int_class *mantissa; | |
115 | struct int_class *exp; | |
fc93b2bd | 116 | int byte_order; |
0a46062b | 117 | /* TODO: we might want to express more info about NaN, +inf and -inf */ |
fc93b2bd MD |
118 | }; |
119 | ||
448d3cc7 MD |
120 | struct enum_table { |
121 | GHashTable *value_to_quark; /* Tuples (value, GQuark) */ | |
122 | GHashTable *quark_to_value; /* Tuples (GQuark, value) */ | |
123 | }; | |
124 | ||
fc93b2bd | 125 | struct type_class_enum { |
7fe00194 | 126 | struct type_class_int p; /* inherit from integer */ |
448d3cc7 | 127 | struct enum_table table; |
fc93b2bd MD |
128 | }; |
129 | ||
bed864a7 MD |
130 | struct type_class_string { |
131 | struct type_class p; | |
132 | }; | |
133 | ||
11796b96 MD |
134 | struct field { |
135 | GQuark name; | |
136 | struct type_class *type_class; | |
137 | }; | |
138 | ||
fc93b2bd MD |
139 | struct type_class_struct { |
140 | struct type_class p; | |
11796b96 MD |
141 | GHashTable *fields_by_name; /* Tuples (field name, field index) */ |
142 | GArray *fields; /* Array of fields */ | |
143 | }; | |
144 | ||
145 | struct type_class_array { | |
146 | struct type_class p; | |
147 | size_t len; | |
148 | struct type_class *elem; | |
149 | }; | |
150 | ||
151 | struct type_class_sequence { | |
152 | struct type_class p; | |
2e7d72cf | 153 | struct type_class_integer *len_class; |
11796b96 | 154 | struct type_class *elem; |
fc93b2bd MD |
155 | }; |
156 | ||
698f0fe4 MD |
157 | struct type_class *ctf_lookup_type(GQuark qname); |
158 | int ctf_register_type(struct type_class *type_class); | |
159 | ||
0a46062b MD |
160 | /* Nameless types can be created by passing a NULL name */ |
161 | ||
162 | struct type_class_integer *integer_type_new(const char *name, | |
0a46062b | 163 | size_t len, int byte_order, |
11d43b90 MD |
164 | int signedness, |
165 | size_t alignment); | |
0a46062b MD |
166 | void integer_type_free(struct type_class_integer *int_class); |
167 | ||
11d43b90 MD |
168 | /* |
169 | * mantissa_len is the length of the number of bytes represented by the mantissa | |
170 | * (e.g. result of DBL_MANT_DIG). It includes the leading 1. | |
171 | */ | |
0a46062b MD |
172 | struct type_class_float *float_type_new(const char *name, |
173 | size_t mantissa_len, | |
174 | size_t exp_len, int byte_order, | |
175 | size_t alignment); | |
176 | void float_type_free(struct type_class_float *float_class); | |
177 | ||
448d3cc7 MD |
178 | /* |
179 | * A GQuark can be translated to/from strings with g_quark_from_string() and | |
180 | * g_quark_to_string(). | |
181 | */ | |
182 | GQuark enum_uint_to_quark(const struct type_class_enum *enum_class, uint64_t v); | |
183 | GQuark enum_int_to_quark(const struct type_class_enum *enum_class, uint64_t v); | |
184 | uint64_t enum_quark_to_uint(const struct type_class_enum *enum_class, | |
185 | size_t len, int byte_order, GQuark q); | |
186 | int64_t enum_quark_to_int(const struct type_class_enum *enum_class, | |
187 | size_t len, int byte_order, GQuark q); | |
188 | void enum_signed_insert(struct type_class_enum *enum_class, | |
189 | int64_t v, GQuark q); | |
190 | void enum_unsigned_insert(struct type_class_enum *enum_class, | |
191 | uint64_t v, GQuark q); | |
192 | ||
193 | struct type_class_enum *enum_type_new(const char *name, | |
448d3cc7 MD |
194 | size_t len, int byte_order, |
195 | int signedness, | |
196 | size_t alignment); | |
197 | void enum_type_free(struct type_class_enum *enum_class); | |
198 | ||
11796b96 MD |
199 | struct type_class_struct *struct_type_new(const char *name); |
200 | void struct_type_free(struct type_class_struct *struct_class); | |
201 | void struct_type_add_field(struct type_class_struct *struct_class, | |
202 | GQuark field_name, | |
203 | struct type_class *type_class); | |
204 | /* | |
205 | * Returns the index of a field within a structure. | |
206 | */ | |
207 | unsigned long | |
208 | struct_type_lookup_field_index(struct type_class_struct *struct_class, | |
209 | GQuark field_name); | |
210 | /* | |
211 | * field returned only valid as long as the field structure is not appended to. | |
212 | */ | |
213 | struct field * | |
214 | struct_type_get_field_from_index(struct type_class_struct *struct_class, | |
215 | unsigned long index); | |
216 | ||
d06d03db MD |
217 | /* |
218 | * elem_class passed as parameter now belongs to the array. No need to free it | |
219 | * explicitely. | |
220 | */ | |
11796b96 MD |
221 | struct type_class_array *array_type_new(const char *name, |
222 | size_t len, | |
223 | struct type_class *elem_class); | |
224 | void array_type_free(struct type_class_array *array_class); | |
225 | ||
d06d03db MD |
226 | /* |
227 | * int_class and elem_class passed as parameter now belongs to the sequence. No | |
228 | * need to free them explicitely. | |
229 | */ | |
11796b96 | 230 | struct type_class_sequence *sequence_type_new(const char *name, |
2e7d72cf | 231 | struct type_class_integer *len_class, |
11796b96 | 232 | struct type_class *elem_class); |
d06d03db | 233 | void sequence_type_free(struct type_class_sequence *sequence_class); |
11796b96 | 234 | |
d79865b9 | 235 | #endif /* _BABELTRACE_TYPES_H */ |