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>
44 struct bt_field_common
*bt_field_integer_copy(struct bt_field_common
*src
);
47 struct bt_field_common
*bt_field_enumeration_copy_recursive(
48 struct bt_field_common
*src
);
51 struct bt_field_common
*bt_field_floating_point_copy(
52 struct bt_field_common
*src
);
55 struct bt_field_common
*bt_field_structure_copy_recursive(
56 struct bt_field_common
*src
);
59 struct bt_field_common
*bt_field_variant_copy_recursive(
60 struct bt_field_common
*src
);
63 struct bt_field_common
*bt_field_array_copy_recursive(
64 struct bt_field_common
*src
);
67 struct bt_field_common
*bt_field_sequence_copy_recursive(
68 struct bt_field_common
*src
);
71 struct bt_field_common
*bt_field_string_copy(struct bt_field_common
*src
);
73 static struct bt_field_common_methods bt_field_integer_methods
= {
74 .freeze
= bt_field_common_generic_freeze
,
75 .validate
= bt_field_common_generic_validate
,
76 .copy
= bt_field_integer_copy
,
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_floating_point_methods
= {
82 .freeze
= bt_field_common_generic_freeze
,
83 .validate
= bt_field_common_generic_validate
,
84 .copy
= bt_field_floating_point_copy
,
85 .is_set
= bt_field_common_generic_is_set
,
86 .reset
= bt_field_common_generic_reset
,
89 static struct bt_field_common_methods bt_field_enumeration_methods
= {
90 .freeze
= bt_field_common_enumeration_freeze_recursive
,
91 .validate
= bt_field_common_enumeration_validate_recursive
,
92 .copy
= bt_field_enumeration_copy_recursive
,
93 .is_set
= bt_field_common_enumeration_is_set_recursive
,
94 .reset
= bt_field_common_enumeration_reset_recursive
,
97 static struct bt_field_common_methods bt_field_string_methods
= {
98 .freeze
= bt_field_common_generic_freeze
,
99 .validate
= bt_field_common_generic_validate
,
100 .copy
= bt_field_string_copy
,
101 .is_set
= bt_field_common_generic_is_set
,
102 .reset
= bt_field_common_string_reset
,
105 static struct bt_field_common_methods bt_field_structure_methods
= {
106 .freeze
= bt_field_common_structure_freeze_recursive
,
107 .validate
= bt_field_common_structure_validate_recursive
,
108 .copy
= bt_field_structure_copy_recursive
,
109 .is_set
= bt_field_common_structure_is_set_recursive
,
110 .reset
= bt_field_common_structure_reset_recursive
,
113 static struct bt_field_common_methods bt_field_sequence_methods
= {
114 .freeze
= bt_field_common_sequence_freeze_recursive
,
115 .validate
= bt_field_common_sequence_validate_recursive
,
116 .copy
= bt_field_sequence_copy_recursive
,
117 .is_set
= bt_field_common_sequence_is_set_recursive
,
118 .reset
= bt_field_common_sequence_reset_recursive
,
121 static struct bt_field_common_methods bt_field_array_methods
= {
122 .freeze
= bt_field_common_array_freeze_recursive
,
123 .validate
= bt_field_common_array_validate_recursive
,
124 .copy
= bt_field_array_copy_recursive
,
125 .is_set
= bt_field_common_array_is_set_recursive
,
126 .reset
= bt_field_common_array_reset_recursive
,
129 static struct bt_field_common_methods bt_field_variant_methods
= {
130 .freeze
= bt_field_common_variant_freeze_recursive
,
131 .validate
= bt_field_common_variant_validate_recursive
,
132 .copy
= bt_field_variant_copy_recursive
,
133 .is_set
= bt_field_common_variant_is_set_recursive
,
134 .reset
= bt_field_common_variant_reset_recursive
,
138 struct bt_field
*bt_field_integer_create(struct bt_field_type
*);
141 struct bt_field
*bt_field_enumeration_create(struct bt_field_type
*);
144 struct bt_field
*bt_field_floating_point_create(struct bt_field_type
*);
147 struct bt_field
*bt_field_structure_create(struct bt_field_type
*);
150 struct bt_field
*bt_field_variant_create(struct bt_field_type
*);
153 struct bt_field
*bt_field_array_create(struct bt_field_type
*);
156 struct bt_field
*bt_field_sequence_create(struct bt_field_type
*);
159 struct bt_field
*bt_field_string_create(struct bt_field_type
*);
162 struct bt_field
*(* const field_create_funcs
[])(struct bt_field_type
*) = {
163 [BT_FIELD_TYPE_ID_INTEGER
] = bt_field_integer_create
,
164 [BT_FIELD_TYPE_ID_ENUM
] = bt_field_enumeration_create
,
165 [BT_FIELD_TYPE_ID_FLOAT
] = bt_field_floating_point_create
,
166 [BT_FIELD_TYPE_ID_STRUCT
] = bt_field_structure_create
,
167 [BT_FIELD_TYPE_ID_VARIANT
] = bt_field_variant_create
,
168 [BT_FIELD_TYPE_ID_ARRAY
] = bt_field_array_create
,
169 [BT_FIELD_TYPE_ID_SEQUENCE
] = bt_field_sequence_create
,
170 [BT_FIELD_TYPE_ID_STRING
] = bt_field_string_create
,
173 struct bt_field
*bt_field_create(struct bt_field_type
*type
)
175 struct bt_field
*field
= NULL
;
176 enum bt_field_type_id type_id
;
178 BT_ASSERT_PRE_NON_NULL(type
, "Field type");
179 BT_ASSERT(field_type_common_has_known_id((void *) type
));
180 BT_ASSERT_PRE(bt_field_type_common_validate((void *) type
) == 0,
181 "Field type is invalid: %!+F", type
);
182 type_id
= bt_field_type_get_type_id(type
);
183 field
= field_create_funcs
[type_id
](type
);
188 bt_field_type_freeze(type
);
194 struct bt_field_type
*bt_field_get_type(struct bt_field
*field
)
196 return (void *) bt_field_common_get_type((void *) field
);
199 enum bt_field_type_id
bt_field_get_type_id(struct bt_field
*field
)
201 struct bt_field_common
*field_common
= (void *) field
;
203 BT_ASSERT_PRE_NON_NULL(field
, "Field");
204 return field_common
->type
->id
;
207 bt_bool
bt_field_is_integer(struct bt_field
*field
)
209 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_INTEGER
;
212 bt_bool
bt_field_is_floating_point(struct bt_field
*field
)
214 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_FLOAT
;
217 bt_bool
bt_field_is_enumeration(struct bt_field
*field
)
219 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_ENUM
;
222 bt_bool
bt_field_is_string(struct bt_field
*field
)
224 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_STRING
;
227 bt_bool
bt_field_is_structure(struct bt_field
*field
)
229 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_STRUCT
;
232 bt_bool
bt_field_is_array(struct bt_field
*field
)
234 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_ARRAY
;
237 bt_bool
bt_field_is_sequence(struct bt_field
*field
)
239 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_SEQUENCE
;
242 bt_bool
bt_field_is_variant(struct bt_field
*field
)
244 return bt_field_get_type_id(field
) == BT_FIELD_TYPE_ID_VARIANT
;
248 int64_t bt_field_sequence_get_int_length(struct bt_field
*field
)
250 return bt_field_common_sequence_get_int_length((void *) field
);
253 struct bt_field
*bt_field_sequence_get_length(struct bt_field
*field
)
255 return (void *) bt_field_common_sequence_get_length((void *) field
);
258 int bt_field_sequence_set_length(struct bt_field
*field
,
259 struct bt_field
*length_field
)
261 return bt_field_common_sequence_set_length((void *) field
,
262 (void *) length_field
);
265 struct bt_field
*bt_field_structure_get_field_by_index(
266 struct bt_field
*field
, uint64_t index
)
268 return (void *) bt_field_common_structure_get_field_by_index(
269 (void *) field
, index
);
272 struct bt_field
*bt_field_structure_get_field_by_name(
273 struct bt_field
*field
, const char *name
)
275 return (void *) bt_field_common_structure_get_field_by_name(
276 (void *) field
, name
);
279 int bt_field_structure_set_field_by_name(struct bt_field_common
*field
,
280 const char *name
, struct bt_field_common
*value
)
282 return bt_field_common_structure_set_field_by_name((void *) field
,
283 name
, (void *) value
);
286 struct bt_field
*bt_field_array_get_field(
287 struct bt_field
*field
, uint64_t index
)
289 return (void *) bt_field_common_array_get_field((void *) field
, index
,
290 (bt_field_common_create_func
) bt_field_create
);
293 struct bt_field
*bt_field_sequence_get_field(
294 struct bt_field
*field
, uint64_t index
)
296 return (void *) bt_field_common_sequence_get_field((void *) field
,
297 index
, (bt_field_common_create_func
) bt_field_create
);
300 struct bt_field
*bt_field_variant_get_field(struct bt_field
*field
,
301 struct bt_field
*tag_field
)
303 return (void *) bt_field_common_variant_get_field((void *) field
,
305 (bt_field_common_create_func
) bt_field_create
);
308 struct bt_field
*bt_field_variant_get_current_field(
309 struct bt_field
*variant_field
)
311 return (void *) bt_field_common_variant_get_current_field(
312 (void *) variant_field
);
315 struct bt_field_common
*bt_field_variant_get_tag(
316 struct bt_field_common
*variant_field
)
318 return (void *) bt_field_common_variant_get_tag((void *) variant_field
);
321 struct bt_field
*bt_field_enumeration_get_container(struct bt_field
*field
)
323 return (void *) bt_field_common_enumeration_get_container(
324 (void *) field
, (bt_field_common_create_func
) bt_field_create
);
327 struct bt_field_type_enumeration_mapping_iterator
*
328 bt_field_enumeration_get_mappings(struct bt_field
*field
)
330 return bt_field_common_enumeration_get_mappings((void *) field
,
331 (bt_field_common_create_func
) bt_field_create
);
334 int bt_field_integer_signed_get_value(struct bt_field
*field
, int64_t *value
)
336 return bt_field_common_integer_signed_get_value((void *) field
, value
);
339 int bt_field_integer_signed_set_value(struct bt_field
*field
,
342 return bt_field_common_integer_signed_set_value((void *) field
, value
);
345 int bt_field_integer_unsigned_get_value(struct bt_field
*field
,
348 return bt_field_common_integer_unsigned_get_value((void *) field
,
352 int bt_field_integer_unsigned_set_value(struct bt_field
*field
, uint64_t value
)
354 return bt_field_common_integer_unsigned_set_value((void *) field
, value
);
357 int bt_field_floating_point_get_value(struct bt_field
*field
,
360 return bt_field_common_floating_point_get_value((void *) field
, value
);
363 int bt_field_floating_point_set_value(struct bt_field
*field
,
366 return bt_field_common_floating_point_set_value((void *) field
, value
);
369 const char *bt_field_string_get_value(struct bt_field
*field
)
371 return bt_field_common_string_get_value((void *) field
);
374 int bt_field_string_set_value(struct bt_field
*field
, const char *value
)
376 return bt_field_common_string_set_value((void *) field
, value
);
379 int bt_field_string_append(struct bt_field
*field
, const char *value
)
381 return bt_field_common_string_append((void *) field
, value
);
384 int bt_field_string_append_len(struct bt_field
*field
,
385 const char *value
, unsigned int length
)
387 return bt_field_common_string_append_len((void *) field
, value
, length
);
391 struct bt_field_common
*bt_field_common_copy(struct bt_field_common
*field
)
393 struct bt_field_common
*copy
= NULL
;
395 BT_ASSERT_PRE_NON_NULL(field
, "Field");
396 BT_ASSERT(field_type_common_has_known_id(field
->type
));
397 BT_ASSERT(field
->methods
->copy
);
398 copy
= field
->methods
->copy(field
);
400 BT_LOGW("Cannot create field: ft-addr=%p", field
->type
);
404 bt_field_common_set(copy
, field
->payload_set
);
410 struct bt_field
*bt_field_copy(struct bt_field
*field
)
412 return (void *) bt_field_common_copy((void *) field
);
415 static void bt_field_common_finalize(struct bt_field_common
*field
)
418 BT_LOGD_STR("Putting field's type.");
423 void bt_field_common_integer_destroy(struct bt_object
*obj
)
425 struct bt_field_common_integer
*integer
= (void *) obj
;
428 bt_field_common_finalize(BT_TO_COMMON(integer
));
429 BT_LOGD("Destroying integer field object: addr=%p", obj
);
434 void bt_field_common_enumeration_destroy_recursive(struct bt_object
*obj
)
436 struct bt_field_common_enumeration
*enumeration
= (void *) obj
;
438 BT_ASSERT(enumeration
);
439 bt_field_common_finalize(BT_TO_COMMON(enumeration
));
440 BT_LOGD("Destroying enumeration field object: addr=%p", obj
);
441 BT_LOGD_STR("Putting payload field.");
442 bt_put(enumeration
->payload
);
447 void bt_field_common_floating_point_destroy(struct bt_object
*obj
)
449 struct bt_field_common_floating_point
*floating_point
= (void *) obj
;
452 bt_field_common_finalize(BT_TO_COMMON(floating_point
));
453 BT_LOGD("Destroying floating point number field object: addr=%p", obj
);
458 void bt_field_common_structure_destroy_recursive(struct bt_object
*obj
)
460 struct bt_field_common_structure
*structure
= (void *) obj
;
463 bt_field_common_finalize(BT_TO_COMMON(structure
));
464 BT_LOGD("Destroying structure field object: addr=%p", obj
);
465 g_ptr_array_free(structure
->fields
, TRUE
);
470 void bt_field_common_variant_destroy_recursive(struct bt_object
*obj
)
472 struct bt_field_common_variant
*variant
= (void *) obj
;
475 bt_field_common_finalize(BT_TO_COMMON(variant
));
476 BT_LOGD("Destroying variant field object: addr=%p", obj
);
477 BT_LOGD_STR("Putting tag field.");
478 bt_put(variant
->tag
);
479 BT_LOGD_STR("Putting payload field.");
480 bt_put(variant
->payload
);
485 void bt_field_common_array_destroy_recursive(struct bt_object
*obj
)
487 struct bt_field_common_array
*array
= (void *) obj
;
490 bt_field_common_finalize(BT_TO_COMMON(array
));
491 BT_LOGD("Destroying array field object: addr=%p", obj
);
492 g_ptr_array_free(array
->elements
, TRUE
);
497 void bt_field_common_sequence_destroy_recursive(struct bt_object
*obj
)
499 struct bt_field_common_sequence
*sequence
= (void *) obj
;
502 bt_field_common_finalize(BT_TO_COMMON(sequence
));
503 BT_LOGD("Destroying sequence field object: addr=%p", obj
);
505 if (sequence
->elements
) {
506 g_ptr_array_free(sequence
->elements
, TRUE
);
509 BT_LOGD_STR("Putting length field.");
510 bt_put(sequence
->length
);
515 void bt_field_common_string_destroy(struct bt_object
*obj
)
517 struct bt_field_common_string
*string
= (void *) obj
;
520 bt_field_common_finalize(BT_TO_COMMON(string
));
521 BT_LOGD("Destroying string field object: addr=%p", obj
);
523 if (string
->payload
) {
524 g_string_free(string
->payload
, TRUE
);
531 struct bt_field
*bt_field_integer_create(struct bt_field_type
*type
)
533 struct bt_field_common_integer
*integer
=
534 g_new0(struct bt_field_common_integer
, 1);
536 BT_LOGD("Creating integer field object: ft-addr=%p", type
);
539 bt_field_common_initialize(BT_TO_COMMON(integer
), (void *) type
,
540 bt_field_common_integer_destroy
,
541 &bt_field_integer_methods
);
542 BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
545 BT_LOGE_STR("Failed to allocate one integer field.");
548 return (void *) integer
;
552 struct bt_field
*bt_field_enumeration_create(struct bt_field_type
*type
)
554 struct bt_field_common_enumeration
*enumeration
= g_new0(
555 struct bt_field_common_enumeration
, 1);
557 BT_LOGD("Creating enumeration field object: ft-addr=%p", type
);
560 bt_field_common_initialize(BT_TO_COMMON(enumeration
),
562 bt_field_common_enumeration_destroy_recursive
,
563 &bt_field_enumeration_methods
);
564 BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
567 BT_LOGE_STR("Failed to allocate one enumeration field.");
570 return (void *) enumeration
;
574 struct bt_field
*bt_field_floating_point_create(struct bt_field_type
*type
)
576 struct bt_field_common_floating_point
*floating_point
;
578 BT_LOGD("Creating floating point number field object: ft-addr=%p", type
);
579 floating_point
= g_new0(struct bt_field_common_floating_point
, 1);
581 if (floating_point
) {
582 bt_field_common_initialize(BT_TO_COMMON(floating_point
),
584 bt_field_common_floating_point_destroy
,
585 &bt_field_floating_point_methods
);
586 BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
587 floating_point
, type
);
589 BT_LOGE_STR("Failed to allocate one floating point number field.");
592 return (void *) floating_point
;
596 int bt_field_common_structure_initialize(struct bt_field_common
*field
,
597 struct bt_field_type_common
*type
,
598 bt_object_release_func release_func
,
599 struct bt_field_common_methods
*methods
,
600 bt_field_common_create_func field_create_func
)
603 struct bt_field_type_common_structure
*structure_type
=
604 BT_FROM_COMMON(type
);
605 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
608 BT_LOGD("Initializing common structure field object: ft-addr=%p", type
);
609 bt_field_common_initialize(field
, type
, release_func
, methods
);
610 structure
->fields
= g_ptr_array_new_with_free_func(
611 (GDestroyNotify
) bt_put
);
612 g_ptr_array_set_size(structure
->fields
, structure_type
->fields
->len
);
614 /* Create all fields contained by the structure field. */
615 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
616 struct bt_field_common
*field
;
617 struct structure_field_common
*struct_field
=
618 g_ptr_array_index(structure_type
->fields
, i
);
620 field
= field_create_func(struct_field
->type
);
622 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
623 g_quark_to_string(struct_field
->name
), i
);
628 g_ptr_array_index(structure
->fields
, i
) = field
;
631 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
639 struct bt_field
*bt_field_structure_create(struct bt_field_type
*type
)
641 struct bt_field_common_structure
*structure
= g_new0(
642 struct bt_field_common_structure
, 1);
645 BT_LOGD("Creating structure field object: ft-addr=%p", type
);
648 BT_LOGE_STR("Failed to allocate one structure field.");
652 iret
= bt_field_common_structure_initialize(BT_TO_COMMON(structure
),
653 (void *) type
, bt_field_common_structure_destroy_recursive
,
654 &bt_field_structure_methods
,
655 (bt_field_common_create_func
) bt_field_create
);
661 BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
665 return (void *) structure
;
669 struct bt_field
*bt_field_variant_create(struct bt_field_type
*type
)
671 struct bt_field_common_variant
*variant
= g_new0(
672 struct bt_field_common_variant
, 1);
674 BT_LOGD("Creating variant field object: ft-addr=%p", type
);
677 bt_field_common_initialize(BT_TO_COMMON(variant
),
679 bt_field_common_variant_destroy_recursive
,
680 &bt_field_variant_methods
);
681 BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
684 BT_LOGE_STR("Failed to allocate one variant field.");
687 return (void *) variant
;
691 int bt_field_common_array_initialize(struct bt_field_common
*field
,
692 struct bt_field_type_common
*type
,
693 bt_object_release_func release_func
,
694 struct bt_field_common_methods
*methods
)
696 struct bt_field_type_common_array
*array_type
= BT_FROM_COMMON(type
);
697 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
698 unsigned int array_length
;
701 BT_LOGD("Initializing common array field object: ft-addr=%p", type
);
703 bt_field_common_initialize(field
, type
, release_func
, methods
);
704 array_length
= array_type
->length
;
705 array
->elements
= g_ptr_array_sized_new(array_length
);
706 if (!array
->elements
) {
711 g_ptr_array_set_free_func(array
->elements
, (GDestroyNotify
) bt_put
);
712 g_ptr_array_set_size(array
->elements
, array_length
);
713 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
721 struct bt_field
*bt_field_array_create(struct bt_field_type
*type
)
723 struct bt_field_common_array
*array
=
724 g_new0(struct bt_field_common_array
, 1);
727 BT_LOGD("Creating array field object: ft-addr=%p", type
);
731 BT_LOGE_STR("Failed to allocate one array field.");
735 ret
= bt_field_common_array_initialize(BT_TO_COMMON(array
),
737 bt_field_common_array_destroy_recursive
,
738 &bt_field_array_methods
);
744 BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
748 return (void *) array
;
752 struct bt_field
*bt_field_sequence_create(struct bt_field_type
*type
)
754 struct bt_field_common_sequence
*sequence
= g_new0(
755 struct bt_field_common_sequence
, 1);
757 BT_LOGD("Creating sequence field object: ft-addr=%p", type
);
760 bt_field_common_initialize(BT_TO_COMMON(sequence
),
762 bt_field_common_sequence_destroy_recursive
,
763 &bt_field_sequence_methods
);
764 BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
767 BT_LOGE_STR("Failed to allocate one sequence field.");
770 return (void *) sequence
;
774 struct bt_field
*bt_field_string_create(struct bt_field_type
*type
)
776 struct bt_field_common_string
*string
= g_new0(
777 struct bt_field_common_string
, 1);
779 BT_LOGD("Creating string field object: ft-addr=%p", type
);
782 bt_field_common_initialize(BT_TO_COMMON(string
),
784 bt_field_common_string_destroy
,
785 &bt_field_string_methods
);
786 BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
789 BT_LOGE_STR("Failed to allocate one string field.");
792 return (void *) string
;
796 int bt_field_common_generic_validate(struct bt_field_common
*field
)
798 return (field
&& field
->payload_set
) ? 0 : -1;
802 int bt_field_common_enumeration_validate_recursive(
803 struct bt_field_common
*field
)
806 struct bt_field_common_enumeration
*enumeration
= BT_FROM_COMMON(field
);
810 if (!enumeration
->payload
) {
811 BT_ASSERT_PRE_MSG("Invalid enumeration field: payload is not set: "
817 ret
= bt_field_common_validate_recursive(enumeration
->payload
);
824 int bt_field_common_structure_validate_recursive(struct bt_field_common
*field
)
828 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
832 for (i
= 0; i
< structure
->fields
->len
; i
++) {
833 ret
= bt_field_common_validate_recursive(
834 (void *) structure
->fields
->pdata
[i
]);
840 this_ret
= bt_field_type_common_structure_get_field_by_index(
841 field
->type
, &name
, NULL
, i
);
842 BT_ASSERT(this_ret
== 0);
843 BT_ASSERT_PRE_MSG("Invalid structure field's field: "
844 "%![struct-field-]+_f, field-name=\"%s\", "
845 "index=%" PRId64
", %![field-]+_f",
846 field
, name
, i
, structure
->fields
->pdata
[i
]);
856 int bt_field_common_variant_validate_recursive(struct bt_field_common
*field
)
859 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
862 ret
= bt_field_common_validate_recursive(variant
->payload
);
864 BT_ASSERT_PRE_MSG("Invalid variant field's payload field: "
865 "%![variant-field-]+_f, %![payload-field-]+_f",
866 field
, variant
->payload
);
873 int bt_field_common_array_validate_recursive(struct bt_field_common
*field
)
877 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
881 for (i
= 0; i
< array
->elements
->len
; i
++) {
882 ret
= bt_field_common_validate_recursive((void *) array
->elements
->pdata
[i
]);
884 BT_ASSERT_PRE_MSG("Invalid array field's element field: "
885 "%![array-field-]+_f, " PRId64
", "
886 "%![elem-field-]+_f",
887 field
, i
, array
->elements
->pdata
[i
]);
897 int bt_field_common_sequence_validate_recursive(struct bt_field_common
*field
)
901 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
905 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
906 ret
= bt_field_common_validate_recursive(
907 (void *) sequence
->elements
->pdata
[i
]);
909 BT_ASSERT_PRE_MSG("Invalid sequence field's element field: "
910 "%![seq-field-]+_f, " PRId64
", "
911 "%![elem-field-]+_f",
912 field
, i
, sequence
->elements
->pdata
[i
]);
921 void bt_field_common_generic_reset(struct bt_field_common
*field
)
924 field
->payload_set
= false;
928 void bt_field_common_enumeration_reset_recursive(struct bt_field_common
*field
)
930 struct bt_field_common_enumeration
*enumeration
= BT_FROM_COMMON(field
);
934 if (!enumeration
->payload
) {
938 bt_field_common_reset_recursive(enumeration
->payload
);
942 void bt_field_common_structure_reset_recursive(struct bt_field_common
*field
)
945 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
949 for (i
= 0; i
< structure
->fields
->len
; i
++) {
950 struct bt_field_common
*member
= structure
->fields
->pdata
[i
];
954 * Structure members are lazily initialized;
955 * skip if this member has not been allocated
961 bt_field_common_reset_recursive(member
);
966 void bt_field_common_variant_reset_recursive(struct bt_field_common
*field
)
968 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
971 BT_PUT(variant
->tag
);
972 BT_PUT(variant
->payload
);
976 void bt_field_common_array_reset_recursive(struct bt_field_common
*field
)
979 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
983 for (i
= 0; i
< array
->elements
->len
; i
++) {
984 struct bt_field_common
*member
= array
->elements
->pdata
[i
];
988 * Array elements are lazily initialized; skip
989 * if this member has not been allocated yet.
994 bt_field_common_reset_recursive(member
);
999 void bt_field_common_sequence_reset_recursive(struct bt_field_common
*field
)
1001 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
1005 if (sequence
->elements
) {
1006 g_ptr_array_free(sequence
->elements
, TRUE
);
1007 sequence
->elements
= NULL
;
1010 BT_PUT(sequence
->length
);
1014 void bt_field_common_string_reset(struct bt_field_common
*field
)
1016 struct bt_field_common_string
*string
= BT_FROM_COMMON(field
);
1019 bt_field_common_generic_reset(field
);
1021 if (string
->payload
) {
1022 g_string_truncate(string
->payload
, 0);
1027 struct bt_field_common
*bt_field_integer_copy(struct bt_field_common
*src
)
1029 struct bt_field_common_integer
*integer_src
= (void *) src
;
1030 struct bt_field_common_integer
*integer_dst
;
1032 BT_LOGD("Copying integer field: src-field-addr=%p", src
);
1033 integer_dst
= (void *) bt_field_create((void *) src
->type
);
1038 integer_dst
->payload
= integer_src
->payload
;
1039 BT_LOGD_STR("Copied integer field.");
1042 return BT_TO_COMMON(integer_dst
);
1046 struct bt_field_common
*bt_field_enumeration_copy_recursive(
1047 struct bt_field_common
*src
)
1049 struct bt_field_common_enumeration
*enum_src
= BT_FROM_COMMON(src
);
1050 struct bt_field_common_enumeration
*enum_dst
;
1052 BT_LOGD("Copying enumeration field: src-field-addr=%p", src
);
1053 enum_dst
= (void *) bt_field_create((void *) src
->type
);
1058 if (enum_src
->payload
) {
1059 BT_LOGD_STR("Copying enumeration field's payload field.");
1060 enum_dst
->payload
= (void *)
1061 bt_field_copy((void *) enum_src
->payload
);
1062 if (!enum_dst
->payload
) {
1063 BT_LOGE_STR("Cannot copy enumeration field's payload field.");
1068 BT_LOGD_STR("Copied enumeration field.");
1075 return BT_TO_COMMON(enum_dst
);
1079 struct bt_field_common
*bt_field_floating_point_copy(
1080 struct bt_field_common
*src
)
1082 struct bt_field_common_floating_point
*float_src
= BT_FROM_COMMON(src
);
1083 struct bt_field_common_floating_point
*float_dst
;
1085 BT_LOGD("Copying floating point number field: src-field-addr=%p", src
);
1086 float_dst
= (void *) bt_field_create((void *) src
->type
);
1091 float_dst
->payload
= float_src
->payload
;
1092 BT_LOGD_STR("Copied floating point number field.");
1095 return BT_TO_COMMON(float_dst
);
1099 struct bt_field_common
*bt_field_structure_copy_recursive(
1100 struct bt_field_common
*src
)
1103 struct bt_field_common_structure
*struct_src
= BT_FROM_COMMON(src
);
1104 struct bt_field_common_structure
*struct_dst
;
1106 BT_LOGD("Copying structure field: src-field-addr=%p", src
);
1107 struct_dst
= (void *) bt_field_create((void *) src
->type
);
1112 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1114 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1115 struct bt_field_common
*field
=
1116 g_ptr_array_index(struct_src
->fields
, i
);
1117 struct bt_field_common
*field_copy
= NULL
;
1120 BT_LOGD("Copying structure field's field: src-field-addr=%p"
1121 "index=%" PRId64
, field
, i
);
1122 field_copy
= (void *) bt_field_copy((void *) field
);
1124 BT_LOGE("Cannot copy structure field's field: "
1125 "src-field-addr=%p, index=%" PRId64
,
1131 BT_MOVE(g_ptr_array_index(struct_dst
->fields
, i
), field_copy
);
1134 BT_LOGD_STR("Copied structure field.");
1141 return BT_TO_COMMON(struct_dst
);
1145 struct bt_field_common
*bt_field_variant_copy_recursive(
1146 struct bt_field_common
*src
)
1148 struct bt_field_common_variant
*variant_src
= BT_FROM_COMMON(src
);
1149 struct bt_field_common_variant
*variant_dst
;
1151 BT_LOGD("Copying variant field: src-field-addr=%p", src
);
1152 variant_dst
= (void *) bt_field_create((void *) src
->type
);
1157 if (variant_src
->tag
) {
1158 BT_LOGD_STR("Copying variant field's tag field.");
1159 variant_dst
->tag
= (void *) bt_field_copy(
1160 (void *) variant_src
->tag
);
1161 if (!variant_dst
->tag
) {
1162 BT_LOGE_STR("Cannot copy variant field's tag field.");
1166 if (variant_src
->payload
) {
1167 BT_LOGD_STR("Copying variant field's payload field.");
1168 variant_dst
->payload
= (void *) bt_field_copy(
1169 (void *) variant_src
->payload
);
1170 if (!variant_dst
->payload
) {
1171 BT_LOGE_STR("Cannot copy variant field's payload field.");
1176 BT_LOGD_STR("Copied variant field.");
1180 BT_PUT(variant_dst
);
1183 return BT_TO_COMMON(variant_dst
);
1187 struct bt_field_common
*bt_field_array_copy_recursive(
1188 struct bt_field_common
*src
)
1191 struct bt_field_common_array
*array_src
= BT_FROM_COMMON(src
);
1192 struct bt_field_common_array
*array_dst
;
1194 BT_LOGD("Copying array field: src-field-addr=%p", src
);
1195 array_dst
= (void *) bt_field_create((void *) src
->type
);
1200 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
1201 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1202 struct bt_field_common
*field
=
1203 g_ptr_array_index(array_src
->elements
, i
);
1204 struct bt_field_common
*field_copy
= NULL
;
1207 BT_LOGD("Copying array field's element field: field-addr=%p, "
1208 "index=%" PRId64
, field
, i
);
1209 field_copy
= (void *) bt_field_copy((void *) field
);
1211 BT_LOGE("Cannot copy array field's element field: "
1212 "src-field-addr=%p, index=%" PRId64
,
1218 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
1221 BT_LOGD_STR("Copied array field.");
1228 return BT_TO_COMMON(array_dst
);
1232 struct bt_field_common
*bt_field_sequence_copy_recursive(
1233 struct bt_field_common
*src
)
1237 struct bt_field_common_sequence
*sequence_src
= BT_FROM_COMMON(src
);
1238 struct bt_field_common_sequence
*sequence_dst
;
1239 struct bt_field_common
*src_length
;
1240 struct bt_field_common
*dst_length
;
1242 BT_LOGD("Copying sequence field: src-field-addr=%p", src
);
1243 sequence_dst
= (void *) bt_field_create((void *) src
->type
);
1244 if (!sequence_dst
) {
1248 src_length
= bt_field_common_sequence_get_length(src
);
1250 /* no length set yet: keep destination sequence empty */
1254 /* copy source length */
1255 BT_LOGD_STR("Copying sequence field's length field.");
1256 dst_length
= (void *) bt_field_copy((void *) src_length
);
1259 BT_LOGE_STR("Cannot copy sequence field's length field.");
1263 /* this will initialize the destination sequence's internal array */
1264 ret
= bt_field_common_sequence_set_length(
1265 BT_TO_COMMON(sequence_dst
), dst_length
);
1268 BT_LOGE("Cannot set sequence field copy's length field: "
1269 "dst-length-field-addr=%p", dst_length
);
1273 BT_ASSERT(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
1275 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
1276 struct bt_field_common
*field
=
1277 g_ptr_array_index(sequence_src
->elements
, i
);
1278 struct bt_field_common
*field_copy
= NULL
;
1281 BT_LOGD("Copying sequence field's element field: field-addr=%p, "
1282 "index=%" PRId64
, field
, i
);
1283 field_copy
= (void *) bt_field_copy((void *) field
);
1285 BT_LOGE("Cannot copy sequence field's element field: "
1286 "src-field-addr=%p, index=%" PRId64
,
1292 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
1295 BT_LOGD_STR("Copied sequence field.");
1299 BT_PUT(sequence_dst
);
1302 return BT_TO_COMMON(sequence_dst
);
1306 struct bt_field_common
*bt_field_string_copy(struct bt_field_common
*src
)
1308 struct bt_field_common_string
*string_src
= BT_FROM_COMMON(src
);
1309 struct bt_field_common_string
*string_dst
;
1311 BT_LOGD("Copying string field: src-field-addr=%p", src
);
1312 string_dst
= (void *) bt_field_create((void *) src
->type
);
1317 if (string_src
->payload
) {
1318 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
1319 if (!string_dst
->payload
) {
1320 BT_LOGE_STR("Failed to allocate a GString.");
1325 BT_LOGD_STR("Copied string field.");
1332 return BT_TO_COMMON(string_dst
);
1336 void bt_field_common_generic_freeze(struct bt_field_common
*field
)
1338 field
->frozen
= true;
1342 void bt_field_common_enumeration_freeze_recursive(struct bt_field_common
*field
)
1344 struct bt_field_common_enumeration
*enum_field
= BT_FROM_COMMON(field
);
1346 BT_LOGD("Freezing enumeration field object: addr=%p", field
);
1347 BT_LOGD("Freezing enumeration field object's contained payload field: payload-field-addr=%p", enum_field
->payload
);
1348 bt_field_common_freeze_recursive(enum_field
->payload
);
1349 bt_field_common_generic_freeze(field
);
1353 void bt_field_common_structure_freeze_recursive(struct bt_field_common
*field
)
1356 struct bt_field_common_structure
*structure_field
=
1357 BT_FROM_COMMON(field
);
1359 BT_LOGD("Freezing structure field object: addr=%p", field
);
1361 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
1362 struct bt_field_common
*field
=
1363 g_ptr_array_index(structure_field
->fields
, i
);
1365 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64
,
1367 bt_field_common_freeze_recursive(field
);
1370 bt_field_common_generic_freeze(field
);
1374 void bt_field_common_variant_freeze_recursive(struct bt_field_common
*field
)
1376 struct bt_field_common_variant
*variant_field
= BT_FROM_COMMON(field
);
1378 BT_LOGD("Freezing variant field object: addr=%p", field
);
1379 BT_LOGD("Freezing variant field object's tag field: tag-field-addr=%p", variant_field
->tag
);
1380 bt_field_common_freeze_recursive(variant_field
->tag
);
1381 BT_LOGD("Freezing variant field object's payload field: payload-field-addr=%p", variant_field
->payload
);
1382 bt_field_common_freeze_recursive(variant_field
->payload
);
1383 bt_field_common_generic_freeze(field
);
1387 void bt_field_common_array_freeze_recursive(struct bt_field_common
*field
)
1390 struct bt_field_common_array
*array_field
= BT_FROM_COMMON(field
);
1392 BT_LOGD("Freezing array field object: addr=%p", field
);
1394 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
1395 struct bt_field_common
*elem_field
=
1396 g_ptr_array_index(array_field
->elements
, i
);
1398 BT_LOGD("Freezing array field object's element field: "
1399 "element-field-addr=%p, index=%" PRId64
,
1401 bt_field_common_freeze_recursive(elem_field
);
1404 bt_field_common_generic_freeze(field
);
1408 void bt_field_common_sequence_freeze_recursive(struct bt_field_common
*field
)
1411 struct bt_field_common_sequence
*sequence_field
=
1412 BT_FROM_COMMON(field
);
1414 BT_LOGD("Freezing sequence field object: addr=%p", field
);
1415 BT_LOGD("Freezing sequence field object's length field: length-field-addr=%p",
1416 sequence_field
->length
);
1417 bt_field_common_freeze_recursive(sequence_field
->length
);
1419 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
1420 struct bt_field_common
*elem_field
=
1421 g_ptr_array_index(sequence_field
->elements
, i
);
1423 BT_LOGD("Freezing sequence field object's element field: "
1424 "element-field-addr=%p, index=%" PRId64
,
1426 bt_field_common_freeze_recursive(elem_field
);
1429 bt_field_common_generic_freeze(field
);
1433 void _bt_field_common_freeze_recursive(struct bt_field_common
*field
)
1439 if (field
->frozen
) {
1443 BT_LOGD("Freezing field object: addr=%p", field
);
1444 BT_ASSERT(field_type_common_has_known_id(field
->type
));
1445 BT_ASSERT(field
->methods
->freeze
);
1446 field
->methods
->freeze(field
);
1453 bt_bool
bt_field_common_generic_is_set(struct bt_field_common
*field
)
1455 return field
&& field
->payload_set
;
1459 bt_bool
bt_field_common_enumeration_is_set_recursive(
1460 struct bt_field_common
*field
)
1462 bt_bool is_set
= BT_FALSE
;
1463 struct bt_field_common_enumeration
*enumeration
= BT_FROM_COMMON(field
);
1467 if (!enumeration
->payload
) {
1471 is_set
= bt_field_common_is_set_recursive(enumeration
->payload
);
1478 bt_bool
bt_field_common_structure_is_set_recursive(
1479 struct bt_field_common
*field
)
1481 bt_bool is_set
= BT_FALSE
;
1483 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
1487 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1488 is_set
= bt_field_common_is_set_recursive(
1489 structure
->fields
->pdata
[i
]);
1500 bt_bool
bt_field_common_variant_is_set_recursive(struct bt_field_common
*field
)
1502 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
1505 return bt_field_common_is_set_recursive(variant
->payload
);
1509 bt_bool
bt_field_common_array_is_set_recursive(struct bt_field_common
*field
)
1512 bt_bool is_set
= BT_FALSE
;
1513 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
1517 for (i
= 0; i
< array
->elements
->len
; i
++) {
1518 is_set
= bt_field_common_is_set_recursive(array
->elements
->pdata
[i
]);
1529 bt_bool
bt_field_common_sequence_is_set_recursive(struct bt_field_common
*field
)
1532 bt_bool is_set
= BT_FALSE
;
1533 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
1537 if (!sequence
->elements
) {
1541 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1542 is_set
= bt_field_common_is_set_recursive(
1543 sequence
->elements
->pdata
[i
]);