2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "FIELDS"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/private-fields.h>
30 #include <babeltrace/trace-ir/fields.h>
31 #include <babeltrace/trace-ir/fields-internal.h>
32 #include <babeltrace/trace-ir/field-classes-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/object.h>
35 #include <babeltrace/compiler-internal.h>
36 #include <babeltrace/compat/fcntl-internal.h>
37 #include <babeltrace/align-internal.h>
38 #include <babeltrace/assert-internal.h>
42 void reset_single_field(struct bt_field
*field
);
45 void reset_array_field(struct bt_field
*field
);
48 void reset_structure_field(struct bt_field
*field
);
51 void reset_variant_field(struct bt_field
*field
);
54 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
57 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
60 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
63 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
);
66 bool single_field_is_set(struct bt_field
*field
);
69 bool array_field_is_set(struct bt_field
*field
);
72 bool structure_field_is_set(struct bt_field
*field
);
75 bool variant_field_is_set(struct bt_field
*field
);
78 struct bt_field_methods integer_field_methods
= {
79 .set_is_frozen
= set_single_field_is_frozen
,
80 .is_set
= single_field_is_set
,
81 .reset
= reset_single_field
,
85 struct bt_field_methods real_field_methods
= {
86 .set_is_frozen
= set_single_field_is_frozen
,
87 .is_set
= single_field_is_set
,
88 .reset
= reset_single_field
,
92 struct bt_field_methods string_field_methods
= {
93 .set_is_frozen
= set_single_field_is_frozen
,
94 .is_set
= single_field_is_set
,
95 .reset
= reset_single_field
,
99 struct bt_field_methods structure_field_methods
= {
100 .set_is_frozen
= set_structure_field_is_frozen
,
101 .is_set
= structure_field_is_set
,
102 .reset
= reset_structure_field
,
106 struct bt_field_methods array_field_methods
= {
107 .set_is_frozen
= set_array_field_is_frozen
,
108 .is_set
= array_field_is_set
,
109 .reset
= reset_array_field
,
113 struct bt_field_methods variant_field_methods
= {
114 .set_is_frozen
= set_variant_field_is_frozen
,
115 .is_set
= variant_field_is_set
,
116 .reset
= reset_variant_field
,
120 struct bt_field
*create_integer_field(struct bt_field_class
*);
123 struct bt_field
*create_real_field(struct bt_field_class
*);
126 struct bt_field
*create_string_field(struct bt_field_class
*);
129 struct bt_field
*create_structure_field(struct bt_field_class
*);
132 struct bt_field
*create_static_array_field(struct bt_field_class
*);
135 struct bt_field
*create_dynamic_array_field(struct bt_field_class
*);
138 struct bt_field
*create_variant_field(struct bt_field_class
*);
141 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_class
*) = {
142 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = create_integer_field
,
143 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = create_integer_field
,
144 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = create_integer_field
,
145 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = create_integer_field
,
146 [BT_FIELD_CLASS_TYPE_REAL
] = create_real_field
,
147 [BT_FIELD_CLASS_TYPE_STRING
] = create_string_field
,
148 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = create_structure_field
,
149 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = create_static_array_field
,
150 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = create_dynamic_array_field
,
151 [BT_FIELD_CLASS_TYPE_VARIANT
] = create_variant_field
,
155 void destroy_integer_field(struct bt_field
*field
);
158 void destroy_real_field(struct bt_field
*field
);
161 void destroy_string_field(struct bt_field
*field
);
164 void destroy_structure_field(struct bt_field
*field
);
167 void destroy_array_field(struct bt_field
*field
);
170 void destroy_variant_field(struct bt_field
*field
);
173 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
174 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
] = destroy_integer_field
,
175 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
] = destroy_integer_field
,
176 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
] = destroy_integer_field
,
177 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
] = destroy_integer_field
,
178 [BT_FIELD_CLASS_TYPE_REAL
] = destroy_real_field
,
179 [BT_FIELD_CLASS_TYPE_STRING
] = destroy_string_field
,
180 [BT_FIELD_CLASS_TYPE_STRUCTURE
] = destroy_structure_field
,
181 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY
] = destroy_array_field
,
182 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
] = destroy_array_field
,
183 [BT_FIELD_CLASS_TYPE_VARIANT
] = destroy_variant_field
,
186 struct bt_field_class
*bt_field_borrow_class(struct bt_field
*field
)
188 BT_ASSERT_PRE_NON_NULL(field
, "Field");
192 struct bt_private_field_class
*bt_private_field_borrow_class(
193 struct bt_private_field
*field
)
195 return (void *) bt_field_borrow_class((void *) field
);
198 enum bt_field_class_type
bt_field_get_class_type(struct bt_field
*field
)
200 BT_ASSERT_PRE_NON_NULL(field
, "Field");
201 return field
->class->type
;
205 struct bt_field
*bt_field_create(struct bt_field_class
*fc
)
207 struct bt_field
*field
= NULL
;
209 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
210 BT_ASSERT(bt_field_class_has_known_type(fc
));
211 field
= field_create_funcs
[fc
->type
](fc
);
213 BT_LIB_LOGE("Cannot create field object from field classe: "
223 void init_field(struct bt_field
*field
, struct bt_field_class
*fc
,
224 struct bt_field_methods
*methods
)
228 bt_object_init_unique(&field
->base
);
229 field
->methods
= methods
;
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(struct bt_field
*field
)
488 struct bt_field_integer
*int_field
= (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_private_field_signed_integer_set_value(
497 struct bt_private_field
*priv_field
, int64_t value
)
499 struct bt_field
*field
= (void *) priv_field
;
500 struct bt_field_integer
*int_field
= (void *) field
;
502 BT_ASSERT_PRE_NON_NULL(field
, "Field");
503 BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field
, "Field");
504 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
505 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(
506 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
507 "Value is out of bounds: value=%" PRId64
", %![field-]+f, "
508 "%![fc-]+F", value
, field
, field
->class);
509 int_field
->value
.i
= value
;
510 bt_field_set_single(field
, true);
513 uint64_t bt_field_unsigned_integer_get_value(struct bt_field
*field
)
515 struct bt_field_integer
*int_field
= (void *) field
;
517 BT_ASSERT_PRE_NON_NULL(field
, "Field");
518 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
519 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
520 return int_field
->value
.u
;
523 void bt_private_field_unsigned_integer_set_value(
524 struct bt_private_field
*priv_field
, uint64_t value
)
526 struct bt_field
*field
= (void *) priv_field
;
527 struct bt_field_integer
*int_field
= (void *) field
;
529 BT_ASSERT_PRE_NON_NULL(field
, "Field");
530 BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field
, "Field");
531 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
532 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(
533 ((struct bt_field_class_integer
*) field
->class)->range
, value
),
534 "Value is out of bounds: value=%" PRIu64
", %![field-]+f, "
535 "%![fc-]+F", value
, field
, field
->class);
536 int_field
->value
.u
= value
;
537 bt_field_set_single(field
, true);
540 double bt_field_real_get_value(struct bt_field
*field
)
542 struct bt_field_real
*real_field
= (void *) field
;
544 BT_ASSERT_PRE_NON_NULL(field
, "Field");
545 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
546 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
547 return real_field
->value
;
550 void bt_private_field_real_set_value(struct bt_private_field
*priv_field
,
553 struct bt_field
*field
= (void *) priv_field
;
554 struct bt_field_real
*real_field
= (void *) field
;
556 BT_ASSERT_PRE_NON_NULL(field
, "Field");
557 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_REAL
, "Field");
558 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
560 !((struct bt_field_class_real
*) field
->class)->is_single_precision
||
561 (double) (float) value
== value
,
562 "Invalid value for a single-precision real number: value=%f, "
563 "%![fc-]+F", value
, field
->class);
564 real_field
->value
= value
;
565 bt_field_set_single(field
, true);
568 int bt_field_unsigned_enumeration_get_mapping_labels(struct bt_field
*field
,
569 bt_field_class_enumeration_mapping_label_array
*label_array
,
572 struct bt_field_integer
*int_field
= (void *) field
;
574 BT_ASSERT_PRE_NON_NULL(field
, "Field");
575 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
576 BT_ASSERT_PRE_NON_NULL(label_array
, "Count (output)");
577 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
578 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
579 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field");
580 return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
581 field
->class, int_field
->value
.u
, label_array
, count
);
584 int bt_field_signed_enumeration_get_mapping_labels(struct bt_field
*field
,
585 bt_field_class_enumeration_mapping_label_array
*label_array
,
588 struct bt_field_integer
*int_field
= (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");
596 return bt_field_class_signed_enumeration_get_mapping_labels_by_value(
597 field
->class, int_field
->value
.i
, label_array
, count
);
600 const char *bt_field_string_get_value(struct bt_field
*field
)
602 struct bt_field_string
*string_field
= (void *) field
;
604 BT_ASSERT_PRE_NON_NULL(field
, "Field");
605 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
606 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
608 return (const char *) string_field
->buf
->data
;
611 uint64_t bt_field_string_get_length(struct bt_field
*field
)
613 struct bt_field_string
*string_field
= (void *) field
;
615 BT_ASSERT_PRE_NON_NULL(field
, "Field");
616 BT_ASSERT_PRE_FIELD_IS_SET(field
, "Field");
617 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
619 return string_field
->length
;
622 int bt_private_field_string_set_value(struct bt_private_field
*priv_field
,
625 struct bt_field
*field
= (void *) priv_field
;
627 BT_ASSERT_PRE_NON_NULL(field
, "Field");
628 BT_ASSERT_PRE_NON_NULL(value
, "Value");
629 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
630 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
, BT_FIELD_CLASS_TYPE_STRING
,
632 bt_private_field_string_clear(priv_field
);
633 return bt_private_field_string_append_with_length(priv_field
, value
,
634 (uint64_t) strlen(value
));
637 int bt_private_field_string_append(struct bt_private_field
*field
,
640 return bt_private_field_string_append_with_length(field
,
641 value
, (uint64_t) strlen(value
));
644 int bt_private_field_string_append_with_length(
645 struct bt_private_field
*priv_field
,
646 const char *value
, uint64_t length
)
648 struct bt_field
*field
= (void *) priv_field
;
649 struct bt_field_string
*string_field
= (void *) field
;
653 BT_ASSERT_PRE_NON_NULL(field
, "Field");
654 BT_ASSERT_PRE_NON_NULL(value
, "Value");
655 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
656 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
657 BT_FIELD_CLASS_TYPE_STRING
, "Field");
659 /* Make sure no null bytes are appended */
660 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
661 "String value to append contains a null character: "
662 "partial-value=\"%.32s\", length=%" PRIu64
, value
, length
);
664 new_length
= length
+ string_field
->length
;
666 if (unlikely(new_length
+ 1 > string_field
->buf
->len
)) {
667 g_array_set_size(string_field
->buf
, new_length
+ 1);
670 data
= string_field
->buf
->data
;
671 memcpy(data
+ string_field
->length
, value
, length
);
672 ((char *) string_field
->buf
->data
)[new_length
] = '\0';
673 string_field
->length
= new_length
;
674 bt_field_set_single(field
, true);
678 int bt_private_field_string_clear(struct bt_private_field
*priv_field
)
680 struct bt_field
*field
= (void *) priv_field
;
681 struct bt_field_string
*string_field
= (void *) 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 string_field
->length
= 0;
688 bt_field_set_single(field
, true);
692 uint64_t bt_field_array_get_length(struct bt_field
*field
)
694 struct bt_field_array
*array_field
= (void *) field
;
696 BT_ASSERT_PRE_NON_NULL(field
, "Field");
697 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
698 return array_field
->length
;
701 int bt_private_field_dynamic_array_set_length(
702 struct bt_private_field
*priv_field
, uint64_t length
)
705 struct bt_field
*field
= (void *) priv_field
;
706 struct bt_field_array
*array_field
= (void *) field
;
708 BT_ASSERT_PRE_NON_NULL(field
, "Field");
709 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
710 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
, "Field");
711 BT_ASSERT_PRE_FIELD_HOT(field
, "Field");
713 if (unlikely(length
> array_field
->fields
->len
)) {
715 struct bt_field_class_array
*array_fc
;
716 uint64_t cur_len
= array_field
->fields
->len
;
719 g_ptr_array_set_size(array_field
->fields
, length
);
720 array_fc
= (void *) field
->class;
722 for (i
= cur_len
; i
< array_field
->fields
->len
; i
++) {
723 struct bt_field
*elem_field
= bt_field_create(
724 array_fc
->element_fc
);
727 BT_LIB_LOGE("Cannot create element field for "
728 "dynamic array field: "
729 "index=%" PRIu64
", "
730 "%![array-field-]+f", i
, field
);
735 BT_ASSERT(!array_field
->fields
->pdata
[i
]);
736 array_field
->fields
->pdata
[i
] = elem_field
;
740 array_field
->length
= length
;
746 struct bt_field
*bt_field_array_borrow_element_field_by_index(
747 struct bt_field
*field
, uint64_t index
)
749 struct bt_field_array
*array_field
= (void *) field
;
751 BT_ASSERT_PRE_NON_NULL(field
, "Field");
752 BT_ASSERT_PRE_FIELD_IS_ARRAY(field
, "Field");
753 BT_ASSERT_PRE_VALID_INDEX(index
, array_field
->length
);
754 return array_field
->fields
->pdata
[index
];
757 struct bt_private_field
*
758 bt_private_field_array_borrow_element_field_by_index(
759 struct bt_private_field
*field
, uint64_t index
)
761 return (void *) bt_field_array_borrow_element_field_by_index(
762 (void *) field
, index
);
765 struct bt_field
*bt_field_structure_borrow_member_field_by_index(
766 struct bt_field
*field
, uint64_t index
)
768 struct bt_field_structure
*struct_field
= (void *) field
;
770 BT_ASSERT_PRE_NON_NULL(field
, "Field");
771 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
772 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
773 BT_ASSERT_PRE_VALID_INDEX(index
, struct_field
->fields
->len
);
774 return struct_field
->fields
->pdata
[index
];
777 struct bt_private_field
*
778 bt_private_field_structure_borrow_member_field_by_index(
779 struct bt_private_field
*field
, uint64_t index
)
781 return (void *) bt_field_structure_borrow_member_field_by_index(
782 (void *) field
, index
);
785 struct bt_field
*bt_field_structure_borrow_member_field_by_name(
786 struct bt_field
*field
, const char *name
)
788 struct bt_field
*ret_field
= NULL
;
789 struct bt_field_class_structure
*struct_fc
;
790 struct bt_field_structure
*struct_field
= (void *) field
;
794 BT_ASSERT_PRE_NON_NULL(field
, "Field");
795 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
796 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
797 BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field");
798 struct_fc
= (void *) field
->class;
800 if (!g_hash_table_lookup_extended(struct_fc
->common
.name_to_index
, name
,
801 &orig_key
, &index
)) {
805 ret_field
= struct_field
->fields
->pdata
[GPOINTER_TO_UINT(index
)];
806 BT_ASSERT(ret_field
);
812 struct bt_private_field
*
813 bt_private_field_structure_borrow_member_field_by_name(
814 struct bt_private_field
*field
, const char *name
)
816 return (void *) bt_field_structure_borrow_member_field_by_name(
817 (void *) field
, name
);
820 struct bt_field
*bt_field_variant_borrow_selected_option_field(
821 struct bt_field
*field
)
823 struct bt_field_variant
*var_field
= (void *) field
;
825 BT_ASSERT_PRE_NON_NULL(field
, "Field");
826 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
827 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
828 BT_ASSERT_PRE(var_field
->selected_field
,
829 "Variant field has no selected field: %!+f", field
);
830 return var_field
->selected_field
;
833 struct bt_private_field
*
834 bt_private_field_variant_borrow_selected_option_field(
835 struct bt_private_field
*field
)
837 return (void *) bt_field_variant_borrow_selected_option_field(
841 int bt_private_field_variant_select_option_field(
842 struct bt_private_field
*priv_field
, uint64_t index
)
844 struct bt_field
*field
= (void *) priv_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_FIELD_HOT(field
, "Field");
851 BT_ASSERT_PRE_VALID_INDEX(index
, var_field
->fields
->len
);
852 var_field
->selected_field
= var_field
->fields
->pdata
[index
];
853 var_field
->selected_index
= index
;
857 uint64_t bt_field_variant_get_selected_option_field_index(
858 struct bt_field
*field
)
860 struct bt_field_variant
*var_field
= (void *) field
;
862 BT_ASSERT_PRE_NON_NULL(field
, "Field");
863 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field
,
864 BT_FIELD_CLASS_TYPE_VARIANT
, "Field");
865 BT_ASSERT_PRE(var_field
->selected_field
,
866 "Variant field has no selected field: %!+f", field
);
867 return var_field
->selected_index
;
871 void bt_field_finalize(struct bt_field
*field
)
874 BT_LOGD_STR("Putting field's class.");
875 BT_OBJECT_PUT_REF_AND_RESET(field
->class);
879 void destroy_integer_field(struct bt_field
*field
)
882 BT_LIB_LOGD("Destroying integer field object: %!+f", field
);
883 bt_field_finalize(field
);
888 void destroy_real_field(struct bt_field
*field
)
891 BT_LIB_LOGD("Destroying real field object: %!+f", field
);
892 bt_field_finalize(field
);
897 void destroy_structure_field(struct bt_field
*field
)
899 struct bt_field_structure
*struct_field
= (void *) field
;
902 BT_LIB_LOGD("Destroying structure field object: %!+f", field
);
903 bt_field_finalize(field
);
905 if (struct_field
->fields
) {
906 g_ptr_array_free(struct_field
->fields
, TRUE
);
907 struct_field
->fields
= NULL
;
914 void destroy_variant_field(struct bt_field
*field
)
916 struct bt_field_variant
*var_field
= (void *) field
;
919 BT_LIB_LOGD("Destroying variant field object: %!+f", field
);
920 bt_field_finalize(field
);
922 if (var_field
->fields
) {
923 g_ptr_array_free(var_field
->fields
, TRUE
);
924 var_field
->fields
= NULL
;
931 void destroy_array_field(struct bt_field
*field
)
933 struct bt_field_array
*array_field
= (void *) field
;
936 BT_LIB_LOGD("Destroying array field object: %!+f", field
);
937 bt_field_finalize(field
);
939 if (array_field
->fields
) {
940 g_ptr_array_free(array_field
->fields
, TRUE
);
941 array_field
->fields
= NULL
;
948 void destroy_string_field(struct bt_field
*field
)
950 struct bt_field_string
*string_field
= (void *) field
;
953 BT_LIB_LOGD("Destroying string field object: %!+f", field
);
954 bt_field_finalize(field
);
956 if (string_field
->buf
) {
957 g_array_free(string_field
->buf
, TRUE
);
958 string_field
->buf
= NULL
;
965 void bt_field_destroy(struct bt_field
*field
)
968 BT_ASSERT(bt_field_class_has_known_type(field
->class));
969 field_destroy_funcs
[field
->class->type
](field
);
973 void reset_single_field(struct bt_field
*field
)
976 field
->is_set
= false;
980 void reset_structure_field(struct bt_field
*field
)
983 struct bt_field_structure
*struct_field
= (void *) field
;
987 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
988 bt_field_reset(struct_field
->fields
->pdata
[i
]);
993 void reset_variant_field(struct bt_field
*field
)
996 struct bt_field_variant
*var_field
= (void *) field
;
1000 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1001 bt_field_reset(var_field
->fields
->pdata
[i
]);
1006 void reset_array_field(struct bt_field
*field
)
1009 struct bt_field_array
*array_field
= (void *) field
;
1013 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1014 bt_field_reset(array_field
->fields
->pdata
[i
]);
1019 void set_single_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1021 field
->frozen
= is_frozen
;
1025 void set_structure_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1028 struct bt_field_structure
*struct_field
= (void *) field
;
1030 BT_LIB_LOGD("Setting structure field's frozen state: "
1031 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1033 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1034 struct bt_field
*member_field
= struct_field
->fields
->pdata
[i
];
1036 BT_LIB_LOGD("Setting structure field's member field's "
1037 "frozen state: %![field-]+f, index=%" PRIu64
,
1039 bt_field_set_is_frozen(member_field
, is_frozen
);
1042 set_single_field_is_frozen(field
, is_frozen
);
1046 void set_variant_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1049 struct bt_field_variant
*var_field
= (void *) field
;
1051 BT_LIB_LOGD("Setting variant field's frozen state: "
1052 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1054 for (i
= 0; i
< var_field
->fields
->len
; i
++) {
1055 struct bt_field
*option_field
= var_field
->fields
->pdata
[i
];
1057 BT_LIB_LOGD("Setting variant field's option field's "
1058 "frozen state: %![field-]+f, index=%" PRIu64
,
1060 bt_field_set_is_frozen(option_field
, is_frozen
);
1063 set_single_field_is_frozen(field
, is_frozen
);
1067 void set_array_field_is_frozen(struct bt_field
*field
, bool is_frozen
)
1070 struct bt_field_array
*array_field
= (void *) field
;
1072 BT_LIB_LOGD("Setting array field's frozen state: "
1073 "%![field-]+f, is-frozen=%d", field
, is_frozen
);
1075 for (i
= 0; i
< array_field
->fields
->len
; i
++) {
1076 struct bt_field
*elem_field
= array_field
->fields
->pdata
[i
];
1078 BT_LIB_LOGD("Setting array field's element field's "
1079 "frozen state: %![field-]+f, index=%" PRIu64
,
1081 bt_field_set_is_frozen(elem_field
, is_frozen
);
1084 set_single_field_is_frozen(field
, is_frozen
);
1088 void _bt_field_set_is_frozen(struct bt_field
*field
,
1092 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1094 BT_ASSERT(field
->methods
->set_is_frozen
);
1095 field
->methods
->set_is_frozen(field
, is_frozen
);
1099 bool single_field_is_set(struct bt_field
*field
)
1102 return field
->is_set
;
1106 bool structure_field_is_set(struct bt_field
*field
)
1110 struct bt_field_structure
*struct_field
= (void *) field
;
1114 for (i
= 0; i
< struct_field
->fields
->len
; i
++) {
1115 is_set
= bt_field_is_set(struct_field
->fields
->pdata
[i
]);
1126 bool variant_field_is_set(struct bt_field
*field
)
1128 struct bt_field_variant
*var_field
= (void *) field
;
1129 bool is_set
= false;
1133 if (var_field
->selected_field
) {
1134 is_set
= bt_field_is_set(var_field
->selected_field
);
1141 bool array_field_is_set(struct bt_field
*field
)
1145 struct bt_field_array
*array_field
= (void *) field
;
1149 for (i
= 0; i
< array_field
->length
; i
++) {
1150 is_set
= bt_field_is_set(array_field
->fields
->pdata
[i
]);