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/object.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/compat/fcntl-internal.h>
36 #include <babeltrace/align-internal.h>
37 #include <babeltrace/assert-internal.h>
41 void reset_single_field(struct bt_field
*field
);
44 void reset_array_field(struct bt_field
*field
);
47 void reset_structure_field(struct bt_field
*field
);
50 void reset_variant_field(struct bt_field
*field
);
53 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
56 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
59 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
62 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
65 bool single_field_is_set(const struct bt_field
*field
);
68 bool array_field_is_set(const struct bt_field
*field
);
71 bool structure_field_is_set(const struct bt_field
*field
);
74 bool variant_field_is_set(const struct bt_field
*field
);
77 struct bt_field_methods integer_field_methods
= {
78 .set_is_frozen
= set_single_field_is_frozen
,
79 .is_set
= single_field_is_set
,
80 .reset
= reset_single_field
,
84 struct bt_field_methods real_field_methods
= {
85 .set_is_frozen
= set_single_field_is_frozen
,
86 .is_set
= single_field_is_set
,
87 .reset
= reset_single_field
,
91 struct bt_field_methods string_field_methods
= {
92 .set_is_frozen
= set_single_field_is_frozen
,
93 .is_set
= single_field_is_set
,
94 .reset
= reset_single_field
,
98 struct bt_field_methods structure_field_methods
= {
99 .set_is_frozen
= set_structure_field_is_frozen
,
100 .is_set
= structure_field_is_set
,
101 .reset
= reset_structure_field
,
105 struct bt_field_methods array_field_methods
= {
106 .set_is_frozen
= set_array_field_is_frozen
,
107 .is_set
= array_field_is_set
,
108 .reset
= reset_array_field
,
112 struct bt_field_methods variant_field_methods
= {
113 .set_is_frozen
= set_variant_field_is_frozen
,
114 .is_set
= variant_field_is_set
,
115 .reset
= reset_variant_field
,
119 struct bt_field
*create_integer_field(struct bt_field_class
*);
122 struct bt_field
*create_real_field(struct bt_field_class
*);
125 struct bt_field
*create_string_field(struct bt_field_class
*);
128 struct bt_field
*create_structure_field(struct bt_field_class
*);
131 struct bt_field
*create_static_array_field(struct bt_field_class
*);
134 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
137 struct bt_field
*create_variant_field(struct bt_field_class
*);
140 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
141 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
142 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
143 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
144 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
145 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
146 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
147 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
148 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
149 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
150 [BT_FIELD_CLASS_TYPE_VARIANT
] = create_variant_field
,
154 void destroy_integer_field(struct bt_field
*field
);
157 void destroy_real_field(struct bt_field
*field
);
160 void destroy_string_field(struct bt_field
*field
);
163 void destroy_structure_field(struct bt_field
*field
);
166 void destroy_array_field(struct bt_field
*field
);
169 void destroy_variant_field(struct bt_field
*field
);
172 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
173 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
174 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
175 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
176 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
177 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
178 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
179 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
180 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
181 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
182 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
185 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
187 BT_ASSERT_PRE_NON_NULL(field
, "Field");
191 const struct bt_field_class
*bt_field_borrow_class_const(
192 const struct bt_field
*field
)
194 BT_ASSERT_PRE_NON_NULL(field
, "Field");
198 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
200 BT_ASSERT_PRE_NON_NULL(field
, "Field");
201 return field
->class->type
;
205 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
207 struct bt_field
*field
= NULL
;
209 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
210 BT_ASSERT(bt_field_class_has_known_type(fc
));
211 field
= field_create_funcs
[fc
->type
](fc
);
213 BT_LIB_LOGE("Cannot create field object from field class: "
223 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
224 struct bt_field_methods
*methods
)
228 bt_object_init_unique(&field
->base
);
229 field
->methods
= methods
;
231 bt_object_get_no_null_check(fc
);
235 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
237 struct bt_field_integer
*int_field
;
239 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
240 int_field
= g_new0(struct bt_field_integer
, 1);
242 BT_LOGE_STR("Failed to allocate one integer field.");
246 init_field((void *) int_field
, fc
, &integer_field_methods
);
247 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
250 return (void *) int_field
;
254 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
256 struct bt_field_real
*real_field
;
258 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
259 real_field
= g_new0(struct bt_field_real
, 1);
261 BT_LOGE_STR("Failed to allocate one real field.");
265 init_field((void *) real_field
, fc
, &real_field_methods
);
266 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
269 return (void *) real_field
;
273 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
275 struct bt_field_string
*string_field
;
277 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
278 string_field
= g_new0(struct bt_field_string
, 1);
280 BT_LOGE_STR("Failed to allocate one string field.");
284 init_field((void *) string_field
, fc
, &string_field_methods
);
285 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
287 if (!string_field
->buf
) {
288 BT_LOGE_STR("Failed to allocate a GArray.");
289 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
293 g_array_index(string_field
->buf
, char, 0) = '\0';
294 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
297 return (void *) string_field
;
301 int create_fields_from_named_field_classes(
302 struct bt_field_class_named_field_class_container
*fc
,
308 *fields
= g_ptr_array_new_with_free_func(
309 (GDestroyNotify
) bt_field_destroy
);
311 BT_LOGE_STR("Failed to allocate a GPtrArray.");
316 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
318 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
319 struct bt_field
*field
;
320 struct bt_named_field_class
*named_fc
=
321 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, i
);
323 field
= bt_field_create(named_fc
->fc
);
325 BT_LIB_LOGE("Failed to create structure member or variant option field: "
326 "name=\"%s\", %![fc-]+F",
327 named_fc
->name
->str
, named_fc
->fc
);
332 g_ptr_array_index(*fields
, i
) = field
;
340 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
342 struct bt_field_structure
*struct_field
;
344 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
345 struct_field
= g_new0(struct bt_field_structure
, 1);
347 BT_LOGE_STR("Failed to allocate one structure field.");
351 init_field((void *) struct_field
, fc
, &structure_field_methods
);
353 if (create_fields_from_named_field_classes((void *) fc
,
354 &struct_field
->fields
)) {
355 BT_LIB_LOGE("Cannot create structure member fields: "
357 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
361 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
364 return (void *) struct_field
;
368 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
370 struct bt_field_variant
*var_field
;
372 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
373 var_field
= g_new0(struct bt_field_variant
, 1);
375 BT_LOGE_STR("Failed to allocate one variant field.");
379 init_field((void *) var_field
, fc
, &variant_field_methods
);
381 if (create_fields_from_named_field_classes((void *) fc
,
382 &var_field
->fields
)) {
383 BT_LIB_LOGE("Cannot create variant member fields: "
385 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
389 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
392 return (void *) var_field
;
396 int init_array_field_fields(struct bt_field_array
*array_field
)
400 struct bt_field_class_array
*array_fc
;
402 BT_ASSERT(array_field
);
403 array_fc
= (void *) array_field
->common
.class;
404 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
405 if (!array_field
->fields
) {
406 BT_LOGE_STR("Failed to allocate a GPtrArray.");
411 g_ptr_array_set_free_func(array_field
->fields
,
412 (GDestroyNotify
) bt_field_destroy
);
413 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
415 for (i
= 0; i
< array_field
->length
; i
++) {
416 array_field
->fields
->pdata
[i
] = bt_field_create(
417 array_fc
->element_fc
);
418 if (!array_field
->fields
->pdata
[i
]) {
419 BT_LIB_LOGE("Cannot create array field's element field: "
420 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
431 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
433 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
434 struct bt_field_array
*array_field
;
436 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
437 array_field
= g_new0(struct bt_field_array
, 1);
439 BT_LOGE_STR("Failed to allocate one static array field.");
443 init_field((void *) array_field
, fc
, &array_field_methods
);
444 array_field
->length
= array_fc
->length
;
446 if (init_array_field_fields(array_field
)) {
447 BT_LIB_LOGE("Cannot create static array fields: "
449 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
453 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
456 return (void *) array_field
;
460 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
462 struct bt_field_array
*array_field
;
464 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
465 array_field
= g_new0(struct bt_field_array
, 1);
467 BT_LOGE_STR("Failed to allocate one dynamic array field.");
471 init_field((void *) array_field
, fc
, &array_field_methods
);
473 if (init_array_field_fields(array_field
)) {
474 BT_LIB_LOGE("Cannot create dynamic array fields: "
476 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
480 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
483 return (void *) array_field
;
486 int64_t bt_field_signed_integer_get_value(const struct bt_field
*field
)
488 const struct bt_field_integer
*int_field
= (const void *) field
;
490 BT_ASSERT_PRE_NON_NULL(field
, "Field");
491 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
492 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
493 return int_field
->value
.i
;
496 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
498 struct bt_field_integer
*int_field
= (void *) field
;
500 BT_ASSERT_PRE_NON_NULL(field
, "Field");
501 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
502 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
503 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(
504 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
505 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
506 "%![fc-]+F", value
, field
, field
->class);
507 int_field
->value
.i
= value
;
508 bt_field_set_single(field
, true);
511 uint64_t bt_field_unsigned_integer_get_value(const struct bt_field
*field
)
513 const struct bt_field_integer
*int_field
= (const void *) field
;
515 BT_ASSERT_PRE_NON_NULL(field
, "Field");
516 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
517 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
518 return int_field
->value
.u
;
521 void bt_field_unsigned_integer_set_value(struct bt_field
*field
, uint64_t value
)
523 struct bt_field_integer
*int_field
= (void *) field
;
525 BT_ASSERT_PRE_NON_NULL(field
, "Field");
526 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
527 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
528 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
529 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
530 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
531 "%![fc-]+F", value
, field
, field
->class);
532 int_field
->value
.u
= value
;
533 bt_field_set_single(field
, true);
536 double bt_field_real_get_value(const struct bt_field
*field
)
538 const struct bt_field_real
*real_field
= (const void *) field
;
540 BT_ASSERT_PRE_NON_NULL(field
, "Field");
541 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
542 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
543 return real_field
->value
;
546 void bt_field_real_set_value(struct bt_field
*field
, double value
)
548 struct bt_field_real
*real_field
= (void *) field
;
550 BT_ASSERT_PRE_NON_NULL(field
, "Field");
551 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
552 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
554 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
555 (double) (float) value
== value
,
556 "Invalid value for a single-precision real number: value=%f, "
557 "%![fc-]+F", value
, field
->class);
558 real_field
->value
= value
;
559 bt_field_set_single(field
, true);
562 int bt_field_unsigned_enumeration_get_mapping_labels(
563 const struct bt_field
*field
,
564 bt_field_class_enumeration_mapping_label_array
*label_array
,
567 const struct bt_field_integer
*int_field
= (const void *) field
;
569 BT_ASSERT_PRE_NON_NULL(field
, "Field");
570 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
571 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
572 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
573 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
574 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
575 return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
576 field
->class, int_field
->value
.u
, label_array
, count
);
579 int 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");
592 return bt_field_class_signed_enumeration_get_mapping_labels_by_value(
593 field
->class, int_field
->value
.i
, label_array
, count
);
596 const char *bt_field_string_get_value(const struct bt_field
*field
)
598 const struct bt_field_string
*string_field
= (const void *) field
;
600 BT_ASSERT_PRE_NON_NULL(field
, "Field");
601 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
602 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
604 return (const char *) string_field
->buf
->data
;
607 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
609 const struct bt_field_string
*string_field
= (const void *) field
;
611 BT_ASSERT_PRE_NON_NULL(field
, "Field");
612 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
613 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
615 return string_field
->length
;
618 int bt_field_string_set_value(struct bt_field
*field
, const char *value
)
620 BT_ASSERT_PRE_NON_NULL(field
, "Field");
621 BT_ASSERT_PRE_NON_NULL(value
, "Value");
622 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
623 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
625 bt_field_string_clear(field
);
626 return bt_field_string_append_with_length(field
, value
,
627 (uint64_t) strlen(value
));
630 int bt_field_string_append(struct bt_field
*field
, const char *value
)
632 return bt_field_string_append_with_length(field
,
633 value
, (uint64_t) strlen(value
));
636 int bt_field_string_append_with_length(struct bt_field
*field
,
637 const char *value
, uint64_t length
)
639 struct bt_field_string
*string_field
= (void *) field
;
643 BT_ASSERT_PRE_NON_NULL(field
, "Field");
644 BT_ASSERT_PRE_NON_NULL(value
, "Value");
645 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
646 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
647 BT_FIELD_CLASS_TYPE_STRING
, "Field");
649 /* Make sure no null bytes are appended */
650 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
651 "String value to append contains a null character: "
652 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
654 new_length
= length
+ string_field
->length
;
656 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
657 g_array_set_size(string_field
->buf
, new_length
+ 1);
660 data
= string_field
->buf
->data
;
661 memcpy(data
+ string_field
->length
, value
, length
);
662 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
663 string_field
->length
= new_length
;
664 bt_field_set_single(field
, true);
668 int bt_field_string_clear(struct bt_field
*field
)
670 struct bt_field_string
*string_field
= (void *) field
;
672 BT_ASSERT_PRE_NON_NULL(field
, "Field");
673 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
674 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
675 BT_FIELD_CLASS_TYPE_STRING
, "Field");
676 string_field
->length
= 0;
677 bt_field_set_single(field
, true);
681 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
683 const struct bt_field_array
*array_field
= (const void *) field
;
685 BT_ASSERT_PRE_NON_NULL(field
, "Field");
686 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
687 return array_field
->length
;
690 int bt_field_dynamic_array_set_length(struct bt_field
*field
, uint64_t length
)
693 struct bt_field_array
*array_field
= (void *) field
;
695 BT_ASSERT_PRE_NON_NULL(field
, "Field");
696 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
697 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
698 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
700 if (unlikely(length
> array_field
->fields
->len
)) {
702 struct bt_field_class_array
*array_fc
;
703 uint64_t cur_len
= array_field
->fields
->len
;
706 g_ptr_array_set_size(array_field
->fields
, length
);
707 array_fc
= (void *) field
->class;
709 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
710 struct bt_field
*elem_field
= bt_field_create(
711 array_fc
->element_fc
);
714 BT_LIB_LOGE("Cannot create element field for "
715 "dynamic array field: "
716 "index=%" PRIu64
", "
717 "%![array-field-]+f", i
, field
);
722 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
723 array_field
->fields
->pdata
[i
] = elem_field
;
727 array_field
->length
= length
;
734 struct bt_field
*borrow_array_field_element_field_by_index(
735 struct bt_field
*field
, uint64_t index
)
737 struct bt_field_array
*array_field
= (void *) field
;
739 BT_ASSERT_PRE_NON_NULL(field
, "Field");
740 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
741 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
742 return array_field
->fields
->pdata
[index
];
745 struct bt_field
*bt_field_array_borrow_element_field_by_index(
746 struct bt_field
*field
, uint64_t index
)
748 return borrow_array_field_element_field_by_index(field
, index
);
751 const struct bt_field
*
752 bt_field_array_borrow_element_field_by_index_const(
753 const struct bt_field
*field
, uint64_t index
)
755 return borrow_array_field_element_field_by_index((void *) field
, index
);
759 struct bt_field
*borrow_structure_field_member_field_by_index(
760 struct bt_field
*field
, uint64_t index
)
762 struct bt_field_structure
*struct_field
= (void *) field
;
764 BT_ASSERT_PRE_NON_NULL(field
, "Field");
765 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
766 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
767 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
768 return struct_field
->fields
->pdata
[index
];
771 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
772 struct bt_field
*field
, uint64_t index
)
774 return borrow_structure_field_member_field_by_index(field
,
778 const struct bt_field
*
779 bt_field_structure_borrow_member_field_by_index_const(
780 const struct bt_field
*field
, uint64_t index
)
782 return borrow_structure_field_member_field_by_index(
783 (void *) field
, index
);
787 struct bt_field
*borrow_structure_field_member_field_by_name(
788 struct bt_field
*field
, const char *name
)
790 struct bt_field
*ret_field
= NULL
;
791 struct bt_field_class_structure
*struct_fc
;
792 struct bt_field_structure
*struct_field
= (void *) field
;
796 BT_ASSERT_PRE_NON_NULL(field
, "Field");
797 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
798 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
799 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
800 struct_fc
= (void *) field
->class;
802 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
803 &orig_key
, &index
)) {
807 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
808 BT_ASSERT(ret_field
);
814 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
815 struct bt_field
*field
, const char *name
)
817 return borrow_structure_field_member_field_by_name(field
, name
);
820 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
821 const struct bt_field
*field
, const char *name
)
823 return borrow_structure_field_member_field_by_name(
824 (void *) field
, name
);
828 struct bt_field
*borrow_variant_field_selected_option_field(
829 struct bt_field
*field
)
831 struct bt_field_variant
*var_field
= (void *) field
;
833 BT_ASSERT_PRE_NON_NULL(field
, "Field");
834 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
835 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
836 BT_ASSERT_PRE(var_field
->selected_field
,
837 "Variant field has no selected field: %!+f", field
);
838 return var_field
->selected_field
;
841 struct bt_field
*bt_field_variant_borrow_selected_option_field(
842 struct bt_field
*field
)
844 return borrow_variant_field_selected_option_field(field
);
847 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
848 const struct bt_field
*field
)
850 return borrow_variant_field_selected_option_field((void *) field
);
853 int bt_field_variant_select_option_field(
854 struct bt_field
*field
, uint64_t index
)
856 struct bt_field_variant
*var_field
= (void *) field
;
858 BT_ASSERT_PRE_NON_NULL(field
, "Field");
859 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
860 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
861 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
862 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
863 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
864 var_field
->selected_index
= index
;
868 uint64_t bt_field_variant_get_selected_option_field_index(
869 const struct bt_field
*field
)
871 const struct bt_field_variant
*var_field
= (const void *) field
;
873 BT_ASSERT_PRE_NON_NULL(field
, "Field");
874 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
875 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
876 BT_ASSERT_PRE(var_field
->selected_field
,
877 "Variant field has no selected field: %!+f", field
);
878 return var_field
->selected_index
;
882 void bt_field_finalize(struct bt_field
*field
)
885 BT_LOGD_STR("Putting field's class.");
886 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
890 void destroy_integer_field(struct bt_field
*field
)
893 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
894 bt_field_finalize(field
);
899 void destroy_real_field(struct bt_field
*field
)
902 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
903 bt_field_finalize(field
);
908 void destroy_structure_field(struct bt_field
*field
)
910 struct bt_field_structure
*struct_field
= (void *) field
;
913 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
914 bt_field_finalize(field
);
916 if (struct_field
->fields
) {
917 g_ptr_array_free(struct_field
->fields
, TRUE
);
918 struct_field
->fields
= NULL
;
925 void destroy_variant_field(struct bt_field
*field
)
927 struct bt_field_variant
*var_field
= (void *) field
;
930 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
931 bt_field_finalize(field
);
933 if (var_field
->fields
) {
934 g_ptr_array_free(var_field
->fields
, TRUE
);
935 var_field
->fields
= NULL
;
942 void destroy_array_field(struct bt_field
*field
)
944 struct bt_field_array
*array_field
= (void *) field
;
947 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
948 bt_field_finalize(field
);
950 if (array_field
->fields
) {
951 g_ptr_array_free(array_field
->fields
, TRUE
);
952 array_field
->fields
= NULL
;
959 void destroy_string_field(struct bt_field
*field
)
961 struct bt_field_string
*string_field
= (void *) field
;
964 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
965 bt_field_finalize(field
);
967 if (string_field
->buf
) {
968 g_array_free(string_field
->buf
, TRUE
);
969 string_field
->buf
= NULL
;
976 void bt_field_destroy(struct bt_field
*field
)
979 BT_ASSERT(bt_field_class_has_known_type(field
->class));
980 field_destroy_funcs
[field
->class->type
](field
);
984 void reset_single_field(struct bt_field
*field
)
987 field
->is_set
= false;
991 void reset_structure_field(struct bt_field
*field
)
994 struct bt_field_structure
*struct_field
= (void *) field
;
998 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
999 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1004 void reset_variant_field(struct bt_field
*field
)
1007 struct bt_field_variant
*var_field
= (void *) field
;
1011 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1012 bt_field_reset(var_field
->fields
->pdata
[i
]);
1017 void reset_array_field(struct bt_field
*field
)
1020 struct bt_field_array
*array_field
= (void *) field
;
1024 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1025 bt_field_reset(array_field
->fields
->pdata
[i
]);
1030 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1032 field
->frozen
= is_frozen
;
1036 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1039 struct bt_field_structure
*struct_field
= (void *) field
;
1041 BT_LIB_LOGD("Setting structure field's frozen state: "
1042 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1044 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1045 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1047 BT_LIB_LOGD("Setting structure field's member field's "
1048 "frozen state: %![field-]+f, index=%" PRIu64
,
1050 bt_field_set_is_frozen(member_field
, is_frozen
);
1053 set_single_field_is_frozen(field
, is_frozen
);
1057 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1060 struct bt_field_variant
*var_field
= (void *) field
;
1062 BT_LIB_LOGD("Setting variant field's frozen state: "
1063 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1065 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1066 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1068 BT_LIB_LOGD("Setting variant field's option field's "
1069 "frozen state: %![field-]+f, index=%" PRIu64
,
1071 bt_field_set_is_frozen(option_field
, is_frozen
);
1074 set_single_field_is_frozen(field
, is_frozen
);
1078 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1081 struct bt_field_array
*array_field
= (void *) field
;
1083 BT_LIB_LOGD("Setting array field's frozen state: "
1084 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1086 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1087 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1089 BT_LIB_LOGD("Setting array field's element field's "
1090 "frozen state: %![field-]+f, index=%" PRIu64
,
1092 bt_field_set_is_frozen(elem_field
, is_frozen
);
1095 set_single_field_is_frozen(field
, is_frozen
);
1099 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1103 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1105 BT_ASSERT(field
->methods
->set_is_frozen
);
1106 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1110 bool single_field_is_set(const struct bt_field
*field
)
1113 return field
->is_set
;
1117 bool structure_field_is_set(const struct bt_field
*field
)
1121 const struct bt_field_structure
*struct_field
= (const void *) field
;
1125 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1126 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1137 bool variant_field_is_set(const struct bt_field
*field
)
1139 const struct bt_field_variant
*var_field
= (const void *) field
;
1140 bool is_set
= false;
1144 if (var_field
->selected_field
) {
1145 is_set
= bt_field_is_set(var_field
->selected_field
);
1152 bool array_field_is_set(const struct bt_field
*field
)
1156 const struct bt_field_array
*array_field
= (const void *) field
;
1160 for (i
= 0; i
< array_field
->length
; i
++) {
1161 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);