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
= BT_CTF_STRING_ENCODING_NONE
;
689 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
690 return &integer
->parent
;
694 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
697 struct bt_ctf_field_type_integer
*integer
;
699 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
704 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
705 ret
= (int) integer
->declaration
.len
;
710 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
713 struct bt_ctf_field_type_integer
*integer
;
715 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
720 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
721 ret
= integer
->declaration
.signedness
;
726 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
730 struct bt_ctf_field_type_integer
*integer
;
732 if (!type
|| type
->frozen
||
733 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
738 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
739 integer
->declaration
.signedness
= !!is_signed
;
745 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
746 struct bt_ctf_field_type
*type
)
748 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
749 struct bt_ctf_field_type_integer
*integer
;
751 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
755 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
756 ret
= integer
->declaration
.base
;
761 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
762 enum bt_ctf_integer_base base
)
766 if (!type
|| type
->frozen
||
767 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
773 case BT_CTF_INTEGER_BASE_BINARY
:
774 case BT_CTF_INTEGER_BASE_OCTAL
:
775 case BT_CTF_INTEGER_BASE_DECIMAL
:
776 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
778 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
779 struct bt_ctf_field_type_integer
, parent
);
780 integer
->declaration
.base
= base
;
791 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
792 struct bt_ctf_field_type
*type
)
794 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
795 struct bt_ctf_field_type_integer
*integer
;
797 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
801 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
802 ret
= integer
->declaration
.encoding
;
807 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
808 enum bt_ctf_string_encoding encoding
)
811 struct bt_ctf_field_type_integer
*integer
;
813 if (!type
|| type
->frozen
||
814 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
815 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
816 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
821 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
822 integer
->declaration
.encoding
= encoding
;
828 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
829 struct bt_ctf_field_type
*type
)
831 struct bt_ctf_field_type_integer
*integer
;
832 struct bt_ctf_clock
*clock
= NULL
;
838 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
839 clock
= integer
->mapped_clock
;
846 int bt_ctf_field_type_integer_set_mapped_clock(
847 struct bt_ctf_field_type
*type
,
848 struct bt_ctf_clock
*clock
)
850 struct bt_ctf_field_type_integer
*integer
;
853 if (!type
|| type
->frozen
) {
858 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
859 bt_put(integer
->mapped_clock
);
861 integer
->mapped_clock
= clock
;
866 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
867 struct bt_ctf_field_type
*integer_container_type
)
869 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
871 if (!integer_container_type
) {
875 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
879 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
884 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
885 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
886 bt_get(integer_container_type
);
887 enumeration
->container
= integer_container_type
;
888 enumeration
->entries
= g_ptr_array_new_with_free_func(
889 (GDestroyNotify
)destroy_enumeration_mapping
);
890 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
891 return &enumeration
->parent
;
898 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
899 struct bt_ctf_field_type
*type
)
901 struct bt_ctf_field_type
*container_type
= NULL
;
902 struct bt_ctf_field_type_enumeration
*enumeration_type
;
908 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
912 enumeration_type
= container_of(type
,
913 struct bt_ctf_field_type_enumeration
, parent
);
914 container_type
= enumeration_type
->container
;
915 bt_get(container_type
);
917 return container_type
;
920 int bt_ctf_field_type_enumeration_add_mapping(
921 struct bt_ctf_field_type
*type
, const char *string
,
922 int64_t range_start
, int64_t range_end
)
926 struct enumeration_mapping
*mapping
;
927 struct bt_ctf_field_type_enumeration
*enumeration
;
928 struct range_overlap_query query
;
929 char *escaped_string
;
931 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
933 (range_end
< range_start
)) {
938 if (!string
|| strlen(string
) == 0) {
943 escaped_string
= g_strescape(string
, NULL
);
944 if (!escaped_string
) {
949 mapping_name
= g_quark_from_string(escaped_string
);
950 query
= (struct range_overlap_query
) {
951 .range_start
._signed
= range_start
,
952 .range_end
._signed
= range_end
,
953 .mapping_name
= mapping_name
,
955 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
958 /* Check that the range does not overlap with one already present */
959 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
960 if (query
.overlaps
) {
965 mapping
= g_new(struct enumeration_mapping
, 1);
971 *mapping
= (struct enumeration_mapping
) {
972 .range_start
._signed
= range_start
,
973 .range_end
._signed
= range_end
, .string
= mapping_name
};
974 g_ptr_array_add(enumeration
->entries
, mapping
);
975 g_ptr_array_sort(enumeration
->entries
,
976 (GCompareFunc
)compare_enumeration_mappings_signed
);
978 free(escaped_string
);
984 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
985 struct bt_ctf_field_type
*type
, const char *string
,
986 uint64_t range_start
, uint64_t range_end
)
990 struct enumeration_mapping
*mapping
;
991 struct bt_ctf_field_type_enumeration
*enumeration
;
992 struct range_overlap_query query
;
993 char *escaped_string
;
995 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
997 (range_end
< range_start
)) {
1002 if (!string
|| strlen(string
) == 0) {
1007 escaped_string
= g_strescape(string
, NULL
);
1008 if (!escaped_string
) {
1013 mapping_name
= g_quark_from_string(escaped_string
);
1014 query
= (struct range_overlap_query
) {
1015 .range_start
._unsigned
= range_start
,
1016 .range_end
._unsigned
= range_end
,
1017 .mapping_name
= mapping_name
,
1019 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1022 /* Check that the range does not overlap with one already present */
1023 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
1025 if (query
.overlaps
) {
1030 mapping
= g_new(struct enumeration_mapping
, 1);
1036 *mapping
= (struct enumeration_mapping
) {
1037 .range_start
._unsigned
= range_start
,
1038 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1039 g_ptr_array_add(enumeration
->entries
, mapping
);
1040 g_ptr_array_sort(enumeration
->entries
,
1041 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1043 free(escaped_string
);
1049 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1050 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1053 const char *name
= NULL
;
1054 struct range_overlap_query query
=
1055 (struct range_overlap_query
) {
1056 .range_start
._unsigned
= value
,
1057 .range_end
._unsigned
= value
,
1060 g_ptr_array_foreach(enumeration_type
->entries
,
1061 check_ranges_overlap_unsigned
,
1063 if (!query
.overlaps
) {
1067 name
= g_quark_to_string(query
.mapping_name
);
1072 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1073 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1076 const char *name
= NULL
;
1077 struct range_overlap_query query
=
1078 (struct range_overlap_query
) {
1079 .range_start
._signed
= value
,
1080 .range_end
._signed
= value
,
1083 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1085 if (!query
.overlaps
) {
1089 name
= g_quark_to_string(query
.mapping_name
);
1094 int bt_ctf_field_type_enumeration_get_mapping_count(
1095 struct bt_ctf_field_type
*type
)
1098 struct bt_ctf_field_type_enumeration
*enumeration
;
1100 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1105 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1107 ret
= (int) enumeration
->entries
->len
;
1113 struct enumeration_mapping
*get_enumeration_mapping(
1114 struct bt_ctf_field_type
*type
, int index
)
1116 struct enumeration_mapping
*mapping
= NULL
;
1117 struct bt_ctf_field_type_enumeration
*enumeration
;
1119 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1121 if (index
>= enumeration
->entries
->len
) {
1125 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1131 int bt_ctf_field_type_enumeration_get_mapping(
1132 struct bt_ctf_field_type
*type
, int index
,
1133 const char **string
, int64_t *range_start
, int64_t *range_end
)
1135 struct enumeration_mapping
*mapping
;
1138 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1139 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1144 mapping
= get_enumeration_mapping(type
, index
);
1150 *string
= g_quark_to_string(mapping
->string
);
1151 *range_start
= mapping
->range_start
._signed
;
1152 *range_end
= mapping
->range_end
._signed
;
1158 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1159 struct bt_ctf_field_type
*type
, int index
,
1160 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1162 struct enumeration_mapping
*mapping
;
1165 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1166 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1171 mapping
= get_enumeration_mapping(type
, index
);
1177 *string
= g_quark_to_string(mapping
->string
);
1178 *range_start
= mapping
->range_start
._unsigned
;
1179 *range_end
= mapping
->range_end
._unsigned
;
1185 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1186 struct bt_ctf_field_type
*type
, const char *name
)
1189 struct bt_ctf_field_type_enumeration
*enumeration
;
1192 if (!type
|| !name
||
1193 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1198 name_quark
= g_quark_try_string(name
);
1204 enumeration
= container_of(type
,
1205 struct bt_ctf_field_type_enumeration
, parent
);
1206 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1207 struct enumeration_mapping
*mapping
=
1208 get_enumeration_mapping(type
, i
);
1210 if (mapping
->string
== name_quark
) {
1222 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1223 struct bt_ctf_field_type
*type
, int64_t value
)
1225 struct bt_ctf_field_type_enumeration
*enumeration
;
1228 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1233 enumeration
= container_of(type
,
1234 struct bt_ctf_field_type_enumeration
, parent
);
1235 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1236 struct enumeration_mapping
*mapping
=
1237 get_enumeration_mapping(type
, i
);
1239 if (value
>= mapping
->range_start
._signed
&&
1240 value
<= mapping
->range_end
._signed
) {
1252 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1253 struct bt_ctf_field_type
*type
, uint64_t value
)
1255 struct bt_ctf_field_type_enumeration
*enumeration
;
1258 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1263 enumeration
= container_of(type
,
1264 struct bt_ctf_field_type_enumeration
, parent
);
1265 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1266 struct enumeration_mapping
*mapping
=
1267 get_enumeration_mapping(type
, i
);
1269 if (value
>= mapping
->range_start
._unsigned
&&
1270 value
<= mapping
->range_end
._unsigned
) {
1281 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1283 struct bt_ctf_field_type_floating_point
*floating_point
=
1284 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1286 if (!floating_point
) {
1290 floating_point
->declaration
.sign
= &floating_point
->sign
;
1291 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1292 floating_point
->declaration
.exp
= &floating_point
->exp
;
1293 floating_point
->sign
.len
= 1;
1294 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1295 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1296 floating_point
->declaration
.exp
->len
=
1297 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1298 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1299 floating_point
->sign
.p
.alignment
= 1;
1300 floating_point
->mantissa
.p
.alignment
= 1;
1301 floating_point
->exp
.p
.alignment
= 1;
1303 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1305 return floating_point
? &floating_point
->parent
: NULL
;
1309 int bt_ctf_field_type_floating_point_get_exponent_digits(
1310 struct bt_ctf_field_type
*type
)
1313 struct bt_ctf_field_type_floating_point
*floating_point
;
1315 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1320 floating_point
= container_of(type
,
1321 struct bt_ctf_field_type_floating_point
, parent
);
1322 ret
= (int) floating_point
->declaration
.exp
->len
;
1327 int bt_ctf_field_type_floating_point_set_exponent_digits(
1328 struct bt_ctf_field_type
*type
,
1329 unsigned int exponent_digits
)
1332 struct bt_ctf_field_type_floating_point
*floating_point
;
1334 if (!type
|| type
->frozen
||
1335 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1340 floating_point
= container_of(type
,
1341 struct bt_ctf_field_type_floating_point
, parent
);
1342 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1343 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1345 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1350 floating_point
->declaration
.exp
->len
= exponent_digits
;
1356 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1357 struct bt_ctf_field_type
*type
)
1360 struct bt_ctf_field_type_floating_point
*floating_point
;
1362 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1367 floating_point
= container_of(type
,
1368 struct bt_ctf_field_type_floating_point
, parent
);
1369 ret
= (int) floating_point
->mantissa
.len
+ 1;
1374 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1375 struct bt_ctf_field_type
*type
,
1376 unsigned int mantissa_digits
)
1379 struct bt_ctf_field_type_floating_point
*floating_point
;
1381 if (!type
|| type
->frozen
||
1382 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1387 floating_point
= container_of(type
,
1388 struct bt_ctf_field_type_floating_point
, parent
);
1390 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1391 (mantissa_digits
!= DBL_MANT_DIG
) &&
1392 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1397 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1402 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1404 struct bt_ctf_field_type_structure
*structure
=
1405 g_new0(struct bt_ctf_field_type_structure
, 1);
1411 structure
->parent
.declaration
= &structure
->declaration
.p
;
1412 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1413 structure
->fields
= g_ptr_array_new_with_free_func(
1414 (GDestroyNotify
)destroy_structure_field
);
1415 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1416 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1417 return &structure
->parent
;
1422 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1423 struct bt_ctf_field_type
*field_type
,
1424 const char *field_name
)
1427 struct bt_ctf_field_type_structure
*structure
;
1429 if (!type
|| !field_type
|| type
->frozen
||
1430 bt_ctf_validate_identifier(field_name
) ||
1431 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1436 structure
= container_of(type
,
1437 struct bt_ctf_field_type_structure
, parent
);
1438 if (add_structure_field(structure
->fields
,
1439 structure
->field_name_to_index
, field_type
, field_name
)) {
1448 int bt_ctf_field_type_structure_get_field_count(
1449 struct bt_ctf_field_type
*type
)
1452 struct bt_ctf_field_type_structure
*structure
;
1454 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1459 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1461 ret
= (int) structure
->fields
->len
;
1466 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1467 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1470 struct bt_ctf_field_type_structure
*structure
;
1471 struct structure_field
*field
;
1474 if (!type
|| index
< 0 ||
1475 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1480 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1482 if (index
>= structure
->fields
->len
) {
1487 field
= g_ptr_array_index(structure
->fields
, index
);
1489 *field_type
= field
->type
;
1490 bt_get(field
->type
);
1493 *field_name
= g_quark_to_string(field
->name
);
1500 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1501 struct bt_ctf_field_type
*type
,
1506 struct structure_field
*field
;
1507 struct bt_ctf_field_type_structure
*structure
;
1508 struct bt_ctf_field_type
*field_type
= NULL
;
1510 if (!type
|| !name
) {
1514 name_quark
= g_quark_try_string(name
);
1519 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1521 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1522 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1526 field
= structure
->fields
->pdata
[index
];
1527 field_type
= field
->type
;
1533 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1534 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1536 struct bt_ctf_field_type_variant
*variant
= NULL
;
1538 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1542 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1547 variant
->parent
.declaration
= &variant
->declaration
.p
;
1548 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1549 variant
->tag_name
= g_string_new(tag_name
);
1550 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1551 variant
->fields
= g_ptr_array_new_with_free_func(
1552 (GDestroyNotify
) destroy_structure_field
);
1555 variant
->tag
= container_of(enum_tag
,
1556 struct bt_ctf_field_type_enumeration
, parent
);
1559 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1560 /* A variant's alignment is undefined */
1561 variant
->parent
.declaration
->alignment
= 0;
1562 return &variant
->parent
;
1568 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1569 struct bt_ctf_field_type
*type
)
1571 struct bt_ctf_field_type_variant
*variant
;
1572 struct bt_ctf_field_type
*tag_type
= NULL
;
1574 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1578 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1579 if (!variant
->tag
) {
1583 tag_type
= &variant
->tag
->parent
;
1590 const char *bt_ctf_field_type_variant_get_tag_name(
1591 struct bt_ctf_field_type
*type
)
1593 struct bt_ctf_field_type_variant
*variant
;
1594 const char *tag_name
= NULL
;
1596 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1600 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1601 if (variant
->tag_name
->len
== 0) {
1605 tag_name
= variant
->tag_name
->str
;
1611 int bt_ctf_field_type_variant_set_tag_name(
1612 struct bt_ctf_field_type
*type
, const char *name
)
1615 struct bt_ctf_field_type_variant
*variant
;
1617 if (!type
|| type
->frozen
||
1618 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1619 bt_ctf_validate_identifier(name
)) {
1624 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1625 g_string_assign(variant
->tag_name
, name
);
1630 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1631 struct bt_ctf_field_type
*field_type
,
1632 const char *field_name
)
1636 struct bt_ctf_field_type_variant
*variant
;
1637 GQuark field_name_quark
= g_quark_from_string(field_name
);
1639 if (!type
|| !field_type
|| type
->frozen
||
1640 bt_ctf_validate_identifier(field_name
) ||
1641 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1646 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1648 /* The user has explicitly provided a tag; validate against it. */
1652 /* Make sure this name is present in the enum tag */
1653 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1654 struct enumeration_mapping
*mapping
=
1655 g_ptr_array_index(variant
->tag
->entries
, i
);
1657 if (mapping
->string
== field_name_quark
) {
1664 /* Validation failed */
1670 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1671 field_type
, field_name
)) {
1680 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1681 struct bt_ctf_field_type
*type
,
1682 const char *field_name
)
1686 struct structure_field
*field
;
1687 struct bt_ctf_field_type_variant
*variant
;
1688 struct bt_ctf_field_type
*field_type
= NULL
;
1690 if (!type
|| !field_name
) {
1694 name_quark
= g_quark_try_string(field_name
);
1699 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1700 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1701 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1705 field
= g_ptr_array_index(variant
->fields
, index
);
1706 field_type
= field
->type
;
1713 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1714 struct bt_ctf_field_type
*type
,
1715 struct bt_ctf_field
*tag
)
1717 const char *enum_value
;
1718 struct bt_ctf_field_type
*field_type
= NULL
;
1720 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1724 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1729 /* Already increments field_type's reference count */
1730 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1736 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1739 struct bt_ctf_field_type_variant
*variant
;
1741 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1746 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1748 ret
= (int) variant
->fields
->len
;
1755 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1756 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1759 struct bt_ctf_field_type_variant
*variant
;
1760 struct structure_field
*field
;
1763 if (!type
|| index
< 0 ||
1764 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1769 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1771 if (index
>= variant
->fields
->len
) {
1776 field
= g_ptr_array_index(variant
->fields
, index
);
1778 *field_type
= field
->type
;
1779 bt_get(field
->type
);
1782 *field_name
= g_quark_to_string(field
->name
);
1788 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1789 struct bt_ctf_field_type
*element_type
,
1790 unsigned int length
)
1792 struct bt_ctf_field_type_array
*array
= NULL
;
1794 if (!element_type
|| length
== 0) {
1798 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1803 array
->parent
.declaration
= &array
->declaration
.p
;
1804 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1806 bt_get(element_type
);
1807 array
->element_type
= element_type
;
1808 array
->length
= length
;
1809 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1810 return &array
->parent
;
1816 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1817 struct bt_ctf_field_type
*type
)
1819 struct bt_ctf_field_type
*ret
= NULL
;
1820 struct bt_ctf_field_type_array
*array
;
1822 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1826 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1827 ret
= array
->element_type
;
1834 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1835 struct bt_ctf_field_type
*element_type
)
1838 struct bt_ctf_field_type_array
*array
;
1840 if (!type
|| !element_type
||
1841 (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1846 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1848 if (array
->element_type
) {
1849 BT_PUT(array
->element_type
);
1852 array
->element_type
= element_type
;
1853 bt_get(array
->element_type
);
1860 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1863 struct bt_ctf_field_type_array
*array
;
1865 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1870 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1871 ret
= (int64_t) array
->length
;
1876 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1877 struct bt_ctf_field_type
*element_type
,
1878 const char *length_field_name
)
1880 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1882 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1886 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1891 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1892 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1893 bt_get(element_type
);
1894 sequence
->element_type
= element_type
;
1895 sequence
->length_field_name
= g_string_new(length_field_name
);
1896 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1897 return &sequence
->parent
;
1903 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1904 struct bt_ctf_field_type
*type
)
1906 struct bt_ctf_field_type
*ret
= NULL
;
1907 struct bt_ctf_field_type_sequence
*sequence
;
1909 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1913 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1915 ret
= sequence
->element_type
;
1922 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1923 struct bt_ctf_field_type
*element_type
)
1926 struct bt_ctf_field_type_sequence
*sequence
;
1928 if (!type
|| !element_type
||
1929 (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1934 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1936 if (sequence
->element_type
) {
1937 BT_PUT(sequence
->element_type
);
1940 sequence
->element_type
= element_type
;
1941 bt_get(sequence
->element_type
);
1948 const char *bt_ctf_field_type_sequence_get_length_field_name(
1949 struct bt_ctf_field_type
*type
)
1951 const char *ret
= NULL
;
1952 struct bt_ctf_field_type_sequence
*sequence
;
1954 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1958 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1960 ret
= sequence
->length_field_name
->str
;
1965 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1967 struct bt_ctf_field_type_string
*string
=
1968 g_new0(struct bt_ctf_field_type_string
, 1);
1974 string
->parent
.declaration
= &string
->declaration
.p
;
1975 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1976 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1977 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1978 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1979 return &string
->parent
;
1983 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1984 struct bt_ctf_field_type
*type
)
1986 struct bt_ctf_field_type_string
*string
;
1987 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1989 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1993 string
= container_of(type
, struct bt_ctf_field_type_string
,
1995 ret
= string
->declaration
.encoding
;
2000 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2001 enum bt_ctf_string_encoding encoding
)
2004 struct bt_ctf_field_type_string
*string
;
2006 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
2007 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2008 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2013 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2014 string
->declaration
.encoding
= encoding
;
2020 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2023 enum ctf_type_id type_id
;
2031 ret
= (int) type
->declaration
->alignment
;
2035 type_id
= bt_ctf_field_type_get_type_id(type
);
2037 case CTF_TYPE_SEQUENCE
:
2039 struct bt_ctf_field_type
*element
=
2040 bt_ctf_field_type_sequence_get_element_type(type
);
2047 ret
= bt_ctf_field_type_get_alignment(element
);
2051 case CTF_TYPE_ARRAY
:
2053 struct bt_ctf_field_type
*element
=
2054 bt_ctf_field_type_array_get_element_type(type
);
2061 ret
= bt_ctf_field_type_get_alignment(element
);
2065 case CTF_TYPE_STRUCT
:
2067 int i
, element_count
;
2069 element_count
= bt_ctf_field_type_structure_get_field_count(
2071 if (element_count
< 0) {
2072 ret
= element_count
;
2076 for (i
= 0; i
< element_count
; i
++) {
2077 struct bt_ctf_field_type
*field
;
2078 int field_alignment
;
2080 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2087 field_alignment
= bt_ctf_field_type_get_alignment(
2090 if (field_alignment
< 0) {
2091 ret
= field_alignment
;
2095 type
->declaration
->alignment
= MAX(field_alignment
,
2096 type
->declaration
->alignment
);
2098 ret
= (int) type
->declaration
->alignment
;
2101 case CTF_TYPE_UNKNOWN
:
2105 ret
= (int) type
->declaration
->alignment
;
2113 int is_power_of_two(unsigned int value
)
2115 return ((value
& (value
- 1)) == 0) && value
> 0;
2118 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2119 unsigned int alignment
)
2122 enum ctf_type_id type_id
;
2124 /* Alignment must be a power of two */
2125 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2130 type_id
= bt_ctf_field_type_get_type_id(type
);
2131 if (type_id
== CTF_TYPE_UNKNOWN
) {
2136 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
2137 alignment
!= CHAR_BIT
) {
2142 if (type_id
== CTF_TYPE_VARIANT
||
2143 type_id
== CTF_TYPE_SEQUENCE
||
2144 type_id
== CTF_TYPE_ARRAY
) {
2145 /* Setting an alignment on these types makes no sense */
2150 type
->declaration
->alignment
= alignment
;
2157 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2158 struct bt_ctf_field_type
*type
)
2160 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2166 switch (type
->declaration
->id
) {
2167 case CTF_TYPE_INTEGER
:
2169 struct bt_ctf_field_type_integer
*integer
= container_of(
2170 type
, struct bt_ctf_field_type_integer
, parent
);
2171 ret
= integer
->user_byte_order
;
2174 case CTF_TYPE_FLOAT
:
2176 struct bt_ctf_field_type_floating_point
*floating_point
=
2178 struct bt_ctf_field_type_floating_point
,
2180 ret
= floating_point
->user_byte_order
;
2187 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2188 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2189 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2190 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2196 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2197 enum bt_ctf_byte_order byte_order
)
2200 int internal_byte_order
;
2201 enum ctf_type_id type_id
;
2203 if (!type
|| type
->frozen
) {
2208 switch (byte_order
) {
2209 case BT_CTF_BYTE_ORDER_NATIVE
:
2210 /* Leave unset. Will be initialized by parent. */
2211 internal_byte_order
= 0;
2213 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2214 internal_byte_order
= LITTLE_ENDIAN
;
2216 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2217 case BT_CTF_BYTE_ORDER_NETWORK
:
2218 internal_byte_order
= BIG_ENDIAN
;
2225 type_id
= type
->declaration
->id
;
2226 if (set_byte_order_funcs
[type_id
]) {
2227 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2234 enum ctf_type_id
bt_ctf_field_type_get_type_id(
2235 struct bt_ctf_field_type
*type
)
2238 return CTF_TYPE_UNKNOWN
;
2241 return type
->declaration
->id
;
2245 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2247 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
2251 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2253 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
2257 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2259 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
2263 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2265 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
2269 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2271 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
2275 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2277 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
2281 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2283 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
2287 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2289 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
2292 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2297 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2303 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2313 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2314 struct bt_ctf_field_type_variant
*variant
,
2317 struct bt_ctf_field_type
*type
= NULL
;
2318 GQuark field_name_quark
;
2320 struct structure_field
*field_entry
;
2321 struct range_overlap_query query
= {
2322 .range_start
._signed
= tag_value
,
2323 .range_end
._signed
= tag_value
,
2324 .mapping_name
= 0, .overlaps
= 0};
2326 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2328 if (!query
.overlaps
) {
2332 field_name_quark
= query
.mapping_name
;
2333 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2334 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2338 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2339 type
= field_entry
->type
;
2345 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2346 struct bt_ctf_field_type_variant
*variant
,
2349 struct bt_ctf_field_type
*type
= NULL
;
2350 GQuark field_name_quark
;
2352 struct structure_field
*field_entry
;
2353 struct range_overlap_query query
= {
2354 .range_start
._unsigned
= tag_value
,
2355 .range_end
._unsigned
= tag_value
,
2356 .mapping_name
= 0, .overlaps
= 0};
2358 g_ptr_array_foreach(variant
->tag
->entries
,
2359 check_ranges_overlap_unsigned
,
2361 if (!query
.overlaps
) {
2365 field_name_quark
= query
.mapping_name
;
2366 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2367 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2371 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2372 type
= field_entry
->type
;
2378 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2379 struct metadata_context
*context
)
2383 if (!type
|| !context
) {
2388 /* Make sure field type is valid before serializing it */
2389 ret
= bt_ctf_field_type_validate(type
);
2395 ret
= type
->serialize(type
, context
);
2401 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2408 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2409 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2410 set_byte_order_funcs
[type
->declaration
->id
](type
,
2416 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2418 struct bt_ctf_field_type
*copy
= NULL
;
2424 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2430 int bt_ctf_field_type_structure_get_field_name_index(
2431 struct bt_ctf_field_type
*type
, const char *name
)
2436 struct bt_ctf_field_type_structure
*structure
;
2438 if (!type
|| !name
||
2439 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2444 name_quark
= g_quark_try_string(name
);
2450 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2452 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2453 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2463 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2464 struct bt_ctf_field_type
*field
, int index
)
2467 struct bt_ctf_field_type_structure
*structure
;
2469 if (!type
|| !field
||
2470 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2475 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2477 if (index
< 0 || index
>= structure
->fields
->len
) {
2483 bt_put(((struct structure_field
*)
2484 g_ptr_array_index(structure
->fields
, index
))->type
);
2485 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2492 int bt_ctf_field_type_variant_get_field_name_index(
2493 struct bt_ctf_field_type
*type
, const char *name
)
2498 struct bt_ctf_field_type_variant
*variant
;
2500 if (!type
|| !name
||
2501 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2506 name_quark
= g_quark_try_string(name
);
2512 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2514 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2515 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2525 int bt_ctf_field_type_sequence_set_length_field_path(
2526 struct bt_ctf_field_type
*type
,
2527 struct bt_ctf_field_path
*path
)
2530 struct bt_ctf_field_type_sequence
*sequence
;
2532 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2533 CTF_TYPE_SEQUENCE
) {
2538 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2541 BT_MOVE(sequence
->length_field_path
, path
);
2547 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2548 struct bt_ctf_field_path
*path
)
2551 struct bt_ctf_field_type_variant
*variant
;
2553 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2559 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2562 BT_MOVE(variant
->tag_field_path
, path
);
2568 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2569 struct bt_ctf_field_type
*tag
)
2572 struct bt_ctf_field_type_variant
*variant
;
2574 if (!type
|| !tag
||
2575 bt_ctf_field_type_get_type_id(tag
) !=
2581 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2585 bt_put(&variant
->tag
->parent
);
2587 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2594 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2595 struct bt_ctf_field_type
*field
, int index
)
2598 struct bt_ctf_field_type_variant
*variant
;
2600 if (!type
|| !field
||
2601 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2606 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2608 if (index
< 0 || index
>= variant
->fields
->len
) {
2614 bt_put(((struct structure_field
*)
2615 g_ptr_array_index(variant
->fields
, index
))->type
);
2616 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2623 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2625 struct bt_ctf_field_type_integer
*integer
=
2626 (struct bt_ctf_field_type_integer
*) type
;
2632 bt_put(integer
->mapped_clock
);
2637 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2639 struct bt_ctf_field_type_enumeration
*enumeration
=
2640 (struct bt_ctf_field_type_enumeration
*) type
;
2646 g_ptr_array_free(enumeration
->entries
, TRUE
);
2647 bt_put(enumeration
->container
);
2648 g_free(enumeration
);
2652 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2654 struct bt_ctf_field_type_floating_point
*floating_point
=
2655 (struct bt_ctf_field_type_floating_point
*) type
;
2661 g_free(floating_point
);
2665 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2667 struct bt_ctf_field_type_structure
*structure
=
2668 (struct bt_ctf_field_type_structure
*) type
;
2674 g_ptr_array_free(structure
->fields
, TRUE
);
2675 g_hash_table_destroy(structure
->field_name_to_index
);
2680 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2682 struct bt_ctf_field_type_variant
*variant
=
2683 (struct bt_ctf_field_type_variant
*) type
;
2689 g_ptr_array_free(variant
->fields
, TRUE
);
2690 g_hash_table_destroy(variant
->field_name_to_index
);
2691 g_string_free(variant
->tag_name
, TRUE
);
2692 bt_put(&variant
->tag
->parent
);
2693 BT_PUT(variant
->tag_field_path
);
2698 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2700 struct bt_ctf_field_type_array
*array
=
2701 (struct bt_ctf_field_type_array
*) type
;
2707 bt_put(array
->element_type
);
2712 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2714 struct bt_ctf_field_type_sequence
*sequence
=
2715 (struct bt_ctf_field_type_sequence
*) type
;
2721 bt_put(sequence
->element_type
);
2722 g_string_free(sequence
->length_field_name
, TRUE
);
2723 BT_PUT(sequence
->length_field_path
);
2728 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2730 struct bt_ctf_field_type_string
*string
=
2731 (struct bt_ctf_field_type_string
*) type
;
2741 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2747 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2749 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2750 type
, struct bt_ctf_field_type_integer
, parent
);
2752 if (integer_type
->mapped_clock
) {
2753 bt_ctf_clock_freeze(integer_type
->mapped_clock
);
2756 generic_field_type_freeze(type
);
2760 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2762 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2763 type
, struct bt_ctf_field_type_enumeration
, parent
);
2765 generic_field_type_freeze(type
);
2766 bt_ctf_field_type_freeze(enumeration_type
->container
);
2770 void freeze_structure_field(struct structure_field
*field
)
2772 bt_ctf_field_type_freeze(field
->type
);
2776 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2778 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2779 type
, struct bt_ctf_field_type_structure
, parent
);
2781 /* Cache the alignment */
2782 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2783 generic_field_type_freeze(type
);
2784 g_ptr_array_foreach(structure_type
->fields
,
2785 (GFunc
) freeze_structure_field
, NULL
);
2789 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2791 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2792 type
, struct bt_ctf_field_type_variant
, parent
);
2794 generic_field_type_freeze(type
);
2795 g_ptr_array_foreach(variant_type
->fields
,
2796 (GFunc
) freeze_structure_field
, NULL
);
2800 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2802 struct bt_ctf_field_type_array
*array_type
= container_of(
2803 type
, struct bt_ctf_field_type_array
, parent
);
2805 /* Cache the alignment */
2806 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2807 generic_field_type_freeze(type
);
2808 bt_ctf_field_type_freeze(array_type
->element_type
);
2812 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2814 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2815 type
, struct bt_ctf_field_type_sequence
, parent
);
2817 /* Cache the alignment */
2818 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2819 generic_field_type_freeze(type
);
2820 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2824 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2826 const char *encoding_string
;
2829 case BT_CTF_STRING_ENCODING_NONE
:
2830 encoding_string
= "none";
2832 case BT_CTF_STRING_ENCODING_ASCII
:
2833 encoding_string
= "ASCII";
2835 case BT_CTF_STRING_ENCODING_UTF8
:
2836 encoding_string
= "UTF8";
2839 encoding_string
= "unknown";
2843 return encoding_string
;
2847 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2849 const char *base_string
;
2852 case BT_CTF_INTEGER_BASE_DECIMAL
:
2853 base_string
= "decimal";
2855 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2856 base_string
= "hexadecimal";
2858 case BT_CTF_INTEGER_BASE_OCTAL
:
2859 base_string
= "octal";
2861 case BT_CTF_INTEGER_BASE_BINARY
:
2862 base_string
= "binary";
2865 base_string
= "unknown";
2873 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2874 struct metadata_context
*context
)
2876 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2877 struct bt_ctf_field_type_integer
, parent
);
2880 g_string_append_printf(context
->string
,
2881 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2882 integer
->declaration
.len
, type
->declaration
->alignment
,
2883 (integer
->declaration
.signedness
? "true" : "false"),
2884 get_encoding_string(integer
->declaration
.encoding
),
2885 get_integer_base_string(integer
->declaration
.base
),
2886 get_byte_order_string(integer
->declaration
.byte_order
));
2887 if (integer
->mapped_clock
) {
2888 const char *clock_name
= bt_ctf_clock_get_name(
2889 integer
->mapped_clock
);
2896 g_string_append_printf(context
->string
,
2897 "; map = clock.%s.value", clock_name
);
2900 g_string_append(context
->string
, "; }");
2906 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2907 struct metadata_context
*context
)
2911 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2912 struct bt_ctf_field_type_enumeration
, parent
);
2913 struct bt_ctf_field_type
*container_type
;
2914 int container_signed
;
2916 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2917 if (!container_type
) {
2922 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2923 if (container_signed
< 0) {
2924 ret
= container_signed
;
2925 goto error_put_container_type
;
2928 g_string_append(context
->string
, "enum : ");
2929 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2931 goto error_put_container_type
;
2934 g_string_append(context
->string
, " { ");
2935 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2936 struct enumeration_mapping
*mapping
=
2937 enumeration
->entries
->pdata
[entry
];
2939 if (container_signed
) {
2940 if (mapping
->range_start
._signed
==
2941 mapping
->range_end
._signed
) {
2942 g_string_append_printf(context
->string
,
2943 "\"%s\" = %" PRId64
,
2944 g_quark_to_string(mapping
->string
),
2945 mapping
->range_start
._signed
);
2947 g_string_append_printf(context
->string
,
2948 "\"%s\" = %" PRId64
" ... %" PRId64
,
2949 g_quark_to_string(mapping
->string
),
2950 mapping
->range_start
._signed
,
2951 mapping
->range_end
._signed
);
2954 if (mapping
->range_start
._unsigned
==
2955 mapping
->range_end
._unsigned
) {
2956 g_string_append_printf(context
->string
,
2957 "\"%s\" = %" PRIu64
,
2958 g_quark_to_string(mapping
->string
),
2959 mapping
->range_start
._unsigned
);
2961 g_string_append_printf(context
->string
,
2962 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2963 g_quark_to_string(mapping
->string
),
2964 mapping
->range_start
._unsigned
,
2965 mapping
->range_end
._unsigned
);
2969 g_string_append(context
->string
,
2970 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2974 if (context
->field_name
->len
) {
2975 g_string_append_printf(context
->string
, " %s",
2976 context
->field_name
->str
);
2977 g_string_assign(context
->field_name
, "");
2979 error_put_container_type
:
2980 bt_put(container_type
);
2986 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2987 struct metadata_context
*context
)
2989 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2990 type
, struct bt_ctf_field_type_floating_point
, parent
);
2992 g_string_append_printf(context
->string
,
2993 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2994 floating_point
->declaration
.exp
->len
,
2995 floating_point
->declaration
.mantissa
->len
+ 1,
2996 get_byte_order_string(floating_point
->declaration
.byte_order
),
2997 type
->declaration
->alignment
);
3002 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3003 struct metadata_context
*context
)
3006 unsigned int indent
;
3008 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3009 struct bt_ctf_field_type_structure
, parent
);
3010 GString
*structure_field_name
= context
->field_name
;
3012 context
->field_name
= g_string_new("");
3014 context
->current_indentation_level
++;
3015 g_string_append(context
->string
, "struct {\n");
3017 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3018 struct structure_field
*field
;
3020 for (indent
= 0; indent
< context
->current_indentation_level
;
3022 g_string_append_c(context
->string
, '\t');
3025 field
= structure
->fields
->pdata
[i
];
3026 g_string_assign(context
->field_name
,
3027 g_quark_to_string(field
->name
));
3028 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3033 if (context
->field_name
->len
) {
3034 g_string_append_printf(context
->string
, " %s",
3035 context
->field_name
->str
);
3037 g_string_append(context
->string
, ";\n");
3040 context
->current_indentation_level
--;
3041 for (indent
= 0; indent
< context
->current_indentation_level
;
3043 g_string_append_c(context
->string
, '\t');
3046 g_string_append_printf(context
->string
, "} align(%zu)",
3047 type
->declaration
->alignment
);
3049 g_string_free(context
->field_name
, TRUE
);
3050 context
->field_name
= structure_field_name
;
3055 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3056 struct metadata_context
*context
)
3059 unsigned int indent
;
3061 struct bt_ctf_field_type_variant
*variant
= container_of(
3062 type
, struct bt_ctf_field_type_variant
, parent
);
3063 GString
*variant_field_name
= context
->field_name
;
3065 context
->field_name
= g_string_new("");
3066 if (variant
->tag_name
->len
> 0) {
3067 g_string_append_printf(context
->string
,
3068 "variant <%s> {\n", variant
->tag_name
->str
);
3070 g_string_append(context
->string
, "variant {\n");
3073 context
->current_indentation_level
++;
3074 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3075 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3077 g_string_assign(context
->field_name
,
3078 g_quark_to_string(field
->name
));
3079 for (indent
= 0; indent
< context
->current_indentation_level
;
3081 g_string_append_c(context
->string
, '\t');
3084 g_string_assign(context
->field_name
,
3085 g_quark_to_string(field
->name
));
3086 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3091 if (context
->field_name
->len
) {
3092 g_string_append_printf(context
->string
, " %s;",
3093 context
->field_name
->str
);
3096 g_string_append_c(context
->string
, '\n');
3099 context
->current_indentation_level
--;
3100 for (indent
= 0; indent
< context
->current_indentation_level
;
3102 g_string_append_c(context
->string
, '\t');
3105 g_string_append(context
->string
, "}");
3107 g_string_free(context
->field_name
, TRUE
);
3108 context
->field_name
= variant_field_name
;
3113 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3114 struct metadata_context
*context
)
3117 struct bt_ctf_field_type_array
*array
= container_of(type
,
3118 struct bt_ctf_field_type_array
, parent
);
3120 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3125 if (context
->field_name
->len
) {
3126 g_string_append_printf(context
->string
, " %s[%u]",
3127 context
->field_name
->str
, array
->length
);
3128 g_string_assign(context
->field_name
, "");
3130 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3138 struct metadata_context
*context
)
3141 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3142 type
, struct bt_ctf_field_type_sequence
, parent
);
3144 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3149 if (context
->field_name
->len
) {
3150 g_string_append_printf(context
->string
, " %s[%s]",
3151 context
->field_name
->str
,
3152 sequence
->length_field_name
->str
);
3153 g_string_assign(context
->field_name
, "");
3155 g_string_append_printf(context
->string
, "[%s]",
3156 sequence
->length_field_name
->str
);
3163 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3164 struct metadata_context
*context
)
3166 struct bt_ctf_field_type_string
*string
= container_of(
3167 type
, struct bt_ctf_field_type_string
, parent
);
3169 g_string_append_printf(context
->string
,
3170 "string { encoding = %s; }",
3171 get_encoding_string(string
->declaration
.encoding
));
3176 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3177 enum bt_ctf_byte_order ret
;
3179 switch (byte_order
) {
3180 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3182 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3184 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3186 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3188 case BT_CTF_BYTE_ORDER_NETWORK
:
3189 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3191 case BT_CTF_BYTE_ORDER_NATIVE
:
3192 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3195 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3203 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3204 int byte_order
, int set_native
)
3206 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3207 struct bt_ctf_field_type_integer
, parent
);
3210 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3212 * User byte order is native, so we can set
3213 * the real byte order.
3215 integer_type
->declaration
.byte_order
=
3219 integer_type
->user_byte_order
=
3220 get_ctf_ir_byte_order(byte_order
);
3221 integer_type
->declaration
.byte_order
= byte_order
;
3226 void bt_ctf_field_type_enumeration_set_byte_order(
3227 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3229 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3230 struct bt_ctf_field_type_enumeration
, parent
);
3232 /* Safe to assume that container is an integer */
3233 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3234 byte_order
, set_native
);
3238 void bt_ctf_field_type_floating_point_set_byte_order(
3239 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3241 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3242 container_of(type
, struct bt_ctf_field_type_floating_point
,
3246 if (floating_point_type
->user_byte_order
==
3247 BT_CTF_BYTE_ORDER_NATIVE
) {
3249 * User byte order is native, so we can set
3250 * the real byte order.
3252 floating_point_type
->declaration
.byte_order
=
3254 floating_point_type
->sign
.byte_order
=
3256 floating_point_type
->mantissa
.byte_order
=
3258 floating_point_type
->exp
.byte_order
=
3262 floating_point_type
->user_byte_order
=
3263 get_ctf_ir_byte_order(byte_order
);
3264 floating_point_type
->declaration
.byte_order
= byte_order
;
3265 floating_point_type
->sign
.byte_order
= byte_order
;
3266 floating_point_type
->mantissa
.byte_order
= byte_order
;
3267 floating_point_type
->exp
.byte_order
= byte_order
;
3272 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3273 int byte_order
, int set_native
)
3276 struct bt_ctf_field_type_structure
*structure_type
=
3277 container_of(type
, struct bt_ctf_field_type_structure
,
3280 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3281 struct structure_field
*field
= g_ptr_array_index(
3282 structure_type
->fields
, i
);
3283 struct bt_ctf_field_type
*field_type
= field
->type
;
3285 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3286 set_byte_order_funcs
[field_type
->declaration
->id
](
3287 field_type
, byte_order
, set_native
);
3293 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3294 int byte_order
, int set_native
)
3297 struct bt_ctf_field_type_variant
*variant_type
=
3298 container_of(type
, struct bt_ctf_field_type_variant
,
3301 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3302 struct structure_field
*field
= g_ptr_array_index(
3303 variant_type
->fields
, i
);
3304 struct bt_ctf_field_type
*field_type
= field
->type
;
3306 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3307 set_byte_order_funcs
[field_type
->declaration
->id
](
3308 field_type
, byte_order
, set_native
);
3314 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3315 int byte_order
, int set_native
)
3317 struct bt_ctf_field_type_array
*array_type
=
3318 container_of(type
, struct bt_ctf_field_type_array
,
3321 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3322 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3323 array_type
->element_type
, byte_order
, set_native
);
3328 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3329 int byte_order
, int set_native
)
3331 struct bt_ctf_field_type_sequence
*sequence_type
=
3332 container_of(type
, struct bt_ctf_field_type_sequence
,
3335 if (set_byte_order_funcs
[
3336 sequence_type
->element_type
->declaration
->id
]) {
3337 set_byte_order_funcs
[
3338 sequence_type
->element_type
->declaration
->id
](
3339 sequence_type
->element_type
, byte_order
, set_native
);
3344 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3345 struct bt_ctf_field_type
*type
)
3347 struct bt_ctf_field_type
*copy
;
3348 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3350 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3351 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3356 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3358 copy_integer
->declaration
= integer
->declaration
;
3359 if (integer
->mapped_clock
) {
3360 bt_get(integer
->mapped_clock
);
3361 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3364 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3371 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3372 struct bt_ctf_field_type
*type
)
3375 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3376 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3378 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3381 /* Copy the source enumeration's container */
3382 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3383 if (!copy_container
) {
3387 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3391 copy_enumeration
= container_of(copy
,
3392 struct bt_ctf_field_type_enumeration
, parent
);
3394 /* Copy all enumaration entries */
3395 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3396 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3397 enumeration
->entries
, i
);
3398 struct enumeration_mapping
* copy_mapping
= g_new0(
3399 struct enumeration_mapping
, 1);
3401 if (!copy_mapping
) {
3405 *copy_mapping
= *mapping
;
3406 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3409 copy_enumeration
->declaration
= enumeration
->declaration
;
3411 bt_put(copy_container
);
3414 bt_put(copy_container
);
3420 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3421 struct bt_ctf_field_type
*type
)
3423 struct bt_ctf_field_type
*copy
;
3424 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3426 floating_point
= container_of(type
,
3427 struct bt_ctf_field_type_floating_point
, parent
);
3428 copy
= bt_ctf_field_type_floating_point_create();
3433 copy_float
= container_of(copy
,
3434 struct bt_ctf_field_type_floating_point
, parent
);
3435 copy_float
->declaration
= floating_point
->declaration
;
3436 copy_float
->sign
= floating_point
->sign
;
3437 copy_float
->mantissa
= floating_point
->mantissa
;
3438 copy_float
->exp
= floating_point
->exp
;
3439 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3440 copy_float
->declaration
.sign
= ©_float
->sign
;
3441 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3442 copy_float
->declaration
.exp
= ©_float
->exp
;
3448 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3449 struct bt_ctf_field_type
*type
)
3452 GHashTableIter iter
;
3453 gpointer key
, value
;
3454 struct bt_ctf_field_type
*copy
;
3455 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3457 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3459 copy
= bt_ctf_field_type_structure_create();
3464 copy_structure
= container_of(copy
,
3465 struct bt_ctf_field_type_structure
, parent
);
3467 /* Copy field_name_to_index */
3468 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3469 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3470 g_hash_table_insert(copy_structure
->field_name_to_index
,
3474 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3475 struct structure_field
*entry
, *copy_entry
;
3476 struct bt_ctf_field_type
*copy_field
;
3478 copy_entry
= g_new0(struct structure_field
, 1);
3483 entry
= g_ptr_array_index(structure
->fields
, i
);
3484 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3490 copy_entry
->name
= entry
->name
;
3491 copy_entry
->type
= copy_field
;
3492 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3495 copy_structure
->declaration
= structure
->declaration
;
3504 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3505 struct bt_ctf_field_type
*type
)
3508 GHashTableIter iter
;
3509 gpointer key
, value
;
3510 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3511 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3513 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3516 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3522 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3523 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3528 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3531 /* Copy field_name_to_index */
3532 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3533 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3534 g_hash_table_insert(copy_variant
->field_name_to_index
,
3538 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3539 struct structure_field
*entry
, *copy_entry
;
3540 struct bt_ctf_field_type
*copy_field
;
3542 copy_entry
= g_new0(struct structure_field
, 1);
3547 entry
= g_ptr_array_index(variant
->fields
, i
);
3548 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3554 copy_entry
->name
= entry
->name
;
3555 copy_entry
->type
= copy_field
;
3556 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3559 copy_variant
->declaration
= variant
->declaration
;
3560 if (variant
->tag_field_path
) {
3561 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3562 variant
->tag_field_path
);
3563 if (!copy_variant
->tag_field_path
) {
3577 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3578 struct bt_ctf_field_type
*type
)
3580 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3581 struct bt_ctf_field_type_array
*array
, *copy_array
;
3583 array
= container_of(type
, struct bt_ctf_field_type_array
,
3585 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3586 if (!copy_element
) {
3590 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3595 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3597 copy_array
->declaration
= array
->declaration
;
3599 bt_put(copy_element
);
3604 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3605 struct bt_ctf_field_type
*type
)
3607 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3608 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3610 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3612 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3613 if (!copy_element
) {
3617 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3618 sequence
->length_field_name
->len
?
3619 sequence
->length_field_name
->str
: NULL
);
3624 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3626 copy_sequence
->declaration
= sequence
->declaration
;
3627 if (sequence
->length_field_path
) {
3628 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3629 sequence
->length_field_path
);
3630 if (!copy_sequence
->length_field_path
) {
3635 bt_put(copy_element
);
3643 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3644 struct bt_ctf_field_type
*type
)
3646 struct bt_ctf_field_type
*copy
;
3647 struct bt_ctf_field_type_string
*string
, *copy_string
;
3649 copy
= bt_ctf_field_type_string_create();
3654 string
= container_of(type
, struct bt_ctf_field_type_string
,
3656 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3658 copy_string
->declaration
= string
->declaration
;
3664 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3665 struct bt_ctf_field_type
*type_b
)
3668 struct bt_ctf_field_type_integer
*integer_a
;
3669 struct bt_ctf_field_type_integer
*integer_b
;
3670 struct declaration_integer
*decl_a
;
3671 struct declaration_integer
*decl_b
;
3673 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3675 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3677 decl_a
= &integer_a
->declaration
;
3678 decl_b
= &integer_b
->declaration
;
3681 if (decl_a
->len
!= decl_b
->len
) {
3686 * Compare user byte orders only, not the cached,
3689 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3694 if (decl_a
->signedness
!= decl_b
->signedness
) {
3699 if (decl_a
->base
!= decl_b
->base
) {
3704 if (decl_a
->encoding
!= decl_b
->encoding
) {
3709 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3721 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3722 struct bt_ctf_field_type
*type_b
)
3725 struct bt_ctf_field_type_floating_point
*float_a
;
3726 struct bt_ctf_field_type_floating_point
*float_b
;
3728 float_a
= container_of(type_a
,
3729 struct bt_ctf_field_type_floating_point
, parent
);
3730 float_b
= container_of(type_b
,
3731 struct bt_ctf_field_type_floating_point
, parent
);
3734 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3738 /* Exponent length */
3739 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3743 /* Mantissa length */
3744 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3749 * Compare user byte orders only, not the cached,
3752 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3764 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3765 struct enumeration_mapping
*mapping_b
)
3770 if (mapping_a
->string
!= mapping_b
->string
) {
3775 if (mapping_a
->range_start
._unsigned
!=
3776 mapping_b
->range_start
._unsigned
) {
3781 if (mapping_a
->range_end
._unsigned
!=
3782 mapping_b
->range_end
._unsigned
) {
3794 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3795 struct bt_ctf_field_type
*type_b
)
3799 struct bt_ctf_field_type_enumeration
*enum_a
;
3800 struct bt_ctf_field_type_enumeration
*enum_b
;
3802 enum_a
= container_of(type_a
,
3803 struct bt_ctf_field_type_enumeration
, parent
);
3804 enum_b
= container_of(type_b
,
3805 struct bt_ctf_field_type_enumeration
, parent
);
3807 /* Container field type */
3808 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3816 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3820 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3821 struct enumeration_mapping
*mapping_a
=
3822 g_ptr_array_index(enum_a
->entries
, i
);
3823 struct enumeration_mapping
*mapping_b
=
3824 g_ptr_array_index(enum_b
->entries
, i
);
3826 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3839 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3840 struct bt_ctf_field_type
*type_b
)
3843 struct bt_ctf_field_type_string
*string_a
;
3844 struct bt_ctf_field_type_string
*string_b
;
3846 string_a
= container_of(type_a
,
3847 struct bt_ctf_field_type_string
, parent
);
3848 string_b
= container_of(type_b
,
3849 struct bt_ctf_field_type_string
, parent
);
3852 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3864 int compare_structure_fields(struct structure_field
*field_a
,
3865 struct structure_field
*field_b
)
3870 if (field_a
->name
!= field_b
->name
) {
3875 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3882 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3883 struct bt_ctf_field_type
*type_b
)
3887 struct bt_ctf_field_type_structure
*struct_a
;
3888 struct bt_ctf_field_type_structure
*struct_b
;
3890 struct_a
= container_of(type_a
,
3891 struct bt_ctf_field_type_structure
, parent
);
3892 struct_b
= container_of(type_b
,
3893 struct bt_ctf_field_type_structure
, parent
);
3896 if (bt_ctf_field_type_get_alignment(type_a
) !=
3897 bt_ctf_field_type_get_alignment(type_b
)) {
3902 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3906 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3907 struct structure_field
*field_a
=
3908 g_ptr_array_index(struct_a
->fields
, i
);
3909 struct structure_field
*field_b
=
3910 g_ptr_array_index(struct_b
->fields
, i
);
3912 ret
= compare_structure_fields(field_a
, field_b
);
3926 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3927 struct bt_ctf_field_type
*type_b
)
3931 struct bt_ctf_field_type_variant
*variant_a
;
3932 struct bt_ctf_field_type_variant
*variant_b
;
3934 variant_a
= container_of(type_a
,
3935 struct bt_ctf_field_type_variant
, parent
);
3936 variant_b
= container_of(type_b
,
3937 struct bt_ctf_field_type_variant
, parent
);
3940 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3945 ret
= bt_ctf_field_type_compare(
3946 (struct bt_ctf_field_type
*) variant_a
->tag
,
3947 (struct bt_ctf_field_type
*) variant_b
->tag
);
3955 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3959 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3960 struct structure_field
*field_a
=
3961 g_ptr_array_index(variant_a
->fields
, i
);
3962 struct structure_field
*field_b
=
3963 g_ptr_array_index(variant_b
->fields
, i
);
3965 ret
= compare_structure_fields(field_a
, field_b
);
3979 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3980 struct bt_ctf_field_type
*type_b
)
3983 struct bt_ctf_field_type_array
*array_a
;
3984 struct bt_ctf_field_type_array
*array_b
;
3986 array_a
= container_of(type_a
,
3987 struct bt_ctf_field_type_array
, parent
);
3988 array_b
= container_of(type_b
,
3989 struct bt_ctf_field_type_array
, parent
);
3992 if (array_a
->length
!= array_b
->length
) {
3997 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3998 array_b
->element_type
);
4005 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4006 struct bt_ctf_field_type
*type_b
)
4009 struct bt_ctf_field_type_sequence
*sequence_a
;
4010 struct bt_ctf_field_type_sequence
*sequence_b
;
4012 sequence_a
= container_of(type_a
,
4013 struct bt_ctf_field_type_sequence
, parent
);
4014 sequence_b
= container_of(type_b
,
4015 struct bt_ctf_field_type_sequence
, parent
);
4018 if (strcmp(sequence_a
->length_field_name
->str
,
4019 sequence_b
->length_field_name
->str
)) {
4024 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4025 sequence_b
->element_type
);
4032 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4033 struct bt_ctf_field_type
*type_b
)
4037 if (type_a
== type_b
) {
4038 /* Same reference: equal (even if both are NULL) */
4043 if (!type_a
|| !type_b
) {
4048 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4049 /* Different type IDs */
4053 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
4054 /* Both have unknown type IDs */
4058 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4065 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4067 int field_count
= -1;
4068 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4071 case CTF_TYPE_STRUCT
:
4073 bt_ctf_field_type_structure_get_field_count(field_type
);
4075 case CTF_TYPE_VARIANT
:
4077 bt_ctf_field_type_variant_get_field_count(field_type
);
4079 case CTF_TYPE_ARRAY
:
4080 case CTF_TYPE_SEQUENCE
:
4082 * Array and sequence types always contain a single member
4083 * (the element type).
4095 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4096 struct bt_ctf_field_type
*field_type
, int index
)
4098 struct bt_ctf_field_type
*field
= NULL
;
4099 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4102 case CTF_TYPE_STRUCT
:
4103 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4106 case CTF_TYPE_VARIANT
:
4108 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4116 case CTF_TYPE_ARRAY
:
4117 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4119 case CTF_TYPE_SEQUENCE
:
4120 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4130 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4133 int field_index
= -1;
4134 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4137 case CTF_TYPE_STRUCT
:
4138 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4141 case CTF_TYPE_VARIANT
:
4142 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4153 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4154 struct bt_ctf_field_type
*type
)
4156 struct bt_ctf_field_path
*field_path
= NULL
;
4157 struct bt_ctf_field_type_variant
*variant
;
4159 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4163 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4165 field_path
= bt_get(variant
->tag_field_path
);
4171 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4172 struct bt_ctf_field_type
*type
)
4174 struct bt_ctf_field_path
*field_path
= NULL
;
4175 struct bt_ctf_field_type_sequence
*sequence
;
4177 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4181 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4183 field_path
= bt_get(sequence
->length_field_path
);