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 integer_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 real_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 string_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 structure_field_methods
= {
100 .set_is_frozen
= set_structure_field_is_frozen
,
101 .is_set
= structure_field_is_set
,
102 .reset
= reset_structure_field
,
106 struct bt_field_methods array_field_methods
= {
107 .set_is_frozen
= set_array_field_is_frozen
,
108 .is_set
= array_field_is_set
,
109 .reset
= reset_array_field
,
113 struct bt_field_methods variant_field_methods
= {
114 .set_is_frozen
= set_variant_field_is_frozen
,
115 .is_set
= variant_field_is_set
,
116 .reset
= reset_variant_field
,
120 struct bt_field
*create_integer_field(struct bt_field_class
*);
123 struct bt_field
*create_real_field(struct bt_field_class
*);
126 struct bt_field
*create_string_field(struct bt_field_class
*);
129 struct bt_field
*create_structure_field(struct bt_field_class
*);
132 struct bt_field
*create_static_array_field(struct bt_field_class
*);
135 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
138 struct bt_field
*create_variant_field(struct bt_field_class
*);
141 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
142 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
143 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
144 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
145 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
146 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
147 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
148 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
149 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
150 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
151 [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
] = create_variant_field
,
152 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
] = create_variant_field
,
153 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
] = create_variant_field
,
157 void destroy_integer_field(struct bt_field
*field
);
160 void destroy_real_field(struct bt_field
*field
);
163 void destroy_string_field(struct bt_field
*field
);
166 void destroy_structure_field(struct bt_field
*field
);
169 void destroy_array_field(struct bt_field
*field
);
172 void destroy_variant_field(struct bt_field
*field
);
175 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
176 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
177 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
178 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
179 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
180 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
181 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
182 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
183 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
184 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
185 [BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
] = destroy_variant_field
,
186 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
] = destroy_variant_field
,
187 [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
] = destroy_variant_field
,
190 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
192 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
196 const struct bt_field_class
*bt_field_borrow_class_const(
197 const struct bt_field
*field
)
199 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
203 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
205 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
206 return field
->class->type
;
210 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
212 struct bt_field
*field
= NULL
;
215 field
= field_create_funcs
[fc
->type
](fc
);
217 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
227 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
228 struct bt_field_methods
*methods
)
232 bt_object_init_unique(&field
->base
);
233 field
->methods
= methods
;
235 bt_object_get_no_null_check(fc
);
239 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
241 struct bt_field_integer
*int_field
;
243 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
244 int_field
= g_new0(struct bt_field_integer
, 1);
246 BT_LIB_LOGE_APPEND_CAUSE(
247 "Failed to allocate one integer field.");
251 init_field((void *) int_field
, fc
, &integer_field_methods
);
252 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
255 return (void *) int_field
;
259 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
261 struct bt_field_real
*real_field
;
263 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
264 real_field
= g_new0(struct bt_field_real
, 1);
266 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
270 init_field((void *) real_field
, fc
, &real_field_methods
);
271 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
274 return (void *) real_field
;
278 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
280 struct bt_field_string
*string_field
;
282 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
283 string_field
= g_new0(struct bt_field_string
, 1);
285 BT_LIB_LOGE_APPEND_CAUSE(
286 "Failed to allocate one string field.");
290 init_field((void *) string_field
, fc
, &string_field_methods
);
291 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
293 if (!string_field
->buf
) {
294 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
295 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
299 g_array_index(string_field
->buf
, char, 0) = '\0';
300 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
303 return (void *) string_field
;
307 int create_fields_from_named_field_classes(
308 struct bt_field_class_named_field_class_container
*fc
,
314 *fields
= g_ptr_array_new_with_free_func(
315 (GDestroyNotify
) bt_field_destroy
);
317 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
322 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
324 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
325 struct bt_field
*field
;
326 struct bt_named_field_class
*named_fc
= fc
->named_fcs
->pdata
[i
];
328 field
= bt_field_create(named_fc
->fc
);
330 BT_LIB_LOGE_APPEND_CAUSE(
331 "Failed to create structure member or variant option field: "
332 "name=\"%s\", %![fc-]+F",
333 named_fc
->name
->str
, named_fc
->fc
);
338 g_ptr_array_index(*fields
, i
) = field
;
346 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
348 struct bt_field_structure
*struct_field
;
350 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
351 struct_field
= g_new0(struct bt_field_structure
, 1);
353 BT_LIB_LOGE_APPEND_CAUSE(
354 "Failed to allocate one structure field.");
358 init_field((void *) struct_field
, fc
, &structure_field_methods
);
360 if (create_fields_from_named_field_classes((void *) fc
,
361 &struct_field
->fields
)) {
362 BT_LIB_LOGE_APPEND_CAUSE(
363 "Cannot create structure member fields: %![fc-]+F", fc
);
364 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
368 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
371 return (void *) struct_field
;
375 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
377 struct bt_field_variant
*var_field
;
379 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
380 var_field
= g_new0(struct bt_field_variant
, 1);
382 BT_LIB_LOGE_APPEND_CAUSE(
383 "Failed to allocate one variant field.");
387 init_field((void *) var_field
, fc
, &variant_field_methods
);
389 if (create_fields_from_named_field_classes((void *) fc
,
390 &var_field
->fields
)) {
391 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
393 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
397 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
400 return (void *) var_field
;
404 int init_array_field_fields(struct bt_field_array
*array_field
)
408 struct bt_field_class_array
*array_fc
;
410 BT_ASSERT(array_field
);
411 array_fc
= (void *) array_field
->common
.class;
412 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
413 if (!array_field
->fields
) {
414 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
419 g_ptr_array_set_free_func(array_field
->fields
,
420 (GDestroyNotify
) bt_field_destroy
);
421 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
423 for (i
= 0; i
< array_field
->length
; i
++) {
424 array_field
->fields
->pdata
[i
] = bt_field_create(
425 array_fc
->element_fc
);
426 if (!array_field
->fields
->pdata
[i
]) {
427 BT_LIB_LOGE_APPEND_CAUSE(
428 "Cannot create array field's element field: "
429 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
440 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
442 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
443 struct bt_field_array
*array_field
;
445 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
446 array_field
= g_new0(struct bt_field_array
, 1);
448 BT_LIB_LOGE_APPEND_CAUSE(
449 "Failed to allocate one static array field.");
453 init_field((void *) array_field
, fc
, &array_field_methods
);
454 array_field
->length
= array_fc
->length
;
456 if (init_array_field_fields(array_field
)) {
457 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
459 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
463 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
466 return (void *) array_field
;
470 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
472 struct bt_field_array
*array_field
;
474 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
475 array_field
= g_new0(struct bt_field_array
, 1);
477 BT_LIB_LOGE_APPEND_CAUSE(
478 "Failed to allocate one dynamic array field.");
482 init_field((void *) array_field
, fc
, &array_field_methods
);
484 if (init_array_field_fields(array_field
)) {
485 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
487 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
491 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
494 return (void *) array_field
;
497 int64_t bt_field_signed_integer_get_value(const struct bt_field
*field
)
499 const struct bt_field_integer
*int_field
= (const void *) field
;
501 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
502 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
503 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
504 return int_field
->value
.i
;
507 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
509 struct bt_field_integer
*int_field
= (void *) field
;
511 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
512 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
513 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
514 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed(
515 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
516 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
517 "%![fc-]+F", value
, field
, field
->class);
518 int_field
->value
.i
= value
;
519 bt_field_set_single(field
, true);
522 uint64_t bt_field_unsigned_integer_get_value(const struct bt_field
*field
)
524 const struct bt_field_integer
*int_field
= (const void *) field
;
526 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
527 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
528 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
529 return int_field
->value
.u
;
532 void bt_field_unsigned_integer_set_value(struct bt_field
*field
, uint64_t value
)
534 struct bt_field_integer
*int_field
= (void *) field
;
536 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
537 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
538 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
539 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned(
540 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
541 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
542 "%![fc-]+F", value
, field
, field
->class);
543 int_field
->value
.u
= value
;
544 bt_field_set_single(field
, true);
547 double bt_field_real_get_value(const struct bt_field
*field
)
549 const struct bt_field_real
*real_field
= (const void *) field
;
551 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
552 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
553 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
554 return real_field
->value
;
557 void bt_field_real_set_value(struct bt_field
*field
, double value
)
559 struct bt_field_real
*real_field
= (void *) field
;
561 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
562 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
563 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
565 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
566 (double) (float) value
== value
,
567 "Invalid value for a single-precision real number: value=%f, "
568 "%![fc-]+F", value
, field
->class);
569 real_field
->value
= value
;
570 bt_field_set_single(field
, true);
573 enum bt_field_enumeration_get_mapping_labels_status
574 bt_field_unsigned_enumeration_get_mapping_labels(
575 const struct bt_field
*field
,
576 bt_field_class_enumeration_mapping_label_array
*label_array
,
579 const struct bt_field_integer
*int_field
= (const void *) field
;
581 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
582 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
583 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
584 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
585 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
586 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
588 bt_field_class_unsigned_enumeration_get_mapping_labels_for_value(
589 field
->class, int_field
->value
.u
, label_array
, count
);
592 enum bt_field_enumeration_get_mapping_labels_status
593 bt_field_signed_enumeration_get_mapping_labels(
594 const struct bt_field
*field
,
595 bt_field_class_enumeration_mapping_label_array
*label_array
,
598 const struct bt_field_integer
*int_field
= (const void *) field
;
600 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
601 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
602 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
603 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
604 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
605 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
607 bt_field_class_signed_enumeration_get_mapping_labels_for_value(
608 field
->class, int_field
->value
.i
, label_array
, count
);
611 const char *bt_field_string_get_value(const struct bt_field
*field
)
613 const struct bt_field_string
*string_field
= (const void *) field
;
615 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
616 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
617 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
619 return (const char *) string_field
->buf
->data
;
622 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
624 const struct bt_field_string
*string_field
= (const void *) field
;
626 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
627 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
628 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
630 return string_field
->length
;
634 void clear_string_field(struct bt_field
*field
)
636 struct bt_field_string
*string_field
= (void *) field
;
639 string_field
->length
= 0;
640 bt_field_set_single(field
, true);
643 enum bt_field_string_set_value_status
bt_field_string_set_value(
644 struct bt_field
*field
, const char *value
)
646 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
647 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
648 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
649 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
651 clear_string_field(field
);
652 return (int) bt_field_string_append_with_length(field
, value
,
653 (uint64_t) strlen(value
));
656 enum bt_field_string_append_status
bt_field_string_append(
657 struct bt_field
*field
, const char *value
)
659 return bt_field_string_append_with_length(field
,
660 value
, (uint64_t) strlen(value
));
663 enum bt_field_string_append_status
bt_field_string_append_with_length(
664 struct bt_field
*field
, const char *value
, uint64_t length
)
666 struct bt_field_string
*string_field
= (void *) field
;
670 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
671 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
672 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
673 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
674 BT_FIELD_CLASS_TYPE_STRING
, "Field");
676 /* Make sure no null bytes are appended */
677 BT_ASSERT_PRE_DEV(memchr(value
, '\0', length
) == NULL
,
678 "String value to append contains a null character: "
679 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
681 new_length
= length
+ string_field
->length
;
683 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
684 g_array_set_size(string_field
->buf
, new_length
+ 1);
687 data
= string_field
->buf
->data
;
688 memcpy(data
+ string_field
->length
, value
, length
);
689 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
690 string_field
->length
= new_length
;
691 bt_field_set_single(field
, true);
692 return BT_FUNC_STATUS_OK
;
695 void bt_field_string_clear(struct bt_field
*field
)
697 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
698 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
699 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
700 BT_FIELD_CLASS_TYPE_STRING
, "Field");
701 clear_string_field(field
);
704 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
706 const struct bt_field_array
*array_field
= (const void *) field
;
708 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
709 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
710 return array_field
->length
;
713 enum bt_field_dynamic_array_set_length_status
bt_field_dynamic_array_set_length(
714 struct bt_field
*field
, uint64_t length
)
716 int ret
= BT_FUNC_STATUS_OK
;
717 struct bt_field_array
*array_field
= (void *) field
;
719 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
720 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
721 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
722 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
724 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
726 struct bt_field_class_array
*array_fc
;
727 uint64_t cur_len
= array_field
->fields
->len
;
730 g_ptr_array_set_size(array_field
->fields
, length
);
731 array_fc
= (void *) field
->class;
733 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
734 struct bt_field
*elem_field
= bt_field_create(
735 array_fc
->element_fc
);
738 BT_LIB_LOGE_APPEND_CAUSE(
739 "Cannot create element field for "
740 "dynamic array field: "
741 "index=%" PRIu64
", "
742 "%![array-field-]+f", i
, field
);
743 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
747 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
748 array_field
->fields
->pdata
[i
] = elem_field
;
752 array_field
->length
= length
;
759 struct bt_field
*borrow_array_field_element_field_by_index(
760 struct bt_field
*field
, uint64_t index
)
762 struct bt_field_array
*array_field
= (void *) field
;
764 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
765 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
766 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, array_field
->length
);
767 return array_field
->fields
->pdata
[index
];
770 struct bt_field
*bt_field_array_borrow_element_field_by_index(
771 struct bt_field
*field
, uint64_t index
)
773 return borrow_array_field_element_field_by_index(field
, index
);
776 const struct bt_field
*
777 bt_field_array_borrow_element_field_by_index_const(
778 const struct bt_field
*field
, uint64_t index
)
780 return borrow_array_field_element_field_by_index((void *) field
, index
);
784 struct bt_field
*borrow_structure_field_member_field_by_index(
785 struct bt_field
*field
, uint64_t index
)
787 struct bt_field_structure
*struct_field
= (void *) field
;
789 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
790 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
791 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
792 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, struct_field
->fields
->len
);
793 return struct_field
->fields
->pdata
[index
];
796 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
797 struct bt_field
*field
, uint64_t index
)
799 return borrow_structure_field_member_field_by_index(field
,
803 const struct bt_field
*
804 bt_field_structure_borrow_member_field_by_index_const(
805 const struct bt_field
*field
, uint64_t index
)
807 return borrow_structure_field_member_field_by_index(
808 (void *) field
, index
);
812 struct bt_field
*borrow_structure_field_member_field_by_name(
813 struct bt_field
*field
, const char *name
)
815 struct bt_field
*ret_field
= NULL
;
816 struct bt_field_class_structure
*struct_fc
;
817 struct bt_field_structure
*struct_field
= (void *) field
;
821 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
822 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Field name");
823 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
824 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
825 struct_fc
= (void *) field
->class;
827 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
828 &orig_key
, &index
)) {
832 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
833 BT_ASSERT(ret_field
);
839 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
840 struct bt_field
*field
, const char *name
)
842 return borrow_structure_field_member_field_by_name(field
, name
);
845 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
846 const struct bt_field
*field
, const char *name
)
848 return borrow_structure_field_member_field_by_name(
849 (void *) field
, name
);
853 struct bt_field
*borrow_variant_field_selected_option_field(
854 struct bt_field
*field
)
856 struct bt_field_variant
*var_field
= (void *) field
;
858 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
859 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
860 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
861 "Variant field has no selected field: %!+f", field
);
862 return var_field
->selected_field
;
865 struct bt_field
*bt_field_variant_borrow_selected_option_field(
866 struct bt_field
*field
)
868 return borrow_variant_field_selected_option_field(field
);
871 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
872 const struct bt_field
*field
)
874 return borrow_variant_field_selected_option_field((void *) field
);
878 const struct bt_field_class_variant_option
*
879 borrow_variant_field_selected_class_option(const struct bt_field
*field
)
881 const struct bt_field_class_named_field_class_container
*container_fc
;
882 const struct bt_field_variant
*var_field
= (const void *) field
;
885 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
886 "Variant field has no selected field: %!+f", field
);
887 container_fc
= (const void *) field
->class;
888 return container_fc
->named_fcs
->pdata
[var_field
->selected_index
];
891 const struct bt_field_class_variant_option
*
892 bt_field_variant_borrow_selected_class_option_const(
893 const struct bt_field
*field
)
895 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
896 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
897 return borrow_variant_field_selected_class_option(field
);
900 const struct bt_field_class_variant_with_unsigned_selector_option
*
901 bt_field_variant_with_unsigned_selector_borrow_selected_class_option_const(
902 const struct bt_field
*field
)
904 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
905 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
906 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
, "Field");
907 return (const void *) borrow_variant_field_selected_class_option(field
);
910 const struct bt_field_class_variant_with_signed_selector_option
*
911 bt_field_variant_with_signed_selector_borrow_selected_class_option_const(
912 const struct bt_field
*field
)
914 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
915 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
916 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
, "Field");
917 return (const void *) borrow_variant_field_selected_class_option(field
);
920 enum bt_field_variant_select_option_field_by_index_status
921 bt_field_variant_select_option_field_by_index(
922 struct bt_field
*field
, uint64_t index
)
924 struct bt_field_variant
*var_field
= (void *) field
;
926 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
927 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
928 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
929 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, var_field
->fields
->len
);
930 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
931 var_field
->selected_index
= index
;
932 return BT_FUNC_STATUS_OK
;
935 uint64_t bt_field_variant_get_selected_option_field_index(
936 const struct bt_field
*field
)
938 const struct bt_field_variant
*var_field
= (const void *) field
;
940 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
941 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(field
, "Field");
942 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
943 "Variant field has no selected field: %!+f", field
);
944 return var_field
->selected_index
;
948 void bt_field_finalize(struct bt_field
*field
)
951 BT_LOGD_STR("Putting field's class.");
952 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
956 void destroy_integer_field(struct bt_field
*field
)
959 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
960 bt_field_finalize(field
);
965 void destroy_real_field(struct bt_field
*field
)
968 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
969 bt_field_finalize(field
);
974 void destroy_structure_field(struct bt_field
*field
)
976 struct bt_field_structure
*struct_field
= (void *) field
;
979 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
980 bt_field_finalize(field
);
982 if (struct_field
->fields
) {
983 g_ptr_array_free(struct_field
->fields
, TRUE
);
984 struct_field
->fields
= NULL
;
991 void destroy_variant_field(struct bt_field
*field
)
993 struct bt_field_variant
*var_field
= (void *) field
;
996 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
997 bt_field_finalize(field
);
999 if (var_field
->fields
) {
1000 g_ptr_array_free(var_field
->fields
, TRUE
);
1001 var_field
->fields
= NULL
;
1008 void destroy_array_field(struct bt_field
*field
)
1010 struct bt_field_array
*array_field
= (void *) field
;
1013 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
1014 bt_field_finalize(field
);
1016 if (array_field
->fields
) {
1017 g_ptr_array_free(array_field
->fields
, TRUE
);
1018 array_field
->fields
= NULL
;
1025 void destroy_string_field(struct bt_field
*field
)
1027 struct bt_field_string
*string_field
= (void *) field
;
1030 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
1031 bt_field_finalize(field
);
1033 if (string_field
->buf
) {
1034 g_array_free(string_field
->buf
, TRUE
);
1035 string_field
->buf
= NULL
;
1042 void bt_field_destroy(struct bt_field
*field
)
1045 field_destroy_funcs
[field
->class->type
](field
);
1049 void reset_single_field(struct bt_field
*field
)
1052 field
->is_set
= false;
1056 void reset_structure_field(struct bt_field
*field
)
1059 struct bt_field_structure
*struct_field
= (void *) field
;
1063 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1064 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1069 void reset_variant_field(struct bt_field
*field
)
1072 struct bt_field_variant
*var_field
= (void *) field
;
1076 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1077 bt_field_reset(var_field
->fields
->pdata
[i
]);
1082 void reset_array_field(struct bt_field
*field
)
1085 struct bt_field_array
*array_field
= (void *) field
;
1089 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1090 bt_field_reset(array_field
->fields
->pdata
[i
]);
1095 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1097 field
->frozen
= is_frozen
;
1101 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1104 struct bt_field_structure
*struct_field
= (void *) field
;
1106 BT_LIB_LOGD("Setting structure field's frozen state: "
1107 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1109 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1110 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1112 BT_LIB_LOGD("Setting structure field's member field's "
1113 "frozen state: %![field-]+f, index=%" PRIu64
,
1115 bt_field_set_is_frozen(member_field
, is_frozen
);
1118 set_single_field_is_frozen(field
, is_frozen
);
1122 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1125 struct bt_field_variant
*var_field
= (void *) field
;
1127 BT_LIB_LOGD("Setting variant field's frozen state: "
1128 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1130 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1131 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1133 BT_LIB_LOGD("Setting variant field's option field's "
1134 "frozen state: %![field-]+f, index=%" PRIu64
,
1136 bt_field_set_is_frozen(option_field
, is_frozen
);
1139 set_single_field_is_frozen(field
, is_frozen
);
1143 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1146 struct bt_field_array
*array_field
= (void *) field
;
1148 BT_LIB_LOGD("Setting array field's frozen state: "
1149 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1151 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1152 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1154 BT_LIB_LOGD("Setting array field's element field's "
1155 "frozen state: %![field-]+f, index=%" PRIu64
,
1157 bt_field_set_is_frozen(elem_field
, is_frozen
);
1160 set_single_field_is_frozen(field
, is_frozen
);
1164 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1168 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1170 BT_ASSERT(field
->methods
->set_is_frozen
);
1171 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1175 bool single_field_is_set(const struct bt_field
*field
)
1178 return field
->is_set
;
1182 bool structure_field_is_set(const struct bt_field
*field
)
1186 const struct bt_field_structure
*struct_field
= (const void *) field
;
1190 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1191 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1202 bool variant_field_is_set(const struct bt_field
*field
)
1204 const struct bt_field_variant
*var_field
= (const void *) field
;
1205 bool is_set
= false;
1209 if (var_field
->selected_field
) {
1210 is_set
= bt_field_is_set(var_field
->selected_field
);
1217 bool array_field_is_set(const struct bt_field
*field
)
1221 const struct bt_field_array
*array_field
= (const void *) field
;
1225 for (i
= 0; i
< array_field
->length
; i
++) {
1226 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);