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
)) {
1179 int bt_ctf_field_type_structure_get_field_count(
1180 struct bt_ctf_field_type
*type
)
1183 struct bt_ctf_field_type_structure
*structure
;
1185 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1190 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1192 ret
= (int) structure
->fields
->len
;
1197 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1198 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1201 struct bt_ctf_field_type_structure
*structure
;
1202 struct structure_field
*field
;
1205 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1210 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1212 if (index
>= structure
->fields
->len
) {
1217 field
= g_ptr_array_index(structure
->fields
, index
);
1219 *field_type
= field
->type
;
1220 bt_ctf_field_type_get(field
->type
);
1223 *field_name
= g_quark_to_string(field
->name
);
1229 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1230 struct bt_ctf_field_type
*type
,
1235 struct structure_field
*field
;
1236 struct bt_ctf_field_type_structure
*structure
;
1237 struct bt_ctf_field_type
*field_type
= NULL
;
1239 if (!type
|| !name
) {
1243 name_quark
= g_quark_try_string(name
);
1248 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1250 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1251 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1255 field
= structure
->fields
->pdata
[index
];
1256 field_type
= field
->type
;
1257 bt_ctf_field_type_get(field_type
);
1262 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1263 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1265 struct bt_ctf_field_type_variant
*variant
= NULL
;
1267 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1271 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1276 variant
->parent
.declaration
= &variant
->declaration
.p
;
1277 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1278 variant
->tag_name
= g_string_new(tag_name
);
1279 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1280 variant
->fields
= g_ptr_array_new_with_free_func(
1281 (GDestroyNotify
)destroy_structure_field
);
1283 bt_ctf_field_type_get(enum_tag
);
1284 variant
->tag
= container_of(enum_tag
,
1285 struct bt_ctf_field_type_enumeration
, parent
);
1288 bt_ctf_field_type_init(&variant
->parent
);
1289 return &variant
->parent
;
1294 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1295 struct bt_ctf_field_type
*type
)
1297 struct bt_ctf_field_type_variant
*variant
;
1298 struct bt_ctf_field_type
*tag_type
= NULL
;
1300 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1304 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1305 if (!variant
->tag
) {
1309 tag_type
= &variant
->tag
->parent
;
1310 bt_ctf_field_type_get(tag_type
);
1315 const char *bt_ctf_field_type_variant_get_tag_name(
1316 struct bt_ctf_field_type
*type
)
1318 struct bt_ctf_field_type_variant
*variant
;
1319 const char *tag_name
= NULL
;
1321 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1325 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1326 if (variant
->tag_name
->len
== 0) {
1330 tag_name
= variant
->tag_name
->str
;
1335 int bt_ctf_field_type_variant_set_tag_name(
1336 struct bt_ctf_field_type
*type
, const char *name
)
1339 struct bt_ctf_field_type_variant
*variant
;
1341 if (!type
|| type
->frozen
||
1342 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1343 bt_ctf_validate_identifier(name
)) {
1348 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1349 g_string_assign(variant
->tag_name
, name
);
1354 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1355 struct bt_ctf_field_type
*field_type
,
1356 const char *field_name
)
1360 struct bt_ctf_field_type_variant
*variant
;
1361 GQuark field_name_quark
= g_quark_from_string(field_name
);
1363 if (!type
|| !field_type
|| type
->frozen
||
1364 bt_ctf_validate_identifier(field_name
) ||
1365 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1366 bt_ctf_field_type_validate(field_type
)) {
1371 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1373 /* The user has explicitly provided a tag; validate against it. */
1377 /* Make sure this name is present in the enum tag */
1378 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1379 struct enumeration_mapping
*mapping
=
1380 g_ptr_array_index(variant
->tag
->entries
, i
);
1382 if (mapping
->string
== field_name_quark
) {
1389 /* Validation failed */
1395 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1396 field_type
, field_name
)) {
1404 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1405 struct bt_ctf_field_type
*type
,
1406 const char *field_name
)
1410 struct structure_field
*field
;
1411 struct bt_ctf_field_type_variant
*variant
;
1412 struct bt_ctf_field_type
*field_type
= NULL
;
1414 if (!type
|| !field_name
) {
1418 name_quark
= g_quark_try_string(field_name
);
1423 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1424 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1425 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1429 field
= g_ptr_array_index(variant
->fields
, index
);
1430 field_type
= field
->type
;
1431 bt_ctf_field_type_get(field_type
);
1436 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1437 struct bt_ctf_field_type
*type
,
1438 struct bt_ctf_field
*tag
)
1440 const char *enum_value
;
1441 struct bt_ctf_field_type
*field_type
= NULL
;
1443 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1447 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1452 /* Already increments field_type's reference count */
1453 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1459 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1462 struct bt_ctf_field_type_variant
*variant
;
1464 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1469 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1471 ret
= (int) variant
->fields
->len
;
1477 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1478 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1481 struct bt_ctf_field_type_variant
*variant
;
1482 struct structure_field
*field
;
1485 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1490 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1492 if (index
>= variant
->fields
->len
) {
1497 field
= g_ptr_array_index(variant
->fields
, index
);
1499 *field_type
= field
->type
;
1500 bt_ctf_field_type_get(field
->type
);
1503 *field_name
= g_quark_to_string(field
->name
);
1509 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1510 struct bt_ctf_field_type
*element_type
,
1511 unsigned int length
)
1513 struct bt_ctf_field_type_array
*array
= NULL
;
1515 if (!element_type
|| length
== 0 ||
1516 bt_ctf_field_type_validate(element_type
)) {
1520 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1525 array
->parent
.declaration
= &array
->declaration
.p
;
1526 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1528 bt_ctf_field_type_get(element_type
);
1529 array
->element_type
= element_type
;
1530 array
->length
= length
;
1531 bt_ctf_field_type_init(&array
->parent
);
1532 return &array
->parent
;
1537 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1538 struct bt_ctf_field_type
*type
)
1540 struct bt_ctf_field_type
*ret
= NULL
;
1541 struct bt_ctf_field_type_array
*array
;
1543 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1547 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1548 ret
= array
->element_type
;
1549 bt_ctf_field_type_get(ret
);
1554 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1557 struct bt_ctf_field_type_array
*array
;
1559 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1564 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1565 ret
= (int64_t) array
->length
;
1570 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1571 struct bt_ctf_field_type
*element_type
,
1572 const char *length_field_name
)
1574 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1576 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1577 bt_ctf_field_type_validate(element_type
)) {
1581 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1586 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1587 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1588 bt_ctf_field_type_get(element_type
);
1589 sequence
->element_type
= element_type
;
1590 sequence
->length_field_name
= g_string_new(length_field_name
);
1591 bt_ctf_field_type_init(&sequence
->parent
);
1592 return &sequence
->parent
;
1597 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1598 struct bt_ctf_field_type
*type
)
1600 struct bt_ctf_field_type
*ret
= NULL
;
1601 struct bt_ctf_field_type_sequence
*sequence
;
1603 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1607 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1609 ret
= sequence
->element_type
;
1610 bt_ctf_field_type_get(ret
);
1615 const char *bt_ctf_field_type_sequence_get_length_field_name(
1616 struct bt_ctf_field_type
*type
)
1618 const char *ret
= NULL
;
1619 struct bt_ctf_field_type_sequence
*sequence
;
1621 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1625 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1627 ret
= sequence
->length_field_name
->str
;
1632 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1634 struct bt_ctf_field_type_string
*string
=
1635 g_new0(struct bt_ctf_field_type_string
, 1);
1641 string
->parent
.declaration
= &string
->declaration
.p
;
1642 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1643 bt_ctf_field_type_init(&string
->parent
);
1644 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1645 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1646 return &string
->parent
;
1649 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1650 struct bt_ctf_field_type
*type
)
1652 struct bt_ctf_field_type_string
*string
;
1653 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1655 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1659 string
= container_of(type
, struct bt_ctf_field_type_string
,
1661 ret
= string
->declaration
.encoding
;
1666 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1667 enum ctf_string_encoding encoding
)
1670 struct bt_ctf_field_type_string
*string
;
1672 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1673 (encoding
!= CTF_STRING_UTF8
&&
1674 encoding
!= CTF_STRING_ASCII
)) {
1679 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1680 string
->declaration
.encoding
= encoding
;
1685 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1688 enum ctf_type_id type_id
;
1696 ret
= (int) type
->declaration
->alignment
;
1700 type_id
= bt_ctf_field_type_get_type_id(type
);
1702 case CTF_TYPE_SEQUENCE
:
1704 struct bt_ctf_field_type
*element
=
1705 bt_ctf_field_type_sequence_get_element_type(type
);
1712 ret
= bt_ctf_field_type_get_alignment(element
);
1713 bt_ctf_field_type_put(element
);
1716 case CTF_TYPE_ARRAY
:
1718 struct bt_ctf_field_type
*element
=
1719 bt_ctf_field_type_array_get_element_type(type
);
1726 ret
= bt_ctf_field_type_get_alignment(element
);
1727 bt_ctf_field_type_put(element
);
1730 case CTF_TYPE_STRUCT
:
1732 int i
, element_count
;
1734 element_count
= bt_ctf_field_type_structure_get_field_count(
1736 if (element_count
< 0) {
1737 ret
= element_count
;
1741 for (i
= 0; i
< element_count
; i
++) {
1742 struct bt_ctf_field_type
*field
;
1743 int field_alignment
;
1745 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
1752 field_alignment
= bt_ctf_field_type_get_alignment(
1754 bt_ctf_field_type_put(field
);
1755 if (field_alignment
< 0) {
1756 ret
= field_alignment
;
1760 type
->declaration
->alignment
= MAX(field_alignment
,
1761 type
->declaration
->alignment
);
1763 ret
= (int) type
->declaration
->alignment
;
1766 case CTF_TYPE_UNKNOWN
:
1770 ret
= (int) type
->declaration
->alignment
;
1777 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1778 unsigned int alignment
)
1781 enum ctf_type_id type_id
;
1783 /* Alignment must be bit-aligned (1) or byte aligned */
1784 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1789 type_id
= bt_ctf_field_type_get_type_id(type
);
1790 if (type_id
== CTF_TYPE_UNKNOWN
) {
1795 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1796 alignment
!= CHAR_BIT
) {
1801 if (type_id
== CTF_TYPE_STRUCT
|| type_id
== CTF_TYPE_VARIANT
||
1802 type_id
== CTF_TYPE_SEQUENCE
|| type_id
== CTF_TYPE_ARRAY
) {
1803 /* Setting an alignment on these types makes no sense */
1808 type
->declaration
->alignment
= alignment
;
1814 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1815 struct bt_ctf_field_type
*type
)
1817 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1818 int internal_byte_order
= -1;
1824 switch (type
->declaration
->id
) {
1825 case CTF_TYPE_INTEGER
:
1827 struct bt_ctf_field_type_integer
*integer
= container_of(
1828 type
, struct bt_ctf_field_type_integer
, parent
);
1829 internal_byte_order
= integer
->declaration
.byte_order
;
1832 case CTF_TYPE_FLOAT
:
1834 struct bt_ctf_field_type_floating_point
*floating_point
=
1836 struct bt_ctf_field_type_floating_point
,
1838 internal_byte_order
= floating_point
->declaration
.byte_order
;
1845 switch (internal_byte_order
) {
1847 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1850 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1853 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1856 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1862 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1863 enum bt_ctf_byte_order byte_order
)
1866 int internal_byte_order
;
1867 enum ctf_type_id type_id
;
1869 if (!type
|| type
->frozen
) {
1874 switch (byte_order
) {
1875 case BT_CTF_BYTE_ORDER_NATIVE
:
1876 /* Leave unset. Will be initialized by parent. */
1877 internal_byte_order
= 0;
1879 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1880 internal_byte_order
= LITTLE_ENDIAN
;
1882 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1883 case BT_CTF_BYTE_ORDER_NETWORK
:
1884 internal_byte_order
= BIG_ENDIAN
;
1891 type_id
= type
->declaration
->id
;
1892 if (set_byte_order_funcs
[type_id
]) {
1893 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1899 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1900 struct bt_ctf_field_type
*type
)
1903 return CTF_TYPE_UNKNOWN
;
1906 return type
->declaration
->id
;
1909 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1915 bt_ctf_ref_get(&type
->ref_count
);
1918 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1924 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1928 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1938 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1939 struct bt_ctf_field_type_variant
*variant
,
1942 struct bt_ctf_field_type
*type
= NULL
;
1943 GQuark field_name_quark
;
1945 struct structure_field
*field_entry
;
1946 struct range_overlap_query query
= {
1947 .range_start
._signed
= tag_value
,
1948 .range_end
._signed
= tag_value
,
1949 .mapping_name
= 0, .overlaps
= 0};
1951 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1953 if (!query
.overlaps
) {
1957 field_name_quark
= query
.mapping_name
;
1958 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1959 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1963 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1964 type
= field_entry
->type
;
1970 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1971 struct bt_ctf_field_type_variant
*variant
,
1974 struct bt_ctf_field_type
*type
= NULL
;
1975 GQuark field_name_quark
;
1977 struct structure_field
*field_entry
;
1978 struct range_overlap_query query
= {
1979 .range_start
._unsigned
= tag_value
,
1980 .range_end
._unsigned
= tag_value
,
1981 .mapping_name
= 0, .overlaps
= 0};
1983 g_ptr_array_foreach(variant
->tag
->entries
,
1984 check_ranges_overlap_unsigned
,
1986 if (!query
.overlaps
) {
1990 field_name_quark
= query
.mapping_name
;
1991 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1992 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1996 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1997 type
= field_entry
->type
;
2003 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2004 struct metadata_context
*context
)
2008 if (!type
|| !context
) {
2013 ret
= type
->serialize(type
, context
);
2019 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2026 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2027 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2028 set_byte_order_funcs
[type
->declaration
->id
](type
,
2034 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2036 struct bt_ctf_field_type
*copy
= NULL
;
2042 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2048 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2050 struct bt_ctf_field_path
*field_path
= NULL
;
2052 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2057 field_path
->root
= CTF_NODE_UNKNOWN
;
2058 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2059 if (!field_path
->path_indexes
) {
2060 bt_ctf_field_path_destroy(field_path
);
2069 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2070 struct bt_ctf_field_path
*path
)
2072 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2078 new_path
->root
= path
->root
;
2079 g_array_insert_vals(new_path
->path_indexes
, 0,
2080 path
->path_indexes
->data
, path
->path_indexes
->len
);
2086 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2092 if (path
->path_indexes
) {
2093 g_array_free(path
->path_indexes
, TRUE
);
2099 int bt_ctf_field_type_structure_get_field_name_index(
2100 struct bt_ctf_field_type
*type
, const char *name
)
2105 struct bt_ctf_field_type_structure
*structure
;
2107 if (!type
|| !name
||
2108 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2113 name_quark
= g_quark_try_string(name
);
2119 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2121 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2122 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2132 int bt_ctf_field_type_variant_get_field_name_index(
2133 struct bt_ctf_field_type
*type
, const char *name
)
2138 struct bt_ctf_field_type_variant
*variant
;
2140 if (!type
|| !name
||
2141 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2146 name_quark
= g_quark_try_string(name
);
2152 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2154 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2155 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2165 int bt_ctf_field_type_sequence_set_length_field_path(
2166 struct bt_ctf_field_type
*type
,
2167 struct bt_ctf_field_path
*path
)
2170 struct bt_ctf_field_type_sequence
*sequence
;
2172 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2177 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2179 if (sequence
->length_field_path
) {
2180 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2182 sequence
->length_field_path
= path
;
2188 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2189 struct bt_ctf_field_path
*path
)
2192 struct bt_ctf_field_type_variant
*variant
;
2194 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2199 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2201 if (variant
->tag_path
) {
2202 bt_ctf_field_path_destroy(variant
->tag_path
);
2204 variant
->tag_path
= path
;
2210 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2211 struct bt_ctf_field_type
*tag
)
2214 struct bt_ctf_field_type_variant
*variant
;
2216 if (!type
|| !tag
|| type
->frozen
||
2217 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2222 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2224 bt_ctf_field_type_get(tag
);
2226 bt_ctf_field_type_put(&variant
->tag
->parent
);
2228 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2235 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
2237 struct bt_ctf_field_type_integer
*integer
;
2243 integer
= container_of(
2244 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2245 struct bt_ctf_field_type_integer
, parent
);
2246 if (integer
->mapped_clock
) {
2247 bt_ctf_clock_put(integer
->mapped_clock
);
2253 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
2255 struct bt_ctf_field_type_enumeration
*enumeration
;
2261 enumeration
= container_of(
2262 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2263 struct bt_ctf_field_type_enumeration
, parent
);
2264 g_ptr_array_free(enumeration
->entries
, TRUE
);
2265 bt_ctf_field_type_put(enumeration
->container
);
2266 g_free(enumeration
);
2270 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
2272 struct bt_ctf_field_type_floating_point
*floating_point
;
2278 floating_point
= container_of(
2279 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2280 struct bt_ctf_field_type_floating_point
, parent
);
2281 g_free(floating_point
);
2285 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
2287 struct bt_ctf_field_type_structure
*structure
;
2293 structure
= container_of(
2294 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2295 struct bt_ctf_field_type_structure
, parent
);
2296 g_ptr_array_free(structure
->fields
, TRUE
);
2297 g_hash_table_destroy(structure
->field_name_to_index
);
2302 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
2304 struct bt_ctf_field_type_variant
*variant
;
2310 variant
= container_of(
2311 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2312 struct bt_ctf_field_type_variant
, parent
);
2313 g_ptr_array_free(variant
->fields
, TRUE
);
2314 g_hash_table_destroy(variant
->field_name_to_index
);
2315 g_string_free(variant
->tag_name
, TRUE
);
2316 bt_ctf_field_type_put(&variant
->tag
->parent
);
2317 bt_ctf_field_path_destroy(variant
->tag_path
);
2322 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2324 struct bt_ctf_field_type_array
*array
;
2330 array
= container_of(
2331 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2332 struct bt_ctf_field_type_array
, parent
);
2333 bt_ctf_field_type_put(array
->element_type
);
2338 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2340 struct bt_ctf_field_type_sequence
*sequence
;
2346 sequence
= container_of(
2347 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2348 struct bt_ctf_field_type_sequence
, parent
);
2349 bt_ctf_field_type_put(sequence
->element_type
);
2350 g_string_free(sequence
->length_field_name
, TRUE
);
2351 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2356 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2358 struct bt_ctf_field_type_string
*string
;
2364 string
= container_of(
2365 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2366 struct bt_ctf_field_type_string
, parent
);
2371 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2377 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2379 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2380 type
, struct bt_ctf_field_type_enumeration
, parent
);
2382 generic_field_type_freeze(type
);
2383 bt_ctf_field_type_freeze(enumeration_type
->container
);
2387 void freeze_structure_field(struct structure_field
*field
)
2389 bt_ctf_field_type_freeze(field
->type
);
2393 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2395 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2396 type
, struct bt_ctf_field_type_structure
, parent
);
2398 /* Cache the alignment */
2399 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2400 generic_field_type_freeze(type
);
2401 g_ptr_array_foreach(structure_type
->fields
,
2402 (GFunc
) freeze_structure_field
, NULL
);
2406 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2408 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2409 type
, struct bt_ctf_field_type_variant
, parent
);
2411 /* Cache the alignment */
2412 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2413 generic_field_type_freeze(type
);
2414 g_ptr_array_foreach(variant_type
->fields
,
2415 (GFunc
) freeze_structure_field
, NULL
);
2419 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2421 struct bt_ctf_field_type_array
*array_type
= container_of(
2422 type
, struct bt_ctf_field_type_array
, parent
);
2424 /* Cache the alignment */
2425 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2426 generic_field_type_freeze(type
);
2427 bt_ctf_field_type_freeze(array_type
->element_type
);
2431 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2433 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2434 type
, struct bt_ctf_field_type_sequence
, parent
);
2436 /* Cache the alignment */
2437 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2438 generic_field_type_freeze(type
);
2439 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2443 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2445 const char *encoding_string
;
2448 case CTF_STRING_NONE
:
2449 encoding_string
= "none";
2451 case CTF_STRING_ASCII
:
2452 encoding_string
= "ASCII";
2454 case CTF_STRING_UTF8
:
2455 encoding_string
= "UTF8";
2458 encoding_string
= "unknown";
2462 return encoding_string
;
2466 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2468 const char *base_string
;
2471 case BT_CTF_INTEGER_BASE_DECIMAL
:
2472 base_string
= "decimal";
2474 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2475 base_string
= "hexadecimal";
2477 case BT_CTF_INTEGER_BASE_OCTAL
:
2478 base_string
= "octal";
2480 case BT_CTF_INTEGER_BASE_BINARY
:
2481 base_string
= "binary";
2484 base_string
= "unknown";
2492 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2493 struct metadata_context
*context
)
2495 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2496 struct bt_ctf_field_type_integer
, parent
);
2499 g_string_append_printf(context
->string
,
2500 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2501 integer
->declaration
.len
, type
->declaration
->alignment
,
2502 (integer
->declaration
.signedness
? "true" : "false"),
2503 get_encoding_string(integer
->declaration
.encoding
),
2504 get_integer_base_string(integer
->declaration
.base
),
2505 get_byte_order_string(integer
->declaration
.byte_order
));
2506 if (integer
->mapped_clock
) {
2507 const char *clock_name
= bt_ctf_clock_get_name(
2508 integer
->mapped_clock
);
2515 g_string_append_printf(context
->string
,
2516 "; map = clock.%s.value", clock_name
);
2519 g_string_append(context
->string
, "; }");
2525 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2526 struct metadata_context
*context
)
2530 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2531 struct bt_ctf_field_type_enumeration
, parent
);
2532 struct bt_ctf_field_type
*container_type
;
2533 int container_signed
;
2535 ret
= bt_ctf_field_type_validate(type
);
2540 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2541 if (!container_type
) {
2546 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2547 if (container_signed
< 0) {
2548 ret
= container_signed
;
2549 goto error_put_container_type
;
2552 g_string_append(context
->string
, "enum : ");
2553 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2555 goto error_put_container_type
;
2558 g_string_append(context
->string
, " { ");
2559 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2560 struct enumeration_mapping
*mapping
=
2561 enumeration
->entries
->pdata
[entry
];
2563 if (container_signed
) {
2564 if (mapping
->range_start
._signed
==
2565 mapping
->range_end
._signed
) {
2566 g_string_append_printf(context
->string
,
2567 "\"%s\" = %" PRId64
,
2568 g_quark_to_string(mapping
->string
),
2569 mapping
->range_start
._signed
);
2571 g_string_append_printf(context
->string
,
2572 "\"%s\" = %" PRId64
" ... %" PRId64
,
2573 g_quark_to_string(mapping
->string
),
2574 mapping
->range_start
._signed
,
2575 mapping
->range_end
._signed
);
2578 if (mapping
->range_start
._unsigned
==
2579 mapping
->range_end
._unsigned
) {
2580 g_string_append_printf(context
->string
,
2581 "\"%s\" = %" PRIu64
,
2582 g_quark_to_string(mapping
->string
),
2583 mapping
->range_start
._unsigned
);
2585 g_string_append_printf(context
->string
,
2586 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2587 g_quark_to_string(mapping
->string
),
2588 mapping
->range_start
._unsigned
,
2589 mapping
->range_end
._unsigned
);
2593 g_string_append(context
->string
,
2594 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2598 if (context
->field_name
->len
) {
2599 g_string_append_printf(context
->string
, " %s",
2600 context
->field_name
->str
);
2601 g_string_assign(context
->field_name
, "");
2603 error_put_container_type
:
2604 bt_ctf_field_type_put(container_type
);
2610 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2611 struct metadata_context
*context
)
2613 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2614 type
, struct bt_ctf_field_type_floating_point
, parent
);
2616 g_string_append_printf(context
->string
,
2617 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2618 floating_point
->declaration
.exp
->len
,
2619 floating_point
->declaration
.mantissa
->len
+ 1,
2620 get_byte_order_string(floating_point
->declaration
.byte_order
),
2621 type
->declaration
->alignment
);
2626 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2627 struct metadata_context
*context
)
2630 unsigned int indent
;
2632 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2633 struct bt_ctf_field_type_structure
, parent
);
2634 GString
*structure_field_name
= context
->field_name
;
2636 context
->field_name
= g_string_new("");
2638 context
->current_indentation_level
++;
2639 g_string_append(context
->string
, "struct {\n");
2641 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2642 struct structure_field
*field
;
2644 for (indent
= 0; indent
< context
->current_indentation_level
;
2646 g_string_append_c(context
->string
, '\t');
2649 field
= structure
->fields
->pdata
[i
];
2650 g_string_assign(context
->field_name
,
2651 g_quark_to_string(field
->name
));
2652 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2657 if (context
->field_name
->len
) {
2658 g_string_append_printf(context
->string
, " %s",
2659 context
->field_name
->str
);
2661 g_string_append(context
->string
, ";\n");
2664 context
->current_indentation_level
--;
2665 for (indent
= 0; indent
< context
->current_indentation_level
;
2667 g_string_append_c(context
->string
, '\t');
2670 g_string_append_printf(context
->string
, "} align(%zu)",
2671 type
->declaration
->alignment
);
2673 g_string_free(context
->field_name
, TRUE
);
2674 context
->field_name
= structure_field_name
;
2679 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2680 struct metadata_context
*context
)
2683 unsigned int indent
;
2685 struct bt_ctf_field_type_variant
*variant
= container_of(
2686 type
, struct bt_ctf_field_type_variant
, parent
);
2687 GString
*variant_field_name
= context
->field_name
;
2689 context
->field_name
= g_string_new("");
2690 if (variant
->tag_name
->len
> 0) {
2691 g_string_append_printf(context
->string
,
2692 "variant <%s> {\n", variant
->tag_name
->str
);
2694 g_string_append(context
->string
, "variant {\n");
2697 context
->current_indentation_level
++;
2698 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2699 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2701 g_string_assign(context
->field_name
,
2702 g_quark_to_string(field
->name
));
2703 for (indent
= 0; indent
< context
->current_indentation_level
;
2705 g_string_append_c(context
->string
, '\t');
2708 g_string_assign(context
->field_name
,
2709 g_quark_to_string(field
->name
));
2710 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2715 if (context
->field_name
->len
) {
2716 g_string_append_printf(context
->string
, " %s;",
2717 context
->field_name
->str
);
2720 g_string_append_c(context
->string
, '\n');
2723 context
->current_indentation_level
--;
2724 for (indent
= 0; indent
< context
->current_indentation_level
;
2726 g_string_append_c(context
->string
, '\t');
2729 g_string_append(context
->string
, "}");
2731 g_string_free(context
->field_name
, TRUE
);
2732 context
->field_name
= variant_field_name
;
2737 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2738 struct metadata_context
*context
)
2741 struct bt_ctf_field_type_array
*array
= container_of(type
,
2742 struct bt_ctf_field_type_array
, parent
);
2744 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2749 if (context
->field_name
->len
) {
2750 g_string_append_printf(context
->string
, " %s[%u]",
2751 context
->field_name
->str
, array
->length
);
2752 g_string_assign(context
->field_name
, "");
2754 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2761 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2762 struct metadata_context
*context
)
2765 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2766 type
, struct bt_ctf_field_type_sequence
, parent
);
2768 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2773 if (context
->field_name
->len
) {
2774 g_string_append_printf(context
->string
, " %s[%s]",
2775 context
->field_name
->str
,
2776 sequence
->length_field_name
->str
);
2777 g_string_assign(context
->field_name
, "");
2779 g_string_append_printf(context
->string
, "[%s]",
2780 sequence
->length_field_name
->str
);
2787 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2788 struct metadata_context
*context
)
2790 struct bt_ctf_field_type_string
*string
= container_of(
2791 type
, struct bt_ctf_field_type_string
, parent
);
2793 g_string_append_printf(context
->string
,
2794 "string { encoding = %s; }",
2795 get_encoding_string(string
->declaration
.encoding
));
2800 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2801 int byte_order
, int set_native
)
2803 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2804 struct bt_ctf_field_type_integer
, parent
);
2807 integer_type
->declaration
.byte_order
=
2808 integer_type
->declaration
.byte_order
== 0 ?
2809 byte_order
: integer_type
->declaration
.byte_order
;
2811 integer_type
->declaration
.byte_order
= byte_order
;
2816 void bt_ctf_field_type_enumeration_set_byte_order(
2817 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2819 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2820 struct bt_ctf_field_type_enumeration
, parent
);
2822 /* Safe to assume that container is an integer */
2823 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2824 byte_order
, set_native
);
2828 void bt_ctf_field_type_floating_point_set_byte_order(
2829 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2831 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2832 container_of(type
, struct bt_ctf_field_type_floating_point
,
2836 floating_point_type
->declaration
.byte_order
=
2837 floating_point_type
->declaration
.byte_order
== 0 ?
2839 floating_point_type
->declaration
.byte_order
;
2840 floating_point_type
->sign
.byte_order
=
2841 floating_point_type
->sign
.byte_order
== 0 ?
2842 byte_order
: floating_point_type
->sign
.byte_order
;
2843 floating_point_type
->mantissa
.byte_order
=
2844 floating_point_type
->mantissa
.byte_order
== 0 ?
2845 byte_order
: floating_point_type
->mantissa
.byte_order
;
2846 floating_point_type
->exp
.byte_order
=
2847 floating_point_type
->exp
.byte_order
== 0 ?
2848 byte_order
: floating_point_type
->exp
.byte_order
;
2850 floating_point_type
->declaration
.byte_order
= byte_order
;
2851 floating_point_type
->sign
.byte_order
= byte_order
;
2852 floating_point_type
->mantissa
.byte_order
= byte_order
;
2853 floating_point_type
->exp
.byte_order
= byte_order
;
2858 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2859 int byte_order
, int set_native
)
2862 struct bt_ctf_field_type_structure
*structure_type
=
2863 container_of(type
, struct bt_ctf_field_type_structure
,
2866 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2867 struct structure_field
*field
= g_ptr_array_index(
2868 structure_type
->fields
, i
);
2869 struct bt_ctf_field_type
*field_type
= field
->type
;
2871 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2872 set_byte_order_funcs
[field_type
->declaration
->id
](
2873 field_type
, byte_order
, set_native
);
2879 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2880 int byte_order
, int set_native
)
2883 struct bt_ctf_field_type_variant
*variant_type
=
2884 container_of(type
, struct bt_ctf_field_type_variant
,
2887 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2888 struct structure_field
*field
= g_ptr_array_index(
2889 variant_type
->fields
, i
);
2890 struct bt_ctf_field_type
*field_type
= field
->type
;
2892 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2893 set_byte_order_funcs
[field_type
->declaration
->id
](
2894 field_type
, byte_order
, set_native
);
2900 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2901 int byte_order
, int set_native
)
2903 struct bt_ctf_field_type_array
*array_type
=
2904 container_of(type
, struct bt_ctf_field_type_array
,
2907 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2908 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2909 array_type
->element_type
, byte_order
, set_native
);
2914 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2915 int byte_order
, int set_native
)
2917 struct bt_ctf_field_type_sequence
*sequence_type
=
2918 container_of(type
, struct bt_ctf_field_type_sequence
,
2921 if (set_byte_order_funcs
[
2922 sequence_type
->element_type
->declaration
->id
]) {
2923 set_byte_order_funcs
[
2924 sequence_type
->element_type
->declaration
->id
](
2925 sequence_type
->element_type
, byte_order
, set_native
);
2930 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2931 struct bt_ctf_field_type
*type
)
2933 struct bt_ctf_field_type
*copy
;
2934 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
2936 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
2937 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
2942 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
2944 copy_integer
->declaration
= integer
->declaration
;
2945 if (integer
->mapped_clock
) {
2946 bt_ctf_clock_get(integer
->mapped_clock
);
2947 copy_integer
->mapped_clock
= integer
->mapped_clock
;
2954 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
2955 struct bt_ctf_field_type
*type
)
2958 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
2959 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
2961 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
2964 /* Copy the source enumeration's container */
2965 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
2966 if (!copy_container
) {
2970 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
2974 copy_enumeration
= container_of(copy
,
2975 struct bt_ctf_field_type_enumeration
, parent
);
2977 /* Copy all enumaration entries */
2978 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
2979 struct enumeration_mapping
*mapping
= g_ptr_array_index(
2980 enumeration
->entries
, i
);
2981 struct enumeration_mapping
* copy_mapping
= g_new0(
2982 struct enumeration_mapping
, 1);
2984 if (!copy_mapping
) {
2988 *copy_mapping
= *mapping
;
2989 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
2992 copy_enumeration
->declaration
= enumeration
->declaration
;
2994 if (copy_container
) {
2995 bt_ctf_field_type_put(copy_container
);
2999 if (copy_container
) {
3000 bt_ctf_field_type_put(copy_container
);
3002 bt_ctf_field_type_put(copy
);
3007 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3008 struct bt_ctf_field_type
*type
)
3010 struct bt_ctf_field_type
*copy
;
3011 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3013 floating_point
= container_of(type
,
3014 struct bt_ctf_field_type_floating_point
, parent
);
3015 copy
= bt_ctf_field_type_floating_point_create();
3020 copy_float
= container_of(copy
,
3021 struct bt_ctf_field_type_floating_point
, parent
);
3022 copy_float
->declaration
= floating_point
->declaration
;
3023 copy_float
->sign
= floating_point
->sign
;
3024 copy_float
->mantissa
= floating_point
->mantissa
;
3025 copy_float
->exp
= floating_point
->exp
;
3031 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3032 struct bt_ctf_field_type
*type
)
3035 GHashTableIter iter
;
3036 gpointer key
, value
;
3037 struct bt_ctf_field_type
*copy
;
3038 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3040 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3042 copy
= bt_ctf_field_type_structure_create();
3047 copy_structure
= container_of(copy
,
3048 struct bt_ctf_field_type_structure
, parent
);
3050 /* Copy field_name_to_index */
3051 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3052 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3053 g_hash_table_insert(copy_structure
->field_name_to_index
,
3057 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3058 struct structure_field
*entry
, *copy_entry
;
3059 struct bt_ctf_field_type
*copy_field
;
3061 copy_entry
= g_new0(struct structure_field
, 1);
3066 entry
= g_ptr_array_index(structure
->fields
, i
);
3067 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3073 copy_entry
->name
= entry
->name
;
3074 copy_entry
->type
= copy_field
;
3075 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3078 copy_structure
->declaration
= structure
->declaration
;
3082 bt_ctf_field_type_put(copy
);
3087 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3088 struct bt_ctf_field_type
*type
)
3091 GHashTableIter iter
;
3092 gpointer key
, value
;
3093 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3094 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3096 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3099 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3105 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3106 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3111 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3114 /* Copy field_name_to_index */
3115 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3116 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3117 g_hash_table_insert(copy_variant
->field_name_to_index
,
3121 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3122 struct structure_field
*entry
, *copy_entry
;
3123 struct bt_ctf_field_type
*copy_field
;
3125 copy_entry
= g_new0(struct structure_field
, 1);
3130 entry
= g_ptr_array_index(variant
->fields
, i
);
3131 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3137 copy_entry
->name
= entry
->name
;
3138 copy_entry
->type
= copy_field
;
3139 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3142 copy_variant
->declaration
= variant
->declaration
;
3143 if (variant
->tag_path
) {
3144 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3146 if (!copy_variant
->tag_path
) {
3152 bt_ctf_field_type_put(copy_tag
);
3158 bt_ctf_field_type_put(copy_tag
);
3161 bt_ctf_field_type_put(copy
);
3166 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3167 struct bt_ctf_field_type
*type
)
3169 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3170 struct bt_ctf_field_type_array
*array
, *copy_array
;
3172 array
= container_of(type
, struct bt_ctf_field_type_array
,
3174 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3175 if (!copy_element
) {
3179 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3184 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3186 copy_array
->declaration
= array
->declaration
;
3189 bt_ctf_field_type_put(copy_element
);
3196 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3197 struct bt_ctf_field_type
*type
)
3199 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3200 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3202 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3204 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3205 if (!copy_element
) {
3209 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3210 sequence
->length_field_name
->len
?
3211 sequence
->length_field_name
->str
: NULL
);
3216 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3218 copy_sequence
->declaration
= sequence
->declaration
;
3219 if (sequence
->length_field_path
) {
3220 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3221 sequence
->length_field_path
);
3222 if (!copy_sequence
->length_field_path
) {
3228 bt_ctf_field_type_put(copy_element
);
3234 bt_ctf_field_type_put(copy
);
3241 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3242 struct bt_ctf_field_type
*type
)
3244 struct bt_ctf_field_type
*copy
;
3245 struct bt_ctf_field_type_string
*string
, *copy_string
;
3247 copy
= bt_ctf_field_type_string_create();
3252 string
= container_of(type
, struct bt_ctf_field_type_string
,
3254 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3256 copy_string
->declaration
= string
->declaration
;