Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / lib / trace-ir / field-class.c
CommitLineData
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>
c4f23e30 40#include <stdbool.h>
5cd6d0e5
PP
41#include <stdlib.h>
42
578e048b
MJ
43#include "clock-class.h"
44#include "field-class.h"
45#include "field.h"
46#include "field-path.h"
47#include "utils.h"
d24d5663 48#include "lib/func-status.h"
45c51519 49#include "lib/integer-range-set.h"
c6962c96 50#include "lib/value.h"
578e048b 51
40f4ba76
PP
52enum bt_field_class_type bt_field_class_get_type(
53 const struct bt_field_class *fc)
5cd6d0e5 54{
bdb288b3 55 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
864cad70 56 return fc->type;
5cd6d0e5
PP
57}
58
59static
c6962c96 60int init_field_class(struct bt_field_class *fc, enum bt_field_class_type type,
5cd6d0e5
PP
61 bt_object_release_func release_func)
62{
c6962c96
PP
63 int ret = 0;
64
5cd6d0e5 65 BT_ASSERT(fc);
5cd6d0e5
PP
66 BT_ASSERT(release_func);
67 bt_object_init_shared(&fc->base, release_func);
864cad70 68 fc->type = type;
c6962c96
PP
69 fc->user_attributes = bt_value_map_create();
70 if (!fc->user_attributes) {
71 BT_LIB_LOGE_APPEND_CAUSE(
72 "Failed to create a map value object.");
73 ret = -1;
74 goto end;
75 }
76
77end:
78 return ret;
79}
80
81static
82void finalize_field_class(struct bt_field_class *fc)
83{
84 BT_OBJECT_PUT_REF_AND_RESET(fc->user_attributes);
5cd6d0e5
PP
85}
86
1094efa4
PP
87static
88void destroy_bit_array_field_class(struct bt_object *obj)
89{
90 BT_ASSERT(obj);
91 BT_LIB_LOGD("Destroying bit array field class object: %!+F", obj);
c6962c96 92 finalize_field_class((void *) obj);
1094efa4
PP
93 g_free(obj);
94}
95
96struct bt_field_class *bt_field_class_bit_array_create(
97 struct bt_trace_class *trace_class, uint64_t length)
98{
99 struct bt_field_class_bit_array *ba_fc = NULL;
100
101 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
102 BT_ASSERT_PRE(length > 0 && length <= 64,
103 "Unsupported length for bit array field class "
104 "(minimum is 1, maximum is 64): length=%" PRIu64, length);
105 BT_LOGD("Creating default bit array field class object.");
106 ba_fc = g_new0(struct bt_field_class_bit_array, 1);
107 if (!ba_fc) {
108 BT_LIB_LOGE_APPEND_CAUSE(
109 "Failed to allocate one bit array field class.");
110 goto error;
111 }
112
c6962c96
PP
113 if (init_field_class((void *) ba_fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY,
114 destroy_bit_array_field_class)) {
115 goto error;
116 }
117
1094efa4
PP
118 ba_fc->length = length;
119 BT_LIB_LOGD("Created bit array field class object: %!+F", ba_fc);
120 goto end;
121
122error:
123 BT_OBJECT_PUT_REF_AND_RESET(ba_fc);
124
125end:
126 return (void *) ba_fc;
127}
128
129uint64_t bt_field_class_bit_array_get_length(const struct bt_field_class *fc)
130{
131 const struct bt_field_class_bit_array *ba_fc = (const void *) fc;
132
133 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
134 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY,
135 "Field class");
136 return ba_fc->length;
137}
138
5cebbe7f
PP
139static
140void destroy_bool_field_class(struct bt_object *obj)
141{
142 BT_ASSERT(obj);
143 BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj);
c6962c96 144 finalize_field_class((void *) obj);
5cebbe7f
PP
145 g_free(obj);
146}
147
148struct bt_field_class *bt_field_class_bool_create(
149 bt_trace_class *trace_class)
150{
151 struct bt_field_class_bool *bool_fc = NULL;
152
153 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
154 BT_LOGD("Creating default boolean field class object.");
155 bool_fc = g_new0(struct bt_field_class_bool, 1);
156 if (!bool_fc) {
157 BT_LIB_LOGE_APPEND_CAUSE(
158 "Failed to allocate one boolean field class.");
159 goto error;
160 }
161
c6962c96
PP
162 if (init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL,
163 destroy_bool_field_class)) {
164 goto error;
165 }
166
5cebbe7f
PP
167 BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc);
168 goto end;
169
170error:
171 BT_OBJECT_PUT_REF_AND_RESET(bool_fc);
172
173end:
174 return (void *) bool_fc;
175}
176
5cd6d0e5 177static
c6962c96 178int init_integer_field_class(struct bt_field_class_integer *fc,
864cad70 179 enum bt_field_class_type type,
5cd6d0e5
PP
180 bt_object_release_func release_func)
181{
c6962c96
PP
182 int ret;
183
184 ret = init_field_class((void *) fc, type, release_func);
185 if (ret) {
186 goto end;
187 }
188
5cd6d0e5
PP
189 fc->range = 64;
190 fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL;
c6962c96
PP
191
192end:
193 return ret;
5cd6d0e5
PP
194}
195
196static
197void destroy_integer_field_class(struct bt_object *obj)
198{
199 BT_ASSERT(obj);
e5be10ef 200 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj);
c6962c96 201 finalize_field_class((void *) obj);
5cd6d0e5
PP
202 g_free(obj);
203}
204
205static inline
1122a43a
PP
206struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class,
207 enum bt_field_class_type type)
5cd6d0e5
PP
208{
209 struct bt_field_class_integer *int_fc = NULL;
210
1122a43a 211 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 212 BT_LOGD("Creating default integer field class object: type=%s",
864cad70 213 bt_common_field_class_type_string(type));
5cd6d0e5
PP
214 int_fc = g_new0(struct bt_field_class_integer, 1);
215 if (!int_fc) {
870631a2
PP
216 BT_LIB_LOGE_APPEND_CAUSE(
217 "Failed to allocate one integer field class.");
5cd6d0e5
PP
218 goto error;
219 }
220
c6962c96
PP
221 if (init_integer_field_class(int_fc, type,
222 destroy_integer_field_class)) {
223 goto error;
224 }
225
e5be10ef 226 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc);
5cd6d0e5
PP
227 goto end;
228
229error:
65300d60 230 BT_OBJECT_PUT_REF_AND_RESET(int_fc);
5cd6d0e5
PP
231
232end:
233 return (void *) int_fc;
234}
235
9c08c816 236struct bt_field_class *bt_field_class_integer_unsigned_create(
1122a43a 237 bt_trace_class *trace_class)
5cd6d0e5 238{
1122a43a 239 return create_integer_field_class(trace_class,
e5be10ef 240 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
5cd6d0e5
PP
241}
242
9c08c816 243struct bt_field_class *bt_field_class_integer_signed_create(
1122a43a 244 bt_trace_class *trace_class)
5cd6d0e5 245{
1122a43a 246 return create_integer_field_class(trace_class,
e5be10ef 247 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER);
5cd6d0e5
PP
248}
249
250uint64_t bt_field_class_integer_get_field_value_range(
40f4ba76 251 const struct bt_field_class *fc)
5cd6d0e5 252{
40f4ba76 253 const struct bt_field_class_integer *int_fc = (const void *) fc;
5cd6d0e5 254
bdb288b3
PP
255 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
256 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
5cd6d0e5
PP
257 return int_fc->range;
258}
259
5cd6d0e5
PP
260static
261bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc,
262 uint64_t size)
263{
264 // TODO
265 return true;
266}
267
40f4ba76
PP
268void bt_field_class_integer_set_field_value_range(
269 struct bt_field_class *fc, uint64_t size)
5cd6d0e5
PP
270{
271 struct bt_field_class_integer *int_fc = (void *) fc;
272
273 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
274 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
bdb288b3 275 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
5cd6d0e5 276 BT_ASSERT_PRE(size <= 64,
e5be10ef 277 "Unsupported size for integer field class's field value range "
5cd6d0e5 278 "(maximum is 64): size=%" PRIu64, size);
864cad70
PP
279 BT_ASSERT_PRE(
280 int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
281 int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
5cd6d0e5 282 size_is_valid_for_enumeration_field_class(fc, size),
e5be10ef 283 "Invalid field value range for enumeration field class: "
5cd6d0e5
PP
284 "at least one of the current mapping ranges contains values "
285 "which are outside this range: %!+F, size=%" PRIu64, fc, size);
286 int_fc->range = size;
3f7d4d90 287 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc);
5cd6d0e5
PP
288}
289
290enum bt_field_class_integer_preferred_display_base
40f4ba76 291bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc)
5cd6d0e5 292{
40f4ba76 293 const struct bt_field_class_integer *int_fc = (const void *) fc;
5cd6d0e5 294
bdb288b3
PP
295 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
296 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
5cd6d0e5
PP
297 return int_fc->base;
298}
299
40f4ba76
PP
300void bt_field_class_integer_set_preferred_display_base(
301 struct bt_field_class *fc,
5cd6d0e5
PP
302 enum bt_field_class_integer_preferred_display_base base)
303{
304 struct bt_field_class_integer *int_fc = (void *) fc;
305
306 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
307 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
bdb288b3 308 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
5cd6d0e5 309 int_fc->base = base;
3f7d4d90 310 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc);
5cd6d0e5
PP
311}
312
313static
314void finalize_enumeration_field_class_mapping(
315 struct bt_field_class_enumeration_mapping *mapping)
316{
317 BT_ASSERT(mapping);
318
319 if (mapping->label) {
320 g_string_free(mapping->label, TRUE);
45c51519 321 mapping->label = NULL;
5cd6d0e5
PP
322 }
323
45c51519 324 BT_OBJECT_PUT_REF_AND_RESET(mapping->range_set);
5cd6d0e5
PP
325}
326
327static
328void destroy_enumeration_field_class(struct bt_object *obj)
329{
330 struct bt_field_class_enumeration *fc = (void *) obj;
331
332 BT_ASSERT(fc);
e5be10ef 333 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc);
c6962c96 334 finalize_field_class((void *) obj);
5cd6d0e5
PP
335
336 if (fc->mappings) {
337 uint64_t i;
338
339 for (i = 0; i < fc->mappings->len; i++) {
340 finalize_enumeration_field_class_mapping(
341 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i));
342 }
343
344 g_array_free(fc->mappings, TRUE);
238b7404 345 fc->mappings = NULL;
5cd6d0e5
PP
346 }
347
348 if (fc->label_buf) {
349 g_ptr_array_free(fc->label_buf, TRUE);
238b7404 350 fc->label_buf = NULL;
5cd6d0e5
PP
351 }
352
353 g_free(fc);
354}
355
356static
40f4ba76 357struct bt_field_class *create_enumeration_field_class(
1122a43a 358 bt_trace_class *trace_class, enum bt_field_class_type type)
5cd6d0e5
PP
359{
360 struct bt_field_class_enumeration *enum_fc = NULL;
361
1122a43a 362 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 363 BT_LOGD("Creating default enumeration field class object: type=%s",
864cad70 364 bt_common_field_class_type_string(type));
5cd6d0e5
PP
365 enum_fc = g_new0(struct bt_field_class_enumeration, 1);
366 if (!enum_fc) {
870631a2
PP
367 BT_LIB_LOGE_APPEND_CAUSE(
368 "Failed to allocate one enumeration field class.");
5cd6d0e5
PP
369 goto error;
370 }
371
c6962c96
PP
372 if (init_integer_field_class((void *) enum_fc, type,
373 destroy_enumeration_field_class)) {
374 goto error;
375 }
376
5cd6d0e5
PP
377 enum_fc->mappings = g_array_new(FALSE, TRUE,
378 sizeof(struct bt_field_class_enumeration_mapping));
379 if (!enum_fc->mappings) {
870631a2 380 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
5cd6d0e5
PP
381 goto error;
382 }
383
384 enum_fc->label_buf = g_ptr_array_new();
385 if (!enum_fc->label_buf) {
870631a2 386 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
5cd6d0e5
PP
387 goto error;
388 }
389
e5be10ef 390 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc);
5cd6d0e5
PP
391 goto end;
392
393error:
65300d60 394 BT_OBJECT_PUT_REF_AND_RESET(enum_fc);
5cd6d0e5
PP
395
396end:
397 return (void *) enum_fc;
398}
399
9c08c816 400struct bt_field_class *bt_field_class_enumeration_unsigned_create(
1122a43a 401 bt_trace_class *trace_class)
5cd6d0e5 402{
1122a43a 403 return create_enumeration_field_class(trace_class,
864cad70 404 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
5cd6d0e5
PP
405}
406
9c08c816 407struct bt_field_class *bt_field_class_enumeration_signed_create(
1122a43a 408 bt_trace_class *trace_class)
5cd6d0e5 409{
1122a43a 410 return create_enumeration_field_class(trace_class,
864cad70 411 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
5cd6d0e5
PP
412}
413
40f4ba76
PP
414uint64_t bt_field_class_enumeration_get_mapping_count(
415 const struct bt_field_class *fc)
5cd6d0e5 416{
40f4ba76 417 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 418
bdb288b3
PP
419 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
420 BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class");
5cd6d0e5
PP
421 return (uint64_t) enum_fc->mappings->len;
422}
423
9c08c816
PP
424const struct bt_field_class_enumeration_unsigned_mapping *
425bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
8f3ccfbc 426 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 427{
40f4ba76 428 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 429
bdb288b3
PP
430 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
431 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
432 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5 433 "Field class");
8f3ccfbc 434 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
5cd6d0e5
PP
435}
436
9c08c816
PP
437const struct bt_field_class_enumeration_signed_mapping *
438bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
8f3ccfbc 439 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 440{
40f4ba76 441 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5 442
bdb288b3
PP
443 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
444 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
445 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5 446 "Field class");
8f3ccfbc 447 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
5cd6d0e5
PP
448}
449
45c51519
PP
450static
451const struct bt_field_class_enumeration_mapping *
452borrow_enumeration_field_class_mapping_by_label(
453 const struct bt_field_class_enumeration *fc, const char *label)
5cd6d0e5 454{
45c51519
PP
455 struct bt_field_class_enumeration_mapping *mapping = NULL;
456 uint64_t i;
457
98b15851 458 BT_ASSERT_DBG(fc);
45c51519
PP
459 BT_ASSERT_PRE_DEV_NON_NULL(label, "Label");
460
461 for (i = 0; i < fc->mappings->len; i++) {
462 struct bt_field_class_enumeration_mapping *this_mapping =
463 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i);
464
465 if (strcmp(this_mapping->label->str, label) == 0) {
466 mapping = this_mapping;
467 goto end;
468 }
469 }
470
471end:
472 return mapping;
5cd6d0e5
PP
473}
474
9c08c816
PP
475const struct bt_field_class_enumeration_signed_mapping *
476bt_field_class_enumeration_signed_borrow_mapping_by_label_const(
45c51519 477 const struct bt_field_class *fc, const char *label)
5cd6d0e5 478{
45c51519
PP
479 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
480 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
481 "Field class");
482 return (const void *) borrow_enumeration_field_class_mapping_by_label(
483 (const void *) fc, label);
5cd6d0e5
PP
484}
485
9c08c816
PP
486const struct bt_field_class_enumeration_unsigned_mapping *
487bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const(
45c51519 488 const struct bt_field_class *fc, const char *label)
5cd6d0e5 489{
45c51519
PP
490 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
491 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
492 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field class");
493 return (const void *) borrow_enumeration_field_class_mapping_by_label(
494 (const void *) fc, label);
495}
5cd6d0e5 496
45c51519
PP
497const char *bt_field_class_enumeration_mapping_get_label(
498 const struct bt_field_class_enumeration_mapping *mapping)
499{
500 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
501 return mapping->label->str;
5cd6d0e5
PP
502}
503
45c51519 504const struct bt_integer_range_set_unsigned *
9c08c816
PP
505bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
506 const struct bt_field_class_enumeration_unsigned_mapping *u_mapping)
5cd6d0e5 507{
45c51519
PP
508 const struct bt_field_class_enumeration_mapping *mapping =
509 (const void *) u_mapping;
510
511 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
512 return (const void *) mapping->range_set;
5cd6d0e5
PP
513}
514
45c51519 515const struct bt_integer_range_set_signed *
9c08c816
PP
516bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
517 const struct bt_field_class_enumeration_signed_mapping *s_mapping)
5cd6d0e5 518{
45c51519
PP
519 const struct bt_field_class_enumeration_mapping *mapping =
520 (const void *) s_mapping;
521
522 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
523 return (const void *) mapping->range_set;
5cd6d0e5
PP
524}
525
185ecf64 526enum bt_field_class_enumeration_get_mapping_labels_for_value_status
9c08c816 527bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
40f4ba76 528 const struct bt_field_class *fc, uint64_t value,
5cd6d0e5
PP
529 bt_field_class_enumeration_mapping_label_array *label_array,
530 uint64_t *count)
531{
40f4ba76 532 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
533 uint64_t i;
534
bdb288b3
PP
535 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
536 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
537 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
538 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5
PP
539 "Field class");
540 g_ptr_array_set_size(enum_fc->label_buf, 0);
541
542 for (i = 0; i < enum_fc->mappings->len; i++) {
543 uint64_t j;
40f4ba76 544 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
545 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
546
45c51519
PP
547 for (j = 0; j < mapping->range_set->ranges->len; j++) {
548 const struct bt_integer_range *range = (const void *)
549 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
550 mapping->range_set, j);
5cd6d0e5
PP
551
552 if (value >= range->lower.u &&
553 value <= range->upper.u) {
554 g_ptr_array_add(enum_fc->label_buf,
555 mapping->label->str);
556 break;
557 }
558 }
559 }
560
561 *label_array = (void *) enum_fc->label_buf->pdata;
562 *count = (uint64_t) enum_fc->label_buf->len;
d24d5663 563 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
564}
565
185ecf64 566enum bt_field_class_enumeration_get_mapping_labels_for_value_status
9c08c816 567bt_field_class_enumeration_signed_get_mapping_labels_for_value(
40f4ba76 568 const struct bt_field_class *fc, int64_t value,
5cd6d0e5
PP
569 bt_field_class_enumeration_mapping_label_array *label_array,
570 uint64_t *count)
571{
40f4ba76 572 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
5cd6d0e5
PP
573 uint64_t i;
574
bdb288b3
PP
575 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
576 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
577 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
578 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5
PP
579 "Field class");
580 g_ptr_array_set_size(enum_fc->label_buf, 0);
581
582 for (i = 0; i < enum_fc->mappings->len; i++) {
583 uint64_t j;
40f4ba76 584 const struct bt_field_class_enumeration_mapping *mapping =
5cd6d0e5
PP
585 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
586
45c51519
PP
587 for (j = 0; j < mapping->range_set->ranges->len; j++) {
588 const struct bt_integer_range *range = (const void *)
589 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
590 mapping->range_set, j);
5cd6d0e5
PP
591
592 if (value >= range->lower.i &&
593 value <= range->upper.i) {
594 g_ptr_array_add(enum_fc->label_buf,
595 mapping->label->str);
596 break;
597 }
598 }
599 }
600
601 *label_array = (void *) enum_fc->label_buf->pdata;
602 *count = (uint64_t) enum_fc->label_buf->len;
d24d5663 603 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
604}
605
45c51519
PP
606static
607bool enumeration_field_class_has_mapping_with_label(
608 const struct bt_field_class_enumeration *enum_fc,
609 const char *label)
5cd6d0e5 610{
5cd6d0e5 611 uint64_t i;
45c51519 612 bool exists = false;
5cd6d0e5 613
45c51519
PP
614 BT_ASSERT(enum_fc);
615 BT_ASSERT(label);
5cd6d0e5 616
5cd6d0e5
PP
617 for (i = 0; i < enum_fc->mappings->len; i++) {
618 struct bt_field_class_enumeration_mapping *mapping_candidate =
619 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
620
621 if (strcmp(mapping_candidate->label->str, label) == 0) {
45c51519
PP
622 exists = true;
623 goto end;
5cd6d0e5
PP
624 }
625 }
626
45c51519
PP
627end:
628 return exists;
629}
5cd6d0e5 630
45c51519
PP
631static inline
632enum bt_field_class_enumeration_add_mapping_status
633add_mapping_to_enumeration_field_class(struct bt_field_class *fc,
634 const char *label, const struct bt_integer_range_set *range_set)
635{
636 enum bt_field_class_enumeration_add_mapping_status status =
637 BT_FUNC_STATUS_OK;
638 struct bt_field_class_enumeration *enum_fc = (void *) fc;
639 struct bt_field_class_enumeration_mapping mapping = { 0 };
640
641 BT_ASSERT(fc);
642 BT_ASSERT_PRE_NON_NULL(label, "Label");
6769570a 643 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
45c51519
PP
644 BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label(
645 enum_fc, label),
646 "Duplicate mapping name in enumeration field class: "
647 "%![enum-fc-]+F, label=\"%s\"", fc, label);
648 mapping.range_set = range_set;
649 bt_object_get_ref(mapping.range_set);
650 mapping.label = g_string_new(label);
651 if (!mapping.label) {
652 finalize_enumeration_field_class_mapping(&mapping);
653 status = BT_FUNC_STATUS_MEMORY_ERROR;
654 goto end;
5cd6d0e5
PP
655 }
656
45c51519 657 g_array_append_val(enum_fc->mappings, mapping);
3f7d4d90 658 BT_LIB_LOGD("Added mapping to enumeration field class: "
45c51519 659 "%![fc-]+F, label=\"%s\"", fc, label);
5cd6d0e5
PP
660
661end:
45c51519 662 return status;
5cd6d0e5
PP
663}
664
45c51519 665enum bt_field_class_enumeration_add_mapping_status
9c08c816 666bt_field_class_enumeration_unsigned_add_mapping(
40f4ba76 667 struct bt_field_class *fc, const char *label,
45c51519 668 const struct bt_integer_range_set_unsigned *range_set)
5cd6d0e5 669{
5cd6d0e5 670 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 671 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
5cd6d0e5 672 "Field class");
45c51519
PP
673 return add_mapping_to_enumeration_field_class(fc, label,
674 (const void *) range_set);
675}
676
677enum bt_field_class_enumeration_add_mapping_status
9c08c816 678bt_field_class_enumeration_signed_add_mapping(
40f4ba76 679 struct bt_field_class *fc, const char *label,
45c51519 680 const struct bt_integer_range_set_signed *range_set)
5cd6d0e5 681{
5cd6d0e5 682 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
864cad70 683 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
5cd6d0e5 684 "Field class");
45c51519
PP
685 return add_mapping_to_enumeration_field_class(fc, label,
686 (const void *) range_set);
5cd6d0e5
PP
687}
688
689static
690void destroy_real_field_class(struct bt_object *obj)
691{
692 BT_ASSERT(obj);
e5be10ef 693 BT_LIB_LOGD("Destroying real field class object: %!+F", obj);
c6962c96 694 finalize_field_class((void *) obj);
5cd6d0e5
PP
695 g_free(obj);
696}
697
fe4df857
FD
698static
699struct bt_field_class *create_real_field_class(bt_trace_class *trace_class,
700 enum bt_field_class_type type)
5cd6d0e5
PP
701{
702 struct bt_field_class_real *real_fc = NULL;
703
1122a43a 704 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
fe4df857
FD
705 BT_LOGD("Creating default real field class object: type=%s",
706 bt_common_field_class_type_string(type));
5cd6d0e5
PP
707 real_fc = g_new0(struct bt_field_class_real, 1);
708 if (!real_fc) {
870631a2 709 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
5cd6d0e5
PP
710 goto error;
711 }
712
fe4df857 713 if (init_field_class((void *) real_fc, type, destroy_real_field_class)) {
c6962c96
PP
714 goto error;
715 }
716
e5be10ef 717 BT_LIB_LOGD("Created real field class object: %!+F", real_fc);
5cd6d0e5
PP
718 goto end;
719
720error:
65300d60 721 BT_OBJECT_PUT_REF_AND_RESET(real_fc);
5cd6d0e5
PP
722
723end:
724 return (void *) real_fc;
725}
726
fe4df857
FD
727struct bt_field_class *bt_field_class_real_single_precision_create(
728 bt_trace_class *trace_class)
5cd6d0e5 729{
fe4df857
FD
730 return create_real_field_class(trace_class,
731 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL);
5cd6d0e5
PP
732}
733
fe4df857
FD
734struct bt_field_class *bt_field_class_real_double_precision_create(
735 bt_trace_class *trace_class)
5cd6d0e5 736{
fe4df857
FD
737 return create_real_field_class(trace_class,
738 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL);
5cd6d0e5
PP
739}
740
741static
742int init_named_field_classes_container(
743 struct bt_field_class_named_field_class_container *fc,
40f4ba76 744 enum bt_field_class_type type,
45c51519
PP
745 bt_object_release_func fc_release_func,
746 GDestroyNotify named_fc_destroy_func)
5cd6d0e5
PP
747{
748 int ret = 0;
749
c6962c96
PP
750 ret = init_field_class((void *) fc, type, fc_release_func);
751 if (ret) {
752 goto end;
753 }
754
45c51519 755 fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func);
5cd6d0e5 756 if (!fc->named_fcs) {
45c51519 757 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
5cd6d0e5
PP
758 ret = -1;
759 goto end;
760 }
761
762 fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal);
763 if (!fc->name_to_index) {
870631a2 764 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
5cd6d0e5
PP
765 ret = -1;
766 goto end;
767 }
768
769end:
770 return ret;
771}
772
773static
774void finalize_named_field_class(struct bt_named_field_class *named_fc)
775{
776 BT_ASSERT(named_fc);
e5be10ef 777 BT_LIB_LOGD("Finalizing named field class: "
5cd6d0e5
PP
778 "addr=%p, name=\"%s\", %![fc-]+F",
779 named_fc, named_fc->name ? named_fc->name->str : NULL,
780 named_fc->fc);
c6962c96 781 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
5cd6d0e5
PP
782
783 if (named_fc->name) {
784 g_string_free(named_fc->name, TRUE);
45c51519 785 named_fc->name = NULL;
5cd6d0e5
PP
786 }
787
e5be10ef 788 BT_LOGD_STR("Putting named field class's field class.");
238b7404 789 BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc);
5cd6d0e5
PP
790}
791
45c51519
PP
792static
793void destroy_named_field_class(gpointer ptr)
794{
c6962c96
PP
795 struct bt_named_field_class *named_fc = ptr;
796
45c51519 797 if (ptr) {
c7d2f7b6 798 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
45c51519
PP
799 finalize_named_field_class(ptr);
800 g_free(ptr);
801 }
802}
803
804static
de821fe5 805void destroy_variant_with_selector_field_option(gpointer ptr)
45c51519 806{
de821fe5 807 struct bt_field_class_variant_with_selector_field_option *opt = ptr;
45c51519
PP
808
809 if (ptr) {
810 finalize_named_field_class(&opt->common);
811 BT_OBJECT_PUT_REF_AND_RESET(opt->range_set);
812 g_free(ptr);
813 }
814}
815
5cd6d0e5
PP
816static
817void finalize_named_field_classes_container(
818 struct bt_field_class_named_field_class_container *fc)
819{
5cd6d0e5
PP
820 BT_ASSERT(fc);
821
822 if (fc->named_fcs) {
45c51519
PP
823 g_ptr_array_free(fc->named_fcs, TRUE);
824 fc->named_fcs = NULL;
5cd6d0e5 825
5cd6d0e5
PP
826 }
827
828 if (fc->name_to_index) {
829 g_hash_table_destroy(fc->name_to_index);
45c51519 830 fc->name_to_index = NULL;
5cd6d0e5
PP
831 }
832}
833
834static
835void destroy_structure_field_class(struct bt_object *obj)
836{
837 BT_ASSERT(obj);
1998d1ab 838 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj);
c6962c96 839 finalize_field_class((void *) obj);
5cd6d0e5
PP
840 finalize_named_field_classes_container((void *) obj);
841 g_free(obj);
842}
843
1122a43a
PP
844struct bt_field_class *bt_field_class_structure_create(
845 bt_trace_class *trace_class)
5cd6d0e5
PP
846{
847 int ret;
848 struct bt_field_class_structure *struct_fc = NULL;
849
1122a43a 850 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 851 BT_LOGD_STR("Creating default structure field class object.");
5cd6d0e5
PP
852 struct_fc = g_new0(struct bt_field_class_structure, 1);
853 if (!struct_fc) {
870631a2
PP
854 BT_LIB_LOGE_APPEND_CAUSE(
855 "Failed to allocate one structure field class.");
5cd6d0e5
PP
856 goto error;
857 }
858
859 ret = init_named_field_classes_container((void *) struct_fc,
45c51519
PP
860 BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class,
861 destroy_named_field_class);
5cd6d0e5 862 if (ret) {
45c51519 863 /* init_named_field_classes_container() logs errors */
5cd6d0e5
PP
864 goto error;
865 }
866
e5be10ef 867 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc);
5cd6d0e5
PP
868 goto end;
869
870error:
65300d60 871 BT_OBJECT_PUT_REF_AND_RESET(struct_fc);
5cd6d0e5
PP
872
873end:
874 return (void *) struct_fc;
875}
876
877static
45c51519 878int init_named_field_class(struct bt_named_field_class *named_fc,
5cd6d0e5
PP
879 const char *name, struct bt_field_class *fc)
880{
45c51519 881 int status = BT_FUNC_STATUS_OK;
5cd6d0e5 882
45c51519
PP
883 BT_ASSERT(named_fc);
884 BT_ASSERT(name);
885 BT_ASSERT(fc);
886 named_fc->name = g_string_new(name);
887 if (!named_fc->name) {
870631a2 888 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
45c51519 889 status = BT_FUNC_STATUS_MEMORY_ERROR;
5cd6d0e5
PP
890 goto end;
891 }
892
c6962c96
PP
893 named_fc->user_attributes = bt_value_map_create();
894 if (!named_fc->user_attributes) {
895 BT_LIB_LOGE_APPEND_CAUSE(
896 "Failed to create a map value object.");
897 status = BT_FUNC_STATUS_MEMORY_ERROR;
898 goto end;
899 }
900
398454ed 901 named_fc->fc = fc;
6871026b 902 bt_object_get_ref_no_null_check(named_fc->fc);
1e6fd1d7 903
45c51519
PP
904end:
905 return status;
906}
907
908static
909struct bt_named_field_class *create_named_field_class(const char *name,
910 struct bt_field_class *fc)
911{
912 struct bt_named_field_class *named_fc = g_new0(
913 struct bt_named_field_class, 1);
914
915 if (!named_fc) {
916 BT_LIB_LOGE_APPEND_CAUSE(
917 "Failed to allocate a named field class.");
918 goto error;
919 }
920
921 if (init_named_field_class(named_fc, name, fc)) {
922 /* init_named_field_class() logs errors */
923 goto error;
924 }
925
926 goto end;
927
928error:
929 destroy_named_field_class(named_fc);
930 named_fc = NULL;
5cd6d0e5
PP
931
932end:
45c51519
PP
933 return named_fc;
934}
935
936static
de821fe5
PP
937struct bt_field_class_variant_with_selector_field_option *
938create_variant_with_selector_field_option(
45c51519
PP
939 const char *name, struct bt_field_class *fc,
940 const struct bt_integer_range_set *range_set)
941{
de821fe5
PP
942 struct bt_field_class_variant_with_selector_field_option *opt = g_new0(
943 struct bt_field_class_variant_with_selector_field_option, 1);
45c51519
PP
944
945 BT_ASSERT(range_set);
946
947 if (!opt) {
948 BT_LIB_LOGE_APPEND_CAUSE(
949 "Failed to allocate a named field class.");
950 goto error;
951 }
952
953 if (init_named_field_class(&opt->common, name, fc)) {
954 goto error;
955 }
956
957 opt->range_set = range_set;
6871026b 958 bt_object_get_ref_no_null_check(opt->range_set);
45c51519
PP
959 bt_integer_range_set_freeze(range_set);
960 goto end;
961
962error:
de821fe5 963 destroy_variant_with_selector_field_option(opt);
45c51519
PP
964 opt = NULL;
965
966end:
967 return opt;
968}
969
970static
971int append_named_field_class_to_container_field_class(
972 struct bt_field_class_named_field_class_container *container_fc,
973 struct bt_named_field_class *named_fc)
974{
975 BT_ASSERT(container_fc);
976 BT_ASSERT(named_fc);
977 BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class");
978 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index,
979 named_fc->name->str),
980 "Duplicate member/option name in structure/variant field class: "
981 "%![container-fc-]+F, name=\"%s\"", container_fc,
982 named_fc->name->str);
c6962c96
PP
983
984 /*
985 * Freeze the contained field class, but not the named field
986 * class itself, as it's still possible afterwards to modify
987 * properties of the member/option object.
988 */
989 bt_field_class_freeze(named_fc->fc);
45c51519
PP
990 g_ptr_array_add(container_fc->named_fcs, named_fc);
991 g_hash_table_insert(container_fc->name_to_index, named_fc->name->str,
992 GUINT_TO_POINTER(container_fc->named_fcs->len - 1));
993 return BT_FUNC_STATUS_OK;
5cd6d0e5
PP
994}
995
d24d5663
PP
996enum bt_field_class_structure_append_member_status
997bt_field_class_structure_append_member(
4295b9e0
PP
998 struct bt_field_class *fc, const char *name,
999 struct bt_field_class *member_fc)
5cd6d0e5 1000{
45c51519
PP
1001 enum bt_field_class_structure_append_member_status status;
1002 struct bt_named_field_class *named_fc = NULL;
e5be10ef 1003
5cd6d0e5 1004 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
740faaf4
PP
1005 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1006 "Field class");
45c51519
PP
1007 named_fc = create_named_field_class(name, member_fc);
1008 if (!named_fc) {
1009 /* create_named_field_class() logs errors */
1010 status = BT_FUNC_STATUS_MEMORY_ERROR;
1011 goto end;
1012 }
1013
1014 status = append_named_field_class_to_container_field_class((void *) fc,
1015 named_fc);
1016 if (status == BT_FUNC_STATUS_OK) {
1017 /* Moved to the container */
1018 named_fc = NULL;
1019 }
1020
1021end:
1022 return status;
5cd6d0e5
PP
1023}
1024
40f4ba76
PP
1025uint64_t bt_field_class_structure_get_member_count(
1026 const struct bt_field_class *fc)
5cd6d0e5
PP
1027{
1028 struct bt_field_class_structure *struct_fc = (void *) fc;
1029
bdb288b3
PP
1030 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1031 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 1032 "Field class");
5cd6d0e5
PP
1033 return (uint64_t) struct_fc->common.named_fcs->len;
1034}
1035
1036static
1e6fd1d7
PP
1037struct bt_named_field_class *
1038borrow_named_field_class_from_container_field_class_at_index(
740faaf4 1039 struct bt_field_class_named_field_class_container *fc,
1e6fd1d7 1040 uint64_t index)
5cd6d0e5 1041{
98b15851 1042 BT_ASSERT_DBG(fc);
bdb288b3 1043 BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len);
45c51519 1044 return fc->named_fcs->pdata[index];
5cd6d0e5
PP
1045}
1046
1e6fd1d7
PP
1047const struct bt_field_class_structure_member *
1048bt_field_class_structure_borrow_member_by_index_const(
1049 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 1050{
bdb288b3
PP
1051 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1052 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 1053 "Field class");
1e6fd1d7
PP
1054 return (const void *)
1055 borrow_named_field_class_from_container_field_class_at_index(
1056 (void *) fc, index);
740faaf4
PP
1057}
1058
1e6fd1d7
PP
1059struct bt_field_class_structure_member *
1060bt_field_class_structure_borrow_member_by_index(
1061 struct bt_field_class *fc, uint64_t index)
740faaf4 1062{
bdb288b3
PP
1063 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1064 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 1065 "Field class");
1e6fd1d7
PP
1066 return (void *)
1067 borrow_named_field_class_from_container_field_class_at_index(
1068 (void *) fc, index);
e5be10ef
PP
1069}
1070
5cd6d0e5 1071static
1e6fd1d7
PP
1072struct bt_named_field_class *
1073borrow_named_field_class_from_container_field_class_by_name(
740faaf4 1074 struct bt_field_class_named_field_class_container *fc,
5cd6d0e5
PP
1075 const char *name)
1076{
1e6fd1d7 1077 struct bt_named_field_class *named_fc = NULL;
5cd6d0e5
PP
1078 gpointer orig_key;
1079 gpointer value;
1080
98b15851 1081 BT_ASSERT_DBG(fc);
bdb288b3 1082 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
5cd6d0e5
PP
1083 if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key,
1084 &value)) {
1085 goto end;
1086 }
1087
45c51519 1088 named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)];
5cd6d0e5
PP
1089
1090end:
1e6fd1d7 1091 return named_fc;
5cd6d0e5
PP
1092}
1093
1e6fd1d7 1094const struct bt_field_class_structure_member *
7c06e353 1095bt_field_class_structure_borrow_member_by_name_const(
40f4ba76 1096 const struct bt_field_class *fc, const char *name)
5cd6d0e5 1097{
bdb288b3
PP
1098 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1099 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 1100 "Field class");
1e6fd1d7
PP
1101 return (const void *)
1102 borrow_named_field_class_from_container_field_class_by_name(
740faaf4
PP
1103 (void *) fc, name);
1104}
1105
1e6fd1d7 1106struct bt_field_class_structure_member *
fffedc87 1107bt_field_class_structure_borrow_member_by_name(
740faaf4
PP
1108 struct bt_field_class *fc, const char *name)
1109{
bdb288b3
PP
1110 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1111 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
740faaf4 1112 "Field class");
1e6fd1d7
PP
1113 return (void *)
1114 borrow_named_field_class_from_container_field_class_by_name(
40f4ba76 1115 (void *) fc, name);
e5be10ef
PP
1116}
1117
1e6fd1d7
PP
1118const char *bt_field_class_structure_member_get_name(
1119 const struct bt_field_class_structure_member *member)
1120{
1121 const struct bt_named_field_class *named_fc = (const void *) member;
1122
bdb288b3 1123 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1e6fd1d7
PP
1124 return named_fc->name->str;
1125}
1126
1127const struct bt_field_class *
1128bt_field_class_structure_member_borrow_field_class_const(
1129 const struct bt_field_class_structure_member *member)
1130{
1131 const struct bt_named_field_class *named_fc = (const void *) member;
1132
bdb288b3 1133 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1e6fd1d7
PP
1134 return named_fc->fc;
1135}
1136
bcd7ef6f
FD
1137struct bt_field_class *
1138bt_field_class_structure_member_borrow_field_class(
1139 struct bt_field_class_structure_member *member)
1140{
1141 struct bt_named_field_class *named_fc = (void *) member;
1142
1143 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1144 return named_fc->fc;
1145}
1146
b38aea74
PP
1147static
1148void destroy_option_field_class(struct bt_object *obj)
1149{
1150 struct bt_field_class_option *fc = (void *) obj;
1151
1152 BT_ASSERT(fc);
1153 BT_LIB_LOGD("Destroying option field class object: %!+F", fc);
c6962c96 1154 finalize_field_class((void *) obj);
b38aea74
PP
1155 BT_LOGD_STR("Putting content field class.");
1156 BT_OBJECT_PUT_REF_AND_RESET(fc->content_fc);
0aa006b7 1157
de821fe5
PP
1158 if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD) {
1159 struct bt_field_class_option_with_selector_field *with_sel_fc =
0aa006b7
PP
1160 (void *) obj;
1161
1162 BT_LOGD_STR("Putting selector field path.");
1163 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_field_path);
1164 BT_LOGD_STR("Putting selector field class.");
1165 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_fc);
1166
de821fe5
PP
1167 if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) {
1168 struct bt_field_class_option_with_selector_field_integer *with_int_sel_fc =
0aa006b7
PP
1169 (void *) obj;
1170
1171 BT_LOGD_STR("Putting integer range set.");
1172 BT_OBJECT_PUT_REF_AND_RESET(with_int_sel_fc->range_set);
1173 }
1174 }
1175
b38aea74
PP
1176 g_free(fc);
1177}
1178
0aa006b7
PP
1179static
1180struct bt_field_class *create_option_field_class(
1181 struct bt_trace_class *trace_class,
1182 enum bt_field_class_type fc_type,
1183 struct bt_field_class *content_fc,
1184 struct bt_field_class *selector_fc)
b38aea74
PP
1185{
1186 struct bt_field_class_option *opt_fc = NULL;
1187
1188 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1189 BT_ASSERT_PRE_NON_NULL(content_fc, "Content field class");
1190 BT_LIB_LOGD("Creating option field class: "
0aa006b7
PP
1191 "type=%s, %![content-fc-]+F, %![sel-fc-]+F",
1192 bt_common_field_class_type_string(fc_type),
1193 content_fc, selector_fc);
1194
de821fe5
PP
1195 if (fc_type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD) {
1196 struct bt_field_class_option_with_selector_field *opt_with_sel_fc = NULL;
0aa006b7
PP
1197
1198 BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class");
1199
de821fe5 1200 if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) {
0aa006b7
PP
1201 BT_ASSERT_PRE_FC_HAS_ID(selector_fc,
1202 BT_FIELD_CLASS_TYPE_BOOL,
1203 "Selector field class");
1204 opt_with_sel_fc = (void *) g_new0(
de821fe5 1205 struct bt_field_class_option_with_selector_field_bool, 1);
0aa006b7
PP
1206 } else {
1207 BT_ASSERT_PRE_FC_IS_INT(selector_fc,
1208 "Selector field class");
1209 opt_with_sel_fc = (void *) g_new0(
de821fe5 1210 struct bt_field_class_option_with_selector_field_integer, 1);
0aa006b7
PP
1211 }
1212
1213 if (!opt_with_sel_fc) {
1214 BT_LIB_LOGE_APPEND_CAUSE(
1215 "Failed to allocate one option with selector field class.");
1216 goto error;
1217 }
1218
1219 opt_with_sel_fc->selector_fc = selector_fc;
6871026b 1220 bt_object_get_ref_no_null_check(opt_with_sel_fc->selector_fc);
0aa006b7
PP
1221 opt_fc = (void *) opt_with_sel_fc;
1222 } else {
1223 opt_fc = g_new0(struct bt_field_class_option, 1);
1224 if (!opt_fc) {
1225 BT_LIB_LOGE_APPEND_CAUSE(
1226 "Failed to allocate one option field class.");
1227 goto error;
1228 }
b38aea74
PP
1229 }
1230
0aa006b7
PP
1231 BT_ASSERT(opt_fc);
1232
1233 if (init_field_class((void *) opt_fc, fc_type,
c6962c96
PP
1234 destroy_option_field_class)) {
1235 goto error;
1236 }
1237
b38aea74 1238 opt_fc->content_fc = content_fc;
6871026b 1239 bt_object_get_ref_no_null_check(opt_fc->content_fc);
b38aea74
PP
1240 bt_field_class_freeze(opt_fc->content_fc);
1241
1242 if (selector_fc) {
b38aea74
PP
1243 bt_field_class_freeze(selector_fc);
1244 }
1245
1246 BT_LIB_LOGD("Created option field class object: "
1247 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc, selector_fc);
1248 goto end;
1249
1250error:
1251 BT_OBJECT_PUT_REF_AND_RESET(opt_fc);
1252
1253end:
1254 return (void *) opt_fc;
1255}
1256
0aa006b7
PP
1257struct bt_field_class *bt_field_class_option_without_selector_create(
1258 struct bt_trace_class *trace_class,
1259 struct bt_field_class *content_fc)
1260{
1261 return create_option_field_class(trace_class,
de821fe5 1262 BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD,
0aa006b7
PP
1263 content_fc, NULL);
1264}
1265
de821fe5 1266struct bt_field_class *bt_field_class_option_with_selector_field_bool_create(
0aa006b7
PP
1267 struct bt_trace_class *trace_class,
1268 struct bt_field_class *content_fc,
1269 struct bt_field_class *selector_fc)
1270{
de821fe5 1271 struct bt_field_class_option_with_selector_field_bool *fc =
0aa006b7 1272 (void *) create_option_field_class(trace_class,
de821fe5 1273 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD,
0aa006b7
PP
1274 content_fc, selector_fc);
1275
1276 if (!fc) {
1277 goto end;
1278 }
1279
1280end:
1281 return (void *) fc;
1282}
1283
1284struct bt_field_class *
de821fe5 1285bt_field_class_option_with_selector_field_integer_unsigned_create(
0aa006b7
PP
1286 struct bt_trace_class *trace_class,
1287 struct bt_field_class *content_fc,
1288 struct bt_field_class *selector_fc,
1289 const struct bt_integer_range_set_unsigned *u_range_set)
1290{
de821fe5 1291 struct bt_field_class_option_with_selector_field_integer *fc;
0aa006b7
PP
1292 const struct bt_integer_range_set *range_set =
1293 (const void *) u_range_set;
1294
1295 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
1296 BT_ASSERT_PRE(range_set->ranges->len > 0,
1297 "Integer range set is empty: %!+R", range_set);
1298 fc = (void *) create_option_field_class(trace_class,
de821fe5 1299 BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD,
0aa006b7
PP
1300 content_fc, selector_fc);
1301
1302 if (!fc) {
1303 goto end;
1304 }
1305
1306 fc->range_set = range_set;
6871026b 1307 bt_object_get_ref_no_null_check(fc->range_set);
0aa006b7
PP
1308 bt_integer_range_set_freeze(range_set);
1309
1310end:
1311 return (void *) fc;
1312}
1313
1314struct bt_field_class *
de821fe5 1315bt_field_class_option_with_selector_field_integer_signed_create(
0aa006b7
PP
1316 struct bt_trace_class *trace_class,
1317 struct bt_field_class *content_fc,
1318 struct bt_field_class *selector_fc,
1319 const struct bt_integer_range_set_signed *i_range_set)
1320{
de821fe5 1321 struct bt_field_class_option_with_selector_field_integer *fc;
0aa006b7
PP
1322 const struct bt_integer_range_set *range_set =
1323 (const void *) i_range_set;
1324
1325 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
1326 BT_ASSERT_PRE(range_set->ranges->len > 0,
1327 "Integer range set is empty: %!+R", range_set);
1328 fc = (void *) create_option_field_class(trace_class,
de821fe5 1329 BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD,
0aa006b7
PP
1330 content_fc, selector_fc);
1331
1332 if (!fc) {
1333 goto end;
1334 }
1335
1336 fc->range_set = range_set;
6871026b 1337 bt_object_get_ref_no_null_check(fc->range_set);
0aa006b7
PP
1338 bt_integer_range_set_freeze(range_set);
1339
1340end:
1341 return (void *) fc;
1342}
1343
b38aea74
PP
1344const struct bt_field_class *bt_field_class_option_borrow_field_class_const(
1345 const struct bt_field_class *fc)
1346{
1347 struct bt_field_class_option *opt_fc = (void *) fc;
1348
1349 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
0aa006b7 1350 BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class");
b38aea74
PP
1351 return opt_fc->content_fc;
1352}
1353
bcd7ef6f
FD
1354struct bt_field_class *bt_field_class_option_borrow_field_class(
1355 struct bt_field_class *fc)
1356{
1357 struct bt_field_class_option *opt_fc = (void *) fc;
1358
1359 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
0aa006b7 1360 BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class");
bcd7ef6f
FD
1361 return opt_fc->content_fc;
1362}
1363
b38aea74 1364const struct bt_field_path *
de821fe5 1365bt_field_class_option_with_selector_field_borrow_selector_field_path_const(
b38aea74
PP
1366 const struct bt_field_class *fc)
1367{
de821fe5 1368 const struct bt_field_class_option_with_selector_field *opt_fc =
0aa006b7 1369 (const void *) fc;
b38aea74
PP
1370
1371 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
0aa006b7 1372 BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(fc, "Field class");
b38aea74
PP
1373 return opt_fc->selector_field_path;
1374}
1375
de821fe5 1376void bt_field_class_option_with_selector_field_bool_set_selector_is_reversed(
0aa006b7
PP
1377 struct bt_field_class *fc, bt_bool sel_is_reversed)
1378{
de821fe5 1379 struct bt_field_class_option_with_selector_field_bool *opt_fc = (void *) fc;
0aa006b7
PP
1380
1381 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1382 BT_ASSERT_PRE_FC_HAS_ID(fc,
de821fe5 1383 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD, "Field class");
0aa006b7
PP
1384 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
1385 opt_fc->sel_is_reversed = sel_is_reversed;
1386}
1387
de821fe5 1388bt_bool bt_field_class_option_with_selector_field_bool_selector_is_reversed(
0aa006b7
PP
1389 const struct bt_field_class *fc)
1390{
de821fe5 1391 struct bt_field_class_option_with_selector_field_bool *opt_fc = (void *) fc;
0aa006b7
PP
1392
1393 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1394 BT_ASSERT_PRE_FC_HAS_ID(fc,
de821fe5 1395 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD, "Field class");
0aa006b7
PP
1396 return opt_fc->sel_is_reversed;
1397}
1398
1399const struct bt_integer_range_set_unsigned *
de821fe5 1400bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(
0aa006b7
PP
1401 const struct bt_field_class *fc)
1402{
de821fe5 1403 struct bt_field_class_option_with_selector_field_integer *opt_fc =
0aa006b7
PP
1404 (void *) fc;
1405
1406 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1407 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class");
1408 return (const void *) opt_fc->range_set;
1409}
1410
1411const struct bt_integer_range_set_signed *
de821fe5 1412bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(
0aa006b7
PP
1413 const struct bt_field_class *fc)
1414{
de821fe5 1415 struct bt_field_class_option_with_selector_field_integer *opt_fc =
0aa006b7
PP
1416 (void *) fc;
1417
1418 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1419 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class");
1420 return (const void *) opt_fc->range_set;
1421}
1422
45c51519
PP
1423static
1424void finalize_variant_field_class(struct bt_field_class_variant *var_fc)
1e6fd1d7 1425{
45c51519
PP
1426 BT_ASSERT(var_fc);
1427 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc);
c6962c96 1428 finalize_field_class((void *) var_fc);
45c51519 1429 finalize_named_field_classes_container((void *) var_fc);
1e6fd1d7
PP
1430}
1431
5cd6d0e5
PP
1432static
1433void destroy_variant_field_class(struct bt_object *obj)
1434{
1435 struct bt_field_class_variant *fc = (void *) obj;
1436
1437 BT_ASSERT(fc);
45c51519
PP
1438 finalize_variant_field_class(fc);
1439 g_free(fc);
1440}
1441
1442static
de821fe5 1443void destroy_variant_with_selector_field_field_class(struct bt_object *obj)
45c51519 1444{
de821fe5 1445 struct bt_field_class_variant_with_selector_field *fc = (void *) obj;
45c51519
PP
1446
1447 BT_ASSERT(fc);
1448 finalize_variant_field_class(&fc->common);
5cd6d0e5 1449 BT_LOGD_STR("Putting selector field path.");
238b7404
PP
1450 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
1451 BT_LOGD_STR("Putting selector field class.");
1452 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
5cd6d0e5
PP
1453 g_free(fc);
1454}
1455
1122a43a 1456struct bt_field_class *bt_field_class_variant_create(
45c51519 1457 bt_trace_class *trace_class, bt_field_class *selector_fc)
5cd6d0e5
PP
1458{
1459 int ret;
1460 struct bt_field_class_variant *var_fc = NULL;
de821fe5 1461 struct bt_field_class_variant_with_selector_field *var_with_sel_fc = NULL;
45c51519 1462 enum bt_field_class_type fc_type;
5cd6d0e5 1463
1122a43a 1464 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
45c51519
PP
1465
1466 if (selector_fc) {
1467 BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class");
5cd6d0e5
PP
1468 }
1469
45c51519
PP
1470 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1471 selector_fc);
1472
1473 if (selector_fc) {
1474 var_with_sel_fc = g_new0(
de821fe5 1475 struct bt_field_class_variant_with_selector_field, 1);
45c51519
PP
1476 if (!var_with_sel_fc) {
1477 BT_LIB_LOGE_APPEND_CAUSE(
1478 "Failed to allocate one variant field class with selector.");
1479 goto error;
1480 }
1481
ebdb6693
PP
1482 if (bt_field_class_type_is(selector_fc->type,
1483 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
de821fe5 1484 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD;
45c51519 1485 } else {
de821fe5 1486 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD;
45c51519
PP
1487 }
1488
1489 ret = init_named_field_classes_container(
1490 (void *) var_with_sel_fc, fc_type,
de821fe5
PP
1491 destroy_variant_with_selector_field_field_class,
1492 destroy_variant_with_selector_field_option);
45c51519
PP
1493 if (ret) {
1494 /* init_named_field_classes_container() logs errors */
1495 goto error;
1496 }
1497
1498 var_with_sel_fc->selector_fc = selector_fc;
6871026b 1499 bt_object_get_ref_no_null_check(var_with_sel_fc->selector_fc);
45c51519
PP
1500 bt_field_class_freeze(selector_fc);
1501 var_fc = (void *) var_with_sel_fc;
d166032f
FD
1502 BT_LIB_LOGD("Created default variant field class with selector object: "
1503 "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc);
45c51519
PP
1504 } else {
1505 var_fc = g_new0(struct bt_field_class_variant, 1);
1506 if (!var_fc) {
1507 BT_LIB_LOGE_APPEND_CAUSE(
1508 "Failed to allocate one variant field class without selector.");
1509 goto error;
1510 }
1511
1512 ret = init_named_field_classes_container((void *) var_fc,
de821fe5 1513 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD,
45c51519
PP
1514 destroy_variant_field_class, destroy_named_field_class);
1515 if (ret) {
1516 /* init_named_field_classes_container() logs errors */
1517 goto error;
1518 }
d166032f
FD
1519 BT_LIB_LOGD("Created default variant field class without selector object: "
1520 "%![var-fc-]+F", var_fc);
5cd6d0e5
PP
1521 }
1522
45c51519 1523 BT_ASSERT(var_fc);
5cd6d0e5
PP
1524 goto end;
1525
1526error:
65300d60 1527 BT_OBJECT_PUT_REF_AND_RESET(var_fc);
9fdff45c 1528 BT_OBJECT_PUT_REF_AND_RESET(var_with_sel_fc);
5cd6d0e5
PP
1529
1530end:
1531 return (void *) var_fc;
1532}
1533
45c51519
PP
1534enum bt_field_class_variant_without_selector_append_option_status
1535bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc,
1536 const char *name, struct bt_field_class *option_fc)
1537{
1538 enum bt_field_class_variant_without_selector_append_option_status status;
1539 struct bt_named_field_class *named_fc = NULL;
1540
1541 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1542 BT_ASSERT_PRE_NON_NULL(name, "Name");
1543 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
1544 BT_ASSERT_PRE_FC_HAS_ID(fc,
de821fe5 1545 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD, "Field class");
45c51519
PP
1546 named_fc = create_named_field_class(name, option_fc);
1547 if (!named_fc) {
1548 /* create_named_field_class() logs errors */
1549 status = BT_FUNC_STATUS_MEMORY_ERROR;
1550 goto end;
1551 }
1552
1553 status = append_named_field_class_to_container_field_class((void *) fc,
1554 named_fc);
1555 if (status == BT_FUNC_STATUS_OK) {
1556 /* Moved to the container */
1557 named_fc = NULL;
1558 }
1559
1560end:
1561 if (named_fc) {
1562 destroy_named_field_class(named_fc);
1563 }
1564
1565 return status;
5cd6d0e5
PP
1566}
1567
45c51519
PP
1568static
1569int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set,
1570 bool is_signed, bool *has_overlap)
1571{
1572 int status = BT_FUNC_STATUS_OK;
1573 struct bt_integer_range_set *full_range_set;
1574 uint64_t i;
1575
1576 *has_overlap = false;
1577
1578 /*
1579 * Build a single range set with all the ranges and test for
1580 * overlaps.
1581 */
1582 if (is_signed) {
1583 full_range_set = (void *) bt_integer_range_set_signed_create();
1584 } else {
1585 full_range_set = (void *) bt_integer_range_set_unsigned_create();
1586 }
1587
1588 if (!full_range_set) {
1589 BT_LOGE_STR("Failed to create a range set.");
1590 status = BT_FUNC_STATUS_MEMORY_ERROR;
1591 goto end;
1592 }
1593
1594 /* Add existing option ranges */
1595 for (i = 0; i < var_fc_opts->len; i++) {
de821fe5 1596 struct bt_field_class_variant_with_selector_field_option *opt =
45c51519
PP
1597 var_fc_opts->pdata[i];
1598 uint64_t j;
1599
1600 for (j = 0; j < opt->range_set->ranges->len; j++) {
1601 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1602 opt->range_set, j);
1603
1604 if (is_signed) {
1605 status = bt_integer_range_set_signed_add_range(
1606 (void *) full_range_set, range->lower.i,
1607 range->upper.i);
1608 } else {
1609 status = bt_integer_range_set_unsigned_add_range(
1610 (void *) full_range_set, range->lower.u,
1611 range->upper.u);
1612 }
1613
1614 if (status) {
1615 goto end;
1616 }
1617 }
1618 }
1619
1620 /* Add new ranges */
1621 for (i = 0; i < range_set->ranges->len; i++) {
1622 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1623 range_set, i);
1624
1625 if (is_signed) {
1626 status = bt_integer_range_set_signed_add_range(
1627 (void *) full_range_set, range->lower.i,
1628 range->upper.i);
1629 } else {
1630 status = bt_integer_range_set_unsigned_add_range(
1631 (void *) full_range_set, range->lower.u,
1632 range->upper.u);
1633 }
1634
1635 if (status) {
1636 goto end;
1637 }
1638 }
1639
1640 /* Check overlaps */
1641 if (is_signed) {
1642 *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set);
1643 } else {
1644 *has_overlap = bt_integer_range_set_unsigned_has_overlaps(
1645 full_range_set);
1646 }
1647
1648end:
1649 bt_object_put_ref(full_range_set);
1650 return status;
1651}
1652
1653static
de821fe5 1654int append_option_to_variant_with_selector_field_field_class(
45c51519
PP
1655 struct bt_field_class *fc, const char *name,
1656 struct bt_field_class *option_fc,
1657 const struct bt_integer_range_set *range_set,
1658 enum bt_field_class_type expected_type)
5cd6d0e5 1659{
45c51519 1660 int status;
de821fe5
PP
1661 struct bt_field_class_variant_with_selector_field *var_fc = (void *) fc;
1662 struct bt_field_class_variant_with_selector_field_option *opt = NULL;
45c51519 1663 bool has_overlap;
e5be10ef 1664
5cd6d0e5 1665 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
45c51519
PP
1666 BT_ASSERT_PRE_NON_NULL(name, "Name");
1667 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
6769570a 1668 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
45c51519
PP
1669 BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class");
1670 BT_ASSERT_PRE(range_set->ranges->len > 0,
6769570a 1671 "Integer range set is empty: %!+R", range_set);
45c51519 1672 status = ranges_overlap(var_fc->common.common.named_fcs, range_set,
de821fe5 1673 expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD,
45c51519
PP
1674 &has_overlap);
1675 if (status) {
1676 /* ranges_overlap() logs errors */
1677 goto end;
1678 }
1679
1680 BT_ASSERT_PRE(!has_overlap,
6769570a
PP
1681 "Integer range set's ranges and existing ranges have an overlap: "
1682 "%!+R", range_set);
de821fe5 1683 opt = create_variant_with_selector_field_option(name, option_fc, range_set);
45c51519 1684 if (!opt) {
de821fe5 1685 /* create_variant_with_selector_field_option() logs errors */
45c51519
PP
1686 status = BT_FUNC_STATUS_MEMORY_ERROR;
1687 goto end;
1688 }
1689
1690 status = append_named_field_class_to_container_field_class((void *) fc,
1691 &opt->common);
1692 if (status == BT_FUNC_STATUS_OK) {
1693 /* Moved to the container */
1694 opt = NULL;
1695 }
1696
1697end:
1698 if (opt) {
de821fe5 1699 destroy_variant_with_selector_field_option(opt);
45c51519
PP
1700 }
1701
1702 return status;
1703}
1704
de821fe5
PP
1705enum bt_field_class_variant_with_selector_field_integer_append_option_status
1706bt_field_class_variant_with_selector_field_integer_unsigned_append_option(
45c51519
PP
1707 struct bt_field_class *fc, const char *name,
1708 struct bt_field_class *option_fc,
1709 const struct bt_integer_range_set_unsigned *range_set)
1710{
de821fe5 1711 return append_option_to_variant_with_selector_field_field_class(fc,
45c51519 1712 name, option_fc, (const void *) range_set,
de821fe5 1713 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD);
45c51519
PP
1714}
1715
de821fe5
PP
1716enum bt_field_class_variant_with_selector_field_integer_append_option_status
1717bt_field_class_variant_with_selector_field_integer_signed_append_option(
45c51519
PP
1718 struct bt_field_class *fc, const char *name,
1719 struct bt_field_class *option_fc,
1720 const struct bt_integer_range_set_signed *range_set)
1721{
de821fe5 1722 return append_option_to_variant_with_selector_field_field_class(fc,
45c51519 1723 name, option_fc, (const void *) range_set,
de821fe5 1724 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD);
45c51519
PP
1725}
1726
1727uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc)
1728{
1729 const struct bt_field_class_variant *var_fc = (const void *) fc;
1730
1731 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1732 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1733 return (uint64_t) var_fc->common.named_fcs->len;
5cd6d0e5
PP
1734}
1735
1e6fd1d7 1736const struct bt_field_class_variant_option *
7c06e353 1737bt_field_class_variant_borrow_option_by_name_const(
40f4ba76 1738 const struct bt_field_class *fc, const char *name)
5cd6d0e5 1739{
bdb288b3 1740 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519 1741 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1e6fd1d7
PP
1742 return (const void *)
1743 borrow_named_field_class_from_container_field_class_by_name(
1744 (void *) fc, name);
740faaf4
PP
1745}
1746
45c51519
PP
1747const struct bt_field_class_variant_option *
1748bt_field_class_variant_borrow_option_by_index_const(
1749 const struct bt_field_class *fc, uint64_t index)
740faaf4 1750{
bdb288b3 1751 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519
PP
1752 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1753 return (const void *)
1754 borrow_named_field_class_from_container_field_class_at_index(
c6fe8c40
PP
1755 (void *) fc, index);
1756}
1757
1758struct bt_field_class_variant_option *
1759bt_field_class_variant_borrow_option_by_name(
1760 struct bt_field_class *fc, const char *name)
1761{
1762 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1763 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1764 return (void *)
1765 borrow_named_field_class_from_container_field_class_by_name(
1766 (void *) fc, name);
1767}
1768
1769struct bt_field_class_variant_option *
1770bt_field_class_variant_borrow_option_by_index(
1771 struct bt_field_class *fc, uint64_t index)
1772{
1773 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1774 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1775 return (void *)
1776 borrow_named_field_class_from_container_field_class_at_index(
45c51519 1777 (void *) fc, index);
e5be10ef
PP
1778}
1779
de821fe5
PP
1780const struct bt_field_class_variant_with_selector_field_integer_unsigned_option *
1781bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const(
45c51519 1782 const struct bt_field_class *fc, const char *name)
5cd6d0e5 1783{
bdb288b3 1784 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519 1785 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
de821fe5 1786 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD,
45c51519
PP
1787 "Field class");
1788 return (const void *)
1789 borrow_named_field_class_from_container_field_class_by_name(
1790 (void *) fc, name);
5cd6d0e5
PP
1791}
1792
de821fe5
PP
1793const struct bt_field_class_variant_with_selector_field_integer_unsigned_option *
1794bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
1e6fd1d7 1795 const struct bt_field_class *fc, uint64_t index)
5cd6d0e5 1796{
bdb288b3 1797 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519 1798 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
de821fe5 1799 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD,
45c51519 1800 "Field class");
1e6fd1d7
PP
1801 return (const void *)
1802 borrow_named_field_class_from_container_field_class_at_index(
1803 (void *) fc, index);
740faaf4
PP
1804}
1805
de821fe5
PP
1806const struct bt_field_class_variant_with_selector_field_integer_signed_option *
1807bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const(
45c51519 1808 const struct bt_field_class *fc, const char *name)
740faaf4 1809{
bdb288b3 1810 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519 1811 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
de821fe5 1812 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD,
45c51519
PP
1813 "Field class");
1814 return (const void *)
1815 borrow_named_field_class_from_container_field_class_by_name(
1816 (void *) fc, name);
1817}
1818
de821fe5
PP
1819const struct bt_field_class_variant_with_selector_field_integer_signed_option *
1820bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
45c51519
PP
1821 const struct bt_field_class *fc, uint64_t index)
1822{
1823 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1824 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
de821fe5 1825 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD,
45c51519
PP
1826 "Field class");
1827 return (const void *)
1e6fd1d7
PP
1828 borrow_named_field_class_from_container_field_class_at_index(
1829 (void *) fc, index);
1830}
1831
1832const char *bt_field_class_variant_option_get_name(
1833 const struct bt_field_class_variant_option *option)
1834{
1835 const struct bt_named_field_class *named_fc = (const void *) option;
1836
bdb288b3 1837 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1e6fd1d7
PP
1838 return named_fc->name->str;
1839}
1840
1841const struct bt_field_class *
1842bt_field_class_variant_option_borrow_field_class_const(
1843 const struct bt_field_class_variant_option *option)
1844{
1845 const struct bt_named_field_class *named_fc = (const void *) option;
1846
bdb288b3 1847 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1e6fd1d7
PP
1848 return named_fc->fc;
1849}
1850
bcd7ef6f
FD
1851struct bt_field_class *
1852bt_field_class_variant_option_borrow_field_class(
1853 struct bt_field_class_variant_option *option)
1854{
1855 struct bt_named_field_class *named_fc = (void *) option;
1856
1857 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1858 return named_fc->fc;
1859}
1860
45c51519 1861const struct bt_integer_range_set_unsigned *
de821fe5
PP
1862bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
1863 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option *option)
1e6fd1d7 1864{
de821fe5 1865 const struct bt_field_class_variant_with_selector_field_option *opt =
45c51519 1866 (const void *) option;
1e6fd1d7 1867
bdb288b3 1868 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
45c51519
PP
1869 return (const void *) opt->range_set;
1870}
1871
1872const struct bt_integer_range_set_signed *
de821fe5
PP
1873bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
1874 const struct bt_field_class_variant_with_selector_field_integer_signed_option *option)
45c51519 1875{
de821fe5 1876 const struct bt_field_class_variant_with_selector_field_option *opt =
45c51519
PP
1877 (const void *) option;
1878
1879 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1880 return (const void *) opt->range_set;
5cd6d0e5
PP
1881}
1882
40f4ba76 1883const struct bt_field_path *
de821fe5 1884bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
40f4ba76 1885 const struct bt_field_class *fc)
e5be10ef 1886{
de821fe5 1887 const struct bt_field_class_variant_with_selector_field *var_fc =
45c51519 1888 (const void *) fc;
5cd6d0e5 1889
bdb288b3 1890 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
45c51519 1891 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class");
5cd6d0e5
PP
1892 return var_fc->selector_field_path;
1893}
1894
1895static
c6962c96 1896int init_array_field_class(struct bt_field_class_array *fc,
864cad70 1897 enum bt_field_class_type type, bt_object_release_func release_func,
5cd6d0e5
PP
1898 struct bt_field_class *element_fc)
1899{
c6962c96
PP
1900 int ret;
1901
5cd6d0e5 1902 BT_ASSERT(element_fc);
c6962c96
PP
1903 ret = init_field_class((void *) fc, type, release_func);
1904 if (ret) {
1905 goto end;
1906 }
1907
398454ed 1908 fc->element_fc = element_fc;
6871026b 1909 bt_object_get_ref_no_null_check(fc->element_fc);
5cd6d0e5 1910 bt_field_class_freeze(element_fc);
c6962c96
PP
1911
1912end:
1913 return ret;
5cd6d0e5
PP
1914}
1915
1916static
1917void finalize_array_field_class(struct bt_field_class_array *array_fc)
1918{
1919 BT_ASSERT(array_fc);
e5be10ef 1920 BT_LOGD_STR("Putting element field class.");
c6962c96 1921 finalize_field_class((void *) array_fc);
238b7404 1922 BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc);
5cd6d0e5
PP
1923}
1924
1925static
1926void destroy_static_array_field_class(struct bt_object *obj)
1927{
1928 BT_ASSERT(obj);
e5be10ef 1929 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj);
5cd6d0e5
PP
1930 finalize_array_field_class((void *) obj);
1931 g_free(obj);
1932}
1933
40f4ba76 1934struct bt_field_class *
9c08c816 1935bt_field_class_array_static_create(bt_trace_class *trace_class,
1122a43a 1936 struct bt_field_class *element_fc, uint64_t length)
5cd6d0e5 1937{
9c08c816 1938 struct bt_field_class_array_static *array_fc = NULL;
5cd6d0e5 1939
1122a43a 1940 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef
PP
1941 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1942 BT_LOGD_STR("Creating default static array field class object.");
9c08c816 1943 array_fc = g_new0(struct bt_field_class_array_static, 1);
5cd6d0e5 1944 if (!array_fc) {
870631a2
PP
1945 BT_LIB_LOGE_APPEND_CAUSE(
1946 "Failed to allocate one static array field class.");
5cd6d0e5
PP
1947 goto error;
1948 }
1949
c6962c96
PP
1950 if (init_array_field_class((void *) array_fc,
1951 BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
1952 destroy_static_array_field_class, element_fc)) {
1953 goto error;
1954 }
1955
5cd6d0e5 1956 array_fc->length = length;
e5be10ef 1957 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc);
5cd6d0e5
PP
1958 goto end;
1959
1960error:
65300d60 1961 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
1962
1963end:
1964 return (void *) array_fc;
1965}
1966
40f4ba76
PP
1967const struct bt_field_class *
1968bt_field_class_array_borrow_element_field_class_const(
1969 const struct bt_field_class *fc)
5cd6d0e5 1970{
40f4ba76 1971 const struct bt_field_class_array *array_fc = (const void *) fc;
5cd6d0e5 1972
bdb288b3
PP
1973 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1974 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
5cd6d0e5
PP
1975 return array_fc->element_fc;
1976}
1977
740faaf4
PP
1978struct bt_field_class *
1979bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc)
1980{
1981 struct bt_field_class_array *array_fc = (void *) fc;
1982
bdb288b3
PP
1983 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1984 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
740faaf4
PP
1985 return array_fc->element_fc;
1986}
1987
9c08c816 1988uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc)
e5be10ef 1989{
9c08c816 1990 const struct bt_field_class_array_static *array_fc = (const void *) fc;
5cd6d0e5 1991
bdb288b3
PP
1992 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1993 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
5cd6d0e5
PP
1994 "Field class");
1995 return (uint64_t) array_fc->length;
1996}
1997
1998static
1999void destroy_dynamic_array_field_class(struct bt_object *obj)
2000{
9c08c816 2001 struct bt_field_class_array_dynamic *fc = (void *) obj;
5cd6d0e5
PP
2002
2003 BT_ASSERT(fc);
e5be10ef 2004 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc);
5cd6d0e5
PP
2005 finalize_array_field_class((void *) fc);
2006 BT_LOGD_STR("Putting length field path.");
238b7404
PP
2007 BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path);
2008 BT_LOGD_STR("Putting length field class.");
2009 BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc);
5cd6d0e5
PP
2010 g_free(fc);
2011}
2012
9c08c816 2013struct bt_field_class *bt_field_class_array_dynamic_create(
1367bc7c
PP
2014 struct bt_trace_class *trace_class,
2015 struct bt_field_class *element_fc,
2016 struct bt_field_class *length_fc)
5cd6d0e5 2017{
9c08c816 2018 struct bt_field_class_array_dynamic *array_fc = NULL;
5cd6d0e5 2019
1122a43a 2020 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef
PP
2021 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
2022 BT_LOGD_STR("Creating default dynamic array field class object.");
9c08c816 2023 array_fc = g_new0(struct bt_field_class_array_dynamic, 1);
5cd6d0e5 2024 if (!array_fc) {
870631a2
PP
2025 BT_LIB_LOGE_APPEND_CAUSE(
2026 "Failed to allocate one dynamic array field class.");
5cd6d0e5
PP
2027 goto error;
2028 }
2029
c6962c96 2030 if (init_array_field_class((void *) array_fc,
81b8fa44
PP
2031 length_fc ?
2032 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD :
2033 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD,
c6962c96
PP
2034 destroy_dynamic_array_field_class, element_fc)) {
2035 goto error;
2036 }
1367bc7c
PP
2037
2038 if (length_fc) {
2039 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc,
2040 "Length field class");
2041 array_fc->length_fc = length_fc;
6871026b 2042 bt_object_get_ref_no_null_check(array_fc->length_fc);
1367bc7c
PP
2043 bt_field_class_freeze(length_fc);
2044 }
2045
e5be10ef 2046 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc);
5cd6d0e5
PP
2047 goto end;
2048
2049error:
65300d60 2050 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
5cd6d0e5
PP
2051
2052end:
2053 return (void *) array_fc;
2054}
2055
40f4ba76 2056const struct bt_field_path *
81b8fa44 2057bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
40f4ba76 2058 const struct bt_field_class *fc)
5cd6d0e5 2059{
9c08c816 2060 const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc;
5cd6d0e5 2061
bdb288b3 2062 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
81b8fa44
PP
2063 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
2064 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD,
5cd6d0e5
PP
2065 "Field class");
2066 return seq_fc->length_field_path;
2067}
2068
2069static
2070void destroy_string_field_class(struct bt_object *obj)
2071{
2072 BT_ASSERT(obj);
e5be10ef 2073 BT_LIB_LOGD("Destroying string field class object: %!+F", obj);
c6962c96 2074 finalize_field_class((void *) obj);
5cd6d0e5
PP
2075 g_free(obj);
2076}
2077
1122a43a 2078struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class)
5cd6d0e5
PP
2079{
2080 struct bt_field_class_string *string_fc = NULL;
2081
1122a43a 2082 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
e5be10ef 2083 BT_LOGD_STR("Creating default string field class object.");
5cd6d0e5
PP
2084 string_fc = g_new0(struct bt_field_class_string, 1);
2085 if (!string_fc) {
870631a2
PP
2086 BT_LIB_LOGE_APPEND_CAUSE(
2087 "Failed to allocate one string field class.");
5cd6d0e5
PP
2088 goto error;
2089 }
2090
c6962c96
PP
2091 if (init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING,
2092 destroy_string_field_class)) {
2093 goto error;
2094 }
2095
e5be10ef 2096 BT_LIB_LOGD("Created string field class object: %!+F", string_fc);
5cd6d0e5
PP
2097 goto end;
2098
2099error:
65300d60 2100 BT_OBJECT_PUT_REF_AND_RESET(string_fc);
5cd6d0e5
PP
2101
2102end:
2103 return (void *) string_fc;
2104}
2105
2106BT_HIDDEN
1e6fd1d7 2107void _bt_field_class_freeze(const struct bt_field_class *c_fc)
5cd6d0e5 2108{
1e6fd1d7
PP
2109 struct bt_field_class *fc = (void *) c_fc;
2110
5cd6d0e5
PP
2111 /*
2112 * Element/member/option field classes are frozen when added to
2113 * their owner.
2114 */
2115 BT_ASSERT(fc);
c6962c96 2116 bt_value_freeze(fc->user_attributes);
1e6fd1d7
PP
2117 fc->frozen = true;
2118
ebdb6693
PP
2119 if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE ||
2120 bt_field_class_type_is(fc->type,
2121 BT_FIELD_CLASS_TYPE_VARIANT)) {
1e6fd1d7
PP
2122 struct bt_field_class_named_field_class_container *container_fc =
2123 (void *) fc;
2124 uint64_t i;
2125
2126 for (i = 0; i < container_fc->named_fcs->len; i++) {
45c51519
PP
2127 bt_named_field_class_freeze(
2128 container_fc->named_fcs->pdata[i]);
1e6fd1d7 2129 }
1e6fd1d7
PP
2130 }
2131}
2132
2133BT_HIDDEN
2134void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc)
2135{
2136 BT_ASSERT(named_fc);
c6962c96
PP
2137 BT_ASSERT(named_fc->fc->frozen);
2138 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
2139 named_fc->user_attributes);
2140 bt_value_freeze(named_fc->user_attributes);
1e6fd1d7 2141 ((struct bt_named_field_class *) named_fc)->frozen = true;
5cd6d0e5
PP
2142}
2143
2144BT_HIDDEN
bdb288b3 2145void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc)
5cd6d0e5 2146{
40f4ba76
PP
2147 struct bt_field_class *fc = (void *) c_fc;
2148
5cd6d0e5 2149 BT_ASSERT(fc);
862ca4ed 2150 BT_ASSERT_PRE(!fc->part_of_trace_class,
5cd6d0e5 2151 "Field class is already part of a trace: %!+F", fc);
862ca4ed 2152 fc->part_of_trace_class = true;
5cd6d0e5 2153
ebdb6693
PP
2154 if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE ||
2155 bt_field_class_type_is(fc->type,
2156 BT_FIELD_CLASS_TYPE_VARIANT)) {
5cd6d0e5
PP
2157 struct bt_field_class_named_field_class_container *container_fc =
2158 (void *) fc;
2159 uint64_t i;
2160
2161 for (i = 0; i < container_fc->named_fcs->len; i++) {
2162 struct bt_named_field_class *named_fc =
45c51519 2163 container_fc->named_fcs->pdata[i];
5cd6d0e5 2164
862ca4ed 2165 bt_field_class_make_part_of_trace_class(named_fc->fc);
5cd6d0e5 2166 }
ebdb6693
PP
2167 } else if (bt_field_class_type_is(fc->type,
2168 BT_FIELD_CLASS_TYPE_ARRAY)) {
5cd6d0e5
PP
2169 struct bt_field_class_array *array_fc = (void *) fc;
2170
862ca4ed 2171 bt_field_class_make_part_of_trace_class(array_fc->element_fc);
5cd6d0e5
PP
2172 }
2173}
c5b9b441 2174
c6962c96
PP
2175const struct bt_value *bt_field_class_borrow_user_attributes_const(
2176 const struct bt_field_class *fc)
2177{
2178 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
2179 return fc->user_attributes;
2180}
2181
2182struct bt_value *bt_field_class_borrow_user_attributes(
2183 struct bt_field_class *field_class)
2184{
2185 return (void *) bt_field_class_borrow_user_attributes_const(
2186 (void *) field_class);
2187}
2188
2189
2190void bt_field_class_set_user_attributes(
2191 struct bt_field_class *fc,
2192 const struct bt_value *user_attributes)
2193{
2194 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
2195 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2196 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2197 "User attributes object is not a map value object.");
2198 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
6871026b 2199 bt_object_put_ref_no_null_check(fc->user_attributes);
c6962c96 2200 fc->user_attributes = (void *) user_attributes;
6871026b 2201 bt_object_get_ref_no_null_check(fc->user_attributes);
c6962c96
PP
2202}
2203
2204static
2205const struct bt_value *bt_named_field_class_borrow_user_attributes_const(
2206 const struct bt_named_field_class *named_fc)
2207{
2208 return named_fc->user_attributes;
2209}
2210
2211static
2212void bt_named_field_class_set_user_attributes(
2213 struct bt_named_field_class *named_fc,
2214 const struct bt_value *user_attributes)
2215{
2216 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2217 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2218 "User attributes object is not a map value object.");
2219 BT_ASSERT_PRE_DEV_HOT(named_fc,
2220 "Structure field class member or variant field class option",
2221 ".");
6871026b 2222 bt_object_put_ref_no_null_check(named_fc->user_attributes);
c6962c96 2223 named_fc->user_attributes = (void *) user_attributes;
6871026b 2224 bt_object_get_ref_no_null_check(named_fc->user_attributes);
c6962c96
PP
2225}
2226
2227const struct bt_value *
2228bt_field_class_structure_member_borrow_user_attributes_const(
2229 const struct bt_field_class_structure_member *member)
2230{
2231 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2232 return bt_named_field_class_borrow_user_attributes_const(
2233 (const void *) member);
2234}
2235
2236struct bt_value *
2237bt_field_class_structure_member_borrow_user_attributes(
2238 struct bt_field_class_structure_member *member)
2239{
2240 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2241 return (void *) bt_named_field_class_borrow_user_attributes_const(
2242 (void *) member);
2243}
2244
2245void bt_field_class_structure_member_set_user_attributes(
2246 struct bt_field_class_structure_member *member,
2247 const struct bt_value *user_attributes)
2248{
2249 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2250 bt_named_field_class_set_user_attributes((void *) member,
2251 user_attributes);
2252}
2253
2254const struct bt_value *bt_field_class_variant_option_borrow_user_attributes_const(
2255 const struct bt_field_class_variant_option *option)
2256{
2257 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2258 return bt_named_field_class_borrow_user_attributes_const(
2259 (const void *) option);
2260}
2261
2262struct bt_value *bt_field_class_variant_option_borrow_user_attributes(
2263 struct bt_field_class_variant_option *option)
2264{
2265 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2266 return (void *) bt_named_field_class_borrow_user_attributes_const(
2267 (void *) option);
2268}
2269
2270void bt_field_class_variant_option_set_user_attributes(
2271 struct bt_field_class_variant_option *option,
2272 const struct bt_value *user_attributes)
2273{
2274 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2275 bt_named_field_class_set_user_attributes((void *) option,
2276 user_attributes);
2277}
2278
c5b9b441
PP
2279void bt_field_class_get_ref(const struct bt_field_class *field_class)
2280{
2281 bt_object_get_ref(field_class);
2282}
2283
2284void bt_field_class_put_ref(const struct bt_field_class *field_class)
2285{
2286 bt_object_put_ref(field_class);
2287}
This page took 0.160611 seconds and 4 git commands to generate.