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-writer/writer-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <babeltrace/endian.h>
38 struct range_overlap_query
{
53 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
55 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
69 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
72 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
73 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
75 bt_ctf_field_type_enumeration_destroy
,
77 bt_ctf_field_type_floating_point_destroy
,
78 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
79 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
80 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
81 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
82 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
86 void generic_field_type_freeze(struct bt_ctf_field_type
*);
88 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
99 type_freeze_func
const type_freeze_funcs
[] = {
100 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
101 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
102 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
103 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
104 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
105 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
106 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
107 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
111 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
112 struct metadata_context
*);
114 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
115 struct metadata_context
*);
117 int bt_ctf_field_type_floating_point_serialize(
118 struct bt_ctf_field_type
*, struct metadata_context
*);
120 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
121 struct metadata_context
*);
123 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
124 struct metadata_context
*);
126 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
127 struct metadata_context
*);
129 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
130 struct metadata_context
*);
132 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
133 struct metadata_context
*);
136 type_serialize_func
const type_serialize_funcs
[] = {
137 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
139 bt_ctf_field_type_enumeration_serialize
,
141 bt_ctf_field_type_floating_point_serialize
,
143 bt_ctf_field_type_structure_serialize
,
144 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
145 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
146 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
147 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
151 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
154 void bt_ctf_field_type_floating_point_set_byte_order(
155 struct bt_ctf_field_type
*, int byte_order
);
158 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
161 bt_ctf_field_type_integer_set_byte_order
,
163 bt_ctf_field_type_floating_point_set_byte_order
,
164 [CTF_TYPE_ENUM
... CTF_TYPE_SEQUENCE
] = NULL
,
168 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
174 void destroy_structure_field(struct structure_field
*field
)
177 bt_ctf_field_type_put(field
->type
);
184 void check_ranges_overlap(gpointer element
, gpointer query
)
186 struct enumeration_mapping
*mapping
= element
;
187 struct range_overlap_query
*overlap_query
= query
;
189 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
190 && overlap_query
->range_start
._signed
<=
191 mapping
->range_end
._signed
) {
192 overlap_query
->overlaps
= 1;
193 overlap_query
->mapping_name
= mapping
->string
;
196 overlap_query
->overlaps
|=
197 mapping
->string
== overlap_query
->mapping_name
;
201 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
203 struct enumeration_mapping
*mapping
= element
;
204 struct range_overlap_query
*overlap_query
= query
;
206 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
207 && overlap_query
->range_start
._unsigned
<=
208 mapping
->range_end
._unsigned
) {
209 overlap_query
->overlaps
= 1;
210 overlap_query
->mapping_name
= mapping
->string
;
213 overlap_query
->overlaps
|=
214 mapping
->string
== overlap_query
->mapping_name
;
218 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
219 struct enumeration_mapping
**b
)
221 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
225 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
226 struct enumeration_mapping
**b
)
228 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
232 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
234 enum ctf_type_id type_id
= type
->declaration
->id
;
237 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
238 (type_id
< NR_CTF_TYPES
));
240 bt_ctf_ref_init(&type
->ref_count
);
241 type
->freeze
= type_freeze_funcs
[type_id
];
242 type
->serialize
= type_serialize_funcs
[type_id
];
243 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
245 type
->declaration
->alignment
= 1;
249 int add_structure_field(GPtrArray
*fields
,
250 GHashTable
*field_name_to_index
,
251 struct bt_ctf_field_type
*field_type
,
252 const char *field_name
)
255 GQuark name_quark
= g_quark_from_string(field_name
);
256 struct structure_field
*field
;
258 /* Make sure structure does not contain a field of the same name */
259 if (g_hash_table_lookup_extended(field_name_to_index
,
260 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
265 field
= g_new0(struct structure_field
, 1);
271 bt_ctf_field_type_get(field_type
);
272 field
->name
= name_quark
;
273 field
->type
= field_type
;
274 g_hash_table_insert(field_name_to_index
,
275 (gpointer
) (unsigned long) name_quark
,
276 (gpointer
) (unsigned long) fields
->len
);
277 g_ptr_array_add(fields
, field
);
278 bt_ctf_field_type_freeze(field_type
);
284 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
286 struct bt_ctf_field_type
*type
;
287 enum ctf_type_id type_id
;
293 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
294 type_id
= type
->declaration
->id
;
295 if (type_id
<= CTF_TYPE_UNKNOWN
||
296 type_id
>= NR_CTF_TYPES
) {
300 if (type
->alias_name
) {
301 g_string_free(type
->alias_name
, TRUE
);
303 type_destroy_funcs
[type_id
](ref
);
307 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
316 if (type
->declaration
->id
== CTF_TYPE_ENUM
) {
317 struct bt_ctf_field_type_enumeration
*enumeration
=
318 container_of(type
, struct bt_ctf_field_type_enumeration
,
321 ret
= enumeration
->entries
->len
? 0 : -1;
327 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
329 struct bt_ctf_field_type_integer
*integer
=
330 g_new0(struct bt_ctf_field_type_integer
, 1);
332 if (!integer
|| size
> 64) {
336 integer
->parent
.declaration
= &integer
->declaration
.p
;
337 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
338 integer
->declaration
.len
= size
;
339 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
340 integer
->declaration
.encoding
= CTF_STRING_NONE
;
341 bt_ctf_field_type_init(&integer
->parent
);
342 return &integer
->parent
;
345 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
348 struct bt_ctf_field_type_integer
*integer
;
350 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
355 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
356 ret
= (int) integer
->declaration
.len
;
361 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
364 struct bt_ctf_field_type_integer
*integer
;
366 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
371 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
372 ret
= integer
->declaration
.signedness
;
377 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
381 struct bt_ctf_field_type_integer
*integer
;
383 if (!type
|| type
->frozen
||
384 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
389 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
390 if (is_signed
&& integer
->declaration
.len
<= 1) {
395 integer
->declaration
.signedness
= !!is_signed
;
400 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
401 struct bt_ctf_field_type
*type
)
403 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
404 struct bt_ctf_field_type_integer
*integer
;
406 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
410 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
411 ret
= integer
->declaration
.base
;
416 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
417 enum bt_ctf_integer_base base
)
421 if (!type
|| type
->frozen
||
422 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
428 case BT_CTF_INTEGER_BASE_BINARY
:
429 case BT_CTF_INTEGER_BASE_OCTAL
:
430 case BT_CTF_INTEGER_BASE_DECIMAL
:
431 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
433 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
434 struct bt_ctf_field_type_integer
, parent
);
435 integer
->declaration
.base
= base
;
445 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
446 struct bt_ctf_field_type
*type
)
448 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
449 struct bt_ctf_field_type_integer
*integer
;
451 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
455 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
456 ret
= integer
->declaration
.encoding
;
461 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
462 enum ctf_string_encoding encoding
)
465 struct bt_ctf_field_type_integer
*integer
;
467 if (!type
|| type
->frozen
||
468 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
469 (encoding
< CTF_STRING_NONE
) ||
470 (encoding
>= CTF_STRING_UNKNOWN
)) {
475 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
476 integer
->declaration
.encoding
= encoding
;
481 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
482 struct bt_ctf_field_type
*integer_container_type
)
484 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
486 if (!integer_container_type
) {
490 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
494 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
499 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
500 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
501 bt_ctf_field_type_get(integer_container_type
);
502 enumeration
->container
= integer_container_type
;
503 enumeration
->entries
= g_ptr_array_new_with_free_func(
504 (GDestroyNotify
)destroy_enumeration_mapping
);
505 bt_ctf_field_type_init(&enumeration
->parent
);
506 return &enumeration
->parent
;
512 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
513 struct bt_ctf_field_type
*type
)
515 struct bt_ctf_field_type
*container_type
= NULL
;
516 struct bt_ctf_field_type_enumeration
*enumeration_type
;
522 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
526 enumeration_type
= container_of(type
,
527 struct bt_ctf_field_type_enumeration
, parent
);
528 container_type
= enumeration_type
->container
;
529 bt_ctf_field_type_get(container_type
);
531 return container_type
;
534 int bt_ctf_field_type_enumeration_add_mapping(
535 struct bt_ctf_field_type
*type
, const char *string
,
536 int64_t range_start
, int64_t range_end
)
540 struct enumeration_mapping
*mapping
;
541 struct bt_ctf_field_type_enumeration
*enumeration
;
542 struct range_overlap_query query
;
543 char *escaped_string
;
545 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
547 (range_end
< range_start
)) {
552 if (!string
|| strlen(string
) == 0) {
557 escaped_string
= g_strescape(string
, NULL
);
558 if (!escaped_string
) {
563 mapping_name
= g_quark_from_string(escaped_string
);
564 query
= (struct range_overlap_query
) {
565 .range_start
._signed
= range_start
,
566 .range_end
._signed
= range_end
,
567 .mapping_name
= mapping_name
,
569 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
572 /* Check that the range does not overlap with one already present */
573 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
574 if (query
.overlaps
) {
579 mapping
= g_new(struct enumeration_mapping
, 1);
585 *mapping
= (struct enumeration_mapping
) {
586 .range_start
._signed
= range_start
,
587 .range_end
._signed
= range_end
, .string
= mapping_name
};
588 g_ptr_array_add(enumeration
->entries
, mapping
);
589 g_ptr_array_sort(enumeration
->entries
,
590 (GCompareFunc
)compare_enumeration_mappings_signed
);
592 free(escaped_string
);
597 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
598 struct bt_ctf_field_type
*type
, const char *string
,
599 uint64_t range_start
, uint64_t range_end
)
603 struct enumeration_mapping
*mapping
;
604 struct bt_ctf_field_type_enumeration
*enumeration
;
605 struct range_overlap_query query
;
606 char *escaped_string
;
608 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
610 (range_end
< range_start
)) {
615 if (!string
|| strlen(string
) == 0) {
620 escaped_string
= g_strescape(string
, NULL
);
621 if (!escaped_string
) {
626 mapping_name
= g_quark_from_string(escaped_string
);
627 query
= (struct range_overlap_query
) {
628 .range_start
._unsigned
= range_start
,
629 .range_end
._unsigned
= range_end
,
630 .mapping_name
= mapping_name
,
632 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
635 /* Check that the range does not overlap with one already present */
636 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
638 if (query
.overlaps
) {
643 mapping
= g_new(struct enumeration_mapping
, 1);
649 *mapping
= (struct enumeration_mapping
) {
650 .range_start
._unsigned
= range_start
,
651 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
652 g_ptr_array_add(enumeration
->entries
, mapping
);
653 g_ptr_array_sort(enumeration
->entries
,
654 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
656 free(escaped_string
);
661 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
662 struct bt_ctf_field_type_enumeration
*enumeration_type
,
665 const char *name
= NULL
;
666 struct range_overlap_query query
=
667 (struct range_overlap_query
) {
668 .range_start
._unsigned
= value
,
669 .range_end
._unsigned
= value
,
672 g_ptr_array_foreach(enumeration_type
->entries
,
673 check_ranges_overlap_unsigned
,
675 if (!query
.overlaps
) {
679 name
= g_quark_to_string(query
.mapping_name
);
684 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
685 struct bt_ctf_field_type_enumeration
*enumeration_type
,
688 const char *name
= NULL
;
689 struct range_overlap_query query
=
690 (struct range_overlap_query
) {
691 .range_start
._signed
= value
,
692 .range_end
._signed
= value
,
695 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
697 if (!query
.overlaps
) {
701 name
= g_quark_to_string(query
.mapping_name
);
706 int bt_ctf_field_type_enumeration_get_mapping_count(
707 struct bt_ctf_field_type
*type
)
710 struct bt_ctf_field_type_enumeration
*enumeration
;
712 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
717 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
719 ret
= (int) enumeration
->entries
->len
;
725 struct enumeration_mapping
*get_enumeration_mapping(
726 struct bt_ctf_field_type
*type
, int index
)
728 struct enumeration_mapping
*mapping
= NULL
;
729 struct bt_ctf_field_type_enumeration
*enumeration
;
731 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
733 if (index
>= enumeration
->entries
->len
) {
737 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
742 int bt_ctf_field_type_enumeration_get_mapping(
743 struct bt_ctf_field_type
*type
, int index
,
744 const char **string
, int64_t *range_start
, int64_t *range_end
)
746 struct enumeration_mapping
*mapping
;
749 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
750 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
755 mapping
= get_enumeration_mapping(type
, index
);
761 *string
= g_quark_to_string(mapping
->string
);
762 *range_start
= mapping
->range_start
._signed
;
763 *range_end
= mapping
->range_end
._signed
;
768 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
769 struct bt_ctf_field_type
*type
, int index
,
770 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
772 struct enumeration_mapping
*mapping
;
775 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
776 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
781 mapping
= get_enumeration_mapping(type
, index
);
787 *string
= g_quark_to_string(mapping
->string
);
788 *range_start
= mapping
->range_start
._unsigned
;
789 *range_end
= mapping
->range_end
._unsigned
;
794 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
795 struct bt_ctf_field_type
*type
, const char *name
)
798 struct bt_ctf_field_type_enumeration
*enumeration
;
801 if (!type
|| !name
||
802 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
807 name_quark
= g_quark_try_string(name
);
813 enumeration
= container_of(type
,
814 struct bt_ctf_field_type_enumeration
, parent
);
815 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
816 struct enumeration_mapping
*mapping
=
817 get_enumeration_mapping(type
, i
);
819 if (mapping
->string
== name_quark
) {
830 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
831 struct bt_ctf_field_type
*type
, int64_t value
)
833 struct bt_ctf_field_type_enumeration
*enumeration
;
836 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
841 enumeration
= container_of(type
,
842 struct bt_ctf_field_type_enumeration
, parent
);
843 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
844 struct enumeration_mapping
*mapping
=
845 get_enumeration_mapping(type
, i
);
847 if (value
>= mapping
->range_start
._signed
&&
848 value
<= mapping
->range_end
._signed
) {
859 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
860 struct bt_ctf_field_type
*type
, uint64_t value
)
862 struct bt_ctf_field_type_enumeration
*enumeration
;
865 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
870 enumeration
= container_of(type
,
871 struct bt_ctf_field_type_enumeration
, parent
);
872 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
873 struct enumeration_mapping
*mapping
=
874 get_enumeration_mapping(type
, i
);
876 if (value
>= mapping
->range_start
._unsigned
&&
877 value
<= mapping
->range_end
._unsigned
) {
888 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
890 struct bt_ctf_field_type_floating_point
*floating_point
=
891 g_new0(struct bt_ctf_field_type_floating_point
, 1);
893 if (!floating_point
) {
897 floating_point
->declaration
.sign
= &floating_point
->sign
;
898 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
899 floating_point
->declaration
.exp
= &floating_point
->exp
;
900 floating_point
->sign
.len
= 1;
901 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
902 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
903 floating_point
->declaration
.exp
->len
=
904 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
905 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
906 floating_point
->sign
.p
.alignment
= 1;
907 floating_point
->mantissa
.p
.alignment
= 1;
908 floating_point
->exp
.p
.alignment
= 1;
910 bt_ctf_field_type_init(&floating_point
->parent
);
912 return floating_point
? &floating_point
->parent
: NULL
;
915 int bt_ctf_field_type_floating_point_get_exponent_digits(
916 struct bt_ctf_field_type
*type
)
919 struct bt_ctf_field_type_floating_point
*floating_point
;
921 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
926 floating_point
= container_of(type
,
927 struct bt_ctf_field_type_floating_point
, parent
);
928 ret
= (int) floating_point
->declaration
.exp
->len
;
933 int bt_ctf_field_type_floating_point_set_exponent_digits(
934 struct bt_ctf_field_type
*type
,
935 unsigned int exponent_digits
)
938 struct bt_ctf_field_type_floating_point
*floating_point
;
940 if (!type
|| type
->frozen
||
941 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
946 floating_point
= container_of(type
,
947 struct bt_ctf_field_type_floating_point
, parent
);
948 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
949 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
951 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
956 floating_point
->declaration
.exp
->len
= exponent_digits
;
961 int bt_ctf_field_type_floating_point_get_mantissa_digits(
962 struct bt_ctf_field_type
*type
)
965 struct bt_ctf_field_type_floating_point
*floating_point
;
967 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
972 floating_point
= container_of(type
,
973 struct bt_ctf_field_type_floating_point
, parent
);
974 ret
= (int) floating_point
->mantissa
.len
+ 1;
979 int bt_ctf_field_type_floating_point_set_mantissa_digits(
980 struct bt_ctf_field_type
*type
,
981 unsigned int mantissa_digits
)
984 struct bt_ctf_field_type_floating_point
*floating_point
;
986 if (!type
|| type
->frozen
||
987 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
992 floating_point
= container_of(type
,
993 struct bt_ctf_field_type_floating_point
, parent
);
995 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
996 (mantissa_digits
!= DBL_MANT_DIG
) &&
997 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1002 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1007 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1009 struct bt_ctf_field_type_structure
*structure
=
1010 g_new0(struct bt_ctf_field_type_structure
, 1);
1016 structure
->parent
.declaration
= &structure
->declaration
.p
;
1017 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1018 bt_ctf_field_type_init(&structure
->parent
);
1019 structure
->fields
= g_ptr_array_new_with_free_func(
1020 (GDestroyNotify
)destroy_structure_field
);
1021 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1022 return &structure
->parent
;
1027 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1028 struct bt_ctf_field_type
*field_type
,
1029 const char *field_name
)
1032 struct bt_ctf_field_type_structure
*structure
;
1034 if (!type
|| !field_type
|| type
->frozen
||
1035 validate_identifier(field_name
) ||
1036 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1037 bt_ctf_field_type_validate(field_type
)) {
1042 structure
= container_of(type
,
1043 struct bt_ctf_field_type_structure
, parent
);
1044 if (add_structure_field(structure
->fields
,
1045 structure
->field_name_to_index
, field_type
, field_name
)) {
1050 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1051 type
->declaration
->alignment
=
1052 field_type
->declaration
->alignment
;
1058 int bt_ctf_field_type_structure_get_field_count(
1059 struct bt_ctf_field_type
*type
)
1062 struct bt_ctf_field_type_structure
*structure
;
1064 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1069 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1071 ret
= (int) structure
->fields
->len
;
1076 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1077 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1080 struct bt_ctf_field_type_structure
*structure
;
1081 struct structure_field
*field
;
1084 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1085 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1090 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1092 if (index
>= structure
->fields
->len
) {
1097 field
= g_ptr_array_index(structure
->fields
, index
);
1098 *field_type
= field
->type
;
1099 bt_ctf_field_type_get(field
->type
);
1100 *field_name
= g_quark_to_string(field
->name
);
1105 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1106 struct bt_ctf_field_type
*type
,
1111 struct structure_field
*field
;
1112 struct bt_ctf_field_type_structure
*structure
;
1113 struct bt_ctf_field_type
*field_type
= NULL
;
1115 if (!type
|| !name
) {
1119 name_quark
= g_quark_try_string(name
);
1124 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1126 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1127 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1131 field
= structure
->fields
->pdata
[index
];
1132 field_type
= field
->type
;
1133 bt_ctf_field_type_get(field_type
);
1138 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1139 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1141 struct bt_ctf_field_type_variant
*variant
= NULL
;
1143 if (!enum_tag
|| validate_identifier(tag_name
) ||
1144 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1148 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1153 variant
->parent
.declaration
= &variant
->declaration
.p
;
1154 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1155 variant
->tag_name
= g_string_new(tag_name
);
1156 bt_ctf_field_type_init(&variant
->parent
);
1157 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1158 variant
->fields
= g_ptr_array_new_with_free_func(
1159 (GDestroyNotify
)destroy_structure_field
);
1160 bt_ctf_field_type_get(enum_tag
);
1161 variant
->tag
= container_of(enum_tag
,
1162 struct bt_ctf_field_type_enumeration
, parent
);
1163 return &variant
->parent
;
1168 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1169 struct bt_ctf_field_type
*type
)
1171 struct bt_ctf_field_type_variant
*variant
;
1172 struct bt_ctf_field_type
*tag_type
= NULL
;
1174 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1178 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1179 tag_type
= &variant
->tag
->parent
;
1180 bt_ctf_field_type_get(tag_type
);
1185 const char *bt_ctf_field_type_variant_get_tag_name(
1186 struct bt_ctf_field_type
*type
)
1188 struct bt_ctf_field_type_variant
*variant
;
1189 const char *tag_name
= NULL
;
1191 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1195 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1196 tag_name
= variant
->tag_name
->str
;
1201 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1202 struct bt_ctf_field_type
*field_type
,
1203 const char *field_name
)
1208 struct bt_ctf_field_type_variant
*variant
;
1209 GQuark field_name_quark
= g_quark_from_string(field_name
);
1211 if (!type
|| !field_type
|| type
->frozen
||
1212 validate_identifier(field_name
) ||
1213 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1214 bt_ctf_field_type_validate(field_type
)) {
1219 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1220 /* Make sure this name is present in the enum tag */
1221 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1222 struct enumeration_mapping
*mapping
=
1223 g_ptr_array_index(variant
->tag
->entries
, i
);
1225 if (mapping
->string
== field_name_quark
) {
1231 if (!name_found
|| add_structure_field(variant
->fields
,
1232 variant
->field_name_to_index
, field_type
, field_name
)) {
1240 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1241 struct bt_ctf_field_type
*type
,
1242 const char *field_name
)
1246 struct structure_field
*field
;
1247 struct bt_ctf_field_type_variant
*variant
;
1248 struct bt_ctf_field_type
*field_type
= NULL
;
1250 if (!type
|| !field_name
) {
1254 name_quark
= g_quark_try_string(field_name
);
1259 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1260 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1261 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1265 field
= g_ptr_array_index(variant
->fields
, index
);
1266 field_type
= field
->type
;
1267 bt_ctf_field_type_get(field_type
);
1272 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1273 struct bt_ctf_field_type
*type
,
1274 struct bt_ctf_field
*tag
)
1276 const char *enum_value
;
1277 struct bt_ctf_field_type
*field_type
= NULL
;
1279 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1283 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1288 /* Already increments field_type's reference count */
1289 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1295 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1298 struct bt_ctf_field_type_variant
*variant
;
1300 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1305 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1307 ret
= (int) variant
->fields
->len
;
1313 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1314 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1317 struct bt_ctf_field_type_variant
*variant
;
1318 struct structure_field
*field
;
1321 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1322 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1327 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1329 if (index
>= variant
->fields
->len
) {
1334 field
= g_ptr_array_index(variant
->fields
, index
);
1335 *field_type
= field
->type
;
1336 bt_ctf_field_type_get(field
->type
);
1337 *field_name
= g_quark_to_string(field
->name
);
1342 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1343 struct bt_ctf_field_type
*element_type
,
1344 unsigned int length
)
1346 struct bt_ctf_field_type_array
*array
= NULL
;
1348 if (!element_type
|| length
== 0 ||
1349 bt_ctf_field_type_validate(element_type
)) {
1353 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1358 array
->parent
.declaration
= &array
->declaration
.p
;
1359 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1360 bt_ctf_field_type_init(&array
->parent
);
1361 bt_ctf_field_type_get(element_type
);
1362 array
->element_type
= element_type
;
1363 array
->length
= length
;
1364 array
->parent
.declaration
->alignment
=
1365 element_type
->declaration
->alignment
;
1366 return &array
->parent
;
1371 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1372 struct bt_ctf_field_type
*type
)
1374 struct bt_ctf_field_type
*ret
= NULL
;
1375 struct bt_ctf_field_type_array
*array
;
1377 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1381 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1382 ret
= array
->element_type
;
1383 bt_ctf_field_type_get(ret
);
1388 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1391 struct bt_ctf_field_type_array
*array
;
1393 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1398 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1399 ret
= (int64_t) array
->length
;
1404 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1405 struct bt_ctf_field_type
*element_type
,
1406 const char *length_field_name
)
1408 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1410 if (!element_type
|| validate_identifier(length_field_name
) ||
1411 bt_ctf_field_type_validate(element_type
)) {
1415 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1420 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1421 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1422 bt_ctf_field_type_init(&sequence
->parent
);
1423 bt_ctf_field_type_get(element_type
);
1424 sequence
->element_type
= element_type
;
1425 sequence
->length_field_name
= g_string_new(length_field_name
);
1426 sequence
->parent
.declaration
->alignment
=
1427 element_type
->declaration
->alignment
;
1428 return &sequence
->parent
;
1433 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1434 struct bt_ctf_field_type
*type
)
1436 struct bt_ctf_field_type
*ret
= NULL
;
1437 struct bt_ctf_field_type_sequence
*sequence
;
1439 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1443 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1445 ret
= sequence
->element_type
;
1446 bt_ctf_field_type_get(ret
);
1451 const char *bt_ctf_field_type_sequence_get_length_field_name(
1452 struct bt_ctf_field_type
*type
)
1454 const char *ret
= NULL
;
1455 struct bt_ctf_field_type_sequence
*sequence
;
1457 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1461 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1463 ret
= sequence
->length_field_name
->str
;
1468 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1470 struct bt_ctf_field_type_string
*string
=
1471 g_new0(struct bt_ctf_field_type_string
, 1);
1477 string
->parent
.declaration
= &string
->declaration
.p
;
1478 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1479 bt_ctf_field_type_init(&string
->parent
);
1480 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1481 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1482 return &string
->parent
;
1485 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1486 struct bt_ctf_field_type
*type
)
1488 struct bt_ctf_field_type_string
*string
;
1489 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1491 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1495 string
= container_of(type
, struct bt_ctf_field_type_string
,
1497 ret
= string
->declaration
.encoding
;
1502 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1503 enum ctf_string_encoding encoding
)
1506 struct bt_ctf_field_type_string
*string
;
1508 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1509 (encoding
!= CTF_STRING_UTF8
&&
1510 encoding
!= CTF_STRING_ASCII
)) {
1515 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1516 string
->declaration
.encoding
= encoding
;
1521 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1530 ret
= (int) type
->declaration
->alignment
;
1535 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1536 unsigned int alignment
)
1540 /* Alignment must be bit-aligned (1) or byte aligned */
1541 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1546 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1547 alignment
!= CHAR_BIT
) {
1552 type
->declaration
->alignment
= alignment
;
1558 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1559 struct bt_ctf_field_type
*type
)
1561 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1567 switch (type
->declaration
->id
) {
1568 case CTF_TYPE_INTEGER
:
1570 struct bt_ctf_field_type_integer
*integer
= container_of(
1571 type
, struct bt_ctf_field_type_integer
, parent
);
1572 ret
= integer
->declaration
.byte_order
== LITTLE_ENDIAN
?
1573 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1574 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1577 case CTF_TYPE_FLOAT
:
1579 struct bt_ctf_field_type_floating_point
*floating_point
=
1581 struct bt_ctf_field_type_floating_point
,
1583 ret
= floating_point
->declaration
.byte_order
== LITTLE_ENDIAN
?
1584 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1585 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1595 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1596 enum bt_ctf_byte_order byte_order
)
1599 int internal_byte_order
;
1600 enum ctf_type_id type_id
;
1602 if (!type
|| type
->frozen
) {
1607 type_id
= type
->declaration
->id
;
1608 switch (byte_order
) {
1609 case BT_CTF_BYTE_ORDER_NATIVE
:
1610 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
?
1611 LITTLE_ENDIAN
: BIG_ENDIAN
);
1613 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1614 internal_byte_order
= LITTLE_ENDIAN
;
1616 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1617 case BT_CTF_BYTE_ORDER_NETWORK
:
1618 internal_byte_order
= BIG_ENDIAN
;
1625 if (set_byte_order_funcs
[type_id
]) {
1626 set_byte_order_funcs
[type_id
](type
, internal_byte_order
);
1632 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1633 struct bt_ctf_field_type
*type
)
1636 return CTF_TYPE_UNKNOWN
;
1639 return type
->declaration
->id
;
1642 const char *bt_ctf_field_type_get_alias_name(
1643 struct bt_ctf_field_type
*type
)
1645 const char *name
= NULL
;
1647 if (!type
|| !type
->alias_name
) {
1651 name
= type
->alias_name
->str
;
1656 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1662 bt_ctf_ref_get(&type
->ref_count
);
1665 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1671 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1675 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1685 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1686 struct bt_ctf_field_type_variant
*variant
,
1689 struct bt_ctf_field_type
*type
= NULL
;
1690 GQuark field_name_quark
;
1692 struct structure_field
*field_entry
;
1693 struct range_overlap_query query
= {
1694 .range_start
._signed
= tag_value
,
1695 .range_end
._signed
= tag_value
,
1696 .mapping_name
= 0, .overlaps
= 0};
1698 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1700 if (!query
.overlaps
) {
1704 field_name_quark
= query
.mapping_name
;
1705 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1706 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1710 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1711 type
= field_entry
->type
;
1717 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1718 struct bt_ctf_field_type_variant
*variant
,
1721 struct bt_ctf_field_type
*type
= NULL
;
1722 GQuark field_name_quark
;
1724 struct structure_field
*field_entry
;
1725 struct range_overlap_query query
= {
1726 .range_start
._unsigned
= tag_value
,
1727 .range_end
._unsigned
= tag_value
,
1728 .mapping_name
= 0, .overlaps
= 0};
1730 g_ptr_array_foreach(variant
->tag
->entries
,
1731 check_ranges_overlap_unsigned
,
1733 if (!query
.overlaps
) {
1737 field_name_quark
= query
.mapping_name
;
1738 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1739 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1743 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1744 type
= field_entry
->type
;
1750 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1751 struct metadata_context
*context
)
1755 if (!type
|| !context
) {
1760 ret
= type
->serialize(type
, context
);
1766 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1768 struct bt_ctf_field_type_integer
*integer
;
1774 integer
= container_of(
1775 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1776 struct bt_ctf_field_type_integer
, parent
);
1781 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1783 struct bt_ctf_field_type_enumeration
*enumeration
;
1789 enumeration
= container_of(
1790 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1791 struct bt_ctf_field_type_enumeration
, parent
);
1792 g_ptr_array_free(enumeration
->entries
, TRUE
);
1793 bt_ctf_field_type_put(enumeration
->container
);
1794 g_free(enumeration
);
1798 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1800 struct bt_ctf_field_type_floating_point
*floating_point
;
1806 floating_point
= container_of(
1807 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1808 struct bt_ctf_field_type_floating_point
, parent
);
1809 g_free(floating_point
);
1813 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1815 struct bt_ctf_field_type_structure
*structure
;
1821 structure
= container_of(
1822 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1823 struct bt_ctf_field_type_structure
, parent
);
1824 g_ptr_array_free(structure
->fields
, TRUE
);
1825 g_hash_table_destroy(structure
->field_name_to_index
);
1830 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1832 struct bt_ctf_field_type_variant
*variant
;
1838 variant
= container_of(
1839 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1840 struct bt_ctf_field_type_variant
, parent
);
1841 g_ptr_array_free(variant
->fields
, TRUE
);
1842 g_hash_table_destroy(variant
->field_name_to_index
);
1843 g_string_free(variant
->tag_name
, TRUE
);
1844 bt_ctf_field_type_put(&variant
->tag
->parent
);
1849 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1851 struct bt_ctf_field_type_array
*array
;
1857 array
= container_of(
1858 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1859 struct bt_ctf_field_type_array
, parent
);
1860 bt_ctf_field_type_put(array
->element_type
);
1865 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1867 struct bt_ctf_field_type_sequence
*sequence
;
1873 sequence
= container_of(
1874 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1875 struct bt_ctf_field_type_sequence
, parent
);
1876 bt_ctf_field_type_put(sequence
->element_type
);
1877 g_string_free(sequence
->length_field_name
, TRUE
);
1882 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1884 struct bt_ctf_field_type_string
*string
;
1890 string
= container_of(
1891 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1892 struct bt_ctf_field_type_string
, parent
);
1897 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
1903 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
1905 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
1906 type
, struct bt_ctf_field_type_enumeration
, parent
);
1908 generic_field_type_freeze(type
);
1909 bt_ctf_field_type_freeze(enumeration_type
->container
);
1913 void freeze_structure_field(struct structure_field
*field
)
1915 bt_ctf_field_type_freeze(field
->type
);
1919 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
1921 struct bt_ctf_field_type_structure
*structure_type
= container_of(
1922 type
, struct bt_ctf_field_type_structure
, parent
);
1924 generic_field_type_freeze(type
);
1925 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
1930 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
1932 struct bt_ctf_field_type_variant
*variant_type
= container_of(
1933 type
, struct bt_ctf_field_type_variant
, parent
);
1935 generic_field_type_freeze(type
);
1936 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
1941 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
1943 struct bt_ctf_field_type_array
*array_type
= container_of(
1944 type
, struct bt_ctf_field_type_array
, parent
);
1946 generic_field_type_freeze(type
);
1947 bt_ctf_field_type_freeze(array_type
->element_type
);
1951 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
1953 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
1954 type
, struct bt_ctf_field_type_sequence
, parent
);
1956 generic_field_type_freeze(type
);
1957 bt_ctf_field_type_freeze(sequence_type
->element_type
);
1961 const char *get_encoding_string(enum ctf_string_encoding encoding
)
1963 const char *encoding_string
;
1966 case CTF_STRING_NONE
:
1967 encoding_string
= "none";
1969 case CTF_STRING_ASCII
:
1970 encoding_string
= "ASCII";
1972 case CTF_STRING_UTF8
:
1973 encoding_string
= "UTF8";
1976 encoding_string
= "unknown";
1980 return encoding_string
;
1984 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
1986 const char *base_string
;
1989 case BT_CTF_INTEGER_BASE_DECIMAL
:
1990 base_string
= "decimal";
1992 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
1993 base_string
= "hexadecimal";
1995 case BT_CTF_INTEGER_BASE_OCTAL
:
1996 base_string
= "octal";
1998 case BT_CTF_INTEGER_BASE_BINARY
:
1999 base_string
= "binary";
2002 base_string
= "unknown";
2010 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2011 struct metadata_context
*context
)
2013 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2014 struct bt_ctf_field_type_integer
, parent
);
2016 g_string_append_printf(context
->string
,
2017 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
2018 integer
->declaration
.len
, type
->declaration
->alignment
,
2019 (integer
->declaration
.signedness
? "true" : "false"),
2020 get_encoding_string(integer
->declaration
.encoding
),
2021 get_integer_base_string(integer
->declaration
.base
),
2022 get_byte_order_string(integer
->declaration
.byte_order
));
2027 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2028 struct metadata_context
*context
)
2032 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2033 struct bt_ctf_field_type_enumeration
, parent
);
2034 struct bt_ctf_field_type
*container_type
;
2035 int container_signed
;
2037 ret
= bt_ctf_field_type_validate(type
);
2042 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2043 if (!container_type
) {
2048 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2049 if (container_signed
< 0) {
2050 ret
= container_signed
;
2051 goto error_put_container_type
;
2054 g_string_append(context
->string
, "enum : ");
2055 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2057 goto error_put_container_type
;
2060 g_string_append(context
->string
, " { ");
2061 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2062 struct enumeration_mapping
*mapping
=
2063 enumeration
->entries
->pdata
[entry
];
2065 if (container_signed
) {
2066 if (mapping
->range_start
._signed
==
2067 mapping
->range_end
._signed
) {
2068 g_string_append_printf(context
->string
,
2069 "\"%s\" = %" PRId64
,
2070 g_quark_to_string(mapping
->string
),
2071 mapping
->range_start
._signed
);
2073 g_string_append_printf(context
->string
,
2074 "\"%s\" = %" PRId64
" ... %" PRId64
,
2075 g_quark_to_string(mapping
->string
),
2076 mapping
->range_start
._signed
,
2077 mapping
->range_end
._signed
);
2080 if (mapping
->range_start
._unsigned
==
2081 mapping
->range_end
._unsigned
) {
2082 g_string_append_printf(context
->string
,
2083 "\"%s\" = %" PRIu64
,
2084 g_quark_to_string(mapping
->string
),
2085 mapping
->range_start
._unsigned
);
2087 g_string_append_printf(context
->string
,
2088 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2089 g_quark_to_string(mapping
->string
),
2090 mapping
->range_start
._unsigned
,
2091 mapping
->range_end
._unsigned
);
2095 g_string_append(context
->string
,
2096 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2100 if (context
->field_name
->len
) {
2101 g_string_append_printf(context
->string
, " %s",
2102 context
->field_name
->str
);
2103 g_string_assign(context
->field_name
, "");
2105 error_put_container_type
:
2106 bt_ctf_field_type_put(container_type
);
2112 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2113 struct metadata_context
*context
)
2115 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2116 type
, struct bt_ctf_field_type_floating_point
, parent
);
2118 g_string_append_printf(context
->string
,
2119 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2120 floating_point
->declaration
.exp
->len
,
2121 floating_point
->declaration
.mantissa
->len
+ 1,
2122 get_byte_order_string(floating_point
->declaration
.byte_order
),
2123 type
->declaration
->alignment
);
2128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2129 struct metadata_context
*context
)
2132 unsigned int indent
;
2134 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2135 struct bt_ctf_field_type_structure
, parent
);
2136 GString
*structure_field_name
= context
->field_name
;
2138 context
->field_name
= g_string_new("");
2140 context
->current_indentation_level
++;
2141 g_string_append(context
->string
, "struct {\n");
2143 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2144 struct structure_field
*field
;
2146 for (indent
= 0; indent
< context
->current_indentation_level
;
2148 g_string_append_c(context
->string
, '\t');
2151 field
= structure
->fields
->pdata
[i
];
2152 g_string_assign(context
->field_name
,
2153 g_quark_to_string(field
->name
));
2154 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2159 if (context
->field_name
->len
) {
2160 g_string_append_printf(context
->string
, " %s",
2161 context
->field_name
->str
);
2163 g_string_append(context
->string
, ";\n");
2166 context
->current_indentation_level
--;
2167 for (indent
= 0; indent
< context
->current_indentation_level
;
2169 g_string_append_c(context
->string
, '\t');
2172 g_string_append_printf(context
->string
, "} align(%zu)",
2173 type
->declaration
->alignment
);
2175 g_string_free(context
->field_name
, TRUE
);
2176 context
->field_name
= structure_field_name
;
2181 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2182 struct metadata_context
*context
)
2185 unsigned int indent
;
2187 struct bt_ctf_field_type_variant
*variant
= container_of(
2188 type
, struct bt_ctf_field_type_variant
, parent
);
2189 GString
*variant_field_name
= context
->field_name
;
2191 context
->field_name
= g_string_new("");
2192 g_string_append_printf(context
->string
,
2193 "variant <%s> {\n", variant
->tag_name
->str
);
2194 context
->current_indentation_level
++;
2195 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2196 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2198 g_string_assign(context
->field_name
,
2199 g_quark_to_string(field
->name
));
2200 for (indent
= 0; indent
< context
->current_indentation_level
;
2202 g_string_append_c(context
->string
, '\t');
2205 g_string_assign(context
->field_name
,
2206 g_quark_to_string(field
->name
));
2207 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2212 if (context
->field_name
->len
) {
2213 g_string_append_printf(context
->string
, " %s;",
2214 context
->field_name
->str
);
2217 g_string_append_c(context
->string
, '\n');
2220 context
->current_indentation_level
--;
2221 for (indent
= 0; indent
< context
->current_indentation_level
;
2223 g_string_append_c(context
->string
, '\t');
2226 g_string_append(context
->string
, "}");
2228 g_string_free(context
->field_name
, TRUE
);
2229 context
->field_name
= variant_field_name
;
2234 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2235 struct metadata_context
*context
)
2238 struct bt_ctf_field_type_array
*array
= container_of(type
,
2239 struct bt_ctf_field_type_array
, parent
);
2241 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2246 if (context
->field_name
->len
) {
2247 g_string_append_printf(context
->string
, " %s[%u]",
2248 context
->field_name
->str
, array
->length
);
2249 g_string_assign(context
->field_name
, "");
2251 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2258 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2259 struct metadata_context
*context
)
2262 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2263 type
, struct bt_ctf_field_type_sequence
, parent
);
2265 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2270 if (context
->field_name
->len
) {
2271 g_string_append_printf(context
->string
, " %s[%s]",
2272 context
->field_name
->str
,
2273 sequence
->length_field_name
->str
);
2274 g_string_assign(context
->field_name
, "");
2276 g_string_append_printf(context
->string
, "[%s]",
2277 sequence
->length_field_name
->str
);
2284 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2285 struct metadata_context
*context
)
2287 struct bt_ctf_field_type_string
*string
= container_of(
2288 type
, struct bt_ctf_field_type_string
, parent
);
2290 g_string_append_printf(context
->string
,
2291 "string { encoding = %s; }",
2292 get_encoding_string(string
->declaration
.encoding
));
2297 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2300 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2301 struct bt_ctf_field_type_integer
, parent
);
2303 integer_type
->declaration
.byte_order
= byte_order
;
2307 void bt_ctf_field_type_floating_point_set_byte_order(
2308 struct bt_ctf_field_type
*type
, int byte_order
)
2310 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2311 container_of(type
, struct bt_ctf_field_type_floating_point
,
2314 floating_point_type
->declaration
.byte_order
= byte_order
;
2315 floating_point_type
->sign
.byte_order
= byte_order
;
2316 floating_point_type
->mantissa
.byte_order
= byte_order
;
2317 floating_point_type
->exp
.byte_order
= byte_order
;