ctf metadata field is set bitmasks
[babeltrace.git] / include / babeltrace / types.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_TYPES_H
2#define _BABELTRACE_TYPES_H
3
4/*
5 * BabelTrace
6 *
fc93b2bd 7 * Type Header
d79865b9 8 *
c054553d 9 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d79865b9 10 *
ccd7e1c8
MD
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:
d79865b9 17 *
ccd7e1c8
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
d79865b9
MD
20 */
21
bed864a7 22#include <babeltrace/align.h>
d65d8abb 23#include <babeltrace/list.h>
4c8bfb7e
MD
24#include <stdbool.h>
25#include <stdint.h>
26#include <limits.h>
bed864a7 27#include <string.h>
4c8bfb7e 28#include <glib.h>
8eab883c 29#include <assert.h>
bed864a7 30
11796b96
MD
31/* Preallocate this many fields for structures */
32#define DEFAULT_NR_STRUCT_FIELDS 8
33
bed864a7
MD
34/*
35 * Always update stream_pos with move_pos and init_pos.
36 */
37struct stream_pos {
47e0f2e2 38 char *base; /* Base address */
bed864a7
MD
39 size_t offset; /* Offset from base, in bits */
40 int dummy; /* Dummy position, for length calculation */
41};
42
43static inline
47e0f2e2 44void init_pos(struct stream_pos *pos, char *base)
bed864a7
MD
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
47e0f2e2 80char *get_pos_addr(struct stream_pos *pos)
bed864a7
MD
81{
82 /* Only makes sense to get the address after aligning on CHAR_BIT */
4c8bfb7e 83 assert(!(pos->offset % CHAR_BIT));
bed864a7
MD
84 return pos->base + (pos->offset / CHAR_BIT);
85}
fc93b2bd 86
4c8bfb7e 87struct format;
e19c3d69 88struct declaration;
4c8bfb7e 89
64893f33
MD
90/* type scope */
91struct type_scope {
e19c3d69
MD
92 /* Hash table mapping type name GQuark to struct type */
93 GHashTable *types;
64893f33
MD
94 struct type_scope *parent_scope;
95};
96
97/* declaration scope */
98struct declaration_scope {
ac88af75
MD
99 /* Hash table mapping field name GQuark to struct declaration */
100 GHashTable *declarations;
6b71274a
MD
101 struct declaration_scope *parent_scope;
102};
103
e19c3d69 104struct type {
fc93b2bd
MD
105 GQuark name; /* type name */
106 size_t alignment; /* type alignment, in bits */
e19c3d69 107 int ref; /* number of references to the type */
c054553d
MD
108 /*
109 * type_free called with type ref is decremented to 0.
110 */
111 void (*type_free)(struct type *type);
e19c3d69
MD
112 struct declaration *
113 (*declaration_new)(struct type *type,
114 struct declaration_scope *parent_scope);
c054553d 115 /*
e19c3d69
MD
116 * declaration_free called with declaration ref is decremented to 0.
117 */
118 void (*declaration_free)(struct declaration *declaration);
119 /*
120 * Declaration copy function. Knows how to find the child declaration
121 * from the parent declaration.
fc93b2bd 122 */
4c8bfb7e
MD
123 void (*copy)(struct stream_pos *dest, const struct format *fdest,
124 struct stream_pos *src, const struct format *fsrc,
e19c3d69 125 struct declaration *declaration);
c054553d
MD
126};
127
e19c3d69
MD
128struct declaration {
129 struct type *type;
130 int ref; /* number of references to the declaration */
fc93b2bd
MD
131};
132
bed864a7
MD
133/*
134 * Because we address in bits, bitfields end up being exactly the same as
135 * integers, except that their read/write functions must be able to deal with
136 * read/write non aligned on CHAR_BIT.
137 */
e19c3d69
MD
138struct type_integer {
139 struct type p;
7fe00194
MD
140 size_t len; /* length, in bits. */
141 int byte_order; /* byte order */
142 int signedness;
fc93b2bd
MD
143};
144
e19c3d69
MD
145struct declaration_integer {
146 struct declaration p;
147 struct type_integer *type;
c054553d
MD
148 /* Last values read */
149 union {
150 uint64_t _unsigned;
151 int64_t _signed;
152 } value;
153};
154
e19c3d69
MD
155struct type_float {
156 struct type p;
157 struct type_integer *sign;
158 struct type_integer *mantissa;
159 struct type_integer *exp;
fc93b2bd 160 int byte_order;
0a46062b 161 /* TODO: we might want to express more info about NaN, +inf and -inf */
fc93b2bd
MD
162};
163
e19c3d69
MD
164struct declaration_float {
165 struct declaration p;
166 struct type_float *type;
c054553d
MD
167 /* Last values read */
168 long double value;
169};
170
d65d8abb
MD
171/*
172 * enum_val_equal assumes that signed and unsigned memory layout overlap.
173 */
174struct enum_range {
175 union {
176 int64_t _signed;
177 uint64_t _unsigned;
178 } start; /* lowest range value */
179 union {
180 int64_t _signed;
181 uint64_t _unsigned;
182 } end; /* highest range value */
183};
184
185struct enum_range_to_quark {
186 struct cds_list_head node;
187 struct enum_range range;
188 GQuark quark;
189};
190
191/*
192 * We optimize the common case (range of size 1: single value) by creating a
193 * hash table mapping values to quark sets. We then lookup the ranges to
194 * complete the quark set.
195 *
196 * TODO: The proper structure to hold the range to quark set mapping would be an
197 * interval tree, with O(n) size, O(n*log(n)) build time and O(log(n)) query
198 * time. Using a simple O(n) list search for now for implementation speed and
199 * given that we can expect to have a _relatively_ small number of enumeration
200 * ranges. This might become untrue if we are fed with symbol tables often
201 * required to lookup function names from instruction pointer value.
202 */
448d3cc7 203struct enum_table {
d65d8abb
MD
204 GHashTable *value_to_quark_set; /* (value, GQuark GArray) */
205 struct cds_list_head range_to_quark; /* (range, GQuark) */
206 GHashTable *quark_to_range_set; /* (GQuark, range GArray) */
448d3cc7
MD
207};
208
e19c3d69
MD
209struct type_enum {
210 struct type p;
211 struct type_integer *integer_type;
448d3cc7 212 struct enum_table table;
fc93b2bd
MD
213};
214
e19c3d69
MD
215struct declaration_enum {
216 struct declaration p;
217 struct declaration_integer *integer;
218 struct type_enum *type;
c054553d
MD
219 /* Last GQuark values read. Keeping a reference on the GQuark array. */
220 GArray *value;
221};
222
c054553d
MD
223struct type_string {
224 struct type p;
c054553d
MD
225};
226
e19c3d69
MD
227struct declaration_string {
228 struct declaration p;
229 struct type_string *type;
230 char *value; /* freed at declaration_string teardown */
11796b96
MD
231};
232
e19c3d69
MD
233struct type_field {
234 GQuark name;
c054553d
MD
235 struct type *type;
236};
237
e19c3d69 238struct field {
ac88af75 239 GQuark name;
6ee5115e 240 struct declaration *declaration;
c054553d
MD
241};
242
243struct type_struct {
244 struct type p;
e19c3d69 245 GHashTable *fields_by_name; /* Tuples (field name, field index) */
64893f33 246 struct type_scope *scope;
e19c3d69
MD
247 GArray *fields; /* Array of type_field */
248};
249
250struct declaration_struct {
251 struct declaration p;
252 struct type_struct *type;
c054553d
MD
253 struct declaration_scope *scope;
254 GArray *fields; /* Array of struct field */
255};
256
e19c3d69
MD
257struct type_variant {
258 struct type p;
c054553d 259 GHashTable *fields_by_tag; /* Tuples (field tag, field index) */
64893f33 260 struct type_scope *scope;
e19c3d69 261 GArray *fields; /* Array of type_field */
c054553d
MD
262};
263
e19c3d69
MD
264struct declaration_variant {
265 struct declaration p;
266 struct type_variant *type;
c054553d 267 struct declaration_scope *scope;
6ee5115e 268 struct declaration *enum_tag;
c054553d
MD
269 GArray *fields; /* Array of struct field */
270 struct field *current_field; /* Last field read */
11796b96
MD
271};
272
e19c3d69
MD
273struct type_array {
274 struct type p;
11796b96 275 size_t len;
e19c3d69 276 struct type *elem;
64893f33 277 struct type_scope *scope;
11796b96
MD
278};
279
e19c3d69
MD
280struct declaration_array {
281 struct declaration p;
282 struct type_array *type;
c054553d
MD
283 struct declaration_scope *scope;
284 struct field current_element; /* struct field */
285};
286
c054553d
MD
287struct type_sequence {
288 struct type p;
e19c3d69
MD
289 struct type_integer *len_type;
290 struct type *elem;
64893f33 291 struct type_scope *scope;
e19c3d69
MD
292};
293
294struct declaration_sequence {
295 struct declaration p;
296 struct type_sequence *type;
c054553d 297 struct declaration_scope *scope;
e19c3d69 298 struct declaration_integer *len;
c054553d
MD
299 struct field current_element; /* struct field */
300};
301
64893f33
MD
302int register_type(GQuark type_name, struct type *type,
303 struct type_scope *scope);
304struct type *lookup_type(GQuark type_name, struct type_scope *scope);
305struct type_scope *new_type_scope(struct type_scope *parent_scope);
306void free_type_scope(struct type_scope *scope);
c054553d 307
ac88af75
MD
308struct declaration *
309 lookup_declaration(GQuark field_name, struct declaration_scope *scope);
310int register_declaration(GQuark field_name, struct declaration *declaration,
311 struct declaration_scope *scope);
c054553d 312struct declaration_scope *
e19c3d69 313 new_declaration_scope(struct declaration_scope *parent_scope);
c054553d 314void free_declaration_scope(struct declaration_scope *scope);
4c8bfb7e 315
64893f33
MD
316void type_ref(struct type *type);
317void type_unref(struct type *type);
318
e19c3d69
MD
319void declaration_ref(struct declaration *declaration);
320void declaration_unref(struct declaration *declaration);
698f0fe4 321
0a46062b
MD
322/* Nameless types can be created by passing a NULL name */
323
e19c3d69
MD
324struct type_integer *integer_type_new(const char *name,
325 size_t len, int byte_order,
326 int signedness, size_t alignment);
0a46062b 327
11d43b90
MD
328/*
329 * mantissa_len is the length of the number of bytes represented by the mantissa
330 * (e.g. result of DBL_MANT_DIG). It includes the leading 1.
331 */
e19c3d69
MD
332struct type_float *float_type_new(const char *name,
333 size_t mantissa_len,
334 size_t exp_len, int byte_order,
335 size_t alignment);
0a46062b 336
448d3cc7
MD
337/*
338 * A GQuark can be translated to/from strings with g_quark_from_string() and
339 * g_quark_to_string().
340 */
47e0f2e2
MD
341
342/*
343 * Returns a GArray of GQuark or NULL.
344 * Caller must release the GArray with g_array_unref().
345 */
e19c3d69 346GArray *enum_uint_to_quark_set(const struct type_enum *enum_type, uint64_t v);
d65d8abb
MD
347
348/*
47e0f2e2 349 * Returns a GArray of GQuark or NULL.
d65d8abb
MD
350 * Caller must release the GArray with g_array_unref().
351 */
e19c3d69 352GArray *enum_int_to_quark_set(const struct type_enum *enum_type, uint64_t v);
47e0f2e2
MD
353
354/*
355 * Returns a GArray of struct enum_range or NULL.
fdacfb73
MD
356 * Callers do _not_ own the returned GArray (and therefore _don't_ need to
357 * release it).
47e0f2e2 358 */
e19c3d69
MD
359GArray *enum_quark_to_range_set(const struct type_enum *enum_type, GQuark q);
360void enum_signed_insert(struct type_enum *enum_type,
d65d8abb 361 int64_t start, int64_t end, GQuark q);
e19c3d69 362void enum_unsigned_insert(struct type_enum *enum_type,
d65d8abb 363 uint64_t start, uint64_t end, GQuark q);
e19c3d69 364size_t enum_get_nr_enumerators(struct type_enum *enum_type);
448d3cc7 365
e19c3d69
MD
366struct type_enum *enum_type_new(const char *name,
367 struct type_integer *integer_type);
448d3cc7 368
64893f33
MD
369struct type_struct *struct_type_new(const char *name,
370 struct type_scope *parent_scope);
e19c3d69
MD
371void struct_type_add_field(struct type_struct *struct_type,
372 const char *field_name, struct type *field_type);
11796b96
MD
373/*
374 * Returns the index of a field within a structure.
375 */
e19c3d69
MD
376unsigned long struct_type_lookup_field_index(struct type_struct *struct_type,
377 GQuark field_name);
11796b96
MD
378/*
379 * field returned only valid as long as the field structure is not appended to.
380 */
e19c3d69
MD
381struct type_field *
382struct_type_get_field_from_index(struct type_struct *struct_type,
11796b96 383 unsigned long index);
e19c3d69
MD
384struct field *
385struct_get_field_from_index(struct declaration_struct *struct_declaration,
386 unsigned long index);
11796b96 387
c054553d
MD
388/*
389 * The tag enumeration is validated to ensure that it contains only mappings
390 * from numeric values to a single tag. Overlapping tag value ranges are
391 * therefore forbidden.
392 */
64893f33
MD
393struct type_variant *variant_type_new(const char *name,
394 struct type_scope *parent_scope);
e19c3d69
MD
395void variant_type_add_field(struct type_variant *variant_type,
396 const char *tag_name, struct type *tag_type);
397struct type_field *
398variant_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag);
c054553d
MD
399/*
400 * Returns 0 on success, -EPERM on error.
401 */
e19c3d69
MD
402int variant_declaration_set_tag(struct declaration_variant *variant,
403 struct declaration *enum_tag);
c054553d
MD
404/*
405 * Returns the field selected by the current tag value.
406 * field returned only valid as long as the variant structure is not appended
407 * to.
408 */
409struct field *
e19c3d69 410variant_get_current_field(struct declaration_variant *variant);
c054553d 411
d06d03db 412/*
e19c3d69 413 * elem_type passed as parameter now belongs to the array. No need to free it
c054553d 414 * explicitly. "len" is the number of elements in the array.
d06d03db 415 */
e19c3d69 416struct type_array *array_type_new(const char *name,
64893f33
MD
417 size_t len, struct type *elem_type,
418 struct type_scope *parent_scope);
11796b96 419
d06d03db 420/*
e19c3d69 421 * int_type and elem_type passed as parameter now belong to the sequence. No
c054553d 422 * need to free them explicitly.
d06d03db 423 */
e19c3d69
MD
424struct type_sequence *sequence_type_new(const char *name,
425 struct type_integer *len_type,
64893f33
MD
426 struct type *elem_type,
427 struct type_scope *parent_scope);
11796b96 428
d79865b9 429#endif /* _BABELTRACE_TYPES_H */
This page took 0.063537 seconds and 4 git commands to generate.