77268c5f2bad990f2465d0850caa29b087a0d1cd
[babeltrace.git] / include / babeltrace / trace-ir / field-class-internal.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 <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/clock-class.h>
29 #include <babeltrace/trace-ir/field-class.h>
30 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/types.h>
33 #include <stdint.h>
34 #include <glib.h>
35
36 #define BT_ASSERT_PRE_FC_IS_INT(_fc, _name) \
37 BT_ASSERT_PRE( \
38 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
39 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
40 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
41 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, \
42 _name " is not an integer field class: %![fc-]+F", (_fc))
43
44 #define BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(_fc, _name) \
45 BT_ASSERT_PRE( \
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 _name " is not an unsigned integer field class: %![fc-]+F", (_fc))
49
50 #define BT_ASSERT_PRE_FC_IS_ENUM(_fc, _name) \
51 BT_ASSERT_PRE( \
52 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
53 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, \
54 _name " is not an enumeration field class: %![fc-]+F", (_fc))
55
56 #define BT_ASSERT_PRE_FC_IS_ARRAY(_fc, _name) \
57 BT_ASSERT_PRE( \
58 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
59 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, \
60 _name " is not an array field class: %![fc-]+F", (_fc))
61
62 #define BT_ASSERT_PRE_FC_HAS_ID(_fc, _type, _name) \
63 BT_ASSERT_PRE(((const struct bt_field_class *) (_fc))->type == (_type), \
64 _name " has the wrong type: expected-type=%s, " \
65 "%![fc-]+F", bt_common_field_class_type_string(_type), (_fc))
66
67 #define BT_ASSERT_PRE_FC_HOT(_fc, _name) \
68 BT_ASSERT_PRE_HOT((const struct bt_field_class *) (_fc), \
69 (_name), ": %!+F", (_fc))
70
71 #define BT_FIELD_CLASS_NAMED_FC_AT_INDEX(_fc, _index) \
72 (&g_array_index(((struct bt_field_class_named_field_class_container *) (_fc))->named_fcs, \
73 struct bt_named_field_class, (_index)))
74
75 #define BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(_fc, _index) \
76 (&g_array_index(((struct bt_field_class_enumeration *) (_fc))->mappings, \
77 struct bt_field_class_enumeration_mapping, (_index)))
78
79 #define BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
80 (&g_array_index((_mapping)->ranges, \
81 struct bt_field_class_enumeration_mapping_range, (_index)))
82
83 struct bt_field;
84 struct bt_field_class;
85
86 struct bt_field_class {
87 struct bt_object base;
88 enum bt_field_class_type type;
89 bool frozen;
90
91 /*
92 * Only used in developer mode, this flag indicates whether or
93 * not this field class is part of a trace class.
94 */
95 bool part_of_trace_class;
96 };
97
98 struct bt_field_class_integer {
99 struct bt_field_class common;
100
101 /*
102 * Value range of fields built from this integer field class:
103 * this is an equivalent integer size in bits. More formally,
104 * `range` is `n` in:
105 *
106 * Unsigned range: [0, 2^n - 1]
107 * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
108 */
109 uint64_t range;
110
111 enum bt_field_class_integer_preferred_display_base base;
112 };
113
114 struct bt_field_class_enumeration_mapping_range {
115 union {
116 uint64_t u;
117 int64_t i;
118 } lower;
119
120 union {
121 uint64_t u;
122 int64_t i;
123 } upper;
124 };
125
126 struct bt_field_class_enumeration_mapping {
127 GString *label;
128
129 /* Array of `struct bt_field_class_enumeration_mapping_range` */
130 GArray *ranges;
131 };
132
133 struct bt_field_class_enumeration {
134 struct bt_field_class_integer common;
135
136 /* Array of `struct bt_field_class_enumeration_mapping *` */
137 GArray *mappings;
138
139 /*
140 * This is an array of `const char *` which acts as a temporary
141 * (potentially growing) buffer for
142 * bt_field_class_unsigned_enumeration_get_mapping_labels_by_value()
143 * and
144 * bt_field_class_signed_enumeration_get_mapping_labels_by_value().
145 *
146 * The actual strings are owned by the mappings above.
147 */
148 GPtrArray *label_buf;
149 };
150
151 struct bt_field_class_real {
152 struct bt_field_class common;
153 bool is_single_precision;
154 };
155
156 struct bt_field_class_string {
157 struct bt_field_class common;
158 };
159
160 /* A named field class is a (name, field class) pair */
161 struct bt_named_field_class {
162 GString *name;
163
164 /* Owned by this */
165 struct bt_field_class *fc;
166
167 bool frozen;
168 };
169
170 struct bt_field_class_structure_member;
171 struct bt_field_class_variant_option;
172
173 /*
174 * This is the base field class for a container of named field classes.
175 * Structure and variant field classes inherit this.
176 */
177 struct bt_field_class_named_field_class_container {
178 struct bt_field_class common;
179
180 /*
181 * Key: `const char *`, not owned by this (owned by named field
182 * type objects contained in `named_fcs` below).
183 */
184 GHashTable *name_to_index;
185
186 /* Array of `struct bt_named_field_class` */
187 GArray *named_fcs;
188 };
189
190 struct bt_field_class_structure {
191 struct bt_field_class_named_field_class_container common;
192 };
193
194 struct bt_field_class_array {
195 struct bt_field_class common;
196
197 /* Owned by this */
198 struct bt_field_class *element_fc;
199 };
200
201 struct bt_field_class_static_array {
202 struct bt_field_class_array common;
203 uint64_t length;
204 };
205
206 struct bt_field_class_dynamic_array {
207 struct bt_field_class_array common;
208
209 /* Weak: never dereferenced, only use to find it elsewhere */
210 struct bt_field_class *length_fc;
211
212 /* Owned by this */
213 struct bt_field_path *length_field_path;
214 };
215
216 struct bt_field_class_variant {
217 struct bt_field_class_named_field_class_container common;
218
219 /* Weak: never dereferenced, only use to find it elsewhere */
220 struct bt_field_class *selector_fc;
221
222 /* Owned by this */
223 struct bt_field_path *selector_field_path;
224 };
225
226 static inline
227 bool bt_field_class_has_known_type(const struct bt_field_class *fc)
228 {
229 return fc->type >= BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER &&
230 fc->type <= BT_FIELD_CLASS_TYPE_VARIANT;
231 }
232
233 BT_HIDDEN
234 void _bt_field_class_freeze(const struct bt_field_class *field_class);
235
236 #ifdef BT_DEV_MODE
237 # define bt_field_class_freeze _bt_field_class_freeze
238 #else
239 # define bt_field_class_freeze(_fc)
240 #endif
241
242 BT_HIDDEN
243 void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc);
244
245 #ifdef BT_DEV_MODE
246 # define bt_named_field_class_freeze _bt_named_field_class_freeze
247 #else
248 # define bt_named_field_class_freeze(_named_fc)
249 #endif
250
251 /*
252 * This function recursively marks `field_class` and its children as
253 * being part of a trace. This is used to validate that all field classes
254 * are used at a single location within trace objects even if they are
255 * shared objects for other purposes.
256 */
257 BT_HIDDEN
258 void _bt_field_class_make_part_of_trace_class(
259 const struct bt_field_class *field_class);
260
261 #ifdef BT_DEV_MODE
262 # define bt_field_class_make_part_of_trace_class _bt_field_class_make_part_of_trace_class
263 #else
264 # define bt_field_class_make_part_of_trace_class(_fc) ((void) _fc)
265 #endif
266
267 #endif /* BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H */
This page took 0.034823 seconds and 3 git commands to generate.