4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-writer/writer-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <babeltrace/endian.h>
38 struct range_overlap_query
{
53 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
55 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
70 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
71 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
73 bt_ctf_field_type_enumeration_destroy
,
75 bt_ctf_field_type_floating_point_destroy
,
76 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
77 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
78 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
79 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
80 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
84 void generic_field_type_freeze(struct bt_ctf_field_type
*);
86 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
88 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
97 type_freeze_func
const type_freeze_funcs
[] = {
98 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
99 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
100 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
101 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
102 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
103 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
104 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
105 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
109 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
110 struct metadata_context
*);
112 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
113 struct metadata_context
*);
115 int bt_ctf_field_type_floating_point_serialize(
116 struct bt_ctf_field_type
*, struct metadata_context
*);
118 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
119 struct metadata_context
*);
121 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
122 struct metadata_context
*);
124 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
125 struct metadata_context
*);
127 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
128 struct metadata_context
*);
130 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
131 struct metadata_context
*);
134 type_serialize_func
const type_serialize_funcs
[] = {
135 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
137 bt_ctf_field_type_enumeration_serialize
,
139 bt_ctf_field_type_floating_point_serialize
,
141 bt_ctf_field_type_structure_serialize
,
142 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
143 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
144 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
145 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
149 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
152 void bt_ctf_field_type_floating_point_set_byte_order(
153 struct bt_ctf_field_type
*, int byte_order
);
156 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
159 bt_ctf_field_type_integer_set_byte_order
,
161 bt_ctf_field_type_floating_point_set_byte_order
,
162 [CTF_TYPE_ENUM
... CTF_TYPE_SEQUENCE
] = NULL
,
166 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
172 void destroy_structure_field(struct structure_field
*field
)
175 bt_ctf_field_type_put(field
->type
);
182 void check_ranges_overlap(gpointer element
, gpointer query
)
184 struct enumeration_mapping
*mapping
= element
;
185 struct range_overlap_query
*overlap_query
= query
;
187 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
188 && overlap_query
->range_start
._signed
<=
189 mapping
->range_end
._signed
) {
190 overlap_query
->overlaps
= 1;
191 overlap_query
->mapping_name
= mapping
->string
;
194 overlap_query
->overlaps
|=
195 mapping
->string
== overlap_query
->mapping_name
;
199 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
201 struct enumeration_mapping
*mapping
= element
;
202 struct range_overlap_query
*overlap_query
= query
;
204 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
205 && overlap_query
->range_start
._unsigned
<=
206 mapping
->range_end
._unsigned
) {
207 overlap_query
->overlaps
= 1;
208 overlap_query
->mapping_name
= mapping
->string
;
211 overlap_query
->overlaps
|=
212 mapping
->string
== overlap_query
->mapping_name
;
216 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
217 struct enumeration_mapping
**b
)
219 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
223 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
224 struct enumeration_mapping
**b
)
226 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
230 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
232 enum ctf_type_id type_id
= type
->declaration
->id
;
235 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
236 (type_id
< NR_CTF_TYPES
));
238 bt_ctf_ref_init(&type
->ref_count
);
239 type
->freeze
= type_freeze_funcs
[type_id
];
240 type
->serialize
= type_serialize_funcs
[type_id
];
241 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
243 type
->declaration
->alignment
= 1;
247 int add_structure_field(GPtrArray
*fields
,
248 GHashTable
*field_name_to_index
,
249 struct bt_ctf_field_type
*field_type
,
250 const char *field_name
)
253 GQuark name_quark
= g_quark_from_string(field_name
);
254 struct structure_field
*field
;
256 /* Make sure structure does not contain a field of the same name */
257 if (g_hash_table_lookup_extended(field_name_to_index
,
258 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
263 field
= g_new0(struct structure_field
, 1);
269 bt_ctf_field_type_get(field_type
);
270 field
->name
= name_quark
;
271 field
->type
= field_type
;
272 g_hash_table_insert(field_name_to_index
,
273 (gpointer
) (unsigned long) name_quark
,
274 (gpointer
) (unsigned long) fields
->len
);
275 g_ptr_array_add(fields
, field
);
276 bt_ctf_field_type_freeze(field_type
);
282 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
291 if (type
->declaration
->id
== CTF_TYPE_ENUM
) {
292 struct bt_ctf_field_type_enumeration
*enumeration
=
293 container_of(type
, struct bt_ctf_field_type_enumeration
,
296 ret
= enumeration
->entries
->len
? 0 : -1;
302 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
304 struct bt_ctf_field_type_integer
*integer
=
305 g_new0(struct bt_ctf_field_type_integer
, 1);
307 if (!integer
|| size
> 64) {
311 integer
->parent
.declaration
= &integer
->declaration
.p
;
312 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
313 integer
->declaration
.len
= size
;
314 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
315 integer
->declaration
.encoding
= CTF_STRING_NONE
;
316 bt_ctf_field_type_init(&integer
->parent
);
317 return &integer
->parent
;
320 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
323 struct bt_ctf_field_type_integer
*integer
;
325 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
330 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
331 ret
= (int) integer
->declaration
.len
;
336 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
339 struct bt_ctf_field_type_integer
*integer
;
341 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
346 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
347 ret
= integer
->declaration
.signedness
;
352 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
356 struct bt_ctf_field_type_integer
*integer
;
358 if (!type
|| type
->frozen
||
359 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
364 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
365 if (is_signed
&& integer
->declaration
.len
<= 1) {
370 integer
->declaration
.signedness
= !!is_signed
;
375 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
376 struct bt_ctf_field_type
*type
)
378 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
379 struct bt_ctf_field_type_integer
*integer
;
381 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
385 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
386 ret
= integer
->declaration
.base
;
391 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
392 enum bt_ctf_integer_base base
)
396 if (!type
|| type
->frozen
||
397 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
403 case BT_CTF_INTEGER_BASE_BINARY
:
404 case BT_CTF_INTEGER_BASE_OCTAL
:
405 case BT_CTF_INTEGER_BASE_DECIMAL
:
406 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
408 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
409 struct bt_ctf_field_type_integer
, parent
);
410 integer
->declaration
.base
= base
;
420 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
421 struct bt_ctf_field_type
*type
)
423 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
424 struct bt_ctf_field_type_integer
*integer
;
426 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
430 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
431 ret
= integer
->declaration
.encoding
;
436 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
437 enum ctf_string_encoding encoding
)
440 struct bt_ctf_field_type_integer
*integer
;
442 if (!type
|| type
->frozen
||
443 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
444 (encoding
< CTF_STRING_NONE
) ||
445 (encoding
>= CTF_STRING_UNKNOWN
)) {
450 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
451 integer
->declaration
.encoding
= encoding
;
456 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
457 struct bt_ctf_field_type
*integer_container_type
)
459 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
461 if (!integer_container_type
) {
465 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
469 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
474 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
475 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
476 bt_ctf_field_type_get(integer_container_type
);
477 enumeration
->container
= integer_container_type
;
478 enumeration
->entries
= g_ptr_array_new_with_free_func(
479 (GDestroyNotify
)destroy_enumeration_mapping
);
480 bt_ctf_field_type_init(&enumeration
->parent
);
481 return &enumeration
->parent
;
487 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
488 struct bt_ctf_field_type
*type
)
490 struct bt_ctf_field_type
*container_type
= NULL
;
491 struct bt_ctf_field_type_enumeration
*enumeration_type
;
497 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
501 enumeration_type
= container_of(type
,
502 struct bt_ctf_field_type_enumeration
, parent
);
503 container_type
= enumeration_type
->container
;
504 bt_ctf_field_type_get(container_type
);
506 return container_type
;
509 int bt_ctf_field_type_enumeration_add_mapping(
510 struct bt_ctf_field_type
*type
, const char *string
,
511 int64_t range_start
, int64_t range_end
)
515 struct enumeration_mapping
*mapping
;
516 struct bt_ctf_field_type_enumeration
*enumeration
;
517 struct range_overlap_query query
;
518 char *escaped_string
;
520 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
522 (range_end
< range_start
)) {
527 if (!string
|| strlen(string
) == 0) {
532 escaped_string
= g_strescape(string
, NULL
);
533 if (!escaped_string
) {
538 mapping_name
= g_quark_from_string(escaped_string
);
539 query
= (struct range_overlap_query
) {
540 .range_start
._signed
= range_start
,
541 .range_end
._signed
= range_end
,
542 .mapping_name
= mapping_name
,
544 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
547 /* Check that the range does not overlap with one already present */
548 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
549 if (query
.overlaps
) {
554 mapping
= g_new(struct enumeration_mapping
, 1);
560 *mapping
= (struct enumeration_mapping
) {
561 .range_start
._signed
= range_start
,
562 .range_end
._signed
= range_end
, .string
= mapping_name
};
563 g_ptr_array_add(enumeration
->entries
, mapping
);
564 g_ptr_array_sort(enumeration
->entries
,
565 (GCompareFunc
)compare_enumeration_mappings_signed
);
567 free(escaped_string
);
572 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
573 struct bt_ctf_field_type
*type
, const char *string
,
574 uint64_t range_start
, uint64_t range_end
)
578 struct enumeration_mapping
*mapping
;
579 struct bt_ctf_field_type_enumeration
*enumeration
;
580 struct range_overlap_query query
;
581 char *escaped_string
;
583 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
585 (range_end
< range_start
)) {
590 if (!string
|| strlen(string
) == 0) {
595 escaped_string
= g_strescape(string
, NULL
);
596 if (!escaped_string
) {
601 mapping_name
= g_quark_from_string(escaped_string
);
602 query
= (struct range_overlap_query
) {
603 .range_start
._unsigned
= range_start
,
604 .range_end
._unsigned
= range_end
,
605 .mapping_name
= mapping_name
,
607 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
610 /* Check that the range does not overlap with one already present */
611 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
613 if (query
.overlaps
) {
618 mapping
= g_new(struct enumeration_mapping
, 1);
624 *mapping
= (struct enumeration_mapping
) {
625 .range_start
._unsigned
= range_start
,
626 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
627 g_ptr_array_add(enumeration
->entries
, mapping
);
628 g_ptr_array_sort(enumeration
->entries
,
629 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
631 free(escaped_string
);
636 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
637 struct bt_ctf_field_type_enumeration
*enumeration_type
,
640 const char *name
= NULL
;
641 struct range_overlap_query query
=
642 (struct range_overlap_query
) {
643 .range_start
._unsigned
= value
,
644 .range_end
._unsigned
= value
,
647 g_ptr_array_foreach(enumeration_type
->entries
,
648 check_ranges_overlap_unsigned
,
650 if (!query
.overlaps
) {
654 name
= g_quark_to_string(query
.mapping_name
);
659 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
660 struct bt_ctf_field_type_enumeration
*enumeration_type
,
663 const char *name
= NULL
;
664 struct range_overlap_query query
=
665 (struct range_overlap_query
) {
666 .range_start
._signed
= value
,
667 .range_end
._signed
= value
,
670 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
672 if (!query
.overlaps
) {
676 name
= g_quark_to_string(query
.mapping_name
);
681 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
682 struct bt_ctf_field_type
*type
)
685 struct bt_ctf_field_type_enumeration
*enumeration
;
687 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
692 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
694 ret
= enumeration
->entries
->len
;
700 struct enumeration_mapping
*get_enumeration_mapping(
701 struct bt_ctf_field_type
*type
, size_t index
)
703 struct enumeration_mapping
*mapping
= NULL
;
704 struct bt_ctf_field_type_enumeration
*enumeration
;
706 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
708 if (index
>= enumeration
->entries
->len
) {
712 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
717 int bt_ctf_field_type_enumeration_get_mapping(
718 struct bt_ctf_field_type
*type
, size_t index
,
719 const char **string
, int64_t *range_start
, int64_t *range_end
)
721 struct enumeration_mapping
*mapping
;
724 if (!type
|| !string
|| !range_start
|| !range_end
||
725 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
730 mapping
= get_enumeration_mapping(type
, index
);
736 *string
= g_quark_to_string(mapping
->string
);
737 *range_start
= mapping
->range_start
._signed
;
738 *range_end
= mapping
->range_end
._signed
;
743 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
744 struct bt_ctf_field_type
*type
, size_t index
,
745 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
747 struct enumeration_mapping
*mapping
;
750 if (!type
|| !string
|| !range_start
|| !range_end
||
751 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
756 mapping
= get_enumeration_mapping(type
, index
);
762 *string
= g_quark_to_string(mapping
->string
);
763 *range_start
= mapping
->range_start
._unsigned
;
764 *range_end
= mapping
->range_end
._unsigned
;
769 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
770 struct bt_ctf_field_type
*type
, const char *name
,
775 struct bt_ctf_field_type_enumeration
*enumeration
;
778 if (!type
|| !name
|| !index
||
779 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
784 name_quark
= g_quark_try_string(name
);
790 enumeration
= container_of(type
,
791 struct bt_ctf_field_type_enumeration
, parent
);
792 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
793 struct enumeration_mapping
*mapping
=
794 get_enumeration_mapping(type
, i
);
796 if (mapping
->string
== name_quark
) {
807 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
808 struct bt_ctf_field_type
*type
, int64_t value
,
811 struct bt_ctf_field_type_enumeration
*enumeration
;
815 if (!type
|| !index
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
820 enumeration
= container_of(type
,
821 struct bt_ctf_field_type_enumeration
, parent
);
822 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
823 struct enumeration_mapping
*mapping
=
824 get_enumeration_mapping(type
, i
);
826 if (value
>= mapping
->range_start
._signed
&&
827 value
<= mapping
->range_end
._signed
) {
838 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
839 struct bt_ctf_field_type
*type
, uint64_t value
,
842 struct bt_ctf_field_type_enumeration
*enumeration
;
846 if (!type
|| !index
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
851 enumeration
= container_of(type
,
852 struct bt_ctf_field_type_enumeration
, parent
);
853 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
854 struct enumeration_mapping
*mapping
=
855 get_enumeration_mapping(type
, i
);
857 if (value
>= mapping
->range_start
._unsigned
&&
858 value
<= mapping
->range_end
._unsigned
) {
869 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
871 struct bt_ctf_field_type_floating_point
*floating_point
=
872 g_new0(struct bt_ctf_field_type_floating_point
, 1);
874 if (!floating_point
) {
878 floating_point
->declaration
.sign
= &floating_point
->sign
;
879 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
880 floating_point
->declaration
.exp
= &floating_point
->exp
;
881 floating_point
->sign
.len
= 1;
882 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
883 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
884 floating_point
->declaration
.exp
->len
=
885 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
886 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
887 floating_point
->sign
.p
.alignment
= 1;
888 floating_point
->mantissa
.p
.alignment
= 1;
889 floating_point
->exp
.p
.alignment
= 1;
891 bt_ctf_field_type_init(&floating_point
->parent
);
893 return floating_point
? &floating_point
->parent
: NULL
;
896 int bt_ctf_field_type_floating_point_get_exponent_digits(
897 struct bt_ctf_field_type
*type
)
900 struct bt_ctf_field_type_floating_point
*floating_point
;
902 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
907 floating_point
= container_of(type
,
908 struct bt_ctf_field_type_floating_point
, parent
);
909 ret
= (int) floating_point
->declaration
.exp
->len
;
914 int bt_ctf_field_type_floating_point_set_exponent_digits(
915 struct bt_ctf_field_type
*type
,
916 unsigned int exponent_digits
)
919 struct bt_ctf_field_type_floating_point
*floating_point
;
921 if (!type
|| type
->frozen
||
922 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
927 floating_point
= container_of(type
,
928 struct bt_ctf_field_type_floating_point
, parent
);
929 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
930 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
932 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
937 floating_point
->declaration
.exp
->len
= exponent_digits
;
942 int bt_ctf_field_type_floating_point_get_mantissa_digits(
943 struct bt_ctf_field_type
*type
)
946 struct bt_ctf_field_type_floating_point
*floating_point
;
948 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
953 floating_point
= container_of(type
,
954 struct bt_ctf_field_type_floating_point
, parent
);
955 ret
= (int) floating_point
->mantissa
.len
+ 1;
960 int bt_ctf_field_type_floating_point_set_mantissa_digits(
961 struct bt_ctf_field_type
*type
,
962 unsigned int mantissa_digits
)
965 struct bt_ctf_field_type_floating_point
*floating_point
;
967 if (!type
|| type
->frozen
||
968 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
973 floating_point
= container_of(type
,
974 struct bt_ctf_field_type_floating_point
, parent
);
976 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
977 (mantissa_digits
!= DBL_MANT_DIG
) &&
978 (mantissa_digits
!= LDBL_MANT_DIG
)) {
983 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
988 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
990 struct bt_ctf_field_type_structure
*structure
=
991 g_new0(struct bt_ctf_field_type_structure
, 1);
997 structure
->parent
.declaration
= &structure
->declaration
.p
;
998 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
999 bt_ctf_field_type_init(&structure
->parent
);
1000 structure
->fields
= g_ptr_array_new_with_free_func(
1001 (GDestroyNotify
)destroy_structure_field
);
1002 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1003 return &structure
->parent
;
1008 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1009 struct bt_ctf_field_type
*field_type
,
1010 const char *field_name
)
1013 struct bt_ctf_field_type_structure
*structure
;
1015 if (!type
|| !field_type
|| type
->frozen
||
1016 validate_identifier(field_name
) ||
1017 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1018 bt_ctf_field_type_validate(field_type
)) {
1023 structure
= container_of(type
,
1024 struct bt_ctf_field_type_structure
, parent
);
1025 if (add_structure_field(structure
->fields
,
1026 structure
->field_name_to_index
, field_type
, field_name
)) {
1031 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1032 type
->declaration
->alignment
=
1033 field_type
->declaration
->alignment
;
1039 int64_t bt_ctf_field_type_structure_get_field_count(
1040 struct bt_ctf_field_type
*type
)
1043 struct bt_ctf_field_type_structure
*structure
;
1045 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1050 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1052 ret
= structure
->fields
->len
;
1057 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1058 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1061 struct bt_ctf_field_type_structure
*structure
;
1062 struct structure_field
*field
;
1065 if (!type
|| !field_name
|| !field_type
||
1066 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1071 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1073 if (index
>= structure
->fields
->len
) {
1078 field
= g_ptr_array_index(structure
->fields
, index
);
1079 *field_type
= field
->type
;
1080 bt_ctf_field_type_get(field
->type
);
1081 *field_name
= g_quark_to_string(field
->name
);
1086 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1087 struct bt_ctf_field_type
*type
,
1092 struct structure_field
*field
;
1093 struct bt_ctf_field_type_structure
*structure
;
1094 struct bt_ctf_field_type
*field_type
= NULL
;
1096 if (!type
|| !name
) {
1100 name_quark
= g_quark_try_string(name
);
1105 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1107 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1108 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1112 field
= structure
->fields
->pdata
[index
];
1113 field_type
= field
->type
;
1114 bt_ctf_field_type_get(field_type
);
1119 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1120 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1122 struct bt_ctf_field_type_variant
*variant
= NULL
;
1124 if (!enum_tag
|| validate_identifier(tag_name
) ||
1125 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1129 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1134 variant
->parent
.declaration
= &variant
->declaration
.p
;
1135 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1136 variant
->tag_name
= g_string_new(tag_name
);
1137 bt_ctf_field_type_init(&variant
->parent
);
1138 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1139 variant
->fields
= g_ptr_array_new_with_free_func(
1140 (GDestroyNotify
)destroy_structure_field
);
1141 bt_ctf_field_type_get(enum_tag
);
1142 variant
->tag
= container_of(enum_tag
,
1143 struct bt_ctf_field_type_enumeration
, parent
);
1144 return &variant
->parent
;
1149 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1150 struct bt_ctf_field_type
*type
)
1152 struct bt_ctf_field_type_variant
*variant
;
1153 struct bt_ctf_field_type
*tag_type
= NULL
;
1155 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1159 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1160 tag_type
= &variant
->tag
->parent
;
1161 bt_ctf_field_type_get(tag_type
);
1166 const char *bt_ctf_field_type_variant_get_tag_name(
1167 struct bt_ctf_field_type
*type
)
1169 struct bt_ctf_field_type_variant
*variant
;
1170 const char *tag_name
= NULL
;
1172 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1176 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1177 tag_name
= variant
->tag_name
->str
;
1182 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1183 struct bt_ctf_field_type
*field_type
,
1184 const char *field_name
)
1189 struct bt_ctf_field_type_variant
*variant
;
1190 GQuark field_name_quark
= g_quark_from_string(field_name
);
1192 if (!type
|| !field_type
|| type
->frozen
||
1193 validate_identifier(field_name
) ||
1194 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1195 bt_ctf_field_type_validate(field_type
)) {
1200 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1201 /* Make sure this name is present in the enum tag */
1202 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1203 struct enumeration_mapping
*mapping
=
1204 g_ptr_array_index(variant
->tag
->entries
, i
);
1206 if (mapping
->string
== field_name_quark
) {
1212 if (!name_found
|| add_structure_field(variant
->fields
,
1213 variant
->field_name_to_index
, field_type
, field_name
)) {
1221 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1222 struct bt_ctf_field_type
*type
,
1223 const char *field_name
)
1227 struct structure_field
*field
;
1228 struct bt_ctf_field_type_variant
*variant
;
1229 struct bt_ctf_field_type
*field_type
= NULL
;
1231 if (!type
|| !field_name
) {
1235 name_quark
= g_quark_try_string(field_name
);
1240 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1241 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1242 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1246 field
= g_ptr_array_index(variant
->fields
, index
);
1247 field_type
= field
->type
;
1248 bt_ctf_field_type_get(field_type
);
1253 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1254 struct bt_ctf_field_type
*type
,
1255 struct bt_ctf_field
*tag
)
1257 const char *enum_value
;
1258 struct bt_ctf_field_type
*field_type
= NULL
;
1260 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1264 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1269 /* Already increments field_type's reference count */
1270 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1276 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1279 struct bt_ctf_field_type_variant
*variant
;
1281 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1286 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1288 ret
= variant
->fields
->len
;
1294 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1295 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1298 struct bt_ctf_field_type_variant
*variant
;
1299 struct structure_field
*field
;
1302 if (!type
|| !field_name
|| !field_type
||
1303 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1308 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1310 if (index
>= variant
->fields
->len
) {
1315 field
= g_ptr_array_index(variant
->fields
, index
);
1316 *field_type
= field
->type
;
1317 bt_ctf_field_type_get(field
->type
);
1318 *field_name
= g_quark_to_string(field
->name
);
1323 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1324 struct bt_ctf_field_type
*element_type
,
1325 unsigned int length
)
1327 struct bt_ctf_field_type_array
*array
= NULL
;
1329 if (!element_type
|| length
== 0 ||
1330 bt_ctf_field_type_validate(element_type
)) {
1334 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1339 array
->parent
.declaration
= &array
->declaration
.p
;
1340 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1341 bt_ctf_field_type_init(&array
->parent
);
1342 bt_ctf_field_type_get(element_type
);
1343 array
->element_type
= element_type
;
1344 array
->length
= length
;
1345 array
->parent
.declaration
->alignment
=
1346 element_type
->declaration
->alignment
;
1347 return &array
->parent
;
1352 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1353 struct bt_ctf_field_type
*type
)
1355 struct bt_ctf_field_type
*ret
= NULL
;
1356 struct bt_ctf_field_type_array
*array
;
1358 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1362 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1363 ret
= array
->element_type
;
1364 bt_ctf_field_type_get(ret
);
1369 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1372 struct bt_ctf_field_type_array
*array
;
1374 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1379 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1380 ret
= (int64_t) array
->length
;
1385 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1386 struct bt_ctf_field_type
*element_type
,
1387 const char *length_field_name
)
1389 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1391 if (!element_type
|| validate_identifier(length_field_name
) ||
1392 bt_ctf_field_type_validate(element_type
)) {
1396 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1401 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1402 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1403 bt_ctf_field_type_init(&sequence
->parent
);
1404 bt_ctf_field_type_get(element_type
);
1405 sequence
->element_type
= element_type
;
1406 sequence
->length_field_name
= g_string_new(length_field_name
);
1407 sequence
->parent
.declaration
->alignment
=
1408 element_type
->declaration
->alignment
;
1409 return &sequence
->parent
;
1414 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1415 struct bt_ctf_field_type
*type
)
1417 struct bt_ctf_field_type
*ret
= NULL
;
1418 struct bt_ctf_field_type_sequence
*sequence
;
1420 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1424 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1426 ret
= sequence
->element_type
;
1427 bt_ctf_field_type_get(ret
);
1432 const char *bt_ctf_field_type_sequence_get_length_field_name(
1433 struct bt_ctf_field_type
*type
)
1435 const char *ret
= NULL
;
1436 struct bt_ctf_field_type_sequence
*sequence
;
1438 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1442 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1444 ret
= sequence
->length_field_name
->str
;
1449 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1451 struct bt_ctf_field_type_string
*string
=
1452 g_new0(struct bt_ctf_field_type_string
, 1);
1458 string
->parent
.declaration
= &string
->declaration
.p
;
1459 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1460 bt_ctf_field_type_init(&string
->parent
);
1461 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1462 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1463 return &string
->parent
;
1466 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1467 struct bt_ctf_field_type
*type
)
1469 struct bt_ctf_field_type_string
*string
;
1470 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1472 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1476 string
= container_of(type
, struct bt_ctf_field_type_string
,
1478 ret
= string
->declaration
.encoding
;
1483 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1484 enum ctf_string_encoding encoding
)
1487 struct bt_ctf_field_type_string
*string
;
1489 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1490 (encoding
!= CTF_STRING_UTF8
&&
1491 encoding
!= CTF_STRING_ASCII
)) {
1496 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1497 string
->declaration
.encoding
= encoding
;
1502 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1511 ret
= (int) type
->declaration
->alignment
;
1516 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1517 unsigned int alignment
)
1521 /* Alignment must be bit-aligned (1) or byte aligned */
1522 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1527 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1528 alignment
!= CHAR_BIT
) {
1533 type
->declaration
->alignment
= alignment
;
1539 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1540 struct bt_ctf_field_type
*type
)
1542 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1548 switch (type
->declaration
->id
) {
1549 case CTF_TYPE_INTEGER
:
1551 struct bt_ctf_field_type_integer
*integer
= container_of(
1552 type
, struct bt_ctf_field_type_integer
, parent
);
1553 ret
= integer
->declaration
.byte_order
== LITTLE_ENDIAN
?
1554 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1555 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1558 case CTF_TYPE_FLOAT
:
1560 struct bt_ctf_field_type_floating_point
*floating_point
=
1562 struct bt_ctf_field_type_floating_point
,
1564 ret
= floating_point
->declaration
.byte_order
== LITTLE_ENDIAN
?
1565 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1566 BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1576 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1577 enum bt_ctf_byte_order byte_order
)
1580 int internal_byte_order
;
1581 enum ctf_type_id type_id
;
1583 if (!type
|| type
->frozen
) {
1588 type_id
= type
->declaration
->id
;
1589 switch (byte_order
) {
1590 case BT_CTF_BYTE_ORDER_NATIVE
:
1591 internal_byte_order
= (G_BYTE_ORDER
== G_LITTLE_ENDIAN
?
1592 LITTLE_ENDIAN
: BIG_ENDIAN
);
1594 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1595 internal_byte_order
= LITTLE_ENDIAN
;
1597 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1598 case BT_CTF_BYTE_ORDER_NETWORK
:
1599 internal_byte_order
= BIG_ENDIAN
;
1606 if (set_byte_order_funcs
[type_id
]) {
1607 set_byte_order_funcs
[type_id
](type
, internal_byte_order
);
1613 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1614 struct bt_ctf_field_type
*type
)
1617 return CTF_TYPE_UNKNOWN
;
1620 return type
->declaration
->id
;
1623 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1629 bt_ctf_ref_get(&type
->ref_count
);
1632 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1634 enum ctf_type_id type_id
;
1640 type_id
= type
->declaration
->id
;
1641 assert(type_id
> CTF_TYPE_UNKNOWN
&& type_id
< NR_CTF_TYPES
);
1642 bt_ctf_ref_put(&type
->ref_count
, type_destroy_funcs
[type_id
]);
1646 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1656 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1657 struct bt_ctf_field_type_variant
*variant
,
1660 struct bt_ctf_field_type
*type
= NULL
;
1661 GQuark field_name_quark
;
1663 struct structure_field
*field_entry
;
1664 struct range_overlap_query query
= {
1665 .range_start
._signed
= tag_value
,
1666 .range_end
._signed
= tag_value
,
1667 .mapping_name
= 0, .overlaps
= 0};
1669 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1671 if (!query
.overlaps
) {
1675 field_name_quark
= query
.mapping_name
;
1676 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1677 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1681 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1682 type
= field_entry
->type
;
1688 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1689 struct bt_ctf_field_type_variant
*variant
,
1692 struct bt_ctf_field_type
*type
= NULL
;
1693 GQuark field_name_quark
;
1695 struct structure_field
*field_entry
;
1696 struct range_overlap_query query
= {
1697 .range_start
._unsigned
= tag_value
,
1698 .range_end
._unsigned
= tag_value
,
1699 .mapping_name
= 0, .overlaps
= 0};
1701 g_ptr_array_foreach(variant
->tag
->entries
,
1702 check_ranges_overlap_unsigned
,
1704 if (!query
.overlaps
) {
1708 field_name_quark
= query
.mapping_name
;
1709 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1710 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1714 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1715 type
= field_entry
->type
;
1721 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1722 struct metadata_context
*context
)
1726 if (!type
|| !context
) {
1731 ret
= type
->serialize(type
, context
);
1737 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1739 struct bt_ctf_field_type_integer
*integer
;
1745 integer
= container_of(
1746 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1747 struct bt_ctf_field_type_integer
, parent
);
1752 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1754 struct bt_ctf_field_type_enumeration
*enumeration
;
1760 enumeration
= container_of(
1761 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1762 struct bt_ctf_field_type_enumeration
, parent
);
1763 g_ptr_array_free(enumeration
->entries
, TRUE
);
1764 bt_ctf_field_type_put(enumeration
->container
);
1765 g_free(enumeration
);
1769 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1771 struct bt_ctf_field_type_floating_point
*floating_point
;
1777 floating_point
= container_of(
1778 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1779 struct bt_ctf_field_type_floating_point
, parent
);
1780 g_free(floating_point
);
1784 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1786 struct bt_ctf_field_type_structure
*structure
;
1792 structure
= container_of(
1793 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1794 struct bt_ctf_field_type_structure
, parent
);
1795 g_ptr_array_free(structure
->fields
, TRUE
);
1796 g_hash_table_destroy(structure
->field_name_to_index
);
1801 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1803 struct bt_ctf_field_type_variant
*variant
;
1809 variant
= container_of(
1810 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1811 struct bt_ctf_field_type_variant
, parent
);
1812 g_ptr_array_free(variant
->fields
, TRUE
);
1813 g_hash_table_destroy(variant
->field_name_to_index
);
1814 g_string_free(variant
->tag_name
, TRUE
);
1815 bt_ctf_field_type_put(&variant
->tag
->parent
);
1820 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1822 struct bt_ctf_field_type_array
*array
;
1828 array
= container_of(
1829 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1830 struct bt_ctf_field_type_array
, parent
);
1831 bt_ctf_field_type_put(array
->element_type
);
1836 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1838 struct bt_ctf_field_type_sequence
*sequence
;
1844 sequence
= container_of(
1845 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1846 struct bt_ctf_field_type_sequence
, parent
);
1847 bt_ctf_field_type_put(sequence
->element_type
);
1848 g_string_free(sequence
->length_field_name
, TRUE
);
1853 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1855 struct bt_ctf_field_type_string
*string
;
1861 string
= container_of(
1862 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1863 struct bt_ctf_field_type_string
, parent
);
1868 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
1874 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
1876 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
1877 type
, struct bt_ctf_field_type_enumeration
, parent
);
1879 generic_field_type_freeze(type
);
1880 bt_ctf_field_type_freeze(enumeration_type
->container
);
1884 void freeze_structure_field(struct structure_field
*field
)
1886 bt_ctf_field_type_freeze(field
->type
);
1890 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
1892 struct bt_ctf_field_type_structure
*structure_type
= container_of(
1893 type
, struct bt_ctf_field_type_structure
, parent
);
1895 generic_field_type_freeze(type
);
1896 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
1901 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
1903 struct bt_ctf_field_type_variant
*variant_type
= container_of(
1904 type
, struct bt_ctf_field_type_variant
, parent
);
1906 generic_field_type_freeze(type
);
1907 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
1912 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
1914 struct bt_ctf_field_type_array
*array_type
= container_of(
1915 type
, struct bt_ctf_field_type_array
, parent
);
1917 generic_field_type_freeze(type
);
1918 bt_ctf_field_type_freeze(array_type
->element_type
);
1922 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
1924 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
1925 type
, struct bt_ctf_field_type_sequence
, parent
);
1927 generic_field_type_freeze(type
);
1928 bt_ctf_field_type_freeze(sequence_type
->element_type
);
1932 const char *get_encoding_string(enum ctf_string_encoding encoding
)
1934 const char *encoding_string
;
1937 case CTF_STRING_NONE
:
1938 encoding_string
= "none";
1940 case CTF_STRING_ASCII
:
1941 encoding_string
= "ASCII";
1943 case CTF_STRING_UTF8
:
1944 encoding_string
= "UTF8";
1947 encoding_string
= "unknown";
1951 return encoding_string
;
1955 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
1957 const char *base_string
;
1960 case BT_CTF_INTEGER_BASE_DECIMAL
:
1961 base_string
= "decimal";
1963 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
1964 base_string
= "hexadecimal";
1966 case BT_CTF_INTEGER_BASE_OCTAL
:
1967 base_string
= "octal";
1969 case BT_CTF_INTEGER_BASE_BINARY
:
1970 base_string
= "binary";
1973 base_string
= "unknown";
1981 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
1982 struct metadata_context
*context
)
1984 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
1985 struct bt_ctf_field_type_integer
, parent
);
1987 g_string_append_printf(context
->string
,
1988 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
1989 integer
->declaration
.len
, type
->declaration
->alignment
,
1990 (integer
->declaration
.signedness
? "true" : "false"),
1991 get_encoding_string(integer
->declaration
.encoding
),
1992 get_integer_base_string(integer
->declaration
.base
),
1993 get_byte_order_string(integer
->declaration
.byte_order
));
1998 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
1999 struct metadata_context
*context
)
2003 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2004 struct bt_ctf_field_type_enumeration
, parent
);
2005 struct bt_ctf_field_type
*container_type
;
2006 int container_signed
;
2008 ret
= bt_ctf_field_type_validate(type
);
2013 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2014 if (!container_type
) {
2019 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2020 if (container_signed
< 0) {
2021 ret
= container_signed
;
2022 goto error_put_container_type
;
2025 g_string_append(context
->string
, "enum : ");
2026 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2028 goto error_put_container_type
;
2031 g_string_append(context
->string
, " { ");
2032 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2033 struct enumeration_mapping
*mapping
=
2034 enumeration
->entries
->pdata
[entry
];
2036 if (container_signed
) {
2037 if (mapping
->range_start
._signed
==
2038 mapping
->range_end
._signed
) {
2039 g_string_append_printf(context
->string
,
2040 "\"%s\" = %" PRId64
,
2041 g_quark_to_string(mapping
->string
),
2042 mapping
->range_start
._signed
);
2044 g_string_append_printf(context
->string
,
2045 "\"%s\" = %" PRId64
" ... %" PRId64
,
2046 g_quark_to_string(mapping
->string
),
2047 mapping
->range_start
._signed
,
2048 mapping
->range_end
._signed
);
2051 if (mapping
->range_start
._unsigned
==
2052 mapping
->range_end
._unsigned
) {
2053 g_string_append_printf(context
->string
,
2054 "\"%s\" = %" PRIu64
,
2055 g_quark_to_string(mapping
->string
),
2056 mapping
->range_start
._unsigned
);
2058 g_string_append_printf(context
->string
,
2059 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2060 g_quark_to_string(mapping
->string
),
2061 mapping
->range_start
._unsigned
,
2062 mapping
->range_end
._unsigned
);
2066 g_string_append(context
->string
,
2067 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2071 if (context
->field_name
->len
) {
2072 g_string_append_printf(context
->string
, " %s",
2073 context
->field_name
->str
);
2074 g_string_assign(context
->field_name
, "");
2076 error_put_container_type
:
2077 bt_ctf_field_type_put(container_type
);
2083 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2084 struct metadata_context
*context
)
2086 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2087 type
, struct bt_ctf_field_type_floating_point
, parent
);
2089 g_string_append_printf(context
->string
,
2090 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2091 floating_point
->declaration
.exp
->len
,
2092 floating_point
->declaration
.mantissa
->len
+ 1,
2093 get_byte_order_string(floating_point
->declaration
.byte_order
),
2094 type
->declaration
->alignment
);
2099 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2100 struct metadata_context
*context
)
2103 unsigned int indent
;
2105 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2106 struct bt_ctf_field_type_structure
, parent
);
2107 GString
*structure_field_name
= context
->field_name
;
2109 context
->field_name
= g_string_new("");
2111 context
->current_indentation_level
++;
2112 g_string_append(context
->string
, "struct {\n");
2114 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2115 struct structure_field
*field
;
2117 for (indent
= 0; indent
< context
->current_indentation_level
;
2119 g_string_append_c(context
->string
, '\t');
2122 field
= structure
->fields
->pdata
[i
];
2123 g_string_assign(context
->field_name
,
2124 g_quark_to_string(field
->name
));
2125 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2130 if (context
->field_name
->len
) {
2131 g_string_append_printf(context
->string
, " %s",
2132 context
->field_name
->str
);
2134 g_string_append(context
->string
, ";\n");
2137 context
->current_indentation_level
--;
2138 for (indent
= 0; indent
< context
->current_indentation_level
;
2140 g_string_append_c(context
->string
, '\t');
2143 g_string_append_printf(context
->string
, "} align(%zu)",
2144 type
->declaration
->alignment
);
2146 g_string_free(context
->field_name
, TRUE
);
2147 context
->field_name
= structure_field_name
;
2152 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2153 struct metadata_context
*context
)
2156 unsigned int indent
;
2158 struct bt_ctf_field_type_variant
*variant
= container_of(
2159 type
, struct bt_ctf_field_type_variant
, parent
);
2160 GString
*variant_field_name
= context
->field_name
;
2162 context
->field_name
= g_string_new("");
2163 g_string_append_printf(context
->string
,
2164 "variant <%s> {\n", variant
->tag_name
->str
);
2165 context
->current_indentation_level
++;
2166 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2167 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2169 g_string_assign(context
->field_name
,
2170 g_quark_to_string(field
->name
));
2171 for (indent
= 0; indent
< context
->current_indentation_level
;
2173 g_string_append_c(context
->string
, '\t');
2176 g_string_assign(context
->field_name
,
2177 g_quark_to_string(field
->name
));
2178 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2183 if (context
->field_name
->len
) {
2184 g_string_append_printf(context
->string
, " %s;",
2185 context
->field_name
->str
);
2188 g_string_append_c(context
->string
, '\n');
2191 context
->current_indentation_level
--;
2192 for (indent
= 0; indent
< context
->current_indentation_level
;
2194 g_string_append_c(context
->string
, '\t');
2197 g_string_append(context
->string
, "}");
2199 g_string_free(context
->field_name
, TRUE
);
2200 context
->field_name
= variant_field_name
;
2205 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2206 struct metadata_context
*context
)
2209 struct bt_ctf_field_type_array
*array
= container_of(type
,
2210 struct bt_ctf_field_type_array
, parent
);
2212 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2217 if (context
->field_name
->len
) {
2218 g_string_append_printf(context
->string
, " %s[%u]",
2219 context
->field_name
->str
, array
->length
);
2220 g_string_assign(context
->field_name
, "");
2222 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2229 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2230 struct metadata_context
*context
)
2233 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2234 type
, struct bt_ctf_field_type_sequence
, parent
);
2236 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2241 if (context
->field_name
->len
) {
2242 g_string_append_printf(context
->string
, " %s[%s]",
2243 context
->field_name
->str
,
2244 sequence
->length_field_name
->str
);
2245 g_string_assign(context
->field_name
, "");
2247 g_string_append_printf(context
->string
, "[%s]",
2248 sequence
->length_field_name
->str
);
2255 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2256 struct metadata_context
*context
)
2258 struct bt_ctf_field_type_string
*string
= container_of(
2259 type
, struct bt_ctf_field_type_string
, parent
);
2261 g_string_append_printf(context
->string
,
2262 "string { encoding = %s; }",
2263 get_encoding_string(string
->declaration
.encoding
));
2268 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2271 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2272 struct bt_ctf_field_type_integer
, parent
);
2274 integer_type
->declaration
.byte_order
= byte_order
;
2278 void bt_ctf_field_type_floating_point_set_byte_order(
2279 struct bt_ctf_field_type
*type
, int byte_order
)
2281 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2282 container_of(type
, struct bt_ctf_field_type_floating_point
,
2285 floating_point_type
->declaration
.byte_order
= byte_order
;
2286 floating_point_type
->sign
.byte_order
= byte_order
;
2287 floating_point_type
->mantissa
.byte_order
= byte_order
;
2288 floating_point_type
->exp
.byte_order
= byte_order
;