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 "FIELDS"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/field.h>
29 #include <babeltrace/trace-ir/field-const.h>
30 #include <babeltrace/trace-ir/field-internal.h>
31 #include <babeltrace/trace-ir/field-class-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/compat/fcntl-internal.h>
35 #include <babeltrace/align-internal.h>
36 #include <babeltrace/assert-internal.h>
40 void reset_single_field(struct bt_field
*field
);
43 void reset_array_field(struct bt_field
*field
);
46 void reset_structure_field(struct bt_field
*field
);
49 void reset_variant_field(struct bt_field
*field
);
52 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
55 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
58 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
61 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
64 bool single_field_is_set(const struct bt_field
*field
);
67 bool array_field_is_set(const struct bt_field
*field
);
70 bool structure_field_is_set(const struct bt_field
*field
);
73 bool variant_field_is_set(const struct bt_field
*field
);
76 struct bt_field_methods integer_field_methods
= {
77 .set_is_frozen
= set_single_field_is_frozen
,
78 .is_set
= single_field_is_set
,
79 .reset
= reset_single_field
,
83 struct bt_field_methods real_field_methods
= {
84 .set_is_frozen
= set_single_field_is_frozen
,
85 .is_set
= single_field_is_set
,
86 .reset
= reset_single_field
,
90 struct bt_field_methods string_field_methods
= {
91 .set_is_frozen
= set_single_field_is_frozen
,
92 .is_set
= single_field_is_set
,
93 .reset
= reset_single_field
,
97 struct bt_field_methods structure_field_methods
= {
98 .set_is_frozen
= set_structure_field_is_frozen
,
99 .is_set
= structure_field_is_set
,
100 .reset
= reset_structure_field
,
104 struct bt_field_methods array_field_methods
= {
105 .set_is_frozen
= set_array_field_is_frozen
,
106 .is_set
= array_field_is_set
,
107 .reset
= reset_array_field
,
111 struct bt_field_methods variant_field_methods
= {
112 .set_is_frozen
= set_variant_field_is_frozen
,
113 .is_set
= variant_field_is_set
,
114 .reset
= reset_variant_field
,
118 struct bt_field
*create_integer_field(struct bt_field_class
*);
121 struct bt_field
*create_real_field(struct bt_field_class
*);
124 struct bt_field
*create_string_field(struct bt_field_class
*);
127 struct bt_field
*create_structure_field(struct bt_field_class
*);
130 struct bt_field
*create_static_array_field(struct bt_field_class
*);
133 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
136 struct bt_field
*create_variant_field(struct bt_field_class
*);
139 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
140 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
141 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
142 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
143 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
144 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
145 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
146 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
147 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
148 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
149 [BT_FIELD_CLASS_TYPE_VARIANT
] = create_variant_field
,
153 void destroy_integer_field(struct bt_field
*field
);
156 void destroy_real_field(struct bt_field
*field
);
159 void destroy_string_field(struct bt_field
*field
);
162 void destroy_structure_field(struct bt_field
*field
);
165 void destroy_array_field(struct bt_field
*field
);
168 void destroy_variant_field(struct bt_field
*field
);
171 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
172 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
173 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
174 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
175 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
176 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
177 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
178 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
179 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
180 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
181 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
184 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
186 BT_ASSERT_PRE_NON_NULL(field
, "Field");
190 const struct bt_field_class
*bt_field_borrow_class_const(
191 const struct bt_field
*field
)
193 BT_ASSERT_PRE_NON_NULL(field
, "Field");
197 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
199 BT_ASSERT_PRE_NON_NULL(field
, "Field");
200 return field
->class->type
;
204 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
206 struct bt_field
*field
= NULL
;
208 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
209 BT_ASSERT(bt_field_class_has_known_type(fc
));
210 field
= field_create_funcs
[fc
->type
](fc
);
212 BT_LIB_LOGE("Cannot create field object from field class: "
222 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
223 struct bt_field_methods
*methods
)
227 bt_object_init_unique(&field
->base
);
228 field
->methods
= methods
;
230 bt_object_get_no_null_check(fc
);
234 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
236 struct bt_field_integer
*int_field
;
238 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
239 int_field
= g_new0(struct bt_field_integer
, 1);
241 BT_LOGE_STR("Failed to allocate one integer field.");
245 init_field((void *) int_field
, fc
, &integer_field_methods
);
246 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
249 return (void *) int_field
;
253 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
255 struct bt_field_real
*real_field
;
257 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
258 real_field
= g_new0(struct bt_field_real
, 1);
260 BT_LOGE_STR("Failed to allocate one real field.");
264 init_field((void *) real_field
, fc
, &real_field_methods
);
265 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
268 return (void *) real_field
;
272 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
274 struct bt_field_string
*string_field
;
276 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
277 string_field
= g_new0(struct bt_field_string
, 1);
279 BT_LOGE_STR("Failed to allocate one string field.");
283 init_field((void *) string_field
, fc
, &string_field_methods
);
284 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
286 if (!string_field
->buf
) {
287 BT_LOGE_STR("Failed to allocate a GArray.");
288 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
292 g_array_index(string_field
->buf
, char, 0) = '\0';
293 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
296 return (void *) string_field
;
300 int create_fields_from_named_field_classes(
301 struct bt_field_class_named_field_class_container
*fc
,
307 *fields
= g_ptr_array_new_with_free_func(
308 (GDestroyNotify
) bt_field_destroy
);
310 BT_LOGE_STR("Failed to allocate a GPtrArray.");
315 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
317 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
318 struct bt_field
*field
;
319 struct bt_named_field_class
*named_fc
=
320 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, i
);
322 field
= bt_field_create(named_fc
->fc
);
324 BT_LIB_LOGE("Failed to create structure member or variant option field: "
325 "name=\"%s\", %![fc-]+F",
326 named_fc
->name
->str
, named_fc
->fc
);
331 g_ptr_array_index(*fields
, i
) = field
;
339 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
341 struct bt_field_structure
*struct_field
;
343 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
344 struct_field
= g_new0(struct bt_field_structure
, 1);
346 BT_LOGE_STR("Failed to allocate one structure field.");
350 init_field((void *) struct_field
, fc
, &structure_field_methods
);
352 if (create_fields_from_named_field_classes((void *) fc
,
353 &struct_field
->fields
)) {
354 BT_LIB_LOGE("Cannot create structure member fields: "
356 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
360 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
363 return (void *) struct_field
;
367 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
369 struct bt_field_variant
*var_field
;
371 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
372 var_field
= g_new0(struct bt_field_variant
, 1);
374 BT_LOGE_STR("Failed to allocate one variant field.");
378 init_field((void *) var_field
, fc
, &variant_field_methods
);
380 if (create_fields_from_named_field_classes((void *) fc
,
381 &var_field
->fields
)) {
382 BT_LIB_LOGE("Cannot create variant member fields: "
384 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
388 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
391 return (void *) var_field
;
395 int init_array_field_fields(struct bt_field_array
*array_field
)
399 struct bt_field_class_array
*array_fc
;
401 BT_ASSERT(array_field
);
402 array_fc
= (void *) array_field
->common
.class;
403 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
404 if (!array_field
->fields
) {
405 BT_LOGE_STR("Failed to allocate a GPtrArray.");
410 g_ptr_array_set_free_func(array_field
->fields
,
411 (GDestroyNotify
) bt_field_destroy
);
412 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
414 for (i
= 0; i
< array_field
->length
; i
++) {
415 array_field
->fields
->pdata
[i
] = bt_field_create(
416 array_fc
->element_fc
);
417 if (!array_field
->fields
->pdata
[i
]) {
418 BT_LIB_LOGE("Cannot create array field's element field: "
419 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
430 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
432 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
433 struct bt_field_array
*array_field
;
435 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
436 array_field
= g_new0(struct bt_field_array
, 1);
438 BT_LOGE_STR("Failed to allocate one static array field.");
442 init_field((void *) array_field
, fc
, &array_field_methods
);
443 array_field
->length
= array_fc
->length
;
445 if (init_array_field_fields(array_field
)) {
446 BT_LIB_LOGE("Cannot create static array fields: "
448 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
452 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
455 return (void *) array_field
;
459 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
461 struct bt_field_array
*array_field
;
463 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
464 array_field
= g_new0(struct bt_field_array
, 1);
466 BT_LOGE_STR("Failed to allocate one dynamic array field.");
470 init_field((void *) array_field
, fc
, &array_field_methods
);
472 if (init_array_field_fields(array_field
)) {
473 BT_LIB_LOGE("Cannot create dynamic array fields: "
475 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
479 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
482 return (void *) array_field
;
485 int64_t bt_field_signed_integer_get_value(const struct bt_field
*field
)
487 const struct bt_field_integer
*int_field
= (const void *) field
;
489 BT_ASSERT_PRE_NON_NULL(field
, "Field");
490 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
491 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
492 return int_field
->value
.i
;
495 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
497 struct bt_field_integer
*int_field
= (void *) field
;
499 BT_ASSERT_PRE_NON_NULL(field
, "Field");
500 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
501 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
502 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(
503 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
504 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
505 "%![fc-]+F", value
, field
, field
->class);
506 int_field
->value
.i
= value
;
507 bt_field_set_single(field
, true);
510 uint64_t bt_field_unsigned_integer_get_value(const struct bt_field
*field
)
512 const struct bt_field_integer
*int_field
= (const void *) field
;
514 BT_ASSERT_PRE_NON_NULL(field
, "Field");
515 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
516 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
517 return int_field
->value
.u
;
520 void bt_field_unsigned_integer_set_value(struct bt_field
*field
, uint64_t value
)
522 struct bt_field_integer
*int_field
= (void *) field
;
524 BT_ASSERT_PRE_NON_NULL(field
, "Field");
525 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
526 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
527 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
528 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
529 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
530 "%![fc-]+F", value
, field
, field
->class);
531 int_field
->value
.u
= value
;
532 bt_field_set_single(field
, true);
535 double bt_field_real_get_value(const struct bt_field
*field
)
537 const struct bt_field_real
*real_field
= (const void *) field
;
539 BT_ASSERT_PRE_NON_NULL(field
, "Field");
540 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
541 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
542 return real_field
->value
;
545 void bt_field_real_set_value(struct bt_field
*field
, double value
)
547 struct bt_field_real
*real_field
= (void *) field
;
549 BT_ASSERT_PRE_NON_NULL(field
, "Field");
550 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
551 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
553 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
554 (double) (float) value
== value
,
555 "Invalid value for a single-precision real number: value=%f, "
556 "%![fc-]+F", value
, field
->class);
557 real_field
->value
= value
;
558 bt_field_set_single(field
, true);
561 enum bt_field_status
bt_field_unsigned_enumeration_get_mapping_labels(
562 const struct bt_field
*field
,
563 bt_field_class_enumeration_mapping_label_array
*label_array
,
566 const struct bt_field_integer
*int_field
= (const void *) field
;
568 BT_ASSERT_PRE_NON_NULL(field
, "Field");
569 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
570 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
571 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
572 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
573 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
575 bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
576 field
->class, int_field
->value
.u
, label_array
, count
);
579 enum bt_field_status
bt_field_signed_enumeration_get_mapping_labels(
580 const struct bt_field
*field
,
581 bt_field_class_enumeration_mapping_label_array
*label_array
,
584 const struct bt_field_integer
*int_field
= (const void *) field
;
586 BT_ASSERT_PRE_NON_NULL(field
, "Field");
587 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
588 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
589 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
590 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
591 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
593 bt_field_class_signed_enumeration_get_mapping_labels_by_value(
594 field
->class, int_field
->value
.i
, label_array
, count
);
597 const char *bt_field_string_get_value(const struct bt_field
*field
)
599 const struct bt_field_string
*string_field
= (const void *) field
;
601 BT_ASSERT_PRE_NON_NULL(field
, "Field");
602 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
603 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
605 return (const char *) string_field
->buf
->data
;
608 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
610 const struct bt_field_string
*string_field
= (const void *) field
;
612 BT_ASSERT_PRE_NON_NULL(field
, "Field");
613 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
614 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
616 return string_field
->length
;
620 void clear_string_field(struct bt_field
*field
)
622 struct bt_field_string
*string_field
= (void *) field
;
625 string_field
->length
= 0;
626 bt_field_set_single(field
, true);
629 enum bt_field_status
bt_field_string_set_value(struct bt_field
*field
,
632 BT_ASSERT_PRE_NON_NULL(field
, "Field");
633 BT_ASSERT_PRE_NON_NULL(value
, "Value");
634 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
635 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
637 clear_string_field(field
);
638 return bt_field_string_append_with_length(field
, value
,
639 (uint64_t) strlen(value
));
642 enum bt_field_status
bt_field_string_append(struct bt_field
*field
, const char *value
)
644 return bt_field_string_append_with_length(field
,
645 value
, (uint64_t) strlen(value
));
648 enum bt_field_status
bt_field_string_append_with_length(struct bt_field
*field
,
649 const char *value
, uint64_t length
)
651 struct bt_field_string
*string_field
= (void *) field
;
655 BT_ASSERT_PRE_NON_NULL(field
, "Field");
656 BT_ASSERT_PRE_NON_NULL(value
, "Value");
657 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
658 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
659 BT_FIELD_CLASS_TYPE_STRING
, "Field");
661 /* Make sure no null bytes are appended */
662 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
663 "String value to append contains a null character: "
664 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
666 new_length
= length
+ string_field
->length
;
668 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
669 g_array_set_size(string_field
->buf
, new_length
+ 1);
672 data
= string_field
->buf
->data
;
673 memcpy(data
+ string_field
->length
, value
, length
);
674 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
675 string_field
->length
= new_length
;
676 bt_field_set_single(field
, true);
677 return BT_FIELD_STATUS_OK
;
680 enum bt_field_status
bt_field_string_clear(struct bt_field
*field
)
682 BT_ASSERT_PRE_NON_NULL(field
, "Field");
683 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
684 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
685 BT_FIELD_CLASS_TYPE_STRING
, "Field");
686 clear_string_field(field
);
687 return BT_FIELD_STATUS_OK
;
690 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
692 const struct bt_field_array
*array_field
= (const void *) field
;
694 BT_ASSERT_PRE_NON_NULL(field
, "Field");
695 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
696 return array_field
->length
;
699 enum bt_field_status
bt_field_dynamic_array_set_length(struct bt_field
*field
,
702 int ret
= BT_FIELD_STATUS_OK
;
703 struct bt_field_array
*array_field
= (void *) field
;
705 BT_ASSERT_PRE_NON_NULL(field
, "Field");
706 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
707 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
708 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
710 if (unlikely(length
> array_field
->fields
->len
)) {
712 struct bt_field_class_array
*array_fc
;
713 uint64_t cur_len
= array_field
->fields
->len
;
716 g_ptr_array_set_size(array_field
->fields
, length
);
717 array_fc
= (void *) field
->class;
719 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
720 struct bt_field
*elem_field
= bt_field_create(
721 array_fc
->element_fc
);
724 BT_LIB_LOGE("Cannot create element field for "
725 "dynamic array field: "
726 "index=%" PRIu64
", "
727 "%![array-field-]+f", i
, field
);
728 ret
= BT_FIELD_STATUS_NOMEM
;
732 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
733 array_field
->fields
->pdata
[i
] = elem_field
;
737 array_field
->length
= length
;
744 struct bt_field
*borrow_array_field_element_field_by_index(
745 struct bt_field
*field
, uint64_t index
)
747 struct bt_field_array
*array_field
= (void *) field
;
749 BT_ASSERT_PRE_NON_NULL(field
, "Field");
750 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
751 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
752 return array_field
->fields
->pdata
[index
];
755 struct bt_field
*bt_field_array_borrow_element_field_by_index(
756 struct bt_field
*field
, uint64_t index
)
758 return borrow_array_field_element_field_by_index(field
, index
);
761 const struct bt_field
*
762 bt_field_array_borrow_element_field_by_index_const(
763 const struct bt_field
*field
, uint64_t index
)
765 return borrow_array_field_element_field_by_index((void *) field
, index
);
769 struct bt_field
*borrow_structure_field_member_field_by_index(
770 struct bt_field
*field
, uint64_t index
)
772 struct bt_field_structure
*struct_field
= (void *) field
;
774 BT_ASSERT_PRE_NON_NULL(field
, "Field");
775 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
776 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
777 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
778 return struct_field
->fields
->pdata
[index
];
781 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
782 struct bt_field
*field
, uint64_t index
)
784 return borrow_structure_field_member_field_by_index(field
,
788 const struct bt_field
*
789 bt_field_structure_borrow_member_field_by_index_const(
790 const struct bt_field
*field
, uint64_t index
)
792 return borrow_structure_field_member_field_by_index(
793 (void *) field
, index
);
797 struct bt_field
*borrow_structure_field_member_field_by_name(
798 struct bt_field
*field
, const char *name
)
800 struct bt_field
*ret_field
= NULL
;
801 struct bt_field_class_structure
*struct_fc
;
802 struct bt_field_structure
*struct_field
= (void *) field
;
806 BT_ASSERT_PRE_NON_NULL(field
, "Field");
807 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
808 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
809 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
810 struct_fc
= (void *) field
->class;
812 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
813 &orig_key
, &index
)) {
817 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
818 BT_ASSERT(ret_field
);
824 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
825 struct bt_field
*field
, const char *name
)
827 return borrow_structure_field_member_field_by_name(field
, name
);
830 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
831 const struct bt_field
*field
, const char *name
)
833 return borrow_structure_field_member_field_by_name(
834 (void *) field
, name
);
838 struct bt_field
*borrow_variant_field_selected_option_field(
839 struct bt_field
*field
)
841 struct bt_field_variant
*var_field
= (void *) field
;
843 BT_ASSERT_PRE_NON_NULL(field
, "Field");
844 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
845 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
846 BT_ASSERT_PRE(var_field
->selected_field
,
847 "Variant field has no selected field: %!+f", field
);
848 return var_field
->selected_field
;
851 struct bt_field
*bt_field_variant_borrow_selected_option_field(
852 struct bt_field
*field
)
854 return borrow_variant_field_selected_option_field(field
);
857 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
858 const struct bt_field
*field
)
860 return borrow_variant_field_selected_option_field((void *) field
);
863 enum bt_field_status
bt_field_variant_select_option_field(
864 struct bt_field
*field
, uint64_t index
)
866 struct bt_field_variant
*var_field
= (void *) field
;
868 BT_ASSERT_PRE_NON_NULL(field
, "Field");
869 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
870 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
871 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
872 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
873 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
874 var_field
->selected_index
= index
;
875 return BT_FIELD_STATUS_OK
;
878 uint64_t bt_field_variant_get_selected_option_field_index(
879 const struct bt_field
*field
)
881 const struct bt_field_variant
*var_field
= (const void *) field
;
883 BT_ASSERT_PRE_NON_NULL(field
, "Field");
884 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
885 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
886 BT_ASSERT_PRE(var_field
->selected_field
,
887 "Variant field has no selected field: %!+f", field
);
888 return var_field
->selected_index
;
892 void bt_field_finalize(struct bt_field
*field
)
895 BT_LOGD_STR("Putting field's class.");
896 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
900 void destroy_integer_field(struct bt_field
*field
)
903 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
904 bt_field_finalize(field
);
909 void destroy_real_field(struct bt_field
*field
)
912 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
913 bt_field_finalize(field
);
918 void destroy_structure_field(struct bt_field
*field
)
920 struct bt_field_structure
*struct_field
= (void *) field
;
923 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
924 bt_field_finalize(field
);
926 if (struct_field
->fields
) {
927 g_ptr_array_free(struct_field
->fields
, TRUE
);
928 struct_field
->fields
= NULL
;
935 void destroy_variant_field(struct bt_field
*field
)
937 struct bt_field_variant
*var_field
= (void *) field
;
940 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
941 bt_field_finalize(field
);
943 if (var_field
->fields
) {
944 g_ptr_array_free(var_field
->fields
, TRUE
);
945 var_field
->fields
= NULL
;
952 void destroy_array_field(struct bt_field
*field
)
954 struct bt_field_array
*array_field
= (void *) field
;
957 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
958 bt_field_finalize(field
);
960 if (array_field
->fields
) {
961 g_ptr_array_free(array_field
->fields
, TRUE
);
962 array_field
->fields
= NULL
;
969 void destroy_string_field(struct bt_field
*field
)
971 struct bt_field_string
*string_field
= (void *) field
;
974 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
975 bt_field_finalize(field
);
977 if (string_field
->buf
) {
978 g_array_free(string_field
->buf
, TRUE
);
979 string_field
->buf
= NULL
;
986 void bt_field_destroy(struct bt_field
*field
)
989 BT_ASSERT(bt_field_class_has_known_type(field
->class));
990 field_destroy_funcs
[field
->class->type
](field
);
994 void reset_single_field(struct bt_field
*field
)
997 field
->is_set
= false;
1001 void reset_structure_field(struct bt_field
*field
)
1004 struct bt_field_structure
*struct_field
= (void *) field
;
1008 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1009 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1014 void reset_variant_field(struct bt_field
*field
)
1017 struct bt_field_variant
*var_field
= (void *) field
;
1021 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1022 bt_field_reset(var_field
->fields
->pdata
[i
]);
1027 void reset_array_field(struct bt_field
*field
)
1030 struct bt_field_array
*array_field
= (void *) field
;
1034 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1035 bt_field_reset(array_field
->fields
->pdata
[i
]);
1040 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1042 field
->frozen
= is_frozen
;
1046 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1049 struct bt_field_structure
*struct_field
= (void *) field
;
1051 BT_LIB_LOGD("Setting structure field's frozen state: "
1052 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1054 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1055 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1057 BT_LIB_LOGD("Setting structure field's member field's "
1058 "frozen state: %![field-]+f, index=%" PRIu64
,
1060 bt_field_set_is_frozen(member_field
, is_frozen
);
1063 set_single_field_is_frozen(field
, is_frozen
);
1067 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1070 struct bt_field_variant
*var_field
= (void *) field
;
1072 BT_LIB_LOGD("Setting variant field's frozen state: "
1073 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1075 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1076 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1078 BT_LIB_LOGD("Setting variant field's option field's "
1079 "frozen state: %![field-]+f, index=%" PRIu64
,
1081 bt_field_set_is_frozen(option_field
, is_frozen
);
1084 set_single_field_is_frozen(field
, is_frozen
);
1088 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1091 struct bt_field_array
*array_field
= (void *) field
;
1093 BT_LIB_LOGD("Setting array field's frozen state: "
1094 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1096 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1097 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1099 BT_LIB_LOGD("Setting array field's element field's "
1100 "frozen state: %![field-]+f, index=%" PRIu64
,
1102 bt_field_set_is_frozen(elem_field
, is_frozen
);
1105 set_single_field_is_frozen(field
, is_frozen
);
1109 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1113 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1115 BT_ASSERT(field
->methods
->set_is_frozen
);
1116 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1120 bool single_field_is_set(const struct bt_field
*field
)
1123 return field
->is_set
;
1127 bool structure_field_is_set(const struct bt_field
*field
)
1131 const struct bt_field_structure
*struct_field
= (const void *) field
;
1135 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1136 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1147 bool variant_field_is_set(const struct bt_field
*field
)
1149 const struct bt_field_variant
*var_field
= (const void *) field
;
1150 bool is_set
= false;
1154 if (var_field
->selected_field
) {
1155 is_set
= bt_field_is_set(var_field
->selected_field
);
1162 bool array_field_is_set(const struct bt_field
*field
)
1166 const struct bt_field_array
*array_field
= (const void *) field
;
1170 for (i
= 0; i
< array_field
->length
; i
++) {
1171 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);