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/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"
39 #include "lib/func-status.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 enum bt_field_enumeration_get_mapping_labels_status
564 bt_field_unsigned_enumeration_get_mapping_labels(
565 const struct bt_field
*field
,
566 bt_field_class_enumeration_mapping_label_array
*label_array
,
569 const struct bt_field_integer
*int_field
= (const void *) field
;
571 BT_ASSERT_PRE_NON_NULL(field
, "Field");
572 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
573 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
574 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
575 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
576 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
578 bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
579 field
->class, int_field
->value
.u
, label_array
, count
);
582 enum bt_field_enumeration_get_mapping_labels_status
583 bt_field_signed_enumeration_get_mapping_labels(
584 const struct bt_field
*field
,
585 bt_field_class_enumeration_mapping_label_array
*label_array
,
588 const struct bt_field_integer
*int_field
= (const void *) field
;
590 BT_ASSERT_PRE_NON_NULL(field
, "Field");
591 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
592 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
593 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
594 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
595 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
, "Field");
597 bt_field_class_signed_enumeration_get_mapping_labels_by_value(
598 field
->class, int_field
->value
.i
, label_array
, count
);
601 const char *bt_field_string_get_value(const struct bt_field
*field
)
603 const struct bt_field_string
*string_field
= (const void *) field
;
605 BT_ASSERT_PRE_NON_NULL(field
, "Field");
606 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
607 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
609 return (const char *) string_field
->buf
->data
;
612 uint64_t bt_field_string_get_length(const struct bt_field
*field
)
614 const struct bt_field_string
*string_field
= (const void *) field
;
616 BT_ASSERT_PRE_NON_NULL(field
, "Field");
617 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
618 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
620 return string_field
->length
;
624 void clear_string_field(struct bt_field
*field
)
626 struct bt_field_string
*string_field
= (void *) field
;
629 string_field
->length
= 0;
630 bt_field_set_single(field
, true);
633 enum bt_field_string_set_value_status
bt_field_string_set_value(
634 struct bt_field
*field
, const char *value
)
636 BT_ASSERT_PRE_NON_NULL(field
, "Field");
637 BT_ASSERT_PRE_NON_NULL(value
, "Value");
638 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
639 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
641 clear_string_field(field
);
642 return (int) bt_field_string_append_with_length(field
, value
,
643 (uint64_t) strlen(value
));
646 enum bt_field_string_append_status
bt_field_string_append(
647 struct bt_field
*field
, const char *value
)
649 return bt_field_string_append_with_length(field
,
650 value
, (uint64_t) strlen(value
));
653 enum bt_field_string_append_status
bt_field_string_append_with_length(
654 struct bt_field
*field
, const char *value
, uint64_t length
)
656 struct bt_field_string
*string_field
= (void *) field
;
660 BT_ASSERT_PRE_NON_NULL(field
, "Field");
661 BT_ASSERT_PRE_NON_NULL(value
, "Value");
662 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
663 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
664 BT_FIELD_CLASS_TYPE_STRING
, "Field");
666 /* Make sure no null bytes are appended */
667 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
668 "String value to append contains a null character: "
669 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
671 new_length
= length
+ string_field
->length
;
673 if (G_UNLIKELY(new_length
+ 1 > string_field
->buf
->len
)) {
674 g_array_set_size(string_field
->buf
, new_length
+ 1);
677 data
= string_field
->buf
->data
;
678 memcpy(data
+ string_field
->length
, value
, length
);
679 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
680 string_field
->length
= new_length
;
681 bt_field_set_single(field
, true);
682 return BT_FUNC_STATUS_OK
;
685 void bt_field_string_clear(struct bt_field
*field
)
687 BT_ASSERT_PRE_NON_NULL(field
, "Field");
688 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
689 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
690 BT_FIELD_CLASS_TYPE_STRING
, "Field");
691 clear_string_field(field
);
694 uint64_t bt_field_array_get_length(const struct bt_field
*field
)
696 const struct bt_field_array
*array_field
= (const void *) field
;
698 BT_ASSERT_PRE_NON_NULL(field
, "Field");
699 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
700 return array_field
->length
;
703 enum bt_field_dynamic_array_set_length_status
bt_field_dynamic_array_set_length(
704 struct bt_field
*field
, uint64_t length
)
706 int ret
= BT_FUNC_STATUS_OK
;
707 struct bt_field_array
*array_field
= (void *) field
;
709 BT_ASSERT_PRE_NON_NULL(field
, "Field");
710 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
711 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
712 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
714 if (G_UNLIKELY(length
> array_field
->fields
->len
)) {
716 struct bt_field_class_array
*array_fc
;
717 uint64_t cur_len
= array_field
->fields
->len
;
720 g_ptr_array_set_size(array_field
->fields
, length
);
721 array_fc
= (void *) field
->class;
723 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
724 struct bt_field
*elem_field
= bt_field_create(
725 array_fc
->element_fc
);
728 BT_LIB_LOGE("Cannot create element field for "
729 "dynamic array field: "
730 "index=%" PRIu64
", "
731 "%![array-field-]+f", i
, field
);
732 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
736 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
737 array_field
->fields
->pdata
[i
] = elem_field
;
741 array_field
->length
= length
;
748 struct bt_field
*borrow_array_field_element_field_by_index(
749 struct bt_field
*field
, uint64_t index
)
751 struct bt_field_array
*array_field
= (void *) field
;
753 BT_ASSERT_PRE_NON_NULL(field
, "Field");
754 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
755 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
756 return array_field
->fields
->pdata
[index
];
759 struct bt_field
*bt_field_array_borrow_element_field_by_index(
760 struct bt_field
*field
, uint64_t index
)
762 return borrow_array_field_element_field_by_index(field
, index
);
765 const struct bt_field
*
766 bt_field_array_borrow_element_field_by_index_const(
767 const struct bt_field
*field
, uint64_t index
)
769 return borrow_array_field_element_field_by_index((void *) field
, index
);
773 struct bt_field
*borrow_structure_field_member_field_by_index(
774 struct bt_field
*field
, uint64_t index
)
776 struct bt_field_structure
*struct_field
= (void *) field
;
778 BT_ASSERT_PRE_NON_NULL(field
, "Field");
779 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
780 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
781 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
782 return struct_field
->fields
->pdata
[index
];
785 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
786 struct bt_field
*field
, uint64_t index
)
788 return borrow_structure_field_member_field_by_index(field
,
792 const struct bt_field
*
793 bt_field_structure_borrow_member_field_by_index_const(
794 const struct bt_field
*field
, uint64_t index
)
796 return borrow_structure_field_member_field_by_index(
797 (void *) field
, index
);
801 struct bt_field
*borrow_structure_field_member_field_by_name(
802 struct bt_field
*field
, const char *name
)
804 struct bt_field
*ret_field
= NULL
;
805 struct bt_field_class_structure
*struct_fc
;
806 struct bt_field_structure
*struct_field
= (void *) field
;
810 BT_ASSERT_PRE_NON_NULL(field
, "Field");
811 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
812 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
813 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
814 struct_fc
= (void *) field
->class;
816 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
817 &orig_key
, &index
)) {
821 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
822 BT_ASSERT(ret_field
);
828 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
829 struct bt_field
*field
, const char *name
)
831 return borrow_structure_field_member_field_by_name(field
, name
);
834 const struct bt_field
*bt_field_structure_borrow_member_field_by_name_const(
835 const struct bt_field
*field
, const char *name
)
837 return borrow_structure_field_member_field_by_name(
838 (void *) field
, name
);
842 struct bt_field
*borrow_variant_field_selected_option_field(
843 struct bt_field
*field
)
845 struct bt_field_variant
*var_field
= (void *) field
;
847 BT_ASSERT_PRE_NON_NULL(field
, "Field");
848 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
849 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
850 BT_ASSERT_PRE(var_field
->selected_field
,
851 "Variant field has no selected field: %!+f", field
);
852 return var_field
->selected_field
;
855 struct bt_field
*bt_field_variant_borrow_selected_option_field(
856 struct bt_field
*field
)
858 return borrow_variant_field_selected_option_field(field
);
861 const struct bt_field
*bt_field_variant_borrow_selected_option_field_const(
862 const struct bt_field
*field
)
864 return borrow_variant_field_selected_option_field((void *) field
);
867 enum bt_field_variant_select_option_field_status
868 bt_field_variant_select_option_field(
869 struct bt_field
*field
, uint64_t index
)
871 struct bt_field_variant
*var_field
= (void *) field
;
873 BT_ASSERT_PRE_NON_NULL(field
, "Field");
874 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
875 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
876 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
877 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
878 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
879 var_field
->selected_index
= index
;
880 return BT_FUNC_STATUS_OK
;
883 uint64_t bt_field_variant_get_selected_option_field_index(
884 const struct bt_field
*field
)
886 const struct bt_field_variant
*var_field
= (const void *) field
;
888 BT_ASSERT_PRE_NON_NULL(field
, "Field");
889 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
890 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
891 BT_ASSERT_PRE(var_field
->selected_field
,
892 "Variant field has no selected field: %!+f", field
);
893 return var_field
->selected_index
;
897 void bt_field_finalize(struct bt_field
*field
)
900 BT_LOGD_STR("Putting field's class.");
901 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
905 void destroy_integer_field(struct bt_field
*field
)
908 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
909 bt_field_finalize(field
);
914 void destroy_real_field(struct bt_field
*field
)
917 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
918 bt_field_finalize(field
);
923 void destroy_structure_field(struct bt_field
*field
)
925 struct bt_field_structure
*struct_field
= (void *) field
;
928 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
929 bt_field_finalize(field
);
931 if (struct_field
->fields
) {
932 g_ptr_array_free(struct_field
->fields
, TRUE
);
933 struct_field
->fields
= NULL
;
940 void destroy_variant_field(struct bt_field
*field
)
942 struct bt_field_variant
*var_field
= (void *) field
;
945 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
946 bt_field_finalize(field
);
948 if (var_field
->fields
) {
949 g_ptr_array_free(var_field
->fields
, TRUE
);
950 var_field
->fields
= NULL
;
957 void destroy_array_field(struct bt_field
*field
)
959 struct bt_field_array
*array_field
= (void *) field
;
962 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
963 bt_field_finalize(field
);
965 if (array_field
->fields
) {
966 g_ptr_array_free(array_field
->fields
, TRUE
);
967 array_field
->fields
= NULL
;
974 void destroy_string_field(struct bt_field
*field
)
976 struct bt_field_string
*string_field
= (void *) field
;
979 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
980 bt_field_finalize(field
);
982 if (string_field
->buf
) {
983 g_array_free(string_field
->buf
, TRUE
);
984 string_field
->buf
= NULL
;
991 void bt_field_destroy(struct bt_field
*field
)
994 BT_ASSERT(bt_field_class_has_known_type(field
->class));
995 field_destroy_funcs
[field
->class->type
](field
);
999 void reset_single_field(struct bt_field
*field
)
1002 field
->is_set
= false;
1006 void reset_structure_field(struct bt_field
*field
)
1009 struct bt_field_structure
*struct_field
= (void *) field
;
1013 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1014 bt_field_reset(struct_field
->fields
->pdata
[i
]);
1019 void reset_variant_field(struct bt_field
*field
)
1022 struct bt_field_variant
*var_field
= (void *) field
;
1026 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1027 bt_field_reset(var_field
->fields
->pdata
[i
]);
1032 void reset_array_field(struct bt_field
*field
)
1035 struct bt_field_array
*array_field
= (void *) field
;
1039 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1040 bt_field_reset(array_field
->fields
->pdata
[i
]);
1045 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1047 field
->frozen
= is_frozen
;
1051 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1054 struct bt_field_structure
*struct_field
= (void *) field
;
1056 BT_LIB_LOGD("Setting structure field's frozen state: "
1057 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1059 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1060 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1062 BT_LIB_LOGD("Setting structure field's member field's "
1063 "frozen state: %![field-]+f, index=%" PRIu64
,
1065 bt_field_set_is_frozen(member_field
, is_frozen
);
1068 set_single_field_is_frozen(field
, is_frozen
);
1072 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1075 struct bt_field_variant
*var_field
= (void *) field
;
1077 BT_LIB_LOGD("Setting variant field's frozen state: "
1078 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1080 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1081 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1083 BT_LIB_LOGD("Setting variant field's option field's "
1084 "frozen state: %![field-]+f, index=%" PRIu64
,
1086 bt_field_set_is_frozen(option_field
, is_frozen
);
1089 set_single_field_is_frozen(field
, is_frozen
);
1093 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1096 struct bt_field_array
*array_field
= (void *) field
;
1098 BT_LIB_LOGD("Setting array field's frozen state: "
1099 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1101 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1102 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1104 BT_LIB_LOGD("Setting array field's element field's "
1105 "frozen state: %![field-]+f, index=%" PRIu64
,
1107 bt_field_set_is_frozen(elem_field
, is_frozen
);
1110 set_single_field_is_frozen(field
, is_frozen
);
1114 void _bt_field_set_is_frozen(const struct bt_field
*field
,
1118 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1120 BT_ASSERT(field
->methods
->set_is_frozen
);
1121 field
->methods
->set_is_frozen((void *) field
, is_frozen
);
1125 bool single_field_is_set(const struct bt_field
*field
)
1128 return field
->is_set
;
1132 bool structure_field_is_set(const struct bt_field
*field
)
1136 const struct bt_field_structure
*struct_field
= (const void *) field
;
1140 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1141 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1152 bool variant_field_is_set(const struct bt_field
*field
)
1154 const struct bt_field_variant
*var_field
= (const void *) field
;
1155 bool is_set
= false;
1159 if (var_field
->selected_field
) {
1160 is_set
= bt_field_is_set(var_field
->selected_field
);
1167 bool array_field_is_set(const struct bt_field
*field
)
1171 const struct bt_field_array
*array_field
= (const void *) field
;
1175 for (i
= 0; i
< array_field
->length
; i
++) {
1176 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);