2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "FIELDS"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/private-fields.h>
30 #include <babeltrace/trace-ir/fields.h>
31 #include <babeltrace/trace-ir/fields-internal.h>
32 #include <babeltrace/trace-ir/field-classes-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/object.h>
35 #include <babeltrace/compiler-internal.h>
36 #include <babeltrace/compat/fcntl-internal.h>
37 #include <babeltrace/align-internal.h>
38 #include <babeltrace/assert-internal.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(struct bt_field
*field
);
69 bool array_field_is_set(struct bt_field
*field
);
72 bool structure_field_is_set(struct bt_field
*field
);
75 bool variant_field_is_set(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(struct bt_field
*field
)
188 BT_ASSERT_PRE_NON_NULL(field
, "Field");
192 struct bt_private_field_class
*bt_private_field_borrow_class(
193 struct bt_private_field
*field
)
195 return (void *) bt_field_borrow_class((void *) field
);
198 enum bt_field_class_type
bt_field_get_class_type(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 classe: "
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
;
230 field
->class = bt_object_get_ref(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(struct bt_field
*field
)
487 struct bt_field_integer
*int_field
= (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_private_field_signed_integer_set_value(
496 struct bt_private_field
*priv_field
, int64_t value
)
498 struct bt_field
*field
= (void *) priv_field
;
499 struct bt_field_integer
*int_field
= (void *) field
;
501 BT_ASSERT_PRE_NON_NULL(field
, "Field");
502 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
503 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
504 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(
505 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
506 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
507 "%![fc-]+F", value
, field
, field
->class);
508 int_field
->value
.i
= value
;
509 bt_field_set_single(field
, true);
512 uint64_t bt_field_unsigned_integer_get_value(struct bt_field
*field
)
514 struct bt_field_integer
*int_field
= (void *) field
;
516 BT_ASSERT_PRE_NON_NULL(field
, "Field");
517 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
518 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
519 return int_field
->value
.u
;
522 void bt_private_field_unsigned_integer_set_value(
523 struct bt_private_field
*priv_field
, uint64_t value
)
525 struct bt_field
*field
= (void *) priv_field
;
526 struct bt_field_integer
*int_field
= (void *) field
;
528 BT_ASSERT_PRE_NON_NULL(field
, "Field");
529 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
530 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
531 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
532 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
533 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
534 "%![fc-]+F", value
, field
, field
->class);
535 int_field
->value
.u
= value
;
536 bt_field_set_single(field
, true);
539 double bt_field_real_get_value(struct bt_field
*field
)
541 struct bt_field_real
*real_field
= (void *) field
;
543 BT_ASSERT_PRE_NON_NULL(field
, "Field");
544 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
545 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
546 return real_field
->value
;
549 void bt_private_field_real_set_value(struct bt_private_field
*priv_field
,
552 struct bt_field
*field
= (void *) priv_field
;
553 struct bt_field_real
*real_field
= (void *) field
;
555 BT_ASSERT_PRE_NON_NULL(field
, "Field");
556 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
557 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
559 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
560 (double) (float) value
== value
,
561 "Invalid value for a single-precision real number: value=%f, "
562 "%![fc-]+F", value
, field
->class);
563 real_field
->value
= value
;
564 bt_field_set_single(field
, true);
567 int bt_field_unsigned_enumeration_get_mapping_labels(struct bt_field
*field
,
568 bt_field_class_enumeration_mapping_label_array
*label_array
,
571 struct bt_field_integer
*int_field
= (void *) field
;
573 BT_ASSERT_PRE_NON_NULL(field
, "Field");
574 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
575 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
576 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
577 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
578 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
579 return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
580 field
->class, int_field
->value
.u
, label_array
, count
);
583 int bt_field_signed_enumeration_get_mapping_labels(struct bt_field
*field
,
584 bt_field_class_enumeration_mapping_label_array
*label_array
,
587 struct bt_field_integer
*int_field
= (void *) field
;
589 BT_ASSERT_PRE_NON_NULL(field
, "Field");
590 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
591 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
592 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
593 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
594 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
595 return bt_field_class_signed_enumeration_get_mapping_labels_by_value(
596 field
->class, int_field
->value
.i
, label_array
, count
);
599 const char *bt_field_string_get_value(struct bt_field
*field
)
601 struct bt_field_string
*string_field
= (void *) field
;
603 BT_ASSERT_PRE_NON_NULL(field
, "Field");
604 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
605 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
607 return (const char *) string_field
->buf
->data
;
610 uint64_t bt_field_string_get_length(struct bt_field
*field
)
612 struct bt_field_string
*string_field
= (void *) field
;
614 BT_ASSERT_PRE_NON_NULL(field
, "Field");
615 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
616 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
618 return string_field
->length
;
621 int bt_private_field_string_set_value(struct bt_private_field
*priv_field
,
624 struct bt_field
*field
= (void *) priv_field
;
626 BT_ASSERT_PRE_NON_NULL(field
, "Field");
627 BT_ASSERT_PRE_NON_NULL(value
, "Value");
628 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
629 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
631 bt_private_field_string_clear(priv_field
);
632 return bt_private_field_string_append_with_length(priv_field
, value
,
633 (uint64_t) strlen(value
));
636 int bt_private_field_string_append(struct bt_private_field
*field
,
639 return bt_private_field_string_append_with_length(field
,
640 value
, (uint64_t) strlen(value
));
643 int bt_private_field_string_append_with_length(
644 struct bt_private_field
*priv_field
,
645 const char *value
, uint64_t length
)
647 struct bt_field
*field
= (void *) priv_field
;
648 struct bt_field_string
*string_field
= (void *) field
;
652 BT_ASSERT_PRE_NON_NULL(field
, "Field");
653 BT_ASSERT_PRE_NON_NULL(value
, "Value");
654 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
655 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
656 BT_FIELD_CLASS_TYPE_STRING
, "Field");
658 /* Make sure no null bytes are appended */
659 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
660 "String value to append contains a null character: "
661 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
663 new_length
= length
+ string_field
->length
;
665 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
666 g_array_set_size(string_field
->buf
, new_length
+ 1);
669 data
= string_field
->buf
->data
;
670 memcpy(data
+ string_field
->length
, value
, length
);
671 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
672 string_field
->length
= new_length
;
673 bt_field_set_single(field
, true);
677 int bt_private_field_string_clear(struct bt_private_field
*priv_field
)
679 struct bt_field
*field
= (void *) priv_field
;
680 struct bt_field_string
*string_field
= (void *) 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 string_field
->length
= 0;
687 bt_field_set_single(field
, true);
691 uint64_t bt_field_array_get_length(struct bt_field
*field
)
693 struct bt_field_array
*array_field
= (void *) field
;
695 BT_ASSERT_PRE_NON_NULL(field
, "Field");
696 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
697 return array_field
->length
;
700 int bt_private_field_dynamic_array_set_length(
701 struct bt_private_field
*priv_field
, uint64_t length
)
704 struct bt_field
*field
= (void *) priv_field
;
705 struct bt_field_array
*array_field
= (void *) field
;
707 BT_ASSERT_PRE_NON_NULL(field
, "Field");
708 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
709 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
710 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
712 if (unlikely(length
> array_field
->fields
->len
)) {
714 struct bt_field_class_array
*array_fc
;
715 uint64_t cur_len
= array_field
->fields
->len
;
718 g_ptr_array_set_size(array_field
->fields
, length
);
719 array_fc
= (void *) field
->class;
721 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
722 struct bt_field
*elem_field
= bt_field_create(
723 array_fc
->element_fc
);
726 BT_LIB_LOGE("Cannot create element field for "
727 "dynamic array field: "
728 "index=%" PRIu64
", "
729 "%![array-field-]+f", i
, field
);
734 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
735 array_field
->fields
->pdata
[i
] = elem_field
;
739 array_field
->length
= length
;
745 struct bt_field
*bt_field_array_borrow_element_field_by_index(
746 struct bt_field
*field
, uint64_t index
)
748 struct bt_field_array
*array_field
= (void *) field
;
750 BT_ASSERT_PRE_NON_NULL(field
, "Field");
751 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
752 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
753 return array_field
->fields
->pdata
[index
];
756 struct bt_private_field
*
757 bt_private_field_array_borrow_element_field_by_index(
758 struct bt_private_field
*field
, uint64_t index
)
760 return (void *) bt_field_array_borrow_element_field_by_index(
761 (void *) field
, index
);
764 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
765 struct bt_field
*field
, uint64_t index
)
767 struct bt_field_structure
*struct_field
= (void *) field
;
769 BT_ASSERT_PRE_NON_NULL(field
, "Field");
770 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
771 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
772 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
773 return struct_field
->fields
->pdata
[index
];
776 struct bt_private_field
*
777 bt_private_field_structure_borrow_member_field_by_index(
778 struct bt_private_field
*field
, uint64_t index
)
780 return (void *) bt_field_structure_borrow_member_field_by_index(
781 (void *) field
, index
);
784 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
785 struct bt_field
*field
, const char *name
)
787 struct bt_field
*ret_field
= NULL
;
788 struct bt_field_class_structure
*struct_fc
;
789 struct bt_field_structure
*struct_field
= (void *) field
;
793 BT_ASSERT_PRE_NON_NULL(field
, "Field");
794 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
795 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
796 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
797 struct_fc
= (void *) field
->class;
799 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
800 &orig_key
, &index
)) {
804 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
805 BT_ASSERT(ret_field
);
811 struct bt_private_field
*
812 bt_private_field_structure_borrow_member_field_by_name(
813 struct bt_private_field
*field
, const char *name
)
815 return (void *) bt_field_structure_borrow_member_field_by_name(
816 (void *) field
, name
);
819 struct bt_field
*bt_field_variant_borrow_selected_option_field(
820 struct bt_field
*field
)
822 struct bt_field_variant
*var_field
= (void *) field
;
824 BT_ASSERT_PRE_NON_NULL(field
, "Field");
825 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
826 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
827 BT_ASSERT_PRE(var_field
->selected_field
,
828 "Variant field has no selected field: %!+f", field
);
829 return var_field
->selected_field
;
832 struct bt_private_field
*
833 bt_private_field_variant_borrow_selected_option_field(
834 struct bt_private_field
*field
)
836 return (void *) bt_field_variant_borrow_selected_option_field(
840 int bt_private_field_variant_select_option_field(
841 struct bt_private_field
*priv_field
, uint64_t index
)
843 struct bt_field
*field
= (void *) priv_field
;
844 struct bt_field_variant
*var_field
= (void *) field
;
846 BT_ASSERT_PRE_NON_NULL(field
, "Field");
847 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
848 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
849 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
850 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
851 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
852 var_field
->selected_index
= index
;
856 uint64_t bt_field_variant_get_selected_option_field_index(
857 struct bt_field
*field
)
859 struct bt_field_variant
*var_field
= (void *) field
;
861 BT_ASSERT_PRE_NON_NULL(field
, "Field");
862 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
863 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
864 BT_ASSERT_PRE(var_field
->selected_field
,
865 "Variant field has no selected field: %!+f", field
);
866 return var_field
->selected_index
;
870 void bt_field_finalize(struct bt_field
*field
)
873 BT_LOGD_STR("Putting field's class.");
874 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
878 void destroy_integer_field(struct bt_field
*field
)
881 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
882 bt_field_finalize(field
);
887 void destroy_real_field(struct bt_field
*field
)
890 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
891 bt_field_finalize(field
);
896 void destroy_structure_field(struct bt_field
*field
)
898 struct bt_field_structure
*struct_field
= (void *) field
;
901 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
902 bt_field_finalize(field
);
904 if (struct_field
->fields
) {
905 g_ptr_array_free(struct_field
->fields
, TRUE
);
906 struct_field
->fields
= NULL
;
913 void destroy_variant_field(struct bt_field
*field
)
915 struct bt_field_variant
*var_field
= (void *) field
;
918 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
919 bt_field_finalize(field
);
921 if (var_field
->fields
) {
922 g_ptr_array_free(var_field
->fields
, TRUE
);
923 var_field
->fields
= NULL
;
930 void destroy_array_field(struct bt_field
*field
)
932 struct bt_field_array
*array_field
= (void *) field
;
935 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
936 bt_field_finalize(field
);
938 if (array_field
->fields
) {
939 g_ptr_array_free(array_field
->fields
, TRUE
);
940 array_field
->fields
= NULL
;
947 void destroy_string_field(struct bt_field
*field
)
949 struct bt_field_string
*string_field
= (void *) field
;
952 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
953 bt_field_finalize(field
);
955 if (string_field
->buf
) {
956 g_array_free(string_field
->buf
, TRUE
);
957 string_field
->buf
= NULL
;
964 void bt_field_destroy(struct bt_field
*field
)
967 BT_ASSERT(bt_field_class_has_known_type(field
->class));
968 field_destroy_funcs
[field
->class->type
](field
);
972 void reset_single_field(struct bt_field
*field
)
975 field
->is_set
= false;
979 void reset_structure_field(struct bt_field
*field
)
982 struct bt_field_structure
*struct_field
= (void *) field
;
986 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
987 bt_field_reset(struct_field
->fields
->pdata
[i
]);
992 void reset_variant_field(struct bt_field
*field
)
995 struct bt_field_variant
*var_field
= (void *) field
;
999 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1000 bt_field_reset(var_field
->fields
->pdata
[i
]);
1005 void reset_array_field(struct bt_field
*field
)
1008 struct bt_field_array
*array_field
= (void *) field
;
1012 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1013 bt_field_reset(array_field
->fields
->pdata
[i
]);
1018 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1020 field
->frozen
= is_frozen
;
1024 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1027 struct bt_field_structure
*struct_field
= (void *) field
;
1029 BT_LIB_LOGD("Setting structure field's frozen state: "
1030 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1032 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1033 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1035 BT_LIB_LOGD("Setting structure field's member field's "
1036 "frozen state: %![field-]+f, index=%" PRIu64
,
1038 bt_field_set_is_frozen(member_field
, is_frozen
);
1041 set_single_field_is_frozen(field
, is_frozen
);
1045 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1048 struct bt_field_variant
*var_field
= (void *) field
;
1050 BT_LIB_LOGD("Setting variant field's frozen state: "
1051 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1053 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1054 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1056 BT_LIB_LOGD("Setting variant field's option field's "
1057 "frozen state: %![field-]+f, index=%" PRIu64
,
1059 bt_field_set_is_frozen(option_field
, is_frozen
);
1062 set_single_field_is_frozen(field
, is_frozen
);
1066 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1069 struct bt_field_array
*array_field
= (void *) field
;
1071 BT_LIB_LOGD("Setting array field's frozen state: "
1072 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1074 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1075 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1077 BT_LIB_LOGD("Setting array field's element field's "
1078 "frozen state: %![field-]+f, index=%" PRIu64
,
1080 bt_field_set_is_frozen(elem_field
, is_frozen
);
1083 set_single_field_is_frozen(field
, is_frozen
);
1087 void _bt_field_set_is_frozen(struct bt_field
*field
,
1091 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1093 BT_ASSERT(field
->methods
->set_is_frozen
);
1094 field
->methods
->set_is_frozen(field
, is_frozen
);
1098 bool single_field_is_set(struct bt_field
*field
)
1101 return field
->is_set
;
1105 bool structure_field_is_set(struct bt_field
*field
)
1109 struct bt_field_structure
*struct_field
= (void *) field
;
1113 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1114 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1125 bool variant_field_is_set(struct bt_field
*field
)
1127 struct bt_field_variant
*var_field
= (void *) field
;
1128 bool is_set
= false;
1132 if (var_field
->selected_field
) {
1133 is_set
= bt_field_is_set(var_field
->selected_field
);
1140 bool array_field_is_set(struct bt_field
*field
)
1144 struct bt_field_array
*array_field
= (void *) field
;
1148 for (i
= 0; i
< array_field
->length
; i
++) {
1149 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);