4 * Babeltrace trace IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "FIELDS"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/trace-ir/fields.h>
34 #include <babeltrace/trace-ir/fields-internal.h>
35 #include <babeltrace/trace-ir/field-classes-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/compat/fcntl-internal.h>
40 #include <babeltrace/align-internal.h>
41 #include <babeltrace/assert-internal.h>
45 void reset_single_field(struct bt_field
*field
);
48 void reset_array_field(struct bt_field
*field
);
51 void reset_structure_field(struct bt_field
*field
);
54 void reset_variant_field(struct bt_field
*field
);
57 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
60 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
63 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
66 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
69 bool single_field_is_set(struct bt_field
*field
);
72 bool array_field_is_set(struct bt_field
*field
);
75 bool structure_field_is_set(struct bt_field
*field
);
78 bool variant_field_is_set(struct bt_field
*field
);
81 struct bt_field_methods integer_field_methods
= {
82 .set_is_frozen
= set_single_field_is_frozen
,
83 .is_set
= single_field_is_set
,
84 .reset
= reset_single_field
,
88 struct bt_field_methods real_field_methods
= {
89 .set_is_frozen
= set_single_field_is_frozen
,
90 .is_set
= single_field_is_set
,
91 .reset
= reset_single_field
,
95 struct bt_field_methods string_field_methods
= {
96 .set_is_frozen
= set_single_field_is_frozen
,
97 .is_set
= single_field_is_set
,
98 .reset
= reset_single_field
,
102 struct bt_field_methods structure_field_methods
= {
103 .set_is_frozen
= set_structure_field_is_frozen
,
104 .is_set
= structure_field_is_set
,
105 .reset
= reset_structure_field
,
109 struct bt_field_methods array_field_methods
= {
110 .set_is_frozen
= set_array_field_is_frozen
,
111 .is_set
= array_field_is_set
,
112 .reset
= reset_array_field
,
116 struct bt_field_methods variant_field_methods
= {
117 .set_is_frozen
= set_variant_field_is_frozen
,
118 .is_set
= variant_field_is_set
,
119 .reset
= reset_variant_field
,
123 struct bt_field
*create_integer_field(struct bt_field_class
*);
126 struct bt_field
*create_real_field(struct bt_field_class
*);
129 struct bt_field
*create_string_field(struct bt_field_class
*);
132 struct bt_field
*create_structure_field(struct bt_field_class
*);
135 struct bt_field
*create_static_array_field(struct bt_field_class
*);
138 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
141 struct bt_field
*create_variant_field(struct bt_field_class
*);
144 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
145 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
146 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
147 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
148 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
149 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
150 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
151 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
152 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
153 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
154 [BT_FIELD_CLASS_TYPE_VARIANT
] = create_variant_field
,
158 void destroy_integer_field(struct bt_field
*field
);
161 void destroy_real_field(struct bt_field
*field
);
164 void destroy_string_field(struct bt_field
*field
);
167 void destroy_structure_field(struct bt_field
*field
);
170 void destroy_array_field(struct bt_field
*field
);
173 void destroy_variant_field(struct bt_field
*field
);
176 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
177 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
178 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
179 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
180 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
181 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
182 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
183 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
184 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
185 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
186 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
189 struct bt_field_class
*bt_field_borrow_class(struct bt_field
*field
)
191 BT_ASSERT_PRE_NON_NULL(field
, "Field");
195 enum bt_field_class_type
bt_field_get_class_type(struct bt_field
*field
)
197 BT_ASSERT_PRE_NON_NULL(field
, "Field");
198 return field
->class->type
;
202 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
204 struct bt_field
*field
= NULL
;
206 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
207 BT_ASSERT(bt_field_class_has_known_type(fc
));
208 field
= field_create_funcs
[fc
->type
](fc
);
210 BT_LIB_LOGE("Cannot create field object from field classe: "
220 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
221 struct bt_field_methods
*methods
)
225 bt_object_init_unique(&field
->base
);
226 field
->methods
= methods
;
227 field
->class = bt_get(fc
);
231 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
233 struct bt_field_integer
*int_field
;
235 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
236 int_field
= g_new0(struct bt_field_integer
, 1);
238 BT_LOGE_STR("Failed to allocate one integer field.");
242 init_field((void *) int_field
, fc
, &integer_field_methods
);
243 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
246 return (void *) int_field
;
250 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
252 struct bt_field_real
*real_field
;
254 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
255 real_field
= g_new0(struct bt_field_real
, 1);
257 BT_LOGE_STR("Failed to allocate one real field.");
261 init_field((void *) real_field
, fc
, &real_field_methods
);
262 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
265 return (void *) real_field
;
269 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
271 struct bt_field_string
*string_field
;
273 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
274 string_field
= g_new0(struct bt_field_string
, 1);
276 BT_LOGE_STR("Failed to allocate one string field.");
280 init_field((void *) string_field
, fc
, &string_field_methods
);
281 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
283 if (!string_field
->buf
) {
284 BT_LOGE_STR("Failed to allocate a GArray.");
285 BT_PUT(string_field
);
289 g_array_index(string_field
->buf
, char, 0) = '\0';
290 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
293 return (void *) string_field
;
297 int create_fields_from_named_field_classes(
298 struct bt_field_class_named_field_class_container
*fc
,
304 *fields
= g_ptr_array_new_with_free_func(
305 (GDestroyNotify
) bt_field_destroy
);
307 BT_LOGE_STR("Failed to allocate a GPtrArray.");
312 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
314 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
315 struct bt_field
*field
;
316 struct bt_named_field_class
*named_fc
=
317 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, i
);
319 field
= bt_field_create(named_fc
->fc
);
321 BT_LIB_LOGE("Failed to create structure member or variant option field: "
322 "name=\"%s\", %![fc-]+F",
323 named_fc
->name
->str
, named_fc
->fc
);
328 g_ptr_array_index(*fields
, i
) = field
;
336 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
338 struct bt_field_structure
*struct_field
;
340 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
341 struct_field
= g_new0(struct bt_field_structure
, 1);
343 BT_LOGE_STR("Failed to allocate one structure field.");
347 init_field((void *) struct_field
, fc
, &structure_field_methods
);
349 if (create_fields_from_named_field_classes((void *) fc
,
350 &struct_field
->fields
)) {
351 BT_LIB_LOGE("Cannot create structure member fields: "
353 BT_PUT(struct_field
);
357 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
360 return (void *) struct_field
;
364 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
366 struct bt_field_variant
*var_field
;
368 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
369 var_field
= g_new0(struct bt_field_variant
, 1);
371 BT_LOGE_STR("Failed to allocate one variant field.");
375 init_field((void *) var_field
, fc
, &variant_field_methods
);
377 if (create_fields_from_named_field_classes((void *) fc
,
378 &var_field
->fields
)) {
379 BT_LIB_LOGE("Cannot create variant member fields: "
385 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
388 return (void *) var_field
;
392 int init_array_field_fields(struct bt_field_array
*array_field
)
396 struct bt_field_class_array
*array_fc
;
398 BT_ASSERT(array_field
);
399 array_fc
= (void *) array_field
->common
.class;
400 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
401 if (!array_field
->fields
) {
402 BT_LOGE_STR("Failed to allocate a GPtrArray.");
407 g_ptr_array_set_free_func(array_field
->fields
,
408 (GDestroyNotify
) bt_field_destroy
);
409 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
411 for (i
= 0; i
< array_field
->length
; i
++) {
412 array_field
->fields
->pdata
[i
] = bt_field_create(
413 array_fc
->element_fc
);
414 if (!array_field
->fields
->pdata
[i
]) {
415 BT_LIB_LOGE("Cannot create array field's element field: "
416 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
427 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
429 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
430 struct bt_field_array
*array_field
;
432 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
433 array_field
= g_new0(struct bt_field_array
, 1);
435 BT_LOGE_STR("Failed to allocate one static array field.");
439 init_field((void *) array_field
, fc
, &array_field_methods
);
440 array_field
->length
= array_fc
->length
;
442 if (init_array_field_fields(array_field
)) {
443 BT_LIB_LOGE("Cannot create static array fields: "
449 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
452 return (void *) array_field
;
456 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
458 struct bt_field_array
*array_field
;
460 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
461 array_field
= g_new0(struct bt_field_array
, 1);
463 BT_LOGE_STR("Failed to allocate one dynamic array field.");
467 init_field((void *) array_field
, fc
, &array_field_methods
);
469 if (init_array_field_fields(array_field
)) {
470 BT_LIB_LOGE("Cannot create dynamic array fields: "
476 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
479 return (void *) array_field
;
482 int64_t bt_field_signed_integer_get_value(struct bt_field
*field
)
484 struct bt_field_integer
*int_field
= (void *) field
;
486 BT_ASSERT_PRE_NON_NULL(field
, "Field");
487 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
488 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
489 return int_field
->value
.i
;
492 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
494 struct bt_field_integer
*int_field
= (void *) field
;
496 BT_ASSERT_PRE_NON_NULL(field
, "Field");
497 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
498 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
499 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(
500 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
501 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
502 "%![fc-]+F", value
, field
, field
->class);
503 int_field
->value
.i
= value
;
504 bt_field_set_single(field
, true);
507 uint64_t bt_field_unsigned_integer_get_value(struct bt_field
*field
)
509 struct bt_field_integer
*int_field
= (void *) field
;
511 BT_ASSERT_PRE_NON_NULL(field
, "Field");
512 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
513 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
514 return int_field
->value
.u
;
517 void bt_field_unsigned_integer_set_value(struct bt_field
*field
,
520 struct bt_field_integer
*int_field
= (void *) field
;
522 BT_ASSERT_PRE_NON_NULL(field
, "Field");
523 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
524 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
525 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
526 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
527 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
528 "%![fc-]+F", value
, field
, field
->class);
529 int_field
->value
.u
= value
;
530 bt_field_set_single(field
, true);
533 double bt_field_real_get_value(struct bt_field
*field
)
535 struct bt_field_real
*real_field
= (void *) field
;
537 BT_ASSERT_PRE_NON_NULL(field
, "Field");
538 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
539 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
540 return real_field
->value
;
543 void bt_field_real_set_value(struct bt_field
*field
, double value
)
545 struct bt_field_real
*real_field
= (void *) field
;
547 BT_ASSERT_PRE_NON_NULL(field
, "Field");
548 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
549 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
551 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
552 (double) (float) value
== value
,
553 "Invalid value for a single-precision real number: value=%f, "
554 "%![fc-]+F", value
, field
->class);
555 real_field
->value
= value
;
556 bt_field_set_single(field
, true);
559 int bt_field_unsigned_enumeration_get_mapping_labels(struct bt_field
*field
,
560 bt_field_class_enumeration_mapping_label_array
*label_array
,
563 struct bt_field_integer
*int_field
= (void *) field
;
565 BT_ASSERT_PRE_NON_NULL(field
, "Field");
566 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
567 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
568 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
569 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
570 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
571 return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
572 field
->class, int_field
->value
.u
, label_array
, count
);
575 int bt_field_signed_enumeration_get_mapping_labels(struct bt_field
*field
,
576 bt_field_class_enumeration_mapping_label_array
*label_array
,
579 struct bt_field_integer
*int_field
= (void *) field
;
581 BT_ASSERT_PRE_NON_NULL(field
, "Field");
582 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
583 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
584 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
585 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
586 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
587 return bt_field_class_signed_enumeration_get_mapping_labels_by_value(
588 field
->class, int_field
->value
.i
, label_array
, count
);
591 const char *bt_field_string_get_value(struct bt_field
*field
)
593 struct bt_field_string
*string_field
= (void *) field
;
595 BT_ASSERT_PRE_NON_NULL(field
, "Field");
596 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
597 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
599 return (const char *) string_field
->buf
->data
;
602 uint64_t bt_field_string_get_length(struct bt_field
*field
)
604 struct bt_field_string
*string_field
= (void *) field
;
606 BT_ASSERT_PRE_NON_NULL(field
, "Field");
607 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
608 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
610 return string_field
->length
;
613 int bt_field_string_set_value(struct bt_field
*field
, const char *value
)
615 BT_ASSERT_PRE_NON_NULL(field
, "Field");
616 BT_ASSERT_PRE_NON_NULL(value
, "Value");
617 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
618 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
620 bt_field_string_clear(field
);
621 return bt_field_string_append_with_length(field
, value
,
622 (uint64_t) strlen(value
));
625 int bt_field_string_append(struct bt_field
*field
, const char *value
)
627 return bt_field_string_append_with_length(field
, value
,
628 (uint64_t) strlen(value
));
631 int bt_field_string_append_with_length(struct bt_field
*field
,
632 const char *value
, uint64_t length
)
634 struct bt_field_string
*string_field
= (void *) field
;
638 BT_ASSERT_PRE_NON_NULL(field
, "Field");
639 BT_ASSERT_PRE_NON_NULL(value
, "Value");
640 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
641 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
642 BT_FIELD_CLASS_TYPE_STRING
, "Field");
644 /* Make sure no null bytes are appended */
645 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
646 "String value to append contains a null character: "
647 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
649 new_length
= length
+ string_field
->length
;
651 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
652 g_array_set_size(string_field
->buf
, new_length
+ 1);
655 data
= string_field
->buf
->data
;
656 memcpy(data
+ string_field
->length
, value
, length
);
657 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
658 string_field
->length
= new_length
;
659 bt_field_set_single(field
, true);
663 int bt_field_string_clear(struct bt_field
*field
)
665 struct bt_field_string
*string_field
= (void *) field
;
667 BT_ASSERT_PRE_NON_NULL(field
, "Field");
668 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
669 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
670 BT_FIELD_CLASS_TYPE_STRING
, "Field");
671 string_field
->length
= 0;
672 bt_field_set_single(field
, true);
676 uint64_t bt_field_array_get_length(struct bt_field
*field
)
678 struct bt_field_array
*array_field
= (void *) field
;
680 BT_ASSERT_PRE_NON_NULL(field
, "Field");
681 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
682 return array_field
->length
;
685 int bt_field_dynamic_array_set_length(struct bt_field
*field
,
689 struct bt_field_array
*array_field
= (void *) field
;
691 BT_ASSERT_PRE_NON_NULL(field
, "Field");
692 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
693 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
694 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
696 if (unlikely(length
> array_field
->fields
->len
)) {
698 struct bt_field_class_array
*array_fc
;
699 uint64_t cur_len
= array_field
->fields
->len
;
702 g_ptr_array_set_size(array_field
->fields
, length
);
703 array_fc
= (void *) field
->class;
705 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
706 struct bt_field
*elem_field
= bt_field_create(
707 array_fc
->element_fc
);
710 BT_LIB_LOGE("Cannot create element field for "
711 "dynamic array field: "
712 "index=%" PRIu64
", "
713 "%![array-field-]+f", i
, field
);
718 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
719 array_field
->fields
->pdata
[i
] = elem_field
;
723 array_field
->length
= length
;
729 struct bt_field
*bt_field_array_borrow_element_field_by_index(
730 struct bt_field
*field
, uint64_t index
)
732 struct bt_field_array
*array_field
= (void *) field
;
734 BT_ASSERT_PRE_NON_NULL(field
, "Field");
735 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
736 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
737 return array_field
->fields
->pdata
[index
];
740 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
741 struct bt_field
*field
, uint64_t index
)
743 struct bt_field_structure
*struct_field
= (void *) field
;
745 BT_ASSERT_PRE_NON_NULL(field
, "Field");
746 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
747 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
748 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
749 return struct_field
->fields
->pdata
[index
];
752 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
753 struct bt_field
*field
, const char *name
)
755 struct bt_field
*ret_field
= NULL
;
756 struct bt_field_class_structure
*struct_fc
;
757 struct bt_field_structure
*struct_field
= (void *) field
;
761 BT_ASSERT_PRE_NON_NULL(field
, "Field");
762 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
763 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
764 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
765 struct_fc
= (void *) field
->class;
767 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
768 &orig_key
, &index
)) {
772 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
773 BT_ASSERT(ret_field
);
779 struct bt_field
*bt_field_variant_borrow_selected_option_field(
780 struct bt_field
*field
)
782 struct bt_field_variant
*var_field
= (void *) field
;
784 BT_ASSERT_PRE_NON_NULL(field
, "Field");
785 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
786 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
787 BT_ASSERT_PRE(var_field
->selected_field
,
788 "Variant field has no selected field: %!+f", field
);
789 return var_field
->selected_field
;
792 int bt_field_variant_select_option_field(struct bt_field
*field
,
795 struct bt_field_variant
*var_field
= (void *) field
;
797 BT_ASSERT_PRE_NON_NULL(field
, "Field");
798 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
799 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
800 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
801 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
802 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
803 var_field
->selected_index
= index
;
807 uint64_t bt_field_variant_get_selected_option_field_index(
808 struct bt_field
*field
)
810 struct bt_field_variant
*var_field
= (void *) field
;
812 BT_ASSERT_PRE_NON_NULL(field
, "Field");
813 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
814 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
815 BT_ASSERT_PRE(var_field
->selected_field
,
816 "Variant field has no selected field: %!+f", field
);
817 return var_field
->selected_index
;
821 void bt_field_finalize(struct bt_field
*field
)
824 BT_LOGD_STR("Putting field's class.");
825 bt_put(field
->class);
829 void destroy_integer_field(struct bt_field
*field
)
832 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
833 bt_field_finalize(field
);
838 void destroy_real_field(struct bt_field
*field
)
841 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
842 bt_field_finalize(field
);
847 void destroy_structure_field(struct bt_field
*field
)
849 struct bt_field_structure
*struct_field
= (void *) field
;
852 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
853 bt_field_finalize(field
);
855 if (struct_field
->fields
) {
856 g_ptr_array_free(struct_field
->fields
, TRUE
);
863 void destroy_variant_field(struct bt_field
*field
)
865 struct bt_field_variant
*var_field
= (void *) field
;
868 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
869 bt_field_finalize(field
);
871 if (var_field
->fields
) {
872 g_ptr_array_free(var_field
->fields
, TRUE
);
879 void destroy_array_field(struct bt_field
*field
)
881 struct bt_field_array
*array_field
= (void *) field
;
884 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
885 bt_field_finalize(field
);
887 if (array_field
->fields
) {
888 g_ptr_array_free(array_field
->fields
, TRUE
);
895 void destroy_string_field(struct bt_field
*field
)
897 struct bt_field_string
*string_field
= (void *) field
;
900 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
901 bt_field_finalize(field
);
903 if (string_field
->buf
) {
904 g_array_free(string_field
->buf
, TRUE
);
911 void bt_field_destroy(struct bt_field
*field
)
914 BT_ASSERT(bt_field_class_has_known_type(field
->class));
915 field_destroy_funcs
[field
->class->type
](field
);
919 void reset_single_field(struct bt_field
*field
)
922 field
->is_set
= false;
926 void reset_structure_field(struct bt_field
*field
)
929 struct bt_field_structure
*struct_field
= (void *) field
;
933 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
934 bt_field_reset(struct_field
->fields
->pdata
[i
]);
939 void reset_variant_field(struct bt_field
*field
)
942 struct bt_field_variant
*var_field
= (void *) field
;
946 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
947 bt_field_reset(var_field
->fields
->pdata
[i
]);
952 void reset_array_field(struct bt_field
*field
)
955 struct bt_field_array
*array_field
= (void *) field
;
959 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
960 bt_field_reset(array_field
->fields
->pdata
[i
]);
965 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
967 field
->frozen
= is_frozen
;
971 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
974 struct bt_field_structure
*struct_field
= (void *) field
;
976 BT_LIB_LOGD("Setting structure field's frozen state: "
977 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
979 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
980 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
982 BT_LIB_LOGD("Setting structure field's member field's "
983 "frozen state: %![field-]+f, index=%" PRIu64
,
985 bt_field_set_is_frozen(member_field
, is_frozen
);
988 set_single_field_is_frozen(field
, is_frozen
);
992 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
995 struct bt_field_variant
*var_field
= (void *) field
;
997 BT_LIB_LOGD("Setting variant field's frozen state: "
998 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1000 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1001 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1003 BT_LIB_LOGD("Setting variant field's option field's "
1004 "frozen state: %![field-]+f, index=%" PRIu64
,
1006 bt_field_set_is_frozen(option_field
, is_frozen
);
1009 set_single_field_is_frozen(field
, is_frozen
);
1013 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1016 struct bt_field_array
*array_field
= (void *) field
;
1018 BT_LIB_LOGD("Setting array field's frozen state: "
1019 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1021 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1022 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1024 BT_LIB_LOGD("Setting array field's element field's "
1025 "frozen state: %![field-]+f, index=%" PRIu64
,
1027 bt_field_set_is_frozen(elem_field
, is_frozen
);
1030 set_single_field_is_frozen(field
, is_frozen
);
1034 void _bt_field_set_is_frozen(struct bt_field
*field
,
1038 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1040 BT_ASSERT(field
->methods
->set_is_frozen
);
1041 field
->methods
->set_is_frozen(field
, is_frozen
);
1045 bool single_field_is_set(struct bt_field
*field
)
1048 return field
->is_set
;
1052 bool structure_field_is_set(struct bt_field
*field
)
1056 struct bt_field_structure
*struct_field
= (void *) field
;
1060 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1061 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1072 bool variant_field_is_set(struct bt_field
*field
)
1074 struct bt_field_variant
*var_field
= (void *) field
;
1075 bool is_set
= false;
1079 if (var_field
->selected_field
) {
1080 is_set
= bt_field_is_set(var_field
->selected_field
);
1087 bool array_field_is_set(struct bt_field
*field
)
1091 struct bt_field_array
*array_field
= (void *) field
;
1095 for (i
= 0; i
< array_field
->length
; i
++) {
1096 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);