2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/FIELD"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/field.h>
29 #include <babeltrace2/trace-ir/field-const.h>
30 #include "lib/object.h"
31 #include "compat/compiler.h"
32 #include "compat/fcntl.h"
33 #include "common/align.h"
34 #include "common/assert.h"
38 #include "field-class.h"
39 #include "lib/func-status.h"
42 void reset_single_field(struct bt_field
*field
);
45 void reset_array_field(struct bt_field
*field
);
48 void reset_structure_field(struct bt_field
*field
);
51 void reset_variant_field(struct bt_field
*field
);
54 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
57 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
60 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
63 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
66 bool single_field_is_set(const struct bt_field
*field
);
69 bool array_field_is_set(const struct bt_field
*field
);
72 bool structure_field_is_set(const struct bt_field
*field
);
75 bool variant_field_is_set(const struct bt_field
*field
);
78 struct bt_field_methods bool_field_methods
= {
79 .set_is_frozen
= set_single_field_is_frozen
,
80 .is_set
= single_field_is_set
,
81 .reset
= reset_single_field
,
85 struct bt_field_methods integer_field_methods
= {
86 .set_is_frozen
= set_single_field_is_frozen
,
87 .is_set
= single_field_is_set
,
88 .reset
= reset_single_field
,
92 struct bt_field_methods real_field_methods
= {
93 .set_is_frozen
= set_single_field_is_frozen
,
94 .is_set
= single_field_is_set
,
95 .reset
= reset_single_field
,
99 struct bt_field_methods string_field_methods
= {
100 .set_is_frozen
= set_single_field_is_frozen
,
101 .is_set
= single_field_is_set
,
102 .reset
= reset_single_field
,
106 struct bt_field_methods structure_field_methods
= {
107 .set_is_frozen
= set_structure_field_is_frozen
,
108 .is_set
= structure_field_is_set
,
109 .reset
= reset_structure_field
,
113 struct bt_field_methods array_field_methods
= {
114 .set_is_frozen
= set_array_field_is_frozen
,
115 .is_set
= array_field_is_set
,
116 .reset
= reset_array_field
,
120 struct bt_field_methods variant_field_methods
= {
121 .set_is_frozen
= set_variant_field_is_frozen
,
122 .is_set
= variant_field_is_set
,
123 .reset
= reset_variant_field
,
127 struct bt_field
*create_bool_field(struct bt_field_class
*);
130 struct bt_field
*create_integer_field(struct bt_field_class
*);
133 struct bt_field
*create_real_field(struct bt_field_class
*);
136 struct bt_field
*create_string_field(struct bt_field_class
*);
139 struct bt_field
*create_structure_field(struct bt_field_class
*);
142 struct bt_field
*create_static_array_field(struct bt_field_class
*);
145 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
148 struct bt_field
*create_variant_field(struct bt_field_class
*);
151 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
152 [BT_FIELD_CLASS_TYPE_BOOL
] = create_bool_field
,
153 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
154 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
155 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
156 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
157 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
158 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
159 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
160 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
161 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
162 [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
] = create_variant_field
,
163 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
] = create_variant_field
,
164 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
] = create_variant_field
,
168 void destroy_bool_field(struct bt_field
*field
);
171 void destroy_integer_field(struct bt_field
*field
);
174 void destroy_real_field(struct bt_field
*field
);
177 void destroy_string_field(struct bt_field
*field
);
180 void destroy_structure_field(struct bt_field
*field
);
183 void destroy_array_field(struct bt_field
*field
);
186 void destroy_variant_field(struct bt_field
*field
);
189 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
190 [BT_FIELD_CLASS_TYPE_BOOL
] = destroy_bool_field
,
191 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
192 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
193 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
194 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
195 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
196 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
197 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
198 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
199 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
200 [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
] = destroy_variant_field
,
201 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
] = destroy_variant_field
,
202 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
] = destroy_variant_field
,
205 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
207 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
211 const struct bt_field_class
*bt_field_borrow_class_const(
212 const struct bt_field
*field
)
214 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
218 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
220 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
221 return field
->class->type
;
225 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
227 struct bt_field
*field
= NULL
;
230 field
= field_create_funcs
[fc
->type
](fc
);
232 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
242 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
243 struct bt_field_methods
*methods
)
247 bt_object_init_unique(&field
->base
);
248 field
->methods
= methods
;
250 bt_object_get_no_null_check(fc
);
254 struct bt_field
*create_bool_field(struct bt_field_class
*fc
)
256 struct bt_field_bool
*bool_field
;
258 BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc
);
259 bool_field
= g_new0(struct bt_field_bool
, 1);
261 BT_LIB_LOGE_APPEND_CAUSE(
262 "Failed to allocate one boolean field.");
266 init_field((void *) bool_field
, fc
, &bool_field_methods
);
267 BT_LIB_LOGD("Created boolean field object: %!+f", bool_field
);
270 return (void *) bool_field
;
274 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
276 struct bt_field_integer
*int_field
;
278 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
279 int_field
= g_new0(struct bt_field_integer
, 1);
281 BT_LIB_LOGE_APPEND_CAUSE(
282 "Failed to allocate one integer field.");
286 init_field((void *) int_field
, fc
, &integer_field_methods
);
287 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
290 return (void *) int_field
;
294 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
296 struct bt_field_real
*real_field
;
298 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
299 real_field
= g_new0(struct bt_field_real
, 1);
301 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
305 init_field((void *) real_field
, fc
, &real_field_methods
);
306 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
309 return (void *) real_field
;
313 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
315 struct bt_field_string
*string_field
;
317 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
318 string_field
= g_new0(struct bt_field_string
, 1);
320 BT_LIB_LOGE_APPEND_CAUSE(
321 "Failed to allocate one string field.");
325 init_field((void *) string_field
, fc
, &string_field_methods
);
326 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
328 if (!string_field
->buf
) {
329 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
330 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
334 g_array_index(string_field
->buf
, char, 0) = '\0';
335 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
338 return (void *) string_field
;
342 int create_fields_from_named_field_classes(
343 struct bt_field_class_named_field_class_container
*fc
,
349 *fields
= g_ptr_array_new_with_free_func(
350 (GDestroyNotify
) bt_field_destroy
);
352 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
357 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
359 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
360 struct bt_field
*field
;
361 struct bt_named_field_class
*named_fc
= fc
->named_fcs
->pdata
[i
];
363 field
= bt_field_create(named_fc
->fc
);
365 BT_LIB_LOGE_APPEND_CAUSE(
366 "Failed to create structure member or variant option field: "
367 "name=\"%s\", %![fc-]+F",
368 named_fc
->name
->str
, named_fc
->fc
);
373 g_ptr_array_index(*fields
, i
) = field
;
381 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
383 struct bt_field_structure
*struct_field
;
385 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
386 struct_field
= g_new0(struct bt_field_structure
, 1);
388 BT_LIB_LOGE_APPEND_CAUSE(
389 "Failed to allocate one structure field.");
393 init_field((void *) struct_field
, fc
, &structure_field_methods
);
395 if (create_fields_from_named_field_classes((void *) fc
,
396 &struct_field
->fields
)) {
397 BT_LIB_LOGE_APPEND_CAUSE(
398 "Cannot create structure member fields: %![fc-]+F", fc
);
399 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
403 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
406 return (void *) struct_field
;
410 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
412 struct bt_field_variant
*var_field
;
414 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
415 var_field
= g_new0(struct bt_field_variant
, 1);
417 BT_LIB_LOGE_APPEND_CAUSE(
418 "Failed to allocate one variant field.");
422 init_field((void *) var_field
, fc
, &variant_field_methods
);
424 if (create_fields_from_named_field_classes((void *) fc
,
425 &var_field
->fields
)) {
426 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
428 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
432 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
435 return (void *) var_field
;
439 int init_array_field_fields(struct bt_field_array
*array_field
)
443 struct bt_field_class_array
*array_fc
;
445 BT_ASSERT(array_field
);
446 array_fc
= (void *) array_field
->common
.class;
447 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
448 if (!array_field
->fields
) {
449 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
454 g_ptr_array_set_free_func(array_field
->fields
,
455 (GDestroyNotify
) bt_field_destroy
);
456 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
458 for (i
= 0; i
< array_field
->length
; i
++) {
459 array_field
->fields
->pdata
[i
] = bt_field_create(
460 array_fc
->element_fc
);
461 if (!array_field
->fields
->pdata
[i
]) {
462 BT_LIB_LOGE_APPEND_CAUSE(
463 "Cannot create array field's element field: "
464 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
475 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
477 struct bt_field_class_array_static
*array_fc
= (void *) fc
;
478 struct bt_field_array
*array_field
;
480 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
481 array_field
= g_new0(struct bt_field_array
, 1);
483 BT_LIB_LOGE_APPEND_CAUSE(
484 "Failed to allocate one static array field.");
488 init_field((void *) array_field
, fc
, &array_field_methods
);
489 array_field
->length
= array_fc
->length
;
491 if (init_array_field_fields(array_field
)) {
492 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
494 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
498 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
501 return (void *) array_field
;
505 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
507 struct bt_field_array
*array_field
;
509 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
510 array_field
= g_new0(struct bt_field_array
, 1);
512 BT_LIB_LOGE_APPEND_CAUSE(
513 "Failed to allocate one dynamic array field.");
517 init_field((void *) array_field
, fc
, &array_field_methods
);
519 if (init_array_field_fields(array_field
)) {
520 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
522 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
526 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
529 return (void *) array_field
;
532 bt_bool
bt_field_bool_get_value(const struct bt_field
*field
)
534 const struct bt_field_bool
*bool_field
= (const void *) field
;
536 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
537 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
538 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_BOOL
,
540 return (bt_bool
) bool_field
->value
;
543 void bt_field_bool_set_value(struct bt_field
*field
, bt_bool value
)
545 struct bt_field_bool
*bool_field
= (void *) field
;
547 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
548 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_BOOL
,
550 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
551 bool_field
->value
= (bool) value
;
552 bt_field_set_single(field
, true);
555 int64_t bt_field_integer_signed_get_value(const struct bt_field
*field
)
557 const struct bt_field_integer
*int_field
= (const void *) field
;
559 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
560 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
561 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
562 return int_field
->value
.i
;
565 void bt_field_integer_signed_set_value(struct bt_field
*field
, int64_t value
)
567 struct bt_field_integer
*int_field
= (void *) field
;
569 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
570 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
571 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
572 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed(
573 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
574 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
575 "%![fc-]+F", value
, field
, field
->class);
576 int_field
->value
.i
= value
;
577 bt_field_set_single(field
, true);
580 uint64_t bt_field_integer_unsigned_get_value(const struct bt_field
*field
)
582 const struct bt_field_integer
*int_field
= (const void *) field
;
584 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
585 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
586 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
587 return int_field
->value
.u
;
590 void bt_field_integer_unsigned_set_value(struct bt_field
*field
, uint64_t value
)
592 struct bt_field_integer
*int_field
= (void *) field
;
594 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
595 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
596 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
597 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned(
598 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
599 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
600 "%![fc-]+F", value
, field
, field
->class);
601 int_field
->value
.u
= value
;
602 bt_field_set_single(field
, true);
605 double bt_field_real_get_value(const struct bt_field
*field
)
607 const struct bt_field_real
*real_field
= (const void *) field
;
609 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
610 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
611 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
612 return real_field
->value
;
615 void bt_field_real_set_value(struct bt_field
*field
, double value
)
617 struct bt_field_real
*real_field
= (void *) field
;
619 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
620 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
621 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
623 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
624 (double) (float) value
== value
,
625 "Invalid value for a single-precision real number: value=%f, "
626 "%![fc-]+F", value
, field
->class);
627 real_field
->value
= value
;
628 bt_field_set_single(field
, true);
631 enum bt_field_enumeration_get_mapping_labels_status
632 bt_field_enumeration_unsigned_get_mapping_labels(
633 const struct bt_field
*field
,
634 bt_field_class_enumeration_mapping_label_array
*label_array
,
637 const struct bt_field_integer
*int_field
= (const void *) field
;
639 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
640 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
641 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
642 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
643 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
644 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
646 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
647 field
->class, int_field
->value
.u
, label_array
, count
);
650 enum bt_field_enumeration_get_mapping_labels_status
651 bt_field_enumeration_signed_get_mapping_labels(
652 const struct bt_field
*field
,
653 bt_field_class_enumeration_mapping_label_array
*label_array
,
656 const struct bt_field_integer
*int_field
= (const void *) field
;
658 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
659 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
660 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
661 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
662 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
663 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
665 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
666 field
->class, int_field
->value
.i
, label_array
, count
);
669 const char *bt_field_string_get_value(const struct bt_field
*field
)
671 const struct bt_field_string
*string_field
= (const void *) field
;
673 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
674 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
675 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
677 return (const char *) string_field
->buf
->data
;
680 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
682 const struct bt_field_string
*string_field
= (const void *) field
;
684 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
685 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
686 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
688 return string_field
->length
;
692 void clear_string_field(struct bt_field
*field
)
694 struct bt_field_string
*string_field
= (void *) field
;
697 string_field
->length
= 0;
698 bt_field_set_single(field
, true);
701 enum bt_field_string_set_value_status
bt_field_string_set_value(
702 struct bt_field
*field
, const char *value
)
704 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
705 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
706 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
707 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
709 clear_string_field(field
);
710 return (int) bt_field_string_append_with_length(field
, value
,
711 (uint64_t) strlen(value
));
714 enum bt_field_string_append_status
bt_field_string_append(
715 struct bt_field
*field
, const char *value
)
717 return bt_field_string_append_with_length(field
,
718 value
, (uint64_t) strlen(value
));
721 enum bt_field_string_append_status
bt_field_string_append_with_length(
722 struct bt_field
*field
, const char *value
, uint64_t length
)
724 struct bt_field_string
*string_field
= (void *) field
;
728 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
729 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
730 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
731 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
732 BT_FIELD_CLASS_TYPE_STRING
, "Field");
734 /* Make sure no null bytes are appended */
735 BT_ASSERT_PRE_DEV(!memchr(value
, '\0', length
),
736 "String value to append contains a null character: "
737 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
739 new_length
= length
+ string_field
->length
;
741 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
742 g_array_set_size(string_field
->buf
, new_length
+ 1);
745 data
= string_field
->buf
->data
;
746 memcpy(data
+ string_field
->length
, value
, length
);
747 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
748 string_field
->length
= new_length
;
749 bt_field_set_single(field
, true);
750 return BT_FUNC_STATUS_OK
;
753 void bt_field_string_clear(struct bt_field
*field
)
755 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
756 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
757 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
758 BT_FIELD_CLASS_TYPE_STRING
, "Field");
759 clear_string_field(field
);
762 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
764 const struct bt_field_array
*array_field
= (const void *) field
;
766 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
767 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
768 return array_field
->length
;
771 enum bt_field_array_dynamic_set_length_status
bt_field_array_dynamic_set_length(
772 struct bt_field
*field
, uint64_t length
)
774 int ret
= BT_FUNC_STATUS_OK
;
775 struct bt_field_array
*array_field
= (void *) field
;
777 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
778 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
779 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
780 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
782 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
784 struct bt_field_class_array
*array_fc
;
785 uint64_t cur_len
= array_field
->fields
->len
;
788 g_ptr_array_set_size(array_field
->fields
, length
);
789 array_fc
= (void *) field
->class;
791 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
792 struct bt_field
*elem_field
= bt_field_create(
793 array_fc
->element_fc
);
796 BT_LIB_LOGE_APPEND_CAUSE(
797 "Cannot create element field for "
798 "dynamic array field: "
799 "index=%" PRIu64
", "
800 "%![array-field-]+f", i
, field
);
801 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
805 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
806 array_field
->fields
->pdata
[i
] = elem_field
;
810 array_field
->length
= length
;
817 struct bt_field
*borrow_array_field_element_field_by_index(
818 struct bt_field
*field
, uint64_t index
)
820 struct bt_field_array
*array_field
= (void *) field
;
822 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
823 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
824 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, array_field
->length
);
825 return array_field
->fields
->pdata
[index
];
828 struct bt_field
*bt_field_array_borrow_element_field_by_index(
829 struct bt_field
*field
, uint64_t index
)
831 return borrow_array_field_element_field_by_index(field
, index
);
834 const struct bt_field
*
835 bt_field_array_borrow_element_field_by_index_const(
836 const struct bt_field
*field
, uint64_t index
)
838 return borrow_array_field_element_field_by_index((void *) field
, index
);
842 struct bt_field
*borrow_structure_field_member_field_by_index(
843 struct bt_field
*field
, uint64_t index
)
845 struct bt_field_structure
*struct_field
= (void *) field
;
847 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
848 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
849 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
850 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, struct_field
->fields
->len
);
851 return struct_field
->fields
->pdata
[index
];
854 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
855 struct bt_field
*field
, uint64_t index
)
857 return borrow_structure_field_member_field_by_index(field
,
861 const struct bt_field
*
862 bt_field_structure_borrow_member_field_by_index_const(
863 const struct bt_field
*field
, uint64_t index
)
865 return borrow_structure_field_member_field_by_index(
866 (void *) field
, index
);
870 struct bt_field
*borrow_structure_field_member_field_by_name(
871 struct bt_field
*field
, const char *name
)
873 struct bt_field
*ret_field
= NULL
;
874 struct bt_field_class_structure
*struct_fc
;
875 struct bt_field_structure
*struct_field
= (void *) field
;
879 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
880 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Field name");
881 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
882 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
883 struct_fc
= (void *) field
->class;
885 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
886 &orig_key
, &index
)) {
890 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
891 BT_ASSERT(ret_field
);
897 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
898 struct bt_field
*field
, const char *name
)
900 return borrow_structure_field_member_field_by_name(field
, name
);
903 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
904 const struct bt_field
*field
, const char *name
)
906 return borrow_structure_field_member_field_by_name(
907 (void *) field
, name
);
911 struct bt_field
*borrow_variant_field_selected_option_field(
912 struct bt_field
*field
)
914 struct bt_field_variant
*var_field
= (void *) field
;
916 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
917 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
918 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
919 "Variant field has no selected field: %!+f", field
);
920 return var_field
->selected_field
;
923 struct bt_field
*bt_field_variant_borrow_selected_option_field(
924 struct bt_field
*field
)
926 return borrow_variant_field_selected_option_field(field
);
929 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
930 const struct bt_field
*field
)
932 return borrow_variant_field_selected_option_field((void *) field
);
936 const struct bt_field_class_variant_option
*
937 borrow_variant_field_selected_class_option(const struct bt_field
*field
)
939 const struct bt_field_class_named_field_class_container
*container_fc
;
940 const struct bt_field_variant
*var_field
= (const void *) field
;
943 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
944 "Variant field has no selected field: %!+f", field
);
945 container_fc
= (const void *) field
->class;
946 return container_fc
->named_fcs
->pdata
[var_field
->selected_index
];
949 const struct bt_field_class_variant_option
*
950 bt_field_variant_borrow_selected_class_option_const(
951 const struct bt_field
*field
)
953 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
954 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
955 return borrow_variant_field_selected_class_option(field
);
958 const struct bt_field_class_variant_with_selector_unsigned_option
*
959 bt_field_variant_with_unsigned_selector_borrow_selected_class_option_const(
960 const struct bt_field
*field
)
962 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
963 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
964 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
, "Field");
965 return (const void *) borrow_variant_field_selected_class_option(field
);
968 const struct bt_field_class_variant_with_selector_signed_option
*
969 bt_field_variant_with_signed_selector_borrow_selected_class_option_const(
970 const struct bt_field
*field
)
972 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
973 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
974 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
, "Field");
975 return (const void *) borrow_variant_field_selected_class_option(field
);
978 enum bt_field_variant_select_option_field_by_index_status
979 bt_field_variant_select_option_field_by_index(
980 struct bt_field
*field
, uint64_t index
)
982 struct bt_field_variant
*var_field
= (void *) field
;
984 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
985 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
986 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
987 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, var_field
->fields
->len
);
988 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
989 var_field
->selected_index
= index
;
990 return BT_FUNC_STATUS_OK
;
993 uint64_t bt_field_variant_get_selected_option_field_index(
994 const struct bt_field
*field
)
996 const struct bt_field_variant
*var_field
= (const void *) field
;
998 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
999 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
1000 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
1001 "Variant field has no selected field: %!+f", field
);
1002 return var_field
->selected_index
;
1006 void bt_field_finalize(struct bt_field
*field
)
1009 BT_LOGD_STR("Putting field's class.");
1010 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
1014 void destroy_bool_field(struct bt_field
*field
)
1017 BT_LIB_LOGD("Destroying boolean field object: %!+f", field
);
1018 bt_field_finalize(field
);
1023 void destroy_integer_field(struct bt_field
*field
)
1026 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
1027 bt_field_finalize(field
);
1032 void destroy_real_field(struct bt_field
*field
)
1035 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
1036 bt_field_finalize(field
);
1041 void destroy_structure_field(struct bt_field
*field
)
1043 struct bt_field_structure
*struct_field
= (void *) field
;
1046 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
1047 bt_field_finalize(field
);
1049 if (struct_field
->fields
) {
1050 g_ptr_array_free(struct_field
->fields
, TRUE
);
1051 struct_field
->fields
= NULL
;
1058 void destroy_variant_field(struct bt_field
*field
)
1060 struct bt_field_variant
*var_field
= (void *) field
;
1063 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
1064 bt_field_finalize(field
);
1066 if (var_field
->fields
) {
1067 g_ptr_array_free(var_field
->fields
, TRUE
);
1068 var_field
->fields
= NULL
;
1075 void destroy_array_field(struct bt_field
*field
)
1077 struct bt_field_array
*array_field
= (void *) field
;
1080 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
1081 bt_field_finalize(field
);
1083 if (array_field
->fields
) {
1084 g_ptr_array_free(array_field
->fields
, TRUE
);
1085 array_field
->fields
= NULL
;
1092 void destroy_string_field(struct bt_field
*field
)
1094 struct bt_field_string
*string_field
= (void *) field
;
1097 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
1098 bt_field_finalize(field
);
1100 if (string_field
->buf
) {
1101 g_array_free(string_field
->buf
, TRUE
);
1102 string_field
->buf
= NULL
;
1109 void bt_field_destroy(struct bt_field
*field
)
1112 field_destroy_funcs
[field
->class->type
](field
);
1116 void reset_single_field(struct bt_field
*field
)
1119 field
->is_set
= false;
1123 void reset_structure_field(struct bt_field
*field
)
1126 struct bt_field_structure
*struct_field
= (void *) field
;
1130 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1131 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1136 void reset_variant_field(struct bt_field
*field
)
1139 struct bt_field_variant
*var_field
= (void *) field
;
1143 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1144 bt_field_reset(var_field
->fields
->pdata
[i
]);
1149 void reset_array_field(struct bt_field
*field
)
1152 struct bt_field_array
*array_field
= (void *) field
;
1156 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1157 bt_field_reset(array_field
->fields
->pdata
[i
]);
1162 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1164 field
->frozen
= is_frozen
;
1168 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1171 struct bt_field_structure
*struct_field
= (void *) field
;
1173 BT_LIB_LOGD("Setting structure field's frozen state: "
1174 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1176 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1177 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1179 BT_LIB_LOGD("Setting structure field's member field's "
1180 "frozen state: %![field-]+f, index=%" PRIu64
,
1182 bt_field_set_is_frozen(member_field
, is_frozen
);
1185 set_single_field_is_frozen(field
, is_frozen
);
1189 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1192 struct bt_field_variant
*var_field
= (void *) field
;
1194 BT_LIB_LOGD("Setting variant field's frozen state: "
1195 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1197 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1198 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1200 BT_LIB_LOGD("Setting variant field's option field's "
1201 "frozen state: %![field-]+f, index=%" PRIu64
,
1203 bt_field_set_is_frozen(option_field
, is_frozen
);
1206 set_single_field_is_frozen(field
, is_frozen
);
1210 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1213 struct bt_field_array
*array_field
= (void *) field
;
1215 BT_LIB_LOGD("Setting array field's frozen state: "
1216 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1218 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1219 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1221 BT_LIB_LOGD("Setting array field's element field's "
1222 "frozen state: %![field-]+f, index=%" PRIu64
,
1224 bt_field_set_is_frozen(elem_field
, is_frozen
);
1227 set_single_field_is_frozen(field
, is_frozen
);
1231 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1235 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1237 BT_ASSERT(field
->methods
->set_is_frozen
);
1238 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1242 bool single_field_is_set(const struct bt_field
*field
)
1245 return field
->is_set
;
1249 bool structure_field_is_set(const struct bt_field
*field
)
1253 const struct bt_field_structure
*struct_field
= (const void *) field
;
1257 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1258 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1269 bool variant_field_is_set(const struct bt_field
*field
)
1271 const struct bt_field_variant
*var_field
= (const void *) field
;
1272 bool is_set
= false;
1276 if (var_field
->selected_field
) {
1277 is_set
= bt_field_is_set(var_field
->selected_field
);
1284 bool array_field_is_set(const struct bt_field
*field
)
1288 const struct bt_field_array
*array_field
= (const void *) field
;
1292 for (i
= 0; i
< array_field
->length
; i
++) {
1293 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);