4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/clock.h>
34 #include <babeltrace/ctf-writer/writer-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/endian.h>
43 struct range_overlap_query
{
58 void bt_ctf_field_type_destroy(struct bt_object
*);
60 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
62 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
64 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
66 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
68 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
70 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
72 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
74 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
77 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
78 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 bt_ctf_field_type_enumeration_destroy
,
82 bt_ctf_field_type_floating_point_destroy
,
83 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
84 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
85 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
86 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
87 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
91 void generic_field_type_freeze(struct bt_ctf_field_type
*);
93 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
99 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
101 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
104 type_freeze_func
const type_freeze_funcs
[] = {
105 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
106 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
107 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
108 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
109 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
110 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
111 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
112 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
116 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_floating_point_serialize(
123 struct bt_ctf_field_type
*, struct metadata_context
*);
125 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
141 type_serialize_func
const type_serialize_funcs
[] = {
142 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
144 bt_ctf_field_type_enumeration_serialize
,
146 bt_ctf_field_type_floating_point_serialize
,
148 bt_ctf_field_type_structure_serialize
,
149 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
150 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
151 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
152 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
156 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_floating_point_set_byte_order(
163 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
165 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 /* The set_native flag only set the byte order if it is set to native */
179 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
180 int byte_order
, int set_native
) = {
181 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
183 bt_ctf_field_type_enumeration_set_byte_order
,
185 bt_ctf_field_type_floating_point_set_byte_order
,
187 bt_ctf_field_type_structure_set_byte_order
,
188 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
189 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
190 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
191 [CTF_TYPE_STRING
] = NULL
,
195 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
217 struct bt_ctf_field_type
*);
220 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
221 struct bt_ctf_field_type
*) = {
222 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
223 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
224 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
225 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
226 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
227 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
228 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
229 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
233 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
234 struct bt_ctf_field_type
*);
236 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
237 struct bt_ctf_field_type
*);
239 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
240 struct bt_ctf_field_type
*);
242 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
243 struct bt_ctf_field_type
*);
245 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
246 struct bt_ctf_field_type
*);
248 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
249 struct bt_ctf_field_type
*);
251 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
252 struct bt_ctf_field_type
*);
254 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
255 struct bt_ctf_field_type
*);
258 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
259 struct bt_ctf_field_type
*) = {
260 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_compare
,
261 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_compare
,
262 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
263 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_compare
,
264 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_compare
,
265 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_compare
,
266 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
267 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_compare
,
271 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
277 void destroy_structure_field(struct structure_field
*field
)
284 void check_ranges_overlap(gpointer element
, gpointer query
)
286 struct enumeration_mapping
*mapping
= element
;
287 struct range_overlap_query
*overlap_query
= query
;
289 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
290 && overlap_query
->range_start
._signed
<=
291 mapping
->range_end
._signed
) {
292 overlap_query
->overlaps
= 1;
293 overlap_query
->mapping_name
= mapping
->string
;
296 overlap_query
->overlaps
|=
297 mapping
->string
== overlap_query
->mapping_name
;
301 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
303 struct enumeration_mapping
*mapping
= element
;
304 struct range_overlap_query
*overlap_query
= query
;
306 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
307 && overlap_query
->range_start
._unsigned
<=
308 mapping
->range_end
._unsigned
) {
309 overlap_query
->overlaps
= 1;
310 overlap_query
->mapping_name
= mapping
->string
;
313 overlap_query
->overlaps
|=
314 mapping
->string
== overlap_query
->mapping_name
;
318 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
319 struct enumeration_mapping
**b
)
321 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
325 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
326 struct enumeration_mapping
**b
)
328 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
332 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
334 enum ctf_type_id type_id
= type
->declaration
->id
;
336 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
337 (type_id
< NR_CTF_TYPES
));
339 bt_object_init(type
, bt_ctf_field_type_destroy
);
340 type
->freeze
= type_freeze_funcs
[type_id
];
341 type
->serialize
= type_serialize_funcs
[type_id
];
344 int ret
= bt_ctf_field_type_set_byte_order(type
,
345 BT_CTF_BYTE_ORDER_NATIVE
);
349 type
->declaration
->alignment
= 1;
353 int add_structure_field(GPtrArray
*fields
,
354 GHashTable
*field_name_to_index
,
355 struct bt_ctf_field_type
*field_type
,
356 const char *field_name
)
359 GQuark name_quark
= g_quark_from_string(field_name
);
360 struct structure_field
*field
;
362 /* Make sure structure does not contain a field of the same name */
363 if (g_hash_table_lookup_extended(field_name_to_index
,
364 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
369 field
= g_new0(struct structure_field
, 1);
376 field
->name
= name_quark
;
377 field
->type
= field_type
;
378 g_hash_table_insert(field_name_to_index
,
379 (gpointer
) (unsigned long) name_quark
,
380 (gpointer
) (unsigned long) fields
->len
);
381 g_ptr_array_add(fields
, field
);
387 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
389 struct bt_ctf_field_type
*type
;
390 enum ctf_type_id type_id
;
392 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
393 type_id
= type
->declaration
->id
;
394 if (type_id
<= CTF_TYPE_UNKNOWN
||
395 type_id
>= NR_CTF_TYPES
) {
399 type_destroy_funcs
[type_id
](type
);
403 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
412 switch (type
->declaration
->id
) {
415 struct bt_ctf_field_type_enumeration
*enumeration
=
416 container_of(type
, struct bt_ctf_field_type_enumeration
,
419 /* Ensure enum has entries */
420 ret
= enumeration
->entries
->len
? 0 : -1;
423 case CTF_TYPE_SEQUENCE
:
425 struct bt_ctf_field_type_sequence
*sequence
=
426 container_of(type
, struct bt_ctf_field_type_sequence
,
429 /* length field name should be set at this point */
430 ret
= sequence
->length_field_name
->len
? 0 : -1;
433 case CTF_TYPE_VARIANT
:
435 struct bt_ctf_field_type_variant
*variant
=
436 container_of(type
, struct bt_ctf_field_type_variant
,
439 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
451 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
453 struct bt_ctf_field_type_integer
*integer
=
454 g_new0(struct bt_ctf_field_type_integer
, 1);
456 if (!integer
|| size
== 0 || size
> 64) {
460 integer
->parent
.declaration
= &integer
->declaration
.p
;
461 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
462 integer
->declaration
.len
= size
;
463 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
464 integer
->declaration
.encoding
= CTF_STRING_NONE
;
465 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
466 return &integer
->parent
;
469 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
472 struct bt_ctf_field_type_integer
*integer
;
474 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
479 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
480 ret
= (int) integer
->declaration
.len
;
485 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
488 struct bt_ctf_field_type_integer
*integer
;
490 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
495 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
496 ret
= integer
->declaration
.signedness
;
501 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
505 struct bt_ctf_field_type_integer
*integer
;
507 if (!type
|| type
->frozen
||
508 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
513 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
514 integer
->declaration
.signedness
= !!is_signed
;
519 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
520 struct bt_ctf_field_type
*type
)
522 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
523 struct bt_ctf_field_type_integer
*integer
;
525 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
529 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
530 ret
= integer
->declaration
.base
;
535 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
536 enum bt_ctf_integer_base base
)
540 if (!type
|| type
->frozen
||
541 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
547 case BT_CTF_INTEGER_BASE_BINARY
:
548 case BT_CTF_INTEGER_BASE_OCTAL
:
549 case BT_CTF_INTEGER_BASE_DECIMAL
:
550 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
552 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
553 struct bt_ctf_field_type_integer
, parent
);
554 integer
->declaration
.base
= base
;
564 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
565 struct bt_ctf_field_type
*type
)
567 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
568 struct bt_ctf_field_type_integer
*integer
;
570 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
574 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
575 ret
= integer
->declaration
.encoding
;
580 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
581 enum ctf_string_encoding encoding
)
584 struct bt_ctf_field_type_integer
*integer
;
586 if (!type
|| type
->frozen
||
587 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
588 (encoding
< CTF_STRING_NONE
) ||
589 (encoding
>= CTF_STRING_UNKNOWN
)) {
594 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
595 integer
->declaration
.encoding
= encoding
;
600 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
601 struct bt_ctf_field_type
*type
)
603 struct bt_ctf_field_type_integer
*integer
;
604 struct bt_ctf_clock
*clock
= NULL
;
610 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
611 clock
= integer
->mapped_clock
;
617 int bt_ctf_field_type_integer_set_mapped_clock(
618 struct bt_ctf_field_type
*type
,
619 struct bt_ctf_clock
*clock
)
621 struct bt_ctf_field_type_integer
*integer
;
624 if (!type
|| type
->frozen
) {
629 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
630 bt_put(integer
->mapped_clock
);
632 integer
->mapped_clock
= clock
;
637 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
638 struct bt_ctf_field_type
*integer_container_type
)
640 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
642 if (!integer_container_type
) {
646 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
650 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
655 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
656 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
657 bt_get(integer_container_type
);
658 enumeration
->container
= integer_container_type
;
659 enumeration
->entries
= g_ptr_array_new_with_free_func(
660 (GDestroyNotify
)destroy_enumeration_mapping
);
661 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
662 return &enumeration
->parent
;
668 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
669 struct bt_ctf_field_type
*type
)
671 struct bt_ctf_field_type
*container_type
= NULL
;
672 struct bt_ctf_field_type_enumeration
*enumeration_type
;
678 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
682 enumeration_type
= container_of(type
,
683 struct bt_ctf_field_type_enumeration
, parent
);
684 container_type
= enumeration_type
->container
;
685 bt_get(container_type
);
687 return container_type
;
690 int bt_ctf_field_type_enumeration_add_mapping(
691 struct bt_ctf_field_type
*type
, const char *string
,
692 int64_t range_start
, int64_t range_end
)
696 struct enumeration_mapping
*mapping
;
697 struct bt_ctf_field_type_enumeration
*enumeration
;
698 struct range_overlap_query query
;
699 char *escaped_string
;
701 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
703 (range_end
< range_start
)) {
708 if (!string
|| strlen(string
) == 0) {
713 escaped_string
= g_strescape(string
, NULL
);
714 if (!escaped_string
) {
719 mapping_name
= g_quark_from_string(escaped_string
);
720 query
= (struct range_overlap_query
) {
721 .range_start
._signed
= range_start
,
722 .range_end
._signed
= range_end
,
723 .mapping_name
= mapping_name
,
725 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
728 /* Check that the range does not overlap with one already present */
729 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
730 if (query
.overlaps
) {
735 mapping
= g_new(struct enumeration_mapping
, 1);
741 *mapping
= (struct enumeration_mapping
) {
742 .range_start
._signed
= range_start
,
743 .range_end
._signed
= range_end
, .string
= mapping_name
};
744 g_ptr_array_add(enumeration
->entries
, mapping
);
745 g_ptr_array_sort(enumeration
->entries
,
746 (GCompareFunc
)compare_enumeration_mappings_signed
);
748 free(escaped_string
);
753 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
754 struct bt_ctf_field_type
*type
, const char *string
,
755 uint64_t range_start
, uint64_t range_end
)
759 struct enumeration_mapping
*mapping
;
760 struct bt_ctf_field_type_enumeration
*enumeration
;
761 struct range_overlap_query query
;
762 char *escaped_string
;
764 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
766 (range_end
< range_start
)) {
771 if (!string
|| strlen(string
) == 0) {
776 escaped_string
= g_strescape(string
, NULL
);
777 if (!escaped_string
) {
782 mapping_name
= g_quark_from_string(escaped_string
);
783 query
= (struct range_overlap_query
) {
784 .range_start
._unsigned
= range_start
,
785 .range_end
._unsigned
= range_end
,
786 .mapping_name
= mapping_name
,
788 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
791 /* Check that the range does not overlap with one already present */
792 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
794 if (query
.overlaps
) {
799 mapping
= g_new(struct enumeration_mapping
, 1);
805 *mapping
= (struct enumeration_mapping
) {
806 .range_start
._unsigned
= range_start
,
807 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
808 g_ptr_array_add(enumeration
->entries
, mapping
);
809 g_ptr_array_sort(enumeration
->entries
,
810 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
812 free(escaped_string
);
817 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
818 struct bt_ctf_field_type_enumeration
*enumeration_type
,
821 const char *name
= NULL
;
822 struct range_overlap_query query
=
823 (struct range_overlap_query
) {
824 .range_start
._unsigned
= value
,
825 .range_end
._unsigned
= value
,
828 g_ptr_array_foreach(enumeration_type
->entries
,
829 check_ranges_overlap_unsigned
,
831 if (!query
.overlaps
) {
835 name
= g_quark_to_string(query
.mapping_name
);
840 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
841 struct bt_ctf_field_type_enumeration
*enumeration_type
,
844 const char *name
= NULL
;
845 struct range_overlap_query query
=
846 (struct range_overlap_query
) {
847 .range_start
._signed
= value
,
848 .range_end
._signed
= value
,
851 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
853 if (!query
.overlaps
) {
857 name
= g_quark_to_string(query
.mapping_name
);
862 int bt_ctf_field_type_enumeration_get_mapping_count(
863 struct bt_ctf_field_type
*type
)
866 struct bt_ctf_field_type_enumeration
*enumeration
;
868 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
873 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
875 ret
= (int) enumeration
->entries
->len
;
881 struct enumeration_mapping
*get_enumeration_mapping(
882 struct bt_ctf_field_type
*type
, int index
)
884 struct enumeration_mapping
*mapping
= NULL
;
885 struct bt_ctf_field_type_enumeration
*enumeration
;
887 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
889 if (index
>= enumeration
->entries
->len
) {
893 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
898 int bt_ctf_field_type_enumeration_get_mapping(
899 struct bt_ctf_field_type
*type
, int index
,
900 const char **string
, int64_t *range_start
, int64_t *range_end
)
902 struct enumeration_mapping
*mapping
;
905 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
906 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
911 mapping
= get_enumeration_mapping(type
, index
);
917 *string
= g_quark_to_string(mapping
->string
);
918 *range_start
= mapping
->range_start
._signed
;
919 *range_end
= mapping
->range_end
._signed
;
924 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
925 struct bt_ctf_field_type
*type
, int index
,
926 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
928 struct enumeration_mapping
*mapping
;
931 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
932 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
937 mapping
= get_enumeration_mapping(type
, index
);
943 *string
= g_quark_to_string(mapping
->string
);
944 *range_start
= mapping
->range_start
._unsigned
;
945 *range_end
= mapping
->range_end
._unsigned
;
950 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
951 struct bt_ctf_field_type
*type
, const char *name
)
954 struct bt_ctf_field_type_enumeration
*enumeration
;
957 if (!type
|| !name
||
958 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
963 name_quark
= g_quark_try_string(name
);
969 enumeration
= container_of(type
,
970 struct bt_ctf_field_type_enumeration
, parent
);
971 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
972 struct enumeration_mapping
*mapping
=
973 get_enumeration_mapping(type
, i
);
975 if (mapping
->string
== name_quark
) {
986 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
987 struct bt_ctf_field_type
*type
, int64_t value
)
989 struct bt_ctf_field_type_enumeration
*enumeration
;
992 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
997 enumeration
= container_of(type
,
998 struct bt_ctf_field_type_enumeration
, parent
);
999 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1000 struct enumeration_mapping
*mapping
=
1001 get_enumeration_mapping(type
, i
);
1003 if (value
>= mapping
->range_start
._signed
&&
1004 value
<= mapping
->range_end
._signed
) {
1015 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1016 struct bt_ctf_field_type
*type
, uint64_t value
)
1018 struct bt_ctf_field_type_enumeration
*enumeration
;
1021 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1026 enumeration
= container_of(type
,
1027 struct bt_ctf_field_type_enumeration
, parent
);
1028 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1029 struct enumeration_mapping
*mapping
=
1030 get_enumeration_mapping(type
, i
);
1032 if (value
>= mapping
->range_start
._unsigned
&&
1033 value
<= mapping
->range_end
._unsigned
) {
1044 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1046 struct bt_ctf_field_type_floating_point
*floating_point
=
1047 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1049 if (!floating_point
) {
1053 floating_point
->declaration
.sign
= &floating_point
->sign
;
1054 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1055 floating_point
->declaration
.exp
= &floating_point
->exp
;
1056 floating_point
->sign
.len
= 1;
1057 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1058 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1059 floating_point
->declaration
.exp
->len
=
1060 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1061 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1062 floating_point
->sign
.p
.alignment
= 1;
1063 floating_point
->mantissa
.p
.alignment
= 1;
1064 floating_point
->exp
.p
.alignment
= 1;
1066 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1068 return floating_point
? &floating_point
->parent
: NULL
;
1071 int bt_ctf_field_type_floating_point_get_exponent_digits(
1072 struct bt_ctf_field_type
*type
)
1075 struct bt_ctf_field_type_floating_point
*floating_point
;
1077 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1082 floating_point
= container_of(type
,
1083 struct bt_ctf_field_type_floating_point
, parent
);
1084 ret
= (int) floating_point
->declaration
.exp
->len
;
1089 int bt_ctf_field_type_floating_point_set_exponent_digits(
1090 struct bt_ctf_field_type
*type
,
1091 unsigned int exponent_digits
)
1094 struct bt_ctf_field_type_floating_point
*floating_point
;
1096 if (!type
|| type
->frozen
||
1097 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1102 floating_point
= container_of(type
,
1103 struct bt_ctf_field_type_floating_point
, parent
);
1104 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1105 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1107 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1112 floating_point
->declaration
.exp
->len
= exponent_digits
;
1117 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1118 struct bt_ctf_field_type
*type
)
1121 struct bt_ctf_field_type_floating_point
*floating_point
;
1123 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1128 floating_point
= container_of(type
,
1129 struct bt_ctf_field_type_floating_point
, parent
);
1130 ret
= (int) floating_point
->mantissa
.len
+ 1;
1135 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1136 struct bt_ctf_field_type
*type
,
1137 unsigned int mantissa_digits
)
1140 struct bt_ctf_field_type_floating_point
*floating_point
;
1142 if (!type
|| type
->frozen
||
1143 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1148 floating_point
= container_of(type
,
1149 struct bt_ctf_field_type_floating_point
, parent
);
1151 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1152 (mantissa_digits
!= DBL_MANT_DIG
) &&
1153 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1158 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1163 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1165 struct bt_ctf_field_type_structure
*structure
=
1166 g_new0(struct bt_ctf_field_type_structure
, 1);
1172 structure
->parent
.declaration
= &structure
->declaration
.p
;
1173 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1174 structure
->fields
= g_ptr_array_new_with_free_func(
1175 (GDestroyNotify
)destroy_structure_field
);
1176 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1177 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1178 return &structure
->parent
;
1183 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1184 struct bt_ctf_field_type
*field_type
,
1185 const char *field_name
)
1188 struct bt_ctf_field_type_structure
*structure
;
1190 if (!type
|| !field_type
|| type
->frozen
||
1191 bt_ctf_validate_identifier(field_name
) ||
1192 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1193 bt_ctf_field_type_validate(field_type
)) {
1198 structure
= container_of(type
,
1199 struct bt_ctf_field_type_structure
, parent
);
1200 if (add_structure_field(structure
->fields
,
1201 structure
->field_name_to_index
, field_type
, field_name
)) {
1209 int bt_ctf_field_type_structure_get_field_count(
1210 struct bt_ctf_field_type
*type
)
1213 struct bt_ctf_field_type_structure
*structure
;
1215 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1220 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1222 ret
= (int) structure
->fields
->len
;
1227 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1228 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1231 struct bt_ctf_field_type_structure
*structure
;
1232 struct structure_field
*field
;
1235 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1240 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1242 if (index
>= structure
->fields
->len
) {
1247 field
= g_ptr_array_index(structure
->fields
, index
);
1249 *field_type
= field
->type
;
1250 bt_get(field
->type
);
1253 *field_name
= g_quark_to_string(field
->name
);
1259 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1260 struct bt_ctf_field_type
*type
,
1265 struct structure_field
*field
;
1266 struct bt_ctf_field_type_structure
*structure
;
1267 struct bt_ctf_field_type
*field_type
= NULL
;
1269 if (!type
|| !name
) {
1273 name_quark
= g_quark_try_string(name
);
1278 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1280 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1281 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1285 field
= structure
->fields
->pdata
[index
];
1286 field_type
= field
->type
;
1292 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1293 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1295 struct bt_ctf_field_type_variant
*variant
= NULL
;
1297 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1301 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1306 variant
->parent
.declaration
= &variant
->declaration
.p
;
1307 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1308 variant
->tag_name
= g_string_new(tag_name
);
1309 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1310 variant
->fields
= g_ptr_array_new_with_free_func(
1311 (GDestroyNotify
) destroy_structure_field
);
1314 variant
->tag
= container_of(enum_tag
,
1315 struct bt_ctf_field_type_enumeration
, parent
);
1318 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1319 /* A variant's alignment is undefined */
1320 variant
->parent
.declaration
->alignment
= 0;
1321 return &variant
->parent
;
1326 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1327 struct bt_ctf_field_type
*type
)
1329 struct bt_ctf_field_type_variant
*variant
;
1330 struct bt_ctf_field_type
*tag_type
= NULL
;
1332 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1336 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1337 if (!variant
->tag
) {
1341 tag_type
= &variant
->tag
->parent
;
1347 const char *bt_ctf_field_type_variant_get_tag_name(
1348 struct bt_ctf_field_type
*type
)
1350 struct bt_ctf_field_type_variant
*variant
;
1351 const char *tag_name
= NULL
;
1353 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1357 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1358 if (variant
->tag_name
->len
== 0) {
1362 tag_name
= variant
->tag_name
->str
;
1367 int bt_ctf_field_type_variant_set_tag_name(
1368 struct bt_ctf_field_type
*type
, const char *name
)
1371 struct bt_ctf_field_type_variant
*variant
;
1373 if (!type
|| type
->frozen
||
1374 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1375 bt_ctf_validate_identifier(name
)) {
1380 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1381 g_string_assign(variant
->tag_name
, name
);
1386 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1387 struct bt_ctf_field_type
*field_type
,
1388 const char *field_name
)
1392 struct bt_ctf_field_type_variant
*variant
;
1393 GQuark field_name_quark
= g_quark_from_string(field_name
);
1395 if (!type
|| !field_type
|| type
->frozen
||
1396 bt_ctf_validate_identifier(field_name
) ||
1397 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1398 bt_ctf_field_type_validate(field_type
)) {
1403 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1405 /* The user has explicitly provided a tag; validate against it. */
1409 /* Make sure this name is present in the enum tag */
1410 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1411 struct enumeration_mapping
*mapping
=
1412 g_ptr_array_index(variant
->tag
->entries
, i
);
1414 if (mapping
->string
== field_name_quark
) {
1421 /* Validation failed */
1427 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1428 field_type
, field_name
)) {
1436 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1437 struct bt_ctf_field_type
*type
,
1438 const char *field_name
)
1442 struct structure_field
*field
;
1443 struct bt_ctf_field_type_variant
*variant
;
1444 struct bt_ctf_field_type
*field_type
= NULL
;
1446 if (!type
|| !field_name
) {
1450 name_quark
= g_quark_try_string(field_name
);
1455 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1456 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1457 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1461 field
= g_ptr_array_index(variant
->fields
, index
);
1462 field_type
= field
->type
;
1468 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1469 struct bt_ctf_field_type
*type
,
1470 struct bt_ctf_field
*tag
)
1472 const char *enum_value
;
1473 struct bt_ctf_field_type
*field_type
= NULL
;
1475 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1479 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1484 /* Already increments field_type's reference count */
1485 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1491 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1494 struct bt_ctf_field_type_variant
*variant
;
1496 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1501 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1503 ret
= (int) variant
->fields
->len
;
1509 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1510 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1513 struct bt_ctf_field_type_variant
*variant
;
1514 struct structure_field
*field
;
1517 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1522 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1524 if (index
>= variant
->fields
->len
) {
1529 field
= g_ptr_array_index(variant
->fields
, index
);
1531 *field_type
= field
->type
;
1532 bt_get(field
->type
);
1535 *field_name
= g_quark_to_string(field
->name
);
1541 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1542 struct bt_ctf_field_type
*element_type
,
1543 unsigned int length
)
1545 struct bt_ctf_field_type_array
*array
= NULL
;
1547 if (!element_type
|| length
== 0 ||
1548 bt_ctf_field_type_validate(element_type
)) {
1552 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1557 array
->parent
.declaration
= &array
->declaration
.p
;
1558 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1560 bt_get(element_type
);
1561 array
->element_type
= element_type
;
1562 array
->length
= length
;
1563 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1564 return &array
->parent
;
1569 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1570 struct bt_ctf_field_type
*type
)
1572 struct bt_ctf_field_type
*ret
= NULL
;
1573 struct bt_ctf_field_type_array
*array
;
1575 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1579 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1580 ret
= array
->element_type
;
1587 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1588 struct bt_ctf_field_type
*element_type
)
1591 struct bt_ctf_field_type_array
*array
;
1593 if (!type
|| !element_type
||
1594 (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1599 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1601 if (array
->element_type
) {
1602 BT_PUT(array
->element_type
);
1605 array
->element_type
= element_type
;
1606 bt_get(array
->element_type
);
1612 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1615 struct bt_ctf_field_type_array
*array
;
1617 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1622 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1623 ret
= (int64_t) array
->length
;
1628 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1629 struct bt_ctf_field_type
*element_type
,
1630 const char *length_field_name
)
1632 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1634 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1635 bt_ctf_field_type_validate(element_type
)) {
1639 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1644 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1645 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1646 bt_get(element_type
);
1647 sequence
->element_type
= element_type
;
1648 sequence
->length_field_name
= g_string_new(length_field_name
);
1649 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1650 return &sequence
->parent
;
1655 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1656 struct bt_ctf_field_type
*type
)
1658 struct bt_ctf_field_type
*ret
= NULL
;
1659 struct bt_ctf_field_type_sequence
*sequence
;
1661 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1665 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1667 ret
= sequence
->element_type
;
1674 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1675 struct bt_ctf_field_type
*element_type
)
1678 struct bt_ctf_field_type_sequence
*sequence
;
1680 if (!type
|| !element_type
||
1681 (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1686 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1688 if (sequence
->element_type
) {
1689 BT_PUT(sequence
->element_type
);
1692 sequence
->element_type
= element_type
;
1693 bt_get(sequence
->element_type
);
1699 const char *bt_ctf_field_type_sequence_get_length_field_name(
1700 struct bt_ctf_field_type
*type
)
1702 const char *ret
= NULL
;
1703 struct bt_ctf_field_type_sequence
*sequence
;
1705 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1709 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1711 ret
= sequence
->length_field_name
->str
;
1716 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1718 struct bt_ctf_field_type_string
*string
=
1719 g_new0(struct bt_ctf_field_type_string
, 1);
1725 string
->parent
.declaration
= &string
->declaration
.p
;
1726 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1727 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1728 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1729 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1730 return &string
->parent
;
1733 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1734 struct bt_ctf_field_type
*type
)
1736 struct bt_ctf_field_type_string
*string
;
1737 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1739 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1743 string
= container_of(type
, struct bt_ctf_field_type_string
,
1745 ret
= string
->declaration
.encoding
;
1750 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1751 enum ctf_string_encoding encoding
)
1754 struct bt_ctf_field_type_string
*string
;
1756 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1757 (encoding
!= CTF_STRING_UTF8
&&
1758 encoding
!= CTF_STRING_ASCII
)) {
1763 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1764 string
->declaration
.encoding
= encoding
;
1769 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1772 enum ctf_type_id type_id
;
1780 ret
= (int) type
->declaration
->alignment
;
1784 type_id
= bt_ctf_field_type_get_type_id(type
);
1786 case CTF_TYPE_SEQUENCE
:
1788 struct bt_ctf_field_type
*element
=
1789 bt_ctf_field_type_sequence_get_element_type(type
);
1796 ret
= bt_ctf_field_type_get_alignment(element
);
1800 case CTF_TYPE_ARRAY
:
1802 struct bt_ctf_field_type
*element
=
1803 bt_ctf_field_type_array_get_element_type(type
);
1810 ret
= bt_ctf_field_type_get_alignment(element
);
1814 case CTF_TYPE_STRUCT
:
1816 int i
, element_count
;
1818 element_count
= bt_ctf_field_type_structure_get_field_count(
1820 if (element_count
< 0) {
1821 ret
= element_count
;
1825 for (i
= 0; i
< element_count
; i
++) {
1826 struct bt_ctf_field_type
*field
;
1827 int field_alignment
;
1829 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
1836 field_alignment
= bt_ctf_field_type_get_alignment(
1839 if (field_alignment
< 0) {
1840 ret
= field_alignment
;
1844 type
->declaration
->alignment
= MAX(field_alignment
,
1845 type
->declaration
->alignment
);
1847 ret
= (int) type
->declaration
->alignment
;
1850 case CTF_TYPE_UNKNOWN
:
1854 ret
= (int) type
->declaration
->alignment
;
1862 int is_power_of_two(unsigned int value
)
1864 return ((value
& (value
- 1)) == 0) && value
> 0;
1867 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1868 unsigned int alignment
)
1871 enum ctf_type_id type_id
;
1873 /* Alignment must be a power of two */
1874 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
1879 type_id
= bt_ctf_field_type_get_type_id(type
);
1880 if (type_id
== CTF_TYPE_UNKNOWN
) {
1885 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1886 alignment
!= CHAR_BIT
) {
1891 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
1892 type_id
== CTF_TYPE_ARRAY
) {
1893 /* Setting an alignment on these types makes no sense */
1898 type
->declaration
->alignment
= alignment
;
1904 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1905 struct bt_ctf_field_type
*type
)
1907 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1913 switch (type
->declaration
->id
) {
1914 case CTF_TYPE_INTEGER
:
1916 struct bt_ctf_field_type_integer
*integer
= container_of(
1917 type
, struct bt_ctf_field_type_integer
, parent
);
1918 ret
= integer
->user_byte_order
;
1921 case CTF_TYPE_FLOAT
:
1923 struct bt_ctf_field_type_floating_point
*floating_point
=
1925 struct bt_ctf_field_type_floating_point
,
1927 ret
= floating_point
->user_byte_order
;
1934 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
1935 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
1936 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
1937 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
1943 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1944 enum bt_ctf_byte_order byte_order
)
1947 int internal_byte_order
;
1948 enum ctf_type_id type_id
;
1950 if (!type
|| type
->frozen
) {
1955 switch (byte_order
) {
1956 case BT_CTF_BYTE_ORDER_NATIVE
:
1957 /* Leave unset. Will be initialized by parent. */
1958 internal_byte_order
= 0;
1960 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1961 internal_byte_order
= LITTLE_ENDIAN
;
1963 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1964 case BT_CTF_BYTE_ORDER_NETWORK
:
1965 internal_byte_order
= BIG_ENDIAN
;
1972 type_id
= type
->declaration
->id
;
1973 if (set_byte_order_funcs
[type_id
]) {
1974 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1980 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1981 struct bt_ctf_field_type
*type
)
1984 return CTF_TYPE_UNKNOWN
;
1987 return type
->declaration
->id
;
1990 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
1992 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
1995 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
1997 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
2000 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2002 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
2005 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2007 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
2010 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2012 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
2015 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2017 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
2020 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2022 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
2025 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2027 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
2030 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2035 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2041 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2051 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2052 struct bt_ctf_field_type_variant
*variant
,
2055 struct bt_ctf_field_type
*type
= NULL
;
2056 GQuark field_name_quark
;
2058 struct structure_field
*field_entry
;
2059 struct range_overlap_query query
= {
2060 .range_start
._signed
= tag_value
,
2061 .range_end
._signed
= tag_value
,
2062 .mapping_name
= 0, .overlaps
= 0};
2064 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2066 if (!query
.overlaps
) {
2070 field_name_quark
= query
.mapping_name
;
2071 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2072 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2076 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2077 type
= field_entry
->type
;
2083 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2084 struct bt_ctf_field_type_variant
*variant
,
2087 struct bt_ctf_field_type
*type
= NULL
;
2088 GQuark field_name_quark
;
2090 struct structure_field
*field_entry
;
2091 struct range_overlap_query query
= {
2092 .range_start
._unsigned
= tag_value
,
2093 .range_end
._unsigned
= tag_value
,
2094 .mapping_name
= 0, .overlaps
= 0};
2096 g_ptr_array_foreach(variant
->tag
->entries
,
2097 check_ranges_overlap_unsigned
,
2099 if (!query
.overlaps
) {
2103 field_name_quark
= query
.mapping_name
;
2104 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2105 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2109 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2110 type
= field_entry
->type
;
2116 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2117 struct metadata_context
*context
)
2121 if (!type
|| !context
) {
2126 ret
= type
->serialize(type
, context
);
2132 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2139 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2140 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2141 set_byte_order_funcs
[type
->declaration
->id
](type
,
2147 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2149 struct bt_ctf_field_type
*copy
= NULL
;
2155 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2161 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2163 struct bt_ctf_field_path
*field_path
= NULL
;
2165 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2170 field_path
->root
= CTF_NODE_UNKNOWN
;
2171 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2172 if (!field_path
->path_indexes
) {
2173 bt_ctf_field_path_destroy(field_path
);
2182 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2183 struct bt_ctf_field_path
*path
)
2185 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2191 new_path
->root
= path
->root
;
2192 g_array_insert_vals(new_path
->path_indexes
, 0,
2193 path
->path_indexes
->data
, path
->path_indexes
->len
);
2199 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2205 if (path
->path_indexes
) {
2206 g_array_free(path
->path_indexes
, TRUE
);
2212 int bt_ctf_field_type_structure_get_field_name_index(
2213 struct bt_ctf_field_type
*type
, const char *name
)
2218 struct bt_ctf_field_type_structure
*structure
;
2220 if (!type
|| !name
||
2221 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2226 name_quark
= g_quark_try_string(name
);
2232 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2234 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2235 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2245 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2246 struct bt_ctf_field_type
*field
, int index
)
2249 struct bt_ctf_field_type_structure
*structure
;
2251 if (!type
|| !field
||
2252 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2257 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2259 if (index
< 0 || index
>= structure
->fields
->len
) {
2265 bt_put(((struct structure_field
*)
2266 g_ptr_array_index(structure
->fields
, index
))->type
);
2267 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2274 int bt_ctf_field_type_variant_get_field_name_index(
2275 struct bt_ctf_field_type
*type
, const char *name
)
2280 struct bt_ctf_field_type_variant
*variant
;
2282 if (!type
|| !name
||
2283 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2288 name_quark
= g_quark_try_string(name
);
2294 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2296 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2297 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2307 int bt_ctf_field_type_sequence_set_length_field_path(
2308 struct bt_ctf_field_type
*type
,
2309 struct bt_ctf_field_path
*path
)
2312 struct bt_ctf_field_type_sequence
*sequence
;
2314 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2319 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2321 if (sequence
->length_field_path
) {
2322 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2324 sequence
->length_field_path
= path
;
2330 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
2331 struct bt_ctf_field_type
*type
)
2333 struct bt_ctf_field_type_sequence
*sequence
;
2335 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2338 return sequence
->length_field_path
;
2342 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2343 struct bt_ctf_field_path
*path
)
2346 struct bt_ctf_field_type_variant
*variant
;
2348 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2353 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2355 if (variant
->tag_path
) {
2356 bt_ctf_field_path_destroy(variant
->tag_path
);
2358 variant
->tag_path
= path
;
2364 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
2365 struct bt_ctf_field_type
*type
)
2367 struct bt_ctf_field_type_variant
*variant
;
2369 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2372 return variant
->tag_path
;
2376 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2377 struct bt_ctf_field_type
*tag
)
2380 struct bt_ctf_field_type_variant
*variant
;
2382 if (!type
|| !tag
|| type
->frozen
||
2383 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2388 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2392 bt_put(&variant
->tag
->parent
);
2394 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2401 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2402 struct bt_ctf_field_type
*field
, int index
)
2405 struct bt_ctf_field_type_variant
*variant
;
2407 if (!type
|| !field
||
2408 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2413 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2415 if (index
< 0 || index
>= variant
->fields
->len
) {
2421 bt_put(((struct structure_field
*)
2422 g_ptr_array_index(variant
->fields
, index
))->type
);
2423 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2430 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2432 struct bt_ctf_field_type_integer
*integer
=
2433 (struct bt_ctf_field_type_integer
*) type
;
2439 bt_put(integer
->mapped_clock
);
2444 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2446 struct bt_ctf_field_type_enumeration
*enumeration
=
2447 (struct bt_ctf_field_type_enumeration
*) type
;
2453 g_ptr_array_free(enumeration
->entries
, TRUE
);
2454 bt_put(enumeration
->container
);
2455 g_free(enumeration
);
2459 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2461 struct bt_ctf_field_type_floating_point
*floating_point
=
2462 (struct bt_ctf_field_type_floating_point
*) type
;
2468 g_free(floating_point
);
2472 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2474 struct bt_ctf_field_type_structure
*structure
=
2475 (struct bt_ctf_field_type_structure
*) type
;
2481 g_ptr_array_free(structure
->fields
, TRUE
);
2482 g_hash_table_destroy(structure
->field_name_to_index
);
2487 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2489 struct bt_ctf_field_type_variant
*variant
=
2490 (struct bt_ctf_field_type_variant
*) type
;
2496 g_ptr_array_free(variant
->fields
, TRUE
);
2497 g_hash_table_destroy(variant
->field_name_to_index
);
2498 g_string_free(variant
->tag_name
, TRUE
);
2499 bt_put(&variant
->tag
->parent
);
2500 bt_ctf_field_path_destroy(variant
->tag_path
);
2505 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2507 struct bt_ctf_field_type_array
*array
=
2508 (struct bt_ctf_field_type_array
*) type
;
2514 bt_put(array
->element_type
);
2519 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2521 struct bt_ctf_field_type_sequence
*sequence
=
2522 (struct bt_ctf_field_type_sequence
*) type
;
2528 bt_put(sequence
->element_type
);
2529 g_string_free(sequence
->length_field_name
, TRUE
);
2530 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2535 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2537 struct bt_ctf_field_type_string
*string
=
2538 (struct bt_ctf_field_type_string
*) type
;
2548 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2554 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2556 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2557 type
, struct bt_ctf_field_type_enumeration
, parent
);
2559 generic_field_type_freeze(type
);
2560 bt_ctf_field_type_freeze(enumeration_type
->container
);
2564 void freeze_structure_field(struct structure_field
*field
)
2566 bt_ctf_field_type_freeze(field
->type
);
2570 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2572 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2573 type
, struct bt_ctf_field_type_structure
, parent
);
2575 /* Cache the alignment */
2576 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2577 generic_field_type_freeze(type
);
2578 g_ptr_array_foreach(structure_type
->fields
,
2579 (GFunc
) freeze_structure_field
, NULL
);
2583 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2585 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2586 type
, struct bt_ctf_field_type_variant
, parent
);
2588 generic_field_type_freeze(type
);
2589 g_ptr_array_foreach(variant_type
->fields
,
2590 (GFunc
) freeze_structure_field
, NULL
);
2594 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2596 struct bt_ctf_field_type_array
*array_type
= container_of(
2597 type
, struct bt_ctf_field_type_array
, parent
);
2599 /* Cache the alignment */
2600 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2601 generic_field_type_freeze(type
);
2602 bt_ctf_field_type_freeze(array_type
->element_type
);
2606 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2608 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2609 type
, struct bt_ctf_field_type_sequence
, parent
);
2611 /* Cache the alignment */
2612 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2613 generic_field_type_freeze(type
);
2614 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2618 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2620 const char *encoding_string
;
2623 case CTF_STRING_NONE
:
2624 encoding_string
= "none";
2626 case CTF_STRING_ASCII
:
2627 encoding_string
= "ASCII";
2629 case CTF_STRING_UTF8
:
2630 encoding_string
= "UTF8";
2633 encoding_string
= "unknown";
2637 return encoding_string
;
2641 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2643 const char *base_string
;
2646 case BT_CTF_INTEGER_BASE_DECIMAL
:
2647 base_string
= "decimal";
2649 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2650 base_string
= "hexadecimal";
2652 case BT_CTF_INTEGER_BASE_OCTAL
:
2653 base_string
= "octal";
2655 case BT_CTF_INTEGER_BASE_BINARY
:
2656 base_string
= "binary";
2659 base_string
= "unknown";
2667 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2668 struct metadata_context
*context
)
2670 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2671 struct bt_ctf_field_type_integer
, parent
);
2674 g_string_append_printf(context
->string
,
2675 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2676 integer
->declaration
.len
, type
->declaration
->alignment
,
2677 (integer
->declaration
.signedness
? "true" : "false"),
2678 get_encoding_string(integer
->declaration
.encoding
),
2679 get_integer_base_string(integer
->declaration
.base
),
2680 get_byte_order_string(integer
->declaration
.byte_order
));
2681 if (integer
->mapped_clock
) {
2682 const char *clock_name
= bt_ctf_clock_get_name(
2683 integer
->mapped_clock
);
2690 g_string_append_printf(context
->string
,
2691 "; map = clock.%s.value", clock_name
);
2694 g_string_append(context
->string
, "; }");
2700 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2701 struct metadata_context
*context
)
2705 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2706 struct bt_ctf_field_type_enumeration
, parent
);
2707 struct bt_ctf_field_type
*container_type
;
2708 int container_signed
;
2710 ret
= bt_ctf_field_type_validate(type
);
2715 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2716 if (!container_type
) {
2721 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2722 if (container_signed
< 0) {
2723 ret
= container_signed
;
2724 goto error_put_container_type
;
2727 g_string_append(context
->string
, "enum : ");
2728 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2730 goto error_put_container_type
;
2733 g_string_append(context
->string
, " { ");
2734 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2735 struct enumeration_mapping
*mapping
=
2736 enumeration
->entries
->pdata
[entry
];
2738 if (container_signed
) {
2739 if (mapping
->range_start
._signed
==
2740 mapping
->range_end
._signed
) {
2741 g_string_append_printf(context
->string
,
2742 "\"%s\" = %" PRId64
,
2743 g_quark_to_string(mapping
->string
),
2744 mapping
->range_start
._signed
);
2746 g_string_append_printf(context
->string
,
2747 "\"%s\" = %" PRId64
" ... %" PRId64
,
2748 g_quark_to_string(mapping
->string
),
2749 mapping
->range_start
._signed
,
2750 mapping
->range_end
._signed
);
2753 if (mapping
->range_start
._unsigned
==
2754 mapping
->range_end
._unsigned
) {
2755 g_string_append_printf(context
->string
,
2756 "\"%s\" = %" PRIu64
,
2757 g_quark_to_string(mapping
->string
),
2758 mapping
->range_start
._unsigned
);
2760 g_string_append_printf(context
->string
,
2761 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2762 g_quark_to_string(mapping
->string
),
2763 mapping
->range_start
._unsigned
,
2764 mapping
->range_end
._unsigned
);
2768 g_string_append(context
->string
,
2769 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2773 if (context
->field_name
->len
) {
2774 g_string_append_printf(context
->string
, " %s",
2775 context
->field_name
->str
);
2776 g_string_assign(context
->field_name
, "");
2778 error_put_container_type
:
2779 bt_put(container_type
);
2785 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2786 struct metadata_context
*context
)
2788 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2789 type
, struct bt_ctf_field_type_floating_point
, parent
);
2791 g_string_append_printf(context
->string
,
2792 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2793 floating_point
->declaration
.exp
->len
,
2794 floating_point
->declaration
.mantissa
->len
+ 1,
2795 get_byte_order_string(floating_point
->declaration
.byte_order
),
2796 type
->declaration
->alignment
);
2801 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2802 struct metadata_context
*context
)
2805 unsigned int indent
;
2807 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2808 struct bt_ctf_field_type_structure
, parent
);
2809 GString
*structure_field_name
= context
->field_name
;
2811 context
->field_name
= g_string_new("");
2813 context
->current_indentation_level
++;
2814 g_string_append(context
->string
, "struct {\n");
2816 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2817 struct structure_field
*field
;
2819 for (indent
= 0; indent
< context
->current_indentation_level
;
2821 g_string_append_c(context
->string
, '\t');
2824 field
= structure
->fields
->pdata
[i
];
2825 g_string_assign(context
->field_name
,
2826 g_quark_to_string(field
->name
));
2827 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2832 if (context
->field_name
->len
) {
2833 g_string_append_printf(context
->string
, " %s",
2834 context
->field_name
->str
);
2836 g_string_append(context
->string
, ";\n");
2839 context
->current_indentation_level
--;
2840 for (indent
= 0; indent
< context
->current_indentation_level
;
2842 g_string_append_c(context
->string
, '\t');
2845 g_string_append_printf(context
->string
, "} align(%zu)",
2846 type
->declaration
->alignment
);
2848 g_string_free(context
->field_name
, TRUE
);
2849 context
->field_name
= structure_field_name
;
2854 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2855 struct metadata_context
*context
)
2858 unsigned int indent
;
2860 struct bt_ctf_field_type_variant
*variant
= container_of(
2861 type
, struct bt_ctf_field_type_variant
, parent
);
2862 GString
*variant_field_name
= context
->field_name
;
2864 context
->field_name
= g_string_new("");
2865 if (variant
->tag_name
->len
> 0) {
2866 g_string_append_printf(context
->string
,
2867 "variant <%s> {\n", variant
->tag_name
->str
);
2869 g_string_append(context
->string
, "variant {\n");
2872 context
->current_indentation_level
++;
2873 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2874 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2876 g_string_assign(context
->field_name
,
2877 g_quark_to_string(field
->name
));
2878 for (indent
= 0; indent
< context
->current_indentation_level
;
2880 g_string_append_c(context
->string
, '\t');
2883 g_string_assign(context
->field_name
,
2884 g_quark_to_string(field
->name
));
2885 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2890 if (context
->field_name
->len
) {
2891 g_string_append_printf(context
->string
, " %s;",
2892 context
->field_name
->str
);
2895 g_string_append_c(context
->string
, '\n');
2898 context
->current_indentation_level
--;
2899 for (indent
= 0; indent
< context
->current_indentation_level
;
2901 g_string_append_c(context
->string
, '\t');
2904 g_string_append(context
->string
, "}");
2906 g_string_free(context
->field_name
, TRUE
);
2907 context
->field_name
= variant_field_name
;
2912 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2913 struct metadata_context
*context
)
2916 struct bt_ctf_field_type_array
*array
= container_of(type
,
2917 struct bt_ctf_field_type_array
, parent
);
2919 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2924 if (context
->field_name
->len
) {
2925 g_string_append_printf(context
->string
, " %s[%u]",
2926 context
->field_name
->str
, array
->length
);
2927 g_string_assign(context
->field_name
, "");
2929 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2936 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2937 struct metadata_context
*context
)
2940 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2941 type
, struct bt_ctf_field_type_sequence
, parent
);
2943 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2948 if (context
->field_name
->len
) {
2949 g_string_append_printf(context
->string
, " %s[%s]",
2950 context
->field_name
->str
,
2951 sequence
->length_field_name
->str
);
2952 g_string_assign(context
->field_name
, "");
2954 g_string_append_printf(context
->string
, "[%s]",
2955 sequence
->length_field_name
->str
);
2962 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2963 struct metadata_context
*context
)
2965 struct bt_ctf_field_type_string
*string
= container_of(
2966 type
, struct bt_ctf_field_type_string
, parent
);
2968 g_string_append_printf(context
->string
,
2969 "string { encoding = %s; }",
2970 get_encoding_string(string
->declaration
.encoding
));
2975 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
2976 enum bt_ctf_byte_order ret
;
2978 switch (byte_order
) {
2979 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2981 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
2983 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2985 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
2987 case BT_CTF_BYTE_ORDER_NETWORK
:
2988 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
2990 case BT_CTF_BYTE_ORDER_NATIVE
:
2991 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
2994 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3002 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3003 int byte_order
, int set_native
)
3005 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3006 struct bt_ctf_field_type_integer
, parent
);
3009 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3011 * User byte order is native, so we can set
3012 * the real byte order.
3014 integer_type
->declaration
.byte_order
=
3018 integer_type
->user_byte_order
=
3019 get_ctf_ir_byte_order(byte_order
);
3020 integer_type
->declaration
.byte_order
= byte_order
;
3025 void bt_ctf_field_type_enumeration_set_byte_order(
3026 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3028 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3029 struct bt_ctf_field_type_enumeration
, parent
);
3031 /* Safe to assume that container is an integer */
3032 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3033 byte_order
, set_native
);
3037 void bt_ctf_field_type_floating_point_set_byte_order(
3038 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3040 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3041 container_of(type
, struct bt_ctf_field_type_floating_point
,
3045 if (floating_point_type
->user_byte_order
==
3046 BT_CTF_BYTE_ORDER_NATIVE
) {
3048 * User byte order is native, so we can set
3049 * the real byte order.
3051 floating_point_type
->declaration
.byte_order
=
3053 floating_point_type
->sign
.byte_order
=
3055 floating_point_type
->mantissa
.byte_order
=
3057 floating_point_type
->exp
.byte_order
=
3061 floating_point_type
->user_byte_order
=
3062 get_ctf_ir_byte_order(byte_order
);
3063 floating_point_type
->declaration
.byte_order
= byte_order
;
3064 floating_point_type
->sign
.byte_order
= byte_order
;
3065 floating_point_type
->mantissa
.byte_order
= byte_order
;
3066 floating_point_type
->exp
.byte_order
= byte_order
;
3071 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3072 int byte_order
, int set_native
)
3075 struct bt_ctf_field_type_structure
*structure_type
=
3076 container_of(type
, struct bt_ctf_field_type_structure
,
3079 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3080 struct structure_field
*field
= g_ptr_array_index(
3081 structure_type
->fields
, i
);
3082 struct bt_ctf_field_type
*field_type
= field
->type
;
3084 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3085 set_byte_order_funcs
[field_type
->declaration
->id
](
3086 field_type
, byte_order
, set_native
);
3092 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3093 int byte_order
, int set_native
)
3096 struct bt_ctf_field_type_variant
*variant_type
=
3097 container_of(type
, struct bt_ctf_field_type_variant
,
3100 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3101 struct structure_field
*field
= g_ptr_array_index(
3102 variant_type
->fields
, i
);
3103 struct bt_ctf_field_type
*field_type
= field
->type
;
3105 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3106 set_byte_order_funcs
[field_type
->declaration
->id
](
3107 field_type
, byte_order
, set_native
);
3113 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3114 int byte_order
, int set_native
)
3116 struct bt_ctf_field_type_array
*array_type
=
3117 container_of(type
, struct bt_ctf_field_type_array
,
3120 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3121 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3122 array_type
->element_type
, byte_order
, set_native
);
3127 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3128 int byte_order
, int set_native
)
3130 struct bt_ctf_field_type_sequence
*sequence_type
=
3131 container_of(type
, struct bt_ctf_field_type_sequence
,
3134 if (set_byte_order_funcs
[
3135 sequence_type
->element_type
->declaration
->id
]) {
3136 set_byte_order_funcs
[
3137 sequence_type
->element_type
->declaration
->id
](
3138 sequence_type
->element_type
, byte_order
, set_native
);
3143 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3144 struct bt_ctf_field_type
*type
)
3146 struct bt_ctf_field_type
*copy
;
3147 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3149 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3150 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3155 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3157 copy_integer
->declaration
= integer
->declaration
;
3158 if (integer
->mapped_clock
) {
3159 bt_get(integer
->mapped_clock
);
3160 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3163 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3170 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3171 struct bt_ctf_field_type
*type
)
3174 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3175 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3177 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3180 /* Copy the source enumeration's container */
3181 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3182 if (!copy_container
) {
3186 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3190 copy_enumeration
= container_of(copy
,
3191 struct bt_ctf_field_type_enumeration
, parent
);
3193 /* Copy all enumaration entries */
3194 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3195 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3196 enumeration
->entries
, i
);
3197 struct enumeration_mapping
* copy_mapping
= g_new0(
3198 struct enumeration_mapping
, 1);
3200 if (!copy_mapping
) {
3204 *copy_mapping
= *mapping
;
3205 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3208 copy_enumeration
->declaration
= enumeration
->declaration
;
3210 bt_put(copy_container
);
3213 bt_put(copy_container
);
3219 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3220 struct bt_ctf_field_type
*type
)
3222 struct bt_ctf_field_type
*copy
;
3223 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3225 floating_point
= container_of(type
,
3226 struct bt_ctf_field_type_floating_point
, parent
);
3227 copy
= bt_ctf_field_type_floating_point_create();
3232 copy_float
= container_of(copy
,
3233 struct bt_ctf_field_type_floating_point
, parent
);
3234 copy_float
->declaration
= floating_point
->declaration
;
3235 copy_float
->sign
= floating_point
->sign
;
3236 copy_float
->mantissa
= floating_point
->mantissa
;
3237 copy_float
->exp
= floating_point
->exp
;
3238 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3239 copy_float
->declaration
.sign
= ©_float
->sign
;
3240 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3241 copy_float
->declaration
.exp
= ©_float
->exp
;
3247 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3248 struct bt_ctf_field_type
*type
)
3251 GHashTableIter iter
;
3252 gpointer key
, value
;
3253 struct bt_ctf_field_type
*copy
;
3254 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3256 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3258 copy
= bt_ctf_field_type_structure_create();
3263 copy_structure
= container_of(copy
,
3264 struct bt_ctf_field_type_structure
, parent
);
3266 /* Copy field_name_to_index */
3267 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3268 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3269 g_hash_table_insert(copy_structure
->field_name_to_index
,
3273 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3274 struct structure_field
*entry
, *copy_entry
;
3275 struct bt_ctf_field_type
*copy_field
;
3277 copy_entry
= g_new0(struct structure_field
, 1);
3282 entry
= g_ptr_array_index(structure
->fields
, i
);
3283 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3289 copy_entry
->name
= entry
->name
;
3290 copy_entry
->type
= copy_field
;
3291 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3294 copy_structure
->declaration
= structure
->declaration
;
3303 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3304 struct bt_ctf_field_type
*type
)
3307 GHashTableIter iter
;
3308 gpointer key
, value
;
3309 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3310 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3312 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3315 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3321 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3322 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3327 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3330 /* Copy field_name_to_index */
3331 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3332 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3333 g_hash_table_insert(copy_variant
->field_name_to_index
,
3337 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3338 struct structure_field
*entry
, *copy_entry
;
3339 struct bt_ctf_field_type
*copy_field
;
3341 copy_entry
= g_new0(struct structure_field
, 1);
3346 entry
= g_ptr_array_index(variant
->fields
, i
);
3347 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3353 copy_entry
->name
= entry
->name
;
3354 copy_entry
->type
= copy_field
;
3355 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3358 copy_variant
->declaration
= variant
->declaration
;
3359 if (variant
->tag_path
) {
3360 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3362 if (!copy_variant
->tag_path
) {
3376 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3377 struct bt_ctf_field_type
*type
)
3379 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3380 struct bt_ctf_field_type_array
*array
, *copy_array
;
3382 array
= container_of(type
, struct bt_ctf_field_type_array
,
3384 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3385 if (!copy_element
) {
3389 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3394 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3396 copy_array
->declaration
= array
->declaration
;
3398 bt_put(copy_element
);
3403 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3404 struct bt_ctf_field_type
*type
)
3406 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3407 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3409 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3411 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3412 if (!copy_element
) {
3416 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3417 sequence
->length_field_name
->len
?
3418 sequence
->length_field_name
->str
: NULL
);
3423 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3425 copy_sequence
->declaration
= sequence
->declaration
;
3426 if (sequence
->length_field_path
) {
3427 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3428 sequence
->length_field_path
);
3429 if (!copy_sequence
->length_field_path
) {
3434 bt_put(copy_element
);
3442 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3443 struct bt_ctf_field_type
*type
)
3445 struct bt_ctf_field_type
*copy
;
3446 struct bt_ctf_field_type_string
*string
, *copy_string
;
3448 copy
= bt_ctf_field_type_string_create();
3453 string
= container_of(type
, struct bt_ctf_field_type_string
,
3455 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3457 copy_string
->declaration
= string
->declaration
;
3463 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3464 struct bt_ctf_field_type
*type_b
)
3467 struct bt_ctf_field_type_integer
*integer_a
;
3468 struct bt_ctf_field_type_integer
*integer_b
;
3469 struct declaration_integer
*decl_a
;
3470 struct declaration_integer
*decl_b
;
3472 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3474 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3476 decl_a
= &integer_a
->declaration
;
3477 decl_b
= &integer_b
->declaration
;
3480 if (decl_a
->len
!= decl_b
->len
) {
3485 * Compare user byte orders only, not the cached,
3488 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3493 if (decl_a
->signedness
!= decl_b
->signedness
) {
3498 if (decl_a
->base
!= decl_b
->base
) {
3503 if (decl_a
->encoding
!= decl_b
->encoding
) {
3508 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3520 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3521 struct bt_ctf_field_type
*type_b
)
3524 struct bt_ctf_field_type_floating_point
*float_a
;
3525 struct bt_ctf_field_type_floating_point
*float_b
;
3527 float_a
= container_of(type_a
,
3528 struct bt_ctf_field_type_floating_point
, parent
);
3529 float_b
= container_of(type_b
,
3530 struct bt_ctf_field_type_floating_point
, parent
);
3533 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3537 /* Exponent length */
3538 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3542 /* Mantissa length */
3543 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3548 * Compare user byte orders only, not the cached,
3551 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3563 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3564 struct enumeration_mapping
*mapping_b
)
3569 if (mapping_a
->string
!= mapping_b
->string
) {
3574 if (mapping_a
->range_start
._unsigned
!=
3575 mapping_b
->range_start
._unsigned
) {
3580 if (mapping_a
->range_end
._unsigned
!=
3581 mapping_b
->range_end
._unsigned
) {
3593 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3594 struct bt_ctf_field_type
*type_b
)
3598 struct bt_ctf_field_type_enumeration
*enum_a
;
3599 struct bt_ctf_field_type_enumeration
*enum_b
;
3601 enum_a
= container_of(type_a
,
3602 struct bt_ctf_field_type_enumeration
, parent
);
3603 enum_b
= container_of(type_b
,
3604 struct bt_ctf_field_type_enumeration
, parent
);
3606 /* Container field type */
3607 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3615 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3619 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3620 struct enumeration_mapping
*mapping_a
=
3621 g_ptr_array_index(enum_a
->entries
, i
);
3622 struct enumeration_mapping
*mapping_b
=
3623 g_ptr_array_index(enum_b
->entries
, i
);
3625 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3638 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3639 struct bt_ctf_field_type
*type_b
)
3642 struct bt_ctf_field_type_string
*string_a
;
3643 struct bt_ctf_field_type_string
*string_b
;
3645 string_a
= container_of(type_a
,
3646 struct bt_ctf_field_type_string
, parent
);
3647 string_b
= container_of(type_b
,
3648 struct bt_ctf_field_type_string
, parent
);
3651 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3663 int compare_structure_fields(struct structure_field
*field_a
,
3664 struct structure_field
*field_b
)
3669 if (field_a
->name
!= field_b
->name
) {
3674 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3681 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3682 struct bt_ctf_field_type
*type_b
)
3686 struct bt_ctf_field_type_structure
*struct_a
;
3687 struct bt_ctf_field_type_structure
*struct_b
;
3689 struct_a
= container_of(type_a
,
3690 struct bt_ctf_field_type_structure
, parent
);
3691 struct_b
= container_of(type_b
,
3692 struct bt_ctf_field_type_structure
, parent
);
3695 if (bt_ctf_field_type_get_alignment(type_a
) !=
3696 bt_ctf_field_type_get_alignment(type_b
)) {
3701 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3705 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3706 struct structure_field
*field_a
=
3707 g_ptr_array_index(struct_a
->fields
, i
);
3708 struct structure_field
*field_b
=
3709 g_ptr_array_index(struct_b
->fields
, i
);
3711 ret
= compare_structure_fields(field_a
, field_b
);
3727 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3728 struct bt_ctf_field_type
*type_b
)
3732 struct bt_ctf_field_type_variant
*variant_a
;
3733 struct bt_ctf_field_type_variant
*variant_b
;
3735 variant_a
= container_of(type_a
,
3736 struct bt_ctf_field_type_variant
, parent
);
3737 variant_b
= container_of(type_b
,
3738 struct bt_ctf_field_type_variant
, parent
);
3741 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3746 ret
= bt_ctf_field_type_compare(
3747 (struct bt_ctf_field_type
*) variant_a
->tag
,
3748 (struct bt_ctf_field_type
*) variant_b
->tag
);
3756 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3760 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3761 struct structure_field
*field_a
=
3762 g_ptr_array_index(variant_a
->fields
, i
);
3763 struct structure_field
*field_b
=
3764 g_ptr_array_index(variant_b
->fields
, i
);
3766 ret
= compare_structure_fields(field_a
, field_b
);
3782 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3783 struct bt_ctf_field_type
*type_b
)
3786 struct bt_ctf_field_type_array
*array_a
;
3787 struct bt_ctf_field_type_array
*array_b
;
3789 array_a
= container_of(type_a
,
3790 struct bt_ctf_field_type_array
, parent
);
3791 array_b
= container_of(type_b
,
3792 struct bt_ctf_field_type_array
, parent
);
3795 if (array_a
->length
!= array_b
->length
) {
3800 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3801 array_b
->element_type
);
3808 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3809 struct bt_ctf_field_type
*type_b
)
3812 struct bt_ctf_field_type_sequence
*sequence_a
;
3813 struct bt_ctf_field_type_sequence
*sequence_b
;
3815 sequence_a
= container_of(type_a
,
3816 struct bt_ctf_field_type_sequence
, parent
);
3817 sequence_b
= container_of(type_b
,
3818 struct bt_ctf_field_type_sequence
, parent
);
3821 if (strcmp(sequence_a
->length_field_name
->str
,
3822 sequence_b
->length_field_name
->str
)) {
3827 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
3828 sequence_b
->element_type
);
3834 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
3835 struct bt_ctf_field_type
*type_b
)
3839 if (type_a
== type_b
) {
3840 /* Same reference: equal (even if both are NULL) */
3845 if (!type_a
|| !type_b
) {
3850 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
3851 /* Different type IDs */
3855 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
3856 /* Both have unknown type IDs */
3860 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);