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 int64_t 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
= enumeration
->entries
->len
;
725 struct enumeration_mapping
*get_enumeration_mapping(
726 struct bt_ctf_field_type
*type
, size_t 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
, size_t index
,
744 const char **string
, int64_t *range_start
, int64_t *range_end
)
746 struct enumeration_mapping
*mapping
;
749 if (!type
|| !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
, size_t index
,
770 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
772 struct enumeration_mapping
*mapping
;
775 if (!type
|| !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
,
800 struct bt_ctf_field_type_enumeration
*enumeration
;
803 if (!type
|| !name
|| !index
||
804 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
809 name_quark
= g_quark_try_string(name
);
815 enumeration
= container_of(type
,
816 struct bt_ctf_field_type_enumeration
, parent
);
817 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
818 struct enumeration_mapping
*mapping
=
819 get_enumeration_mapping(type
, i
);
821 if (mapping
->string
== name_quark
) {
832 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
833 struct bt_ctf_field_type
*type
, int64_t value
,
836 struct bt_ctf_field_type_enumeration
*enumeration
;
840 if (!type
|| !index
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
845 enumeration
= container_of(type
,
846 struct bt_ctf_field_type_enumeration
, parent
);
847 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
848 struct enumeration_mapping
*mapping
=
849 get_enumeration_mapping(type
, i
);
851 if (value
>= mapping
->range_start
._signed
&&
852 value
<= mapping
->range_end
._signed
) {
863 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
864 struct bt_ctf_field_type
*type
, uint64_t value
,
867 struct bt_ctf_field_type_enumeration
*enumeration
;
871 if (!type
|| !index
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
876 enumeration
= container_of(type
,
877 struct bt_ctf_field_type_enumeration
, parent
);
878 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
879 struct enumeration_mapping
*mapping
=
880 get_enumeration_mapping(type
, i
);
882 if (value
>= mapping
->range_start
._unsigned
&&
883 value
<= mapping
->range_end
._unsigned
) {
894 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
896 struct bt_ctf_field_type_floating_point
*floating_point
=
897 g_new0(struct bt_ctf_field_type_floating_point
, 1);
899 if (!floating_point
) {
903 floating_point
->declaration
.sign
= &floating_point
->sign
;
904 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
905 floating_point
->declaration
.exp
= &floating_point
->exp
;
906 floating_point
->sign
.len
= 1;
907 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
908 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
909 floating_point
->declaration
.exp
->len
=
910 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
911 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
912 floating_point
->sign
.p
.alignment
= 1;
913 floating_point
->mantissa
.p
.alignment
= 1;
914 floating_point
->exp
.p
.alignment
= 1;
916 bt_ctf_field_type_init(&floating_point
->parent
);
918 return floating_point
? &floating_point
->parent
: NULL
;
921 int bt_ctf_field_type_floating_point_get_exponent_digits(
922 struct bt_ctf_field_type
*type
)
925 struct bt_ctf_field_type_floating_point
*floating_point
;
927 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
932 floating_point
= container_of(type
,
933 struct bt_ctf_field_type_floating_point
, parent
);
934 ret
= (int) floating_point
->declaration
.exp
->len
;
939 int bt_ctf_field_type_floating_point_set_exponent_digits(
940 struct bt_ctf_field_type
*type
,
941 unsigned int exponent_digits
)
944 struct bt_ctf_field_type_floating_point
*floating_point
;
946 if (!type
|| type
->frozen
||
947 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
952 floating_point
= container_of(type
,
953 struct bt_ctf_field_type_floating_point
, parent
);
954 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
955 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
957 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
962 floating_point
->declaration
.exp
->len
= exponent_digits
;
967 int bt_ctf_field_type_floating_point_get_mantissa_digits(
968 struct bt_ctf_field_type
*type
)
971 struct bt_ctf_field_type_floating_point
*floating_point
;
973 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
978 floating_point
= container_of(type
,
979 struct bt_ctf_field_type_floating_point
, parent
);
980 ret
= (int) floating_point
->mantissa
.len
+ 1;
985 int bt_ctf_field_type_floating_point_set_mantissa_digits(
986 struct bt_ctf_field_type
*type
,
987 unsigned int mantissa_digits
)
990 struct bt_ctf_field_type_floating_point
*floating_point
;
992 if (!type
|| type
->frozen
||
993 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
998 floating_point
= container_of(type
,
999 struct bt_ctf_field_type_floating_point
, parent
);
1001 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1002 (mantissa_digits
!= DBL_MANT_DIG
) &&
1003 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1008 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1013 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1015 struct bt_ctf_field_type_structure
*structure
=
1016 g_new0(struct bt_ctf_field_type_structure
, 1);
1022 structure
->parent
.declaration
= &structure
->declaration
.p
;
1023 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1024 bt_ctf_field_type_init(&structure
->parent
);
1025 structure
->fields
= g_ptr_array_new_with_free_func(
1026 (GDestroyNotify
)destroy_structure_field
);
1027 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1028 return &structure
->parent
;
1033 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1034 struct bt_ctf_field_type
*field_type
,
1035 const char *field_name
)
1038 struct bt_ctf_field_type_structure
*structure
;
1040 if (!type
|| !field_type
|| type
->frozen
||
1041 validate_identifier(field_name
) ||
1042 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1043 bt_ctf_field_type_validate(field_type
)) {
1048 structure
= container_of(type
,
1049 struct bt_ctf_field_type_structure
, parent
);
1050 if (add_structure_field(structure
->fields
,
1051 structure
->field_name_to_index
, field_type
, field_name
)) {
1056 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1057 type
->declaration
->alignment
=
1058 field_type
->declaration
->alignment
;
1064 int64_t bt_ctf_field_type_structure_get_field_count(
1065 struct bt_ctf_field_type
*type
)
1068 struct bt_ctf_field_type_structure
*structure
;
1070 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1075 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1077 ret
= structure
->fields
->len
;
1082 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1083 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1086 struct bt_ctf_field_type_structure
*structure
;
1087 struct structure_field
*field
;
1090 if (!type
|| !field_name
|| !field_type
||
1091 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1096 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1098 if (index
>= structure
->fields
->len
) {
1103 field
= g_ptr_array_index(structure
->fields
, index
);
1104 *field_type
= field
->type
;
1105 bt_ctf_field_type_get(field
->type
);
1106 *field_name
= g_quark_to_string(field
->name
);
1111 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1112 struct bt_ctf_field_type
*type
,
1117 struct structure_field
*field
;
1118 struct bt_ctf_field_type_structure
*structure
;
1119 struct bt_ctf_field_type
*field_type
= NULL
;
1121 if (!type
|| !name
) {
1125 name_quark
= g_quark_try_string(name
);
1130 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1132 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1133 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1137 field
= structure
->fields
->pdata
[index
];
1138 field_type
= field
->type
;
1139 bt_ctf_field_type_get(field_type
);
1144 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1145 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1147 struct bt_ctf_field_type_variant
*variant
= NULL
;
1149 if (!enum_tag
|| validate_identifier(tag_name
) ||
1150 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1154 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1159 variant
->parent
.declaration
= &variant
->declaration
.p
;
1160 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1161 variant
->tag_name
= g_string_new(tag_name
);
1162 bt_ctf_field_type_init(&variant
->parent
);
1163 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1164 variant
->fields
= g_ptr_array_new_with_free_func(
1165 (GDestroyNotify
)destroy_structure_field
);
1166 bt_ctf_field_type_get(enum_tag
);
1167 variant
->tag
= container_of(enum_tag
,
1168 struct bt_ctf_field_type_enumeration
, parent
);
1169 return &variant
->parent
;
1174 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1175 struct bt_ctf_field_type
*type
)
1177 struct bt_ctf_field_type_variant
*variant
;
1178 struct bt_ctf_field_type
*tag_type
= NULL
;
1180 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1184 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1185 tag_type
= &variant
->tag
->parent
;
1186 bt_ctf_field_type_get(tag_type
);
1191 const char *bt_ctf_field_type_variant_get_tag_name(
1192 struct bt_ctf_field_type
*type
)
1194 struct bt_ctf_field_type_variant
*variant
;
1195 const char *tag_name
= NULL
;
1197 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1201 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1202 tag_name
= variant
->tag_name
->str
;
1207 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1208 struct bt_ctf_field_type
*field_type
,
1209 const char *field_name
)
1214 struct bt_ctf_field_type_variant
*variant
;
1215 GQuark field_name_quark
= g_quark_from_string(field_name
);
1217 if (!type
|| !field_type
|| type
->frozen
||
1218 validate_identifier(field_name
) ||
1219 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1220 bt_ctf_field_type_validate(field_type
)) {
1225 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1226 /* Make sure this name is present in the enum tag */
1227 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1228 struct enumeration_mapping
*mapping
=
1229 g_ptr_array_index(variant
->tag
->entries
, i
);
1231 if (mapping
->string
== field_name_quark
) {
1237 if (!name_found
|| add_structure_field(variant
->fields
,
1238 variant
->field_name_to_index
, field_type
, field_name
)) {
1246 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1247 struct bt_ctf_field_type
*type
,
1248 const char *field_name
)
1252 struct structure_field
*field
;
1253 struct bt_ctf_field_type_variant
*variant
;
1254 struct bt_ctf_field_type
*field_type
= NULL
;
1256 if (!type
|| !field_name
) {
1260 name_quark
= g_quark_try_string(field_name
);
1265 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1266 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1267 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1271 field
= g_ptr_array_index(variant
->fields
, index
);
1272 field_type
= field
->type
;
1273 bt_ctf_field_type_get(field_type
);
1278 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1279 struct bt_ctf_field_type
*type
,
1280 struct bt_ctf_field
*tag
)
1282 const char *enum_value
;
1283 struct bt_ctf_field_type
*field_type
= NULL
;
1285 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1289 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1294 /* Already increments field_type's reference count */
1295 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1301 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1304 struct bt_ctf_field_type_variant
*variant
;
1306 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1311 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1313 ret
= variant
->fields
->len
;
1319 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1320 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1323 struct bt_ctf_field_type_variant
*variant
;
1324 struct structure_field
*field
;
1327 if (!type
|| !field_name
|| !field_type
||
1328 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1333 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1335 if (index
>= variant
->fields
->len
) {
1340 field
= g_ptr_array_index(variant
->fields
, index
);
1341 *field_type
= field
->type
;
1342 bt_ctf_field_type_get(field
->type
);
1343 *field_name
= g_quark_to_string(field
->name
);
1348 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1349 struct bt_ctf_field_type
*element_type
,
1350 unsigned int length
)
1352 struct bt_ctf_field_type_array
*array
= NULL
;
1354 if (!element_type
|| length
== 0 ||
1355 bt_ctf_field_type_validate(element_type
)) {
1359 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1364 array
->parent
.declaration
= &array
->declaration
.p
;
1365 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1366 bt_ctf_field_type_init(&array
->parent
);
1367 bt_ctf_field_type_get(element_type
);
1368 array
->element_type
= element_type
;
1369 array
->length
= length
;
1370 array
->parent
.declaration
->alignment
=
1371 element_type
->declaration
->alignment
;
1372 return &array
->parent
;
1377 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1378 struct bt_ctf_field_type
*type
)
1380 struct bt_ctf_field_type
*ret
= NULL
;
1381 struct bt_ctf_field_type_array
*array
;
1383 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1387 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1388 ret
= array
->element_type
;
1389 bt_ctf_field_type_get(ret
);
1394 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1397 struct bt_ctf_field_type_array
*array
;
1399 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1404 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1405 ret
= (int64_t) array
->length
;
1410 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1411 struct bt_ctf_field_type
*element_type
,
1412 const char *length_field_name
)
1414 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1416 if (!element_type
|| validate_identifier(length_field_name
) ||
1417 bt_ctf_field_type_validate(element_type
)) {
1421 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1426 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1427 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1428 bt_ctf_field_type_init(&sequence
->parent
);
1429 bt_ctf_field_type_get(element_type
);
1430 sequence
->element_type
= element_type
;
1431 sequence
->length_field_name
= g_string_new(length_field_name
);
1432 sequence
->parent
.declaration
->alignment
=
1433 element_type
->declaration
->alignment
;
1434 return &sequence
->parent
;
1439 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1440 struct bt_ctf_field_type
*type
)
1442 struct bt_ctf_field_type
*ret
= NULL
;
1443 struct bt_ctf_field_type_sequence
*sequence
;
1445 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1449 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1451 ret
= sequence
->element_type
;
1452 bt_ctf_field_type_get(ret
);
1457 const char *bt_ctf_field_type_sequence_get_length_field_name(
1458 struct bt_ctf_field_type
*type
)
1460 const char *ret
= NULL
;
1461 struct bt_ctf_field_type_sequence
*sequence
;
1463 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1467 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1469 ret
= sequence
->length_field_name
->str
;
1474 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1476 struct bt_ctf_field_type_string
*string
=
1477 g_new0(struct bt_ctf_field_type_string
, 1);
1483 string
->parent
.declaration
= &string
->declaration
.p
;
1484 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1485 bt_ctf_field_type_init(&string
->parent
);
1486 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1487 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1488 return &string
->parent
;
1491 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1492 struct bt_ctf_field_type
*type
)
1494 struct bt_ctf_field_type_string
*string
;
1495 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1497 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1501 string
= container_of(type
, struct bt_ctf_field_type_string
,
1503 ret
= string
->declaration
.encoding
;
1508 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1509 enum ctf_string_encoding encoding
)
1512 struct bt_ctf_field_type_string
*string
;
1514 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1515 (encoding
!= CTF_STRING_UTF8
&&
1516 encoding
!= CTF_STRING_ASCII
)) {
1521 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1522 string
->declaration
.encoding
= encoding
;
1527 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1536 ret
= (int) type
->declaration
->alignment
;
1541 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1542 unsigned int alignment
)
1546 /* Alignment must be bit-aligned (1) or byte aligned */
1547 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1552 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1553 alignment
!= CHAR_BIT
) {
1558 type
->declaration
->alignment
= alignment
;
1564 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1565 struct bt_ctf_field_type
*type
)
1567 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1573 switch (type
->declaration
->id
) {
1574 case CTF_TYPE_INTEGER
:
1576 struct bt_ctf_field_type_integer
*integer
= container_of(
1577 type
, struct bt_ctf_field_type_integer
, parent
);
1578 ret
= integer
->declaration
.byte_order
== LITTLE_ENDIAN
?
1579 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1580 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1583 case CTF_TYPE_FLOAT
:
1585 struct bt_ctf_field_type_floating_point
*floating_point
=
1587 struct bt_ctf_field_type_floating_point
,
1589 ret
= floating_point
->declaration
.byte_order
== LITTLE_ENDIAN
?
1590 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1591 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1601 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1602 enum bt_ctf_byte_order byte_order
)
1605 int internal_byte_order
;
1606 enum ctf_type_id type_id
;
1608 if (!type
|| type
->frozen
) {
1613 type_id
= type
->declaration
->id
;
1614 switch (byte_order
) {
1615 case BT_CTF_BYTE_ORDER_NATIVE
:
1616 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
?
1617 LITTLE_ENDIAN
: BIG_ENDIAN
);
1619 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1620 internal_byte_order
= LITTLE_ENDIAN
;
1622 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1623 case BT_CTF_BYTE_ORDER_NETWORK
:
1624 internal_byte_order
= BIG_ENDIAN
;
1631 if (set_byte_order_funcs
[type_id
]) {
1632 set_byte_order_funcs
[type_id
](type
, internal_byte_order
);
1638 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1639 struct bt_ctf_field_type
*type
)
1642 return CTF_TYPE_UNKNOWN
;
1645 return type
->declaration
->id
;
1648 const char *bt_ctf_field_type_get_alias_name(
1649 struct bt_ctf_field_type
*type
)
1651 const char *name
= NULL
;
1653 if (!type
|| !type
->alias_name
) {
1657 name
= type
->alias_name
->str
;
1662 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1668 bt_ctf_ref_get(&type
->ref_count
);
1671 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1677 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1681 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1691 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1692 struct bt_ctf_field_type_variant
*variant
,
1695 struct bt_ctf_field_type
*type
= NULL
;
1696 GQuark field_name_quark
;
1698 struct structure_field
*field_entry
;
1699 struct range_overlap_query query
= {
1700 .range_start
._signed
= tag_value
,
1701 .range_end
._signed
= tag_value
,
1702 .mapping_name
= 0, .overlaps
= 0};
1704 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1706 if (!query
.overlaps
) {
1710 field_name_quark
= query
.mapping_name
;
1711 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1712 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1716 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1717 type
= field_entry
->type
;
1723 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1724 struct bt_ctf_field_type_variant
*variant
,
1727 struct bt_ctf_field_type
*type
= NULL
;
1728 GQuark field_name_quark
;
1730 struct structure_field
*field_entry
;
1731 struct range_overlap_query query
= {
1732 .range_start
._unsigned
= tag_value
,
1733 .range_end
._unsigned
= tag_value
,
1734 .mapping_name
= 0, .overlaps
= 0};
1736 g_ptr_array_foreach(variant
->tag
->entries
,
1737 check_ranges_overlap_unsigned
,
1739 if (!query
.overlaps
) {
1743 field_name_quark
= query
.mapping_name
;
1744 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1745 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1749 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1750 type
= field_entry
->type
;
1756 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1757 struct metadata_context
*context
)
1761 if (!type
|| !context
) {
1766 ret
= type
->serialize(type
, context
);
1772 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1774 struct bt_ctf_field_type_integer
*integer
;
1780 integer
= container_of(
1781 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1782 struct bt_ctf_field_type_integer
, parent
);
1787 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1789 struct bt_ctf_field_type_enumeration
*enumeration
;
1795 enumeration
= container_of(
1796 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1797 struct bt_ctf_field_type_enumeration
, parent
);
1798 g_ptr_array_free(enumeration
->entries
, TRUE
);
1799 bt_ctf_field_type_put(enumeration
->container
);
1800 g_free(enumeration
);
1804 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1806 struct bt_ctf_field_type_floating_point
*floating_point
;
1812 floating_point
= container_of(
1813 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1814 struct bt_ctf_field_type_floating_point
, parent
);
1815 g_free(floating_point
);
1819 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1821 struct bt_ctf_field_type_structure
*structure
;
1827 structure
= container_of(
1828 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1829 struct bt_ctf_field_type_structure
, parent
);
1830 g_ptr_array_free(structure
->fields
, TRUE
);
1831 g_hash_table_destroy(structure
->field_name_to_index
);
1836 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1838 struct bt_ctf_field_type_variant
*variant
;
1844 variant
= container_of(
1845 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1846 struct bt_ctf_field_type_variant
, parent
);
1847 g_ptr_array_free(variant
->fields
, TRUE
);
1848 g_hash_table_destroy(variant
->field_name_to_index
);
1849 g_string_free(variant
->tag_name
, TRUE
);
1850 bt_ctf_field_type_put(&variant
->tag
->parent
);
1855 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1857 struct bt_ctf_field_type_array
*array
;
1863 array
= container_of(
1864 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1865 struct bt_ctf_field_type_array
, parent
);
1866 bt_ctf_field_type_put(array
->element_type
);
1871 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1873 struct bt_ctf_field_type_sequence
*sequence
;
1879 sequence
= container_of(
1880 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1881 struct bt_ctf_field_type_sequence
, parent
);
1882 bt_ctf_field_type_put(sequence
->element_type
);
1883 g_string_free(sequence
->length_field_name
, TRUE
);
1888 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1890 struct bt_ctf_field_type_string
*string
;
1896 string
= container_of(
1897 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1898 struct bt_ctf_field_type_string
, parent
);
1903 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
1909 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
1911 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
1912 type
, struct bt_ctf_field_type_enumeration
, parent
);
1914 generic_field_type_freeze(type
);
1915 bt_ctf_field_type_freeze(enumeration_type
->container
);
1919 void freeze_structure_field(struct structure_field
*field
)
1921 bt_ctf_field_type_freeze(field
->type
);
1925 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
1927 struct bt_ctf_field_type_structure
*structure_type
= container_of(
1928 type
, struct bt_ctf_field_type_structure
, parent
);
1930 generic_field_type_freeze(type
);
1931 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
1936 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
1938 struct bt_ctf_field_type_variant
*variant_type
= container_of(
1939 type
, struct bt_ctf_field_type_variant
, parent
);
1941 generic_field_type_freeze(type
);
1942 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
1947 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
1949 struct bt_ctf_field_type_array
*array_type
= container_of(
1950 type
, struct bt_ctf_field_type_array
, parent
);
1952 generic_field_type_freeze(type
);
1953 bt_ctf_field_type_freeze(array_type
->element_type
);
1957 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
1959 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
1960 type
, struct bt_ctf_field_type_sequence
, parent
);
1962 generic_field_type_freeze(type
);
1963 bt_ctf_field_type_freeze(sequence_type
->element_type
);
1967 const char *get_encoding_string(enum ctf_string_encoding encoding
)
1969 const char *encoding_string
;
1972 case CTF_STRING_NONE
:
1973 encoding_string
= "none";
1975 case CTF_STRING_ASCII
:
1976 encoding_string
= "ASCII";
1978 case CTF_STRING_UTF8
:
1979 encoding_string
= "UTF8";
1982 encoding_string
= "unknown";
1986 return encoding_string
;
1990 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
1992 const char *base_string
;
1995 case BT_CTF_INTEGER_BASE_DECIMAL
:
1996 base_string
= "decimal";
1998 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
1999 base_string
= "hexadecimal";
2001 case BT_CTF_INTEGER_BASE_OCTAL
:
2002 base_string
= "octal";
2004 case BT_CTF_INTEGER_BASE_BINARY
:
2005 base_string
= "binary";
2008 base_string
= "unknown";
2016 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2017 struct metadata_context
*context
)
2019 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2020 struct bt_ctf_field_type_integer
, parent
);
2022 g_string_append_printf(context
->string
,
2023 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
2024 integer
->declaration
.len
, type
->declaration
->alignment
,
2025 (integer
->declaration
.signedness
? "true" : "false"),
2026 get_encoding_string(integer
->declaration
.encoding
),
2027 get_integer_base_string(integer
->declaration
.base
),
2028 get_byte_order_string(integer
->declaration
.byte_order
));
2033 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2034 struct metadata_context
*context
)
2038 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2039 struct bt_ctf_field_type_enumeration
, parent
);
2040 struct bt_ctf_field_type
*container_type
;
2041 int container_signed
;
2043 ret
= bt_ctf_field_type_validate(type
);
2048 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2049 if (!container_type
) {
2054 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2055 if (container_signed
< 0) {
2056 ret
= container_signed
;
2057 goto error_put_container_type
;
2060 g_string_append(context
->string
, "enum : ");
2061 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2063 goto error_put_container_type
;
2066 g_string_append(context
->string
, " { ");
2067 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2068 struct enumeration_mapping
*mapping
=
2069 enumeration
->entries
->pdata
[entry
];
2071 if (container_signed
) {
2072 if (mapping
->range_start
._signed
==
2073 mapping
->range_end
._signed
) {
2074 g_string_append_printf(context
->string
,
2075 "\"%s\" = %" PRId64
,
2076 g_quark_to_string(mapping
->string
),
2077 mapping
->range_start
._signed
);
2079 g_string_append_printf(context
->string
,
2080 "\"%s\" = %" PRId64
" ... %" PRId64
,
2081 g_quark_to_string(mapping
->string
),
2082 mapping
->range_start
._signed
,
2083 mapping
->range_end
._signed
);
2086 if (mapping
->range_start
._unsigned
==
2087 mapping
->range_end
._unsigned
) {
2088 g_string_append_printf(context
->string
,
2089 "\"%s\" = %" PRIu64
,
2090 g_quark_to_string(mapping
->string
),
2091 mapping
->range_start
._unsigned
);
2093 g_string_append_printf(context
->string
,
2094 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2095 g_quark_to_string(mapping
->string
),
2096 mapping
->range_start
._unsigned
,
2097 mapping
->range_end
._unsigned
);
2101 g_string_append(context
->string
,
2102 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2106 if (context
->field_name
->len
) {
2107 g_string_append_printf(context
->string
, " %s",
2108 context
->field_name
->str
);
2109 g_string_assign(context
->field_name
, "");
2111 error_put_container_type
:
2112 bt_ctf_field_type_put(container_type
);
2118 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2119 struct metadata_context
*context
)
2121 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2122 type
, struct bt_ctf_field_type_floating_point
, parent
);
2124 g_string_append_printf(context
->string
,
2125 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2126 floating_point
->declaration
.exp
->len
,
2127 floating_point
->declaration
.mantissa
->len
+ 1,
2128 get_byte_order_string(floating_point
->declaration
.byte_order
),
2129 type
->declaration
->alignment
);
2134 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2135 struct metadata_context
*context
)
2138 unsigned int indent
;
2140 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2141 struct bt_ctf_field_type_structure
, parent
);
2142 GString
*structure_field_name
= context
->field_name
;
2144 context
->field_name
= g_string_new("");
2146 context
->current_indentation_level
++;
2147 g_string_append(context
->string
, "struct {\n");
2149 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2150 struct structure_field
*field
;
2152 for (indent
= 0; indent
< context
->current_indentation_level
;
2154 g_string_append_c(context
->string
, '\t');
2157 field
= structure
->fields
->pdata
[i
];
2158 g_string_assign(context
->field_name
,
2159 g_quark_to_string(field
->name
));
2160 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2165 if (context
->field_name
->len
) {
2166 g_string_append_printf(context
->string
, " %s",
2167 context
->field_name
->str
);
2169 g_string_append(context
->string
, ";\n");
2172 context
->current_indentation_level
--;
2173 for (indent
= 0; indent
< context
->current_indentation_level
;
2175 g_string_append_c(context
->string
, '\t');
2178 g_string_append_printf(context
->string
, "} align(%zu)",
2179 type
->declaration
->alignment
);
2181 g_string_free(context
->field_name
, TRUE
);
2182 context
->field_name
= structure_field_name
;
2187 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2188 struct metadata_context
*context
)
2191 unsigned int indent
;
2193 struct bt_ctf_field_type_variant
*variant
= container_of(
2194 type
, struct bt_ctf_field_type_variant
, parent
);
2195 GString
*variant_field_name
= context
->field_name
;
2197 context
->field_name
= g_string_new("");
2198 g_string_append_printf(context
->string
,
2199 "variant <%s> {\n", variant
->tag_name
->str
);
2200 context
->current_indentation_level
++;
2201 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2202 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2204 g_string_assign(context
->field_name
,
2205 g_quark_to_string(field
->name
));
2206 for (indent
= 0; indent
< context
->current_indentation_level
;
2208 g_string_append_c(context
->string
, '\t');
2211 g_string_assign(context
->field_name
,
2212 g_quark_to_string(field
->name
));
2213 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2218 if (context
->field_name
->len
) {
2219 g_string_append_printf(context
->string
, " %s;",
2220 context
->field_name
->str
);
2223 g_string_append_c(context
->string
, '\n');
2226 context
->current_indentation_level
--;
2227 for (indent
= 0; indent
< context
->current_indentation_level
;
2229 g_string_append_c(context
->string
, '\t');
2232 g_string_append(context
->string
, "}");
2234 g_string_free(context
->field_name
, TRUE
);
2235 context
->field_name
= variant_field_name
;
2240 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2241 struct metadata_context
*context
)
2244 struct bt_ctf_field_type_array
*array
= container_of(type
,
2245 struct bt_ctf_field_type_array
, parent
);
2247 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2252 if (context
->field_name
->len
) {
2253 g_string_append_printf(context
->string
, " %s[%u]",
2254 context
->field_name
->str
, array
->length
);
2255 g_string_assign(context
->field_name
, "");
2257 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2264 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2265 struct metadata_context
*context
)
2268 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2269 type
, struct bt_ctf_field_type_sequence
, parent
);
2271 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2276 if (context
->field_name
->len
) {
2277 g_string_append_printf(context
->string
, " %s[%s]",
2278 context
->field_name
->str
,
2279 sequence
->length_field_name
->str
);
2280 g_string_assign(context
->field_name
, "");
2282 g_string_append_printf(context
->string
, "[%s]",
2283 sequence
->length_field_name
->str
);
2290 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2291 struct metadata_context
*context
)
2293 struct bt_ctf_field_type_string
*string
= container_of(
2294 type
, struct bt_ctf_field_type_string
, parent
);
2296 g_string_append_printf(context
->string
,
2297 "string { encoding = %s; }",
2298 get_encoding_string(string
->declaration
.encoding
));
2303 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2306 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2307 struct bt_ctf_field_type_integer
, parent
);
2309 integer_type
->declaration
.byte_order
= byte_order
;
2313 void bt_ctf_field_type_floating_point_set_byte_order(
2314 struct bt_ctf_field_type
*type
, int byte_order
)
2316 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2317 container_of(type
, struct bt_ctf_field_type_floating_point
,
2320 floating_point_type
->declaration
.byte_order
= byte_order
;
2321 floating_point_type
->sign
.byte_order
= byte_order
;
2322 floating_point_type
->mantissa
.byte_order
= byte_order
;
2323 floating_point_type
->exp
.byte_order
= byte_order
;