I/O structures accessors generation (work in progress)
[babeltrace.git] / include / babeltrace / types.h
... / ...
CommitLineData
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 */
37struct 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
43static inline
44void 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 */
56static inline
57void 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 */
67static inline
68void align_pos(struct stream_pos *pos, size_t offset)
69{
70 pos->offset += offset_align(pos->offset, offset);
71}
72
73static inline
74void copy_pos(struct stream_pos *dest, struct stream_pos *src)
75{
76 memcpy(dest, src, sizeof(struct stream_pos));
77}
78
79static inline
80char *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
87struct format;
88struct declaration;
89
90/* type scope */
91struct type_scope {
92 /* Hash table mapping type name GQuark to struct type */
93 GHashTable *types;
94 struct type_scope *parent_scope;
95};
96
97/* declaration scope */
98struct declaration_scope {
99 /* Hash table mapping field name GQuark to struct declaration */
100 GHashTable *declarations;
101 struct declaration_scope *parent_scope;
102};
103
104enum ctf_type_id {
105 CTF_TYPE_UNKNOWN = 0,
106 CTF_TYPE_INTEGER,
107 CTF_TYPE_FLOAT,
108 CTF_TYPE_ENUM,
109 CTF_TYPE_STRING,
110 CTF_TYPE_STRUCT,
111 CTF_TYPE_VARIANT,
112 CTF_TYPE_ARRAY,
113 CTF_TYPE_SEQUENCE,
114 NR_CTF_TYPES,
115};
116
117struct type {
118 enum ctf_type_id id;
119 GQuark name; /* type name */
120 size_t alignment; /* type alignment, in bits */
121 int ref; /* number of references to the type */
122 /*
123 * type_free called with type ref is decremented to 0.
124 */
125 void (*type_free)(struct type *type);
126 struct declaration *
127 (*declaration_new)(struct type *type,
128 struct declaration_scope *parent_scope);
129 /*
130 * declaration_free called with declaration ref is decremented to 0.
131 */
132 void (*declaration_free)(struct declaration *declaration);
133 /*
134 * Declaration copy function. Knows how to find the child declaration
135 * from the parent declaration.
136 */
137 void (*copy)(struct stream_pos *dest, const struct format *fdest,
138 struct stream_pos *src, const struct format *fsrc,
139 struct declaration *declaration);
140};
141
142struct declaration {
143 struct type *type;
144 int ref; /* number of references to the declaration */
145};
146
147/*
148 * Because we address in bits, bitfields end up being exactly the same as
149 * integers, except that their read/write functions must be able to deal with
150 * read/write non aligned on CHAR_BIT.
151 */
152struct type_integer {
153 struct type p;
154 size_t len; /* length, in bits. */
155 int byte_order; /* byte order */
156 int signedness;
157};
158
159struct declaration_integer {
160 struct declaration p;
161 struct type_integer *type;
162 /* Last values read */
163 union {
164 uint64_t _unsigned;
165 int64_t _signed;
166 } value;
167};
168
169struct type_float {
170 struct type p;
171 struct type_integer *sign;
172 struct type_integer *mantissa;
173 struct type_integer *exp;
174 int byte_order;
175 /* TODO: we might want to express more info about NaN, +inf and -inf */
176};
177
178struct declaration_float {
179 struct declaration p;
180 struct type_float *type;
181 /* Last values read */
182 long double value;
183};
184
185/*
186 * enum_val_equal assumes that signed and unsigned memory layout overlap.
187 */
188struct enum_range {
189 union {
190 int64_t _signed;
191 uint64_t _unsigned;
192 } start; /* lowest range value */
193 union {
194 int64_t _signed;
195 uint64_t _unsigned;
196 } end; /* highest range value */
197};
198
199struct enum_range_to_quark {
200 struct cds_list_head node;
201 struct enum_range range;
202 GQuark quark;
203};
204
205/*
206 * We optimize the common case (range of size 1: single value) by creating a
207 * hash table mapping values to quark sets. We then lookup the ranges to
208 * complete the quark set.
209 *
210 * TODO: The proper structure to hold the range to quark set mapping would be an
211 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
212 * time. Using a simple O(n) list search for now for implementation speed and
213 * given that we can expect to have a _relatively_ small number of enumeration
214 * ranges. This might become untrue if we are fed with symbol tables often
215 * required to lookup function names from instruction pointer value.
216 */
217struct enum_table {
218 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
219 struct cds_list_head range_to_quark; /* (range, GQuark) */
220 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
221};
222
223struct type_enum {
224 struct type p;
225 struct type_integer *integer_type;
226 struct enum_table table;
227};
228
229struct declaration_enum {
230 struct declaration p;
231 struct declaration_integer *integer;
232 struct type_enum *type;
233 /* Last GQuark values read. Keeping a reference on the GQuark array. */
234 GArray *value;
235};
236
237struct type_string {
238 struct type p;
239};
240
241struct declaration_string {
242 struct declaration p;
243 struct type_string *type;
244 char *value; /* freed at declaration_string teardown */
245};
246
247struct type_field {
248 GQuark name;
249 struct type *type;
250};
251
252struct field {
253 GQuark name;
254 struct declaration *declaration;
255};
256
257struct type_struct {
258 struct type p;
259 GHashTable *fields_by_name; /* Tuples (field name, field index) */
260 struct type_scope *scope;
261 GArray *fields; /* Array of type_field */
262};
263
264struct declaration_struct {
265 struct declaration p;
266 struct type_struct *type;
267 struct declaration_scope *scope;
268 GArray *fields; /* Array of struct field */
269};
270
271struct type_variant {
272 struct type p;
273 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
274 struct type_scope *scope;
275 GArray *fields; /* Array of type_field */
276};
277
278struct declaration_variant {
279 struct declaration p;
280 struct type_variant *type;
281 struct declaration_scope *scope;
282 struct declaration *enum_tag;
283 GArray *fields; /* Array of struct field */
284 struct field *current_field; /* Last field read */
285};
286
287struct type_array {
288 struct type p;
289 size_t len;
290 struct type *elem;
291 struct type_scope *scope;
292};
293
294struct declaration_array {
295 struct declaration p;
296 struct type_array *type;
297 struct declaration_scope *scope;
298 struct field current_element; /* struct field */
299};
300
301struct type_sequence {
302 struct type p;
303 struct type_integer *len_type;
304 struct type *elem;
305 struct type_scope *scope;
306};
307
308struct declaration_sequence {
309 struct declaration p;
310 struct type_sequence *type;
311 struct declaration_scope *scope;
312 struct declaration_integer *len;
313 struct field current_element; /* struct field */
314};
315
316int register_type(GQuark type_name, struct type *type,
317 struct type_scope *scope);
318struct type *lookup_type(GQuark type_name, struct type_scope *scope);
319struct type_scope *new_type_scope(struct type_scope *parent_scope);
320void free_type_scope(struct type_scope *scope);
321
322struct declaration *
323 lookup_declaration(GQuark field_name, struct declaration_scope *scope);
324int register_declaration(GQuark field_name, struct declaration *declaration,
325 struct declaration_scope *scope);
326struct declaration_scope *
327 new_declaration_scope(struct declaration_scope *parent_scope);
328void free_declaration_scope(struct declaration_scope *scope);
329
330void type_ref(struct type *type);
331void type_unref(struct type *type);
332
333void declaration_ref(struct declaration *declaration);
334void declaration_unref(struct declaration *declaration);
335
336/* Nameless types can be created by passing a NULL name */
337
338struct type_integer *integer_type_new(const char *name,
339 size_t len, int byte_order,
340 int signedness, size_t alignment);
341
342/*
343 * mantissa_len is the length of the number of bytes represented by the mantissa
344 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
345 */
346struct type_float *float_type_new(const char *name,
347 size_t mantissa_len,
348 size_t exp_len, int byte_order,
349 size_t alignment);
350
351/*
352 * A GQuark can be translated to/from strings with g_quark_from_string() and
353 * g_quark_to_string().
354 */
355
356/*
357 * Returns a GArray of GQuark or NULL.
358 * Caller must release the GArray with g_array_unref().
359 */
360GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
361
362/*
363 * Returns a GArray of GQuark or NULL.
364 * Caller must release the GArray with g_array_unref().
365 */
366GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
367
368/*
369 * Returns a GArray of struct enum_range or NULL.
370 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
371 * release it).
372 */
373GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
374void enum_signed_insert(struct type_enum *enum_type,
375 int64_t start, int64_t end, GQuark q);
376void enum_unsigned_insert(struct type_enum *enum_type,
377 uint64_t start, uint64_t end, GQuark q);
378size_t enum_get_nr_enumerators(struct type_enum *enum_type);
379
380struct type_enum *enum_type_new(const char *name,
381 struct type_integer *integer_type);
382
383struct type_struct *struct_type_new(const char *name,
384 struct type_scope *parent_scope);
385void struct_type_add_field(struct type_struct *struct_type,
386 const char *field_name, struct type *field_type);
387/*
388 * Returns the index of a field within a structure.
389 */
390unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
391 GQuark field_name);
392/*
393 * field returned only valid as long as the field structure is not appended to.
394 */
395struct type_field *
396struct_type_get_field_from_index(struct type_struct *struct_type,
397 unsigned long index);
398struct field *
399struct_get_field_from_index(struct declaration_struct *struct_declaration,
400 unsigned long index);
401
402/*
403 * The tag enumeration is validated to ensure that it contains only mappings
404 * from numeric values to a single tag. Overlapping tag value ranges are
405 * therefore forbidden.
406 */
407struct type_variant *variant_type_new(const char *name,
408 struct type_scope *parent_scope);
409void variant_type_add_field(struct type_variant *variant_type,
410 const char *tag_name, struct type *tag_type);
411struct type_field *
412variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
413/*
414 * Returns 0 on success, -EPERM on error.
415 */
416int variant_declaration_set_tag(struct declaration_variant *variant,
417 struct declaration *enum_tag);
418/*
419 * Returns the field selected by the current tag value.
420 * field returned only valid as long as the variant structure is not appended
421 * to.
422 */
423struct field *
424variant_get_current_field(struct declaration_variant *variant);
425
426/*
427 * elem_type passed as parameter now belongs to the array. No need to free it
428 * explicitly. "len" is the number of elements in the array.
429 */
430struct type_array *array_type_new(const char *name,
431 size_t len, struct type *elem_type,
432 struct type_scope *parent_scope);
433
434/*
435 * int_type and elem_type passed as parameter now belong to the sequence. No
436 * need to free them explicitly.
437 */
438struct type_sequence *sequence_type_new(const char *name,
439 struct type_integer *len_type,
440 struct type *elem_type,
441 struct type_scope *parent_scope);
442
443#endif /* _BABELTRACE_TYPES_H */
This page took 0.024965 seconds and 4 git commands to generate.