4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ctf-ir/clock.h>
33 #include <babeltrace/ctf-writer/writer-internal.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/endian.h>
40 struct range_overlap_query
{
55 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
69 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
71 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
74 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
75 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
77 bt_ctf_field_type_enumeration_destroy
,
79 bt_ctf_field_type_floating_point_destroy
,
80 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
81 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
82 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
83 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
84 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
88 void generic_field_type_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
101 type_freeze_func
const type_freeze_funcs
[] = {
102 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
103 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
104 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
105 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
106 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
107 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
108 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
109 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
113 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
114 struct metadata_context
*);
116 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_floating_point_serialize(
120 struct bt_ctf_field_type
*, struct metadata_context
*);
122 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
138 type_serialize_func
const type_serialize_funcs
[] = {
139 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
141 bt_ctf_field_type_enumeration_serialize
,
143 bt_ctf_field_type_floating_point_serialize
,
145 bt_ctf_field_type_structure_serialize
,
146 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
147 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
148 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
149 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
153 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
154 int byte_order
, int set_native
);
156 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_floating_point_set_byte_order(
160 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
162 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 /* The set_native flag only set the byte order if it is set to native */
176 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
177 int byte_order
, int set_native
) = {
178 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
180 bt_ctf_field_type_enumeration_set_byte_order
,
182 bt_ctf_field_type_floating_point_set_byte_order
,
184 bt_ctf_field_type_structure_set_byte_order
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
188 [CTF_TYPE_STRING
] = NULL
,
192 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
198 void destroy_structure_field(struct structure_field
*field
)
201 bt_ctf_field_type_put(field
->type
);
208 void check_ranges_overlap(gpointer element
, gpointer query
)
210 struct enumeration_mapping
*mapping
= element
;
211 struct range_overlap_query
*overlap_query
= query
;
213 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
214 && overlap_query
->range_start
._signed
<=
215 mapping
->range_end
._signed
) {
216 overlap_query
->overlaps
= 1;
217 overlap_query
->mapping_name
= mapping
->string
;
220 overlap_query
->overlaps
|=
221 mapping
->string
== overlap_query
->mapping_name
;
225 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
227 struct enumeration_mapping
*mapping
= element
;
228 struct range_overlap_query
*overlap_query
= query
;
230 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
231 && overlap_query
->range_start
._unsigned
<=
232 mapping
->range_end
._unsigned
) {
233 overlap_query
->overlaps
= 1;
234 overlap_query
->mapping_name
= mapping
->string
;
237 overlap_query
->overlaps
|=
238 mapping
->string
== overlap_query
->mapping_name
;
242 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
243 struct enumeration_mapping
**b
)
245 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
249 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
250 struct enumeration_mapping
**b
)
252 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
256 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
258 enum ctf_type_id type_id
= type
->declaration
->id
;
261 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
262 (type_id
< NR_CTF_TYPES
));
264 bt_ctf_ref_init(&type
->ref_count
);
265 type
->freeze
= type_freeze_funcs
[type_id
];
266 type
->serialize
= type_serialize_funcs
[type_id
];
267 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
269 type
->declaration
->alignment
= 1;
273 int add_structure_field(GPtrArray
*fields
,
274 GHashTable
*field_name_to_index
,
275 struct bt_ctf_field_type
*field_type
,
276 const char *field_name
)
279 GQuark name_quark
= g_quark_from_string(field_name
);
280 struct structure_field
*field
;
282 /* Make sure structure does not contain a field of the same name */
283 if (g_hash_table_lookup_extended(field_name_to_index
,
284 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
289 field
= g_new0(struct structure_field
, 1);
295 bt_ctf_field_type_get(field_type
);
296 field
->name
= name_quark
;
297 field
->type
= field_type
;
298 g_hash_table_insert(field_name_to_index
,
299 (gpointer
) (unsigned long) name_quark
,
300 (gpointer
) (unsigned long) fields
->len
);
301 g_ptr_array_add(fields
, field
);
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
== 0 || 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 integer
->declaration
.signedness
= !!is_signed
;
425 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
426 struct bt_ctf_field_type
*type
)
428 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
429 struct bt_ctf_field_type_integer
*integer
;
431 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
435 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
436 ret
= integer
->declaration
.base
;
441 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
442 enum bt_ctf_integer_base base
)
446 if (!type
|| type
->frozen
||
447 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
453 case BT_CTF_INTEGER_BASE_BINARY
:
454 case BT_CTF_INTEGER_BASE_OCTAL
:
455 case BT_CTF_INTEGER_BASE_DECIMAL
:
456 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
458 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
459 struct bt_ctf_field_type_integer
, parent
);
460 integer
->declaration
.base
= base
;
470 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
471 struct bt_ctf_field_type
*type
)
473 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
474 struct bt_ctf_field_type_integer
*integer
;
476 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
480 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
481 ret
= integer
->declaration
.encoding
;
486 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
487 enum ctf_string_encoding encoding
)
490 struct bt_ctf_field_type_integer
*integer
;
492 if (!type
|| type
->frozen
||
493 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
494 (encoding
< CTF_STRING_NONE
) ||
495 (encoding
>= CTF_STRING_UNKNOWN
)) {
500 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
501 integer
->declaration
.encoding
= encoding
;
506 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
507 struct bt_ctf_field_type
*type
)
509 struct bt_ctf_field_type_integer
*integer
;
510 struct bt_ctf_clock
*clock
= NULL
;
516 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
517 clock
= integer
->mapped_clock
;
519 bt_ctf_clock_get(clock
);
525 int bt_ctf_field_type_integer_set_mapped_clock(
526 struct bt_ctf_field_type
*type
,
527 struct bt_ctf_clock
*clock
)
529 struct bt_ctf_field_type_integer
*integer
;
532 if (!type
|| type
->frozen
) {
537 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
538 if (integer
->mapped_clock
) {
539 bt_ctf_clock_put(integer
->mapped_clock
);
543 bt_ctf_clock_get(clock
);
546 integer
->mapped_clock
= clock
;
551 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
552 struct bt_ctf_field_type
*integer_container_type
)
554 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
556 if (!integer_container_type
) {
560 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
564 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
569 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
570 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
571 bt_ctf_field_type_get(integer_container_type
);
572 enumeration
->container
= integer_container_type
;
573 enumeration
->entries
= g_ptr_array_new_with_free_func(
574 (GDestroyNotify
)destroy_enumeration_mapping
);
575 bt_ctf_field_type_init(&enumeration
->parent
);
576 return &enumeration
->parent
;
582 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
583 struct bt_ctf_field_type
*type
)
585 struct bt_ctf_field_type
*container_type
= NULL
;
586 struct bt_ctf_field_type_enumeration
*enumeration_type
;
592 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
596 enumeration_type
= container_of(type
,
597 struct bt_ctf_field_type_enumeration
, parent
);
598 container_type
= enumeration_type
->container
;
599 bt_ctf_field_type_get(container_type
);
601 return container_type
;
604 int bt_ctf_field_type_enumeration_add_mapping(
605 struct bt_ctf_field_type
*type
, const char *string
,
606 int64_t range_start
, int64_t range_end
)
610 struct enumeration_mapping
*mapping
;
611 struct bt_ctf_field_type_enumeration
*enumeration
;
612 struct range_overlap_query query
;
613 char *escaped_string
;
615 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
617 (range_end
< range_start
)) {
622 if (!string
|| strlen(string
) == 0) {
627 escaped_string
= g_strescape(string
, NULL
);
628 if (!escaped_string
) {
633 mapping_name
= g_quark_from_string(escaped_string
);
634 query
= (struct range_overlap_query
) {
635 .range_start
._signed
= range_start
,
636 .range_end
._signed
= range_end
,
637 .mapping_name
= mapping_name
,
639 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
642 /* Check that the range does not overlap with one already present */
643 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
644 if (query
.overlaps
) {
649 mapping
= g_new(struct enumeration_mapping
, 1);
655 *mapping
= (struct enumeration_mapping
) {
656 .range_start
._signed
= range_start
,
657 .range_end
._signed
= range_end
, .string
= mapping_name
};
658 g_ptr_array_add(enumeration
->entries
, mapping
);
659 g_ptr_array_sort(enumeration
->entries
,
660 (GCompareFunc
)compare_enumeration_mappings_signed
);
662 free(escaped_string
);
667 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
668 struct bt_ctf_field_type
*type
, const char *string
,
669 uint64_t range_start
, uint64_t range_end
)
673 struct enumeration_mapping
*mapping
;
674 struct bt_ctf_field_type_enumeration
*enumeration
;
675 struct range_overlap_query query
;
676 char *escaped_string
;
678 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
680 (range_end
< range_start
)) {
685 if (!string
|| strlen(string
) == 0) {
690 escaped_string
= g_strescape(string
, NULL
);
691 if (!escaped_string
) {
696 mapping_name
= g_quark_from_string(escaped_string
);
697 query
= (struct range_overlap_query
) {
698 .range_start
._unsigned
= range_start
,
699 .range_end
._unsigned
= range_end
,
700 .mapping_name
= mapping_name
,
702 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
705 /* Check that the range does not overlap with one already present */
706 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
708 if (query
.overlaps
) {
713 mapping
= g_new(struct enumeration_mapping
, 1);
719 *mapping
= (struct enumeration_mapping
) {
720 .range_start
._unsigned
= range_start
,
721 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
722 g_ptr_array_add(enumeration
->entries
, mapping
);
723 g_ptr_array_sort(enumeration
->entries
,
724 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
726 free(escaped_string
);
731 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
732 struct bt_ctf_field_type_enumeration
*enumeration_type
,
735 const char *name
= NULL
;
736 struct range_overlap_query query
=
737 (struct range_overlap_query
) {
738 .range_start
._unsigned
= value
,
739 .range_end
._unsigned
= value
,
742 g_ptr_array_foreach(enumeration_type
->entries
,
743 check_ranges_overlap_unsigned
,
745 if (!query
.overlaps
) {
749 name
= g_quark_to_string(query
.mapping_name
);
754 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
755 struct bt_ctf_field_type_enumeration
*enumeration_type
,
758 const char *name
= NULL
;
759 struct range_overlap_query query
=
760 (struct range_overlap_query
) {
761 .range_start
._signed
= value
,
762 .range_end
._signed
= value
,
765 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
767 if (!query
.overlaps
) {
771 name
= g_quark_to_string(query
.mapping_name
);
776 int bt_ctf_field_type_enumeration_get_mapping_count(
777 struct bt_ctf_field_type
*type
)
780 struct bt_ctf_field_type_enumeration
*enumeration
;
782 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
787 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
789 ret
= (int) enumeration
->entries
->len
;
795 struct enumeration_mapping
*get_enumeration_mapping(
796 struct bt_ctf_field_type
*type
, int index
)
798 struct enumeration_mapping
*mapping
= NULL
;
799 struct bt_ctf_field_type_enumeration
*enumeration
;
801 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
803 if (index
>= enumeration
->entries
->len
) {
807 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
812 int bt_ctf_field_type_enumeration_get_mapping(
813 struct bt_ctf_field_type
*type
, int index
,
814 const char **string
, int64_t *range_start
, int64_t *range_end
)
816 struct enumeration_mapping
*mapping
;
819 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
820 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
825 mapping
= get_enumeration_mapping(type
, index
);
831 *string
= g_quark_to_string(mapping
->string
);
832 *range_start
= mapping
->range_start
._signed
;
833 *range_end
= mapping
->range_end
._signed
;
838 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
839 struct bt_ctf_field_type
*type
, int index
,
840 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
842 struct enumeration_mapping
*mapping
;
845 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
846 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
851 mapping
= get_enumeration_mapping(type
, index
);
857 *string
= g_quark_to_string(mapping
->string
);
858 *range_start
= mapping
->range_start
._unsigned
;
859 *range_end
= mapping
->range_end
._unsigned
;
864 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
865 struct bt_ctf_field_type
*type
, const char *name
)
868 struct bt_ctf_field_type_enumeration
*enumeration
;
871 if (!type
|| !name
||
872 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
877 name_quark
= g_quark_try_string(name
);
883 enumeration
= container_of(type
,
884 struct bt_ctf_field_type_enumeration
, parent
);
885 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
886 struct enumeration_mapping
*mapping
=
887 get_enumeration_mapping(type
, i
);
889 if (mapping
->string
== name_quark
) {
900 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
901 struct bt_ctf_field_type
*type
, int64_t value
)
903 struct bt_ctf_field_type_enumeration
*enumeration
;
906 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
911 enumeration
= container_of(type
,
912 struct bt_ctf_field_type_enumeration
, parent
);
913 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
914 struct enumeration_mapping
*mapping
=
915 get_enumeration_mapping(type
, i
);
917 if (value
>= mapping
->range_start
._signed
&&
918 value
<= mapping
->range_end
._signed
) {
929 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
930 struct bt_ctf_field_type
*type
, uint64_t value
)
932 struct bt_ctf_field_type_enumeration
*enumeration
;
935 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
940 enumeration
= container_of(type
,
941 struct bt_ctf_field_type_enumeration
, parent
);
942 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
943 struct enumeration_mapping
*mapping
=
944 get_enumeration_mapping(type
, i
);
946 if (value
>= mapping
->range_start
._unsigned
&&
947 value
<= mapping
->range_end
._unsigned
) {
958 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
960 struct bt_ctf_field_type_floating_point
*floating_point
=
961 g_new0(struct bt_ctf_field_type_floating_point
, 1);
963 if (!floating_point
) {
967 floating_point
->declaration
.sign
= &floating_point
->sign
;
968 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
969 floating_point
->declaration
.exp
= &floating_point
->exp
;
970 floating_point
->sign
.len
= 1;
971 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
972 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
973 floating_point
->declaration
.exp
->len
=
974 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
975 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
976 floating_point
->sign
.p
.alignment
= 1;
977 floating_point
->mantissa
.p
.alignment
= 1;
978 floating_point
->exp
.p
.alignment
= 1;
980 bt_ctf_field_type_init(&floating_point
->parent
);
982 return floating_point
? &floating_point
->parent
: NULL
;
985 int bt_ctf_field_type_floating_point_get_exponent_digits(
986 struct bt_ctf_field_type
*type
)
989 struct bt_ctf_field_type_floating_point
*floating_point
;
991 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
996 floating_point
= container_of(type
,
997 struct bt_ctf_field_type_floating_point
, parent
);
998 ret
= (int) floating_point
->declaration
.exp
->len
;
1003 int bt_ctf_field_type_floating_point_set_exponent_digits(
1004 struct bt_ctf_field_type
*type
,
1005 unsigned int exponent_digits
)
1008 struct bt_ctf_field_type_floating_point
*floating_point
;
1010 if (!type
|| type
->frozen
||
1011 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1016 floating_point
= container_of(type
,
1017 struct bt_ctf_field_type_floating_point
, parent
);
1018 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1019 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1021 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1026 floating_point
->declaration
.exp
->len
= exponent_digits
;
1031 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1032 struct bt_ctf_field_type
*type
)
1035 struct bt_ctf_field_type_floating_point
*floating_point
;
1037 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1042 floating_point
= container_of(type
,
1043 struct bt_ctf_field_type_floating_point
, parent
);
1044 ret
= (int) floating_point
->mantissa
.len
+ 1;
1049 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1050 struct bt_ctf_field_type
*type
,
1051 unsigned int mantissa_digits
)
1054 struct bt_ctf_field_type_floating_point
*floating_point
;
1056 if (!type
|| type
->frozen
||
1057 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1062 floating_point
= container_of(type
,
1063 struct bt_ctf_field_type_floating_point
, parent
);
1065 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1066 (mantissa_digits
!= DBL_MANT_DIG
) &&
1067 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1072 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1077 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1079 struct bt_ctf_field_type_structure
*structure
=
1080 g_new0(struct bt_ctf_field_type_structure
, 1);
1086 structure
->parent
.declaration
= &structure
->declaration
.p
;
1087 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1088 structure
->fields
= g_ptr_array_new_with_free_func(
1089 (GDestroyNotify
)destroy_structure_field
);
1090 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1091 bt_ctf_field_type_init(&structure
->parent
);
1092 return &structure
->parent
;
1097 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1098 struct bt_ctf_field_type
*field_type
,
1099 const char *field_name
)
1102 struct bt_ctf_field_type_structure
*structure
;
1104 if (!type
|| !field_type
|| type
->frozen
||
1105 bt_ctf_validate_identifier(field_name
) ||
1106 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1107 bt_ctf_field_type_validate(field_type
)) {
1112 structure
= container_of(type
,
1113 struct bt_ctf_field_type_structure
, parent
);
1114 if (add_structure_field(structure
->fields
,
1115 structure
->field_name_to_index
, field_type
, field_name
)) {
1120 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1121 type
->declaration
->alignment
=
1122 field_type
->declaration
->alignment
;
1128 int bt_ctf_field_type_structure_get_field_count(
1129 struct bt_ctf_field_type
*type
)
1132 struct bt_ctf_field_type_structure
*structure
;
1134 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1139 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1141 ret
= (int) structure
->fields
->len
;
1146 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1147 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1150 struct bt_ctf_field_type_structure
*structure
;
1151 struct structure_field
*field
;
1154 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1155 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1160 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1162 if (index
>= structure
->fields
->len
) {
1167 field
= g_ptr_array_index(structure
->fields
, index
);
1168 *field_type
= field
->type
;
1169 bt_ctf_field_type_get(field
->type
);
1170 *field_name
= g_quark_to_string(field
->name
);
1175 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1176 struct bt_ctf_field_type
*type
,
1181 struct structure_field
*field
;
1182 struct bt_ctf_field_type_structure
*structure
;
1183 struct bt_ctf_field_type
*field_type
= NULL
;
1185 if (!type
|| !name
) {
1189 name_quark
= g_quark_try_string(name
);
1194 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1196 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1197 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1201 field
= structure
->fields
->pdata
[index
];
1202 field_type
= field
->type
;
1203 bt_ctf_field_type_get(field_type
);
1208 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1209 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1211 struct bt_ctf_field_type_variant
*variant
= NULL
;
1213 if (!enum_tag
|| bt_ctf_validate_identifier(tag_name
) ||
1214 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1218 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1223 variant
->parent
.declaration
= &variant
->declaration
.p
;
1224 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1225 variant
->tag_name
= g_string_new(tag_name
);
1226 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1227 variant
->fields
= g_ptr_array_new_with_free_func(
1228 (GDestroyNotify
)destroy_structure_field
);
1229 bt_ctf_field_type_get(enum_tag
);
1230 variant
->tag
= container_of(enum_tag
,
1231 struct bt_ctf_field_type_enumeration
, parent
);
1232 bt_ctf_field_type_init(&variant
->parent
);
1233 return &variant
->parent
;
1238 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1239 struct bt_ctf_field_type
*type
)
1241 struct bt_ctf_field_type_variant
*variant
;
1242 struct bt_ctf_field_type
*tag_type
= NULL
;
1244 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1248 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1249 tag_type
= &variant
->tag
->parent
;
1250 bt_ctf_field_type_get(tag_type
);
1255 const char *bt_ctf_field_type_variant_get_tag_name(
1256 struct bt_ctf_field_type
*type
)
1258 struct bt_ctf_field_type_variant
*variant
;
1259 const char *tag_name
= NULL
;
1261 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1265 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1266 tag_name
= variant
->tag_name
->str
;
1271 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1272 struct bt_ctf_field_type
*field_type
,
1273 const char *field_name
)
1278 struct bt_ctf_field_type_variant
*variant
;
1279 GQuark field_name_quark
= g_quark_from_string(field_name
);
1281 if (!type
|| !field_type
|| type
->frozen
||
1282 bt_ctf_validate_identifier(field_name
) ||
1283 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1284 bt_ctf_field_type_validate(field_type
)) {
1289 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1290 /* Make sure this name is present in the enum tag */
1291 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1292 struct enumeration_mapping
*mapping
=
1293 g_ptr_array_index(variant
->tag
->entries
, i
);
1295 if (mapping
->string
== field_name_quark
) {
1301 if (!name_found
|| add_structure_field(variant
->fields
,
1302 variant
->field_name_to_index
, field_type
, field_name
)) {
1310 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1311 struct bt_ctf_field_type
*type
,
1312 const char *field_name
)
1316 struct structure_field
*field
;
1317 struct bt_ctf_field_type_variant
*variant
;
1318 struct bt_ctf_field_type
*field_type
= NULL
;
1320 if (!type
|| !field_name
) {
1324 name_quark
= g_quark_try_string(field_name
);
1329 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1330 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1331 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1335 field
= g_ptr_array_index(variant
->fields
, index
);
1336 field_type
= field
->type
;
1337 bt_ctf_field_type_get(field_type
);
1342 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1343 struct bt_ctf_field_type
*type
,
1344 struct bt_ctf_field
*tag
)
1346 const char *enum_value
;
1347 struct bt_ctf_field_type
*field_type
= NULL
;
1349 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1353 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1358 /* Already increments field_type's reference count */
1359 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1365 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1368 struct bt_ctf_field_type_variant
*variant
;
1370 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1375 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1377 ret
= (int) variant
->fields
->len
;
1383 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1384 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1387 struct bt_ctf_field_type_variant
*variant
;
1388 struct structure_field
*field
;
1391 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1392 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1397 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1399 if (index
>= variant
->fields
->len
) {
1404 field
= g_ptr_array_index(variant
->fields
, index
);
1405 *field_type
= field
->type
;
1406 bt_ctf_field_type_get(field
->type
);
1407 *field_name
= g_quark_to_string(field
->name
);
1412 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1413 struct bt_ctf_field_type
*element_type
,
1414 unsigned int length
)
1416 struct bt_ctf_field_type_array
*array
= NULL
;
1418 if (!element_type
|| length
== 0 ||
1419 bt_ctf_field_type_validate(element_type
)) {
1423 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1428 array
->parent
.declaration
= &array
->declaration
.p
;
1429 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1431 bt_ctf_field_type_get(element_type
);
1432 array
->element_type
= element_type
;
1433 array
->length
= length
;
1434 bt_ctf_field_type_init(&array
->parent
);
1435 array
->parent
.declaration
->alignment
=
1436 element_type
->declaration
->alignment
;
1437 return &array
->parent
;
1442 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1443 struct bt_ctf_field_type
*type
)
1445 struct bt_ctf_field_type
*ret
= NULL
;
1446 struct bt_ctf_field_type_array
*array
;
1448 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1452 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1453 ret
= array
->element_type
;
1454 bt_ctf_field_type_get(ret
);
1459 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1462 struct bt_ctf_field_type_array
*array
;
1464 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1469 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1470 ret
= (int64_t) array
->length
;
1475 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1476 struct bt_ctf_field_type
*element_type
,
1477 const char *length_field_name
)
1479 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1481 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1482 bt_ctf_field_type_validate(element_type
)) {
1486 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1491 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1492 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1493 bt_ctf_field_type_get(element_type
);
1494 sequence
->element_type
= element_type
;
1495 sequence
->length_field_name
= g_string_new(length_field_name
);
1496 bt_ctf_field_type_init(&sequence
->parent
);
1497 sequence
->parent
.declaration
->alignment
=
1498 element_type
->declaration
->alignment
;
1499 return &sequence
->parent
;
1504 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1505 struct bt_ctf_field_type
*type
)
1507 struct bt_ctf_field_type
*ret
= NULL
;
1508 struct bt_ctf_field_type_sequence
*sequence
;
1510 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1514 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1516 ret
= sequence
->element_type
;
1517 bt_ctf_field_type_get(ret
);
1522 const char *bt_ctf_field_type_sequence_get_length_field_name(
1523 struct bt_ctf_field_type
*type
)
1525 const char *ret
= NULL
;
1526 struct bt_ctf_field_type_sequence
*sequence
;
1528 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1532 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1534 ret
= sequence
->length_field_name
->str
;
1539 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1541 struct bt_ctf_field_type_string
*string
=
1542 g_new0(struct bt_ctf_field_type_string
, 1);
1548 string
->parent
.declaration
= &string
->declaration
.p
;
1549 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1550 bt_ctf_field_type_init(&string
->parent
);
1551 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1552 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1553 return &string
->parent
;
1556 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1557 struct bt_ctf_field_type
*type
)
1559 struct bt_ctf_field_type_string
*string
;
1560 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1562 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1566 string
= container_of(type
, struct bt_ctf_field_type_string
,
1568 ret
= string
->declaration
.encoding
;
1573 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1574 enum ctf_string_encoding encoding
)
1577 struct bt_ctf_field_type_string
*string
;
1579 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1580 (encoding
!= CTF_STRING_UTF8
&&
1581 encoding
!= CTF_STRING_ASCII
)) {
1586 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1587 string
->declaration
.encoding
= encoding
;
1592 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1601 ret
= (int) type
->declaration
->alignment
;
1606 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1607 unsigned int alignment
)
1611 /* Alignment must be bit-aligned (1) or byte aligned */
1612 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1617 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1618 alignment
!= CHAR_BIT
) {
1623 type
->declaration
->alignment
= alignment
;
1629 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1630 struct bt_ctf_field_type
*type
)
1632 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1633 int internal_byte_order
= -1;
1639 switch (type
->declaration
->id
) {
1640 case CTF_TYPE_INTEGER
:
1642 struct bt_ctf_field_type_integer
*integer
= container_of(
1643 type
, struct bt_ctf_field_type_integer
, parent
);
1644 internal_byte_order
= integer
->declaration
.byte_order
;
1647 case CTF_TYPE_FLOAT
:
1649 struct bt_ctf_field_type_floating_point
*floating_point
=
1651 struct bt_ctf_field_type_floating_point
,
1653 internal_byte_order
= floating_point
->declaration
.byte_order
;
1660 switch (internal_byte_order
) {
1662 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1665 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1668 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1671 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1677 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1678 enum bt_ctf_byte_order byte_order
)
1681 int internal_byte_order
;
1682 enum ctf_type_id type_id
;
1684 if (!type
|| type
->frozen
) {
1689 type_id
= type
->declaration
->id
;
1690 switch (byte_order
) {
1691 case BT_CTF_BYTE_ORDER_NATIVE
:
1692 /* Leave unset. Will be initialized by parent. */
1693 internal_byte_order
= 0;
1695 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1696 internal_byte_order
= LITTLE_ENDIAN
;
1698 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1699 case BT_CTF_BYTE_ORDER_NETWORK
:
1700 internal_byte_order
= BIG_ENDIAN
;
1707 if (set_byte_order_funcs
[type_id
]) {
1708 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1714 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1715 struct bt_ctf_field_type
*type
)
1718 return CTF_TYPE_UNKNOWN
;
1721 return type
->declaration
->id
;
1724 const char *bt_ctf_field_type_get_alias_name(
1725 struct bt_ctf_field_type
*type
)
1727 const char *name
= NULL
;
1729 if (!type
|| !type
->alias_name
) {
1733 name
= type
->alias_name
->str
;
1738 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1744 bt_ctf_ref_get(&type
->ref_count
);
1747 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1753 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1757 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1767 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1768 struct bt_ctf_field_type_variant
*variant
,
1771 struct bt_ctf_field_type
*type
= NULL
;
1772 GQuark field_name_quark
;
1774 struct structure_field
*field_entry
;
1775 struct range_overlap_query query
= {
1776 .range_start
._signed
= tag_value
,
1777 .range_end
._signed
= tag_value
,
1778 .mapping_name
= 0, .overlaps
= 0};
1780 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1782 if (!query
.overlaps
) {
1786 field_name_quark
= query
.mapping_name
;
1787 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1788 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1792 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1793 type
= field_entry
->type
;
1799 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1800 struct bt_ctf_field_type_variant
*variant
,
1803 struct bt_ctf_field_type
*type
= NULL
;
1804 GQuark field_name_quark
;
1806 struct structure_field
*field_entry
;
1807 struct range_overlap_query query
= {
1808 .range_start
._unsigned
= tag_value
,
1809 .range_end
._unsigned
= tag_value
,
1810 .mapping_name
= 0, .overlaps
= 0};
1812 g_ptr_array_foreach(variant
->tag
->entries
,
1813 check_ranges_overlap_unsigned
,
1815 if (!query
.overlaps
) {
1819 field_name_quark
= query
.mapping_name
;
1820 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1821 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1825 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1826 type
= field_entry
->type
;
1832 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1833 struct metadata_context
*context
)
1837 if (!type
|| !context
) {
1842 ret
= type
->serialize(type
, context
);
1848 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1855 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1856 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1857 set_byte_order_funcs
[type
->declaration
->id
](type
,
1863 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1865 struct bt_ctf_field_type_integer
*integer
;
1871 integer
= container_of(
1872 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1873 struct bt_ctf_field_type_integer
, parent
);
1874 if (integer
->mapped_clock
) {
1875 bt_ctf_clock_put(integer
->mapped_clock
);
1881 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1883 struct bt_ctf_field_type_enumeration
*enumeration
;
1889 enumeration
= container_of(
1890 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1891 struct bt_ctf_field_type_enumeration
, parent
);
1892 g_ptr_array_free(enumeration
->entries
, TRUE
);
1893 bt_ctf_field_type_put(enumeration
->container
);
1894 g_free(enumeration
);
1898 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1900 struct bt_ctf_field_type_floating_point
*floating_point
;
1906 floating_point
= container_of(
1907 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1908 struct bt_ctf_field_type_floating_point
, parent
);
1909 g_free(floating_point
);
1913 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1915 struct bt_ctf_field_type_structure
*structure
;
1921 structure
= container_of(
1922 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1923 struct bt_ctf_field_type_structure
, parent
);
1924 g_ptr_array_free(structure
->fields
, TRUE
);
1925 g_hash_table_destroy(structure
->field_name_to_index
);
1930 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1932 struct bt_ctf_field_type_variant
*variant
;
1938 variant
= container_of(
1939 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1940 struct bt_ctf_field_type_variant
, parent
);
1941 g_ptr_array_free(variant
->fields
, TRUE
);
1942 g_hash_table_destroy(variant
->field_name_to_index
);
1943 g_string_free(variant
->tag_name
, TRUE
);
1944 bt_ctf_field_type_put(&variant
->tag
->parent
);
1949 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1951 struct bt_ctf_field_type_array
*array
;
1957 array
= container_of(
1958 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1959 struct bt_ctf_field_type_array
, parent
);
1960 bt_ctf_field_type_put(array
->element_type
);
1965 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1967 struct bt_ctf_field_type_sequence
*sequence
;
1973 sequence
= container_of(
1974 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1975 struct bt_ctf_field_type_sequence
, parent
);
1976 bt_ctf_field_type_put(sequence
->element_type
);
1977 g_string_free(sequence
->length_field_name
, TRUE
);
1982 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1984 struct bt_ctf_field_type_string
*string
;
1990 string
= container_of(
1991 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1992 struct bt_ctf_field_type_string
, parent
);
1997 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2003 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2005 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2006 type
, struct bt_ctf_field_type_enumeration
, parent
);
2008 generic_field_type_freeze(type
);
2009 bt_ctf_field_type_freeze(enumeration_type
->container
);
2013 void freeze_structure_field(struct structure_field
*field
)
2015 bt_ctf_field_type_freeze(field
->type
);
2019 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2021 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2022 type
, struct bt_ctf_field_type_structure
, parent
);
2024 generic_field_type_freeze(type
);
2025 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
2030 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2032 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2033 type
, struct bt_ctf_field_type_variant
, parent
);
2035 generic_field_type_freeze(type
);
2036 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
2041 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2043 struct bt_ctf_field_type_array
*array_type
= container_of(
2044 type
, struct bt_ctf_field_type_array
, parent
);
2046 generic_field_type_freeze(type
);
2047 bt_ctf_field_type_freeze(array_type
->element_type
);
2051 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2053 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2054 type
, struct bt_ctf_field_type_sequence
, parent
);
2056 generic_field_type_freeze(type
);
2057 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2061 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2063 const char *encoding_string
;
2066 case CTF_STRING_NONE
:
2067 encoding_string
= "none";
2069 case CTF_STRING_ASCII
:
2070 encoding_string
= "ASCII";
2072 case CTF_STRING_UTF8
:
2073 encoding_string
= "UTF8";
2076 encoding_string
= "unknown";
2080 return encoding_string
;
2084 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2086 const char *base_string
;
2089 case BT_CTF_INTEGER_BASE_DECIMAL
:
2090 base_string
= "decimal";
2092 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2093 base_string
= "hexadecimal";
2095 case BT_CTF_INTEGER_BASE_OCTAL
:
2096 base_string
= "octal";
2098 case BT_CTF_INTEGER_BASE_BINARY
:
2099 base_string
= "binary";
2102 base_string
= "unknown";
2110 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2111 struct metadata_context
*context
)
2113 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2114 struct bt_ctf_field_type_integer
, parent
);
2117 g_string_append_printf(context
->string
,
2118 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2119 integer
->declaration
.len
, type
->declaration
->alignment
,
2120 (integer
->declaration
.signedness
? "true" : "false"),
2121 get_encoding_string(integer
->declaration
.encoding
),
2122 get_integer_base_string(integer
->declaration
.base
),
2123 get_byte_order_string(integer
->declaration
.byte_order
));
2124 if (integer
->mapped_clock
) {
2125 const char *clock_name
= bt_ctf_clock_get_name(
2126 integer
->mapped_clock
);
2133 g_string_append_printf(context
->string
,
2134 "; map = clock.%s.value", clock_name
);
2137 g_string_append(context
->string
, "; }");
2143 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2144 struct metadata_context
*context
)
2148 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2149 struct bt_ctf_field_type_enumeration
, parent
);
2150 struct bt_ctf_field_type
*container_type
;
2151 int container_signed
;
2153 ret
= bt_ctf_field_type_validate(type
);
2158 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2159 if (!container_type
) {
2164 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2165 if (container_signed
< 0) {
2166 ret
= container_signed
;
2167 goto error_put_container_type
;
2170 g_string_append(context
->string
, "enum : ");
2171 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2173 goto error_put_container_type
;
2176 g_string_append(context
->string
, " { ");
2177 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2178 struct enumeration_mapping
*mapping
=
2179 enumeration
->entries
->pdata
[entry
];
2181 if (container_signed
) {
2182 if (mapping
->range_start
._signed
==
2183 mapping
->range_end
._signed
) {
2184 g_string_append_printf(context
->string
,
2185 "\"%s\" = %" PRId64
,
2186 g_quark_to_string(mapping
->string
),
2187 mapping
->range_start
._signed
);
2189 g_string_append_printf(context
->string
,
2190 "\"%s\" = %" PRId64
" ... %" PRId64
,
2191 g_quark_to_string(mapping
->string
),
2192 mapping
->range_start
._signed
,
2193 mapping
->range_end
._signed
);
2196 if (mapping
->range_start
._unsigned
==
2197 mapping
->range_end
._unsigned
) {
2198 g_string_append_printf(context
->string
,
2199 "\"%s\" = %" PRIu64
,
2200 g_quark_to_string(mapping
->string
),
2201 mapping
->range_start
._unsigned
);
2203 g_string_append_printf(context
->string
,
2204 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2205 g_quark_to_string(mapping
->string
),
2206 mapping
->range_start
._unsigned
,
2207 mapping
->range_end
._unsigned
);
2211 g_string_append(context
->string
,
2212 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2216 if (context
->field_name
->len
) {
2217 g_string_append_printf(context
->string
, " %s",
2218 context
->field_name
->str
);
2219 g_string_assign(context
->field_name
, "");
2221 error_put_container_type
:
2222 bt_ctf_field_type_put(container_type
);
2228 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2229 struct metadata_context
*context
)
2231 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2232 type
, struct bt_ctf_field_type_floating_point
, parent
);
2234 g_string_append_printf(context
->string
,
2235 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2236 floating_point
->declaration
.exp
->len
,
2237 floating_point
->declaration
.mantissa
->len
+ 1,
2238 get_byte_order_string(floating_point
->declaration
.byte_order
),
2239 type
->declaration
->alignment
);
2244 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2245 struct metadata_context
*context
)
2248 unsigned int indent
;
2250 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2251 struct bt_ctf_field_type_structure
, parent
);
2252 GString
*structure_field_name
= context
->field_name
;
2254 context
->field_name
= g_string_new("");
2256 context
->current_indentation_level
++;
2257 g_string_append(context
->string
, "struct {\n");
2259 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2260 struct structure_field
*field
;
2262 for (indent
= 0; indent
< context
->current_indentation_level
;
2264 g_string_append_c(context
->string
, '\t');
2267 field
= structure
->fields
->pdata
[i
];
2268 g_string_assign(context
->field_name
,
2269 g_quark_to_string(field
->name
));
2270 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2275 if (context
->field_name
->len
) {
2276 g_string_append_printf(context
->string
, " %s",
2277 context
->field_name
->str
);
2279 g_string_append(context
->string
, ";\n");
2282 context
->current_indentation_level
--;
2283 for (indent
= 0; indent
< context
->current_indentation_level
;
2285 g_string_append_c(context
->string
, '\t');
2288 g_string_append_printf(context
->string
, "} align(%zu)",
2289 type
->declaration
->alignment
);
2291 g_string_free(context
->field_name
, TRUE
);
2292 context
->field_name
= structure_field_name
;
2297 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2298 struct metadata_context
*context
)
2301 unsigned int indent
;
2303 struct bt_ctf_field_type_variant
*variant
= container_of(
2304 type
, struct bt_ctf_field_type_variant
, parent
);
2305 GString
*variant_field_name
= context
->field_name
;
2307 context
->field_name
= g_string_new("");
2308 g_string_append_printf(context
->string
,
2309 "variant <%s> {\n", variant
->tag_name
->str
);
2310 context
->current_indentation_level
++;
2311 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2312 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2314 g_string_assign(context
->field_name
,
2315 g_quark_to_string(field
->name
));
2316 for (indent
= 0; indent
< context
->current_indentation_level
;
2318 g_string_append_c(context
->string
, '\t');
2321 g_string_assign(context
->field_name
,
2322 g_quark_to_string(field
->name
));
2323 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2328 if (context
->field_name
->len
) {
2329 g_string_append_printf(context
->string
, " %s;",
2330 context
->field_name
->str
);
2333 g_string_append_c(context
->string
, '\n');
2336 context
->current_indentation_level
--;
2337 for (indent
= 0; indent
< context
->current_indentation_level
;
2339 g_string_append_c(context
->string
, '\t');
2342 g_string_append(context
->string
, "}");
2344 g_string_free(context
->field_name
, TRUE
);
2345 context
->field_name
= variant_field_name
;
2350 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2351 struct metadata_context
*context
)
2354 struct bt_ctf_field_type_array
*array
= container_of(type
,
2355 struct bt_ctf_field_type_array
, parent
);
2357 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2362 if (context
->field_name
->len
) {
2363 g_string_append_printf(context
->string
, " %s[%u]",
2364 context
->field_name
->str
, array
->length
);
2365 g_string_assign(context
->field_name
, "");
2367 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2374 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2375 struct metadata_context
*context
)
2378 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2379 type
, struct bt_ctf_field_type_sequence
, parent
);
2381 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2386 if (context
->field_name
->len
) {
2387 g_string_append_printf(context
->string
, " %s[%s]",
2388 context
->field_name
->str
,
2389 sequence
->length_field_name
->str
);
2390 g_string_assign(context
->field_name
, "");
2392 g_string_append_printf(context
->string
, "[%s]",
2393 sequence
->length_field_name
->str
);
2400 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2401 struct metadata_context
*context
)
2403 struct bt_ctf_field_type_string
*string
= container_of(
2404 type
, struct bt_ctf_field_type_string
, parent
);
2406 g_string_append_printf(context
->string
,
2407 "string { encoding = %s; }",
2408 get_encoding_string(string
->declaration
.encoding
));
2413 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2414 int byte_order
, int set_native
)
2416 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2417 struct bt_ctf_field_type_integer
, parent
);
2420 integer_type
->declaration
.byte_order
=
2421 integer_type
->declaration
.byte_order
== 0 ?
2422 byte_order
: integer_type
->declaration
.byte_order
;
2424 integer_type
->declaration
.byte_order
= byte_order
;
2429 void bt_ctf_field_type_enumeration_set_byte_order(
2430 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2432 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2433 struct bt_ctf_field_type_enumeration
, parent
);
2435 /* Safe to assume that container is an integer */
2436 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2437 byte_order
, set_native
);
2441 void bt_ctf_field_type_floating_point_set_byte_order(
2442 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2444 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2445 container_of(type
, struct bt_ctf_field_type_floating_point
,
2449 floating_point_type
->declaration
.byte_order
=
2450 floating_point_type
->declaration
.byte_order
== 0 ?
2452 floating_point_type
->declaration
.byte_order
;
2453 floating_point_type
->sign
.byte_order
=
2454 floating_point_type
->sign
.byte_order
== 0 ?
2455 byte_order
: floating_point_type
->sign
.byte_order
;
2456 floating_point_type
->mantissa
.byte_order
=
2457 floating_point_type
->mantissa
.byte_order
== 0 ?
2458 byte_order
: floating_point_type
->mantissa
.byte_order
;
2459 floating_point_type
->exp
.byte_order
=
2460 floating_point_type
->exp
.byte_order
== 0 ?
2461 byte_order
: floating_point_type
->exp
.byte_order
;
2463 floating_point_type
->declaration
.byte_order
= byte_order
;
2464 floating_point_type
->sign
.byte_order
= byte_order
;
2465 floating_point_type
->mantissa
.byte_order
= byte_order
;
2466 floating_point_type
->exp
.byte_order
= byte_order
;
2471 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2472 int byte_order
, int set_native
)
2475 struct bt_ctf_field_type_structure
*structure_type
=
2476 container_of(type
, struct bt_ctf_field_type_structure
,
2479 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2480 struct structure_field
*field
= g_ptr_array_index(
2481 structure_type
->fields
, i
);
2482 struct bt_ctf_field_type
*field_type
= field
->type
;
2484 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2485 set_byte_order_funcs
[field_type
->declaration
->id
](
2486 field_type
, byte_order
, set_native
);
2492 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2493 int byte_order
, int set_native
)
2496 struct bt_ctf_field_type_variant
*variant_type
=
2497 container_of(type
, struct bt_ctf_field_type_variant
,
2500 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2501 struct structure_field
*field
= g_ptr_array_index(
2502 variant_type
->fields
, i
);
2503 struct bt_ctf_field_type
*field_type
= field
->type
;
2505 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2506 set_byte_order_funcs
[field_type
->declaration
->id
](
2507 field_type
, byte_order
, set_native
);
2513 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2514 int byte_order
, int set_native
)
2516 struct bt_ctf_field_type_array
*array_type
=
2517 container_of(type
, struct bt_ctf_field_type_array
,
2520 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2521 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2522 array_type
->element_type
, byte_order
, set_native
);
2527 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2528 int byte_order
, int set_native
)
2530 struct bt_ctf_field_type_sequence
*sequence_type
=
2531 container_of(type
, struct bt_ctf_field_type_sequence
,
2534 if (set_byte_order_funcs
[
2535 sequence_type
->element_type
->declaration
->id
]) {
2536 set_byte_order_funcs
[
2537 sequence_type
->element_type
->declaration
->id
](
2538 sequence_type
->element_type
, byte_order
, set_native
);