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 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2226 struct bt_ctf_field_path
*path
)
2229 struct bt_ctf_field_type_variant
*variant
;
2231 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2236 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2238 if (variant
->tag_path
) {
2239 bt_ctf_field_path_destroy(variant
->tag_path
);
2241 variant
->tag_path
= path
;
2247 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2248 struct bt_ctf_field_type
*tag
)
2251 struct bt_ctf_field_type_variant
*variant
;
2253 if (!type
|| !tag
|| type
->frozen
||
2254 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2259 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2261 bt_ctf_field_type_get(tag
);
2263 bt_ctf_field_type_put(&variant
->tag
->parent
);
2265 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2272 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2273 struct bt_ctf_field_type
*field
, int index
)
2276 struct bt_ctf_field_type_variant
*variant
;
2278 if (!type
|| !field
||
2279 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2284 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2286 if (index
< 0 || index
>= variant
->fields
->len
) {
2291 bt_ctf_field_type_get(field
);
2292 bt_ctf_field_type_put(((struct structure_field
*)
2293 g_ptr_array_index(variant
->fields
, index
))->type
);
2294 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2301 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2303 struct bt_ctf_field_type_integer
*integer
=
2304 (struct bt_ctf_field_type_integer
*) type
;
2310 if (integer
->mapped_clock
) {
2311 bt_ctf_clock_put(integer
->mapped_clock
);
2317 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2319 struct bt_ctf_field_type_enumeration
*enumeration
=
2320 (struct bt_ctf_field_type_enumeration
*) type
;
2326 g_ptr_array_free(enumeration
->entries
, TRUE
);
2327 bt_ctf_field_type_put(enumeration
->container
);
2328 g_free(enumeration
);
2332 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2334 struct bt_ctf_field_type_floating_point
*floating_point
=
2335 (struct bt_ctf_field_type_floating_point
*) type
;
2341 g_free(floating_point
);
2345 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2347 struct bt_ctf_field_type_structure
*structure
=
2348 (struct bt_ctf_field_type_structure
*) type
;
2354 g_ptr_array_free(structure
->fields
, TRUE
);
2355 g_hash_table_destroy(structure
->field_name_to_index
);
2360 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2362 struct bt_ctf_field_type_variant
*variant
=
2363 (struct bt_ctf_field_type_variant
*) type
;
2369 g_ptr_array_free(variant
->fields
, TRUE
);
2370 g_hash_table_destroy(variant
->field_name_to_index
);
2371 g_string_free(variant
->tag_name
, TRUE
);
2372 bt_ctf_field_type_put(&variant
->tag
->parent
);
2373 bt_ctf_field_path_destroy(variant
->tag_path
);
2378 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2380 struct bt_ctf_field_type_array
*array
=
2381 (struct bt_ctf_field_type_array
*) type
;
2387 bt_ctf_field_type_put(array
->element_type
);
2392 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2394 struct bt_ctf_field_type_sequence
*sequence
=
2395 (struct bt_ctf_field_type_sequence
*) type
;
2401 bt_ctf_field_type_put(sequence
->element_type
);
2402 g_string_free(sequence
->length_field_name
, TRUE
);
2403 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2408 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2410 struct bt_ctf_field_type_string
*string
=
2411 (struct bt_ctf_field_type_string
*) type
;
2421 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2427 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2429 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2430 type
, struct bt_ctf_field_type_enumeration
, parent
);
2432 generic_field_type_freeze(type
);
2433 bt_ctf_field_type_freeze(enumeration_type
->container
);
2437 void freeze_structure_field(struct structure_field
*field
)
2439 bt_ctf_field_type_freeze(field
->type
);
2443 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2445 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2446 type
, struct bt_ctf_field_type_structure
, parent
);
2448 /* Cache the alignment */
2449 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2450 generic_field_type_freeze(type
);
2451 g_ptr_array_foreach(structure_type
->fields
,
2452 (GFunc
) freeze_structure_field
, NULL
);
2456 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2458 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2459 type
, struct bt_ctf_field_type_variant
, parent
);
2461 /* Cache the alignment */
2462 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2463 generic_field_type_freeze(type
);
2464 g_ptr_array_foreach(variant_type
->fields
,
2465 (GFunc
) freeze_structure_field
, NULL
);
2469 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2471 struct bt_ctf_field_type_array
*array_type
= container_of(
2472 type
, struct bt_ctf_field_type_array
, parent
);
2474 /* Cache the alignment */
2475 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2476 generic_field_type_freeze(type
);
2477 bt_ctf_field_type_freeze(array_type
->element_type
);
2481 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2483 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2484 type
, struct bt_ctf_field_type_sequence
, parent
);
2486 /* Cache the alignment */
2487 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2488 generic_field_type_freeze(type
);
2489 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2493 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2495 const char *encoding_string
;
2498 case CTF_STRING_NONE
:
2499 encoding_string
= "none";
2501 case CTF_STRING_ASCII
:
2502 encoding_string
= "ASCII";
2504 case CTF_STRING_UTF8
:
2505 encoding_string
= "UTF8";
2508 encoding_string
= "unknown";
2512 return encoding_string
;
2516 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2518 const char *base_string
;
2521 case BT_CTF_INTEGER_BASE_DECIMAL
:
2522 base_string
= "decimal";
2524 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2525 base_string
= "hexadecimal";
2527 case BT_CTF_INTEGER_BASE_OCTAL
:
2528 base_string
= "octal";
2530 case BT_CTF_INTEGER_BASE_BINARY
:
2531 base_string
= "binary";
2534 base_string
= "unknown";
2542 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2543 struct metadata_context
*context
)
2545 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2546 struct bt_ctf_field_type_integer
, parent
);
2549 g_string_append_printf(context
->string
,
2550 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2551 integer
->declaration
.len
, type
->declaration
->alignment
,
2552 (integer
->declaration
.signedness
? "true" : "false"),
2553 get_encoding_string(integer
->declaration
.encoding
),
2554 get_integer_base_string(integer
->declaration
.base
),
2555 get_byte_order_string(integer
->declaration
.byte_order
));
2556 if (integer
->mapped_clock
) {
2557 const char *clock_name
= bt_ctf_clock_get_name(
2558 integer
->mapped_clock
);
2565 g_string_append_printf(context
->string
,
2566 "; map = clock.%s.value", clock_name
);
2569 g_string_append(context
->string
, "; }");
2575 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2576 struct metadata_context
*context
)
2580 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2581 struct bt_ctf_field_type_enumeration
, parent
);
2582 struct bt_ctf_field_type
*container_type
;
2583 int container_signed
;
2585 ret
= bt_ctf_field_type_validate(type
);
2590 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2591 if (!container_type
) {
2596 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2597 if (container_signed
< 0) {
2598 ret
= container_signed
;
2599 goto error_put_container_type
;
2602 g_string_append(context
->string
, "enum : ");
2603 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2605 goto error_put_container_type
;
2608 g_string_append(context
->string
, " { ");
2609 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2610 struct enumeration_mapping
*mapping
=
2611 enumeration
->entries
->pdata
[entry
];
2613 if (container_signed
) {
2614 if (mapping
->range_start
._signed
==
2615 mapping
->range_end
._signed
) {
2616 g_string_append_printf(context
->string
,
2617 "\"%s\" = %" PRId64
,
2618 g_quark_to_string(mapping
->string
),
2619 mapping
->range_start
._signed
);
2621 g_string_append_printf(context
->string
,
2622 "\"%s\" = %" PRId64
" ... %" PRId64
,
2623 g_quark_to_string(mapping
->string
),
2624 mapping
->range_start
._signed
,
2625 mapping
->range_end
._signed
);
2628 if (mapping
->range_start
._unsigned
==
2629 mapping
->range_end
._unsigned
) {
2630 g_string_append_printf(context
->string
,
2631 "\"%s\" = %" PRIu64
,
2632 g_quark_to_string(mapping
->string
),
2633 mapping
->range_start
._unsigned
);
2635 g_string_append_printf(context
->string
,
2636 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2637 g_quark_to_string(mapping
->string
),
2638 mapping
->range_start
._unsigned
,
2639 mapping
->range_end
._unsigned
);
2643 g_string_append(context
->string
,
2644 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2648 if (context
->field_name
->len
) {
2649 g_string_append_printf(context
->string
, " %s",
2650 context
->field_name
->str
);
2651 g_string_assign(context
->field_name
, "");
2653 error_put_container_type
:
2654 bt_ctf_field_type_put(container_type
);
2660 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2661 struct metadata_context
*context
)
2663 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2664 type
, struct bt_ctf_field_type_floating_point
, parent
);
2666 g_string_append_printf(context
->string
,
2667 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2668 floating_point
->declaration
.exp
->len
,
2669 floating_point
->declaration
.mantissa
->len
+ 1,
2670 get_byte_order_string(floating_point
->declaration
.byte_order
),
2671 type
->declaration
->alignment
);
2676 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2677 struct metadata_context
*context
)
2680 unsigned int indent
;
2682 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2683 struct bt_ctf_field_type_structure
, parent
);
2684 GString
*structure_field_name
= context
->field_name
;
2686 context
->field_name
= g_string_new("");
2688 context
->current_indentation_level
++;
2689 g_string_append(context
->string
, "struct {\n");
2691 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2692 struct structure_field
*field
;
2694 for (indent
= 0; indent
< context
->current_indentation_level
;
2696 g_string_append_c(context
->string
, '\t');
2699 field
= structure
->fields
->pdata
[i
];
2700 g_string_assign(context
->field_name
,
2701 g_quark_to_string(field
->name
));
2702 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2707 if (context
->field_name
->len
) {
2708 g_string_append_printf(context
->string
, " %s",
2709 context
->field_name
->str
);
2711 g_string_append(context
->string
, ";\n");
2714 context
->current_indentation_level
--;
2715 for (indent
= 0; indent
< context
->current_indentation_level
;
2717 g_string_append_c(context
->string
, '\t');
2720 g_string_append_printf(context
->string
, "} align(%zu)",
2721 type
->declaration
->alignment
);
2723 g_string_free(context
->field_name
, TRUE
);
2724 context
->field_name
= structure_field_name
;
2729 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2730 struct metadata_context
*context
)
2733 unsigned int indent
;
2735 struct bt_ctf_field_type_variant
*variant
= container_of(
2736 type
, struct bt_ctf_field_type_variant
, parent
);
2737 GString
*variant_field_name
= context
->field_name
;
2739 context
->field_name
= g_string_new("");
2740 if (variant
->tag_name
->len
> 0) {
2741 g_string_append_printf(context
->string
,
2742 "variant <%s> {\n", variant
->tag_name
->str
);
2744 g_string_append(context
->string
, "variant {\n");
2747 context
->current_indentation_level
++;
2748 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2749 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2751 g_string_assign(context
->field_name
,
2752 g_quark_to_string(field
->name
));
2753 for (indent
= 0; indent
< context
->current_indentation_level
;
2755 g_string_append_c(context
->string
, '\t');
2758 g_string_assign(context
->field_name
,
2759 g_quark_to_string(field
->name
));
2760 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2765 if (context
->field_name
->len
) {
2766 g_string_append_printf(context
->string
, " %s;",
2767 context
->field_name
->str
);
2770 g_string_append_c(context
->string
, '\n');
2773 context
->current_indentation_level
--;
2774 for (indent
= 0; indent
< context
->current_indentation_level
;
2776 g_string_append_c(context
->string
, '\t');
2779 g_string_append(context
->string
, "}");
2781 g_string_free(context
->field_name
, TRUE
);
2782 context
->field_name
= variant_field_name
;
2787 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2788 struct metadata_context
*context
)
2791 struct bt_ctf_field_type_array
*array
= container_of(type
,
2792 struct bt_ctf_field_type_array
, parent
);
2794 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2799 if (context
->field_name
->len
) {
2800 g_string_append_printf(context
->string
, " %s[%u]",
2801 context
->field_name
->str
, array
->length
);
2802 g_string_assign(context
->field_name
, "");
2804 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2811 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2812 struct metadata_context
*context
)
2815 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2816 type
, struct bt_ctf_field_type_sequence
, parent
);
2818 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2823 if (context
->field_name
->len
) {
2824 g_string_append_printf(context
->string
, " %s[%s]",
2825 context
->field_name
->str
,
2826 sequence
->length_field_name
->str
);
2827 g_string_assign(context
->field_name
, "");
2829 g_string_append_printf(context
->string
, "[%s]",
2830 sequence
->length_field_name
->str
);
2837 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2838 struct metadata_context
*context
)
2840 struct bt_ctf_field_type_string
*string
= container_of(
2841 type
, struct bt_ctf_field_type_string
, parent
);
2843 g_string_append_printf(context
->string
,
2844 "string { encoding = %s; }",
2845 get_encoding_string(string
->declaration
.encoding
));
2850 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2851 int byte_order
, int set_native
)
2853 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2854 struct bt_ctf_field_type_integer
, parent
);
2857 integer_type
->declaration
.byte_order
=
2858 integer_type
->declaration
.byte_order
== 0 ?
2859 byte_order
: integer_type
->declaration
.byte_order
;
2861 integer_type
->declaration
.byte_order
= byte_order
;
2866 void bt_ctf_field_type_enumeration_set_byte_order(
2867 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2869 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2870 struct bt_ctf_field_type_enumeration
, parent
);
2872 /* Safe to assume that container is an integer */
2873 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2874 byte_order
, set_native
);
2878 void bt_ctf_field_type_floating_point_set_byte_order(
2879 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2881 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2882 container_of(type
, struct bt_ctf_field_type_floating_point
,
2886 floating_point_type
->declaration
.byte_order
=
2887 floating_point_type
->declaration
.byte_order
== 0 ?
2889 floating_point_type
->declaration
.byte_order
;
2890 floating_point_type
->sign
.byte_order
=
2891 floating_point_type
->sign
.byte_order
== 0 ?
2892 byte_order
: floating_point_type
->sign
.byte_order
;
2893 floating_point_type
->mantissa
.byte_order
=
2894 floating_point_type
->mantissa
.byte_order
== 0 ?
2895 byte_order
: floating_point_type
->mantissa
.byte_order
;
2896 floating_point_type
->exp
.byte_order
=
2897 floating_point_type
->exp
.byte_order
== 0 ?
2898 byte_order
: floating_point_type
->exp
.byte_order
;
2900 floating_point_type
->declaration
.byte_order
= byte_order
;
2901 floating_point_type
->sign
.byte_order
= byte_order
;
2902 floating_point_type
->mantissa
.byte_order
= byte_order
;
2903 floating_point_type
->exp
.byte_order
= byte_order
;
2908 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2909 int byte_order
, int set_native
)
2912 struct bt_ctf_field_type_structure
*structure_type
=
2913 container_of(type
, struct bt_ctf_field_type_structure
,
2916 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2917 struct structure_field
*field
= g_ptr_array_index(
2918 structure_type
->fields
, i
);
2919 struct bt_ctf_field_type
*field_type
= field
->type
;
2921 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2922 set_byte_order_funcs
[field_type
->declaration
->id
](
2923 field_type
, byte_order
, set_native
);
2929 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2930 int byte_order
, int set_native
)
2933 struct bt_ctf_field_type_variant
*variant_type
=
2934 container_of(type
, struct bt_ctf_field_type_variant
,
2937 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2938 struct structure_field
*field
= g_ptr_array_index(
2939 variant_type
->fields
, i
);
2940 struct bt_ctf_field_type
*field_type
= field
->type
;
2942 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2943 set_byte_order_funcs
[field_type
->declaration
->id
](
2944 field_type
, byte_order
, set_native
);
2950 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2951 int byte_order
, int set_native
)
2953 struct bt_ctf_field_type_array
*array_type
=
2954 container_of(type
, struct bt_ctf_field_type_array
,
2957 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2958 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2959 array_type
->element_type
, byte_order
, set_native
);
2964 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2965 int byte_order
, int set_native
)
2967 struct bt_ctf_field_type_sequence
*sequence_type
=
2968 container_of(type
, struct bt_ctf_field_type_sequence
,
2971 if (set_byte_order_funcs
[
2972 sequence_type
->element_type
->declaration
->id
]) {
2973 set_byte_order_funcs
[
2974 sequence_type
->element_type
->declaration
->id
](
2975 sequence_type
->element_type
, byte_order
, set_native
);
2980 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2981 struct bt_ctf_field_type
*type
)
2983 struct bt_ctf_field_type
*copy
;
2984 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
2986 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
2987 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
2992 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
2994 copy_integer
->declaration
= integer
->declaration
;
2995 if (integer
->mapped_clock
) {
2996 bt_ctf_clock_get(integer
->mapped_clock
);
2997 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3004 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3005 struct bt_ctf_field_type
*type
)
3008 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3009 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3011 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3014 /* Copy the source enumeration's container */
3015 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3016 if (!copy_container
) {
3020 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3024 copy_enumeration
= container_of(copy
,
3025 struct bt_ctf_field_type_enumeration
, parent
);
3027 /* Copy all enumaration entries */
3028 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3029 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3030 enumeration
->entries
, i
);
3031 struct enumeration_mapping
* copy_mapping
= g_new0(
3032 struct enumeration_mapping
, 1);
3034 if (!copy_mapping
) {
3038 *copy_mapping
= *mapping
;
3039 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3042 copy_enumeration
->declaration
= enumeration
->declaration
;
3044 if (copy_container
) {
3045 bt_ctf_field_type_put(copy_container
);
3049 if (copy_container
) {
3050 bt_ctf_field_type_put(copy_container
);
3052 bt_ctf_field_type_put(copy
);
3057 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3058 struct bt_ctf_field_type
*type
)
3060 struct bt_ctf_field_type
*copy
;
3061 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3063 floating_point
= container_of(type
,
3064 struct bt_ctf_field_type_floating_point
, parent
);
3065 copy
= bt_ctf_field_type_floating_point_create();
3070 copy_float
= container_of(copy
,
3071 struct bt_ctf_field_type_floating_point
, parent
);
3072 copy_float
->declaration
= floating_point
->declaration
;
3073 copy_float
->sign
= floating_point
->sign
;
3074 copy_float
->mantissa
= floating_point
->mantissa
;
3075 copy_float
->exp
= floating_point
->exp
;
3081 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3082 struct bt_ctf_field_type
*type
)
3085 GHashTableIter iter
;
3086 gpointer key
, value
;
3087 struct bt_ctf_field_type
*copy
;
3088 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3090 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3092 copy
= bt_ctf_field_type_structure_create();
3097 copy_structure
= container_of(copy
,
3098 struct bt_ctf_field_type_structure
, parent
);
3100 /* Copy field_name_to_index */
3101 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3102 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3103 g_hash_table_insert(copy_structure
->field_name_to_index
,
3107 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3108 struct structure_field
*entry
, *copy_entry
;
3109 struct bt_ctf_field_type
*copy_field
;
3111 copy_entry
= g_new0(struct structure_field
, 1);
3116 entry
= g_ptr_array_index(structure
->fields
, i
);
3117 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3123 copy_entry
->name
= entry
->name
;
3124 copy_entry
->type
= copy_field
;
3125 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3128 copy_structure
->declaration
= structure
->declaration
;
3132 bt_ctf_field_type_put(copy
);
3137 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3138 struct bt_ctf_field_type
*type
)
3141 GHashTableIter iter
;
3142 gpointer key
, value
;
3143 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3144 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3146 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3149 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3155 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3156 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3161 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3164 /* Copy field_name_to_index */
3165 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3166 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3167 g_hash_table_insert(copy_variant
->field_name_to_index
,
3171 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3172 struct structure_field
*entry
, *copy_entry
;
3173 struct bt_ctf_field_type
*copy_field
;
3175 copy_entry
= g_new0(struct structure_field
, 1);
3180 entry
= g_ptr_array_index(variant
->fields
, i
);
3181 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3187 copy_entry
->name
= entry
->name
;
3188 copy_entry
->type
= copy_field
;
3189 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3192 copy_variant
->declaration
= variant
->declaration
;
3193 if (variant
->tag_path
) {
3194 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3196 if (!copy_variant
->tag_path
) {
3202 bt_ctf_field_type_put(copy_tag
);
3208 bt_ctf_field_type_put(copy_tag
);
3211 bt_ctf_field_type_put(copy
);
3216 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3217 struct bt_ctf_field_type
*type
)
3219 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3220 struct bt_ctf_field_type_array
*array
, *copy_array
;
3222 array
= container_of(type
, struct bt_ctf_field_type_array
,
3224 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3225 if (!copy_element
) {
3229 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3234 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3236 copy_array
->declaration
= array
->declaration
;
3239 bt_ctf_field_type_put(copy_element
);
3246 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3247 struct bt_ctf_field_type
*type
)
3249 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3250 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3252 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3254 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3255 if (!copy_element
) {
3259 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3260 sequence
->length_field_name
->len
?
3261 sequence
->length_field_name
->str
: NULL
);
3266 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3268 copy_sequence
->declaration
= sequence
->declaration
;
3269 if (sequence
->length_field_path
) {
3270 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3271 sequence
->length_field_path
);
3272 if (!copy_sequence
->length_field_path
) {
3278 bt_ctf_field_type_put(copy_element
);
3284 bt_ctf_field_type_put(copy
);
3291 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3292 struct bt_ctf_field_type
*type
)
3294 struct bt_ctf_field_type
*copy
;
3295 struct bt_ctf_field_type_string
*string
, *copy_string
;
3297 copy
= bt_ctf_field_type_string_create();
3302 string
= container_of(type
, struct bt_ctf_field_type_string
,
3304 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3306 copy_string
->declaration
= string
->declaration
;