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
;
1783 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1784 unsigned int alignment
)
1787 enum ctf_type_id type_id
;
1789 /* Alignment must be bit-aligned (1) or byte aligned */
1790 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1795 type_id
= bt_ctf_field_type_get_type_id(type
);
1796 if (type_id
== CTF_TYPE_UNKNOWN
) {
1801 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1802 alignment
!= CHAR_BIT
) {
1807 if (type_id
== CTF_TYPE_STRUCT
|| type_id
== CTF_TYPE_VARIANT
||
1808 type_id
== CTF_TYPE_SEQUENCE
|| type_id
== CTF_TYPE_ARRAY
) {
1809 /* Setting an alignment on these types makes no sense */
1814 type
->declaration
->alignment
= alignment
;
1820 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1821 struct bt_ctf_field_type
*type
)
1823 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1824 int internal_byte_order
= -1;
1830 switch (type
->declaration
->id
) {
1831 case CTF_TYPE_INTEGER
:
1833 struct bt_ctf_field_type_integer
*integer
= container_of(
1834 type
, struct bt_ctf_field_type_integer
, parent
);
1835 internal_byte_order
= integer
->declaration
.byte_order
;
1838 case CTF_TYPE_FLOAT
:
1840 struct bt_ctf_field_type_floating_point
*floating_point
=
1842 struct bt_ctf_field_type_floating_point
,
1844 internal_byte_order
= floating_point
->declaration
.byte_order
;
1851 switch (internal_byte_order
) {
1853 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1856 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1859 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1862 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1868 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1869 enum bt_ctf_byte_order byte_order
)
1872 int internal_byte_order
;
1873 enum ctf_type_id type_id
;
1875 if (!type
|| type
->frozen
) {
1880 switch (byte_order
) {
1881 case BT_CTF_BYTE_ORDER_NATIVE
:
1882 /* Leave unset. Will be initialized by parent. */
1883 internal_byte_order
= 0;
1885 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1886 internal_byte_order
= LITTLE_ENDIAN
;
1888 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1889 case BT_CTF_BYTE_ORDER_NETWORK
:
1890 internal_byte_order
= BIG_ENDIAN
;
1897 type_id
= type
->declaration
->id
;
1898 if (set_byte_order_funcs
[type_id
]) {
1899 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1905 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1906 struct bt_ctf_field_type
*type
)
1909 return CTF_TYPE_UNKNOWN
;
1912 return type
->declaration
->id
;
1915 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1921 bt_ctf_ref_get(&type
->ref_count
);
1924 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1930 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1934 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1944 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1945 struct bt_ctf_field_type_variant
*variant
,
1948 struct bt_ctf_field_type
*type
= NULL
;
1949 GQuark field_name_quark
;
1951 struct structure_field
*field_entry
;
1952 struct range_overlap_query query
= {
1953 .range_start
._signed
= tag_value
,
1954 .range_end
._signed
= tag_value
,
1955 .mapping_name
= 0, .overlaps
= 0};
1957 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1959 if (!query
.overlaps
) {
1963 field_name_quark
= query
.mapping_name
;
1964 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1965 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1969 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1970 type
= field_entry
->type
;
1976 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1977 struct bt_ctf_field_type_variant
*variant
,
1980 struct bt_ctf_field_type
*type
= NULL
;
1981 GQuark field_name_quark
;
1983 struct structure_field
*field_entry
;
1984 struct range_overlap_query query
= {
1985 .range_start
._unsigned
= tag_value
,
1986 .range_end
._unsigned
= tag_value
,
1987 .mapping_name
= 0, .overlaps
= 0};
1989 g_ptr_array_foreach(variant
->tag
->entries
,
1990 check_ranges_overlap_unsigned
,
1992 if (!query
.overlaps
) {
1996 field_name_quark
= query
.mapping_name
;
1997 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1998 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2002 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2003 type
= field_entry
->type
;
2009 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2010 struct metadata_context
*context
)
2014 if (!type
|| !context
) {
2019 ret
= type
->serialize(type
, context
);
2025 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2032 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2033 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2034 set_byte_order_funcs
[type
->declaration
->id
](type
,
2040 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2042 struct bt_ctf_field_type
*copy
= NULL
;
2048 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2054 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2056 struct bt_ctf_field_path
*field_path
= NULL
;
2058 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2063 field_path
->root
= CTF_NODE_UNKNOWN
;
2064 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2065 if (!field_path
->path_indexes
) {
2066 bt_ctf_field_path_destroy(field_path
);
2075 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2076 struct bt_ctf_field_path
*path
)
2078 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2084 new_path
->root
= path
->root
;
2085 g_array_insert_vals(new_path
->path_indexes
, 0,
2086 path
->path_indexes
->data
, path
->path_indexes
->len
);
2092 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2098 if (path
->path_indexes
) {
2099 g_array_free(path
->path_indexes
, TRUE
);
2105 int bt_ctf_field_type_structure_get_field_name_index(
2106 struct bt_ctf_field_type
*type
, const char *name
)
2111 struct bt_ctf_field_type_structure
*structure
;
2113 if (!type
|| !name
||
2114 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2119 name_quark
= g_quark_try_string(name
);
2125 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2127 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2128 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2138 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2139 struct bt_ctf_field_type
*field
, int index
)
2142 struct bt_ctf_field_type_structure
*structure
;
2144 if (!type
|| !field
||
2145 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2150 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2152 if (index
< 0 || index
>= structure
->fields
->len
) {
2157 bt_ctf_field_type_get(field
);
2158 bt_ctf_field_type_put(((struct structure_field
*)
2159 g_ptr_array_index(structure
->fields
, index
))->type
);
2160 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2167 int bt_ctf_field_type_variant_get_field_name_index(
2168 struct bt_ctf_field_type
*type
, const char *name
)
2173 struct bt_ctf_field_type_variant
*variant
;
2175 if (!type
|| !name
||
2176 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2181 name_quark
= g_quark_try_string(name
);
2187 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2189 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2190 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2200 int bt_ctf_field_type_sequence_set_length_field_path(
2201 struct bt_ctf_field_type
*type
,
2202 struct bt_ctf_field_path
*path
)
2205 struct bt_ctf_field_type_sequence
*sequence
;
2207 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2212 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2214 if (sequence
->length_field_path
) {
2215 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2217 sequence
->length_field_path
= path
;
2223 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2224 struct bt_ctf_field_path
*path
)
2227 struct bt_ctf_field_type_variant
*variant
;
2229 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2234 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2236 if (variant
->tag_path
) {
2237 bt_ctf_field_path_destroy(variant
->tag_path
);
2239 variant
->tag_path
= path
;
2245 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2246 struct bt_ctf_field_type
*tag
)
2249 struct bt_ctf_field_type_variant
*variant
;
2251 if (!type
|| !tag
|| type
->frozen
||
2252 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2257 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2259 bt_ctf_field_type_get(tag
);
2261 bt_ctf_field_type_put(&variant
->tag
->parent
);
2263 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2270 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2271 struct bt_ctf_field_type
*field
, int index
)
2274 struct bt_ctf_field_type_variant
*variant
;
2276 if (!type
|| !field
||
2277 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2282 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2284 if (index
< 0 || index
>= variant
->fields
->len
) {
2289 bt_ctf_field_type_get(field
);
2290 bt_ctf_field_type_put(((struct structure_field
*)
2291 g_ptr_array_index(variant
->fields
, index
))->type
);
2292 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2299 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
2301 struct bt_ctf_field_type_integer
*integer
;
2307 integer
= container_of(
2308 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2309 struct bt_ctf_field_type_integer
, parent
);
2310 if (integer
->mapped_clock
) {
2311 bt_ctf_clock_put(integer
->mapped_clock
);
2317 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
2319 struct bt_ctf_field_type_enumeration
*enumeration
;
2325 enumeration
= container_of(
2326 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2327 struct bt_ctf_field_type_enumeration
, parent
);
2328 g_ptr_array_free(enumeration
->entries
, TRUE
);
2329 bt_ctf_field_type_put(enumeration
->container
);
2330 g_free(enumeration
);
2334 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
2336 struct bt_ctf_field_type_floating_point
*floating_point
;
2342 floating_point
= container_of(
2343 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2344 struct bt_ctf_field_type_floating_point
, parent
);
2345 g_free(floating_point
);
2349 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
2351 struct bt_ctf_field_type_structure
*structure
;
2357 structure
= container_of(
2358 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2359 struct bt_ctf_field_type_structure
, parent
);
2360 g_ptr_array_free(structure
->fields
, TRUE
);
2361 g_hash_table_destroy(structure
->field_name_to_index
);
2366 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
2368 struct bt_ctf_field_type_variant
*variant
;
2374 variant
= container_of(
2375 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2376 struct bt_ctf_field_type_variant
, parent
);
2377 g_ptr_array_free(variant
->fields
, TRUE
);
2378 g_hash_table_destroy(variant
->field_name_to_index
);
2379 g_string_free(variant
->tag_name
, TRUE
);
2380 bt_ctf_field_type_put(&variant
->tag
->parent
);
2381 bt_ctf_field_path_destroy(variant
->tag_path
);
2386 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2388 struct bt_ctf_field_type_array
*array
;
2394 array
= container_of(
2395 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2396 struct bt_ctf_field_type_array
, parent
);
2397 bt_ctf_field_type_put(array
->element_type
);
2402 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2404 struct bt_ctf_field_type_sequence
*sequence
;
2410 sequence
= container_of(
2411 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2412 struct bt_ctf_field_type_sequence
, parent
);
2413 bt_ctf_field_type_put(sequence
->element_type
);
2414 g_string_free(sequence
->length_field_name
, TRUE
);
2415 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2420 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2422 struct bt_ctf_field_type_string
*string
;
2428 string
= container_of(
2429 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2430 struct bt_ctf_field_type_string
, parent
);
2435 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2441 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2443 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2444 type
, struct bt_ctf_field_type_enumeration
, parent
);
2446 generic_field_type_freeze(type
);
2447 bt_ctf_field_type_freeze(enumeration_type
->container
);
2451 void freeze_structure_field(struct structure_field
*field
)
2453 bt_ctf_field_type_freeze(field
->type
);
2457 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2459 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2460 type
, struct bt_ctf_field_type_structure
, parent
);
2462 /* Cache the alignment */
2463 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2464 generic_field_type_freeze(type
);
2465 g_ptr_array_foreach(structure_type
->fields
,
2466 (GFunc
) freeze_structure_field
, NULL
);
2470 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2472 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2473 type
, struct bt_ctf_field_type_variant
, parent
);
2475 /* Cache the alignment */
2476 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2477 generic_field_type_freeze(type
);
2478 g_ptr_array_foreach(variant_type
->fields
,
2479 (GFunc
) freeze_structure_field
, NULL
);
2483 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2485 struct bt_ctf_field_type_array
*array_type
= container_of(
2486 type
, struct bt_ctf_field_type_array
, parent
);
2488 /* Cache the alignment */
2489 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2490 generic_field_type_freeze(type
);
2491 bt_ctf_field_type_freeze(array_type
->element_type
);
2495 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2497 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2498 type
, struct bt_ctf_field_type_sequence
, parent
);
2500 /* Cache the alignment */
2501 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2502 generic_field_type_freeze(type
);
2503 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2507 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2509 const char *encoding_string
;
2512 case CTF_STRING_NONE
:
2513 encoding_string
= "none";
2515 case CTF_STRING_ASCII
:
2516 encoding_string
= "ASCII";
2518 case CTF_STRING_UTF8
:
2519 encoding_string
= "UTF8";
2522 encoding_string
= "unknown";
2526 return encoding_string
;
2530 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2532 const char *base_string
;
2535 case BT_CTF_INTEGER_BASE_DECIMAL
:
2536 base_string
= "decimal";
2538 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2539 base_string
= "hexadecimal";
2541 case BT_CTF_INTEGER_BASE_OCTAL
:
2542 base_string
= "octal";
2544 case BT_CTF_INTEGER_BASE_BINARY
:
2545 base_string
= "binary";
2548 base_string
= "unknown";
2556 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2557 struct metadata_context
*context
)
2559 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2560 struct bt_ctf_field_type_integer
, parent
);
2563 g_string_append_printf(context
->string
,
2564 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2565 integer
->declaration
.len
, type
->declaration
->alignment
,
2566 (integer
->declaration
.signedness
? "true" : "false"),
2567 get_encoding_string(integer
->declaration
.encoding
),
2568 get_integer_base_string(integer
->declaration
.base
),
2569 get_byte_order_string(integer
->declaration
.byte_order
));
2570 if (integer
->mapped_clock
) {
2571 const char *clock_name
= bt_ctf_clock_get_name(
2572 integer
->mapped_clock
);
2579 g_string_append_printf(context
->string
,
2580 "; map = clock.%s.value", clock_name
);
2583 g_string_append(context
->string
, "; }");
2589 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2590 struct metadata_context
*context
)
2594 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2595 struct bt_ctf_field_type_enumeration
, parent
);
2596 struct bt_ctf_field_type
*container_type
;
2597 int container_signed
;
2599 ret
= bt_ctf_field_type_validate(type
);
2604 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2605 if (!container_type
) {
2610 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2611 if (container_signed
< 0) {
2612 ret
= container_signed
;
2613 goto error_put_container_type
;
2616 g_string_append(context
->string
, "enum : ");
2617 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2619 goto error_put_container_type
;
2622 g_string_append(context
->string
, " { ");
2623 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2624 struct enumeration_mapping
*mapping
=
2625 enumeration
->entries
->pdata
[entry
];
2627 if (container_signed
) {
2628 if (mapping
->range_start
._signed
==
2629 mapping
->range_end
._signed
) {
2630 g_string_append_printf(context
->string
,
2631 "\"%s\" = %" PRId64
,
2632 g_quark_to_string(mapping
->string
),
2633 mapping
->range_start
._signed
);
2635 g_string_append_printf(context
->string
,
2636 "\"%s\" = %" PRId64
" ... %" PRId64
,
2637 g_quark_to_string(mapping
->string
),
2638 mapping
->range_start
._signed
,
2639 mapping
->range_end
._signed
);
2642 if (mapping
->range_start
._unsigned
==
2643 mapping
->range_end
._unsigned
) {
2644 g_string_append_printf(context
->string
,
2645 "\"%s\" = %" PRIu64
,
2646 g_quark_to_string(mapping
->string
),
2647 mapping
->range_start
._unsigned
);
2649 g_string_append_printf(context
->string
,
2650 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2651 g_quark_to_string(mapping
->string
),
2652 mapping
->range_start
._unsigned
,
2653 mapping
->range_end
._unsigned
);
2657 g_string_append(context
->string
,
2658 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2662 if (context
->field_name
->len
) {
2663 g_string_append_printf(context
->string
, " %s",
2664 context
->field_name
->str
);
2665 g_string_assign(context
->field_name
, "");
2667 error_put_container_type
:
2668 bt_ctf_field_type_put(container_type
);
2674 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2675 struct metadata_context
*context
)
2677 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2678 type
, struct bt_ctf_field_type_floating_point
, parent
);
2680 g_string_append_printf(context
->string
,
2681 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2682 floating_point
->declaration
.exp
->len
,
2683 floating_point
->declaration
.mantissa
->len
+ 1,
2684 get_byte_order_string(floating_point
->declaration
.byte_order
),
2685 type
->declaration
->alignment
);
2690 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2691 struct metadata_context
*context
)
2694 unsigned int indent
;
2696 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2697 struct bt_ctf_field_type_structure
, parent
);
2698 GString
*structure_field_name
= context
->field_name
;
2700 context
->field_name
= g_string_new("");
2702 context
->current_indentation_level
++;
2703 g_string_append(context
->string
, "struct {\n");
2705 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2706 struct structure_field
*field
;
2708 for (indent
= 0; indent
< context
->current_indentation_level
;
2710 g_string_append_c(context
->string
, '\t');
2713 field
= structure
->fields
->pdata
[i
];
2714 g_string_assign(context
->field_name
,
2715 g_quark_to_string(field
->name
));
2716 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2721 if (context
->field_name
->len
) {
2722 g_string_append_printf(context
->string
, " %s",
2723 context
->field_name
->str
);
2725 g_string_append(context
->string
, ";\n");
2728 context
->current_indentation_level
--;
2729 for (indent
= 0; indent
< context
->current_indentation_level
;
2731 g_string_append_c(context
->string
, '\t');
2734 g_string_append_printf(context
->string
, "} align(%zu)",
2735 type
->declaration
->alignment
);
2737 g_string_free(context
->field_name
, TRUE
);
2738 context
->field_name
= structure_field_name
;
2743 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2744 struct metadata_context
*context
)
2747 unsigned int indent
;
2749 struct bt_ctf_field_type_variant
*variant
= container_of(
2750 type
, struct bt_ctf_field_type_variant
, parent
);
2751 GString
*variant_field_name
= context
->field_name
;
2753 context
->field_name
= g_string_new("");
2754 if (variant
->tag_name
->len
> 0) {
2755 g_string_append_printf(context
->string
,
2756 "variant <%s> {\n", variant
->tag_name
->str
);
2758 g_string_append(context
->string
, "variant {\n");
2761 context
->current_indentation_level
++;
2762 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2763 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2765 g_string_assign(context
->field_name
,
2766 g_quark_to_string(field
->name
));
2767 for (indent
= 0; indent
< context
->current_indentation_level
;
2769 g_string_append_c(context
->string
, '\t');
2772 g_string_assign(context
->field_name
,
2773 g_quark_to_string(field
->name
));
2774 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2779 if (context
->field_name
->len
) {
2780 g_string_append_printf(context
->string
, " %s;",
2781 context
->field_name
->str
);
2784 g_string_append_c(context
->string
, '\n');
2787 context
->current_indentation_level
--;
2788 for (indent
= 0; indent
< context
->current_indentation_level
;
2790 g_string_append_c(context
->string
, '\t');
2793 g_string_append(context
->string
, "}");
2795 g_string_free(context
->field_name
, TRUE
);
2796 context
->field_name
= variant_field_name
;
2801 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2802 struct metadata_context
*context
)
2805 struct bt_ctf_field_type_array
*array
= container_of(type
,
2806 struct bt_ctf_field_type_array
, parent
);
2808 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2813 if (context
->field_name
->len
) {
2814 g_string_append_printf(context
->string
, " %s[%u]",
2815 context
->field_name
->str
, array
->length
);
2816 g_string_assign(context
->field_name
, "");
2818 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2825 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2826 struct metadata_context
*context
)
2829 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2830 type
, struct bt_ctf_field_type_sequence
, parent
);
2832 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2837 if (context
->field_name
->len
) {
2838 g_string_append_printf(context
->string
, " %s[%s]",
2839 context
->field_name
->str
,
2840 sequence
->length_field_name
->str
);
2841 g_string_assign(context
->field_name
, "");
2843 g_string_append_printf(context
->string
, "[%s]",
2844 sequence
->length_field_name
->str
);
2851 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2852 struct metadata_context
*context
)
2854 struct bt_ctf_field_type_string
*string
= container_of(
2855 type
, struct bt_ctf_field_type_string
, parent
);
2857 g_string_append_printf(context
->string
,
2858 "string { encoding = %s; }",
2859 get_encoding_string(string
->declaration
.encoding
));
2864 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2865 int byte_order
, int set_native
)
2867 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2868 struct bt_ctf_field_type_integer
, parent
);
2871 integer_type
->declaration
.byte_order
=
2872 integer_type
->declaration
.byte_order
== 0 ?
2873 byte_order
: integer_type
->declaration
.byte_order
;
2875 integer_type
->declaration
.byte_order
= byte_order
;
2880 void bt_ctf_field_type_enumeration_set_byte_order(
2881 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2883 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2884 struct bt_ctf_field_type_enumeration
, parent
);
2886 /* Safe to assume that container is an integer */
2887 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2888 byte_order
, set_native
);
2892 void bt_ctf_field_type_floating_point_set_byte_order(
2893 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2895 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2896 container_of(type
, struct bt_ctf_field_type_floating_point
,
2900 floating_point_type
->declaration
.byte_order
=
2901 floating_point_type
->declaration
.byte_order
== 0 ?
2903 floating_point_type
->declaration
.byte_order
;
2904 floating_point_type
->sign
.byte_order
=
2905 floating_point_type
->sign
.byte_order
== 0 ?
2906 byte_order
: floating_point_type
->sign
.byte_order
;
2907 floating_point_type
->mantissa
.byte_order
=
2908 floating_point_type
->mantissa
.byte_order
== 0 ?
2909 byte_order
: floating_point_type
->mantissa
.byte_order
;
2910 floating_point_type
->exp
.byte_order
=
2911 floating_point_type
->exp
.byte_order
== 0 ?
2912 byte_order
: floating_point_type
->exp
.byte_order
;
2914 floating_point_type
->declaration
.byte_order
= byte_order
;
2915 floating_point_type
->sign
.byte_order
= byte_order
;
2916 floating_point_type
->mantissa
.byte_order
= byte_order
;
2917 floating_point_type
->exp
.byte_order
= byte_order
;
2922 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2923 int byte_order
, int set_native
)
2926 struct bt_ctf_field_type_structure
*structure_type
=
2927 container_of(type
, struct bt_ctf_field_type_structure
,
2930 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2931 struct structure_field
*field
= g_ptr_array_index(
2932 structure_type
->fields
, i
);
2933 struct bt_ctf_field_type
*field_type
= field
->type
;
2935 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2936 set_byte_order_funcs
[field_type
->declaration
->id
](
2937 field_type
, byte_order
, set_native
);
2943 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2944 int byte_order
, int set_native
)
2947 struct bt_ctf_field_type_variant
*variant_type
=
2948 container_of(type
, struct bt_ctf_field_type_variant
,
2951 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2952 struct structure_field
*field
= g_ptr_array_index(
2953 variant_type
->fields
, i
);
2954 struct bt_ctf_field_type
*field_type
= field
->type
;
2956 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2957 set_byte_order_funcs
[field_type
->declaration
->id
](
2958 field_type
, byte_order
, set_native
);
2964 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2965 int byte_order
, int set_native
)
2967 struct bt_ctf_field_type_array
*array_type
=
2968 container_of(type
, struct bt_ctf_field_type_array
,
2971 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2972 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2973 array_type
->element_type
, byte_order
, set_native
);
2978 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2979 int byte_order
, int set_native
)
2981 struct bt_ctf_field_type_sequence
*sequence_type
=
2982 container_of(type
, struct bt_ctf_field_type_sequence
,
2985 if (set_byte_order_funcs
[
2986 sequence_type
->element_type
->declaration
->id
]) {
2987 set_byte_order_funcs
[
2988 sequence_type
->element_type
->declaration
->id
](
2989 sequence_type
->element_type
, byte_order
, set_native
);
2994 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2995 struct bt_ctf_field_type
*type
)
2997 struct bt_ctf_field_type
*copy
;
2998 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3000 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3001 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3006 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3008 copy_integer
->declaration
= integer
->declaration
;
3009 if (integer
->mapped_clock
) {
3010 bt_ctf_clock_get(integer
->mapped_clock
);
3011 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3018 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3019 struct bt_ctf_field_type
*type
)
3022 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3023 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3025 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3028 /* Copy the source enumeration's container */
3029 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3030 if (!copy_container
) {
3034 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3038 copy_enumeration
= container_of(copy
,
3039 struct bt_ctf_field_type_enumeration
, parent
);
3041 /* Copy all enumaration entries */
3042 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3043 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3044 enumeration
->entries
, i
);
3045 struct enumeration_mapping
* copy_mapping
= g_new0(
3046 struct enumeration_mapping
, 1);
3048 if (!copy_mapping
) {
3052 *copy_mapping
= *mapping
;
3053 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3056 copy_enumeration
->declaration
= enumeration
->declaration
;
3058 if (copy_container
) {
3059 bt_ctf_field_type_put(copy_container
);
3063 if (copy_container
) {
3064 bt_ctf_field_type_put(copy_container
);
3066 bt_ctf_field_type_put(copy
);
3071 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3072 struct bt_ctf_field_type
*type
)
3074 struct bt_ctf_field_type
*copy
;
3075 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3077 floating_point
= container_of(type
,
3078 struct bt_ctf_field_type_floating_point
, parent
);
3079 copy
= bt_ctf_field_type_floating_point_create();
3084 copy_float
= container_of(copy
,
3085 struct bt_ctf_field_type_floating_point
, parent
);
3086 copy_float
->declaration
= floating_point
->declaration
;
3087 copy_float
->sign
= floating_point
->sign
;
3088 copy_float
->mantissa
= floating_point
->mantissa
;
3089 copy_float
->exp
= floating_point
->exp
;
3095 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3096 struct bt_ctf_field_type
*type
)
3099 GHashTableIter iter
;
3100 gpointer key
, value
;
3101 struct bt_ctf_field_type
*copy
;
3102 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3104 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3106 copy
= bt_ctf_field_type_structure_create();
3111 copy_structure
= container_of(copy
,
3112 struct bt_ctf_field_type_structure
, parent
);
3114 /* Copy field_name_to_index */
3115 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3116 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3117 g_hash_table_insert(copy_structure
->field_name_to_index
,
3121 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3122 struct structure_field
*entry
, *copy_entry
;
3123 struct bt_ctf_field_type
*copy_field
;
3125 copy_entry
= g_new0(struct structure_field
, 1);
3130 entry
= g_ptr_array_index(structure
->fields
, i
);
3131 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3137 copy_entry
->name
= entry
->name
;
3138 copy_entry
->type
= copy_field
;
3139 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3142 copy_structure
->declaration
= structure
->declaration
;
3146 bt_ctf_field_type_put(copy
);
3151 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3152 struct bt_ctf_field_type
*type
)
3155 GHashTableIter iter
;
3156 gpointer key
, value
;
3157 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3158 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3160 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3163 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3169 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3170 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3175 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3178 /* Copy field_name_to_index */
3179 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3180 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3181 g_hash_table_insert(copy_variant
->field_name_to_index
,
3185 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3186 struct structure_field
*entry
, *copy_entry
;
3187 struct bt_ctf_field_type
*copy_field
;
3189 copy_entry
= g_new0(struct structure_field
, 1);
3194 entry
= g_ptr_array_index(variant
->fields
, i
);
3195 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3201 copy_entry
->name
= entry
->name
;
3202 copy_entry
->type
= copy_field
;
3203 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3206 copy_variant
->declaration
= variant
->declaration
;
3207 if (variant
->tag_path
) {
3208 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3210 if (!copy_variant
->tag_path
) {
3216 bt_ctf_field_type_put(copy_tag
);
3222 bt_ctf_field_type_put(copy_tag
);
3225 bt_ctf_field_type_put(copy
);
3230 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3231 struct bt_ctf_field_type
*type
)
3233 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3234 struct bt_ctf_field_type_array
*array
, *copy_array
;
3236 array
= container_of(type
, struct bt_ctf_field_type_array
,
3238 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3239 if (!copy_element
) {
3243 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3248 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3250 copy_array
->declaration
= array
->declaration
;
3253 bt_ctf_field_type_put(copy_element
);
3260 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3261 struct bt_ctf_field_type
*type
)
3263 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3264 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3266 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3268 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3269 if (!copy_element
) {
3273 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3274 sequence
->length_field_name
->len
?
3275 sequence
->length_field_name
->str
: NULL
);
3280 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3282 copy_sequence
->declaration
= sequence
->declaration
;
3283 if (sequence
->length_field_path
) {
3284 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3285 sequence
->length_field_path
);
3286 if (!copy_sequence
->length_field_path
) {
3292 bt_ctf_field_type_put(copy_element
);
3298 bt_ctf_field_type_put(copy
);
3305 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3306 struct bt_ctf_field_type
*type
)
3308 struct bt_ctf_field_type
*copy
;
3309 struct bt_ctf_field_type_string
*string
, *copy_string
;
3311 copy
= bt_ctf_field_type_string_create();
3316 string
= container_of(type
, struct bt_ctf_field_type_string
,
3318 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3320 copy_string
->declaration
= string
->declaration
;