lib: make BT_ASSERT_{PRE,POST}() always on; add BT_ASSERT_{PRE,POST}_DEV()
[babeltrace.git] / src / lib / trace-ir / field-class.h
1 #ifndef BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
2 #define BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/clock-class.h>
29 #include <babeltrace2/trace-ir/field-class.h>
30 #include "common/macros.h"
31 #include "lib/object.h"
32 #include <babeltrace2/types.h>
33 #include <stdint.h>
34 #include <glib.h>
35
36 #define _BT_ASSERT_PRE_FC_IS_INT_COND(_fc) \
37 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
38 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
39 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
40 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
41
42 #define _BT_ASSERT_PRE_FC_IS_INT_FMT(_name) \
43 _name " is not an integer field class: %![fc-]+F"
44
45 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc) \
46 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
47 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION)
48
49 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name) \
50 _name " is not an unsigned integer field class: %![fc-]+F"
51
52 #define _BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc) \
53 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
54 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
55
56 #define _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name) \
57 _name " is not an enumeration field class: %![fc-]+F"
58
59 #define _BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc) \
60 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
61 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)
62
63 #define _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name) \
64 _name " is not an array field class: %![fc-]+F"
65
66 #define _BT_ASSERT_PRE_FC_HAS_ID_COND(_fc, _type) \
67 (((const struct bt_field_class *) (_fc))->type == (_type))
68
69 #define _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name) \
70 _name " has the wrong type: expected-type=%s, %![fc-]+F"
71
72 #define BT_ASSERT_PRE_FC_IS_INT(_fc, _name) \
73 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
74 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
75
76 #define BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(_fc, _name) \
77 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
78 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
79
80 #define BT_ASSERT_PRE_FC_IS_ENUM(_fc, _name) \
81 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
82 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
83
84 #define BT_ASSERT_PRE_FC_IS_ARRAY(_fc, _name) \
85 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
86 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
87
88 #define BT_ASSERT_PRE_FC_HAS_ID(_fc, _type, _name) \
89 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
90 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
91 bt_common_field_class_type_string(_type), (_fc))
92
93 #define BT_ASSERT_PRE_DEV_FC_IS_INT(_fc, _name) \
94 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
95 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
96
97 #define BT_ASSERT_PRE_DEV_FC_IS_UNSIGNED_INT(_fc, _name) \
98 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
99 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
100
101 #define BT_ASSERT_PRE_DEV_FC_IS_ENUM(_fc, _name) \
102 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
103 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
104
105 #define BT_ASSERT_PRE_DEV_FC_IS_ARRAY(_fc, _name) \
106 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
107 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
108
109 #define BT_ASSERT_PRE_DEV_FC_HAS_ID(_fc, _type, _name) \
110 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
111 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
112 bt_common_field_class_type_string(_type), (_fc))
113
114 #define BT_ASSERT_PRE_DEV_FC_HOT(_fc, _name) \
115 BT_ASSERT_PRE_DEV_HOT((const struct bt_field_class *) (_fc), \
116 (_name), ": %!+F", (_fc))
117
118 #define BT_FIELD_CLASS_NAMED_FC_AT_INDEX(_fc, _index) \
119 (&g_array_index(((struct bt_field_class_named_field_class_container *) (_fc))->named_fcs, \
120 struct bt_named_field_class, (_index)))
121
122 #define BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(_fc, _index) \
123 (&g_array_index(((struct bt_field_class_enumeration *) (_fc))->mappings, \
124 struct bt_field_class_enumeration_mapping, (_index)))
125
126 #define BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
127 (&g_array_index((_mapping)->ranges, \
128 struct bt_field_class_enumeration_mapping_range, (_index)))
129
130 struct bt_field;
131 struct bt_field_class;
132
133 struct bt_field_class {
134 struct bt_object base;
135 enum bt_field_class_type type;
136 bool frozen;
137
138 /*
139 * This flag indicates whether or not this field class is part
140 * of a trace class.
141 */
142 bool part_of_trace_class;
143 };
144
145 struct bt_field_class_integer {
146 struct bt_field_class common;
147
148 /*
149 * Value range of fields built from this integer field class:
150 * this is an equivalent integer size in bits. More formally,
151 * `range` is `n` in:
152 *
153 * Unsigned range: [0, 2^n - 1]
154 * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
155 */
156 uint64_t range;
157
158 enum bt_field_class_integer_preferred_display_base base;
159 };
160
161 struct bt_field_class_enumeration_mapping_range {
162 union {
163 uint64_t u;
164 int64_t i;
165 } lower;
166
167 union {
168 uint64_t u;
169 int64_t i;
170 } upper;
171 };
172
173 struct bt_field_class_enumeration_mapping {
174 GString *label;
175
176 /* Array of `struct bt_field_class_enumeration_mapping_range` */
177 GArray *ranges;
178 };
179
180 struct bt_field_class_unsigned_enumeration_mapping;
181 struct bt_field_class_signed_enumeration_mapping;
182
183 struct bt_field_class_enumeration {
184 struct bt_field_class_integer common;
185
186 /* Array of `struct bt_field_class_enumeration_mapping *` */
187 GArray *mappings;
188
189 /*
190 * This is an array of `const char *` which acts as a temporary
191 * (potentially growing) buffer for
192 * bt_field_class_unsigned_enumeration_get_mapping_labels_for_value()
193 * and
194 * bt_field_class_signed_enumeration_get_mapping_labels_for_value().
195 *
196 * The actual strings are owned by the mappings above.
197 */
198 GPtrArray *label_buf;
199 };
200
201 struct bt_field_class_real {
202 struct bt_field_class common;
203 bool is_single_precision;
204 };
205
206 struct bt_field_class_string {
207 struct bt_field_class common;
208 };
209
210 /* A named field class is a (name, field class) pair */
211 struct bt_named_field_class {
212 GString *name;
213
214 /* Owned by this */
215 struct bt_field_class *fc;
216
217 bool frozen;
218 };
219
220 struct bt_field_class_structure_member;
221 struct bt_field_class_variant_option;
222
223 /*
224 * This is the base field class for a container of named field classes.
225 * Structure and variant field classes inherit this.
226 */
227 struct bt_field_class_named_field_class_container {
228 struct bt_field_class common;
229
230 /*
231 * Key: `const char *`, not owned by this (owned by named field
232 * type objects contained in `named_fcs` below).
233 */
234 GHashTable *name_to_index;
235
236 /* Array of `struct bt_named_field_class` */
237 GArray *named_fcs;
238 };
239
240 struct bt_field_class_structure {
241 struct bt_field_class_named_field_class_container common;
242 };
243
244 struct bt_field_class_array {
245 struct bt_field_class common;
246
247 /* Owned by this */
248 struct bt_field_class *element_fc;
249 };
250
251 struct bt_field_class_static_array {
252 struct bt_field_class_array common;
253 uint64_t length;
254 };
255
256 struct bt_field_class_dynamic_array {
257 struct bt_field_class_array common;
258
259 /* Weak: never dereferenced, only use to find it elsewhere */
260 struct bt_field_class *length_fc;
261
262 /* Owned by this */
263 struct bt_field_path *length_field_path;
264 };
265
266 struct bt_field_class_variant {
267 struct bt_field_class_named_field_class_container common;
268
269 /* Weak: never dereferenced, only use to find it elsewhere */
270 struct bt_field_class *selector_fc;
271
272 /* Owned by this */
273 struct bt_field_path *selector_field_path;
274 };
275
276 static inline
277 bool bt_field_class_has_known_type(const struct bt_field_class *fc)
278 {
279 return fc->type >= BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER &&
280 fc->type <= BT_FIELD_CLASS_TYPE_VARIANT;
281 }
282
283 BT_HIDDEN
284 void _bt_field_class_freeze(const struct bt_field_class *field_class);
285
286 #ifdef BT_DEV_MODE
287 # define bt_field_class_freeze _bt_field_class_freeze
288 #else
289 # define bt_field_class_freeze(_fc) ((void) _fc)
290 #endif
291
292 BT_HIDDEN
293 void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc);
294
295 #ifdef BT_DEV_MODE
296 # define bt_named_field_class_freeze _bt_named_field_class_freeze
297 #else
298 # define bt_named_field_class_freeze(_named_fc) ((void) _named_fc)
299 #endif
300
301 /*
302 * This function recursively marks `field_class` and its children as
303 * being part of a trace. This is used to validate that all field classes
304 * are used at a single location within trace objects even if they are
305 * shared objects for other purposes.
306 */
307 BT_HIDDEN
308 void bt_field_class_make_part_of_trace_class(
309 const struct bt_field_class *field_class);
310
311 #endif /* BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H */
This page took 0.036085 seconds and 4 git commands to generate.