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 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/field-path-internal.h>
32 #include <babeltrace/ctf-ir/utils.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/endian.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
81 bt_ctf_field_type_enumeration_destroy
,
83 bt_ctf_field_type_floating_point_destroy
,
84 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
105 type_freeze_func
const type_freeze_funcs
[] = {
106 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
107 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
108 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
109 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
110 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
111 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
112 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
113 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
117 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
118 struct metadata_context
*);
120 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
121 struct metadata_context
*);
123 int bt_ctf_field_type_floating_point_serialize(
124 struct bt_ctf_field_type
*, struct metadata_context
*);
126 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
127 struct metadata_context
*);
129 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
130 struct metadata_context
*);
132 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
133 struct metadata_context
*);
135 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
136 struct metadata_context
*);
138 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
139 struct metadata_context
*);
142 type_serialize_func
const type_serialize_funcs
[] = {
143 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
145 bt_ctf_field_type_enumeration_serialize
,
147 bt_ctf_field_type_floating_point_serialize
,
149 bt_ctf_field_type_structure_serialize
,
150 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
151 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
152 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
153 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
157 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
158 int byte_order
, int set_native
);
160 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
161 int byte_order
, int set_native
);
163 void bt_ctf_field_type_floating_point_set_byte_order(
164 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
166 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
167 int byte_order
, int set_native
);
169 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
170 int byte_order
, int set_native
);
172 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
173 int byte_order
, int set_native
);
175 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
176 int byte_order
, int set_native
);
178 /* The set_native flag only set the byte order if it is set to native */
180 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
181 int byte_order
, int set_native
) = {
182 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
184 bt_ctf_field_type_enumeration_set_byte_order
,
186 bt_ctf_field_type_floating_point_set_byte_order
,
188 bt_ctf_field_type_structure_set_byte_order
,
189 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
190 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
191 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
192 [CTF_TYPE_STRING
] = NULL
,
196 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
197 struct bt_ctf_field_type
*);
199 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
200 struct bt_ctf_field_type
*);
202 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
203 struct bt_ctf_field_type
*);
205 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
206 struct bt_ctf_field_type
*);
208 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
209 struct bt_ctf_field_type
*);
211 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
212 struct bt_ctf_field_type
*);
214 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
215 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
218 struct bt_ctf_field_type
*);
221 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
222 struct bt_ctf_field_type
*) = {
223 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
224 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
225 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
226 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
227 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
228 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
229 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
230 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
234 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
235 struct bt_ctf_field_type
*);
237 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
238 struct bt_ctf_field_type
*);
240 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
241 struct bt_ctf_field_type
*);
243 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
244 struct bt_ctf_field_type
*);
246 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
247 struct bt_ctf_field_type
*);
249 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
250 struct bt_ctf_field_type
*);
252 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
253 struct bt_ctf_field_type
*);
255 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
256 struct bt_ctf_field_type
*);
259 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
260 struct bt_ctf_field_type
*) = {
261 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_compare
,
262 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_compare
,
263 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
264 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_compare
,
265 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_compare
,
266 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_compare
,
267 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
268 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_compare
,
272 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
274 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
283 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
284 [CTF_TYPE_INTEGER
] = NULL
,
285 [CTF_TYPE_FLOAT
] = NULL
,
286 [CTF_TYPE_STRING
] = NULL
,
287 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_validate
,
288 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_validate
,
289 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_validate
,
290 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_validate
,
291 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
295 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
301 void destroy_structure_field(struct structure_field
*field
)
308 void check_ranges_overlap(gpointer element
, gpointer query
)
310 struct enumeration_mapping
*mapping
= element
;
311 struct range_overlap_query
*overlap_query
= query
;
313 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
314 && overlap_query
->range_start
._signed
<=
315 mapping
->range_end
._signed
) {
316 overlap_query
->overlaps
= 1;
317 overlap_query
->mapping_name
= mapping
->string
;
320 overlap_query
->overlaps
|=
321 mapping
->string
== overlap_query
->mapping_name
;
325 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
327 struct enumeration_mapping
*mapping
= element
;
328 struct range_overlap_query
*overlap_query
= query
;
330 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
331 && overlap_query
->range_start
._unsigned
<=
332 mapping
->range_end
._unsigned
) {
333 overlap_query
->overlaps
= 1;
334 overlap_query
->mapping_name
= mapping
->string
;
337 overlap_query
->overlaps
|=
338 mapping
->string
== overlap_query
->mapping_name
;
342 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
343 struct enumeration_mapping
**b
)
345 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
349 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
350 struct enumeration_mapping
**b
)
352 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
356 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
358 enum ctf_type_id type_id
= type
->declaration
->id
;
360 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
361 (type_id
< NR_CTF_TYPES
));
363 bt_object_init(type
, bt_ctf_field_type_destroy
);
364 type
->freeze
= type_freeze_funcs
[type_id
];
365 type
->serialize
= type_serialize_funcs
[type_id
];
368 int ret
= bt_ctf_field_type_set_byte_order(type
,
369 BT_CTF_BYTE_ORDER_NATIVE
);
373 type
->declaration
->alignment
= 1;
377 int add_structure_field(GPtrArray
*fields
,
378 GHashTable
*field_name_to_index
,
379 struct bt_ctf_field_type
*field_type
,
380 const char *field_name
)
383 GQuark name_quark
= g_quark_from_string(field_name
);
384 struct structure_field
*field
;
386 /* Make sure structure does not contain a field of the same name */
387 if (g_hash_table_lookup_extended(field_name_to_index
,
388 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
393 field
= g_new0(struct structure_field
, 1);
400 field
->name
= name_quark
;
401 field
->type
= field_type
;
402 g_hash_table_insert(field_name_to_index
,
403 (gpointer
) (unsigned long) name_quark
,
404 (gpointer
) (unsigned long) fields
->len
);
405 g_ptr_array_add(fields
, field
);
411 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
413 struct bt_ctf_field_type
*type
;
414 enum ctf_type_id type_id
;
416 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
417 type_id
= type
->declaration
->id
;
418 if (type_id
<= CTF_TYPE_UNKNOWN
||
419 type_id
>= NR_CTF_TYPES
) {
423 type_destroy_funcs
[type_id
](type
);
427 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
431 struct bt_ctf_field_type_enumeration
*enumeration
=
432 container_of(type
, struct bt_ctf_field_type_enumeration
,
434 struct bt_ctf_field_type
*container_type
=
435 bt_ctf_field_type_enumeration_get_container_type(type
);
437 if (!container_type
) {
442 ret
= bt_ctf_field_type_validate(container_type
);
447 /* Ensure enum has entries */
448 ret
= enumeration
->entries
->len
? 0 : -1;
451 BT_PUT(container_type
);
456 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
459 struct bt_ctf_field_type
*element_type
= NULL
;
460 struct bt_ctf_field_type_sequence
*sequence
=
461 container_of(type
, struct bt_ctf_field_type_sequence
,
464 /* Length field name should be set at this point */
465 if (sequence
->length_field_name
->len
== 0) {
470 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
476 ret
= bt_ctf_field_type_validate(element_type
);
479 BT_PUT(element_type
);
485 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
488 struct bt_ctf_field_type
*element_type
= NULL
;
490 element_type
= bt_ctf_field_type_array_get_element_type(type
);
496 ret
= bt_ctf_field_type_validate(element_type
);
499 BT_PUT(element_type
);
505 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
508 struct bt_ctf_field_type
*child_type
= NULL
;
509 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
512 if (field_count
< 0) {
517 for (i
= 0; i
< field_count
; ++i
) {
518 ret
= bt_ctf_field_type_structure_get_field(type
,
519 NULL
, &child_type
, i
);
524 ret
= bt_ctf_field_type_validate(child_type
);
539 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
543 struct bt_ctf_field_type
*child_type
= NULL
;
544 struct bt_ctf_field_type_variant
*variant
=
545 container_of(type
, struct bt_ctf_field_type_variant
,
548 int tag_mappings_count
;
550 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
556 bt_ctf_field_type_enumeration_get_mapping_count(
557 (struct bt_ctf_field_type
*) variant
->tag
);
559 if (tag_mappings_count
!= variant
->fields
->len
) {
564 for (i
= 0; i
< tag_mappings_count
; ++i
) {
566 int64_t range_start
, range_end
;
567 struct bt_ctf_field_type
*ft
;
569 ret
= bt_ctf_field_type_enumeration_get_mapping(
570 (struct bt_ctf_field_type
*) variant
->tag
,
571 i
, &label
, &range_start
, &range_end
);
580 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
590 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
591 if (field_count
< 0) {
596 for (i
= 0; i
< field_count
; ++i
) {
597 ret
= bt_ctf_field_type_variant_get_field(type
,
598 NULL
, &child_type
, i
);
603 ret
= bt_ctf_field_type_validate(child_type
);
618 * This function validates a given field type without considering
619 * where this field type is located. It only validates the properties
620 * of the given field type and the properties of its children if
624 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
627 enum ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
635 /* Already marked as valid */
639 if (type_validate_funcs
[id
]) {
640 ret
= type_validate_funcs
[id
](type
);
643 if (!ret
&& type
->frozen
) {
644 /* Field type is valid */
652 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
654 struct bt_ctf_field_type_integer
*integer
=
655 g_new0(struct bt_ctf_field_type_integer
, 1);
657 if (!integer
|| size
== 0 || size
> 64) {
661 integer
->parent
.declaration
= &integer
->declaration
.p
;
662 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
663 integer
->declaration
.len
= size
;
664 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
665 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
666 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
667 return &integer
->parent
;
670 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
673 struct bt_ctf_field_type_integer
*integer
;
675 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
680 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
681 ret
= (int) integer
->declaration
.len
;
686 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
689 struct bt_ctf_field_type_integer
*integer
;
691 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
696 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
697 ret
= integer
->declaration
.signedness
;
702 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
706 struct bt_ctf_field_type_integer
*integer
;
708 if (!type
|| type
->frozen
||
709 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
714 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
715 integer
->declaration
.signedness
= !!is_signed
;
720 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
721 struct bt_ctf_field_type
*type
)
723 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
724 struct bt_ctf_field_type_integer
*integer
;
726 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
730 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
731 ret
= integer
->declaration
.base
;
736 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
737 enum bt_ctf_integer_base base
)
741 if (!type
|| type
->frozen
||
742 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
748 case BT_CTF_INTEGER_BASE_BINARY
:
749 case BT_CTF_INTEGER_BASE_OCTAL
:
750 case BT_CTF_INTEGER_BASE_DECIMAL
:
751 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
753 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
754 struct bt_ctf_field_type_integer
, parent
);
755 integer
->declaration
.base
= base
;
765 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
766 struct bt_ctf_field_type
*type
)
768 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
769 struct bt_ctf_field_type_integer
*integer
;
771 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
775 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
776 ret
= integer
->declaration
.encoding
;
781 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
782 enum bt_ctf_string_encoding encoding
)
785 struct bt_ctf_field_type_integer
*integer
;
787 if (!type
|| type
->frozen
||
788 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
789 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
790 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
795 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
796 integer
->declaration
.encoding
= encoding
;
801 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
802 struct bt_ctf_field_type
*type
)
804 struct bt_ctf_field_type_integer
*integer
;
805 struct bt_ctf_clock
*clock
= NULL
;
811 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
812 clock
= integer
->mapped_clock
;
818 int bt_ctf_field_type_integer_set_mapped_clock(
819 struct bt_ctf_field_type
*type
,
820 struct bt_ctf_clock
*clock
)
822 struct bt_ctf_field_type_integer
*integer
;
825 if (!type
|| type
->frozen
) {
830 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
831 bt_put(integer
->mapped_clock
);
833 integer
->mapped_clock
= clock
;
838 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
839 struct bt_ctf_field_type
*integer_container_type
)
841 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
843 if (!integer_container_type
) {
847 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
851 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
856 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
857 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
858 bt_get(integer_container_type
);
859 enumeration
->container
= integer_container_type
;
860 enumeration
->entries
= g_ptr_array_new_with_free_func(
861 (GDestroyNotify
)destroy_enumeration_mapping
);
862 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
863 return &enumeration
->parent
;
869 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
870 struct bt_ctf_field_type
*type
)
872 struct bt_ctf_field_type
*container_type
= NULL
;
873 struct bt_ctf_field_type_enumeration
*enumeration_type
;
879 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
883 enumeration_type
= container_of(type
,
884 struct bt_ctf_field_type_enumeration
, parent
);
885 container_type
= enumeration_type
->container
;
886 bt_get(container_type
);
888 return container_type
;
891 int bt_ctf_field_type_enumeration_add_mapping(
892 struct bt_ctf_field_type
*type
, const char *string
,
893 int64_t range_start
, int64_t range_end
)
897 struct enumeration_mapping
*mapping
;
898 struct bt_ctf_field_type_enumeration
*enumeration
;
899 struct range_overlap_query query
;
900 char *escaped_string
;
902 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
904 (range_end
< range_start
)) {
909 if (!string
|| strlen(string
) == 0) {
914 escaped_string
= g_strescape(string
, NULL
);
915 if (!escaped_string
) {
920 mapping_name
= g_quark_from_string(escaped_string
);
921 query
= (struct range_overlap_query
) {
922 .range_start
._signed
= range_start
,
923 .range_end
._signed
= range_end
,
924 .mapping_name
= mapping_name
,
926 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
929 /* Check that the range does not overlap with one already present */
930 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
931 if (query
.overlaps
) {
936 mapping
= g_new(struct enumeration_mapping
, 1);
942 *mapping
= (struct enumeration_mapping
) {
943 .range_start
._signed
= range_start
,
944 .range_end
._signed
= range_end
, .string
= mapping_name
};
945 g_ptr_array_add(enumeration
->entries
, mapping
);
946 g_ptr_array_sort(enumeration
->entries
,
947 (GCompareFunc
)compare_enumeration_mappings_signed
);
949 free(escaped_string
);
954 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
955 struct bt_ctf_field_type
*type
, const char *string
,
956 uint64_t range_start
, uint64_t range_end
)
960 struct enumeration_mapping
*mapping
;
961 struct bt_ctf_field_type_enumeration
*enumeration
;
962 struct range_overlap_query query
;
963 char *escaped_string
;
965 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
967 (range_end
< range_start
)) {
972 if (!string
|| strlen(string
) == 0) {
977 escaped_string
= g_strescape(string
, NULL
);
978 if (!escaped_string
) {
983 mapping_name
= g_quark_from_string(escaped_string
);
984 query
= (struct range_overlap_query
) {
985 .range_start
._unsigned
= range_start
,
986 .range_end
._unsigned
= range_end
,
987 .mapping_name
= mapping_name
,
989 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
992 /* Check that the range does not overlap with one already present */
993 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
995 if (query
.overlaps
) {
1000 mapping
= g_new(struct enumeration_mapping
, 1);
1006 *mapping
= (struct enumeration_mapping
) {
1007 .range_start
._unsigned
= range_start
,
1008 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1009 g_ptr_array_add(enumeration
->entries
, mapping
);
1010 g_ptr_array_sort(enumeration
->entries
,
1011 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1013 free(escaped_string
);
1018 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1019 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1022 const char *name
= NULL
;
1023 struct range_overlap_query query
=
1024 (struct range_overlap_query
) {
1025 .range_start
._unsigned
= value
,
1026 .range_end
._unsigned
= value
,
1029 g_ptr_array_foreach(enumeration_type
->entries
,
1030 check_ranges_overlap_unsigned
,
1032 if (!query
.overlaps
) {
1036 name
= g_quark_to_string(query
.mapping_name
);
1041 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1042 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1045 const char *name
= NULL
;
1046 struct range_overlap_query query
=
1047 (struct range_overlap_query
) {
1048 .range_start
._signed
= value
,
1049 .range_end
._signed
= value
,
1052 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1054 if (!query
.overlaps
) {
1058 name
= g_quark_to_string(query
.mapping_name
);
1063 int bt_ctf_field_type_enumeration_get_mapping_count(
1064 struct bt_ctf_field_type
*type
)
1067 struct bt_ctf_field_type_enumeration
*enumeration
;
1069 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1074 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1076 ret
= (int) enumeration
->entries
->len
;
1082 struct enumeration_mapping
*get_enumeration_mapping(
1083 struct bt_ctf_field_type
*type
, int index
)
1085 struct enumeration_mapping
*mapping
= NULL
;
1086 struct bt_ctf_field_type_enumeration
*enumeration
;
1088 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1090 if (index
>= enumeration
->entries
->len
) {
1094 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1099 int bt_ctf_field_type_enumeration_get_mapping(
1100 struct bt_ctf_field_type
*type
, int index
,
1101 const char **string
, int64_t *range_start
, int64_t *range_end
)
1103 struct enumeration_mapping
*mapping
;
1106 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1107 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1112 mapping
= get_enumeration_mapping(type
, index
);
1118 *string
= g_quark_to_string(mapping
->string
);
1119 *range_start
= mapping
->range_start
._signed
;
1120 *range_end
= mapping
->range_end
._signed
;
1125 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1126 struct bt_ctf_field_type
*type
, int index
,
1127 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1129 struct enumeration_mapping
*mapping
;
1132 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1133 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1138 mapping
= get_enumeration_mapping(type
, index
);
1144 *string
= g_quark_to_string(mapping
->string
);
1145 *range_start
= mapping
->range_start
._unsigned
;
1146 *range_end
= mapping
->range_end
._unsigned
;
1151 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1152 struct bt_ctf_field_type
*type
, const char *name
)
1155 struct bt_ctf_field_type_enumeration
*enumeration
;
1158 if (!type
|| !name
||
1159 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1164 name_quark
= g_quark_try_string(name
);
1170 enumeration
= container_of(type
,
1171 struct bt_ctf_field_type_enumeration
, parent
);
1172 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1173 struct enumeration_mapping
*mapping
=
1174 get_enumeration_mapping(type
, i
);
1176 if (mapping
->string
== name_quark
) {
1187 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1188 struct bt_ctf_field_type
*type
, int64_t value
)
1190 struct bt_ctf_field_type_enumeration
*enumeration
;
1193 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1198 enumeration
= container_of(type
,
1199 struct bt_ctf_field_type_enumeration
, parent
);
1200 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1201 struct enumeration_mapping
*mapping
=
1202 get_enumeration_mapping(type
, i
);
1204 if (value
>= mapping
->range_start
._signed
&&
1205 value
<= mapping
->range_end
._signed
) {
1216 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1217 struct bt_ctf_field_type
*type
, uint64_t value
)
1219 struct bt_ctf_field_type_enumeration
*enumeration
;
1222 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1227 enumeration
= container_of(type
,
1228 struct bt_ctf_field_type_enumeration
, parent
);
1229 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1230 struct enumeration_mapping
*mapping
=
1231 get_enumeration_mapping(type
, i
);
1233 if (value
>= mapping
->range_start
._unsigned
&&
1234 value
<= mapping
->range_end
._unsigned
) {
1245 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1247 struct bt_ctf_field_type_floating_point
*floating_point
=
1248 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1250 if (!floating_point
) {
1254 floating_point
->declaration
.sign
= &floating_point
->sign
;
1255 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1256 floating_point
->declaration
.exp
= &floating_point
->exp
;
1257 floating_point
->sign
.len
= 1;
1258 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1259 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1260 floating_point
->declaration
.exp
->len
=
1261 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1262 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1263 floating_point
->sign
.p
.alignment
= 1;
1264 floating_point
->mantissa
.p
.alignment
= 1;
1265 floating_point
->exp
.p
.alignment
= 1;
1267 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1269 return floating_point
? &floating_point
->parent
: NULL
;
1272 int bt_ctf_field_type_floating_point_get_exponent_digits(
1273 struct bt_ctf_field_type
*type
)
1276 struct bt_ctf_field_type_floating_point
*floating_point
;
1278 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1283 floating_point
= container_of(type
,
1284 struct bt_ctf_field_type_floating_point
, parent
);
1285 ret
= (int) floating_point
->declaration
.exp
->len
;
1290 int bt_ctf_field_type_floating_point_set_exponent_digits(
1291 struct bt_ctf_field_type
*type
,
1292 unsigned int exponent_digits
)
1295 struct bt_ctf_field_type_floating_point
*floating_point
;
1297 if (!type
|| type
->frozen
||
1298 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1303 floating_point
= container_of(type
,
1304 struct bt_ctf_field_type_floating_point
, parent
);
1305 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1306 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1308 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1313 floating_point
->declaration
.exp
->len
= exponent_digits
;
1318 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1319 struct bt_ctf_field_type
*type
)
1322 struct bt_ctf_field_type_floating_point
*floating_point
;
1324 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1329 floating_point
= container_of(type
,
1330 struct bt_ctf_field_type_floating_point
, parent
);
1331 ret
= (int) floating_point
->mantissa
.len
+ 1;
1336 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1337 struct bt_ctf_field_type
*type
,
1338 unsigned int mantissa_digits
)
1341 struct bt_ctf_field_type_floating_point
*floating_point
;
1343 if (!type
|| type
->frozen
||
1344 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1349 floating_point
= container_of(type
,
1350 struct bt_ctf_field_type_floating_point
, parent
);
1352 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1353 (mantissa_digits
!= DBL_MANT_DIG
) &&
1354 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1359 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1364 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1366 struct bt_ctf_field_type_structure
*structure
=
1367 g_new0(struct bt_ctf_field_type_structure
, 1);
1373 structure
->parent
.declaration
= &structure
->declaration
.p
;
1374 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1375 structure
->fields
= g_ptr_array_new_with_free_func(
1376 (GDestroyNotify
)destroy_structure_field
);
1377 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1378 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1379 return &structure
->parent
;
1384 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1385 struct bt_ctf_field_type
*field_type
,
1386 const char *field_name
)
1389 struct bt_ctf_field_type_structure
*structure
;
1391 if (!type
|| !field_type
|| type
->frozen
||
1392 bt_ctf_validate_identifier(field_name
) ||
1393 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1398 structure
= container_of(type
,
1399 struct bt_ctf_field_type_structure
, parent
);
1400 if (add_structure_field(structure
->fields
,
1401 structure
->field_name_to_index
, field_type
, field_name
)) {
1409 int bt_ctf_field_type_structure_get_field_count(
1410 struct bt_ctf_field_type
*type
)
1413 struct bt_ctf_field_type_structure
*structure
;
1415 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1420 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1422 ret
= (int) structure
->fields
->len
;
1427 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1428 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1431 struct bt_ctf_field_type_structure
*structure
;
1432 struct structure_field
*field
;
1435 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1440 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1442 if (index
>= structure
->fields
->len
) {
1447 field
= g_ptr_array_index(structure
->fields
, index
);
1449 *field_type
= field
->type
;
1450 bt_get(field
->type
);
1453 *field_name
= g_quark_to_string(field
->name
);
1459 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1460 struct bt_ctf_field_type
*type
,
1465 struct structure_field
*field
;
1466 struct bt_ctf_field_type_structure
*structure
;
1467 struct bt_ctf_field_type
*field_type
= NULL
;
1469 if (!type
|| !name
) {
1473 name_quark
= g_quark_try_string(name
);
1478 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1480 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1481 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1485 field
= structure
->fields
->pdata
[index
];
1486 field_type
= field
->type
;
1492 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1493 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1495 struct bt_ctf_field_type_variant
*variant
= NULL
;
1497 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1501 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1506 variant
->parent
.declaration
= &variant
->declaration
.p
;
1507 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1508 variant
->tag_name
= g_string_new(tag_name
);
1509 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1510 variant
->fields
= g_ptr_array_new_with_free_func(
1511 (GDestroyNotify
) destroy_structure_field
);
1514 variant
->tag
= container_of(enum_tag
,
1515 struct bt_ctf_field_type_enumeration
, parent
);
1518 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1519 /* A variant's alignment is undefined */
1520 variant
->parent
.declaration
->alignment
= 0;
1521 return &variant
->parent
;
1526 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1527 struct bt_ctf_field_type
*type
)
1529 struct bt_ctf_field_type_variant
*variant
;
1530 struct bt_ctf_field_type
*tag_type
= NULL
;
1532 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1536 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1537 if (!variant
->tag
) {
1541 tag_type
= &variant
->tag
->parent
;
1547 const char *bt_ctf_field_type_variant_get_tag_name(
1548 struct bt_ctf_field_type
*type
)
1550 struct bt_ctf_field_type_variant
*variant
;
1551 const char *tag_name
= NULL
;
1553 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1557 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1558 if (variant
->tag_name
->len
== 0) {
1562 tag_name
= variant
->tag_name
->str
;
1567 int bt_ctf_field_type_variant_set_tag_name(
1568 struct bt_ctf_field_type
*type
, const char *name
)
1571 struct bt_ctf_field_type_variant
*variant
;
1573 if (!type
|| type
->frozen
||
1574 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1575 bt_ctf_validate_identifier(name
)) {
1580 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1581 g_string_assign(variant
->tag_name
, name
);
1586 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1587 struct bt_ctf_field_type
*field_type
,
1588 const char *field_name
)
1592 struct bt_ctf_field_type_variant
*variant
;
1593 GQuark field_name_quark
= g_quark_from_string(field_name
);
1595 if (!type
|| !field_type
|| type
->frozen
||
1596 bt_ctf_validate_identifier(field_name
) ||
1597 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1602 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1604 /* The user has explicitly provided a tag; validate against it. */
1608 /* Make sure this name is present in the enum tag */
1609 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1610 struct enumeration_mapping
*mapping
=
1611 g_ptr_array_index(variant
->tag
->entries
, i
);
1613 if (mapping
->string
== field_name_quark
) {
1620 /* Validation failed */
1626 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1627 field_type
, field_name
)) {
1635 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1636 struct bt_ctf_field_type
*type
,
1637 const char *field_name
)
1641 struct structure_field
*field
;
1642 struct bt_ctf_field_type_variant
*variant
;
1643 struct bt_ctf_field_type
*field_type
= NULL
;
1645 if (!type
|| !field_name
) {
1649 name_quark
= g_quark_try_string(field_name
);
1654 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1655 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1656 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1660 field
= g_ptr_array_index(variant
->fields
, index
);
1661 field_type
= field
->type
;
1667 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1668 struct bt_ctf_field_type
*type
,
1669 struct bt_ctf_field
*tag
)
1671 const char *enum_value
;
1672 struct bt_ctf_field_type
*field_type
= NULL
;
1674 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1678 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1683 /* Already increments field_type's reference count */
1684 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1690 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1693 struct bt_ctf_field_type_variant
*variant
;
1695 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1700 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1702 ret
= (int) variant
->fields
->len
;
1708 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1709 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1712 struct bt_ctf_field_type_variant
*variant
;
1713 struct structure_field
*field
;
1716 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1721 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1723 if (index
>= variant
->fields
->len
) {
1728 field
= g_ptr_array_index(variant
->fields
, index
);
1730 *field_type
= field
->type
;
1731 bt_get(field
->type
);
1734 *field_name
= g_quark_to_string(field
->name
);
1740 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1741 struct bt_ctf_field_type
*element_type
,
1742 unsigned int length
)
1744 struct bt_ctf_field_type_array
*array
= NULL
;
1746 if (!element_type
|| length
== 0) {
1750 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1755 array
->parent
.declaration
= &array
->declaration
.p
;
1756 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1758 bt_get(element_type
);
1759 array
->element_type
= element_type
;
1760 array
->length
= length
;
1761 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1762 return &array
->parent
;
1767 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1768 struct bt_ctf_field_type
*type
)
1770 struct bt_ctf_field_type
*ret
= NULL
;
1771 struct bt_ctf_field_type_array
*array
;
1773 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1777 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1778 ret
= array
->element_type
;
1785 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1786 struct bt_ctf_field_type
*element_type
)
1789 struct bt_ctf_field_type_array
*array
;
1791 if (!type
|| !element_type
||
1792 (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1797 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1799 if (array
->element_type
) {
1800 BT_PUT(array
->element_type
);
1803 array
->element_type
= element_type
;
1804 bt_get(array
->element_type
);
1810 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1813 struct bt_ctf_field_type_array
*array
;
1815 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1820 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1821 ret
= (int64_t) array
->length
;
1826 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1827 struct bt_ctf_field_type
*element_type
,
1828 const char *length_field_name
)
1830 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1832 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1836 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1841 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1842 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1843 bt_get(element_type
);
1844 sequence
->element_type
= element_type
;
1845 sequence
->length_field_name
= g_string_new(length_field_name
);
1846 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1847 return &sequence
->parent
;
1852 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1853 struct bt_ctf_field_type
*type
)
1855 struct bt_ctf_field_type
*ret
= NULL
;
1856 struct bt_ctf_field_type_sequence
*sequence
;
1858 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1862 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1864 ret
= sequence
->element_type
;
1871 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1872 struct bt_ctf_field_type
*element_type
)
1875 struct bt_ctf_field_type_sequence
*sequence
;
1877 if (!type
|| !element_type
||
1878 (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1883 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1885 if (sequence
->element_type
) {
1886 BT_PUT(sequence
->element_type
);
1889 sequence
->element_type
= element_type
;
1890 bt_get(sequence
->element_type
);
1896 const char *bt_ctf_field_type_sequence_get_length_field_name(
1897 struct bt_ctf_field_type
*type
)
1899 const char *ret
= NULL
;
1900 struct bt_ctf_field_type_sequence
*sequence
;
1902 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1906 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1908 ret
= sequence
->length_field_name
->str
;
1913 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1915 struct bt_ctf_field_type_string
*string
=
1916 g_new0(struct bt_ctf_field_type_string
, 1);
1922 string
->parent
.declaration
= &string
->declaration
.p
;
1923 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1924 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1925 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1926 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1927 return &string
->parent
;
1930 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1931 struct bt_ctf_field_type
*type
)
1933 struct bt_ctf_field_type_string
*string
;
1934 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1936 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1940 string
= container_of(type
, struct bt_ctf_field_type_string
,
1942 ret
= string
->declaration
.encoding
;
1947 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1948 enum bt_ctf_string_encoding encoding
)
1951 struct bt_ctf_field_type_string
*string
;
1953 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1954 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
1955 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
1960 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1961 string
->declaration
.encoding
= encoding
;
1966 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1969 enum ctf_type_id type_id
;
1977 ret
= (int) type
->declaration
->alignment
;
1981 type_id
= bt_ctf_field_type_get_type_id(type
);
1983 case CTF_TYPE_SEQUENCE
:
1985 struct bt_ctf_field_type
*element
=
1986 bt_ctf_field_type_sequence_get_element_type(type
);
1993 ret
= bt_ctf_field_type_get_alignment(element
);
1997 case CTF_TYPE_ARRAY
:
1999 struct bt_ctf_field_type
*element
=
2000 bt_ctf_field_type_array_get_element_type(type
);
2007 ret
= bt_ctf_field_type_get_alignment(element
);
2011 case CTF_TYPE_STRUCT
:
2013 int i
, element_count
;
2015 element_count
= bt_ctf_field_type_structure_get_field_count(
2017 if (element_count
< 0) {
2018 ret
= element_count
;
2022 for (i
= 0; i
< element_count
; i
++) {
2023 struct bt_ctf_field_type
*field
;
2024 int field_alignment
;
2026 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2033 field_alignment
= bt_ctf_field_type_get_alignment(
2036 if (field_alignment
< 0) {
2037 ret
= field_alignment
;
2041 type
->declaration
->alignment
= MAX(field_alignment
,
2042 type
->declaration
->alignment
);
2044 ret
= (int) type
->declaration
->alignment
;
2047 case CTF_TYPE_UNKNOWN
:
2051 ret
= (int) type
->declaration
->alignment
;
2059 int is_power_of_two(unsigned int value
)
2061 return ((value
& (value
- 1)) == 0) && value
> 0;
2064 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2065 unsigned int alignment
)
2068 enum ctf_type_id type_id
;
2070 /* Alignment must be a power of two */
2071 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2076 type_id
= bt_ctf_field_type_get_type_id(type
);
2077 if (type_id
== CTF_TYPE_UNKNOWN
) {
2082 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
2083 alignment
!= CHAR_BIT
) {
2088 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
2089 type_id
== CTF_TYPE_ARRAY
) {
2090 /* Setting an alignment on these types makes no sense */
2095 type
->declaration
->alignment
= alignment
;
2101 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2102 struct bt_ctf_field_type
*type
)
2104 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2110 switch (type
->declaration
->id
) {
2111 case CTF_TYPE_INTEGER
:
2113 struct bt_ctf_field_type_integer
*integer
= container_of(
2114 type
, struct bt_ctf_field_type_integer
, parent
);
2115 ret
= integer
->user_byte_order
;
2118 case CTF_TYPE_FLOAT
:
2120 struct bt_ctf_field_type_floating_point
*floating_point
=
2122 struct bt_ctf_field_type_floating_point
,
2124 ret
= floating_point
->user_byte_order
;
2131 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2132 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2133 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2134 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2140 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2141 enum bt_ctf_byte_order byte_order
)
2144 int internal_byte_order
;
2145 enum ctf_type_id type_id
;
2147 if (!type
|| type
->frozen
) {
2152 switch (byte_order
) {
2153 case BT_CTF_BYTE_ORDER_NATIVE
:
2154 /* Leave unset. Will be initialized by parent. */
2155 internal_byte_order
= 0;
2157 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2158 internal_byte_order
= LITTLE_ENDIAN
;
2160 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2161 case BT_CTF_BYTE_ORDER_NETWORK
:
2162 internal_byte_order
= BIG_ENDIAN
;
2169 type_id
= type
->declaration
->id
;
2170 if (set_byte_order_funcs
[type_id
]) {
2171 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2177 enum ctf_type_id
bt_ctf_field_type_get_type_id(
2178 struct bt_ctf_field_type
*type
)
2181 return CTF_TYPE_UNKNOWN
;
2184 return type
->declaration
->id
;
2187 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2189 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
2192 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2194 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
2197 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2199 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
2202 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2204 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
2207 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2209 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
2212 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2214 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
2217 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2219 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
2222 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2224 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
2227 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2232 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2238 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2248 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2249 struct bt_ctf_field_type_variant
*variant
,
2252 struct bt_ctf_field_type
*type
= NULL
;
2253 GQuark field_name_quark
;
2255 struct structure_field
*field_entry
;
2256 struct range_overlap_query query
= {
2257 .range_start
._signed
= tag_value
,
2258 .range_end
._signed
= tag_value
,
2259 .mapping_name
= 0, .overlaps
= 0};
2261 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2263 if (!query
.overlaps
) {
2267 field_name_quark
= query
.mapping_name
;
2268 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2269 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2273 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2274 type
= field_entry
->type
;
2280 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2281 struct bt_ctf_field_type_variant
*variant
,
2284 struct bt_ctf_field_type
*type
= NULL
;
2285 GQuark field_name_quark
;
2287 struct structure_field
*field_entry
;
2288 struct range_overlap_query query
= {
2289 .range_start
._unsigned
= tag_value
,
2290 .range_end
._unsigned
= tag_value
,
2291 .mapping_name
= 0, .overlaps
= 0};
2293 g_ptr_array_foreach(variant
->tag
->entries
,
2294 check_ranges_overlap_unsigned
,
2296 if (!query
.overlaps
) {
2300 field_name_quark
= query
.mapping_name
;
2301 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2302 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2306 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2307 type
= field_entry
->type
;
2313 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2314 struct metadata_context
*context
)
2318 if (!type
|| !context
) {
2323 /* Make sure field type is valid before serializing it */
2324 ret
= bt_ctf_field_type_validate(type
);
2330 ret
= type
->serialize(type
, context
);
2336 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2343 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2344 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2345 set_byte_order_funcs
[type
->declaration
->id
](type
,
2351 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2353 struct bt_ctf_field_type
*copy
= NULL
;
2359 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2365 int bt_ctf_field_type_structure_get_field_name_index(
2366 struct bt_ctf_field_type
*type
, const char *name
)
2371 struct bt_ctf_field_type_structure
*structure
;
2373 if (!type
|| !name
||
2374 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2379 name_quark
= g_quark_try_string(name
);
2385 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2387 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2388 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2398 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2399 struct bt_ctf_field_type
*field
, int index
)
2402 struct bt_ctf_field_type_structure
*structure
;
2404 if (!type
|| !field
||
2405 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2410 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2412 if (index
< 0 || index
>= structure
->fields
->len
) {
2418 bt_put(((struct structure_field
*)
2419 g_ptr_array_index(structure
->fields
, index
))->type
);
2420 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2427 int bt_ctf_field_type_variant_get_field_name_index(
2428 struct bt_ctf_field_type
*type
, const char *name
)
2433 struct bt_ctf_field_type_variant
*variant
;
2435 if (!type
|| !name
||
2436 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2441 name_quark
= g_quark_try_string(name
);
2447 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2449 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2450 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2460 int bt_ctf_field_type_sequence_set_length_field_path(
2461 struct bt_ctf_field_type
*type
,
2462 struct bt_ctf_field_path
*path
)
2465 struct bt_ctf_field_type_sequence
*sequence
;
2467 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2472 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2475 BT_MOVE(sequence
->length_field_path
, path
);
2481 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2482 struct bt_ctf_field_path
*path
)
2485 struct bt_ctf_field_type_variant
*variant
;
2487 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2492 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2495 BT_MOVE(variant
->tag_field_path
, path
);
2501 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2502 struct bt_ctf_field_type
*tag
)
2505 struct bt_ctf_field_type_variant
*variant
;
2507 if (!type
|| !tag
||
2508 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2513 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2517 bt_put(&variant
->tag
->parent
);
2519 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2526 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2527 struct bt_ctf_field_type
*field
, int index
)
2530 struct bt_ctf_field_type_variant
*variant
;
2532 if (!type
|| !field
||
2533 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2538 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2540 if (index
< 0 || index
>= variant
->fields
->len
) {
2546 bt_put(((struct structure_field
*)
2547 g_ptr_array_index(variant
->fields
, index
))->type
);
2548 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2555 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2557 struct bt_ctf_field_type_integer
*integer
=
2558 (struct bt_ctf_field_type_integer
*) type
;
2564 bt_put(integer
->mapped_clock
);
2569 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2571 struct bt_ctf_field_type_enumeration
*enumeration
=
2572 (struct bt_ctf_field_type_enumeration
*) type
;
2578 g_ptr_array_free(enumeration
->entries
, TRUE
);
2579 bt_put(enumeration
->container
);
2580 g_free(enumeration
);
2584 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2586 struct bt_ctf_field_type_floating_point
*floating_point
=
2587 (struct bt_ctf_field_type_floating_point
*) type
;
2593 g_free(floating_point
);
2597 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2599 struct bt_ctf_field_type_structure
*structure
=
2600 (struct bt_ctf_field_type_structure
*) type
;
2606 g_ptr_array_free(structure
->fields
, TRUE
);
2607 g_hash_table_destroy(structure
->field_name_to_index
);
2612 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2614 struct bt_ctf_field_type_variant
*variant
=
2615 (struct bt_ctf_field_type_variant
*) type
;
2621 g_ptr_array_free(variant
->fields
, TRUE
);
2622 g_hash_table_destroy(variant
->field_name_to_index
);
2623 g_string_free(variant
->tag_name
, TRUE
);
2624 bt_put(&variant
->tag
->parent
);
2625 BT_PUT(variant
->tag_field_path
);
2630 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2632 struct bt_ctf_field_type_array
*array
=
2633 (struct bt_ctf_field_type_array
*) type
;
2639 bt_put(array
->element_type
);
2644 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2646 struct bt_ctf_field_type_sequence
*sequence
=
2647 (struct bt_ctf_field_type_sequence
*) type
;
2653 bt_put(sequence
->element_type
);
2654 g_string_free(sequence
->length_field_name
, TRUE
);
2655 BT_PUT(sequence
->length_field_path
);
2660 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2662 struct bt_ctf_field_type_string
*string
=
2663 (struct bt_ctf_field_type_string
*) type
;
2673 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2679 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2681 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2682 type
, struct bt_ctf_field_type_enumeration
, parent
);
2684 generic_field_type_freeze(type
);
2685 bt_ctf_field_type_freeze(enumeration_type
->container
);
2689 void freeze_structure_field(struct structure_field
*field
)
2691 bt_ctf_field_type_freeze(field
->type
);
2695 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2697 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2698 type
, struct bt_ctf_field_type_structure
, parent
);
2700 /* Cache the alignment */
2701 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2702 generic_field_type_freeze(type
);
2703 g_ptr_array_foreach(structure_type
->fields
,
2704 (GFunc
) freeze_structure_field
, NULL
);
2708 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2710 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2711 type
, struct bt_ctf_field_type_variant
, parent
);
2713 generic_field_type_freeze(type
);
2714 g_ptr_array_foreach(variant_type
->fields
,
2715 (GFunc
) freeze_structure_field
, NULL
);
2719 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2721 struct bt_ctf_field_type_array
*array_type
= container_of(
2722 type
, struct bt_ctf_field_type_array
, parent
);
2724 /* Cache the alignment */
2725 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2726 generic_field_type_freeze(type
);
2727 bt_ctf_field_type_freeze(array_type
->element_type
);
2731 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2733 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2734 type
, struct bt_ctf_field_type_sequence
, parent
);
2736 /* Cache the alignment */
2737 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2738 generic_field_type_freeze(type
);
2739 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2743 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2745 const char *encoding_string
;
2748 case BT_CTF_STRING_ENCODING_NONE
:
2749 encoding_string
= "none";
2751 case BT_CTF_STRING_ENCODING_ASCII
:
2752 encoding_string
= "ASCII";
2754 case BT_CTF_STRING_ENCODING_UTF8
:
2755 encoding_string
= "UTF8";
2758 encoding_string
= "unknown";
2762 return encoding_string
;
2766 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2768 const char *base_string
;
2771 case BT_CTF_INTEGER_BASE_DECIMAL
:
2772 base_string
= "decimal";
2774 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2775 base_string
= "hexadecimal";
2777 case BT_CTF_INTEGER_BASE_OCTAL
:
2778 base_string
= "octal";
2780 case BT_CTF_INTEGER_BASE_BINARY
:
2781 base_string
= "binary";
2784 base_string
= "unknown";
2792 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2793 struct metadata_context
*context
)
2795 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2796 struct bt_ctf_field_type_integer
, parent
);
2799 g_string_append_printf(context
->string
,
2800 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2801 integer
->declaration
.len
, type
->declaration
->alignment
,
2802 (integer
->declaration
.signedness
? "true" : "false"),
2803 get_encoding_string(integer
->declaration
.encoding
),
2804 get_integer_base_string(integer
->declaration
.base
),
2805 get_byte_order_string(integer
->declaration
.byte_order
));
2806 if (integer
->mapped_clock
) {
2807 const char *clock_name
= bt_ctf_clock_get_name(
2808 integer
->mapped_clock
);
2815 g_string_append_printf(context
->string
,
2816 "; map = clock.%s.value", clock_name
);
2819 g_string_append(context
->string
, "; }");
2825 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2826 struct metadata_context
*context
)
2830 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2831 struct bt_ctf_field_type_enumeration
, parent
);
2832 struct bt_ctf_field_type
*container_type
;
2833 int container_signed
;
2835 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2836 if (!container_type
) {
2841 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2842 if (container_signed
< 0) {
2843 ret
= container_signed
;
2844 goto error_put_container_type
;
2847 g_string_append(context
->string
, "enum : ");
2848 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2850 goto error_put_container_type
;
2853 g_string_append(context
->string
, " { ");
2854 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2855 struct enumeration_mapping
*mapping
=
2856 enumeration
->entries
->pdata
[entry
];
2858 if (container_signed
) {
2859 if (mapping
->range_start
._signed
==
2860 mapping
->range_end
._signed
) {
2861 g_string_append_printf(context
->string
,
2862 "\"%s\" = %" PRId64
,
2863 g_quark_to_string(mapping
->string
),
2864 mapping
->range_start
._signed
);
2866 g_string_append_printf(context
->string
,
2867 "\"%s\" = %" PRId64
" ... %" PRId64
,
2868 g_quark_to_string(mapping
->string
),
2869 mapping
->range_start
._signed
,
2870 mapping
->range_end
._signed
);
2873 if (mapping
->range_start
._unsigned
==
2874 mapping
->range_end
._unsigned
) {
2875 g_string_append_printf(context
->string
,
2876 "\"%s\" = %" PRIu64
,
2877 g_quark_to_string(mapping
->string
),
2878 mapping
->range_start
._unsigned
);
2880 g_string_append_printf(context
->string
,
2881 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2882 g_quark_to_string(mapping
->string
),
2883 mapping
->range_start
._unsigned
,
2884 mapping
->range_end
._unsigned
);
2888 g_string_append(context
->string
,
2889 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2893 if (context
->field_name
->len
) {
2894 g_string_append_printf(context
->string
, " %s",
2895 context
->field_name
->str
);
2896 g_string_assign(context
->field_name
, "");
2898 error_put_container_type
:
2899 bt_put(container_type
);
2905 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2906 struct metadata_context
*context
)
2908 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2909 type
, struct bt_ctf_field_type_floating_point
, parent
);
2911 g_string_append_printf(context
->string
,
2912 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2913 floating_point
->declaration
.exp
->len
,
2914 floating_point
->declaration
.mantissa
->len
+ 1,
2915 get_byte_order_string(floating_point
->declaration
.byte_order
),
2916 type
->declaration
->alignment
);
2921 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2922 struct metadata_context
*context
)
2925 unsigned int indent
;
2927 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2928 struct bt_ctf_field_type_structure
, parent
);
2929 GString
*structure_field_name
= context
->field_name
;
2931 context
->field_name
= g_string_new("");
2933 context
->current_indentation_level
++;
2934 g_string_append(context
->string
, "struct {\n");
2936 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2937 struct structure_field
*field
;
2939 for (indent
= 0; indent
< context
->current_indentation_level
;
2941 g_string_append_c(context
->string
, '\t');
2944 field
= structure
->fields
->pdata
[i
];
2945 g_string_assign(context
->field_name
,
2946 g_quark_to_string(field
->name
));
2947 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2952 if (context
->field_name
->len
) {
2953 g_string_append_printf(context
->string
, " %s",
2954 context
->field_name
->str
);
2956 g_string_append(context
->string
, ";\n");
2959 context
->current_indentation_level
--;
2960 for (indent
= 0; indent
< context
->current_indentation_level
;
2962 g_string_append_c(context
->string
, '\t');
2965 g_string_append_printf(context
->string
, "} align(%zu)",
2966 type
->declaration
->alignment
);
2968 g_string_free(context
->field_name
, TRUE
);
2969 context
->field_name
= structure_field_name
;
2974 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2975 struct metadata_context
*context
)
2978 unsigned int indent
;
2980 struct bt_ctf_field_type_variant
*variant
= container_of(
2981 type
, struct bt_ctf_field_type_variant
, parent
);
2982 GString
*variant_field_name
= context
->field_name
;
2984 context
->field_name
= g_string_new("");
2985 if (variant
->tag_name
->len
> 0) {
2986 g_string_append_printf(context
->string
,
2987 "variant <%s> {\n", variant
->tag_name
->str
);
2989 g_string_append(context
->string
, "variant {\n");
2992 context
->current_indentation_level
++;
2993 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2994 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2996 g_string_assign(context
->field_name
,
2997 g_quark_to_string(field
->name
));
2998 for (indent
= 0; indent
< context
->current_indentation_level
;
3000 g_string_append_c(context
->string
, '\t');
3003 g_string_assign(context
->field_name
,
3004 g_quark_to_string(field
->name
));
3005 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3010 if (context
->field_name
->len
) {
3011 g_string_append_printf(context
->string
, " %s;",
3012 context
->field_name
->str
);
3015 g_string_append_c(context
->string
, '\n');
3018 context
->current_indentation_level
--;
3019 for (indent
= 0; indent
< context
->current_indentation_level
;
3021 g_string_append_c(context
->string
, '\t');
3024 g_string_append(context
->string
, "}");
3026 g_string_free(context
->field_name
, TRUE
);
3027 context
->field_name
= variant_field_name
;
3032 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3033 struct metadata_context
*context
)
3036 struct bt_ctf_field_type_array
*array
= container_of(type
,
3037 struct bt_ctf_field_type_array
, parent
);
3039 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3044 if (context
->field_name
->len
) {
3045 g_string_append_printf(context
->string
, " %s[%u]",
3046 context
->field_name
->str
, array
->length
);
3047 g_string_assign(context
->field_name
, "");
3049 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3056 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3057 struct metadata_context
*context
)
3060 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3061 type
, struct bt_ctf_field_type_sequence
, parent
);
3063 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3068 if (context
->field_name
->len
) {
3069 g_string_append_printf(context
->string
, " %s[%s]",
3070 context
->field_name
->str
,
3071 sequence
->length_field_name
->str
);
3072 g_string_assign(context
->field_name
, "");
3074 g_string_append_printf(context
->string
, "[%s]",
3075 sequence
->length_field_name
->str
);
3082 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3083 struct metadata_context
*context
)
3085 struct bt_ctf_field_type_string
*string
= container_of(
3086 type
, struct bt_ctf_field_type_string
, parent
);
3088 g_string_append_printf(context
->string
,
3089 "string { encoding = %s; }",
3090 get_encoding_string(string
->declaration
.encoding
));
3095 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3096 enum bt_ctf_byte_order ret
;
3098 switch (byte_order
) {
3099 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3101 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3103 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3105 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3107 case BT_CTF_BYTE_ORDER_NETWORK
:
3108 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3110 case BT_CTF_BYTE_ORDER_NATIVE
:
3111 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3114 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3122 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3123 int byte_order
, int set_native
)
3125 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3126 struct bt_ctf_field_type_integer
, parent
);
3129 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3131 * User byte order is native, so we can set
3132 * the real byte order.
3134 integer_type
->declaration
.byte_order
=
3138 integer_type
->user_byte_order
=
3139 get_ctf_ir_byte_order(byte_order
);
3140 integer_type
->declaration
.byte_order
= byte_order
;
3145 void bt_ctf_field_type_enumeration_set_byte_order(
3146 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3148 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3149 struct bt_ctf_field_type_enumeration
, parent
);
3151 /* Safe to assume that container is an integer */
3152 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3153 byte_order
, set_native
);
3157 void bt_ctf_field_type_floating_point_set_byte_order(
3158 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3160 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3161 container_of(type
, struct bt_ctf_field_type_floating_point
,
3165 if (floating_point_type
->user_byte_order
==
3166 BT_CTF_BYTE_ORDER_NATIVE
) {
3168 * User byte order is native, so we can set
3169 * the real byte order.
3171 floating_point_type
->declaration
.byte_order
=
3173 floating_point_type
->sign
.byte_order
=
3175 floating_point_type
->mantissa
.byte_order
=
3177 floating_point_type
->exp
.byte_order
=
3181 floating_point_type
->user_byte_order
=
3182 get_ctf_ir_byte_order(byte_order
);
3183 floating_point_type
->declaration
.byte_order
= byte_order
;
3184 floating_point_type
->sign
.byte_order
= byte_order
;
3185 floating_point_type
->mantissa
.byte_order
= byte_order
;
3186 floating_point_type
->exp
.byte_order
= byte_order
;
3191 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3192 int byte_order
, int set_native
)
3195 struct bt_ctf_field_type_structure
*structure_type
=
3196 container_of(type
, struct bt_ctf_field_type_structure
,
3199 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3200 struct structure_field
*field
= g_ptr_array_index(
3201 structure_type
->fields
, i
);
3202 struct bt_ctf_field_type
*field_type
= field
->type
;
3204 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3205 set_byte_order_funcs
[field_type
->declaration
->id
](
3206 field_type
, byte_order
, set_native
);
3212 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3213 int byte_order
, int set_native
)
3216 struct bt_ctf_field_type_variant
*variant_type
=
3217 container_of(type
, struct bt_ctf_field_type_variant
,
3220 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3221 struct structure_field
*field
= g_ptr_array_index(
3222 variant_type
->fields
, i
);
3223 struct bt_ctf_field_type
*field_type
= field
->type
;
3225 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3226 set_byte_order_funcs
[field_type
->declaration
->id
](
3227 field_type
, byte_order
, set_native
);
3233 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3234 int byte_order
, int set_native
)
3236 struct bt_ctf_field_type_array
*array_type
=
3237 container_of(type
, struct bt_ctf_field_type_array
,
3240 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3241 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3242 array_type
->element_type
, byte_order
, set_native
);
3247 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3248 int byte_order
, int set_native
)
3250 struct bt_ctf_field_type_sequence
*sequence_type
=
3251 container_of(type
, struct bt_ctf_field_type_sequence
,
3254 if (set_byte_order_funcs
[
3255 sequence_type
->element_type
->declaration
->id
]) {
3256 set_byte_order_funcs
[
3257 sequence_type
->element_type
->declaration
->id
](
3258 sequence_type
->element_type
, byte_order
, set_native
);
3263 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3264 struct bt_ctf_field_type
*type
)
3266 struct bt_ctf_field_type
*copy
;
3267 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3269 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3270 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3275 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3277 copy_integer
->declaration
= integer
->declaration
;
3278 if (integer
->mapped_clock
) {
3279 bt_get(integer
->mapped_clock
);
3280 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3283 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3290 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3291 struct bt_ctf_field_type
*type
)
3294 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3295 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3297 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3300 /* Copy the source enumeration's container */
3301 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3302 if (!copy_container
) {
3306 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3310 copy_enumeration
= container_of(copy
,
3311 struct bt_ctf_field_type_enumeration
, parent
);
3313 /* Copy all enumaration entries */
3314 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3315 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3316 enumeration
->entries
, i
);
3317 struct enumeration_mapping
* copy_mapping
= g_new0(
3318 struct enumeration_mapping
, 1);
3320 if (!copy_mapping
) {
3324 *copy_mapping
= *mapping
;
3325 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3328 copy_enumeration
->declaration
= enumeration
->declaration
;
3330 bt_put(copy_container
);
3333 bt_put(copy_container
);
3339 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3340 struct bt_ctf_field_type
*type
)
3342 struct bt_ctf_field_type
*copy
;
3343 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3345 floating_point
= container_of(type
,
3346 struct bt_ctf_field_type_floating_point
, parent
);
3347 copy
= bt_ctf_field_type_floating_point_create();
3352 copy_float
= container_of(copy
,
3353 struct bt_ctf_field_type_floating_point
, parent
);
3354 copy_float
->declaration
= floating_point
->declaration
;
3355 copy_float
->sign
= floating_point
->sign
;
3356 copy_float
->mantissa
= floating_point
->mantissa
;
3357 copy_float
->exp
= floating_point
->exp
;
3358 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3359 copy_float
->declaration
.sign
= ©_float
->sign
;
3360 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3361 copy_float
->declaration
.exp
= ©_float
->exp
;
3367 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3368 struct bt_ctf_field_type
*type
)
3371 GHashTableIter iter
;
3372 gpointer key
, value
;
3373 struct bt_ctf_field_type
*copy
;
3374 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3376 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3378 copy
= bt_ctf_field_type_structure_create();
3383 copy_structure
= container_of(copy
,
3384 struct bt_ctf_field_type_structure
, parent
);
3386 /* Copy field_name_to_index */
3387 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3388 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3389 g_hash_table_insert(copy_structure
->field_name_to_index
,
3393 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3394 struct structure_field
*entry
, *copy_entry
;
3395 struct bt_ctf_field_type
*copy_field
;
3397 copy_entry
= g_new0(struct structure_field
, 1);
3402 entry
= g_ptr_array_index(structure
->fields
, i
);
3403 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3409 copy_entry
->name
= entry
->name
;
3410 copy_entry
->type
= copy_field
;
3411 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3414 copy_structure
->declaration
= structure
->declaration
;
3423 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3424 struct bt_ctf_field_type
*type
)
3427 GHashTableIter iter
;
3428 gpointer key
, value
;
3429 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3430 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3432 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3435 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3441 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3442 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3447 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3450 /* Copy field_name_to_index */
3451 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3452 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3453 g_hash_table_insert(copy_variant
->field_name_to_index
,
3457 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3458 struct structure_field
*entry
, *copy_entry
;
3459 struct bt_ctf_field_type
*copy_field
;
3461 copy_entry
= g_new0(struct structure_field
, 1);
3466 entry
= g_ptr_array_index(variant
->fields
, i
);
3467 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3473 copy_entry
->name
= entry
->name
;
3474 copy_entry
->type
= copy_field
;
3475 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3478 copy_variant
->declaration
= variant
->declaration
;
3479 if (variant
->tag_field_path
) {
3480 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3481 variant
->tag_field_path
);
3482 if (!copy_variant
->tag_field_path
) {
3496 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3497 struct bt_ctf_field_type
*type
)
3499 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3500 struct bt_ctf_field_type_array
*array
, *copy_array
;
3502 array
= container_of(type
, struct bt_ctf_field_type_array
,
3504 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3505 if (!copy_element
) {
3509 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3514 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3516 copy_array
->declaration
= array
->declaration
;
3518 bt_put(copy_element
);
3523 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3524 struct bt_ctf_field_type
*type
)
3526 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3527 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3529 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3531 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3532 if (!copy_element
) {
3536 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3537 sequence
->length_field_name
->len
?
3538 sequence
->length_field_name
->str
: NULL
);
3543 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3545 copy_sequence
->declaration
= sequence
->declaration
;
3546 if (sequence
->length_field_path
) {
3547 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3548 sequence
->length_field_path
);
3549 if (!copy_sequence
->length_field_path
) {
3554 bt_put(copy_element
);
3562 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3563 struct bt_ctf_field_type
*type
)
3565 struct bt_ctf_field_type
*copy
;
3566 struct bt_ctf_field_type_string
*string
, *copy_string
;
3568 copy
= bt_ctf_field_type_string_create();
3573 string
= container_of(type
, struct bt_ctf_field_type_string
,
3575 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3577 copy_string
->declaration
= string
->declaration
;
3583 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3584 struct bt_ctf_field_type
*type_b
)
3587 struct bt_ctf_field_type_integer
*integer_a
;
3588 struct bt_ctf_field_type_integer
*integer_b
;
3589 struct declaration_integer
*decl_a
;
3590 struct declaration_integer
*decl_b
;
3592 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3594 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3596 decl_a
= &integer_a
->declaration
;
3597 decl_b
= &integer_b
->declaration
;
3600 if (decl_a
->len
!= decl_b
->len
) {
3605 * Compare user byte orders only, not the cached,
3608 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3613 if (decl_a
->signedness
!= decl_b
->signedness
) {
3618 if (decl_a
->base
!= decl_b
->base
) {
3623 if (decl_a
->encoding
!= decl_b
->encoding
) {
3628 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3640 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3641 struct bt_ctf_field_type
*type_b
)
3644 struct bt_ctf_field_type_floating_point
*float_a
;
3645 struct bt_ctf_field_type_floating_point
*float_b
;
3647 float_a
= container_of(type_a
,
3648 struct bt_ctf_field_type_floating_point
, parent
);
3649 float_b
= container_of(type_b
,
3650 struct bt_ctf_field_type_floating_point
, parent
);
3653 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3657 /* Exponent length */
3658 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3662 /* Mantissa length */
3663 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3668 * Compare user byte orders only, not the cached,
3671 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3683 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3684 struct enumeration_mapping
*mapping_b
)
3689 if (mapping_a
->string
!= mapping_b
->string
) {
3694 if (mapping_a
->range_start
._unsigned
!=
3695 mapping_b
->range_start
._unsigned
) {
3700 if (mapping_a
->range_end
._unsigned
!=
3701 mapping_b
->range_end
._unsigned
) {
3713 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3714 struct bt_ctf_field_type
*type_b
)
3718 struct bt_ctf_field_type_enumeration
*enum_a
;
3719 struct bt_ctf_field_type_enumeration
*enum_b
;
3721 enum_a
= container_of(type_a
,
3722 struct bt_ctf_field_type_enumeration
, parent
);
3723 enum_b
= container_of(type_b
,
3724 struct bt_ctf_field_type_enumeration
, parent
);
3726 /* Container field type */
3727 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3735 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3739 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3740 struct enumeration_mapping
*mapping_a
=
3741 g_ptr_array_index(enum_a
->entries
, i
);
3742 struct enumeration_mapping
*mapping_b
=
3743 g_ptr_array_index(enum_b
->entries
, i
);
3745 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3758 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3759 struct bt_ctf_field_type
*type_b
)
3762 struct bt_ctf_field_type_string
*string_a
;
3763 struct bt_ctf_field_type_string
*string_b
;
3765 string_a
= container_of(type_a
,
3766 struct bt_ctf_field_type_string
, parent
);
3767 string_b
= container_of(type_b
,
3768 struct bt_ctf_field_type_string
, parent
);
3771 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3783 int compare_structure_fields(struct structure_field
*field_a
,
3784 struct structure_field
*field_b
)
3789 if (field_a
->name
!= field_b
->name
) {
3794 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3801 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3802 struct bt_ctf_field_type
*type_b
)
3806 struct bt_ctf_field_type_structure
*struct_a
;
3807 struct bt_ctf_field_type_structure
*struct_b
;
3809 struct_a
= container_of(type_a
,
3810 struct bt_ctf_field_type_structure
, parent
);
3811 struct_b
= container_of(type_b
,
3812 struct bt_ctf_field_type_structure
, parent
);
3815 if (bt_ctf_field_type_get_alignment(type_a
) !=
3816 bt_ctf_field_type_get_alignment(type_b
)) {
3821 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3825 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3826 struct structure_field
*field_a
=
3827 g_ptr_array_index(struct_a
->fields
, i
);
3828 struct structure_field
*field_b
=
3829 g_ptr_array_index(struct_b
->fields
, i
);
3831 ret
= compare_structure_fields(field_a
, field_b
);
3845 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3846 struct bt_ctf_field_type
*type_b
)
3850 struct bt_ctf_field_type_variant
*variant_a
;
3851 struct bt_ctf_field_type_variant
*variant_b
;
3853 variant_a
= container_of(type_a
,
3854 struct bt_ctf_field_type_variant
, parent
);
3855 variant_b
= container_of(type_b
,
3856 struct bt_ctf_field_type_variant
, parent
);
3859 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3864 ret
= bt_ctf_field_type_compare(
3865 (struct bt_ctf_field_type
*) variant_a
->tag
,
3866 (struct bt_ctf_field_type
*) variant_b
->tag
);
3874 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3878 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3879 struct structure_field
*field_a
=
3880 g_ptr_array_index(variant_a
->fields
, i
);
3881 struct structure_field
*field_b
=
3882 g_ptr_array_index(variant_b
->fields
, i
);
3884 ret
= compare_structure_fields(field_a
, field_b
);
3898 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3899 struct bt_ctf_field_type
*type_b
)
3902 struct bt_ctf_field_type_array
*array_a
;
3903 struct bt_ctf_field_type_array
*array_b
;
3905 array_a
= container_of(type_a
,
3906 struct bt_ctf_field_type_array
, parent
);
3907 array_b
= container_of(type_b
,
3908 struct bt_ctf_field_type_array
, parent
);
3911 if (array_a
->length
!= array_b
->length
) {
3916 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3917 array_b
->element_type
);
3924 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3925 struct bt_ctf_field_type
*type_b
)
3928 struct bt_ctf_field_type_sequence
*sequence_a
;
3929 struct bt_ctf_field_type_sequence
*sequence_b
;
3931 sequence_a
= container_of(type_a
,
3932 struct bt_ctf_field_type_sequence
, parent
);
3933 sequence_b
= container_of(type_b
,
3934 struct bt_ctf_field_type_sequence
, parent
);
3937 if (strcmp(sequence_a
->length_field_name
->str
,
3938 sequence_b
->length_field_name
->str
)) {
3943 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
3944 sequence_b
->element_type
);
3950 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
3951 struct bt_ctf_field_type
*type_b
)
3955 if (type_a
== type_b
) {
3956 /* Same reference: equal (even if both are NULL) */
3961 if (!type_a
|| !type_b
) {
3966 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
3967 /* Different type IDs */
3971 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
3972 /* Both have unknown type IDs */
3976 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
3983 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
3985 int field_count
= -1;
3986 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
3989 case CTF_TYPE_STRUCT
:
3991 bt_ctf_field_type_structure_get_field_count(field_type
);
3993 case CTF_TYPE_VARIANT
:
3995 bt_ctf_field_type_variant_get_field_count(field_type
);
3997 case CTF_TYPE_ARRAY
:
3998 case CTF_TYPE_SEQUENCE
:
4000 * Array and sequence types always contain a single member
4001 * (the element type).
4013 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4014 struct bt_ctf_field_type
*field_type
, int index
)
4016 struct bt_ctf_field_type
*field
= NULL
;
4017 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4020 case CTF_TYPE_STRUCT
:
4021 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4024 case CTF_TYPE_VARIANT
:
4025 bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4028 case CTF_TYPE_ARRAY
:
4029 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4031 case CTF_TYPE_SEQUENCE
:
4032 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4042 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4045 int field_index
= -1;
4046 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4049 case CTF_TYPE_STRUCT
:
4050 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4053 case CTF_TYPE_VARIANT
:
4054 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4064 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4065 struct bt_ctf_field_type
*type
)
4067 struct bt_ctf_field_path
*field_path
= NULL
;
4068 struct bt_ctf_field_type_variant
*variant
;
4070 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4074 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4076 field_path
= bt_get(variant
->tag_field_path
);
4081 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4082 struct bt_ctf_field_type
*type
)
4084 struct bt_ctf_field_path
*field_path
= NULL
;
4085 struct bt_ctf_field_type_sequence
*sequence
;
4087 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4091 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4093 field_path
= bt_get(sequence
->length_field_path
);