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/fields.h>
30 #include <babeltrace/trace-ir/fields-const.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(const struct bt_field
*field
);
69 bool array_field_is_set(const struct bt_field
*field
);
72 bool structure_field_is_set(const struct bt_field
*field
);
75 bool variant_field_is_set(const struct bt_field
*field
);
78 struct bt_field_methods integer_field_methods
= {
79 .set_is_frozen
= set_single_field_is_frozen
,
80 .is_set
= single_field_is_set
,
81 .reset
= reset_single_field
,
85 struct bt_field_methods real_field_methods
= {
86 .set_is_frozen
= set_single_field_is_frozen
,
87 .is_set
= single_field_is_set
,
88 .reset
= reset_single_field
,
92 struct bt_field_methods string_field_methods
= {
93 .set_is_frozen
= set_single_field_is_frozen
,
94 .is_set
= single_field_is_set
,
95 .reset
= reset_single_field
,
99 struct bt_field_methods structure_field_methods
= {
100 .set_is_frozen
= set_structure_field_is_frozen
,
101 .is_set
= structure_field_is_set
,
102 .reset
= reset_structure_field
,
106 struct bt_field_methods array_field_methods
= {
107 .set_is_frozen
= set_array_field_is_frozen
,
108 .is_set
= array_field_is_set
,
109 .reset
= reset_array_field
,
113 struct bt_field_methods variant_field_methods
= {
114 .set_is_frozen
= set_variant_field_is_frozen
,
115 .is_set
= variant_field_is_set
,
116 .reset
= reset_variant_field
,
120 struct bt_field
*create_integer_field(struct bt_field_class
*);
123 struct bt_field
*create_real_field(struct bt_field_class
*);
126 struct bt_field
*create_string_field(struct bt_field_class
*);
129 struct bt_field
*create_structure_field(struct bt_field_class
*);
132 struct bt_field
*create_static_array_field(struct bt_field_class
*);
135 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
138 struct bt_field
*create_variant_field(struct bt_field_class
*);
141 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
142 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
143 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
144 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
145 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
146 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
147 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
148 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
149 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
150 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
151 [BT_FIELD_CLASS_TYPE_VARIANT
] = create_variant_field
,
155 void destroy_integer_field(struct bt_field
*field
);
158 void destroy_real_field(struct bt_field
*field
);
161 void destroy_string_field(struct bt_field
*field
);
164 void destroy_structure_field(struct bt_field
*field
);
167 void destroy_array_field(struct bt_field
*field
);
170 void destroy_variant_field(struct bt_field
*field
);
173 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
174 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
175 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
176 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
177 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
178 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
179 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
180 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
181 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
182 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
183 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
186 struct bt_field_class
*bt_field_borrow_class(const struct bt_field
*field
)
188 BT_ASSERT_PRE_NON_NULL(field
, "Field");
192 const struct bt_field_class
*bt_field_borrow_class_const(
193 const struct bt_field
*field
)
195 BT_ASSERT_PRE_NON_NULL(field
, "Field");
199 enum bt_field_class_type
bt_field_get_class_type(const struct bt_field
*field
)
201 BT_ASSERT_PRE_NON_NULL(field
, "Field");
202 return field
->class->type
;
206 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
208 struct bt_field
*field
= NULL
;
210 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
211 BT_ASSERT(bt_field_class_has_known_type(fc
));
212 field
= field_create_funcs
[fc
->type
](fc
);
214 BT_LIB_LOGE("Cannot create field object from field class: "
224 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
225 struct bt_field_methods
*methods
)
229 bt_object_init_unique(&field
->base
);
230 field
->methods
= methods
;
232 bt_object_get_no_null_check(fc
);
236 struct bt_field
*create_integer_field(struct bt_field_class
*fc
)
238 struct bt_field_integer
*int_field
;
240 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc
);
241 int_field
= g_new0(struct bt_field_integer
, 1);
243 BT_LOGE_STR("Failed to allocate one integer field.");
247 init_field((void *) int_field
, fc
, &integer_field_methods
);
248 BT_LIB_LOGD("Created integer field object: %!+f", int_field
);
251 return (void *) int_field
;
255 struct bt_field
*create_real_field(struct bt_field_class
*fc
)
257 struct bt_field_real
*real_field
;
259 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc
);
260 real_field
= g_new0(struct bt_field_real
, 1);
262 BT_LOGE_STR("Failed to allocate one real field.");
266 init_field((void *) real_field
, fc
, &real_field_methods
);
267 BT_LIB_LOGD("Created real field object: %!+f", real_field
);
270 return (void *) real_field
;
274 struct bt_field
*create_string_field(struct bt_field_class
*fc
)
276 struct bt_field_string
*string_field
;
278 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc
);
279 string_field
= g_new0(struct bt_field_string
, 1);
281 BT_LOGE_STR("Failed to allocate one string field.");
285 init_field((void *) string_field
, fc
, &string_field_methods
);
286 string_field
->buf
= g_array_sized_new(FALSE
, FALSE
,
288 if (!string_field
->buf
) {
289 BT_LOGE_STR("Failed to allocate a GArray.");
290 BT_OBJECT_PUT_REF_AND_RESET(string_field
);
294 g_array_index(string_field
->buf
, char, 0) = '\0';
295 BT_LIB_LOGD("Created string field object: %!+f", string_field
);
298 return (void *) string_field
;
302 int create_fields_from_named_field_classes(
303 struct bt_field_class_named_field_class_container
*fc
,
309 *fields
= g_ptr_array_new_with_free_func(
310 (GDestroyNotify
) bt_field_destroy
);
312 BT_LOGE_STR("Failed to allocate a GPtrArray.");
317 g_ptr_array_set_size(*fields
, fc
->named_fcs
->len
);
319 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
320 struct bt_field
*field
;
321 struct bt_named_field_class
*named_fc
=
322 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, i
);
324 field
= bt_field_create(named_fc
->fc
);
326 BT_LIB_LOGE("Failed to create structure member or variant option field: "
327 "name=\"%s\", %![fc-]+F",
328 named_fc
->name
->str
, named_fc
->fc
);
333 g_ptr_array_index(*fields
, i
) = field
;
341 struct bt_field
*create_structure_field(struct bt_field_class
*fc
)
343 struct bt_field_structure
*struct_field
;
345 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc
);
346 struct_field
= g_new0(struct bt_field_structure
, 1);
348 BT_LOGE_STR("Failed to allocate one structure field.");
352 init_field((void *) struct_field
, fc
, &structure_field_methods
);
354 if (create_fields_from_named_field_classes((void *) fc
,
355 &struct_field
->fields
)) {
356 BT_LIB_LOGE("Cannot create structure member fields: "
358 BT_OBJECT_PUT_REF_AND_RESET(struct_field
);
362 BT_LIB_LOGD("Created structure field object: %!+f", struct_field
);
365 return (void *) struct_field
;
369 struct bt_field
*create_variant_field(struct bt_field_class
*fc
)
371 struct bt_field_variant
*var_field
;
373 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc
);
374 var_field
= g_new0(struct bt_field_variant
, 1);
376 BT_LOGE_STR("Failed to allocate one variant field.");
380 init_field((void *) var_field
, fc
, &variant_field_methods
);
382 if (create_fields_from_named_field_classes((void *) fc
,
383 &var_field
->fields
)) {
384 BT_LIB_LOGE("Cannot create variant member fields: "
386 BT_OBJECT_PUT_REF_AND_RESET(var_field
);
390 BT_LIB_LOGD("Created variant field object: %!+f", var_field
);
393 return (void *) var_field
;
397 int init_array_field_fields(struct bt_field_array
*array_field
)
401 struct bt_field_class_array
*array_fc
;
403 BT_ASSERT(array_field
);
404 array_fc
= (void *) array_field
->common
.class;
405 array_field
->fields
= g_ptr_array_sized_new(array_field
->length
);
406 if (!array_field
->fields
) {
407 BT_LOGE_STR("Failed to allocate a GPtrArray.");
412 g_ptr_array_set_free_func(array_field
->fields
,
413 (GDestroyNotify
) bt_field_destroy
);
414 g_ptr_array_set_size(array_field
->fields
, array_field
->length
);
416 for (i
= 0; i
< array_field
->length
; i
++) {
417 array_field
->fields
->pdata
[i
] = bt_field_create(
418 array_fc
->element_fc
);
419 if (!array_field
->fields
->pdata
[i
]) {
420 BT_LIB_LOGE("Cannot create array field's element field: "
421 "index=%" PRIu64
", %![fc-]+F", i
, array_fc
);
432 struct bt_field
*create_static_array_field(struct bt_field_class
*fc
)
434 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
435 struct bt_field_array
*array_field
;
437 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc
);
438 array_field
= g_new0(struct bt_field_array
, 1);
440 BT_LOGE_STR("Failed to allocate one static array field.");
444 init_field((void *) array_field
, fc
, &array_field_methods
);
445 array_field
->length
= array_fc
->length
;
447 if (init_array_field_fields(array_field
)) {
448 BT_LIB_LOGE("Cannot create static array fields: "
450 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
454 BT_LIB_LOGD("Created static array field object: %!+f", array_field
);
457 return (void *) array_field
;
461 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*fc
)
463 struct bt_field_array
*array_field
;
465 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc
);
466 array_field
= g_new0(struct bt_field_array
, 1);
468 BT_LOGE_STR("Failed to allocate one dynamic array field.");
472 init_field((void *) array_field
, fc
, &array_field_methods
);
474 if (init_array_field_fields(array_field
)) {
475 BT_LIB_LOGE("Cannot create dynamic array fields: "
477 BT_OBJECT_PUT_REF_AND_RESET(array_field
);
481 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field
);
484 return (void *) array_field
;
487 int64_t bt_field_signed_integer_get_value(const struct bt_field
*field
)
489 const struct bt_field_integer
*int_field
= (const void *) field
;
491 BT_ASSERT_PRE_NON_NULL(field
, "Field");
492 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
493 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
494 return int_field
->value
.i
;
497 void bt_field_signed_integer_set_value(struct bt_field
*field
, int64_t value
)
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(const struct bt_field
*field
)
514 const struct bt_field_integer
*int_field
= (const 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_field_unsigned_integer_set_value(struct bt_field
*field
, uint64_t value
)
524 struct bt_field_integer
*int_field
= (void *) field
;
526 BT_ASSERT_PRE_NON_NULL(field
, "Field");
527 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
528 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
529 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
530 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
531 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
532 "%![fc-]+F", value
, field
, field
->class);
533 int_field
->value
.u
= value
;
534 bt_field_set_single(field
, true);
537 double bt_field_real_get_value(const struct bt_field
*field
)
539 const struct bt_field_real
*real_field
= (const void *) field
;
541 BT_ASSERT_PRE_NON_NULL(field
, "Field");
542 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
543 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
544 return real_field
->value
;
547 void bt_field_real_set_value(struct bt_field
*field
, double value
)
549 struct bt_field_real
*real_field
= (void *) field
;
551 BT_ASSERT_PRE_NON_NULL(field
, "Field");
552 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
553 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
555 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
556 (double) (float) value
== value
,
557 "Invalid value for a single-precision real number: value=%f, "
558 "%![fc-]+F", value
, field
->class);
559 real_field
->value
= value
;
560 bt_field_set_single(field
, true);
563 int bt_field_unsigned_enumeration_get_mapping_labels(
564 const struct bt_field
*field
,
565 bt_field_class_enumeration_mapping_label_array
*label_array
,
568 const struct bt_field_integer
*int_field
= (const void *) field
;
570 BT_ASSERT_PRE_NON_NULL(field
, "Field");
571 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
572 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
573 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
574 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
575 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
576 return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
577 field
->class, int_field
->value
.u
, label_array
, count
);
580 int bt_field_signed_enumeration_get_mapping_labels(
581 const struct bt_field
*field
,
582 bt_field_class_enumeration_mapping_label_array
*label_array
,
585 const struct bt_field_integer
*int_field
= (const void *) field
;
587 BT_ASSERT_PRE_NON_NULL(field
, "Field");
588 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
589 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
590 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
591 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
592 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
593 return 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
;
619 int bt_field_string_set_value(struct bt_field
*field
, const char *value
)
621 BT_ASSERT_PRE_NON_NULL(field
, "Field");
622 BT_ASSERT_PRE_NON_NULL(value
, "Value");
623 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
624 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
626 bt_field_string_clear(field
);
627 return bt_field_string_append_with_length(field
, value
,
628 (uint64_t) strlen(value
));
631 int bt_field_string_append(struct bt_field
*field
, const char *value
)
633 return bt_field_string_append_with_length(field
,
634 value
, (uint64_t) strlen(value
));
637 int bt_field_string_append_with_length(struct bt_field
*field
,
638 const char *value
, uint64_t length
)
640 struct bt_field_string
*string_field
= (void *) field
;
644 BT_ASSERT_PRE_NON_NULL(field
, "Field");
645 BT_ASSERT_PRE_NON_NULL(value
, "Value");
646 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
647 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
648 BT_FIELD_CLASS_TYPE_STRING
, "Field");
650 /* Make sure no null bytes are appended */
651 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
652 "String value to append contains a null character: "
653 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
655 new_length
= length
+ string_field
->length
;
657 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
658 g_array_set_size(string_field
->buf
, new_length
+ 1);
661 data
= string_field
->buf
->data
;
662 memcpy(data
+ string_field
->length
, value
, length
);
663 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
664 string_field
->length
= new_length
;
665 bt_field_set_single(field
, true);
669 int bt_field_string_clear(struct bt_field
*field
)
671 struct bt_field_string
*string_field
= (void *) field
;
673 BT_ASSERT_PRE_NON_NULL(field
, "Field");
674 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
675 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
676 BT_FIELD_CLASS_TYPE_STRING
, "Field");
677 string_field
->length
= 0;
678 bt_field_set_single(field
, true);
682 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
684 const struct bt_field_array
*array_field
= (const void *) field
;
686 BT_ASSERT_PRE_NON_NULL(field
, "Field");
687 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
688 return array_field
->length
;
691 int bt_field_dynamic_array_set_length(struct bt_field
*field
, uint64_t length
)
694 struct bt_field_array
*array_field
= (void *) field
;
696 BT_ASSERT_PRE_NON_NULL(field
, "Field");
697 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
698 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
699 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
701 if (unlikely(length
> array_field
->fields
->len
)) {
703 struct bt_field_class_array
*array_fc
;
704 uint64_t cur_len
= array_field
->fields
->len
;
707 g_ptr_array_set_size(array_field
->fields
, length
);
708 array_fc
= (void *) field
->class;
710 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
711 struct bt_field
*elem_field
= bt_field_create(
712 array_fc
->element_fc
);
715 BT_LIB_LOGE("Cannot create element field for "
716 "dynamic array field: "
717 "index=%" PRIu64
", "
718 "%![array-field-]+f", i
, field
);
723 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
724 array_field
->fields
->pdata
[i
] = elem_field
;
728 array_field
->length
= length
;
735 struct bt_field
*borrow_array_field_element_field_by_index(
736 struct bt_field
*field
, uint64_t index
)
738 struct bt_field_array
*array_field
= (void *) field
;
740 BT_ASSERT_PRE_NON_NULL(field
, "Field");
741 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
742 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
743 return array_field
->fields
->pdata
[index
];
746 struct bt_field
*bt_field_array_borrow_element_field_by_index(
747 struct bt_field
*field
, uint64_t index
)
749 return borrow_array_field_element_field_by_index(field
, index
);
752 const struct bt_field
*
753 bt_field_array_borrow_element_field_by_index_const(
754 const struct bt_field
*field
, uint64_t index
)
756 return borrow_array_field_element_field_by_index((void *) field
, index
);
760 struct bt_field
*borrow_structure_field_member_field_by_index(
761 struct bt_field
*field
, uint64_t index
)
763 struct bt_field_structure
*struct_field
= (void *) field
;
765 BT_ASSERT_PRE_NON_NULL(field
, "Field");
766 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
767 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
768 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
769 return struct_field
->fields
->pdata
[index
];
772 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
773 struct bt_field
*field
, uint64_t index
)
775 return borrow_structure_field_member_field_by_index(field
,
779 const struct bt_field
*
780 bt_field_structure_borrow_member_field_by_index_const(
781 const struct bt_field
*field
, uint64_t index
)
783 return borrow_structure_field_member_field_by_index(
784 (void *) field
, index
);
788 struct bt_field
*borrow_structure_field_member_field_by_name(
789 struct bt_field
*field
, const char *name
)
791 struct bt_field
*ret_field
= NULL
;
792 struct bt_field_class_structure
*struct_fc
;
793 struct bt_field_structure
*struct_field
= (void *) field
;
797 BT_ASSERT_PRE_NON_NULL(field
, "Field");
798 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
799 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
800 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
801 struct_fc
= (void *) field
->class;
803 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
804 &orig_key
, &index
)) {
808 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
809 BT_ASSERT(ret_field
);
815 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
816 struct bt_field
*field
, const char *name
)
818 return borrow_structure_field_member_field_by_name(field
, name
);
821 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
822 const struct bt_field
*field
, const char *name
)
824 return borrow_structure_field_member_field_by_name(
825 (void *) field
, name
);
829 struct bt_field
*borrow_variant_field_selected_option_field(
830 struct bt_field
*field
)
832 struct bt_field_variant
*var_field
= (void *) field
;
834 BT_ASSERT_PRE_NON_NULL(field
, "Field");
835 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
836 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
837 BT_ASSERT_PRE(var_field
->selected_field
,
838 "Variant field has no selected field: %!+f", field
);
839 return var_field
->selected_field
;
842 struct bt_field
*bt_field_variant_borrow_selected_option_field(
843 struct bt_field
*field
)
845 return borrow_variant_field_selected_option_field(field
);
848 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
849 const struct bt_field
*field
)
851 return borrow_variant_field_selected_option_field((void *) field
);
854 int bt_field_variant_select_option_field(
855 struct bt_field
*field
, uint64_t index
)
857 struct bt_field_variant
*var_field
= (void *) field
;
859 BT_ASSERT_PRE_NON_NULL(field
, "Field");
860 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
861 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
862 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
863 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
864 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
865 var_field
->selected_index
= index
;
869 uint64_t bt_field_variant_get_selected_option_field_index(
870 const struct bt_field
*field
)
872 const struct bt_field_variant
*var_field
= (const void *) field
;
874 BT_ASSERT_PRE_NON_NULL(field
, "Field");
875 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
876 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
877 BT_ASSERT_PRE(var_field
->selected_field
,
878 "Variant field has no selected field: %!+f", field
);
879 return var_field
->selected_index
;
883 void bt_field_finalize(struct bt_field
*field
)
886 BT_LOGD_STR("Putting field's class.");
887 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
891 void destroy_integer_field(struct bt_field
*field
)
894 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
895 bt_field_finalize(field
);
900 void destroy_real_field(struct bt_field
*field
)
903 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
904 bt_field_finalize(field
);
909 void destroy_structure_field(struct bt_field
*field
)
911 struct bt_field_structure
*struct_field
= (void *) field
;
914 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
915 bt_field_finalize(field
);
917 if (struct_field
->fields
) {
918 g_ptr_array_free(struct_field
->fields
, TRUE
);
919 struct_field
->fields
= NULL
;
926 void destroy_variant_field(struct bt_field
*field
)
928 struct bt_field_variant
*var_field
= (void *) field
;
931 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
932 bt_field_finalize(field
);
934 if (var_field
->fields
) {
935 g_ptr_array_free(var_field
->fields
, TRUE
);
936 var_field
->fields
= NULL
;
943 void destroy_array_field(struct bt_field
*field
)
945 struct bt_field_array
*array_field
= (void *) field
;
948 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
949 bt_field_finalize(field
);
951 if (array_field
->fields
) {
952 g_ptr_array_free(array_field
->fields
, TRUE
);
953 array_field
->fields
= NULL
;
960 void destroy_string_field(struct bt_field
*field
)
962 struct bt_field_string
*string_field
= (void *) field
;
965 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
966 bt_field_finalize(field
);
968 if (string_field
->buf
) {
969 g_array_free(string_field
->buf
, TRUE
);
970 string_field
->buf
= NULL
;
977 void bt_field_destroy(struct bt_field
*field
)
980 BT_ASSERT(bt_field_class_has_known_type(field
->class));
981 field_destroy_funcs
[field
->class->type
](field
);
985 void reset_single_field(struct bt_field
*field
)
988 field
->is_set
= false;
992 void reset_structure_field(struct bt_field
*field
)
995 struct bt_field_structure
*struct_field
= (void *) field
;
999 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1000 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1005 void reset_variant_field(struct bt_field
*field
)
1008 struct bt_field_variant
*var_field
= (void *) field
;
1012 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1013 bt_field_reset(var_field
->fields
->pdata
[i
]);
1018 void reset_array_field(struct bt_field
*field
)
1021 struct bt_field_array
*array_field
= (void *) field
;
1025 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1026 bt_field_reset(array_field
->fields
->pdata
[i
]);
1031 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1033 field
->frozen
= is_frozen
;
1037 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1040 struct bt_field_structure
*struct_field
= (void *) field
;
1042 BT_LIB_LOGD("Setting structure field's frozen state: "
1043 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1045 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1046 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1048 BT_LIB_LOGD("Setting structure field's member field's "
1049 "frozen state: %![field-]+f, index=%" PRIu64
,
1051 bt_field_set_is_frozen(member_field
, is_frozen
);
1054 set_single_field_is_frozen(field
, is_frozen
);
1058 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1061 struct bt_field_variant
*var_field
= (void *) field
;
1063 BT_LIB_LOGD("Setting variant field's frozen state: "
1064 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1066 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1067 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1069 BT_LIB_LOGD("Setting variant field's option field's "
1070 "frozen state: %![field-]+f, index=%" PRIu64
,
1072 bt_field_set_is_frozen(option_field
, is_frozen
);
1075 set_single_field_is_frozen(field
, is_frozen
);
1079 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1082 struct bt_field_array
*array_field
= (void *) field
;
1084 BT_LIB_LOGD("Setting array field's frozen state: "
1085 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1087 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1088 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1090 BT_LIB_LOGD("Setting array field's element field's "
1091 "frozen state: %![field-]+f, index=%" PRIu64
,
1093 bt_field_set_is_frozen(elem_field
, is_frozen
);
1096 set_single_field_is_frozen(field
, is_frozen
);
1100 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1104 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1106 BT_ASSERT(field
->methods
->set_is_frozen
);
1107 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1111 bool single_field_is_set(const struct bt_field
*field
)
1114 return field
->is_set
;
1118 bool structure_field_is_set(const struct bt_field
*field
)
1122 const struct bt_field_structure
*struct_field
= (const void *) field
;
1126 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1127 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1138 bool variant_field_is_set(const struct bt_field
*field
)
1140 const struct bt_field_variant
*var_field
= (const void *) field
;
1141 bool is_set
= false;
1145 if (var_field
->selected_field
) {
1146 is_set
= bt_field_is_set(var_field
->selected_field
);
1153 bool array_field_is_set(const struct bt_field
*field
)
1157 const struct bt_field_array
*array_field
= (const void *) field
;
1161 for (i
= 0; i
< array_field
->length
; i
++) {
1162 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);