4 * Babeltrace CTF IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "FIELDS"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/fields-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/compat/fcntl-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/assert-internal.h>
43 #define BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(_field, _name) \
44 BT_ASSERT_PRE((_field)->type->id == BT_FIELD_TYPE_ID_INTEGER || \
45 (_field)->type->id == BT_FIELD_TYPE_ID_ENUM, \
46 _name " is not an integer or an enumeration field: " \
49 static struct bt_field_common_methods bt_field_integer_methods
= {
50 .set_is_frozen
= bt_field_common_generic_set_is_frozen
,
51 .validate
= bt_field_common_generic_validate
,
53 .is_set
= bt_field_common_generic_is_set
,
54 .reset
= bt_field_common_generic_reset
,
57 static struct bt_field_common_methods bt_field_floating_point_methods
= {
58 .set_is_frozen
= bt_field_common_generic_set_is_frozen
,
59 .validate
= bt_field_common_generic_validate
,
61 .is_set
= bt_field_common_generic_is_set
,
62 .reset
= bt_field_common_generic_reset
,
65 static struct bt_field_common_methods bt_field_enumeration_methods
= {
66 .set_is_frozen
= bt_field_common_generic_set_is_frozen
,
67 .validate
= bt_field_common_generic_validate
,
69 .is_set
= bt_field_common_generic_is_set
,
70 .reset
= bt_field_common_generic_reset
,
73 static struct bt_field_common_methods bt_field_string_methods
= {
74 .set_is_frozen
= bt_field_common_generic_set_is_frozen
,
75 .validate
= bt_field_common_generic_validate
,
77 .is_set
= bt_field_common_generic_is_set
,
78 .reset
= bt_field_common_generic_reset
,
81 static struct bt_field_common_methods bt_field_structure_methods
= {
82 .set_is_frozen
= bt_field_common_structure_set_is_frozen_recursive
,
83 .validate
= bt_field_common_structure_validate_recursive
,
85 .is_set
= bt_field_common_structure_is_set_recursive
,
86 .reset
= bt_field_common_structure_reset_recursive
,
89 static struct bt_field_common_methods bt_field_sequence_methods
= {
90 .set_is_frozen
= bt_field_common_sequence_set_is_frozen_recursive
,
91 .validate
= bt_field_common_sequence_validate_recursive
,
93 .is_set
= bt_field_common_sequence_is_set_recursive
,
94 .reset
= bt_field_common_sequence_reset_recursive
,
97 static struct bt_field_common_methods bt_field_array_methods
= {
98 .set_is_frozen
= bt_field_common_array_set_is_frozen_recursive
,
99 .validate
= bt_field_common_array_validate_recursive
,
101 .is_set
= bt_field_common_array_is_set_recursive
,
102 .reset
= bt_field_common_array_reset_recursive
,
105 static struct bt_field_common_methods bt_field_variant_methods
= {
106 .set_is_frozen
= bt_field_common_variant_set_is_frozen_recursive
,
107 .validate
= bt_field_common_variant_validate_recursive
,
109 .is_set
= bt_field_common_variant_is_set_recursive
,
110 .reset
= bt_field_common_variant_reset_recursive
,
114 struct bt_field
*bt_field_integer_create(struct bt_field_type
*);
117 struct bt_field
*bt_field_enumeration_create(struct bt_field_type
*);
120 struct bt_field
*bt_field_floating_point_create(struct bt_field_type
*);
123 struct bt_field
*bt_field_structure_create(struct bt_field_type
*);
126 struct bt_field
*bt_field_variant_create(struct bt_field_type
*);
129 struct bt_field
*bt_field_array_create(struct bt_field_type
*);
132 struct bt_field
*bt_field_sequence_create(struct bt_field_type
*);
135 struct bt_field
*bt_field_string_create(struct bt_field_type
*);
138 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_type
*) = {
139 [BT_FIELD_TYPE_ID_INTEGER
] = bt_field_integer_create
,
140 [BT_FIELD_TYPE_ID_ENUM
] = bt_field_enumeration_create
,
141 [BT_FIELD_TYPE_ID_FLOAT
] = bt_field_floating_point_create
,
142 [BT_FIELD_TYPE_ID_STRUCT
] = bt_field_structure_create
,
143 [BT_FIELD_TYPE_ID_VARIANT
] = bt_field_variant_create
,
144 [BT_FIELD_TYPE_ID_ARRAY
] = bt_field_array_create
,
145 [BT_FIELD_TYPE_ID_SEQUENCE
] = bt_field_sequence_create
,
146 [BT_FIELD_TYPE_ID_STRING
] = bt_field_string_create
,
150 void bt_field_integer_destroy(struct bt_field
*field
);
153 void bt_field_enumeration_destroy(struct bt_field
*field
);
156 void bt_field_floating_point_destroy(struct bt_field
*field
);
159 void bt_field_structure_destroy_recursive(struct bt_field
*field
);
162 void bt_field_variant_destroy_recursive(struct bt_field
*field
);
165 void bt_field_array_destroy_recursive(struct bt_field
*field
);
168 void bt_field_sequence_destroy_recursive(struct bt_field
*field
);
171 void bt_field_string_destroy(struct bt_field
*field
);
174 void (* const field_destroy_funcs
[])(struct bt_field
*) = {
175 [BT_FIELD_TYPE_ID_INTEGER
] = bt_field_integer_destroy
,
176 [BT_FIELD_TYPE_ID_ENUM
] = bt_field_enumeration_destroy
,
177 [BT_FIELD_TYPE_ID_FLOAT
] = bt_field_floating_point_destroy
,
178 [BT_FIELD_TYPE_ID_STRUCT
] = bt_field_structure_destroy_recursive
,
179 [BT_FIELD_TYPE_ID_VARIANT
] = bt_field_variant_destroy_recursive
,
180 [BT_FIELD_TYPE_ID_ARRAY
] = bt_field_array_destroy_recursive
,
181 [BT_FIELD_TYPE_ID_SEQUENCE
] = bt_field_sequence_destroy_recursive
,
182 [BT_FIELD_TYPE_ID_STRING
] = bt_field_string_destroy
,
186 struct bt_field
*bt_field_create_recursive(struct bt_field_type
*type
)
188 struct bt_field
*field
= NULL
;
189 enum bt_field_type_id type_id
;
191 BT_ASSERT_PRE_NON_NULL(type
, "Field type");
192 BT_ASSERT(field_type_common_has_known_id((void *) type
));
193 BT_ASSERT_PRE(bt_field_type_common_validate((void *) type
) == 0,
194 "Field type is invalid: %!+F", type
);
195 type_id
= bt_field_type_get_type_id(type
);
196 field
= field_create_funcs
[type_id
](type
);
201 bt_field_type_freeze(type
);
207 struct bt_field_type
*bt_field_borrow_type(struct bt_field
*field
)
209 return (void *) bt_field_common_borrow_type((void *) field
);
212 enum bt_field_type_id
bt_field_get_type_id(struct bt_field
*field
)
214 struct bt_field_common
*field_common
= (void *) field
;
216 BT_ASSERT_PRE_NON_NULL(field
, "Field");
217 return field_common
->type
->id
;
220 int64_t bt_field_sequence_get_length(struct bt_field
*field
)
222 return bt_field_common_sequence_get_length((void *) field
);
225 int bt_field_sequence_set_length(struct bt_field
*field
, uint64_t length
)
227 return bt_field_common_sequence_set_length((void *) field
,
228 length
, (bt_field_common_create_func
) bt_field_create_recursive
);
231 struct bt_field
*bt_field_structure_borrow_field_by_index(
232 struct bt_field
*field
, uint64_t index
)
234 return (void *) bt_field_common_structure_borrow_field_by_index(
235 (void *) field
, index
);
238 struct bt_field
*bt_field_structure_borrow_field_by_name(
239 struct bt_field
*field
, const char *name
)
241 return (void *) bt_field_common_structure_borrow_field_by_name(
242 (void *) field
, name
);
245 struct bt_field
*bt_field_array_borrow_field(
246 struct bt_field
*field
, uint64_t index
)
248 return (void *) bt_field_common_array_borrow_field((void *) field
,
252 struct bt_field
*bt_field_sequence_borrow_field(
253 struct bt_field
*field
, uint64_t index
)
255 return (void *) bt_field_common_sequence_borrow_field((void *) field
,
259 struct bt_field
*bt_field_variant_borrow_current_field(
260 struct bt_field
*variant_field
)
262 return (void *) bt_field_common_variant_borrow_current_field(
263 (void *) variant_field
);
266 int bt_field_variant_set_tag_signed(struct bt_field
*variant_field
,
269 return bt_field_variant_common_set_tag((void *) variant_field
,
270 (uint64_t) tag
, true);
273 int bt_field_variant_set_tag_unsigned(struct bt_field
*variant_field
,
276 return bt_field_variant_common_set_tag((void *) variant_field
,
277 (uint64_t) tag
, false);
280 int bt_field_variant_get_tag_signed(struct bt_field
*variant_field
,
283 return bt_field_common_variant_get_tag_signed((void *) variant_field
, tag
);
286 int bt_field_variant_get_tag_unsigned(struct bt_field
*variant_field
,
289 return bt_field_common_variant_get_tag_unsigned((void *) variant_field
, tag
);
292 struct bt_field_type_enumeration_mapping_iterator
*
293 bt_field_enumeration_get_mappings(struct bt_field
*field
)
295 struct bt_field_enumeration
*enum_field
= (void *) field
;
297 BT_ASSERT_PRE_NON_NULL(field
, "Enumeration field");
298 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID((struct bt_field_common
*) field
,
299 BT_FIELD_TYPE_ID_ENUM
, "Field");
300 BT_ASSERT_PRE_FIELD_COMMON_IS_SET((struct bt_field_common
*) field
,
301 "Enumeration field");
302 return bt_field_common_enumeration_get_mappings((void *) field
,
303 (bt_field_common_create_func
) bt_field_create_recursive
,
304 enum_field
->common
.payload
.unsignd
);
309 struct bt_field_type_common_integer
*get_int_enum_int_ft(
310 struct bt_field
*field
)
312 struct bt_field_common_integer
*int_field
= (void *) field
;
313 struct bt_field_type_common_integer
*int_ft
= NULL
;
315 if (int_field
->common
.type
->id
== BT_FIELD_TYPE_ID_INTEGER
) {
316 int_ft
= BT_FROM_COMMON(int_field
->common
.type
);
317 } else if (int_field
->common
.type
->id
== BT_FIELD_TYPE_ID_ENUM
) {
318 struct bt_field_type_common_enumeration
*enum_ft
=
319 BT_FROM_COMMON(int_field
->common
.type
);
320 int_ft
= enum_ft
->container_ft
;
327 int bt_field_integer_signed_get_value(struct bt_field
*field
, int64_t *value
)
329 struct bt_field_common_integer
*integer
= (void *) field
;
331 BT_ASSERT_PRE_NON_NULL(field
, "Integer/enumeration field");
332 BT_ASSERT_PRE_NON_NULL(value
, "Value");
333 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer
),
334 "Integer/enumeration field");
335 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer
), "Field");
336 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
337 BT_TO_COMMON(get_int_enum_int_ft(field
))),
338 "Field's type is unsigned: %!+f", field
);
339 *value
= integer
->payload
.signd
;
343 int bt_field_integer_signed_set_value(struct bt_field
*field
, int64_t value
)
346 struct bt_field_common_integer
*integer
= (void *) field
;
348 BT_ASSERT_PRE_NON_NULL(field
, "Integer field");
349 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer
), "Integer field");
350 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer
), "Field");
351 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
352 BT_FROM_COMMON(get_int_enum_int_ft(field
))),
353 "Field's type is unsigned: %!+f", field
);
354 BT_ASSERT_PRE(value_is_in_range_signed(
355 get_int_enum_int_ft(field
)->size
, value
),
356 "Value is out of bounds: value=%" PRId64
", %![field-]+f",
358 integer
->payload
.signd
= value
;
359 bt_field_set(field
, true);
363 int bt_field_integer_unsigned_get_value(struct bt_field
*field
, uint64_t *value
)
365 struct bt_field_common_integer
*integer
= (void *) field
;
367 BT_ASSERT_PRE_NON_NULL(field
, "Integer field");
368 BT_ASSERT_PRE_NON_NULL(value
, "Value");
369 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer
), "Integer field");
370 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer
), "Field");
371 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
372 BT_FROM_COMMON(get_int_enum_int_ft(field
))),
373 "Field's type is signed: %!+f", field
);
374 *value
= integer
->payload
.unsignd
;
378 int bt_field_integer_unsigned_set_value(struct bt_field
*field
,
381 struct bt_field_common_integer
*integer
= (void *) field
;
383 BT_ASSERT_PRE_NON_NULL(field
, "Integer field");
384 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer
), "Integer field");
385 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer
), "Field");
386 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
387 BT_FROM_COMMON(get_int_enum_int_ft(field
))),
388 "Field's type is signed: %!+f", field
);
389 BT_ASSERT_PRE(value_is_in_range_unsigned(
390 get_int_enum_int_ft(field
)->size
, value
),
391 "Value is out of bounds: value=%" PRIu64
", %![field-]+f",
393 integer
->payload
.unsignd
= value
;
394 bt_field_set(field
, true);
398 int bt_field_floating_point_get_value(struct bt_field
*field
,
401 return bt_field_common_floating_point_get_value((void *) field
, value
);
404 int bt_field_floating_point_set_value(struct bt_field
*field
,
407 return bt_field_common_floating_point_set_value((void *) field
, value
);
410 const char *bt_field_string_get_value(struct bt_field
*field
)
412 return bt_field_common_string_get_value((void *) field
);
415 int bt_field_string_set_value(struct bt_field
*field
, const char *value
)
417 return bt_field_common_string_set_value((void *) field
, value
);
420 int bt_field_string_append(struct bt_field
*field
, const char *value
)
422 return bt_field_common_string_append((void *) field
, value
);
425 int bt_field_string_append_len(struct bt_field
*field
,
426 const char *value
, unsigned int length
)
428 return bt_field_common_string_append_len((void *) field
, value
, length
);
431 int bt_field_string_clear(struct bt_field
*string_field
)
433 return bt_field_common_string_clear((void *) string_field
);
437 struct bt_field_common
*bt_field_common_copy(struct bt_field_common
*field
)
439 struct bt_field_common
*copy
= NULL
;
441 BT_ASSERT_PRE_NON_NULL(field
, "Field");
442 BT_ASSERT(field_type_common_has_known_id(field
->type
));
443 BT_ASSERT(field
->methods
->copy
);
444 copy
= field
->methods
->copy(field
);
446 BT_LOGW("Cannot create field: ft-addr=%p", field
->type
);
450 bt_field_common_set(copy
, field
->payload_set
);
457 void bt_field_integer_destroy(struct bt_field
*field
)
459 BT_LOGD("Destroying integer field object: addr=%p", field
);
460 bt_field_common_integer_finalize((void *) field
);
465 void bt_field_floating_point_destroy(struct bt_field
*field
)
467 BT_LOGD("Destroying floating point field object: addr=%p", field
);
468 bt_field_common_floating_point_finalize((void *) field
);
473 void bt_field_enumeration_destroy(struct bt_field
*field
)
475 BT_LOGD("Destroying enumeration field object: addr=%p", field
);
476 bt_field_common_finalize((void *) field
);
481 void bt_field_structure_destroy_recursive(struct bt_field
*field
)
483 BT_LOGD("Destroying structure field object: addr=%p", field
);
484 bt_field_common_structure_finalize_recursive((void *) field
);
489 void bt_field_variant_destroy_recursive(struct bt_field
*field
)
491 BT_LOGD("Destroying variant field object: addr=%p", field
);
492 bt_field_common_variant_finalize_recursive((void *) field
);
497 void bt_field_array_destroy_recursive(struct bt_field
*field
)
499 BT_LOGD("Destroying array field object: addr=%p", field
);
500 bt_field_common_array_finalize_recursive((void *) field
);
505 void bt_field_sequence_destroy_recursive(struct bt_field
*field
)
507 BT_LOGD("Destroying sequence field object: addr=%p", field
);
508 bt_field_common_sequence_finalize_recursive((void *) field
);
513 void bt_field_string_destroy(struct bt_field
*field
)
515 BT_LOGD("Destroying string field object: addr=%p", field
);
516 bt_field_common_string_finalize((void *) field
);
521 void bt_field_destroy_recursive(struct bt_field
*field
)
523 struct bt_field_common
*field_common
= (void *) field
;
529 BT_ASSERT(field_type_common_has_known_id((void *) field_common
->type
));
530 field_destroy_funcs
[field_common
->type
->id
](field
);
534 struct bt_field
*bt_field_integer_create(struct bt_field_type
*type
)
536 struct bt_field_common_integer
*integer
=
537 g_new0(struct bt_field_common_integer
, 1);
539 BT_LOGD("Creating integer field object: ft-addr=%p", type
);
542 bt_field_common_initialize(BT_TO_COMMON(integer
), (void *) type
,
543 false, NULL
, &bt_field_integer_methods
);
544 BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
547 BT_LOGE_STR("Failed to allocate one integer field.");
550 return (void *) integer
;
554 struct bt_field
*bt_field_enumeration_create(struct bt_field_type
*type
)
556 struct bt_field_enumeration
*enumeration
= g_new0(
557 struct bt_field_enumeration
, 1);
559 BT_LOGD("Creating enumeration field object: ft-addr=%p", type
);
562 bt_field_common_initialize(
563 BT_TO_COMMON(BT_TO_COMMON(enumeration
)),
564 (void *) type
, false, NULL
,
565 &bt_field_enumeration_methods
);
566 BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
569 BT_LOGE_STR("Failed to allocate one enumeration field.");
572 return (void *) enumeration
;
576 struct bt_field
*bt_field_floating_point_create(struct bt_field_type
*type
)
578 struct bt_field_common_floating_point
*floating_point
;
580 BT_LOGD("Creating floating point number field object: ft-addr=%p", type
);
581 floating_point
= g_new0(struct bt_field_common_floating_point
, 1);
583 if (floating_point
) {
584 bt_field_common_initialize(BT_TO_COMMON(floating_point
),
585 (void *) type
, false, NULL
,
586 &bt_field_floating_point_methods
);
587 BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
588 floating_point
, type
);
590 BT_LOGE_STR("Failed to allocate one floating point number field.");
593 return (void *) floating_point
;
597 int bt_field_common_structure_initialize(struct bt_field_common
*field
,
598 struct bt_field_type_common
*type
,
599 bool is_shared
, bt_object_release_func release_func
,
600 struct bt_field_common_methods
*methods
,
601 bt_field_common_create_func field_create_func
,
602 GDestroyNotify field_release_func
)
605 struct bt_field_type_common_structure
*structure_type
=
606 BT_FROM_COMMON(type
);
607 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
610 BT_LOGD("Initializing common structure field object: ft-addr=%p", type
);
611 bt_field_common_initialize(field
, type
, is_shared
,
612 release_func
, methods
);
613 structure
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
614 g_ptr_array_set_size(structure
->fields
, structure_type
->fields
->len
);
616 /* Create all fields contained in the structure field. */
617 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
618 struct bt_field_common
*field
;
619 struct bt_field_type_common_structure_field
*struct_field
=
620 BT_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
622 field
= field_create_func(struct_field
->type
);
624 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
625 g_quark_to_string(struct_field
->name
), i
);
630 g_ptr_array_index(structure
->fields
, i
) = field
;
633 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
641 struct bt_field
*bt_field_structure_create(struct bt_field_type
*type
)
643 struct bt_field_common_structure
*structure
= g_new0(
644 struct bt_field_common_structure
, 1);
647 BT_LOGD("Creating structure field object: ft-addr=%p", type
);
650 BT_LOGE_STR("Failed to allocate one structure field.");
654 iret
= bt_field_common_structure_initialize(BT_TO_COMMON(structure
),
655 (void *) type
, false, NULL
, &bt_field_structure_methods
,
656 (bt_field_common_create_func
) bt_field_create_recursive
,
657 (GDestroyNotify
) bt_field_destroy_recursive
);
663 BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
667 return (void *) structure
;
671 int bt_field_common_variant_initialize(struct bt_field_common
*field
,
672 struct bt_field_type_common
*type
,
673 bool is_shared
, bt_object_release_func release_func
,
674 struct bt_field_common_methods
*methods
,
675 bt_field_common_create_func field_create_func
,
676 GDestroyNotify field_release_func
)
679 struct bt_field_type_common_variant
*variant_type
=
680 BT_FROM_COMMON(type
);
681 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
684 BT_LOGD("Initializing common variant field object: ft-addr=%p", type
);
685 bt_field_common_initialize(field
, type
, is_shared
,
686 release_func
, methods
);
687 ret
= bt_field_type_common_variant_update_choices(type
);
689 BT_LOGE("Cannot update common variant field type choices: "
694 variant
->fields
= g_ptr_array_new_with_free_func(field_release_func
);
695 g_ptr_array_set_size(variant
->fields
, variant_type
->choices
->len
);
697 /* Create all fields contained in the variant field. */
698 for (i
= 0; i
< variant_type
->choices
->len
; i
++) {
699 struct bt_field_common
*field
;
700 struct bt_field_type_common_variant_choice
*var_choice
=
701 BT_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
704 field
= field_create_func(var_choice
->type
);
706 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
707 g_quark_to_string(var_choice
->name
), i
);
712 g_ptr_array_index(variant
->fields
, i
) = field
;
715 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
723 int bt_field_common_string_initialize(struct bt_field_common
*field
,
724 struct bt_field_type_common
*type
,
725 bool is_shared
, bt_object_release_func release_func
,
726 struct bt_field_common_methods
*methods
)
729 struct bt_field_common_string
*string
= BT_FROM_COMMON(field
);
731 BT_LOGD("Initializing common string field object: ft-addr=%p", type
);
732 bt_field_common_initialize(field
, type
, is_shared
,
733 release_func
, methods
);
734 string
->buf
= g_array_sized_new(FALSE
, FALSE
, sizeof(char), 1);
740 g_array_index(string
->buf
, char, 0) = '\0';
741 BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
749 struct bt_field
*bt_field_variant_create(struct bt_field_type
*type
)
751 struct bt_field_common_variant
*variant
= g_new0(
752 struct bt_field_common_variant
, 1);
755 BT_LOGD("Creating variant field object: ft-addr=%p", type
);
758 BT_LOGE_STR("Failed to allocate one variant field.");
762 iret
= bt_field_common_variant_initialize(BT_TO_COMMON(variant
),
763 (void *) type
, false, NULL
, &bt_field_variant_methods
,
764 (bt_field_common_create_func
) bt_field_create_recursive
,
765 (GDestroyNotify
) bt_field_destroy_recursive
);
771 BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
775 return (void *) variant
;
779 int bt_field_common_array_initialize(struct bt_field_common
*field
,
780 struct bt_field_type_common
*type
,
781 bool is_shared
, bt_object_release_func release_func
,
782 struct bt_field_common_methods
*methods
,
783 bt_field_common_create_func field_create_func
,
784 GDestroyNotify field_destroy_func
)
786 struct bt_field_type_common_array
*array_type
= BT_FROM_COMMON(type
);
787 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
788 unsigned int array_length
;
792 BT_LOGD("Initializing common array field object: ft-addr=%p", type
);
794 bt_field_common_initialize(field
, type
, is_shared
,
795 release_func
, methods
);
796 array_length
= array_type
->length
;
797 array
->elements
= g_ptr_array_sized_new(array_length
);
798 if (!array
->elements
) {
803 g_ptr_array_set_free_func(array
->elements
, field_destroy_func
);
804 g_ptr_array_set_size(array
->elements
, array_length
);
806 for (i
= 0; i
< array_length
; i
++) {
807 array
->elements
->pdata
[i
] = field_create_func(
808 array_type
->element_ft
);
809 if (!array
->elements
->pdata
[i
]) {
815 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
823 struct bt_field
*bt_field_array_create(struct bt_field_type
*type
)
825 struct bt_field_common_array
*array
=
826 g_new0(struct bt_field_common_array
, 1);
829 BT_LOGD("Creating array field object: ft-addr=%p", type
);
833 BT_LOGE_STR("Failed to allocate one array field.");
837 ret
= bt_field_common_array_initialize(BT_TO_COMMON(array
),
838 (void *) type
, false, NULL
, &bt_field_array_methods
,
839 (bt_field_common_create_func
) bt_field_create_recursive
,
840 (GDestroyNotify
) bt_field_destroy_recursive
);
846 BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
850 return (void *) array
;
854 int bt_field_common_sequence_initialize(struct bt_field_common
*field
,
855 struct bt_field_type_common
*type
,
856 bool is_shared
, bt_object_release_func release_func
,
857 struct bt_field_common_methods
*methods
,
858 GDestroyNotify field_destroy_func
)
860 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
863 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type
);
865 bt_field_common_initialize(field
, type
, is_shared
,
866 release_func
, methods
);
867 sequence
->elements
= g_ptr_array_new();
868 if (!sequence
->elements
) {
873 g_ptr_array_set_free_func(sequence
->elements
, field_destroy_func
);
874 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
882 struct bt_field
*bt_field_sequence_create(struct bt_field_type
*type
)
884 struct bt_field_common_sequence
*sequence
=
885 g_new0(struct bt_field_common_sequence
, 1);
888 BT_LOGD("Creating sequence field object: ft-addr=%p", type
);
892 BT_LOGE_STR("Failed to allocate one sequence field.");
896 ret
= bt_field_common_sequence_initialize(BT_TO_COMMON(sequence
),
897 (void *) type
, false, NULL
, &bt_field_sequence_methods
,
898 (GDestroyNotify
) bt_field_destroy_recursive
);
904 BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
908 return (void *) sequence
;
912 struct bt_field
*bt_field_string_create(struct bt_field_type
*type
)
914 struct bt_field_common_string
*string
= g_new0(
915 struct bt_field_common_string
, 1);
917 BT_LOGD("Creating string field object: ft-addr=%p", type
);
920 bt_field_common_string_initialize(BT_TO_COMMON(string
),
921 (void *) type
, false, NULL
, &bt_field_string_methods
);
922 BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
925 BT_LOGE_STR("Failed to allocate one string field.");
928 return (void *) string
;
932 int bt_field_common_generic_validate(struct bt_field_common
*field
)
934 return (field
&& field
->payload_set
) ? 0 : -1;
938 int bt_field_common_structure_validate_recursive(struct bt_field_common
*field
)
942 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
946 for (i
= 0; i
< structure
->fields
->len
; i
++) {
947 ret
= bt_field_common_validate_recursive(
948 (void *) structure
->fields
->pdata
[i
]);
954 this_ret
= bt_field_type_common_structure_borrow_field_by_index(
955 field
->type
, &name
, NULL
, i
);
956 BT_ASSERT(this_ret
== 0);
957 BT_ASSERT_PRE_MSG("Invalid structure field's field: "
958 "%![struct-field-]+_f, field-name=\"%s\", "
959 "index=%" PRId64
", %![field-]+_f",
960 field
, name
, i
, structure
->fields
->pdata
[i
]);
970 int bt_field_common_variant_validate_recursive(struct bt_field_common
*field
)
973 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
977 if (!variant
->current_field
) {
982 ret
= bt_field_common_validate_recursive(variant
->current_field
);
989 int bt_field_common_array_validate_recursive(struct bt_field_common
*field
)
993 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
997 for (i
= 0; i
< array
->elements
->len
; i
++) {
998 ret
= bt_field_common_validate_recursive((void *) array
->elements
->pdata
[i
]);
1000 BT_ASSERT_PRE_MSG("Invalid array field's element field: "
1001 "%![array-field-]+_f, " PRId64
", "
1002 "%![elem-field-]+_f",
1003 field
, i
, array
->elements
->pdata
[i
]);
1013 int bt_field_common_sequence_validate_recursive(struct bt_field_common
*field
)
1017 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
1021 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1022 ret
= bt_field_common_validate_recursive(
1023 (void *) sequence
->elements
->pdata
[i
]);
1025 BT_ASSERT_PRE_MSG("Invalid sequence field's element field: "
1026 "%![seq-field-]+_f, " PRId64
", "
1027 "%![elem-field-]+_f",
1028 field
, i
, sequence
->elements
->pdata
[i
]);
1037 void bt_field_common_generic_reset(struct bt_field_common
*field
)
1040 field
->payload_set
= false;
1044 void bt_field_common_structure_reset_recursive(struct bt_field_common
*field
)
1047 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
1051 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1052 struct bt_field_common
*member
= structure
->fields
->pdata
[i
];
1056 * Structure members are lazily initialized;
1057 * skip if this member has not been allocated
1063 bt_field_common_reset_recursive(member
);
1068 void bt_field_common_variant_reset_recursive(struct bt_field_common
*field
)
1070 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
1073 variant
->current_field
= NULL
;
1077 void bt_field_common_array_reset_recursive(struct bt_field_common
*field
)
1080 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
1084 for (i
= 0; i
< array
->elements
->len
; i
++) {
1085 struct bt_field_common
*member
= array
->elements
->pdata
[i
];
1089 * Array elements are lazily initialized; skip
1090 * if this member has not been allocated yet.
1095 bt_field_common_reset_recursive(member
);
1100 void bt_field_common_sequence_reset_recursive(struct bt_field_common
*field
)
1102 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
1107 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1108 if (sequence
->elements
->pdata
[i
]) {
1109 bt_field_common_reset_recursive(
1110 sequence
->elements
->pdata
[i
]);
1114 sequence
->length
= 0;
1118 void bt_field_common_generic_set_is_frozen(struct bt_field_common
*field
,
1121 field
->frozen
= is_frozen
;
1125 void bt_field_common_structure_set_is_frozen_recursive(
1126 struct bt_field_common
*field
, bool is_frozen
)
1129 struct bt_field_common_structure
*structure_field
=
1130 BT_FROM_COMMON(field
);
1132 BT_LOGD("Freezing structure field object: addr=%p", field
);
1134 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
1135 struct bt_field_common
*struct_field
=
1136 g_ptr_array_index(structure_field
->fields
, i
);
1138 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64
,
1140 bt_field_common_set_is_frozen_recursive(struct_field
,
1144 bt_field_common_generic_set_is_frozen(field
, is_frozen
);
1148 void bt_field_common_variant_set_is_frozen_recursive(
1149 struct bt_field_common
*field
, bool is_frozen
)
1152 struct bt_field_common_variant
*variant_field
= BT_FROM_COMMON(field
);
1154 BT_LOGD("Freezing variant field object: addr=%p", field
);
1156 for (i
= 0; i
< variant_field
->fields
->len
; i
++) {
1157 struct bt_field_common
*var_field
=
1158 g_ptr_array_index(variant_field
->fields
, i
);
1160 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64
,
1162 bt_field_common_set_is_frozen_recursive(var_field
, is_frozen
);
1165 bt_field_common_generic_set_is_frozen(field
, is_frozen
);
1169 void bt_field_common_array_set_is_frozen_recursive(
1170 struct bt_field_common
*field
, bool is_frozen
)
1173 struct bt_field_common_array
*array_field
= BT_FROM_COMMON(field
);
1175 BT_LOGD("Freezing array field object: addr=%p", field
);
1177 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
1178 struct bt_field_common
*elem_field
=
1179 g_ptr_array_index(array_field
->elements
, i
);
1181 BT_LOGD("Freezing array field object's element field: "
1182 "element-field-addr=%p, index=%" PRId64
,
1184 bt_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
1187 bt_field_common_generic_set_is_frozen(field
, is_frozen
);
1191 void bt_field_common_sequence_set_is_frozen_recursive(
1192 struct bt_field_common
*field
, bool is_frozen
)
1195 struct bt_field_common_sequence
*sequence_field
=
1196 BT_FROM_COMMON(field
);
1198 BT_LOGD("Freezing sequence field object: addr=%p", field
);
1200 for (i
= 0; i
< sequence_field
->length
; i
++) {
1201 struct bt_field_common
*elem_field
=
1202 g_ptr_array_index(sequence_field
->elements
, i
);
1204 BT_LOGD("Freezing sequence field object's element field: "
1205 "element-field-addr=%p, index=%" PRId64
,
1207 bt_field_common_set_is_frozen_recursive(elem_field
, is_frozen
);
1210 bt_field_common_generic_set_is_frozen(field
, is_frozen
);
1214 void _bt_field_common_set_is_frozen_recursive(struct bt_field_common
*field
,
1221 if (field
->frozen
) {
1225 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
1227 BT_ASSERT(field_type_common_has_known_id(field
->type
));
1228 BT_ASSERT(field
->methods
->set_is_frozen
);
1229 field
->methods
->set_is_frozen(field
, is_frozen
);
1236 bt_bool
bt_field_common_generic_is_set(struct bt_field_common
*field
)
1238 return field
&& field
->payload_set
;
1242 bt_bool
bt_field_common_structure_is_set_recursive(
1243 struct bt_field_common
*field
)
1245 bt_bool is_set
= BT_FALSE
;
1247 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
1251 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1252 is_set
= bt_field_common_is_set_recursive(
1253 structure
->fields
->pdata
[i
]);
1264 bt_bool
bt_field_common_variant_is_set_recursive(struct bt_field_common
*field
)
1266 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
1267 bt_bool is_set
= BT_FALSE
;
1271 if (variant
->current_field
) {
1272 is_set
= bt_field_common_is_set_recursive(
1273 variant
->current_field
);
1280 bt_bool
bt_field_common_array_is_set_recursive(struct bt_field_common
*field
)
1283 bt_bool is_set
= BT_FALSE
;
1284 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
1288 for (i
= 0; i
< array
->elements
->len
; i
++) {
1289 is_set
= bt_field_common_is_set_recursive(array
->elements
->pdata
[i
]);
1300 bt_bool
bt_field_common_sequence_is_set_recursive(struct bt_field_common
*field
)
1303 bt_bool is_set
= BT_FALSE
;
1304 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
1308 if (!sequence
->elements
) {
1312 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1313 is_set
= bt_field_common_is_set_recursive(
1314 sequence
->elements
->pdata
[i
]);