4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ctf-writer/writer-internal.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/endian.h>
39 struct range_overlap_query
{
54 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
56 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
58 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
60 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
62 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
64 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
66 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
68 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
70 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
73 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
74 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
76 bt_ctf_field_type_enumeration_destroy
,
78 bt_ctf_field_type_floating_point_destroy
,
79 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
80 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
81 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
82 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
83 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
87 void generic_field_type_freeze(struct bt_ctf_field_type
*);
89 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
91 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
93 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
100 type_freeze_func
const type_freeze_funcs
[] = {
101 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
102 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
103 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
104 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
105 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
106 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
107 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
108 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
112 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
113 struct metadata_context
*);
115 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
116 struct metadata_context
*);
118 int bt_ctf_field_type_floating_point_serialize(
119 struct bt_ctf_field_type
*, struct metadata_context
*);
121 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
122 struct metadata_context
*);
124 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
125 struct metadata_context
*);
127 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
128 struct metadata_context
*);
130 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
131 struct metadata_context
*);
133 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
134 struct metadata_context
*);
137 type_serialize_func
const type_serialize_funcs
[] = {
138 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
140 bt_ctf_field_type_enumeration_serialize
,
142 bt_ctf_field_type_floating_point_serialize
,
144 bt_ctf_field_type_structure_serialize
,
145 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
146 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
147 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
148 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
152 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
153 int byte_order
, int set_native
);
155 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
156 int byte_order
, int set_native
);
158 void bt_ctf_field_type_floating_point_set_byte_order(
159 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
161 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
162 int byte_order
, int set_native
);
164 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
165 int byte_order
, int set_native
);
167 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
168 int byte_order
, int set_native
);
170 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
171 int byte_order
, int set_native
);
173 /* The set_native flag only set the byte order if it is set to native */
175 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
176 int byte_order
, int set_native
) = {
177 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
179 bt_ctf_field_type_enumeration_set_byte_order
,
181 bt_ctf_field_type_floating_point_set_byte_order
,
183 bt_ctf_field_type_structure_set_byte_order
,
184 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
185 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
186 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
187 [CTF_TYPE_STRING
] = NULL
,
191 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
197 void destroy_structure_field(struct structure_field
*field
)
200 bt_ctf_field_type_put(field
->type
);
207 void check_ranges_overlap(gpointer element
, gpointer query
)
209 struct enumeration_mapping
*mapping
= element
;
210 struct range_overlap_query
*overlap_query
= query
;
212 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
213 && overlap_query
->range_start
._signed
<=
214 mapping
->range_end
._signed
) {
215 overlap_query
->overlaps
= 1;
216 overlap_query
->mapping_name
= mapping
->string
;
219 overlap_query
->overlaps
|=
220 mapping
->string
== overlap_query
->mapping_name
;
224 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
226 struct enumeration_mapping
*mapping
= element
;
227 struct range_overlap_query
*overlap_query
= query
;
229 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
230 && overlap_query
->range_start
._unsigned
<=
231 mapping
->range_end
._unsigned
) {
232 overlap_query
->overlaps
= 1;
233 overlap_query
->mapping_name
= mapping
->string
;
236 overlap_query
->overlaps
|=
237 mapping
->string
== overlap_query
->mapping_name
;
241 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
242 struct enumeration_mapping
**b
)
244 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
248 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
249 struct enumeration_mapping
**b
)
251 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
255 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
257 enum ctf_type_id type_id
= type
->declaration
->id
;
260 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
261 (type_id
< NR_CTF_TYPES
));
263 bt_ctf_ref_init(&type
->ref_count
);
264 type
->freeze
= type_freeze_funcs
[type_id
];
265 type
->serialize
= type_serialize_funcs
[type_id
];
266 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
268 type
->declaration
->alignment
= 1;
272 int add_structure_field(GPtrArray
*fields
,
273 GHashTable
*field_name_to_index
,
274 struct bt_ctf_field_type
*field_type
,
275 const char *field_name
)
278 GQuark name_quark
= g_quark_from_string(field_name
);
279 struct structure_field
*field
;
281 /* Make sure structure does not contain a field of the same name */
282 if (g_hash_table_lookup_extended(field_name_to_index
,
283 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
288 field
= g_new0(struct structure_field
, 1);
294 bt_ctf_field_type_get(field_type
);
295 field
->name
= name_quark
;
296 field
->type
= field_type
;
297 g_hash_table_insert(field_name_to_index
,
298 (gpointer
) (unsigned long) name_quark
,
299 (gpointer
) (unsigned long) fields
->len
);
300 g_ptr_array_add(fields
, field
);
301 bt_ctf_field_type_freeze(field_type
);
307 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
309 struct bt_ctf_field_type
*type
;
310 enum ctf_type_id type_id
;
316 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
317 type_id
= type
->declaration
->id
;
318 if (type_id
<= CTF_TYPE_UNKNOWN
||
319 type_id
>= NR_CTF_TYPES
) {
323 if (type
->alias_name
) {
324 g_string_free(type
->alias_name
, TRUE
);
326 type_destroy_funcs
[type_id
](ref
);
330 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
339 if (type
->declaration
->id
== CTF_TYPE_ENUM
) {
340 struct bt_ctf_field_type_enumeration
*enumeration
=
341 container_of(type
, struct bt_ctf_field_type_enumeration
,
344 ret
= enumeration
->entries
->len
? 0 : -1;
350 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
352 struct bt_ctf_field_type_integer
*integer
=
353 g_new0(struct bt_ctf_field_type_integer
, 1);
355 if (!integer
|| size
> 64) {
359 integer
->parent
.declaration
= &integer
->declaration
.p
;
360 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
361 integer
->declaration
.len
= size
;
362 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
363 integer
->declaration
.encoding
= CTF_STRING_NONE
;
364 bt_ctf_field_type_init(&integer
->parent
);
365 return &integer
->parent
;
368 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
371 struct bt_ctf_field_type_integer
*integer
;
373 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
378 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
379 ret
= (int) integer
->declaration
.len
;
384 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
387 struct bt_ctf_field_type_integer
*integer
;
389 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
394 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
395 ret
= integer
->declaration
.signedness
;
400 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
404 struct bt_ctf_field_type_integer
*integer
;
406 if (!type
|| type
->frozen
||
407 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
412 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
413 if (is_signed
&& integer
->declaration
.len
<= 1) {
418 integer
->declaration
.signedness
= !!is_signed
;
423 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
424 struct bt_ctf_field_type
*type
)
426 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
427 struct bt_ctf_field_type_integer
*integer
;
429 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
433 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
434 ret
= integer
->declaration
.base
;
439 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
440 enum bt_ctf_integer_base base
)
444 if (!type
|| type
->frozen
||
445 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
451 case BT_CTF_INTEGER_BASE_BINARY
:
452 case BT_CTF_INTEGER_BASE_OCTAL
:
453 case BT_CTF_INTEGER_BASE_DECIMAL
:
454 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
456 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
457 struct bt_ctf_field_type_integer
, parent
);
458 integer
->declaration
.base
= base
;
468 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
469 struct bt_ctf_field_type
*type
)
471 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
472 struct bt_ctf_field_type_integer
*integer
;
474 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
478 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
479 ret
= integer
->declaration
.encoding
;
484 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
485 enum ctf_string_encoding encoding
)
488 struct bt_ctf_field_type_integer
*integer
;
490 if (!type
|| type
->frozen
||
491 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
492 (encoding
< CTF_STRING_NONE
) ||
493 (encoding
>= CTF_STRING_UNKNOWN
)) {
498 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
499 integer
->declaration
.encoding
= encoding
;
504 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
505 struct bt_ctf_field_type
*integer_container_type
)
507 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
509 if (!integer_container_type
) {
513 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
517 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
522 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
523 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
524 bt_ctf_field_type_get(integer_container_type
);
525 enumeration
->container
= integer_container_type
;
526 enumeration
->entries
= g_ptr_array_new_with_free_func(
527 (GDestroyNotify
)destroy_enumeration_mapping
);
528 bt_ctf_field_type_init(&enumeration
->parent
);
529 return &enumeration
->parent
;
535 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
536 struct bt_ctf_field_type
*type
)
538 struct bt_ctf_field_type
*container_type
= NULL
;
539 struct bt_ctf_field_type_enumeration
*enumeration_type
;
545 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
549 enumeration_type
= container_of(type
,
550 struct bt_ctf_field_type_enumeration
, parent
);
551 container_type
= enumeration_type
->container
;
552 bt_ctf_field_type_get(container_type
);
554 return container_type
;
557 int bt_ctf_field_type_enumeration_add_mapping(
558 struct bt_ctf_field_type
*type
, const char *string
,
559 int64_t range_start
, int64_t range_end
)
563 struct enumeration_mapping
*mapping
;
564 struct bt_ctf_field_type_enumeration
*enumeration
;
565 struct range_overlap_query query
;
566 char *escaped_string
;
568 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
570 (range_end
< range_start
)) {
575 if (!string
|| strlen(string
) == 0) {
580 escaped_string
= g_strescape(string
, NULL
);
581 if (!escaped_string
) {
586 mapping_name
= g_quark_from_string(escaped_string
);
587 query
= (struct range_overlap_query
) {
588 .range_start
._signed
= range_start
,
589 .range_end
._signed
= range_end
,
590 .mapping_name
= mapping_name
,
592 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
595 /* Check that the range does not overlap with one already present */
596 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
597 if (query
.overlaps
) {
602 mapping
= g_new(struct enumeration_mapping
, 1);
608 *mapping
= (struct enumeration_mapping
) {
609 .range_start
._signed
= range_start
,
610 .range_end
._signed
= range_end
, .string
= mapping_name
};
611 g_ptr_array_add(enumeration
->entries
, mapping
);
612 g_ptr_array_sort(enumeration
->entries
,
613 (GCompareFunc
)compare_enumeration_mappings_signed
);
615 free(escaped_string
);
620 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
621 struct bt_ctf_field_type
*type
, const char *string
,
622 uint64_t range_start
, uint64_t range_end
)
626 struct enumeration_mapping
*mapping
;
627 struct bt_ctf_field_type_enumeration
*enumeration
;
628 struct range_overlap_query query
;
629 char *escaped_string
;
631 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
633 (range_end
< range_start
)) {
638 if (!string
|| strlen(string
) == 0) {
643 escaped_string
= g_strescape(string
, NULL
);
644 if (!escaped_string
) {
649 mapping_name
= g_quark_from_string(escaped_string
);
650 query
= (struct range_overlap_query
) {
651 .range_start
._unsigned
= range_start
,
652 .range_end
._unsigned
= range_end
,
653 .mapping_name
= mapping_name
,
655 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
658 /* Check that the range does not overlap with one already present */
659 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
661 if (query
.overlaps
) {
666 mapping
= g_new(struct enumeration_mapping
, 1);
672 *mapping
= (struct enumeration_mapping
) {
673 .range_start
._unsigned
= range_start
,
674 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
675 g_ptr_array_add(enumeration
->entries
, mapping
);
676 g_ptr_array_sort(enumeration
->entries
,
677 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
679 free(escaped_string
);
684 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
685 struct bt_ctf_field_type_enumeration
*enumeration_type
,
688 const char *name
= NULL
;
689 struct range_overlap_query query
=
690 (struct range_overlap_query
) {
691 .range_start
._unsigned
= value
,
692 .range_end
._unsigned
= value
,
695 g_ptr_array_foreach(enumeration_type
->entries
,
696 check_ranges_overlap_unsigned
,
698 if (!query
.overlaps
) {
702 name
= g_quark_to_string(query
.mapping_name
);
707 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
708 struct bt_ctf_field_type_enumeration
*enumeration_type
,
711 const char *name
= NULL
;
712 struct range_overlap_query query
=
713 (struct range_overlap_query
) {
714 .range_start
._signed
= value
,
715 .range_end
._signed
= value
,
718 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
720 if (!query
.overlaps
) {
724 name
= g_quark_to_string(query
.mapping_name
);
729 int bt_ctf_field_type_enumeration_get_mapping_count(
730 struct bt_ctf_field_type
*type
)
733 struct bt_ctf_field_type_enumeration
*enumeration
;
735 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
740 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
742 ret
= (int) enumeration
->entries
->len
;
748 struct enumeration_mapping
*get_enumeration_mapping(
749 struct bt_ctf_field_type
*type
, int index
)
751 struct enumeration_mapping
*mapping
= NULL
;
752 struct bt_ctf_field_type_enumeration
*enumeration
;
754 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
756 if (index
>= enumeration
->entries
->len
) {
760 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
765 int bt_ctf_field_type_enumeration_get_mapping(
766 struct bt_ctf_field_type
*type
, int index
,
767 const char **string
, int64_t *range_start
, int64_t *range_end
)
769 struct enumeration_mapping
*mapping
;
772 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
773 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
778 mapping
= get_enumeration_mapping(type
, index
);
784 *string
= g_quark_to_string(mapping
->string
);
785 *range_start
= mapping
->range_start
._signed
;
786 *range_end
= mapping
->range_end
._signed
;
791 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
792 struct bt_ctf_field_type
*type
, int index
,
793 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
795 struct enumeration_mapping
*mapping
;
798 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
799 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
804 mapping
= get_enumeration_mapping(type
, index
);
810 *string
= g_quark_to_string(mapping
->string
);
811 *range_start
= mapping
->range_start
._unsigned
;
812 *range_end
= mapping
->range_end
._unsigned
;
817 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
818 struct bt_ctf_field_type
*type
, const char *name
)
821 struct bt_ctf_field_type_enumeration
*enumeration
;
824 if (!type
|| !name
||
825 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
830 name_quark
= g_quark_try_string(name
);
836 enumeration
= container_of(type
,
837 struct bt_ctf_field_type_enumeration
, parent
);
838 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
839 struct enumeration_mapping
*mapping
=
840 get_enumeration_mapping(type
, i
);
842 if (mapping
->string
== name_quark
) {
853 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
854 struct bt_ctf_field_type
*type
, int64_t value
)
856 struct bt_ctf_field_type_enumeration
*enumeration
;
859 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
864 enumeration
= container_of(type
,
865 struct bt_ctf_field_type_enumeration
, parent
);
866 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
867 struct enumeration_mapping
*mapping
=
868 get_enumeration_mapping(type
, i
);
870 if (value
>= mapping
->range_start
._signed
&&
871 value
<= mapping
->range_end
._signed
) {
882 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
883 struct bt_ctf_field_type
*type
, uint64_t value
)
885 struct bt_ctf_field_type_enumeration
*enumeration
;
888 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
893 enumeration
= container_of(type
,
894 struct bt_ctf_field_type_enumeration
, parent
);
895 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
896 struct enumeration_mapping
*mapping
=
897 get_enumeration_mapping(type
, i
);
899 if (value
>= mapping
->range_start
._unsigned
&&
900 value
<= mapping
->range_end
._unsigned
) {
911 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
913 struct bt_ctf_field_type_floating_point
*floating_point
=
914 g_new0(struct bt_ctf_field_type_floating_point
, 1);
916 if (!floating_point
) {
920 floating_point
->declaration
.sign
= &floating_point
->sign
;
921 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
922 floating_point
->declaration
.exp
= &floating_point
->exp
;
923 floating_point
->sign
.len
= 1;
924 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
925 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
926 floating_point
->declaration
.exp
->len
=
927 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
928 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
929 floating_point
->sign
.p
.alignment
= 1;
930 floating_point
->mantissa
.p
.alignment
= 1;
931 floating_point
->exp
.p
.alignment
= 1;
933 bt_ctf_field_type_init(&floating_point
->parent
);
935 return floating_point
? &floating_point
->parent
: NULL
;
938 int bt_ctf_field_type_floating_point_get_exponent_digits(
939 struct bt_ctf_field_type
*type
)
942 struct bt_ctf_field_type_floating_point
*floating_point
;
944 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
949 floating_point
= container_of(type
,
950 struct bt_ctf_field_type_floating_point
, parent
);
951 ret
= (int) floating_point
->declaration
.exp
->len
;
956 int bt_ctf_field_type_floating_point_set_exponent_digits(
957 struct bt_ctf_field_type
*type
,
958 unsigned int exponent_digits
)
961 struct bt_ctf_field_type_floating_point
*floating_point
;
963 if (!type
|| type
->frozen
||
964 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
969 floating_point
= container_of(type
,
970 struct bt_ctf_field_type_floating_point
, parent
);
971 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
972 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
974 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
979 floating_point
->declaration
.exp
->len
= exponent_digits
;
984 int bt_ctf_field_type_floating_point_get_mantissa_digits(
985 struct bt_ctf_field_type
*type
)
988 struct bt_ctf_field_type_floating_point
*floating_point
;
990 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
995 floating_point
= container_of(type
,
996 struct bt_ctf_field_type_floating_point
, parent
);
997 ret
= (int) floating_point
->mantissa
.len
+ 1;
1002 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1003 struct bt_ctf_field_type
*type
,
1004 unsigned int mantissa_digits
)
1007 struct bt_ctf_field_type_floating_point
*floating_point
;
1009 if (!type
|| type
->frozen
||
1010 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1015 floating_point
= container_of(type
,
1016 struct bt_ctf_field_type_floating_point
, parent
);
1018 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1019 (mantissa_digits
!= DBL_MANT_DIG
) &&
1020 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1025 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1030 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1032 struct bt_ctf_field_type_structure
*structure
=
1033 g_new0(struct bt_ctf_field_type_structure
, 1);
1039 structure
->parent
.declaration
= &structure
->declaration
.p
;
1040 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1041 structure
->fields
= g_ptr_array_new_with_free_func(
1042 (GDestroyNotify
)destroy_structure_field
);
1043 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1044 bt_ctf_field_type_init(&structure
->parent
);
1045 return &structure
->parent
;
1050 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1051 struct bt_ctf_field_type
*field_type
,
1052 const char *field_name
)
1055 struct bt_ctf_field_type_structure
*structure
;
1057 if (!type
|| !field_type
|| type
->frozen
||
1058 bt_ctf_validate_identifier(field_name
) ||
1059 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1060 bt_ctf_field_type_validate(field_type
)) {
1065 structure
= container_of(type
,
1066 struct bt_ctf_field_type_structure
, parent
);
1067 if (add_structure_field(structure
->fields
,
1068 structure
->field_name_to_index
, field_type
, field_name
)) {
1073 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1074 type
->declaration
->alignment
=
1075 field_type
->declaration
->alignment
;
1081 int bt_ctf_field_type_structure_get_field_count(
1082 struct bt_ctf_field_type
*type
)
1085 struct bt_ctf_field_type_structure
*structure
;
1087 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1092 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1094 ret
= (int) structure
->fields
->len
;
1099 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1100 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1103 struct bt_ctf_field_type_structure
*structure
;
1104 struct structure_field
*field
;
1107 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1108 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1113 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1115 if (index
>= structure
->fields
->len
) {
1120 field
= g_ptr_array_index(structure
->fields
, index
);
1121 *field_type
= field
->type
;
1122 bt_ctf_field_type_get(field
->type
);
1123 *field_name
= g_quark_to_string(field
->name
);
1128 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1129 struct bt_ctf_field_type
*type
,
1134 struct structure_field
*field
;
1135 struct bt_ctf_field_type_structure
*structure
;
1136 struct bt_ctf_field_type
*field_type
= NULL
;
1138 if (!type
|| !name
) {
1142 name_quark
= g_quark_try_string(name
);
1147 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1149 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1150 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1154 field
= structure
->fields
->pdata
[index
];
1155 field_type
= field
->type
;
1156 bt_ctf_field_type_get(field_type
);
1161 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1162 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1164 struct bt_ctf_field_type_variant
*variant
= NULL
;
1166 if (!enum_tag
|| bt_ctf_validate_identifier(tag_name
) ||
1167 (enum_tag
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1171 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1176 variant
->parent
.declaration
= &variant
->declaration
.p
;
1177 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1178 variant
->tag_name
= g_string_new(tag_name
);
1179 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1180 variant
->fields
= g_ptr_array_new_with_free_func(
1181 (GDestroyNotify
)destroy_structure_field
);
1182 bt_ctf_field_type_get(enum_tag
);
1183 variant
->tag
= container_of(enum_tag
,
1184 struct bt_ctf_field_type_enumeration
, parent
);
1185 bt_ctf_field_type_init(&variant
->parent
);
1186 return &variant
->parent
;
1191 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1192 struct bt_ctf_field_type
*type
)
1194 struct bt_ctf_field_type_variant
*variant
;
1195 struct bt_ctf_field_type
*tag_type
= NULL
;
1197 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1201 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1202 tag_type
= &variant
->tag
->parent
;
1203 bt_ctf_field_type_get(tag_type
);
1208 const char *bt_ctf_field_type_variant_get_tag_name(
1209 struct bt_ctf_field_type
*type
)
1211 struct bt_ctf_field_type_variant
*variant
;
1212 const char *tag_name
= NULL
;
1214 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1218 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1219 tag_name
= variant
->tag_name
->str
;
1224 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1225 struct bt_ctf_field_type
*field_type
,
1226 const char *field_name
)
1231 struct bt_ctf_field_type_variant
*variant
;
1232 GQuark field_name_quark
= g_quark_from_string(field_name
);
1234 if (!type
|| !field_type
|| type
->frozen
||
1235 bt_ctf_validate_identifier(field_name
) ||
1236 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1237 bt_ctf_field_type_validate(field_type
)) {
1242 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1243 /* Make sure this name is present in the enum tag */
1244 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1245 struct enumeration_mapping
*mapping
=
1246 g_ptr_array_index(variant
->tag
->entries
, i
);
1248 if (mapping
->string
== field_name_quark
) {
1254 if (!name_found
|| add_structure_field(variant
->fields
,
1255 variant
->field_name_to_index
, field_type
, field_name
)) {
1263 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1264 struct bt_ctf_field_type
*type
,
1265 const char *field_name
)
1269 struct structure_field
*field
;
1270 struct bt_ctf_field_type_variant
*variant
;
1271 struct bt_ctf_field_type
*field_type
= NULL
;
1273 if (!type
|| !field_name
) {
1277 name_quark
= g_quark_try_string(field_name
);
1282 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1283 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1284 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1288 field
= g_ptr_array_index(variant
->fields
, index
);
1289 field_type
= field
->type
;
1290 bt_ctf_field_type_get(field_type
);
1295 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1296 struct bt_ctf_field_type
*type
,
1297 struct bt_ctf_field
*tag
)
1299 const char *enum_value
;
1300 struct bt_ctf_field_type
*field_type
= NULL
;
1302 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1306 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1311 /* Already increments field_type's reference count */
1312 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1318 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1321 struct bt_ctf_field_type_variant
*variant
;
1323 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1328 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1330 ret
= (int) variant
->fields
->len
;
1336 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1337 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1340 struct bt_ctf_field_type_variant
*variant
;
1341 struct structure_field
*field
;
1344 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1345 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1350 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1352 if (index
>= variant
->fields
->len
) {
1357 field
= g_ptr_array_index(variant
->fields
, index
);
1358 *field_type
= field
->type
;
1359 bt_ctf_field_type_get(field
->type
);
1360 *field_name
= g_quark_to_string(field
->name
);
1365 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1366 struct bt_ctf_field_type
*element_type
,
1367 unsigned int length
)
1369 struct bt_ctf_field_type_array
*array
= NULL
;
1371 if (!element_type
|| length
== 0 ||
1372 bt_ctf_field_type_validate(element_type
)) {
1376 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1381 array
->parent
.declaration
= &array
->declaration
.p
;
1382 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1384 bt_ctf_field_type_get(element_type
);
1385 array
->element_type
= element_type
;
1386 array
->length
= length
;
1387 bt_ctf_field_type_init(&array
->parent
);
1388 array
->parent
.declaration
->alignment
=
1389 element_type
->declaration
->alignment
;
1390 return &array
->parent
;
1395 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1396 struct bt_ctf_field_type
*type
)
1398 struct bt_ctf_field_type
*ret
= NULL
;
1399 struct bt_ctf_field_type_array
*array
;
1401 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1405 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1406 ret
= array
->element_type
;
1407 bt_ctf_field_type_get(ret
);
1412 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1415 struct bt_ctf_field_type_array
*array
;
1417 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1422 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1423 ret
= (int64_t) array
->length
;
1428 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1429 struct bt_ctf_field_type
*element_type
,
1430 const char *length_field_name
)
1432 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1434 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1435 bt_ctf_field_type_validate(element_type
)) {
1439 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1444 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1445 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1446 bt_ctf_field_type_get(element_type
);
1447 sequence
->element_type
= element_type
;
1448 sequence
->length_field_name
= g_string_new(length_field_name
);
1449 bt_ctf_field_type_init(&sequence
->parent
);
1450 sequence
->parent
.declaration
->alignment
=
1451 element_type
->declaration
->alignment
;
1452 return &sequence
->parent
;
1457 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1458 struct bt_ctf_field_type
*type
)
1460 struct bt_ctf_field_type
*ret
= NULL
;
1461 struct bt_ctf_field_type_sequence
*sequence
;
1463 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1467 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1469 ret
= sequence
->element_type
;
1470 bt_ctf_field_type_get(ret
);
1475 const char *bt_ctf_field_type_sequence_get_length_field_name(
1476 struct bt_ctf_field_type
*type
)
1478 const char *ret
= NULL
;
1479 struct bt_ctf_field_type_sequence
*sequence
;
1481 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1485 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1487 ret
= sequence
->length_field_name
->str
;
1492 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1494 struct bt_ctf_field_type_string
*string
=
1495 g_new0(struct bt_ctf_field_type_string
, 1);
1501 string
->parent
.declaration
= &string
->declaration
.p
;
1502 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1503 bt_ctf_field_type_init(&string
->parent
);
1504 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1505 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1506 return &string
->parent
;
1509 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1510 struct bt_ctf_field_type
*type
)
1512 struct bt_ctf_field_type_string
*string
;
1513 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1515 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1519 string
= container_of(type
, struct bt_ctf_field_type_string
,
1521 ret
= string
->declaration
.encoding
;
1526 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1527 enum ctf_string_encoding encoding
)
1530 struct bt_ctf_field_type_string
*string
;
1532 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1533 (encoding
!= CTF_STRING_UTF8
&&
1534 encoding
!= CTF_STRING_ASCII
)) {
1539 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1540 string
->declaration
.encoding
= encoding
;
1545 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1554 ret
= (int) type
->declaration
->alignment
;
1559 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1560 unsigned int alignment
)
1564 /* Alignment must be bit-aligned (1) or byte aligned */
1565 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1570 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1571 alignment
!= CHAR_BIT
) {
1576 type
->declaration
->alignment
= alignment
;
1582 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1583 struct bt_ctf_field_type
*type
)
1585 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1586 int internal_byte_order
= -1;
1592 switch (type
->declaration
->id
) {
1593 case CTF_TYPE_INTEGER
:
1595 struct bt_ctf_field_type_integer
*integer
= container_of(
1596 type
, struct bt_ctf_field_type_integer
, parent
);
1597 internal_byte_order
= integer
->declaration
.byte_order
;
1600 case CTF_TYPE_FLOAT
:
1602 struct bt_ctf_field_type_floating_point
*floating_point
=
1604 struct bt_ctf_field_type_floating_point
,
1606 internal_byte_order
= floating_point
->declaration
.byte_order
;
1613 switch (internal_byte_order
) {
1615 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1618 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1621 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1624 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1630 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1631 enum bt_ctf_byte_order byte_order
)
1634 int internal_byte_order
;
1635 enum ctf_type_id type_id
;
1637 if (!type
|| type
->frozen
) {
1642 type_id
= type
->declaration
->id
;
1643 switch (byte_order
) {
1644 case BT_CTF_BYTE_ORDER_NATIVE
:
1645 /* Leave unset. Will be initialized by parent. */
1646 internal_byte_order
= 0;
1648 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1649 internal_byte_order
= LITTLE_ENDIAN
;
1651 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1652 case BT_CTF_BYTE_ORDER_NETWORK
:
1653 internal_byte_order
= BIG_ENDIAN
;
1660 if (set_byte_order_funcs
[type_id
]) {
1661 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1667 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1668 struct bt_ctf_field_type
*type
)
1671 return CTF_TYPE_UNKNOWN
;
1674 return type
->declaration
->id
;
1677 const char *bt_ctf_field_type_get_alias_name(
1678 struct bt_ctf_field_type
*type
)
1680 const char *name
= NULL
;
1682 if (!type
|| !type
->alias_name
) {
1686 name
= type
->alias_name
->str
;
1691 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1697 bt_ctf_ref_get(&type
->ref_count
);
1700 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1706 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1710 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1720 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1721 struct bt_ctf_field_type_variant
*variant
,
1724 struct bt_ctf_field_type
*type
= NULL
;
1725 GQuark field_name_quark
;
1727 struct structure_field
*field_entry
;
1728 struct range_overlap_query query
= {
1729 .range_start
._signed
= tag_value
,
1730 .range_end
._signed
= tag_value
,
1731 .mapping_name
= 0, .overlaps
= 0};
1733 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1735 if (!query
.overlaps
) {
1739 field_name_quark
= query
.mapping_name
;
1740 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1741 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1745 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1746 type
= field_entry
->type
;
1752 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1753 struct bt_ctf_field_type_variant
*variant
,
1756 struct bt_ctf_field_type
*type
= NULL
;
1757 GQuark field_name_quark
;
1759 struct structure_field
*field_entry
;
1760 struct range_overlap_query query
= {
1761 .range_start
._unsigned
= tag_value
,
1762 .range_end
._unsigned
= tag_value
,
1763 .mapping_name
= 0, .overlaps
= 0};
1765 g_ptr_array_foreach(variant
->tag
->entries
,
1766 check_ranges_overlap_unsigned
,
1768 if (!query
.overlaps
) {
1772 field_name_quark
= query
.mapping_name
;
1773 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1774 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1778 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1779 type
= field_entry
->type
;
1785 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1786 struct metadata_context
*context
)
1790 if (!type
|| !context
) {
1795 ret
= type
->serialize(type
, context
);
1801 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1808 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1809 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1810 set_byte_order_funcs
[type
->declaration
->id
](type
,
1816 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1818 struct bt_ctf_field_type_integer
*integer
;
1824 integer
= container_of(
1825 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1826 struct bt_ctf_field_type_integer
, parent
);
1831 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1833 struct bt_ctf_field_type_enumeration
*enumeration
;
1839 enumeration
= container_of(
1840 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1841 struct bt_ctf_field_type_enumeration
, parent
);
1842 g_ptr_array_free(enumeration
->entries
, TRUE
);
1843 bt_ctf_field_type_put(enumeration
->container
);
1844 g_free(enumeration
);
1848 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1850 struct bt_ctf_field_type_floating_point
*floating_point
;
1856 floating_point
= container_of(
1857 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1858 struct bt_ctf_field_type_floating_point
, parent
);
1859 g_free(floating_point
);
1863 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1865 struct bt_ctf_field_type_structure
*structure
;
1871 structure
= container_of(
1872 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1873 struct bt_ctf_field_type_structure
, parent
);
1874 g_ptr_array_free(structure
->fields
, TRUE
);
1875 g_hash_table_destroy(structure
->field_name_to_index
);
1880 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1882 struct bt_ctf_field_type_variant
*variant
;
1888 variant
= container_of(
1889 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1890 struct bt_ctf_field_type_variant
, parent
);
1891 g_ptr_array_free(variant
->fields
, TRUE
);
1892 g_hash_table_destroy(variant
->field_name_to_index
);
1893 g_string_free(variant
->tag_name
, TRUE
);
1894 bt_ctf_field_type_put(&variant
->tag
->parent
);
1899 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
1901 struct bt_ctf_field_type_array
*array
;
1907 array
= container_of(
1908 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1909 struct bt_ctf_field_type_array
, parent
);
1910 bt_ctf_field_type_put(array
->element_type
);
1915 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
1917 struct bt_ctf_field_type_sequence
*sequence
;
1923 sequence
= container_of(
1924 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1925 struct bt_ctf_field_type_sequence
, parent
);
1926 bt_ctf_field_type_put(sequence
->element_type
);
1927 g_string_free(sequence
->length_field_name
, TRUE
);
1932 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
1934 struct bt_ctf_field_type_string
*string
;
1940 string
= container_of(
1941 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1942 struct bt_ctf_field_type_string
, parent
);
1947 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
1953 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
1955 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
1956 type
, struct bt_ctf_field_type_enumeration
, parent
);
1958 generic_field_type_freeze(type
);
1959 bt_ctf_field_type_freeze(enumeration_type
->container
);
1963 void freeze_structure_field(struct structure_field
*field
)
1965 bt_ctf_field_type_freeze(field
->type
);
1969 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
1971 struct bt_ctf_field_type_structure
*structure_type
= container_of(
1972 type
, struct bt_ctf_field_type_structure
, parent
);
1974 generic_field_type_freeze(type
);
1975 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
1980 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
1982 struct bt_ctf_field_type_variant
*variant_type
= container_of(
1983 type
, struct bt_ctf_field_type_variant
, parent
);
1985 generic_field_type_freeze(type
);
1986 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
1991 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
1993 struct bt_ctf_field_type_array
*array_type
= container_of(
1994 type
, struct bt_ctf_field_type_array
, parent
);
1996 generic_field_type_freeze(type
);
1997 bt_ctf_field_type_freeze(array_type
->element_type
);
2001 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2003 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2004 type
, struct bt_ctf_field_type_sequence
, parent
);
2006 generic_field_type_freeze(type
);
2007 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2011 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2013 const char *encoding_string
;
2016 case CTF_STRING_NONE
:
2017 encoding_string
= "none";
2019 case CTF_STRING_ASCII
:
2020 encoding_string
= "ASCII";
2022 case CTF_STRING_UTF8
:
2023 encoding_string
= "UTF8";
2026 encoding_string
= "unknown";
2030 return encoding_string
;
2034 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2036 const char *base_string
;
2039 case BT_CTF_INTEGER_BASE_DECIMAL
:
2040 base_string
= "decimal";
2042 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2043 base_string
= "hexadecimal";
2045 case BT_CTF_INTEGER_BASE_OCTAL
:
2046 base_string
= "octal";
2048 case BT_CTF_INTEGER_BASE_BINARY
:
2049 base_string
= "binary";
2052 base_string
= "unknown";
2060 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2061 struct metadata_context
*context
)
2063 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2064 struct bt_ctf_field_type_integer
, parent
);
2066 g_string_append_printf(context
->string
,
2067 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
2068 integer
->declaration
.len
, type
->declaration
->alignment
,
2069 (integer
->declaration
.signedness
? "true" : "false"),
2070 get_encoding_string(integer
->declaration
.encoding
),
2071 get_integer_base_string(integer
->declaration
.base
),
2072 get_byte_order_string(integer
->declaration
.byte_order
));
2077 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2078 struct metadata_context
*context
)
2082 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2083 struct bt_ctf_field_type_enumeration
, parent
);
2084 struct bt_ctf_field_type
*container_type
;
2085 int container_signed
;
2087 ret
= bt_ctf_field_type_validate(type
);
2092 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2093 if (!container_type
) {
2098 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2099 if (container_signed
< 0) {
2100 ret
= container_signed
;
2101 goto error_put_container_type
;
2104 g_string_append(context
->string
, "enum : ");
2105 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2107 goto error_put_container_type
;
2110 g_string_append(context
->string
, " { ");
2111 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2112 struct enumeration_mapping
*mapping
=
2113 enumeration
->entries
->pdata
[entry
];
2115 if (container_signed
) {
2116 if (mapping
->range_start
._signed
==
2117 mapping
->range_end
._signed
) {
2118 g_string_append_printf(context
->string
,
2119 "\"%s\" = %" PRId64
,
2120 g_quark_to_string(mapping
->string
),
2121 mapping
->range_start
._signed
);
2123 g_string_append_printf(context
->string
,
2124 "\"%s\" = %" PRId64
" ... %" PRId64
,
2125 g_quark_to_string(mapping
->string
),
2126 mapping
->range_start
._signed
,
2127 mapping
->range_end
._signed
);
2130 if (mapping
->range_start
._unsigned
==
2131 mapping
->range_end
._unsigned
) {
2132 g_string_append_printf(context
->string
,
2133 "\"%s\" = %" PRIu64
,
2134 g_quark_to_string(mapping
->string
),
2135 mapping
->range_start
._unsigned
);
2137 g_string_append_printf(context
->string
,
2138 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2139 g_quark_to_string(mapping
->string
),
2140 mapping
->range_start
._unsigned
,
2141 mapping
->range_end
._unsigned
);
2145 g_string_append(context
->string
,
2146 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2150 if (context
->field_name
->len
) {
2151 g_string_append_printf(context
->string
, " %s",
2152 context
->field_name
->str
);
2153 g_string_assign(context
->field_name
, "");
2155 error_put_container_type
:
2156 bt_ctf_field_type_put(container_type
);
2162 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2163 struct metadata_context
*context
)
2165 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2166 type
, struct bt_ctf_field_type_floating_point
, parent
);
2168 g_string_append_printf(context
->string
,
2169 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2170 floating_point
->declaration
.exp
->len
,
2171 floating_point
->declaration
.mantissa
->len
+ 1,
2172 get_byte_order_string(floating_point
->declaration
.byte_order
),
2173 type
->declaration
->alignment
);
2178 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2179 struct metadata_context
*context
)
2182 unsigned int indent
;
2184 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2185 struct bt_ctf_field_type_structure
, parent
);
2186 GString
*structure_field_name
= context
->field_name
;
2188 context
->field_name
= g_string_new("");
2190 context
->current_indentation_level
++;
2191 g_string_append(context
->string
, "struct {\n");
2193 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2194 struct structure_field
*field
;
2196 for (indent
= 0; indent
< context
->current_indentation_level
;
2198 g_string_append_c(context
->string
, '\t');
2201 field
= structure
->fields
->pdata
[i
];
2202 g_string_assign(context
->field_name
,
2203 g_quark_to_string(field
->name
));
2204 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2209 if (context
->field_name
->len
) {
2210 g_string_append_printf(context
->string
, " %s",
2211 context
->field_name
->str
);
2213 g_string_append(context
->string
, ";\n");
2216 context
->current_indentation_level
--;
2217 for (indent
= 0; indent
< context
->current_indentation_level
;
2219 g_string_append_c(context
->string
, '\t');
2222 g_string_append_printf(context
->string
, "} align(%zu)",
2223 type
->declaration
->alignment
);
2225 g_string_free(context
->field_name
, TRUE
);
2226 context
->field_name
= structure_field_name
;
2231 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2232 struct metadata_context
*context
)
2235 unsigned int indent
;
2237 struct bt_ctf_field_type_variant
*variant
= container_of(
2238 type
, struct bt_ctf_field_type_variant
, parent
);
2239 GString
*variant_field_name
= context
->field_name
;
2241 context
->field_name
= g_string_new("");
2242 g_string_append_printf(context
->string
,
2243 "variant <%s> {\n", variant
->tag_name
->str
);
2244 context
->current_indentation_level
++;
2245 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2246 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2248 g_string_assign(context
->field_name
,
2249 g_quark_to_string(field
->name
));
2250 for (indent
= 0; indent
< context
->current_indentation_level
;
2252 g_string_append_c(context
->string
, '\t');
2255 g_string_assign(context
->field_name
,
2256 g_quark_to_string(field
->name
));
2257 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2262 if (context
->field_name
->len
) {
2263 g_string_append_printf(context
->string
, " %s;",
2264 context
->field_name
->str
);
2267 g_string_append_c(context
->string
, '\n');
2270 context
->current_indentation_level
--;
2271 for (indent
= 0; indent
< context
->current_indentation_level
;
2273 g_string_append_c(context
->string
, '\t');
2276 g_string_append(context
->string
, "}");
2278 g_string_free(context
->field_name
, TRUE
);
2279 context
->field_name
= variant_field_name
;
2284 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2285 struct metadata_context
*context
)
2288 struct bt_ctf_field_type_array
*array
= container_of(type
,
2289 struct bt_ctf_field_type_array
, parent
);
2291 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2296 if (context
->field_name
->len
) {
2297 g_string_append_printf(context
->string
, " %s[%u]",
2298 context
->field_name
->str
, array
->length
);
2299 g_string_assign(context
->field_name
, "");
2301 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2308 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2309 struct metadata_context
*context
)
2312 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2313 type
, struct bt_ctf_field_type_sequence
, parent
);
2315 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2320 if (context
->field_name
->len
) {
2321 g_string_append_printf(context
->string
, " %s[%s]",
2322 context
->field_name
->str
,
2323 sequence
->length_field_name
->str
);
2324 g_string_assign(context
->field_name
, "");
2326 g_string_append_printf(context
->string
, "[%s]",
2327 sequence
->length_field_name
->str
);
2334 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2335 struct metadata_context
*context
)
2337 struct bt_ctf_field_type_string
*string
= container_of(
2338 type
, struct bt_ctf_field_type_string
, parent
);
2340 g_string_append_printf(context
->string
,
2341 "string { encoding = %s; }",
2342 get_encoding_string(string
->declaration
.encoding
));
2347 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2348 int byte_order
, int set_native
)
2350 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2351 struct bt_ctf_field_type_integer
, parent
);
2354 integer_type
->declaration
.byte_order
=
2355 integer_type
->declaration
.byte_order
== 0 ?
2356 byte_order
: integer_type
->declaration
.byte_order
;
2358 integer_type
->declaration
.byte_order
= byte_order
;
2363 void bt_ctf_field_type_enumeration_set_byte_order(
2364 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2366 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2367 struct bt_ctf_field_type_enumeration
, parent
);
2369 /* Safe to assume that container is an integer */
2370 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2371 byte_order
, set_native
);
2375 void bt_ctf_field_type_floating_point_set_byte_order(
2376 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2378 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2379 container_of(type
, struct bt_ctf_field_type_floating_point
,
2383 floating_point_type
->declaration
.byte_order
=
2384 floating_point_type
->declaration
.byte_order
== 0 ?
2386 floating_point_type
->declaration
.byte_order
;
2387 floating_point_type
->sign
.byte_order
=
2388 floating_point_type
->sign
.byte_order
== 0 ?
2389 byte_order
: floating_point_type
->sign
.byte_order
;
2390 floating_point_type
->mantissa
.byte_order
=
2391 floating_point_type
->mantissa
.byte_order
== 0 ?
2392 byte_order
: floating_point_type
->mantissa
.byte_order
;
2393 floating_point_type
->exp
.byte_order
=
2394 floating_point_type
->exp
.byte_order
== 0 ?
2395 byte_order
: floating_point_type
->exp
.byte_order
;
2397 floating_point_type
->declaration
.byte_order
= byte_order
;
2398 floating_point_type
->sign
.byte_order
= byte_order
;
2399 floating_point_type
->mantissa
.byte_order
= byte_order
;
2400 floating_point_type
->exp
.byte_order
= byte_order
;
2405 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2406 int byte_order
, int set_native
)
2409 struct bt_ctf_field_type_structure
*structure_type
=
2410 container_of(type
, struct bt_ctf_field_type_structure
,
2413 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2414 struct structure_field
*field
= g_ptr_array_index(
2415 structure_type
->fields
, i
);
2416 struct bt_ctf_field_type
*field_type
= field
->type
;
2418 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2419 set_byte_order_funcs
[field_type
->declaration
->id
](
2420 field_type
, byte_order
, set_native
);
2426 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2427 int byte_order
, int set_native
)
2430 struct bt_ctf_field_type_variant
*variant_type
=
2431 container_of(type
, struct bt_ctf_field_type_variant
,
2434 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2435 struct structure_field
*field
= g_ptr_array_index(
2436 variant_type
->fields
, i
);
2437 struct bt_ctf_field_type
*field_type
= field
->type
;
2439 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2440 set_byte_order_funcs
[field_type
->declaration
->id
](
2441 field_type
, byte_order
, set_native
);
2447 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2448 int byte_order
, int set_native
)
2450 struct bt_ctf_field_type_array
*array_type
=
2451 container_of(type
, struct bt_ctf_field_type_array
,
2454 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2455 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2456 array_type
->element_type
, byte_order
, set_native
);
2461 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2462 int byte_order
, int set_native
)
2464 struct bt_ctf_field_type_sequence
*sequence_type
=
2465 container_of(type
, struct bt_ctf_field_type_sequence
,
2468 if (set_byte_order_funcs
[
2469 sequence_type
->element_type
->declaration
->id
]) {
2470 set_byte_order_funcs
[
2471 sequence_type
->element_type
->declaration
->id
](
2472 sequence_type
->element_type
, byte_order
, set_native
);