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 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
273 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
275 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
277 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
279 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
282 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
283 [CTF_TYPE_INTEGER
] = NULL
,
284 [CTF_TYPE_FLOAT
] = NULL
,
285 [CTF_TYPE_STRING
] = NULL
,
286 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_validate
,
287 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_validate
,
288 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_validate
,
289 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_validate
,
290 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
294 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
300 void destroy_structure_field(struct structure_field
*field
)
307 void check_ranges_overlap(gpointer element
, gpointer query
)
309 struct enumeration_mapping
*mapping
= element
;
310 struct range_overlap_query
*overlap_query
= query
;
312 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
313 && overlap_query
->range_start
._signed
<=
314 mapping
->range_end
._signed
) {
315 overlap_query
->overlaps
= 1;
316 overlap_query
->mapping_name
= mapping
->string
;
319 overlap_query
->overlaps
|=
320 mapping
->string
== overlap_query
->mapping_name
;
324 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
326 struct enumeration_mapping
*mapping
= element
;
327 struct range_overlap_query
*overlap_query
= query
;
329 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
330 && overlap_query
->range_start
._unsigned
<=
331 mapping
->range_end
._unsigned
) {
332 overlap_query
->overlaps
= 1;
333 overlap_query
->mapping_name
= mapping
->string
;
336 overlap_query
->overlaps
|=
337 mapping
->string
== overlap_query
->mapping_name
;
341 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
342 struct enumeration_mapping
**b
)
344 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
348 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
349 struct enumeration_mapping
**b
)
351 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
355 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
357 enum ctf_type_id type_id
= type
->declaration
->id
;
359 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
360 (type_id
< NR_CTF_TYPES
));
362 bt_object_init(type
, bt_ctf_field_type_destroy
);
363 type
->freeze
= type_freeze_funcs
[type_id
];
364 type
->serialize
= type_serialize_funcs
[type_id
];
367 int ret
= bt_ctf_field_type_set_byte_order(type
,
368 BT_CTF_BYTE_ORDER_NATIVE
);
372 type
->declaration
->alignment
= 1;
376 int add_structure_field(GPtrArray
*fields
,
377 GHashTable
*field_name_to_index
,
378 struct bt_ctf_field_type
*field_type
,
379 const char *field_name
)
382 GQuark name_quark
= g_quark_from_string(field_name
);
383 struct structure_field
*field
;
385 /* Make sure structure does not contain a field of the same name */
386 if (g_hash_table_lookup_extended(field_name_to_index
,
387 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
392 field
= g_new0(struct structure_field
, 1);
399 field
->name
= name_quark
;
400 field
->type
= field_type
;
401 g_hash_table_insert(field_name_to_index
,
402 (gpointer
) (unsigned long) name_quark
,
403 (gpointer
) (unsigned long) fields
->len
);
404 g_ptr_array_add(fields
, field
);
410 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
412 struct bt_ctf_field_type
*type
;
413 enum ctf_type_id type_id
;
415 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
416 type_id
= type
->declaration
->id
;
417 if (type_id
<= CTF_TYPE_UNKNOWN
||
418 type_id
>= NR_CTF_TYPES
) {
422 type_destroy_funcs
[type_id
](type
);
426 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
430 struct bt_ctf_field_type_enumeration
*enumeration
=
431 container_of(type
, struct bt_ctf_field_type_enumeration
,
433 struct bt_ctf_field_type
*container_type
=
434 bt_ctf_field_type_enumeration_get_container_type(type
);
436 if (!container_type
) {
441 ret
= bt_ctf_field_type_validate(container_type
);
446 /* Ensure enum has entries */
447 ret
= enumeration
->entries
->len
? 0 : -1;
450 BT_PUT(container_type
);
455 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
458 struct bt_ctf_field_type
*element_type
= NULL
;
459 struct bt_ctf_field_type_sequence
*sequence
=
460 container_of(type
, struct bt_ctf_field_type_sequence
,
463 /* Length field name should be set at this point */
464 if (sequence
->length_field_name
->len
== 0) {
469 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
475 ret
= bt_ctf_field_type_validate(element_type
);
478 BT_PUT(element_type
);
484 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
487 struct bt_ctf_field_type
*element_type
= NULL
;
489 element_type
= bt_ctf_field_type_array_get_element_type(type
);
495 ret
= bt_ctf_field_type_validate(element_type
);
498 BT_PUT(element_type
);
504 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
507 struct bt_ctf_field_type
*child_type
= NULL
;
508 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
511 if (field_count
< 0) {
516 for (i
= 0; i
< field_count
; ++i
) {
517 ret
= bt_ctf_field_type_structure_get_field(type
,
518 NULL
, &child_type
, i
);
523 ret
= bt_ctf_field_type_validate(child_type
);
538 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
542 struct bt_ctf_field_type
*child_type
= NULL
;
543 struct bt_ctf_field_type_variant
*variant
=
544 container_of(type
, struct bt_ctf_field_type_variant
,
547 int tag_mappings_count
;
549 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
555 bt_ctf_field_type_enumeration_get_mapping_count(
556 (struct bt_ctf_field_type
*) variant
->tag
);
558 if (tag_mappings_count
!= variant
->fields
->len
) {
563 for (i
= 0; i
< tag_mappings_count
; ++i
) {
565 int64_t range_start
, range_end
;
566 struct bt_ctf_field_type
*ft
;
568 ret
= bt_ctf_field_type_enumeration_get_mapping(
569 (struct bt_ctf_field_type
*) variant
->tag
,
570 i
, &label
, &range_start
, &range_end
);
579 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
589 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
590 if (field_count
< 0) {
595 for (i
= 0; i
< field_count
; ++i
) {
596 ret
= bt_ctf_field_type_variant_get_field(type
,
597 NULL
, &child_type
, i
);
602 ret
= bt_ctf_field_type_validate(child_type
);
617 * This function validates a given field type without considering
618 * where this field type is located. It only validates the properties
619 * of the given field type and the properties of its children if
623 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
626 enum ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
634 /* Already marked as valid */
638 if (type_validate_funcs
[id
]) {
639 ret
= type_validate_funcs
[id
](type
);
642 if (!ret
&& type
->frozen
) {
643 /* Field type is valid */
651 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
653 struct bt_ctf_field_type_integer
*integer
=
654 g_new0(struct bt_ctf_field_type_integer
, 1);
656 if (!integer
|| size
== 0 || size
> 64) {
660 integer
->parent
.declaration
= &integer
->declaration
.p
;
661 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
662 integer
->declaration
.len
= size
;
663 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
664 integer
->declaration
.encoding
= CTF_STRING_NONE
;
665 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
666 return &integer
->parent
;
669 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
672 struct bt_ctf_field_type_integer
*integer
;
674 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
679 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
680 ret
= (int) integer
->declaration
.len
;
685 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
688 struct bt_ctf_field_type_integer
*integer
;
690 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
695 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
696 ret
= integer
->declaration
.signedness
;
701 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
705 struct bt_ctf_field_type_integer
*integer
;
707 if (!type
|| type
->frozen
||
708 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
713 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
714 integer
->declaration
.signedness
= !!is_signed
;
719 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
720 struct bt_ctf_field_type
*type
)
722 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
723 struct bt_ctf_field_type_integer
*integer
;
725 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
729 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
730 ret
= integer
->declaration
.base
;
735 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
736 enum bt_ctf_integer_base base
)
740 if (!type
|| type
->frozen
||
741 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
747 case BT_CTF_INTEGER_BASE_BINARY
:
748 case BT_CTF_INTEGER_BASE_OCTAL
:
749 case BT_CTF_INTEGER_BASE_DECIMAL
:
750 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
752 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
753 struct bt_ctf_field_type_integer
, parent
);
754 integer
->declaration
.base
= base
;
764 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
765 struct bt_ctf_field_type
*type
)
767 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
768 struct bt_ctf_field_type_integer
*integer
;
770 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
774 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
775 ret
= integer
->declaration
.encoding
;
780 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
781 enum ctf_string_encoding encoding
)
784 struct bt_ctf_field_type_integer
*integer
;
786 if (!type
|| type
->frozen
||
787 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
788 (encoding
< CTF_STRING_NONE
) ||
789 (encoding
>= CTF_STRING_UNKNOWN
)) {
794 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
795 integer
->declaration
.encoding
= encoding
;
800 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
801 struct bt_ctf_field_type
*type
)
803 struct bt_ctf_field_type_integer
*integer
;
804 struct bt_ctf_clock
*clock
= NULL
;
810 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
811 clock
= integer
->mapped_clock
;
817 int bt_ctf_field_type_integer_set_mapped_clock(
818 struct bt_ctf_field_type
*type
,
819 struct bt_ctf_clock
*clock
)
821 struct bt_ctf_field_type_integer
*integer
;
824 if (!type
|| type
->frozen
) {
829 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
830 bt_put(integer
->mapped_clock
);
832 integer
->mapped_clock
= clock
;
837 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
838 struct bt_ctf_field_type
*integer_container_type
)
840 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
842 if (!integer_container_type
) {
846 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
850 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
855 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
856 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
857 bt_get(integer_container_type
);
858 enumeration
->container
= integer_container_type
;
859 enumeration
->entries
= g_ptr_array_new_with_free_func(
860 (GDestroyNotify
)destroy_enumeration_mapping
);
861 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
862 return &enumeration
->parent
;
868 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
869 struct bt_ctf_field_type
*type
)
871 struct bt_ctf_field_type
*container_type
= NULL
;
872 struct bt_ctf_field_type_enumeration
*enumeration_type
;
878 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
882 enumeration_type
= container_of(type
,
883 struct bt_ctf_field_type_enumeration
, parent
);
884 container_type
= enumeration_type
->container
;
885 bt_get(container_type
);
887 return container_type
;
890 int bt_ctf_field_type_enumeration_add_mapping(
891 struct bt_ctf_field_type
*type
, const char *string
,
892 int64_t range_start
, int64_t range_end
)
896 struct enumeration_mapping
*mapping
;
897 struct bt_ctf_field_type_enumeration
*enumeration
;
898 struct range_overlap_query query
;
899 char *escaped_string
;
901 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
903 (range_end
< range_start
)) {
908 if (!string
|| strlen(string
) == 0) {
913 escaped_string
= g_strescape(string
, NULL
);
914 if (!escaped_string
) {
919 mapping_name
= g_quark_from_string(escaped_string
);
920 query
= (struct range_overlap_query
) {
921 .range_start
._signed
= range_start
,
922 .range_end
._signed
= range_end
,
923 .mapping_name
= mapping_name
,
925 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
928 /* Check that the range does not overlap with one already present */
929 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
930 if (query
.overlaps
) {
935 mapping
= g_new(struct enumeration_mapping
, 1);
941 *mapping
= (struct enumeration_mapping
) {
942 .range_start
._signed
= range_start
,
943 .range_end
._signed
= range_end
, .string
= mapping_name
};
944 g_ptr_array_add(enumeration
->entries
, mapping
);
945 g_ptr_array_sort(enumeration
->entries
,
946 (GCompareFunc
)compare_enumeration_mappings_signed
);
948 free(escaped_string
);
953 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
954 struct bt_ctf_field_type
*type
, const char *string
,
955 uint64_t range_start
, uint64_t range_end
)
959 struct enumeration_mapping
*mapping
;
960 struct bt_ctf_field_type_enumeration
*enumeration
;
961 struct range_overlap_query query
;
962 char *escaped_string
;
964 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
966 (range_end
< range_start
)) {
971 if (!string
|| strlen(string
) == 0) {
976 escaped_string
= g_strescape(string
, NULL
);
977 if (!escaped_string
) {
982 mapping_name
= g_quark_from_string(escaped_string
);
983 query
= (struct range_overlap_query
) {
984 .range_start
._unsigned
= range_start
,
985 .range_end
._unsigned
= range_end
,
986 .mapping_name
= mapping_name
,
988 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
991 /* Check that the range does not overlap with one already present */
992 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
994 if (query
.overlaps
) {
999 mapping
= g_new(struct enumeration_mapping
, 1);
1005 *mapping
= (struct enumeration_mapping
) {
1006 .range_start
._unsigned
= range_start
,
1007 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1008 g_ptr_array_add(enumeration
->entries
, mapping
);
1009 g_ptr_array_sort(enumeration
->entries
,
1010 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1012 free(escaped_string
);
1017 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1018 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1021 const char *name
= NULL
;
1022 struct range_overlap_query query
=
1023 (struct range_overlap_query
) {
1024 .range_start
._unsigned
= value
,
1025 .range_end
._unsigned
= value
,
1028 g_ptr_array_foreach(enumeration_type
->entries
,
1029 check_ranges_overlap_unsigned
,
1031 if (!query
.overlaps
) {
1035 name
= g_quark_to_string(query
.mapping_name
);
1040 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1041 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1044 const char *name
= NULL
;
1045 struct range_overlap_query query
=
1046 (struct range_overlap_query
) {
1047 .range_start
._signed
= value
,
1048 .range_end
._signed
= value
,
1051 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1053 if (!query
.overlaps
) {
1057 name
= g_quark_to_string(query
.mapping_name
);
1062 int bt_ctf_field_type_enumeration_get_mapping_count(
1063 struct bt_ctf_field_type
*type
)
1066 struct bt_ctf_field_type_enumeration
*enumeration
;
1068 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1073 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1075 ret
= (int) enumeration
->entries
->len
;
1081 struct enumeration_mapping
*get_enumeration_mapping(
1082 struct bt_ctf_field_type
*type
, int index
)
1084 struct enumeration_mapping
*mapping
= NULL
;
1085 struct bt_ctf_field_type_enumeration
*enumeration
;
1087 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1089 if (index
>= enumeration
->entries
->len
) {
1093 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1098 int bt_ctf_field_type_enumeration_get_mapping(
1099 struct bt_ctf_field_type
*type
, int index
,
1100 const char **string
, int64_t *range_start
, int64_t *range_end
)
1102 struct enumeration_mapping
*mapping
;
1105 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1106 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1111 mapping
= get_enumeration_mapping(type
, index
);
1117 *string
= g_quark_to_string(mapping
->string
);
1118 *range_start
= mapping
->range_start
._signed
;
1119 *range_end
= mapping
->range_end
._signed
;
1124 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1125 struct bt_ctf_field_type
*type
, int index
,
1126 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1128 struct enumeration_mapping
*mapping
;
1131 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1132 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1137 mapping
= get_enumeration_mapping(type
, index
);
1143 *string
= g_quark_to_string(mapping
->string
);
1144 *range_start
= mapping
->range_start
._unsigned
;
1145 *range_end
= mapping
->range_end
._unsigned
;
1150 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1151 struct bt_ctf_field_type
*type
, const char *name
)
1154 struct bt_ctf_field_type_enumeration
*enumeration
;
1157 if (!type
|| !name
||
1158 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1163 name_quark
= g_quark_try_string(name
);
1169 enumeration
= container_of(type
,
1170 struct bt_ctf_field_type_enumeration
, parent
);
1171 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1172 struct enumeration_mapping
*mapping
=
1173 get_enumeration_mapping(type
, i
);
1175 if (mapping
->string
== name_quark
) {
1186 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1187 struct bt_ctf_field_type
*type
, int64_t value
)
1189 struct bt_ctf_field_type_enumeration
*enumeration
;
1192 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1197 enumeration
= container_of(type
,
1198 struct bt_ctf_field_type_enumeration
, parent
);
1199 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1200 struct enumeration_mapping
*mapping
=
1201 get_enumeration_mapping(type
, i
);
1203 if (value
>= mapping
->range_start
._signed
&&
1204 value
<= mapping
->range_end
._signed
) {
1215 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1216 struct bt_ctf_field_type
*type
, uint64_t value
)
1218 struct bt_ctf_field_type_enumeration
*enumeration
;
1221 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1226 enumeration
= container_of(type
,
1227 struct bt_ctf_field_type_enumeration
, parent
);
1228 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1229 struct enumeration_mapping
*mapping
=
1230 get_enumeration_mapping(type
, i
);
1232 if (value
>= mapping
->range_start
._unsigned
&&
1233 value
<= mapping
->range_end
._unsigned
) {
1244 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1246 struct bt_ctf_field_type_floating_point
*floating_point
=
1247 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1249 if (!floating_point
) {
1253 floating_point
->declaration
.sign
= &floating_point
->sign
;
1254 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1255 floating_point
->declaration
.exp
= &floating_point
->exp
;
1256 floating_point
->sign
.len
= 1;
1257 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1258 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1259 floating_point
->declaration
.exp
->len
=
1260 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1261 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1262 floating_point
->sign
.p
.alignment
= 1;
1263 floating_point
->mantissa
.p
.alignment
= 1;
1264 floating_point
->exp
.p
.alignment
= 1;
1266 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1268 return floating_point
? &floating_point
->parent
: NULL
;
1271 int bt_ctf_field_type_floating_point_get_exponent_digits(
1272 struct bt_ctf_field_type
*type
)
1275 struct bt_ctf_field_type_floating_point
*floating_point
;
1277 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1282 floating_point
= container_of(type
,
1283 struct bt_ctf_field_type_floating_point
, parent
);
1284 ret
= (int) floating_point
->declaration
.exp
->len
;
1289 int bt_ctf_field_type_floating_point_set_exponent_digits(
1290 struct bt_ctf_field_type
*type
,
1291 unsigned int exponent_digits
)
1294 struct bt_ctf_field_type_floating_point
*floating_point
;
1296 if (!type
|| type
->frozen
||
1297 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1302 floating_point
= container_of(type
,
1303 struct bt_ctf_field_type_floating_point
, parent
);
1304 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1305 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1307 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1312 floating_point
->declaration
.exp
->len
= exponent_digits
;
1317 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1318 struct bt_ctf_field_type
*type
)
1321 struct bt_ctf_field_type_floating_point
*floating_point
;
1323 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1328 floating_point
= container_of(type
,
1329 struct bt_ctf_field_type_floating_point
, parent
);
1330 ret
= (int) floating_point
->mantissa
.len
+ 1;
1335 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1336 struct bt_ctf_field_type
*type
,
1337 unsigned int mantissa_digits
)
1340 struct bt_ctf_field_type_floating_point
*floating_point
;
1342 if (!type
|| type
->frozen
||
1343 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1348 floating_point
= container_of(type
,
1349 struct bt_ctf_field_type_floating_point
, parent
);
1351 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1352 (mantissa_digits
!= DBL_MANT_DIG
) &&
1353 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1358 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1363 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1365 struct bt_ctf_field_type_structure
*structure
=
1366 g_new0(struct bt_ctf_field_type_structure
, 1);
1372 structure
->parent
.declaration
= &structure
->declaration
.p
;
1373 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1374 structure
->fields
= g_ptr_array_new_with_free_func(
1375 (GDestroyNotify
)destroy_structure_field
);
1376 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1377 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1378 return &structure
->parent
;
1383 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1384 struct bt_ctf_field_type
*field_type
,
1385 const char *field_name
)
1388 struct bt_ctf_field_type_structure
*structure
;
1390 if (!type
|| !field_type
|| type
->frozen
||
1391 bt_ctf_validate_identifier(field_name
) ||
1392 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1397 structure
= container_of(type
,
1398 struct bt_ctf_field_type_structure
, parent
);
1399 if (add_structure_field(structure
->fields
,
1400 structure
->field_name_to_index
, field_type
, field_name
)) {
1408 int bt_ctf_field_type_structure_get_field_count(
1409 struct bt_ctf_field_type
*type
)
1412 struct bt_ctf_field_type_structure
*structure
;
1414 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1419 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1421 ret
= (int) structure
->fields
->len
;
1426 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1427 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1430 struct bt_ctf_field_type_structure
*structure
;
1431 struct structure_field
*field
;
1434 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1439 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1441 if (index
>= structure
->fields
->len
) {
1446 field
= g_ptr_array_index(structure
->fields
, index
);
1448 *field_type
= field
->type
;
1449 bt_get(field
->type
);
1452 *field_name
= g_quark_to_string(field
->name
);
1458 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1459 struct bt_ctf_field_type
*type
,
1464 struct structure_field
*field
;
1465 struct bt_ctf_field_type_structure
*structure
;
1466 struct bt_ctf_field_type
*field_type
= NULL
;
1468 if (!type
|| !name
) {
1472 name_quark
= g_quark_try_string(name
);
1477 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1479 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1480 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1484 field
= structure
->fields
->pdata
[index
];
1485 field_type
= field
->type
;
1491 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1492 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1494 struct bt_ctf_field_type_variant
*variant
= NULL
;
1496 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1500 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1505 variant
->parent
.declaration
= &variant
->declaration
.p
;
1506 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1507 variant
->tag_name
= g_string_new(tag_name
);
1508 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1509 variant
->fields
= g_ptr_array_new_with_free_func(
1510 (GDestroyNotify
) destroy_structure_field
);
1513 variant
->tag
= container_of(enum_tag
,
1514 struct bt_ctf_field_type_enumeration
, parent
);
1517 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1518 /* A variant's alignment is undefined */
1519 variant
->parent
.declaration
->alignment
= 0;
1520 return &variant
->parent
;
1525 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1526 struct bt_ctf_field_type
*type
)
1528 struct bt_ctf_field_type_variant
*variant
;
1529 struct bt_ctf_field_type
*tag_type
= NULL
;
1531 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1535 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1536 if (!variant
->tag
) {
1540 tag_type
= &variant
->tag
->parent
;
1546 const char *bt_ctf_field_type_variant_get_tag_name(
1547 struct bt_ctf_field_type
*type
)
1549 struct bt_ctf_field_type_variant
*variant
;
1550 const char *tag_name
= NULL
;
1552 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1556 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1557 if (variant
->tag_name
->len
== 0) {
1561 tag_name
= variant
->tag_name
->str
;
1566 int bt_ctf_field_type_variant_set_tag_name(
1567 struct bt_ctf_field_type
*type
, const char *name
)
1570 struct bt_ctf_field_type_variant
*variant
;
1572 if (!type
|| type
->frozen
||
1573 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1574 bt_ctf_validate_identifier(name
)) {
1579 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1580 g_string_assign(variant
->tag_name
, name
);
1585 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1586 struct bt_ctf_field_type
*field_type
,
1587 const char *field_name
)
1591 struct bt_ctf_field_type_variant
*variant
;
1592 GQuark field_name_quark
= g_quark_from_string(field_name
);
1594 if (!type
|| !field_type
|| type
->frozen
||
1595 bt_ctf_validate_identifier(field_name
) ||
1596 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1601 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1603 /* The user has explicitly provided a tag; validate against it. */
1607 /* Make sure this name is present in the enum tag */
1608 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1609 struct enumeration_mapping
*mapping
=
1610 g_ptr_array_index(variant
->tag
->entries
, i
);
1612 if (mapping
->string
== field_name_quark
) {
1619 /* Validation failed */
1625 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1626 field_type
, field_name
)) {
1634 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1635 struct bt_ctf_field_type
*type
,
1636 const char *field_name
)
1640 struct structure_field
*field
;
1641 struct bt_ctf_field_type_variant
*variant
;
1642 struct bt_ctf_field_type
*field_type
= NULL
;
1644 if (!type
|| !field_name
) {
1648 name_quark
= g_quark_try_string(field_name
);
1653 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1654 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1655 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1659 field
= g_ptr_array_index(variant
->fields
, index
);
1660 field_type
= field
->type
;
1666 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1667 struct bt_ctf_field_type
*type
,
1668 struct bt_ctf_field
*tag
)
1670 const char *enum_value
;
1671 struct bt_ctf_field_type
*field_type
= NULL
;
1673 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1677 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1682 /* Already increments field_type's reference count */
1683 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1689 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1692 struct bt_ctf_field_type_variant
*variant
;
1694 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1699 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1701 ret
= (int) variant
->fields
->len
;
1707 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1708 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1711 struct bt_ctf_field_type_variant
*variant
;
1712 struct structure_field
*field
;
1715 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1720 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1722 if (index
>= variant
->fields
->len
) {
1727 field
= g_ptr_array_index(variant
->fields
, index
);
1729 *field_type
= field
->type
;
1730 bt_get(field
->type
);
1733 *field_name
= g_quark_to_string(field
->name
);
1739 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1740 struct bt_ctf_field_type
*element_type
,
1741 unsigned int length
)
1743 struct bt_ctf_field_type_array
*array
= NULL
;
1745 if (!element_type
|| length
== 0) {
1749 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1754 array
->parent
.declaration
= &array
->declaration
.p
;
1755 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1757 bt_get(element_type
);
1758 array
->element_type
= element_type
;
1759 array
->length
= length
;
1760 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1761 return &array
->parent
;
1766 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1767 struct bt_ctf_field_type
*type
)
1769 struct bt_ctf_field_type
*ret
= NULL
;
1770 struct bt_ctf_field_type_array
*array
;
1772 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1776 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1777 ret
= array
->element_type
;
1784 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1785 struct bt_ctf_field_type
*element_type
)
1788 struct bt_ctf_field_type_array
*array
;
1790 if (!type
|| !element_type
||
1791 (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1796 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1798 if (array
->element_type
) {
1799 BT_PUT(array
->element_type
);
1802 array
->element_type
= element_type
;
1803 bt_get(array
->element_type
);
1809 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1812 struct bt_ctf_field_type_array
*array
;
1814 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1819 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1820 ret
= (int64_t) array
->length
;
1825 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1826 struct bt_ctf_field_type
*element_type
,
1827 const char *length_field_name
)
1829 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1831 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1835 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1840 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1841 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1842 bt_get(element_type
);
1843 sequence
->element_type
= element_type
;
1844 sequence
->length_field_name
= g_string_new(length_field_name
);
1845 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1846 return &sequence
->parent
;
1851 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1852 struct bt_ctf_field_type
*type
)
1854 struct bt_ctf_field_type
*ret
= NULL
;
1855 struct bt_ctf_field_type_sequence
*sequence
;
1857 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1861 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1863 ret
= sequence
->element_type
;
1870 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1871 struct bt_ctf_field_type
*element_type
)
1874 struct bt_ctf_field_type_sequence
*sequence
;
1876 if (!type
|| !element_type
||
1877 (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1882 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1884 if (sequence
->element_type
) {
1885 BT_PUT(sequence
->element_type
);
1888 sequence
->element_type
= element_type
;
1889 bt_get(sequence
->element_type
);
1895 const char *bt_ctf_field_type_sequence_get_length_field_name(
1896 struct bt_ctf_field_type
*type
)
1898 const char *ret
= NULL
;
1899 struct bt_ctf_field_type_sequence
*sequence
;
1901 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1905 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1907 ret
= sequence
->length_field_name
->str
;
1912 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1914 struct bt_ctf_field_type_string
*string
=
1915 g_new0(struct bt_ctf_field_type_string
, 1);
1921 string
->parent
.declaration
= &string
->declaration
.p
;
1922 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1923 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1924 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1925 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1926 return &string
->parent
;
1929 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1930 struct bt_ctf_field_type
*type
)
1932 struct bt_ctf_field_type_string
*string
;
1933 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1935 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1939 string
= container_of(type
, struct bt_ctf_field_type_string
,
1941 ret
= string
->declaration
.encoding
;
1946 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1947 enum ctf_string_encoding encoding
)
1950 struct bt_ctf_field_type_string
*string
;
1952 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1953 (encoding
!= CTF_STRING_UTF8
&&
1954 encoding
!= CTF_STRING_ASCII
)) {
1959 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1960 string
->declaration
.encoding
= encoding
;
1965 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1968 enum ctf_type_id type_id
;
1976 ret
= (int) type
->declaration
->alignment
;
1980 type_id
= bt_ctf_field_type_get_type_id(type
);
1982 case CTF_TYPE_SEQUENCE
:
1984 struct bt_ctf_field_type
*element
=
1985 bt_ctf_field_type_sequence_get_element_type(type
);
1992 ret
= bt_ctf_field_type_get_alignment(element
);
1996 case CTF_TYPE_ARRAY
:
1998 struct bt_ctf_field_type
*element
=
1999 bt_ctf_field_type_array_get_element_type(type
);
2006 ret
= bt_ctf_field_type_get_alignment(element
);
2010 case CTF_TYPE_STRUCT
:
2012 int i
, element_count
;
2014 element_count
= bt_ctf_field_type_structure_get_field_count(
2016 if (element_count
< 0) {
2017 ret
= element_count
;
2021 for (i
= 0; i
< element_count
; i
++) {
2022 struct bt_ctf_field_type
*field
;
2023 int field_alignment
;
2025 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2032 field_alignment
= bt_ctf_field_type_get_alignment(
2035 if (field_alignment
< 0) {
2036 ret
= field_alignment
;
2040 type
->declaration
->alignment
= MAX(field_alignment
,
2041 type
->declaration
->alignment
);
2043 ret
= (int) type
->declaration
->alignment
;
2046 case CTF_TYPE_UNKNOWN
:
2050 ret
= (int) type
->declaration
->alignment
;
2058 int is_power_of_two(unsigned int value
)
2060 return ((value
& (value
- 1)) == 0) && value
> 0;
2063 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2064 unsigned int alignment
)
2067 enum ctf_type_id type_id
;
2069 /* Alignment must be a power of two */
2070 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2075 type_id
= bt_ctf_field_type_get_type_id(type
);
2076 if (type_id
== CTF_TYPE_UNKNOWN
) {
2081 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
2082 alignment
!= CHAR_BIT
) {
2087 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
2088 type_id
== CTF_TYPE_ARRAY
) {
2089 /* Setting an alignment on these types makes no sense */
2094 type
->declaration
->alignment
= alignment
;
2100 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2101 struct bt_ctf_field_type
*type
)
2103 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2109 switch (type
->declaration
->id
) {
2110 case CTF_TYPE_INTEGER
:
2112 struct bt_ctf_field_type_integer
*integer
= container_of(
2113 type
, struct bt_ctf_field_type_integer
, parent
);
2114 ret
= integer
->user_byte_order
;
2117 case CTF_TYPE_FLOAT
:
2119 struct bt_ctf_field_type_floating_point
*floating_point
=
2121 struct bt_ctf_field_type_floating_point
,
2123 ret
= floating_point
->user_byte_order
;
2130 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2131 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2132 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2133 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2139 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2140 enum bt_ctf_byte_order byte_order
)
2143 int internal_byte_order
;
2144 enum ctf_type_id type_id
;
2146 if (!type
|| type
->frozen
) {
2151 switch (byte_order
) {
2152 case BT_CTF_BYTE_ORDER_NATIVE
:
2153 /* Leave unset. Will be initialized by parent. */
2154 internal_byte_order
= 0;
2156 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2157 internal_byte_order
= LITTLE_ENDIAN
;
2159 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2160 case BT_CTF_BYTE_ORDER_NETWORK
:
2161 internal_byte_order
= BIG_ENDIAN
;
2168 type_id
= type
->declaration
->id
;
2169 if (set_byte_order_funcs
[type_id
]) {
2170 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2176 enum ctf_type_id
bt_ctf_field_type_get_type_id(
2177 struct bt_ctf_field_type
*type
)
2180 return CTF_TYPE_UNKNOWN
;
2183 return type
->declaration
->id
;
2186 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2188 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
2191 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2193 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
2196 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2198 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
2201 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2203 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
2206 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2208 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
2211 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2213 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
2216 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2218 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
2221 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2223 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
2226 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2231 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2237 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2247 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2248 struct bt_ctf_field_type_variant
*variant
,
2251 struct bt_ctf_field_type
*type
= NULL
;
2252 GQuark field_name_quark
;
2254 struct structure_field
*field_entry
;
2255 struct range_overlap_query query
= {
2256 .range_start
._signed
= tag_value
,
2257 .range_end
._signed
= tag_value
,
2258 .mapping_name
= 0, .overlaps
= 0};
2260 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2262 if (!query
.overlaps
) {
2266 field_name_quark
= query
.mapping_name
;
2267 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2268 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2272 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2273 type
= field_entry
->type
;
2279 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2280 struct bt_ctf_field_type_variant
*variant
,
2283 struct bt_ctf_field_type
*type
= NULL
;
2284 GQuark field_name_quark
;
2286 struct structure_field
*field_entry
;
2287 struct range_overlap_query query
= {
2288 .range_start
._unsigned
= tag_value
,
2289 .range_end
._unsigned
= tag_value
,
2290 .mapping_name
= 0, .overlaps
= 0};
2292 g_ptr_array_foreach(variant
->tag
->entries
,
2293 check_ranges_overlap_unsigned
,
2295 if (!query
.overlaps
) {
2299 field_name_quark
= query
.mapping_name
;
2300 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2301 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2305 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2306 type
= field_entry
->type
;
2312 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2313 struct metadata_context
*context
)
2317 if (!type
|| !context
) {
2322 /* Make sure field type is valid before serializing it */
2323 ret
= bt_ctf_field_type_validate(type
);
2329 ret
= type
->serialize(type
, context
);
2335 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2342 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2343 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2344 set_byte_order_funcs
[type
->declaration
->id
](type
,
2350 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2352 struct bt_ctf_field_type
*copy
= NULL
;
2358 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2364 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2366 struct bt_ctf_field_path
*field_path
= NULL
;
2368 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2373 field_path
->root
= CTF_NODE_UNKNOWN
;
2374 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2375 if (!field_path
->path_indexes
) {
2376 bt_ctf_field_path_destroy(field_path
);
2384 void bt_ctf_field_path_clear(struct bt_ctf_field_path
*field_path
)
2386 if (field_path
->path_indexes
->len
> 0) {
2387 g_array_remove_range(field_path
->path_indexes
, 0,
2388 field_path
->path_indexes
->len
);
2393 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2394 struct bt_ctf_field_path
*path
)
2396 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2402 new_path
->root
= path
->root
;
2403 g_array_insert_vals(new_path
->path_indexes
, 0,
2404 path
->path_indexes
->data
, path
->path_indexes
->len
);
2410 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2416 if (path
->path_indexes
) {
2417 g_array_free(path
->path_indexes
, TRUE
);
2423 int bt_ctf_field_type_structure_get_field_name_index(
2424 struct bt_ctf_field_type
*type
, const char *name
)
2429 struct bt_ctf_field_type_structure
*structure
;
2431 if (!type
|| !name
||
2432 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2437 name_quark
= g_quark_try_string(name
);
2443 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2445 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2446 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2456 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2457 struct bt_ctf_field_type
*field
, int index
)
2460 struct bt_ctf_field_type_structure
*structure
;
2462 if (!type
|| !field
||
2463 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2468 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2470 if (index
< 0 || index
>= structure
->fields
->len
) {
2476 bt_put(((struct structure_field
*)
2477 g_ptr_array_index(structure
->fields
, index
))->type
);
2478 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2485 int bt_ctf_field_type_variant_get_field_name_index(
2486 struct bt_ctf_field_type
*type
, const char *name
)
2491 struct bt_ctf_field_type_variant
*variant
;
2493 if (!type
|| !name
||
2494 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2499 name_quark
= g_quark_try_string(name
);
2505 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2507 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2508 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2518 int bt_ctf_field_type_sequence_set_length_field_path(
2519 struct bt_ctf_field_type
*type
,
2520 struct bt_ctf_field_path
*path
)
2523 struct bt_ctf_field_type_sequence
*sequence
;
2525 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2530 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2532 if (sequence
->length_field_path
) {
2533 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2535 sequence
->length_field_path
= path
;
2541 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
2542 struct bt_ctf_field_type
*type
)
2544 struct bt_ctf_field_type_sequence
*sequence
;
2546 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2549 return sequence
->length_field_path
;
2553 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2554 struct bt_ctf_field_path
*path
)
2557 struct bt_ctf_field_type_variant
*variant
;
2559 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2564 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2566 if (variant
->tag_path
) {
2567 bt_ctf_field_path_destroy(variant
->tag_path
);
2569 variant
->tag_path
= path
;
2575 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
2576 struct bt_ctf_field_type
*type
)
2578 struct bt_ctf_field_type_variant
*variant
;
2580 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2583 return variant
->tag_path
;
2587 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2588 struct bt_ctf_field_type
*tag
)
2591 struct bt_ctf_field_type_variant
*variant
;
2593 if (!type
|| !tag
|| type
->frozen
||
2594 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2599 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2603 bt_put(&variant
->tag
->parent
);
2605 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2612 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2613 struct bt_ctf_field_type
*field
, int index
)
2616 struct bt_ctf_field_type_variant
*variant
;
2618 if (!type
|| !field
||
2619 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2624 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2626 if (index
< 0 || index
>= variant
->fields
->len
) {
2632 bt_put(((struct structure_field
*)
2633 g_ptr_array_index(variant
->fields
, index
))->type
);
2634 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2641 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2643 struct bt_ctf_field_type_integer
*integer
=
2644 (struct bt_ctf_field_type_integer
*) type
;
2650 bt_put(integer
->mapped_clock
);
2655 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2657 struct bt_ctf_field_type_enumeration
*enumeration
=
2658 (struct bt_ctf_field_type_enumeration
*) type
;
2664 g_ptr_array_free(enumeration
->entries
, TRUE
);
2665 bt_put(enumeration
->container
);
2666 g_free(enumeration
);
2670 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2672 struct bt_ctf_field_type_floating_point
*floating_point
=
2673 (struct bt_ctf_field_type_floating_point
*) type
;
2679 g_free(floating_point
);
2683 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2685 struct bt_ctf_field_type_structure
*structure
=
2686 (struct bt_ctf_field_type_structure
*) type
;
2692 g_ptr_array_free(structure
->fields
, TRUE
);
2693 g_hash_table_destroy(structure
->field_name_to_index
);
2698 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2700 struct bt_ctf_field_type_variant
*variant
=
2701 (struct bt_ctf_field_type_variant
*) type
;
2707 g_ptr_array_free(variant
->fields
, TRUE
);
2708 g_hash_table_destroy(variant
->field_name_to_index
);
2709 g_string_free(variant
->tag_name
, TRUE
);
2710 bt_put(&variant
->tag
->parent
);
2711 bt_ctf_field_path_destroy(variant
->tag_path
);
2716 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2718 struct bt_ctf_field_type_array
*array
=
2719 (struct bt_ctf_field_type_array
*) type
;
2725 bt_put(array
->element_type
);
2730 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2732 struct bt_ctf_field_type_sequence
*sequence
=
2733 (struct bt_ctf_field_type_sequence
*) type
;
2739 bt_put(sequence
->element_type
);
2740 g_string_free(sequence
->length_field_name
, TRUE
);
2741 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2746 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2748 struct bt_ctf_field_type_string
*string
=
2749 (struct bt_ctf_field_type_string
*) type
;
2759 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2765 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2767 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2768 type
, struct bt_ctf_field_type_enumeration
, parent
);
2770 generic_field_type_freeze(type
);
2771 bt_ctf_field_type_freeze(enumeration_type
->container
);
2775 void freeze_structure_field(struct structure_field
*field
)
2777 bt_ctf_field_type_freeze(field
->type
);
2781 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2783 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2784 type
, struct bt_ctf_field_type_structure
, parent
);
2786 /* Cache the alignment */
2787 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2788 generic_field_type_freeze(type
);
2789 g_ptr_array_foreach(structure_type
->fields
,
2790 (GFunc
) freeze_structure_field
, NULL
);
2794 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2796 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2797 type
, struct bt_ctf_field_type_variant
, parent
);
2799 generic_field_type_freeze(type
);
2800 g_ptr_array_foreach(variant_type
->fields
,
2801 (GFunc
) freeze_structure_field
, NULL
);
2805 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2807 struct bt_ctf_field_type_array
*array_type
= container_of(
2808 type
, struct bt_ctf_field_type_array
, parent
);
2810 /* Cache the alignment */
2811 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2812 generic_field_type_freeze(type
);
2813 bt_ctf_field_type_freeze(array_type
->element_type
);
2817 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2819 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2820 type
, struct bt_ctf_field_type_sequence
, parent
);
2822 /* Cache the alignment */
2823 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2824 generic_field_type_freeze(type
);
2825 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2829 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2831 const char *encoding_string
;
2834 case CTF_STRING_NONE
:
2835 encoding_string
= "none";
2837 case CTF_STRING_ASCII
:
2838 encoding_string
= "ASCII";
2840 case CTF_STRING_UTF8
:
2841 encoding_string
= "UTF8";
2844 encoding_string
= "unknown";
2848 return encoding_string
;
2852 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2854 const char *base_string
;
2857 case BT_CTF_INTEGER_BASE_DECIMAL
:
2858 base_string
= "decimal";
2860 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2861 base_string
= "hexadecimal";
2863 case BT_CTF_INTEGER_BASE_OCTAL
:
2864 base_string
= "octal";
2866 case BT_CTF_INTEGER_BASE_BINARY
:
2867 base_string
= "binary";
2870 base_string
= "unknown";
2878 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2879 struct metadata_context
*context
)
2881 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2882 struct bt_ctf_field_type_integer
, parent
);
2885 g_string_append_printf(context
->string
,
2886 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2887 integer
->declaration
.len
, type
->declaration
->alignment
,
2888 (integer
->declaration
.signedness
? "true" : "false"),
2889 get_encoding_string(integer
->declaration
.encoding
),
2890 get_integer_base_string(integer
->declaration
.base
),
2891 get_byte_order_string(integer
->declaration
.byte_order
));
2892 if (integer
->mapped_clock
) {
2893 const char *clock_name
= bt_ctf_clock_get_name(
2894 integer
->mapped_clock
);
2901 g_string_append_printf(context
->string
,
2902 "; map = clock.%s.value", clock_name
);
2905 g_string_append(context
->string
, "; }");
2911 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2912 struct metadata_context
*context
)
2916 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2917 struct bt_ctf_field_type_enumeration
, parent
);
2918 struct bt_ctf_field_type
*container_type
;
2919 int container_signed
;
2921 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2922 if (!container_type
) {
2927 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2928 if (container_signed
< 0) {
2929 ret
= container_signed
;
2930 goto error_put_container_type
;
2933 g_string_append(context
->string
, "enum : ");
2934 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2936 goto error_put_container_type
;
2939 g_string_append(context
->string
, " { ");
2940 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2941 struct enumeration_mapping
*mapping
=
2942 enumeration
->entries
->pdata
[entry
];
2944 if (container_signed
) {
2945 if (mapping
->range_start
._signed
==
2946 mapping
->range_end
._signed
) {
2947 g_string_append_printf(context
->string
,
2948 "\"%s\" = %" PRId64
,
2949 g_quark_to_string(mapping
->string
),
2950 mapping
->range_start
._signed
);
2952 g_string_append_printf(context
->string
,
2953 "\"%s\" = %" PRId64
" ... %" PRId64
,
2954 g_quark_to_string(mapping
->string
),
2955 mapping
->range_start
._signed
,
2956 mapping
->range_end
._signed
);
2959 if (mapping
->range_start
._unsigned
==
2960 mapping
->range_end
._unsigned
) {
2961 g_string_append_printf(context
->string
,
2962 "\"%s\" = %" PRIu64
,
2963 g_quark_to_string(mapping
->string
),
2964 mapping
->range_start
._unsigned
);
2966 g_string_append_printf(context
->string
,
2967 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2968 g_quark_to_string(mapping
->string
),
2969 mapping
->range_start
._unsigned
,
2970 mapping
->range_end
._unsigned
);
2974 g_string_append(context
->string
,
2975 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2979 if (context
->field_name
->len
) {
2980 g_string_append_printf(context
->string
, " %s",
2981 context
->field_name
->str
);
2982 g_string_assign(context
->field_name
, "");
2984 error_put_container_type
:
2985 bt_put(container_type
);
2991 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2992 struct metadata_context
*context
)
2994 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2995 type
, struct bt_ctf_field_type_floating_point
, parent
);
2997 g_string_append_printf(context
->string
,
2998 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2999 floating_point
->declaration
.exp
->len
,
3000 floating_point
->declaration
.mantissa
->len
+ 1,
3001 get_byte_order_string(floating_point
->declaration
.byte_order
),
3002 type
->declaration
->alignment
);
3007 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3008 struct metadata_context
*context
)
3011 unsigned int indent
;
3013 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3014 struct bt_ctf_field_type_structure
, parent
);
3015 GString
*structure_field_name
= context
->field_name
;
3017 context
->field_name
= g_string_new("");
3019 context
->current_indentation_level
++;
3020 g_string_append(context
->string
, "struct {\n");
3022 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3023 struct structure_field
*field
;
3025 for (indent
= 0; indent
< context
->current_indentation_level
;
3027 g_string_append_c(context
->string
, '\t');
3030 field
= structure
->fields
->pdata
[i
];
3031 g_string_assign(context
->field_name
,
3032 g_quark_to_string(field
->name
));
3033 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3038 if (context
->field_name
->len
) {
3039 g_string_append_printf(context
->string
, " %s",
3040 context
->field_name
->str
);
3042 g_string_append(context
->string
, ";\n");
3045 context
->current_indentation_level
--;
3046 for (indent
= 0; indent
< context
->current_indentation_level
;
3048 g_string_append_c(context
->string
, '\t');
3051 g_string_append_printf(context
->string
, "} align(%zu)",
3052 type
->declaration
->alignment
);
3054 g_string_free(context
->field_name
, TRUE
);
3055 context
->field_name
= structure_field_name
;
3060 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3061 struct metadata_context
*context
)
3064 unsigned int indent
;
3066 struct bt_ctf_field_type_variant
*variant
= container_of(
3067 type
, struct bt_ctf_field_type_variant
, parent
);
3068 GString
*variant_field_name
= context
->field_name
;
3070 context
->field_name
= g_string_new("");
3071 if (variant
->tag_name
->len
> 0) {
3072 g_string_append_printf(context
->string
,
3073 "variant <%s> {\n", variant
->tag_name
->str
);
3075 g_string_append(context
->string
, "variant {\n");
3078 context
->current_indentation_level
++;
3079 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3080 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3082 g_string_assign(context
->field_name
,
3083 g_quark_to_string(field
->name
));
3084 for (indent
= 0; indent
< context
->current_indentation_level
;
3086 g_string_append_c(context
->string
, '\t');
3089 g_string_assign(context
->field_name
,
3090 g_quark_to_string(field
->name
));
3091 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3096 if (context
->field_name
->len
) {
3097 g_string_append_printf(context
->string
, " %s;",
3098 context
->field_name
->str
);
3101 g_string_append_c(context
->string
, '\n');
3104 context
->current_indentation_level
--;
3105 for (indent
= 0; indent
< context
->current_indentation_level
;
3107 g_string_append_c(context
->string
, '\t');
3110 g_string_append(context
->string
, "}");
3112 g_string_free(context
->field_name
, TRUE
);
3113 context
->field_name
= variant_field_name
;
3118 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3119 struct metadata_context
*context
)
3122 struct bt_ctf_field_type_array
*array
= container_of(type
,
3123 struct bt_ctf_field_type_array
, parent
);
3125 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3130 if (context
->field_name
->len
) {
3131 g_string_append_printf(context
->string
, " %s[%u]",
3132 context
->field_name
->str
, array
->length
);
3133 g_string_assign(context
->field_name
, "");
3135 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3142 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3143 struct metadata_context
*context
)
3146 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3147 type
, struct bt_ctf_field_type_sequence
, parent
);
3149 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3154 if (context
->field_name
->len
) {
3155 g_string_append_printf(context
->string
, " %s[%s]",
3156 context
->field_name
->str
,
3157 sequence
->length_field_name
->str
);
3158 g_string_assign(context
->field_name
, "");
3160 g_string_append_printf(context
->string
, "[%s]",
3161 sequence
->length_field_name
->str
);
3168 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3169 struct metadata_context
*context
)
3171 struct bt_ctf_field_type_string
*string
= container_of(
3172 type
, struct bt_ctf_field_type_string
, parent
);
3174 g_string_append_printf(context
->string
,
3175 "string { encoding = %s; }",
3176 get_encoding_string(string
->declaration
.encoding
));
3181 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3182 enum bt_ctf_byte_order ret
;
3184 switch (byte_order
) {
3185 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3187 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3189 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3191 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3193 case BT_CTF_BYTE_ORDER_NETWORK
:
3194 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3196 case BT_CTF_BYTE_ORDER_NATIVE
:
3197 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3200 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3208 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3209 int byte_order
, int set_native
)
3211 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3212 struct bt_ctf_field_type_integer
, parent
);
3215 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3217 * User byte order is native, so we can set
3218 * the real byte order.
3220 integer_type
->declaration
.byte_order
=
3224 integer_type
->user_byte_order
=
3225 get_ctf_ir_byte_order(byte_order
);
3226 integer_type
->declaration
.byte_order
= byte_order
;
3231 void bt_ctf_field_type_enumeration_set_byte_order(
3232 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3234 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3235 struct bt_ctf_field_type_enumeration
, parent
);
3237 /* Safe to assume that container is an integer */
3238 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3239 byte_order
, set_native
);
3243 void bt_ctf_field_type_floating_point_set_byte_order(
3244 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3246 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3247 container_of(type
, struct bt_ctf_field_type_floating_point
,
3251 if (floating_point_type
->user_byte_order
==
3252 BT_CTF_BYTE_ORDER_NATIVE
) {
3254 * User byte order is native, so we can set
3255 * the real byte order.
3257 floating_point_type
->declaration
.byte_order
=
3259 floating_point_type
->sign
.byte_order
=
3261 floating_point_type
->mantissa
.byte_order
=
3263 floating_point_type
->exp
.byte_order
=
3267 floating_point_type
->user_byte_order
=
3268 get_ctf_ir_byte_order(byte_order
);
3269 floating_point_type
->declaration
.byte_order
= byte_order
;
3270 floating_point_type
->sign
.byte_order
= byte_order
;
3271 floating_point_type
->mantissa
.byte_order
= byte_order
;
3272 floating_point_type
->exp
.byte_order
= byte_order
;
3277 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3278 int byte_order
, int set_native
)
3281 struct bt_ctf_field_type_structure
*structure_type
=
3282 container_of(type
, struct bt_ctf_field_type_structure
,
3285 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3286 struct structure_field
*field
= g_ptr_array_index(
3287 structure_type
->fields
, i
);
3288 struct bt_ctf_field_type
*field_type
= field
->type
;
3290 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3291 set_byte_order_funcs
[field_type
->declaration
->id
](
3292 field_type
, byte_order
, set_native
);
3298 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3299 int byte_order
, int set_native
)
3302 struct bt_ctf_field_type_variant
*variant_type
=
3303 container_of(type
, struct bt_ctf_field_type_variant
,
3306 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3307 struct structure_field
*field
= g_ptr_array_index(
3308 variant_type
->fields
, i
);
3309 struct bt_ctf_field_type
*field_type
= field
->type
;
3311 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3312 set_byte_order_funcs
[field_type
->declaration
->id
](
3313 field_type
, byte_order
, set_native
);
3319 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3320 int byte_order
, int set_native
)
3322 struct bt_ctf_field_type_array
*array_type
=
3323 container_of(type
, struct bt_ctf_field_type_array
,
3326 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3327 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3328 array_type
->element_type
, byte_order
, set_native
);
3333 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3334 int byte_order
, int set_native
)
3336 struct bt_ctf_field_type_sequence
*sequence_type
=
3337 container_of(type
, struct bt_ctf_field_type_sequence
,
3340 if (set_byte_order_funcs
[
3341 sequence_type
->element_type
->declaration
->id
]) {
3342 set_byte_order_funcs
[
3343 sequence_type
->element_type
->declaration
->id
](
3344 sequence_type
->element_type
, byte_order
, set_native
);
3349 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3350 struct bt_ctf_field_type
*type
)
3352 struct bt_ctf_field_type
*copy
;
3353 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3355 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3356 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3361 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3363 copy_integer
->declaration
= integer
->declaration
;
3364 if (integer
->mapped_clock
) {
3365 bt_get(integer
->mapped_clock
);
3366 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3369 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3376 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3377 struct bt_ctf_field_type
*type
)
3380 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3381 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3383 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3386 /* Copy the source enumeration's container */
3387 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3388 if (!copy_container
) {
3392 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3396 copy_enumeration
= container_of(copy
,
3397 struct bt_ctf_field_type_enumeration
, parent
);
3399 /* Copy all enumaration entries */
3400 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3401 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3402 enumeration
->entries
, i
);
3403 struct enumeration_mapping
* copy_mapping
= g_new0(
3404 struct enumeration_mapping
, 1);
3406 if (!copy_mapping
) {
3410 *copy_mapping
= *mapping
;
3411 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3414 copy_enumeration
->declaration
= enumeration
->declaration
;
3416 bt_put(copy_container
);
3419 bt_put(copy_container
);
3425 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3426 struct bt_ctf_field_type
*type
)
3428 struct bt_ctf_field_type
*copy
;
3429 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3431 floating_point
= container_of(type
,
3432 struct bt_ctf_field_type_floating_point
, parent
);
3433 copy
= bt_ctf_field_type_floating_point_create();
3438 copy_float
= container_of(copy
,
3439 struct bt_ctf_field_type_floating_point
, parent
);
3440 copy_float
->declaration
= floating_point
->declaration
;
3441 copy_float
->sign
= floating_point
->sign
;
3442 copy_float
->mantissa
= floating_point
->mantissa
;
3443 copy_float
->exp
= floating_point
->exp
;
3444 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3445 copy_float
->declaration
.sign
= ©_float
->sign
;
3446 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3447 copy_float
->declaration
.exp
= ©_float
->exp
;
3453 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3454 struct bt_ctf_field_type
*type
)
3457 GHashTableIter iter
;
3458 gpointer key
, value
;
3459 struct bt_ctf_field_type
*copy
;
3460 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3462 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3464 copy
= bt_ctf_field_type_structure_create();
3469 copy_structure
= container_of(copy
,
3470 struct bt_ctf_field_type_structure
, parent
);
3472 /* Copy field_name_to_index */
3473 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3474 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3475 g_hash_table_insert(copy_structure
->field_name_to_index
,
3479 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3480 struct structure_field
*entry
, *copy_entry
;
3481 struct bt_ctf_field_type
*copy_field
;
3483 copy_entry
= g_new0(struct structure_field
, 1);
3488 entry
= g_ptr_array_index(structure
->fields
, i
);
3489 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3495 copy_entry
->name
= entry
->name
;
3496 copy_entry
->type
= copy_field
;
3497 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3500 copy_structure
->declaration
= structure
->declaration
;
3509 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3510 struct bt_ctf_field_type
*type
)
3513 GHashTableIter iter
;
3514 gpointer key
, value
;
3515 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3516 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3518 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3521 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3527 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3528 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3533 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3536 /* Copy field_name_to_index */
3537 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3538 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3539 g_hash_table_insert(copy_variant
->field_name_to_index
,
3543 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3544 struct structure_field
*entry
, *copy_entry
;
3545 struct bt_ctf_field_type
*copy_field
;
3547 copy_entry
= g_new0(struct structure_field
, 1);
3552 entry
= g_ptr_array_index(variant
->fields
, i
);
3553 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3559 copy_entry
->name
= entry
->name
;
3560 copy_entry
->type
= copy_field
;
3561 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3564 copy_variant
->declaration
= variant
->declaration
;
3565 if (variant
->tag_path
) {
3566 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3568 if (!copy_variant
->tag_path
) {
3582 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3583 struct bt_ctf_field_type
*type
)
3585 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3586 struct bt_ctf_field_type_array
*array
, *copy_array
;
3588 array
= container_of(type
, struct bt_ctf_field_type_array
,
3590 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3591 if (!copy_element
) {
3595 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3600 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3602 copy_array
->declaration
= array
->declaration
;
3604 bt_put(copy_element
);
3609 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3610 struct bt_ctf_field_type
*type
)
3612 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3613 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3615 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3617 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3618 if (!copy_element
) {
3622 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3623 sequence
->length_field_name
->len
?
3624 sequence
->length_field_name
->str
: NULL
);
3629 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3631 copy_sequence
->declaration
= sequence
->declaration
;
3632 if (sequence
->length_field_path
) {
3633 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3634 sequence
->length_field_path
);
3635 if (!copy_sequence
->length_field_path
) {
3640 bt_put(copy_element
);
3648 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3649 struct bt_ctf_field_type
*type
)
3651 struct bt_ctf_field_type
*copy
;
3652 struct bt_ctf_field_type_string
*string
, *copy_string
;
3654 copy
= bt_ctf_field_type_string_create();
3659 string
= container_of(type
, struct bt_ctf_field_type_string
,
3661 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3663 copy_string
->declaration
= string
->declaration
;
3669 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3670 struct bt_ctf_field_type
*type_b
)
3673 struct bt_ctf_field_type_integer
*integer_a
;
3674 struct bt_ctf_field_type_integer
*integer_b
;
3675 struct declaration_integer
*decl_a
;
3676 struct declaration_integer
*decl_b
;
3678 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3680 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3682 decl_a
= &integer_a
->declaration
;
3683 decl_b
= &integer_b
->declaration
;
3686 if (decl_a
->len
!= decl_b
->len
) {
3691 * Compare user byte orders only, not the cached,
3694 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3699 if (decl_a
->signedness
!= decl_b
->signedness
) {
3704 if (decl_a
->base
!= decl_b
->base
) {
3709 if (decl_a
->encoding
!= decl_b
->encoding
) {
3714 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3726 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3727 struct bt_ctf_field_type
*type_b
)
3730 struct bt_ctf_field_type_floating_point
*float_a
;
3731 struct bt_ctf_field_type_floating_point
*float_b
;
3733 float_a
= container_of(type_a
,
3734 struct bt_ctf_field_type_floating_point
, parent
);
3735 float_b
= container_of(type_b
,
3736 struct bt_ctf_field_type_floating_point
, parent
);
3739 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3743 /* Exponent length */
3744 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3748 /* Mantissa length */
3749 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3754 * Compare user byte orders only, not the cached,
3757 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3769 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3770 struct enumeration_mapping
*mapping_b
)
3775 if (mapping_a
->string
!= mapping_b
->string
) {
3780 if (mapping_a
->range_start
._unsigned
!=
3781 mapping_b
->range_start
._unsigned
) {
3786 if (mapping_a
->range_end
._unsigned
!=
3787 mapping_b
->range_end
._unsigned
) {
3799 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3800 struct bt_ctf_field_type
*type_b
)
3804 struct bt_ctf_field_type_enumeration
*enum_a
;
3805 struct bt_ctf_field_type_enumeration
*enum_b
;
3807 enum_a
= container_of(type_a
,
3808 struct bt_ctf_field_type_enumeration
, parent
);
3809 enum_b
= container_of(type_b
,
3810 struct bt_ctf_field_type_enumeration
, parent
);
3812 /* Container field type */
3813 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3821 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3825 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3826 struct enumeration_mapping
*mapping_a
=
3827 g_ptr_array_index(enum_a
->entries
, i
);
3828 struct enumeration_mapping
*mapping_b
=
3829 g_ptr_array_index(enum_b
->entries
, i
);
3831 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3844 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3845 struct bt_ctf_field_type
*type_b
)
3848 struct bt_ctf_field_type_string
*string_a
;
3849 struct bt_ctf_field_type_string
*string_b
;
3851 string_a
= container_of(type_a
,
3852 struct bt_ctf_field_type_string
, parent
);
3853 string_b
= container_of(type_b
,
3854 struct bt_ctf_field_type_string
, parent
);
3857 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3869 int compare_structure_fields(struct structure_field
*field_a
,
3870 struct structure_field
*field_b
)
3875 if (field_a
->name
!= field_b
->name
) {
3880 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3887 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3888 struct bt_ctf_field_type
*type_b
)
3892 struct bt_ctf_field_type_structure
*struct_a
;
3893 struct bt_ctf_field_type_structure
*struct_b
;
3895 struct_a
= container_of(type_a
,
3896 struct bt_ctf_field_type_structure
, parent
);
3897 struct_b
= container_of(type_b
,
3898 struct bt_ctf_field_type_structure
, parent
);
3901 if (bt_ctf_field_type_get_alignment(type_a
) !=
3902 bt_ctf_field_type_get_alignment(type_b
)) {
3907 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3911 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3912 struct structure_field
*field_a
=
3913 g_ptr_array_index(struct_a
->fields
, i
);
3914 struct structure_field
*field_b
=
3915 g_ptr_array_index(struct_b
->fields
, i
);
3917 ret
= compare_structure_fields(field_a
, field_b
);
3933 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3934 struct bt_ctf_field_type
*type_b
)
3938 struct bt_ctf_field_type_variant
*variant_a
;
3939 struct bt_ctf_field_type_variant
*variant_b
;
3941 variant_a
= container_of(type_a
,
3942 struct bt_ctf_field_type_variant
, parent
);
3943 variant_b
= container_of(type_b
,
3944 struct bt_ctf_field_type_variant
, parent
);
3947 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3952 ret
= bt_ctf_field_type_compare(
3953 (struct bt_ctf_field_type
*) variant_a
->tag
,
3954 (struct bt_ctf_field_type
*) variant_b
->tag
);
3962 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3966 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3967 struct structure_field
*field_a
=
3968 g_ptr_array_index(variant_a
->fields
, i
);
3969 struct structure_field
*field_b
=
3970 g_ptr_array_index(variant_b
->fields
, i
);
3972 ret
= compare_structure_fields(field_a
, field_b
);
3988 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3989 struct bt_ctf_field_type
*type_b
)
3992 struct bt_ctf_field_type_array
*array_a
;
3993 struct bt_ctf_field_type_array
*array_b
;
3995 array_a
= container_of(type_a
,
3996 struct bt_ctf_field_type_array
, parent
);
3997 array_b
= container_of(type_b
,
3998 struct bt_ctf_field_type_array
, parent
);
4001 if (array_a
->length
!= array_b
->length
) {
4006 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4007 array_b
->element_type
);
4014 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4015 struct bt_ctf_field_type
*type_b
)
4018 struct bt_ctf_field_type_sequence
*sequence_a
;
4019 struct bt_ctf_field_type_sequence
*sequence_b
;
4021 sequence_a
= container_of(type_a
,
4022 struct bt_ctf_field_type_sequence
, parent
);
4023 sequence_b
= container_of(type_b
,
4024 struct bt_ctf_field_type_sequence
, parent
);
4027 if (strcmp(sequence_a
->length_field_name
->str
,
4028 sequence_b
->length_field_name
->str
)) {
4033 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4034 sequence_b
->element_type
);
4040 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4041 struct bt_ctf_field_type
*type_b
)
4045 if (type_a
== type_b
) {
4046 /* Same reference: equal (even if both are NULL) */
4051 if (!type_a
|| !type_b
) {
4056 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4057 /* Different type IDs */
4061 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
4062 /* Both have unknown type IDs */
4066 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);