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 "LIB/FIELD"
25 #include "lib/lib-logging.h"
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/field.h>
29 #include <babeltrace2/trace-ir/field-const.h>
30 #include "lib/object.h"
31 #include "compat/compiler.h"
32 #include "compat/fcntl.h"
33 #include "common/align.h"
34 #include "common/assert.h"
38 #include "field-class.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 enum bt_field_status
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");
576 bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
577 field
->class, int_field
->value
.u
, label_array
, count
);
580 enum bt_field_status
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");
594 bt_field_class_signed_enumeration_get_mapping_labels_by_value(
595 field
->class, int_field
->value
.i
, label_array
, count
);
598 const char *bt_field_string_get_value(const struct bt_field
*field
)
600 const struct bt_field_string
*string_field
= (const void *) field
;
602 BT_ASSERT_PRE_NON_NULL(field
, "Field");
603 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
604 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
606 return (const char *) string_field
->buf
->data
;
609 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
611 const struct bt_field_string
*string_field
= (const void *) field
;
613 BT_ASSERT_PRE_NON_NULL(field
, "Field");
614 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
615 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
617 return string_field
->length
;
621 void clear_string_field(struct bt_field
*field
)
623 struct bt_field_string
*string_field
= (void *) field
;
626 string_field
->length
= 0;
627 bt_field_set_single(field
, true);
630 enum bt_field_status
bt_field_string_set_value(struct bt_field
*field
,
633 BT_ASSERT_PRE_NON_NULL(field
, "Field");
634 BT_ASSERT_PRE_NON_NULL(value
, "Value");
635 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
636 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
638 clear_string_field(field
);
639 return bt_field_string_append_with_length(field
, value
,
640 (uint64_t) strlen(value
));
643 enum bt_field_status
bt_field_string_append(struct bt_field
*field
, const char *value
)
645 return bt_field_string_append_with_length(field
,
646 value
, (uint64_t) strlen(value
));
649 enum bt_field_status
bt_field_string_append_with_length(struct bt_field
*field
,
650 const char *value
, uint64_t length
)
652 struct bt_field_string
*string_field
= (void *) field
;
656 BT_ASSERT_PRE_NON_NULL(field
, "Field");
657 BT_ASSERT_PRE_NON_NULL(value
, "Value");
658 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
659 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
660 BT_FIELD_CLASS_TYPE_STRING
, "Field");
662 /* Make sure no null bytes are appended */
663 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
664 "String value to append contains a null character: "
665 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
667 new_length
= length
+ string_field
->length
;
669 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
670 g_array_set_size(string_field
->buf
, new_length
+ 1);
673 data
= string_field
->buf
->data
;
674 memcpy(data
+ string_field
->length
, value
, length
);
675 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
676 string_field
->length
= new_length
;
677 bt_field_set_single(field
, true);
678 return BT_FIELD_STATUS_OK
;
681 enum bt_field_status
bt_field_string_clear(struct bt_field
*field
)
683 BT_ASSERT_PRE_NON_NULL(field
, "Field");
684 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
685 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
686 BT_FIELD_CLASS_TYPE_STRING
, "Field");
687 clear_string_field(field
);
688 return BT_FIELD_STATUS_OK
;
691 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
693 const struct bt_field_array
*array_field
= (const 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 enum bt_field_status
bt_field_dynamic_array_set_length(struct bt_field
*field
,
703 int ret
= BT_FIELD_STATUS_OK
;
704 struct bt_field_array
*array_field
= (void *) field
;
706 BT_ASSERT_PRE_NON_NULL(field
, "Field");
707 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
708 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
709 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
711 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
713 struct bt_field_class_array
*array_fc
;
714 uint64_t cur_len
= array_field
->fields
->len
;
717 g_ptr_array_set_size(array_field
->fields
, length
);
718 array_fc
= (void *) field
->class;
720 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
721 struct bt_field
*elem_field
= bt_field_create(
722 array_fc
->element_fc
);
725 BT_LIB_LOGE("Cannot create element field for "
726 "dynamic array field: "
727 "index=%" PRIu64
", "
728 "%![array-field-]+f", i
, field
);
729 ret
= BT_FIELD_STATUS_NOMEM
;
733 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
734 array_field
->fields
->pdata
[i
] = elem_field
;
738 array_field
->length
= length
;
745 struct bt_field
*borrow_array_field_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_field
*bt_field_array_borrow_element_field_by_index(
757 struct bt_field
*field
, uint64_t index
)
759 return borrow_array_field_element_field_by_index(field
, index
);
762 const struct bt_field
*
763 bt_field_array_borrow_element_field_by_index_const(
764 const struct bt_field
*field
, uint64_t index
)
766 return borrow_array_field_element_field_by_index((void *) field
, index
);
770 struct bt_field
*borrow_structure_field_member_field_by_index(
771 struct bt_field
*field
, uint64_t index
)
773 struct bt_field_structure
*struct_field
= (void *) field
;
775 BT_ASSERT_PRE_NON_NULL(field
, "Field");
776 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
777 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
778 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
779 return struct_field
->fields
->pdata
[index
];
782 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
783 struct bt_field
*field
, uint64_t index
)
785 return borrow_structure_field_member_field_by_index(field
,
789 const struct bt_field
*
790 bt_field_structure_borrow_member_field_by_index_const(
791 const struct bt_field
*field
, uint64_t index
)
793 return borrow_structure_field_member_field_by_index(
794 (void *) field
, index
);
798 struct bt_field
*borrow_structure_field_member_field_by_name(
799 struct bt_field
*field
, const char *name
)
801 struct bt_field
*ret_field
= NULL
;
802 struct bt_field_class_structure
*struct_fc
;
803 struct bt_field_structure
*struct_field
= (void *) field
;
807 BT_ASSERT_PRE_NON_NULL(field
, "Field");
808 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
809 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
810 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
811 struct_fc
= (void *) field
->class;
813 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
814 &orig_key
, &index
)) {
818 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
819 BT_ASSERT(ret_field
);
825 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
826 struct bt_field
*field
, const char *name
)
828 return borrow_structure_field_member_field_by_name(field
, name
);
831 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
832 const struct bt_field
*field
, const char *name
)
834 return borrow_structure_field_member_field_by_name(
835 (void *) field
, name
);
839 struct bt_field
*borrow_variant_field_selected_option_field(
840 struct bt_field
*field
)
842 struct bt_field_variant
*var_field
= (void *) field
;
844 BT_ASSERT_PRE_NON_NULL(field
, "Field");
845 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
846 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
847 BT_ASSERT_PRE(var_field
->selected_field
,
848 "Variant field has no selected field: %!+f", field
);
849 return var_field
->selected_field
;
852 struct bt_field
*bt_field_variant_borrow_selected_option_field(
853 struct bt_field
*field
)
855 return borrow_variant_field_selected_option_field(field
);
858 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
859 const struct bt_field
*field
)
861 return borrow_variant_field_selected_option_field((void *) field
);
864 enum bt_field_status
bt_field_variant_select_option_field(
865 struct bt_field
*field
, uint64_t index
)
867 struct bt_field_variant
*var_field
= (void *) field
;
869 BT_ASSERT_PRE_NON_NULL(field
, "Field");
870 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
871 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
872 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
873 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
874 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
875 var_field
->selected_index
= index
;
876 return BT_FIELD_STATUS_OK
;
879 uint64_t bt_field_variant_get_selected_option_field_index(
880 const struct bt_field
*field
)
882 const struct bt_field_variant
*var_field
= (const void *) field
;
884 BT_ASSERT_PRE_NON_NULL(field
, "Field");
885 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
886 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
887 BT_ASSERT_PRE(var_field
->selected_field
,
888 "Variant field has no selected field: %!+f", field
);
889 return var_field
->selected_index
;
893 void bt_field_finalize(struct bt_field
*field
)
896 BT_LOGD_STR("Putting field's class.");
897 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
901 void destroy_integer_field(struct bt_field
*field
)
904 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
905 bt_field_finalize(field
);
910 void destroy_real_field(struct bt_field
*field
)
913 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
914 bt_field_finalize(field
);
919 void destroy_structure_field(struct bt_field
*field
)
921 struct bt_field_structure
*struct_field
= (void *) field
;
924 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
925 bt_field_finalize(field
);
927 if (struct_field
->fields
) {
928 g_ptr_array_free(struct_field
->fields
, TRUE
);
929 struct_field
->fields
= NULL
;
936 void destroy_variant_field(struct bt_field
*field
)
938 struct bt_field_variant
*var_field
= (void *) field
;
941 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
942 bt_field_finalize(field
);
944 if (var_field
->fields
) {
945 g_ptr_array_free(var_field
->fields
, TRUE
);
946 var_field
->fields
= NULL
;
953 void destroy_array_field(struct bt_field
*field
)
955 struct bt_field_array
*array_field
= (void *) field
;
958 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
959 bt_field_finalize(field
);
961 if (array_field
->fields
) {
962 g_ptr_array_free(array_field
->fields
, TRUE
);
963 array_field
->fields
= NULL
;
970 void destroy_string_field(struct bt_field
*field
)
972 struct bt_field_string
*string_field
= (void *) field
;
975 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
976 bt_field_finalize(field
);
978 if (string_field
->buf
) {
979 g_array_free(string_field
->buf
, TRUE
);
980 string_field
->buf
= NULL
;
987 void bt_field_destroy(struct bt_field
*field
)
990 BT_ASSERT(bt_field_class_has_known_type(field
->class));
991 field_destroy_funcs
[field
->class->type
](field
);
995 void reset_single_field(struct bt_field
*field
)
998 field
->is_set
= false;
1002 void reset_structure_field(struct bt_field
*field
)
1005 struct bt_field_structure
*struct_field
= (void *) field
;
1009 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1010 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1015 void reset_variant_field(struct bt_field
*field
)
1018 struct bt_field_variant
*var_field
= (void *) field
;
1022 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1023 bt_field_reset(var_field
->fields
->pdata
[i
]);
1028 void reset_array_field(struct bt_field
*field
)
1031 struct bt_field_array
*array_field
= (void *) field
;
1035 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1036 bt_field_reset(array_field
->fields
->pdata
[i
]);
1041 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1043 field
->frozen
= is_frozen
;
1047 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1050 struct bt_field_structure
*struct_field
= (void *) field
;
1052 BT_LIB_LOGD("Setting structure field's frozen state: "
1053 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1055 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1056 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1058 BT_LIB_LOGD("Setting structure field's member field's "
1059 "frozen state: %![field-]+f, index=%" PRIu64
,
1061 bt_field_set_is_frozen(member_field
, is_frozen
);
1064 set_single_field_is_frozen(field
, is_frozen
);
1068 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1071 struct bt_field_variant
*var_field
= (void *) field
;
1073 BT_LIB_LOGD("Setting variant field's frozen state: "
1074 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1076 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1077 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1079 BT_LIB_LOGD("Setting variant field's option field's "
1080 "frozen state: %![field-]+f, index=%" PRIu64
,
1082 bt_field_set_is_frozen(option_field
, is_frozen
);
1085 set_single_field_is_frozen(field
, is_frozen
);
1089 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1092 struct bt_field_array
*array_field
= (void *) field
;
1094 BT_LIB_LOGD("Setting array field's frozen state: "
1095 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1097 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1098 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1100 BT_LIB_LOGD("Setting array field's element field's "
1101 "frozen state: %![field-]+f, index=%" PRIu64
,
1103 bt_field_set_is_frozen(elem_field
, is_frozen
);
1106 set_single_field_is_frozen(field
, is_frozen
);
1110 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1114 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1116 BT_ASSERT(field
->methods
->set_is_frozen
);
1117 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1121 bool single_field_is_set(const struct bt_field
*field
)
1124 return field
->is_set
;
1128 bool structure_field_is_set(const struct bt_field
*field
)
1132 const struct bt_field_structure
*struct_field
= (const void *) field
;
1136 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1137 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1148 bool variant_field_is_set(const struct bt_field
*field
)
1150 const struct bt_field_variant
*var_field
= (const void *) field
;
1151 bool is_set
= false;
1155 if (var_field
->selected_field
) {
1156 is_set
= bt_field_is_set(var_field
->selected_field
);
1163 bool array_field_is_set(const struct bt_field
*field
)
1167 const struct bt_field_array
*array_field
= (const void *) field
;
1171 for (i
= 0; i
< array_field
->length
; i
++) {
1172 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);