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"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/field.h>
13 #include "lib/object.h"
14 #include "compat/compiler.h"
15 #include "compat/fcntl.h"
16 #include "common/assert.h"
21 #include "field-class.h"
22 #include "lib/func-status.h"
25 #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field) \
26 BT_ASSERT_PRE_DEV_HOT("field", \
27 (const struct bt_field *) (_field), "Field", ": %!+f", (_field))
30 void reset_single_field(struct bt_field
*field
);
33 void reset_array_field(struct bt_field
*field
);
36 void reset_structure_field(struct bt_field
*field
);
39 void reset_option_field(struct bt_field
*field
);
42 void reset_variant_field(struct bt_field
*field
);
45 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
48 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
51 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
54 void set_option_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
57 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
60 bool single_field_is_set(const struct bt_field
*field
);
63 bool array_field_is_set(const struct bt_field
*field
);
66 bool structure_field_is_set(const struct bt_field
*field
);
69 bool option_field_is_set(const struct bt_field
*field
);
72 bool variant_field_is_set(const struct bt_field
*field
);
75 struct bt_field_methods bool_field_methods
= {
76 .set_is_frozen
= set_single_field_is_frozen
,
77 .is_set
= single_field_is_set
,
78 .reset
= reset_single_field
,
82 struct bt_field_methods bit_array_field_methods
= {
83 .set_is_frozen
= set_single_field_is_frozen
,
84 .is_set
= single_field_is_set
,
85 .reset
= reset_single_field
,
89 struct bt_field_methods integer_field_methods
= {
90 .set_is_frozen
= set_single_field_is_frozen
,
91 .is_set
= single_field_is_set
,
92 .reset
= reset_single_field
,
96 struct bt_field_methods real_field_methods
= {
97 .set_is_frozen
= set_single_field_is_frozen
,
98 .is_set
= single_field_is_set
,
99 .reset
= reset_single_field
,
103 struct bt_field_methods string_field_methods
= {
104 .set_is_frozen
= set_single_field_is_frozen
,
105 .is_set
= single_field_is_set
,
106 .reset
= reset_single_field
,
110 struct bt_field_methods structure_field_methods
= {
111 .set_is_frozen
= set_structure_field_is_frozen
,
112 .is_set
= structure_field_is_set
,
113 .reset
= reset_structure_field
,
117 struct bt_field_methods array_field_methods
= {
118 .set_is_frozen
= set_array_field_is_frozen
,
119 .is_set
= array_field_is_set
,
120 .reset
= reset_array_field
,
124 struct bt_field_methods option_field_methods
= {
125 .set_is_frozen
= set_option_field_is_frozen
,
126 .is_set
= option_field_is_set
,
127 .reset
= reset_option_field
,
131 struct bt_field_methods variant_field_methods
= {
132 .set_is_frozen
= set_variant_field_is_frozen
,
133 .is_set
= variant_field_is_set
,
134 .reset
= reset_variant_field
,
138 struct bt_field_methods blob_field_methods
= {
139 .set_is_frozen
= set_single_field_is_frozen
,
140 .is_set
= single_field_is_set
,
141 .reset
= reset_single_field
,
145 struct bt_field
*create_bool_field(struct bt_field_class
*);
148 struct bt_field
*create_bit_array_field(struct bt_field_class
*);
151 struct bt_field
*create_integer_field(struct bt_field_class
*);
154 struct bt_field
*create_real_field(struct bt_field_class
*);
157 struct bt_field
*create_string_field(struct bt_field_class
*);
160 struct bt_field
*create_structure_field(struct bt_field_class
*);
163 struct bt_field
*create_static_array_field(struct bt_field_class
*);
166 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
169 struct bt_field
*create_option_field(struct bt_field_class
*);
172 struct bt_field
*create_variant_field(struct bt_field_class
*);
175 struct bt_field
*create_blob_field(struct bt_field_class
*);
178 void destroy_bool_field(struct bt_field
*field
);
181 void destroy_bit_array_field(struct bt_field
*field
);
184 void destroy_integer_field(struct bt_field
*field
);
187 void destroy_real_field(struct bt_field
*field
);
190 void destroy_string_field(struct bt_field
*field
);
193 void destroy_structure_field(struct bt_field
*field
);
196 void destroy_array_field(struct bt_field
*field
);
199 void destroy_option_field(struct bt_field
*field
);
202 void destroy_variant_field(struct bt_field
*field
);
205 void destroy_blob_field(struct bt_field
*field
);
208 struct bt_field_class
*bt_field_borrow_class(struct bt_field
*field
)
210 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
215 const struct bt_field_class
*bt_field_borrow_class_const(
216 const struct bt_field
*field
)
218 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
223 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
225 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
226 return field
->class->type
;
229 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
231 struct bt_field
*field
= NULL
;
236 case BT_FIELD_CLASS_TYPE_BOOL
:
237 field
= create_bool_field(fc
);
239 case BT_FIELD_CLASS_TYPE_BIT_ARRAY
:
240 field
= create_bit_array_field(fc
);
242 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
243 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
244 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
245 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
246 field
= create_integer_field(fc
);
248 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
:
249 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
:
250 field
= create_real_field(fc
);
252 case BT_FIELD_CLASS_TYPE_STRING
:
253 field
= create_string_field(fc
);
255 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
256 field
= create_structure_field(fc
);
258 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
259 field
= create_static_array_field(fc
);
261 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
:
262 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
263 field
= create_dynamic_array_field(fc
);
265 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
:
266 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
:
267 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
268 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
269 field
= create_option_field(fc
);
271 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
:
272 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
273 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
274 field
= create_variant_field(fc
);
276 case BT_FIELD_CLASS_TYPE_STATIC_BLOB
:
277 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD
:
278 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD
:
279 field
= create_blob_field(fc
);
286 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
296 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
297 struct bt_field_methods
*methods
)
301 bt_object_init_unique(&field
->base
);
302 field
->methods
= methods
;
304 bt_object_get_ref_no_null_check(fc
);
308 struct bt_field
*create_bool_field(struct bt_field_class
*fc
)
310 struct bt_field_bool
*bool_field
;
312 BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc
);
313 bool_field
= g_new0(struct bt_field_bool
, 1);
315 BT_LIB_LOGE_APPEND_CAUSE(
316 "Failed to allocate one boolean field.");
320 init_field((void *) bool_field
, fc
, &bool_field_methods
);
321 BT_LIB_LOGD("Created boolean field object: %!+f", bool_field
);
324 return (void *) bool_field
;
328 struct bt_field
*create_bit_array_field(struct bt_field_class
*fc
)
330 struct bt_field_bit_array
*ba_field
;
332 BT_LIB_LOGD("Creating bit array field object: %![fc-]+F", fc
);
333 ba_field
= g_new0(struct bt_field_bit_array
, 1);
335 BT_LIB_LOGE_APPEND_CAUSE(
336 "Failed to allocate one bit array field.");
340 init_field((void *) ba_field
, fc
, &bit_array_field_methods
);
341 BT_LIB_LOGD("Created bit array field object: %!+f", ba_field
);
344 return (void *) ba_field
;
348 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
350 struct bt_field_integer
*int_field
;
352 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
353 int_field
= g_new0(struct bt_field_integer
, 1);
355 BT_LIB_LOGE_APPEND_CAUSE(
356 "Failed to allocate one integer field.");
360 init_field((void *) int_field
, fc
, &integer_field_methods
);
361 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
364 return (void *) int_field
;
368 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
370 struct bt_field_real
*real_field
;
372 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
373 real_field
= g_new0(struct bt_field_real
, 1);
375 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
379 init_field((void *) real_field
, fc
, &real_field_methods
);
380 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
383 return (void *) real_field
;
387 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
389 struct bt_field_string
*string_field
;
391 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
392 string_field
= g_new0(struct bt_field_string
, 1);
394 BT_LIB_LOGE_APPEND_CAUSE(
395 "Failed to allocate one string field.");
399 init_field((void *) string_field
, fc
, &string_field_methods
);
400 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
402 if (!string_field
->buf
) {
403 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
404 bt_field_destroy((void *) string_field
);
409 g_array_set_size(string_field
->buf
, 1);
410 bt_g_array_index(string_field
->buf
, char, 0) = '\0';
411 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
414 return (void *) string_field
;
418 int create_fields_from_named_field_classes(
419 struct bt_field_class_named_field_class_container
*fc
,
425 *fields
= g_ptr_array_new_with_free_func(
426 (GDestroyNotify
) bt_field_destroy
);
428 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
433 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
435 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
436 struct bt_field
*field
;
437 struct bt_named_field_class
*named_fc
= fc
->named_fcs
->pdata
[i
];
439 field
= bt_field_create(named_fc
->fc
);
441 BT_LIB_LOGE_APPEND_CAUSE(
442 "Failed to create structure member or variant option field: "
443 "name=\"%s\", %![fc-]+F",
444 named_fc
->name
->str
, named_fc
->fc
);
449 g_ptr_array_index(*fields
, i
) = field
;
457 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
459 struct bt_field_structure
*struct_field
;
461 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
462 struct_field
= g_new0(struct bt_field_structure
, 1);
464 BT_LIB_LOGE_APPEND_CAUSE(
465 "Failed to allocate one structure field.");
469 init_field((void *) struct_field
, fc
, &structure_field_methods
);
471 if (create_fields_from_named_field_classes((void *) fc
,
472 &struct_field
->fields
)) {
473 BT_LIB_LOGE_APPEND_CAUSE(
474 "Cannot create structure member fields: %![fc-]+F", fc
);
475 bt_field_destroy((void *) struct_field
);
480 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
483 return (void *) struct_field
;
487 struct bt_field
*create_option_field(struct bt_field_class
*fc
)
489 struct bt_field_option
*opt_field
;
490 struct bt_field_class_option
*opt_fc
= (void *) fc
;
492 BT_LIB_LOGD("Creating option field object: %![fc-]+F", fc
);
493 opt_field
= g_new0(struct bt_field_option
, 1);
495 BT_LIB_LOGE_APPEND_CAUSE(
496 "Failed to allocate one option field.");
500 init_field((void *) opt_field
, fc
, &option_field_methods
);
501 opt_field
->content_field
= bt_field_create(opt_fc
->content_fc
);
502 if (!opt_field
->content_field
) {
503 BT_LIB_LOGE_APPEND_CAUSE(
504 "Failed to create option field's content field: "
505 "%![opt-fc-]+F, %![content-fc-]+F",
506 opt_fc
, opt_fc
->content_fc
);
507 bt_field_destroy((void *) opt_field
);
512 BT_LIB_LOGD("Created option field object: %!+f", opt_field
);
515 return (void *) opt_field
;
519 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
521 struct bt_field_variant
*var_field
;
523 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
524 var_field
= g_new0(struct bt_field_variant
, 1);
526 BT_LIB_LOGE_APPEND_CAUSE(
527 "Failed to allocate one variant field.");
531 init_field((void *) var_field
, fc
, &variant_field_methods
);
533 if (create_fields_from_named_field_classes((void *) fc
,
534 &var_field
->fields
)) {
535 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
537 bt_field_destroy((void *) var_field
);
542 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
545 return (void *) var_field
;
549 struct bt_field
*create_blob_field(struct bt_field_class
*fc
)
551 struct bt_field_blob
*blob_field
;
553 BT_LIB_LOGD("Creating BLOB field object: %![fc-]+F", fc
);
554 blob_field
= g_new0(struct bt_field_blob
, 1);
556 BT_LIB_LOGE_APPEND_CAUSE(
557 "Failed to allocate one BLOB field.");
561 init_field((void *) blob_field
, fc
, &blob_field_methods
);
563 if (bt_field_class_type_is(fc
->type
, BT_FIELD_CLASS_TYPE_STATIC_BLOB
)) {
564 struct bt_field_class_blob_static
*blob_static_fc
=
566 blob_field
->length
= blob_static_fc
->length
;
567 blob_field
->data
= g_malloc(blob_field
->length
);
568 if (!blob_field
->data
) {
569 BT_LIB_LOGE_APPEND_CAUSE(
570 "Failed to allocate BLOB field data: %![fc-]+F",
578 bt_field_destroy((void *) blob_field
);
582 return (void *) blob_field
;
586 int init_array_field_fields(struct bt_field_array
*array_field
)
590 struct bt_field_class_array
*array_fc
;
592 BT_ASSERT(array_field
);
593 array_fc
= (void *) array_field
->common
.class;
594 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
595 if (!array_field
->fields
) {
596 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
601 g_ptr_array_set_free_func(array_field
->fields
,
602 (GDestroyNotify
) bt_field_destroy
);
603 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
605 for (i
= 0; i
< array_field
->length
; i
++) {
606 array_field
->fields
->pdata
[i
] = bt_field_create(
607 array_fc
->element_fc
);
608 if (!array_field
->fields
->pdata
[i
]) {
609 BT_LIB_LOGE_APPEND_CAUSE(
610 "Cannot create array field's element field: "
611 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
622 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
624 struct bt_field_class_array_static
*array_fc
= (void *) fc
;
625 struct bt_field_array
*array_field
;
627 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
628 array_field
= g_new0(struct bt_field_array
, 1);
630 BT_LIB_LOGE_APPEND_CAUSE(
631 "Failed to allocate one static array field.");
635 init_field((void *) array_field
, fc
, &array_field_methods
);
636 array_field
->length
= array_fc
->length
;
638 if (init_array_field_fields(array_field
)) {
639 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
641 bt_field_destroy((void *) array_field
);
646 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
649 return (void *) array_field
;
653 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
655 struct bt_field_array
*array_field
;
657 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
658 array_field
= g_new0(struct bt_field_array
, 1);
660 BT_LIB_LOGE_APPEND_CAUSE(
661 "Failed to allocate one dynamic array field.");
665 init_field((void *) array_field
, fc
, &array_field_methods
);
667 if (init_array_field_fields(array_field
)) {
668 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
670 bt_field_destroy((void *) array_field
);
675 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
678 return (void *) array_field
;
682 bt_bool
bt_field_bool_get_value(const struct bt_field
*field
)
684 const struct bt_field_bool
*bool_field
= (const void *) field
;
686 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
687 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
688 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
689 "boolean-field", BT_FIELD_CLASS_TYPE_BOOL
, "Field");
690 return (bt_bool
) bool_field
->value
;
694 void bt_field_bool_set_value(struct bt_field
*field
, bt_bool value
)
696 struct bt_field_bool
*bool_field
= (void *) field
;
698 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
699 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
700 "boolean-field", BT_FIELD_CLASS_TYPE_BOOL
, "Field");
701 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
702 bool_field
->value
= (bool) value
;
703 bt_field_set_single(field
, true);
707 uint64_t bt_field_bit_array_get_value_as_integer(const struct bt_field
*field
)
709 const struct bt_field_bit_array
*ba_field
= (const void *) field
;
711 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
712 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
713 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
714 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY
, "Field");
715 return ba_field
->value_as_int
;
719 void bt_field_bit_array_set_value_as_integer(struct bt_field
*field
,
722 struct bt_field_bit_array
*ba_field
= (void *) field
;
723 struct bt_field_class_bit_array
*ba_fc
;
725 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
726 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
727 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY
, "Field");
728 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
729 ba_fc
= (void *) field
->class;
730 ba_field
->value_as_int
= value
;
732 if (ba_fc
->length
< 64) {
734 ba_field
->value_as_int
&= ((UINT64_C(1) << ba_fc
->length
) - 1);
737 bt_field_set_single(field
, true);
741 bt_field_bit_array_get_active_flag_labels_status
742 bt_field_bit_array_get_active_flag_labels(const struct bt_field
*field
,
743 bt_field_class_bit_array_flag_label_array
*label_array
,
746 const struct bt_field_bit_array
*ba_field
= (const void *) field
;
748 BT_ASSERT_PRE_DEV_NO_ERROR();
749 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
750 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
751 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
752 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY
, "Field");
753 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array
,
754 "Label array (output)");
755 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count
, "Count (output)");
757 bt_field_class_bit_array_get_active_flag_labels_for_value_as_integer(
758 field
->class, ba_field
->value_as_int
, label_array
, count
);
762 int64_t bt_field_integer_signed_get_value(const struct bt_field
*field
)
764 const struct bt_field_integer
*int_field
= (const void *) field
;
766 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
767 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
768 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT("field", field
, "Field");
769 return int_field
->value
.i
;
773 void bt_field_integer_signed_set_value(struct bt_field
*field
, int64_t value
)
775 struct bt_field_integer
*int_field
= (void *) field
;
777 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
778 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT("field", field
, "Field");
779 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
780 BT_ASSERT_PRE_DEV("valid-value-for-field-class-field-value-range",
781 bt_util_value_is_in_range_signed(
782 ((struct bt_field_class_integer
*) field
->class)->range
,
784 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
785 "%![fc-]+F", value
, field
, field
->class);
786 int_field
->value
.i
= value
;
787 bt_field_set_single(field
, true);
791 uint64_t bt_field_integer_unsigned_get_value(const struct bt_field
*field
)
793 const struct bt_field_integer
*int_field
= (const void *) field
;
795 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
796 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
797 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT("field", field
, "Field");
798 return int_field
->value
.u
;
802 void bt_field_integer_unsigned_set_value(struct bt_field
*field
, uint64_t value
)
804 struct bt_field_integer
*int_field
= (void *) field
;
806 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
807 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT("field", field
, "Field");
808 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
809 BT_ASSERT_PRE_DEV("valid-value-for-field-class-field-value-range",
810 bt_util_value_is_in_range_unsigned(
811 ((struct bt_field_class_integer
*) field
->class)->range
,
813 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
814 "%![fc-]+F", value
, field
, field
->class);
815 int_field
->value
.u
= value
;
816 bt_field_set_single(field
, true);
820 float bt_field_real_single_precision_get_value(const struct bt_field
*field
)
822 const struct bt_field_real
*real_field
= (const void *) field
;
824 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
825 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
826 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
827 "single-precision-real-field",
828 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
, "Field");
829 return (float) real_field
->value
;
833 double bt_field_real_double_precision_get_value(const struct bt_field
*field
)
835 const struct bt_field_real
*real_field
= (const void *) field
;
837 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
838 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
839 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
840 "double-precision-real-field",
841 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
, "Field");
842 return real_field
->value
;
846 void bt_field_real_single_precision_set_value(struct bt_field
*field
,
849 struct bt_field_real
*real_field
= (void *) field
;
851 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
852 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
853 "single-precision-real-field",
854 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
, "Field");
855 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
857 real_field
->value
= (double) value
;
858 bt_field_set_single(field
, true);
862 void bt_field_real_double_precision_set_value(struct bt_field
*field
,
865 struct bt_field_real
*real_field
= (void *) field
;
867 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
868 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
869 "double-precision-real-field",
870 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
, "Field");
871 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
873 real_field
->value
= value
;
874 bt_field_set_single(field
, true);
878 enum bt_field_enumeration_get_mapping_labels_status
879 bt_field_enumeration_unsigned_get_mapping_labels(
880 const struct bt_field
*field
,
881 bt_field_class_enumeration_mapping_label_array
*label_array
,
884 const struct bt_field_integer
*int_field
= (const void *) field
;
886 BT_ASSERT_PRE_DEV_NO_ERROR();
887 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
888 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array
,
889 "Label array (output)");
890 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count
, "Count (output)");
891 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
892 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
893 "unsigned-enumeration-field",
894 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
896 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
897 field
->class, int_field
->value
.u
, label_array
, count
);
901 enum bt_field_enumeration_get_mapping_labels_status
902 bt_field_enumeration_signed_get_mapping_labels(
903 const struct bt_field
*field
,
904 bt_field_class_enumeration_mapping_label_array
*label_array
,
907 const struct bt_field_integer
*int_field
= (const void *) field
;
909 BT_ASSERT_PRE_DEV_NO_ERROR();
910 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
911 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array
,
912 "Label array (output)");
913 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count
, "Count (output)");
914 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
915 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
916 "signed-enumeration-field",
917 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
919 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
920 field
->class, int_field
->value
.i
, label_array
, count
);
924 const char *bt_field_string_get_value(const struct bt_field
*field
)
926 const struct bt_field_string
*string_field
= (const void *) field
;
928 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
929 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
930 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
, "string-field",
931 BT_FIELD_CLASS_TYPE_STRING
, "Field");
932 return (const char *) string_field
->buf
->data
;
936 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
938 const struct bt_field_string
*string_field
= (const void *) field
;
940 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
941 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field
);
942 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
, "string-field",
943 BT_FIELD_CLASS_TYPE_STRING
, "Field");
944 return string_field
->length
;
948 void clear_string_field(struct bt_field
*field
)
950 struct bt_field_string
*string_field
= (void *) field
;
952 BT_ASSERT_DBG(field
);
953 string_field
->length
= 0;
954 bt_g_array_index(string_field
->buf
, char, 0) = '\0';
955 bt_field_set_single(field
, true);
959 enum bt_field_string_set_value_status
bt_field_string_set_value(
960 struct bt_field
*field
, const char *value
)
962 BT_ASSERT_PRE_DEV_NO_ERROR();
963 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
964 BT_ASSERT_PRE_DEV_NON_NULL("value", value
, "Value");
965 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
966 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
, "string-field",
967 BT_FIELD_CLASS_TYPE_STRING
, "Field");
968 clear_string_field(field
);
969 return (int) bt_field_string_append_with_length(field
, value
,
970 (uint64_t) strlen(value
));
973 #define BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(_field, _value, _length) \
975 BT_ASSERT_PRE_DEV_NO_ERROR(); \
976 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(_field); \
977 BT_ASSERT_PRE_DEV_NON_NULL("value", (_value), "Value"); \
978 BT_ASSERT_PRE_DEV_FIELD_HOT(_field); \
979 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", \
980 (_field), "string-field", \
981 BT_FIELD_CLASS_TYPE_STRING, "Field"); \
982 BT_ASSERT_PRE_DEV("value-has-no-null-byte", \
983 !memchr((_value), '\0', (_length)), \
984 "String value to append contains a null character: " \
985 "partial-value=\"%.32s\", length=%" PRIu64, \
986 (_value), (_length)); \
990 enum bt_field_string_append_status
append_to_string_field_with_length(
991 struct bt_field
*field
, const char *value
, uint64_t length
)
993 struct bt_field_string
*string_field
= (void *) field
;
997 BT_ASSERT_DBG(field
);
998 BT_ASSERT_DBG(value
);
999 new_length
= length
+ string_field
->length
;
1001 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
1002 g_array_set_size(string_field
->buf
, new_length
+ 1);
1005 data
= string_field
->buf
->data
;
1006 memcpy(data
+ string_field
->length
, value
, length
);
1007 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
1008 string_field
->length
= new_length
;
1009 bt_field_set_single(field
, true);
1010 return BT_FUNC_STATUS_OK
;
1014 enum bt_field_string_append_status
bt_field_string_append_with_length(
1015 struct bt_field
*field
, const char *value
, uint64_t length
)
1017 BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(field
, value
,
1019 return append_to_string_field_with_length(field
, value
, length
);
1023 enum bt_field_string_append_status
bt_field_string_append(
1024 struct bt_field
*field
, const char *value
)
1026 uint64_t length
= (uint64_t) strlen(value
);
1028 BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(field
, value
,
1030 return append_to_string_field_with_length(field
, value
, length
);
1034 void bt_field_string_clear(struct bt_field
*field
)
1036 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1037 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1038 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
, "string-field",
1039 BT_FIELD_CLASS_TYPE_STRING
, "Field");
1040 clear_string_field(field
);
1044 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
1046 const struct bt_field_array
*array_field
= (const void *) field
;
1048 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1049 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY("field", field
, "Field");
1050 return array_field
->length
;
1054 enum bt_field_array_dynamic_set_length_status
bt_field_array_dynamic_set_length(
1055 struct bt_field
*field
, uint64_t length
)
1057 int ret
= BT_FUNC_STATUS_OK
;
1058 struct bt_field_array
*array_field
= (void *) field
;
1060 BT_ASSERT_PRE_DEV_NO_ERROR();
1061 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1062 BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY("field", field
, "Field");
1063 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1065 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
1066 /* Make more room */
1067 struct bt_field_class_array
*array_fc
;
1068 uint64_t cur_len
= array_field
->fields
->len
;
1071 g_ptr_array_set_size(array_field
->fields
, length
);
1072 array_fc
= (void *) field
->class;
1074 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
1075 struct bt_field
*elem_field
= bt_field_create(
1076 array_fc
->element_fc
);
1079 BT_LIB_LOGE_APPEND_CAUSE(
1080 "Cannot create element field for "
1081 "dynamic array field: "
1082 "index=%" PRIu64
", "
1083 "%![array-field-]+f", i
, field
);
1084 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
1088 BT_ASSERT_DBG(!array_field
->fields
->pdata
[i
]);
1089 array_field
->fields
->pdata
[i
] = elem_field
;
1093 array_field
->length
= length
;
1100 struct bt_field
*borrow_array_field_element_field_by_index(
1101 struct bt_field
*field
, uint64_t index
, const char *api_func
)
1103 struct bt_field_array
*array_field
= (void *) field
;
1105 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func
, field
);
1106 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY_FROM_FUNC(api_func
, "field", field
,
1108 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
,
1109 array_field
->length
);
1110 return array_field
->fields
->pdata
[index
];
1114 struct bt_field
*bt_field_array_borrow_element_field_by_index(
1115 struct bt_field
*field
, uint64_t index
)
1117 return borrow_array_field_element_field_by_index(field
, index
,
1122 const struct bt_field
*
1123 bt_field_array_borrow_element_field_by_index_const(
1124 const struct bt_field
*field
, uint64_t index
)
1126 return borrow_array_field_element_field_by_index((void *) field
, index
,
1131 struct bt_field
*borrow_structure_field_member_field_by_index(
1132 struct bt_field
*field
, uint64_t index
, const char *api_func
)
1134 struct bt_field_structure
*struct_field
= (void *) field
;
1136 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func
, field
);
1137 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func
, "field",
1138 field
, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE
,
1140 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
,
1141 struct_field
->fields
->len
);
1142 return struct_field
->fields
->pdata
[index
];
1146 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
1147 struct bt_field
*field
, uint64_t index
)
1149 return borrow_structure_field_member_field_by_index(field
,
1154 const struct bt_field
*
1155 bt_field_structure_borrow_member_field_by_index_const(
1156 const struct bt_field
*field
, uint64_t index
)
1158 return borrow_structure_field_member_field_by_index(
1159 (void *) field
, index
, __func__
);
1163 struct bt_field
*borrow_structure_field_member_field_by_name(
1164 struct bt_field
*field
, const char *name
, const char *api_func
)
1166 struct bt_field
*ret_field
= NULL
;
1167 struct bt_field_class_structure
*struct_fc
;
1168 struct bt_field_structure
*struct_field
= (void *) field
;
1172 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func
, field
);
1173 BT_ASSERT_PRE_DEV_NON_NULL_FROM_FUNC(api_func
, "member-name", name
,
1175 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func
, "field",
1176 field
, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE
,
1178 struct_fc
= (void *) field
->class;
1180 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
1181 &orig_key
, &index
)) {
1185 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
1186 BT_ASSERT_DBG(ret_field
);
1193 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
1194 struct bt_field
*field
, const char *name
)
1196 return borrow_structure_field_member_field_by_name(field
, name
,
1201 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
1202 const struct bt_field
*field
, const char *name
)
1204 return borrow_structure_field_member_field_by_name(
1205 (void *) field
, name
, __func__
);
1209 void bt_field_option_set_has_field(struct bt_field
*field
, bt_bool has_field
)
1211 struct bt_field_option
*opt_field
= (void *) field
;
1213 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1214 BT_ASSERT_PRE_DEV_FIELD_IS_OPTION("field", field
, "Field");
1215 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1218 opt_field
->selected_field
= opt_field
->content_field
;
1220 opt_field
->selected_field
= NULL
;
1225 struct bt_field
*bt_field_option_borrow_field(struct bt_field
*field
)
1227 struct bt_field_option
*opt_field
= (void *) field
;
1229 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1230 BT_ASSERT_PRE_DEV_FIELD_IS_OPTION("field", field
, "Field");
1231 return opt_field
->selected_field
;
1235 const struct bt_field
*bt_field_option_borrow_field_const(
1236 const struct bt_field
*field
)
1238 return (const void *) bt_field_option_borrow_field((void *) field
);
1241 #define BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(_field) \
1243 struct bt_field_variant *_var_field = (void *) field; \
1244 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(_field); \
1245 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", (_field), \
1247 BT_ASSERT_PRE_DEV("has-selected-field", \
1248 _var_field->selected_field, \
1249 "Variant field has no selected field: %!+f", \
1254 struct bt_field
*borrow_variant_field_selected_option_field(
1255 struct bt_field
*field
)
1257 struct bt_field_variant
*var_field
= (void *) field
;
1259 BT_ASSERT_DBG(field
);
1260 return var_field
->selected_field
;
1264 struct bt_field
*bt_field_variant_borrow_selected_option_field(
1265 struct bt_field
*field
)
1267 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(field
);
1268 return borrow_variant_field_selected_option_field(field
);
1272 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
1273 const struct bt_field
*field
)
1275 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(field
);
1276 return borrow_variant_field_selected_option_field((void *) field
);
1279 #define BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(_field) \
1281 struct bt_field_variant *_var_field = (void *) field; \
1282 BT_ASSERT_PRE_DEV("has-selected-field", \
1283 _var_field->selected_field, \
1284 "Variant field has no selected field: %!+f", \
1289 const struct bt_field_class_variant_option
*
1290 borrow_variant_field_selected_class_option(const struct bt_field
*field
)
1292 const struct bt_field_class_named_field_class_container
*container_fc
;
1293 const struct bt_field_variant
*var_field
= (const void *) field
;
1295 BT_ASSERT_DBG(field
);
1296 container_fc
= (const void *) field
->class;
1297 return container_fc
->named_fcs
->pdata
[var_field
->selected_index
];
1301 const struct bt_field_class_variant_option
*
1302 bt_field_variant_borrow_selected_option_class_const(
1303 const struct bt_field
*field
)
1305 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1306 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field
, "Field");
1307 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field
);
1308 return borrow_variant_field_selected_class_option(field
);
1312 const struct bt_field_class_variant_with_selector_field_integer_unsigned_option
*
1313 bt_field_variant_with_selector_field_integer_unsigned_borrow_selected_option_class_const(
1314 const struct bt_field
*field
)
1316 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1317 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
1318 "variant-field-with-unsigned-selector-field",
1319 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
,
1321 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field
);
1322 return (const void *) borrow_variant_field_selected_class_option(field
);
1326 const struct bt_field_class_variant_with_selector_field_integer_signed_option
*
1327 bt_field_variant_with_selector_field_integer_signed_borrow_selected_option_class_const(
1328 const struct bt_field
*field
)
1330 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1331 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field
,
1332 "variant-field-with-signed-selector-field",
1333 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
,
1335 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field
);
1336 return (const void *) borrow_variant_field_selected_class_option(field
);
1340 enum bt_field_variant_select_option_by_index_status
1341 bt_field_variant_select_option_by_index(
1342 struct bt_field
*field
, uint64_t index
)
1344 struct bt_field_variant
*var_field
= (void *) field
;
1346 BT_ASSERT_PRE_DEV_NO_ERROR();
1347 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1348 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field
, "Field");
1349 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1350 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, var_field
->fields
->len
);
1351 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
1352 var_field
->selected_index
= index
;
1353 return BT_FUNC_STATUS_OK
;
1357 uint64_t bt_field_variant_get_selected_option_index(
1358 const struct bt_field
*field
)
1360 const struct bt_field_variant
*var_field
= (const void *) field
;
1362 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1363 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field
, "Field");
1364 BT_ASSERT_PRE_DEV("has-selected-field", var_field
->selected_field
,
1365 "Variant field has no selected field: %!+f", field
);
1366 return var_field
->selected_index
;
1370 uint8_t *bt_field_blob_get_data(struct bt_field
*field
)
1372 const struct bt_field_blob
*blob_field
= (const void *) field
;
1374 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1375 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field
, "Field");
1376 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1377 BT_ASSERT_PRE_DEV("blob-field-length-is-set",
1378 blob_field
->length
> 0,
1379 "BLOB field length is not set: %!+f", field
);
1381 /* Assume that the user will fill the bytes. */
1382 bt_field_set_single(field
, true);
1384 return blob_field
->data
;
1388 const uint8_t *bt_field_blob_get_data_const(const struct bt_field
*field
)
1390 const struct bt_field_blob
*blob_field
= (const void *) field
;
1392 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1393 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field
, "Field");
1395 return blob_field
->data
;
1399 enum bt_field_blob_dynamic_set_length_status
bt_field_blob_dynamic_set_length(
1400 struct bt_field
*field
, uint64_t length
)
1403 struct bt_field_blob
*blob_field
= (void *) field
;
1405 BT_ASSERT_PRE_DEV_NO_ERROR();
1406 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1407 BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_BLOB("field", field
, "Field");
1408 BT_ASSERT_PRE_DEV_FIELD_HOT(field
);
1410 if (G_UNLIKELY(length
> blob_field
->length
)) {
1411 /* Make more room */
1412 uint8_t *data
= g_realloc(blob_field
->data
, length
);
1415 BT_LIB_LOGE_APPEND_CAUSE(
1416 "Failed to reallocate BLOB field data: %!+f",
1418 ret
= BT_FIELD_DYNAMIC_BLOB_SET_LENGTH_STATUS_MEMORY_ERROR
;
1422 blob_field
->data
= data
;
1425 blob_field
->length
= length
;
1426 ret
= BT_FIELD_DYNAMIC_BLOB_SET_LENGTH_STATUS_OK
;
1433 uint64_t bt_field_blob_get_length(const struct bt_field
*field
)
1435 const struct bt_field_blob
*blob_field
= (const void *) field
;
1437 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field
);
1438 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field
, "Field");
1440 return blob_field
->length
;
1445 void bt_field_finalize(struct bt_field
*field
)
1448 BT_LOGD_STR("Putting field's class.");
1449 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
1453 void destroy_bool_field(struct bt_field
*field
)
1456 BT_LIB_LOGD("Destroying boolean field object: %!+f", field
);
1457 bt_field_finalize(field
);
1462 void destroy_bit_array_field(struct bt_field
*field
)
1465 BT_LIB_LOGD("Destroying bit array field object: %!+f", field
);
1466 bt_field_finalize(field
);
1471 void destroy_integer_field(struct bt_field
*field
)
1474 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
1475 bt_field_finalize(field
);
1480 void destroy_real_field(struct bt_field
*field
)
1483 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
1484 bt_field_finalize(field
);
1489 void destroy_structure_field(struct bt_field
*field
)
1491 struct bt_field_structure
*struct_field
= (void *) field
;
1494 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
1495 bt_field_finalize(field
);
1497 if (struct_field
->fields
) {
1498 g_ptr_array_free(struct_field
->fields
, TRUE
);
1499 struct_field
->fields
= NULL
;
1506 void destroy_option_field(struct bt_field
*field
)
1508 struct bt_field_option
*opt_field
= (void *) field
;
1511 BT_LIB_LOGD("Destroying option field object: %!+f", field
);
1512 bt_field_finalize(field
);
1514 if (opt_field
->content_field
) {
1515 bt_field_destroy(opt_field
->content_field
);
1522 void destroy_variant_field(struct bt_field
*field
)
1524 struct bt_field_variant
*var_field
= (void *) field
;
1527 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
1528 bt_field_finalize(field
);
1530 if (var_field
->fields
) {
1531 g_ptr_array_free(var_field
->fields
, TRUE
);
1532 var_field
->fields
= NULL
;
1539 void destroy_blob_field(struct bt_field
*field
)
1541 struct bt_field_blob
*blob_field
= (void *) field
;
1544 BT_LIB_LOGD("Destroying BLOB field object: %!+f", field
);
1545 bt_field_finalize(field
);
1547 g_free(blob_field
->data
);
1553 void destroy_array_field(struct bt_field
*field
)
1555 struct bt_field_array
*array_field
= (void *) field
;
1558 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
1559 bt_field_finalize(field
);
1561 if (array_field
->fields
) {
1562 g_ptr_array_free(array_field
->fields
, TRUE
);
1563 array_field
->fields
= NULL
;
1570 void destroy_string_field(struct bt_field
*field
)
1572 struct bt_field_string
*string_field
= (void *) field
;
1575 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
1576 bt_field_finalize(field
);
1578 if (string_field
->buf
) {
1579 g_array_free(string_field
->buf
, TRUE
);
1580 string_field
->buf
= NULL
;
1586 void bt_field_destroy(struct bt_field
*field
)
1590 switch (field
->class->type
) {
1591 case BT_FIELD_CLASS_TYPE_BOOL
:
1592 destroy_bool_field(field
);
1594 case BT_FIELD_CLASS_TYPE_BIT_ARRAY
:
1595 destroy_bit_array_field(field
);
1597 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
1598 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
1599 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
1600 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
1601 destroy_integer_field(field
);
1603 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
:
1604 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
:
1605 destroy_real_field(field
);
1607 case BT_FIELD_CLASS_TYPE_STRING
:
1608 destroy_string_field(field
);
1610 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1611 destroy_structure_field(field
);
1613 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1614 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
:
1615 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
1616 destroy_array_field(field
);
1618 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
:
1619 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
:
1620 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
1621 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
1622 destroy_option_field(field
);
1624 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
:
1625 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
1626 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
1627 destroy_variant_field(field
);
1629 case BT_FIELD_CLASS_TYPE_STATIC_BLOB
:
1630 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD
:
1631 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD
:
1632 destroy_blob_field(field
);
1640 void reset_single_field(struct bt_field
*field
)
1642 BT_ASSERT_DBG(field
);
1643 field
->is_set
= false;
1647 void reset_structure_field(struct bt_field
*field
)
1650 struct bt_field_structure
*struct_field
= (void *) field
;
1652 BT_ASSERT_DBG(field
);
1654 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1655 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1660 void reset_option_field(struct bt_field
*field
)
1662 struct bt_field_option
*opt_field
= (void *) field
;
1664 BT_ASSERT_DBG(opt_field
);
1665 bt_field_reset(opt_field
->content_field
);
1666 opt_field
->selected_field
= NULL
;
1670 void reset_variant_field(struct bt_field
*field
)
1673 struct bt_field_variant
*var_field
= (void *) field
;
1675 BT_ASSERT_DBG(field
);
1677 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1678 bt_field_reset(var_field
->fields
->pdata
[i
]);
1683 void reset_array_field(struct bt_field
*field
)
1686 struct bt_field_array
*array_field
= (void *) field
;
1688 BT_ASSERT_DBG(field
);
1690 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1691 bt_field_reset(array_field
->fields
->pdata
[i
]);
1696 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1698 field
->frozen
= is_frozen
;
1702 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1705 struct bt_field_structure
*struct_field
= (void *) field
;
1707 BT_LIB_LOGD("Setting structure field's frozen state: "
1708 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1710 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1711 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1713 BT_LIB_LOGD("Setting structure field's member field's "
1714 "frozen state: %![field-]+f, index=%" PRIu64
,
1716 _bt_field_set_is_frozen(member_field
, is_frozen
);
1719 set_single_field_is_frozen(field
, is_frozen
);
1723 void set_option_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1725 struct bt_field_option
*opt_field
= (void *) field
;
1727 BT_LIB_LOGD("Setting option field's frozen state: "
1728 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1729 _bt_field_set_is_frozen(opt_field
->content_field
, is_frozen
);
1730 set_single_field_is_frozen(field
, is_frozen
);
1734 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1737 struct bt_field_variant
*var_field
= (void *) field
;
1739 BT_LIB_LOGD("Setting variant field's frozen state: "
1740 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1742 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1743 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1745 BT_LIB_LOGD("Setting variant field's option field's "
1746 "frozen state: %![field-]+f, index=%" PRIu64
,
1748 _bt_field_set_is_frozen(option_field
, is_frozen
);
1751 set_single_field_is_frozen(field
, is_frozen
);
1755 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1758 struct bt_field_array
*array_field
= (void *) field
;
1760 BT_LIB_LOGD("Setting array field's frozen state: "
1761 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1763 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1764 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1766 BT_LIB_LOGD("Setting array field's element field's "
1767 "frozen state: %![field-]+f, index=%" PRIu64
,
1769 _bt_field_set_is_frozen(elem_field
, is_frozen
);
1772 set_single_field_is_frozen(field
, is_frozen
);
1775 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1778 BT_ASSERT_DBG(field
);
1779 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1781 BT_ASSERT_DBG(field
->methods
->set_is_frozen
);
1782 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1786 bool single_field_is_set(const struct bt_field
*field
)
1788 BT_ASSERT_DBG(field
);
1789 return field
->is_set
;
1793 bool structure_field_is_set(const struct bt_field
*field
)
1797 const struct bt_field_structure
*struct_field
= (const void *) field
;
1799 BT_ASSERT_DBG(field
);
1801 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1802 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1813 bool option_field_is_set(const struct bt_field
*field
)
1815 const struct bt_field_option
*opt_field
= (const void *) field
;
1816 bool is_set
= false;
1818 BT_ASSERT_DBG(field
);
1820 if (opt_field
->selected_field
) {
1821 is_set
= bt_field_is_set(opt_field
->selected_field
);
1828 bool variant_field_is_set(const struct bt_field
*field
)
1830 const struct bt_field_variant
*var_field
= (const void *) field
;
1831 bool is_set
= false;
1833 BT_ASSERT_DBG(field
);
1835 if (var_field
->selected_field
) {
1836 is_set
= bt_field_is_set(var_field
->selected_field
);
1843 bool array_field_is_set(const struct bt_field
*field
)
1847 const struct bt_field_array
*array_field
= (const void *) field
;
1849 BT_ASSERT_DBG(field
);
1851 for (i
= 0; i
< array_field
->length
; i
++) {
1852 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);