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-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/fields-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-ir/clock-internal.h>
36 #include <babeltrace/ctf-writer/writer-internal.h>
37 #include <babeltrace/object-internal.h>
38 #include <babeltrace/ref.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/endian.h>
45 struct range_overlap_query
{
60 void bt_ctf_field_type_destroy(struct bt_object
*);
62 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
64 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
66 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
68 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
70 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
72 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
74 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
76 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
79 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
80 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
82 bt_ctf_field_type_enumeration_destroy
,
84 bt_ctf_field_type_floating_point_destroy
,
85 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
86 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
87 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
88 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
89 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
93 void generic_field_type_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
99 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
101 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
103 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
105 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
108 type_freeze_func
const type_freeze_funcs
[] = {
109 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_freeze
,
110 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
111 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
112 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
113 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
114 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
115 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
116 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
120 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
121 struct metadata_context
*);
123 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
124 struct metadata_context
*);
126 int bt_ctf_field_type_floating_point_serialize(
127 struct bt_ctf_field_type
*, struct metadata_context
*);
129 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
130 struct metadata_context
*);
132 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
133 struct metadata_context
*);
135 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
136 struct metadata_context
*);
138 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
139 struct metadata_context
*);
141 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
142 struct metadata_context
*);
145 type_serialize_func
const type_serialize_funcs
[] = {
146 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
148 bt_ctf_field_type_enumeration_serialize
,
150 bt_ctf_field_type_floating_point_serialize
,
152 bt_ctf_field_type_structure_serialize
,
153 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
154 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
155 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
156 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
160 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
161 int byte_order
, int set_native
);
163 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
164 int byte_order
, int set_native
);
166 void bt_ctf_field_type_floating_point_set_byte_order(
167 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
169 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
170 int byte_order
, int set_native
);
172 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
173 int byte_order
, int set_native
);
175 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
176 int byte_order
, int set_native
);
178 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
179 int byte_order
, int set_native
);
181 /* The set_native flag only set the byte order if it is set to native */
183 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
184 int byte_order
, int set_native
) = {
185 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
187 bt_ctf_field_type_enumeration_set_byte_order
,
189 bt_ctf_field_type_floating_point_set_byte_order
,
191 bt_ctf_field_type_structure_set_byte_order
,
192 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
193 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
194 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
195 [CTF_TYPE_STRING
] = NULL
,
199 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
200 struct bt_ctf_field_type
*);
202 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
203 struct bt_ctf_field_type
*);
205 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
206 struct bt_ctf_field_type
*);
208 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
209 struct bt_ctf_field_type
*);
211 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
212 struct bt_ctf_field_type
*);
214 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
215 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
218 struct bt_ctf_field_type
*);
220 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
221 struct bt_ctf_field_type
*);
224 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
225 struct bt_ctf_field_type
*) = {
226 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
227 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
228 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
229 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
230 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
231 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
232 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
233 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
237 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
238 struct bt_ctf_field_type
*);
240 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
241 struct bt_ctf_field_type
*);
243 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
244 struct bt_ctf_field_type
*);
246 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
247 struct bt_ctf_field_type
*);
249 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
250 struct bt_ctf_field_type
*);
252 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
253 struct bt_ctf_field_type
*);
255 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
256 struct bt_ctf_field_type
*);
258 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
259 struct bt_ctf_field_type
*);
262 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
263 struct bt_ctf_field_type
*) = {
264 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_compare
,
265 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_compare
,
266 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
267 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_compare
,
268 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_compare
,
269 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_compare
,
270 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
271 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_compare
,
275 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
277 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
279 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
281 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
283 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
285 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
288 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
289 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_validate
,
290 [CTF_TYPE_FLOAT
] = NULL
,
291 [CTF_TYPE_STRING
] = NULL
,
292 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_validate
,
293 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_validate
,
294 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_validate
,
295 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_validate
,
296 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
300 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
306 void destroy_structure_field(struct structure_field
*field
)
313 void check_ranges_overlap(gpointer element
, gpointer query
)
315 struct enumeration_mapping
*mapping
= element
;
316 struct range_overlap_query
*overlap_query
= query
;
318 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
319 && overlap_query
->range_start
._signed
<=
320 mapping
->range_end
._signed
) {
321 overlap_query
->overlaps
= 1;
322 overlap_query
->mapping_name
= mapping
->string
;
325 overlap_query
->overlaps
|=
326 mapping
->string
== overlap_query
->mapping_name
;
330 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
332 struct enumeration_mapping
*mapping
= element
;
333 struct range_overlap_query
*overlap_query
= query
;
335 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
336 && overlap_query
->range_start
._unsigned
<=
337 mapping
->range_end
._unsigned
) {
338 overlap_query
->overlaps
= 1;
339 overlap_query
->mapping_name
= mapping
->string
;
342 overlap_query
->overlaps
|=
343 mapping
->string
== overlap_query
->mapping_name
;
347 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
348 struct enumeration_mapping
**b
)
350 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
354 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
355 struct enumeration_mapping
**b
)
357 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
361 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
363 enum ctf_type_id type_id
= type
->declaration
->id
;
365 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
366 (type_id
< NR_CTF_TYPES
));
368 bt_object_init(type
, bt_ctf_field_type_destroy
);
369 type
->freeze
= type_freeze_funcs
[type_id
];
370 type
->serialize
= type_serialize_funcs
[type_id
];
373 int ret
= bt_ctf_field_type_set_byte_order(type
,
374 BT_CTF_BYTE_ORDER_NATIVE
);
378 type
->declaration
->alignment
= 1;
382 int add_structure_field(GPtrArray
*fields
,
383 GHashTable
*field_name_to_index
,
384 struct bt_ctf_field_type
*field_type
,
385 const char *field_name
)
388 GQuark name_quark
= g_quark_from_string(field_name
);
389 struct structure_field
*field
;
391 /* Make sure structure does not contain a field of the same name */
392 if (g_hash_table_lookup_extended(field_name_to_index
,
393 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
398 field
= g_new0(struct structure_field
, 1);
405 field
->name
= name_quark
;
406 field
->type
= field_type
;
407 g_hash_table_insert(field_name_to_index
,
408 (gpointer
) (unsigned long) name_quark
,
409 (gpointer
) (unsigned long) fields
->len
);
410 g_ptr_array_add(fields
, field
);
416 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
418 struct bt_ctf_field_type
*type
;
419 enum ctf_type_id type_id
;
421 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
422 type_id
= type
->declaration
->id
;
423 if (type_id
<= CTF_TYPE_UNKNOWN
||
424 type_id
>= NR_CTF_TYPES
) {
428 type_destroy_funcs
[type_id
](type
);
432 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
436 struct bt_ctf_field_type_integer
*integer
=
437 container_of(type
, struct bt_ctf_field_type_integer
,
440 if (integer
->mapped_clock
&& integer
->declaration
.signedness
) {
450 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
454 struct bt_ctf_field_type_enumeration
*enumeration
=
455 container_of(type
, struct bt_ctf_field_type_enumeration
,
457 struct bt_ctf_field_type
*container_type
=
458 bt_ctf_field_type_enumeration_get_container_type(type
);
460 if (!container_type
) {
465 ret
= bt_ctf_field_type_validate(container_type
);
470 /* Ensure enum has entries */
471 ret
= enumeration
->entries
->len
? 0 : -1;
474 BT_PUT(container_type
);
479 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
482 struct bt_ctf_field_type
*element_type
= NULL
;
483 struct bt_ctf_field_type_sequence
*sequence
=
484 container_of(type
, struct bt_ctf_field_type_sequence
,
487 /* Length field name should be set at this point */
488 if (sequence
->length_field_name
->len
== 0) {
493 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
499 ret
= bt_ctf_field_type_validate(element_type
);
502 BT_PUT(element_type
);
508 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
511 struct bt_ctf_field_type
*element_type
= NULL
;
513 element_type
= bt_ctf_field_type_array_get_element_type(type
);
519 ret
= bt_ctf_field_type_validate(element_type
);
522 BT_PUT(element_type
);
528 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
531 struct bt_ctf_field_type
*child_type
= NULL
;
532 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
535 if (field_count
< 0) {
540 for (i
= 0; i
< field_count
; ++i
) {
541 ret
= bt_ctf_field_type_structure_get_field(type
,
542 NULL
, &child_type
, i
);
547 ret
= bt_ctf_field_type_validate(child_type
);
562 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
566 struct bt_ctf_field_type
*child_type
= NULL
;
567 struct bt_ctf_field_type_variant
*variant
=
568 container_of(type
, struct bt_ctf_field_type_variant
,
571 int tag_mappings_count
;
573 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
579 bt_ctf_field_type_enumeration_get_mapping_count(
580 (struct bt_ctf_field_type
*) variant
->tag
);
582 if (tag_mappings_count
!= variant
->fields
->len
) {
587 for (i
= 0; i
< tag_mappings_count
; ++i
) {
589 int64_t range_start
, range_end
;
590 struct bt_ctf_field_type
*ft
;
592 ret
= bt_ctf_field_type_enumeration_get_mapping(
593 (struct bt_ctf_field_type
*) variant
->tag
,
594 i
, &label
, &range_start
, &range_end
);
603 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
613 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
614 if (field_count
< 0) {
619 for (i
= 0; i
< field_count
; ++i
) {
620 ret
= bt_ctf_field_type_variant_get_field(type
,
621 NULL
, &child_type
, i
);
626 ret
= bt_ctf_field_type_validate(child_type
);
641 * This function validates a given field type without considering
642 * where this field type is located. It only validates the properties
643 * of the given field type and the properties of its children if
647 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
650 enum ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
658 /* Already marked as valid */
662 if (type_validate_funcs
[id
]) {
663 ret
= type_validate_funcs
[id
](type
);
666 if (!ret
&& type
->frozen
) {
667 /* Field type is valid */
675 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
677 struct bt_ctf_field_type_integer
*integer
=
678 g_new0(struct bt_ctf_field_type_integer
, 1);
680 if (!integer
|| size
== 0 || size
> 64) {
684 integer
->parent
.declaration
= &integer
->declaration
.p
;
685 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
686 integer
->declaration
.len
= size
;
687 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
688 integer
->declaration
.encoding
=
689 (enum ctf_string_encoding
) BT_CTF_STRING_ENCODING_NONE
;
690 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
691 return &integer
->parent
;
695 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
698 struct bt_ctf_field_type_integer
*integer
;
700 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
705 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
706 ret
= (int) integer
->declaration
.len
;
711 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
714 struct bt_ctf_field_type_integer
*integer
;
716 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
721 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
722 ret
= integer
->declaration
.signedness
;
727 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
731 struct bt_ctf_field_type_integer
*integer
;
733 if (!type
|| type
->frozen
||
734 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
739 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
740 integer
->declaration
.signedness
= !!is_signed
;
746 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
747 struct bt_ctf_field_type
*type
)
749 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
750 struct bt_ctf_field_type_integer
*integer
;
752 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
756 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
757 ret
= integer
->declaration
.base
;
762 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
763 enum bt_ctf_integer_base base
)
767 if (!type
|| type
->frozen
||
768 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
774 case BT_CTF_INTEGER_BASE_BINARY
:
775 case BT_CTF_INTEGER_BASE_OCTAL
:
776 case BT_CTF_INTEGER_BASE_DECIMAL
:
777 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
779 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
780 struct bt_ctf_field_type_integer
, parent
);
781 integer
->declaration
.base
= base
;
792 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
793 struct bt_ctf_field_type
*type
)
795 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
796 struct bt_ctf_field_type_integer
*integer
;
798 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
802 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
803 ret
= (enum bt_ctf_string_encoding
) integer
->declaration
.encoding
;
808 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
809 enum bt_ctf_string_encoding encoding
)
812 struct bt_ctf_field_type_integer
*integer
;
814 if (!type
|| type
->frozen
||
815 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
816 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
817 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
822 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
823 integer
->declaration
.encoding
= (enum ctf_string_encoding
) encoding
;
829 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
830 struct bt_ctf_field_type
*type
)
832 struct bt_ctf_field_type_integer
*integer
;
833 struct bt_ctf_clock
*clock
= NULL
;
839 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
840 clock
= integer
->mapped_clock
;
847 int bt_ctf_field_type_integer_set_mapped_clock(
848 struct bt_ctf_field_type
*type
,
849 struct bt_ctf_clock
*clock
)
851 struct bt_ctf_field_type_integer
*integer
;
854 if (!type
|| type
->frozen
) {
859 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
860 bt_put(integer
->mapped_clock
);
862 integer
->mapped_clock
= clock
;
867 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
868 struct bt_ctf_field_type
*integer_container_type
)
870 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
872 if (!integer_container_type
) {
876 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
880 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
885 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
886 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
887 bt_get(integer_container_type
);
888 enumeration
->container
= integer_container_type
;
889 enumeration
->entries
= g_ptr_array_new_with_free_func(
890 (GDestroyNotify
)destroy_enumeration_mapping
);
891 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
892 return &enumeration
->parent
;
899 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
900 struct bt_ctf_field_type
*type
)
902 struct bt_ctf_field_type
*container_type
= NULL
;
903 struct bt_ctf_field_type_enumeration
*enumeration_type
;
909 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
913 enumeration_type
= container_of(type
,
914 struct bt_ctf_field_type_enumeration
, parent
);
915 container_type
= enumeration_type
->container
;
916 bt_get(container_type
);
918 return container_type
;
921 int bt_ctf_field_type_enumeration_add_mapping(
922 struct bt_ctf_field_type
*type
, const char *string
,
923 int64_t range_start
, int64_t range_end
)
927 struct enumeration_mapping
*mapping
;
928 struct bt_ctf_field_type_enumeration
*enumeration
;
929 struct range_overlap_query query
;
930 char *escaped_string
;
932 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
934 (range_end
< range_start
)) {
939 if (!string
|| strlen(string
) == 0) {
944 escaped_string
= g_strescape(string
, NULL
);
945 if (!escaped_string
) {
950 mapping_name
= g_quark_from_string(escaped_string
);
951 query
= (struct range_overlap_query
) {
952 .range_start
._signed
= range_start
,
953 .range_end
._signed
= range_end
,
954 .mapping_name
= mapping_name
,
956 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
959 /* Check that the range does not overlap with one already present */
960 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
961 if (query
.overlaps
) {
966 mapping
= g_new(struct enumeration_mapping
, 1);
972 *mapping
= (struct enumeration_mapping
) {
973 .range_start
._signed
= range_start
,
974 .range_end
._signed
= range_end
, .string
= mapping_name
};
975 g_ptr_array_add(enumeration
->entries
, mapping
);
976 g_ptr_array_sort(enumeration
->entries
,
977 (GCompareFunc
)compare_enumeration_mappings_signed
);
979 free(escaped_string
);
985 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
986 struct bt_ctf_field_type
*type
, const char *string
,
987 uint64_t range_start
, uint64_t range_end
)
991 struct enumeration_mapping
*mapping
;
992 struct bt_ctf_field_type_enumeration
*enumeration
;
993 struct range_overlap_query query
;
994 char *escaped_string
;
996 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
998 (range_end
< range_start
)) {
1003 if (!string
|| strlen(string
) == 0) {
1008 escaped_string
= g_strescape(string
, NULL
);
1009 if (!escaped_string
) {
1014 mapping_name
= g_quark_from_string(escaped_string
);
1015 query
= (struct range_overlap_query
) {
1016 .range_start
._unsigned
= range_start
,
1017 .range_end
._unsigned
= range_end
,
1018 .mapping_name
= mapping_name
,
1020 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1023 /* Check that the range does not overlap with one already present */
1024 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
1026 if (query
.overlaps
) {
1031 mapping
= g_new(struct enumeration_mapping
, 1);
1037 *mapping
= (struct enumeration_mapping
) {
1038 .range_start
._unsigned
= range_start
,
1039 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1040 g_ptr_array_add(enumeration
->entries
, mapping
);
1041 g_ptr_array_sort(enumeration
->entries
,
1042 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1044 free(escaped_string
);
1050 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1051 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1054 const char *name
= NULL
;
1055 struct range_overlap_query query
=
1056 (struct range_overlap_query
) {
1057 .range_start
._unsigned
= value
,
1058 .range_end
._unsigned
= value
,
1061 g_ptr_array_foreach(enumeration_type
->entries
,
1062 check_ranges_overlap_unsigned
,
1064 if (!query
.overlaps
) {
1068 name
= g_quark_to_string(query
.mapping_name
);
1073 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1074 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1077 const char *name
= NULL
;
1078 struct range_overlap_query query
=
1079 (struct range_overlap_query
) {
1080 .range_start
._signed
= value
,
1081 .range_end
._signed
= value
,
1084 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1086 if (!query
.overlaps
) {
1090 name
= g_quark_to_string(query
.mapping_name
);
1095 int bt_ctf_field_type_enumeration_get_mapping_count(
1096 struct bt_ctf_field_type
*type
)
1099 struct bt_ctf_field_type_enumeration
*enumeration
;
1101 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1106 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1108 ret
= (int) enumeration
->entries
->len
;
1114 struct enumeration_mapping
*get_enumeration_mapping(
1115 struct bt_ctf_field_type
*type
, int index
)
1117 struct enumeration_mapping
*mapping
= NULL
;
1118 struct bt_ctf_field_type_enumeration
*enumeration
;
1120 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1122 if (index
>= enumeration
->entries
->len
) {
1126 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1132 int bt_ctf_field_type_enumeration_get_mapping(
1133 struct bt_ctf_field_type
*type
, int index
,
1134 const char **string
, int64_t *range_start
, int64_t *range_end
)
1136 struct enumeration_mapping
*mapping
;
1139 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1140 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1145 mapping
= get_enumeration_mapping(type
, index
);
1151 *string
= g_quark_to_string(mapping
->string
);
1152 *range_start
= mapping
->range_start
._signed
;
1153 *range_end
= mapping
->range_end
._signed
;
1159 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1160 struct bt_ctf_field_type
*type
, int index
,
1161 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1163 struct enumeration_mapping
*mapping
;
1166 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1167 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1172 mapping
= get_enumeration_mapping(type
, index
);
1178 *string
= g_quark_to_string(mapping
->string
);
1179 *range_start
= mapping
->range_start
._unsigned
;
1180 *range_end
= mapping
->range_end
._unsigned
;
1186 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1187 struct bt_ctf_field_type
*type
, const char *name
)
1190 struct bt_ctf_field_type_enumeration
*enumeration
;
1193 if (!type
|| !name
||
1194 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1199 name_quark
= g_quark_try_string(name
);
1205 enumeration
= container_of(type
,
1206 struct bt_ctf_field_type_enumeration
, parent
);
1207 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1208 struct enumeration_mapping
*mapping
=
1209 get_enumeration_mapping(type
, i
);
1211 if (mapping
->string
== name_quark
) {
1223 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1224 struct bt_ctf_field_type
*type
, int64_t value
)
1226 struct bt_ctf_field_type_enumeration
*enumeration
;
1229 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1234 enumeration
= container_of(type
,
1235 struct bt_ctf_field_type_enumeration
, parent
);
1236 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1237 struct enumeration_mapping
*mapping
=
1238 get_enumeration_mapping(type
, i
);
1240 if (value
>= mapping
->range_start
._signed
&&
1241 value
<= mapping
->range_end
._signed
) {
1253 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1254 struct bt_ctf_field_type
*type
, uint64_t value
)
1256 struct bt_ctf_field_type_enumeration
*enumeration
;
1259 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1264 enumeration
= container_of(type
,
1265 struct bt_ctf_field_type_enumeration
, parent
);
1266 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1267 struct enumeration_mapping
*mapping
=
1268 get_enumeration_mapping(type
, i
);
1270 if (value
>= mapping
->range_start
._unsigned
&&
1271 value
<= mapping
->range_end
._unsigned
) {
1282 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1284 struct bt_ctf_field_type_floating_point
*floating_point
=
1285 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1287 if (!floating_point
) {
1291 floating_point
->declaration
.sign
= &floating_point
->sign
;
1292 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1293 floating_point
->declaration
.exp
= &floating_point
->exp
;
1294 floating_point
->sign
.len
= 1;
1295 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1296 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1297 floating_point
->declaration
.exp
->len
=
1298 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1299 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1300 floating_point
->sign
.p
.alignment
= 1;
1301 floating_point
->mantissa
.p
.alignment
= 1;
1302 floating_point
->exp
.p
.alignment
= 1;
1304 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1306 return floating_point
? &floating_point
->parent
: NULL
;
1310 int bt_ctf_field_type_floating_point_get_exponent_digits(
1311 struct bt_ctf_field_type
*type
)
1314 struct bt_ctf_field_type_floating_point
*floating_point
;
1316 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1321 floating_point
= container_of(type
,
1322 struct bt_ctf_field_type_floating_point
, parent
);
1323 ret
= (int) floating_point
->declaration
.exp
->len
;
1328 int bt_ctf_field_type_floating_point_set_exponent_digits(
1329 struct bt_ctf_field_type
*type
,
1330 unsigned int exponent_digits
)
1333 struct bt_ctf_field_type_floating_point
*floating_point
;
1335 if (!type
|| type
->frozen
||
1336 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1341 floating_point
= container_of(type
,
1342 struct bt_ctf_field_type_floating_point
, parent
);
1343 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1344 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1346 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1351 floating_point
->declaration
.exp
->len
= exponent_digits
;
1357 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1358 struct bt_ctf_field_type
*type
)
1361 struct bt_ctf_field_type_floating_point
*floating_point
;
1363 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1368 floating_point
= container_of(type
,
1369 struct bt_ctf_field_type_floating_point
, parent
);
1370 ret
= (int) floating_point
->mantissa
.len
+ 1;
1375 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1376 struct bt_ctf_field_type
*type
,
1377 unsigned int mantissa_digits
)
1380 struct bt_ctf_field_type_floating_point
*floating_point
;
1382 if (!type
|| type
->frozen
||
1383 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1388 floating_point
= container_of(type
,
1389 struct bt_ctf_field_type_floating_point
, parent
);
1391 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1392 (mantissa_digits
!= DBL_MANT_DIG
) &&
1393 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1398 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1403 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1405 struct bt_ctf_field_type_structure
*structure
=
1406 g_new0(struct bt_ctf_field_type_structure
, 1);
1412 structure
->parent
.declaration
= &structure
->declaration
.p
;
1413 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1414 structure
->fields
= g_ptr_array_new_with_free_func(
1415 (GDestroyNotify
)destroy_structure_field
);
1416 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1417 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1418 return &structure
->parent
;
1423 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1424 struct bt_ctf_field_type
*field_type
,
1425 const char *field_name
)
1428 struct bt_ctf_field_type_structure
*structure
;
1430 if (!type
|| !field_type
|| type
->frozen
||
1431 bt_ctf_validate_identifier(field_name
) ||
1432 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1437 structure
= container_of(type
,
1438 struct bt_ctf_field_type_structure
, parent
);
1439 if (add_structure_field(structure
->fields
,
1440 structure
->field_name_to_index
, field_type
, field_name
)) {
1449 int bt_ctf_field_type_structure_get_field_count(
1450 struct bt_ctf_field_type
*type
)
1453 struct bt_ctf_field_type_structure
*structure
;
1455 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1460 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1462 ret
= (int) structure
->fields
->len
;
1467 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1468 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1471 struct bt_ctf_field_type_structure
*structure
;
1472 struct structure_field
*field
;
1475 if (!type
|| index
< 0 ||
1476 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1481 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1483 if (index
>= structure
->fields
->len
) {
1488 field
= g_ptr_array_index(structure
->fields
, index
);
1490 *field_type
= field
->type
;
1491 bt_get(field
->type
);
1494 *field_name
= g_quark_to_string(field
->name
);
1501 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1502 struct bt_ctf_field_type
*type
,
1507 struct structure_field
*field
;
1508 struct bt_ctf_field_type_structure
*structure
;
1509 struct bt_ctf_field_type
*field_type
= NULL
;
1511 if (!type
|| !name
) {
1515 name_quark
= g_quark_try_string(name
);
1520 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1522 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1523 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1527 field
= structure
->fields
->pdata
[index
];
1528 field_type
= field
->type
;
1534 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1535 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1537 struct bt_ctf_field_type_variant
*variant
= NULL
;
1539 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1543 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1548 variant
->parent
.declaration
= &variant
->declaration
.p
;
1549 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1550 variant
->tag_name
= g_string_new(tag_name
);
1551 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1552 variant
->fields
= g_ptr_array_new_with_free_func(
1553 (GDestroyNotify
) destroy_structure_field
);
1556 variant
->tag
= container_of(enum_tag
,
1557 struct bt_ctf_field_type_enumeration
, parent
);
1560 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1561 /* A variant's alignment is undefined */
1562 variant
->parent
.declaration
->alignment
= 0;
1563 return &variant
->parent
;
1569 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1570 struct bt_ctf_field_type
*type
)
1572 struct bt_ctf_field_type_variant
*variant
;
1573 struct bt_ctf_field_type
*tag_type
= NULL
;
1575 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1579 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1580 if (!variant
->tag
) {
1584 tag_type
= &variant
->tag
->parent
;
1591 const char *bt_ctf_field_type_variant_get_tag_name(
1592 struct bt_ctf_field_type
*type
)
1594 struct bt_ctf_field_type_variant
*variant
;
1595 const char *tag_name
= NULL
;
1597 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1601 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1602 if (variant
->tag_name
->len
== 0) {
1606 tag_name
= variant
->tag_name
->str
;
1612 int bt_ctf_field_type_variant_set_tag_name(
1613 struct bt_ctf_field_type
*type
, const char *name
)
1616 struct bt_ctf_field_type_variant
*variant
;
1618 if (!type
|| type
->frozen
||
1619 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1620 bt_ctf_validate_identifier(name
)) {
1625 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1626 g_string_assign(variant
->tag_name
, name
);
1631 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1632 struct bt_ctf_field_type
*field_type
,
1633 const char *field_name
)
1637 struct bt_ctf_field_type_variant
*variant
;
1638 GQuark field_name_quark
= g_quark_from_string(field_name
);
1640 if (!type
|| !field_type
|| type
->frozen
||
1641 bt_ctf_validate_identifier(field_name
) ||
1642 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1647 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1649 /* The user has explicitly provided a tag; validate against it. */
1653 /* Make sure this name is present in the enum tag */
1654 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1655 struct enumeration_mapping
*mapping
=
1656 g_ptr_array_index(variant
->tag
->entries
, i
);
1658 if (mapping
->string
== field_name_quark
) {
1665 /* Validation failed */
1671 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1672 field_type
, field_name
)) {
1681 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1682 struct bt_ctf_field_type
*type
,
1683 const char *field_name
)
1687 struct structure_field
*field
;
1688 struct bt_ctf_field_type_variant
*variant
;
1689 struct bt_ctf_field_type
*field_type
= NULL
;
1691 if (!type
|| !field_name
) {
1695 name_quark
= g_quark_try_string(field_name
);
1700 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1701 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1702 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1706 field
= g_ptr_array_index(variant
->fields
, index
);
1707 field_type
= field
->type
;
1714 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1715 struct bt_ctf_field_type
*type
,
1716 struct bt_ctf_field
*tag
)
1718 const char *enum_value
;
1719 struct bt_ctf_field_type
*field_type
= NULL
;
1721 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1725 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1730 /* Already increments field_type's reference count */
1731 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1737 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1740 struct bt_ctf_field_type_variant
*variant
;
1742 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1747 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1749 ret
= (int) variant
->fields
->len
;
1756 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1757 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1760 struct bt_ctf_field_type_variant
*variant
;
1761 struct structure_field
*field
;
1764 if (!type
|| index
< 0 ||
1765 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1770 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1772 if (index
>= variant
->fields
->len
) {
1777 field
= g_ptr_array_index(variant
->fields
, index
);
1779 *field_type
= field
->type
;
1780 bt_get(field
->type
);
1783 *field_name
= g_quark_to_string(field
->name
);
1789 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1790 struct bt_ctf_field_type
*element_type
,
1791 unsigned int length
)
1793 struct bt_ctf_field_type_array
*array
= NULL
;
1795 if (!element_type
|| length
== 0) {
1799 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1804 array
->parent
.declaration
= &array
->declaration
.p
;
1805 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1807 bt_get(element_type
);
1808 array
->element_type
= element_type
;
1809 array
->length
= length
;
1810 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1811 return &array
->parent
;
1817 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1818 struct bt_ctf_field_type
*type
)
1820 struct bt_ctf_field_type
*ret
= NULL
;
1821 struct bt_ctf_field_type_array
*array
;
1823 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1827 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1828 ret
= array
->element_type
;
1835 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1836 struct bt_ctf_field_type
*element_type
)
1839 struct bt_ctf_field_type_array
*array
;
1841 if (!type
|| !element_type
||
1842 (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1847 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1849 if (array
->element_type
) {
1850 BT_PUT(array
->element_type
);
1853 array
->element_type
= element_type
;
1854 bt_get(array
->element_type
);
1861 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1864 struct bt_ctf_field_type_array
*array
;
1866 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1871 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1872 ret
= (int64_t) array
->length
;
1877 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1878 struct bt_ctf_field_type
*element_type
,
1879 const char *length_field_name
)
1881 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1883 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1887 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1892 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1893 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1894 bt_get(element_type
);
1895 sequence
->element_type
= element_type
;
1896 sequence
->length_field_name
= g_string_new(length_field_name
);
1897 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1898 return &sequence
->parent
;
1904 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1905 struct bt_ctf_field_type
*type
)
1907 struct bt_ctf_field_type
*ret
= NULL
;
1908 struct bt_ctf_field_type_sequence
*sequence
;
1910 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1914 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1916 ret
= sequence
->element_type
;
1923 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1924 struct bt_ctf_field_type
*element_type
)
1927 struct bt_ctf_field_type_sequence
*sequence
;
1929 if (!type
|| !element_type
||
1930 (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1935 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1937 if (sequence
->element_type
) {
1938 BT_PUT(sequence
->element_type
);
1941 sequence
->element_type
= element_type
;
1942 bt_get(sequence
->element_type
);
1949 const char *bt_ctf_field_type_sequence_get_length_field_name(
1950 struct bt_ctf_field_type
*type
)
1952 const char *ret
= NULL
;
1953 struct bt_ctf_field_type_sequence
*sequence
;
1955 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1959 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1961 ret
= sequence
->length_field_name
->str
;
1966 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1968 struct bt_ctf_field_type_string
*string
=
1969 g_new0(struct bt_ctf_field_type_string
, 1);
1975 string
->parent
.declaration
= &string
->declaration
.p
;
1976 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1977 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1978 string
->declaration
.encoding
=
1979 (enum ctf_string_encoding
) BT_CTF_STRING_ENCODING_UTF8
;
1980 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1981 return &string
->parent
;
1985 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1986 struct bt_ctf_field_type
*type
)
1988 struct bt_ctf_field_type_string
*string
;
1989 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1991 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1995 string
= container_of(type
, struct bt_ctf_field_type_string
,
1997 ret
= (enum bt_ctf_string_encoding
) string
->declaration
.encoding
;
2002 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2003 enum bt_ctf_string_encoding encoding
)
2006 struct bt_ctf_field_type_string
*string
;
2008 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
2009 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2010 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2015 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2016 string
->declaration
.encoding
= (enum ctf_string_encoding
) encoding
;
2022 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2025 enum ctf_type_id type_id
;
2033 ret
= (int) type
->declaration
->alignment
;
2037 type_id
= bt_ctf_field_type_get_type_id(type
);
2039 case CTF_TYPE_SEQUENCE
:
2041 struct bt_ctf_field_type
*element
=
2042 bt_ctf_field_type_sequence_get_element_type(type
);
2049 ret
= bt_ctf_field_type_get_alignment(element
);
2053 case CTF_TYPE_ARRAY
:
2055 struct bt_ctf_field_type
*element
=
2056 bt_ctf_field_type_array_get_element_type(type
);
2063 ret
= bt_ctf_field_type_get_alignment(element
);
2067 case CTF_TYPE_STRUCT
:
2069 int i
, element_count
;
2071 element_count
= bt_ctf_field_type_structure_get_field_count(
2073 if (element_count
< 0) {
2074 ret
= element_count
;
2078 for (i
= 0; i
< element_count
; i
++) {
2079 struct bt_ctf_field_type
*field
;
2080 int field_alignment
;
2082 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2089 field_alignment
= bt_ctf_field_type_get_alignment(
2092 if (field_alignment
< 0) {
2093 ret
= field_alignment
;
2097 type
->declaration
->alignment
= MAX(field_alignment
,
2098 type
->declaration
->alignment
);
2100 ret
= (int) type
->declaration
->alignment
;
2103 case CTF_TYPE_UNKNOWN
:
2107 ret
= (int) type
->declaration
->alignment
;
2115 int is_power_of_two(unsigned int value
)
2117 return ((value
& (value
- 1)) == 0) && value
> 0;
2120 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2121 unsigned int alignment
)
2124 enum ctf_type_id type_id
;
2126 /* Alignment must be a power of two */
2127 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2132 type_id
= bt_ctf_field_type_get_type_id(type
);
2133 if (type_id
== CTF_TYPE_UNKNOWN
) {
2138 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
2139 alignment
!= CHAR_BIT
) {
2144 if (type_id
== CTF_TYPE_VARIANT
||
2145 type_id
== CTF_TYPE_SEQUENCE
||
2146 type_id
== CTF_TYPE_ARRAY
) {
2147 /* Setting an alignment on these types makes no sense */
2152 type
->declaration
->alignment
= alignment
;
2159 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2160 struct bt_ctf_field_type
*type
)
2162 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2168 switch (type
->declaration
->id
) {
2169 case CTF_TYPE_INTEGER
:
2171 struct bt_ctf_field_type_integer
*integer
= container_of(
2172 type
, struct bt_ctf_field_type_integer
, parent
);
2173 ret
= integer
->user_byte_order
;
2176 case CTF_TYPE_FLOAT
:
2178 struct bt_ctf_field_type_floating_point
*floating_point
=
2180 struct bt_ctf_field_type_floating_point
,
2182 ret
= floating_point
->user_byte_order
;
2189 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2190 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2191 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2192 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2198 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2199 enum bt_ctf_byte_order byte_order
)
2202 int internal_byte_order
;
2203 enum ctf_type_id type_id
;
2205 if (!type
|| type
->frozen
) {
2210 switch (byte_order
) {
2211 case BT_CTF_BYTE_ORDER_NATIVE
:
2212 /* Leave unset. Will be initialized by parent. */
2213 internal_byte_order
= 0;
2215 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2216 internal_byte_order
= LITTLE_ENDIAN
;
2218 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2219 case BT_CTF_BYTE_ORDER_NETWORK
:
2220 internal_byte_order
= BIG_ENDIAN
;
2227 type_id
= type
->declaration
->id
;
2228 if (set_byte_order_funcs
[type_id
]) {
2229 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2236 enum ctf_type_id
bt_ctf_field_type_get_type_id(
2237 struct bt_ctf_field_type
*type
)
2240 return CTF_TYPE_UNKNOWN
;
2243 return type
->declaration
->id
;
2247 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2249 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
2253 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2255 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
2259 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2261 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
2265 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2267 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
2271 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2273 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
2277 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2279 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
2283 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2285 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
2289 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2291 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
2294 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2299 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2305 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2315 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2316 struct bt_ctf_field_type_variant
*variant
,
2319 struct bt_ctf_field_type
*type
= NULL
;
2320 GQuark field_name_quark
;
2322 struct structure_field
*field_entry
;
2323 struct range_overlap_query query
= {
2324 .range_start
._signed
= tag_value
,
2325 .range_end
._signed
= tag_value
,
2326 .mapping_name
= 0, .overlaps
= 0};
2328 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2330 if (!query
.overlaps
) {
2334 field_name_quark
= query
.mapping_name
;
2335 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2336 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2340 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2341 type
= field_entry
->type
;
2347 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2348 struct bt_ctf_field_type_variant
*variant
,
2351 struct bt_ctf_field_type
*type
= NULL
;
2352 GQuark field_name_quark
;
2354 struct structure_field
*field_entry
;
2355 struct range_overlap_query query
= {
2356 .range_start
._unsigned
= tag_value
,
2357 .range_end
._unsigned
= tag_value
,
2358 .mapping_name
= 0, .overlaps
= 0};
2360 g_ptr_array_foreach(variant
->tag
->entries
,
2361 check_ranges_overlap_unsigned
,
2363 if (!query
.overlaps
) {
2367 field_name_quark
= query
.mapping_name
;
2368 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2369 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2373 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2374 type
= field_entry
->type
;
2380 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2381 struct metadata_context
*context
)
2385 if (!type
|| !context
) {
2390 /* Make sure field type is valid before serializing it */
2391 ret
= bt_ctf_field_type_validate(type
);
2397 ret
= type
->serialize(type
, context
);
2403 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2410 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2411 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2412 set_byte_order_funcs
[type
->declaration
->id
](type
,
2418 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2420 struct bt_ctf_field_type
*copy
= NULL
;
2426 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2432 int bt_ctf_field_type_structure_get_field_name_index(
2433 struct bt_ctf_field_type
*type
, const char *name
)
2438 struct bt_ctf_field_type_structure
*structure
;
2440 if (!type
|| !name
||
2441 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2446 name_quark
= g_quark_try_string(name
);
2452 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2454 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2455 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2465 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2466 struct bt_ctf_field_type
*field
, int index
)
2469 struct bt_ctf_field_type_structure
*structure
;
2471 if (!type
|| !field
||
2472 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2477 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2479 if (index
< 0 || index
>= structure
->fields
->len
) {
2485 bt_put(((struct structure_field
*)
2486 g_ptr_array_index(structure
->fields
, index
))->type
);
2487 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2494 int bt_ctf_field_type_variant_get_field_name_index(
2495 struct bt_ctf_field_type
*type
, const char *name
)
2500 struct bt_ctf_field_type_variant
*variant
;
2502 if (!type
|| !name
||
2503 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2508 name_quark
= g_quark_try_string(name
);
2514 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2516 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2517 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2527 int bt_ctf_field_type_sequence_set_length_field_path(
2528 struct bt_ctf_field_type
*type
,
2529 struct bt_ctf_field_path
*path
)
2532 struct bt_ctf_field_type_sequence
*sequence
;
2534 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2535 CTF_TYPE_SEQUENCE
) {
2540 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2543 BT_MOVE(sequence
->length_field_path
, path
);
2549 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2550 struct bt_ctf_field_path
*path
)
2553 struct bt_ctf_field_type_variant
*variant
;
2555 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2561 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2564 BT_MOVE(variant
->tag_field_path
, path
);
2570 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2571 struct bt_ctf_field_type
*tag
)
2574 struct bt_ctf_field_type_variant
*variant
;
2576 if (!type
|| !tag
||
2577 bt_ctf_field_type_get_type_id(tag
) !=
2583 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2587 bt_put(&variant
->tag
->parent
);
2589 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2596 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2597 struct bt_ctf_field_type
*field
, int index
)
2600 struct bt_ctf_field_type_variant
*variant
;
2602 if (!type
|| !field
||
2603 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2608 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2610 if (index
< 0 || index
>= variant
->fields
->len
) {
2616 bt_put(((struct structure_field
*)
2617 g_ptr_array_index(variant
->fields
, index
))->type
);
2618 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2625 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2627 struct bt_ctf_field_type_integer
*integer
=
2628 (struct bt_ctf_field_type_integer
*) type
;
2634 bt_put(integer
->mapped_clock
);
2639 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2641 struct bt_ctf_field_type_enumeration
*enumeration
=
2642 (struct bt_ctf_field_type_enumeration
*) type
;
2648 g_ptr_array_free(enumeration
->entries
, TRUE
);
2649 bt_put(enumeration
->container
);
2650 g_free(enumeration
);
2654 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2656 struct bt_ctf_field_type_floating_point
*floating_point
=
2657 (struct bt_ctf_field_type_floating_point
*) type
;
2663 g_free(floating_point
);
2667 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2669 struct bt_ctf_field_type_structure
*structure
=
2670 (struct bt_ctf_field_type_structure
*) type
;
2676 g_ptr_array_free(structure
->fields
, TRUE
);
2677 g_hash_table_destroy(structure
->field_name_to_index
);
2682 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2684 struct bt_ctf_field_type_variant
*variant
=
2685 (struct bt_ctf_field_type_variant
*) type
;
2691 g_ptr_array_free(variant
->fields
, TRUE
);
2692 g_hash_table_destroy(variant
->field_name_to_index
);
2693 g_string_free(variant
->tag_name
, TRUE
);
2694 bt_put(&variant
->tag
->parent
);
2695 BT_PUT(variant
->tag_field_path
);
2700 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2702 struct bt_ctf_field_type_array
*array
=
2703 (struct bt_ctf_field_type_array
*) type
;
2709 bt_put(array
->element_type
);
2714 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2716 struct bt_ctf_field_type_sequence
*sequence
=
2717 (struct bt_ctf_field_type_sequence
*) type
;
2723 bt_put(sequence
->element_type
);
2724 g_string_free(sequence
->length_field_name
, TRUE
);
2725 BT_PUT(sequence
->length_field_path
);
2730 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2732 struct bt_ctf_field_type_string
*string
=
2733 (struct bt_ctf_field_type_string
*) type
;
2743 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2749 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2751 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2752 type
, struct bt_ctf_field_type_integer
, parent
);
2754 if (integer_type
->mapped_clock
) {
2755 bt_ctf_clock_freeze(integer_type
->mapped_clock
);
2758 generic_field_type_freeze(type
);
2762 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2764 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2765 type
, struct bt_ctf_field_type_enumeration
, parent
);
2767 generic_field_type_freeze(type
);
2768 bt_ctf_field_type_freeze(enumeration_type
->container
);
2772 void freeze_structure_field(struct structure_field
*field
)
2774 bt_ctf_field_type_freeze(field
->type
);
2778 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2780 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2781 type
, struct bt_ctf_field_type_structure
, parent
);
2783 /* Cache the alignment */
2784 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2785 generic_field_type_freeze(type
);
2786 g_ptr_array_foreach(structure_type
->fields
,
2787 (GFunc
) freeze_structure_field
, NULL
);
2791 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2793 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2794 type
, struct bt_ctf_field_type_variant
, parent
);
2796 generic_field_type_freeze(type
);
2797 g_ptr_array_foreach(variant_type
->fields
,
2798 (GFunc
) freeze_structure_field
, NULL
);
2802 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2804 struct bt_ctf_field_type_array
*array_type
= container_of(
2805 type
, struct bt_ctf_field_type_array
, parent
);
2807 /* Cache the alignment */
2808 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2809 generic_field_type_freeze(type
);
2810 bt_ctf_field_type_freeze(array_type
->element_type
);
2814 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2816 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2817 type
, struct bt_ctf_field_type_sequence
, parent
);
2819 /* Cache the alignment */
2820 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2821 generic_field_type_freeze(type
);
2822 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2826 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2828 const char *encoding_string
;
2831 case BT_CTF_STRING_ENCODING_NONE
:
2832 encoding_string
= "none";
2834 case BT_CTF_STRING_ENCODING_ASCII
:
2835 encoding_string
= "ASCII";
2837 case BT_CTF_STRING_ENCODING_UTF8
:
2838 encoding_string
= "UTF8";
2841 encoding_string
= "unknown";
2845 return encoding_string
;
2849 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2851 const char *base_string
;
2854 case BT_CTF_INTEGER_BASE_DECIMAL
:
2855 base_string
= "decimal";
2857 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2858 base_string
= "hexadecimal";
2860 case BT_CTF_INTEGER_BASE_OCTAL
:
2861 base_string
= "octal";
2863 case BT_CTF_INTEGER_BASE_BINARY
:
2864 base_string
= "binary";
2867 base_string
= "unknown";
2875 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2876 struct metadata_context
*context
)
2878 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2879 struct bt_ctf_field_type_integer
, parent
);
2882 g_string_append_printf(context
->string
,
2883 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2884 integer
->declaration
.len
, type
->declaration
->alignment
,
2885 (integer
->declaration
.signedness
? "true" : "false"),
2886 get_encoding_string((enum bt_ctf_string_encoding
) integer
->declaration
.encoding
),
2887 get_integer_base_string(integer
->declaration
.base
),
2888 get_byte_order_string(integer
->declaration
.byte_order
));
2889 if (integer
->mapped_clock
) {
2890 const char *clock_name
= bt_ctf_clock_get_name(
2891 integer
->mapped_clock
);
2898 g_string_append_printf(context
->string
,
2899 "; map = clock.%s.value", clock_name
);
2902 g_string_append(context
->string
, "; }");
2908 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2909 struct metadata_context
*context
)
2913 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2914 struct bt_ctf_field_type_enumeration
, parent
);
2915 struct bt_ctf_field_type
*container_type
;
2916 int container_signed
;
2918 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2919 if (!container_type
) {
2924 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2925 if (container_signed
< 0) {
2926 ret
= container_signed
;
2927 goto error_put_container_type
;
2930 g_string_append(context
->string
, "enum : ");
2931 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2933 goto error_put_container_type
;
2936 g_string_append(context
->string
, " { ");
2937 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2938 struct enumeration_mapping
*mapping
=
2939 enumeration
->entries
->pdata
[entry
];
2941 if (container_signed
) {
2942 if (mapping
->range_start
._signed
==
2943 mapping
->range_end
._signed
) {
2944 g_string_append_printf(context
->string
,
2945 "\"%s\" = %" PRId64
,
2946 g_quark_to_string(mapping
->string
),
2947 mapping
->range_start
._signed
);
2949 g_string_append_printf(context
->string
,
2950 "\"%s\" = %" PRId64
" ... %" PRId64
,
2951 g_quark_to_string(mapping
->string
),
2952 mapping
->range_start
._signed
,
2953 mapping
->range_end
._signed
);
2956 if (mapping
->range_start
._unsigned
==
2957 mapping
->range_end
._unsigned
) {
2958 g_string_append_printf(context
->string
,
2959 "\"%s\" = %" PRIu64
,
2960 g_quark_to_string(mapping
->string
),
2961 mapping
->range_start
._unsigned
);
2963 g_string_append_printf(context
->string
,
2964 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2965 g_quark_to_string(mapping
->string
),
2966 mapping
->range_start
._unsigned
,
2967 mapping
->range_end
._unsigned
);
2971 g_string_append(context
->string
,
2972 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2976 if (context
->field_name
->len
) {
2977 g_string_append_printf(context
->string
, " %s",
2978 context
->field_name
->str
);
2979 g_string_assign(context
->field_name
, "");
2981 error_put_container_type
:
2982 bt_put(container_type
);
2988 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2989 struct metadata_context
*context
)
2991 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2992 type
, struct bt_ctf_field_type_floating_point
, parent
);
2994 g_string_append_printf(context
->string
,
2995 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2996 floating_point
->declaration
.exp
->len
,
2997 floating_point
->declaration
.mantissa
->len
+ 1,
2998 get_byte_order_string(floating_point
->declaration
.byte_order
),
2999 type
->declaration
->alignment
);
3004 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3005 struct metadata_context
*context
)
3008 unsigned int indent
;
3010 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3011 struct bt_ctf_field_type_structure
, parent
);
3012 GString
*structure_field_name
= context
->field_name
;
3014 context
->field_name
= g_string_new("");
3016 context
->current_indentation_level
++;
3017 g_string_append(context
->string
, "struct {\n");
3019 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3020 struct structure_field
*field
;
3022 for (indent
= 0; indent
< context
->current_indentation_level
;
3024 g_string_append_c(context
->string
, '\t');
3027 field
= structure
->fields
->pdata
[i
];
3028 g_string_assign(context
->field_name
,
3029 g_quark_to_string(field
->name
));
3030 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3035 if (context
->field_name
->len
) {
3036 g_string_append_printf(context
->string
, " %s",
3037 context
->field_name
->str
);
3039 g_string_append(context
->string
, ";\n");
3042 context
->current_indentation_level
--;
3043 for (indent
= 0; indent
< context
->current_indentation_level
;
3045 g_string_append_c(context
->string
, '\t');
3048 g_string_append_printf(context
->string
, "} align(%zu)",
3049 type
->declaration
->alignment
);
3051 g_string_free(context
->field_name
, TRUE
);
3052 context
->field_name
= structure_field_name
;
3057 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3058 struct metadata_context
*context
)
3061 unsigned int indent
;
3063 struct bt_ctf_field_type_variant
*variant
= container_of(
3064 type
, struct bt_ctf_field_type_variant
, parent
);
3065 GString
*variant_field_name
= context
->field_name
;
3067 context
->field_name
= g_string_new("");
3068 if (variant
->tag_name
->len
> 0) {
3069 g_string_append_printf(context
->string
,
3070 "variant <%s> {\n", variant
->tag_name
->str
);
3072 g_string_append(context
->string
, "variant {\n");
3075 context
->current_indentation_level
++;
3076 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3077 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3079 g_string_assign(context
->field_name
,
3080 g_quark_to_string(field
->name
));
3081 for (indent
= 0; indent
< context
->current_indentation_level
;
3083 g_string_append_c(context
->string
, '\t');
3086 g_string_assign(context
->field_name
,
3087 g_quark_to_string(field
->name
));
3088 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3093 if (context
->field_name
->len
) {
3094 g_string_append_printf(context
->string
, " %s;",
3095 context
->field_name
->str
);
3098 g_string_append_c(context
->string
, '\n');
3101 context
->current_indentation_level
--;
3102 for (indent
= 0; indent
< context
->current_indentation_level
;
3104 g_string_append_c(context
->string
, '\t');
3107 g_string_append(context
->string
, "}");
3109 g_string_free(context
->field_name
, TRUE
);
3110 context
->field_name
= variant_field_name
;
3115 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3116 struct metadata_context
*context
)
3119 struct bt_ctf_field_type_array
*array
= container_of(type
,
3120 struct bt_ctf_field_type_array
, parent
);
3122 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3127 if (context
->field_name
->len
) {
3128 g_string_append_printf(context
->string
, " %s[%u]",
3129 context
->field_name
->str
, array
->length
);
3130 g_string_assign(context
->field_name
, "");
3132 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3139 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3140 struct metadata_context
*context
)
3143 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3144 type
, struct bt_ctf_field_type_sequence
, parent
);
3146 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3151 if (context
->field_name
->len
) {
3152 g_string_append_printf(context
->string
, " %s[%s]",
3153 context
->field_name
->str
,
3154 sequence
->length_field_name
->str
);
3155 g_string_assign(context
->field_name
, "");
3157 g_string_append_printf(context
->string
, "[%s]",
3158 sequence
->length_field_name
->str
);
3165 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3166 struct metadata_context
*context
)
3168 struct bt_ctf_field_type_string
*string
= container_of(
3169 type
, struct bt_ctf_field_type_string
, parent
);
3171 g_string_append_printf(context
->string
,
3172 "string { encoding = %s; }",
3173 get_encoding_string((enum bt_ctf_string_encoding
) string
->declaration
.encoding
));
3178 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3179 enum bt_ctf_byte_order ret
;
3181 switch (byte_order
) {
3182 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3184 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3186 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3188 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3190 case BT_CTF_BYTE_ORDER_NETWORK
:
3191 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3193 case BT_CTF_BYTE_ORDER_NATIVE
:
3194 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3197 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3205 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3206 int byte_order
, int set_native
)
3208 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3209 struct bt_ctf_field_type_integer
, parent
);
3212 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3214 * User byte order is native, so we can set
3215 * the real byte order.
3217 integer_type
->declaration
.byte_order
=
3221 integer_type
->user_byte_order
=
3222 get_ctf_ir_byte_order(byte_order
);
3223 integer_type
->declaration
.byte_order
= byte_order
;
3228 void bt_ctf_field_type_enumeration_set_byte_order(
3229 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3231 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3232 struct bt_ctf_field_type_enumeration
, parent
);
3234 /* Safe to assume that container is an integer */
3235 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3236 byte_order
, set_native
);
3240 void bt_ctf_field_type_floating_point_set_byte_order(
3241 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3243 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3244 container_of(type
, struct bt_ctf_field_type_floating_point
,
3248 if (floating_point_type
->user_byte_order
==
3249 BT_CTF_BYTE_ORDER_NATIVE
) {
3251 * User byte order is native, so we can set
3252 * the real byte order.
3254 floating_point_type
->declaration
.byte_order
=
3256 floating_point_type
->sign
.byte_order
=
3258 floating_point_type
->mantissa
.byte_order
=
3260 floating_point_type
->exp
.byte_order
=
3264 floating_point_type
->user_byte_order
=
3265 get_ctf_ir_byte_order(byte_order
);
3266 floating_point_type
->declaration
.byte_order
= byte_order
;
3267 floating_point_type
->sign
.byte_order
= byte_order
;
3268 floating_point_type
->mantissa
.byte_order
= byte_order
;
3269 floating_point_type
->exp
.byte_order
= byte_order
;
3274 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3275 int byte_order
, int set_native
)
3278 struct bt_ctf_field_type_structure
*structure_type
=
3279 container_of(type
, struct bt_ctf_field_type_structure
,
3282 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3283 struct structure_field
*field
= g_ptr_array_index(
3284 structure_type
->fields
, i
);
3285 struct bt_ctf_field_type
*field_type
= field
->type
;
3287 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3288 set_byte_order_funcs
[field_type
->declaration
->id
](
3289 field_type
, byte_order
, set_native
);
3295 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3296 int byte_order
, int set_native
)
3299 struct bt_ctf_field_type_variant
*variant_type
=
3300 container_of(type
, struct bt_ctf_field_type_variant
,
3303 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3304 struct structure_field
*field
= g_ptr_array_index(
3305 variant_type
->fields
, i
);
3306 struct bt_ctf_field_type
*field_type
= field
->type
;
3308 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3309 set_byte_order_funcs
[field_type
->declaration
->id
](
3310 field_type
, byte_order
, set_native
);
3316 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3317 int byte_order
, int set_native
)
3319 struct bt_ctf_field_type_array
*array_type
=
3320 container_of(type
, struct bt_ctf_field_type_array
,
3323 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3324 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3325 array_type
->element_type
, byte_order
, set_native
);
3330 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3331 int byte_order
, int set_native
)
3333 struct bt_ctf_field_type_sequence
*sequence_type
=
3334 container_of(type
, struct bt_ctf_field_type_sequence
,
3337 if (set_byte_order_funcs
[
3338 sequence_type
->element_type
->declaration
->id
]) {
3339 set_byte_order_funcs
[
3340 sequence_type
->element_type
->declaration
->id
](
3341 sequence_type
->element_type
, byte_order
, set_native
);
3346 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3347 struct bt_ctf_field_type
*type
)
3349 struct bt_ctf_field_type
*copy
;
3350 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3352 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3353 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3358 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3360 copy_integer
->declaration
= integer
->declaration
;
3361 if (integer
->mapped_clock
) {
3362 bt_get(integer
->mapped_clock
);
3363 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3366 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3373 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3374 struct bt_ctf_field_type
*type
)
3377 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3378 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3380 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3383 /* Copy the source enumeration's container */
3384 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3385 if (!copy_container
) {
3389 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3393 copy_enumeration
= container_of(copy
,
3394 struct bt_ctf_field_type_enumeration
, parent
);
3396 /* Copy all enumaration entries */
3397 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3398 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3399 enumeration
->entries
, i
);
3400 struct enumeration_mapping
* copy_mapping
= g_new0(
3401 struct enumeration_mapping
, 1);
3403 if (!copy_mapping
) {
3407 *copy_mapping
= *mapping
;
3408 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3411 copy_enumeration
->declaration
= enumeration
->declaration
;
3413 bt_put(copy_container
);
3416 bt_put(copy_container
);
3422 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3423 struct bt_ctf_field_type
*type
)
3425 struct bt_ctf_field_type
*copy
;
3426 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3428 floating_point
= container_of(type
,
3429 struct bt_ctf_field_type_floating_point
, parent
);
3430 copy
= bt_ctf_field_type_floating_point_create();
3435 copy_float
= container_of(copy
,
3436 struct bt_ctf_field_type_floating_point
, parent
);
3437 copy_float
->declaration
= floating_point
->declaration
;
3438 copy_float
->sign
= floating_point
->sign
;
3439 copy_float
->mantissa
= floating_point
->mantissa
;
3440 copy_float
->exp
= floating_point
->exp
;
3441 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3442 copy_float
->declaration
.sign
= ©_float
->sign
;
3443 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3444 copy_float
->declaration
.exp
= ©_float
->exp
;
3450 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3451 struct bt_ctf_field_type
*type
)
3454 GHashTableIter iter
;
3455 gpointer key
, value
;
3456 struct bt_ctf_field_type
*copy
;
3457 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3459 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3461 copy
= bt_ctf_field_type_structure_create();
3466 copy_structure
= container_of(copy
,
3467 struct bt_ctf_field_type_structure
, parent
);
3469 /* Copy field_name_to_index */
3470 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3471 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3472 g_hash_table_insert(copy_structure
->field_name_to_index
,
3476 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3477 struct structure_field
*entry
, *copy_entry
;
3478 struct bt_ctf_field_type
*copy_field
;
3480 copy_entry
= g_new0(struct structure_field
, 1);
3485 entry
= g_ptr_array_index(structure
->fields
, i
);
3486 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3492 copy_entry
->name
= entry
->name
;
3493 copy_entry
->type
= copy_field
;
3494 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3497 copy_structure
->declaration
= structure
->declaration
;
3506 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3507 struct bt_ctf_field_type
*type
)
3510 GHashTableIter iter
;
3511 gpointer key
, value
;
3512 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3513 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3515 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3518 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3524 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3525 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3530 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3533 /* Copy field_name_to_index */
3534 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3535 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3536 g_hash_table_insert(copy_variant
->field_name_to_index
,
3540 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3541 struct structure_field
*entry
, *copy_entry
;
3542 struct bt_ctf_field_type
*copy_field
;
3544 copy_entry
= g_new0(struct structure_field
, 1);
3549 entry
= g_ptr_array_index(variant
->fields
, i
);
3550 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3556 copy_entry
->name
= entry
->name
;
3557 copy_entry
->type
= copy_field
;
3558 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3561 copy_variant
->declaration
= variant
->declaration
;
3562 if (variant
->tag_field_path
) {
3563 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3564 variant
->tag_field_path
);
3565 if (!copy_variant
->tag_field_path
) {
3579 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3580 struct bt_ctf_field_type
*type
)
3582 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3583 struct bt_ctf_field_type_array
*array
, *copy_array
;
3585 array
= container_of(type
, struct bt_ctf_field_type_array
,
3587 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3588 if (!copy_element
) {
3592 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3597 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3599 copy_array
->declaration
= array
->declaration
;
3601 bt_put(copy_element
);
3606 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3607 struct bt_ctf_field_type
*type
)
3609 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3610 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3612 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3614 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3615 if (!copy_element
) {
3619 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3620 sequence
->length_field_name
->len
?
3621 sequence
->length_field_name
->str
: NULL
);
3626 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3628 copy_sequence
->declaration
= sequence
->declaration
;
3629 if (sequence
->length_field_path
) {
3630 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3631 sequence
->length_field_path
);
3632 if (!copy_sequence
->length_field_path
) {
3637 bt_put(copy_element
);
3645 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3646 struct bt_ctf_field_type
*type
)
3648 struct bt_ctf_field_type
*copy
;
3649 struct bt_ctf_field_type_string
*string
, *copy_string
;
3651 copy
= bt_ctf_field_type_string_create();
3656 string
= container_of(type
, struct bt_ctf_field_type_string
,
3658 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3660 copy_string
->declaration
= string
->declaration
;
3666 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3667 struct bt_ctf_field_type
*type_b
)
3670 struct bt_ctf_field_type_integer
*integer_a
;
3671 struct bt_ctf_field_type_integer
*integer_b
;
3672 struct declaration_integer
*decl_a
;
3673 struct declaration_integer
*decl_b
;
3675 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3677 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3679 decl_a
= &integer_a
->declaration
;
3680 decl_b
= &integer_b
->declaration
;
3683 if (decl_a
->len
!= decl_b
->len
) {
3688 * Compare user byte orders only, not the cached,
3691 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3696 if (decl_a
->signedness
!= decl_b
->signedness
) {
3701 if (decl_a
->base
!= decl_b
->base
) {
3706 if (decl_a
->encoding
!= decl_b
->encoding
) {
3711 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3723 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3724 struct bt_ctf_field_type
*type_b
)
3727 struct bt_ctf_field_type_floating_point
*float_a
;
3728 struct bt_ctf_field_type_floating_point
*float_b
;
3730 float_a
= container_of(type_a
,
3731 struct bt_ctf_field_type_floating_point
, parent
);
3732 float_b
= container_of(type_b
,
3733 struct bt_ctf_field_type_floating_point
, parent
);
3736 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3740 /* Exponent length */
3741 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3745 /* Mantissa length */
3746 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3751 * Compare user byte orders only, not the cached,
3754 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3766 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3767 struct enumeration_mapping
*mapping_b
)
3772 if (mapping_a
->string
!= mapping_b
->string
) {
3777 if (mapping_a
->range_start
._unsigned
!=
3778 mapping_b
->range_start
._unsigned
) {
3783 if (mapping_a
->range_end
._unsigned
!=
3784 mapping_b
->range_end
._unsigned
) {
3796 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3797 struct bt_ctf_field_type
*type_b
)
3801 struct bt_ctf_field_type_enumeration
*enum_a
;
3802 struct bt_ctf_field_type_enumeration
*enum_b
;
3804 enum_a
= container_of(type_a
,
3805 struct bt_ctf_field_type_enumeration
, parent
);
3806 enum_b
= container_of(type_b
,
3807 struct bt_ctf_field_type_enumeration
, parent
);
3809 /* Container field type */
3810 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3818 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3822 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3823 struct enumeration_mapping
*mapping_a
=
3824 g_ptr_array_index(enum_a
->entries
, i
);
3825 struct enumeration_mapping
*mapping_b
=
3826 g_ptr_array_index(enum_b
->entries
, i
);
3828 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3841 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3842 struct bt_ctf_field_type
*type_b
)
3845 struct bt_ctf_field_type_string
*string_a
;
3846 struct bt_ctf_field_type_string
*string_b
;
3848 string_a
= container_of(type_a
,
3849 struct bt_ctf_field_type_string
, parent
);
3850 string_b
= container_of(type_b
,
3851 struct bt_ctf_field_type_string
, parent
);
3854 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3866 int compare_structure_fields(struct structure_field
*field_a
,
3867 struct structure_field
*field_b
)
3872 if (field_a
->name
!= field_b
->name
) {
3877 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3884 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3885 struct bt_ctf_field_type
*type_b
)
3889 struct bt_ctf_field_type_structure
*struct_a
;
3890 struct bt_ctf_field_type_structure
*struct_b
;
3892 struct_a
= container_of(type_a
,
3893 struct bt_ctf_field_type_structure
, parent
);
3894 struct_b
= container_of(type_b
,
3895 struct bt_ctf_field_type_structure
, parent
);
3898 if (bt_ctf_field_type_get_alignment(type_a
) !=
3899 bt_ctf_field_type_get_alignment(type_b
)) {
3904 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3908 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3909 struct structure_field
*field_a
=
3910 g_ptr_array_index(struct_a
->fields
, i
);
3911 struct structure_field
*field_b
=
3912 g_ptr_array_index(struct_b
->fields
, i
);
3914 ret
= compare_structure_fields(field_a
, field_b
);
3928 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3929 struct bt_ctf_field_type
*type_b
)
3933 struct bt_ctf_field_type_variant
*variant_a
;
3934 struct bt_ctf_field_type_variant
*variant_b
;
3936 variant_a
= container_of(type_a
,
3937 struct bt_ctf_field_type_variant
, parent
);
3938 variant_b
= container_of(type_b
,
3939 struct bt_ctf_field_type_variant
, parent
);
3942 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3947 ret
= bt_ctf_field_type_compare(
3948 (struct bt_ctf_field_type
*) variant_a
->tag
,
3949 (struct bt_ctf_field_type
*) variant_b
->tag
);
3957 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3961 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3962 struct structure_field
*field_a
=
3963 g_ptr_array_index(variant_a
->fields
, i
);
3964 struct structure_field
*field_b
=
3965 g_ptr_array_index(variant_b
->fields
, i
);
3967 ret
= compare_structure_fields(field_a
, field_b
);
3981 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3982 struct bt_ctf_field_type
*type_b
)
3985 struct bt_ctf_field_type_array
*array_a
;
3986 struct bt_ctf_field_type_array
*array_b
;
3988 array_a
= container_of(type_a
,
3989 struct bt_ctf_field_type_array
, parent
);
3990 array_b
= container_of(type_b
,
3991 struct bt_ctf_field_type_array
, parent
);
3994 if (array_a
->length
!= array_b
->length
) {
3999 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4000 array_b
->element_type
);
4007 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4008 struct bt_ctf_field_type
*type_b
)
4011 struct bt_ctf_field_type_sequence
*sequence_a
;
4012 struct bt_ctf_field_type_sequence
*sequence_b
;
4014 sequence_a
= container_of(type_a
,
4015 struct bt_ctf_field_type_sequence
, parent
);
4016 sequence_b
= container_of(type_b
,
4017 struct bt_ctf_field_type_sequence
, parent
);
4020 if (strcmp(sequence_a
->length_field_name
->str
,
4021 sequence_b
->length_field_name
->str
)) {
4026 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4027 sequence_b
->element_type
);
4034 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4035 struct bt_ctf_field_type
*type_b
)
4039 if (type_a
== type_b
) {
4040 /* Same reference: equal (even if both are NULL) */
4045 if (!type_a
|| !type_b
) {
4050 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4051 /* Different type IDs */
4055 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
4056 /* Both have unknown type IDs */
4060 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4067 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4069 int field_count
= -1;
4070 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4073 case CTF_TYPE_STRUCT
:
4075 bt_ctf_field_type_structure_get_field_count(field_type
);
4077 case CTF_TYPE_VARIANT
:
4079 bt_ctf_field_type_variant_get_field_count(field_type
);
4081 case CTF_TYPE_ARRAY
:
4082 case CTF_TYPE_SEQUENCE
:
4084 * Array and sequence types always contain a single member
4085 * (the element type).
4097 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4098 struct bt_ctf_field_type
*field_type
, int index
)
4100 struct bt_ctf_field_type
*field
= NULL
;
4101 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4104 case CTF_TYPE_STRUCT
:
4105 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4108 case CTF_TYPE_VARIANT
:
4110 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4118 case CTF_TYPE_ARRAY
:
4119 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4121 case CTF_TYPE_SEQUENCE
:
4122 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4132 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4135 int field_index
= -1;
4136 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4139 case CTF_TYPE_STRUCT
:
4140 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4143 case CTF_TYPE_VARIANT
:
4144 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4155 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4156 struct bt_ctf_field_type
*type
)
4158 struct bt_ctf_field_path
*field_path
= NULL
;
4159 struct bt_ctf_field_type_variant
*variant
;
4161 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4165 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4167 field_path
= bt_get(variant
->tag_field_path
);
4173 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4174 struct bt_ctf_field_type
*type
)
4176 struct bt_ctf_field_path
*field_path
= NULL
;
4177 struct bt_ctf_field_type_sequence
*sequence
;
4179 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4183 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4185 field_path
= bt_get(sequence
->length_field_path
);