4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ctf-ir/clock.h>
33 #include <babeltrace/ctf-writer/writer-internal.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/endian.h>
40 struct range_overlap_query
{
55 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
69 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
71 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
74 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
75 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
77 bt_ctf_field_type_enumeration_destroy
,
79 bt_ctf_field_type_floating_point_destroy
,
80 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
81 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
82 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
83 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
84 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
88 void generic_field_type_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
101 type_freeze_func
const type_freeze_funcs
[] = {
102 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
103 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
104 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
105 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
106 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
107 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
108 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
109 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
113 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
114 struct metadata_context
*);
116 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_floating_point_serialize(
120 struct bt_ctf_field_type
*, struct metadata_context
*);
122 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
138 type_serialize_func
const type_serialize_funcs
[] = {
139 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
141 bt_ctf_field_type_enumeration_serialize
,
143 bt_ctf_field_type_floating_point_serialize
,
145 bt_ctf_field_type_structure_serialize
,
146 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
147 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
148 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
149 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
153 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
154 int byte_order
, int set_native
);
156 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_floating_point_set_byte_order(
160 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
162 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 /* The set_native flag only set the byte order if it is set to native */
176 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
177 int byte_order
, int set_native
) = {
178 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
180 bt_ctf_field_type_enumeration_set_byte_order
,
182 bt_ctf_field_type_floating_point_set_byte_order
,
184 bt_ctf_field_type_structure_set_byte_order
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
188 [CTF_TYPE_STRING
] = NULL
,
192 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
193 struct bt_ctf_field_type
*);
195 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
214 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
218 struct bt_ctf_field_type
*) = {
219 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
220 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
221 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
222 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
223 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
224 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
225 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
226 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
230 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
236 void destroy_structure_field(struct structure_field
*field
)
239 bt_ctf_field_type_put(field
->type
);
246 void check_ranges_overlap(gpointer element
, gpointer query
)
248 struct enumeration_mapping
*mapping
= element
;
249 struct range_overlap_query
*overlap_query
= query
;
251 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
252 && overlap_query
->range_start
._signed
<=
253 mapping
->range_end
._signed
) {
254 overlap_query
->overlaps
= 1;
255 overlap_query
->mapping_name
= mapping
->string
;
258 overlap_query
->overlaps
|=
259 mapping
->string
== overlap_query
->mapping_name
;
263 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
265 struct enumeration_mapping
*mapping
= element
;
266 struct range_overlap_query
*overlap_query
= query
;
268 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
269 && overlap_query
->range_start
._unsigned
<=
270 mapping
->range_end
._unsigned
) {
271 overlap_query
->overlaps
= 1;
272 overlap_query
->mapping_name
= mapping
->string
;
275 overlap_query
->overlaps
|=
276 mapping
->string
== overlap_query
->mapping_name
;
280 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
281 struct enumeration_mapping
**b
)
283 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
287 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
288 struct enumeration_mapping
**b
)
290 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
294 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
296 enum ctf_type_id type_id
= type
->declaration
->id
;
298 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
299 (type_id
< NR_CTF_TYPES
));
301 bt_ctf_ref_init(&type
->ref_count
);
302 type
->freeze
= type_freeze_funcs
[type_id
];
303 type
->serialize
= type_serialize_funcs
[type_id
];
306 int ret
= bt_ctf_field_type_set_byte_order(type
,
307 BT_CTF_BYTE_ORDER_NATIVE
);
311 type
->declaration
->alignment
= 1;
315 int add_structure_field(GPtrArray
*fields
,
316 GHashTable
*field_name_to_index
,
317 struct bt_ctf_field_type
*field_type
,
318 const char *field_name
)
321 GQuark name_quark
= g_quark_from_string(field_name
);
322 struct structure_field
*field
;
324 /* Make sure structure does not contain a field of the same name */
325 if (g_hash_table_lookup_extended(field_name_to_index
,
326 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
331 field
= g_new0(struct structure_field
, 1);
337 bt_ctf_field_type_get(field_type
);
338 field
->name
= name_quark
;
339 field
->type
= field_type
;
340 g_hash_table_insert(field_name_to_index
,
341 (gpointer
) (unsigned long) name_quark
,
342 (gpointer
) (unsigned long) fields
->len
);
343 g_ptr_array_add(fields
, field
);
349 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
351 struct bt_ctf_field_type
*type
;
352 enum ctf_type_id type_id
;
358 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
359 type_id
= type
->declaration
->id
;
360 if (type_id
<= CTF_TYPE_UNKNOWN
||
361 type_id
>= NR_CTF_TYPES
) {
365 type_destroy_funcs
[type_id
](ref
);
369 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
378 switch (type
->declaration
->id
) {
381 struct bt_ctf_field_type_enumeration
*enumeration
=
382 container_of(type
, struct bt_ctf_field_type_enumeration
,
385 /* Ensure enum has entries */
386 ret
= enumeration
->entries
->len
? 0 : -1;
389 case CTF_TYPE_SEQUENCE
:
391 struct bt_ctf_field_type_sequence
*sequence
=
392 container_of(type
, struct bt_ctf_field_type_sequence
,
395 /* length field name should be set at this point */
396 ret
= sequence
->length_field_name
->len
? 0 : -1;
399 case CTF_TYPE_VARIANT
:
401 struct bt_ctf_field_type_variant
*variant
=
402 container_of(type
, struct bt_ctf_field_type_variant
,
405 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
417 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
419 struct bt_ctf_field_type_integer
*integer
=
420 g_new0(struct bt_ctf_field_type_integer
, 1);
422 if (!integer
|| size
== 0 || size
> 64) {
426 integer
->parent
.declaration
= &integer
->declaration
.p
;
427 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
428 integer
->declaration
.len
= size
;
429 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
430 integer
->declaration
.encoding
= CTF_STRING_NONE
;
431 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
432 return &integer
->parent
;
435 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
438 struct bt_ctf_field_type_integer
*integer
;
440 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
445 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
446 ret
= (int) integer
->declaration
.len
;
451 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
454 struct bt_ctf_field_type_integer
*integer
;
456 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
461 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
462 ret
= integer
->declaration
.signedness
;
467 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
471 struct bt_ctf_field_type_integer
*integer
;
473 if (!type
|| type
->frozen
||
474 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
479 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
480 integer
->declaration
.signedness
= !!is_signed
;
485 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
486 struct bt_ctf_field_type
*type
)
488 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
489 struct bt_ctf_field_type_integer
*integer
;
491 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
495 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
496 ret
= integer
->declaration
.base
;
501 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
502 enum bt_ctf_integer_base base
)
506 if (!type
|| type
->frozen
||
507 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
513 case BT_CTF_INTEGER_BASE_BINARY
:
514 case BT_CTF_INTEGER_BASE_OCTAL
:
515 case BT_CTF_INTEGER_BASE_DECIMAL
:
516 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
518 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
519 struct bt_ctf_field_type_integer
, parent
);
520 integer
->declaration
.base
= base
;
530 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
531 struct bt_ctf_field_type
*type
)
533 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
534 struct bt_ctf_field_type_integer
*integer
;
536 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
540 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
541 ret
= integer
->declaration
.encoding
;
546 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
547 enum ctf_string_encoding encoding
)
550 struct bt_ctf_field_type_integer
*integer
;
552 if (!type
|| type
->frozen
||
553 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
554 (encoding
< CTF_STRING_NONE
) ||
555 (encoding
>= CTF_STRING_UNKNOWN
)) {
560 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
561 integer
->declaration
.encoding
= encoding
;
566 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
567 struct bt_ctf_field_type
*type
)
569 struct bt_ctf_field_type_integer
*integer
;
570 struct bt_ctf_clock
*clock
= NULL
;
576 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
577 clock
= integer
->mapped_clock
;
579 bt_ctf_clock_get(clock
);
585 int bt_ctf_field_type_integer_set_mapped_clock(
586 struct bt_ctf_field_type
*type
,
587 struct bt_ctf_clock
*clock
)
589 struct bt_ctf_field_type_integer
*integer
;
592 if (!type
|| type
->frozen
) {
597 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
598 if (integer
->mapped_clock
) {
599 bt_ctf_clock_put(integer
->mapped_clock
);
603 bt_ctf_clock_get(clock
);
606 integer
->mapped_clock
= clock
;
611 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
612 struct bt_ctf_field_type
*integer_container_type
)
614 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
616 if (!integer_container_type
) {
620 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
624 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
629 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
630 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
631 bt_ctf_field_type_get(integer_container_type
);
632 enumeration
->container
= integer_container_type
;
633 enumeration
->entries
= g_ptr_array_new_with_free_func(
634 (GDestroyNotify
)destroy_enumeration_mapping
);
635 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
636 return &enumeration
->parent
;
642 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
643 struct bt_ctf_field_type
*type
)
645 struct bt_ctf_field_type
*container_type
= NULL
;
646 struct bt_ctf_field_type_enumeration
*enumeration_type
;
652 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
656 enumeration_type
= container_of(type
,
657 struct bt_ctf_field_type_enumeration
, parent
);
658 container_type
= enumeration_type
->container
;
659 bt_ctf_field_type_get(container_type
);
661 return container_type
;
664 int bt_ctf_field_type_enumeration_add_mapping(
665 struct bt_ctf_field_type
*type
, const char *string
,
666 int64_t range_start
, int64_t range_end
)
670 struct enumeration_mapping
*mapping
;
671 struct bt_ctf_field_type_enumeration
*enumeration
;
672 struct range_overlap_query query
;
673 char *escaped_string
;
675 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
677 (range_end
< range_start
)) {
682 if (!string
|| strlen(string
) == 0) {
687 escaped_string
= g_strescape(string
, NULL
);
688 if (!escaped_string
) {
693 mapping_name
= g_quark_from_string(escaped_string
);
694 query
= (struct range_overlap_query
) {
695 .range_start
._signed
= range_start
,
696 .range_end
._signed
= range_end
,
697 .mapping_name
= mapping_name
,
699 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
702 /* Check that the range does not overlap with one already present */
703 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
704 if (query
.overlaps
) {
709 mapping
= g_new(struct enumeration_mapping
, 1);
715 *mapping
= (struct enumeration_mapping
) {
716 .range_start
._signed
= range_start
,
717 .range_end
._signed
= range_end
, .string
= mapping_name
};
718 g_ptr_array_add(enumeration
->entries
, mapping
);
719 g_ptr_array_sort(enumeration
->entries
,
720 (GCompareFunc
)compare_enumeration_mappings_signed
);
722 free(escaped_string
);
727 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
728 struct bt_ctf_field_type
*type
, const char *string
,
729 uint64_t range_start
, uint64_t range_end
)
733 struct enumeration_mapping
*mapping
;
734 struct bt_ctf_field_type_enumeration
*enumeration
;
735 struct range_overlap_query query
;
736 char *escaped_string
;
738 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
740 (range_end
< range_start
)) {
745 if (!string
|| strlen(string
) == 0) {
750 escaped_string
= g_strescape(string
, NULL
);
751 if (!escaped_string
) {
756 mapping_name
= g_quark_from_string(escaped_string
);
757 query
= (struct range_overlap_query
) {
758 .range_start
._unsigned
= range_start
,
759 .range_end
._unsigned
= range_end
,
760 .mapping_name
= mapping_name
,
762 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
765 /* Check that the range does not overlap with one already present */
766 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
768 if (query
.overlaps
) {
773 mapping
= g_new(struct enumeration_mapping
, 1);
779 *mapping
= (struct enumeration_mapping
) {
780 .range_start
._unsigned
= range_start
,
781 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
782 g_ptr_array_add(enumeration
->entries
, mapping
);
783 g_ptr_array_sort(enumeration
->entries
,
784 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
786 free(escaped_string
);
791 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
792 struct bt_ctf_field_type_enumeration
*enumeration_type
,
795 const char *name
= NULL
;
796 struct range_overlap_query query
=
797 (struct range_overlap_query
) {
798 .range_start
._unsigned
= value
,
799 .range_end
._unsigned
= value
,
802 g_ptr_array_foreach(enumeration_type
->entries
,
803 check_ranges_overlap_unsigned
,
805 if (!query
.overlaps
) {
809 name
= g_quark_to_string(query
.mapping_name
);
814 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
815 struct bt_ctf_field_type_enumeration
*enumeration_type
,
818 const char *name
= NULL
;
819 struct range_overlap_query query
=
820 (struct range_overlap_query
) {
821 .range_start
._signed
= value
,
822 .range_end
._signed
= value
,
825 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
827 if (!query
.overlaps
) {
831 name
= g_quark_to_string(query
.mapping_name
);
836 int bt_ctf_field_type_enumeration_get_mapping_count(
837 struct bt_ctf_field_type
*type
)
840 struct bt_ctf_field_type_enumeration
*enumeration
;
842 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
847 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
849 ret
= (int) enumeration
->entries
->len
;
855 struct enumeration_mapping
*get_enumeration_mapping(
856 struct bt_ctf_field_type
*type
, int index
)
858 struct enumeration_mapping
*mapping
= NULL
;
859 struct bt_ctf_field_type_enumeration
*enumeration
;
861 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
863 if (index
>= enumeration
->entries
->len
) {
867 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
872 int bt_ctf_field_type_enumeration_get_mapping(
873 struct bt_ctf_field_type
*type
, int index
,
874 const char **string
, int64_t *range_start
, int64_t *range_end
)
876 struct enumeration_mapping
*mapping
;
879 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
880 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
885 mapping
= get_enumeration_mapping(type
, index
);
891 *string
= g_quark_to_string(mapping
->string
);
892 *range_start
= mapping
->range_start
._signed
;
893 *range_end
= mapping
->range_end
._signed
;
898 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
899 struct bt_ctf_field_type
*type
, int index
,
900 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
902 struct enumeration_mapping
*mapping
;
905 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
906 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
911 mapping
= get_enumeration_mapping(type
, index
);
917 *string
= g_quark_to_string(mapping
->string
);
918 *range_start
= mapping
->range_start
._unsigned
;
919 *range_end
= mapping
->range_end
._unsigned
;
924 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
925 struct bt_ctf_field_type
*type
, const char *name
)
928 struct bt_ctf_field_type_enumeration
*enumeration
;
931 if (!type
|| !name
||
932 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
937 name_quark
= g_quark_try_string(name
);
943 enumeration
= container_of(type
,
944 struct bt_ctf_field_type_enumeration
, parent
);
945 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
946 struct enumeration_mapping
*mapping
=
947 get_enumeration_mapping(type
, i
);
949 if (mapping
->string
== name_quark
) {
960 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
961 struct bt_ctf_field_type
*type
, int64_t value
)
963 struct bt_ctf_field_type_enumeration
*enumeration
;
966 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
971 enumeration
= container_of(type
,
972 struct bt_ctf_field_type_enumeration
, parent
);
973 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
974 struct enumeration_mapping
*mapping
=
975 get_enumeration_mapping(type
, i
);
977 if (value
>= mapping
->range_start
._signed
&&
978 value
<= mapping
->range_end
._signed
) {
989 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
990 struct bt_ctf_field_type
*type
, uint64_t value
)
992 struct bt_ctf_field_type_enumeration
*enumeration
;
995 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1000 enumeration
= container_of(type
,
1001 struct bt_ctf_field_type_enumeration
, parent
);
1002 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1003 struct enumeration_mapping
*mapping
=
1004 get_enumeration_mapping(type
, i
);
1006 if (value
>= mapping
->range_start
._unsigned
&&
1007 value
<= mapping
->range_end
._unsigned
) {
1018 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1020 struct bt_ctf_field_type_floating_point
*floating_point
=
1021 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1023 if (!floating_point
) {
1027 floating_point
->declaration
.sign
= &floating_point
->sign
;
1028 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1029 floating_point
->declaration
.exp
= &floating_point
->exp
;
1030 floating_point
->sign
.len
= 1;
1031 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1032 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1033 floating_point
->declaration
.exp
->len
=
1034 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1035 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1036 floating_point
->sign
.p
.alignment
= 1;
1037 floating_point
->mantissa
.p
.alignment
= 1;
1038 floating_point
->exp
.p
.alignment
= 1;
1040 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1042 return floating_point
? &floating_point
->parent
: NULL
;
1045 int bt_ctf_field_type_floating_point_get_exponent_digits(
1046 struct bt_ctf_field_type
*type
)
1049 struct bt_ctf_field_type_floating_point
*floating_point
;
1051 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1056 floating_point
= container_of(type
,
1057 struct bt_ctf_field_type_floating_point
, parent
);
1058 ret
= (int) floating_point
->declaration
.exp
->len
;
1063 int bt_ctf_field_type_floating_point_set_exponent_digits(
1064 struct bt_ctf_field_type
*type
,
1065 unsigned int exponent_digits
)
1068 struct bt_ctf_field_type_floating_point
*floating_point
;
1070 if (!type
|| type
->frozen
||
1071 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1076 floating_point
= container_of(type
,
1077 struct bt_ctf_field_type_floating_point
, parent
);
1078 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1079 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1081 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1086 floating_point
->declaration
.exp
->len
= exponent_digits
;
1091 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1092 struct bt_ctf_field_type
*type
)
1095 struct bt_ctf_field_type_floating_point
*floating_point
;
1097 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1102 floating_point
= container_of(type
,
1103 struct bt_ctf_field_type_floating_point
, parent
);
1104 ret
= (int) floating_point
->mantissa
.len
+ 1;
1109 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1110 struct bt_ctf_field_type
*type
,
1111 unsigned int mantissa_digits
)
1114 struct bt_ctf_field_type_floating_point
*floating_point
;
1116 if (!type
|| type
->frozen
||
1117 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1122 floating_point
= container_of(type
,
1123 struct bt_ctf_field_type_floating_point
, parent
);
1125 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1126 (mantissa_digits
!= DBL_MANT_DIG
) &&
1127 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1132 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1137 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1139 struct bt_ctf_field_type_structure
*structure
=
1140 g_new0(struct bt_ctf_field_type_structure
, 1);
1146 structure
->parent
.declaration
= &structure
->declaration
.p
;
1147 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1148 structure
->fields
= g_ptr_array_new_with_free_func(
1149 (GDestroyNotify
)destroy_structure_field
);
1150 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1151 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1152 return &structure
->parent
;
1157 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1158 struct bt_ctf_field_type
*field_type
,
1159 const char *field_name
)
1162 struct bt_ctf_field_type_structure
*structure
;
1164 if (!type
|| !field_type
|| type
->frozen
||
1165 bt_ctf_validate_identifier(field_name
) ||
1166 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1167 bt_ctf_field_type_validate(field_type
)) {
1172 structure
= container_of(type
,
1173 struct bt_ctf_field_type_structure
, parent
);
1174 if (add_structure_field(structure
->fields
,
1175 structure
->field_name_to_index
, field_type
, field_name
)) {
1183 int bt_ctf_field_type_structure_get_field_count(
1184 struct bt_ctf_field_type
*type
)
1187 struct bt_ctf_field_type_structure
*structure
;
1189 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1194 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1196 ret
= (int) structure
->fields
->len
;
1201 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1202 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1205 struct bt_ctf_field_type_structure
*structure
;
1206 struct structure_field
*field
;
1209 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1214 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1216 if (index
>= structure
->fields
->len
) {
1221 field
= g_ptr_array_index(structure
->fields
, index
);
1223 *field_type
= field
->type
;
1224 bt_ctf_field_type_get(field
->type
);
1227 *field_name
= g_quark_to_string(field
->name
);
1233 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1234 struct bt_ctf_field_type
*type
,
1239 struct structure_field
*field
;
1240 struct bt_ctf_field_type_structure
*structure
;
1241 struct bt_ctf_field_type
*field_type
= NULL
;
1243 if (!type
|| !name
) {
1247 name_quark
= g_quark_try_string(name
);
1252 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1254 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1255 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1259 field
= structure
->fields
->pdata
[index
];
1260 field_type
= field
->type
;
1261 bt_ctf_field_type_get(field_type
);
1266 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1267 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1269 struct bt_ctf_field_type_variant
*variant
= NULL
;
1271 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1275 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1280 variant
->parent
.declaration
= &variant
->declaration
.p
;
1281 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1282 variant
->tag_name
= g_string_new(tag_name
);
1283 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1284 variant
->fields
= g_ptr_array_new_with_free_func(
1285 (GDestroyNotify
)destroy_structure_field
);
1287 bt_ctf_field_type_get(enum_tag
);
1288 variant
->tag
= container_of(enum_tag
,
1289 struct bt_ctf_field_type_enumeration
, parent
);
1292 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1293 /* A variant's alignment is undefined */
1294 variant
->parent
.declaration
->alignment
= 0;
1295 return &variant
->parent
;
1300 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1301 struct bt_ctf_field_type
*type
)
1303 struct bt_ctf_field_type_variant
*variant
;
1304 struct bt_ctf_field_type
*tag_type
= NULL
;
1306 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1310 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1311 if (!variant
->tag
) {
1315 tag_type
= &variant
->tag
->parent
;
1316 bt_ctf_field_type_get(tag_type
);
1321 const char *bt_ctf_field_type_variant_get_tag_name(
1322 struct bt_ctf_field_type
*type
)
1324 struct bt_ctf_field_type_variant
*variant
;
1325 const char *tag_name
= NULL
;
1327 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1331 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1332 if (variant
->tag_name
->len
== 0) {
1336 tag_name
= variant
->tag_name
->str
;
1341 int bt_ctf_field_type_variant_set_tag_name(
1342 struct bt_ctf_field_type
*type
, const char *name
)
1345 struct bt_ctf_field_type_variant
*variant
;
1347 if (!type
|| type
->frozen
||
1348 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1349 bt_ctf_validate_identifier(name
)) {
1354 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1355 g_string_assign(variant
->tag_name
, name
);
1360 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1361 struct bt_ctf_field_type
*field_type
,
1362 const char *field_name
)
1366 struct bt_ctf_field_type_variant
*variant
;
1367 GQuark field_name_quark
= g_quark_from_string(field_name
);
1369 if (!type
|| !field_type
|| type
->frozen
||
1370 bt_ctf_validate_identifier(field_name
) ||
1371 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1372 bt_ctf_field_type_validate(field_type
)) {
1377 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1379 /* The user has explicitly provided a tag; validate against it. */
1383 /* Make sure this name is present in the enum tag */
1384 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1385 struct enumeration_mapping
*mapping
=
1386 g_ptr_array_index(variant
->tag
->entries
, i
);
1388 if (mapping
->string
== field_name_quark
) {
1395 /* Validation failed */
1401 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1402 field_type
, field_name
)) {
1410 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1411 struct bt_ctf_field_type
*type
,
1412 const char *field_name
)
1416 struct structure_field
*field
;
1417 struct bt_ctf_field_type_variant
*variant
;
1418 struct bt_ctf_field_type
*field_type
= NULL
;
1420 if (!type
|| !field_name
) {
1424 name_quark
= g_quark_try_string(field_name
);
1429 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1430 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1431 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1435 field
= g_ptr_array_index(variant
->fields
, index
);
1436 field_type
= field
->type
;
1437 bt_ctf_field_type_get(field_type
);
1442 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1443 struct bt_ctf_field_type
*type
,
1444 struct bt_ctf_field
*tag
)
1446 const char *enum_value
;
1447 struct bt_ctf_field_type
*field_type
= NULL
;
1449 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1453 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1458 /* Already increments field_type's reference count */
1459 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1465 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1468 struct bt_ctf_field_type_variant
*variant
;
1470 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1475 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1477 ret
= (int) variant
->fields
->len
;
1483 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1484 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1487 struct bt_ctf_field_type_variant
*variant
;
1488 struct structure_field
*field
;
1491 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1496 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1498 if (index
>= variant
->fields
->len
) {
1503 field
= g_ptr_array_index(variant
->fields
, index
);
1505 *field_type
= field
->type
;
1506 bt_ctf_field_type_get(field
->type
);
1509 *field_name
= g_quark_to_string(field
->name
);
1515 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1516 struct bt_ctf_field_type
*element_type
,
1517 unsigned int length
)
1519 struct bt_ctf_field_type_array
*array
= NULL
;
1521 if (!element_type
|| length
== 0 ||
1522 bt_ctf_field_type_validate(element_type
)) {
1526 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1531 array
->parent
.declaration
= &array
->declaration
.p
;
1532 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1534 bt_ctf_field_type_get(element_type
);
1535 array
->element_type
= element_type
;
1536 array
->length
= length
;
1537 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1538 return &array
->parent
;
1543 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1544 struct bt_ctf_field_type
*type
)
1546 struct bt_ctf_field_type
*ret
= NULL
;
1547 struct bt_ctf_field_type_array
*array
;
1549 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1553 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1554 ret
= array
->element_type
;
1555 bt_ctf_field_type_get(ret
);
1560 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1563 struct bt_ctf_field_type_array
*array
;
1565 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1570 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1571 ret
= (int64_t) array
->length
;
1576 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1577 struct bt_ctf_field_type
*element_type
,
1578 const char *length_field_name
)
1580 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1582 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1583 bt_ctf_field_type_validate(element_type
)) {
1587 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1592 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1593 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1594 bt_ctf_field_type_get(element_type
);
1595 sequence
->element_type
= element_type
;
1596 sequence
->length_field_name
= g_string_new(length_field_name
);
1597 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1598 return &sequence
->parent
;
1603 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1604 struct bt_ctf_field_type
*type
)
1606 struct bt_ctf_field_type
*ret
= NULL
;
1607 struct bt_ctf_field_type_sequence
*sequence
;
1609 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1613 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1615 ret
= sequence
->element_type
;
1616 bt_ctf_field_type_get(ret
);
1621 const char *bt_ctf_field_type_sequence_get_length_field_name(
1622 struct bt_ctf_field_type
*type
)
1624 const char *ret
= NULL
;
1625 struct bt_ctf_field_type_sequence
*sequence
;
1627 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1631 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1633 ret
= sequence
->length_field_name
->str
;
1638 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1640 struct bt_ctf_field_type_string
*string
=
1641 g_new0(struct bt_ctf_field_type_string
, 1);
1647 string
->parent
.declaration
= &string
->declaration
.p
;
1648 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1649 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1650 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1651 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1652 return &string
->parent
;
1655 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1656 struct bt_ctf_field_type
*type
)
1658 struct bt_ctf_field_type_string
*string
;
1659 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1661 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1665 string
= container_of(type
, struct bt_ctf_field_type_string
,
1667 ret
= string
->declaration
.encoding
;
1672 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1673 enum ctf_string_encoding encoding
)
1676 struct bt_ctf_field_type_string
*string
;
1678 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1679 (encoding
!= CTF_STRING_UTF8
&&
1680 encoding
!= CTF_STRING_ASCII
)) {
1685 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1686 string
->declaration
.encoding
= encoding
;
1691 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1694 enum ctf_type_id type_id
;
1702 ret
= (int) type
->declaration
->alignment
;
1706 type_id
= bt_ctf_field_type_get_type_id(type
);
1708 case CTF_TYPE_SEQUENCE
:
1710 struct bt_ctf_field_type
*element
=
1711 bt_ctf_field_type_sequence_get_element_type(type
);
1718 ret
= bt_ctf_field_type_get_alignment(element
);
1719 bt_ctf_field_type_put(element
);
1722 case CTF_TYPE_ARRAY
:
1724 struct bt_ctf_field_type
*element
=
1725 bt_ctf_field_type_array_get_element_type(type
);
1732 ret
= bt_ctf_field_type_get_alignment(element
);
1733 bt_ctf_field_type_put(element
);
1736 case CTF_TYPE_STRUCT
:
1738 int i
, element_count
;
1740 element_count
= bt_ctf_field_type_structure_get_field_count(
1742 if (element_count
< 0) {
1743 ret
= element_count
;
1747 for (i
= 0; i
< element_count
; i
++) {
1748 struct bt_ctf_field_type
*field
;
1749 int field_alignment
;
1751 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
1758 field_alignment
= bt_ctf_field_type_get_alignment(
1760 bt_ctf_field_type_put(field
);
1761 if (field_alignment
< 0) {
1762 ret
= field_alignment
;
1766 type
->declaration
->alignment
= MAX(field_alignment
,
1767 type
->declaration
->alignment
);
1769 ret
= (int) type
->declaration
->alignment
;
1772 case CTF_TYPE_UNKNOWN
:
1776 ret
= (int) type
->declaration
->alignment
;
1784 int is_power_of_two(unsigned int value
)
1786 return ((value
& (value
- 1)) == 0) && value
> 0;
1789 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1790 unsigned int alignment
)
1793 enum ctf_type_id type_id
;
1795 /* Alignment must be a power of two */
1796 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
1801 type_id
= bt_ctf_field_type_get_type_id(type
);
1802 if (type_id
== CTF_TYPE_UNKNOWN
) {
1807 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1808 alignment
!= CHAR_BIT
) {
1813 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
1814 type_id
== CTF_TYPE_ARRAY
) {
1815 /* Setting an alignment on these types makes no sense */
1820 type
->declaration
->alignment
= alignment
;
1826 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1827 struct bt_ctf_field_type
*type
)
1829 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1830 int internal_byte_order
= -1;
1836 switch (type
->declaration
->id
) {
1837 case CTF_TYPE_INTEGER
:
1839 struct bt_ctf_field_type_integer
*integer
= container_of(
1840 type
, struct bt_ctf_field_type_integer
, parent
);
1841 internal_byte_order
= integer
->declaration
.byte_order
;
1844 case CTF_TYPE_FLOAT
:
1846 struct bt_ctf_field_type_floating_point
*floating_point
=
1848 struct bt_ctf_field_type_floating_point
,
1850 internal_byte_order
= floating_point
->declaration
.byte_order
;
1857 switch (internal_byte_order
) {
1859 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1862 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1865 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1868 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1874 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1875 enum bt_ctf_byte_order byte_order
)
1878 int internal_byte_order
;
1879 enum ctf_type_id type_id
;
1881 if (!type
|| type
->frozen
) {
1886 switch (byte_order
) {
1887 case BT_CTF_BYTE_ORDER_NATIVE
:
1888 /* Leave unset. Will be initialized by parent. */
1889 internal_byte_order
= 0;
1891 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1892 internal_byte_order
= LITTLE_ENDIAN
;
1894 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1895 case BT_CTF_BYTE_ORDER_NETWORK
:
1896 internal_byte_order
= BIG_ENDIAN
;
1903 type_id
= type
->declaration
->id
;
1904 if (set_byte_order_funcs
[type_id
]) {
1905 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1911 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1912 struct bt_ctf_field_type
*type
)
1915 return CTF_TYPE_UNKNOWN
;
1918 return type
->declaration
->id
;
1921 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1927 bt_ctf_ref_get(&type
->ref_count
);
1930 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1936 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1940 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1950 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1951 struct bt_ctf_field_type_variant
*variant
,
1954 struct bt_ctf_field_type
*type
= NULL
;
1955 GQuark field_name_quark
;
1957 struct structure_field
*field_entry
;
1958 struct range_overlap_query query
= {
1959 .range_start
._signed
= tag_value
,
1960 .range_end
._signed
= tag_value
,
1961 .mapping_name
= 0, .overlaps
= 0};
1963 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1965 if (!query
.overlaps
) {
1969 field_name_quark
= query
.mapping_name
;
1970 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1971 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1975 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1976 type
= field_entry
->type
;
1982 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1983 struct bt_ctf_field_type_variant
*variant
,
1986 struct bt_ctf_field_type
*type
= NULL
;
1987 GQuark field_name_quark
;
1989 struct structure_field
*field_entry
;
1990 struct range_overlap_query query
= {
1991 .range_start
._unsigned
= tag_value
,
1992 .range_end
._unsigned
= tag_value
,
1993 .mapping_name
= 0, .overlaps
= 0};
1995 g_ptr_array_foreach(variant
->tag
->entries
,
1996 check_ranges_overlap_unsigned
,
1998 if (!query
.overlaps
) {
2002 field_name_quark
= query
.mapping_name
;
2003 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2004 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2008 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2009 type
= field_entry
->type
;
2015 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2016 struct metadata_context
*context
)
2020 if (!type
|| !context
) {
2025 ret
= type
->serialize(type
, context
);
2031 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2038 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2039 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2040 set_byte_order_funcs
[type
->declaration
->id
](type
,
2046 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2048 struct bt_ctf_field_type
*copy
= NULL
;
2054 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2060 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2062 struct bt_ctf_field_path
*field_path
= NULL
;
2064 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2069 field_path
->root
= CTF_NODE_UNKNOWN
;
2070 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2071 if (!field_path
->path_indexes
) {
2072 bt_ctf_field_path_destroy(field_path
);
2081 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2082 struct bt_ctf_field_path
*path
)
2084 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2090 new_path
->root
= path
->root
;
2091 g_array_insert_vals(new_path
->path_indexes
, 0,
2092 path
->path_indexes
->data
, path
->path_indexes
->len
);
2098 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2104 if (path
->path_indexes
) {
2105 g_array_free(path
->path_indexes
, TRUE
);
2111 int bt_ctf_field_type_structure_get_field_name_index(
2112 struct bt_ctf_field_type
*type
, const char *name
)
2117 struct bt_ctf_field_type_structure
*structure
;
2119 if (!type
|| !name
||
2120 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2125 name_quark
= g_quark_try_string(name
);
2131 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2133 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2134 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2144 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2145 struct bt_ctf_field_type
*field
, int index
)
2148 struct bt_ctf_field_type_structure
*structure
;
2150 if (!type
|| !field
||
2151 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2156 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2158 if (index
< 0 || index
>= structure
->fields
->len
) {
2163 bt_ctf_field_type_get(field
);
2164 bt_ctf_field_type_put(((struct structure_field
*)
2165 g_ptr_array_index(structure
->fields
, index
))->type
);
2166 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2173 int bt_ctf_field_type_variant_get_field_name_index(
2174 struct bt_ctf_field_type
*type
, const char *name
)
2179 struct bt_ctf_field_type_variant
*variant
;
2181 if (!type
|| !name
||
2182 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2187 name_quark
= g_quark_try_string(name
);
2193 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2195 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2196 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2206 int bt_ctf_field_type_sequence_set_length_field_path(
2207 struct bt_ctf_field_type
*type
,
2208 struct bt_ctf_field_path
*path
)
2211 struct bt_ctf_field_type_sequence
*sequence
;
2213 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2218 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2220 if (sequence
->length_field_path
) {
2221 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2223 sequence
->length_field_path
= path
;
2229 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2230 struct bt_ctf_field_path
*path
)
2233 struct bt_ctf_field_type_variant
*variant
;
2235 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2240 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2242 if (variant
->tag_path
) {
2243 bt_ctf_field_path_destroy(variant
->tag_path
);
2245 variant
->tag_path
= path
;
2251 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2252 struct bt_ctf_field_type
*tag
)
2255 struct bt_ctf_field_type_variant
*variant
;
2257 if (!type
|| !tag
|| type
->frozen
||
2258 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2263 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2265 bt_ctf_field_type_get(tag
);
2267 bt_ctf_field_type_put(&variant
->tag
->parent
);
2269 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2276 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2277 struct bt_ctf_field_type
*field
, int index
)
2280 struct bt_ctf_field_type_variant
*variant
;
2282 if (!type
|| !field
||
2283 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2288 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2290 if (index
< 0 || index
>= variant
->fields
->len
) {
2295 bt_ctf_field_type_get(field
);
2296 bt_ctf_field_type_put(((struct structure_field
*)
2297 g_ptr_array_index(variant
->fields
, index
))->type
);
2298 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2305 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
2307 struct bt_ctf_field_type_integer
*integer
;
2313 integer
= container_of(
2314 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2315 struct bt_ctf_field_type_integer
, parent
);
2316 if (integer
->mapped_clock
) {
2317 bt_ctf_clock_put(integer
->mapped_clock
);
2323 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
2325 struct bt_ctf_field_type_enumeration
*enumeration
;
2331 enumeration
= container_of(
2332 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2333 struct bt_ctf_field_type_enumeration
, parent
);
2334 g_ptr_array_free(enumeration
->entries
, TRUE
);
2335 bt_ctf_field_type_put(enumeration
->container
);
2336 g_free(enumeration
);
2340 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
2342 struct bt_ctf_field_type_floating_point
*floating_point
;
2348 floating_point
= container_of(
2349 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2350 struct bt_ctf_field_type_floating_point
, parent
);
2351 g_free(floating_point
);
2355 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
2357 struct bt_ctf_field_type_structure
*structure
;
2363 structure
= container_of(
2364 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2365 struct bt_ctf_field_type_structure
, parent
);
2366 g_ptr_array_free(structure
->fields
, TRUE
);
2367 g_hash_table_destroy(structure
->field_name_to_index
);
2372 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
2374 struct bt_ctf_field_type_variant
*variant
;
2380 variant
= container_of(
2381 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2382 struct bt_ctf_field_type_variant
, parent
);
2383 g_ptr_array_free(variant
->fields
, TRUE
);
2384 g_hash_table_destroy(variant
->field_name_to_index
);
2385 g_string_free(variant
->tag_name
, TRUE
);
2386 bt_ctf_field_type_put(&variant
->tag
->parent
);
2387 bt_ctf_field_path_destroy(variant
->tag_path
);
2392 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2394 struct bt_ctf_field_type_array
*array
;
2400 array
= container_of(
2401 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2402 struct bt_ctf_field_type_array
, parent
);
2403 bt_ctf_field_type_put(array
->element_type
);
2408 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2410 struct bt_ctf_field_type_sequence
*sequence
;
2416 sequence
= container_of(
2417 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2418 struct bt_ctf_field_type_sequence
, parent
);
2419 bt_ctf_field_type_put(sequence
->element_type
);
2420 g_string_free(sequence
->length_field_name
, TRUE
);
2421 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2426 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2428 struct bt_ctf_field_type_string
*string
;
2434 string
= container_of(
2435 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2436 struct bt_ctf_field_type_string
, parent
);
2441 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2447 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2449 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2450 type
, struct bt_ctf_field_type_enumeration
, parent
);
2452 generic_field_type_freeze(type
);
2453 bt_ctf_field_type_freeze(enumeration_type
->container
);
2457 void freeze_structure_field(struct structure_field
*field
)
2459 bt_ctf_field_type_freeze(field
->type
);
2463 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2465 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2466 type
, struct bt_ctf_field_type_structure
, parent
);
2468 /* Cache the alignment */
2469 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2470 generic_field_type_freeze(type
);
2471 g_ptr_array_foreach(structure_type
->fields
,
2472 (GFunc
) freeze_structure_field
, NULL
);
2476 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2478 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2479 type
, struct bt_ctf_field_type_variant
, parent
);
2481 /* Cache the alignment */
2482 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2483 generic_field_type_freeze(type
);
2484 g_ptr_array_foreach(variant_type
->fields
,
2485 (GFunc
) freeze_structure_field
, NULL
);
2489 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2491 struct bt_ctf_field_type_array
*array_type
= container_of(
2492 type
, struct bt_ctf_field_type_array
, parent
);
2494 /* Cache the alignment */
2495 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2496 generic_field_type_freeze(type
);
2497 bt_ctf_field_type_freeze(array_type
->element_type
);
2501 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2503 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2504 type
, struct bt_ctf_field_type_sequence
, parent
);
2506 /* Cache the alignment */
2507 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2508 generic_field_type_freeze(type
);
2509 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2513 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2515 const char *encoding_string
;
2518 case CTF_STRING_NONE
:
2519 encoding_string
= "none";
2521 case CTF_STRING_ASCII
:
2522 encoding_string
= "ASCII";
2524 case CTF_STRING_UTF8
:
2525 encoding_string
= "UTF8";
2528 encoding_string
= "unknown";
2532 return encoding_string
;
2536 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2538 const char *base_string
;
2541 case BT_CTF_INTEGER_BASE_DECIMAL
:
2542 base_string
= "decimal";
2544 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2545 base_string
= "hexadecimal";
2547 case BT_CTF_INTEGER_BASE_OCTAL
:
2548 base_string
= "octal";
2550 case BT_CTF_INTEGER_BASE_BINARY
:
2551 base_string
= "binary";
2554 base_string
= "unknown";
2562 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2563 struct metadata_context
*context
)
2565 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2566 struct bt_ctf_field_type_integer
, parent
);
2569 g_string_append_printf(context
->string
,
2570 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2571 integer
->declaration
.len
, type
->declaration
->alignment
,
2572 (integer
->declaration
.signedness
? "true" : "false"),
2573 get_encoding_string(integer
->declaration
.encoding
),
2574 get_integer_base_string(integer
->declaration
.base
),
2575 get_byte_order_string(integer
->declaration
.byte_order
));
2576 if (integer
->mapped_clock
) {
2577 const char *clock_name
= bt_ctf_clock_get_name(
2578 integer
->mapped_clock
);
2585 g_string_append_printf(context
->string
,
2586 "; map = clock.%s.value", clock_name
);
2589 g_string_append(context
->string
, "; }");
2595 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2596 struct metadata_context
*context
)
2600 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2601 struct bt_ctf_field_type_enumeration
, parent
);
2602 struct bt_ctf_field_type
*container_type
;
2603 int container_signed
;
2605 ret
= bt_ctf_field_type_validate(type
);
2610 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2611 if (!container_type
) {
2616 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2617 if (container_signed
< 0) {
2618 ret
= container_signed
;
2619 goto error_put_container_type
;
2622 g_string_append(context
->string
, "enum : ");
2623 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2625 goto error_put_container_type
;
2628 g_string_append(context
->string
, " { ");
2629 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2630 struct enumeration_mapping
*mapping
=
2631 enumeration
->entries
->pdata
[entry
];
2633 if (container_signed
) {
2634 if (mapping
->range_start
._signed
==
2635 mapping
->range_end
._signed
) {
2636 g_string_append_printf(context
->string
,
2637 "\"%s\" = %" PRId64
,
2638 g_quark_to_string(mapping
->string
),
2639 mapping
->range_start
._signed
);
2641 g_string_append_printf(context
->string
,
2642 "\"%s\" = %" PRId64
" ... %" PRId64
,
2643 g_quark_to_string(mapping
->string
),
2644 mapping
->range_start
._signed
,
2645 mapping
->range_end
._signed
);
2648 if (mapping
->range_start
._unsigned
==
2649 mapping
->range_end
._unsigned
) {
2650 g_string_append_printf(context
->string
,
2651 "\"%s\" = %" PRIu64
,
2652 g_quark_to_string(mapping
->string
),
2653 mapping
->range_start
._unsigned
);
2655 g_string_append_printf(context
->string
,
2656 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2657 g_quark_to_string(mapping
->string
),
2658 mapping
->range_start
._unsigned
,
2659 mapping
->range_end
._unsigned
);
2663 g_string_append(context
->string
,
2664 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2668 if (context
->field_name
->len
) {
2669 g_string_append_printf(context
->string
, " %s",
2670 context
->field_name
->str
);
2671 g_string_assign(context
->field_name
, "");
2673 error_put_container_type
:
2674 bt_ctf_field_type_put(container_type
);
2680 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2681 struct metadata_context
*context
)
2683 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2684 type
, struct bt_ctf_field_type_floating_point
, parent
);
2686 g_string_append_printf(context
->string
,
2687 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2688 floating_point
->declaration
.exp
->len
,
2689 floating_point
->declaration
.mantissa
->len
+ 1,
2690 get_byte_order_string(floating_point
->declaration
.byte_order
),
2691 type
->declaration
->alignment
);
2696 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2697 struct metadata_context
*context
)
2700 unsigned int indent
;
2702 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2703 struct bt_ctf_field_type_structure
, parent
);
2704 GString
*structure_field_name
= context
->field_name
;
2706 context
->field_name
= g_string_new("");
2708 context
->current_indentation_level
++;
2709 g_string_append(context
->string
, "struct {\n");
2711 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2712 struct structure_field
*field
;
2714 for (indent
= 0; indent
< context
->current_indentation_level
;
2716 g_string_append_c(context
->string
, '\t');
2719 field
= structure
->fields
->pdata
[i
];
2720 g_string_assign(context
->field_name
,
2721 g_quark_to_string(field
->name
));
2722 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2727 if (context
->field_name
->len
) {
2728 g_string_append_printf(context
->string
, " %s",
2729 context
->field_name
->str
);
2731 g_string_append(context
->string
, ";\n");
2734 context
->current_indentation_level
--;
2735 for (indent
= 0; indent
< context
->current_indentation_level
;
2737 g_string_append_c(context
->string
, '\t');
2740 g_string_append_printf(context
->string
, "} align(%zu)",
2741 type
->declaration
->alignment
);
2743 g_string_free(context
->field_name
, TRUE
);
2744 context
->field_name
= structure_field_name
;
2749 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2750 struct metadata_context
*context
)
2753 unsigned int indent
;
2755 struct bt_ctf_field_type_variant
*variant
= container_of(
2756 type
, struct bt_ctf_field_type_variant
, parent
);
2757 GString
*variant_field_name
= context
->field_name
;
2759 context
->field_name
= g_string_new("");
2760 if (variant
->tag_name
->len
> 0) {
2761 g_string_append_printf(context
->string
,
2762 "variant <%s> {\n", variant
->tag_name
->str
);
2764 g_string_append(context
->string
, "variant {\n");
2767 context
->current_indentation_level
++;
2768 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2769 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2771 g_string_assign(context
->field_name
,
2772 g_quark_to_string(field
->name
));
2773 for (indent
= 0; indent
< context
->current_indentation_level
;
2775 g_string_append_c(context
->string
, '\t');
2778 g_string_assign(context
->field_name
,
2779 g_quark_to_string(field
->name
));
2780 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2785 if (context
->field_name
->len
) {
2786 g_string_append_printf(context
->string
, " %s;",
2787 context
->field_name
->str
);
2790 g_string_append_c(context
->string
, '\n');
2793 context
->current_indentation_level
--;
2794 for (indent
= 0; indent
< context
->current_indentation_level
;
2796 g_string_append_c(context
->string
, '\t');
2799 g_string_append(context
->string
, "}");
2801 g_string_free(context
->field_name
, TRUE
);
2802 context
->field_name
= variant_field_name
;
2807 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2808 struct metadata_context
*context
)
2811 struct bt_ctf_field_type_array
*array
= container_of(type
,
2812 struct bt_ctf_field_type_array
, parent
);
2814 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2819 if (context
->field_name
->len
) {
2820 g_string_append_printf(context
->string
, " %s[%u]",
2821 context
->field_name
->str
, array
->length
);
2822 g_string_assign(context
->field_name
, "");
2824 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2831 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2832 struct metadata_context
*context
)
2835 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2836 type
, struct bt_ctf_field_type_sequence
, parent
);
2838 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2843 if (context
->field_name
->len
) {
2844 g_string_append_printf(context
->string
, " %s[%s]",
2845 context
->field_name
->str
,
2846 sequence
->length_field_name
->str
);
2847 g_string_assign(context
->field_name
, "");
2849 g_string_append_printf(context
->string
, "[%s]",
2850 sequence
->length_field_name
->str
);
2857 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2858 struct metadata_context
*context
)
2860 struct bt_ctf_field_type_string
*string
= container_of(
2861 type
, struct bt_ctf_field_type_string
, parent
);
2863 g_string_append_printf(context
->string
,
2864 "string { encoding = %s; }",
2865 get_encoding_string(string
->declaration
.encoding
));
2870 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2871 int byte_order
, int set_native
)
2873 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2874 struct bt_ctf_field_type_integer
, parent
);
2877 integer_type
->declaration
.byte_order
=
2878 integer_type
->declaration
.byte_order
== 0 ?
2879 byte_order
: integer_type
->declaration
.byte_order
;
2881 integer_type
->declaration
.byte_order
= byte_order
;
2886 void bt_ctf_field_type_enumeration_set_byte_order(
2887 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2889 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2890 struct bt_ctf_field_type_enumeration
, parent
);
2892 /* Safe to assume that container is an integer */
2893 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2894 byte_order
, set_native
);
2898 void bt_ctf_field_type_floating_point_set_byte_order(
2899 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2901 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2902 container_of(type
, struct bt_ctf_field_type_floating_point
,
2906 floating_point_type
->declaration
.byte_order
=
2907 floating_point_type
->declaration
.byte_order
== 0 ?
2909 floating_point_type
->declaration
.byte_order
;
2910 floating_point_type
->sign
.byte_order
=
2911 floating_point_type
->sign
.byte_order
== 0 ?
2912 byte_order
: floating_point_type
->sign
.byte_order
;
2913 floating_point_type
->mantissa
.byte_order
=
2914 floating_point_type
->mantissa
.byte_order
== 0 ?
2915 byte_order
: floating_point_type
->mantissa
.byte_order
;
2916 floating_point_type
->exp
.byte_order
=
2917 floating_point_type
->exp
.byte_order
== 0 ?
2918 byte_order
: floating_point_type
->exp
.byte_order
;
2920 floating_point_type
->declaration
.byte_order
= byte_order
;
2921 floating_point_type
->sign
.byte_order
= byte_order
;
2922 floating_point_type
->mantissa
.byte_order
= byte_order
;
2923 floating_point_type
->exp
.byte_order
= byte_order
;
2928 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2929 int byte_order
, int set_native
)
2932 struct bt_ctf_field_type_structure
*structure_type
=
2933 container_of(type
, struct bt_ctf_field_type_structure
,
2936 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2937 struct structure_field
*field
= g_ptr_array_index(
2938 structure_type
->fields
, i
);
2939 struct bt_ctf_field_type
*field_type
= field
->type
;
2941 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2942 set_byte_order_funcs
[field_type
->declaration
->id
](
2943 field_type
, byte_order
, set_native
);
2949 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2950 int byte_order
, int set_native
)
2953 struct bt_ctf_field_type_variant
*variant_type
=
2954 container_of(type
, struct bt_ctf_field_type_variant
,
2957 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2958 struct structure_field
*field
= g_ptr_array_index(
2959 variant_type
->fields
, i
);
2960 struct bt_ctf_field_type
*field_type
= field
->type
;
2962 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2963 set_byte_order_funcs
[field_type
->declaration
->id
](
2964 field_type
, byte_order
, set_native
);
2970 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2971 int byte_order
, int set_native
)
2973 struct bt_ctf_field_type_array
*array_type
=
2974 container_of(type
, struct bt_ctf_field_type_array
,
2977 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2978 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2979 array_type
->element_type
, byte_order
, set_native
);
2984 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2985 int byte_order
, int set_native
)
2987 struct bt_ctf_field_type_sequence
*sequence_type
=
2988 container_of(type
, struct bt_ctf_field_type_sequence
,
2991 if (set_byte_order_funcs
[
2992 sequence_type
->element_type
->declaration
->id
]) {
2993 set_byte_order_funcs
[
2994 sequence_type
->element_type
->declaration
->id
](
2995 sequence_type
->element_type
, byte_order
, set_native
);
3000 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3001 struct bt_ctf_field_type
*type
)
3003 struct bt_ctf_field_type
*copy
;
3004 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3006 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3007 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3012 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3014 copy_integer
->declaration
= integer
->declaration
;
3015 if (integer
->mapped_clock
) {
3016 bt_ctf_clock_get(integer
->mapped_clock
);
3017 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3024 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3025 struct bt_ctf_field_type
*type
)
3028 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3029 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3031 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3034 /* Copy the source enumeration's container */
3035 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3036 if (!copy_container
) {
3040 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3044 copy_enumeration
= container_of(copy
,
3045 struct bt_ctf_field_type_enumeration
, parent
);
3047 /* Copy all enumaration entries */
3048 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3049 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3050 enumeration
->entries
, i
);
3051 struct enumeration_mapping
* copy_mapping
= g_new0(
3052 struct enumeration_mapping
, 1);
3054 if (!copy_mapping
) {
3058 *copy_mapping
= *mapping
;
3059 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3062 copy_enumeration
->declaration
= enumeration
->declaration
;
3064 if (copy_container
) {
3065 bt_ctf_field_type_put(copy_container
);
3069 if (copy_container
) {
3070 bt_ctf_field_type_put(copy_container
);
3072 bt_ctf_field_type_put(copy
);
3077 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3078 struct bt_ctf_field_type
*type
)
3080 struct bt_ctf_field_type
*copy
;
3081 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3083 floating_point
= container_of(type
,
3084 struct bt_ctf_field_type_floating_point
, parent
);
3085 copy
= bt_ctf_field_type_floating_point_create();
3090 copy_float
= container_of(copy
,
3091 struct bt_ctf_field_type_floating_point
, parent
);
3092 copy_float
->declaration
= floating_point
->declaration
;
3093 copy_float
->sign
= floating_point
->sign
;
3094 copy_float
->mantissa
= floating_point
->mantissa
;
3095 copy_float
->exp
= floating_point
->exp
;
3101 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3102 struct bt_ctf_field_type
*type
)
3105 GHashTableIter iter
;
3106 gpointer key
, value
;
3107 struct bt_ctf_field_type
*copy
;
3108 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3110 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3112 copy
= bt_ctf_field_type_structure_create();
3117 copy_structure
= container_of(copy
,
3118 struct bt_ctf_field_type_structure
, parent
);
3120 /* Copy field_name_to_index */
3121 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3122 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3123 g_hash_table_insert(copy_structure
->field_name_to_index
,
3127 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3128 struct structure_field
*entry
, *copy_entry
;
3129 struct bt_ctf_field_type
*copy_field
;
3131 copy_entry
= g_new0(struct structure_field
, 1);
3136 entry
= g_ptr_array_index(structure
->fields
, i
);
3137 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3143 copy_entry
->name
= entry
->name
;
3144 copy_entry
->type
= copy_field
;
3145 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3148 copy_structure
->declaration
= structure
->declaration
;
3152 bt_ctf_field_type_put(copy
);
3157 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3158 struct bt_ctf_field_type
*type
)
3161 GHashTableIter iter
;
3162 gpointer key
, value
;
3163 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3164 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3166 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3169 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3175 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3176 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3181 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3184 /* Copy field_name_to_index */
3185 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3186 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3187 g_hash_table_insert(copy_variant
->field_name_to_index
,
3191 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3192 struct structure_field
*entry
, *copy_entry
;
3193 struct bt_ctf_field_type
*copy_field
;
3195 copy_entry
= g_new0(struct structure_field
, 1);
3200 entry
= g_ptr_array_index(variant
->fields
, i
);
3201 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3207 copy_entry
->name
= entry
->name
;
3208 copy_entry
->type
= copy_field
;
3209 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3212 copy_variant
->declaration
= variant
->declaration
;
3213 if (variant
->tag_path
) {
3214 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3216 if (!copy_variant
->tag_path
) {
3222 bt_ctf_field_type_put(copy_tag
);
3228 bt_ctf_field_type_put(copy_tag
);
3231 bt_ctf_field_type_put(copy
);
3236 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3237 struct bt_ctf_field_type
*type
)
3239 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3240 struct bt_ctf_field_type_array
*array
, *copy_array
;
3242 array
= container_of(type
, struct bt_ctf_field_type_array
,
3244 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3245 if (!copy_element
) {
3249 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3254 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3256 copy_array
->declaration
= array
->declaration
;
3259 bt_ctf_field_type_put(copy_element
);
3266 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3267 struct bt_ctf_field_type
*type
)
3269 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3270 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3272 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3274 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3275 if (!copy_element
) {
3279 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3280 sequence
->length_field_name
->len
?
3281 sequence
->length_field_name
->str
: NULL
);
3286 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3288 copy_sequence
->declaration
= sequence
->declaration
;
3289 if (sequence
->length_field_path
) {
3290 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3291 sequence
->length_field_path
);
3292 if (!copy_sequence
->length_field_path
) {
3298 bt_ctf_field_type_put(copy_element
);
3304 bt_ctf_field_type_put(copy
);
3311 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3312 struct bt_ctf_field_type
*type
)
3314 struct bt_ctf_field_type
*copy
;
3315 struct bt_ctf_field_type_string
*string
, *copy_string
;
3317 copy
= bt_ctf_field_type_string_create();
3322 string
= container_of(type
, struct bt_ctf_field_type_string
,
3324 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3326 copy_string
->declaration
= string
->declaration
;