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
);
2385 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2386 struct bt_ctf_field_path
*path
)
2388 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2394 new_path
->root
= path
->root
;
2395 g_array_insert_vals(new_path
->path_indexes
, 0,
2396 path
->path_indexes
->data
, path
->path_indexes
->len
);
2402 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2408 if (path
->path_indexes
) {
2409 g_array_free(path
->path_indexes
, TRUE
);
2415 int bt_ctf_field_type_structure_get_field_name_index(
2416 struct bt_ctf_field_type
*type
, const char *name
)
2421 struct bt_ctf_field_type_structure
*structure
;
2423 if (!type
|| !name
||
2424 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2429 name_quark
= g_quark_try_string(name
);
2435 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2437 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2438 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2448 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2449 struct bt_ctf_field_type
*field
, int index
)
2452 struct bt_ctf_field_type_structure
*structure
;
2454 if (!type
|| !field
||
2455 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2460 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2462 if (index
< 0 || index
>= structure
->fields
->len
) {
2468 bt_put(((struct structure_field
*)
2469 g_ptr_array_index(structure
->fields
, index
))->type
);
2470 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2477 int bt_ctf_field_type_variant_get_field_name_index(
2478 struct bt_ctf_field_type
*type
, const char *name
)
2483 struct bt_ctf_field_type_variant
*variant
;
2485 if (!type
|| !name
||
2486 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2491 name_quark
= g_quark_try_string(name
);
2497 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2499 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2500 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2510 int bt_ctf_field_type_sequence_set_length_field_path(
2511 struct bt_ctf_field_type
*type
,
2512 struct bt_ctf_field_path
*path
)
2515 struct bt_ctf_field_type_sequence
*sequence
;
2517 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2522 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2524 if (sequence
->length_field_path
) {
2525 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2527 sequence
->length_field_path
= path
;
2533 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
2534 struct bt_ctf_field_type
*type
)
2536 struct bt_ctf_field_type_sequence
*sequence
;
2538 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2541 return sequence
->length_field_path
;
2545 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2546 struct bt_ctf_field_path
*path
)
2549 struct bt_ctf_field_type_variant
*variant
;
2551 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2556 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2558 if (variant
->tag_path
) {
2559 bt_ctf_field_path_destroy(variant
->tag_path
);
2561 variant
->tag_path
= path
;
2567 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
2568 struct bt_ctf_field_type
*type
)
2570 struct bt_ctf_field_type_variant
*variant
;
2572 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2575 return variant
->tag_path
;
2579 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2580 struct bt_ctf_field_type
*tag
)
2583 struct bt_ctf_field_type_variant
*variant
;
2585 if (!type
|| !tag
|| type
->frozen
||
2586 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2591 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2595 bt_put(&variant
->tag
->parent
);
2597 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2604 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2605 struct bt_ctf_field_type
*field
, int index
)
2608 struct bt_ctf_field_type_variant
*variant
;
2610 if (!type
|| !field
||
2611 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2616 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2618 if (index
< 0 || index
>= variant
->fields
->len
) {
2624 bt_put(((struct structure_field
*)
2625 g_ptr_array_index(variant
->fields
, index
))->type
);
2626 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2633 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2635 struct bt_ctf_field_type_integer
*integer
=
2636 (struct bt_ctf_field_type_integer
*) type
;
2642 bt_put(integer
->mapped_clock
);
2647 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2649 struct bt_ctf_field_type_enumeration
*enumeration
=
2650 (struct bt_ctf_field_type_enumeration
*) type
;
2656 g_ptr_array_free(enumeration
->entries
, TRUE
);
2657 bt_put(enumeration
->container
);
2658 g_free(enumeration
);
2662 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2664 struct bt_ctf_field_type_floating_point
*floating_point
=
2665 (struct bt_ctf_field_type_floating_point
*) type
;
2671 g_free(floating_point
);
2675 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2677 struct bt_ctf_field_type_structure
*structure
=
2678 (struct bt_ctf_field_type_structure
*) type
;
2684 g_ptr_array_free(structure
->fields
, TRUE
);
2685 g_hash_table_destroy(structure
->field_name_to_index
);
2690 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2692 struct bt_ctf_field_type_variant
*variant
=
2693 (struct bt_ctf_field_type_variant
*) type
;
2699 g_ptr_array_free(variant
->fields
, TRUE
);
2700 g_hash_table_destroy(variant
->field_name_to_index
);
2701 g_string_free(variant
->tag_name
, TRUE
);
2702 bt_put(&variant
->tag
->parent
);
2703 bt_ctf_field_path_destroy(variant
->tag_path
);
2708 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2710 struct bt_ctf_field_type_array
*array
=
2711 (struct bt_ctf_field_type_array
*) type
;
2717 bt_put(array
->element_type
);
2722 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2724 struct bt_ctf_field_type_sequence
*sequence
=
2725 (struct bt_ctf_field_type_sequence
*) type
;
2731 bt_put(sequence
->element_type
);
2732 g_string_free(sequence
->length_field_name
, TRUE
);
2733 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2738 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2740 struct bt_ctf_field_type_string
*string
=
2741 (struct bt_ctf_field_type_string
*) type
;
2751 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2757 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2759 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2760 type
, struct bt_ctf_field_type_enumeration
, parent
);
2762 generic_field_type_freeze(type
);
2763 bt_ctf_field_type_freeze(enumeration_type
->container
);
2767 void freeze_structure_field(struct structure_field
*field
)
2769 bt_ctf_field_type_freeze(field
->type
);
2773 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2775 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2776 type
, struct bt_ctf_field_type_structure
, parent
);
2778 /* Cache the alignment */
2779 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2780 generic_field_type_freeze(type
);
2781 g_ptr_array_foreach(structure_type
->fields
,
2782 (GFunc
) freeze_structure_field
, NULL
);
2786 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2788 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2789 type
, struct bt_ctf_field_type_variant
, parent
);
2791 generic_field_type_freeze(type
);
2792 g_ptr_array_foreach(variant_type
->fields
,
2793 (GFunc
) freeze_structure_field
, NULL
);
2797 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2799 struct bt_ctf_field_type_array
*array_type
= container_of(
2800 type
, struct bt_ctf_field_type_array
, parent
);
2802 /* Cache the alignment */
2803 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2804 generic_field_type_freeze(type
);
2805 bt_ctf_field_type_freeze(array_type
->element_type
);
2809 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2811 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2812 type
, struct bt_ctf_field_type_sequence
, parent
);
2814 /* Cache the alignment */
2815 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2816 generic_field_type_freeze(type
);
2817 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2821 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2823 const char *encoding_string
;
2826 case CTF_STRING_NONE
:
2827 encoding_string
= "none";
2829 case CTF_STRING_ASCII
:
2830 encoding_string
= "ASCII";
2832 case CTF_STRING_UTF8
:
2833 encoding_string
= "UTF8";
2836 encoding_string
= "unknown";
2840 return encoding_string
;
2844 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2846 const char *base_string
;
2849 case BT_CTF_INTEGER_BASE_DECIMAL
:
2850 base_string
= "decimal";
2852 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2853 base_string
= "hexadecimal";
2855 case BT_CTF_INTEGER_BASE_OCTAL
:
2856 base_string
= "octal";
2858 case BT_CTF_INTEGER_BASE_BINARY
:
2859 base_string
= "binary";
2862 base_string
= "unknown";
2870 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2871 struct metadata_context
*context
)
2873 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2874 struct bt_ctf_field_type_integer
, parent
);
2877 g_string_append_printf(context
->string
,
2878 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2879 integer
->declaration
.len
, type
->declaration
->alignment
,
2880 (integer
->declaration
.signedness
? "true" : "false"),
2881 get_encoding_string(integer
->declaration
.encoding
),
2882 get_integer_base_string(integer
->declaration
.base
),
2883 get_byte_order_string(integer
->declaration
.byte_order
));
2884 if (integer
->mapped_clock
) {
2885 const char *clock_name
= bt_ctf_clock_get_name(
2886 integer
->mapped_clock
);
2893 g_string_append_printf(context
->string
,
2894 "; map = clock.%s.value", clock_name
);
2897 g_string_append(context
->string
, "; }");
2903 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2904 struct metadata_context
*context
)
2908 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2909 struct bt_ctf_field_type_enumeration
, parent
);
2910 struct bt_ctf_field_type
*container_type
;
2911 int container_signed
;
2913 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2914 if (!container_type
) {
2919 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2920 if (container_signed
< 0) {
2921 ret
= container_signed
;
2922 goto error_put_container_type
;
2925 g_string_append(context
->string
, "enum : ");
2926 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2928 goto error_put_container_type
;
2931 g_string_append(context
->string
, " { ");
2932 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2933 struct enumeration_mapping
*mapping
=
2934 enumeration
->entries
->pdata
[entry
];
2936 if (container_signed
) {
2937 if (mapping
->range_start
._signed
==
2938 mapping
->range_end
._signed
) {
2939 g_string_append_printf(context
->string
,
2940 "\"%s\" = %" PRId64
,
2941 g_quark_to_string(mapping
->string
),
2942 mapping
->range_start
._signed
);
2944 g_string_append_printf(context
->string
,
2945 "\"%s\" = %" PRId64
" ... %" PRId64
,
2946 g_quark_to_string(mapping
->string
),
2947 mapping
->range_start
._signed
,
2948 mapping
->range_end
._signed
);
2951 if (mapping
->range_start
._unsigned
==
2952 mapping
->range_end
._unsigned
) {
2953 g_string_append_printf(context
->string
,
2954 "\"%s\" = %" PRIu64
,
2955 g_quark_to_string(mapping
->string
),
2956 mapping
->range_start
._unsigned
);
2958 g_string_append_printf(context
->string
,
2959 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2960 g_quark_to_string(mapping
->string
),
2961 mapping
->range_start
._unsigned
,
2962 mapping
->range_end
._unsigned
);
2966 g_string_append(context
->string
,
2967 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2971 if (context
->field_name
->len
) {
2972 g_string_append_printf(context
->string
, " %s",
2973 context
->field_name
->str
);
2974 g_string_assign(context
->field_name
, "");
2976 error_put_container_type
:
2977 bt_put(container_type
);
2983 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2984 struct metadata_context
*context
)
2986 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2987 type
, struct bt_ctf_field_type_floating_point
, parent
);
2989 g_string_append_printf(context
->string
,
2990 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2991 floating_point
->declaration
.exp
->len
,
2992 floating_point
->declaration
.mantissa
->len
+ 1,
2993 get_byte_order_string(floating_point
->declaration
.byte_order
),
2994 type
->declaration
->alignment
);
2999 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3000 struct metadata_context
*context
)
3003 unsigned int indent
;
3005 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3006 struct bt_ctf_field_type_structure
, parent
);
3007 GString
*structure_field_name
= context
->field_name
;
3009 context
->field_name
= g_string_new("");
3011 context
->current_indentation_level
++;
3012 g_string_append(context
->string
, "struct {\n");
3014 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3015 struct structure_field
*field
;
3017 for (indent
= 0; indent
< context
->current_indentation_level
;
3019 g_string_append_c(context
->string
, '\t');
3022 field
= structure
->fields
->pdata
[i
];
3023 g_string_assign(context
->field_name
,
3024 g_quark_to_string(field
->name
));
3025 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3030 if (context
->field_name
->len
) {
3031 g_string_append_printf(context
->string
, " %s",
3032 context
->field_name
->str
);
3034 g_string_append(context
->string
, ";\n");
3037 context
->current_indentation_level
--;
3038 for (indent
= 0; indent
< context
->current_indentation_level
;
3040 g_string_append_c(context
->string
, '\t');
3043 g_string_append_printf(context
->string
, "} align(%zu)",
3044 type
->declaration
->alignment
);
3046 g_string_free(context
->field_name
, TRUE
);
3047 context
->field_name
= structure_field_name
;
3052 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3053 struct metadata_context
*context
)
3056 unsigned int indent
;
3058 struct bt_ctf_field_type_variant
*variant
= container_of(
3059 type
, struct bt_ctf_field_type_variant
, parent
);
3060 GString
*variant_field_name
= context
->field_name
;
3062 context
->field_name
= g_string_new("");
3063 if (variant
->tag_name
->len
> 0) {
3064 g_string_append_printf(context
->string
,
3065 "variant <%s> {\n", variant
->tag_name
->str
);
3067 g_string_append(context
->string
, "variant {\n");
3070 context
->current_indentation_level
++;
3071 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3072 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3074 g_string_assign(context
->field_name
,
3075 g_quark_to_string(field
->name
));
3076 for (indent
= 0; indent
< context
->current_indentation_level
;
3078 g_string_append_c(context
->string
, '\t');
3081 g_string_assign(context
->field_name
,
3082 g_quark_to_string(field
->name
));
3083 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3088 if (context
->field_name
->len
) {
3089 g_string_append_printf(context
->string
, " %s;",
3090 context
->field_name
->str
);
3093 g_string_append_c(context
->string
, '\n');
3096 context
->current_indentation_level
--;
3097 for (indent
= 0; indent
< context
->current_indentation_level
;
3099 g_string_append_c(context
->string
, '\t');
3102 g_string_append(context
->string
, "}");
3104 g_string_free(context
->field_name
, TRUE
);
3105 context
->field_name
= variant_field_name
;
3110 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3111 struct metadata_context
*context
)
3114 struct bt_ctf_field_type_array
*array
= container_of(type
,
3115 struct bt_ctf_field_type_array
, parent
);
3117 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3122 if (context
->field_name
->len
) {
3123 g_string_append_printf(context
->string
, " %s[%u]",
3124 context
->field_name
->str
, array
->length
);
3125 g_string_assign(context
->field_name
, "");
3127 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3134 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3135 struct metadata_context
*context
)
3138 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3139 type
, struct bt_ctf_field_type_sequence
, parent
);
3141 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3146 if (context
->field_name
->len
) {
3147 g_string_append_printf(context
->string
, " %s[%s]",
3148 context
->field_name
->str
,
3149 sequence
->length_field_name
->str
);
3150 g_string_assign(context
->field_name
, "");
3152 g_string_append_printf(context
->string
, "[%s]",
3153 sequence
->length_field_name
->str
);
3160 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3161 struct metadata_context
*context
)
3163 struct bt_ctf_field_type_string
*string
= container_of(
3164 type
, struct bt_ctf_field_type_string
, parent
);
3166 g_string_append_printf(context
->string
,
3167 "string { encoding = %s; }",
3168 get_encoding_string(string
->declaration
.encoding
));
3173 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3174 enum bt_ctf_byte_order ret
;
3176 switch (byte_order
) {
3177 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3179 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3181 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3183 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3185 case BT_CTF_BYTE_ORDER_NETWORK
:
3186 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3188 case BT_CTF_BYTE_ORDER_NATIVE
:
3189 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3192 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3200 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3201 int byte_order
, int set_native
)
3203 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3204 struct bt_ctf_field_type_integer
, parent
);
3207 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3209 * User byte order is native, so we can set
3210 * the real byte order.
3212 integer_type
->declaration
.byte_order
=
3216 integer_type
->user_byte_order
=
3217 get_ctf_ir_byte_order(byte_order
);
3218 integer_type
->declaration
.byte_order
= byte_order
;
3223 void bt_ctf_field_type_enumeration_set_byte_order(
3224 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3226 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3227 struct bt_ctf_field_type_enumeration
, parent
);
3229 /* Safe to assume that container is an integer */
3230 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3231 byte_order
, set_native
);
3235 void bt_ctf_field_type_floating_point_set_byte_order(
3236 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3238 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3239 container_of(type
, struct bt_ctf_field_type_floating_point
,
3243 if (floating_point_type
->user_byte_order
==
3244 BT_CTF_BYTE_ORDER_NATIVE
) {
3246 * User byte order is native, so we can set
3247 * the real byte order.
3249 floating_point_type
->declaration
.byte_order
=
3251 floating_point_type
->sign
.byte_order
=
3253 floating_point_type
->mantissa
.byte_order
=
3255 floating_point_type
->exp
.byte_order
=
3259 floating_point_type
->user_byte_order
=
3260 get_ctf_ir_byte_order(byte_order
);
3261 floating_point_type
->declaration
.byte_order
= byte_order
;
3262 floating_point_type
->sign
.byte_order
= byte_order
;
3263 floating_point_type
->mantissa
.byte_order
= byte_order
;
3264 floating_point_type
->exp
.byte_order
= byte_order
;
3269 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3270 int byte_order
, int set_native
)
3273 struct bt_ctf_field_type_structure
*structure_type
=
3274 container_of(type
, struct bt_ctf_field_type_structure
,
3277 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3278 struct structure_field
*field
= g_ptr_array_index(
3279 structure_type
->fields
, i
);
3280 struct bt_ctf_field_type
*field_type
= field
->type
;
3282 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3283 set_byte_order_funcs
[field_type
->declaration
->id
](
3284 field_type
, byte_order
, set_native
);
3290 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3291 int byte_order
, int set_native
)
3294 struct bt_ctf_field_type_variant
*variant_type
=
3295 container_of(type
, struct bt_ctf_field_type_variant
,
3298 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3299 struct structure_field
*field
= g_ptr_array_index(
3300 variant_type
->fields
, i
);
3301 struct bt_ctf_field_type
*field_type
= field
->type
;
3303 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3304 set_byte_order_funcs
[field_type
->declaration
->id
](
3305 field_type
, byte_order
, set_native
);
3311 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3312 int byte_order
, int set_native
)
3314 struct bt_ctf_field_type_array
*array_type
=
3315 container_of(type
, struct bt_ctf_field_type_array
,
3318 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3319 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3320 array_type
->element_type
, byte_order
, set_native
);
3325 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3326 int byte_order
, int set_native
)
3328 struct bt_ctf_field_type_sequence
*sequence_type
=
3329 container_of(type
, struct bt_ctf_field_type_sequence
,
3332 if (set_byte_order_funcs
[
3333 sequence_type
->element_type
->declaration
->id
]) {
3334 set_byte_order_funcs
[
3335 sequence_type
->element_type
->declaration
->id
](
3336 sequence_type
->element_type
, byte_order
, set_native
);
3341 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3342 struct bt_ctf_field_type
*type
)
3344 struct bt_ctf_field_type
*copy
;
3345 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3347 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3348 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3353 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3355 copy_integer
->declaration
= integer
->declaration
;
3356 if (integer
->mapped_clock
) {
3357 bt_get(integer
->mapped_clock
);
3358 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3361 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3368 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3369 struct bt_ctf_field_type
*type
)
3372 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3373 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3375 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3378 /* Copy the source enumeration's container */
3379 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3380 if (!copy_container
) {
3384 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3388 copy_enumeration
= container_of(copy
,
3389 struct bt_ctf_field_type_enumeration
, parent
);
3391 /* Copy all enumaration entries */
3392 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3393 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3394 enumeration
->entries
, i
);
3395 struct enumeration_mapping
* copy_mapping
= g_new0(
3396 struct enumeration_mapping
, 1);
3398 if (!copy_mapping
) {
3402 *copy_mapping
= *mapping
;
3403 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3406 copy_enumeration
->declaration
= enumeration
->declaration
;
3408 bt_put(copy_container
);
3411 bt_put(copy_container
);
3417 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3418 struct bt_ctf_field_type
*type
)
3420 struct bt_ctf_field_type
*copy
;
3421 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3423 floating_point
= container_of(type
,
3424 struct bt_ctf_field_type_floating_point
, parent
);
3425 copy
= bt_ctf_field_type_floating_point_create();
3430 copy_float
= container_of(copy
,
3431 struct bt_ctf_field_type_floating_point
, parent
);
3432 copy_float
->declaration
= floating_point
->declaration
;
3433 copy_float
->sign
= floating_point
->sign
;
3434 copy_float
->mantissa
= floating_point
->mantissa
;
3435 copy_float
->exp
= floating_point
->exp
;
3436 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3437 copy_float
->declaration
.sign
= ©_float
->sign
;
3438 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3439 copy_float
->declaration
.exp
= ©_float
->exp
;
3445 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3446 struct bt_ctf_field_type
*type
)
3449 GHashTableIter iter
;
3450 gpointer key
, value
;
3451 struct bt_ctf_field_type
*copy
;
3452 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3454 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3456 copy
= bt_ctf_field_type_structure_create();
3461 copy_structure
= container_of(copy
,
3462 struct bt_ctf_field_type_structure
, parent
);
3464 /* Copy field_name_to_index */
3465 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3466 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3467 g_hash_table_insert(copy_structure
->field_name_to_index
,
3471 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3472 struct structure_field
*entry
, *copy_entry
;
3473 struct bt_ctf_field_type
*copy_field
;
3475 copy_entry
= g_new0(struct structure_field
, 1);
3480 entry
= g_ptr_array_index(structure
->fields
, i
);
3481 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3487 copy_entry
->name
= entry
->name
;
3488 copy_entry
->type
= copy_field
;
3489 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3492 copy_structure
->declaration
= structure
->declaration
;
3501 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3502 struct bt_ctf_field_type
*type
)
3505 GHashTableIter iter
;
3506 gpointer key
, value
;
3507 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3508 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3510 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3513 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3519 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3520 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3525 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3528 /* Copy field_name_to_index */
3529 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3530 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3531 g_hash_table_insert(copy_variant
->field_name_to_index
,
3535 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3536 struct structure_field
*entry
, *copy_entry
;
3537 struct bt_ctf_field_type
*copy_field
;
3539 copy_entry
= g_new0(struct structure_field
, 1);
3544 entry
= g_ptr_array_index(variant
->fields
, i
);
3545 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3551 copy_entry
->name
= entry
->name
;
3552 copy_entry
->type
= copy_field
;
3553 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3556 copy_variant
->declaration
= variant
->declaration
;
3557 if (variant
->tag_path
) {
3558 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3560 if (!copy_variant
->tag_path
) {
3574 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3575 struct bt_ctf_field_type
*type
)
3577 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3578 struct bt_ctf_field_type_array
*array
, *copy_array
;
3580 array
= container_of(type
, struct bt_ctf_field_type_array
,
3582 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3583 if (!copy_element
) {
3587 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3592 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3594 copy_array
->declaration
= array
->declaration
;
3596 bt_put(copy_element
);
3601 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3602 struct bt_ctf_field_type
*type
)
3604 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3605 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3607 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3609 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3610 if (!copy_element
) {
3614 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3615 sequence
->length_field_name
->len
?
3616 sequence
->length_field_name
->str
: NULL
);
3621 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3623 copy_sequence
->declaration
= sequence
->declaration
;
3624 if (sequence
->length_field_path
) {
3625 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3626 sequence
->length_field_path
);
3627 if (!copy_sequence
->length_field_path
) {
3632 bt_put(copy_element
);
3640 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3641 struct bt_ctf_field_type
*type
)
3643 struct bt_ctf_field_type
*copy
;
3644 struct bt_ctf_field_type_string
*string
, *copy_string
;
3646 copy
= bt_ctf_field_type_string_create();
3651 string
= container_of(type
, struct bt_ctf_field_type_string
,
3653 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3655 copy_string
->declaration
= string
->declaration
;
3661 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3662 struct bt_ctf_field_type
*type_b
)
3665 struct bt_ctf_field_type_integer
*integer_a
;
3666 struct bt_ctf_field_type_integer
*integer_b
;
3667 struct declaration_integer
*decl_a
;
3668 struct declaration_integer
*decl_b
;
3670 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3672 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3674 decl_a
= &integer_a
->declaration
;
3675 decl_b
= &integer_b
->declaration
;
3678 if (decl_a
->len
!= decl_b
->len
) {
3683 * Compare user byte orders only, not the cached,
3686 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3691 if (decl_a
->signedness
!= decl_b
->signedness
) {
3696 if (decl_a
->base
!= decl_b
->base
) {
3701 if (decl_a
->encoding
!= decl_b
->encoding
) {
3706 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3718 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3719 struct bt_ctf_field_type
*type_b
)
3722 struct bt_ctf_field_type_floating_point
*float_a
;
3723 struct bt_ctf_field_type_floating_point
*float_b
;
3725 float_a
= container_of(type_a
,
3726 struct bt_ctf_field_type_floating_point
, parent
);
3727 float_b
= container_of(type_b
,
3728 struct bt_ctf_field_type_floating_point
, parent
);
3731 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3735 /* Exponent length */
3736 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3740 /* Mantissa length */
3741 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3746 * Compare user byte orders only, not the cached,
3749 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3761 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3762 struct enumeration_mapping
*mapping_b
)
3767 if (mapping_a
->string
!= mapping_b
->string
) {
3772 if (mapping_a
->range_start
._unsigned
!=
3773 mapping_b
->range_start
._unsigned
) {
3778 if (mapping_a
->range_end
._unsigned
!=
3779 mapping_b
->range_end
._unsigned
) {
3791 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3792 struct bt_ctf_field_type
*type_b
)
3796 struct bt_ctf_field_type_enumeration
*enum_a
;
3797 struct bt_ctf_field_type_enumeration
*enum_b
;
3799 enum_a
= container_of(type_a
,
3800 struct bt_ctf_field_type_enumeration
, parent
);
3801 enum_b
= container_of(type_b
,
3802 struct bt_ctf_field_type_enumeration
, parent
);
3804 /* Container field type */
3805 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3813 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3817 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3818 struct enumeration_mapping
*mapping_a
=
3819 g_ptr_array_index(enum_a
->entries
, i
);
3820 struct enumeration_mapping
*mapping_b
=
3821 g_ptr_array_index(enum_b
->entries
, i
);
3823 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3836 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3837 struct bt_ctf_field_type
*type_b
)
3840 struct bt_ctf_field_type_string
*string_a
;
3841 struct bt_ctf_field_type_string
*string_b
;
3843 string_a
= container_of(type_a
,
3844 struct bt_ctf_field_type_string
, parent
);
3845 string_b
= container_of(type_b
,
3846 struct bt_ctf_field_type_string
, parent
);
3849 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3861 int compare_structure_fields(struct structure_field
*field_a
,
3862 struct structure_field
*field_b
)
3867 if (field_a
->name
!= field_b
->name
) {
3872 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3879 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3880 struct bt_ctf_field_type
*type_b
)
3884 struct bt_ctf_field_type_structure
*struct_a
;
3885 struct bt_ctf_field_type_structure
*struct_b
;
3887 struct_a
= container_of(type_a
,
3888 struct bt_ctf_field_type_structure
, parent
);
3889 struct_b
= container_of(type_b
,
3890 struct bt_ctf_field_type_structure
, parent
);
3893 if (bt_ctf_field_type_get_alignment(type_a
) !=
3894 bt_ctf_field_type_get_alignment(type_b
)) {
3899 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3903 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3904 struct structure_field
*field_a
=
3905 g_ptr_array_index(struct_a
->fields
, i
);
3906 struct structure_field
*field_b
=
3907 g_ptr_array_index(struct_b
->fields
, i
);
3909 ret
= compare_structure_fields(field_a
, field_b
);
3925 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3926 struct bt_ctf_field_type
*type_b
)
3930 struct bt_ctf_field_type_variant
*variant_a
;
3931 struct bt_ctf_field_type_variant
*variant_b
;
3933 variant_a
= container_of(type_a
,
3934 struct bt_ctf_field_type_variant
, parent
);
3935 variant_b
= container_of(type_b
,
3936 struct bt_ctf_field_type_variant
, parent
);
3939 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3944 ret
= bt_ctf_field_type_compare(
3945 (struct bt_ctf_field_type
*) variant_a
->tag
,
3946 (struct bt_ctf_field_type
*) variant_b
->tag
);
3954 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3958 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3959 struct structure_field
*field_a
=
3960 g_ptr_array_index(variant_a
->fields
, i
);
3961 struct structure_field
*field_b
=
3962 g_ptr_array_index(variant_b
->fields
, i
);
3964 ret
= compare_structure_fields(field_a
, field_b
);
3980 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3981 struct bt_ctf_field_type
*type_b
)
3984 struct bt_ctf_field_type_array
*array_a
;
3985 struct bt_ctf_field_type_array
*array_b
;
3987 array_a
= container_of(type_a
,
3988 struct bt_ctf_field_type_array
, parent
);
3989 array_b
= container_of(type_b
,
3990 struct bt_ctf_field_type_array
, parent
);
3993 if (array_a
->length
!= array_b
->length
) {
3998 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3999 array_b
->element_type
);
4006 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4007 struct bt_ctf_field_type
*type_b
)
4010 struct bt_ctf_field_type_sequence
*sequence_a
;
4011 struct bt_ctf_field_type_sequence
*sequence_b
;
4013 sequence_a
= container_of(type_a
,
4014 struct bt_ctf_field_type_sequence
, parent
);
4015 sequence_b
= container_of(type_b
,
4016 struct bt_ctf_field_type_sequence
, parent
);
4019 if (strcmp(sequence_a
->length_field_name
->str
,
4020 sequence_b
->length_field_name
->str
)) {
4025 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4026 sequence_b
->element_type
);
4032 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4033 struct bt_ctf_field_type
*type_b
)
4037 if (type_a
== type_b
) {
4038 /* Same reference: equal (even if both are NULL) */
4043 if (!type_a
|| !type_b
) {
4048 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4049 /* Different type IDs */
4053 if (type_a
->declaration
->id
== CTF_TYPE_UNKNOWN
) {
4054 /* Both have unknown type IDs */
4058 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);