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
] = create_variant_field
,
155 void destroy_integer_field(struct bt_field
*field
);
158 void destroy_real_field(struct bt_field
*field
);
161 void destroy_string_field(struct bt_field
*field
);
164 void destroy_structure_field(struct bt_field
*field
);
167 void destroy_array_field(struct bt_field
*field
);
170 void destroy_variant_field(struct bt_field
*field
);
173 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
174 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
175 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
176 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
177 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
178 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
179 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
180 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
181 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
182 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
183 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
186 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
188 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
192 const struct bt_field_class
*bt_field_borrow_class_const(
193 const struct bt_field
*field
)
195 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
199 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
201 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
202 return field
->class->type
;
206 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
208 struct bt_field
*field
= NULL
;
211 BT_ASSERT(bt_field_class_has_known_type(fc
));
212 field
= field_create_funcs
[fc
->type
](fc
);
214 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
224 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
225 struct bt_field_methods
*methods
)
229 bt_object_init_unique(&field
->base
);
230 field
->methods
= methods
;
232 bt_object_get_no_null_check(fc
);
236 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
238 struct bt_field_integer
*int_field
;
240 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
241 int_field
= g_new0(struct bt_field_integer
, 1);
243 BT_LIB_LOGE_APPEND_CAUSE(
244 "Failed to allocate one integer field.");
248 init_field((void *) int_field
, fc
, &integer_field_methods
);
249 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
252 return (void *) int_field
;
256 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
258 struct bt_field_real
*real_field
;
260 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
261 real_field
= g_new0(struct bt_field_real
, 1);
263 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
267 init_field((void *) real_field
, fc
, &real_field_methods
);
268 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
271 return (void *) real_field
;
275 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
277 struct bt_field_string
*string_field
;
279 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
280 string_field
= g_new0(struct bt_field_string
, 1);
282 BT_LIB_LOGE_APPEND_CAUSE(
283 "Failed to allocate one string field.");
287 init_field((void *) string_field
, fc
, &string_field_methods
);
288 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
290 if (!string_field
->buf
) {
291 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
292 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
296 g_array_index(string_field
->buf
, char, 0) = '\0';
297 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
300 return (void *) string_field
;
304 int create_fields_from_named_field_classes(
305 struct bt_field_class_named_field_class_container
*fc
,
311 *fields
= g_ptr_array_new_with_free_func(
312 (GDestroyNotify
) bt_field_destroy
);
314 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
319 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
321 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
322 struct bt_field
*field
;
323 struct bt_named_field_class
*named_fc
=
324 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, i
);
326 field
= bt_field_create(named_fc
->fc
);
328 BT_LIB_LOGE_APPEND_CAUSE(
329 "Failed to create structure member or variant option field: "
330 "name=\"%s\", %![fc-]+F",
331 named_fc
->name
->str
, named_fc
->fc
);
336 g_ptr_array_index(*fields
, i
) = field
;
344 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
346 struct bt_field_structure
*struct_field
;
348 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
349 struct_field
= g_new0(struct bt_field_structure
, 1);
351 BT_LIB_LOGE_APPEND_CAUSE(
352 "Failed to allocate one structure field.");
356 init_field((void *) struct_field
, fc
, &structure_field_methods
);
358 if (create_fields_from_named_field_classes((void *) fc
,
359 &struct_field
->fields
)) {
360 BT_LIB_LOGE_APPEND_CAUSE(
361 "Cannot create structure member fields: %![fc-]+F", fc
);
362 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
366 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
369 return (void *) struct_field
;
373 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
375 struct bt_field_variant
*var_field
;
377 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
378 var_field
= g_new0(struct bt_field_variant
, 1);
380 BT_LIB_LOGE_APPEND_CAUSE(
381 "Failed to allocate one variant field.");
385 init_field((void *) var_field
, fc
, &variant_field_methods
);
387 if (create_fields_from_named_field_classes((void *) fc
,
388 &var_field
->fields
)) {
389 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
391 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
395 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
398 return (void *) var_field
;
402 int init_array_field_fields(struct bt_field_array
*array_field
)
406 struct bt_field_class_array
*array_fc
;
408 BT_ASSERT(array_field
);
409 array_fc
= (void *) array_field
->common
.class;
410 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
411 if (!array_field
->fields
) {
412 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
417 g_ptr_array_set_free_func(array_field
->fields
,
418 (GDestroyNotify
) bt_field_destroy
);
419 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
421 for (i
= 0; i
< array_field
->length
; i
++) {
422 array_field
->fields
->pdata
[i
] = bt_field_create(
423 array_fc
->element_fc
);
424 if (!array_field
->fields
->pdata
[i
]) {
425 BT_LIB_LOGE_APPEND_CAUSE(
426 "Cannot create array field's element field: "
427 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
438 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
440 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
441 struct bt_field_array
*array_field
;
443 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
444 array_field
= g_new0(struct bt_field_array
, 1);
446 BT_LIB_LOGE_APPEND_CAUSE(
447 "Failed to allocate one static array field.");
451 init_field((void *) array_field
, fc
, &array_field_methods
);
452 array_field
->length
= array_fc
->length
;
454 if (init_array_field_fields(array_field
)) {
455 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
457 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
461 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
464 return (void *) array_field
;
468 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
470 struct bt_field_array
*array_field
;
472 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
473 array_field
= g_new0(struct bt_field_array
, 1);
475 BT_LIB_LOGE_APPEND_CAUSE(
476 "Failed to allocate one dynamic array field.");
480 init_field((void *) array_field
, fc
, &array_field_methods
);
482 if (init_array_field_fields(array_field
)) {
483 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
485 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
489 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
492 return (void *) array_field
;
495 int64_t bt_field_signed_integer_get_value(const struct bt_field
*field
)
497 const struct bt_field_integer
*int_field
= (const void *) field
;
499 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
500 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
501 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
502 return int_field
->value
.i
;
505 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
507 struct bt_field_integer
*int_field
= (void *) field
;
509 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
510 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field
, "Field");
511 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
512 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed(
513 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
514 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
515 "%![fc-]+F", value
, field
, field
->class);
516 int_field
->value
.i
= value
;
517 bt_field_set_single(field
, true);
520 uint64_t bt_field_unsigned_integer_get_value(const struct bt_field
*field
)
522 const struct bt_field_integer
*int_field
= (const void *) field
;
524 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
525 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
526 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
527 return int_field
->value
.u
;
530 void bt_field_unsigned_integer_set_value(struct bt_field
*field
, uint64_t value
)
532 struct bt_field_integer
*int_field
= (void *) field
;
534 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
535 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field
, "Field");
536 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
537 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned(
538 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
539 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
540 "%![fc-]+F", value
, field
, field
->class);
541 int_field
->value
.u
= value
;
542 bt_field_set_single(field
, true);
545 double bt_field_real_get_value(const struct bt_field
*field
)
547 const struct bt_field_real
*real_field
= (const void *) field
;
549 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
550 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
551 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
552 return real_field
->value
;
555 void bt_field_real_set_value(struct bt_field
*field
, double value
)
557 struct bt_field_real
*real_field
= (void *) field
;
559 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
560 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
561 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
563 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
564 (double) (float) value
== value
,
565 "Invalid value for a single-precision real number: value=%f, "
566 "%![fc-]+F", value
, field
->class);
567 real_field
->value
= value
;
568 bt_field_set_single(field
, true);
571 enum bt_field_enumeration_get_mapping_labels_status
572 bt_field_unsigned_enumeration_get_mapping_labels(
573 const struct bt_field
*field
,
574 bt_field_class_enumeration_mapping_label_array
*label_array
,
577 const struct bt_field_integer
*int_field
= (const void *) field
;
579 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
580 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
581 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
582 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
583 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
584 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
586 bt_field_class_unsigned_enumeration_get_mapping_labels_for_value(
587 field
->class, int_field
->value
.u
, label_array
, count
);
590 enum bt_field_enumeration_get_mapping_labels_status
591 bt_field_signed_enumeration_get_mapping_labels(
592 const struct bt_field
*field
,
593 bt_field_class_enumeration_mapping_label_array
*label_array
,
596 const struct bt_field_integer
*int_field
= (const void *) field
;
598 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
599 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
600 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Count (output)");
601 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
602 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
603 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
605 bt_field_class_signed_enumeration_get_mapping_labels_for_value(
606 field
->class, int_field
->value
.i
, label_array
, count
);
609 const char *bt_field_string_get_value(const struct bt_field
*field
)
611 const struct bt_field_string
*string_field
= (const void *) field
;
613 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
614 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
615 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
617 return (const char *) string_field
->buf
->data
;
620 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
622 const struct bt_field_string
*string_field
= (const void *) field
;
624 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
625 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field
, "Field");
626 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
628 return string_field
->length
;
632 void clear_string_field(struct bt_field
*field
)
634 struct bt_field_string
*string_field
= (void *) field
;
637 string_field
->length
= 0;
638 bt_field_set_single(field
, true);
641 enum bt_field_string_set_value_status
bt_field_string_set_value(
642 struct bt_field
*field
, const char *value
)
644 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
645 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
646 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
647 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
649 clear_string_field(field
);
650 return (int) bt_field_string_append_with_length(field
, value
,
651 (uint64_t) strlen(value
));
654 enum bt_field_string_append_status
bt_field_string_append(
655 struct bt_field
*field
, const char *value
)
657 return bt_field_string_append_with_length(field
,
658 value
, (uint64_t) strlen(value
));
661 enum bt_field_string_append_status
bt_field_string_append_with_length(
662 struct bt_field
*field
, const char *value
, uint64_t length
)
664 struct bt_field_string
*string_field
= (void *) field
;
668 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
669 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
670 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
671 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
672 BT_FIELD_CLASS_TYPE_STRING
, "Field");
674 /* Make sure no null bytes are appended */
675 BT_ASSERT_PRE_DEV(memchr(value
, '\0', length
) == NULL
,
676 "String value to append contains a null character: "
677 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
679 new_length
= length
+ string_field
->length
;
681 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
682 g_array_set_size(string_field
->buf
, new_length
+ 1);
685 data
= string_field
->buf
->data
;
686 memcpy(data
+ string_field
->length
, value
, length
);
687 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
688 string_field
->length
= new_length
;
689 bt_field_set_single(field
, true);
690 return BT_FUNC_STATUS_OK
;
693 void bt_field_string_clear(struct bt_field
*field
)
695 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
696 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
697 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
698 BT_FIELD_CLASS_TYPE_STRING
, "Field");
699 clear_string_field(field
);
702 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
704 const struct bt_field_array
*array_field
= (const void *) field
;
706 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
707 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
708 return array_field
->length
;
711 enum bt_field_dynamic_array_set_length_status
bt_field_dynamic_array_set_length(
712 struct bt_field
*field
, uint64_t length
)
714 int ret
= BT_FUNC_STATUS_OK
;
715 struct bt_field_array
*array_field
= (void *) field
;
717 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
718 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
719 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
720 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
722 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
724 struct bt_field_class_array
*array_fc
;
725 uint64_t cur_len
= array_field
->fields
->len
;
728 g_ptr_array_set_size(array_field
->fields
, length
);
729 array_fc
= (void *) field
->class;
731 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
732 struct bt_field
*elem_field
= bt_field_create(
733 array_fc
->element_fc
);
736 BT_LIB_LOGE_APPEND_CAUSE(
737 "Cannot create element field for "
738 "dynamic array field: "
739 "index=%" PRIu64
", "
740 "%![array-field-]+f", i
, field
);
741 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
745 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
746 array_field
->fields
->pdata
[i
] = elem_field
;
750 array_field
->length
= length
;
757 struct bt_field
*borrow_array_field_element_field_by_index(
758 struct bt_field
*field
, uint64_t index
)
760 struct bt_field_array
*array_field
= (void *) field
;
762 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
763 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field
, "Field");
764 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, array_field
->length
);
765 return array_field
->fields
->pdata
[index
];
768 struct bt_field
*bt_field_array_borrow_element_field_by_index(
769 struct bt_field
*field
, uint64_t index
)
771 return borrow_array_field_element_field_by_index(field
, index
);
774 const struct bt_field
*
775 bt_field_array_borrow_element_field_by_index_const(
776 const struct bt_field
*field
, uint64_t index
)
778 return borrow_array_field_element_field_by_index((void *) field
, index
);
782 struct bt_field
*borrow_structure_field_member_field_by_index(
783 struct bt_field
*field
, uint64_t index
)
785 struct bt_field_structure
*struct_field
= (void *) field
;
787 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
788 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
789 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
790 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, struct_field
->fields
->len
);
791 return struct_field
->fields
->pdata
[index
];
794 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
795 struct bt_field
*field
, uint64_t index
)
797 return borrow_structure_field_member_field_by_index(field
,
801 const struct bt_field
*
802 bt_field_structure_borrow_member_field_by_index_const(
803 const struct bt_field
*field
, uint64_t index
)
805 return borrow_structure_field_member_field_by_index(
806 (void *) field
, index
);
810 struct bt_field
*borrow_structure_field_member_field_by_name(
811 struct bt_field
*field
, const char *name
)
813 struct bt_field
*ret_field
= NULL
;
814 struct bt_field_class_structure
*struct_fc
;
815 struct bt_field_structure
*struct_field
= (void *) field
;
819 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
820 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Field name");
821 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
822 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
823 struct_fc
= (void *) field
->class;
825 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
826 &orig_key
, &index
)) {
830 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
831 BT_ASSERT(ret_field
);
837 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
838 struct bt_field
*field
, const char *name
)
840 return borrow_structure_field_member_field_by_name(field
, name
);
843 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
844 const struct bt_field
*field
, const char *name
)
846 return borrow_structure_field_member_field_by_name(
847 (void *) field
, name
);
851 struct bt_field
*borrow_variant_field_selected_option_field(
852 struct bt_field
*field
)
854 struct bt_field_variant
*var_field
= (void *) field
;
856 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
857 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
858 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
859 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
860 "Variant field has no selected field: %!+f", field
);
861 return var_field
->selected_field
;
864 struct bt_field
*bt_field_variant_borrow_selected_option_field(
865 struct bt_field
*field
)
867 return borrow_variant_field_selected_option_field(field
);
870 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
871 const struct bt_field
*field
)
873 return borrow_variant_field_selected_option_field((void *) field
);
876 enum bt_field_variant_select_option_field_status
877 bt_field_variant_select_option_field(
878 struct bt_field
*field
, uint64_t index
)
880 struct bt_field_variant
*var_field
= (void *) field
;
882 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
883 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
884 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
885 BT_ASSERT_PRE_DEV_FIELD_HOT(field
, "Field");
886 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, var_field
->fields
->len
);
887 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
888 var_field
->selected_index
= index
;
889 return BT_FUNC_STATUS_OK
;
892 uint64_t bt_field_variant_get_selected_option_field_index(
893 const struct bt_field
*field
)
895 const struct bt_field_variant
*var_field
= (const void *) field
;
897 BT_ASSERT_PRE_DEV_NON_NULL(field
, "Field");
898 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field
,
899 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
900 BT_ASSERT_PRE_DEV(var_field
->selected_field
,
901 "Variant field has no selected field: %!+f", field
);
902 return var_field
->selected_index
;
906 void bt_field_finalize(struct bt_field
*field
)
909 BT_LOGD_STR("Putting field's class.");
910 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
914 void destroy_integer_field(struct bt_field
*field
)
917 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
918 bt_field_finalize(field
);
923 void destroy_real_field(struct bt_field
*field
)
926 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
927 bt_field_finalize(field
);
932 void destroy_structure_field(struct bt_field
*field
)
934 struct bt_field_structure
*struct_field
= (void *) field
;
937 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
938 bt_field_finalize(field
);
940 if (struct_field
->fields
) {
941 g_ptr_array_free(struct_field
->fields
, TRUE
);
942 struct_field
->fields
= NULL
;
949 void destroy_variant_field(struct bt_field
*field
)
951 struct bt_field_variant
*var_field
= (void *) field
;
954 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
955 bt_field_finalize(field
);
957 if (var_field
->fields
) {
958 g_ptr_array_free(var_field
->fields
, TRUE
);
959 var_field
->fields
= NULL
;
966 void destroy_array_field(struct bt_field
*field
)
968 struct bt_field_array
*array_field
= (void *) field
;
971 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
972 bt_field_finalize(field
);
974 if (array_field
->fields
) {
975 g_ptr_array_free(array_field
->fields
, TRUE
);
976 array_field
->fields
= NULL
;
983 void destroy_string_field(struct bt_field
*field
)
985 struct bt_field_string
*string_field
= (void *) field
;
988 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
989 bt_field_finalize(field
);
991 if (string_field
->buf
) {
992 g_array_free(string_field
->buf
, TRUE
);
993 string_field
->buf
= NULL
;
1000 void bt_field_destroy(struct bt_field
*field
)
1003 BT_ASSERT(bt_field_class_has_known_type(field
->class));
1004 field_destroy_funcs
[field
->class->type
](field
);
1008 void reset_single_field(struct bt_field
*field
)
1011 field
->is_set
= false;
1015 void reset_structure_field(struct bt_field
*field
)
1018 struct bt_field_structure
*struct_field
= (void *) field
;
1022 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1023 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1028 void reset_variant_field(struct bt_field
*field
)
1031 struct bt_field_variant
*var_field
= (void *) field
;
1035 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1036 bt_field_reset(var_field
->fields
->pdata
[i
]);
1041 void reset_array_field(struct bt_field
*field
)
1044 struct bt_field_array
*array_field
= (void *) field
;
1048 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1049 bt_field_reset(array_field
->fields
->pdata
[i
]);
1054 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1056 field
->frozen
= is_frozen
;
1060 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1063 struct bt_field_structure
*struct_field
= (void *) field
;
1065 BT_LIB_LOGD("Setting structure field's frozen state: "
1066 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1068 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1069 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1071 BT_LIB_LOGD("Setting structure field's member field's "
1072 "frozen state: %![field-]+f, index=%" PRIu64
,
1074 bt_field_set_is_frozen(member_field
, is_frozen
);
1077 set_single_field_is_frozen(field
, is_frozen
);
1081 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1084 struct bt_field_variant
*var_field
= (void *) field
;
1086 BT_LIB_LOGD("Setting variant field's frozen state: "
1087 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1089 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1090 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1092 BT_LIB_LOGD("Setting variant field's option field's "
1093 "frozen state: %![field-]+f, index=%" PRIu64
,
1095 bt_field_set_is_frozen(option_field
, is_frozen
);
1098 set_single_field_is_frozen(field
, is_frozen
);
1102 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1105 struct bt_field_array
*array_field
= (void *) field
;
1107 BT_LIB_LOGD("Setting array field's frozen state: "
1108 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1110 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1111 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1113 BT_LIB_LOGD("Setting array field's element field's "
1114 "frozen state: %![field-]+f, index=%" PRIu64
,
1116 bt_field_set_is_frozen(elem_field
, is_frozen
);
1119 set_single_field_is_frozen(field
, is_frozen
);
1123 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1127 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1129 BT_ASSERT(field
->methods
->set_is_frozen
);
1130 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1134 bool single_field_is_set(const struct bt_field
*field
)
1137 return field
->is_set
;
1141 bool structure_field_is_set(const struct bt_field
*field
)
1145 const struct bt_field_structure
*struct_field
= (const void *) field
;
1149 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1150 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1161 bool variant_field_is_set(const struct bt_field
*field
)
1163 const struct bt_field_variant
*var_field
= (const void *) field
;
1164 bool is_set
= false;
1168 if (var_field
->selected_field
) {
1169 is_set
= bt_field_is_set(var_field
->selected_field
);
1176 bool array_field_is_set(const struct bt_field
*field
)
1180 const struct bt_field_array
*array_field
= (const void *) field
;
1184 for (i
= 0; i
< array_field
->length
; i
++) {
1185 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);