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/ctf-ir/clock.h>
33 #include <babeltrace/ctf-writer/writer-internal.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/endian.h>
40 struct range_overlap_query
{
55 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
69 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
71 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
74 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
75 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
77 bt_ctf_field_type_enumeration_destroy
,
79 bt_ctf_field_type_floating_point_destroy
,
80 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
81 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
82 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
83 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
84 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
88 void generic_field_type_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
101 type_freeze_func
const type_freeze_funcs
[] = {
102 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
103 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
104 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
105 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
106 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
107 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
108 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
109 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
113 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
114 struct metadata_context
*);
116 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_floating_point_serialize(
120 struct bt_ctf_field_type
*, struct metadata_context
*);
122 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
138 type_serialize_func
const type_serialize_funcs
[] = {
139 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
141 bt_ctf_field_type_enumeration_serialize
,
143 bt_ctf_field_type_floating_point_serialize
,
145 bt_ctf_field_type_structure_serialize
,
146 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
147 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
148 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
149 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
153 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
154 int byte_order
, int set_native
);
156 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_floating_point_set_byte_order(
160 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
162 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 /* The set_native flag only set the byte order if it is set to native */
176 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
177 int byte_order
, int set_native
) = {
178 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
180 bt_ctf_field_type_enumeration_set_byte_order
,
182 bt_ctf_field_type_floating_point_set_byte_order
,
184 bt_ctf_field_type_structure_set_byte_order
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
188 [CTF_TYPE_STRING
] = NULL
,
192 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
193 struct bt_ctf_field_type
*);
195 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
214 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
218 struct bt_ctf_field_type
*) = {
219 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
220 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
221 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
222 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
223 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
224 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
225 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
226 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
230 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
236 void destroy_structure_field(struct structure_field
*field
)
239 bt_ctf_field_type_put(field
->type
);
246 void check_ranges_overlap(gpointer element
, gpointer query
)
248 struct enumeration_mapping
*mapping
= element
;
249 struct range_overlap_query
*overlap_query
= query
;
251 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
252 && overlap_query
->range_start
._signed
<=
253 mapping
->range_end
._signed
) {
254 overlap_query
->overlaps
= 1;
255 overlap_query
->mapping_name
= mapping
->string
;
258 overlap_query
->overlaps
|=
259 mapping
->string
== overlap_query
->mapping_name
;
263 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
265 struct enumeration_mapping
*mapping
= element
;
266 struct range_overlap_query
*overlap_query
= query
;
268 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
269 && overlap_query
->range_start
._unsigned
<=
270 mapping
->range_end
._unsigned
) {
271 overlap_query
->overlaps
= 1;
272 overlap_query
->mapping_name
= mapping
->string
;
275 overlap_query
->overlaps
|=
276 mapping
->string
== overlap_query
->mapping_name
;
280 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
281 struct enumeration_mapping
**b
)
283 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
287 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
288 struct enumeration_mapping
**b
)
290 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
294 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
296 enum ctf_type_id type_id
= type
->declaration
->id
;
299 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
300 (type_id
< NR_CTF_TYPES
));
302 bt_ctf_ref_init(&type
->ref_count
);
303 type
->freeze
= type_freeze_funcs
[type_id
];
304 type
->serialize
= type_serialize_funcs
[type_id
];
305 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
307 type
->declaration
->alignment
= 1;
311 int add_structure_field(GPtrArray
*fields
,
312 GHashTable
*field_name_to_index
,
313 struct bt_ctf_field_type
*field_type
,
314 const char *field_name
)
317 GQuark name_quark
= g_quark_from_string(field_name
);
318 struct structure_field
*field
;
320 /* Make sure structure does not contain a field of the same name */
321 if (g_hash_table_lookup_extended(field_name_to_index
,
322 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
327 field
= g_new0(struct structure_field
, 1);
333 bt_ctf_field_type_get(field_type
);
334 field
->name
= name_quark
;
335 field
->type
= field_type
;
336 g_hash_table_insert(field_name_to_index
,
337 (gpointer
) (unsigned long) name_quark
,
338 (gpointer
) (unsigned long) fields
->len
);
339 g_ptr_array_add(fields
, field
);
345 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
347 struct bt_ctf_field_type
*type
;
348 enum ctf_type_id type_id
;
354 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
355 type_id
= type
->declaration
->id
;
356 if (type_id
<= CTF_TYPE_UNKNOWN
||
357 type_id
>= NR_CTF_TYPES
) {
361 type_destroy_funcs
[type_id
](ref
);
365 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
374 switch (type
->declaration
->id
) {
377 struct bt_ctf_field_type_enumeration
*enumeration
=
378 container_of(type
, struct bt_ctf_field_type_enumeration
,
381 /* Ensure enum has entries */
382 ret
= enumeration
->entries
->len
? 0 : -1;
385 case CTF_TYPE_SEQUENCE
:
387 struct bt_ctf_field_type_sequence
*sequence
=
388 container_of(type
, struct bt_ctf_field_type_sequence
,
391 /* length field name should be set at this point */
392 ret
= sequence
->length_field_name
->len
? 0 : -1;
395 case CTF_TYPE_VARIANT
:
397 struct bt_ctf_field_type_variant
*variant
=
398 container_of(type
, struct bt_ctf_field_type_variant
,
401 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
413 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
415 struct bt_ctf_field_type_integer
*integer
=
416 g_new0(struct bt_ctf_field_type_integer
, 1);
418 if (!integer
|| size
== 0 || size
> 64) {
422 integer
->parent
.declaration
= &integer
->declaration
.p
;
423 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
424 integer
->declaration
.len
= size
;
425 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
426 integer
->declaration
.encoding
= CTF_STRING_NONE
;
427 bt_ctf_field_type_init(&integer
->parent
);
428 return &integer
->parent
;
431 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
434 struct bt_ctf_field_type_integer
*integer
;
436 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
441 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
442 ret
= (int) integer
->declaration
.len
;
447 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
450 struct bt_ctf_field_type_integer
*integer
;
452 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
457 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
458 ret
= integer
->declaration
.signedness
;
463 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
467 struct bt_ctf_field_type_integer
*integer
;
469 if (!type
|| type
->frozen
||
470 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
475 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
476 integer
->declaration
.signedness
= !!is_signed
;
481 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
482 struct bt_ctf_field_type
*type
)
484 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
485 struct bt_ctf_field_type_integer
*integer
;
487 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
491 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
492 ret
= integer
->declaration
.base
;
497 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
498 enum bt_ctf_integer_base base
)
502 if (!type
|| type
->frozen
||
503 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
509 case BT_CTF_INTEGER_BASE_BINARY
:
510 case BT_CTF_INTEGER_BASE_OCTAL
:
511 case BT_CTF_INTEGER_BASE_DECIMAL
:
512 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
514 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
515 struct bt_ctf_field_type_integer
, parent
);
516 integer
->declaration
.base
= base
;
526 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
527 struct bt_ctf_field_type
*type
)
529 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
530 struct bt_ctf_field_type_integer
*integer
;
532 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
536 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
537 ret
= integer
->declaration
.encoding
;
542 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
543 enum ctf_string_encoding encoding
)
546 struct bt_ctf_field_type_integer
*integer
;
548 if (!type
|| type
->frozen
||
549 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
550 (encoding
< CTF_STRING_NONE
) ||
551 (encoding
>= CTF_STRING_UNKNOWN
)) {
556 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
557 integer
->declaration
.encoding
= encoding
;
562 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
563 struct bt_ctf_field_type
*type
)
565 struct bt_ctf_field_type_integer
*integer
;
566 struct bt_ctf_clock
*clock
= NULL
;
572 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
573 clock
= integer
->mapped_clock
;
575 bt_ctf_clock_get(clock
);
581 int bt_ctf_field_type_integer_set_mapped_clock(
582 struct bt_ctf_field_type
*type
,
583 struct bt_ctf_clock
*clock
)
585 struct bt_ctf_field_type_integer
*integer
;
588 if (!type
|| type
->frozen
) {
593 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
594 if (integer
->mapped_clock
) {
595 bt_ctf_clock_put(integer
->mapped_clock
);
599 bt_ctf_clock_get(clock
);
602 integer
->mapped_clock
= clock
;
607 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
608 struct bt_ctf_field_type
*integer_container_type
)
610 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
612 if (!integer_container_type
) {
616 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
620 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
625 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
626 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
627 bt_ctf_field_type_get(integer_container_type
);
628 enumeration
->container
= integer_container_type
;
629 enumeration
->entries
= g_ptr_array_new_with_free_func(
630 (GDestroyNotify
)destroy_enumeration_mapping
);
631 bt_ctf_field_type_init(&enumeration
->parent
);
632 return &enumeration
->parent
;
638 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
639 struct bt_ctf_field_type
*type
)
641 struct bt_ctf_field_type
*container_type
= NULL
;
642 struct bt_ctf_field_type_enumeration
*enumeration_type
;
648 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
652 enumeration_type
= container_of(type
,
653 struct bt_ctf_field_type_enumeration
, parent
);
654 container_type
= enumeration_type
->container
;
655 bt_ctf_field_type_get(container_type
);
657 return container_type
;
660 int bt_ctf_field_type_enumeration_add_mapping(
661 struct bt_ctf_field_type
*type
, const char *string
,
662 int64_t range_start
, int64_t range_end
)
666 struct enumeration_mapping
*mapping
;
667 struct bt_ctf_field_type_enumeration
*enumeration
;
668 struct range_overlap_query query
;
669 char *escaped_string
;
671 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
673 (range_end
< range_start
)) {
678 if (!string
|| strlen(string
) == 0) {
683 escaped_string
= g_strescape(string
, NULL
);
684 if (!escaped_string
) {
689 mapping_name
= g_quark_from_string(escaped_string
);
690 query
= (struct range_overlap_query
) {
691 .range_start
._signed
= range_start
,
692 .range_end
._signed
= range_end
,
693 .mapping_name
= mapping_name
,
695 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
698 /* Check that the range does not overlap with one already present */
699 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
700 if (query
.overlaps
) {
705 mapping
= g_new(struct enumeration_mapping
, 1);
711 *mapping
= (struct enumeration_mapping
) {
712 .range_start
._signed
= range_start
,
713 .range_end
._signed
= range_end
, .string
= mapping_name
};
714 g_ptr_array_add(enumeration
->entries
, mapping
);
715 g_ptr_array_sort(enumeration
->entries
,
716 (GCompareFunc
)compare_enumeration_mappings_signed
);
718 free(escaped_string
);
723 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
724 struct bt_ctf_field_type
*type
, const char *string
,
725 uint64_t range_start
, uint64_t range_end
)
729 struct enumeration_mapping
*mapping
;
730 struct bt_ctf_field_type_enumeration
*enumeration
;
731 struct range_overlap_query query
;
732 char *escaped_string
;
734 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
736 (range_end
< range_start
)) {
741 if (!string
|| strlen(string
) == 0) {
746 escaped_string
= g_strescape(string
, NULL
);
747 if (!escaped_string
) {
752 mapping_name
= g_quark_from_string(escaped_string
);
753 query
= (struct range_overlap_query
) {
754 .range_start
._unsigned
= range_start
,
755 .range_end
._unsigned
= range_end
,
756 .mapping_name
= mapping_name
,
758 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
761 /* Check that the range does not overlap with one already present */
762 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
764 if (query
.overlaps
) {
769 mapping
= g_new(struct enumeration_mapping
, 1);
775 *mapping
= (struct enumeration_mapping
) {
776 .range_start
._unsigned
= range_start
,
777 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
778 g_ptr_array_add(enumeration
->entries
, mapping
);
779 g_ptr_array_sort(enumeration
->entries
,
780 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
782 free(escaped_string
);
787 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
788 struct bt_ctf_field_type_enumeration
*enumeration_type
,
791 const char *name
= NULL
;
792 struct range_overlap_query query
=
793 (struct range_overlap_query
) {
794 .range_start
._unsigned
= value
,
795 .range_end
._unsigned
= value
,
798 g_ptr_array_foreach(enumeration_type
->entries
,
799 check_ranges_overlap_unsigned
,
801 if (!query
.overlaps
) {
805 name
= g_quark_to_string(query
.mapping_name
);
810 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
811 struct bt_ctf_field_type_enumeration
*enumeration_type
,
814 const char *name
= NULL
;
815 struct range_overlap_query query
=
816 (struct range_overlap_query
) {
817 .range_start
._signed
= value
,
818 .range_end
._signed
= value
,
821 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
823 if (!query
.overlaps
) {
827 name
= g_quark_to_string(query
.mapping_name
);
832 int bt_ctf_field_type_enumeration_get_mapping_count(
833 struct bt_ctf_field_type
*type
)
836 struct bt_ctf_field_type_enumeration
*enumeration
;
838 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
843 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
845 ret
= (int) enumeration
->entries
->len
;
851 struct enumeration_mapping
*get_enumeration_mapping(
852 struct bt_ctf_field_type
*type
, int index
)
854 struct enumeration_mapping
*mapping
= NULL
;
855 struct bt_ctf_field_type_enumeration
*enumeration
;
857 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
859 if (index
>= enumeration
->entries
->len
) {
863 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
868 int bt_ctf_field_type_enumeration_get_mapping(
869 struct bt_ctf_field_type
*type
, int index
,
870 const char **string
, int64_t *range_start
, int64_t *range_end
)
872 struct enumeration_mapping
*mapping
;
875 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
876 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
881 mapping
= get_enumeration_mapping(type
, index
);
887 *string
= g_quark_to_string(mapping
->string
);
888 *range_start
= mapping
->range_start
._signed
;
889 *range_end
= mapping
->range_end
._signed
;
894 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
895 struct bt_ctf_field_type
*type
, int index
,
896 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
898 struct enumeration_mapping
*mapping
;
901 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
902 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
907 mapping
= get_enumeration_mapping(type
, index
);
913 *string
= g_quark_to_string(mapping
->string
);
914 *range_start
= mapping
->range_start
._unsigned
;
915 *range_end
= mapping
->range_end
._unsigned
;
920 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
921 struct bt_ctf_field_type
*type
, const char *name
)
924 struct bt_ctf_field_type_enumeration
*enumeration
;
927 if (!type
|| !name
||
928 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
933 name_quark
= g_quark_try_string(name
);
939 enumeration
= container_of(type
,
940 struct bt_ctf_field_type_enumeration
, parent
);
941 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
942 struct enumeration_mapping
*mapping
=
943 get_enumeration_mapping(type
, i
);
945 if (mapping
->string
== name_quark
) {
956 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
957 struct bt_ctf_field_type
*type
, int64_t value
)
959 struct bt_ctf_field_type_enumeration
*enumeration
;
962 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
967 enumeration
= container_of(type
,
968 struct bt_ctf_field_type_enumeration
, parent
);
969 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
970 struct enumeration_mapping
*mapping
=
971 get_enumeration_mapping(type
, i
);
973 if (value
>= mapping
->range_start
._signed
&&
974 value
<= mapping
->range_end
._signed
) {
985 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
986 struct bt_ctf_field_type
*type
, uint64_t value
)
988 struct bt_ctf_field_type_enumeration
*enumeration
;
991 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
996 enumeration
= container_of(type
,
997 struct bt_ctf_field_type_enumeration
, parent
);
998 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
999 struct enumeration_mapping
*mapping
=
1000 get_enumeration_mapping(type
, i
);
1002 if (value
>= mapping
->range_start
._unsigned
&&
1003 value
<= mapping
->range_end
._unsigned
) {
1014 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1016 struct bt_ctf_field_type_floating_point
*floating_point
=
1017 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1019 if (!floating_point
) {
1023 floating_point
->declaration
.sign
= &floating_point
->sign
;
1024 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1025 floating_point
->declaration
.exp
= &floating_point
->exp
;
1026 floating_point
->sign
.len
= 1;
1027 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1028 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1029 floating_point
->declaration
.exp
->len
=
1030 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1031 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1032 floating_point
->sign
.p
.alignment
= 1;
1033 floating_point
->mantissa
.p
.alignment
= 1;
1034 floating_point
->exp
.p
.alignment
= 1;
1036 bt_ctf_field_type_init(&floating_point
->parent
);
1038 return floating_point
? &floating_point
->parent
: NULL
;
1041 int bt_ctf_field_type_floating_point_get_exponent_digits(
1042 struct bt_ctf_field_type
*type
)
1045 struct bt_ctf_field_type_floating_point
*floating_point
;
1047 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1052 floating_point
= container_of(type
,
1053 struct bt_ctf_field_type_floating_point
, parent
);
1054 ret
= (int) floating_point
->declaration
.exp
->len
;
1059 int bt_ctf_field_type_floating_point_set_exponent_digits(
1060 struct bt_ctf_field_type
*type
,
1061 unsigned int exponent_digits
)
1064 struct bt_ctf_field_type_floating_point
*floating_point
;
1066 if (!type
|| type
->frozen
||
1067 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1072 floating_point
= container_of(type
,
1073 struct bt_ctf_field_type_floating_point
, parent
);
1074 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1075 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1077 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1082 floating_point
->declaration
.exp
->len
= exponent_digits
;
1087 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1088 struct bt_ctf_field_type
*type
)
1091 struct bt_ctf_field_type_floating_point
*floating_point
;
1093 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1098 floating_point
= container_of(type
,
1099 struct bt_ctf_field_type_floating_point
, parent
);
1100 ret
= (int) floating_point
->mantissa
.len
+ 1;
1105 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1106 struct bt_ctf_field_type
*type
,
1107 unsigned int mantissa_digits
)
1110 struct bt_ctf_field_type_floating_point
*floating_point
;
1112 if (!type
|| type
->frozen
||
1113 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1118 floating_point
= container_of(type
,
1119 struct bt_ctf_field_type_floating_point
, parent
);
1121 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1122 (mantissa_digits
!= DBL_MANT_DIG
) &&
1123 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1128 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1133 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1135 struct bt_ctf_field_type_structure
*structure
=
1136 g_new0(struct bt_ctf_field_type_structure
, 1);
1142 structure
->parent
.declaration
= &structure
->declaration
.p
;
1143 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1144 structure
->fields
= g_ptr_array_new_with_free_func(
1145 (GDestroyNotify
)destroy_structure_field
);
1146 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1147 bt_ctf_field_type_init(&structure
->parent
);
1148 return &structure
->parent
;
1153 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1154 struct bt_ctf_field_type
*field_type
,
1155 const char *field_name
)
1158 struct bt_ctf_field_type_structure
*structure
;
1160 if (!type
|| !field_type
|| type
->frozen
||
1161 bt_ctf_validate_identifier(field_name
) ||
1162 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1163 bt_ctf_field_type_validate(field_type
)) {
1168 structure
= container_of(type
,
1169 struct bt_ctf_field_type_structure
, parent
);
1170 if (add_structure_field(structure
->fields
,
1171 structure
->field_name_to_index
, field_type
, field_name
)) {
1176 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1177 type
->declaration
->alignment
=
1178 field_type
->declaration
->alignment
;
1184 int bt_ctf_field_type_structure_get_field_count(
1185 struct bt_ctf_field_type
*type
)
1188 struct bt_ctf_field_type_structure
*structure
;
1190 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1195 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1197 ret
= (int) structure
->fields
->len
;
1202 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1203 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1206 struct bt_ctf_field_type_structure
*structure
;
1207 struct structure_field
*field
;
1210 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1211 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1216 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1218 if (index
>= structure
->fields
->len
) {
1223 field
= g_ptr_array_index(structure
->fields
, index
);
1224 *field_type
= field
->type
;
1225 bt_ctf_field_type_get(field
->type
);
1226 *field_name
= g_quark_to_string(field
->name
);
1231 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1232 struct bt_ctf_field_type
*type
,
1237 struct structure_field
*field
;
1238 struct bt_ctf_field_type_structure
*structure
;
1239 struct bt_ctf_field_type
*field_type
= NULL
;
1241 if (!type
|| !name
) {
1245 name_quark
= g_quark_try_string(name
);
1250 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1252 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1253 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1257 field
= structure
->fields
->pdata
[index
];
1258 field_type
= field
->type
;
1259 bt_ctf_field_type_get(field_type
);
1264 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1265 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1267 struct bt_ctf_field_type_variant
*variant
= NULL
;
1269 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1273 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1278 variant
->parent
.declaration
= &variant
->declaration
.p
;
1279 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1280 variant
->tag_name
= g_string_new(tag_name
);
1281 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1282 variant
->fields
= g_ptr_array_new_with_free_func(
1283 (GDestroyNotify
)destroy_structure_field
);
1285 bt_ctf_field_type_get(enum_tag
);
1286 variant
->tag
= container_of(enum_tag
,
1287 struct bt_ctf_field_type_enumeration
, parent
);
1290 bt_ctf_field_type_init(&variant
->parent
);
1291 return &variant
->parent
;
1296 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1297 struct bt_ctf_field_type
*type
)
1299 struct bt_ctf_field_type_variant
*variant
;
1300 struct bt_ctf_field_type
*tag_type
= NULL
;
1302 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1306 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1307 if (!variant
->tag
) {
1311 tag_type
= &variant
->tag
->parent
;
1312 bt_ctf_field_type_get(tag_type
);
1317 const char *bt_ctf_field_type_variant_get_tag_name(
1318 struct bt_ctf_field_type
*type
)
1320 struct bt_ctf_field_type_variant
*variant
;
1321 const char *tag_name
= NULL
;
1323 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1327 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1328 if (variant
->tag_name
->len
== 0) {
1332 tag_name
= variant
->tag_name
->str
;
1337 int bt_ctf_field_type_variant_set_tag_name(
1338 struct bt_ctf_field_type
*type
, const char *name
)
1341 struct bt_ctf_field_type_variant
*variant
;
1343 if (!type
|| type
->frozen
||
1344 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1345 bt_ctf_validate_identifier(name
)) {
1350 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1351 g_string_assign(variant
->tag_name
, name
);
1356 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1357 struct bt_ctf_field_type
*field_type
,
1358 const char *field_name
)
1362 struct bt_ctf_field_type_variant
*variant
;
1363 GQuark field_name_quark
= g_quark_from_string(field_name
);
1365 if (!type
|| !field_type
|| type
->frozen
||
1366 bt_ctf_validate_identifier(field_name
) ||
1367 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1368 bt_ctf_field_type_validate(field_type
)) {
1373 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1375 /* The user has explicitly provided a tag; validate against it. */
1379 /* Make sure this name is present in the enum tag */
1380 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1381 struct enumeration_mapping
*mapping
=
1382 g_ptr_array_index(variant
->tag
->entries
, i
);
1384 if (mapping
->string
== field_name_quark
) {
1391 /* Validation failed */
1397 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1398 field_type
, field_name
)) {
1406 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1407 struct bt_ctf_field_type
*type
,
1408 const char *field_name
)
1412 struct structure_field
*field
;
1413 struct bt_ctf_field_type_variant
*variant
;
1414 struct bt_ctf_field_type
*field_type
= NULL
;
1416 if (!type
|| !field_name
) {
1420 name_quark
= g_quark_try_string(field_name
);
1425 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1426 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1427 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1431 field
= g_ptr_array_index(variant
->fields
, index
);
1432 field_type
= field
->type
;
1433 bt_ctf_field_type_get(field_type
);
1438 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1439 struct bt_ctf_field_type
*type
,
1440 struct bt_ctf_field
*tag
)
1442 const char *enum_value
;
1443 struct bt_ctf_field_type
*field_type
= NULL
;
1445 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1449 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1454 /* Already increments field_type's reference count */
1455 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1461 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1464 struct bt_ctf_field_type_variant
*variant
;
1466 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1471 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1473 ret
= (int) variant
->fields
->len
;
1479 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1480 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1483 struct bt_ctf_field_type_variant
*variant
;
1484 struct structure_field
*field
;
1487 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1488 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1493 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1495 if (index
>= variant
->fields
->len
) {
1500 field
= g_ptr_array_index(variant
->fields
, index
);
1501 *field_type
= field
->type
;
1502 bt_ctf_field_type_get(field
->type
);
1503 *field_name
= g_quark_to_string(field
->name
);
1508 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1509 struct bt_ctf_field_type
*element_type
,
1510 unsigned int length
)
1512 struct bt_ctf_field_type_array
*array
= NULL
;
1514 if (!element_type
|| length
== 0 ||
1515 bt_ctf_field_type_validate(element_type
)) {
1519 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1524 array
->parent
.declaration
= &array
->declaration
.p
;
1525 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1527 bt_ctf_field_type_get(element_type
);
1528 array
->element_type
= element_type
;
1529 array
->length
= length
;
1530 bt_ctf_field_type_init(&array
->parent
);
1531 array
->parent
.declaration
->alignment
=
1532 element_type
->declaration
->alignment
;
1533 return &array
->parent
;
1538 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1539 struct bt_ctf_field_type
*type
)
1541 struct bt_ctf_field_type
*ret
= NULL
;
1542 struct bt_ctf_field_type_array
*array
;
1544 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1548 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1549 ret
= array
->element_type
;
1550 bt_ctf_field_type_get(ret
);
1555 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1558 struct bt_ctf_field_type_array
*array
;
1560 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1565 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1566 ret
= (int64_t) array
->length
;
1571 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1572 struct bt_ctf_field_type
*element_type
,
1573 const char *length_field_name
)
1575 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1577 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1578 bt_ctf_field_type_validate(element_type
)) {
1582 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1587 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1588 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1589 bt_ctf_field_type_get(element_type
);
1590 sequence
->element_type
= element_type
;
1591 sequence
->length_field_name
= g_string_new(length_field_name
);
1592 bt_ctf_field_type_init(&sequence
->parent
);
1593 sequence
->parent
.declaration
->alignment
=
1594 element_type
->declaration
->alignment
;
1595 return &sequence
->parent
;
1600 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1601 struct bt_ctf_field_type
*type
)
1603 struct bt_ctf_field_type
*ret
= NULL
;
1604 struct bt_ctf_field_type_sequence
*sequence
;
1606 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1610 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1612 ret
= sequence
->element_type
;
1613 bt_ctf_field_type_get(ret
);
1618 const char *bt_ctf_field_type_sequence_get_length_field_name(
1619 struct bt_ctf_field_type
*type
)
1621 const char *ret
= NULL
;
1622 struct bt_ctf_field_type_sequence
*sequence
;
1624 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1628 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1630 ret
= sequence
->length_field_name
->str
;
1635 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1637 struct bt_ctf_field_type_string
*string
=
1638 g_new0(struct bt_ctf_field_type_string
, 1);
1644 string
->parent
.declaration
= &string
->declaration
.p
;
1645 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1646 bt_ctf_field_type_init(&string
->parent
);
1647 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1648 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1649 return &string
->parent
;
1652 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1653 struct bt_ctf_field_type
*type
)
1655 struct bt_ctf_field_type_string
*string
;
1656 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1658 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1662 string
= container_of(type
, struct bt_ctf_field_type_string
,
1664 ret
= string
->declaration
.encoding
;
1669 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1670 enum ctf_string_encoding encoding
)
1673 struct bt_ctf_field_type_string
*string
;
1675 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1676 (encoding
!= CTF_STRING_UTF8
&&
1677 encoding
!= CTF_STRING_ASCII
)) {
1682 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1683 string
->declaration
.encoding
= encoding
;
1688 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1697 ret
= (int) type
->declaration
->alignment
;
1702 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1703 unsigned int alignment
)
1707 /* Alignment must be bit-aligned (1) or byte aligned */
1708 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1713 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1714 alignment
!= CHAR_BIT
) {
1719 type
->declaration
->alignment
= alignment
;
1725 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1726 struct bt_ctf_field_type
*type
)
1728 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1729 int internal_byte_order
= -1;
1735 switch (type
->declaration
->id
) {
1736 case CTF_TYPE_INTEGER
:
1738 struct bt_ctf_field_type_integer
*integer
= container_of(
1739 type
, struct bt_ctf_field_type_integer
, parent
);
1740 internal_byte_order
= integer
->declaration
.byte_order
;
1743 case CTF_TYPE_FLOAT
:
1745 struct bt_ctf_field_type_floating_point
*floating_point
=
1747 struct bt_ctf_field_type_floating_point
,
1749 internal_byte_order
= floating_point
->declaration
.byte_order
;
1756 switch (internal_byte_order
) {
1758 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1761 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1764 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1767 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1773 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1774 enum bt_ctf_byte_order byte_order
)
1777 int internal_byte_order
;
1778 enum ctf_type_id type_id
;
1780 if (!type
|| type
->frozen
) {
1785 switch (byte_order
) {
1786 case BT_CTF_BYTE_ORDER_NATIVE
:
1787 /* Leave unset. Will be initialized by parent. */
1788 internal_byte_order
= 0;
1790 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1791 internal_byte_order
= LITTLE_ENDIAN
;
1793 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1794 case BT_CTF_BYTE_ORDER_NETWORK
:
1795 internal_byte_order
= BIG_ENDIAN
;
1802 type_id
= type
->declaration
->id
;
1803 if (set_byte_order_funcs
[type_id
]) {
1804 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1810 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1811 struct bt_ctf_field_type
*type
)
1814 return CTF_TYPE_UNKNOWN
;
1817 return type
->declaration
->id
;
1820 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1826 bt_ctf_ref_get(&type
->ref_count
);
1829 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1835 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1839 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1849 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1850 struct bt_ctf_field_type_variant
*variant
,
1853 struct bt_ctf_field_type
*type
= NULL
;
1854 GQuark field_name_quark
;
1856 struct structure_field
*field_entry
;
1857 struct range_overlap_query query
= {
1858 .range_start
._signed
= tag_value
,
1859 .range_end
._signed
= tag_value
,
1860 .mapping_name
= 0, .overlaps
= 0};
1862 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1864 if (!query
.overlaps
) {
1868 field_name_quark
= query
.mapping_name
;
1869 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1870 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1874 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1875 type
= field_entry
->type
;
1881 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1882 struct bt_ctf_field_type_variant
*variant
,
1885 struct bt_ctf_field_type
*type
= NULL
;
1886 GQuark field_name_quark
;
1888 struct structure_field
*field_entry
;
1889 struct range_overlap_query query
= {
1890 .range_start
._unsigned
= tag_value
,
1891 .range_end
._unsigned
= tag_value
,
1892 .mapping_name
= 0, .overlaps
= 0};
1894 g_ptr_array_foreach(variant
->tag
->entries
,
1895 check_ranges_overlap_unsigned
,
1897 if (!query
.overlaps
) {
1901 field_name_quark
= query
.mapping_name
;
1902 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1903 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1907 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1908 type
= field_entry
->type
;
1914 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1915 struct metadata_context
*context
)
1919 if (!type
|| !context
) {
1924 ret
= type
->serialize(type
, context
);
1930 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1937 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1938 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1939 set_byte_order_funcs
[type
->declaration
->id
](type
,
1945 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
1947 struct bt_ctf_field_type
*copy
= NULL
;
1953 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
1959 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1961 struct bt_ctf_field_type_integer
*integer
;
1967 integer
= container_of(
1968 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1969 struct bt_ctf_field_type_integer
, parent
);
1970 if (integer
->mapped_clock
) {
1971 bt_ctf_clock_put(integer
->mapped_clock
);
1977 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1979 struct bt_ctf_field_type_enumeration
*enumeration
;
1985 enumeration
= container_of(
1986 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1987 struct bt_ctf_field_type_enumeration
, parent
);
1988 g_ptr_array_free(enumeration
->entries
, TRUE
);
1989 bt_ctf_field_type_put(enumeration
->container
);
1990 g_free(enumeration
);
1994 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1996 struct bt_ctf_field_type_floating_point
*floating_point
;
2002 floating_point
= container_of(
2003 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2004 struct bt_ctf_field_type_floating_point
, parent
);
2005 g_free(floating_point
);
2009 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
2011 struct bt_ctf_field_type_structure
*structure
;
2017 structure
= container_of(
2018 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2019 struct bt_ctf_field_type_structure
, parent
);
2020 g_ptr_array_free(structure
->fields
, TRUE
);
2021 g_hash_table_destroy(structure
->field_name_to_index
);
2026 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
2028 struct bt_ctf_field_type_variant
*variant
;
2034 variant
= container_of(
2035 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2036 struct bt_ctf_field_type_variant
, parent
);
2037 g_ptr_array_free(variant
->fields
, TRUE
);
2038 g_hash_table_destroy(variant
->field_name_to_index
);
2039 g_string_free(variant
->tag_name
, TRUE
);
2040 bt_ctf_field_type_put(&variant
->tag
->parent
);
2045 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2047 struct bt_ctf_field_type_array
*array
;
2053 array
= container_of(
2054 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2055 struct bt_ctf_field_type_array
, parent
);
2056 bt_ctf_field_type_put(array
->element_type
);
2061 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2063 struct bt_ctf_field_type_sequence
*sequence
;
2069 sequence
= container_of(
2070 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2071 struct bt_ctf_field_type_sequence
, parent
);
2072 bt_ctf_field_type_put(sequence
->element_type
);
2073 g_string_free(sequence
->length_field_name
, TRUE
);
2078 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2080 struct bt_ctf_field_type_string
*string
;
2086 string
= container_of(
2087 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2088 struct bt_ctf_field_type_string
, parent
);
2093 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2099 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2101 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2102 type
, struct bt_ctf_field_type_enumeration
, parent
);
2104 generic_field_type_freeze(type
);
2105 bt_ctf_field_type_freeze(enumeration_type
->container
);
2109 void freeze_structure_field(struct structure_field
*field
)
2111 bt_ctf_field_type_freeze(field
->type
);
2115 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2117 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2118 type
, struct bt_ctf_field_type_structure
, parent
);
2120 generic_field_type_freeze(type
);
2121 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
2126 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2128 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2129 type
, struct bt_ctf_field_type_variant
, parent
);
2131 generic_field_type_freeze(type
);
2132 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
2137 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2139 struct bt_ctf_field_type_array
*array_type
= container_of(
2140 type
, struct bt_ctf_field_type_array
, parent
);
2142 generic_field_type_freeze(type
);
2143 bt_ctf_field_type_freeze(array_type
->element_type
);
2147 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2149 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2150 type
, struct bt_ctf_field_type_sequence
, parent
);
2152 generic_field_type_freeze(type
);
2153 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2157 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2159 const char *encoding_string
;
2162 case CTF_STRING_NONE
:
2163 encoding_string
= "none";
2165 case CTF_STRING_ASCII
:
2166 encoding_string
= "ASCII";
2168 case CTF_STRING_UTF8
:
2169 encoding_string
= "UTF8";
2172 encoding_string
= "unknown";
2176 return encoding_string
;
2180 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2182 const char *base_string
;
2185 case BT_CTF_INTEGER_BASE_DECIMAL
:
2186 base_string
= "decimal";
2188 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2189 base_string
= "hexadecimal";
2191 case BT_CTF_INTEGER_BASE_OCTAL
:
2192 base_string
= "octal";
2194 case BT_CTF_INTEGER_BASE_BINARY
:
2195 base_string
= "binary";
2198 base_string
= "unknown";
2206 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2207 struct metadata_context
*context
)
2209 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2210 struct bt_ctf_field_type_integer
, parent
);
2213 g_string_append_printf(context
->string
,
2214 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2215 integer
->declaration
.len
, type
->declaration
->alignment
,
2216 (integer
->declaration
.signedness
? "true" : "false"),
2217 get_encoding_string(integer
->declaration
.encoding
),
2218 get_integer_base_string(integer
->declaration
.base
),
2219 get_byte_order_string(integer
->declaration
.byte_order
));
2220 if (integer
->mapped_clock
) {
2221 const char *clock_name
= bt_ctf_clock_get_name(
2222 integer
->mapped_clock
);
2229 g_string_append_printf(context
->string
,
2230 "; map = clock.%s.value", clock_name
);
2233 g_string_append(context
->string
, "; }");
2239 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2240 struct metadata_context
*context
)
2244 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2245 struct bt_ctf_field_type_enumeration
, parent
);
2246 struct bt_ctf_field_type
*container_type
;
2247 int container_signed
;
2249 ret
= bt_ctf_field_type_validate(type
);
2254 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2255 if (!container_type
) {
2260 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2261 if (container_signed
< 0) {
2262 ret
= container_signed
;
2263 goto error_put_container_type
;
2266 g_string_append(context
->string
, "enum : ");
2267 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2269 goto error_put_container_type
;
2272 g_string_append(context
->string
, " { ");
2273 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2274 struct enumeration_mapping
*mapping
=
2275 enumeration
->entries
->pdata
[entry
];
2277 if (container_signed
) {
2278 if (mapping
->range_start
._signed
==
2279 mapping
->range_end
._signed
) {
2280 g_string_append_printf(context
->string
,
2281 "\"%s\" = %" PRId64
,
2282 g_quark_to_string(mapping
->string
),
2283 mapping
->range_start
._signed
);
2285 g_string_append_printf(context
->string
,
2286 "\"%s\" = %" PRId64
" ... %" PRId64
,
2287 g_quark_to_string(mapping
->string
),
2288 mapping
->range_start
._signed
,
2289 mapping
->range_end
._signed
);
2292 if (mapping
->range_start
._unsigned
==
2293 mapping
->range_end
._unsigned
) {
2294 g_string_append_printf(context
->string
,
2295 "\"%s\" = %" PRIu64
,
2296 g_quark_to_string(mapping
->string
),
2297 mapping
->range_start
._unsigned
);
2299 g_string_append_printf(context
->string
,
2300 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2301 g_quark_to_string(mapping
->string
),
2302 mapping
->range_start
._unsigned
,
2303 mapping
->range_end
._unsigned
);
2307 g_string_append(context
->string
,
2308 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2312 if (context
->field_name
->len
) {
2313 g_string_append_printf(context
->string
, " %s",
2314 context
->field_name
->str
);
2315 g_string_assign(context
->field_name
, "");
2317 error_put_container_type
:
2318 bt_ctf_field_type_put(container_type
);
2324 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2325 struct metadata_context
*context
)
2327 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2328 type
, struct bt_ctf_field_type_floating_point
, parent
);
2330 g_string_append_printf(context
->string
,
2331 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2332 floating_point
->declaration
.exp
->len
,
2333 floating_point
->declaration
.mantissa
->len
+ 1,
2334 get_byte_order_string(floating_point
->declaration
.byte_order
),
2335 type
->declaration
->alignment
);
2340 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2341 struct metadata_context
*context
)
2344 unsigned int indent
;
2346 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2347 struct bt_ctf_field_type_structure
, parent
);
2348 GString
*structure_field_name
= context
->field_name
;
2350 context
->field_name
= g_string_new("");
2352 context
->current_indentation_level
++;
2353 g_string_append(context
->string
, "struct {\n");
2355 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2356 struct structure_field
*field
;
2358 for (indent
= 0; indent
< context
->current_indentation_level
;
2360 g_string_append_c(context
->string
, '\t');
2363 field
= structure
->fields
->pdata
[i
];
2364 g_string_assign(context
->field_name
,
2365 g_quark_to_string(field
->name
));
2366 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2371 if (context
->field_name
->len
) {
2372 g_string_append_printf(context
->string
, " %s",
2373 context
->field_name
->str
);
2375 g_string_append(context
->string
, ";\n");
2378 context
->current_indentation_level
--;
2379 for (indent
= 0; indent
< context
->current_indentation_level
;
2381 g_string_append_c(context
->string
, '\t');
2384 g_string_append_printf(context
->string
, "} align(%zu)",
2385 type
->declaration
->alignment
);
2387 g_string_free(context
->field_name
, TRUE
);
2388 context
->field_name
= structure_field_name
;
2393 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2394 struct metadata_context
*context
)
2397 unsigned int indent
;
2399 struct bt_ctf_field_type_variant
*variant
= container_of(
2400 type
, struct bt_ctf_field_type_variant
, parent
);
2401 GString
*variant_field_name
= context
->field_name
;
2403 context
->field_name
= g_string_new("");
2404 if (variant
->tag_name
->len
> 0) {
2405 g_string_append_printf(context
->string
,
2406 "variant <%s> {\n", variant
->tag_name
->str
);
2408 g_string_append(context
->string
, "variant {\n");
2411 context
->current_indentation_level
++;
2412 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2413 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2415 g_string_assign(context
->field_name
,
2416 g_quark_to_string(field
->name
));
2417 for (indent
= 0; indent
< context
->current_indentation_level
;
2419 g_string_append_c(context
->string
, '\t');
2422 g_string_assign(context
->field_name
,
2423 g_quark_to_string(field
->name
));
2424 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2429 if (context
->field_name
->len
) {
2430 g_string_append_printf(context
->string
, " %s;",
2431 context
->field_name
->str
);
2434 g_string_append_c(context
->string
, '\n');
2437 context
->current_indentation_level
--;
2438 for (indent
= 0; indent
< context
->current_indentation_level
;
2440 g_string_append_c(context
->string
, '\t');
2443 g_string_append(context
->string
, "}");
2445 g_string_free(context
->field_name
, TRUE
);
2446 context
->field_name
= variant_field_name
;
2451 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2452 struct metadata_context
*context
)
2455 struct bt_ctf_field_type_array
*array
= container_of(type
,
2456 struct bt_ctf_field_type_array
, parent
);
2458 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2463 if (context
->field_name
->len
) {
2464 g_string_append_printf(context
->string
, " %s[%u]",
2465 context
->field_name
->str
, array
->length
);
2466 g_string_assign(context
->field_name
, "");
2468 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2475 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2476 struct metadata_context
*context
)
2479 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2480 type
, struct bt_ctf_field_type_sequence
, parent
);
2482 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2487 if (context
->field_name
->len
) {
2488 g_string_append_printf(context
->string
, " %s[%s]",
2489 context
->field_name
->str
,
2490 sequence
->length_field_name
->str
);
2491 g_string_assign(context
->field_name
, "");
2493 g_string_append_printf(context
->string
, "[%s]",
2494 sequence
->length_field_name
->str
);
2501 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2502 struct metadata_context
*context
)
2504 struct bt_ctf_field_type_string
*string
= container_of(
2505 type
, struct bt_ctf_field_type_string
, parent
);
2507 g_string_append_printf(context
->string
,
2508 "string { encoding = %s; }",
2509 get_encoding_string(string
->declaration
.encoding
));
2514 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2515 int byte_order
, int set_native
)
2517 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2518 struct bt_ctf_field_type_integer
, parent
);
2521 integer_type
->declaration
.byte_order
=
2522 integer_type
->declaration
.byte_order
== 0 ?
2523 byte_order
: integer_type
->declaration
.byte_order
;
2525 integer_type
->declaration
.byte_order
= byte_order
;
2530 void bt_ctf_field_type_enumeration_set_byte_order(
2531 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2533 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2534 struct bt_ctf_field_type_enumeration
, parent
);
2536 /* Safe to assume that container is an integer */
2537 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2538 byte_order
, set_native
);
2542 void bt_ctf_field_type_floating_point_set_byte_order(
2543 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2545 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2546 container_of(type
, struct bt_ctf_field_type_floating_point
,
2550 floating_point_type
->declaration
.byte_order
=
2551 floating_point_type
->declaration
.byte_order
== 0 ?
2553 floating_point_type
->declaration
.byte_order
;
2554 floating_point_type
->sign
.byte_order
=
2555 floating_point_type
->sign
.byte_order
== 0 ?
2556 byte_order
: floating_point_type
->sign
.byte_order
;
2557 floating_point_type
->mantissa
.byte_order
=
2558 floating_point_type
->mantissa
.byte_order
== 0 ?
2559 byte_order
: floating_point_type
->mantissa
.byte_order
;
2560 floating_point_type
->exp
.byte_order
=
2561 floating_point_type
->exp
.byte_order
== 0 ?
2562 byte_order
: floating_point_type
->exp
.byte_order
;
2564 floating_point_type
->declaration
.byte_order
= byte_order
;
2565 floating_point_type
->sign
.byte_order
= byte_order
;
2566 floating_point_type
->mantissa
.byte_order
= byte_order
;
2567 floating_point_type
->exp
.byte_order
= byte_order
;
2572 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2573 int byte_order
, int set_native
)
2576 struct bt_ctf_field_type_structure
*structure_type
=
2577 container_of(type
, struct bt_ctf_field_type_structure
,
2580 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2581 struct structure_field
*field
= g_ptr_array_index(
2582 structure_type
->fields
, i
);
2583 struct bt_ctf_field_type
*field_type
= field
->type
;
2585 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2586 set_byte_order_funcs
[field_type
->declaration
->id
](
2587 field_type
, byte_order
, set_native
);
2593 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2594 int byte_order
, int set_native
)
2597 struct bt_ctf_field_type_variant
*variant_type
=
2598 container_of(type
, struct bt_ctf_field_type_variant
,
2601 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2602 struct structure_field
*field
= g_ptr_array_index(
2603 variant_type
->fields
, i
);
2604 struct bt_ctf_field_type
*field_type
= field
->type
;
2606 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2607 set_byte_order_funcs
[field_type
->declaration
->id
](
2608 field_type
, byte_order
, set_native
);
2614 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2615 int byte_order
, int set_native
)
2617 struct bt_ctf_field_type_array
*array_type
=
2618 container_of(type
, struct bt_ctf_field_type_array
,
2621 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2622 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2623 array_type
->element_type
, byte_order
, set_native
);
2628 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2629 int byte_order
, int set_native
)
2631 struct bt_ctf_field_type_sequence
*sequence_type
=
2632 container_of(type
, struct bt_ctf_field_type_sequence
,
2635 if (set_byte_order_funcs
[
2636 sequence_type
->element_type
->declaration
->id
]) {
2637 set_byte_order_funcs
[
2638 sequence_type
->element_type
->declaration
->id
](
2639 sequence_type
->element_type
, byte_order
, set_native
);
2644 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2645 struct bt_ctf_field_type
*type
)
2647 struct bt_ctf_field_type
*copy
;
2648 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
2650 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
2651 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
2656 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
2658 copy_integer
->declaration
= integer
->declaration
;
2659 if (integer
->mapped_clock
) {
2660 bt_ctf_clock_get(integer
->mapped_clock
);
2661 copy_integer
->mapped_clock
= integer
->mapped_clock
;
2668 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
2669 struct bt_ctf_field_type
*type
)
2672 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
2673 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
2675 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
2678 /* Copy the source enumeration's container */
2679 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
2680 if (!copy_container
) {
2684 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
2688 copy_enumeration
= container_of(copy
,
2689 struct bt_ctf_field_type_enumeration
, parent
);
2691 /* Copy all enumaration entries */
2692 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
2693 struct enumeration_mapping
*mapping
= g_ptr_array_index(
2694 enumeration
->entries
, i
);
2695 struct enumeration_mapping
* copy_mapping
= g_new0(
2696 struct enumeration_mapping
, 1);
2698 if (!copy_mapping
) {
2702 *copy_mapping
= *mapping
;
2703 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
2706 copy_enumeration
->declaration
= enumeration
->declaration
;
2708 if (copy_container
) {
2709 bt_ctf_field_type_put(copy_container
);
2713 if (copy_container
) {
2714 bt_ctf_field_type_put(copy_container
);
2716 bt_ctf_field_type_put(copy
);
2721 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
2722 struct bt_ctf_field_type
*type
)
2724 struct bt_ctf_field_type
*copy
;
2725 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
2727 floating_point
= container_of(type
,
2728 struct bt_ctf_field_type_floating_point
, parent
);
2729 copy
= bt_ctf_field_type_floating_point_create();
2734 copy_float
= container_of(copy
,
2735 struct bt_ctf_field_type_floating_point
, parent
);
2736 copy_float
->declaration
= floating_point
->declaration
;
2737 copy_float
->sign
= floating_point
->sign
;
2738 copy_float
->mantissa
= floating_point
->mantissa
;
2739 copy_float
->exp
= floating_point
->exp
;
2745 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
2746 struct bt_ctf_field_type
*type
)
2749 GHashTableIter iter
;
2750 gpointer key
, value
;
2751 struct bt_ctf_field_type
*copy
;
2752 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
2754 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2756 copy
= bt_ctf_field_type_structure_create();
2761 copy_structure
= container_of(copy
,
2762 struct bt_ctf_field_type_structure
, parent
);
2764 /* Copy field_name_to_index */
2765 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
2766 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2767 g_hash_table_insert(copy_structure
->field_name_to_index
,
2771 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2772 struct structure_field
*entry
, *copy_entry
;
2773 struct bt_ctf_field_type
*copy_field
;
2775 copy_entry
= g_new0(struct structure_field
, 1);
2780 entry
= g_ptr_array_index(structure
->fields
, i
);
2781 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2787 copy_entry
->name
= entry
->name
;
2788 copy_entry
->type
= copy_field
;
2789 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
2792 copy_structure
->declaration
= structure
->declaration
;
2796 bt_ctf_field_type_put(copy
);
2801 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
2802 struct bt_ctf_field_type
*type
)
2805 GHashTableIter iter
;
2806 gpointer key
, value
;
2807 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
2808 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
2810 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2813 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
2819 copy
= bt_ctf_field_type_variant_create(copy_tag
,
2820 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
2825 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
2828 /* Copy field_name_to_index */
2829 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
2830 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2831 g_hash_table_insert(copy_variant
->field_name_to_index
,
2835 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2836 struct structure_field
*entry
, *copy_entry
;
2837 struct bt_ctf_field_type
*copy_field
;
2839 copy_entry
= g_new0(struct structure_field
, 1);
2844 entry
= g_ptr_array_index(variant
->fields
, i
);
2845 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2851 copy_entry
->name
= entry
->name
;
2852 copy_entry
->type
= copy_field
;
2853 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
2856 copy_variant
->declaration
= variant
->declaration
;
2859 bt_ctf_field_type_put(copy_tag
);
2865 bt_ctf_field_type_put(copy_tag
);
2868 bt_ctf_field_type_put(copy
);
2873 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
2874 struct bt_ctf_field_type
*type
)
2876 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2877 struct bt_ctf_field_type_array
*array
, *copy_array
;
2879 array
= container_of(type
, struct bt_ctf_field_type_array
,
2881 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
2882 if (!copy_element
) {
2886 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
2891 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
2893 copy_array
->declaration
= array
->declaration
;
2896 bt_ctf_field_type_put(copy_element
);
2903 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
2904 struct bt_ctf_field_type
*type
)
2906 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2907 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
2909 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2911 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
2912 if (!copy_element
) {
2916 copy
= bt_ctf_field_type_sequence_create(copy_element
,
2917 sequence
->length_field_name
->len
?
2918 sequence
->length_field_name
->str
: NULL
);
2923 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
2925 copy_sequence
->declaration
= sequence
->declaration
;
2928 bt_ctf_field_type_put(copy_element
);
2935 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
2936 struct bt_ctf_field_type
*type
)
2938 struct bt_ctf_field_type
*copy
;
2939 struct bt_ctf_field_type_string
*string
, *copy_string
;
2941 copy
= bt_ctf_field_type_string_create();
2946 string
= container_of(type
, struct bt_ctf_field_type_string
,
2948 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
2950 copy_string
->declaration
= string
->declaration
;