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