4 * Babeltrace CTF IR - Event Types
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 "FIELD-TYPES"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/field-types-internal.h>
34 #include <babeltrace/ctf-ir/field-path-internal.h>
35 #include <babeltrace/ctf-ir/fields-internal.h>
36 #include <babeltrace/ctf-ir/fields.h>
37 #include <babeltrace/ctf-ir/utils.h>
38 #include <babeltrace/ctf-ir/utils-internal.h>
39 #include <babeltrace/ref.h>
40 #include <babeltrace/ctf-ir/clock-class.h>
41 #include <babeltrace/ctf-ir/clock-class-internal.h>
42 #include <babeltrace/object-internal.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/endian-internal.h>
46 #include <babeltrace/assert-internal.h>
52 struct bt_field_type
*bt_field_type_integer_copy(
53 struct bt_field_type
*ft
);
56 struct bt_field_type
*bt_field_type_enumeration_copy_recursive(
57 struct bt_field_type
*ft
);
60 struct bt_field_type
*bt_field_type_floating_point_copy(
61 struct bt_field_type
*ft
);
64 struct bt_field_type
*bt_field_type_structure_copy_recursive(
65 struct bt_field_type
*ft
);
68 struct bt_field_type
*bt_field_type_variant_copy_recursive(
69 struct bt_field_type
*ft
);
72 struct bt_field_type
*bt_field_type_array_copy_recursive(
73 struct bt_field_type
*ft
);
76 struct bt_field_type
*bt_field_type_sequence_copy_recursive(
77 struct bt_field_type
*type
);
80 struct bt_field_type
*bt_field_type_string_copy(struct bt_field_type
*ft
);
83 int bt_field_type_integer_validate(struct bt_field_type
*ft
);
86 int bt_field_type_enumeration_validate_recursive(
87 struct bt_field_type
*ft
);
89 int bt_field_type_sequence_validate_recursive(
90 struct bt_field_type
*ft
);
92 int bt_field_type_array_validate_recursive(
93 struct bt_field_type
*ft
);
95 int bt_field_type_structure_validate_recursive(
96 struct bt_field_type
*ft
);
98 int bt_field_type_variant_validate_recursive(
99 struct bt_field_type
*ft
);
102 void bt_field_type_generic_freeze(struct bt_field_type
*ft
);
105 void bt_field_type_enumeration_freeze_recursive(
106 struct bt_field_type
*ft
);
108 void bt_field_type_structure_freeze_recursive(
109 struct bt_field_type
*ft
);
111 void bt_field_type_variant_freeze_recursive(
112 struct bt_field_type
*ft
);
114 void bt_field_type_array_freeze_recursive(
115 struct bt_field_type
*ft
);
117 void bt_field_type_sequence_freeze_recursive(
118 struct bt_field_type
*ft
);
121 void bt_field_type_integer_set_byte_order(
122 struct bt_field_type
*ft
, enum bt_byte_order byte_order
);
125 void bt_field_type_enumeration_set_byte_order_recursive(
126 struct bt_field_type
*ft
, enum bt_byte_order byte_order
);
129 void bt_field_type_floating_point_set_byte_order(
130 struct bt_field_type
*ft
, enum bt_byte_order byte_order
);
133 void bt_field_type_structure_set_byte_order_recursive(
134 struct bt_field_type
*ft
,
135 enum bt_byte_order byte_order
);
137 void bt_field_type_variant_set_byte_order_recursive(
138 struct bt_field_type
*ft
,
139 enum bt_byte_order byte_order
);
142 void bt_field_type_array_set_byte_order_recursive(
143 struct bt_field_type
*ft
,
144 enum bt_byte_order byte_order
);
147 void bt_field_type_sequence_set_byte_order_recursive(
148 struct bt_field_type
*ft
,
149 enum bt_byte_order byte_order
);
152 int bt_field_type_integer_compare(struct bt_field_type
*ft_a
,
153 struct bt_field_type
*ft_b
);
156 int bt_field_type_floating_point_compare(
157 struct bt_field_type
*ft_a
,
158 struct bt_field_type
*ft_b
);
161 int bt_field_type_enumeration_compare_recursive(
162 struct bt_field_type
*ft_a
,
163 struct bt_field_type
*ft_b
);
166 int bt_field_type_string_compare(struct bt_field_type
*ft_a
,
167 struct bt_field_type
*ft_b
);
170 int bt_field_type_structure_compare_recursive(
171 struct bt_field_type
*ft_a
,
172 struct bt_field_type
*ft_b
);
175 int bt_field_type_variant_compare_recursive(
176 struct bt_field_type
*ft_a
,
177 struct bt_field_type
*ft_b
);
180 int bt_field_type_array_compare_recursive(
181 struct bt_field_type
*ft_a
,
182 struct bt_field_type
*ft_b
);
185 int bt_field_type_sequence_compare_recursive(
186 struct bt_field_type
*ft_a
,
187 struct bt_field_type
*ft_b
);
189 static struct bt_field_type_methods bt_field_type_integer_methods
= {
190 .freeze
= bt_field_type_generic_freeze
,
191 .validate
= bt_field_type_integer_validate
,
192 .set_byte_order
= bt_field_type_integer_set_byte_order
,
193 .copy
= (bt_field_type_method_copy
)
194 bt_field_type_integer_copy
,
195 .compare
= bt_field_type_integer_compare
,
198 static struct bt_field_type_methods bt_field_type_floating_point_methods
= {
199 .freeze
= bt_field_type_generic_freeze
,
201 .set_byte_order
= bt_field_type_floating_point_set_byte_order
,
202 .copy
= (bt_field_type_method_copy
)
203 bt_field_type_floating_point_copy
,
204 .compare
= bt_field_type_floating_point_compare
,
207 static struct bt_field_type_methods bt_field_type_enumeration_methods
= {
208 .freeze
= bt_field_type_enumeration_freeze_recursive
,
209 .validate
= bt_field_type_enumeration_validate_recursive
,
210 .set_byte_order
= bt_field_type_enumeration_set_byte_order_recursive
,
211 .copy
= (bt_field_type_method_copy
)
212 bt_field_type_enumeration_copy_recursive
,
213 .compare
= bt_field_type_enumeration_compare_recursive
,
216 static struct bt_field_type_methods bt_field_type_string_methods
= {
217 .freeze
= bt_field_type_generic_freeze
,
219 .set_byte_order
= NULL
,
220 .copy
= (bt_field_type_method_copy
)
221 bt_field_type_string_copy
,
222 .compare
= bt_field_type_string_compare
,
225 static struct bt_field_type_methods bt_field_type_array_methods
= {
226 .freeze
= bt_field_type_array_freeze_recursive
,
227 .validate
= bt_field_type_array_validate_recursive
,
228 .set_byte_order
= bt_field_type_array_set_byte_order_recursive
,
229 .copy
= (bt_field_type_method_copy
)
230 bt_field_type_array_copy_recursive
,
231 .compare
= bt_field_type_array_compare_recursive
,
234 static struct bt_field_type_methods bt_field_type_sequence_methods
= {
235 .freeze
= bt_field_type_sequence_freeze_recursive
,
236 .validate
= bt_field_type_sequence_validate_recursive
,
237 .set_byte_order
= bt_field_type_sequence_set_byte_order_recursive
,
238 .copy
= (bt_field_type_method_copy
)
239 bt_field_type_sequence_copy_recursive
,
240 .compare
= bt_field_type_sequence_compare_recursive
,
243 static struct bt_field_type_methods bt_field_type_structure_methods
= {
244 .freeze
= bt_field_type_structure_freeze_recursive
,
245 .validate
= bt_field_type_structure_validate_recursive
,
246 .set_byte_order
= bt_field_type_structure_set_byte_order_recursive
,
247 .copy
= (bt_field_type_method_copy
)
248 bt_field_type_structure_copy_recursive
,
249 .compare
= bt_field_type_structure_compare_recursive
,
252 static struct bt_field_type_methods bt_field_type_variant_methods
= {
253 .freeze
= bt_field_type_variant_freeze_recursive
,
254 .validate
= bt_field_type_variant_validate_recursive
,
255 .set_byte_order
= bt_field_type_variant_set_byte_order_recursive
,
256 .copy
= (bt_field_type_method_copy
)
257 bt_field_type_variant_copy_recursive
,
258 .compare
= bt_field_type_variant_compare_recursive
,
262 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
268 void bt_field_type_initialize(struct bt_field_type
*ft
,
269 bool init_bo
, bt_object_release_func release_func
,
270 struct bt_field_type_methods
*methods
)
272 BT_ASSERT(ft
&& (ft
->id
> BT_FIELD_TYPE_ID_UNKNOWN
) &&
273 (ft
->id
< BT_FIELD_TYPE_ID_NR
));
275 bt_object_init_shared(&ft
->base
, release_func
);
276 ft
->methods
= methods
;
280 const enum bt_byte_order bo
= BT_BYTE_ORDER_NATIVE
;
282 BT_LOGD("Setting initial field type's byte order: bo=%s",
283 bt_common_byte_order_string(bo
));
284 ret
= bt_field_type_set_byte_order(ft
, bo
);
292 void bt_field_type_integer_initialize(
293 struct bt_field_type
*ft
,
294 unsigned int size
, bt_object_release_func release_func
,
295 struct bt_field_type_methods
*methods
)
297 struct bt_field_type_integer
*int_ft
= (void *) ft
;
300 BT_LOGD("Initializing integer field type object: size=%u",
302 ft
->id
= BT_FIELD_TYPE_ID_INTEGER
;
304 int_ft
->base
= BT_INTEGER_BASE_DECIMAL
;
305 int_ft
->encoding
= BT_STRING_ENCODING_NONE
;
306 bt_field_type_initialize(ft
, true, release_func
, methods
);
307 BT_LOGD("Initialized integer field type object: addr=%p, size=%u",
312 void bt_field_type_floating_point_initialize(
313 struct bt_field_type
*ft
,
314 bt_object_release_func release_func
,
315 struct bt_field_type_methods
*methods
)
317 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
319 BT_LOGD_STR("Initializing floating point number field type object.");
320 ft
->id
= BT_FIELD_TYPE_ID_FLOAT
;
321 flt_ft
->exp_dig
= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
322 flt_ft
->mant_dig
= FLT_MANT_DIG
;
323 bt_field_type_initialize(ft
, true, release_func
, methods
);
324 BT_LOGD("Initialized floating point number field type object: addr=%p, "
325 "exp-size=%u, mant-size=%u", ft
, flt_ft
->exp_dig
,
330 void bt_field_type_enumeration_initialize(
331 struct bt_field_type
*ft
,
332 struct bt_field_type
*container_ft
,
333 bt_object_release_func release_func
,
334 struct bt_field_type_methods
*methods
)
336 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
338 BT_ASSERT(container_ft
);
339 BT_LOGD("Initializing enumeration field type object: int-ft-addr=%p",
341 ft
->id
= BT_FIELD_TYPE_ID_ENUM
;
342 enum_ft
->container_ft
= bt_get(container_ft
);
343 enum_ft
->entries
= g_ptr_array_new_with_free_func(
344 (GDestroyNotify
) destroy_enumeration_mapping
);
345 bt_field_type_initialize(ft
, false, release_func
, methods
);
346 BT_LOGD("Initialized enumeration field type object: addr=%p, "
347 "int-ft-addr=%p, int-ft-size=%u", ft
, container_ft
,
348 bt_field_type_integer_get_size(container_ft
));
352 void bt_field_type_string_initialize(
353 struct bt_field_type
*ft
,
354 bt_object_release_func release_func
,
355 struct bt_field_type_methods
*methods
)
357 struct bt_field_type_string
*string_ft
= (void *) ft
;
359 BT_LOGD_STR("Initializing string field type object.");
360 ft
->id
= BT_FIELD_TYPE_ID_STRING
;
361 bt_field_type_initialize(ft
, true, release_func
, methods
);
362 string_ft
->encoding
= BT_STRING_ENCODING_UTF8
;
363 ft
->alignment
= CHAR_BIT
;
364 BT_LOGD("Initialized string field type object: addr=%p", ft
);
368 void bt_field_type_structure_initialize(
369 struct bt_field_type
*ft
,
370 bt_object_release_func release_func
,
371 struct bt_field_type_methods
*methods
)
373 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
375 BT_LOGD_STR("Initializing structure field type object.");
376 ft
->id
= BT_FIELD_TYPE_ID_STRUCT
;
377 struct_ft
->fields
= g_array_new(FALSE
, TRUE
,
378 sizeof(struct bt_field_type_structure_field
));
379 struct_ft
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
380 bt_field_type_initialize(ft
, true, release_func
, methods
);
381 BT_LOGD("Initialized structure field type object: addr=%p", ft
);
385 void bt_field_type_array_initialize(
386 struct bt_field_type
*ft
,
387 struct bt_field_type
*element_ft
,
388 unsigned int length
, bt_object_release_func release_func
,
389 struct bt_field_type_methods
*methods
)
391 struct bt_field_type_array
*array_ft
= (void *) ft
;
393 BT_ASSERT(element_ft
);
394 BT_LOGD("Initializing array field type object: element-ft-addr=%p, "
395 "length=%u", element_ft
, length
);
396 ft
->id
= BT_FIELD_TYPE_ID_ARRAY
;
397 array_ft
->element_ft
= bt_get(element_ft
);
398 array_ft
->length
= length
;
399 bt_field_type_initialize(ft
, false, release_func
, methods
);
400 BT_LOGD("Initialized array field type object: addr=%p, "
401 "element-ft-addr=%p, length=%u", ft
, element_ft
, length
);
405 void bt_field_type_sequence_initialize(
406 struct bt_field_type
*ft
,
407 struct bt_field_type
*element_ft
,
408 const char *length_field_name
,
409 bt_object_release_func release_func
,
410 struct bt_field_type_methods
*methods
)
412 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
414 BT_ASSERT(element_ft
);
415 BT_ASSERT(length_field_name
);
416 BT_ASSERT(bt_identifier_is_valid(length_field_name
));
417 BT_LOGD("Initializing sequence field type object: element-ft-addr=%p, "
418 "length-field-name=\"%s\"", element_ft
, length_field_name
);
419 ft
->id
= BT_FIELD_TYPE_ID_SEQUENCE
;
420 seq_ft
->element_ft
= bt_get(element_ft
);
421 seq_ft
->length_field_name
= g_string_new(length_field_name
);
422 bt_field_type_initialize(ft
, false, release_func
, methods
);
423 BT_LOGD("Initialized sequence field type object: addr=%p, "
424 "element-ft-addr=%p, length-field-name=\"%s\"",
425 ft
, element_ft
, length_field_name
);
429 void bt_field_type_variant_initialize(
430 struct bt_field_type
*ft
,
431 struct bt_field_type
*tag_ft
,
432 const char *tag_name
,
433 bt_object_release_func release_func
,
434 struct bt_field_type_methods
*methods
)
436 struct bt_field_type_variant
*var_ft
= (void *) ft
;
438 BT_ASSERT(!tag_name
|| bt_identifier_is_valid(tag_name
));
439 BT_LOGD("Initializing variant field type object: "
440 "tag-ft-addr=%p, tag-field-name=\"%s\"",
442 ft
->id
= BT_FIELD_TYPE_ID_VARIANT
;
443 var_ft
->tag_name
= g_string_new(tag_name
);
444 var_ft
->choice_name_to_index
= g_hash_table_new(NULL
, NULL
);
445 var_ft
->choices
= g_array_new(FALSE
, TRUE
,
446 sizeof(struct bt_field_type_variant_choice
));
449 var_ft
->tag_ft
= bt_get(tag_ft
);
452 bt_field_type_initialize(ft
, true, release_func
, methods
);
453 /* A variant's alignment is undefined */
455 BT_LOGD("Initialized variant field type object: addr=%p, "
456 "tag-ft-addr=%p, tag-field-name=\"%s\"",
457 ft
, tag_ft
, tag_name
);
461 void bt_field_type_integer_destroy(struct bt_object
*obj
)
463 struct bt_field_type_integer
*ft
= (void *) obj
;
469 BT_LOGD("Destroying integer field type object: addr=%p", ft
);
470 BT_LOGD_STR("Putting mapped clock class.");
471 bt_put(ft
->mapped_clock_class
);
476 void bt_field_type_floating_point_destroy(struct bt_object
*obj
)
478 struct bt_field_type_floating_point
*ft
= (void *) obj
;
484 BT_LOGD("Destroying floating point number field type object: addr=%p", ft
);
489 void bt_field_type_enumeration_destroy_recursive(struct bt_object
*obj
)
491 struct bt_field_type_enumeration
*ft
= (void *) obj
;
497 BT_LOGD("Destroying enumeration field type object: addr=%p", ft
);
498 g_ptr_array_free(ft
->entries
, TRUE
);
499 BT_LOGD_STR("Putting container field type.");
500 bt_put(ft
->container_ft
);
505 void bt_field_type_string_destroy(struct bt_object
*obj
)
507 struct bt_field_type_string
*ft
= (void *) obj
;
513 BT_LOGD("Destroying string field type object: addr=%p", ft
);
518 void bt_field_type_structure_field_finalize(
519 struct bt_field_type_structure_field
*field
)
525 BT_LOGD("Finalizing structure field type's field: "
526 "addr=%p, field-ft-addr=%p, field-name=\"%s\"",
527 field
, field
->type
, g_quark_to_string(field
->name
));
528 BT_LOGD_STR("Putting field type.");
533 void bt_field_type_structure_destroy_recursive(struct bt_object
*obj
)
535 struct bt_field_type_structure
*ft
= (void *) obj
;
542 BT_LOGD("Destroying structure field type object: addr=%p", ft
);
545 for (i
= 0; i
< ft
->fields
->len
; i
++) {
546 bt_field_type_structure_field_finalize(
547 BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
551 g_array_free(ft
->fields
, TRUE
);
554 if (ft
->field_name_to_index
) {
555 g_hash_table_destroy(ft
->field_name_to_index
);
562 void bt_field_type_array_destroy_recursive(struct bt_object
*obj
)
564 struct bt_field_type_array
*ft
= (void *) obj
;
570 BT_LOGD("Destroying array field type object: addr=%p", ft
);
571 BT_LOGD_STR("Putting element field type.");
572 bt_put(ft
->element_ft
);
577 void bt_field_type_sequence_destroy_recursive(struct bt_object
*obj
)
579 struct bt_field_type_sequence
*ft
= (void *) obj
;
585 BT_LOGD("Destroying sequence field type object: addr=%p", ft
);
586 BT_LOGD_STR("Putting element field type.");
587 bt_put(ft
->element_ft
);
588 g_string_free(ft
->length_field_name
, TRUE
);
589 BT_LOGD_STR("Putting length field path.");
590 bt_put(ft
->length_field_path
);
595 void bt_field_type_variant_choice_finalize(
596 struct bt_field_type_variant_choice
*choice
)
602 BT_LOGD("Finalizing variant field type's choice: "
603 "addr=%p, field-ft-addr=%p, field-name=\"%s\"",
604 choice
, choice
->type
, g_quark_to_string(choice
->name
));
605 BT_LOGD_STR("Putting field type.");
606 bt_put(choice
->type
);
608 if (choice
->ranges
) {
609 g_array_free(choice
->ranges
, TRUE
);
614 void bt_field_type_variant_destroy_recursive(struct bt_object
*obj
)
616 struct bt_field_type_variant
*ft
= (void *) obj
;
623 BT_LOGD("Destroying variant field type object: addr=%p", ft
);
626 for (i
= 0; i
< ft
->choices
->len
; i
++) {
627 bt_field_type_variant_choice_finalize(
628 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
632 g_array_free(ft
->choices
, TRUE
);
635 if (ft
->choice_name_to_index
) {
636 g_hash_table_destroy(ft
->choice_name_to_index
);
640 g_string_free(ft
->tag_name
, TRUE
);
643 BT_LOGD_STR("Putting tag field type.");
645 BT_LOGD_STR("Putting tag field path.");
646 bt_put(ft
->tag_field_path
);
650 struct range_overlap_query
{
665 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
666 struct enumeration_mapping
**b
)
668 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
672 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
673 struct enumeration_mapping
**b
)
675 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
679 int add_structure_variant_member(GArray
*members
,
680 GHashTable
*field_name_to_index
,
681 struct bt_field_type
*field_type
, const char *field_name
,
685 GQuark name_quark
= g_quark_from_string(field_name
);
686 struct bt_field_type
**member_ft
;
689 /* Make sure structure does not contain a field of the same name */
690 if (g_hash_table_lookup_extended(field_name_to_index
,
691 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
692 BT_LOGW("Structure or variant field type already contains a field type with this name: "
693 "field-name=\"%s\"", field_name
);
698 g_array_set_size(members
, members
->len
+ 1);
701 struct bt_field_type_variant_choice
*choice
=
702 &g_array_index(members
,
703 struct bt_field_type_variant_choice
,
706 member_ft
= &choice
->type
;
707 member_name
= &choice
->name
;
708 BT_ASSERT(!choice
->ranges
);
709 choice
->ranges
= g_array_new(FALSE
, TRUE
,
710 sizeof(struct bt_field_type_variant_choice_range
));
711 BT_ASSERT(choice
->ranges
);
713 struct bt_field_type_structure_field
*field
=
714 &g_array_index(members
,
715 struct bt_field_type_structure_field
,
718 member_ft
= &field
->type
;
719 member_name
= &field
->name
;
722 *member_name
= name_quark
;
723 *member_ft
= bt_get(field_type
);
724 g_hash_table_insert(field_name_to_index
,
725 GUINT_TO_POINTER(name_quark
),
726 GUINT_TO_POINTER(members
->len
- 1));
727 BT_LOGV("Added structure/variant field type member: member-ft-addr=%p, "
728 "member-name=\"%s\"", field_type
, field_name
);
735 int bt_field_type_integer_validate(struct bt_field_type
*ft
)
738 struct bt_field_type_integer
*int_ft
= (void *) ft
;
740 if (int_ft
->mapped_clock_class
&& int_ft
->is_signed
) {
741 BT_LOGW("Invalid integer field type: cannot be signed and have a mapped clock class: "
742 "ft-addr=%p, clock-class-addr=%p, clock-class-name=\"%s\"",
743 ft
, int_ft
->mapped_clock_class
,
744 bt_clock_class_get_name(int_ft
->mapped_clock_class
));
754 struct enumeration_mapping
*bt_field_type_enumeration_get_mapping_by_index(
755 struct bt_field_type
*ft
, uint64_t index
)
757 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
758 struct enumeration_mapping
*mapping
= NULL
;
760 if (index
>= enum_ft
->entries
->len
) {
761 BT_LOGW("Invalid parameter: index is out of bounds: "
762 "addr=%p, index=%" PRIu64
", count=%u",
763 ft
, index
, enum_ft
->entries
->len
);
767 mapping
= g_ptr_array_index(enum_ft
->entries
, index
);
774 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
775 * Only used when freezing an enumeration.
778 void bt_field_type_enumeration_set_range_overlap(
779 struct bt_field_type_enumeration
*ft
)
784 BT_LOGV("Setting enumeration field type's overlap flag: addr=%p",
786 len
= ft
->entries
->len
;
787 is_signed
= bt_field_type_integer_is_signed((void *) ft
->container_ft
);
789 for (i
= 0; i
< len
; i
++) {
790 for (j
= i
+ 1; j
< len
; j
++) {
791 struct enumeration_mapping
*mapping
[2];
793 mapping
[0] = bt_field_type_enumeration_get_mapping_by_index(
795 mapping
[1] = bt_field_type_enumeration_get_mapping_by_index(
798 if (mapping
[0]->range_start
._signed
799 <= mapping
[1]->range_end
._signed
800 && mapping
[0]->range_end
._signed
801 >= mapping
[1]->range_start
._signed
) {
802 ft
->has_overlapping_ranges
= BT_TRUE
;
806 if (mapping
[0]->range_start
._unsigned
807 <= mapping
[1]->range_end
._unsigned
808 && mapping
[0]->range_end
._unsigned
809 >= mapping
[1]->range_start
._unsigned
) {
810 ft
->has_overlapping_ranges
= BT_TRUE
;
818 if (ft
->has_overlapping_ranges
) {
819 BT_LOGV_STR("Enumeration field type has overlapping ranges.");
821 BT_LOGV_STR("Enumeration field type has no overlapping ranges.");
826 int bt_field_type_enumeration_validate_recursive(
827 struct bt_field_type
*ft
)
830 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
832 ret
= bt_field_type_integer_validate((void *) enum_ft
->container_ft
);
834 BT_LOGW("Invalid enumeration field type: container type is invalid: "
835 "enum-ft-addr=%p, int-ft-addr=%p",
836 ft
, enum_ft
->container_ft
);
840 /* Ensure enum has entries */
841 if (enum_ft
->entries
->len
== 0) {
842 BT_LOGW("Invalid enumeration field type: no entries: "
853 int bt_field_type_sequence_validate_recursive(
854 struct bt_field_type
*ft
)
857 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
859 /* Length field name should be set at this point */
860 if (seq_ft
->length_field_name
->len
== 0) {
861 BT_LOGW("Invalid sequence field type: no length field name: "
867 ret
= bt_field_type_validate(seq_ft
->element_ft
);
869 BT_LOGW("Invalid sequence field type: invalid element field type: "
870 "seq-ft-addr=%p, element-ft-add=%p",
871 ft
, seq_ft
->element_ft
);
879 int bt_field_type_array_validate_recursive(
880 struct bt_field_type
*ft
)
883 struct bt_field_type_array
*array_ft
= (void *) ft
;
885 ret
= bt_field_type_validate(array_ft
->element_ft
);
887 BT_LOGW("Invalid array field type: invalid element field type: "
888 "array-ft-addr=%p, element-ft-add=%p",
889 ft
, array_ft
->element_ft
);
896 int bt_field_type_structure_validate_recursive(
897 struct bt_field_type
*ft
)
900 struct bt_field_type
*child_ft
= NULL
;
901 int64_t field_count
=
902 bt_field_type_structure_get_field_count(ft
);
905 BT_ASSERT(field_count
>= 0);
907 for (i
= 0; i
< field_count
; ++i
) {
908 const char *field_name
;
910 ret
= bt_field_type_structure_borrow_field_by_index(ft
,
911 &field_name
, &child_ft
, i
);
913 ret
= bt_field_type_validate(child_ft
);
915 BT_LOGW("Invalid structure field type: "
916 "a contained field type is invalid: "
917 "struct-ft-addr=%p, field-ft-addr=%p, "
918 "field-name=\"%s\", field-index=%" PRId64
,
919 ft
, child_ft
, field_name
, i
);
929 bt_bool
bt_field_type_enumeration_has_overlapping_ranges(
930 struct bt_field_type_enumeration
*enum_ft
)
932 if (!enum_ft
->common
.frozen
) {
933 bt_field_type_enumeration_set_range_overlap(enum_ft
);
936 return enum_ft
->has_overlapping_ranges
;
940 int bt_field_type_variant_validate_recursive(
941 struct bt_field_type
*ft
)
945 struct bt_field_type
*child_ft
= NULL
;
946 struct bt_field_type_variant
*var_ft
= (void *) ft
;
949 if (var_ft
->tag_name
->len
== 0) {
950 BT_LOGW("Invalid variant field type: no tag field name: "
956 if (!var_ft
->tag_ft
) {
957 BT_LOGW("Invalid variant field type: no tag field type: "
958 "addr=%p, tag-field-name=\"%s\"", var_ft
,
959 var_ft
->tag_name
->str
);
964 if (bt_field_type_enumeration_has_overlapping_ranges(var_ft
->tag_ft
)) {
965 BT_LOGW("Invalid variant field type: enumeration tag field type has overlapping ranges: "
966 "variant-ft-addr=%p, tag-field-name=\"%s\", "
967 "enum-ft-addr=%p", ft
, var_ft
->tag_name
->str
,
974 * It is valid to have a variant field type which does not have
975 * the fields corresponding to each label in the associated
978 * It is also valid to have variant field type fields which
979 * cannot be selected because the variant field type tag has no
980 * mapping named as such. This scenario, while not ideal, cannot
983 * If a non-existing field happens to be selected by an
984 * enumeration while reading a variant field, an error will be
985 * generated at that point (while reading the stream).
987 field_count
= bt_field_type_variant_get_field_count(ft
);
988 if (field_count
< 0) {
989 BT_LOGW("Invalid variant field type: no fields: "
990 "addr=%p, tag-field-name=\"%s\"",
991 ft
, var_ft
->tag_name
->str
);
996 for (i
= 0; i
< field_count
; ++i
) {
997 const char *field_name
;
999 ret
= bt_field_type_variant_borrow_field_by_index(ft
,
1000 &field_name
, &child_ft
, i
);
1001 BT_ASSERT(ret
== 0);
1002 ret
= bt_field_type_validate(child_ft
);
1004 BT_LOGW("Invalid variant field type: "
1005 "a contained field type is invalid: "
1006 "variant-ft-addr=%p, tag-field-name=\"%s\", "
1007 "field-ft-addr=%p, field-name=\"%s\", "
1008 "field-index=%" PRId64
,
1009 ft
, var_ft
->tag_name
->str
, child_ft
,
1020 * This function validates a given field type without considering
1021 * where this field type is located. It only validates the properties
1022 * of the given field type and the properties of its children if
1026 int bt_field_type_validate(struct bt_field_type
*ft
)
1033 /* Already marked as valid */
1037 if (ft
->methods
->validate
) {
1038 ret
= ft
->methods
->validate(ft
);
1041 if (ret
== 0 && ft
->frozen
) {
1042 /* Field type is valid */
1043 BT_LOGV("Marking field type as valid: addr=%p", ft
);
1051 struct bt_field_type
*bt_field_type_integer_create(unsigned int size
)
1053 struct bt_field_type_integer
*integer
= NULL
;
1055 BT_LOGD("Creating integer field type object: size=%u", size
);
1057 if (size
== 0 || size
> 64) {
1058 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
1063 integer
= g_new0(struct bt_field_type_integer
, 1);
1065 BT_LOGE_STR("Failed to allocate one integer field type.");
1069 bt_field_type_integer_initialize((void *) integer
,
1070 size
, bt_field_type_integer_destroy
,
1071 &bt_field_type_integer_methods
);
1072 BT_LOGD("Created integer field type object: addr=%p, size=%u",
1080 return (void *) integer
;
1084 int bt_field_type_integer_get_size(struct bt_field_type
*ft
)
1086 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1088 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1089 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_INTEGER
,
1091 return (int) int_ft
->size
;
1094 bt_bool
bt_field_type_integer_is_signed(struct bt_field_type
*ft
)
1096 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1098 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1099 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_INTEGER
,
1101 return int_ft
->is_signed
;
1104 int bt_field_type_integer_set_is_signed(struct bt_field_type
*ft
,
1108 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1111 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1117 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1123 if (ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1124 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1125 "addr=%p, ft-id=%s", ft
,
1126 bt_common_field_type_id_string(ft
->id
));
1131 int_ft
->is_signed
= !!is_signed
;
1132 BT_LOGV("Set integer field type's signedness: addr=%p, is-signed=%d",
1139 int bt_field_type_integer_set_size(struct bt_field_type
*ft
,
1143 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1146 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1152 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1158 if (ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1159 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1160 "addr=%p, ft-id=%s", ft
,
1161 bt_common_field_type_id_string(ft
->id
));
1166 if (size
== 0 || size
> 64) {
1167 BT_LOGW("Invalid parameter: size must be between 1 and 64: "
1168 "addr=%p, size=%u", ft
, size
);
1173 int_ft
->size
= size
;
1174 BT_LOGV("Set integer field type's size: addr=%p, size=%u",
1181 enum bt_integer_base
bt_field_type_integer_get_base(
1182 struct bt_field_type
*ft
)
1184 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1186 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1187 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_INTEGER
,
1189 return int_ft
->base
;
1192 int bt_field_type_integer_set_base(struct bt_field_type
*ft
,
1193 enum bt_integer_base base
)
1196 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1199 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1205 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1211 if (ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1212 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1213 "addr=%p, ft-id=%s", ft
,
1214 bt_common_field_type_id_string(ft
->id
));
1220 case BT_INTEGER_BASE_UNSPECIFIED
:
1221 case BT_INTEGER_BASE_BINARY
:
1222 case BT_INTEGER_BASE_OCTAL
:
1223 case BT_INTEGER_BASE_DECIMAL
:
1224 case BT_INTEGER_BASE_HEXADECIMAL
:
1226 int_ft
->base
= base
;
1230 BT_LOGW("Invalid parameter: unknown integer field type base: "
1231 "addr=%p, base=%d", ft
, base
);
1235 BT_LOGV("Set integer field type's base: addr=%p, base=%s",
1236 ft
, bt_common_integer_base_string(base
));
1242 enum bt_string_encoding
bt_field_type_integer_get_encoding(
1243 struct bt_field_type
*ft
)
1245 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1247 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1248 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_INTEGER
,
1250 return int_ft
->encoding
;
1253 int bt_field_type_integer_set_encoding(struct bt_field_type
*ft
,
1254 enum bt_string_encoding encoding
)
1257 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1260 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1266 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1272 if (ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1273 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1274 "addr=%p, ft-id=%s", ft
,
1275 bt_common_field_type_id_string(ft
->id
));
1280 if (encoding
!= BT_STRING_ENCODING_UTF8
&&
1281 encoding
!= BT_STRING_ENCODING_ASCII
&&
1282 encoding
!= BT_STRING_ENCODING_NONE
) {
1283 BT_LOGW("Invalid parameter: unknown string encoding: "
1284 "addr=%p, encoding=%d", ft
, encoding
);
1289 int_ft
->encoding
= encoding
;
1290 BT_LOGV("Set integer field type's encoding: addr=%p, encoding=%s",
1291 ft
, bt_common_string_encoding_string(encoding
));
1297 struct bt_clock_class
*bt_field_type_integer_borrow_mapped_clock_class(
1298 struct bt_field_type
*ft
)
1300 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1302 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1303 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_INTEGER
,
1305 return int_ft
->mapped_clock_class
;
1309 int bt_field_type_integer_set_mapped_clock_class_no_check_frozen(
1310 struct bt_field_type
*ft
,
1311 struct bt_clock_class
*clock_class
)
1313 struct bt_field_type_integer
*int_ft
= (void *) ft
;
1317 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
1322 if (ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1323 BT_LOGW("Invalid parameter: field type is not an integer field type: "
1324 "addr=%p, ft-id=%s", ft
,
1325 bt_common_field_type_id_string(ft
->id
));
1329 if (!bt_clock_class_is_valid(clock_class
)) {
1330 BT_LOGW("Invalid parameter: clock class is invalid: ft-addr=%p"
1331 "clock-class-addr=%p, clock-class-name=\"%s\"",
1333 bt_clock_class_get_name(clock_class
));
1338 bt_put(int_ft
->mapped_clock_class
);
1339 int_ft
->mapped_clock_class
= bt_get(clock_class
);
1340 BT_LOGV("Set integer field type's mapped clock class: ft-addr=%p, "
1341 "clock-class-addr=%p, clock-class-name=\"%s\"",
1342 ft
, clock_class
, bt_clock_class_get_name(clock_class
));
1348 int bt_field_type_integer_set_mapped_clock_class(struct bt_field_type
*ft
,
1349 struct bt_clock_class
*clock_class
)
1354 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1360 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1366 ret
= bt_field_type_integer_set_mapped_clock_class_no_check_frozen(
1374 void bt_field_type_enum_iter_destroy(struct bt_object
*obj
)
1376 struct bt_field_type_enumeration_mapping_iterator
*iter
=
1378 struct bt_field_type_enumeration_mapping_iterator
,
1381 BT_LOGD("Destroying enumeration field type mapping iterator: addr=%p",
1383 BT_LOGD_STR("Putting parent enumeration field type.");
1384 bt_put(iter
->enumeration_ft
);
1389 struct bt_field_type_enumeration_mapping_iterator
*
1390 bt_field_type_enumeration_find_mappings_type(
1391 struct bt_field_type
*ft
,
1392 enum bt_field_type_enumeration_mapping_iterator_type iterator_type
)
1394 struct bt_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1396 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1397 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ENUM
,
1399 iter
= g_new0(struct bt_field_type_enumeration_mapping_iterator
, 1);
1401 BT_LOGE_STR("Failed to allocate one enumeration field type mapping.");
1405 bt_object_init_shared(&iter
->base
, bt_field_type_enum_iter_destroy
);
1406 iter
->enumeration_ft
= bt_get(ft
);
1408 iter
->type
= iterator_type
;
1414 struct bt_field_type_enumeration_mapping_iterator
*
1415 bt_field_type_enumeration_find_mappings_by_name(
1416 struct bt_field_type
*ft
, const char *name
)
1418 struct bt_field_type_enumeration_mapping_iterator
*iter
;
1420 iter
= bt_field_type_enumeration_find_mappings_type(
1421 ft
, ITERATOR_BY_NAME
);
1423 BT_LOGW("Cannot create enumeration field type mapping iterator: "
1424 "ft-addr=%p, mapping-name=\"%s\"", ft
, name
);
1428 iter
->u
.name_quark
= g_quark_try_string(name
);
1429 if (!iter
->u
.name_quark
) {
1431 * No results are possible, set the iterator's position at the
1434 iter
->index
= iter
->enumeration_ft
->entries
->len
;
1444 int bt_field_type_enumeration_mapping_iterator_next(
1445 struct bt_field_type_enumeration_mapping_iterator
*iter
)
1447 struct bt_field_type_enumeration
*enum_ft
= iter
->enumeration_ft
;
1448 int i
, ret
= 0, len
;
1450 BT_ASSERT_PRE_NON_NULL(iter
, "Enumeration field type mapping iterator");
1451 len
= enum_ft
->entries
->len
;
1452 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1453 struct enumeration_mapping
*mapping
=
1454 bt_field_type_enumeration_get_mapping_by_index(
1455 (void *) enum_ft
, i
);
1457 switch (iter
->type
) {
1458 case ITERATOR_BY_NAME
:
1459 if (mapping
->string
== iter
->u
.name_quark
) {
1464 case ITERATOR_BY_SIGNED_VALUE
:
1466 int64_t value
= iter
->u
.signed_value
;
1468 if (value
>= mapping
->range_start
._signed
&&
1469 value
<= mapping
->range_end
._signed
) {
1475 case ITERATOR_BY_UNSIGNED_VALUE
:
1477 uint64_t value
= iter
->u
.unsigned_value
;
1479 if (value
>= mapping
->range_start
._unsigned
&&
1480 value
<= mapping
->range_end
._unsigned
) {
1487 BT_LOGF("Invalid enumeration field type mapping iterator type: "
1488 "type=%d", iter
->type
);
1499 struct bt_field_type_enumeration_mapping_iterator
*
1500 bt_field_type_enumeration_signed_find_mappings_by_value(
1501 struct bt_field_type
*ft
, int64_t value
)
1503 struct bt_field_type_enumeration_mapping_iterator
*iter
;
1505 iter
= bt_field_type_enumeration_find_mappings_type(
1506 ft
, ITERATOR_BY_SIGNED_VALUE
);
1508 BT_LOGW("Cannot create enumeration field type mapping iterator: "
1509 "ft-addr=%p, value=%" PRId64
, ft
, value
);
1513 if (bt_field_type_integer_is_signed(
1514 (void *) iter
->enumeration_ft
->container_ft
) != 1) {
1515 BT_LOGW("Invalid parameter: enumeration field type is unsigned: "
1516 "enum-ft-addr=%p, int-ft-addr=%p",
1517 ft
, iter
->enumeration_ft
->container_ft
);
1521 iter
->u
.signed_value
= value
;
1529 struct bt_field_type_enumeration_mapping_iterator
*
1530 bt_field_type_enumeration_unsigned_find_mappings_by_value(
1531 struct bt_field_type
*ft
, uint64_t value
)
1533 struct bt_field_type_enumeration_mapping_iterator
*iter
;
1535 iter
= bt_field_type_enumeration_find_mappings_type(
1536 ft
, ITERATOR_BY_UNSIGNED_VALUE
);
1538 BT_LOGW("Cannot create enumeration field type mapping iterator: "
1539 "ft-addr=%p, value=%" PRIu64
, ft
, value
);
1543 if (bt_field_type_integer_is_signed(
1544 (void *) iter
->enumeration_ft
->container_ft
) != 0) {
1545 BT_LOGW("Invalid parameter: enumeration field type is signed: "
1546 "enum-ft-addr=%p, int-ft-addr=%p",
1547 ft
, iter
->enumeration_ft
->container_ft
);
1551 iter
->u
.unsigned_value
= value
;
1559 int bt_field_type_enumeration_mapping_iterator_signed_get(
1560 struct bt_field_type_enumeration_mapping_iterator
*iter
,
1561 const char **mapping_name
, int64_t *range_begin
,
1564 BT_ASSERT_PRE_NON_NULL(iter
, "Enumeration field type mapping iterator");
1565 BT_ASSERT_PRE(iter
->index
!= -1,
1566 "Invalid enumeration field type mapping iterator access: "
1567 "addr=%p, position=-1", iter
);
1568 return bt_field_type_enumeration_signed_get_mapping_by_index(
1569 (void *) iter
->enumeration_ft
, iter
->index
,
1570 mapping_name
, range_begin
, range_end
);
1573 int bt_field_type_enumeration_mapping_iterator_unsigned_get(
1574 struct bt_field_type_enumeration_mapping_iterator
*iter
,
1575 const char **mapping_name
, uint64_t *range_begin
,
1576 uint64_t *range_end
)
1578 BT_ASSERT_PRE_NON_NULL(iter
, "Enumeration field type mapping iterator");
1579 BT_ASSERT_PRE(iter
->index
!= -1,
1580 "Invalid enumeration field type mapping iterator access: "
1581 "addr=%p, position=-1", iter
);
1582 return bt_field_type_enumeration_unsigned_get_mapping_by_index(
1583 (void *) iter
->enumeration_ft
, iter
->index
,
1584 mapping_name
, range_begin
, range_end
);
1587 int bt_field_type_enumeration_signed_get_mapping_by_index(
1588 struct bt_field_type
*ft
, uint64_t index
,
1589 const char **mapping_name
, int64_t *range_begin
,
1593 struct enumeration_mapping
*mapping
;
1595 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1596 BT_ASSERT_PRE_FT_HAS_ID(ft
,
1597 BT_FIELD_TYPE_ID_ENUM
, "Field type");
1598 mapping
= bt_field_type_enumeration_get_mapping_by_index(ft
,
1601 /* bt_field_type_enumeration_get_mapping_by_index() logs errors */
1607 *mapping_name
= g_quark_to_string(mapping
->string
);
1608 BT_ASSERT(*mapping_name
);
1612 *range_begin
= mapping
->range_start
._signed
;
1616 *range_end
= mapping
->range_end
._signed
;
1623 int bt_field_type_enumeration_unsigned_get_mapping_by_index(
1624 struct bt_field_type
*ft
, uint64_t index
,
1625 const char **mapping_name
, uint64_t *range_begin
,
1626 uint64_t *range_end
)
1629 struct enumeration_mapping
*mapping
;
1631 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1632 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ENUM
, "Field type");
1633 mapping
= bt_field_type_enumeration_get_mapping_by_index(
1636 /* bt_field_type_enumeration_get_mapping_by_index() reports any error */
1642 *mapping_name
= g_quark_to_string(mapping
->string
);
1643 BT_ASSERT(*mapping_name
);
1647 *range_begin
= mapping
->range_start
._unsigned
;
1651 *range_end
= mapping
->range_end
._unsigned
;
1658 struct bt_field_type
*bt_field_type_enumeration_create(
1659 struct bt_field_type
*container_ft
)
1661 struct bt_field_type_enumeration
*enumeration
= NULL
;
1662 struct bt_field_type
*int_ft
= (void *) container_ft
;
1664 BT_LOGD("Creating enumeration field type object: int-ft-addr=%p",
1667 if (!container_ft
) {
1668 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1672 if (int_ft
->id
!= BT_FIELD_TYPE_ID_INTEGER
) {
1673 BT_LOGW("Invalid parameter: container field type is not an integer field type: "
1674 "container-ft-addr=%p, container-ft-id=%s",
1675 container_ft
, bt_common_field_type_id_string(int_ft
->id
));
1679 enumeration
= g_new0(struct bt_field_type_enumeration
, 1);
1681 BT_LOGE_STR("Failed to allocate one enumeration field type.");
1685 bt_field_type_enumeration_initialize((void *) enumeration
,
1686 int_ft
, bt_field_type_enumeration_destroy_recursive
,
1687 &bt_field_type_enumeration_methods
);
1688 BT_LOGD("Created enumeration field type object: addr=%p, "
1689 "int-ft-addr=%p, int-ft-size=%u",
1690 enumeration
, container_ft
,
1691 bt_field_type_integer_get_size(container_ft
));
1695 BT_PUT(enumeration
);
1698 return (void *) enumeration
;
1701 struct bt_field_type
*bt_field_type_enumeration_borrow_container_field_type(
1702 struct bt_field_type
*ft
)
1704 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
1706 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1707 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ENUM
, "Field type");
1708 return (void *) enum_ft
->container_ft
;
1711 int bt_field_type_enumeration_signed_add_mapping(
1712 struct bt_field_type
*ft
, const char *string
,
1713 int64_t range_start
, int64_t range_end
)
1716 GQuark mapping_name
;
1717 struct enumeration_mapping
*mapping
;
1718 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
1719 char *escaped_string
;
1722 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1728 BT_LOGW_STR("Invalid parameter: string is NULL.");
1734 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1740 if (ft
->id
!= BT_FIELD_TYPE_ID_ENUM
) {
1741 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1742 "addr=%p, ft-id=%s", ft
,
1743 bt_common_field_type_id_string(ft
->id
));
1748 if (range_end
< range_start
) {
1749 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1750 "addr=%p, range-start=%" PRId64
", range-end=%" PRId64
,
1751 ft
, range_start
, range_end
);
1756 if (strlen(string
) == 0) {
1757 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1758 "enum-ft-addr=%p, mapping-name-addr=%p", ft
,
1764 escaped_string
= g_strescape(string
, NULL
);
1765 if (!escaped_string
) {
1766 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1767 "mapping-name-addr=%p, mapping-name=\"%s\"",
1768 ft
, string
, string
);
1773 mapping
= g_new(struct enumeration_mapping
, 1);
1775 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1779 mapping_name
= g_quark_from_string(escaped_string
);
1780 *mapping
= (struct enumeration_mapping
) {
1781 .range_start
._signed
= range_start
,
1782 .range_end
._signed
= range_end
,
1783 .string
= mapping_name
,
1785 g_ptr_array_add(enum_ft
->entries
, mapping
);
1786 g_ptr_array_sort(enum_ft
->entries
,
1787 (GCompareFunc
) compare_enumeration_mappings_signed
);
1788 BT_LOGV("Added mapping to signed enumeration field type: addr=%p, "
1789 "name=\"%s\", range-start=%" PRId64
", "
1790 "range-end=%" PRId64
,
1791 ft
, string
, range_start
, range_end
);
1794 free(escaped_string
);
1800 int bt_field_type_enumeration_unsigned_add_mapping(
1801 struct bt_field_type
*ft
, const char *string
,
1802 uint64_t range_start
, uint64_t range_end
)
1805 GQuark mapping_name
;
1806 struct enumeration_mapping
*mapping
;
1807 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
1808 char *escaped_string
;
1811 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1817 BT_LOGW_STR("Invalid parameter: string is NULL.");
1823 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1829 if (ft
->id
!= BT_FIELD_TYPE_ID_ENUM
) {
1830 BT_LOGW("Invalid parameter: field type is not an enumeration field type: "
1831 "addr=%p, ft-id=%s", ft
,
1832 bt_common_field_type_id_string(ft
->id
));
1837 if (range_end
< range_start
) {
1838 BT_LOGW("Invalid parameter: range's end is lesser than range's start: "
1839 "addr=%p, range-start=%" PRIu64
", range-end=%" PRIu64
,
1840 ft
, range_start
, range_end
);
1845 if (strlen(string
) == 0) {
1846 BT_LOGW("Invalid parameter: mapping name is an empty string: "
1847 "enum-ft-addr=%p, mapping-name-addr=%p", ft
,
1853 escaped_string
= g_strescape(string
, NULL
);
1854 if (!escaped_string
) {
1855 BT_LOGE("Cannot escape mapping name: enum-ft-addr=%p, "
1856 "mapping-name-addr=%p, mapping-name=\"%s\"",
1857 ft
, string
, string
);
1862 mapping
= g_new(struct enumeration_mapping
, 1);
1864 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
1868 mapping_name
= g_quark_from_string(escaped_string
);
1869 *mapping
= (struct enumeration_mapping
) {
1870 .range_start
._unsigned
= range_start
,
1871 .range_end
._unsigned
= range_end
,
1872 .string
= mapping_name
,
1874 g_ptr_array_add(enum_ft
->entries
, mapping
);
1875 g_ptr_array_sort(enum_ft
->entries
,
1876 (GCompareFunc
) compare_enumeration_mappings_unsigned
);
1877 BT_LOGV("Added mapping to unsigned enumeration field type: addr=%p, "
1878 "name=\"%s\", range-start=%" PRIu64
", "
1879 "range-end=%" PRIu64
,
1880 ft
, string
, range_start
, range_end
);
1883 free(escaped_string
);
1889 int64_t bt_field_type_enumeration_get_mapping_count(
1890 struct bt_field_type
*ft
)
1892 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
1894 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1895 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ENUM
, "Field type");
1896 return (int64_t) enum_ft
->entries
->len
;
1899 struct bt_field_type
*bt_field_type_floating_point_create(void)
1901 struct bt_field_type_floating_point
*floating_point
=
1902 g_new0(struct bt_field_type_floating_point
, 1);
1904 BT_LOGD_STR("Creating floating point number field type object.");
1906 if (!floating_point
) {
1907 BT_LOGE_STR("Failed to allocate one floating point number field type.");
1911 bt_field_type_floating_point_initialize(
1912 (void *) floating_point
,
1913 bt_field_type_floating_point_destroy
,
1914 &bt_field_type_floating_point_methods
);
1915 BT_LOGD("Created floating point number field type object: addr=%p, "
1916 "exp-size=%u, mant-size=%u", floating_point
,
1917 floating_point
->exp_dig
, floating_point
->mant_dig
);
1920 return (void *) floating_point
;
1923 int bt_field_type_floating_point_get_exponent_digits(
1924 struct bt_field_type
*ft
)
1926 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
1928 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1929 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_FLOAT
,
1931 return (int) flt_ft
->exp_dig
;
1934 int bt_field_type_floating_point_set_exponent_digits(
1935 struct bt_field_type
*ft
, unsigned int exponent_digits
)
1938 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
1941 BT_LOGW_STR("Invalid parameter: field type is NULL.");
1947 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
1953 if (ft
->id
!= BT_FIELD_TYPE_ID_FLOAT
) {
1954 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
1955 "addr=%p, ft-id=%s", ft
,
1956 bt_common_field_type_id_string(ft
->id
));
1961 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1962 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1964 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1965 BT_LOGW("Invalid parameter: invalid exponent size: "
1966 "addr=%p, exp-size=%u", ft
, exponent_digits
);
1971 flt_ft
->exp_dig
= exponent_digits
;
1972 BT_LOGV("Set floating point number field type's exponent size: addr=%p, "
1973 "exp-size=%u", ft
, exponent_digits
);
1979 int bt_field_type_floating_point_get_mantissa_digits(
1980 struct bt_field_type
*ft
)
1982 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
1984 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
1985 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_FLOAT
,
1987 return (int) flt_ft
->mant_dig
;
1990 int bt_field_type_floating_point_set_mantissa_digits(
1991 struct bt_field_type
*ft
, unsigned int mantissa_digits
)
1994 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
1997 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2003 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2009 if (ft
->id
!= BT_FIELD_TYPE_ID_FLOAT
) {
2010 BT_LOGW("Invalid parameter: field type is not a floating point number field type: "
2011 "addr=%p, ft-id=%s", ft
,
2012 bt_common_field_type_id_string(ft
->id
));
2017 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
2018 (mantissa_digits
!= DBL_MANT_DIG
) &&
2019 (mantissa_digits
!= LDBL_MANT_DIG
)) {
2020 BT_LOGW("Invalid parameter: invalid mantissa size: "
2021 "addr=%p, mant-size=%u", ft
, mantissa_digits
);
2026 flt_ft
->mant_dig
= mantissa_digits
;
2027 BT_LOGV("Set floating point number field type's mantissa size: addr=%p, "
2028 "mant-size=%u", ft
, mantissa_digits
);
2034 struct bt_field_type
*bt_field_type_structure_create(void)
2036 struct bt_field_type_structure
*structure
=
2037 g_new0(struct bt_field_type_structure
, 1);
2039 BT_LOGD_STR("Creating structure field type object.");
2042 BT_LOGE_STR("Failed to allocate one structure field type.");
2046 bt_field_type_structure_initialize((void *) structure
,
2047 bt_field_type_structure_destroy_recursive
,
2048 &bt_field_type_structure_methods
);
2049 BT_LOGD("Created structure field type object: addr=%p",
2057 return (void *) structure
;
2060 int bt_field_type_structure_add_field(struct bt_field_type
*ft
,
2061 struct bt_field_type
*field_type
,
2062 const char *field_name
)
2065 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
2068 * TODO: check that `field_type` does not contain `type`,
2072 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2078 BT_LOGW_STR("Invalid parameter: field name is NULL.");
2084 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2090 if (ft
->id
!= BT_FIELD_TYPE_ID_STRUCT
) {
2091 BT_LOGW("Invalid parameter: field type is not a structure field type: "
2092 "addr=%p, ft-id=%s", ft
,
2093 bt_common_field_type_id_string(ft
->id
));
2098 if (ft
== field_type
) {
2099 BT_LOGW("Invalid parameter: structure field type and field type to add are the same: "
2105 if (add_structure_variant_member(struct_ft
->fields
,
2106 struct_ft
->field_name_to_index
, field_type
, field_name
,
2108 BT_LOGW("Cannot add field to structure field type: "
2109 "struct-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2110 ft
, field_type
, field_name
);
2115 BT_LOGV("Added structure field type field: struct-ft-addr=%p, "
2116 "field-ft-addr=%p, field-name=\"%s\"", ft
,
2117 field_type
, field_name
);
2123 int64_t bt_field_type_structure_get_field_count(struct bt_field_type
*ft
)
2125 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
2127 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2128 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_STRUCT
,
2130 return (int64_t) struct_ft
->fields
->len
;
2133 int bt_field_type_structure_borrow_field_by_index(
2134 struct bt_field_type
*ft
,
2135 const char **field_name
,
2136 struct bt_field_type
**field_type
, uint64_t index
)
2138 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
2139 struct bt_field_type_structure_field
*field
;
2141 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2142 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_STRUCT
,
2144 BT_ASSERT_PRE(index
< struct_ft
->fields
->len
,
2145 "Index is out of bounds: index=%" PRIu64
", "
2146 "count=%u, %![ft-]+F",
2147 index
, struct_ft
->fields
->len
, ft
);
2148 field
= BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(struct_ft
, index
);
2151 *field_type
= field
->type
;
2155 *field_name
= g_quark_to_string(field
->name
);
2156 BT_ASSERT(*field_name
);
2162 struct bt_field_type
*bt_field_type_structure_borrow_field_type_by_name(
2163 struct bt_field_type
*ft
, const char *name
)
2167 struct bt_field_type_structure_field
*field
;
2168 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
2169 struct bt_field_type
*field_type
= NULL
;
2171 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2172 BT_ASSERT_PRE_NON_NULL(name
, "Name");
2173 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_STRUCT
,
2175 name_quark
= g_quark_try_string(name
);
2177 BT_LOGV("No such structure field type field name: "
2178 "ft-addr=%p, field-name=\"%s\"",
2183 if (!g_hash_table_lookup_extended(struct_ft
->field_name_to_index
,
2184 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*) &index
)) {
2185 BT_LOGV("No such structure field type field name: "
2186 "ft-addr=%p, field-name=\"%s\"",
2191 field
= BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(ft
, index
);
2192 field_type
= field
->type
;
2198 struct bt_field_type
*bt_field_type_variant_create(
2199 struct bt_field_type
*tag_ft
, const char *tag_name
)
2201 struct bt_field_type_variant
*var_ft
= NULL
;
2203 BT_LOGD("Creating variant field type object: "
2204 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2207 if (tag_name
&& !bt_identifier_is_valid(tag_name
)) {
2208 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2209 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2214 var_ft
= g_new0(struct bt_field_type_variant
, 1);
2216 BT_LOGE_STR("Failed to allocate one variant field type.");
2220 bt_field_type_variant_initialize((void *) var_ft
,
2221 (void *) tag_ft
, tag_name
,
2222 bt_field_type_variant_destroy_recursive
,
2223 &bt_field_type_variant_methods
);
2224 BT_LOGD("Created variant field type object: addr=%p, "
2225 "tag-ft-addr=%p, tag-field-name=\"%s\"",
2226 var_ft
, tag_ft
, tag_name
);
2233 return (void *) var_ft
;
2236 struct bt_field_type
*bt_field_type_variant_borrow_tag_field_type(
2237 struct bt_field_type
*ft
)
2239 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2240 struct bt_field_type
*tag_ft
= NULL
;
2242 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2243 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
2246 if (!var_ft
->tag_ft
) {
2247 BT_LOGV("Variant field type has no tag field type: "
2252 tag_ft
= (void *) var_ft
->tag_ft
;
2258 const char *bt_field_type_variant_get_tag_name(struct bt_field_type
*ft
)
2260 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2261 const char *tag_name
= NULL
;
2263 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2264 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
2267 if (var_ft
->tag_name
->len
== 0) {
2268 BT_LOGV("Variant field type has no tag field name: "
2273 tag_name
= var_ft
->tag_name
->str
;
2279 int bt_field_type_variant_set_tag_name(
2280 struct bt_field_type
*ft
, const char *name
)
2283 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2286 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2292 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2298 if (ft
->id
!= BT_FIELD_TYPE_ID_VARIANT
) {
2299 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2300 "addr=%p, ft-id=%s", ft
, bt_common_field_type_id_string(ft
->id
));
2305 if (!bt_identifier_is_valid(name
)) {
2306 BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: "
2307 "variant-ft-addr=%p, tag-field-name=\"%s\"",
2313 g_string_assign(var_ft
->tag_name
, name
);
2314 BT_LOGV("Set variant field type's tag field name: addr=%p, "
2315 "tag-field-name=\"%s\"", ft
, name
);
2321 int bt_field_type_variant_add_field(struct bt_field_type
*ft
,
2322 struct bt_field_type
*field_type
,
2323 const char *field_name
)
2327 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2328 GQuark field_name_quark
= g_quark_from_string(field_name
);
2331 * TODO: check that `field_type` does not contain `type`,
2335 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2341 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2347 if (ft
->id
!= BT_FIELD_TYPE_ID_VARIANT
) {
2348 BT_LOGW("Invalid parameter: field type is not a variant field type: "
2349 "addr=%p, ft-id=%s", ft
,
2350 bt_common_field_type_id_string(ft
->id
));
2355 if (ft
== field_type
) {
2356 BT_LOGW("Invalid parameter: variant field type and field type to add are the same: "
2362 /* The user has explicitly provided a tag; validate against it. */
2363 if (var_ft
->tag_ft
) {
2366 /* Make sure this name is present in the enum tag */
2367 for (i
= 0; i
< var_ft
->tag_ft
->entries
->len
; i
++) {
2368 struct enumeration_mapping
*mapping
=
2369 g_ptr_array_index(var_ft
->tag_ft
->entries
, i
);
2371 if (mapping
->string
== field_name_quark
) {
2378 /* Validation failed */
2379 BT_LOGW("Invalid parameter: field name does not name a tag field type's mapping: "
2380 "variant-ft-addr=%p, tag-ft-addr=%p, "
2381 "tag-field-name=\"%s\""
2382 "field-ft-addr=%p, field-name=\"%s\"",
2383 ft
, var_ft
->tag_ft
, var_ft
->tag_name
->str
,
2384 field_type
, field_name
);
2390 if (add_structure_variant_member(var_ft
->choices
,
2391 var_ft
->choice_name_to_index
, field_type
,
2392 field_name
, true)) {
2393 BT_LOGW("Cannot add field to variant field type: "
2394 "variant-ft-addr=%p, field-ft-addr=%p, field-name=\"%s\"",
2395 ft
, field_type
, field_name
);
2400 BT_LOGV("Added variant field type field: variant-ft-addr=%p, "
2401 "field-ft-addr=%p, field-name=\"%s\"", ft
,
2402 field_type
, field_name
);
2408 struct bt_field_type
*bt_field_type_variant_borrow_field_type_by_name(
2409 struct bt_field_type
*ft
,
2410 const char *field_name
)
2414 struct bt_field_type_variant_choice
*choice
;
2415 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2416 struct bt_field_type
*field_type
= NULL
;
2418 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2419 BT_ASSERT_PRE_NON_NULL(field_name
, "Name");
2420 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
2422 name_quark
= g_quark_try_string(field_name
);
2424 BT_LOGV("No such variant field type field name: "
2425 "ft-addr=%p, field-name=\"%s\"",
2430 if (!g_hash_table_lookup_extended(var_ft
->choice_name_to_index
,
2431 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*) &index
)) {
2432 BT_LOGV("No such variant field type field name: "
2433 "ft-addr=%p, field-name=\"%s\"",
2438 choice
= BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(ft
, index
);
2439 field_type
= choice
->type
;
2445 int64_t bt_field_type_variant_get_field_count(struct bt_field_type
*ft
)
2447 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2449 BT_ASSERT_PRE_NON_NULL(ft
, "Variant field type");
2450 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
2452 return (int64_t) var_ft
->choices
->len
;
2455 int bt_field_type_variant_borrow_field_by_index(struct bt_field_type
*ft
,
2456 const char **field_name
, struct bt_field_type
**field_type
,
2459 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2460 struct bt_field_type_variant_choice
*choice
;
2462 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2463 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
2465 BT_ASSERT_PRE(index
< var_ft
->choices
->len
,
2466 "Index is out of bounds: index=%" PRIu64
", "
2467 "count=%u, %![ft-]+F",
2468 index
, var_ft
->choices
->len
, ft
);
2469 choice
= BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(ft
, index
);
2472 *field_type
= choice
->type
;
2476 *field_name
= g_quark_to_string(choice
->name
);
2477 BT_ASSERT(*field_name
);
2484 int64_t bt_field_type_variant_find_choice_index(
2485 struct bt_field_type
*ft
, uint64_t uval
,
2490 struct bt_field_type_variant
*var_ft
= (void *) ft
;
2493 BT_ASSERT(ft
->id
== BT_FIELD_TYPE_ID_VARIANT
);
2495 if (bt_field_type_variant_update_choices(ft
)) {
2500 for (i
= 0; i
< var_ft
->choices
->len
; i
++) {
2502 struct bt_field_type_variant_choice
*choice
=
2503 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
2506 for (range_i
= 0; range_i
< choice
->ranges
->len
; range_i
++) {
2507 struct bt_field_type_variant_choice_range
*range
=
2510 struct bt_field_type_variant_choice_range
,
2514 int64_t tag_ival
= (int64_t) uval
;
2516 if (tag_ival
>= range
->lower
.i
&&
2517 tag_ival
<= range
->upper
.i
) {
2521 if (uval
>= range
->lower
.u
&&
2522 uval
<= range
->upper
.u
) {
2529 /* Range not found */
2540 struct bt_field_type
*bt_field_type_array_create(
2541 struct bt_field_type
*element_ft
, unsigned int length
)
2543 struct bt_field_type_array
*array
= NULL
;
2545 BT_LOGD("Creating array field type object: element-ft-addr=%p, "
2546 "length=%u", element_ft
, length
);
2549 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2554 BT_LOGW_STR("Invalid parameter: length is zero.");
2558 array
= g_new0(struct bt_field_type_array
, 1);
2560 BT_LOGE_STR("Failed to allocate one array field type.");
2564 bt_field_type_array_initialize((void *) array
,
2565 (void *) element_ft
, length
,
2566 bt_field_type_array_destroy_recursive
,
2567 &bt_field_type_array_methods
);
2568 BT_LOGD("Created array field type object: addr=%p, "
2569 "element-ft-addr=%p, length=%u",
2570 array
, element_ft
, length
);
2577 return (void *) array
;
2580 struct bt_field_type
*bt_field_type_array_borrow_element_field_type(
2581 struct bt_field_type
*ft
)
2583 struct bt_field_type_array
*array_ft
= (void *) ft
;
2585 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2586 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ARRAY
,
2588 BT_ASSERT(array_ft
&& array_ft
->element_ft
);
2589 return array_ft
->element_ft
;
2592 int64_t bt_field_type_array_get_length(struct bt_field_type
*ft
)
2594 struct bt_field_type_array
*array_ft
= (void *) ft
;
2596 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2597 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_ARRAY
,
2599 return (int64_t) array_ft
->length
;
2602 struct bt_field_type
*bt_field_type_sequence_create(
2603 struct bt_field_type
*element_ft
,
2604 const char *length_field_name
)
2606 struct bt_field_type_sequence
*sequence
= NULL
;
2608 BT_LOGD("Creating sequence field type object: element-ft-addr=%p, "
2609 "length-field-name=\"%s\"", element_ft
, length_field_name
);
2612 BT_LOGW_STR("Invalid parameter: element field type is NULL.");
2616 if (!bt_identifier_is_valid(length_field_name
)) {
2617 BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: "
2618 "length-field-name=\"%s\"", length_field_name
);
2622 sequence
= g_new0(struct bt_field_type_sequence
, 1);
2624 BT_LOGE_STR("Failed to allocate one sequence field type.");
2628 bt_field_type_sequence_initialize((void *) sequence
,
2629 (void *) element_ft
, length_field_name
,
2630 bt_field_type_sequence_destroy_recursive
,
2631 &bt_field_type_sequence_methods
);
2632 BT_LOGD("Created sequence field type object: addr=%p, "
2633 "element-ft-addr=%p, length-field-name=\"%s\"",
2634 sequence
, element_ft
, length_field_name
);
2641 return (void *) sequence
;
2644 struct bt_field_type
*bt_field_type_sequence_borrow_element_field_type(
2645 struct bt_field_type
*ft
)
2647 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
2649 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2650 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_SEQUENCE
,
2652 return seq_ft
->element_ft
;
2655 const char *bt_field_type_sequence_get_length_field_name(
2656 struct bt_field_type
*ft
)
2658 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
2660 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2661 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_SEQUENCE
,
2663 return seq_ft
->length_field_name
?
2664 seq_ft
->length_field_name
->str
: NULL
;
2667 struct bt_field_type
*bt_field_type_string_create(void)
2669 struct bt_field_type_string
*string
=
2670 g_new0(struct bt_field_type_string
, 1);
2672 BT_LOGD_STR("Creating string field type object.");
2675 BT_LOGE_STR("Failed to allocate one string field type.");
2679 bt_field_type_string_initialize((void *) string
,
2680 bt_field_type_string_destroy
,
2681 &bt_field_type_string_methods
);
2682 BT_LOGD("Created string field type object: addr=%p", string
);
2683 return (void *) string
;
2686 enum bt_string_encoding
bt_field_type_string_get_encoding(
2687 struct bt_field_type
*ft
)
2689 struct bt_field_type_string
*string_ft
= (void *) ft
;
2691 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2692 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_STRING
,
2694 return string_ft
->encoding
;
2697 int bt_field_type_string_set_encoding(struct bt_field_type
*ft
,
2698 enum bt_string_encoding encoding
)
2701 struct bt_field_type_string
*string_ft
= (void *) ft
;
2704 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2709 if (ft
->id
!= BT_FIELD_TYPE_ID_STRING
) {
2710 BT_LOGW("Invalid parameter: field type is not a string field type: "
2711 "addr=%p, ft-id=%s", ft
,
2712 bt_common_field_type_id_string(ft
->id
));
2717 if (encoding
!= BT_STRING_ENCODING_UTF8
&&
2718 encoding
!= BT_STRING_ENCODING_ASCII
) {
2719 BT_LOGW("Invalid parameter: unknown string encoding: "
2720 "addr=%p, encoding=%d", ft
, encoding
);
2725 string_ft
->encoding
= encoding
;
2726 BT_LOGV("Set string field type's encoding: addr=%p, encoding=%s",
2727 ft
, bt_common_string_encoding_string(encoding
));
2733 int bt_field_type_get_alignment(struct bt_field_type
*ft
)
2736 enum bt_field_type_id type_id
;
2738 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2741 ret
= (int) ft
->alignment
;
2745 type_id
= bt_field_type_get_type_id(ft
);
2747 case BT_FIELD_TYPE_ID_SEQUENCE
:
2749 struct bt_field_type
*element_ft
=
2750 bt_field_type_sequence_borrow_element_field_type(ft
);
2752 BT_ASSERT(element_ft
);
2753 ret
= bt_field_type_get_alignment(element_ft
);
2756 case BT_FIELD_TYPE_ID_ARRAY
:
2758 struct bt_field_type
*element_ft
=
2759 bt_field_type_array_borrow_element_field_type(ft
);
2761 BT_ASSERT(element_ft
);
2762 ret
= bt_field_type_get_alignment(element_ft
);
2765 case BT_FIELD_TYPE_ID_STRUCT
:
2767 int64_t i
, element_count
;
2769 element_count
= bt_field_type_structure_get_field_count(
2771 BT_ASSERT(element_count
>= 0);
2773 for (i
= 0; i
< element_count
; i
++) {
2774 struct bt_field_type
*field
= NULL
;
2775 int field_alignment
;
2777 ret
= bt_field_type_structure_borrow_field_by_index(
2778 ft
, NULL
, &field
, i
);
2779 BT_ASSERT(ret
== 0);
2781 field_alignment
= bt_field_type_get_alignment(
2783 if (field_alignment
< 0) {
2784 ret
= field_alignment
;
2788 ft
->alignment
= MAX(field_alignment
, ft
->alignment
);
2790 ret
= (int) ft
->alignment
;
2793 case BT_FIELD_TYPE_ID_UNKNOWN
:
2794 BT_LOGW("Invalid parameter: unknown field type ID: "
2795 "addr=%p, ft-id=%d", ft
, type_id
);
2799 ret
= (int) ft
->alignment
;
2808 int is_power_of_two(unsigned int value
)
2810 return ((value
& (value
- 1)) == 0) && value
> 0;
2813 int bt_field_type_set_alignment(struct bt_field_type
*ft
,
2814 unsigned int alignment
)
2817 enum bt_field_type_id type_id
;
2819 /* Alignment must be a power of two */
2821 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2827 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2833 if (!is_power_of_two(alignment
)) {
2834 BT_LOGW("Invalid parameter: alignment is not a power of two: "
2835 "addr=%p, align=%u", ft
, alignment
);
2840 type_id
= bt_field_type_get_type_id(ft
);
2841 if (type_id
== BT_FIELD_TYPE_ID_UNKNOWN
) {
2842 BT_LOGW("Invalid parameter: unknown field type ID: "
2843 "addr=%p, ft-id=%d", ft
, type_id
);
2848 if (ft
->id
== BT_FIELD_TYPE_ID_STRING
&& alignment
!= CHAR_BIT
) {
2849 BT_LOGW("Invalid parameter: alignment must be %u for a string field type: "
2850 "addr=%p, align=%u", CHAR_BIT
, ft
, alignment
);
2855 if (type_id
== BT_FIELD_TYPE_ID_VARIANT
||
2856 type_id
== BT_FIELD_TYPE_ID_SEQUENCE
||
2857 type_id
== BT_FIELD_TYPE_ID_ARRAY
) {
2858 /* Setting an alignment on these types makes no sense */
2859 BT_LOGW("Invalid parameter: cannot set the alignment of this field type: "
2860 "addr=%p, ft-id=%s", ft
,
2861 bt_common_field_type_id_string(ft
->id
));
2866 ft
->alignment
= alignment
;
2868 BT_LOGV("Set field type's alignment: addr=%p, align=%u",
2875 enum bt_byte_order
bt_field_type_get_byte_order(struct bt_field_type
*ft
)
2877 enum bt_byte_order ret
= BT_BYTE_ORDER_UNKNOWN
;
2879 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2882 case BT_FIELD_TYPE_ID_INTEGER
:
2884 struct bt_field_type_integer
*integer
= (void *) ft
;
2886 ret
= integer
->user_byte_order
;
2889 case BT_FIELD_TYPE_ID_ENUM
:
2891 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
2893 ret
= bt_field_type_get_byte_order(
2894 (void *) enum_ft
->container_ft
);
2897 case BT_FIELD_TYPE_ID_FLOAT
:
2899 struct bt_field_type_floating_point
*floating_point
= (void *) ft
;
2900 ret
= floating_point
->user_byte_order
;
2904 BT_LOGW("Invalid parameter: cannot get the byte order of this field type: "
2905 "addr=%p, ft-id=%s", ft
,
2906 bt_common_field_type_id_string(ft
->id
));
2910 BT_ASSERT(ret
== BT_BYTE_ORDER_NATIVE
||
2911 ret
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
2912 ret
== BT_BYTE_ORDER_BIG_ENDIAN
||
2913 ret
== BT_BYTE_ORDER_NETWORK
);
2919 int bt_field_type_set_byte_order(struct bt_field_type
*ft
,
2920 enum bt_byte_order byte_order
)
2925 BT_LOGW_STR("Invalid parameter: field type is NULL.");
2931 BT_LOGW("Invalid parameter: field type is frozen: addr=%p",
2937 if (byte_order
!= BT_BYTE_ORDER_NATIVE
&&
2938 byte_order
!= BT_BYTE_ORDER_LITTLE_ENDIAN
&&
2939 byte_order
!= BT_BYTE_ORDER_BIG_ENDIAN
&&
2940 byte_order
!= BT_BYTE_ORDER_NETWORK
) {
2941 BT_LOGW("Invalid parameter: invalid byte order: "
2942 "addr=%p, bo=%s", ft
,
2943 bt_common_byte_order_string(byte_order
));
2948 if (ft
->methods
->set_byte_order
) {
2949 ft
->methods
->set_byte_order(ft
, byte_order
);
2952 BT_LOGV("Set field type's byte order: addr=%p, bo=%s",
2953 ft
, bt_common_byte_order_string(byte_order
));
2959 enum bt_field_type_id
bt_field_type_get_type_id(struct bt_field_type
*ft
)
2961 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2966 void bt_field_type_freeze(struct bt_field_type
*ft
)
2968 if (!ft
|| ft
->frozen
) {
2972 BT_ASSERT(ft
->methods
->freeze
);
2973 ft
->methods
->freeze(ft
);
2976 struct bt_field_type
*bt_field_type_copy(struct bt_field_type
*ft
)
2978 struct bt_field_type
*ft_copy
= NULL
;
2980 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
2981 BT_ASSERT(ft
->methods
->copy
);
2982 ft_copy
= ft
->methods
->copy(ft
);
2984 BT_LOGE_STR("Cannot copy field type.");
2988 ft_copy
->alignment
= ft
->alignment
;
2995 int bt_field_type_structure_get_field_name_index(
2996 struct bt_field_type
*ft
, const char *name
)
3001 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
3003 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
3004 BT_ASSERT_PRE_NON_NULL(name
, "Name");
3005 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_STRUCT
,
3008 name_quark
= g_quark_try_string(name
);
3010 BT_LOGV("No such structure field type field name: "
3011 "ft-addr=%p, field-name=\"%s\"",
3017 if (!g_hash_table_lookup_extended(struct_ft
->field_name_to_index
,
3018 GUINT_TO_POINTER(name_quark
),
3019 NULL
, (gpointer
*) &index
)) {
3020 BT_LOGV("No such structure field type field name: "
3021 "ft-addr=%p, field-name=\"%s\"",
3034 int bt_field_type_variant_get_field_name_index(
3035 struct bt_field_type
*ft
, const char *name
)
3040 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3042 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
3043 BT_ASSERT_PRE_NON_NULL(name
, "Name");
3044 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
3046 name_quark
= g_quark_try_string(name
);
3048 BT_LOGV("No such variant field type field name: "
3049 "ft-addr=%p, field-name=\"%s\"",
3055 if (!g_hash_table_lookup_extended(var_ft
->choice_name_to_index
,
3056 GUINT_TO_POINTER(name_quark
),
3057 NULL
, (gpointer
*) &index
)) {
3058 BT_LOGV("No such variant field type field name: "
3059 "ft-addr=%p, field-name=\"%s\"",
3072 int bt_field_type_sequence_set_length_field_path(
3073 struct bt_field_type
*ft
, struct bt_field_path
*path
)
3076 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
3079 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3084 if (ft
->id
!= BT_FIELD_TYPE_ID_SEQUENCE
) {
3085 BT_LOGW("Invalid parameter: field type is not a sequence field type: "
3086 "addr=%p, ft-id=%s", ft
,
3087 bt_common_field_type_id_string(ft
->id
));
3093 BT_MOVE(seq_ft
->length_field_path
, path
);
3094 BT_LOGV("Set sequence field type's length field path: ft-addr=%p, "
3095 "field-path-addr=%p", ft
, path
);
3102 int bt_field_type_variant_set_tag_field_path(
3103 struct bt_field_type
*ft
,
3104 struct bt_field_path
*path
)
3107 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3110 BT_LOGW_STR("Invalid parameter: field type is NULL.");
3115 if (ft
->id
!= BT_FIELD_TYPE_ID_VARIANT
) {
3116 BT_LOGW("Invalid parameter: field type is not a variant field type: "
3117 "addr=%p, ft-id=%s", ft
,
3118 bt_common_field_type_id_string(ft
->id
));
3124 BT_MOVE(var_ft
->tag_field_path
, path
);
3125 BT_LOGV("Set variant field type's tag field path: ft-addr=%p, "
3126 "field-path-addr=%p", ft
, path
);
3133 int bt_field_type_variant_set_tag_field_type(
3134 struct bt_field_type
*ft
,
3135 struct bt_field_type
*tag_ft
)
3138 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3141 BT_LOGW_STR("Invalid parameter: variant field type is NULL.");
3147 BT_LOGW_STR("Invalid parameter: tag field type is NULL.");
3152 if (tag_ft
->id
!= BT_FIELD_TYPE_ID_ENUM
) {
3153 BT_LOGW("Invalid parameter: tag field type is not an enumeration field type: "
3154 "addr=%p, ft-id=%s", tag_ft
,
3155 bt_common_field_type_id_string(tag_ft
->id
));
3160 bt_put(var_ft
->tag_ft
);
3161 var_ft
->tag_ft
= bt_get(tag_ft
);
3162 BT_LOGV("Set variant field type's tag field type: variant-ft-addr=%p, "
3163 "tag-ft-addr=%p", ft
, tag_ft
);
3170 void bt_field_type_generic_freeze(struct bt_field_type
*ft
)
3176 void bt_field_type_enumeration_freeze_recursive(
3177 struct bt_field_type
*ft
)
3179 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
3181 BT_LOGD("Freezing enumeration field type object: addr=%p", ft
);
3182 bt_field_type_enumeration_set_range_overlap(enum_ft
);
3183 bt_field_type_generic_freeze(ft
);
3184 BT_LOGD("Freezing enumeration field type object's container field type: int-ft-addr=%p",
3185 enum_ft
->container_ft
);
3186 bt_field_type_freeze((void *) enum_ft
->container_ft
);
3190 void bt_field_type_structure_freeze_recursive(
3191 struct bt_field_type
*ft
)
3193 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
3196 /* Cache the alignment */
3197 BT_LOGD("Freezing structure field type object: addr=%p", ft
);
3198 ft
->alignment
= bt_field_type_get_alignment(ft
);
3199 bt_field_type_generic_freeze(ft
);
3201 for (i
= 0; i
< struct_ft
->fields
->len
; i
++) {
3202 struct bt_field_type_structure_field
*field
=
3203 BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(ft
, i
);
3205 BT_LOGD("Freezing structure field type field: "
3206 "ft-addr=%p, name=\"%s\"",
3207 field
->type
, g_quark_to_string(field
->name
));
3208 bt_field_type_freeze(field
->type
);
3213 int bt_field_type_variant_update_choices(struct bt_field_type
*ft
)
3215 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3220 if (ft
->frozen
&& var_ft
->choices_up_to_date
) {
3224 BT_ASSERT(var_ft
->tag_ft
);
3225 is_signed
= !!var_ft
->tag_ft
->container_ft
->is_signed
;
3227 for (i
= 0; i
< var_ft
->choices
->len
; i
++) {
3228 struct bt_field_type_variant_choice
*choice
=
3229 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(ft
, i
);
3230 const char *choice_name
= g_quark_to_string(choice
->name
);
3231 struct bt_field_type_enumeration_mapping_iterator
*iter
=
3232 bt_field_type_enumeration_find_mappings_by_name(
3233 (void *) var_ft
->tag_ft
, choice_name
);
3240 BT_ASSERT(choice
->ranges
);
3241 g_array_set_size(choice
->ranges
, 0);
3243 while (bt_field_type_enumeration_mapping_iterator_next(iter
) == 0) {
3244 struct bt_field_type_variant_choice_range range
;
3247 ret
= bt_field_type_enumeration_mapping_iterator_signed_get(
3249 &range
.lower
.i
, &range
.upper
.i
);
3251 ret
= bt_field_type_enumeration_mapping_iterator_unsigned_get(
3253 &range
.lower
.u
, &range
.upper
.u
);
3256 BT_ASSERT(ret
== 0);
3257 g_array_append_val(choice
->ranges
, range
);
3263 var_ft
->choices_up_to_date
= true;
3270 void bt_field_type_variant_freeze_recursive(
3271 struct bt_field_type
*ft
)
3273 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3276 BT_LOGD("Freezing variant field type object: addr=%p", ft
);
3277 bt_field_type_generic_freeze(ft
);
3279 for (i
= 0; i
< var_ft
->choices
->len
; i
++) {
3280 struct bt_field_type_variant_choice
*choice
=
3281 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(ft
, i
);
3283 BT_LOGD("Freezing variant field type member: "
3284 "ft-addr=%p, name=\"%s\"",
3285 choice
->type
, g_quark_to_string(choice
->name
));
3286 bt_field_type_freeze(choice
->type
);
3291 void bt_field_type_array_freeze_recursive(
3292 struct bt_field_type
*ft
)
3294 struct bt_field_type_array
*array_ft
= (void *) ft
;
3296 /* Cache the alignment */
3297 BT_LOGD("Freezing array field type object: addr=%p", ft
);
3298 ft
->alignment
= bt_field_type_get_alignment(ft
);
3299 bt_field_type_generic_freeze(ft
);
3300 BT_LOGD("Freezing array field type object's element field type: element-ft-addr=%p",
3301 array_ft
->element_ft
);
3302 bt_field_type_freeze(array_ft
->element_ft
);
3306 void bt_field_type_sequence_freeze_recursive(
3307 struct bt_field_type
*ft
)
3309 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
3311 /* Cache the alignment */
3312 BT_LOGD("Freezing sequence field type object: addr=%p", ft
);
3313 ft
->alignment
= bt_field_type_get_alignment(ft
);
3314 bt_field_type_generic_freeze(ft
);
3315 BT_LOGD("Freezing sequence field type object's element field type: element-ft-addr=%p",
3316 seq_ft
->element_ft
);
3317 bt_field_type_freeze(seq_ft
->element_ft
);
3321 void bt_field_type_integer_set_byte_order(
3322 struct bt_field_type
*ft
, enum bt_byte_order byte_order
)
3324 struct bt_field_type_integer
*int_ft
= (void *) ft
;
3326 int_ft
->user_byte_order
= byte_order
;
3330 void bt_field_type_enumeration_set_byte_order_recursive(
3331 struct bt_field_type
*ft
, enum bt_byte_order byte_order
)
3333 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
3335 bt_field_type_set_byte_order((void *) enum_ft
->container_ft
,
3340 void bt_field_type_floating_point_set_byte_order(
3341 struct bt_field_type
*ft
, enum bt_byte_order byte_order
)
3343 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
3345 flt_ft
->user_byte_order
= byte_order
;
3349 void bt_field_type_structure_set_byte_order_recursive(
3350 struct bt_field_type
*ft
,
3351 enum bt_byte_order byte_order
)
3354 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
3356 for (i
= 0; i
< struct_ft
->fields
->len
; i
++) {
3357 struct bt_field_type_structure_field
*field
=
3358 BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
3360 struct bt_field_type
*field_type
= field
->type
;
3362 bt_field_type_set_byte_order(field_type
, byte_order
);
3367 void bt_field_type_variant_set_byte_order_recursive(
3368 struct bt_field_type
*ft
,
3369 enum bt_byte_order byte_order
)
3372 struct bt_field_type_variant
*var_ft
= (void *) ft
;
3374 for (i
= 0; i
< var_ft
->choices
->len
; i
++) {
3375 struct bt_field_type_variant_choice
*choice
=
3376 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
3378 struct bt_field_type
*field_type
= choice
->type
;
3380 bt_field_type_set_byte_order(field_type
, byte_order
);
3385 void bt_field_type_array_set_byte_order_recursive(
3386 struct bt_field_type
*ft
,
3387 enum bt_byte_order byte_order
)
3389 struct bt_field_type_array
*array_ft
= (void *) ft
;
3391 bt_field_type_set_byte_order(array_ft
->element_ft
, byte_order
);
3395 void bt_field_type_sequence_set_byte_order_recursive(
3396 struct bt_field_type
*ft
,
3397 enum bt_byte_order byte_order
)
3399 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
3401 bt_field_type_set_byte_order(seq_ft
->element_ft
, byte_order
);
3406 int bt_field_type_integer_compare(struct bt_field_type
*ft_a
,
3407 struct bt_field_type
*ft_b
)
3410 struct bt_field_type_integer
*int_ft_a
= (void *) ft_a
;
3411 struct bt_field_type_integer
*int_ft_b
= (void *) ft_b
;
3414 if (int_ft_a
->size
!= int_ft_b
->size
) {
3415 BT_LOGV("Integer field types differ: different sizes: "
3416 "ft-a-size=%u, ft-b-size=%u",
3417 int_ft_a
->size
, int_ft_b
->size
);
3422 if (int_ft_a
->user_byte_order
!= int_ft_b
->user_byte_order
) {
3423 BT_LOGV("Integer field types differ: different byte orders: "
3424 "ft-a-bo=%s, ft-b-bo=%s",
3425 bt_common_byte_order_string(int_ft_a
->user_byte_order
),
3426 bt_common_byte_order_string(int_ft_b
->user_byte_order
));
3431 if (int_ft_a
->is_signed
!= int_ft_b
->is_signed
) {
3432 BT_LOGV("Integer field types differ: different signedness: "
3433 "ft-a-is-signed=%d, ft-b-is-signed=%d",
3434 int_ft_a
->is_signed
,
3435 int_ft_b
->is_signed
);
3440 if (int_ft_a
->base
!= int_ft_b
->base
) {
3441 BT_LOGV("Integer field types differ: different bases: "
3442 "ft-a-base=%s, ft-b-base=%s",
3443 bt_common_integer_base_string(int_ft_a
->base
),
3444 bt_common_integer_base_string(int_ft_b
->base
));
3449 if (int_ft_a
->encoding
!= int_ft_b
->encoding
) {
3450 BT_LOGV("Integer field types differ: different encodings: "
3451 "ft-a-encoding=%s, ft-b-encoding=%s",
3452 bt_common_string_encoding_string(int_ft_a
->encoding
),
3453 bt_common_string_encoding_string(int_ft_b
->encoding
));
3457 /* Mapped clock class */
3458 if (int_ft_a
->mapped_clock_class
) {
3459 if (!int_ft_b
->mapped_clock_class
) {
3460 BT_LOGV_STR("Integer field types differ: field type A "
3461 "has a mapped clock class, but field type B "
3466 if (bt_clock_class_compare(int_ft_a
->mapped_clock_class
,
3467 int_ft_b
->mapped_clock_class
) != 0) {
3468 BT_LOGV_STR("Integer field types differ: different "
3469 "mapped clock classes.");
3472 if (int_ft_b
->mapped_clock_class
) {
3473 BT_LOGV_STR("Integer field types differ: field type A "
3474 "has no description, but field type B has one.");
3487 int bt_field_type_floating_point_compare(
3488 struct bt_field_type
*ft_a
,
3489 struct bt_field_type
*ft_b
)
3492 struct bt_field_type_floating_point
*flt_ft_a
= (void *) ft_a
;
3493 struct bt_field_type_floating_point
*flt_ft_b
= (void *) ft_b
;
3496 if (flt_ft_a
->user_byte_order
!= flt_ft_b
->user_byte_order
) {
3497 BT_LOGV("Floating point number field types differ: different byte orders: "
3498 "ft-a-bo=%s, ft-b-bo=%s",
3499 bt_common_byte_order_string(flt_ft_a
->user_byte_order
),
3500 bt_common_byte_order_string(flt_ft_b
->user_byte_order
));
3504 /* Exponent length */
3505 if (flt_ft_a
->exp_dig
!= flt_ft_b
->exp_dig
) {
3506 BT_LOGV("Floating point number field types differ: different exponent sizes: "
3507 "ft-a-exp-size=%u, ft-b-exp-size=%u",
3508 flt_ft_a
->exp_dig
, flt_ft_b
->exp_dig
);
3512 /* Mantissa length */
3513 if (flt_ft_a
->mant_dig
!= flt_ft_b
->mant_dig
) {
3514 BT_LOGV("Floating point number field types differ: different mantissa sizes: "
3515 "ft-a-mant-size=%u, ft-b-mant-size=%u",
3516 flt_ft_a
->mant_dig
, flt_ft_b
->mant_dig
);
3528 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3529 struct enumeration_mapping
*mapping_b
)
3534 if (mapping_a
->string
!= mapping_b
->string
) {
3535 BT_LOGV("Enumeration field type mappings differ: different names: "
3536 "mapping-a-name=\"%s\", mapping-b-name=\"%s\"",
3537 g_quark_to_string(mapping_a
->string
),
3538 g_quark_to_string(mapping_b
->string
));
3543 if (mapping_a
->range_start
._unsigned
!=
3544 mapping_b
->range_start
._unsigned
) {
3545 BT_LOGV("Enumeration field type mappings differ: different starts of range: "
3546 "mapping-a-range-start-unsigned=%" PRIu64
", "
3547 "mapping-b-range-start-unsigned=%" PRIu64
,
3548 mapping_a
->range_start
._unsigned
,
3549 mapping_b
->range_start
._unsigned
);
3554 if (mapping_a
->range_end
._unsigned
!=
3555 mapping_b
->range_end
._unsigned
) {
3556 BT_LOGV("Enumeration field type mappings differ: different ends of range: "
3557 "mapping-a-range-end-unsigned=%" PRIu64
", "
3558 "mapping-b-range-end-unsigned=%" PRIu64
,
3559 mapping_a
->range_end
._unsigned
,
3560 mapping_b
->range_end
._unsigned
);
3572 int bt_field_type_enumeration_compare_recursive(
3573 struct bt_field_type
*ft_a
,
3574 struct bt_field_type
*ft_b
)
3578 struct bt_field_type_enumeration
*enum_ft_a
= (void *) ft_a
;
3579 struct bt_field_type_enumeration
*enum_ft_b
= (void *) ft_b
;
3581 /* Container field type */
3582 ret
= bt_field_type_compare((void *) enum_ft_a
->container_ft
,
3583 (void *) enum_ft_b
->container_ft
);
3585 BT_LOGV("Enumeration field types differ: different container field types: "
3586 "ft-a-container-ft-addr=%p, ft-b-container-ft-addr=%p",
3587 enum_ft_a
->container_ft
, enum_ft_b
->container_ft
);
3594 if (enum_ft_a
->entries
->len
!= enum_ft_b
->entries
->len
) {
3598 for (i
= 0; i
< enum_ft_a
->entries
->len
; ++i
) {
3599 struct enumeration_mapping
*mapping_a
=
3600 g_ptr_array_index(enum_ft_a
->entries
, i
);
3601 struct enumeration_mapping
*mapping_b
=
3602 g_ptr_array_index(enum_ft_b
->entries
, i
);
3604 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3605 BT_LOGV("Enumeration field types differ: different mappings: "
3606 "ft-a-mapping-addr=%p, ft-b-mapping-addr=%p, "
3607 "ft-a-mapping-name=\"%s\", ft-b-mapping-name=\"%s\"",
3608 mapping_a
, mapping_b
,
3609 g_quark_to_string(mapping_a
->string
),
3610 g_quark_to_string(mapping_b
->string
));
3623 int bt_field_type_string_compare(struct bt_field_type
*ft_a
,
3624 struct bt_field_type
*ft_b
)
3627 struct bt_field_type_string
*string_ft_a
= (void *) ft_a
;
3628 struct bt_field_type_string
*string_ft_b
= (void *) ft_b
;
3631 if (string_ft_a
->encoding
!= string_ft_b
->encoding
) {
3632 BT_LOGV("String field types differ: different encodings: "
3633 "ft-a-encoding=%s, ft-b-encoding=%s",
3634 bt_common_string_encoding_string(string_ft_a
->encoding
),
3635 bt_common_string_encoding_string(string_ft_b
->encoding
));
3647 int compare_structure_variant_members(
3648 struct bt_field_type
*member_a_ft
,
3649 struct bt_field_type
*member_b_ft
,
3650 GQuark member_a_name
, GQuark member_b_name
)
3655 if (member_a_name
!= member_b_name
) {
3656 BT_LOGV("Structure/variant field type fields differ: different names: "
3657 "field-a-name=%s, field-b-name=%s",
3658 g_quark_to_string(member_a_name
),
3659 g_quark_to_string(member_b_name
));
3664 ret
= bt_field_type_compare(member_a_ft
, member_b_ft
);
3666 BT_LOGV("Structure/variant field type fields differ: different field types: "
3667 "field-name=\"%s\", field-a-ft-addr=%p, field-b-ft-addr=%p",
3668 g_quark_to_string(member_a_name
),
3669 member_a_ft
, member_b_ft
);
3677 int bt_field_type_structure_compare_recursive(
3678 struct bt_field_type
*ft_a
,
3679 struct bt_field_type
*ft_b
)
3683 struct bt_field_type_structure
*struct_ft_a
= (void *) ft_a
;
3684 struct bt_field_type_structure
*struct_ft_b
= (void *) ft_b
;
3687 if (bt_field_type_get_alignment(ft_a
) !=
3688 bt_field_type_get_alignment(ft_b
)) {
3689 BT_LOGV("Structure field types differ: different alignments: "
3690 "ft-a-align=%u, ft-b-align=%u",
3691 bt_field_type_get_alignment(ft_a
),
3692 bt_field_type_get_alignment(ft_b
));
3697 if (struct_ft_a
->fields
->len
!= struct_ft_b
->fields
->len
) {
3698 BT_LOGV("Structure field types differ: different field counts: "
3699 "ft-a-field-count=%u, ft-b-field-count=%u",
3700 struct_ft_a
->fields
->len
, struct_ft_b
->fields
->len
);
3704 for (i
= 0; i
< struct_ft_a
->fields
->len
; ++i
) {
3705 struct bt_field_type_structure_field
*field_a
=
3706 BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
3708 struct bt_field_type_structure_field
*field_b
=
3709 BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
3712 ret
= compare_structure_variant_members(field_a
->type
,
3713 field_b
->type
, field_a
->name
, field_b
->name
);
3715 /* compare_structure_variant_members() logs what differs */
3716 BT_LOGV_STR("Structure field types differ: different fields.");
3729 int bt_field_type_variant_compare_recursive(
3730 struct bt_field_type
*ft_a
,
3731 struct bt_field_type
*ft_b
)
3735 struct bt_field_type_variant
*var_ft_a
= (void *) ft_a
;
3736 struct bt_field_type_variant
*var_ft_b
= (void *) ft_b
;
3739 if (strcmp(var_ft_a
->tag_name
->str
, var_ft_b
->tag_name
->str
)) {
3740 BT_LOGV("Variant field types differ: different tag field names: "
3741 "ft-a-tag-field-name=\"%s\", ft-b-tag-field-name=\"%s\"",
3742 var_ft_a
->tag_name
->str
, var_ft_b
->tag_name
->str
);
3747 ret
= bt_field_type_compare((void *) var_ft_a
->tag_ft
,
3748 (void *) var_ft_b
->tag_ft
);
3750 BT_LOGV("Variant field types differ: different tag field types: "
3751 "ft-a-tag-ft-addr=%p, ft-b-tag-ft-addr=%p",
3752 var_ft_a
->tag_ft
, var_ft_b
->tag_ft
);
3759 if (var_ft_a
->choices
->len
!= var_ft_b
->choices
->len
) {
3760 BT_LOGV("Variant field types differ: different field counts: "
3761 "ft-a-field-count=%u, ft-b-field-count=%u",
3762 var_ft_a
->choices
->len
, var_ft_b
->choices
->len
);
3766 for (i
= 0; i
< var_ft_a
->choices
->len
; ++i
) {
3767 struct bt_field_type_variant_choice
*choice_a
=
3768 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
3770 struct bt_field_type_variant_choice
*choice_b
=
3771 BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
3774 ret
= compare_structure_variant_members(choice_a
->type
,
3775 choice_b
->type
, choice_a
->name
, choice_b
->name
);
3777 /* compare_structure_variant_members() logs what differs */
3778 BT_LOGV_STR("Variant field types differ: different fields.");
3791 int bt_field_type_array_compare_recursive(
3792 struct bt_field_type
*ft_a
,
3793 struct bt_field_type
*ft_b
)
3796 struct bt_field_type_array
*array_ft_a
= (void *) ft_a
;
3797 struct bt_field_type_array
*array_ft_b
= (void *) ft_b
;
3800 if (array_ft_a
->length
!= array_ft_b
->length
) {
3801 BT_LOGV("Structure field types differ: different lengths: "
3802 "ft-a-length=%u, ft-b-length=%u",
3803 array_ft_a
->length
, array_ft_b
->length
);
3808 ret
= bt_field_type_compare(array_ft_a
->element_ft
,
3809 array_ft_b
->element_ft
);
3811 BT_LOGV("Array field types differ: different element field types: "
3812 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
3813 array_ft_a
->element_ft
, array_ft_b
->element_ft
);
3821 int bt_field_type_sequence_compare_recursive(
3822 struct bt_field_type
*ft_a
,
3823 struct bt_field_type
*ft_b
)
3826 struct bt_field_type_sequence
*seq_ft_a
= (void *) ft_a
;
3827 struct bt_field_type_sequence
*seq_ft_b
= (void *) ft_b
;
3830 if (strcmp(seq_ft_a
->length_field_name
->str
,
3831 seq_ft_b
->length_field_name
->str
)) {
3832 BT_LOGV("Sequence field types differ: different length field names: "
3833 "ft-a-length-field-name=\"%s\", "
3834 "ft-b-length-field-name=\"%s\"",
3835 seq_ft_a
->length_field_name
->str
,
3836 seq_ft_b
->length_field_name
->str
);
3841 ret
= bt_field_type_compare(seq_ft_a
->element_ft
,
3842 seq_ft_b
->element_ft
);
3844 BT_LOGV("Sequence field types differ: different element field types: "
3845 "ft-a-element-ft-addr=%p, ft-b-element-ft-addr=%p",
3846 seq_ft_a
->element_ft
, seq_ft_b
->element_ft
);
3854 int bt_field_type_compare(struct bt_field_type
*ft_a
,
3855 struct bt_field_type
*ft_b
)
3859 BT_ASSERT_PRE_NON_NULL(ft_a
, "Field type A");
3860 BT_ASSERT_PRE_NON_NULL(ft_b
, "Field type B");
3863 /* Same reference: equal (even if both are NULL) */
3869 BT_LOGW_STR("Invalid parameter: field type A is NULL.");
3875 BT_LOGW_STR("Invalid parameter: field type B is NULL.");
3880 if (ft_a
->id
!= ft_b
->id
) {
3881 /* Different type IDs */
3882 BT_LOGV("Field types differ: different IDs: "
3883 "ft-a-addr=%p, ft-b-addr=%p, "
3884 "ft-a-id=%s, ft-b-id=%s",
3886 bt_common_field_type_id_string(ft_a
->id
),
3887 bt_common_field_type_id_string(ft_b
->id
));
3891 if (ft_a
->id
== BT_FIELD_TYPE_ID_UNKNOWN
) {
3892 /* Both have unknown type IDs */
3893 BT_LOGW_STR("Invalid parameter: field type IDs are unknown.");
3897 BT_ASSERT(ft_a
->methods
->compare
);
3898 ret
= ft_a
->methods
->compare(ft_a
, ft_b
);
3900 BT_LOGV("Field types differ: ft-a-addr=%p, ft-b-addr=%p",
3909 int64_t bt_field_type_get_field_count(struct bt_field_type
*ft
)
3911 int64_t field_count
= -1;
3914 case BT_FIELD_TYPE_ID_STRUCT
:
3916 bt_field_type_structure_get_field_count(ft
);
3918 case BT_FIELD_TYPE_ID_VARIANT
:
3920 bt_field_type_variant_get_field_count(ft
);
3922 case BT_FIELD_TYPE_ID_ARRAY
:
3923 case BT_FIELD_TYPE_ID_SEQUENCE
:
3925 * Array and sequence types always contain a single member
3926 * (the element type).
3938 struct bt_field_type
*bt_field_type_borrow_field_at_index(
3939 struct bt_field_type
*ft
, int index
)
3941 struct bt_field_type
*field_type
= NULL
;
3944 case BT_FIELD_TYPE_ID_STRUCT
:
3946 int ret
= bt_field_type_structure_borrow_field_by_index(
3947 ft
, NULL
, &field_type
, index
);
3954 case BT_FIELD_TYPE_ID_VARIANT
:
3956 int ret
= bt_field_type_variant_borrow_field_by_index(
3957 ft
, NULL
, &field_type
, index
);
3964 case BT_FIELD_TYPE_ID_ARRAY
:
3966 bt_field_type_array_borrow_element_field_type(ft
);
3968 case BT_FIELD_TYPE_ID_SEQUENCE
:
3970 bt_field_type_sequence_borrow_element_field_type(ft
);
3981 int bt_field_type_get_field_index(struct bt_field_type
*ft
,
3984 int field_index
= -1;
3987 case BT_FIELD_TYPE_ID_STRUCT
:
3988 field_index
= bt_field_type_structure_get_field_name_index(
3991 case BT_FIELD_TYPE_ID_VARIANT
:
3992 field_index
= bt_field_type_variant_get_field_name_index(
4002 struct bt_field_path
*bt_field_type_variant_borrow_tag_field_path(
4003 struct bt_field_type
*ft
)
4005 struct bt_field_type_variant
*var_ft
= (void *) ft
;
4007 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
4008 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_VARIANT
,
4010 return var_ft
->tag_field_path
;
4013 struct bt_field_path
*bt_field_type_sequence_borrow_length_field_path(
4014 struct bt_field_type
*ft
)
4016 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
4018 BT_ASSERT_PRE_NON_NULL(ft
, "Field type");
4019 BT_ASSERT_PRE_FT_HAS_ID(ft
, BT_FIELD_TYPE_ID_SEQUENCE
,
4021 return seq_ft
->length_field_path
;
4025 int bt_field_type_validate_single_clock_class(
4026 struct bt_field_type
*ft
,
4027 struct bt_clock_class
**expected_clock_class
)
4035 BT_ASSERT(expected_clock_class
);
4038 case BT_FIELD_TYPE_ID_INTEGER
:
4040 struct bt_clock_class
*mapped_clock_class
=
4041 bt_field_type_integer_borrow_mapped_clock_class(ft
);
4043 if (!mapped_clock_class
) {
4047 if (!*expected_clock_class
) {
4048 /* Move reference to output parameter */
4049 *expected_clock_class
= bt_get(mapped_clock_class
);
4050 mapped_clock_class
= NULL
;
4051 BT_LOGV("Setting expected clock class: "
4052 "expected-clock-class-addr=%p",
4053 *expected_clock_class
);
4055 if (mapped_clock_class
!= *expected_clock_class
) {
4056 BT_LOGW("Integer field type is not mapped to "
4057 "the expected clock class: "
4058 "mapped-clock-class-addr=%p, "
4059 "mapped-clock-class-name=\"%s\", "
4060 "expected-clock-class-addr=%p, "
4061 "expected-clock-class-name=\"%s\"",
4063 bt_clock_class_get_name(mapped_clock_class
),
4064 *expected_clock_class
,
4065 bt_clock_class_get_name(*expected_clock_class
));
4066 bt_put(mapped_clock_class
);
4074 case BT_FIELD_TYPE_ID_ENUM
:
4075 case BT_FIELD_TYPE_ID_ARRAY
:
4076 case BT_FIELD_TYPE_ID_SEQUENCE
:
4078 struct bt_field_type
*sub_ft
= NULL
;
4081 case BT_FIELD_TYPE_ID_ENUM
:
4082 sub_ft
= bt_field_type_enumeration_borrow_container_field_type(
4085 case BT_FIELD_TYPE_ID_ARRAY
:
4086 sub_ft
= bt_field_type_array_borrow_element_field_type(
4089 case BT_FIELD_TYPE_ID_SEQUENCE
:
4090 sub_ft
= bt_field_type_sequence_borrow_element_field_type(
4094 BT_LOGF("Unexpected field type ID: id=%d", ft
->id
);
4099 ret
= bt_field_type_validate_single_clock_class(sub_ft
,
4100 expected_clock_class
);
4103 case BT_FIELD_TYPE_ID_STRUCT
:
4106 int64_t count
= bt_field_type_structure_get_field_count(
4109 for (i
= 0; i
< count
; i
++) {
4111 struct bt_field_type
*member_type
;
4113 ret
= bt_field_type_structure_borrow_field_by_index(
4114 ft
, &name
, &member_type
, i
);
4115 BT_ASSERT(ret
== 0);
4116 ret
= bt_field_type_validate_single_clock_class(
4117 member_type
, expected_clock_class
);
4119 BT_LOGW("Structure field type's field's type "
4120 "is not recursively mapped to the "
4121 "expected clock class: "
4122 "field-ft-addr=%p, field-name=\"%s\"",
4129 case BT_FIELD_TYPE_ID_VARIANT
:
4132 int64_t count
= bt_field_type_variant_get_field_count(
4135 for (i
= 0; i
< count
; i
++) {
4137 struct bt_field_type
*member_type
;
4139 ret
= bt_field_type_variant_borrow_field_by_index(
4140 ft
, &name
, &member_type
, i
);
4141 BT_ASSERT(ret
== 0);
4142 ret
= bt_field_type_validate_single_clock_class(
4143 member_type
, expected_clock_class
);
4145 BT_LOGW("Variant field type's field's type "
4146 "is not recursively mapped to the "
4147 "expected clock class: "
4148 "field-ft-addr=%p, field-name=\"%s\"",
4164 struct bt_field_type
*bt_field_type_integer_copy(
4165 struct bt_field_type
*ft
)
4167 struct bt_field_type_integer
*int_ft
= (void *) ft
;
4168 struct bt_field_type_integer
*copy_ft
;
4170 BT_LOGD("Copying integer field type's: addr=%p", ft
);
4171 copy_ft
= (void *) bt_field_type_integer_create(int_ft
->size
);
4173 BT_LOGE_STR("Cannot create integer field type.");
4177 copy_ft
->mapped_clock_class
= bt_get(int_ft
->mapped_clock_class
);
4178 copy_ft
->user_byte_order
= int_ft
->user_byte_order
;
4179 copy_ft
->is_signed
= int_ft
->is_signed
;
4180 copy_ft
->size
= int_ft
->size
;
4181 copy_ft
->base
= int_ft
->base
;
4182 copy_ft
->encoding
= int_ft
->encoding
;
4183 BT_LOGD("Copied integer field type: original-ft-addr=%p, copy-ft-addr=%p",
4187 return (void *) copy_ft
;
4191 struct bt_field_type
*bt_field_type_enumeration_copy_recursive(
4192 struct bt_field_type
*ft
)
4195 struct bt_field_type_enumeration
*enum_ft
= (void *) ft
;
4196 struct bt_field_type_enumeration
*copy_ft
= NULL
;
4197 struct bt_field_type_enumeration
*container_copy_ft
;
4199 BT_LOGD("Copying enumeration field type's: addr=%p", ft
);
4201 /* Copy the source enumeration's container */
4202 BT_LOGD_STR("Copying enumeration field type's container field type.");
4204 (void *) bt_field_type_copy((void *) enum_ft
->container_ft
);
4205 if (!container_copy_ft
) {
4206 BT_LOGE_STR("Cannot copy enumeration field type's container field type.");
4210 copy_ft
= (void *) bt_field_type_enumeration_create(
4211 (void *) container_copy_ft
);
4213 BT_LOGE_STR("Cannot create enumeration field type.");
4217 /* Copy all enumaration entries */
4218 for (i
= 0; i
< enum_ft
->entries
->len
; i
++) {
4219 struct enumeration_mapping
*mapping
= g_ptr_array_index(
4220 enum_ft
->entries
, i
);
4221 struct enumeration_mapping
*copy_mapping
= g_new0(
4222 struct enumeration_mapping
, 1);
4224 if (!copy_mapping
) {
4225 BT_LOGE_STR("Failed to allocate one enumeration mapping.");
4229 *copy_mapping
= *mapping
;
4230 g_ptr_array_add(copy_ft
->entries
, copy_mapping
);
4233 BT_LOGD("Copied enumeration field type: original-ft-addr=%p, copy-ft-addr=%p",
4237 bt_put(container_copy_ft
);
4238 return (void *) copy_ft
;
4241 bt_put(container_copy_ft
);
4243 return (void *) copy_ft
;
4247 struct bt_field_type
*bt_field_type_floating_point_copy(
4248 struct bt_field_type
*ft
)
4250 struct bt_field_type_floating_point
*flt_ft
= (void *) ft
;
4251 struct bt_field_type_floating_point
*copy_ft
;
4253 BT_LOGD("Copying floating point number field type's: addr=%p", ft
);
4254 copy_ft
= (void *) bt_field_type_floating_point_create();
4256 BT_LOGE_STR("Cannot create floating point number field type.");
4260 copy_ft
->user_byte_order
= flt_ft
->user_byte_order
;
4261 copy_ft
->exp_dig
= flt_ft
->exp_dig
;
4262 copy_ft
->mant_dig
= flt_ft
->mant_dig
;
4263 BT_LOGD("Copied floating point number field type: original-ft-addr=%p, copy-ft-addr=%p",
4267 return (void *) copy_ft
;
4271 struct bt_field_type
*bt_field_type_structure_copy_recursive(
4272 struct bt_field_type
*ft
)
4275 GHashTableIter iter
;
4276 gpointer key
, value
;
4277 struct bt_field_type_structure
*struct_ft
= (void *) ft
;
4278 struct bt_field_type_structure
*copy_ft
;
4280 BT_LOGD("Copying structure field type's: addr=%p", ft
);
4281 copy_ft
= (void *) bt_field_type_structure_create();
4283 BT_LOGE_STR("Cannot create structure field type.");
4287 /* Copy field_name_to_index */
4288 g_hash_table_iter_init(&iter
, struct_ft
->field_name_to_index
);
4289 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
4290 g_hash_table_insert(copy_ft
->field_name_to_index
,
4294 g_array_set_size(copy_ft
->fields
, struct_ft
->fields
->len
);
4296 for (i
= 0; i
< struct_ft
->fields
->len
; i
++) {
4297 struct bt_field_type_structure_field
*entry
, *copy_entry
;
4298 struct bt_field_type
*field_ft_copy
;
4300 entry
= BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
4302 copy_entry
= BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(
4304 BT_LOGD("Copying structure field type's field: "
4305 "index=%" PRId64
", "
4306 "field-ft-addr=%p, field-name=\"%s\"",
4307 i
, entry
, g_quark_to_string(entry
->name
));
4309 field_ft_copy
= (void *) bt_field_type_copy(
4310 (void *) entry
->type
);
4311 if (!field_ft_copy
) {
4312 BT_LOGE("Cannot copy structure field type's field: "
4313 "index=%" PRId64
", "
4314 "field-ft-addr=%p, field-name=\"%s\"",
4315 i
, entry
, g_quark_to_string(entry
->name
));
4319 copy_entry
->name
= entry
->name
;
4320 copy_entry
->type
= field_ft_copy
;
4323 BT_LOGD("Copied structure field type: original-ft-addr=%p, copy-ft-addr=%p",
4327 return (void *) copy_ft
;
4335 struct bt_field_type
*bt_field_type_variant_copy_recursive(
4336 struct bt_field_type
*ft
)
4339 GHashTableIter iter
;
4340 gpointer key
, value
;
4341 struct bt_field_type
*tag_ft_copy
= NULL
;
4342 struct bt_field_type_variant
*var_ft
= (void *) ft
;
4343 struct bt_field_type_variant
*copy_ft
= NULL
;
4345 BT_LOGD("Copying variant field type's: addr=%p", ft
);
4346 if (var_ft
->tag_ft
) {
4347 BT_LOGD_STR("Copying variant field type's tag field type.");
4348 tag_ft_copy
= bt_field_type_copy((void *) var_ft
->tag_ft
);
4350 BT_LOGE_STR("Cannot copy variant field type's tag field type.");
4355 copy_ft
= (void *) bt_field_type_variant_create(
4356 (void *) tag_ft_copy
,
4357 var_ft
->tag_name
->len
? var_ft
->tag_name
->str
: NULL
);
4359 BT_LOGE_STR("Cannot create variant field type.");
4363 /* Copy field_name_to_index */
4364 g_hash_table_iter_init(&iter
, var_ft
->choice_name_to_index
);
4365 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
4366 g_hash_table_insert(copy_ft
->choice_name_to_index
,
4370 g_array_set_size(copy_ft
->choices
, var_ft
->choices
->len
);
4372 for (i
= 0; i
< var_ft
->choices
->len
; i
++) {
4373 struct bt_field_type_variant_choice
*entry
, *copy_entry
;
4374 struct bt_field_type
*field_ft_copy
;
4377 entry
= BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(var_ft
, i
);
4378 copy_entry
= BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(
4380 BT_LOGD("Copying variant field type's field: "
4381 "index=%" PRId64
", "
4382 "field-ft-addr=%p, field-name=\"%s\"",
4383 i
, entry
, g_quark_to_string(entry
->name
));
4385 field_ft_copy
= (void *) bt_field_type_copy(
4386 (void *) entry
->type
);
4387 if (!field_ft_copy
) {
4388 BT_LOGE("Cannot copy variant field type's field: "
4389 "index=%" PRId64
", "
4390 "field-ft-addr=%p, field-name=\"%s\"",
4391 i
, entry
, g_quark_to_string(entry
->name
));
4396 copy_entry
->name
= entry
->name
;
4397 copy_entry
->type
= field_ft_copy
;
4400 copy_entry
->ranges
= g_array_new(FALSE
, TRUE
,
4401 sizeof(struct bt_field_type_variant_choice_range
));
4402 BT_ASSERT(copy_entry
->ranges
);
4403 g_array_set_size(copy_entry
->ranges
, entry
->ranges
->len
);
4405 for (range_i
= 0; range_i
< entry
->ranges
->len
; range_i
++) {
4406 copy_entry
->ranges
[range_i
] = entry
->ranges
[range_i
];
4410 if (var_ft
->tag_field_path
) {
4411 BT_LOGD_STR("Copying variant field type's tag field path.");
4412 copy_ft
->tag_field_path
= bt_field_path_copy(
4413 var_ft
->tag_field_path
);
4414 if (!copy_ft
->tag_field_path
) {
4415 BT_LOGE_STR("Cannot copy variant field type's tag field path.");
4420 copy_ft
->choices_up_to_date
= var_ft
->choices_up_to_date
;
4421 BT_LOGD("Copied variant field type: original-ft-addr=%p, copy-ft-addr=%p",
4425 bt_put(tag_ft_copy
);
4426 return (void *) copy_ft
;
4429 bt_put(tag_ft_copy
);
4435 struct bt_field_type
*bt_field_type_array_copy_recursive(
4436 struct bt_field_type
*ft
)
4438 struct bt_field_type
*container_ft_copy
= NULL
;
4439 struct bt_field_type_array
*array_ft
= (void *) ft
;
4440 struct bt_field_type_array
*copy_ft
= NULL
;
4442 BT_LOGD("Copying array field type's: addr=%p", ft
);
4443 BT_LOGD_STR("Copying array field type's element field type.");
4444 container_ft_copy
= bt_field_type_copy(array_ft
->element_ft
);
4445 if (!container_ft_copy
) {
4446 BT_LOGE_STR("Cannot copy array field type's element field type.");
4450 copy_ft
= (void *) bt_field_type_array_create(
4451 (void *) container_ft_copy
, array_ft
->length
);
4453 BT_LOGE_STR("Cannot create array field type.");
4457 BT_LOGD("Copied array field type: original-ft-addr=%p, copy-ft-addr=%p",
4461 bt_put(container_ft_copy
);
4462 return (void *) copy_ft
;
4466 struct bt_field_type
*bt_field_type_sequence_copy_recursive(
4467 struct bt_field_type
*ft
)
4469 struct bt_field_type
*container_ft_copy
= NULL
;
4470 struct bt_field_type_sequence
*seq_ft
= (void *) ft
;
4471 struct bt_field_type_sequence
*copy_ft
= NULL
;
4473 BT_LOGD("Copying sequence field type's: addr=%p", ft
);
4474 BT_LOGD_STR("Copying sequence field type's element field type.");
4475 container_ft_copy
= bt_field_type_copy(seq_ft
->element_ft
);
4476 if (!container_ft_copy
) {
4477 BT_LOGE_STR("Cannot copy sequence field type's element field type.");
4481 copy_ft
= (void *) bt_field_type_sequence_create(
4482 (void *) container_ft_copy
,
4483 seq_ft
->length_field_name
->len
?
4484 seq_ft
->length_field_name
->str
: NULL
);
4486 BT_LOGE_STR("Cannot create sequence field type.");
4490 if (seq_ft
->length_field_path
) {
4491 BT_LOGD_STR("Copying sequence field type's length field path.");
4492 copy_ft
->length_field_path
= bt_field_path_copy(
4493 seq_ft
->length_field_path
);
4494 if (!copy_ft
->length_field_path
) {
4495 BT_LOGE_STR("Cannot copy sequence field type's length field path.");
4500 BT_LOGD("Copied sequence field type: original-ft-addr=%p, copy-ft-addr=%p",
4504 bt_put(container_ft_copy
);
4505 return (void *) copy_ft
;
4507 bt_put(container_ft_copy
);
4513 struct bt_field_type
*bt_field_type_string_copy(struct bt_field_type
*ft
)
4515 struct bt_field_type_string
*string_ft
= (void *) ft
;
4516 struct bt_field_type_string
*copy_ft
= NULL
;
4518 BT_LOGD("Copying string field type's: addr=%p", ft
);
4519 copy_ft
= (void *) bt_field_type_string_create();
4521 BT_LOGE_STR("Cannot create string field type.");
4525 copy_ft
->encoding
= string_ft
->encoding
;
4526 BT_LOGD("Copied string field type: original-ft-addr=%p, copy-ft-addr=%p",
4530 return (void *) copy_ft
;