2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/FIELD-CLASS"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/field-class.h>
13 #include <babeltrace2/trace-ir/field.h>
14 #include <babeltrace2/trace-ir/clock-class.h>
15 #include "lib/object.h"
16 #include "compat/compiler.h"
17 #include "compat/endian.h"
18 #include "common/assert.h"
19 #include "compat/glib.h"
25 #include "clock-class.h"
26 #include "field-class.h"
28 #include "field-path.h"
30 #include "lib/func-status.h"
31 #include "lib/integer-range-set.h"
32 #include "lib/value.h"
34 enum bt_field_class_type
bt_field_class_get_type(
35 const struct bt_field_class
*fc
)
37 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
42 int init_field_class(struct bt_field_class
*fc
, enum bt_field_class_type type
,
43 bt_object_release_func release_func
)
48 BT_ASSERT(release_func
);
49 bt_object_init_shared(&fc
->base
, release_func
);
51 fc
->user_attributes
= bt_value_map_create();
52 if (!fc
->user_attributes
) {
53 BT_LIB_LOGE_APPEND_CAUSE(
54 "Failed to create a map value object.");
64 void finalize_field_class(struct bt_field_class
*fc
)
66 BT_OBJECT_PUT_REF_AND_RESET(fc
->user_attributes
);
70 void destroy_bit_array_field_class(struct bt_object
*obj
)
73 BT_LIB_LOGD("Destroying bit array field class object: %!+F", obj
);
74 finalize_field_class((void *) obj
);
78 struct bt_field_class
*bt_field_class_bit_array_create(
79 struct bt_trace_class
*trace_class
, uint64_t length
)
81 struct bt_field_class_bit_array
*ba_fc
= NULL
;
83 BT_ASSERT_PRE_NO_ERROR();
84 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
85 BT_ASSERT_PRE("valid-length", length
> 0 && length
<= 64,
86 "Unsupported length for bit array field class "
87 "(minimum is 1, maximum is 64): length=%" PRIu64
, length
);
88 BT_LOGD("Creating default bit array field class object.");
89 ba_fc
= g_new0(struct bt_field_class_bit_array
, 1);
91 BT_LIB_LOGE_APPEND_CAUSE(
92 "Failed to allocate one bit array field class.");
96 if (init_field_class((void *) ba_fc
, BT_FIELD_CLASS_TYPE_BIT_ARRAY
,
97 destroy_bit_array_field_class
)) {
101 ba_fc
->length
= length
;
102 BT_LIB_LOGD("Created bit array field class object: %!+F", ba_fc
);
106 BT_OBJECT_PUT_REF_AND_RESET(ba_fc
);
109 return (void *) ba_fc
;
112 uint64_t bt_field_class_bit_array_get_length(const struct bt_field_class
*fc
)
114 const struct bt_field_class_bit_array
*ba_fc
= (const void *) fc
;
116 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
117 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "bit-array",
118 BT_FIELD_CLASS_TYPE_BIT_ARRAY
, "Field class");
119 return ba_fc
->length
;
123 void destroy_bool_field_class(struct bt_object
*obj
)
126 BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj
);
127 finalize_field_class((void *) obj
);
131 struct bt_field_class
*bt_field_class_bool_create(
132 bt_trace_class
*trace_class
)
134 struct bt_field_class_bool
*bool_fc
= NULL
;
136 BT_ASSERT_PRE_NO_ERROR();
137 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
138 BT_LOGD("Creating default boolean field class object.");
139 bool_fc
= g_new0(struct bt_field_class_bool
, 1);
141 BT_LIB_LOGE_APPEND_CAUSE(
142 "Failed to allocate one boolean field class.");
146 if (init_field_class((void *) bool_fc
, BT_FIELD_CLASS_TYPE_BOOL
,
147 destroy_bool_field_class
)) {
151 BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc
);
155 BT_OBJECT_PUT_REF_AND_RESET(bool_fc
);
158 return (void *) bool_fc
;
162 int init_integer_field_class(struct bt_field_class_integer
*fc
,
163 enum bt_field_class_type type
,
164 bt_object_release_func release_func
)
168 ret
= init_field_class((void *) fc
, type
, release_func
);
174 fc
->base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
181 void destroy_integer_field_class(struct bt_object
*obj
)
184 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj
);
185 finalize_field_class((void *) obj
);
190 struct bt_field_class
*create_integer_field_class(bt_trace_class
*trace_class
,
191 enum bt_field_class_type type
)
193 struct bt_field_class_integer
*int_fc
= NULL
;
195 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
196 BT_LOGD("Creating default integer field class object: type=%s",
197 bt_common_field_class_type_string(type
));
198 int_fc
= g_new0(struct bt_field_class_integer
, 1);
200 BT_LIB_LOGE_APPEND_CAUSE(
201 "Failed to allocate one integer field class.");
205 if (init_integer_field_class(int_fc
, type
,
206 destroy_integer_field_class
)) {
210 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc
);
214 BT_OBJECT_PUT_REF_AND_RESET(int_fc
);
217 return (void *) int_fc
;
220 struct bt_field_class
*bt_field_class_integer_unsigned_create(
221 bt_trace_class
*trace_class
)
223 BT_ASSERT_PRE_NO_ERROR();
225 return create_integer_field_class(trace_class
,
226 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
229 struct bt_field_class
*bt_field_class_integer_signed_create(
230 bt_trace_class
*trace_class
)
232 BT_ASSERT_PRE_NO_ERROR();
234 return create_integer_field_class(trace_class
,
235 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
);
238 uint64_t bt_field_class_integer_get_field_value_range(
239 const struct bt_field_class
*fc
)
241 const struct bt_field_class_integer
*int_fc
= (const void *) fc
;
243 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
244 BT_ASSERT_PRE_DEV_FC_IS_INT("field-class", fc
, "Field class");
245 return int_fc
->range
;
249 bool size_is_valid_for_enumeration_field_class(struct bt_field_class
*fc
,
256 void bt_field_class_integer_set_field_value_range(
257 struct bt_field_class
*fc
, uint64_t size
)
259 struct bt_field_class_integer
*int_fc
= (void *) fc
;
261 BT_ASSERT_PRE_FC_NON_NULL(fc
);
262 BT_ASSERT_PRE_FC_IS_INT("field-class", fc
, "Field class");
263 BT_ASSERT_PRE_DEV_FC_HOT(fc
);
264 BT_ASSERT_PRE("valid-n",
265 size
>= 1 && size
<= 64,
266 "Unsupported size for integer field class's field value range "
267 "(minimum is 1, maximum is 64): size=%" PRIu64
, size
);
268 BT_ASSERT_PRE("valid-n-for-enumeration-field-class",
269 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
270 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
271 size_is_valid_for_enumeration_field_class(fc
, size
),
272 "Invalid field value range for enumeration field class: "
273 "at least one of the current mapping ranges contains values "
274 "which are outside this range: %!+F, size=%" PRIu64
, fc
, size
);
275 int_fc
->range
= size
;
276 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc
);
279 enum bt_field_class_integer_preferred_display_base
280 bt_field_class_integer_get_preferred_display_base(const struct bt_field_class
*fc
)
282 const struct bt_field_class_integer
*int_fc
= (const void *) fc
;
284 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
285 BT_ASSERT_PRE_DEV_FC_IS_INT("field-class", fc
, "Field class");
289 void bt_field_class_integer_set_preferred_display_base(
290 struct bt_field_class
*fc
,
291 enum bt_field_class_integer_preferred_display_base base
)
293 struct bt_field_class_integer
*int_fc
= (void *) fc
;
295 BT_ASSERT_PRE_FC_NON_NULL(fc
);
296 BT_ASSERT_PRE_FC_IS_INT("field-class", fc
, "Field class");
297 BT_ASSERT_PRE_DEV_FC_HOT(fc
);
299 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc
);
303 void finalize_enumeration_field_class_mapping(
304 struct bt_field_class_enumeration_mapping
*mapping
)
308 if (mapping
->label
) {
309 g_string_free(mapping
->label
, TRUE
);
310 mapping
->label
= NULL
;
313 BT_OBJECT_PUT_REF_AND_RESET(mapping
->range_set
);
317 void destroy_enumeration_field_class(struct bt_object
*obj
)
319 struct bt_field_class_enumeration
*fc
= (void *) obj
;
322 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc
);
323 finalize_field_class((void *) obj
);
328 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
329 finalize_enumeration_field_class_mapping(
330 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
));
333 g_array_free(fc
->mappings
, TRUE
);
338 g_ptr_array_free(fc
->label_buf
, TRUE
);
339 fc
->label_buf
= NULL
;
346 struct bt_field_class
*create_enumeration_field_class(
347 bt_trace_class
*trace_class
, enum bt_field_class_type type
)
349 struct bt_field_class_enumeration
*enum_fc
= NULL
;
351 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
352 BT_LOGD("Creating default enumeration field class object: type=%s",
353 bt_common_field_class_type_string(type
));
354 enum_fc
= g_new0(struct bt_field_class_enumeration
, 1);
356 BT_LIB_LOGE_APPEND_CAUSE(
357 "Failed to allocate one enumeration field class.");
361 if (init_integer_field_class((void *) enum_fc
, type
,
362 destroy_enumeration_field_class
)) {
366 enum_fc
->mappings
= g_array_new(FALSE
, TRUE
,
367 sizeof(struct bt_field_class_enumeration_mapping
));
368 if (!enum_fc
->mappings
) {
369 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
373 enum_fc
->label_buf
= g_ptr_array_new();
374 if (!enum_fc
->label_buf
) {
375 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
379 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc
);
383 BT_OBJECT_PUT_REF_AND_RESET(enum_fc
);
386 return (void *) enum_fc
;
389 struct bt_field_class
*bt_field_class_enumeration_unsigned_create(
390 bt_trace_class
*trace_class
)
392 BT_ASSERT_PRE_NO_ERROR();
394 return create_enumeration_field_class(trace_class
,
395 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
398 struct bt_field_class
*bt_field_class_enumeration_signed_create(
399 bt_trace_class
*trace_class
)
401 BT_ASSERT_PRE_NO_ERROR();
403 return create_enumeration_field_class(trace_class
,
404 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
407 uint64_t bt_field_class_enumeration_get_mapping_count(
408 const struct bt_field_class
*fc
)
410 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
412 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
413 BT_ASSERT_PRE_DEV_FC_IS_ENUM("field-class", fc
, "Field class");
414 return (uint64_t) enum_fc
->mappings
->len
;
417 const struct bt_field_class_enumeration_unsigned_mapping
*
418 bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
419 const struct bt_field_class
*fc
, uint64_t index
)
421 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
423 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
424 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
425 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "unsigned-enumeration",
426 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
427 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
430 const struct bt_field_class_enumeration_signed_mapping
*
431 bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
432 const struct bt_field_class
*fc
, uint64_t index
)
434 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
436 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
437 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
438 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "signed-enumeration",
439 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field class");
440 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
444 const struct bt_field_class_enumeration_mapping
*
445 borrow_enumeration_field_class_mapping_by_label(
446 const struct bt_field_class_enumeration
*fc
, const char *label
,
447 const char *api_func
)
449 struct bt_field_class_enumeration_mapping
*mapping
= NULL
;
453 BT_ASSERT_PRE_DEV_NON_NULL_FROM_FUNC(api_func
, "label", label
,
456 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
457 struct bt_field_class_enumeration_mapping
*this_mapping
=
458 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
);
460 if (strcmp(this_mapping
->label
->str
, label
) == 0) {
461 mapping
= this_mapping
;
470 const struct bt_field_class_enumeration_signed_mapping
*
471 bt_field_class_enumeration_signed_borrow_mapping_by_label_const(
472 const struct bt_field_class
*fc
, const char *label
)
474 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
475 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "signed-enumeration",
476 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field class");
477 return (const void *) borrow_enumeration_field_class_mapping_by_label(
478 (const void *) fc
, label
, __func__
);
481 const struct bt_field_class_enumeration_unsigned_mapping
*
482 bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const(
483 const struct bt_field_class
*fc
, const char *label
)
485 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
486 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "unsigned-enumeration",
487 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
488 return (const void *) borrow_enumeration_field_class_mapping_by_label(
489 (const void *) fc
, label
, __func__
);
492 const char *bt_field_class_enumeration_mapping_get_label(
493 const struct bt_field_class_enumeration_mapping
*mapping
)
495 BT_ASSERT_PRE_DEV_NON_NULL("enumeration-field-class-mapping",
496 mapping
, "Enumeration field class mapping");
497 return mapping
->label
->str
;
500 const struct bt_integer_range_set_unsigned
*
501 bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
502 const struct bt_field_class_enumeration_unsigned_mapping
*u_mapping
)
504 const struct bt_field_class_enumeration_mapping
*mapping
=
505 (const void *) u_mapping
;
507 BT_ASSERT_PRE_DEV_NON_NULL("enumeration-field-class-mapping",
508 mapping
, "Enumeration field class mapping");
509 return (const void *) mapping
->range_set
;
512 const struct bt_integer_range_set_signed
*
513 bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
514 const struct bt_field_class_enumeration_signed_mapping
*s_mapping
)
516 const struct bt_field_class_enumeration_mapping
*mapping
=
517 (const void *) s_mapping
;
519 BT_ASSERT_PRE_DEV_NON_NULL("enumeration-field-class-mapping",
520 mapping
, "Enumeration field class mapping");
521 return (const void *) mapping
->range_set
;
524 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
525 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
526 const struct bt_field_class
*fc
, uint64_t value
,
527 bt_field_class_enumeration_mapping_label_array
*label_array
,
530 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
533 BT_ASSERT_PRE_DEV_NO_ERROR();
534 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
535 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array
,
536 "Label array (output)");
537 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count
, "Count (output)");
538 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "unsigned-enumeration",
539 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
540 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
542 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
544 const struct bt_field_class_enumeration_mapping
*mapping
=
545 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
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
);
552 if (value
>= range
->lower
.u
&&
553 value
<= range
->upper
.u
) {
554 g_ptr_array_add(enum_fc
->label_buf
,
555 mapping
->label
->str
);
561 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
562 *count
= (uint64_t) enum_fc
->label_buf
->len
;
563 return BT_FUNC_STATUS_OK
;
566 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
567 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
568 const struct bt_field_class
*fc
, int64_t value
,
569 bt_field_class_enumeration_mapping_label_array
*label_array
,
572 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
575 BT_ASSERT_PRE_DEV_NO_ERROR();
576 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
577 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array
,
578 "Label array (output)");
579 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count
, "Count (output)");
580 BT_ASSERT_PRE_DEV_FC_HAS_TYPE("field-class", fc
, "signed-enumeration",
581 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field class");
582 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
584 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
586 const struct bt_field_class_enumeration_mapping
*mapping
=
587 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
589 for (j
= 0; j
< mapping
->range_set
->ranges
->len
; j
++) {
590 const struct bt_integer_range
*range
= (const void *)
591 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
592 mapping
->range_set
, j
);
594 if (value
>= range
->lower
.i
&&
595 value
<= range
->upper
.i
) {
596 g_ptr_array_add(enum_fc
->label_buf
,
597 mapping
->label
->str
);
603 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
604 *count
= (uint64_t) enum_fc
->label_buf
->len
;
605 return BT_FUNC_STATUS_OK
;
609 bool enumeration_field_class_has_mapping_with_label(
610 const struct bt_field_class_enumeration
*enum_fc
,
619 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
620 struct bt_field_class_enumeration_mapping
*mapping_candidate
=
621 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
623 if (strcmp(mapping_candidate
->label
->str
, label
) == 0) {
634 enum bt_field_class_enumeration_add_mapping_status
635 add_mapping_to_enumeration_field_class(struct bt_field_class
*fc
,
636 const char *label
, const struct bt_integer_range_set
*range_set
,
637 const char *api_func
)
639 enum bt_field_class_enumeration_add_mapping_status status
=
641 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
642 struct bt_field_class_enumeration_mapping mapping
= { 0 };
644 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
646 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "label", label
, "Label");
647 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL_FROM_FUNC(api_func
, range_set
);
648 BT_ASSERT_PRE_FROM_FUNC(api_func
,
649 "enumeration-field-class-mapping-label-is-unique",
650 !enumeration_field_class_has_mapping_with_label(
652 "Duplicate mapping name in enumeration field class: "
653 "%![enum-fc-]+F, label=\"%s\"", fc
, label
);
654 mapping
.range_set
= range_set
;
655 bt_object_get_ref(mapping
.range_set
);
656 mapping
.label
= g_string_new(label
);
657 if (!mapping
.label
) {
658 finalize_enumeration_field_class_mapping(&mapping
);
659 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
663 g_array_append_val(enum_fc
->mappings
, mapping
);
664 BT_LIB_LOGD("Added mapping to enumeration field class: "
665 "%![fc-]+F, label=\"%s\"", fc
, label
);
671 enum bt_field_class_enumeration_add_mapping_status
672 bt_field_class_enumeration_unsigned_add_mapping(
673 struct bt_field_class
*fc
, const char *label
,
674 const struct bt_integer_range_set_unsigned
*range_set
)
676 BT_ASSERT_PRE_NO_ERROR();
677 BT_ASSERT_PRE_FC_NON_NULL(fc
);
678 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
679 "unsigned-enumeration-field-class",
680 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
681 return add_mapping_to_enumeration_field_class(fc
, label
,
682 (const void *) range_set
, __func__
);
685 enum bt_field_class_enumeration_add_mapping_status
686 bt_field_class_enumeration_signed_add_mapping(
687 struct bt_field_class
*fc
, const char *label
,
688 const struct bt_integer_range_set_signed
*range_set
)
690 BT_ASSERT_PRE_NO_ERROR();
691 BT_ASSERT_PRE_FC_NON_NULL(fc
);
692 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
693 "signed-enumeration-field-class",
694 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field class");
695 return add_mapping_to_enumeration_field_class(fc
, label
,
696 (const void *) range_set
, __func__
);
700 void destroy_real_field_class(struct bt_object
*obj
)
703 BT_LIB_LOGD("Destroying real field class object: %!+F", obj
);
704 finalize_field_class((void *) obj
);
709 struct bt_field_class
*create_real_field_class(bt_trace_class
*trace_class
,
710 enum bt_field_class_type type
)
712 struct bt_field_class_real
*real_fc
= NULL
;
714 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
715 BT_LOGD("Creating default real field class object: type=%s",
716 bt_common_field_class_type_string(type
));
717 real_fc
= g_new0(struct bt_field_class_real
, 1);
719 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
723 if (init_field_class((void *) real_fc
, type
, destroy_real_field_class
)) {
727 BT_LIB_LOGD("Created real field class object: %!+F", real_fc
);
731 BT_OBJECT_PUT_REF_AND_RESET(real_fc
);
734 return (void *) real_fc
;
737 struct bt_field_class
*bt_field_class_real_single_precision_create(
738 bt_trace_class
*trace_class
)
740 BT_ASSERT_PRE_NO_ERROR();
742 return create_real_field_class(trace_class
,
743 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
);
746 struct bt_field_class
*bt_field_class_real_double_precision_create(
747 bt_trace_class
*trace_class
)
749 BT_ASSERT_PRE_NO_ERROR();
751 return create_real_field_class(trace_class
,
752 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
);
756 int init_named_field_classes_container(
757 struct bt_field_class_named_field_class_container
*fc
,
758 enum bt_field_class_type type
,
759 bt_object_release_func fc_release_func
,
760 GDestroyNotify named_fc_destroy_func
)
764 ret
= init_field_class((void *) fc
, type
, fc_release_func
);
769 fc
->named_fcs
= g_ptr_array_new_with_free_func(named_fc_destroy_func
);
770 if (!fc
->named_fcs
) {
771 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
776 fc
->name_to_index
= g_hash_table_new(g_str_hash
, g_str_equal
);
777 if (!fc
->name_to_index
) {
778 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
788 void finalize_named_field_class(struct bt_named_field_class
*named_fc
)
791 BT_LIB_LOGD("Finalizing named field class: "
792 "addr=%p, name=\"%s\", %![fc-]+F",
793 named_fc
, named_fc
->name
? named_fc
->name
->str
: NULL
,
795 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
797 if (named_fc
->name
) {
798 g_string_free(named_fc
->name
, TRUE
);
799 named_fc
->name
= NULL
;
802 BT_LOGD_STR("Putting named field class's field class.");
803 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->fc
);
807 void destroy_named_field_class(gpointer ptr
)
809 struct bt_named_field_class
*named_fc
= ptr
;
812 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
813 finalize_named_field_class(ptr
);
819 void destroy_variant_with_selector_field_option(gpointer ptr
)
821 struct bt_field_class_variant_with_selector_field_option
*opt
= ptr
;
824 finalize_named_field_class(&opt
->common
);
825 BT_OBJECT_PUT_REF_AND_RESET(opt
->range_set
);
831 void finalize_named_field_classes_container(
832 struct bt_field_class_named_field_class_container
*fc
)
837 g_ptr_array_free(fc
->named_fcs
, TRUE
);
838 fc
->named_fcs
= NULL
;
842 if (fc
->name_to_index
) {
843 g_hash_table_destroy(fc
->name_to_index
);
844 fc
->name_to_index
= NULL
;
849 void destroy_structure_field_class(struct bt_object
*obj
)
852 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj
);
853 finalize_field_class((void *) obj
);
854 finalize_named_field_classes_container((void *) obj
);
858 struct bt_field_class
*bt_field_class_structure_create(
859 bt_trace_class
*trace_class
)
862 struct bt_field_class_structure
*struct_fc
= NULL
;
864 BT_ASSERT_PRE_NO_ERROR();
865 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
866 BT_LOGD_STR("Creating default structure field class object.");
867 struct_fc
= g_new0(struct bt_field_class_structure
, 1);
869 BT_LIB_LOGE_APPEND_CAUSE(
870 "Failed to allocate one structure field class.");
874 ret
= init_named_field_classes_container((void *) struct_fc
,
875 BT_FIELD_CLASS_TYPE_STRUCTURE
, destroy_structure_field_class
,
876 destroy_named_field_class
);
878 /* init_named_field_classes_container() logs errors */
882 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc
);
886 BT_OBJECT_PUT_REF_AND_RESET(struct_fc
);
889 return (void *) struct_fc
;
893 int init_named_field_class(struct bt_named_field_class
*named_fc
,
894 const char *name
, struct bt_field_class
*fc
)
896 int status
= BT_FUNC_STATUS_OK
;
901 named_fc
->name
= g_string_new(name
);
902 if (!named_fc
->name
) {
903 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
904 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
908 named_fc
->user_attributes
= bt_value_map_create();
909 if (!named_fc
->user_attributes
) {
910 BT_LIB_LOGE_APPEND_CAUSE(
911 "Failed to create a map value object.");
912 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
917 bt_object_get_ref_no_null_check(named_fc
->fc
);
924 struct bt_named_field_class
*create_named_field_class(const char *name
,
925 struct bt_field_class
*fc
)
927 struct bt_named_field_class
*named_fc
= g_new0(
928 struct bt_named_field_class
, 1);
931 BT_LIB_LOGE_APPEND_CAUSE(
932 "Failed to allocate a named field class.");
936 if (init_named_field_class(named_fc
, name
, fc
)) {
937 /* init_named_field_class() logs errors */
944 destroy_named_field_class(named_fc
);
952 struct bt_field_class_variant_with_selector_field_option
*
953 create_variant_with_selector_field_option(
954 const char *name
, struct bt_field_class
*fc
,
955 const struct bt_integer_range_set
*range_set
)
957 struct bt_field_class_variant_with_selector_field_option
*opt
= g_new0(
958 struct bt_field_class_variant_with_selector_field_option
, 1);
960 BT_ASSERT(range_set
);
963 BT_LIB_LOGE_APPEND_CAUSE(
964 "Failed to allocate a named field class.");
968 if (init_named_field_class(&opt
->common
, name
, fc
)) {
972 opt
->range_set
= range_set
;
973 bt_object_get_ref_no_null_check(opt
->range_set
);
974 bt_integer_range_set_freeze(range_set
);
978 destroy_variant_with_selector_field_option(opt
);
986 int append_named_field_class_to_container_field_class(
987 struct bt_field_class_named_field_class_container
*container_fc
,
988 struct bt_named_field_class
*named_fc
, const char *api_func
,
989 const char *unique_entry_precond_id
)
991 BT_ASSERT(container_fc
);
993 BT_ASSERT_PRE_DEV_FC_HOT_FROM_FUNC(api_func
, container_fc
);
994 BT_ASSERT_PRE_FROM_FUNC(api_func
, unique_entry_precond_id
,
995 !bt_g_hash_table_contains(container_fc
->name_to_index
,
996 named_fc
->name
->str
),
997 "Duplicate member/option name in structure/variant field class: "
998 "%![container-fc-]+F, name=\"%s\"", container_fc
,
999 named_fc
->name
->str
);
1002 * Freeze the contained field class, but not the named field
1003 * class itself, as it's still possible afterwards to modify
1004 * properties of the member/option object.
1006 bt_field_class_freeze(named_fc
->fc
);
1007 g_ptr_array_add(container_fc
->named_fcs
, named_fc
);
1008 g_hash_table_insert(container_fc
->name_to_index
, named_fc
->name
->str
,
1009 GUINT_TO_POINTER(container_fc
->named_fcs
->len
- 1));
1010 return BT_FUNC_STATUS_OK
;
1013 enum bt_field_class_structure_append_member_status
1014 bt_field_class_structure_append_member(
1015 struct bt_field_class
*fc
, const char *name
,
1016 struct bt_field_class
*member_fc
)
1018 enum bt_field_class_structure_append_member_status status
;
1019 struct bt_named_field_class
*named_fc
= NULL
;
1021 BT_ASSERT_PRE_NO_ERROR();
1022 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1023 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1024 named_fc
= create_named_field_class(name
, member_fc
);
1026 /* create_named_field_class() logs errors */
1027 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1031 status
= append_named_field_class_to_container_field_class((void *) fc
,
1033 "structure-field-class-member-name-is-unique");
1034 if (status
== BT_FUNC_STATUS_OK
) {
1035 /* Moved to the container */
1043 uint64_t bt_field_class_structure_get_member_count(
1044 const struct bt_field_class
*fc
)
1046 struct bt_field_class_structure
*struct_fc
= (void *) fc
;
1048 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1049 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1050 return (uint64_t) struct_fc
->common
.named_fcs
->len
;
1054 struct bt_named_field_class
*
1055 borrow_named_field_class_from_container_field_class_at_index(
1056 struct bt_field_class_named_field_class_container
*fc
,
1057 uint64_t index
, const char *api_func
)
1060 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
,
1061 fc
->named_fcs
->len
);
1062 return fc
->named_fcs
->pdata
[index
];
1065 const struct bt_field_class_structure_member
*
1066 bt_field_class_structure_borrow_member_by_index_const(
1067 const struct bt_field_class
*fc
, uint64_t index
)
1069 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1070 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1071 return (const void *)
1072 borrow_named_field_class_from_container_field_class_at_index(
1073 (void *) fc
, index
, __func__
);
1076 struct bt_field_class_structure_member
*
1077 bt_field_class_structure_borrow_member_by_index(
1078 struct bt_field_class
*fc
, uint64_t index
)
1080 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1081 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1083 borrow_named_field_class_from_container_field_class_at_index(
1084 (void *) fc
, index
, __func__
);
1088 struct bt_named_field_class
*
1089 borrow_named_field_class_from_container_field_class_by_name(
1090 struct bt_field_class_named_field_class_container
*fc
,
1091 const char *name
, const char *api_func
)
1093 struct bt_named_field_class
*named_fc
= NULL
;
1098 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
1099 if (!g_hash_table_lookup_extended(fc
->name_to_index
, name
, &orig_key
,
1104 named_fc
= fc
->named_fcs
->pdata
[GPOINTER_TO_UINT(value
)];
1110 const struct bt_field_class_structure_member
*
1111 bt_field_class_structure_borrow_member_by_name_const(
1112 const struct bt_field_class
*fc
, const char *name
)
1114 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1115 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1116 return (const void *)
1117 borrow_named_field_class_from_container_field_class_by_name(
1118 (void *) fc
, name
, __func__
);
1121 struct bt_field_class_structure_member
*
1122 bt_field_class_structure_borrow_member_by_name(
1123 struct bt_field_class
*fc
, const char *name
)
1125 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1126 BT_ASSERT_PRE_FC_IS_STRUCT("field-class", fc
, "Field class");
1128 borrow_named_field_class_from_container_field_class_by_name(
1129 (void *) fc
, name
, __func__
);
1132 const char *bt_field_class_structure_member_get_name(
1133 const struct bt_field_class_structure_member
*member
)
1135 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1137 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1138 return named_fc
->name
->str
;
1141 const struct bt_field_class
*
1142 bt_field_class_structure_member_borrow_field_class_const(
1143 const struct bt_field_class_structure_member
*member
)
1145 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1147 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1148 return named_fc
->fc
;
1151 struct bt_field_class
*
1152 bt_field_class_structure_member_borrow_field_class(
1153 struct bt_field_class_structure_member
*member
)
1155 struct bt_named_field_class
*named_fc
= (void *) member
;
1157 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1158 return named_fc
->fc
;
1162 void destroy_option_field_class(struct bt_object
*obj
)
1164 struct bt_field_class_option
*fc
= (void *) obj
;
1167 BT_LIB_LOGD("Destroying option field class object: %!+F", fc
);
1168 finalize_field_class((void *) obj
);
1169 BT_LOGD_STR("Putting content field class.");
1170 BT_OBJECT_PUT_REF_AND_RESET(fc
->content_fc
);
1172 if (fc
->common
.type
!= BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
) {
1173 struct bt_field_class_option_with_selector_field
*with_sel_fc
=
1176 BT_LOGD_STR("Putting selector field path.");
1177 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc
->selector_field_path
);
1178 BT_LOGD_STR("Putting selector field class.");
1179 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc
->selector_fc
);
1181 if (fc
->common
.type
!= BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
) {
1182 struct bt_field_class_option_with_selector_field_integer
*with_int_sel_fc
=
1185 BT_LOGD_STR("Putting integer range set.");
1186 BT_OBJECT_PUT_REF_AND_RESET(with_int_sel_fc
->range_set
);
1194 struct bt_field_class
*create_option_field_class(
1195 struct bt_trace_class
*trace_class
,
1196 enum bt_field_class_type fc_type
,
1197 struct bt_field_class
*content_fc
,
1198 struct bt_field_class
*selector_fc
,
1199 const char *api_func
)
1201 struct bt_field_class_option
*opt_fc
= NULL
;
1203 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
1204 BT_ASSERT_PRE_TC_NON_NULL_FROM_FUNC(api_func
, trace_class
);
1205 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "content-field-class",
1206 content_fc
, "Content field class");
1207 BT_LIB_LOGD("Creating option field class: "
1208 "type=%s, %![content-fc-]+F, %![sel-fc-]+F",
1209 bt_common_field_class_type_string(fc_type
),
1210 content_fc
, selector_fc
);
1212 if (fc_type
!= BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
) {
1213 struct bt_field_class_option_with_selector_field
*opt_with_sel_fc
= NULL
;
1215 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
,
1216 "selector-field-class", selector_fc
,
1217 "Selector field class");
1219 if (fc_type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
) {
1220 BT_ASSERT_PRE_FC_HAS_TYPE_FROM_FUNC(api_func
,
1221 "selector-field-class", selector_fc
,
1222 "boolean-field-class", BT_FIELD_CLASS_TYPE_BOOL
,
1223 "Selector field class");
1224 opt_with_sel_fc
= (void *) g_new0(
1225 struct bt_field_class_option_with_selector_field_bool
, 1);
1227 BT_ASSERT_PRE_FC_IS_INT_FROM_FUNC(api_func
,
1228 "selector-field-class",
1229 selector_fc
, "Selector field class");
1230 opt_with_sel_fc
= (void *) g_new0(
1231 struct bt_field_class_option_with_selector_field_integer
, 1);
1234 if (!opt_with_sel_fc
) {
1235 BT_LIB_LOGE_APPEND_CAUSE(
1236 "Failed to allocate one option with selector field class.");
1240 opt_with_sel_fc
->selector_fc
= selector_fc
;
1241 bt_object_get_ref_no_null_check(opt_with_sel_fc
->selector_fc
);
1242 opt_fc
= (void *) opt_with_sel_fc
;
1244 opt_fc
= g_new0(struct bt_field_class_option
, 1);
1246 BT_LIB_LOGE_APPEND_CAUSE(
1247 "Failed to allocate one option field class.");
1254 if (init_field_class((void *) opt_fc
, fc_type
,
1255 destroy_option_field_class
)) {
1259 opt_fc
->content_fc
= content_fc
;
1260 bt_object_get_ref_no_null_check(opt_fc
->content_fc
);
1261 bt_field_class_freeze(opt_fc
->content_fc
);
1264 bt_field_class_freeze(selector_fc
);
1267 BT_LIB_LOGD("Created option field class object: "
1268 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc
, selector_fc
);
1272 BT_OBJECT_PUT_REF_AND_RESET(opt_fc
);
1275 return (void *) opt_fc
;
1278 struct bt_field_class
*bt_field_class_option_without_selector_create(
1279 struct bt_trace_class
*trace_class
,
1280 struct bt_field_class
*content_fc
)
1282 return create_option_field_class(trace_class
,
1283 BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
,
1284 content_fc
, NULL
, __func__
);
1287 struct bt_field_class
*bt_field_class_option_with_selector_field_bool_create(
1288 struct bt_trace_class
*trace_class
,
1289 struct bt_field_class
*content_fc
,
1290 struct bt_field_class
*selector_fc
)
1292 BT_ASSERT_PRE_NO_ERROR();
1294 return create_option_field_class(trace_class
,
1295 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
,
1296 content_fc
, selector_fc
, __func__
);
1299 struct bt_field_class
*
1300 bt_field_class_option_with_selector_field_integer_unsigned_create(
1301 struct bt_trace_class
*trace_class
,
1302 struct bt_field_class
*content_fc
,
1303 struct bt_field_class
*selector_fc
,
1304 const struct bt_integer_range_set_unsigned
*u_range_set
)
1306 struct bt_field_class_option_with_selector_field_integer
*fc
;
1307 const struct bt_integer_range_set
*range_set
=
1308 (const void *) u_range_set
;
1310 BT_ASSERT_PRE_NO_ERROR();
1311 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(range_set
);
1312 BT_ASSERT_PRE_INT_RANGE_SET_NOT_EMPTY(range_set
);
1313 fc
= (void *) create_option_field_class(trace_class
,
1314 BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1315 content_fc
, selector_fc
, __func__
);
1321 fc
->range_set
= range_set
;
1322 bt_object_get_ref_no_null_check(fc
->range_set
);
1323 bt_integer_range_set_freeze(range_set
);
1329 struct bt_field_class
*
1330 bt_field_class_option_with_selector_field_integer_signed_create(
1331 struct bt_trace_class
*trace_class
,
1332 struct bt_field_class
*content_fc
,
1333 struct bt_field_class
*selector_fc
,
1334 const struct bt_integer_range_set_signed
*i_range_set
)
1336 struct bt_field_class_option_with_selector_field_integer
*fc
;
1337 const struct bt_integer_range_set
*range_set
=
1338 (const void *) i_range_set
;
1340 BT_ASSERT_PRE_NO_ERROR();
1341 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(range_set
);
1342 BT_ASSERT_PRE_INT_RANGE_SET_NOT_EMPTY(range_set
);
1343 fc
= (void *) create_option_field_class(trace_class
,
1344 BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1345 content_fc
, selector_fc
, __func__
);
1351 fc
->range_set
= range_set
;
1352 bt_object_get_ref_no_null_check(fc
->range_set
);
1353 bt_integer_range_set_freeze(range_set
);
1359 const struct bt_field_class
*bt_field_class_option_borrow_field_class_const(
1360 const struct bt_field_class
*fc
)
1362 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1364 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1365 BT_ASSERT_PRE_FC_IS_OPTION("field-class", fc
, "Field class");
1366 return opt_fc
->content_fc
;
1369 struct bt_field_class
*bt_field_class_option_borrow_field_class(
1370 struct bt_field_class
*fc
)
1372 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1374 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1375 BT_ASSERT_PRE_FC_IS_OPTION("field-class", fc
, "Field class");
1376 return opt_fc
->content_fc
;
1379 const struct bt_field_path
*
1380 bt_field_class_option_with_selector_field_borrow_selector_field_path_const(
1381 const struct bt_field_class
*fc
)
1383 const struct bt_field_class_option_with_selector_field
*opt_fc
=
1386 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1387 BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL("field-class", fc
, "Field class");
1388 return opt_fc
->selector_field_path
;
1391 void bt_field_class_option_with_selector_field_bool_set_selector_is_reversed(
1392 struct bt_field_class
*fc
, bt_bool sel_is_reversed
)
1394 struct bt_field_class_option_with_selector_field_bool
*opt_fc
= (void *) fc
;
1396 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1397 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1398 "option-field-class-with-boolean-selector-field",
1399 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
,
1401 BT_ASSERT_PRE_DEV_FC_HOT(fc
);
1402 opt_fc
->sel_is_reversed
= sel_is_reversed
;
1405 bt_bool
bt_field_class_option_with_selector_field_bool_selector_is_reversed(
1406 const struct bt_field_class
*fc
)
1408 struct bt_field_class_option_with_selector_field_bool
*opt_fc
= (void *) fc
;
1410 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1411 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1412 "option-field-class-with-boolean-selector-field",
1413 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
,
1415 return opt_fc
->sel_is_reversed
;
1418 const struct bt_integer_range_set_unsigned
*
1419 bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(
1420 const struct bt_field_class
*fc
)
1422 struct bt_field_class_option_with_selector_field_integer
*opt_fc
=
1425 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1426 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL("field-class", fc
,
1428 return (const void *) opt_fc
->range_set
;
1431 const struct bt_integer_range_set_signed
*
1432 bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(
1433 const struct bt_field_class
*fc
)
1435 struct bt_field_class_option_with_selector_field_integer
*opt_fc
=
1438 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1439 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL("field-class", fc
,
1441 return (const void *) opt_fc
->range_set
;
1445 void finalize_variant_field_class(struct bt_field_class_variant
*var_fc
)
1448 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc
);
1449 finalize_field_class((void *) var_fc
);
1450 finalize_named_field_classes_container((void *) var_fc
);
1454 void destroy_variant_field_class(struct bt_object
*obj
)
1456 struct bt_field_class_variant
*fc
= (void *) obj
;
1459 finalize_variant_field_class(fc
);
1464 void destroy_variant_with_selector_field_field_class(struct bt_object
*obj
)
1466 struct bt_field_class_variant_with_selector_field
*fc
= (void *) obj
;
1469 finalize_variant_field_class(&fc
->common
);
1470 BT_LOGD_STR("Putting selector field path.");
1471 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_field_path
);
1472 BT_LOGD_STR("Putting selector field class.");
1473 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_fc
);
1477 struct bt_field_class
*bt_field_class_variant_create(
1478 bt_trace_class
*trace_class
, bt_field_class
*selector_fc
)
1481 struct bt_field_class_variant
*var_fc
= NULL
;
1482 struct bt_field_class_variant_with_selector_field
*var_with_sel_fc
= NULL
;
1483 enum bt_field_class_type fc_type
;
1485 BT_ASSERT_PRE_NO_ERROR();
1486 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
1489 BT_ASSERT_PRE_FC_IS_INT("selector-field-class", selector_fc
,
1490 "Selector field class");
1493 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1497 var_with_sel_fc
= g_new0(
1498 struct bt_field_class_variant_with_selector_field
, 1);
1499 if (!var_with_sel_fc
) {
1500 BT_LIB_LOGE_APPEND_CAUSE(
1501 "Failed to allocate one variant field class with selector.");
1505 if (bt_field_class_type_is(selector_fc
->type
,
1506 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
1507 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
;
1509 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
;
1512 ret
= init_named_field_classes_container(
1513 (void *) var_with_sel_fc
, fc_type
,
1514 destroy_variant_with_selector_field_field_class
,
1515 destroy_variant_with_selector_field_option
);
1517 /* init_named_field_classes_container() logs errors */
1521 var_with_sel_fc
->selector_fc
= selector_fc
;
1522 bt_object_get_ref_no_null_check(var_with_sel_fc
->selector_fc
);
1523 bt_field_class_freeze(selector_fc
);
1524 var_fc
= (void *) var_with_sel_fc
;
1525 BT_LIB_LOGD("Created default variant field class with selector object: "
1526 "%![var-fc-]+F, %![sel-fc-]+F", var_fc
, selector_fc
);
1528 var_fc
= g_new0(struct bt_field_class_variant
, 1);
1530 BT_LIB_LOGE_APPEND_CAUSE(
1531 "Failed to allocate one variant field class without selector.");
1535 ret
= init_named_field_classes_container((void *) var_fc
,
1536 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
,
1537 destroy_variant_field_class
, destroy_named_field_class
);
1539 /* init_named_field_classes_container() logs errors */
1542 BT_LIB_LOGD("Created default variant field class without selector object: "
1543 "%![var-fc-]+F", var_fc
);
1550 BT_OBJECT_PUT_REF_AND_RESET(var_fc
);
1551 BT_OBJECT_PUT_REF_AND_RESET(var_with_sel_fc
);
1554 return (void *) var_fc
;
1557 #define VAR_FC_OPT_NAME_IS_UNIQUE_ID \
1558 "variant-field-class-option-name-is-unique"
1560 enum bt_field_class_variant_without_selector_append_option_status
1561 bt_field_class_variant_without_selector_append_option(struct bt_field_class
*fc
,
1562 const char *name
, struct bt_field_class
*option_fc
)
1564 enum bt_field_class_variant_without_selector_append_option_status status
;
1565 struct bt_named_field_class
*named_fc
= NULL
;
1567 BT_ASSERT_PRE_NO_ERROR();
1568 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1569 BT_ASSERT_PRE_NAME_NON_NULL(name
);
1570 BT_ASSERT_PRE_NON_NULL("option-field-class", option_fc
,
1571 "Option field class");
1572 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1573 "variant-field-class-without-selector-field",
1574 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
,
1576 named_fc
= create_named_field_class(name
, option_fc
);
1578 /* create_named_field_class() logs errors */
1579 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1583 status
= append_named_field_class_to_container_field_class((void *) fc
,
1584 named_fc
, __func__
, VAR_FC_OPT_NAME_IS_UNIQUE_ID
);
1585 if (status
== BT_FUNC_STATUS_OK
) {
1586 /* Moved to the container */
1592 destroy_named_field_class(named_fc
);
1599 int ranges_overlap(GPtrArray
*var_fc_opts
, const struct bt_integer_range_set
*range_set
,
1600 bool is_signed
, bool *has_overlap
)
1602 int status
= BT_FUNC_STATUS_OK
;
1603 struct bt_integer_range_set
*full_range_set
;
1606 *has_overlap
= false;
1609 * Build a single range set with all the ranges and test for
1613 full_range_set
= (void *) bt_integer_range_set_signed_create();
1615 full_range_set
= (void *) bt_integer_range_set_unsigned_create();
1618 if (!full_range_set
) {
1619 BT_LOGE_STR("Failed to create a range set.");
1620 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1624 /* Add existing option ranges */
1625 for (i
= 0; i
< var_fc_opts
->len
; i
++) {
1626 struct bt_field_class_variant_with_selector_field_option
*opt
=
1627 var_fc_opts
->pdata
[i
];
1630 for (j
= 0; j
< opt
->range_set
->ranges
->len
; j
++) {
1631 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1635 status
= bt_integer_range_set_signed_add_range(
1636 (void *) full_range_set
, range
->lower
.i
,
1639 status
= bt_integer_range_set_unsigned_add_range(
1640 (void *) full_range_set
, range
->lower
.u
,
1650 /* Add new ranges */
1651 for (i
= 0; i
< range_set
->ranges
->len
; i
++) {
1652 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1656 status
= bt_integer_range_set_signed_add_range(
1657 (void *) full_range_set
, range
->lower
.i
,
1660 status
= bt_integer_range_set_unsigned_add_range(
1661 (void *) full_range_set
, range
->lower
.u
,
1670 /* Check overlaps */
1672 *has_overlap
= bt_integer_range_set_signed_has_overlaps(full_range_set
);
1674 *has_overlap
= bt_integer_range_set_unsigned_has_overlaps(
1679 bt_object_put_ref(full_range_set
);
1684 int append_option_to_variant_with_selector_field_field_class(
1685 struct bt_field_class
*fc
, const char *name
,
1686 struct bt_field_class
*option_fc
,
1687 const struct bt_integer_range_set
*range_set
,
1688 enum bt_field_class_type expected_type
,
1689 const char *api_func
)
1692 struct bt_field_class_variant_with_selector_field
*var_fc
= (void *) fc
;
1693 struct bt_field_class_variant_with_selector_field_option
*opt
= NULL
;
1697 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
1698 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "option-field-class",
1699 option_fc
, "Option field class");
1700 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL_FROM_FUNC(api_func
, range_set
);
1701 BT_ASSERT_PRE_INT_RANGE_SET_NOT_EMPTY_FROM_FUNC(api_func
, range_set
);
1702 status
= ranges_overlap(var_fc
->common
.common
.named_fcs
, range_set
,
1703 expected_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1706 /* ranges_overlap() logs errors */
1710 BT_ASSERT_PRE_FROM_FUNC(api_func
, "ranges-do-not-overlap",
1712 "Integer range set's ranges and existing ranges have an overlap: "
1714 opt
= create_variant_with_selector_field_option(name
, option_fc
, range_set
);
1716 /* create_variant_with_selector_field_option() logs errors */
1717 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1721 status
= append_named_field_class_to_container_field_class((void *) fc
,
1722 &opt
->common
, __func__
, VAR_FC_OPT_NAME_IS_UNIQUE_ID
);
1723 if (status
== BT_FUNC_STATUS_OK
) {
1724 /* Moved to the container */
1730 destroy_variant_with_selector_field_option(opt
);
1736 enum bt_field_class_variant_with_selector_field_integer_append_option_status
1737 bt_field_class_variant_with_selector_field_integer_unsigned_append_option(
1738 struct bt_field_class
*fc
, const char *name
,
1739 struct bt_field_class
*option_fc
,
1740 const struct bt_integer_range_set_unsigned
*range_set
)
1742 BT_ASSERT_PRE_NO_ERROR();
1743 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1744 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1745 "variant-field-class-with-unsigned-integer-selector-field",
1746 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1748 return append_option_to_variant_with_selector_field_field_class(fc
,
1749 name
, option_fc
, (const void *) range_set
,
1750 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1754 enum bt_field_class_variant_with_selector_field_integer_append_option_status
1755 bt_field_class_variant_with_selector_field_integer_signed_append_option(
1756 struct bt_field_class
*fc
, const char *name
,
1757 struct bt_field_class
*option_fc
,
1758 const struct bt_integer_range_set_signed
*range_set
)
1760 BT_ASSERT_PRE_NO_ERROR();
1761 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1762 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1763 "variant-field-class-with-signed-integer-selector-field",
1764 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1766 return append_option_to_variant_with_selector_field_field_class(fc
,
1767 name
, option_fc
, (const void *) range_set
,
1768 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1772 uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class
*fc
)
1774 const struct bt_field_class_variant
*var_fc
= (const void *) fc
;
1776 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1777 BT_ASSERT_PRE_DEV_FC_IS_VARIANT("field-class", fc
, "Field class");
1778 return (uint64_t) var_fc
->common
.named_fcs
->len
;
1781 const struct bt_field_class_variant_option
*
1782 bt_field_class_variant_borrow_option_by_name_const(
1783 const struct bt_field_class
*fc
, const char *name
)
1785 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1786 BT_ASSERT_PRE_DEV_FC_IS_VARIANT("field-class", fc
, "Field class");
1787 return (const void *)
1788 borrow_named_field_class_from_container_field_class_by_name(
1789 (void *) fc
, name
, __func__
);
1792 const struct bt_field_class_variant_option
*
1793 bt_field_class_variant_borrow_option_by_index_const(
1794 const struct bt_field_class
*fc
, uint64_t index
)
1796 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1797 BT_ASSERT_PRE_DEV_FC_IS_VARIANT("field-class", fc
, "Field class");
1798 return (const void *)
1799 borrow_named_field_class_from_container_field_class_at_index(
1800 (void *) fc
, index
, __func__
);
1803 struct bt_field_class_variant_option
*
1804 bt_field_class_variant_borrow_option_by_name(
1805 struct bt_field_class
*fc
, const char *name
)
1807 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1808 BT_ASSERT_PRE_DEV_FC_IS_VARIANT("field-class", fc
, "Field class");
1810 borrow_named_field_class_from_container_field_class_by_name(
1811 (void *) fc
, name
, __func__
);
1814 struct bt_field_class_variant_option
*
1815 bt_field_class_variant_borrow_option_by_index(
1816 struct bt_field_class
*fc
, uint64_t index
)
1818 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1819 BT_ASSERT_PRE_DEV_FC_IS_VARIANT("field-class", fc
, "Field class");
1821 borrow_named_field_class_from_container_field_class_at_index(
1822 (void *) fc
, index
, __func__
);
1825 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*
1826 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const(
1827 const struct bt_field_class
*fc
, const char *name
)
1829 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1830 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1831 "variant-field-class-with-unsigned-integer-selector-field",
1832 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1834 return (const void *)
1835 borrow_named_field_class_from_container_field_class_by_name(
1836 (void *) fc
, name
, __func__
);
1839 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*
1840 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
1841 const struct bt_field_class
*fc
, uint64_t index
)
1843 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1844 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1845 "variant-field-class-with-unsigned-integer-selector-field",
1846 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1848 return (const void *)
1849 borrow_named_field_class_from_container_field_class_at_index(
1850 (void *) fc
, index
, __func__
);
1853 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*
1854 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const(
1855 const struct bt_field_class
*fc
, const char *name
)
1857 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1858 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1859 "variant-field-class-with-signed-integer-selector-field",
1860 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1862 return (const void *)
1863 borrow_named_field_class_from_container_field_class_by_name(
1864 (void *) fc
, name
, __func__
);
1867 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*
1868 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
1869 const struct bt_field_class
*fc
, uint64_t index
)
1871 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1872 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
1873 "variant-field-class-with-signed-integer-selector-field",
1874 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1876 return (const void *)
1877 borrow_named_field_class_from_container_field_class_at_index(
1878 (void *) fc
, index
, __func__
);
1881 const char *bt_field_class_variant_option_get_name(
1882 const struct bt_field_class_variant_option
*option
)
1884 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1886 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1887 return named_fc
->name
->str
;
1890 const struct bt_field_class
*
1891 bt_field_class_variant_option_borrow_field_class_const(
1892 const struct bt_field_class_variant_option
*option
)
1894 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1896 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1897 return named_fc
->fc
;
1900 struct bt_field_class
*
1901 bt_field_class_variant_option_borrow_field_class(
1902 struct bt_field_class_variant_option
*option
)
1904 struct bt_named_field_class
*named_fc
= (void *) option
;
1906 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1907 return named_fc
->fc
;
1910 const struct bt_integer_range_set_unsigned
*
1911 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
1912 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*option
)
1914 const struct bt_field_class_variant_with_selector_field_option
*opt
=
1915 (const void *) option
;
1917 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1918 return (const void *) opt
->range_set
;
1921 const struct bt_integer_range_set_signed
*
1922 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
1923 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*option
)
1925 const struct bt_field_class_variant_with_selector_field_option
*opt
=
1926 (const void *) option
;
1928 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1929 return (const void *) opt
->range_set
;
1932 const struct bt_field_path
*
1933 bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
1934 const struct bt_field_class
*fc
)
1936 const struct bt_field_class_variant_with_selector_field
*var_fc
=
1939 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1940 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL("field-class", fc
,
1942 return var_fc
->selector_field_path
;
1946 int init_array_field_class(struct bt_field_class_array
*fc
,
1947 enum bt_field_class_type type
, bt_object_release_func release_func
,
1948 struct bt_field_class
*element_fc
)
1952 BT_ASSERT(element_fc
);
1953 ret
= init_field_class((void *) fc
, type
, release_func
);
1958 fc
->element_fc
= element_fc
;
1959 bt_object_get_ref_no_null_check(fc
->element_fc
);
1960 bt_field_class_freeze(element_fc
);
1967 void finalize_array_field_class(struct bt_field_class_array
*array_fc
)
1969 BT_ASSERT(array_fc
);
1970 BT_LOGD_STR("Putting element field class.");
1971 finalize_field_class((void *) array_fc
);
1972 BT_OBJECT_PUT_REF_AND_RESET(array_fc
->element_fc
);
1976 void destroy_static_array_field_class(struct bt_object
*obj
)
1979 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj
);
1980 finalize_array_field_class((void *) obj
);
1984 struct bt_field_class
*
1985 bt_field_class_array_static_create(bt_trace_class
*trace_class
,
1986 struct bt_field_class
*element_fc
, uint64_t length
)
1988 struct bt_field_class_array_static
*array_fc
= NULL
;
1990 BT_ASSERT_PRE_NO_ERROR();
1991 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
1992 BT_ASSERT_PRE_NON_NULL("element-field-class", element_fc
,
1993 "Element field class");
1994 BT_LOGD_STR("Creating default static array field class object.");
1995 array_fc
= g_new0(struct bt_field_class_array_static
, 1);
1997 BT_LIB_LOGE_APPEND_CAUSE(
1998 "Failed to allocate one static array field class.");
2002 if (init_array_field_class((void *) array_fc
,
2003 BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
2004 destroy_static_array_field_class
, element_fc
)) {
2008 array_fc
->length
= length
;
2009 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc
);
2013 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
2016 return (void *) array_fc
;
2019 const struct bt_field_class
*
2020 bt_field_class_array_borrow_element_field_class_const(
2021 const struct bt_field_class
*fc
)
2023 const struct bt_field_class_array
*array_fc
= (const void *) fc
;
2025 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2026 BT_ASSERT_PRE_DEV_FC_IS_ARRAY("field-class", fc
, "Field class");
2027 return array_fc
->element_fc
;
2030 struct bt_field_class
*
2031 bt_field_class_array_borrow_element_field_class(struct bt_field_class
*fc
)
2033 struct bt_field_class_array
*array_fc
= (void *) fc
;
2035 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2036 BT_ASSERT_PRE_DEV_FC_IS_ARRAY("field-class", fc
, "Field class");
2037 return array_fc
->element_fc
;
2040 uint64_t bt_field_class_array_static_get_length(const struct bt_field_class
*fc
)
2042 const struct bt_field_class_array_static
*array_fc
= (const void *) fc
;
2044 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2045 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
2046 "static-array-field-class", BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
2048 return (uint64_t) array_fc
->length
;
2052 void destroy_dynamic_array_field_class(struct bt_object
*obj
)
2054 struct bt_field_class_array_dynamic
*fc
= (void *) obj
;
2057 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc
);
2058 finalize_array_field_class((void *) fc
);
2059 BT_LOGD_STR("Putting length field path.");
2060 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_field_path
);
2061 BT_LOGD_STR("Putting length field class.");
2062 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_fc
);
2066 struct bt_field_class
*bt_field_class_array_dynamic_create(
2067 struct bt_trace_class
*trace_class
,
2068 struct bt_field_class
*element_fc
,
2069 struct bt_field_class
*length_fc
)
2071 struct bt_field_class_array_dynamic
*array_fc
= NULL
;
2073 BT_ASSERT_PRE_NO_ERROR();
2074 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
2075 BT_ASSERT_PRE_NON_NULL("element-field-class", element_fc
,
2076 "Element field class");
2077 BT_LOGD_STR("Creating default dynamic array field class object.");
2078 array_fc
= g_new0(struct bt_field_class_array_dynamic
, 1);
2080 BT_LIB_LOGE_APPEND_CAUSE(
2081 "Failed to allocate one dynamic array field class.");
2085 if (init_array_field_class((void *) array_fc
,
2087 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
2088 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
,
2089 destroy_dynamic_array_field_class
, element_fc
)) {
2094 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT("length-field-class",
2095 length_fc
, "Length field class");
2096 array_fc
->length_fc
= length_fc
;
2097 bt_object_get_ref_no_null_check(array_fc
->length_fc
);
2098 bt_field_class_freeze(length_fc
);
2101 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc
);
2105 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
2108 return (void *) array_fc
;
2111 const struct bt_field_path
*
2112 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
2113 const struct bt_field_class
*fc
)
2115 const struct bt_field_class_array_dynamic
*seq_fc
= (const void *) fc
;
2117 BT_ASSERT_PRE_NO_ERROR();
2118 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2119 BT_ASSERT_PRE_FC_HAS_TYPE("field-class", fc
,
2120 "dynamic-array-field-class-with-length-field",
2121 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
,
2123 return seq_fc
->length_field_path
;
2127 void destroy_string_field_class(struct bt_object
*obj
)
2130 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
2131 finalize_field_class((void *) obj
);
2135 struct bt_field_class
*bt_field_class_string_create(bt_trace_class
*trace_class
)
2137 struct bt_field_class_string
*string_fc
= NULL
;
2139 BT_ASSERT_PRE_NO_ERROR();
2140 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
2141 BT_LOGD_STR("Creating default string field class object.");
2142 string_fc
= g_new0(struct bt_field_class_string
, 1);
2144 BT_LIB_LOGE_APPEND_CAUSE(
2145 "Failed to allocate one string field class.");
2149 if (init_field_class((void *) string_fc
, BT_FIELD_CLASS_TYPE_STRING
,
2150 destroy_string_field_class
)) {
2154 BT_LIB_LOGD("Created string field class object: %!+F", string_fc
);
2158 BT_OBJECT_PUT_REF_AND_RESET(string_fc
);
2161 return (void *) string_fc
;
2165 void _bt_field_class_freeze(const struct bt_field_class
*c_fc
)
2167 struct bt_field_class
*fc
= (void *) c_fc
;
2170 * Element/member/option field classes are frozen when added to
2174 bt_value_freeze(fc
->user_attributes
);
2177 if (fc
->type
== BT_FIELD_CLASS_TYPE_STRUCTURE
||
2178 bt_field_class_type_is(fc
->type
,
2179 BT_FIELD_CLASS_TYPE_VARIANT
)) {
2180 struct bt_field_class_named_field_class_container
*container_fc
=
2184 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
2185 bt_named_field_class_freeze(
2186 container_fc
->named_fcs
->pdata
[i
]);
2192 void _bt_named_field_class_freeze(const struct bt_named_field_class
*named_fc
)
2194 BT_ASSERT(named_fc
);
2195 BT_ASSERT(named_fc
->fc
->frozen
);
2196 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
2197 named_fc
->user_attributes
);
2198 bt_value_freeze(named_fc
->user_attributes
);
2199 ((struct bt_named_field_class
*) named_fc
)->frozen
= true;
2203 void bt_field_class_make_part_of_trace_class(const struct bt_field_class
*c_fc
)
2205 struct bt_field_class
*fc
= (void *) c_fc
;
2208 BT_ASSERT_PRE("field-class-is-not-part-of-trace-class",
2209 !fc
->part_of_trace_class
,
2210 "Field class is already part of a trace class: %!+F", fc
);
2211 fc
->part_of_trace_class
= true;
2213 if (fc
->type
== BT_FIELD_CLASS_TYPE_STRUCTURE
||
2214 bt_field_class_type_is(fc
->type
,
2215 BT_FIELD_CLASS_TYPE_VARIANT
)) {
2216 struct bt_field_class_named_field_class_container
*container_fc
=
2220 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
2221 struct bt_named_field_class
*named_fc
=
2222 container_fc
->named_fcs
->pdata
[i
];
2224 bt_field_class_make_part_of_trace_class(named_fc
->fc
);
2226 } else if (bt_field_class_type_is(fc
->type
,
2227 BT_FIELD_CLASS_TYPE_ARRAY
)) {
2228 struct bt_field_class_array
*array_fc
= (void *) fc
;
2230 bt_field_class_make_part_of_trace_class(array_fc
->element_fc
);
2234 const struct bt_value
*bt_field_class_borrow_user_attributes_const(
2235 const struct bt_field_class
*fc
)
2237 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2238 return fc
->user_attributes
;
2241 struct bt_value
*bt_field_class_borrow_user_attributes(
2242 struct bt_field_class
*field_class
)
2244 return (void *) bt_field_class_borrow_user_attributes_const(
2245 (void *) field_class
);
2249 void bt_field_class_set_user_attributes(
2250 struct bt_field_class
*fc
,
2251 const struct bt_value
*user_attributes
)
2253 BT_ASSERT_PRE_FC_NON_NULL(fc
);
2254 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
2255 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
2256 BT_ASSERT_PRE_DEV_FC_HOT(fc
);
2257 bt_object_put_ref_no_null_check(fc
->user_attributes
);
2258 fc
->user_attributes
= (void *) user_attributes
;
2259 bt_object_get_ref_no_null_check(fc
->user_attributes
);
2263 const struct bt_value
*bt_named_field_class_borrow_user_attributes_const(
2264 const struct bt_named_field_class
*named_fc
)
2266 return named_fc
->user_attributes
;
2270 void set_named_field_class_user_attributes(
2271 struct bt_named_field_class
*named_fc
,
2272 const struct bt_value
*user_attributes
, const char *api_func
)
2274 BT_ASSERT_PRE_USER_ATTRS_NON_NULL_FROM_FUNC(api_func
, user_attributes
);
2275 BT_ASSERT_PRE_USER_ATTRS_NON_NULL_FROM_FUNC(api_func
, user_attributes
);
2276 bt_object_put_ref_no_null_check(named_fc
->user_attributes
);
2277 named_fc
->user_attributes
= (void *) user_attributes
;
2278 bt_object_get_ref_no_null_check(named_fc
->user_attributes
);
2281 const struct bt_value
*
2282 bt_field_class_structure_member_borrow_user_attributes_const(
2283 const struct bt_field_class_structure_member
*member
)
2285 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2286 return bt_named_field_class_borrow_user_attributes_const(
2287 (const void *) member
);
2291 bt_field_class_structure_member_borrow_user_attributes(
2292 struct bt_field_class_structure_member
*member
)
2294 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2295 return (void *) bt_named_field_class_borrow_user_attributes_const(
2299 void bt_field_class_structure_member_set_user_attributes(
2300 struct bt_field_class_structure_member
*member
,
2301 const struct bt_value
*user_attributes
)
2303 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2304 BT_ASSERT_PRE_DEV_HOT("structure-field-class-member",
2305 (struct bt_named_field_class
*) member
,
2306 "Structure field class member", ".");
2307 set_named_field_class_user_attributes((void *) member
,
2308 user_attributes
, __func__
);
2311 const struct bt_value
*bt_field_class_variant_option_borrow_user_attributes_const(
2312 const struct bt_field_class_variant_option
*option
)
2314 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2315 return bt_named_field_class_borrow_user_attributes_const(
2316 (const void *) option
);
2319 struct bt_value
*bt_field_class_variant_option_borrow_user_attributes(
2320 struct bt_field_class_variant_option
*option
)
2322 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2323 return (void *) bt_named_field_class_borrow_user_attributes_const(
2327 void bt_field_class_variant_option_set_user_attributes(
2328 struct bt_field_class_variant_option
*option
,
2329 const struct bt_value
*user_attributes
)
2331 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2332 BT_ASSERT_PRE_DEV_HOT("variant-field-class-option",
2333 (struct bt_named_field_class
*) option
,
2334 "Variant field class option", ".");
2335 set_named_field_class_user_attributes((void *) option
,
2336 user_attributes
, __func__
);
2339 void bt_field_class_get_ref(const struct bt_field_class
*field_class
)
2341 bt_object_get_ref(field_class
);
2344 void bt_field_class_put_ref(const struct bt_field_class
*field_class
)
2346 bt_object_put_ref(field_class
);