fd174b08c3704be074129bd7a094da3b72bb3a66
[babeltrace.git] / include / babeltrace / types.h
1 #ifndef _BABELTRACE_TYPES_H
2 #define _BABELTRACE_TYPES_H
3
4 /*
5 * BabelTrace
6 *
7 * Type Header
8 *
9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22 #include <babeltrace/align.h>
23 #include <babeltrace/list.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <limits.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <assert.h>
30
31 /* Preallocate this many fields for structures */
32 #define DEFAULT_NR_STRUCT_FIELDS 8
33
34 /*
35 * Always update stream_pos with move_pos and init_pos.
36 */
37 struct stream_pos {
38 char *base; /* Base address */
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41 };
42
43 static inline
44 void init_pos(struct stream_pos *pos, char *base)
45 {
46 pos->base = base; /* initial base, page-aligned */
47 pos->offset = 0;
48 pos->dummy = false;
49 }
50
51 /*
52 * move_pos - move position of a relative bit offset
53 *
54 * TODO: allow larger files by updating base too.
55 */
56 static inline
57 void move_pos(struct stream_pos *pos, size_t offset)
58 {
59 pos->offset = pos->offset + offset;
60 }
61
62 /*
63 * align_pos - align position on a bit offset (> 0)
64 *
65 * TODO: allow larger files by updating base too.
66 */
67 static inline
68 void align_pos(struct stream_pos *pos, size_t offset)
69 {
70 pos->offset += offset_align(pos->offset, offset);
71 }
72
73 static inline
74 void copy_pos(struct stream_pos *dest, struct stream_pos *src)
75 {
76 memcpy(dest, src, sizeof(struct stream_pos));
77 }
78
79 static inline
80 char *get_pos_addr(struct stream_pos *pos)
81 {
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);
85 }
86
87 struct format;
88 struct declaration;
89
90 /* type scope */
91 struct type_scope {
92 /* Hash table mapping type name GQuark to "struct declaration" */
93 GHashTable *type_declarations;
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;
101 };
102
103 /* declaration scope */
104 struct declaration_scope {
105 /* Hash table mapping field name GQuark to "struct declaration" */
106 GHashTable *declarations;
107 struct declaration_scope *parent_scope;
108 };
109
110 enum ctf_type_id {
111 CTF_TYPE_UNKNOWN = 0,
112 CTF_TYPE_INTEGER,
113 CTF_TYPE_FLOAT,
114 CTF_TYPE_ENUM,
115 CTF_TYPE_STRING,
116 CTF_TYPE_STRUCT,
117 CTF_TYPE_VARIANT,
118 CTF_TYPE_ARRAY,
119 CTF_TYPE_SEQUENCE,
120 NR_CTF_TYPES,
121 };
122
123 struct type {
124 enum ctf_type_id id;
125 GQuark name; /* type name */
126 size_t alignment; /* type alignment, in bits */
127 int ref; /* number of references to the type */
128 /*
129 * type_free called with type ref is decremented to 0.
130 */
131 void (*type_free)(struct type *type);
132 struct declaration *
133 (*declaration_new)(struct type *type,
134 struct declaration_scope *parent_scope);
135 /*
136 * declaration_free called with declaration ref is decremented to 0.
137 */
138 void (*declaration_free)(struct declaration *declaration);
139 /*
140 * Declaration copy function. Knows how to find the child declaration
141 * from the parent declaration.
142 */
143 void (*copy)(struct stream_pos *dest, const struct format *fdest,
144 struct stream_pos *src, const struct format *fsrc,
145 struct declaration *declaration);
146 };
147
148 struct declaration {
149 struct type *type;
150 int ref; /* number of references to the declaration */
151 };
152
153 /*
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.
157 */
158 struct type_integer {
159 struct type p;
160 size_t len; /* length, in bits. */
161 int byte_order; /* byte order */
162 int signedness;
163 };
164
165 struct declaration_integer {
166 struct declaration p;
167 struct type_integer *type;
168 /* Last values read */
169 union {
170 uint64_t _unsigned;
171 int64_t _signed;
172 } value;
173 };
174
175 struct type_float {
176 struct type p;
177 struct type_integer *sign;
178 struct type_integer *mantissa;
179 struct type_integer *exp;
180 int byte_order;
181 /* TODO: we might want to express more info about NaN, +inf and -inf */
182 };
183
184 struct declaration_float {
185 struct declaration p;
186 struct type_float *type;
187 /* Last values read */
188 long double value;
189 };
190
191 /*
192 * enum_val_equal assumes that signed and unsigned memory layout overlap.
193 */
194 struct enum_range {
195 union {
196 int64_t _signed;
197 uint64_t _unsigned;
198 } start; /* lowest range value */
199 union {
200 int64_t _signed;
201 uint64_t _unsigned;
202 } end; /* highest range value */
203 };
204
205 struct enum_range_to_quark {
206 struct cds_list_head node;
207 struct enum_range range;
208 GQuark quark;
209 };
210
211 /*
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.
215 *
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.
222 */
223 struct enum_table {
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) */
227 };
228
229 struct type_enum {
230 struct type p;
231 struct type_integer *integer_type;
232 struct enum_table table;
233 };
234
235 struct declaration_enum {
236 struct declaration p;
237 struct declaration_integer *integer;
238 struct type_enum *type;
239 /* Last GQuark values read. Keeping a reference on the GQuark array. */
240 GArray *value;
241 };
242
243 struct type_string {
244 struct type p;
245 };
246
247 struct declaration_string {
248 struct declaration p;
249 struct type_string *type;
250 char *value; /* freed at declaration_string teardown */
251 };
252
253 struct type_field {
254 GQuark name;
255 struct type *type;
256 };
257
258 struct field {
259 GQuark name;
260 struct declaration *declaration;
261 };
262
263 struct type_struct {
264 struct type p;
265 GHashTable *fields_by_name; /* Tuples (field name, field index) */
266 struct type_scope *scope;
267 GArray *fields; /* Array of type_field */
268 };
269
270 struct declaration_struct {
271 struct declaration p;
272 struct type_struct *type;
273 struct declaration_scope *scope;
274 GArray *fields; /* Array of struct field */
275 };
276
277 struct type_variant {
278 struct type p;
279 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
280 struct type_scope *scope;
281 GArray *fields; /* Array of type_field */
282 };
283
284 struct declaration_variant {
285 struct declaration p;
286 struct type_variant *type;
287 struct declaration_scope *scope;
288 struct declaration *enum_tag;
289 GArray *fields; /* Array of struct field */
290 struct field *current_field; /* Last field read */
291 };
292
293 struct type_array {
294 struct type p;
295 size_t len;
296 struct type *elem;
297 struct type_scope *scope;
298 };
299
300 struct declaration_array {
301 struct declaration p;
302 struct type_array *type;
303 struct declaration_scope *scope;
304 struct field current_element; /* struct field */
305 };
306
307 struct type_sequence {
308 struct type p;
309 struct type_integer *len_type;
310 struct type *elem;
311 struct type_scope *scope;
312 };
313
314 struct declaration_sequence {
315 struct declaration p;
316 struct type_sequence *type;
317 struct declaration_scope *scope;
318 struct declaration_integer *len;
319 struct field current_element; /* struct field */
320 };
321
322 /*
323 * type_declaration is for typedef and typealias. They are registered
324 * into type scopes.
325 */
326 int register_type_declaration(GQuark type_name, struct declaration *declaration,
327 struct type_scope *scope);
328 struct declaration *lookup_type_declaration(GQuark type_name,
329 struct type_scope *scope);
330
331 /*
332 * Type scopes also contain a separate registry for struct, variant and
333 * enum types. Those register types rather than type declarations, so
334 * that a named variant can be declared without specifying its target
335 * "choice" tag field immediately.
336 */
337 int register_struct_type(GQuark struct_name, struct type_struct *struct_type,
338 struct type_scope *scope);
339 struct type_struct *lookup_struct_type(GQuark struct_name,
340 struct type_scope *scope);
341 int register_variant_type(GQuark variant_name,
342 struct type_variant *variant_type,
343 struct type_scope *scope);
344 struct type_variant *lookup_variant_type(GQuark variant_name,
345 struct type_scope *scope);
346 int register_enum_type(GQuark enum_name, struct type_enum *enum_type,
347 struct type_scope *scope);
348 struct type_enum *lookup_enum_type(GQuark enum_name,
349 struct type_scope *scope);
350
351 struct type_scope *new_type_scope(struct type_scope *parent_scope);
352 void free_type_scope(struct type_scope *scope);
353
354 /*
355 * field_declaration is for field declarations. They are registered into
356 * declaration scopes.
357 */
358 struct declaration *
359 lookup_field_declaration(GQuark field_name,
360 struct declaration_scope *scope);
361 int register_field_declaration(GQuark field_name,
362 struct declaration *declaration,
363 struct declaration_scope *scope);
364 struct declaration_scope *
365 new_declaration_scope(struct declaration_scope *parent_scope);
366 void free_declaration_scope(struct declaration_scope *scope);
367
368 void type_ref(struct type *type);
369 void type_unref(struct type *type);
370
371 void declaration_ref(struct declaration *declaration);
372 void declaration_unref(struct declaration *declaration);
373
374 /* Nameless types can be created by passing a NULL name */
375
376 struct type_integer *integer_type_new(const char *name,
377 size_t len, int byte_order,
378 int signedness, size_t alignment);
379
380 /*
381 * mantissa_len is the length of the number of bytes represented by the mantissa
382 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
383 */
384 struct type_float *float_type_new(const char *name,
385 size_t mantissa_len,
386 size_t exp_len, int byte_order,
387 size_t alignment);
388
389 /*
390 * A GQuark can be translated to/from strings with g_quark_from_string() and
391 * g_quark_to_string().
392 */
393
394 /*
395 * Returns a GArray of GQuark or NULL.
396 * Caller must release the GArray with g_array_unref().
397 */
398 GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
399
400 /*
401 * Returns a GArray of GQuark or NULL.
402 * Caller must release the GArray with g_array_unref().
403 */
404 GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
405
406 /*
407 * Returns a GArray of struct enum_range or NULL.
408 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
409 * release it).
410 */
411 GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
412 void enum_signed_insert(struct type_enum *enum_type,
413 int64_t start, int64_t end, GQuark q);
414 void enum_unsigned_insert(struct type_enum *enum_type,
415 uint64_t start, uint64_t end, GQuark q);
416 size_t enum_get_nr_enumerators(struct type_enum *enum_type);
417
418 struct type_enum *enum_type_new(const char *name,
419 struct type_integer *integer_type);
420
421 struct type_struct *struct_type_new(const char *name,
422 struct type_scope *parent_scope);
423 void struct_type_add_field(struct type_struct *struct_type,
424 const char *field_name, struct type *field_type);
425 /*
426 * Returns the index of a field within a structure.
427 */
428 unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
429 GQuark field_name);
430 /*
431 * field returned only valid as long as the field structure is not appended to.
432 */
433 struct type_field *
434 struct_type_get_field_from_index(struct type_struct *struct_type,
435 unsigned long index);
436 struct field *
437 struct_get_field_from_index(struct declaration_struct *struct_declaration,
438 unsigned long index);
439
440 /*
441 * The tag enumeration is validated to ensure that it contains only mappings
442 * from numeric values to a single tag. Overlapping tag value ranges are
443 * therefore forbidden.
444 */
445 struct type_variant *variant_type_new(const char *name,
446 struct type_scope *parent_scope);
447 void variant_type_add_field(struct type_variant *variant_type,
448 const char *tag_name, struct type *tag_type);
449 struct type_field *
450 variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
451 /*
452 * Returns 0 on success, -EPERM on error.
453 */
454 int variant_declaration_set_tag(struct declaration_variant *variant,
455 struct declaration *enum_tag);
456 /*
457 * Returns the field selected by the current tag value.
458 * field returned only valid as long as the variant structure is not appended
459 * to.
460 */
461 struct field *
462 variant_get_current_field(struct declaration_variant *variant);
463
464 /*
465 * elem_type passed as parameter now belongs to the array. No need to free it
466 * explicitly. "len" is the number of elements in the array.
467 */
468 struct type_array *array_type_new(const char *name,
469 size_t len, struct type *elem_type,
470 struct type_scope *parent_scope);
471
472 /*
473 * int_type and elem_type passed as parameter now belong to the sequence. No
474 * need to free them explicitly.
475 */
476 struct type_sequence *sequence_type_new(const char *name,
477 struct type_integer *len_type,
478 struct type *elem_type,
479 struct type_scope *parent_scope);
480
481 #endif /* _BABELTRACE_TYPES_H */
This page took 0.040372 seconds and 3 git commands to generate.