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(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(fc
, BT_FIELD_CLASS_TYPE_BIT_ARRAY
,
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(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(fc
, "Field class");
263 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
264 BT_ASSERT_PRE(size
>= 1 && size
<= 64,
265 "Unsupported size for integer field class's field value range "
266 "(minimum is 1, maximum is 64): size=%" PRIu64
, size
);
268 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
269 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
270 size_is_valid_for_enumeration_field_class(fc
, size
),
271 "Invalid field value range for enumeration field class: "
272 "at least one of the current mapping ranges contains values "
273 "which are outside this range: %!+F, size=%" PRIu64
, fc
, size
);
274 int_fc
->range
= size
;
275 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc
);
278 enum bt_field_class_integer_preferred_display_base
279 bt_field_class_integer_get_preferred_display_base(const struct bt_field_class
*fc
)
281 const struct bt_field_class_integer
*int_fc
= (const void *) fc
;
283 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
284 BT_ASSERT_PRE_DEV_FC_IS_INT(fc
, "Field class");
288 void bt_field_class_integer_set_preferred_display_base(
289 struct bt_field_class
*fc
,
290 enum bt_field_class_integer_preferred_display_base base
)
292 struct bt_field_class_integer
*int_fc
= (void *) fc
;
294 BT_ASSERT_PRE_FC_NON_NULL(fc
);
295 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
296 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
298 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc
);
302 void finalize_enumeration_field_class_mapping(
303 struct bt_field_class_enumeration_mapping
*mapping
)
307 if (mapping
->label
) {
308 g_string_free(mapping
->label
, TRUE
);
309 mapping
->label
= NULL
;
312 BT_OBJECT_PUT_REF_AND_RESET(mapping
->range_set
);
316 void destroy_enumeration_field_class(struct bt_object
*obj
)
318 struct bt_field_class_enumeration
*fc
= (void *) obj
;
321 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc
);
322 finalize_field_class((void *) obj
);
327 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
328 finalize_enumeration_field_class_mapping(
329 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
));
332 g_array_free(fc
->mappings
, TRUE
);
337 g_ptr_array_free(fc
->label_buf
, TRUE
);
338 fc
->label_buf
= NULL
;
345 struct bt_field_class
*create_enumeration_field_class(
346 bt_trace_class
*trace_class
, enum bt_field_class_type type
)
348 struct bt_field_class_enumeration
*enum_fc
= NULL
;
350 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
351 BT_LOGD("Creating default enumeration field class object: type=%s",
352 bt_common_field_class_type_string(type
));
353 enum_fc
= g_new0(struct bt_field_class_enumeration
, 1);
355 BT_LIB_LOGE_APPEND_CAUSE(
356 "Failed to allocate one enumeration field class.");
360 if (init_integer_field_class((void *) enum_fc
, type
,
361 destroy_enumeration_field_class
)) {
365 enum_fc
->mappings
= g_array_new(FALSE
, TRUE
,
366 sizeof(struct bt_field_class_enumeration_mapping
));
367 if (!enum_fc
->mappings
) {
368 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
372 enum_fc
->label_buf
= g_ptr_array_new();
373 if (!enum_fc
->label_buf
) {
374 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
378 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc
);
382 BT_OBJECT_PUT_REF_AND_RESET(enum_fc
);
385 return (void *) enum_fc
;
388 struct bt_field_class
*bt_field_class_enumeration_unsigned_create(
389 bt_trace_class
*trace_class
)
391 BT_ASSERT_PRE_NO_ERROR();
393 return create_enumeration_field_class(trace_class
,
394 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
397 struct bt_field_class
*bt_field_class_enumeration_signed_create(
398 bt_trace_class
*trace_class
)
400 BT_ASSERT_PRE_NO_ERROR();
402 return create_enumeration_field_class(trace_class
,
403 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
406 uint64_t bt_field_class_enumeration_get_mapping_count(
407 const struct bt_field_class
*fc
)
409 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
411 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
412 BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc
, "Field class");
413 return (uint64_t) enum_fc
->mappings
->len
;
416 const struct bt_field_class_enumeration_unsigned_mapping
*
417 bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
418 const struct bt_field_class
*fc
, uint64_t index
)
420 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
422 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
423 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
424 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
426 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
429 const struct bt_field_class_enumeration_signed_mapping
*
430 bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
431 const struct bt_field_class
*fc
, uint64_t index
)
433 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
435 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
436 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
437 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
439 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
443 const struct bt_field_class_enumeration_mapping
*
444 borrow_enumeration_field_class_mapping_by_label(
445 const struct bt_field_class_enumeration
*fc
, const char *label
)
447 struct bt_field_class_enumeration_mapping
*mapping
= NULL
;
451 BT_ASSERT_PRE_DEV_NON_NULL(label
, "Label");
453 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
454 struct bt_field_class_enumeration_mapping
*this_mapping
=
455 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
);
457 if (strcmp(this_mapping
->label
->str
, label
) == 0) {
458 mapping
= this_mapping
;
467 const struct bt_field_class_enumeration_signed_mapping
*
468 bt_field_class_enumeration_signed_borrow_mapping_by_label_const(
469 const struct bt_field_class
*fc
, const char *label
)
471 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
472 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
474 return (const void *) borrow_enumeration_field_class_mapping_by_label(
475 (const void *) fc
, label
);
478 const struct bt_field_class_enumeration_unsigned_mapping
*
479 bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const(
480 const struct bt_field_class
*fc
, const char *label
)
482 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
483 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
484 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
485 return (const void *) borrow_enumeration_field_class_mapping_by_label(
486 (const void *) fc
, label
);
489 const char *bt_field_class_enumeration_mapping_get_label(
490 const struct bt_field_class_enumeration_mapping
*mapping
)
492 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
493 return mapping
->label
->str
;
496 const struct bt_integer_range_set_unsigned
*
497 bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
498 const struct bt_field_class_enumeration_unsigned_mapping
*u_mapping
)
500 const struct bt_field_class_enumeration_mapping
*mapping
=
501 (const void *) u_mapping
;
503 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
504 return (const void *) mapping
->range_set
;
507 const struct bt_integer_range_set_signed
*
508 bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
509 const struct bt_field_class_enumeration_signed_mapping
*s_mapping
)
511 const struct bt_field_class_enumeration_mapping
*mapping
=
512 (const void *) s_mapping
;
514 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
515 return (const void *) mapping
->range_set
;
518 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
519 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
520 const struct bt_field_class
*fc
, uint64_t value
,
521 bt_field_class_enumeration_mapping_label_array
*label_array
,
524 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
527 BT_ASSERT_PRE_DEV_NO_ERROR();
528 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
529 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
530 BT_ASSERT_PRE_DEV_NON_NULL(count
, "Count (output)");
531 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
533 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
535 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
537 const struct bt_field_class_enumeration_mapping
*mapping
=
538 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
540 for (j
= 0; j
< mapping
->range_set
->ranges
->len
; j
++) {
541 const struct bt_integer_range
*range
= (const void *)
542 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
543 mapping
->range_set
, j
);
545 if (value
>= range
->lower
.u
&&
546 value
<= range
->upper
.u
) {
547 g_ptr_array_add(enum_fc
->label_buf
,
548 mapping
->label
->str
);
554 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
555 *count
= (uint64_t) enum_fc
->label_buf
->len
;
556 return BT_FUNC_STATUS_OK
;
559 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
560 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
561 const struct bt_field_class
*fc
, int64_t value
,
562 bt_field_class_enumeration_mapping_label_array
*label_array
,
565 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
568 BT_ASSERT_PRE_DEV_NO_ERROR();
569 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
570 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
571 BT_ASSERT_PRE_DEV_NON_NULL(count
, "Count (output)");
572 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
574 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
576 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
578 const struct bt_field_class_enumeration_mapping
*mapping
=
579 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
581 for (j
= 0; j
< mapping
->range_set
->ranges
->len
; j
++) {
582 const struct bt_integer_range
*range
= (const void *)
583 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
584 mapping
->range_set
, j
);
586 if (value
>= range
->lower
.i
&&
587 value
<= range
->upper
.i
) {
588 g_ptr_array_add(enum_fc
->label_buf
,
589 mapping
->label
->str
);
595 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
596 *count
= (uint64_t) enum_fc
->label_buf
->len
;
597 return BT_FUNC_STATUS_OK
;
601 bool enumeration_field_class_has_mapping_with_label(
602 const struct bt_field_class_enumeration
*enum_fc
,
611 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
612 struct bt_field_class_enumeration_mapping
*mapping_candidate
=
613 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
615 if (strcmp(mapping_candidate
->label
->str
, label
) == 0) {
626 enum bt_field_class_enumeration_add_mapping_status
627 add_mapping_to_enumeration_field_class(struct bt_field_class
*fc
,
628 const char *label
, const struct bt_integer_range_set
*range_set
)
630 enum bt_field_class_enumeration_add_mapping_status status
=
632 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
633 struct bt_field_class_enumeration_mapping mapping
= { 0 };
635 BT_ASSERT_PRE_NO_ERROR();
637 BT_ASSERT_PRE_NON_NULL(label
, "Label");
638 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(range_set
);
639 BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label(
641 "Duplicate mapping name in enumeration field class: "
642 "%![enum-fc-]+F, label=\"%s\"", fc
, label
);
643 mapping
.range_set
= range_set
;
644 bt_object_get_ref(mapping
.range_set
);
645 mapping
.label
= g_string_new(label
);
646 if (!mapping
.label
) {
647 finalize_enumeration_field_class_mapping(&mapping
);
648 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
652 g_array_append_val(enum_fc
->mappings
, mapping
);
653 BT_LIB_LOGD("Added mapping to enumeration field class: "
654 "%![fc-]+F, label=\"%s\"", fc
, label
);
660 enum bt_field_class_enumeration_add_mapping_status
661 bt_field_class_enumeration_unsigned_add_mapping(
662 struct bt_field_class
*fc
, const char *label
,
663 const struct bt_integer_range_set_unsigned
*range_set
)
665 BT_ASSERT_PRE_NO_ERROR();
666 BT_ASSERT_PRE_FC_NON_NULL(fc
);
667 BT_ASSERT_PRE_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
669 return add_mapping_to_enumeration_field_class(fc
, label
,
670 (const void *) range_set
);
673 enum bt_field_class_enumeration_add_mapping_status
674 bt_field_class_enumeration_signed_add_mapping(
675 struct bt_field_class
*fc
, const char *label
,
676 const struct bt_integer_range_set_signed
*range_set
)
678 BT_ASSERT_PRE_NO_ERROR();
679 BT_ASSERT_PRE_FC_NON_NULL(fc
);
680 BT_ASSERT_PRE_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
682 return add_mapping_to_enumeration_field_class(fc
, label
,
683 (const void *) range_set
);
687 void destroy_real_field_class(struct bt_object
*obj
)
690 BT_LIB_LOGD("Destroying real field class object: %!+F", obj
);
691 finalize_field_class((void *) obj
);
696 struct bt_field_class
*create_real_field_class(bt_trace_class
*trace_class
,
697 enum bt_field_class_type type
)
699 struct bt_field_class_real
*real_fc
= NULL
;
701 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
702 BT_LOGD("Creating default real field class object: type=%s",
703 bt_common_field_class_type_string(type
));
704 real_fc
= g_new0(struct bt_field_class_real
, 1);
706 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
710 if (init_field_class((void *) real_fc
, type
, destroy_real_field_class
)) {
714 BT_LIB_LOGD("Created real field class object: %!+F", real_fc
);
718 BT_OBJECT_PUT_REF_AND_RESET(real_fc
);
721 return (void *) real_fc
;
724 struct bt_field_class
*bt_field_class_real_single_precision_create(
725 bt_trace_class
*trace_class
)
727 BT_ASSERT_PRE_NO_ERROR();
729 return create_real_field_class(trace_class
,
730 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
);
733 struct bt_field_class
*bt_field_class_real_double_precision_create(
734 bt_trace_class
*trace_class
)
736 BT_ASSERT_PRE_NO_ERROR();
738 return create_real_field_class(trace_class
,
739 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
);
743 int init_named_field_classes_container(
744 struct bt_field_class_named_field_class_container
*fc
,
745 enum bt_field_class_type type
,
746 bt_object_release_func fc_release_func
,
747 GDestroyNotify named_fc_destroy_func
)
751 ret
= init_field_class((void *) fc
, type
, fc_release_func
);
756 fc
->named_fcs
= g_ptr_array_new_with_free_func(named_fc_destroy_func
);
757 if (!fc
->named_fcs
) {
758 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
763 fc
->name_to_index
= g_hash_table_new(g_str_hash
, g_str_equal
);
764 if (!fc
->name_to_index
) {
765 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
775 void finalize_named_field_class(struct bt_named_field_class
*named_fc
)
778 BT_LIB_LOGD("Finalizing named field class: "
779 "addr=%p, name=\"%s\", %![fc-]+F",
780 named_fc
, named_fc
->name
? named_fc
->name
->str
: NULL
,
782 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
784 if (named_fc
->name
) {
785 g_string_free(named_fc
->name
, TRUE
);
786 named_fc
->name
= NULL
;
789 BT_LOGD_STR("Putting named field class's field class.");
790 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->fc
);
794 void destroy_named_field_class(gpointer ptr
)
796 struct bt_named_field_class
*named_fc
= ptr
;
799 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
800 finalize_named_field_class(ptr
);
806 void destroy_variant_with_selector_field_option(gpointer ptr
)
808 struct bt_field_class_variant_with_selector_field_option
*opt
= ptr
;
811 finalize_named_field_class(&opt
->common
);
812 BT_OBJECT_PUT_REF_AND_RESET(opt
->range_set
);
818 void finalize_named_field_classes_container(
819 struct bt_field_class_named_field_class_container
*fc
)
824 g_ptr_array_free(fc
->named_fcs
, TRUE
);
825 fc
->named_fcs
= NULL
;
829 if (fc
->name_to_index
) {
830 g_hash_table_destroy(fc
->name_to_index
);
831 fc
->name_to_index
= NULL
;
836 void destroy_structure_field_class(struct bt_object
*obj
)
839 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj
);
840 finalize_field_class((void *) obj
);
841 finalize_named_field_classes_container((void *) obj
);
845 struct bt_field_class
*bt_field_class_structure_create(
846 bt_trace_class
*trace_class
)
849 struct bt_field_class_structure
*struct_fc
= NULL
;
851 BT_ASSERT_PRE_NO_ERROR();
852 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
853 BT_LOGD_STR("Creating default structure field class object.");
854 struct_fc
= g_new0(struct bt_field_class_structure
, 1);
856 BT_LIB_LOGE_APPEND_CAUSE(
857 "Failed to allocate one structure field class.");
861 ret
= init_named_field_classes_container((void *) struct_fc
,
862 BT_FIELD_CLASS_TYPE_STRUCTURE
, destroy_structure_field_class
,
863 destroy_named_field_class
);
865 /* init_named_field_classes_container() logs errors */
869 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc
);
873 BT_OBJECT_PUT_REF_AND_RESET(struct_fc
);
876 return (void *) struct_fc
;
880 int init_named_field_class(struct bt_named_field_class
*named_fc
,
881 const char *name
, struct bt_field_class
*fc
)
883 int status
= BT_FUNC_STATUS_OK
;
888 named_fc
->name
= g_string_new(name
);
889 if (!named_fc
->name
) {
890 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
891 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
895 named_fc
->user_attributes
= bt_value_map_create();
896 if (!named_fc
->user_attributes
) {
897 BT_LIB_LOGE_APPEND_CAUSE(
898 "Failed to create a map value object.");
899 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
904 bt_object_get_ref_no_null_check(named_fc
->fc
);
911 struct bt_named_field_class
*create_named_field_class(const char *name
,
912 struct bt_field_class
*fc
)
914 struct bt_named_field_class
*named_fc
= g_new0(
915 struct bt_named_field_class
, 1);
918 BT_LIB_LOGE_APPEND_CAUSE(
919 "Failed to allocate a named field class.");
923 if (init_named_field_class(named_fc
, name
, fc
)) {
924 /* init_named_field_class() logs errors */
931 destroy_named_field_class(named_fc
);
939 struct bt_field_class_variant_with_selector_field_option
*
940 create_variant_with_selector_field_option(
941 const char *name
, struct bt_field_class
*fc
,
942 const struct bt_integer_range_set
*range_set
)
944 struct bt_field_class_variant_with_selector_field_option
*opt
= g_new0(
945 struct bt_field_class_variant_with_selector_field_option
, 1);
947 BT_ASSERT(range_set
);
950 BT_LIB_LOGE_APPEND_CAUSE(
951 "Failed to allocate a named field class.");
955 if (init_named_field_class(&opt
->common
, name
, fc
)) {
959 opt
->range_set
= range_set
;
960 bt_object_get_ref_no_null_check(opt
->range_set
);
961 bt_integer_range_set_freeze(range_set
);
965 destroy_variant_with_selector_field_option(opt
);
973 int append_named_field_class_to_container_field_class(
974 struct bt_field_class_named_field_class_container
*container_fc
,
975 struct bt_named_field_class
*named_fc
)
977 BT_ASSERT(container_fc
);
979 BT_ASSERT_PRE_DEV_FC_HOT(container_fc
, "Field class");
980 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc
->name_to_index
,
981 named_fc
->name
->str
),
982 "Duplicate member/option name in structure/variant field class: "
983 "%![container-fc-]+F, name=\"%s\"", container_fc
,
984 named_fc
->name
->str
);
987 * Freeze the contained field class, but not the named field
988 * class itself, as it's still possible afterwards to modify
989 * properties of the member/option object.
991 bt_field_class_freeze(named_fc
->fc
);
992 g_ptr_array_add(container_fc
->named_fcs
, named_fc
);
993 g_hash_table_insert(container_fc
->name_to_index
, named_fc
->name
->str
,
994 GUINT_TO_POINTER(container_fc
->named_fcs
->len
- 1));
995 return BT_FUNC_STATUS_OK
;
998 enum bt_field_class_structure_append_member_status
999 bt_field_class_structure_append_member(
1000 struct bt_field_class
*fc
, const char *name
,
1001 struct bt_field_class
*member_fc
)
1003 enum bt_field_class_structure_append_member_status status
;
1004 struct bt_named_field_class
*named_fc
= NULL
;
1006 BT_ASSERT_PRE_NO_ERROR();
1007 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1008 BT_ASSERT_PRE_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1010 named_fc
= create_named_field_class(name
, member_fc
);
1012 /* create_named_field_class() logs errors */
1013 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1017 status
= append_named_field_class_to_container_field_class((void *) fc
,
1019 if (status
== BT_FUNC_STATUS_OK
) {
1020 /* Moved to the container */
1028 uint64_t bt_field_class_structure_get_member_count(
1029 const struct bt_field_class
*fc
)
1031 struct bt_field_class_structure
*struct_fc
= (void *) fc
;
1033 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1034 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1036 return (uint64_t) struct_fc
->common
.named_fcs
->len
;
1040 struct bt_named_field_class
*
1041 borrow_named_field_class_from_container_field_class_at_index(
1042 struct bt_field_class_named_field_class_container
*fc
,
1046 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, fc
->named_fcs
->len
);
1047 return fc
->named_fcs
->pdata
[index
];
1050 const struct bt_field_class_structure_member
*
1051 bt_field_class_structure_borrow_member_by_index_const(
1052 const struct bt_field_class
*fc
, uint64_t index
)
1054 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1055 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1057 return (const void *)
1058 borrow_named_field_class_from_container_field_class_at_index(
1059 (void *) fc
, index
);
1062 struct bt_field_class_structure_member
*
1063 bt_field_class_structure_borrow_member_by_index(
1064 struct bt_field_class
*fc
, uint64_t index
)
1066 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1067 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1070 borrow_named_field_class_from_container_field_class_at_index(
1071 (void *) fc
, index
);
1075 struct bt_named_field_class
*
1076 borrow_named_field_class_from_container_field_class_by_name(
1077 struct bt_field_class_named_field_class_container
*fc
,
1080 struct bt_named_field_class
*named_fc
= NULL
;
1085 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name
);
1086 if (!g_hash_table_lookup_extended(fc
->name_to_index
, name
, &orig_key
,
1091 named_fc
= fc
->named_fcs
->pdata
[GPOINTER_TO_UINT(value
)];
1097 const struct bt_field_class_structure_member
*
1098 bt_field_class_structure_borrow_member_by_name_const(
1099 const struct bt_field_class
*fc
, const char *name
)
1101 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1102 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1104 return (const void *)
1105 borrow_named_field_class_from_container_field_class_by_name(
1109 struct bt_field_class_structure_member
*
1110 bt_field_class_structure_borrow_member_by_name(
1111 struct bt_field_class
*fc
, const char *name
)
1113 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1114 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1117 borrow_named_field_class_from_container_field_class_by_name(
1121 const char *bt_field_class_structure_member_get_name(
1122 const struct bt_field_class_structure_member
*member
)
1124 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1126 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1127 return named_fc
->name
->str
;
1130 const struct bt_field_class
*
1131 bt_field_class_structure_member_borrow_field_class_const(
1132 const struct bt_field_class_structure_member
*member
)
1134 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1136 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1137 return named_fc
->fc
;
1140 struct bt_field_class
*
1141 bt_field_class_structure_member_borrow_field_class(
1142 struct bt_field_class_structure_member
*member
)
1144 struct bt_named_field_class
*named_fc
= (void *) member
;
1146 BT_ASSERT_PRE_DEV_STRUCT_FC_MEMBER_NON_NULL(member
);
1147 return named_fc
->fc
;
1151 void destroy_option_field_class(struct bt_object
*obj
)
1153 struct bt_field_class_option
*fc
= (void *) obj
;
1156 BT_LIB_LOGD("Destroying option field class object: %!+F", fc
);
1157 finalize_field_class((void *) obj
);
1158 BT_LOGD_STR("Putting content field class.");
1159 BT_OBJECT_PUT_REF_AND_RESET(fc
->content_fc
);
1161 if (fc
->common
.type
!= BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
) {
1162 struct bt_field_class_option_with_selector_field
*with_sel_fc
=
1165 BT_LOGD_STR("Putting selector field path.");
1166 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc
->selector_field_path
);
1167 BT_LOGD_STR("Putting selector field class.");
1168 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc
->selector_fc
);
1170 if (fc
->common
.type
!= BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
) {
1171 struct bt_field_class_option_with_selector_field_integer
*with_int_sel_fc
=
1174 BT_LOGD_STR("Putting integer range set.");
1175 BT_OBJECT_PUT_REF_AND_RESET(with_int_sel_fc
->range_set
);
1183 struct bt_field_class
*create_option_field_class(
1184 struct bt_trace_class
*trace_class
,
1185 enum bt_field_class_type fc_type
,
1186 struct bt_field_class
*content_fc
,
1187 struct bt_field_class
*selector_fc
)
1189 struct bt_field_class_option
*opt_fc
= NULL
;
1191 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
1192 BT_ASSERT_PRE_NON_NULL(content_fc
, "Content field class");
1193 BT_LIB_LOGD("Creating option field class: "
1194 "type=%s, %![content-fc-]+F, %![sel-fc-]+F",
1195 bt_common_field_class_type_string(fc_type
),
1196 content_fc
, selector_fc
);
1198 if (fc_type
!= BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
) {
1199 struct bt_field_class_option_with_selector_field
*opt_with_sel_fc
= NULL
;
1201 BT_ASSERT_PRE_NON_NULL(selector_fc
, "Selector field class");
1203 if (fc_type
== BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
) {
1204 BT_ASSERT_PRE_FC_HAS_TYPE(selector_fc
,
1205 BT_FIELD_CLASS_TYPE_BOOL
,
1206 "Selector field class");
1207 opt_with_sel_fc
= (void *) g_new0(
1208 struct bt_field_class_option_with_selector_field_bool
, 1);
1210 BT_ASSERT_PRE_FC_IS_INT(selector_fc
,
1211 "Selector field class");
1212 opt_with_sel_fc
= (void *) g_new0(
1213 struct bt_field_class_option_with_selector_field_integer
, 1);
1216 if (!opt_with_sel_fc
) {
1217 BT_LIB_LOGE_APPEND_CAUSE(
1218 "Failed to allocate one option with selector field class.");
1222 opt_with_sel_fc
->selector_fc
= selector_fc
;
1223 bt_object_get_ref_no_null_check(opt_with_sel_fc
->selector_fc
);
1224 opt_fc
= (void *) opt_with_sel_fc
;
1226 opt_fc
= g_new0(struct bt_field_class_option
, 1);
1228 BT_LIB_LOGE_APPEND_CAUSE(
1229 "Failed to allocate one option field class.");
1236 if (init_field_class((void *) opt_fc
, fc_type
,
1237 destroy_option_field_class
)) {
1241 opt_fc
->content_fc
= content_fc
;
1242 bt_object_get_ref_no_null_check(opt_fc
->content_fc
);
1243 bt_field_class_freeze(opt_fc
->content_fc
);
1246 bt_field_class_freeze(selector_fc
);
1249 BT_LIB_LOGD("Created option field class object: "
1250 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc
, selector_fc
);
1254 BT_OBJECT_PUT_REF_AND_RESET(opt_fc
);
1257 return (void *) opt_fc
;
1260 struct bt_field_class
*bt_field_class_option_without_selector_create(
1261 struct bt_trace_class
*trace_class
,
1262 struct bt_field_class
*content_fc
)
1264 BT_ASSERT_PRE_NO_ERROR();
1266 return create_option_field_class(trace_class
,
1267 BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
,
1271 struct bt_field_class
*bt_field_class_option_with_selector_field_bool_create(
1272 struct bt_trace_class
*trace_class
,
1273 struct bt_field_class
*content_fc
,
1274 struct bt_field_class
*selector_fc
)
1276 BT_ASSERT_PRE_NO_ERROR();
1278 return create_option_field_class(trace_class
,
1279 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
,
1280 content_fc
, selector_fc
);
1283 struct bt_field_class
*
1284 bt_field_class_option_with_selector_field_integer_unsigned_create(
1285 struct bt_trace_class
*trace_class
,
1286 struct bt_field_class
*content_fc
,
1287 struct bt_field_class
*selector_fc
,
1288 const struct bt_integer_range_set_unsigned
*u_range_set
)
1290 struct bt_field_class_option_with_selector_field_integer
*fc
;
1291 const struct bt_integer_range_set
*range_set
=
1292 (const void *) u_range_set
;
1294 BT_ASSERT_PRE_NO_ERROR();
1295 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(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
,
1299 BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1300 content_fc
, selector_fc
);
1306 fc
->range_set
= range_set
;
1307 bt_object_get_ref_no_null_check(fc
->range_set
);
1308 bt_integer_range_set_freeze(range_set
);
1314 struct bt_field_class
*
1315 bt_field_class_option_with_selector_field_integer_signed_create(
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
)
1321 struct bt_field_class_option_with_selector_field_integer
*fc
;
1322 const struct bt_integer_range_set
*range_set
=
1323 (const void *) i_range_set
;
1325 BT_ASSERT_PRE_NO_ERROR();
1326 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(range_set
);
1327 BT_ASSERT_PRE(range_set
->ranges
->len
> 0,
1328 "Integer range set is empty: %!+R", range_set
);
1329 fc
= (void *) create_option_field_class(trace_class
,
1330 BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1331 content_fc
, selector_fc
);
1337 fc
->range_set
= range_set
;
1338 bt_object_get_ref_no_null_check(fc
->range_set
);
1339 bt_integer_range_set_freeze(range_set
);
1345 const struct bt_field_class
*bt_field_class_option_borrow_field_class_const(
1346 const struct bt_field_class
*fc
)
1348 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1350 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1351 BT_ASSERT_PRE_FC_IS_OPTION(fc
, "Field class");
1352 return opt_fc
->content_fc
;
1355 struct bt_field_class
*bt_field_class_option_borrow_field_class(
1356 struct bt_field_class
*fc
)
1358 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1360 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1361 BT_ASSERT_PRE_FC_IS_OPTION(fc
, "Field class");
1362 return opt_fc
->content_fc
;
1365 const struct bt_field_path
*
1366 bt_field_class_option_with_selector_field_borrow_selector_field_path_const(
1367 const struct bt_field_class
*fc
)
1369 const struct bt_field_class_option_with_selector_field
*opt_fc
=
1372 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1373 BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(fc
, "Field class");
1374 return opt_fc
->selector_field_path
;
1377 void bt_field_class_option_with_selector_field_bool_set_selector_is_reversed(
1378 struct bt_field_class
*fc
, bt_bool sel_is_reversed
)
1380 struct bt_field_class_option_with_selector_field_bool
*opt_fc
= (void *) fc
;
1382 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1383 BT_ASSERT_PRE_FC_HAS_TYPE(fc
,
1384 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
, "Field class");
1385 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
1386 opt_fc
->sel_is_reversed
= sel_is_reversed
;
1389 bt_bool
bt_field_class_option_with_selector_field_bool_selector_is_reversed(
1390 const struct bt_field_class
*fc
)
1392 struct bt_field_class_option_with_selector_field_bool
*opt_fc
= (void *) fc
;
1394 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1395 BT_ASSERT_PRE_FC_HAS_TYPE(fc
,
1396 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
, "Field class");
1397 return opt_fc
->sel_is_reversed
;
1400 const struct bt_integer_range_set_unsigned
*
1401 bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(
1402 const struct bt_field_class
*fc
)
1404 struct bt_field_class_option_with_selector_field_integer
*opt_fc
=
1407 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1408 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc
, "Field class");
1409 return (const void *) opt_fc
->range_set
;
1412 const struct bt_integer_range_set_signed
*
1413 bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(
1414 const struct bt_field_class
*fc
)
1416 struct bt_field_class_option_with_selector_field_integer
*opt_fc
=
1419 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1420 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc
, "Field class");
1421 return (const void *) opt_fc
->range_set
;
1425 void finalize_variant_field_class(struct bt_field_class_variant
*var_fc
)
1428 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc
);
1429 finalize_field_class((void *) var_fc
);
1430 finalize_named_field_classes_container((void *) var_fc
);
1434 void destroy_variant_field_class(struct bt_object
*obj
)
1436 struct bt_field_class_variant
*fc
= (void *) obj
;
1439 finalize_variant_field_class(fc
);
1444 void destroy_variant_with_selector_field_field_class(struct bt_object
*obj
)
1446 struct bt_field_class_variant_with_selector_field
*fc
= (void *) obj
;
1449 finalize_variant_field_class(&fc
->common
);
1450 BT_LOGD_STR("Putting selector field path.");
1451 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_field_path
);
1452 BT_LOGD_STR("Putting selector field class.");
1453 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_fc
);
1457 struct bt_field_class
*bt_field_class_variant_create(
1458 bt_trace_class
*trace_class
, bt_field_class
*selector_fc
)
1461 struct bt_field_class_variant
*var_fc
= NULL
;
1462 struct bt_field_class_variant_with_selector_field
*var_with_sel_fc
= NULL
;
1463 enum bt_field_class_type fc_type
;
1465 BT_ASSERT_PRE_NO_ERROR();
1466 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
1469 BT_ASSERT_PRE_FC_IS_INT(selector_fc
, "Selector field class");
1472 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1476 var_with_sel_fc
= g_new0(
1477 struct bt_field_class_variant_with_selector_field
, 1);
1478 if (!var_with_sel_fc
) {
1479 BT_LIB_LOGE_APPEND_CAUSE(
1480 "Failed to allocate one variant field class with selector.");
1484 if (bt_field_class_type_is(selector_fc
->type
,
1485 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
)) {
1486 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
;
1488 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
;
1491 ret
= init_named_field_classes_container(
1492 (void *) var_with_sel_fc
, fc_type
,
1493 destroy_variant_with_selector_field_field_class
,
1494 destroy_variant_with_selector_field_option
);
1496 /* init_named_field_classes_container() logs errors */
1500 var_with_sel_fc
->selector_fc
= selector_fc
;
1501 bt_object_get_ref_no_null_check(var_with_sel_fc
->selector_fc
);
1502 bt_field_class_freeze(selector_fc
);
1503 var_fc
= (void *) var_with_sel_fc
;
1504 BT_LIB_LOGD("Created default variant field class with selector object: "
1505 "%![var-fc-]+F, %![sel-fc-]+F", var_fc
, selector_fc
);
1507 var_fc
= g_new0(struct bt_field_class_variant
, 1);
1509 BT_LIB_LOGE_APPEND_CAUSE(
1510 "Failed to allocate one variant field class without selector.");
1514 ret
= init_named_field_classes_container((void *) var_fc
,
1515 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
,
1516 destroy_variant_field_class
, destroy_named_field_class
);
1518 /* init_named_field_classes_container() logs errors */
1521 BT_LIB_LOGD("Created default variant field class without selector object: "
1522 "%![var-fc-]+F", var_fc
);
1529 BT_OBJECT_PUT_REF_AND_RESET(var_fc
);
1530 BT_OBJECT_PUT_REF_AND_RESET(var_with_sel_fc
);
1533 return (void *) var_fc
;
1536 enum bt_field_class_variant_without_selector_append_option_status
1537 bt_field_class_variant_without_selector_append_option(struct bt_field_class
*fc
,
1538 const char *name
, struct bt_field_class
*option_fc
)
1540 enum bt_field_class_variant_without_selector_append_option_status status
;
1541 struct bt_named_field_class
*named_fc
= NULL
;
1543 BT_ASSERT_PRE_NO_ERROR();
1544 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1545 BT_ASSERT_PRE_NAME_NON_NULL(name
);
1546 BT_ASSERT_PRE_NON_NULL(option_fc
, "Option field class");
1547 BT_ASSERT_PRE_FC_HAS_TYPE(fc
,
1548 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
, "Field class");
1549 named_fc
= create_named_field_class(name
, option_fc
);
1551 /* create_named_field_class() logs errors */
1552 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1556 status
= append_named_field_class_to_container_field_class((void *) fc
,
1558 if (status
== BT_FUNC_STATUS_OK
) {
1559 /* Moved to the container */
1565 destroy_named_field_class(named_fc
);
1572 int ranges_overlap(GPtrArray
*var_fc_opts
, const struct bt_integer_range_set
*range_set
,
1573 bool is_signed
, bool *has_overlap
)
1575 int status
= BT_FUNC_STATUS_OK
;
1576 struct bt_integer_range_set
*full_range_set
;
1579 *has_overlap
= false;
1582 * Build a single range set with all the ranges and test for
1586 full_range_set
= (void *) bt_integer_range_set_signed_create();
1588 full_range_set
= (void *) bt_integer_range_set_unsigned_create();
1591 if (!full_range_set
) {
1592 BT_LOGE_STR("Failed to create a range set.");
1593 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1597 /* Add existing option ranges */
1598 for (i
= 0; i
< var_fc_opts
->len
; i
++) {
1599 struct bt_field_class_variant_with_selector_field_option
*opt
=
1600 var_fc_opts
->pdata
[i
];
1603 for (j
= 0; j
< opt
->range_set
->ranges
->len
; j
++) {
1604 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1608 status
= bt_integer_range_set_signed_add_range(
1609 (void *) full_range_set
, range
->lower
.i
,
1612 status
= bt_integer_range_set_unsigned_add_range(
1613 (void *) full_range_set
, range
->lower
.u
,
1623 /* Add new ranges */
1624 for (i
= 0; i
< range_set
->ranges
->len
; i
++) {
1625 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1629 status
= bt_integer_range_set_signed_add_range(
1630 (void *) full_range_set
, range
->lower
.i
,
1633 status
= bt_integer_range_set_unsigned_add_range(
1634 (void *) full_range_set
, range
->lower
.u
,
1643 /* Check overlaps */
1645 *has_overlap
= bt_integer_range_set_signed_has_overlaps(full_range_set
);
1647 *has_overlap
= bt_integer_range_set_unsigned_has_overlaps(
1652 bt_object_put_ref(full_range_set
);
1657 int append_option_to_variant_with_selector_field_field_class(
1658 struct bt_field_class
*fc
, const char *name
,
1659 struct bt_field_class
*option_fc
,
1660 const struct bt_integer_range_set
*range_set
,
1661 enum bt_field_class_type expected_type
)
1664 struct bt_field_class_variant_with_selector_field
*var_fc
= (void *) fc
;
1665 struct bt_field_class_variant_with_selector_field_option
*opt
= NULL
;
1668 BT_ASSERT_PRE_FC_NON_NULL(fc
);
1669 BT_ASSERT_PRE_NAME_NON_NULL(name
);
1670 BT_ASSERT_PRE_NON_NULL(option_fc
, "Option field class");
1671 BT_ASSERT_PRE_INT_RANGE_SET_NON_NULL(range_set
);
1672 BT_ASSERT_PRE_FC_HAS_TYPE(fc
, expected_type
, "Field class");
1673 BT_ASSERT_PRE(range_set
->ranges
->len
> 0,
1674 "Integer range set is empty: %!+R", range_set
);
1675 status
= ranges_overlap(var_fc
->common
.common
.named_fcs
, range_set
,
1676 expected_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1679 /* ranges_overlap() logs errors */
1683 BT_ASSERT_PRE(!has_overlap
,
1684 "Integer range set's ranges and existing ranges have an overlap: "
1686 opt
= create_variant_with_selector_field_option(name
, option_fc
, range_set
);
1688 /* create_variant_with_selector_field_option() logs errors */
1689 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1693 status
= append_named_field_class_to_container_field_class((void *) fc
,
1695 if (status
== BT_FUNC_STATUS_OK
) {
1696 /* Moved to the container */
1702 destroy_variant_with_selector_field_option(opt
);
1708 enum bt_field_class_variant_with_selector_field_integer_append_option_status
1709 bt_field_class_variant_with_selector_field_integer_unsigned_append_option(
1710 struct bt_field_class
*fc
, const char *name
,
1711 struct bt_field_class
*option_fc
,
1712 const struct bt_integer_range_set_unsigned
*range_set
)
1714 BT_ASSERT_PRE_NO_ERROR();
1716 return append_option_to_variant_with_selector_field_field_class(fc
,
1717 name
, option_fc
, (const void *) range_set
,
1718 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
);
1721 enum bt_field_class_variant_with_selector_field_integer_append_option_status
1722 bt_field_class_variant_with_selector_field_integer_signed_append_option(
1723 struct bt_field_class
*fc
, const char *name
,
1724 struct bt_field_class
*option_fc
,
1725 const struct bt_integer_range_set_signed
*range_set
)
1727 BT_ASSERT_PRE_NO_ERROR();
1729 return append_option_to_variant_with_selector_field_field_class(fc
,
1730 name
, option_fc
, (const void *) range_set
,
1731 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
);
1734 uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class
*fc
)
1736 const struct bt_field_class_variant
*var_fc
= (const void *) fc
;
1738 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1739 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1740 return (uint64_t) var_fc
->common
.named_fcs
->len
;
1743 const struct bt_field_class_variant_option
*
1744 bt_field_class_variant_borrow_option_by_name_const(
1745 const struct bt_field_class
*fc
, const char *name
)
1747 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1748 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1749 return (const void *)
1750 borrow_named_field_class_from_container_field_class_by_name(
1754 const struct bt_field_class_variant_option
*
1755 bt_field_class_variant_borrow_option_by_index_const(
1756 const struct bt_field_class
*fc
, uint64_t index
)
1758 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1759 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1760 return (const void *)
1761 borrow_named_field_class_from_container_field_class_at_index(
1762 (void *) fc
, index
);
1765 struct bt_field_class_variant_option
*
1766 bt_field_class_variant_borrow_option_by_name(
1767 struct bt_field_class
*fc
, const char *name
)
1769 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1770 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1772 borrow_named_field_class_from_container_field_class_by_name(
1776 struct bt_field_class_variant_option
*
1777 bt_field_class_variant_borrow_option_by_index(
1778 struct bt_field_class
*fc
, uint64_t index
)
1780 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1781 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1783 borrow_named_field_class_from_container_field_class_at_index(
1784 (void *) fc
, index
);
1787 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*
1788 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_name_const(
1789 const struct bt_field_class
*fc
, const char *name
)
1791 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1792 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
1793 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1795 return (const void *)
1796 borrow_named_field_class_from_container_field_class_by_name(
1800 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*
1801 bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const(
1802 const struct bt_field_class
*fc
, uint64_t index
)
1804 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1805 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
1806 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1808 return (const void *)
1809 borrow_named_field_class_from_container_field_class_at_index(
1810 (void *) fc
, index
);
1813 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*
1814 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_name_const(
1815 const struct bt_field_class
*fc
, const char *name
)
1817 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1818 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
1819 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1821 return (const void *)
1822 borrow_named_field_class_from_container_field_class_by_name(
1826 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*
1827 bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const(
1828 const struct bt_field_class
*fc
, uint64_t index
)
1830 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1831 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
1832 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1834 return (const void *)
1835 borrow_named_field_class_from_container_field_class_at_index(
1836 (void *) fc
, index
);
1839 const char *bt_field_class_variant_option_get_name(
1840 const struct bt_field_class_variant_option
*option
)
1842 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1844 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1845 return named_fc
->name
->str
;
1848 const struct bt_field_class
*
1849 bt_field_class_variant_option_borrow_field_class_const(
1850 const struct bt_field_class_variant_option
*option
)
1852 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1854 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1855 return named_fc
->fc
;
1858 struct bt_field_class
*
1859 bt_field_class_variant_option_borrow_field_class(
1860 struct bt_field_class_variant_option
*option
)
1862 struct bt_named_field_class
*named_fc
= (void *) option
;
1864 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1865 return named_fc
->fc
;
1868 const struct bt_integer_range_set_unsigned
*
1869 bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const(
1870 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*option
)
1872 const struct bt_field_class_variant_with_selector_field_option
*opt
=
1873 (const void *) option
;
1875 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1876 return (const void *) opt
->range_set
;
1879 const struct bt_integer_range_set_signed
*
1880 bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const(
1881 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*option
)
1883 const struct bt_field_class_variant_with_selector_field_option
*opt
=
1884 (const void *) option
;
1886 BT_ASSERT_PRE_DEV_VAR_FC_OPT_NON_NULL(option
);
1887 return (const void *) opt
->range_set
;
1890 const struct bt_field_path
*
1891 bt_field_class_variant_with_selector_field_borrow_selector_field_path_const(
1892 const struct bt_field_class
*fc
)
1894 const struct bt_field_class_variant_with_selector_field
*var_fc
=
1897 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1898 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc
, "Field class");
1899 return var_fc
->selector_field_path
;
1903 int init_array_field_class(struct bt_field_class_array
*fc
,
1904 enum bt_field_class_type type
, bt_object_release_func release_func
,
1905 struct bt_field_class
*element_fc
)
1909 BT_ASSERT(element_fc
);
1910 ret
= init_field_class((void *) fc
, type
, release_func
);
1915 fc
->element_fc
= element_fc
;
1916 bt_object_get_ref_no_null_check(fc
->element_fc
);
1917 bt_field_class_freeze(element_fc
);
1924 void finalize_array_field_class(struct bt_field_class_array
*array_fc
)
1926 BT_ASSERT(array_fc
);
1927 BT_LOGD_STR("Putting element field class.");
1928 finalize_field_class((void *) array_fc
);
1929 BT_OBJECT_PUT_REF_AND_RESET(array_fc
->element_fc
);
1933 void destroy_static_array_field_class(struct bt_object
*obj
)
1936 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj
);
1937 finalize_array_field_class((void *) obj
);
1941 struct bt_field_class
*
1942 bt_field_class_array_static_create(bt_trace_class
*trace_class
,
1943 struct bt_field_class
*element_fc
, uint64_t length
)
1945 struct bt_field_class_array_static
*array_fc
= NULL
;
1947 BT_ASSERT_PRE_NO_ERROR();
1948 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
1949 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1950 BT_LOGD_STR("Creating default static array field class object.");
1951 array_fc
= g_new0(struct bt_field_class_array_static
, 1);
1953 BT_LIB_LOGE_APPEND_CAUSE(
1954 "Failed to allocate one static array field class.");
1958 if (init_array_field_class((void *) array_fc
,
1959 BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1960 destroy_static_array_field_class
, element_fc
)) {
1964 array_fc
->length
= length
;
1965 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc
);
1969 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1972 return (void *) array_fc
;
1975 const struct bt_field_class
*
1976 bt_field_class_array_borrow_element_field_class_const(
1977 const struct bt_field_class
*fc
)
1979 const struct bt_field_class_array
*array_fc
= (const void *) fc
;
1981 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1982 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc
, "Field class");
1983 return array_fc
->element_fc
;
1986 struct bt_field_class
*
1987 bt_field_class_array_borrow_element_field_class(struct bt_field_class
*fc
)
1989 struct bt_field_class_array
*array_fc
= (void *) fc
;
1991 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
1992 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc
, "Field class");
1993 return array_fc
->element_fc
;
1996 uint64_t bt_field_class_array_static_get_length(const struct bt_field_class
*fc
)
1998 const struct bt_field_class_array_static
*array_fc
= (const void *) fc
;
2000 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2001 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
2003 return (uint64_t) array_fc
->length
;
2007 void destroy_dynamic_array_field_class(struct bt_object
*obj
)
2009 struct bt_field_class_array_dynamic
*fc
= (void *) obj
;
2012 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc
);
2013 finalize_array_field_class((void *) fc
);
2014 BT_LOGD_STR("Putting length field path.");
2015 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_field_path
);
2016 BT_LOGD_STR("Putting length field class.");
2017 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_fc
);
2021 struct bt_field_class
*bt_field_class_array_dynamic_create(
2022 struct bt_trace_class
*trace_class
,
2023 struct bt_field_class
*element_fc
,
2024 struct bt_field_class
*length_fc
)
2026 struct bt_field_class_array_dynamic
*array_fc
= NULL
;
2028 BT_ASSERT_PRE_NO_ERROR();
2029 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
2030 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
2031 BT_LOGD_STR("Creating default dynamic array field class object.");
2032 array_fc
= g_new0(struct bt_field_class_array_dynamic
, 1);
2034 BT_LIB_LOGE_APPEND_CAUSE(
2035 "Failed to allocate one dynamic array field class.");
2039 if (init_array_field_class((void *) array_fc
,
2041 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
2042 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
,
2043 destroy_dynamic_array_field_class
, element_fc
)) {
2048 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc
,
2049 "Length field class");
2050 array_fc
->length_fc
= length_fc
;
2051 bt_object_get_ref_no_null_check(array_fc
->length_fc
);
2052 bt_field_class_freeze(length_fc
);
2055 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc
);
2059 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
2062 return (void *) array_fc
;
2065 const struct bt_field_path
*
2066 bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const(
2067 const struct bt_field_class
*fc
)
2069 const struct bt_field_class_array_dynamic
*seq_fc
= (const void *) fc
;
2071 BT_ASSERT_PRE_NO_ERROR();
2072 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2073 BT_ASSERT_PRE_DEV_FC_HAS_TYPE(fc
,
2074 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
,
2076 return seq_fc
->length_field_path
;
2080 void destroy_string_field_class(struct bt_object
*obj
)
2083 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
2084 finalize_field_class((void *) obj
);
2088 struct bt_field_class
*bt_field_class_string_create(bt_trace_class
*trace_class
)
2090 struct bt_field_class_string
*string_fc
= NULL
;
2092 BT_ASSERT_PRE_NO_ERROR();
2093 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
2094 BT_LOGD_STR("Creating default string field class object.");
2095 string_fc
= g_new0(struct bt_field_class_string
, 1);
2097 BT_LIB_LOGE_APPEND_CAUSE(
2098 "Failed to allocate one string field class.");
2102 if (init_field_class((void *) string_fc
, BT_FIELD_CLASS_TYPE_STRING
,
2103 destroy_string_field_class
)) {
2107 BT_LIB_LOGD("Created string field class object: %!+F", string_fc
);
2111 BT_OBJECT_PUT_REF_AND_RESET(string_fc
);
2114 return (void *) string_fc
;
2118 void _bt_field_class_freeze(const struct bt_field_class
*c_fc
)
2120 struct bt_field_class
*fc
= (void *) c_fc
;
2123 * Element/member/option field classes are frozen when added to
2127 bt_value_freeze(fc
->user_attributes
);
2130 if (fc
->type
== BT_FIELD_CLASS_TYPE_STRUCTURE
||
2131 bt_field_class_type_is(fc
->type
,
2132 BT_FIELD_CLASS_TYPE_VARIANT
)) {
2133 struct bt_field_class_named_field_class_container
*container_fc
=
2137 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
2138 bt_named_field_class_freeze(
2139 container_fc
->named_fcs
->pdata
[i
]);
2145 void _bt_named_field_class_freeze(const struct bt_named_field_class
*named_fc
)
2147 BT_ASSERT(named_fc
);
2148 BT_ASSERT(named_fc
->fc
->frozen
);
2149 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
2150 named_fc
->user_attributes
);
2151 bt_value_freeze(named_fc
->user_attributes
);
2152 ((struct bt_named_field_class
*) named_fc
)->frozen
= true;
2156 void bt_field_class_make_part_of_trace_class(const struct bt_field_class
*c_fc
)
2158 struct bt_field_class
*fc
= (void *) c_fc
;
2161 BT_ASSERT_PRE(!fc
->part_of_trace_class
,
2162 "Field class is already part of a trace: %!+F", fc
);
2163 fc
->part_of_trace_class
= true;
2165 if (fc
->type
== BT_FIELD_CLASS_TYPE_STRUCTURE
||
2166 bt_field_class_type_is(fc
->type
,
2167 BT_FIELD_CLASS_TYPE_VARIANT
)) {
2168 struct bt_field_class_named_field_class_container
*container_fc
=
2172 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
2173 struct bt_named_field_class
*named_fc
=
2174 container_fc
->named_fcs
->pdata
[i
];
2176 bt_field_class_make_part_of_trace_class(named_fc
->fc
);
2178 } else if (bt_field_class_type_is(fc
->type
,
2179 BT_FIELD_CLASS_TYPE_ARRAY
)) {
2180 struct bt_field_class_array
*array_fc
= (void *) fc
;
2182 bt_field_class_make_part_of_trace_class(array_fc
->element_fc
);
2186 const struct bt_value
*bt_field_class_borrow_user_attributes_const(
2187 const struct bt_field_class
*fc
)
2189 BT_ASSERT_PRE_DEV_FC_NON_NULL(fc
);
2190 return fc
->user_attributes
;
2193 struct bt_value
*bt_field_class_borrow_user_attributes(
2194 struct bt_field_class
*field_class
)
2196 return (void *) bt_field_class_borrow_user_attributes_const(
2197 (void *) field_class
);
2201 void bt_field_class_set_user_attributes(
2202 struct bt_field_class
*fc
,
2203 const struct bt_value
*user_attributes
)
2205 BT_ASSERT_PRE_FC_NON_NULL(fc
);
2206 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
2207 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
2208 "User attributes object is not a map value object.");
2209 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
2210 bt_object_put_ref_no_null_check(fc
->user_attributes
);
2211 fc
->user_attributes
= (void *) user_attributes
;
2212 bt_object_get_ref_no_null_check(fc
->user_attributes
);
2216 const struct bt_value
*bt_named_field_class_borrow_user_attributes_const(
2217 const struct bt_named_field_class
*named_fc
)
2219 return named_fc
->user_attributes
;
2223 void bt_named_field_class_set_user_attributes(
2224 struct bt_named_field_class
*named_fc
,
2225 const struct bt_value
*user_attributes
)
2227 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
2228 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
2229 BT_ASSERT_PRE_DEV_HOT(named_fc
,
2230 "Structure field class member or variant field class option",
2232 bt_object_put_ref_no_null_check(named_fc
->user_attributes
);
2233 named_fc
->user_attributes
= (void *) user_attributes
;
2234 bt_object_get_ref_no_null_check(named_fc
->user_attributes
);
2237 const struct bt_value
*
2238 bt_field_class_structure_member_borrow_user_attributes_const(
2239 const struct bt_field_class_structure_member
*member
)
2241 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2242 return bt_named_field_class_borrow_user_attributes_const(
2243 (const void *) member
);
2247 bt_field_class_structure_member_borrow_user_attributes(
2248 struct bt_field_class_structure_member
*member
)
2250 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2251 return (void *) bt_named_field_class_borrow_user_attributes_const(
2255 void bt_field_class_structure_member_set_user_attributes(
2256 struct bt_field_class_structure_member
*member
,
2257 const struct bt_value
*user_attributes
)
2259 BT_ASSERT_PRE_STRUCT_FC_MEMBER_NON_NULL(member
);
2260 bt_named_field_class_set_user_attributes((void *) member
,
2264 const struct bt_value
*bt_field_class_variant_option_borrow_user_attributes_const(
2265 const struct bt_field_class_variant_option
*option
)
2267 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2268 return bt_named_field_class_borrow_user_attributes_const(
2269 (const void *) option
);
2272 struct bt_value
*bt_field_class_variant_option_borrow_user_attributes(
2273 struct bt_field_class_variant_option
*option
)
2275 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2276 return (void *) bt_named_field_class_borrow_user_attributes_const(
2280 void bt_field_class_variant_option_set_user_attributes(
2281 struct bt_field_class_variant_option
*option
,
2282 const struct bt_value
*user_attributes
)
2284 BT_ASSERT_PRE_VAR_FC_OPT_NON_NULL(option
);
2285 bt_named_field_class_set_user_attributes((void *) option
,
2289 void bt_field_class_get_ref(const struct bt_field_class
*field_class
)
2291 bt_object_get_ref(field_class
);
2294 void bt_field_class_put_ref(const struct bt_field_class
*field_class
)
2296 bt_object_put_ref(field_class
);