4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ctf-writer/writer-internal.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/endian.h>
39 struct range_overlap_query
{
54 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
56 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
58 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
60 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
62 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
64 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
66 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
68 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
70 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
73 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
74 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
76 bt_ctf_field_type_enumeration_destroy
,
78 bt_ctf_field_type_floating_point_destroy
,
79 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
80 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
81 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
82 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
83 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
87 void generic_field_type_freeze(struct bt_ctf_field_type
*);
89 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
91 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
93 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
100 type_freeze_func
const type_freeze_funcs
[] = {
101 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
102 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
103 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
104 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
105 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
106 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
107 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
108 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
112 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
113 struct metadata_context
*);
115 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
116 struct metadata_context
*);
118 int bt_ctf_field_type_floating_point_serialize(
119 struct bt_ctf_field_type
*, struct metadata_context
*);
121 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
122 struct metadata_context
*);
124 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
125 struct metadata_context
*);
127 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
128 struct metadata_context
*);
130 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
131 struct metadata_context
*);
133 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
134 struct metadata_context
*);
137 type_serialize_func
const type_serialize_funcs
[] = {
138 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
140 bt_ctf_field_type_enumeration_serialize
,
142 bt_ctf_field_type_floating_point_serialize
,
144 bt_ctf_field_type_structure_serialize
,
145 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
146 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
147 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
148 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
152 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
153 int byte_order
, int set_native
);
155 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
156 int byte_order
, int set_native
);
158 void bt_ctf_field_type_floating_point_set_byte_order(
159 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
161 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
162 int byte_order
, int set_native
);
164 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
165 int byte_order
, int set_native
);
167 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
168 int byte_order
, int set_native
);
170 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
171 int byte_order
, int set_native
);
173 /* The set_native flag only set the byte order if it is set to native */
175 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
176 int byte_order
, int set_native
) = {
177 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
179 bt_ctf_field_type_enumeration_set_byte_order
,
181 bt_ctf_field_type_floating_point_set_byte_order
,
183 bt_ctf_field_type_structure_set_byte_order
,
184 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
185 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
186 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
187 [CTF_TYPE_STRING
] = NULL
,
191 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
197 void destroy_structure_field(struct structure_field
*field
)
200 bt_ctf_field_type_put(field
->type
);
207 void check_ranges_overlap(gpointer element
, gpointer query
)
209 struct enumeration_mapping
*mapping
= element
;
210 struct range_overlap_query
*overlap_query
= query
;
212 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
213 && overlap_query
->range_start
._signed
<=
214 mapping
->range_end
._signed
) {
215 overlap_query
->overlaps
= 1;
216 overlap_query
->mapping_name
= mapping
->string
;
219 overlap_query
->overlaps
|=
220 mapping
->string
== overlap_query
->mapping_name
;
224 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
226 struct enumeration_mapping
*mapping
= element
;
227 struct range_overlap_query
*overlap_query
= query
;
229 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
230 && overlap_query
->range_start
._unsigned
<=
231 mapping
->range_end
._unsigned
) {
232 overlap_query
->overlaps
= 1;
233 overlap_query
->mapping_name
= mapping
->string
;
236 overlap_query
->overlaps
|=
237 mapping
->string
== overlap_query
->mapping_name
;
241 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
242 struct enumeration_mapping
**b
)
244 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
248 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
249 struct enumeration_mapping
**b
)
251 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
255 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
257 enum ctf_type_id type_id
= type
->declaration
->id
;
260 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
261 (type_id
< NR_CTF_TYPES
));
263 bt_ctf_ref_init(&type
->ref_count
);
264 type
->freeze
= type_freeze_funcs
[type_id
];
265 type
->serialize
= type_serialize_funcs
[type_id
];
266 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
268 type
->declaration
->alignment
= 1;
272 int add_structure_field(GPtrArray
*fields
,
273 GHashTable
*field_name_to_index
,
274 struct bt_ctf_field_type
*field_type
,
275 const char *field_name
)
278 GQuark name_quark
= g_quark_from_string(field_name
);
279 struct structure_field
*field
;
281 /* Make sure structure does not contain a field of the same name */
282 if (g_hash_table_lookup_extended(field_name_to_index
,
283 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
288 field
= g_new0(struct structure_field
, 1);
294 bt_ctf_field_type_get(field_type
);
295 field
->name
= name_quark
;
296 field
->type
= field_type
;
297 g_hash_table_insert(field_name_to_index
,
298 (gpointer
) (unsigned long) name_quark
,
299 (gpointer
) (unsigned long) fields
->len
);
300 g_ptr_array_add(fields
, field
);
301 bt_ctf_field_type_freeze(field_type
);
307 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
309 struct bt_ctf_field_type
*type
;
310 enum ctf_type_id type_id
;
316 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
317 type_id
= type
->declaration
->id
;
318 if (type_id
<= CTF_TYPE_UNKNOWN
||
319 type_id
>= NR_CTF_TYPES
) {
323 if (type
->alias_name
) {
324 g_string_free(type
->alias_name
, TRUE
);
326 type_destroy_funcs
[type_id
](ref
);
330 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
339 switch (type
->declaration
->id
) {
342 struct bt_ctf_field_type_enumeration
*enumeration
=
343 container_of(type
, struct bt_ctf_field_type_enumeration
,
346 /* Ensure enum has entries */
347 ret
= enumeration
->entries
->len
? 0 : -1;
357 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
359 struct bt_ctf_field_type_integer
*integer
=
360 g_new0(struct bt_ctf_field_type_integer
, 1);
362 if (!integer
|| size
> 64) {
366 integer
->parent
.declaration
= &integer
->declaration
.p
;
367 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
368 integer
->declaration
.len
= size
;
369 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
370 integer
->declaration
.encoding
= CTF_STRING_NONE
;
371 bt_ctf_field_type_init(&integer
->parent
);
372 return &integer
->parent
;
375 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
378 struct bt_ctf_field_type_integer
*integer
;
380 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
385 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
386 ret
= (int) integer
->declaration
.len
;
391 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
394 struct bt_ctf_field_type_integer
*integer
;
396 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
401 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
402 ret
= integer
->declaration
.signedness
;
407 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
411 struct bt_ctf_field_type_integer
*integer
;
413 if (!type
|| type
->frozen
||
414 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
419 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
420 if (is_signed
&& integer
->declaration
.len
<= 1) {
425 integer
->declaration
.signedness
= !!is_signed
;
430 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
431 struct bt_ctf_field_type
*type
)
433 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
434 struct bt_ctf_field_type_integer
*integer
;
436 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
440 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
441 ret
= integer
->declaration
.base
;
446 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
447 enum bt_ctf_integer_base base
)
451 if (!type
|| type
->frozen
||
452 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
458 case BT_CTF_INTEGER_BASE_BINARY
:
459 case BT_CTF_INTEGER_BASE_OCTAL
:
460 case BT_CTF_INTEGER_BASE_DECIMAL
:
461 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
463 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
464 struct bt_ctf_field_type_integer
, parent
);
465 integer
->declaration
.base
= base
;
475 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
476 struct bt_ctf_field_type
*type
)
478 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
479 struct bt_ctf_field_type_integer
*integer
;
481 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
485 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
486 ret
= integer
->declaration
.encoding
;
491 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
492 enum ctf_string_encoding encoding
)
495 struct bt_ctf_field_type_integer
*integer
;
497 if (!type
|| type
->frozen
||
498 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
499 (encoding
< CTF_STRING_NONE
) ||
500 (encoding
>= CTF_STRING_UNKNOWN
)) {
505 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
506 integer
->declaration
.encoding
= encoding
;
511 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
512 struct bt_ctf_field_type
*integer_container_type
)
514 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
516 if (!integer_container_type
) {
520 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
524 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
529 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
530 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
531 bt_ctf_field_type_get(integer_container_type
);
532 enumeration
->container
= integer_container_type
;
533 enumeration
->entries
= g_ptr_array_new_with_free_func(
534 (GDestroyNotify
)destroy_enumeration_mapping
);
535 bt_ctf_field_type_init(&enumeration
->parent
);
536 return &enumeration
->parent
;
542 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
543 struct bt_ctf_field_type
*type
)
545 struct bt_ctf_field_type
*container_type
= NULL
;
546 struct bt_ctf_field_type_enumeration
*enumeration_type
;
552 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
556 enumeration_type
= container_of(type
,
557 struct bt_ctf_field_type_enumeration
, parent
);
558 container_type
= enumeration_type
->container
;
559 bt_ctf_field_type_get(container_type
);
561 return container_type
;
564 int bt_ctf_field_type_enumeration_add_mapping(
565 struct bt_ctf_field_type
*type
, const char *string
,
566 int64_t range_start
, int64_t range_end
)
570 struct enumeration_mapping
*mapping
;
571 struct bt_ctf_field_type_enumeration
*enumeration
;
572 struct range_overlap_query query
;
573 char *escaped_string
;
575 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
577 (range_end
< range_start
)) {
582 if (!string
|| strlen(string
) == 0) {
587 escaped_string
= g_strescape(string
, NULL
);
588 if (!escaped_string
) {
593 mapping_name
= g_quark_from_string(escaped_string
);
594 query
= (struct range_overlap_query
) {
595 .range_start
._signed
= range_start
,
596 .range_end
._signed
= range_end
,
597 .mapping_name
= mapping_name
,
599 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
602 /* Check that the range does not overlap with one already present */
603 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
604 if (query
.overlaps
) {
609 mapping
= g_new(struct enumeration_mapping
, 1);
615 *mapping
= (struct enumeration_mapping
) {
616 .range_start
._signed
= range_start
,
617 .range_end
._signed
= range_end
, .string
= mapping_name
};
618 g_ptr_array_add(enumeration
->entries
, mapping
);
619 g_ptr_array_sort(enumeration
->entries
,
620 (GCompareFunc
)compare_enumeration_mappings_signed
);
622 free(escaped_string
);
627 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
628 struct bt_ctf_field_type
*type
, const char *string
,
629 uint64_t range_start
, uint64_t range_end
)
633 struct enumeration_mapping
*mapping
;
634 struct bt_ctf_field_type_enumeration
*enumeration
;
635 struct range_overlap_query query
;
636 char *escaped_string
;
638 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
640 (range_end
< range_start
)) {
645 if (!string
|| strlen(string
) == 0) {
650 escaped_string
= g_strescape(string
, NULL
);
651 if (!escaped_string
) {
656 mapping_name
= g_quark_from_string(escaped_string
);
657 query
= (struct range_overlap_query
) {
658 .range_start
._unsigned
= range_start
,
659 .range_end
._unsigned
= range_end
,
660 .mapping_name
= mapping_name
,
662 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
665 /* Check that the range does not overlap with one already present */
666 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
668 if (query
.overlaps
) {
673 mapping
= g_new(struct enumeration_mapping
, 1);
679 *mapping
= (struct enumeration_mapping
) {
680 .range_start
._unsigned
= range_start
,
681 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
682 g_ptr_array_add(enumeration
->entries
, mapping
);
683 g_ptr_array_sort(enumeration
->entries
,
684 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
686 free(escaped_string
);
691 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
692 struct bt_ctf_field_type_enumeration
*enumeration_type
,
695 const char *name
= NULL
;
696 struct range_overlap_query query
=
697 (struct range_overlap_query
) {
698 .range_start
._unsigned
= value
,
699 .range_end
._unsigned
= value
,
702 g_ptr_array_foreach(enumeration_type
->entries
,
703 check_ranges_overlap_unsigned
,
705 if (!query
.overlaps
) {
709 name
= g_quark_to_string(query
.mapping_name
);
714 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
715 struct bt_ctf_field_type_enumeration
*enumeration_type
,
718 const char *name
= NULL
;
719 struct range_overlap_query query
=
720 (struct range_overlap_query
) {
721 .range_start
._signed
= value
,
722 .range_end
._signed
= value
,
725 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
727 if (!query
.overlaps
) {
731 name
= g_quark_to_string(query
.mapping_name
);
736 int bt_ctf_field_type_enumeration_get_mapping_count(
737 struct bt_ctf_field_type
*type
)
740 struct bt_ctf_field_type_enumeration
*enumeration
;
742 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
747 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
749 ret
= (int) enumeration
->entries
->len
;
755 struct enumeration_mapping
*get_enumeration_mapping(
756 struct bt_ctf_field_type
*type
, int index
)
758 struct enumeration_mapping
*mapping
= NULL
;
759 struct bt_ctf_field_type_enumeration
*enumeration
;
761 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
763 if (index
>= enumeration
->entries
->len
) {
767 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
772 int bt_ctf_field_type_enumeration_get_mapping(
773 struct bt_ctf_field_type
*type
, int index
,
774 const char **string
, int64_t *range_start
, int64_t *range_end
)
776 struct enumeration_mapping
*mapping
;
779 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
780 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
785 mapping
= get_enumeration_mapping(type
, index
);
791 *string
= g_quark_to_string(mapping
->string
);
792 *range_start
= mapping
->range_start
._signed
;
793 *range_end
= mapping
->range_end
._signed
;
798 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
799 struct bt_ctf_field_type
*type
, int index
,
800 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
802 struct enumeration_mapping
*mapping
;
805 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
806 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
811 mapping
= get_enumeration_mapping(type
, index
);
817 *string
= g_quark_to_string(mapping
->string
);
818 *range_start
= mapping
->range_start
._unsigned
;
819 *range_end
= mapping
->range_end
._unsigned
;
824 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
825 struct bt_ctf_field_type
*type
, const char *name
)
828 struct bt_ctf_field_type_enumeration
*enumeration
;
831 if (!type
|| !name
||
832 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
837 name_quark
= g_quark_try_string(name
);
843 enumeration
= container_of(type
,
844 struct bt_ctf_field_type_enumeration
, parent
);
845 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
846 struct enumeration_mapping
*mapping
=
847 get_enumeration_mapping(type
, i
);
849 if (mapping
->string
== name_quark
) {
860 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
861 struct bt_ctf_field_type
*type
, int64_t value
)
863 struct bt_ctf_field_type_enumeration
*enumeration
;
866 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
871 enumeration
= container_of(type
,
872 struct bt_ctf_field_type_enumeration
, parent
);
873 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
874 struct enumeration_mapping
*mapping
=
875 get_enumeration_mapping(type
, i
);
877 if (value
>= mapping
->range_start
._signed
&&
878 value
<= mapping
->range_end
._signed
) {
889 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
890 struct bt_ctf_field_type
*type
, uint64_t value
)
892 struct bt_ctf_field_type_enumeration
*enumeration
;
895 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
900 enumeration
= container_of(type
,
901 struct bt_ctf_field_type_enumeration
, parent
);
902 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
903 struct enumeration_mapping
*mapping
=
904 get_enumeration_mapping(type
, i
);
906 if (value
>= mapping
->range_start
._unsigned
&&
907 value
<= mapping
->range_end
._unsigned
) {
918 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
920 struct bt_ctf_field_type_floating_point
*floating_point
=
921 g_new0(struct bt_ctf_field_type_floating_point
, 1);
923 if (!floating_point
) {
927 floating_point
->declaration
.sign
= &floating_point
->sign
;
928 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
929 floating_point
->declaration
.exp
= &floating_point
->exp
;
930 floating_point
->sign
.len
= 1;
931 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
932 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
933 floating_point
->declaration
.exp
->len
=
934 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
935 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
936 floating_point
->sign
.p
.alignment
= 1;
937 floating_point
->mantissa
.p
.alignment
= 1;
938 floating_point
->exp
.p
.alignment
= 1;
940 bt_ctf_field_type_init(&floating_point
->parent
);
942 return floating_point
? &floating_point
->parent
: NULL
;
945 int bt_ctf_field_type_floating_point_get_exponent_digits(
946 struct bt_ctf_field_type
*type
)
949 struct bt_ctf_field_type_floating_point
*floating_point
;
951 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
956 floating_point
= container_of(type
,
957 struct bt_ctf_field_type_floating_point
, parent
);
958 ret
= (int) floating_point
->declaration
.exp
->len
;
963 int bt_ctf_field_type_floating_point_set_exponent_digits(
964 struct bt_ctf_field_type
*type
,
965 unsigned int exponent_digits
)
968 struct bt_ctf_field_type_floating_point
*floating_point
;
970 if (!type
|| type
->frozen
||
971 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
976 floating_point
= container_of(type
,
977 struct bt_ctf_field_type_floating_point
, parent
);
978 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
979 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
981 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
986 floating_point
->declaration
.exp
->len
= exponent_digits
;
991 int bt_ctf_field_type_floating_point_get_mantissa_digits(
992 struct bt_ctf_field_type
*type
)
995 struct bt_ctf_field_type_floating_point
*floating_point
;
997 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1002 floating_point
= container_of(type
,
1003 struct bt_ctf_field_type_floating_point
, parent
);
1004 ret
= (int) floating_point
->mantissa
.len
+ 1;
1009 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1010 struct bt_ctf_field_type
*type
,
1011 unsigned int mantissa_digits
)
1014 struct bt_ctf_field_type_floating_point
*floating_point
;
1016 if (!type
|| type
->frozen
||
1017 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1022 floating_point
= container_of(type
,
1023 struct bt_ctf_field_type_floating_point
, parent
);
1025 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1026 (mantissa_digits
!= DBL_MANT_DIG
) &&
1027 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1032 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1037 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1039 struct bt_ctf_field_type_structure
*structure
=
1040 g_new0(struct bt_ctf_field_type_structure
, 1);
1046 structure
->parent
.declaration
= &structure
->declaration
.p
;
1047 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1048 structure
->fields
= g_ptr_array_new_with_free_func(
1049 (GDestroyNotify
)destroy_structure_field
);
1050 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1051 bt_ctf_field_type_init(&structure
->parent
);
1052 return &structure
->parent
;
1057 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1058 struct bt_ctf_field_type
*field_type
,
1059 const char *field_name
)
1062 struct bt_ctf_field_type_structure
*structure
;
1064 if (!type
|| !field_type
|| type
->frozen
||
1065 bt_ctf_validate_identifier(field_name
) ||
1066 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1067 bt_ctf_field_type_validate(field_type
)) {
1072 structure
= container_of(type
,
1073 struct bt_ctf_field_type_structure
, parent
);
1074 if (add_structure_field(structure
->fields
,
1075 structure
->field_name_to_index
, field_type
, field_name
)) {
1080 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1081 type
->declaration
->alignment
=
1082 field_type
->declaration
->alignment
;
1088 int bt_ctf_field_type_structure_get_field_count(
1089 struct bt_ctf_field_type
*type
)
1092 struct bt_ctf_field_type_structure
*structure
;
1094 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1099 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1101 ret
= (int) structure
->fields
->len
;
1106 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1107 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1110 struct bt_ctf_field_type_structure
*structure
;
1111 struct structure_field
*field
;
1114 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1115 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1120 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1122 if (index
>= structure
->fields
->len
) {
1127 field
= g_ptr_array_index(structure
->fields
, index
);
1128 *field_type
= field
->type
;
1129 bt_ctf_field_type_get(field
->type
);
1130 *field_name
= g_quark_to_string(field
->name
);
1135 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1136 struct bt_ctf_field_type
*type
,
1141 struct structure_field
*field
;
1142 struct bt_ctf_field_type_structure
*structure
;
1143 struct bt_ctf_field_type
*field_type
= NULL
;
1145 if (!type
|| !name
) {
1149 name_quark
= g_quark_try_string(name
);
1154 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1156 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1157 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1161 field
= structure
->fields
->pdata
[index
];
1162 field_type
= field
->type
;
1163 bt_ctf_field_type_get(field_type
);
1168 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1169 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1171 struct bt_ctf_field_type_variant
*variant
= NULL
;
1173 if (!enum_tag
|| bt_ctf_validate_identifier(tag_name
) ||
1174 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1178 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1183 variant
->parent
.declaration
= &variant
->declaration
.p
;
1184 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1185 variant
->tag_name
= g_string_new(tag_name
);
1186 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1187 variant
->fields
= g_ptr_array_new_with_free_func(
1188 (GDestroyNotify
)destroy_structure_field
);
1189 bt_ctf_field_type_get(enum_tag
);
1190 variant
->tag
= container_of(enum_tag
,
1191 struct bt_ctf_field_type_enumeration
, parent
);
1192 bt_ctf_field_type_init(&variant
->parent
);
1193 return &variant
->parent
;
1198 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1199 struct bt_ctf_field_type
*type
)
1201 struct bt_ctf_field_type_variant
*variant
;
1202 struct bt_ctf_field_type
*tag_type
= NULL
;
1204 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1208 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1209 tag_type
= &variant
->tag
->parent
;
1210 bt_ctf_field_type_get(tag_type
);
1215 const char *bt_ctf_field_type_variant_get_tag_name(
1216 struct bt_ctf_field_type
*type
)
1218 struct bt_ctf_field_type_variant
*variant
;
1219 const char *tag_name
= NULL
;
1221 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1225 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1226 tag_name
= variant
->tag_name
->str
;
1231 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1232 struct bt_ctf_field_type
*field_type
,
1233 const char *field_name
)
1238 struct bt_ctf_field_type_variant
*variant
;
1239 GQuark field_name_quark
= g_quark_from_string(field_name
);
1241 if (!type
|| !field_type
|| type
->frozen
||
1242 bt_ctf_validate_identifier(field_name
) ||
1243 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1244 bt_ctf_field_type_validate(field_type
)) {
1249 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1250 /* Make sure this name is present in the enum tag */
1251 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1252 struct enumeration_mapping
*mapping
=
1253 g_ptr_array_index(variant
->tag
->entries
, i
);
1255 if (mapping
->string
== field_name_quark
) {
1261 if (!name_found
|| add_structure_field(variant
->fields
,
1262 variant
->field_name_to_index
, field_type
, field_name
)) {
1270 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1271 struct bt_ctf_field_type
*type
,
1272 const char *field_name
)
1276 struct structure_field
*field
;
1277 struct bt_ctf_field_type_variant
*variant
;
1278 struct bt_ctf_field_type
*field_type
= NULL
;
1280 if (!type
|| !field_name
) {
1284 name_quark
= g_quark_try_string(field_name
);
1289 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1290 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1291 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1295 field
= g_ptr_array_index(variant
->fields
, index
);
1296 field_type
= field
->type
;
1297 bt_ctf_field_type_get(field_type
);
1302 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1303 struct bt_ctf_field_type
*type
,
1304 struct bt_ctf_field
*tag
)
1306 const char *enum_value
;
1307 struct bt_ctf_field_type
*field_type
= NULL
;
1309 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1313 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1318 /* Already increments field_type's reference count */
1319 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1325 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1328 struct bt_ctf_field_type_variant
*variant
;
1330 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1335 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1337 ret
= (int) variant
->fields
->len
;
1343 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1344 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1347 struct bt_ctf_field_type_variant
*variant
;
1348 struct structure_field
*field
;
1351 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1352 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1357 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1359 if (index
>= variant
->fields
->len
) {
1364 field
= g_ptr_array_index(variant
->fields
, index
);
1365 *field_type
= field
->type
;
1366 bt_ctf_field_type_get(field
->type
);
1367 *field_name
= g_quark_to_string(field
->name
);
1372 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1373 struct bt_ctf_field_type
*element_type
,
1374 unsigned int length
)
1376 struct bt_ctf_field_type_array
*array
= NULL
;
1378 if (!element_type
|| length
== 0 ||
1379 bt_ctf_field_type_validate(element_type
)) {
1383 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1388 array
->parent
.declaration
= &array
->declaration
.p
;
1389 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1391 bt_ctf_field_type_get(element_type
);
1392 array
->element_type
= element_type
;
1393 array
->length
= length
;
1394 bt_ctf_field_type_init(&array
->parent
);
1395 array
->parent
.declaration
->alignment
=
1396 element_type
->declaration
->alignment
;
1397 return &array
->parent
;
1402 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1403 struct bt_ctf_field_type
*type
)
1405 struct bt_ctf_field_type
*ret
= NULL
;
1406 struct bt_ctf_field_type_array
*array
;
1408 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1412 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1413 ret
= array
->element_type
;
1414 bt_ctf_field_type_get(ret
);
1419 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1422 struct bt_ctf_field_type_array
*array
;
1424 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1429 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1430 ret
= (int64_t) array
->length
;
1435 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1436 struct bt_ctf_field_type
*element_type
,
1437 const char *length_field_name
)
1439 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1441 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1442 bt_ctf_field_type_validate(element_type
)) {
1446 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1451 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1452 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1453 bt_ctf_field_type_get(element_type
);
1454 sequence
->element_type
= element_type
;
1455 sequence
->length_field_name
= g_string_new(length_field_name
);
1456 bt_ctf_field_type_init(&sequence
->parent
);
1457 sequence
->parent
.declaration
->alignment
=
1458 element_type
->declaration
->alignment
;
1459 return &sequence
->parent
;
1464 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1465 struct bt_ctf_field_type
*type
)
1467 struct bt_ctf_field_type
*ret
= NULL
;
1468 struct bt_ctf_field_type_sequence
*sequence
;
1470 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1474 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1476 ret
= sequence
->element_type
;
1477 bt_ctf_field_type_get(ret
);
1482 const char *bt_ctf_field_type_sequence_get_length_field_name(
1483 struct bt_ctf_field_type
*type
)
1485 const char *ret
= NULL
;
1486 struct bt_ctf_field_type_sequence
*sequence
;
1488 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1492 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1494 ret
= sequence
->length_field_name
->str
;
1499 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1501 struct bt_ctf_field_type_string
*string
=
1502 g_new0(struct bt_ctf_field_type_string
, 1);
1508 string
->parent
.declaration
= &string
->declaration
.p
;
1509 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1510 bt_ctf_field_type_init(&string
->parent
);
1511 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1512 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1513 return &string
->parent
;
1516 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1517 struct bt_ctf_field_type
*type
)
1519 struct bt_ctf_field_type_string
*string
;
1520 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1522 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1526 string
= container_of(type
, struct bt_ctf_field_type_string
,
1528 ret
= string
->declaration
.encoding
;
1533 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1534 enum ctf_string_encoding encoding
)
1537 struct bt_ctf_field_type_string
*string
;
1539 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1540 (encoding
!= CTF_STRING_UTF8
&&
1541 encoding
!= CTF_STRING_ASCII
)) {
1546 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1547 string
->declaration
.encoding
= encoding
;
1552 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1561 ret
= (int) type
->declaration
->alignment
;
1566 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1567 unsigned int alignment
)
1571 /* Alignment must be bit-aligned (1) or byte aligned */
1572 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1577 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1578 alignment
!= CHAR_BIT
) {
1583 type
->declaration
->alignment
= alignment
;
1589 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1590 struct bt_ctf_field_type
*type
)
1592 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1593 int internal_byte_order
= -1;
1599 switch (type
->declaration
->id
) {
1600 case CTF_TYPE_INTEGER
:
1602 struct bt_ctf_field_type_integer
*integer
= container_of(
1603 type
, struct bt_ctf_field_type_integer
, parent
);
1604 internal_byte_order
= integer
->declaration
.byte_order
;
1607 case CTF_TYPE_FLOAT
:
1609 struct bt_ctf_field_type_floating_point
*floating_point
=
1611 struct bt_ctf_field_type_floating_point
,
1613 internal_byte_order
= floating_point
->declaration
.byte_order
;
1620 switch (internal_byte_order
) {
1622 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1625 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1628 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1631 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1637 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1638 enum bt_ctf_byte_order byte_order
)
1641 int internal_byte_order
;
1642 enum ctf_type_id type_id
;
1644 if (!type
|| type
->frozen
) {
1649 type_id
= type
->declaration
->id
;
1650 switch (byte_order
) {
1651 case BT_CTF_BYTE_ORDER_NATIVE
:
1652 /* Leave unset. Will be initialized by parent. */
1653 internal_byte_order
= 0;
1655 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1656 internal_byte_order
= LITTLE_ENDIAN
;
1658 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1659 case BT_CTF_BYTE_ORDER_NETWORK
:
1660 internal_byte_order
= BIG_ENDIAN
;
1667 if (set_byte_order_funcs
[type_id
]) {
1668 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1674 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1675 struct bt_ctf_field_type
*type
)
1678 return CTF_TYPE_UNKNOWN
;
1681 return type
->declaration
->id
;
1684 const char *bt_ctf_field_type_get_alias_name(
1685 struct bt_ctf_field_type
*type
)
1687 const char *name
= NULL
;
1689 if (!type
|| !type
->alias_name
) {
1693 name
= type
->alias_name
->str
;
1698 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1704 bt_ctf_ref_get(&type
->ref_count
);
1707 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1713 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1717 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1727 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1728 struct bt_ctf_field_type_variant
*variant
,
1731 struct bt_ctf_field_type
*type
= NULL
;
1732 GQuark field_name_quark
;
1734 struct structure_field
*field_entry
;
1735 struct range_overlap_query query
= {
1736 .range_start
._signed
= tag_value
,
1737 .range_end
._signed
= tag_value
,
1738 .mapping_name
= 0, .overlaps
= 0};
1740 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1742 if (!query
.overlaps
) {
1746 field_name_quark
= query
.mapping_name
;
1747 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1748 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1752 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1753 type
= field_entry
->type
;
1759 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1760 struct bt_ctf_field_type_variant
*variant
,
1763 struct bt_ctf_field_type
*type
= NULL
;
1764 GQuark field_name_quark
;
1766 struct structure_field
*field_entry
;
1767 struct range_overlap_query query
= {
1768 .range_start
._unsigned
= tag_value
,
1769 .range_end
._unsigned
= tag_value
,
1770 .mapping_name
= 0, .overlaps
= 0};
1772 g_ptr_array_foreach(variant
->tag
->entries
,
1773 check_ranges_overlap_unsigned
,
1775 if (!query
.overlaps
) {
1779 field_name_quark
= query
.mapping_name
;
1780 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1781 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1785 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1786 type
= field_entry
->type
;
1792 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1793 struct metadata_context
*context
)
1797 if (!type
|| !context
) {
1802 ret
= type
->serialize(type
, context
);
1808 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1815 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1816 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1817 set_byte_order_funcs
[type
->declaration
->id
](type
,
1823 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1825 struct bt_ctf_field_type_integer
*integer
;
1831 integer
= container_of(
1832 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1833 struct bt_ctf_field_type_integer
, parent
);
1838 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1840 struct bt_ctf_field_type_enumeration
*enumeration
;
1846 enumeration
= container_of(
1847 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1848 struct bt_ctf_field_type_enumeration
, parent
);
1849 g_ptr_array_free(enumeration
->entries
, TRUE
);
1850 bt_ctf_field_type_put(enumeration
->container
);
1851 g_free(enumeration
);
1855 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1857 struct bt_ctf_field_type_floating_point
*floating_point
;
1863 floating_point
= container_of(
1864 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1865 struct bt_ctf_field_type_floating_point
, parent
);
1866 g_free(floating_point
);
1870 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1872 struct bt_ctf_field_type_structure
*structure
;
1878 structure
= container_of(
1879 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1880 struct bt_ctf_field_type_structure
, parent
);
1881 g_ptr_array_free(structure
->fields
, TRUE
);
1882 g_hash_table_destroy(structure
->field_name_to_index
);
1887 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1889 struct bt_ctf_field_type_variant
*variant
;
1895 variant
= container_of(
1896 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1897 struct bt_ctf_field_type_variant
, parent
);
1898 g_ptr_array_free(variant
->fields
, TRUE
);
1899 g_hash_table_destroy(variant
->field_name_to_index
);
1900 g_string_free(variant
->tag_name
, TRUE
);
1901 bt_ctf_field_type_put(&variant
->tag
->parent
);
1906 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1908 struct bt_ctf_field_type_array
*array
;
1914 array
= container_of(
1915 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1916 struct bt_ctf_field_type_array
, parent
);
1917 bt_ctf_field_type_put(array
->element_type
);
1922 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1924 struct bt_ctf_field_type_sequence
*sequence
;
1930 sequence
= container_of(
1931 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1932 struct bt_ctf_field_type_sequence
, parent
);
1933 bt_ctf_field_type_put(sequence
->element_type
);
1934 g_string_free(sequence
->length_field_name
, TRUE
);
1939 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1941 struct bt_ctf_field_type_string
*string
;
1947 string
= container_of(
1948 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1949 struct bt_ctf_field_type_string
, parent
);
1954 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
1960 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
1962 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
1963 type
, struct bt_ctf_field_type_enumeration
, parent
);
1965 generic_field_type_freeze(type
);
1966 bt_ctf_field_type_freeze(enumeration_type
->container
);
1970 void freeze_structure_field(struct structure_field
*field
)
1972 bt_ctf_field_type_freeze(field
->type
);
1976 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
1978 struct bt_ctf_field_type_structure
*structure_type
= container_of(
1979 type
, struct bt_ctf_field_type_structure
, parent
);
1981 generic_field_type_freeze(type
);
1982 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
1987 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
1989 struct bt_ctf_field_type_variant
*variant_type
= container_of(
1990 type
, struct bt_ctf_field_type_variant
, parent
);
1992 generic_field_type_freeze(type
);
1993 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
1998 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2000 struct bt_ctf_field_type_array
*array_type
= container_of(
2001 type
, struct bt_ctf_field_type_array
, parent
);
2003 generic_field_type_freeze(type
);
2004 bt_ctf_field_type_freeze(array_type
->element_type
);
2008 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2010 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2011 type
, struct bt_ctf_field_type_sequence
, parent
);
2013 generic_field_type_freeze(type
);
2014 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2018 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2020 const char *encoding_string
;
2023 case CTF_STRING_NONE
:
2024 encoding_string
= "none";
2026 case CTF_STRING_ASCII
:
2027 encoding_string
= "ASCII";
2029 case CTF_STRING_UTF8
:
2030 encoding_string
= "UTF8";
2033 encoding_string
= "unknown";
2037 return encoding_string
;
2041 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2043 const char *base_string
;
2046 case BT_CTF_INTEGER_BASE_DECIMAL
:
2047 base_string
= "decimal";
2049 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2050 base_string
= "hexadecimal";
2052 case BT_CTF_INTEGER_BASE_OCTAL
:
2053 base_string
= "octal";
2055 case BT_CTF_INTEGER_BASE_BINARY
:
2056 base_string
= "binary";
2059 base_string
= "unknown";
2067 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2068 struct metadata_context
*context
)
2070 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2071 struct bt_ctf_field_type_integer
, parent
);
2073 g_string_append_printf(context
->string
,
2074 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
2075 integer
->declaration
.len
, type
->declaration
->alignment
,
2076 (integer
->declaration
.signedness
? "true" : "false"),
2077 get_encoding_string(integer
->declaration
.encoding
),
2078 get_integer_base_string(integer
->declaration
.base
),
2079 get_byte_order_string(integer
->declaration
.byte_order
));
2084 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2085 struct metadata_context
*context
)
2089 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2090 struct bt_ctf_field_type_enumeration
, parent
);
2091 struct bt_ctf_field_type
*container_type
;
2092 int container_signed
;
2094 ret
= bt_ctf_field_type_validate(type
);
2099 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2100 if (!container_type
) {
2105 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2106 if (container_signed
< 0) {
2107 ret
= container_signed
;
2108 goto error_put_container_type
;
2111 g_string_append(context
->string
, "enum : ");
2112 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2114 goto error_put_container_type
;
2117 g_string_append(context
->string
, " { ");
2118 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2119 struct enumeration_mapping
*mapping
=
2120 enumeration
->entries
->pdata
[entry
];
2122 if (container_signed
) {
2123 if (mapping
->range_start
._signed
==
2124 mapping
->range_end
._signed
) {
2125 g_string_append_printf(context
->string
,
2126 "\"%s\" = %" PRId64
,
2127 g_quark_to_string(mapping
->string
),
2128 mapping
->range_start
._signed
);
2130 g_string_append_printf(context
->string
,
2131 "\"%s\" = %" PRId64
" ... %" PRId64
,
2132 g_quark_to_string(mapping
->string
),
2133 mapping
->range_start
._signed
,
2134 mapping
->range_end
._signed
);
2137 if (mapping
->range_start
._unsigned
==
2138 mapping
->range_end
._unsigned
) {
2139 g_string_append_printf(context
->string
,
2140 "\"%s\" = %" PRIu64
,
2141 g_quark_to_string(mapping
->string
),
2142 mapping
->range_start
._unsigned
);
2144 g_string_append_printf(context
->string
,
2145 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2146 g_quark_to_string(mapping
->string
),
2147 mapping
->range_start
._unsigned
,
2148 mapping
->range_end
._unsigned
);
2152 g_string_append(context
->string
,
2153 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2157 if (context
->field_name
->len
) {
2158 g_string_append_printf(context
->string
, " %s",
2159 context
->field_name
->str
);
2160 g_string_assign(context
->field_name
, "");
2162 error_put_container_type
:
2163 bt_ctf_field_type_put(container_type
);
2169 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2170 struct metadata_context
*context
)
2172 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2173 type
, struct bt_ctf_field_type_floating_point
, parent
);
2175 g_string_append_printf(context
->string
,
2176 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2177 floating_point
->declaration
.exp
->len
,
2178 floating_point
->declaration
.mantissa
->len
+ 1,
2179 get_byte_order_string(floating_point
->declaration
.byte_order
),
2180 type
->declaration
->alignment
);
2185 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2186 struct metadata_context
*context
)
2189 unsigned int indent
;
2191 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2192 struct bt_ctf_field_type_structure
, parent
);
2193 GString
*structure_field_name
= context
->field_name
;
2195 context
->field_name
= g_string_new("");
2197 context
->current_indentation_level
++;
2198 g_string_append(context
->string
, "struct {\n");
2200 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2201 struct structure_field
*field
;
2203 for (indent
= 0; indent
< context
->current_indentation_level
;
2205 g_string_append_c(context
->string
, '\t');
2208 field
= structure
->fields
->pdata
[i
];
2209 g_string_assign(context
->field_name
,
2210 g_quark_to_string(field
->name
));
2211 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2216 if (context
->field_name
->len
) {
2217 g_string_append_printf(context
->string
, " %s",
2218 context
->field_name
->str
);
2220 g_string_append(context
->string
, ";\n");
2223 context
->current_indentation_level
--;
2224 for (indent
= 0; indent
< context
->current_indentation_level
;
2226 g_string_append_c(context
->string
, '\t');
2229 g_string_append_printf(context
->string
, "} align(%zu)",
2230 type
->declaration
->alignment
);
2232 g_string_free(context
->field_name
, TRUE
);
2233 context
->field_name
= structure_field_name
;
2238 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2239 struct metadata_context
*context
)
2242 unsigned int indent
;
2244 struct bt_ctf_field_type_variant
*variant
= container_of(
2245 type
, struct bt_ctf_field_type_variant
, parent
);
2246 GString
*variant_field_name
= context
->field_name
;
2248 context
->field_name
= g_string_new("");
2249 g_string_append_printf(context
->string
,
2250 "variant <%s> {\n", variant
->tag_name
->str
);
2251 context
->current_indentation_level
++;
2252 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2253 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2255 g_string_assign(context
->field_name
,
2256 g_quark_to_string(field
->name
));
2257 for (indent
= 0; indent
< context
->current_indentation_level
;
2259 g_string_append_c(context
->string
, '\t');
2262 g_string_assign(context
->field_name
,
2263 g_quark_to_string(field
->name
));
2264 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2269 if (context
->field_name
->len
) {
2270 g_string_append_printf(context
->string
, " %s;",
2271 context
->field_name
->str
);
2274 g_string_append_c(context
->string
, '\n');
2277 context
->current_indentation_level
--;
2278 for (indent
= 0; indent
< context
->current_indentation_level
;
2280 g_string_append_c(context
->string
, '\t');
2283 g_string_append(context
->string
, "}");
2285 g_string_free(context
->field_name
, TRUE
);
2286 context
->field_name
= variant_field_name
;
2291 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2292 struct metadata_context
*context
)
2295 struct bt_ctf_field_type_array
*array
= container_of(type
,
2296 struct bt_ctf_field_type_array
, parent
);
2298 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2303 if (context
->field_name
->len
) {
2304 g_string_append_printf(context
->string
, " %s[%u]",
2305 context
->field_name
->str
, array
->length
);
2306 g_string_assign(context
->field_name
, "");
2308 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2315 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2316 struct metadata_context
*context
)
2319 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2320 type
, struct bt_ctf_field_type_sequence
, parent
);
2322 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2327 if (context
->field_name
->len
) {
2328 g_string_append_printf(context
->string
, " %s[%s]",
2329 context
->field_name
->str
,
2330 sequence
->length_field_name
->str
);
2331 g_string_assign(context
->field_name
, "");
2333 g_string_append_printf(context
->string
, "[%s]",
2334 sequence
->length_field_name
->str
);
2341 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2342 struct metadata_context
*context
)
2344 struct bt_ctf_field_type_string
*string
= container_of(
2345 type
, struct bt_ctf_field_type_string
, parent
);
2347 g_string_append_printf(context
->string
,
2348 "string { encoding = %s; }",
2349 get_encoding_string(string
->declaration
.encoding
));
2354 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2355 int byte_order
, int set_native
)
2357 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2358 struct bt_ctf_field_type_integer
, parent
);
2361 integer_type
->declaration
.byte_order
=
2362 integer_type
->declaration
.byte_order
== 0 ?
2363 byte_order
: integer_type
->declaration
.byte_order
;
2365 integer_type
->declaration
.byte_order
= byte_order
;
2370 void bt_ctf_field_type_enumeration_set_byte_order(
2371 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2373 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2374 struct bt_ctf_field_type_enumeration
, parent
);
2376 /* Safe to assume that container is an integer */
2377 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2378 byte_order
, set_native
);
2382 void bt_ctf_field_type_floating_point_set_byte_order(
2383 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2385 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2386 container_of(type
, struct bt_ctf_field_type_floating_point
,
2390 floating_point_type
->declaration
.byte_order
=
2391 floating_point_type
->declaration
.byte_order
== 0 ?
2393 floating_point_type
->declaration
.byte_order
;
2394 floating_point_type
->sign
.byte_order
=
2395 floating_point_type
->sign
.byte_order
== 0 ?
2396 byte_order
: floating_point_type
->sign
.byte_order
;
2397 floating_point_type
->mantissa
.byte_order
=
2398 floating_point_type
->mantissa
.byte_order
== 0 ?
2399 byte_order
: floating_point_type
->mantissa
.byte_order
;
2400 floating_point_type
->exp
.byte_order
=
2401 floating_point_type
->exp
.byte_order
== 0 ?
2402 byte_order
: floating_point_type
->exp
.byte_order
;
2404 floating_point_type
->declaration
.byte_order
= byte_order
;
2405 floating_point_type
->sign
.byte_order
= byte_order
;
2406 floating_point_type
->mantissa
.byte_order
= byte_order
;
2407 floating_point_type
->exp
.byte_order
= byte_order
;
2412 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2413 int byte_order
, int set_native
)
2416 struct bt_ctf_field_type_structure
*structure_type
=
2417 container_of(type
, struct bt_ctf_field_type_structure
,
2420 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2421 struct structure_field
*field
= g_ptr_array_index(
2422 structure_type
->fields
, i
);
2423 struct bt_ctf_field_type
*field_type
= field
->type
;
2425 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2426 set_byte_order_funcs
[field_type
->declaration
->id
](
2427 field_type
, byte_order
, set_native
);
2433 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2434 int byte_order
, int set_native
)
2437 struct bt_ctf_field_type_variant
*variant_type
=
2438 container_of(type
, struct bt_ctf_field_type_variant
,
2441 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2442 struct structure_field
*field
= g_ptr_array_index(
2443 variant_type
->fields
, i
);
2444 struct bt_ctf_field_type
*field_type
= field
->type
;
2446 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2447 set_byte_order_funcs
[field_type
->declaration
->id
](
2448 field_type
, byte_order
, set_native
);
2454 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2455 int byte_order
, int set_native
)
2457 struct bt_ctf_field_type_array
*array_type
=
2458 container_of(type
, struct bt_ctf_field_type_array
,
2461 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2462 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2463 array_type
->element_type
, byte_order
, set_native
);
2468 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2469 int byte_order
, int set_native
)
2471 struct bt_ctf_field_type_sequence
*sequence_type
=
2472 container_of(type
, struct bt_ctf_field_type_sequence
,
2475 if (set_byte_order_funcs
[
2476 sequence_type
->element_type
->declaration
->id
]) {
2477 set_byte_order_funcs
[
2478 sequence_type
->element_type
->declaration
->id
](
2479 sequence_type
->element_type
, byte_order
, set_native
);