af9254f29de5e3d7af2c2869a7745691dd18665c
[babeltrace.git] / src / lib / trace-ir / field-class.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #ifndef BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
9 #define BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
10
11 #include "lib/assert-pre.h"
12 #include <babeltrace2/trace-ir/clock-class.h>
13 #include <babeltrace2/trace-ir/field-class.h>
14 #include "common/macros.h"
15 #include "common/common.h"
16 #include "lib/object.h"
17 #include <babeltrace2/types.h>
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <glib.h>
21
22 #define _BT_ASSERT_PRE_FC_IS_INT_COND(_fc) \
23 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
24 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
25 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
26 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
27
28 #define _BT_ASSERT_PRE_FC_IS_INT_FMT(_name) \
29 _name " is not an integer field class: %![fc-]+F"
30
31 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc) \
32 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
33 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION)
34
35 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name) \
36 _name " is not an unsigned integer field class: %![fc-]+F"
37
38
39 #define _BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc) \
40 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
41 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
42
43 #define _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name) \
44 _name " is not a signed integer field class: %![fc-]+F"
45
46 #define _BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc) \
47 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
48 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
49
50 #define _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name) \
51 _name " is not an enumeration field class: %![fc-]+F"
52
53 #define _BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc) \
54 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
55 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
56 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD)
57
58 #define _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name) \
59 _name " is not an array field class: %![fc-]+F"
60
61 #define _BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc) \
62 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD || \
63 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
64 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
65 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
66
67 #define _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name) \
68 _name " is not an option field class: %![fc-]+F"
69
70 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc) \
71 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
72 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
73 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
74
75 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name) \
76 _name " is not an option field class with a selector: %![fc-]+F"
77
78 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc) \
79 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
80 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
81
82 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name) \
83 _name " is not an option field class with an integer selector: %![fc-]+F"
84
85 #define _BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc) \
86 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD || \
87 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
88 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
89
90 #define _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name) \
91 _name " is not a variant field class: %![fc-]+F"
92
93 #define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc) \
94 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
95 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
96
97 #define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name) \
98 _name " is not a variant field class with a selector: %![fc-]+F"
99
100 #define _BT_ASSERT_PRE_FC_HAS_ID_COND(_fc, _type) \
101 (((const struct bt_field_class *) (_fc))->type == (_type))
102
103 #define _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name) \
104 _name " has the wrong type: expected-type=%s, %![fc-]+F"
105
106 #define BT_ASSERT_PRE_FC_IS_INT(_fc, _name) \
107 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
108 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
109
110 #define BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(_fc, _name) \
111 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
112 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
113
114 #define BT_ASSERT_PRE_FC_IS_SIGNED_INT(_fc, _name) \
115 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
116 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
117
118 #define BT_ASSERT_PRE_FC_IS_ENUM(_fc, _name) \
119 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
120 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
121
122 #define BT_ASSERT_PRE_FC_IS_ARRAY(_fc, _name) \
123 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
124 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
125
126 #define BT_ASSERT_PRE_FC_IS_OPTION(_fc, _name) \
127 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc), \
128 _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name), (_fc))
129
130 #define BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(_fc, _name) \
131 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc), \
132 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name), (_fc))
133
134 #define BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(_fc, _name) \
135 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc), \
136 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name), (_fc))
137
138 #define BT_ASSERT_PRE_FC_IS_VARIANT(_fc, _name) \
139 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
140 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
141
142 #define BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
143 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
144 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
145
146 #define BT_ASSERT_PRE_FC_HAS_ID(_fc, _type, _name) \
147 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
148 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
149 bt_common_field_class_type_string(_type), (_fc))
150
151 #define BT_ASSERT_PRE_DEV_FC_IS_INT(_fc, _name) \
152 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
153 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
154
155 #define BT_ASSERT_PRE_DEV_FC_IS_UNSIGNED_INT(_fc, _name) \
156 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
157 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
158
159 #define BT_ASSERT_PRE_DEV_FC_IS_SIGNED_INT(_fc, _name) \
160 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
161 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
162
163 #define BT_ASSERT_PRE_DEV_FC_IS_ENUM(_fc, _name) \
164 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
165 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
166
167 #define BT_ASSERT_PRE_DEV_FC_IS_ARRAY(_fc, _name) \
168 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
169 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
170
171 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION(_fc, _name) \
172 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc), \
173 _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name), (_fc))
174
175 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION_WITH_SEL(_fc, _name) \
176 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc), \
177 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name), (_fc))
178
179 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION_WITH_INT_SEL(_fc, _name) \
180 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc), \
181 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name), (_fc))
182
183 #define BT_ASSERT_PRE_DEV_FC_IS_VARIANT(_fc, _name) \
184 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
185 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
186
187 #define BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
188 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
189 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
190
191 #define BT_ASSERT_PRE_DEV_FC_HAS_ID(_fc, _type, _name) \
192 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
193 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
194 bt_common_field_class_type_string(_type), (_fc))
195
196 #define BT_ASSERT_PRE_DEV_FC_HOT(_fc, _name) \
197 BT_ASSERT_PRE_DEV_HOT((const struct bt_field_class *) (_fc), \
198 (_name), ": %!+F", (_fc))
199
200 #define BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(_fc, _index) \
201 (&g_array_index(((struct bt_field_class_enumeration *) (_fc))->mappings, \
202 struct bt_field_class_enumeration_mapping, (_index)))
203
204 #define BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
205 (&g_array_index((_mapping)->ranges, \
206 struct bt_field_class_enumeration_mapping_range, (_index)))
207
208 struct bt_field;
209 struct bt_field_class;
210
211 struct bt_field_class {
212 struct bt_object base;
213 enum bt_field_class_type type;
214 bool frozen;
215
216 /* Owned by this */
217 struct bt_value *user_attributes;
218
219 /*
220 * This flag indicates whether or not this field class is part
221 * of a trace class.
222 */
223 bool part_of_trace_class;
224 };
225
226 struct bt_field_class_bool {
227 struct bt_field_class common;
228 };
229
230 struct bt_field_class_bit_array {
231 struct bt_field_class common;
232 uint64_t length;
233 };
234
235 struct bt_field_class_integer {
236 struct bt_field_class common;
237
238 /*
239 * Value range of fields built from this integer field class:
240 * this is an equivalent integer size in bits. More formally,
241 * `range` is `n` in:
242 *
243 * Unsigned range: [0, 2^n - 1]
244 * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
245 */
246 uint64_t range;
247
248 enum bt_field_class_integer_preferred_display_base base;
249 };
250
251 struct bt_field_class_enumeration_mapping {
252 GString *label;
253
254 /* Owner by this */
255 const struct bt_integer_range_set *range_set;
256 };
257
258 struct bt_field_class_enumeration_unsigned_mapping;
259 struct bt_field_class_enumeration_signed_mapping;
260
261 struct bt_field_class_enumeration {
262 struct bt_field_class_integer common;
263
264 /* Array of `struct bt_field_class_enumeration_mapping *` */
265 GArray *mappings;
266
267 /*
268 * This is an array of `const char *` which acts as a temporary
269 * (potentially growing) buffer for
270 * bt_field_class_enumeration_unsigned_get_mapping_labels_for_value()
271 * and
272 * bt_field_class_enumeration_signed_get_mapping_labels_for_value().
273 *
274 * The actual strings are owned by the mappings above.
275 */
276 GPtrArray *label_buf;
277 };
278
279 struct bt_field_class_real {
280 struct bt_field_class common;
281 };
282
283 struct bt_field_class_string {
284 struct bt_field_class common;
285 };
286
287 /* A named field class is a (name, field class) pair */
288 struct bt_named_field_class {
289 GString *name;
290
291 /* Owned by this */
292 struct bt_value *user_attributes;
293
294 /* Owned by this */
295 struct bt_field_class *fc;
296
297 bool frozen;
298 };
299
300 struct bt_field_class_structure_member;
301 struct bt_field_class_variant_option;
302 struct bt_field_class_variant_with_selector_field_integer_unsigned_option;
303 struct bt_field_class_variant_with_selector_field_integer_signed_option;
304
305 struct bt_field_class_named_field_class_container {
306 struct bt_field_class common;
307
308 /*
309 * Key: `const char *`, not owned by this (owned by named field
310 * class objects contained in `named_fcs` below).
311 */
312 GHashTable *name_to_index;
313
314 /* Array of `struct bt_named_field_class *` */
315 GPtrArray *named_fcs;
316 };
317
318 struct bt_field_class_structure {
319 struct bt_field_class_named_field_class_container common;
320 };
321
322 struct bt_field_class_array {
323 struct bt_field_class common;
324
325 /* Owned by this */
326 struct bt_field_class *element_fc;
327 };
328
329 struct bt_field_class_array_static {
330 struct bt_field_class_array common;
331 uint64_t length;
332 };
333
334 struct bt_field_class_array_dynamic {
335 struct bt_field_class_array common;
336
337 /* Owned by this */
338 struct bt_field_class *length_fc;
339
340 /* Owned by this */
341 struct bt_field_path *length_field_path;
342 };
343
344 struct bt_field_class_option {
345 struct bt_field_class common;
346
347 /* Owned by this */
348 struct bt_field_class *content_fc;
349 };
350
351 struct bt_field_class_option_with_selector_field {
352 struct bt_field_class_option common;
353
354 /* Owned by this */
355 struct bt_field_class *selector_fc;
356
357 /* Owned by this */
358 struct bt_field_path *selector_field_path;
359 };
360
361 struct bt_field_class_option_with_selector_field_bool {
362 struct bt_field_class_option_with_selector_field common;
363
364 /* Owned by this */
365 bool sel_is_reversed;
366 };
367
368 struct bt_field_class_option_with_selector_field_integer {
369 struct bt_field_class_option_with_selector_field common;
370
371 /* Owned by this */
372 const struct bt_integer_range_set *range_set;
373 };
374
375 /* Variant FC (with selector) option: named field class + range set */
376 struct bt_field_class_variant_with_selector_field_option {
377 struct bt_named_field_class common;
378
379 /* Owned by this */
380 const struct bt_integer_range_set *range_set;
381 };
382
383 struct bt_field_class_variant {
384 /*
385 * Depending on the variant field class type, the contained
386 * named field classes are of type
387 * `struct bt_named_field_class *` if the variant field class
388 * doesn't have a selector, or
389 * `struct bt_field_class_variant_with_selector_field_option *`
390 * if it has.
391 */
392 struct bt_field_class_named_field_class_container common;
393 };
394
395 struct bt_field_class_variant_with_selector_field {
396 struct bt_field_class_variant common;
397
398 /*
399 * Owned by this, but never dereferenced: only use to find it
400 * elsewhere.
401 */
402 const struct bt_field_class *selector_fc;
403
404 /* Owned by this */
405 struct bt_field_path *selector_field_path;
406 };
407
408 BT_HIDDEN
409 void _bt_field_class_freeze(const struct bt_field_class *field_class);
410
411 #ifdef BT_DEV_MODE
412 # define bt_field_class_freeze _bt_field_class_freeze
413 #else
414 # define bt_field_class_freeze(_fc) ((void) _fc)
415 #endif
416
417 BT_HIDDEN
418 void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc);
419
420 #ifdef BT_DEV_MODE
421 # define bt_named_field_class_freeze _bt_named_field_class_freeze
422 #else
423 # define bt_named_field_class_freeze(_named_fc) ((void) _named_fc)
424 #endif
425
426 /*
427 * This function recursively marks `field_class` and its children as
428 * being part of a trace. This is used to validate that all field classes
429 * are used at a single location within trace objects even if they are
430 * shared objects for other purposes.
431 */
432 BT_HIDDEN
433 void bt_field_class_make_part_of_trace_class(
434 const struct bt_field_class *field_class);
435
436 #endif /* BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H */
This page took 0.038479 seconds and 4 git commands to generate.