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/ref.h>
33 #include <babeltrace/ctf-ir/common-internal.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/compiler.h>
37 #include <babeltrace/endian.h>
42 struct range_overlap_query
{
57 void bt_ctf_field_type_destroy(struct bt_ref
*);
59 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
61 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
76 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
77 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
79 bt_ctf_field_type_enumeration_destroy
,
81 bt_ctf_field_type_floating_point_destroy
,
82 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
83 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
84 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
85 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
86 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
90 void generic_field_type_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
103 type_freeze_func
const type_freeze_funcs
[] = {
104 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
105 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
106 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
107 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
108 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
109 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
110 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
111 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
115 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
116 struct metadata_context
*);
118 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
119 struct metadata_context
*);
121 int bt_ctf_field_type_floating_point_serialize(
122 struct bt_ctf_field_type
*, struct metadata_context
*);
124 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
125 struct metadata_context
*);
127 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
128 struct metadata_context
*);
130 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
131 struct metadata_context
*);
133 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
134 struct metadata_context
*);
136 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
137 struct metadata_context
*);
140 type_serialize_func
const type_serialize_funcs
[] = {
141 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
143 bt_ctf_field_type_enumeration_serialize
,
145 bt_ctf_field_type_floating_point_serialize
,
147 bt_ctf_field_type_structure_serialize
,
148 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
149 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
150 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
151 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
155 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
156 int byte_order
, int set_native
);
158 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
159 int byte_order
, int set_native
);
161 void bt_ctf_field_type_floating_point_set_byte_order(
162 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
164 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
165 int byte_order
, int set_native
);
167 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
168 int byte_order
, int set_native
);
170 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
171 int byte_order
, int set_native
);
173 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
174 int byte_order
, int set_native
);
176 /* The set_native flag only set the byte order if it is set to native */
178 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
179 int byte_order
, int set_native
) = {
180 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
182 bt_ctf_field_type_enumeration_set_byte_order
,
184 bt_ctf_field_type_floating_point_set_byte_order
,
186 bt_ctf_field_type_structure_set_byte_order
,
187 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
188 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
189 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
190 [CTF_TYPE_STRING
] = NULL
,
194 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
195 struct bt_ctf_field_type
*);
197 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
198 struct bt_ctf_field_type
*);
200 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
201 struct bt_ctf_field_type
*);
203 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
204 struct bt_ctf_field_type
*);
206 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
207 struct bt_ctf_field_type
*);
209 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
210 struct bt_ctf_field_type
*);
212 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
213 struct bt_ctf_field_type
*);
215 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
216 struct bt_ctf_field_type
*);
219 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
220 struct bt_ctf_field_type
*) = {
221 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
222 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
223 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
224 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
225 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
226 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
227 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
228 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
232 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
238 void destroy_structure_field(struct structure_field
*field
)
241 bt_ctf_field_type_put(field
->type
);
248 void check_ranges_overlap(gpointer element
, gpointer query
)
250 struct enumeration_mapping
*mapping
= element
;
251 struct range_overlap_query
*overlap_query
= query
;
253 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
254 && overlap_query
->range_start
._signed
<=
255 mapping
->range_end
._signed
) {
256 overlap_query
->overlaps
= 1;
257 overlap_query
->mapping_name
= mapping
->string
;
260 overlap_query
->overlaps
|=
261 mapping
->string
== overlap_query
->mapping_name
;
265 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
267 struct enumeration_mapping
*mapping
= element
;
268 struct range_overlap_query
*overlap_query
= query
;
270 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
271 && overlap_query
->range_start
._unsigned
<=
272 mapping
->range_end
._unsigned
) {
273 overlap_query
->overlaps
= 1;
274 overlap_query
->mapping_name
= mapping
->string
;
277 overlap_query
->overlaps
|=
278 mapping
->string
== overlap_query
->mapping_name
;
282 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
283 struct enumeration_mapping
**b
)
285 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
289 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
290 struct enumeration_mapping
**b
)
292 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
296 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
298 enum ctf_type_id type_id
= type
->declaration
->id
;
300 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
301 (type_id
< NR_CTF_TYPES
));
303 bt_ctf_base_init(type
, bt_ctf_field_type_destroy
);
304 type
->freeze
= type_freeze_funcs
[type_id
];
305 type
->serialize
= type_serialize_funcs
[type_id
];
308 int ret
= bt_ctf_field_type_set_byte_order(type
,
309 BT_CTF_BYTE_ORDER_NATIVE
);
313 type
->declaration
->alignment
= 1;
317 int add_structure_field(GPtrArray
*fields
,
318 GHashTable
*field_name_to_index
,
319 struct bt_ctf_field_type
*field_type
,
320 const char *field_name
)
323 GQuark name_quark
= g_quark_from_string(field_name
);
324 struct structure_field
*field
;
326 /* Make sure structure does not contain a field of the same name */
327 if (g_hash_table_lookup_extended(field_name_to_index
,
328 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
333 field
= g_new0(struct structure_field
, 1);
339 bt_ctf_field_type_get(field_type
);
340 field
->name
= name_quark
;
341 field
->type
= field_type
;
342 g_hash_table_insert(field_name_to_index
,
343 (gpointer
) (unsigned long) name_quark
,
344 (gpointer
) (unsigned long) fields
->len
);
345 g_ptr_array_add(fields
, field
);
351 void bt_ctf_field_type_destroy(struct bt_ref
*ref
)
353 struct bt_ctf_field_type
*type
;
354 struct bt_ctf_base
*base
;
355 enum ctf_type_id type_id
;
361 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
362 type
= container_of(base
, struct bt_ctf_field_type
, base
);
363 type_id
= type
->declaration
->id
;
364 if (type_id
<= CTF_TYPE_UNKNOWN
||
365 type_id
>= NR_CTF_TYPES
) {
369 type_destroy_funcs
[type_id
](type
);
373 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
382 switch (type
->declaration
->id
) {
385 struct bt_ctf_field_type_enumeration
*enumeration
=
386 container_of(type
, struct bt_ctf_field_type_enumeration
,
389 /* Ensure enum has entries */
390 ret
= enumeration
->entries
->len
? 0 : -1;
393 case CTF_TYPE_SEQUENCE
:
395 struct bt_ctf_field_type_sequence
*sequence
=
396 container_of(type
, struct bt_ctf_field_type_sequence
,
399 /* length field name should be set at this point */
400 ret
= sequence
->length_field_name
->len
? 0 : -1;
403 case CTF_TYPE_VARIANT
:
405 struct bt_ctf_field_type_variant
*variant
=
406 container_of(type
, struct bt_ctf_field_type_variant
,
409 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
421 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
423 struct bt_ctf_field_type_integer
*integer
=
424 g_new0(struct bt_ctf_field_type_integer
, 1);
426 if (!integer
|| size
== 0 || size
> 64) {
430 integer
->parent
.declaration
= &integer
->declaration
.p
;
431 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
432 integer
->declaration
.len
= size
;
433 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
434 integer
->declaration
.encoding
= CTF_STRING_NONE
;
435 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
436 return &integer
->parent
;
439 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
442 struct bt_ctf_field_type_integer
*integer
;
444 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
449 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
450 ret
= (int) integer
->declaration
.len
;
455 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
458 struct bt_ctf_field_type_integer
*integer
;
460 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
465 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
466 ret
= integer
->declaration
.signedness
;
471 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
475 struct bt_ctf_field_type_integer
*integer
;
477 if (!type
|| type
->frozen
||
478 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
483 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
484 integer
->declaration
.signedness
= !!is_signed
;
489 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
490 struct bt_ctf_field_type
*type
)
492 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
493 struct bt_ctf_field_type_integer
*integer
;
495 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
499 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
500 ret
= integer
->declaration
.base
;
505 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
506 enum bt_ctf_integer_base base
)
510 if (!type
|| type
->frozen
||
511 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
517 case BT_CTF_INTEGER_BASE_BINARY
:
518 case BT_CTF_INTEGER_BASE_OCTAL
:
519 case BT_CTF_INTEGER_BASE_DECIMAL
:
520 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
522 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
523 struct bt_ctf_field_type_integer
, parent
);
524 integer
->declaration
.base
= base
;
534 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
535 struct bt_ctf_field_type
*type
)
537 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
538 struct bt_ctf_field_type_integer
*integer
;
540 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
544 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
545 ret
= integer
->declaration
.encoding
;
550 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
551 enum ctf_string_encoding encoding
)
554 struct bt_ctf_field_type_integer
*integer
;
556 if (!type
|| type
->frozen
||
557 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
558 (encoding
< CTF_STRING_NONE
) ||
559 (encoding
>= CTF_STRING_UNKNOWN
)) {
564 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
565 integer
->declaration
.encoding
= encoding
;
570 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
571 struct bt_ctf_field_type
*type
)
573 struct bt_ctf_field_type_integer
*integer
;
574 struct bt_ctf_clock
*clock
= NULL
;
580 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
581 clock
= integer
->mapped_clock
;
583 bt_ctf_clock_get(clock
);
589 int bt_ctf_field_type_integer_set_mapped_clock(
590 struct bt_ctf_field_type
*type
,
591 struct bt_ctf_clock
*clock
)
593 struct bt_ctf_field_type_integer
*integer
;
596 if (!type
|| type
->frozen
) {
601 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
602 if (integer
->mapped_clock
) {
603 bt_ctf_clock_put(integer
->mapped_clock
);
607 bt_ctf_clock_get(clock
);
610 integer
->mapped_clock
= clock
;
615 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
616 struct bt_ctf_field_type
*integer_container_type
)
618 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
620 if (!integer_container_type
) {
624 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
628 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
633 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
634 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
635 bt_ctf_field_type_get(integer_container_type
);
636 enumeration
->container
= integer_container_type
;
637 enumeration
->entries
= g_ptr_array_new_with_free_func(
638 (GDestroyNotify
)destroy_enumeration_mapping
);
639 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
640 return &enumeration
->parent
;
646 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
647 struct bt_ctf_field_type
*type
)
649 struct bt_ctf_field_type
*container_type
= NULL
;
650 struct bt_ctf_field_type_enumeration
*enumeration_type
;
656 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
660 enumeration_type
= container_of(type
,
661 struct bt_ctf_field_type_enumeration
, parent
);
662 container_type
= enumeration_type
->container
;
663 bt_ctf_field_type_get(container_type
);
665 return container_type
;
668 int bt_ctf_field_type_enumeration_add_mapping(
669 struct bt_ctf_field_type
*type
, const char *string
,
670 int64_t range_start
, int64_t range_end
)
674 struct enumeration_mapping
*mapping
;
675 struct bt_ctf_field_type_enumeration
*enumeration
;
676 struct range_overlap_query query
;
677 char *escaped_string
;
679 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
681 (range_end
< range_start
)) {
686 if (!string
|| strlen(string
) == 0) {
691 escaped_string
= g_strescape(string
, NULL
);
692 if (!escaped_string
) {
697 mapping_name
= g_quark_from_string(escaped_string
);
698 query
= (struct range_overlap_query
) {
699 .range_start
._signed
= range_start
,
700 .range_end
._signed
= range_end
,
701 .mapping_name
= mapping_name
,
703 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
706 /* Check that the range does not overlap with one already present */
707 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
708 if (query
.overlaps
) {
713 mapping
= g_new(struct enumeration_mapping
, 1);
719 *mapping
= (struct enumeration_mapping
) {
720 .range_start
._signed
= range_start
,
721 .range_end
._signed
= range_end
, .string
= mapping_name
};
722 g_ptr_array_add(enumeration
->entries
, mapping
);
723 g_ptr_array_sort(enumeration
->entries
,
724 (GCompareFunc
)compare_enumeration_mappings_signed
);
726 free(escaped_string
);
731 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
732 struct bt_ctf_field_type
*type
, const char *string
,
733 uint64_t range_start
, uint64_t range_end
)
737 struct enumeration_mapping
*mapping
;
738 struct bt_ctf_field_type_enumeration
*enumeration
;
739 struct range_overlap_query query
;
740 char *escaped_string
;
742 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
744 (range_end
< range_start
)) {
749 if (!string
|| strlen(string
) == 0) {
754 escaped_string
= g_strescape(string
, NULL
);
755 if (!escaped_string
) {
760 mapping_name
= g_quark_from_string(escaped_string
);
761 query
= (struct range_overlap_query
) {
762 .range_start
._unsigned
= range_start
,
763 .range_end
._unsigned
= range_end
,
764 .mapping_name
= mapping_name
,
766 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
769 /* Check that the range does not overlap with one already present */
770 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
772 if (query
.overlaps
) {
777 mapping
= g_new(struct enumeration_mapping
, 1);
783 *mapping
= (struct enumeration_mapping
) {
784 .range_start
._unsigned
= range_start
,
785 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
786 g_ptr_array_add(enumeration
->entries
, mapping
);
787 g_ptr_array_sort(enumeration
->entries
,
788 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
790 free(escaped_string
);
795 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
796 struct bt_ctf_field_type_enumeration
*enumeration_type
,
799 const char *name
= NULL
;
800 struct range_overlap_query query
=
801 (struct range_overlap_query
) {
802 .range_start
._unsigned
= value
,
803 .range_end
._unsigned
= value
,
806 g_ptr_array_foreach(enumeration_type
->entries
,
807 check_ranges_overlap_unsigned
,
809 if (!query
.overlaps
) {
813 name
= g_quark_to_string(query
.mapping_name
);
818 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
819 struct bt_ctf_field_type_enumeration
*enumeration_type
,
822 const char *name
= NULL
;
823 struct range_overlap_query query
=
824 (struct range_overlap_query
) {
825 .range_start
._signed
= value
,
826 .range_end
._signed
= value
,
829 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
831 if (!query
.overlaps
) {
835 name
= g_quark_to_string(query
.mapping_name
);
840 int bt_ctf_field_type_enumeration_get_mapping_count(
841 struct bt_ctf_field_type
*type
)
844 struct bt_ctf_field_type_enumeration
*enumeration
;
846 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
851 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
853 ret
= (int) enumeration
->entries
->len
;
859 struct enumeration_mapping
*get_enumeration_mapping(
860 struct bt_ctf_field_type
*type
, int index
)
862 struct enumeration_mapping
*mapping
= NULL
;
863 struct bt_ctf_field_type_enumeration
*enumeration
;
865 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
867 if (index
>= enumeration
->entries
->len
) {
871 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
876 int bt_ctf_field_type_enumeration_get_mapping(
877 struct bt_ctf_field_type
*type
, int index
,
878 const char **string
, int64_t *range_start
, int64_t *range_end
)
880 struct enumeration_mapping
*mapping
;
883 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
884 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
889 mapping
= get_enumeration_mapping(type
, index
);
895 *string
= g_quark_to_string(mapping
->string
);
896 *range_start
= mapping
->range_start
._signed
;
897 *range_end
= mapping
->range_end
._signed
;
902 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
903 struct bt_ctf_field_type
*type
, int index
,
904 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
906 struct enumeration_mapping
*mapping
;
909 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
910 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
915 mapping
= get_enumeration_mapping(type
, index
);
921 *string
= g_quark_to_string(mapping
->string
);
922 *range_start
= mapping
->range_start
._unsigned
;
923 *range_end
= mapping
->range_end
._unsigned
;
928 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
929 struct bt_ctf_field_type
*type
, const char *name
)
932 struct bt_ctf_field_type_enumeration
*enumeration
;
935 if (!type
|| !name
||
936 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
941 name_quark
= g_quark_try_string(name
);
947 enumeration
= container_of(type
,
948 struct bt_ctf_field_type_enumeration
, parent
);
949 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
950 struct enumeration_mapping
*mapping
=
951 get_enumeration_mapping(type
, i
);
953 if (mapping
->string
== name_quark
) {
964 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
965 struct bt_ctf_field_type
*type
, int64_t value
)
967 struct bt_ctf_field_type_enumeration
*enumeration
;
970 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
975 enumeration
= container_of(type
,
976 struct bt_ctf_field_type_enumeration
, parent
);
977 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
978 struct enumeration_mapping
*mapping
=
979 get_enumeration_mapping(type
, i
);
981 if (value
>= mapping
->range_start
._signed
&&
982 value
<= mapping
->range_end
._signed
) {
993 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
994 struct bt_ctf_field_type
*type
, uint64_t value
)
996 struct bt_ctf_field_type_enumeration
*enumeration
;
999 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
1004 enumeration
= container_of(type
,
1005 struct bt_ctf_field_type_enumeration
, parent
);
1006 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1007 struct enumeration_mapping
*mapping
=
1008 get_enumeration_mapping(type
, i
);
1010 if (value
>= mapping
->range_start
._unsigned
&&
1011 value
<= mapping
->range_end
._unsigned
) {
1022 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1024 struct bt_ctf_field_type_floating_point
*floating_point
=
1025 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1027 if (!floating_point
) {
1031 floating_point
->declaration
.sign
= &floating_point
->sign
;
1032 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1033 floating_point
->declaration
.exp
= &floating_point
->exp
;
1034 floating_point
->sign
.len
= 1;
1035 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1036 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1037 floating_point
->declaration
.exp
->len
=
1038 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1039 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1040 floating_point
->sign
.p
.alignment
= 1;
1041 floating_point
->mantissa
.p
.alignment
= 1;
1042 floating_point
->exp
.p
.alignment
= 1;
1044 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1046 return floating_point
? &floating_point
->parent
: NULL
;
1049 int bt_ctf_field_type_floating_point_get_exponent_digits(
1050 struct bt_ctf_field_type
*type
)
1053 struct bt_ctf_field_type_floating_point
*floating_point
;
1055 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1060 floating_point
= container_of(type
,
1061 struct bt_ctf_field_type_floating_point
, parent
);
1062 ret
= (int) floating_point
->declaration
.exp
->len
;
1067 int bt_ctf_field_type_floating_point_set_exponent_digits(
1068 struct bt_ctf_field_type
*type
,
1069 unsigned int exponent_digits
)
1072 struct bt_ctf_field_type_floating_point
*floating_point
;
1074 if (!type
|| type
->frozen
||
1075 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1080 floating_point
= container_of(type
,
1081 struct bt_ctf_field_type_floating_point
, parent
);
1082 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1083 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1085 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1090 floating_point
->declaration
.exp
->len
= exponent_digits
;
1095 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1096 struct bt_ctf_field_type
*type
)
1099 struct bt_ctf_field_type_floating_point
*floating_point
;
1101 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1106 floating_point
= container_of(type
,
1107 struct bt_ctf_field_type_floating_point
, parent
);
1108 ret
= (int) floating_point
->mantissa
.len
+ 1;
1113 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1114 struct bt_ctf_field_type
*type
,
1115 unsigned int mantissa_digits
)
1118 struct bt_ctf_field_type_floating_point
*floating_point
;
1120 if (!type
|| type
->frozen
||
1121 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1126 floating_point
= container_of(type
,
1127 struct bt_ctf_field_type_floating_point
, parent
);
1129 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1130 (mantissa_digits
!= DBL_MANT_DIG
) &&
1131 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1136 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1141 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1143 struct bt_ctf_field_type_structure
*structure
=
1144 g_new0(struct bt_ctf_field_type_structure
, 1);
1150 structure
->parent
.declaration
= &structure
->declaration
.p
;
1151 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1152 structure
->fields
= g_ptr_array_new_with_free_func(
1153 (GDestroyNotify
)destroy_structure_field
);
1154 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1155 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1156 return &structure
->parent
;
1161 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1162 struct bt_ctf_field_type
*field_type
,
1163 const char *field_name
)
1166 struct bt_ctf_field_type_structure
*structure
;
1168 if (!type
|| !field_type
|| type
->frozen
||
1169 bt_ctf_validate_identifier(field_name
) ||
1170 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1171 bt_ctf_field_type_validate(field_type
)) {
1176 structure
= container_of(type
,
1177 struct bt_ctf_field_type_structure
, parent
);
1178 if (add_structure_field(structure
->fields
,
1179 structure
->field_name_to_index
, field_type
, field_name
)) {
1187 int bt_ctf_field_type_structure_get_field_count(
1188 struct bt_ctf_field_type
*type
)
1191 struct bt_ctf_field_type_structure
*structure
;
1193 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1198 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1200 ret
= (int) structure
->fields
->len
;
1205 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1206 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1209 struct bt_ctf_field_type_structure
*structure
;
1210 struct structure_field
*field
;
1213 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1218 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1220 if (index
>= structure
->fields
->len
) {
1225 field
= g_ptr_array_index(structure
->fields
, index
);
1227 *field_type
= field
->type
;
1228 bt_ctf_field_type_get(field
->type
);
1231 *field_name
= g_quark_to_string(field
->name
);
1237 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1238 struct bt_ctf_field_type
*type
,
1243 struct structure_field
*field
;
1244 struct bt_ctf_field_type_structure
*structure
;
1245 struct bt_ctf_field_type
*field_type
= NULL
;
1247 if (!type
|| !name
) {
1251 name_quark
= g_quark_try_string(name
);
1256 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1258 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1259 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1263 field
= structure
->fields
->pdata
[index
];
1264 field_type
= field
->type
;
1265 bt_ctf_field_type_get(field_type
);
1270 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1271 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1273 struct bt_ctf_field_type_variant
*variant
= NULL
;
1275 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1279 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1284 variant
->parent
.declaration
= &variant
->declaration
.p
;
1285 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1286 variant
->tag_name
= g_string_new(tag_name
);
1287 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1288 variant
->fields
= g_ptr_array_new_with_free_func(
1289 (GDestroyNotify
)destroy_structure_field
);
1291 bt_ctf_field_type_get(enum_tag
);
1292 variant
->tag
= container_of(enum_tag
,
1293 struct bt_ctf_field_type_enumeration
, parent
);
1296 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1297 /* A variant's alignment is undefined */
1298 variant
->parent
.declaration
->alignment
= 0;
1299 return &variant
->parent
;
1304 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1305 struct bt_ctf_field_type
*type
)
1307 struct bt_ctf_field_type_variant
*variant
;
1308 struct bt_ctf_field_type
*tag_type
= NULL
;
1310 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1314 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1315 if (!variant
->tag
) {
1319 tag_type
= &variant
->tag
->parent
;
1320 bt_ctf_field_type_get(tag_type
);
1325 const char *bt_ctf_field_type_variant_get_tag_name(
1326 struct bt_ctf_field_type
*type
)
1328 struct bt_ctf_field_type_variant
*variant
;
1329 const char *tag_name
= NULL
;
1331 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1335 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1336 if (variant
->tag_name
->len
== 0) {
1340 tag_name
= variant
->tag_name
->str
;
1345 int bt_ctf_field_type_variant_set_tag_name(
1346 struct bt_ctf_field_type
*type
, const char *name
)
1349 struct bt_ctf_field_type_variant
*variant
;
1351 if (!type
|| type
->frozen
||
1352 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1353 bt_ctf_validate_identifier(name
)) {
1358 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1359 g_string_assign(variant
->tag_name
, name
);
1364 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1365 struct bt_ctf_field_type
*field_type
,
1366 const char *field_name
)
1370 struct bt_ctf_field_type_variant
*variant
;
1371 GQuark field_name_quark
= g_quark_from_string(field_name
);
1373 if (!type
|| !field_type
|| type
->frozen
||
1374 bt_ctf_validate_identifier(field_name
) ||
1375 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1376 bt_ctf_field_type_validate(field_type
)) {
1381 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1383 /* The user has explicitly provided a tag; validate against it. */
1387 /* Make sure this name is present in the enum tag */
1388 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1389 struct enumeration_mapping
*mapping
=
1390 g_ptr_array_index(variant
->tag
->entries
, i
);
1392 if (mapping
->string
== field_name_quark
) {
1399 /* Validation failed */
1405 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1406 field_type
, field_name
)) {
1414 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1415 struct bt_ctf_field_type
*type
,
1416 const char *field_name
)
1420 struct structure_field
*field
;
1421 struct bt_ctf_field_type_variant
*variant
;
1422 struct bt_ctf_field_type
*field_type
= NULL
;
1424 if (!type
|| !field_name
) {
1428 name_quark
= g_quark_try_string(field_name
);
1433 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1434 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1435 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1439 field
= g_ptr_array_index(variant
->fields
, index
);
1440 field_type
= field
->type
;
1441 bt_ctf_field_type_get(field_type
);
1446 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1447 struct bt_ctf_field_type
*type
,
1448 struct bt_ctf_field
*tag
)
1450 const char *enum_value
;
1451 struct bt_ctf_field_type
*field_type
= NULL
;
1453 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1457 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1462 /* Already increments field_type's reference count */
1463 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1469 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1472 struct bt_ctf_field_type_variant
*variant
;
1474 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1479 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1481 ret
= (int) variant
->fields
->len
;
1487 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1488 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1491 struct bt_ctf_field_type_variant
*variant
;
1492 struct structure_field
*field
;
1495 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1500 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1502 if (index
>= variant
->fields
->len
) {
1507 field
= g_ptr_array_index(variant
->fields
, index
);
1509 *field_type
= field
->type
;
1510 bt_ctf_field_type_get(field
->type
);
1513 *field_name
= g_quark_to_string(field
->name
);
1519 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1520 struct bt_ctf_field_type
*element_type
,
1521 unsigned int length
)
1523 struct bt_ctf_field_type_array
*array
= NULL
;
1525 if (!element_type
|| length
== 0 ||
1526 bt_ctf_field_type_validate(element_type
)) {
1530 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1535 array
->parent
.declaration
= &array
->declaration
.p
;
1536 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1538 bt_ctf_field_type_get(element_type
);
1539 array
->element_type
= element_type
;
1540 array
->length
= length
;
1541 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1542 return &array
->parent
;
1547 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1548 struct bt_ctf_field_type
*type
)
1550 struct bt_ctf_field_type
*ret
= NULL
;
1551 struct bt_ctf_field_type_array
*array
;
1553 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1557 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1558 ret
= array
->element_type
;
1559 bt_ctf_field_type_get(ret
);
1564 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1567 struct bt_ctf_field_type_array
*array
;
1569 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1574 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1575 ret
= (int64_t) array
->length
;
1580 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1581 struct bt_ctf_field_type
*element_type
,
1582 const char *length_field_name
)
1584 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1586 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1587 bt_ctf_field_type_validate(element_type
)) {
1591 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1596 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1597 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1598 bt_ctf_field_type_get(element_type
);
1599 sequence
->element_type
= element_type
;
1600 sequence
->length_field_name
= g_string_new(length_field_name
);
1601 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1602 return &sequence
->parent
;
1607 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1608 struct bt_ctf_field_type
*type
)
1610 struct bt_ctf_field_type
*ret
= NULL
;
1611 struct bt_ctf_field_type_sequence
*sequence
;
1613 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1617 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1619 ret
= sequence
->element_type
;
1620 bt_ctf_field_type_get(ret
);
1625 const char *bt_ctf_field_type_sequence_get_length_field_name(
1626 struct bt_ctf_field_type
*type
)
1628 const char *ret
= NULL
;
1629 struct bt_ctf_field_type_sequence
*sequence
;
1631 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1635 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1637 ret
= sequence
->length_field_name
->str
;
1642 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1644 struct bt_ctf_field_type_string
*string
=
1645 g_new0(struct bt_ctf_field_type_string
, 1);
1651 string
->parent
.declaration
= &string
->declaration
.p
;
1652 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1653 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1654 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1655 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1656 return &string
->parent
;
1659 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1660 struct bt_ctf_field_type
*type
)
1662 struct bt_ctf_field_type_string
*string
;
1663 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1665 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1669 string
= container_of(type
, struct bt_ctf_field_type_string
,
1671 ret
= string
->declaration
.encoding
;
1676 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1677 enum ctf_string_encoding encoding
)
1680 struct bt_ctf_field_type_string
*string
;
1682 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1683 (encoding
!= CTF_STRING_UTF8
&&
1684 encoding
!= CTF_STRING_ASCII
)) {
1689 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1690 string
->declaration
.encoding
= encoding
;
1695 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1698 enum ctf_type_id type_id
;
1706 ret
= (int) type
->declaration
->alignment
;
1710 type_id
= bt_ctf_field_type_get_type_id(type
);
1712 case CTF_TYPE_SEQUENCE
:
1714 struct bt_ctf_field_type
*element
=
1715 bt_ctf_field_type_sequence_get_element_type(type
);
1722 ret
= bt_ctf_field_type_get_alignment(element
);
1723 bt_ctf_field_type_put(element
);
1726 case CTF_TYPE_ARRAY
:
1728 struct bt_ctf_field_type
*element
=
1729 bt_ctf_field_type_array_get_element_type(type
);
1736 ret
= bt_ctf_field_type_get_alignment(element
);
1737 bt_ctf_field_type_put(element
);
1740 case CTF_TYPE_STRUCT
:
1742 int i
, element_count
;
1744 element_count
= bt_ctf_field_type_structure_get_field_count(
1746 if (element_count
< 0) {
1747 ret
= element_count
;
1751 for (i
= 0; i
< element_count
; i
++) {
1752 struct bt_ctf_field_type
*field
;
1753 int field_alignment
;
1755 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
1762 field_alignment
= bt_ctf_field_type_get_alignment(
1764 bt_ctf_field_type_put(field
);
1765 if (field_alignment
< 0) {
1766 ret
= field_alignment
;
1770 type
->declaration
->alignment
= MAX(field_alignment
,
1771 type
->declaration
->alignment
);
1773 ret
= (int) type
->declaration
->alignment
;
1776 case CTF_TYPE_UNKNOWN
:
1780 ret
= (int) type
->declaration
->alignment
;
1788 int is_power_of_two(unsigned int value
)
1790 return ((value
& (value
- 1)) == 0) && value
> 0;
1793 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1794 unsigned int alignment
)
1797 enum ctf_type_id type_id
;
1799 /* Alignment must be a power of two */
1800 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
1805 type_id
= bt_ctf_field_type_get_type_id(type
);
1806 if (type_id
== CTF_TYPE_UNKNOWN
) {
1811 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1812 alignment
!= CHAR_BIT
) {
1817 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
1818 type_id
== CTF_TYPE_ARRAY
) {
1819 /* Setting an alignment on these types makes no sense */
1824 type
->declaration
->alignment
= alignment
;
1830 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1831 struct bt_ctf_field_type
*type
)
1833 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1834 int internal_byte_order
= -1;
1840 switch (type
->declaration
->id
) {
1841 case CTF_TYPE_INTEGER
:
1843 struct bt_ctf_field_type_integer
*integer
= container_of(
1844 type
, struct bt_ctf_field_type_integer
, parent
);
1845 internal_byte_order
= integer
->declaration
.byte_order
;
1848 case CTF_TYPE_FLOAT
:
1850 struct bt_ctf_field_type_floating_point
*floating_point
=
1852 struct bt_ctf_field_type_floating_point
,
1854 internal_byte_order
= floating_point
->declaration
.byte_order
;
1861 switch (internal_byte_order
) {
1863 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1866 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1869 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1872 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1878 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1879 enum bt_ctf_byte_order byte_order
)
1882 int internal_byte_order
;
1883 enum ctf_type_id type_id
;
1885 if (!type
|| type
->frozen
) {
1890 switch (byte_order
) {
1891 case BT_CTF_BYTE_ORDER_NATIVE
:
1892 /* Leave unset. Will be initialized by parent. */
1893 internal_byte_order
= 0;
1895 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1896 internal_byte_order
= LITTLE_ENDIAN
;
1898 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1899 case BT_CTF_BYTE_ORDER_NETWORK
:
1900 internal_byte_order
= BIG_ENDIAN
;
1907 type_id
= type
->declaration
->id
;
1908 if (set_byte_order_funcs
[type_id
]) {
1909 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1915 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1916 struct bt_ctf_field_type
*type
)
1919 return CTF_TYPE_UNKNOWN
;
1922 return type
->declaration
->id
;
1925 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1930 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1936 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1946 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1947 struct bt_ctf_field_type_variant
*variant
,
1950 struct bt_ctf_field_type
*type
= NULL
;
1951 GQuark field_name_quark
;
1953 struct structure_field
*field_entry
;
1954 struct range_overlap_query query
= {
1955 .range_start
._signed
= tag_value
,
1956 .range_end
._signed
= tag_value
,
1957 .mapping_name
= 0, .overlaps
= 0};
1959 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1961 if (!query
.overlaps
) {
1965 field_name_quark
= query
.mapping_name
;
1966 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1967 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1971 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1972 type
= field_entry
->type
;
1978 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1979 struct bt_ctf_field_type_variant
*variant
,
1982 struct bt_ctf_field_type
*type
= NULL
;
1983 GQuark field_name_quark
;
1985 struct structure_field
*field_entry
;
1986 struct range_overlap_query query
= {
1987 .range_start
._unsigned
= tag_value
,
1988 .range_end
._unsigned
= tag_value
,
1989 .mapping_name
= 0, .overlaps
= 0};
1991 g_ptr_array_foreach(variant
->tag
->entries
,
1992 check_ranges_overlap_unsigned
,
1994 if (!query
.overlaps
) {
1998 field_name_quark
= query
.mapping_name
;
1999 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2000 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2004 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2005 type
= field_entry
->type
;
2011 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2012 struct metadata_context
*context
)
2016 if (!type
|| !context
) {
2021 ret
= type
->serialize(type
, context
);
2027 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2034 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2035 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2036 set_byte_order_funcs
[type
->declaration
->id
](type
,
2042 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2044 struct bt_ctf_field_type
*copy
= NULL
;
2050 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2056 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2058 struct bt_ctf_field_path
*field_path
= NULL
;
2060 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2065 field_path
->root
= CTF_NODE_UNKNOWN
;
2066 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2067 if (!field_path
->path_indexes
) {
2068 bt_ctf_field_path_destroy(field_path
);
2077 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2078 struct bt_ctf_field_path
*path
)
2080 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2086 new_path
->root
= path
->root
;
2087 g_array_insert_vals(new_path
->path_indexes
, 0,
2088 path
->path_indexes
->data
, path
->path_indexes
->len
);
2094 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2100 if (path
->path_indexes
) {
2101 g_array_free(path
->path_indexes
, TRUE
);
2107 int bt_ctf_field_type_structure_get_field_name_index(
2108 struct bt_ctf_field_type
*type
, const char *name
)
2113 struct bt_ctf_field_type_structure
*structure
;
2115 if (!type
|| !name
||
2116 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2121 name_quark
= g_quark_try_string(name
);
2127 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2129 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2130 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2140 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2141 struct bt_ctf_field_type
*field
, int index
)
2144 struct bt_ctf_field_type_structure
*structure
;
2146 if (!type
|| !field
||
2147 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2152 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2154 if (index
< 0 || index
>= structure
->fields
->len
) {
2159 bt_ctf_field_type_get(field
);
2160 bt_ctf_field_type_put(((struct structure_field
*)
2161 g_ptr_array_index(structure
->fields
, index
))->type
);
2162 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2169 int bt_ctf_field_type_variant_get_field_name_index(
2170 struct bt_ctf_field_type
*type
, const char *name
)
2175 struct bt_ctf_field_type_variant
*variant
;
2177 if (!type
|| !name
||
2178 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2183 name_quark
= g_quark_try_string(name
);
2189 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2191 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2192 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2202 int bt_ctf_field_type_sequence_set_length_field_path(
2203 struct bt_ctf_field_type
*type
,
2204 struct bt_ctf_field_path
*path
)
2207 struct bt_ctf_field_type_sequence
*sequence
;
2209 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2214 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2216 if (sequence
->length_field_path
) {
2217 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2219 sequence
->length_field_path
= path
;
2225 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
2226 struct bt_ctf_field_type
*type
)
2228 struct bt_ctf_field_type_sequence
*sequence
;
2230 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2233 return sequence
->length_field_path
;
2237 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2238 struct bt_ctf_field_path
*path
)
2241 struct bt_ctf_field_type_variant
*variant
;
2243 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2248 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2250 if (variant
->tag_path
) {
2251 bt_ctf_field_path_destroy(variant
->tag_path
);
2253 variant
->tag_path
= path
;
2259 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
2260 struct bt_ctf_field_type
*type
)
2262 struct bt_ctf_field_type_variant
*variant
;
2264 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2267 return variant
->tag_path
;
2271 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2272 struct bt_ctf_field_type
*tag
)
2275 struct bt_ctf_field_type_variant
*variant
;
2277 if (!type
|| !tag
|| type
->frozen
||
2278 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2283 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2285 bt_ctf_field_type_get(tag
);
2287 bt_ctf_field_type_put(&variant
->tag
->parent
);
2289 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2296 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2297 struct bt_ctf_field_type
*field
, int index
)
2300 struct bt_ctf_field_type_variant
*variant
;
2302 if (!type
|| !field
||
2303 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2308 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2310 if (index
< 0 || index
>= variant
->fields
->len
) {
2315 bt_ctf_field_type_get(field
);
2316 bt_ctf_field_type_put(((struct structure_field
*)
2317 g_ptr_array_index(variant
->fields
, index
))->type
);
2318 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2325 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2327 struct bt_ctf_field_type_integer
*integer
=
2328 (struct bt_ctf_field_type_integer
*) type
;
2334 if (integer
->mapped_clock
) {
2335 bt_ctf_clock_put(integer
->mapped_clock
);
2341 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2343 struct bt_ctf_field_type_enumeration
*enumeration
=
2344 (struct bt_ctf_field_type_enumeration
*) type
;
2350 g_ptr_array_free(enumeration
->entries
, TRUE
);
2351 bt_ctf_field_type_put(enumeration
->container
);
2352 g_free(enumeration
);
2356 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2358 struct bt_ctf_field_type_floating_point
*floating_point
=
2359 (struct bt_ctf_field_type_floating_point
*) type
;
2365 g_free(floating_point
);
2369 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2371 struct bt_ctf_field_type_structure
*structure
=
2372 (struct bt_ctf_field_type_structure
*) type
;
2378 g_ptr_array_free(structure
->fields
, TRUE
);
2379 g_hash_table_destroy(structure
->field_name_to_index
);
2384 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2386 struct bt_ctf_field_type_variant
*variant
=
2387 (struct bt_ctf_field_type_variant
*) type
;
2393 g_ptr_array_free(variant
->fields
, TRUE
);
2394 g_hash_table_destroy(variant
->field_name_to_index
);
2395 g_string_free(variant
->tag_name
, TRUE
);
2396 bt_ctf_field_type_put(&variant
->tag
->parent
);
2397 bt_ctf_field_path_destroy(variant
->tag_path
);
2402 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2404 struct bt_ctf_field_type_array
*array
=
2405 (struct bt_ctf_field_type_array
*) type
;
2411 bt_ctf_field_type_put(array
->element_type
);
2416 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2418 struct bt_ctf_field_type_sequence
*sequence
=
2419 (struct bt_ctf_field_type_sequence
*) type
;
2425 bt_ctf_field_type_put(sequence
->element_type
);
2426 g_string_free(sequence
->length_field_name
, TRUE
);
2427 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2432 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2434 struct bt_ctf_field_type_string
*string
=
2435 (struct bt_ctf_field_type_string
*) type
;
2445 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2451 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2453 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2454 type
, struct bt_ctf_field_type_enumeration
, parent
);
2456 generic_field_type_freeze(type
);
2457 bt_ctf_field_type_freeze(enumeration_type
->container
);
2461 void freeze_structure_field(struct structure_field
*field
)
2463 bt_ctf_field_type_freeze(field
->type
);
2467 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2469 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2470 type
, struct bt_ctf_field_type_structure
, parent
);
2472 /* Cache the alignment */
2473 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2474 generic_field_type_freeze(type
);
2475 g_ptr_array_foreach(structure_type
->fields
,
2476 (GFunc
) freeze_structure_field
, NULL
);
2480 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2482 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2483 type
, struct bt_ctf_field_type_variant
, parent
);
2485 /* Cache the alignment */
2486 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2487 generic_field_type_freeze(type
);
2488 g_ptr_array_foreach(variant_type
->fields
,
2489 (GFunc
) freeze_structure_field
, NULL
);
2493 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2495 struct bt_ctf_field_type_array
*array_type
= container_of(
2496 type
, struct bt_ctf_field_type_array
, parent
);
2498 /* Cache the alignment */
2499 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2500 generic_field_type_freeze(type
);
2501 bt_ctf_field_type_freeze(array_type
->element_type
);
2505 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2507 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2508 type
, struct bt_ctf_field_type_sequence
, parent
);
2510 /* Cache the alignment */
2511 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2512 generic_field_type_freeze(type
);
2513 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2517 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2519 const char *encoding_string
;
2522 case CTF_STRING_NONE
:
2523 encoding_string
= "none";
2525 case CTF_STRING_ASCII
:
2526 encoding_string
= "ASCII";
2528 case CTF_STRING_UTF8
:
2529 encoding_string
= "UTF8";
2532 encoding_string
= "unknown";
2536 return encoding_string
;
2540 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2542 const char *base_string
;
2545 case BT_CTF_INTEGER_BASE_DECIMAL
:
2546 base_string
= "decimal";
2548 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2549 base_string
= "hexadecimal";
2551 case BT_CTF_INTEGER_BASE_OCTAL
:
2552 base_string
= "octal";
2554 case BT_CTF_INTEGER_BASE_BINARY
:
2555 base_string
= "binary";
2558 base_string
= "unknown";
2566 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2567 struct metadata_context
*context
)
2569 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2570 struct bt_ctf_field_type_integer
, parent
);
2573 g_string_append_printf(context
->string
,
2574 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2575 integer
->declaration
.len
, type
->declaration
->alignment
,
2576 (integer
->declaration
.signedness
? "true" : "false"),
2577 get_encoding_string(integer
->declaration
.encoding
),
2578 get_integer_base_string(integer
->declaration
.base
),
2579 get_byte_order_string(integer
->declaration
.byte_order
));
2580 if (integer
->mapped_clock
) {
2581 const char *clock_name
= bt_ctf_clock_get_name(
2582 integer
->mapped_clock
);
2589 g_string_append_printf(context
->string
,
2590 "; map = clock.%s.value", clock_name
);
2593 g_string_append(context
->string
, "; }");
2599 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2600 struct metadata_context
*context
)
2604 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2605 struct bt_ctf_field_type_enumeration
, parent
);
2606 struct bt_ctf_field_type
*container_type
;
2607 int container_signed
;
2609 ret
= bt_ctf_field_type_validate(type
);
2614 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2615 if (!container_type
) {
2620 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2621 if (container_signed
< 0) {
2622 ret
= container_signed
;
2623 goto error_put_container_type
;
2626 g_string_append(context
->string
, "enum : ");
2627 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2629 goto error_put_container_type
;
2632 g_string_append(context
->string
, " { ");
2633 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2634 struct enumeration_mapping
*mapping
=
2635 enumeration
->entries
->pdata
[entry
];
2637 if (container_signed
) {
2638 if (mapping
->range_start
._signed
==
2639 mapping
->range_end
._signed
) {
2640 g_string_append_printf(context
->string
,
2641 "\"%s\" = %" PRId64
,
2642 g_quark_to_string(mapping
->string
),
2643 mapping
->range_start
._signed
);
2645 g_string_append_printf(context
->string
,
2646 "\"%s\" = %" PRId64
" ... %" PRId64
,
2647 g_quark_to_string(mapping
->string
),
2648 mapping
->range_start
._signed
,
2649 mapping
->range_end
._signed
);
2652 if (mapping
->range_start
._unsigned
==
2653 mapping
->range_end
._unsigned
) {
2654 g_string_append_printf(context
->string
,
2655 "\"%s\" = %" PRIu64
,
2656 g_quark_to_string(mapping
->string
),
2657 mapping
->range_start
._unsigned
);
2659 g_string_append_printf(context
->string
,
2660 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2661 g_quark_to_string(mapping
->string
),
2662 mapping
->range_start
._unsigned
,
2663 mapping
->range_end
._unsigned
);
2667 g_string_append(context
->string
,
2668 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2672 if (context
->field_name
->len
) {
2673 g_string_append_printf(context
->string
, " %s",
2674 context
->field_name
->str
);
2675 g_string_assign(context
->field_name
, "");
2677 error_put_container_type
:
2678 bt_ctf_field_type_put(container_type
);
2684 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2685 struct metadata_context
*context
)
2687 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2688 type
, struct bt_ctf_field_type_floating_point
, parent
);
2690 g_string_append_printf(context
->string
,
2691 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2692 floating_point
->declaration
.exp
->len
,
2693 floating_point
->declaration
.mantissa
->len
+ 1,
2694 get_byte_order_string(floating_point
->declaration
.byte_order
),
2695 type
->declaration
->alignment
);
2700 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2701 struct metadata_context
*context
)
2704 unsigned int indent
;
2706 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2707 struct bt_ctf_field_type_structure
, parent
);
2708 GString
*structure_field_name
= context
->field_name
;
2710 context
->field_name
= g_string_new("");
2712 context
->current_indentation_level
++;
2713 g_string_append(context
->string
, "struct {\n");
2715 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2716 struct structure_field
*field
;
2718 for (indent
= 0; indent
< context
->current_indentation_level
;
2720 g_string_append_c(context
->string
, '\t');
2723 field
= structure
->fields
->pdata
[i
];
2724 g_string_assign(context
->field_name
,
2725 g_quark_to_string(field
->name
));
2726 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2731 if (context
->field_name
->len
) {
2732 g_string_append_printf(context
->string
, " %s",
2733 context
->field_name
->str
);
2735 g_string_append(context
->string
, ";\n");
2738 context
->current_indentation_level
--;
2739 for (indent
= 0; indent
< context
->current_indentation_level
;
2741 g_string_append_c(context
->string
, '\t');
2744 g_string_append_printf(context
->string
, "} align(%zu)",
2745 type
->declaration
->alignment
);
2747 g_string_free(context
->field_name
, TRUE
);
2748 context
->field_name
= structure_field_name
;
2753 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2754 struct metadata_context
*context
)
2757 unsigned int indent
;
2759 struct bt_ctf_field_type_variant
*variant
= container_of(
2760 type
, struct bt_ctf_field_type_variant
, parent
);
2761 GString
*variant_field_name
= context
->field_name
;
2763 context
->field_name
= g_string_new("");
2764 if (variant
->tag_name
->len
> 0) {
2765 g_string_append_printf(context
->string
,
2766 "variant <%s> {\n", variant
->tag_name
->str
);
2768 g_string_append(context
->string
, "variant {\n");
2771 context
->current_indentation_level
++;
2772 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2773 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2775 g_string_assign(context
->field_name
,
2776 g_quark_to_string(field
->name
));
2777 for (indent
= 0; indent
< context
->current_indentation_level
;
2779 g_string_append_c(context
->string
, '\t');
2782 g_string_assign(context
->field_name
,
2783 g_quark_to_string(field
->name
));
2784 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2789 if (context
->field_name
->len
) {
2790 g_string_append_printf(context
->string
, " %s;",
2791 context
->field_name
->str
);
2794 g_string_append_c(context
->string
, '\n');
2797 context
->current_indentation_level
--;
2798 for (indent
= 0; indent
< context
->current_indentation_level
;
2800 g_string_append_c(context
->string
, '\t');
2803 g_string_append(context
->string
, "}");
2805 g_string_free(context
->field_name
, TRUE
);
2806 context
->field_name
= variant_field_name
;
2811 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2812 struct metadata_context
*context
)
2815 struct bt_ctf_field_type_array
*array
= container_of(type
,
2816 struct bt_ctf_field_type_array
, parent
);
2818 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2823 if (context
->field_name
->len
) {
2824 g_string_append_printf(context
->string
, " %s[%u]",
2825 context
->field_name
->str
, array
->length
);
2826 g_string_assign(context
->field_name
, "");
2828 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2835 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2836 struct metadata_context
*context
)
2839 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2840 type
, struct bt_ctf_field_type_sequence
, parent
);
2842 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2847 if (context
->field_name
->len
) {
2848 g_string_append_printf(context
->string
, " %s[%s]",
2849 context
->field_name
->str
,
2850 sequence
->length_field_name
->str
);
2851 g_string_assign(context
->field_name
, "");
2853 g_string_append_printf(context
->string
, "[%s]",
2854 sequence
->length_field_name
->str
);
2861 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2862 struct metadata_context
*context
)
2864 struct bt_ctf_field_type_string
*string
= container_of(
2865 type
, struct bt_ctf_field_type_string
, parent
);
2867 g_string_append_printf(context
->string
,
2868 "string { encoding = %s; }",
2869 get_encoding_string(string
->declaration
.encoding
));
2874 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2875 int byte_order
, int set_native
)
2877 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2878 struct bt_ctf_field_type_integer
, parent
);
2881 integer_type
->declaration
.byte_order
=
2882 integer_type
->declaration
.byte_order
== 0 ?
2883 byte_order
: integer_type
->declaration
.byte_order
;
2885 integer_type
->declaration
.byte_order
= byte_order
;
2890 void bt_ctf_field_type_enumeration_set_byte_order(
2891 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2893 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2894 struct bt_ctf_field_type_enumeration
, parent
);
2896 /* Safe to assume that container is an integer */
2897 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2898 byte_order
, set_native
);
2902 void bt_ctf_field_type_floating_point_set_byte_order(
2903 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2905 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2906 container_of(type
, struct bt_ctf_field_type_floating_point
,
2910 floating_point_type
->declaration
.byte_order
=
2911 floating_point_type
->declaration
.byte_order
== 0 ?
2913 floating_point_type
->declaration
.byte_order
;
2914 floating_point_type
->sign
.byte_order
=
2915 floating_point_type
->sign
.byte_order
== 0 ?
2916 byte_order
: floating_point_type
->sign
.byte_order
;
2917 floating_point_type
->mantissa
.byte_order
=
2918 floating_point_type
->mantissa
.byte_order
== 0 ?
2919 byte_order
: floating_point_type
->mantissa
.byte_order
;
2920 floating_point_type
->exp
.byte_order
=
2921 floating_point_type
->exp
.byte_order
== 0 ?
2922 byte_order
: floating_point_type
->exp
.byte_order
;
2924 floating_point_type
->declaration
.byte_order
= byte_order
;
2925 floating_point_type
->sign
.byte_order
= byte_order
;
2926 floating_point_type
->mantissa
.byte_order
= byte_order
;
2927 floating_point_type
->exp
.byte_order
= byte_order
;
2932 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2933 int byte_order
, int set_native
)
2936 struct bt_ctf_field_type_structure
*structure_type
=
2937 container_of(type
, struct bt_ctf_field_type_structure
,
2940 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2941 struct structure_field
*field
= g_ptr_array_index(
2942 structure_type
->fields
, i
);
2943 struct bt_ctf_field_type
*field_type
= field
->type
;
2945 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2946 set_byte_order_funcs
[field_type
->declaration
->id
](
2947 field_type
, byte_order
, set_native
);
2953 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2954 int byte_order
, int set_native
)
2957 struct bt_ctf_field_type_variant
*variant_type
=
2958 container_of(type
, struct bt_ctf_field_type_variant
,
2961 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2962 struct structure_field
*field
= g_ptr_array_index(
2963 variant_type
->fields
, i
);
2964 struct bt_ctf_field_type
*field_type
= field
->type
;
2966 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2967 set_byte_order_funcs
[field_type
->declaration
->id
](
2968 field_type
, byte_order
, set_native
);
2974 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2975 int byte_order
, int set_native
)
2977 struct bt_ctf_field_type_array
*array_type
=
2978 container_of(type
, struct bt_ctf_field_type_array
,
2981 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2982 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2983 array_type
->element_type
, byte_order
, set_native
);
2988 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2989 int byte_order
, int set_native
)
2991 struct bt_ctf_field_type_sequence
*sequence_type
=
2992 container_of(type
, struct bt_ctf_field_type_sequence
,
2995 if (set_byte_order_funcs
[
2996 sequence_type
->element_type
->declaration
->id
]) {
2997 set_byte_order_funcs
[
2998 sequence_type
->element_type
->declaration
->id
](
2999 sequence_type
->element_type
, byte_order
, set_native
);
3004 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3005 struct bt_ctf_field_type
*type
)
3007 struct bt_ctf_field_type
*copy
;
3008 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3010 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3011 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3016 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3018 copy_integer
->declaration
= integer
->declaration
;
3019 if (integer
->mapped_clock
) {
3020 bt_ctf_clock_get(integer
->mapped_clock
);
3021 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3028 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3029 struct bt_ctf_field_type
*type
)
3032 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3033 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3035 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3038 /* Copy the source enumeration's container */
3039 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3040 if (!copy_container
) {
3044 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3048 copy_enumeration
= container_of(copy
,
3049 struct bt_ctf_field_type_enumeration
, parent
);
3051 /* Copy all enumaration entries */
3052 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3053 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3054 enumeration
->entries
, i
);
3055 struct enumeration_mapping
* copy_mapping
= g_new0(
3056 struct enumeration_mapping
, 1);
3058 if (!copy_mapping
) {
3062 *copy_mapping
= *mapping
;
3063 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3066 copy_enumeration
->declaration
= enumeration
->declaration
;
3068 if (copy_container
) {
3069 bt_ctf_field_type_put(copy_container
);
3073 if (copy_container
) {
3074 bt_ctf_field_type_put(copy_container
);
3076 bt_ctf_field_type_put(copy
);
3081 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3082 struct bt_ctf_field_type
*type
)
3084 struct bt_ctf_field_type
*copy
;
3085 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3087 floating_point
= container_of(type
,
3088 struct bt_ctf_field_type_floating_point
, parent
);
3089 copy
= bt_ctf_field_type_floating_point_create();
3094 copy_float
= container_of(copy
,
3095 struct bt_ctf_field_type_floating_point
, parent
);
3096 copy_float
->declaration
= floating_point
->declaration
;
3097 copy_float
->sign
= floating_point
->sign
;
3098 copy_float
->mantissa
= floating_point
->mantissa
;
3099 copy_float
->exp
= floating_point
->exp
;
3105 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3106 struct bt_ctf_field_type
*type
)
3109 GHashTableIter iter
;
3110 gpointer key
, value
;
3111 struct bt_ctf_field_type
*copy
;
3112 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3114 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3116 copy
= bt_ctf_field_type_structure_create();
3121 copy_structure
= container_of(copy
,
3122 struct bt_ctf_field_type_structure
, parent
);
3124 /* Copy field_name_to_index */
3125 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3126 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3127 g_hash_table_insert(copy_structure
->field_name_to_index
,
3131 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3132 struct structure_field
*entry
, *copy_entry
;
3133 struct bt_ctf_field_type
*copy_field
;
3135 copy_entry
= g_new0(struct structure_field
, 1);
3140 entry
= g_ptr_array_index(structure
->fields
, i
);
3141 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3147 copy_entry
->name
= entry
->name
;
3148 copy_entry
->type
= copy_field
;
3149 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3152 copy_structure
->declaration
= structure
->declaration
;
3156 bt_ctf_field_type_put(copy
);
3161 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3162 struct bt_ctf_field_type
*type
)
3165 GHashTableIter iter
;
3166 gpointer key
, value
;
3167 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3168 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3170 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3173 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3179 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3180 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3185 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3188 /* Copy field_name_to_index */
3189 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3190 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3191 g_hash_table_insert(copy_variant
->field_name_to_index
,
3195 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3196 struct structure_field
*entry
, *copy_entry
;
3197 struct bt_ctf_field_type
*copy_field
;
3199 copy_entry
= g_new0(struct structure_field
, 1);
3204 entry
= g_ptr_array_index(variant
->fields
, i
);
3205 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3211 copy_entry
->name
= entry
->name
;
3212 copy_entry
->type
= copy_field
;
3213 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3216 copy_variant
->declaration
= variant
->declaration
;
3217 if (variant
->tag_path
) {
3218 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3220 if (!copy_variant
->tag_path
) {
3226 bt_ctf_field_type_put(copy_tag
);
3232 bt_ctf_field_type_put(copy_tag
);
3235 bt_ctf_field_type_put(copy
);
3240 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3241 struct bt_ctf_field_type
*type
)
3243 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3244 struct bt_ctf_field_type_array
*array
, *copy_array
;
3246 array
= container_of(type
, struct bt_ctf_field_type_array
,
3248 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3249 if (!copy_element
) {
3253 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3258 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3260 copy_array
->declaration
= array
->declaration
;
3263 bt_ctf_field_type_put(copy_element
);
3270 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3271 struct bt_ctf_field_type
*type
)
3273 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3274 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3276 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3278 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3279 if (!copy_element
) {
3283 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3284 sequence
->length_field_name
->len
?
3285 sequence
->length_field_name
->str
: NULL
);
3290 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3292 copy_sequence
->declaration
= sequence
->declaration
;
3293 if (sequence
->length_field_path
) {
3294 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3295 sequence
->length_field_path
);
3296 if (!copy_sequence
->length_field_path
) {
3302 bt_ctf_field_type_put(copy_element
);
3308 bt_ctf_field_type_put(copy
);
3315 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3316 struct bt_ctf_field_type
*type
)
3318 struct bt_ctf_field_type
*copy
;
3319 struct bt_ctf_field_type_string
*string
, *copy_string
;
3321 copy
= bt_ctf_field_type_string_create();
3326 string
= container_of(type
, struct bt_ctf_field_type_string
,
3328 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3330 copy_string
->declaration
= string
->declaration
;