Commit | Line | Data |
---|---|---|
5cd6d0e5 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
5cd6d0e5 PP |
3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
5cd6d0e5 PP |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/FIELD-CLASS" |
c2d9d9cf | 25 | #include "lib/logging.h" |
5cd6d0e5 | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
3fadfbc0 MJ |
28 | #include <babeltrace2/trace-ir/field-class.h> |
29 | #include <babeltrace2/trace-ir/field-class-const.h> | |
3fadfbc0 MJ |
30 | #include <babeltrace2/trace-ir/field-const.h> |
31 | #include <babeltrace2/trace-ir/field.h> | |
3fadfbc0 | 32 | #include <babeltrace2/trace-ir/clock-class.h> |
578e048b MJ |
33 | #include "lib/object.h" |
34 | #include "compat/compiler.h" | |
35 | #include "compat/endian.h" | |
36 | #include "common/assert.h" | |
37 | #include "compat/glib.h" | |
5cd6d0e5 PP |
38 | #include <float.h> |
39 | #include <inttypes.h> | |
40 | #include <stdlib.h> | |
41 | ||
578e048b MJ |
42 | #include "clock-class.h" |
43 | #include "field-class.h" | |
44 | #include "field.h" | |
45 | #include "field-path.h" | |
46 | #include "utils.h" | |
d24d5663 | 47 | #include "lib/func-status.h" |
45c51519 | 48 | #include "lib/integer-range-set.h" |
578e048b | 49 | |
40f4ba76 PP |
50 | enum bt_field_class_type bt_field_class_get_type( |
51 | const struct bt_field_class *fc) | |
5cd6d0e5 | 52 | { |
bdb288b3 | 53 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
864cad70 | 54 | return fc->type; |
5cd6d0e5 PP |
55 | } |
56 | ||
57 | static | |
864cad70 | 58 | void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
5cd6d0e5 PP |
59 | bt_object_release_func release_func) |
60 | { | |
61 | BT_ASSERT(fc); | |
5cd6d0e5 PP |
62 | BT_ASSERT(release_func); |
63 | bt_object_init_shared(&fc->base, release_func); | |
864cad70 | 64 | fc->type = type; |
5cd6d0e5 PP |
65 | } |
66 | ||
67 | static | |
864cad70 PP |
68 | void init_integer_field_class(struct bt_field_class_integer *fc, |
69 | enum bt_field_class_type type, | |
5cd6d0e5 PP |
70 | bt_object_release_func release_func) |
71 | { | |
864cad70 | 72 | init_field_class((void *) fc, type, release_func); |
5cd6d0e5 PP |
73 | fc->range = 64; |
74 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; | |
75 | } | |
76 | ||
77 | static | |
78 | void destroy_integer_field_class(struct bt_object *obj) | |
79 | { | |
80 | BT_ASSERT(obj); | |
e5be10ef | 81 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
5cd6d0e5 PP |
82 | g_free(obj); |
83 | } | |
84 | ||
85 | static inline | |
1122a43a PP |
86 | struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class, |
87 | enum bt_field_class_type type) | |
5cd6d0e5 PP |
88 | { |
89 | struct bt_field_class_integer *int_fc = NULL; | |
90 | ||
1122a43a | 91 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef | 92 | BT_LOGD("Creating default integer field class object: type=%s", |
864cad70 | 93 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
94 | int_fc = g_new0(struct bt_field_class_integer, 1); |
95 | if (!int_fc) { | |
870631a2 PP |
96 | BT_LIB_LOGE_APPEND_CAUSE( |
97 | "Failed to allocate one integer field class."); | |
5cd6d0e5 PP |
98 | goto error; |
99 | } | |
100 | ||
864cad70 | 101 | init_integer_field_class(int_fc, type, destroy_integer_field_class); |
e5be10ef | 102 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
5cd6d0e5 PP |
103 | goto end; |
104 | ||
105 | error: | |
65300d60 | 106 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
5cd6d0e5 PP |
107 | |
108 | end: | |
109 | return (void *) int_fc; | |
110 | } | |
111 | ||
9c08c816 | 112 | struct bt_field_class *bt_field_class_integer_unsigned_create( |
1122a43a | 113 | bt_trace_class *trace_class) |
5cd6d0e5 | 114 | { |
1122a43a | 115 | return create_integer_field_class(trace_class, |
e5be10ef | 116 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); |
5cd6d0e5 PP |
117 | } |
118 | ||
9c08c816 | 119 | struct bt_field_class *bt_field_class_integer_signed_create( |
1122a43a | 120 | bt_trace_class *trace_class) |
5cd6d0e5 | 121 | { |
1122a43a | 122 | return create_integer_field_class(trace_class, |
e5be10ef | 123 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
5cd6d0e5 PP |
124 | } |
125 | ||
126 | uint64_t bt_field_class_integer_get_field_value_range( | |
40f4ba76 | 127 | const struct bt_field_class *fc) |
5cd6d0e5 | 128 | { |
40f4ba76 | 129 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
5cd6d0e5 | 130 | |
bdb288b3 PP |
131 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
132 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); | |
5cd6d0e5 PP |
133 | return int_fc->range; |
134 | } | |
135 | ||
5cd6d0e5 PP |
136 | static |
137 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, | |
138 | uint64_t size) | |
139 | { | |
140 | // TODO | |
141 | return true; | |
142 | } | |
143 | ||
40f4ba76 PP |
144 | void bt_field_class_integer_set_field_value_range( |
145 | struct bt_field_class *fc, uint64_t size) | |
5cd6d0e5 PP |
146 | { |
147 | struct bt_field_class_integer *int_fc = (void *) fc; | |
148 | ||
149 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
150 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
bdb288b3 | 151 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
5cd6d0e5 | 152 | BT_ASSERT_PRE(size <= 64, |
e5be10ef | 153 | "Unsupported size for integer field class's field value range " |
5cd6d0e5 | 154 | "(maximum is 64): size=%" PRIu64, size); |
864cad70 PP |
155 | BT_ASSERT_PRE( |
156 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
157 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || | |
5cd6d0e5 | 158 | size_is_valid_for_enumeration_field_class(fc, size), |
e5be10ef | 159 | "Invalid field value range for enumeration field class: " |
5cd6d0e5 PP |
160 | "at least one of the current mapping ranges contains values " |
161 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); | |
162 | int_fc->range = size; | |
3f7d4d90 | 163 | BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc); |
5cd6d0e5 PP |
164 | } |
165 | ||
166 | enum bt_field_class_integer_preferred_display_base | |
40f4ba76 | 167 | bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc) |
5cd6d0e5 | 168 | { |
40f4ba76 | 169 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
5cd6d0e5 | 170 | |
bdb288b3 PP |
171 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
172 | BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class"); | |
5cd6d0e5 PP |
173 | return int_fc->base; |
174 | } | |
175 | ||
40f4ba76 PP |
176 | void bt_field_class_integer_set_preferred_display_base( |
177 | struct bt_field_class *fc, | |
5cd6d0e5 PP |
178 | enum bt_field_class_integer_preferred_display_base base) |
179 | { | |
180 | struct bt_field_class_integer *int_fc = (void *) fc; | |
181 | ||
182 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
183 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
bdb288b3 | 184 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
5cd6d0e5 | 185 | int_fc->base = base; |
3f7d4d90 | 186 | BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc); |
5cd6d0e5 PP |
187 | } |
188 | ||
189 | static | |
190 | void finalize_enumeration_field_class_mapping( | |
191 | struct bt_field_class_enumeration_mapping *mapping) | |
192 | { | |
193 | BT_ASSERT(mapping); | |
194 | ||
195 | if (mapping->label) { | |
196 | g_string_free(mapping->label, TRUE); | |
45c51519 | 197 | mapping->label = NULL; |
5cd6d0e5 PP |
198 | } |
199 | ||
45c51519 | 200 | BT_OBJECT_PUT_REF_AND_RESET(mapping->range_set); |
5cd6d0e5 PP |
201 | } |
202 | ||
203 | static | |
204 | void destroy_enumeration_field_class(struct bt_object *obj) | |
205 | { | |
206 | struct bt_field_class_enumeration *fc = (void *) obj; | |
207 | ||
208 | BT_ASSERT(fc); | |
e5be10ef | 209 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
5cd6d0e5 PP |
210 | |
211 | if (fc->mappings) { | |
212 | uint64_t i; | |
213 | ||
214 | for (i = 0; i < fc->mappings->len; i++) { | |
215 | finalize_enumeration_field_class_mapping( | |
216 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); | |
217 | } | |
218 | ||
219 | g_array_free(fc->mappings, TRUE); | |
238b7404 | 220 | fc->mappings = NULL; |
5cd6d0e5 PP |
221 | } |
222 | ||
223 | if (fc->label_buf) { | |
224 | g_ptr_array_free(fc->label_buf, TRUE); | |
238b7404 | 225 | fc->label_buf = NULL; |
5cd6d0e5 PP |
226 | } |
227 | ||
228 | g_free(fc); | |
229 | } | |
230 | ||
231 | static | |
40f4ba76 | 232 | struct bt_field_class *create_enumeration_field_class( |
1122a43a | 233 | bt_trace_class *trace_class, enum bt_field_class_type type) |
5cd6d0e5 PP |
234 | { |
235 | struct bt_field_class_enumeration *enum_fc = NULL; | |
236 | ||
1122a43a | 237 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef | 238 | BT_LOGD("Creating default enumeration field class object: type=%s", |
864cad70 | 239 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
240 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
241 | if (!enum_fc) { | |
870631a2 PP |
242 | BT_LIB_LOGE_APPEND_CAUSE( |
243 | "Failed to allocate one enumeration field class."); | |
5cd6d0e5 PP |
244 | goto error; |
245 | } | |
246 | ||
864cad70 | 247 | init_integer_field_class((void *) enum_fc, type, |
5cd6d0e5 PP |
248 | destroy_enumeration_field_class); |
249 | enum_fc->mappings = g_array_new(FALSE, TRUE, | |
250 | sizeof(struct bt_field_class_enumeration_mapping)); | |
251 | if (!enum_fc->mappings) { | |
870631a2 | 252 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
5cd6d0e5 PP |
253 | goto error; |
254 | } | |
255 | ||
256 | enum_fc->label_buf = g_ptr_array_new(); | |
257 | if (!enum_fc->label_buf) { | |
870631a2 | 258 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
5cd6d0e5 PP |
259 | goto error; |
260 | } | |
261 | ||
e5be10ef | 262 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
5cd6d0e5 PP |
263 | goto end; |
264 | ||
265 | error: | |
65300d60 | 266 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
5cd6d0e5 PP |
267 | |
268 | end: | |
269 | return (void *) enum_fc; | |
270 | } | |
271 | ||
9c08c816 | 272 | struct bt_field_class *bt_field_class_enumeration_unsigned_create( |
1122a43a | 273 | bt_trace_class *trace_class) |
5cd6d0e5 | 274 | { |
1122a43a | 275 | return create_enumeration_field_class(trace_class, |
864cad70 | 276 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
5cd6d0e5 PP |
277 | } |
278 | ||
9c08c816 | 279 | struct bt_field_class *bt_field_class_enumeration_signed_create( |
1122a43a | 280 | bt_trace_class *trace_class) |
5cd6d0e5 | 281 | { |
1122a43a | 282 | return create_enumeration_field_class(trace_class, |
864cad70 | 283 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
5cd6d0e5 PP |
284 | } |
285 | ||
40f4ba76 PP |
286 | uint64_t bt_field_class_enumeration_get_mapping_count( |
287 | const struct bt_field_class *fc) | |
5cd6d0e5 | 288 | { |
40f4ba76 | 289 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 | 290 | |
bdb288b3 PP |
291 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
292 | BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class"); | |
5cd6d0e5 PP |
293 | return (uint64_t) enum_fc->mappings->len; |
294 | } | |
295 | ||
9c08c816 PP |
296 | const struct bt_field_class_enumeration_unsigned_mapping * |
297 | bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( | |
8f3ccfbc | 298 | const struct bt_field_class *fc, uint64_t index) |
5cd6d0e5 | 299 | { |
40f4ba76 | 300 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 | 301 | |
bdb288b3 PP |
302 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
303 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); | |
304 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, | |
5cd6d0e5 | 305 | "Field class"); |
8f3ccfbc | 306 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
5cd6d0e5 PP |
307 | } |
308 | ||
9c08c816 PP |
309 | const struct bt_field_class_enumeration_signed_mapping * |
310 | bt_field_class_enumeration_signed_borrow_mapping_by_index_const( | |
8f3ccfbc | 311 | const struct bt_field_class *fc, uint64_t index) |
5cd6d0e5 | 312 | { |
40f4ba76 | 313 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 | 314 | |
bdb288b3 PP |
315 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
316 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len); | |
317 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
5cd6d0e5 | 318 | "Field class"); |
8f3ccfbc | 319 | return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); |
5cd6d0e5 PP |
320 | } |
321 | ||
45c51519 PP |
322 | static |
323 | const struct bt_field_class_enumeration_mapping * | |
324 | borrow_enumeration_field_class_mapping_by_label( | |
325 | const struct bt_field_class_enumeration *fc, const char *label) | |
5cd6d0e5 | 326 | { |
45c51519 PP |
327 | struct bt_field_class_enumeration_mapping *mapping = NULL; |
328 | uint64_t i; | |
329 | ||
330 | BT_ASSERT(fc); | |
331 | BT_ASSERT_PRE_DEV_NON_NULL(label, "Label"); | |
332 | ||
333 | for (i = 0; i < fc->mappings->len; i++) { | |
334 | struct bt_field_class_enumeration_mapping *this_mapping = | |
335 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i); | |
336 | ||
337 | if (strcmp(this_mapping->label->str, label) == 0) { | |
338 | mapping = this_mapping; | |
339 | goto end; | |
340 | } | |
341 | } | |
342 | ||
343 | end: | |
344 | return mapping; | |
5cd6d0e5 PP |
345 | } |
346 | ||
9c08c816 PP |
347 | const struct bt_field_class_enumeration_signed_mapping * |
348 | bt_field_class_enumeration_signed_borrow_mapping_by_label_const( | |
45c51519 | 349 | const struct bt_field_class *fc, const char *label) |
5cd6d0e5 | 350 | { |
45c51519 PP |
351 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
352 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
353 | "Field class"); | |
354 | return (const void *) borrow_enumeration_field_class_mapping_by_label( | |
355 | (const void *) fc, label); | |
5cd6d0e5 PP |
356 | } |
357 | ||
9c08c816 PP |
358 | const struct bt_field_class_enumeration_unsigned_mapping * |
359 | bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const( | |
45c51519 | 360 | const struct bt_field_class *fc, const char *label) |
5cd6d0e5 | 361 | { |
45c51519 PP |
362 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
363 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, | |
364 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field class"); | |
365 | return (const void *) borrow_enumeration_field_class_mapping_by_label( | |
366 | (const void *) fc, label); | |
367 | } | |
5cd6d0e5 | 368 | |
45c51519 PP |
369 | const char *bt_field_class_enumeration_mapping_get_label( |
370 | const struct bt_field_class_enumeration_mapping *mapping) | |
371 | { | |
372 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
373 | return mapping->label->str; | |
5cd6d0e5 PP |
374 | } |
375 | ||
45c51519 | 376 | const struct bt_integer_range_set_unsigned * |
9c08c816 PP |
377 | bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
378 | const struct bt_field_class_enumeration_unsigned_mapping *u_mapping) | |
5cd6d0e5 | 379 | { |
45c51519 PP |
380 | const struct bt_field_class_enumeration_mapping *mapping = |
381 | (const void *) u_mapping; | |
382 | ||
383 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
384 | return (const void *) mapping->range_set; | |
5cd6d0e5 PP |
385 | } |
386 | ||
45c51519 | 387 | const struct bt_integer_range_set_signed * |
9c08c816 PP |
388 | bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
389 | const struct bt_field_class_enumeration_signed_mapping *s_mapping) | |
5cd6d0e5 | 390 | { |
45c51519 PP |
391 | const struct bt_field_class_enumeration_mapping *mapping = |
392 | (const void *) s_mapping; | |
393 | ||
394 | BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping"); | |
395 | return (const void *) mapping->range_set; | |
5cd6d0e5 PP |
396 | } |
397 | ||
185ecf64 | 398 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
9c08c816 | 399 | bt_field_class_enumeration_unsigned_get_mapping_labels_for_value( |
40f4ba76 | 400 | const struct bt_field_class *fc, uint64_t value, |
5cd6d0e5 PP |
401 | bt_field_class_enumeration_mapping_label_array *label_array, |
402 | uint64_t *count) | |
403 | { | |
40f4ba76 | 404 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 PP |
405 | uint64_t i; |
406 | ||
bdb288b3 PP |
407 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
408 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
409 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); | |
410 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, | |
5cd6d0e5 PP |
411 | "Field class"); |
412 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
413 | ||
414 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
415 | uint64_t j; | |
40f4ba76 | 416 | const struct bt_field_class_enumeration_mapping *mapping = |
5cd6d0e5 PP |
417 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
418 | ||
45c51519 PP |
419 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
420 | const struct bt_integer_range *range = (const void *) | |
421 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
422 | mapping->range_set, j); | |
5cd6d0e5 PP |
423 | |
424 | if (value >= range->lower.u && | |
425 | value <= range->upper.u) { | |
426 | g_ptr_array_add(enum_fc->label_buf, | |
427 | mapping->label->str); | |
428 | break; | |
429 | } | |
430 | } | |
431 | } | |
432 | ||
433 | *label_array = (void *) enum_fc->label_buf->pdata; | |
434 | *count = (uint64_t) enum_fc->label_buf->len; | |
d24d5663 | 435 | return BT_FUNC_STATUS_OK; |
5cd6d0e5 PP |
436 | } |
437 | ||
185ecf64 | 438 | enum bt_field_class_enumeration_get_mapping_labels_for_value_status |
9c08c816 | 439 | bt_field_class_enumeration_signed_get_mapping_labels_for_value( |
40f4ba76 | 440 | const struct bt_field_class *fc, int64_t value, |
5cd6d0e5 PP |
441 | bt_field_class_enumeration_mapping_label_array *label_array, |
442 | uint64_t *count) | |
443 | { | |
40f4ba76 | 444 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 PP |
445 | uint64_t i; |
446 | ||
bdb288b3 PP |
447 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
448 | BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)"); | |
449 | BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)"); | |
450 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, | |
5cd6d0e5 PP |
451 | "Field class"); |
452 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
453 | ||
454 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
455 | uint64_t j; | |
40f4ba76 | 456 | const struct bt_field_class_enumeration_mapping *mapping = |
5cd6d0e5 PP |
457 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
458 | ||
45c51519 PP |
459 | for (j = 0; j < mapping->range_set->ranges->len; j++) { |
460 | const struct bt_integer_range *range = (const void *) | |
461 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
462 | mapping->range_set, j); | |
5cd6d0e5 PP |
463 | |
464 | if (value >= range->lower.i && | |
465 | value <= range->upper.i) { | |
466 | g_ptr_array_add(enum_fc->label_buf, | |
467 | mapping->label->str); | |
468 | break; | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | *label_array = (void *) enum_fc->label_buf->pdata; | |
474 | *count = (uint64_t) enum_fc->label_buf->len; | |
d24d5663 | 475 | return BT_FUNC_STATUS_OK; |
5cd6d0e5 PP |
476 | } |
477 | ||
45c51519 PP |
478 | static |
479 | bool enumeration_field_class_has_mapping_with_label( | |
480 | const struct bt_field_class_enumeration *enum_fc, | |
481 | const char *label) | |
5cd6d0e5 | 482 | { |
5cd6d0e5 | 483 | uint64_t i; |
45c51519 | 484 | bool exists = false; |
5cd6d0e5 | 485 | |
45c51519 PP |
486 | BT_ASSERT(enum_fc); |
487 | BT_ASSERT(label); | |
5cd6d0e5 | 488 | |
5cd6d0e5 PP |
489 | for (i = 0; i < enum_fc->mappings->len; i++) { |
490 | struct bt_field_class_enumeration_mapping *mapping_candidate = | |
491 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
492 | ||
493 | if (strcmp(mapping_candidate->label->str, label) == 0) { | |
45c51519 PP |
494 | exists = true; |
495 | goto end; | |
5cd6d0e5 PP |
496 | } |
497 | } | |
498 | ||
45c51519 PP |
499 | end: |
500 | return exists; | |
501 | } | |
5cd6d0e5 | 502 | |
45c51519 PP |
503 | static inline |
504 | enum bt_field_class_enumeration_add_mapping_status | |
505 | add_mapping_to_enumeration_field_class(struct bt_field_class *fc, | |
506 | const char *label, const struct bt_integer_range_set *range_set) | |
507 | { | |
508 | enum bt_field_class_enumeration_add_mapping_status status = | |
509 | BT_FUNC_STATUS_OK; | |
510 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
511 | struct bt_field_class_enumeration_mapping mapping = { 0 }; | |
512 | ||
513 | BT_ASSERT(fc); | |
514 | BT_ASSERT_PRE_NON_NULL(label, "Label"); | |
6769570a | 515 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
45c51519 PP |
516 | BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label( |
517 | enum_fc, label), | |
518 | "Duplicate mapping name in enumeration field class: " | |
519 | "%![enum-fc-]+F, label=\"%s\"", fc, label); | |
520 | mapping.range_set = range_set; | |
521 | bt_object_get_ref(mapping.range_set); | |
522 | mapping.label = g_string_new(label); | |
523 | if (!mapping.label) { | |
524 | finalize_enumeration_field_class_mapping(&mapping); | |
525 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
526 | goto end; | |
5cd6d0e5 PP |
527 | } |
528 | ||
45c51519 | 529 | g_array_append_val(enum_fc->mappings, mapping); |
3f7d4d90 | 530 | BT_LIB_LOGD("Added mapping to enumeration field class: " |
45c51519 | 531 | "%![fc-]+F, label=\"%s\"", fc, label); |
5cd6d0e5 PP |
532 | |
533 | end: | |
45c51519 | 534 | return status; |
5cd6d0e5 PP |
535 | } |
536 | ||
45c51519 | 537 | enum bt_field_class_enumeration_add_mapping_status |
9c08c816 | 538 | bt_field_class_enumeration_unsigned_add_mapping( |
40f4ba76 | 539 | struct bt_field_class *fc, const char *label, |
45c51519 | 540 | const struct bt_integer_range_set_unsigned *range_set) |
5cd6d0e5 | 541 | { |
5cd6d0e5 | 542 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 543 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 | 544 | "Field class"); |
45c51519 PP |
545 | return add_mapping_to_enumeration_field_class(fc, label, |
546 | (const void *) range_set); | |
547 | } | |
548 | ||
549 | enum bt_field_class_enumeration_add_mapping_status | |
9c08c816 | 550 | bt_field_class_enumeration_signed_add_mapping( |
40f4ba76 | 551 | struct bt_field_class *fc, const char *label, |
45c51519 | 552 | const struct bt_integer_range_set_signed *range_set) |
5cd6d0e5 | 553 | { |
5cd6d0e5 | 554 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 555 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 | 556 | "Field class"); |
45c51519 PP |
557 | return add_mapping_to_enumeration_field_class(fc, label, |
558 | (const void *) range_set); | |
5cd6d0e5 PP |
559 | } |
560 | ||
561 | static | |
562 | void destroy_real_field_class(struct bt_object *obj) | |
563 | { | |
564 | BT_ASSERT(obj); | |
e5be10ef | 565 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
5cd6d0e5 PP |
566 | g_free(obj); |
567 | } | |
568 | ||
1122a43a | 569 | struct bt_field_class *bt_field_class_real_create(bt_trace_class *trace_class) |
5cd6d0e5 PP |
570 | { |
571 | struct bt_field_class_real *real_fc = NULL; | |
572 | ||
1122a43a | 573 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef | 574 | BT_LOGD_STR("Creating default real field class object."); |
5cd6d0e5 PP |
575 | real_fc = g_new0(struct bt_field_class_real, 1); |
576 | if (!real_fc) { | |
870631a2 | 577 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class."); |
5cd6d0e5 PP |
578 | goto error; |
579 | } | |
580 | ||
864cad70 | 581 | init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL, |
5cd6d0e5 | 582 | destroy_real_field_class); |
e5be10ef | 583 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
5cd6d0e5 PP |
584 | goto end; |
585 | ||
586 | error: | |
65300d60 | 587 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
5cd6d0e5 PP |
588 | |
589 | end: | |
590 | return (void *) real_fc; | |
591 | } | |
592 | ||
40f4ba76 | 593 | bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc) |
5cd6d0e5 | 594 | { |
40f4ba76 | 595 | const struct bt_field_class_real *real_fc = (const void *) fc; |
5cd6d0e5 | 596 | |
bdb288b3 PP |
597 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
598 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); | |
5cd6d0e5 PP |
599 | return real_fc->is_single_precision; |
600 | } | |
601 | ||
40f4ba76 | 602 | void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc, |
5cd6d0e5 PP |
603 | bt_bool is_single_precision) |
604 | { | |
605 | struct bt_field_class_real *real_fc = (void *) fc; | |
606 | ||
607 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 608 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
bdb288b3 | 609 | BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class"); |
5cd6d0e5 | 610 | real_fc->is_single_precision = (bool) is_single_precision; |
3f7d4d90 | 611 | BT_LIB_LOGD("Set real field class's \"is single precision\" property: " |
5cd6d0e5 | 612 | "%!+F", fc); |
5cd6d0e5 PP |
613 | } |
614 | ||
615 | static | |
616 | int init_named_field_classes_container( | |
617 | struct bt_field_class_named_field_class_container *fc, | |
40f4ba76 | 618 | enum bt_field_class_type type, |
45c51519 PP |
619 | bt_object_release_func fc_release_func, |
620 | GDestroyNotify named_fc_destroy_func) | |
5cd6d0e5 PP |
621 | { |
622 | int ret = 0; | |
623 | ||
45c51519 PP |
624 | init_field_class((void *) fc, type, fc_release_func); |
625 | fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func); | |
5cd6d0e5 | 626 | if (!fc->named_fcs) { |
45c51519 | 627 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
5cd6d0e5 PP |
628 | ret = -1; |
629 | goto end; | |
630 | } | |
631 | ||
632 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); | |
633 | if (!fc->name_to_index) { | |
870631a2 | 634 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable."); |
5cd6d0e5 PP |
635 | ret = -1; |
636 | goto end; | |
637 | } | |
638 | ||
639 | end: | |
640 | return ret; | |
641 | } | |
642 | ||
643 | static | |
644 | void finalize_named_field_class(struct bt_named_field_class *named_fc) | |
645 | { | |
646 | BT_ASSERT(named_fc); | |
e5be10ef | 647 | BT_LIB_LOGD("Finalizing named field class: " |
5cd6d0e5 PP |
648 | "addr=%p, name=\"%s\", %![fc-]+F", |
649 | named_fc, named_fc->name ? named_fc->name->str : NULL, | |
650 | named_fc->fc); | |
651 | ||
652 | if (named_fc->name) { | |
653 | g_string_free(named_fc->name, TRUE); | |
45c51519 | 654 | named_fc->name = NULL; |
5cd6d0e5 PP |
655 | } |
656 | ||
e5be10ef | 657 | BT_LOGD_STR("Putting named field class's field class."); |
238b7404 | 658 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc); |
5cd6d0e5 PP |
659 | } |
660 | ||
45c51519 PP |
661 | static |
662 | void destroy_named_field_class(gpointer ptr) | |
663 | { | |
664 | if (ptr) { | |
665 | finalize_named_field_class(ptr); | |
666 | g_free(ptr); | |
667 | } | |
668 | } | |
669 | ||
670 | static | |
671 | void destroy_variant_with_selector_option(gpointer ptr) | |
672 | { | |
673 | struct bt_field_class_variant_with_selector_option *opt = ptr; | |
674 | ||
675 | if (ptr) { | |
676 | finalize_named_field_class(&opt->common); | |
677 | BT_OBJECT_PUT_REF_AND_RESET(opt->range_set); | |
678 | g_free(ptr); | |
679 | } | |
680 | } | |
681 | ||
5cd6d0e5 PP |
682 | static |
683 | void finalize_named_field_classes_container( | |
684 | struct bt_field_class_named_field_class_container *fc) | |
685 | { | |
5cd6d0e5 PP |
686 | BT_ASSERT(fc); |
687 | ||
688 | if (fc->named_fcs) { | |
45c51519 PP |
689 | g_ptr_array_free(fc->named_fcs, TRUE); |
690 | fc->named_fcs = NULL; | |
5cd6d0e5 | 691 | |
5cd6d0e5 PP |
692 | } |
693 | ||
694 | if (fc->name_to_index) { | |
695 | g_hash_table_destroy(fc->name_to_index); | |
45c51519 | 696 | fc->name_to_index = NULL; |
5cd6d0e5 PP |
697 | } |
698 | } | |
699 | ||
700 | static | |
701 | void destroy_structure_field_class(struct bt_object *obj) | |
702 | { | |
703 | BT_ASSERT(obj); | |
1998d1ab | 704 | BT_LIB_LOGD("Destroying structure field class object: %!+F", obj); |
5cd6d0e5 PP |
705 | finalize_named_field_classes_container((void *) obj); |
706 | g_free(obj); | |
707 | } | |
708 | ||
1122a43a PP |
709 | struct bt_field_class *bt_field_class_structure_create( |
710 | bt_trace_class *trace_class) | |
5cd6d0e5 PP |
711 | { |
712 | int ret; | |
713 | struct bt_field_class_structure *struct_fc = NULL; | |
714 | ||
1122a43a | 715 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef | 716 | BT_LOGD_STR("Creating default structure field class object."); |
5cd6d0e5 PP |
717 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
718 | if (!struct_fc) { | |
870631a2 PP |
719 | BT_LIB_LOGE_APPEND_CAUSE( |
720 | "Failed to allocate one structure field class."); | |
5cd6d0e5 PP |
721 | goto error; |
722 | } | |
723 | ||
724 | ret = init_named_field_classes_container((void *) struct_fc, | |
45c51519 PP |
725 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class, |
726 | destroy_named_field_class); | |
5cd6d0e5 | 727 | if (ret) { |
45c51519 | 728 | /* init_named_field_classes_container() logs errors */ |
5cd6d0e5 PP |
729 | goto error; |
730 | } | |
731 | ||
e5be10ef | 732 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
5cd6d0e5 PP |
733 | goto end; |
734 | ||
735 | error: | |
65300d60 | 736 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
5cd6d0e5 PP |
737 | |
738 | end: | |
739 | return (void *) struct_fc; | |
740 | } | |
741 | ||
742 | static | |
45c51519 | 743 | int init_named_field_class(struct bt_named_field_class *named_fc, |
5cd6d0e5 PP |
744 | const char *name, struct bt_field_class *fc) |
745 | { | |
45c51519 | 746 | int status = BT_FUNC_STATUS_OK; |
5cd6d0e5 | 747 | |
45c51519 PP |
748 | BT_ASSERT(named_fc); |
749 | BT_ASSERT(name); | |
750 | BT_ASSERT(fc); | |
751 | named_fc->name = g_string_new(name); | |
752 | if (!named_fc->name) { | |
870631a2 | 753 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
45c51519 | 754 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5cd6d0e5 PP |
755 | goto end; |
756 | } | |
757 | ||
398454ed | 758 | named_fc->fc = fc; |
45c51519 PP |
759 | bt_object_get_no_null_check(named_fc->fc); |
760 | bt_named_field_class_freeze(named_fc); | |
1e6fd1d7 | 761 | |
45c51519 PP |
762 | end: |
763 | return status; | |
764 | } | |
765 | ||
766 | static | |
767 | struct bt_named_field_class *create_named_field_class(const char *name, | |
768 | struct bt_field_class *fc) | |
769 | { | |
770 | struct bt_named_field_class *named_fc = g_new0( | |
771 | struct bt_named_field_class, 1); | |
772 | ||
773 | if (!named_fc) { | |
774 | BT_LIB_LOGE_APPEND_CAUSE( | |
775 | "Failed to allocate a named field class."); | |
776 | goto error; | |
777 | } | |
778 | ||
779 | if (init_named_field_class(named_fc, name, fc)) { | |
780 | /* init_named_field_class() logs errors */ | |
781 | goto error; | |
782 | } | |
783 | ||
784 | goto end; | |
785 | ||
786 | error: | |
787 | destroy_named_field_class(named_fc); | |
788 | named_fc = NULL; | |
5cd6d0e5 PP |
789 | |
790 | end: | |
45c51519 PP |
791 | return named_fc; |
792 | } | |
793 | ||
794 | static | |
795 | struct bt_field_class_variant_with_selector_option * | |
796 | create_variant_with_selector_option( | |
797 | const char *name, struct bt_field_class *fc, | |
798 | const struct bt_integer_range_set *range_set) | |
799 | { | |
800 | struct bt_field_class_variant_with_selector_option *opt = g_new0( | |
801 | struct bt_field_class_variant_with_selector_option, 1); | |
802 | ||
803 | BT_ASSERT(range_set); | |
804 | ||
805 | if (!opt) { | |
806 | BT_LIB_LOGE_APPEND_CAUSE( | |
807 | "Failed to allocate a named field class."); | |
808 | goto error; | |
809 | } | |
810 | ||
811 | if (init_named_field_class(&opt->common, name, fc)) { | |
812 | goto error; | |
813 | } | |
814 | ||
815 | opt->range_set = range_set; | |
816 | bt_object_get_no_null_check(opt->range_set); | |
817 | bt_integer_range_set_freeze(range_set); | |
818 | goto end; | |
819 | ||
820 | error: | |
821 | destroy_variant_with_selector_option(opt); | |
822 | opt = NULL; | |
823 | ||
824 | end: | |
825 | return opt; | |
826 | } | |
827 | ||
828 | static | |
829 | int append_named_field_class_to_container_field_class( | |
830 | struct bt_field_class_named_field_class_container *container_fc, | |
831 | struct bt_named_field_class *named_fc) | |
832 | { | |
833 | BT_ASSERT(container_fc); | |
834 | BT_ASSERT(named_fc); | |
835 | BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class"); | |
836 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, | |
837 | named_fc->name->str), | |
838 | "Duplicate member/option name in structure/variant field class: " | |
839 | "%![container-fc-]+F, name=\"%s\"", container_fc, | |
840 | named_fc->name->str); | |
841 | g_ptr_array_add(container_fc->named_fcs, named_fc); | |
842 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, | |
843 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); | |
844 | return BT_FUNC_STATUS_OK; | |
5cd6d0e5 PP |
845 | } |
846 | ||
d24d5663 PP |
847 | enum bt_field_class_structure_append_member_status |
848 | bt_field_class_structure_append_member( | |
4295b9e0 PP |
849 | struct bt_field_class *fc, const char *name, |
850 | struct bt_field_class *member_fc) | |
5cd6d0e5 | 851 | { |
45c51519 PP |
852 | enum bt_field_class_structure_append_member_status status; |
853 | struct bt_named_field_class *named_fc = NULL; | |
e5be10ef | 854 | |
5cd6d0e5 | 855 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
740faaf4 PP |
856 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, |
857 | "Field class"); | |
45c51519 PP |
858 | named_fc = create_named_field_class(name, member_fc); |
859 | if (!named_fc) { | |
860 | /* create_named_field_class() logs errors */ | |
861 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
862 | goto end; | |
863 | } | |
864 | ||
865 | status = append_named_field_class_to_container_field_class((void *) fc, | |
866 | named_fc); | |
867 | if (status == BT_FUNC_STATUS_OK) { | |
868 | /* Moved to the container */ | |
869 | named_fc = NULL; | |
870 | } | |
871 | ||
872 | end: | |
873 | return status; | |
5cd6d0e5 PP |
874 | } |
875 | ||
40f4ba76 PP |
876 | uint64_t bt_field_class_structure_get_member_count( |
877 | const struct bt_field_class *fc) | |
5cd6d0e5 PP |
878 | { |
879 | struct bt_field_class_structure *struct_fc = (void *) fc; | |
880 | ||
bdb288b3 PP |
881 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
882 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
740faaf4 | 883 | "Field class"); |
5cd6d0e5 PP |
884 | return (uint64_t) struct_fc->common.named_fcs->len; |
885 | } | |
886 | ||
887 | static | |
1e6fd1d7 PP |
888 | struct bt_named_field_class * |
889 | borrow_named_field_class_from_container_field_class_at_index( | |
740faaf4 | 890 | struct bt_field_class_named_field_class_container *fc, |
1e6fd1d7 | 891 | uint64_t index) |
5cd6d0e5 | 892 | { |
5cd6d0e5 | 893 | BT_ASSERT(fc); |
bdb288b3 | 894 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len); |
45c51519 | 895 | return fc->named_fcs->pdata[index]; |
5cd6d0e5 PP |
896 | } |
897 | ||
1e6fd1d7 PP |
898 | const struct bt_field_class_structure_member * |
899 | bt_field_class_structure_borrow_member_by_index_const( | |
900 | const struct bt_field_class *fc, uint64_t index) | |
5cd6d0e5 | 901 | { |
bdb288b3 PP |
902 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
903 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
740faaf4 | 904 | "Field class"); |
1e6fd1d7 PP |
905 | return (const void *) |
906 | borrow_named_field_class_from_container_field_class_at_index( | |
907 | (void *) fc, index); | |
740faaf4 PP |
908 | } |
909 | ||
1e6fd1d7 PP |
910 | struct bt_field_class_structure_member * |
911 | bt_field_class_structure_borrow_member_by_index( | |
912 | struct bt_field_class *fc, uint64_t index) | |
740faaf4 | 913 | { |
bdb288b3 PP |
914 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
915 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
740faaf4 | 916 | "Field class"); |
1e6fd1d7 PP |
917 | return (void *) |
918 | borrow_named_field_class_from_container_field_class_at_index( | |
919 | (void *) fc, index); | |
e5be10ef PP |
920 | } |
921 | ||
5cd6d0e5 | 922 | static |
1e6fd1d7 PP |
923 | struct bt_named_field_class * |
924 | borrow_named_field_class_from_container_field_class_by_name( | |
740faaf4 | 925 | struct bt_field_class_named_field_class_container *fc, |
5cd6d0e5 PP |
926 | const char *name) |
927 | { | |
1e6fd1d7 | 928 | struct bt_named_field_class *named_fc = NULL; |
5cd6d0e5 PP |
929 | gpointer orig_key; |
930 | gpointer value; | |
931 | ||
932 | BT_ASSERT(fc); | |
bdb288b3 | 933 | BT_ASSERT_PRE_DEV_NON_NULL(name, "Name"); |
5cd6d0e5 PP |
934 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, |
935 | &value)) { | |
936 | goto end; | |
937 | } | |
938 | ||
45c51519 | 939 | named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)]; |
5cd6d0e5 PP |
940 | |
941 | end: | |
1e6fd1d7 | 942 | return named_fc; |
5cd6d0e5 PP |
943 | } |
944 | ||
1e6fd1d7 | 945 | const struct bt_field_class_structure_member * |
7c06e353 | 946 | bt_field_class_structure_borrow_member_by_name_const( |
40f4ba76 | 947 | const struct bt_field_class *fc, const char *name) |
5cd6d0e5 | 948 | { |
bdb288b3 PP |
949 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
950 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
740faaf4 | 951 | "Field class"); |
1e6fd1d7 PP |
952 | return (const void *) |
953 | borrow_named_field_class_from_container_field_class_by_name( | |
740faaf4 PP |
954 | (void *) fc, name); |
955 | } | |
956 | ||
1e6fd1d7 | 957 | struct bt_field_class_structure_member * |
fffedc87 | 958 | bt_field_class_structure_borrow_member_by_name( |
740faaf4 PP |
959 | struct bt_field_class *fc, const char *name) |
960 | { | |
bdb288b3 PP |
961 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
962 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, | |
740faaf4 | 963 | "Field class"); |
1e6fd1d7 PP |
964 | return (void *) |
965 | borrow_named_field_class_from_container_field_class_by_name( | |
40f4ba76 | 966 | (void *) fc, name); |
e5be10ef PP |
967 | } |
968 | ||
1e6fd1d7 PP |
969 | const char *bt_field_class_structure_member_get_name( |
970 | const struct bt_field_class_structure_member *member) | |
971 | { | |
972 | const struct bt_named_field_class *named_fc = (const void *) member; | |
973 | ||
bdb288b3 | 974 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
1e6fd1d7 PP |
975 | return named_fc->name->str; |
976 | } | |
977 | ||
978 | const struct bt_field_class * | |
979 | bt_field_class_structure_member_borrow_field_class_const( | |
980 | const struct bt_field_class_structure_member *member) | |
981 | { | |
982 | const struct bt_named_field_class *named_fc = (const void *) member; | |
983 | ||
bdb288b3 | 984 | BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member"); |
1e6fd1d7 PP |
985 | return named_fc->fc; |
986 | } | |
987 | ||
45c51519 PP |
988 | static |
989 | void finalize_variant_field_class(struct bt_field_class_variant *var_fc) | |
1e6fd1d7 | 990 | { |
45c51519 PP |
991 | BT_ASSERT(var_fc); |
992 | BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc); | |
993 | finalize_named_field_classes_container((void *) var_fc); | |
1e6fd1d7 PP |
994 | } |
995 | ||
5cd6d0e5 PP |
996 | static |
997 | void destroy_variant_field_class(struct bt_object *obj) | |
998 | { | |
999 | struct bt_field_class_variant *fc = (void *) obj; | |
1000 | ||
1001 | BT_ASSERT(fc); | |
45c51519 PP |
1002 | finalize_variant_field_class(fc); |
1003 | g_free(fc); | |
1004 | } | |
1005 | ||
1006 | static | |
1007 | void destroy_variant_with_selector_field_class(struct bt_object *obj) | |
1008 | { | |
1009 | struct bt_field_class_variant_with_selector *fc = (void *) obj; | |
1010 | ||
1011 | BT_ASSERT(fc); | |
1012 | finalize_variant_field_class(&fc->common); | |
5cd6d0e5 | 1013 | BT_LOGD_STR("Putting selector field path."); |
238b7404 PP |
1014 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path); |
1015 | BT_LOGD_STR("Putting selector field class."); | |
1016 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc); | |
5cd6d0e5 PP |
1017 | g_free(fc); |
1018 | } | |
1019 | ||
1122a43a | 1020 | struct bt_field_class *bt_field_class_variant_create( |
45c51519 | 1021 | bt_trace_class *trace_class, bt_field_class *selector_fc) |
5cd6d0e5 PP |
1022 | { |
1023 | int ret; | |
1024 | struct bt_field_class_variant *var_fc = NULL; | |
45c51519 PP |
1025 | struct bt_field_class_variant_with_selector *var_with_sel_fc = NULL; |
1026 | enum bt_field_class_type fc_type; | |
5cd6d0e5 | 1027 | |
1122a43a | 1028 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
45c51519 PP |
1029 | |
1030 | if (selector_fc) { | |
1031 | BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class"); | |
5cd6d0e5 PP |
1032 | } |
1033 | ||
45c51519 PP |
1034 | BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F", |
1035 | selector_fc); | |
1036 | ||
1037 | if (selector_fc) { | |
1038 | var_with_sel_fc = g_new0( | |
1039 | struct bt_field_class_variant_with_selector, 1); | |
1040 | if (!var_with_sel_fc) { | |
1041 | BT_LIB_LOGE_APPEND_CAUSE( | |
1042 | "Failed to allocate one variant field class with selector."); | |
1043 | goto error; | |
1044 | } | |
1045 | ||
1046 | if (selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
1047 | selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) { | |
1048 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR; | |
1049 | } else { | |
1050 | fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR; | |
1051 | } | |
1052 | ||
1053 | ret = init_named_field_classes_container( | |
1054 | (void *) var_with_sel_fc, fc_type, | |
1055 | destroy_variant_with_selector_field_class, | |
1056 | destroy_variant_with_selector_option); | |
1057 | if (ret) { | |
1058 | /* init_named_field_classes_container() logs errors */ | |
1059 | goto error; | |
1060 | } | |
1061 | ||
1062 | var_with_sel_fc->selector_fc = selector_fc; | |
1063 | bt_object_get_no_null_check(var_with_sel_fc->selector_fc); | |
1064 | bt_field_class_freeze(selector_fc); | |
1065 | var_fc = (void *) var_with_sel_fc; | |
1066 | } else { | |
1067 | var_fc = g_new0(struct bt_field_class_variant, 1); | |
1068 | if (!var_fc) { | |
1069 | BT_LIB_LOGE_APPEND_CAUSE( | |
1070 | "Failed to allocate one variant field class without selector."); | |
1071 | goto error; | |
1072 | } | |
1073 | ||
1074 | ret = init_named_field_classes_container((void *) var_fc, | |
1075 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, | |
1076 | destroy_variant_field_class, destroy_named_field_class); | |
1077 | if (ret) { | |
1078 | /* init_named_field_classes_container() logs errors */ | |
1079 | goto error; | |
1080 | } | |
5cd6d0e5 PP |
1081 | } |
1082 | ||
45c51519 PP |
1083 | BT_ASSERT(var_fc); |
1084 | BT_LIB_LOGD("Created default variant field class with selector object: " | |
1085 | "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc); | |
5cd6d0e5 PP |
1086 | goto end; |
1087 | ||
1088 | error: | |
65300d60 | 1089 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
5cd6d0e5 PP |
1090 | |
1091 | end: | |
1092 | return (void *) var_fc; | |
1093 | } | |
1094 | ||
45c51519 PP |
1095 | enum bt_field_class_variant_without_selector_append_option_status |
1096 | bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc, | |
1097 | const char *name, struct bt_field_class *option_fc) | |
1098 | { | |
1099 | enum bt_field_class_variant_without_selector_append_option_status status; | |
1100 | struct bt_named_field_class *named_fc = NULL; | |
1101 | ||
1102 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1103 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
1104 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); | |
1105 | BT_ASSERT_PRE_FC_HAS_ID(fc, | |
1106 | BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, "Field class"); | |
1107 | named_fc = create_named_field_class(name, option_fc); | |
1108 | if (!named_fc) { | |
1109 | /* create_named_field_class() logs errors */ | |
1110 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1111 | goto end; | |
1112 | } | |
1113 | ||
1114 | status = append_named_field_class_to_container_field_class((void *) fc, | |
1115 | named_fc); | |
1116 | if (status == BT_FUNC_STATUS_OK) { | |
1117 | /* Moved to the container */ | |
1118 | named_fc = NULL; | |
1119 | } | |
1120 | ||
1121 | end: | |
1122 | if (named_fc) { | |
1123 | destroy_named_field_class(named_fc); | |
1124 | } | |
1125 | ||
1126 | return status; | |
5cd6d0e5 PP |
1127 | } |
1128 | ||
45c51519 PP |
1129 | static |
1130 | int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set, | |
1131 | bool is_signed, bool *has_overlap) | |
1132 | { | |
1133 | int status = BT_FUNC_STATUS_OK; | |
1134 | struct bt_integer_range_set *full_range_set; | |
1135 | uint64_t i; | |
1136 | ||
1137 | *has_overlap = false; | |
1138 | ||
1139 | /* | |
1140 | * Build a single range set with all the ranges and test for | |
1141 | * overlaps. | |
1142 | */ | |
1143 | if (is_signed) { | |
1144 | full_range_set = (void *) bt_integer_range_set_signed_create(); | |
1145 | } else { | |
1146 | full_range_set = (void *) bt_integer_range_set_unsigned_create(); | |
1147 | } | |
1148 | ||
1149 | if (!full_range_set) { | |
1150 | BT_LOGE_STR("Failed to create a range set."); | |
1151 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1152 | goto end; | |
1153 | } | |
1154 | ||
1155 | /* Add existing option ranges */ | |
1156 | for (i = 0; i < var_fc_opts->len; i++) { | |
1157 | struct bt_field_class_variant_with_selector_option *opt = | |
1158 | var_fc_opts->pdata[i]; | |
1159 | uint64_t j; | |
1160 | ||
1161 | for (j = 0; j < opt->range_set->ranges->len; j++) { | |
1162 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
1163 | opt->range_set, j); | |
1164 | ||
1165 | if (is_signed) { | |
1166 | status = bt_integer_range_set_signed_add_range( | |
1167 | (void *) full_range_set, range->lower.i, | |
1168 | range->upper.i); | |
1169 | } else { | |
1170 | status = bt_integer_range_set_unsigned_add_range( | |
1171 | (void *) full_range_set, range->lower.u, | |
1172 | range->upper.u); | |
1173 | } | |
1174 | ||
1175 | if (status) { | |
1176 | goto end; | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | /* Add new ranges */ | |
1182 | for (i = 0; i < range_set->ranges->len; i++) { | |
1183 | struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX( | |
1184 | range_set, i); | |
1185 | ||
1186 | if (is_signed) { | |
1187 | status = bt_integer_range_set_signed_add_range( | |
1188 | (void *) full_range_set, range->lower.i, | |
1189 | range->upper.i); | |
1190 | } else { | |
1191 | status = bt_integer_range_set_unsigned_add_range( | |
1192 | (void *) full_range_set, range->lower.u, | |
1193 | range->upper.u); | |
1194 | } | |
1195 | ||
1196 | if (status) { | |
1197 | goto end; | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | /* Check overlaps */ | |
1202 | if (is_signed) { | |
1203 | *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set); | |
1204 | } else { | |
1205 | *has_overlap = bt_integer_range_set_unsigned_has_overlaps( | |
1206 | full_range_set); | |
1207 | } | |
1208 | ||
1209 | end: | |
1210 | bt_object_put_ref(full_range_set); | |
1211 | return status; | |
1212 | } | |
1213 | ||
1214 | static | |
1215 | int append_option_to_variant_with_selector_field_class( | |
1216 | struct bt_field_class *fc, const char *name, | |
1217 | struct bt_field_class *option_fc, | |
1218 | const struct bt_integer_range_set *range_set, | |
1219 | enum bt_field_class_type expected_type) | |
5cd6d0e5 | 1220 | { |
45c51519 PP |
1221 | int status; |
1222 | struct bt_field_class_variant_with_selector *var_fc = (void *) fc; | |
1223 | struct bt_field_class_variant_with_selector_option *opt = NULL; | |
1224 | bool has_overlap; | |
e5be10ef | 1225 | |
5cd6d0e5 | 1226 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
45c51519 PP |
1227 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
1228 | BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class"); | |
6769570a | 1229 | BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set"); |
45c51519 PP |
1230 | BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class"); |
1231 | BT_ASSERT_PRE(range_set->ranges->len > 0, | |
6769570a | 1232 | "Integer range set is empty: %!+R", range_set); |
45c51519 PP |
1233 | status = ranges_overlap(var_fc->common.common.named_fcs, range_set, |
1234 | expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, | |
1235 | &has_overlap); | |
1236 | if (status) { | |
1237 | /* ranges_overlap() logs errors */ | |
1238 | goto end; | |
1239 | } | |
1240 | ||
1241 | BT_ASSERT_PRE(!has_overlap, | |
6769570a PP |
1242 | "Integer range set's ranges and existing ranges have an overlap: " |
1243 | "%!+R", range_set); | |
45c51519 PP |
1244 | opt = create_variant_with_selector_option(name, option_fc, range_set); |
1245 | if (!opt) { | |
1246 | /* create_variant_with_selector_option() logs errors */ | |
1247 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1248 | goto end; | |
1249 | } | |
1250 | ||
1251 | status = append_named_field_class_to_container_field_class((void *) fc, | |
1252 | &opt->common); | |
1253 | if (status == BT_FUNC_STATUS_OK) { | |
1254 | /* Moved to the container */ | |
1255 | opt = NULL; | |
1256 | } | |
1257 | ||
1258 | end: | |
1259 | if (opt) { | |
1260 | destroy_variant_with_selector_option(opt); | |
1261 | } | |
1262 | ||
1263 | return status; | |
1264 | } | |
1265 | ||
1266 | enum bt_field_class_variant_with_selector_append_option_status | |
9c08c816 | 1267 | bt_field_class_variant_with_selector_unsigned_append_option( |
45c51519 PP |
1268 | struct bt_field_class *fc, const char *name, |
1269 | struct bt_field_class *option_fc, | |
1270 | const struct bt_integer_range_set_unsigned *range_set) | |
1271 | { | |
1272 | return append_option_to_variant_with_selector_field_class(fc, | |
1273 | name, option_fc, (const void *) range_set, | |
1274 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR); | |
1275 | } | |
1276 | ||
1277 | enum bt_field_class_variant_with_selector_append_option_status | |
9c08c816 | 1278 | bt_field_class_variant_with_selector_signed_append_option( |
45c51519 PP |
1279 | struct bt_field_class *fc, const char *name, |
1280 | struct bt_field_class *option_fc, | |
1281 | const struct bt_integer_range_set_signed *range_set) | |
1282 | { | |
1283 | return append_option_to_variant_with_selector_field_class(fc, | |
1284 | name, option_fc, (const void *) range_set, | |
1285 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR); | |
1286 | } | |
1287 | ||
1288 | uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc) | |
1289 | { | |
1290 | const struct bt_field_class_variant *var_fc = (const void *) fc; | |
1291 | ||
1292 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1293 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); | |
1294 | return (uint64_t) var_fc->common.named_fcs->len; | |
5cd6d0e5 PP |
1295 | } |
1296 | ||
1e6fd1d7 | 1297 | const struct bt_field_class_variant_option * |
7c06e353 | 1298 | bt_field_class_variant_borrow_option_by_name_const( |
40f4ba76 | 1299 | const struct bt_field_class *fc, const char *name) |
5cd6d0e5 | 1300 | { |
bdb288b3 | 1301 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 | 1302 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
1e6fd1d7 PP |
1303 | return (const void *) |
1304 | borrow_named_field_class_from_container_field_class_by_name( | |
1305 | (void *) fc, name); | |
740faaf4 PP |
1306 | } |
1307 | ||
45c51519 PP |
1308 | const struct bt_field_class_variant_option * |
1309 | bt_field_class_variant_borrow_option_by_index_const( | |
1310 | const struct bt_field_class *fc, uint64_t index) | |
740faaf4 | 1311 | { |
bdb288b3 | 1312 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 PP |
1313 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class"); |
1314 | return (const void *) | |
1315 | borrow_named_field_class_from_container_field_class_at_index( | |
1316 | (void *) fc, index); | |
e5be10ef PP |
1317 | } |
1318 | ||
9c08c816 PP |
1319 | const struct bt_field_class_variant_with_selector_unsigned_option * |
1320 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_name_const( | |
45c51519 | 1321 | const struct bt_field_class *fc, const char *name) |
5cd6d0e5 | 1322 | { |
bdb288b3 | 1323 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 PP |
1324 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
1325 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR, | |
1326 | "Field class"); | |
1327 | return (const void *) | |
1328 | borrow_named_field_class_from_container_field_class_by_name( | |
1329 | (void *) fc, name); | |
5cd6d0e5 PP |
1330 | } |
1331 | ||
9c08c816 PP |
1332 | const struct bt_field_class_variant_with_selector_unsigned_option * |
1333 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const( | |
1e6fd1d7 | 1334 | const struct bt_field_class *fc, uint64_t index) |
5cd6d0e5 | 1335 | { |
bdb288b3 | 1336 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 PP |
1337 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
1338 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR, | |
1339 | "Field class"); | |
1e6fd1d7 PP |
1340 | return (const void *) |
1341 | borrow_named_field_class_from_container_field_class_at_index( | |
1342 | (void *) fc, index); | |
740faaf4 PP |
1343 | } |
1344 | ||
9c08c816 PP |
1345 | const struct bt_field_class_variant_with_selector_signed_option * |
1346 | bt_field_class_variant_with_selector_signed_borrow_option_by_name_const( | |
45c51519 | 1347 | const struct bt_field_class *fc, const char *name) |
740faaf4 | 1348 | { |
bdb288b3 | 1349 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 PP |
1350 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, |
1351 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, | |
1352 | "Field class"); | |
1353 | return (const void *) | |
1354 | borrow_named_field_class_from_container_field_class_by_name( | |
1355 | (void *) fc, name); | |
1356 | } | |
1357 | ||
9c08c816 PP |
1358 | const struct bt_field_class_variant_with_selector_signed_option * |
1359 | bt_field_class_variant_with_selector_signed_borrow_option_by_index_const( | |
45c51519 PP |
1360 | const struct bt_field_class *fc, uint64_t index) |
1361 | { | |
1362 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); | |
1363 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, | |
1364 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, | |
1365 | "Field class"); | |
1366 | return (const void *) | |
1e6fd1d7 PP |
1367 | borrow_named_field_class_from_container_field_class_at_index( |
1368 | (void *) fc, index); | |
1369 | } | |
1370 | ||
1371 | const char *bt_field_class_variant_option_get_name( | |
1372 | const struct bt_field_class_variant_option *option) | |
1373 | { | |
1374 | const struct bt_named_field_class *named_fc = (const void *) option; | |
1375 | ||
bdb288b3 | 1376 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
1e6fd1d7 PP |
1377 | return named_fc->name->str; |
1378 | } | |
1379 | ||
1380 | const struct bt_field_class * | |
1381 | bt_field_class_variant_option_borrow_field_class_const( | |
1382 | const struct bt_field_class_variant_option *option) | |
1383 | { | |
1384 | const struct bt_named_field_class *named_fc = (const void *) option; | |
1385 | ||
bdb288b3 | 1386 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
1e6fd1d7 PP |
1387 | return named_fc->fc; |
1388 | } | |
1389 | ||
45c51519 | 1390 | const struct bt_integer_range_set_unsigned * |
9c08c816 PP |
1391 | bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const( |
1392 | const struct bt_field_class_variant_with_selector_unsigned_option *option) | |
1e6fd1d7 | 1393 | { |
45c51519 PP |
1394 | const struct bt_field_class_variant_with_selector_option *opt = |
1395 | (const void *) option; | |
1e6fd1d7 | 1396 | |
bdb288b3 | 1397 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); |
45c51519 PP |
1398 | return (const void *) opt->range_set; |
1399 | } | |
1400 | ||
1401 | const struct bt_integer_range_set_signed * | |
9c08c816 PP |
1402 | bt_field_class_variant_with_selector_signed_option_borrow_ranges_const( |
1403 | const struct bt_field_class_variant_with_selector_signed_option *option) | |
45c51519 PP |
1404 | { |
1405 | const struct bt_field_class_variant_with_selector_option *opt = | |
1406 | (const void *) option; | |
1407 | ||
1408 | BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option"); | |
1409 | return (const void *) opt->range_set; | |
5cd6d0e5 PP |
1410 | } |
1411 | ||
40f4ba76 | 1412 | const struct bt_field_path * |
45c51519 | 1413 | bt_field_class_variant_with_selector_borrow_selector_field_path_const( |
40f4ba76 | 1414 | const struct bt_field_class *fc) |
e5be10ef | 1415 | { |
45c51519 PP |
1416 | const struct bt_field_class_variant_with_selector *var_fc = |
1417 | (const void *) fc; | |
5cd6d0e5 | 1418 | |
bdb288b3 | 1419 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
45c51519 | 1420 | BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class"); |
5cd6d0e5 PP |
1421 | return var_fc->selector_field_path; |
1422 | } | |
1423 | ||
1424 | static | |
1425 | void init_array_field_class(struct bt_field_class_array *fc, | |
864cad70 | 1426 | enum bt_field_class_type type, bt_object_release_func release_func, |
5cd6d0e5 PP |
1427 | struct bt_field_class *element_fc) |
1428 | { | |
1429 | BT_ASSERT(element_fc); | |
864cad70 | 1430 | init_field_class((void *) fc, type, release_func); |
398454ed | 1431 | fc->element_fc = element_fc; |
45c51519 | 1432 | bt_object_get_no_null_check(fc->element_fc); |
5cd6d0e5 PP |
1433 | bt_field_class_freeze(element_fc); |
1434 | } | |
1435 | ||
1436 | static | |
1437 | void finalize_array_field_class(struct bt_field_class_array *array_fc) | |
1438 | { | |
1439 | BT_ASSERT(array_fc); | |
e5be10ef | 1440 | BT_LOGD_STR("Putting element field class."); |
238b7404 | 1441 | BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc); |
5cd6d0e5 PP |
1442 | } |
1443 | ||
1444 | static | |
1445 | void destroy_static_array_field_class(struct bt_object *obj) | |
1446 | { | |
1447 | BT_ASSERT(obj); | |
e5be10ef | 1448 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
5cd6d0e5 PP |
1449 | finalize_array_field_class((void *) obj); |
1450 | g_free(obj); | |
1451 | } | |
1452 | ||
40f4ba76 | 1453 | struct bt_field_class * |
9c08c816 | 1454 | bt_field_class_array_static_create(bt_trace_class *trace_class, |
1122a43a | 1455 | struct bt_field_class *element_fc, uint64_t length) |
5cd6d0e5 | 1456 | { |
9c08c816 | 1457 | struct bt_field_class_array_static *array_fc = NULL; |
5cd6d0e5 | 1458 | |
1122a43a | 1459 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef PP |
1460 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1461 | BT_LOGD_STR("Creating default static array field class object."); | |
9c08c816 | 1462 | array_fc = g_new0(struct bt_field_class_array_static, 1); |
5cd6d0e5 | 1463 | if (!array_fc) { |
870631a2 PP |
1464 | BT_LIB_LOGE_APPEND_CAUSE( |
1465 | "Failed to allocate one static array field class."); | |
5cd6d0e5 PP |
1466 | goto error; |
1467 | } | |
1468 | ||
864cad70 | 1469 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
5cd6d0e5 PP |
1470 | destroy_static_array_field_class, element_fc); |
1471 | array_fc->length = length; | |
e5be10ef | 1472 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1473 | goto end; |
1474 | ||
1475 | error: | |
65300d60 | 1476 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1477 | |
1478 | end: | |
1479 | return (void *) array_fc; | |
1480 | } | |
1481 | ||
40f4ba76 PP |
1482 | const struct bt_field_class * |
1483 | bt_field_class_array_borrow_element_field_class_const( | |
1484 | const struct bt_field_class *fc) | |
5cd6d0e5 | 1485 | { |
40f4ba76 | 1486 | const struct bt_field_class_array *array_fc = (const void *) fc; |
5cd6d0e5 | 1487 | |
bdb288b3 PP |
1488 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1489 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); | |
5cd6d0e5 PP |
1490 | return array_fc->element_fc; |
1491 | } | |
1492 | ||
740faaf4 PP |
1493 | struct bt_field_class * |
1494 | bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc) | |
1495 | { | |
1496 | struct bt_field_class_array *array_fc = (void *) fc; | |
1497 | ||
bdb288b3 PP |
1498 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1499 | BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class"); | |
740faaf4 PP |
1500 | return array_fc->element_fc; |
1501 | } | |
1502 | ||
9c08c816 | 1503 | uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc) |
e5be10ef | 1504 | { |
9c08c816 | 1505 | const struct bt_field_class_array_static *array_fc = (const void *) fc; |
5cd6d0e5 | 1506 | |
bdb288b3 PP |
1507 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1508 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, | |
5cd6d0e5 PP |
1509 | "Field class"); |
1510 | return (uint64_t) array_fc->length; | |
1511 | } | |
1512 | ||
1513 | static | |
1514 | void destroy_dynamic_array_field_class(struct bt_object *obj) | |
1515 | { | |
9c08c816 | 1516 | struct bt_field_class_array_dynamic *fc = (void *) obj; |
5cd6d0e5 PP |
1517 | |
1518 | BT_ASSERT(fc); | |
e5be10ef | 1519 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
5cd6d0e5 PP |
1520 | finalize_array_field_class((void *) fc); |
1521 | BT_LOGD_STR("Putting length field path."); | |
238b7404 PP |
1522 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path); |
1523 | BT_LOGD_STR("Putting length field class."); | |
1524 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc); | |
5cd6d0e5 PP |
1525 | g_free(fc); |
1526 | } | |
1527 | ||
9c08c816 | 1528 | struct bt_field_class *bt_field_class_array_dynamic_create( |
1367bc7c PP |
1529 | struct bt_trace_class *trace_class, |
1530 | struct bt_field_class *element_fc, | |
1531 | struct bt_field_class *length_fc) | |
5cd6d0e5 | 1532 | { |
9c08c816 | 1533 | struct bt_field_class_array_dynamic *array_fc = NULL; |
5cd6d0e5 | 1534 | |
1122a43a | 1535 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef PP |
1536 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1537 | BT_LOGD_STR("Creating default dynamic array field class object."); | |
9c08c816 | 1538 | array_fc = g_new0(struct bt_field_class_array_dynamic, 1); |
5cd6d0e5 | 1539 | if (!array_fc) { |
870631a2 PP |
1540 | BT_LIB_LOGE_APPEND_CAUSE( |
1541 | "Failed to allocate one dynamic array field class."); | |
5cd6d0e5 PP |
1542 | goto error; |
1543 | } | |
1544 | ||
40f4ba76 PP |
1545 | init_array_field_class((void *) array_fc, |
1546 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, | |
5cd6d0e5 | 1547 | destroy_dynamic_array_field_class, element_fc); |
1367bc7c PP |
1548 | |
1549 | if (length_fc) { | |
1550 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, | |
1551 | "Length field class"); | |
1552 | array_fc->length_fc = length_fc; | |
1553 | bt_object_get_no_null_check(array_fc->length_fc); | |
1554 | bt_field_class_freeze(length_fc); | |
1555 | } | |
1556 | ||
e5be10ef | 1557 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1558 | goto end; |
1559 | ||
1560 | error: | |
65300d60 | 1561 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1562 | |
1563 | end: | |
1564 | return (void *) array_fc; | |
1565 | } | |
1566 | ||
40f4ba76 | 1567 | const struct bt_field_path * |
9c08c816 | 1568 | bt_field_class_array_dynamic_borrow_length_field_path_const( |
40f4ba76 | 1569 | const struct bt_field_class *fc) |
5cd6d0e5 | 1570 | { |
9c08c816 | 1571 | const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc; |
5cd6d0e5 | 1572 | |
bdb288b3 PP |
1573 | BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class"); |
1574 | BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, | |
5cd6d0e5 PP |
1575 | "Field class"); |
1576 | return seq_fc->length_field_path; | |
1577 | } | |
1578 | ||
1579 | static | |
1580 | void destroy_string_field_class(struct bt_object *obj) | |
1581 | { | |
1582 | BT_ASSERT(obj); | |
e5be10ef | 1583 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
5cd6d0e5 PP |
1584 | g_free(obj); |
1585 | } | |
1586 | ||
1122a43a | 1587 | struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class) |
5cd6d0e5 PP |
1588 | { |
1589 | struct bt_field_class_string *string_fc = NULL; | |
1590 | ||
1122a43a | 1591 | BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); |
e5be10ef | 1592 | BT_LOGD_STR("Creating default string field class object."); |
5cd6d0e5 PP |
1593 | string_fc = g_new0(struct bt_field_class_string, 1); |
1594 | if (!string_fc) { | |
870631a2 PP |
1595 | BT_LIB_LOGE_APPEND_CAUSE( |
1596 | "Failed to allocate one string field class."); | |
5cd6d0e5 PP |
1597 | goto error; |
1598 | } | |
1599 | ||
864cad70 | 1600 | init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
5cd6d0e5 | 1601 | destroy_string_field_class); |
e5be10ef | 1602 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
5cd6d0e5 PP |
1603 | goto end; |
1604 | ||
1605 | error: | |
65300d60 | 1606 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
5cd6d0e5 PP |
1607 | |
1608 | end: | |
1609 | return (void *) string_fc; | |
1610 | } | |
1611 | ||
1612 | BT_HIDDEN | |
1e6fd1d7 | 1613 | void _bt_field_class_freeze(const struct bt_field_class *c_fc) |
5cd6d0e5 | 1614 | { |
1e6fd1d7 PP |
1615 | struct bt_field_class *fc = (void *) c_fc; |
1616 | ||
5cd6d0e5 PP |
1617 | /* |
1618 | * Element/member/option field classes are frozen when added to | |
1619 | * their owner. | |
1620 | */ | |
1621 | BT_ASSERT(fc); | |
1e6fd1d7 PP |
1622 | fc->frozen = true; |
1623 | ||
1624 | switch (fc->type) { | |
1625 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
45c51519 PP |
1626 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
1627 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
1628 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
1e6fd1d7 PP |
1629 | { |
1630 | struct bt_field_class_named_field_class_container *container_fc = | |
1631 | (void *) fc; | |
1632 | uint64_t i; | |
1633 | ||
1634 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
45c51519 PP |
1635 | bt_named_field_class_freeze( |
1636 | container_fc->named_fcs->pdata[i]); | |
1e6fd1d7 PP |
1637 | } |
1638 | ||
1639 | break; | |
1640 | } | |
1641 | default: | |
1642 | break; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | BT_HIDDEN | |
1647 | void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc) | |
1648 | { | |
1649 | BT_ASSERT(named_fc); | |
1650 | ((struct bt_named_field_class *) named_fc)->frozen = true; | |
1651 | bt_field_class_freeze(named_fc->fc); | |
5cd6d0e5 PP |
1652 | } |
1653 | ||
1654 | BT_HIDDEN | |
bdb288b3 | 1655 | void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc) |
5cd6d0e5 | 1656 | { |
40f4ba76 PP |
1657 | struct bt_field_class *fc = (void *) c_fc; |
1658 | ||
5cd6d0e5 | 1659 | BT_ASSERT(fc); |
862ca4ed | 1660 | BT_ASSERT_PRE(!fc->part_of_trace_class, |
5cd6d0e5 | 1661 | "Field class is already part of a trace: %!+F", fc); |
862ca4ed | 1662 | fc->part_of_trace_class = true; |
5cd6d0e5 | 1663 | |
864cad70 PP |
1664 | switch (fc->type) { |
1665 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
45c51519 PP |
1666 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
1667 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
1668 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
5cd6d0e5 PP |
1669 | { |
1670 | struct bt_field_class_named_field_class_container *container_fc = | |
1671 | (void *) fc; | |
1672 | uint64_t i; | |
1673 | ||
1674 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
1675 | struct bt_named_field_class *named_fc = | |
45c51519 | 1676 | container_fc->named_fcs->pdata[i]; |
5cd6d0e5 | 1677 | |
862ca4ed | 1678 | bt_field_class_make_part_of_trace_class(named_fc->fc); |
5cd6d0e5 PP |
1679 | } |
1680 | ||
1681 | break; | |
1682 | } | |
864cad70 PP |
1683 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
1684 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
5cd6d0e5 PP |
1685 | { |
1686 | struct bt_field_class_array *array_fc = (void *) fc; | |
1687 | ||
862ca4ed | 1688 | bt_field_class_make_part_of_trace_class(array_fc->element_fc); |
5cd6d0e5 PP |
1689 | break; |
1690 | } | |
1691 | default: | |
1692 | break; | |
1693 | } | |
1694 | } | |
c5b9b441 PP |
1695 | |
1696 | void bt_field_class_get_ref(const struct bt_field_class *field_class) | |
1697 | { | |
1698 | bt_object_get_ref(field_class); | |
1699 | } | |
1700 | ||
1701 | void bt_field_class_put_ref(const struct bt_field_class *field_class) | |
1702 | { | |
1703 | bt_object_put_ref(field_class); | |
1704 | } |