4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-types.h>
30 #include <babeltrace/ctf-ir/event-types-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ctf-ir/clock.h>
33 #include <babeltrace/ctf-writer/writer-internal.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/endian.h>
40 struct range_overlap_query
{
55 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*);
57 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*);
59 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*);
61 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*);
63 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*);
65 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*);
67 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*);
69 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*);
71 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*);
74 void (* const type_destroy_funcs
[])(struct bt_ctf_ref
*) = {
75 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
77 bt_ctf_field_type_enumeration_destroy
,
79 bt_ctf_field_type_floating_point_destroy
,
80 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
81 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
82 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
83 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
84 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
88 void generic_field_type_freeze(struct bt_ctf_field_type
*);
90 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
92 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
101 type_freeze_func
const type_freeze_funcs
[] = {
102 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
103 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
104 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
105 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
106 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
107 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
108 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
109 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
113 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
114 struct metadata_context
*);
116 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_floating_point_serialize(
120 struct bt_ctf_field_type
*, struct metadata_context
*);
122 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
138 type_serialize_func
const type_serialize_funcs
[] = {
139 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
141 bt_ctf_field_type_enumeration_serialize
,
143 bt_ctf_field_type_floating_point_serialize
,
145 bt_ctf_field_type_structure_serialize
,
146 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
147 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
148 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
149 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
153 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
154 int byte_order
, int set_native
);
156 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_floating_point_set_byte_order(
160 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
162 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 /* The set_native flag only set the byte order if it is set to native */
176 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
177 int byte_order
, int set_native
) = {
178 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
180 bt_ctf_field_type_enumeration_set_byte_order
,
182 bt_ctf_field_type_floating_point_set_byte_order
,
184 bt_ctf_field_type_structure_set_byte_order
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
188 [CTF_TYPE_STRING
] = NULL
,
192 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
193 struct bt_ctf_field_type
*);
195 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
214 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
218 struct bt_ctf_field_type
*) = {
219 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
220 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
221 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
222 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
223 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
224 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
225 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
226 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
230 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
236 void destroy_structure_field(struct structure_field
*field
)
239 bt_ctf_field_type_put(field
->type
);
246 void check_ranges_overlap(gpointer element
, gpointer query
)
248 struct enumeration_mapping
*mapping
= element
;
249 struct range_overlap_query
*overlap_query
= query
;
251 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
252 && overlap_query
->range_start
._signed
<=
253 mapping
->range_end
._signed
) {
254 overlap_query
->overlaps
= 1;
255 overlap_query
->mapping_name
= mapping
->string
;
258 overlap_query
->overlaps
|=
259 mapping
->string
== overlap_query
->mapping_name
;
263 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
265 struct enumeration_mapping
*mapping
= element
;
266 struct range_overlap_query
*overlap_query
= query
;
268 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
269 && overlap_query
->range_start
._unsigned
<=
270 mapping
->range_end
._unsigned
) {
271 overlap_query
->overlaps
= 1;
272 overlap_query
->mapping_name
= mapping
->string
;
275 overlap_query
->overlaps
|=
276 mapping
->string
== overlap_query
->mapping_name
;
280 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
281 struct enumeration_mapping
**b
)
283 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
287 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
288 struct enumeration_mapping
**b
)
290 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
294 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
)
296 enum ctf_type_id type_id
= type
->declaration
->id
;
299 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
300 (type_id
< NR_CTF_TYPES
));
302 bt_ctf_ref_init(&type
->ref_count
);
303 type
->freeze
= type_freeze_funcs
[type_id
];
304 type
->serialize
= type_serialize_funcs
[type_id
];
305 ret
= bt_ctf_field_type_set_byte_order(type
, BT_CTF_BYTE_ORDER_NATIVE
);
307 type
->declaration
->alignment
= 1;
311 int add_structure_field(GPtrArray
*fields
,
312 GHashTable
*field_name_to_index
,
313 struct bt_ctf_field_type
*field_type
,
314 const char *field_name
)
317 GQuark name_quark
= g_quark_from_string(field_name
);
318 struct structure_field
*field
;
320 /* Make sure structure does not contain a field of the same name */
321 if (g_hash_table_lookup_extended(field_name_to_index
,
322 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
327 field
= g_new0(struct structure_field
, 1);
333 bt_ctf_field_type_get(field_type
);
334 field
->name
= name_quark
;
335 field
->type
= field_type
;
336 g_hash_table_insert(field_name_to_index
,
337 (gpointer
) (unsigned long) name_quark
,
338 (gpointer
) (unsigned long) fields
->len
);
339 g_ptr_array_add(fields
, field
);
345 void bt_ctf_field_type_destroy(struct bt_ctf_ref
*ref
)
347 struct bt_ctf_field_type
*type
;
348 enum ctf_type_id type_id
;
354 type
= container_of(ref
, struct bt_ctf_field_type
, ref_count
);
355 type_id
= type
->declaration
->id
;
356 if (type_id
<= CTF_TYPE_UNKNOWN
||
357 type_id
>= NR_CTF_TYPES
) {
361 type_destroy_funcs
[type_id
](ref
);
365 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
374 switch (type
->declaration
->id
) {
377 struct bt_ctf_field_type_enumeration
*enumeration
=
378 container_of(type
, struct bt_ctf_field_type_enumeration
,
381 /* Ensure enum has entries */
382 ret
= enumeration
->entries
->len
? 0 : -1;
392 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
394 struct bt_ctf_field_type_integer
*integer
=
395 g_new0(struct bt_ctf_field_type_integer
, 1);
397 if (!integer
|| size
== 0 || size
> 64) {
401 integer
->parent
.declaration
= &integer
->declaration
.p
;
402 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
403 integer
->declaration
.len
= size
;
404 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
405 integer
->declaration
.encoding
= CTF_STRING_NONE
;
406 bt_ctf_field_type_init(&integer
->parent
);
407 return &integer
->parent
;
410 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
413 struct bt_ctf_field_type_integer
*integer
;
415 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
420 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
421 ret
= (int) integer
->declaration
.len
;
426 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
429 struct bt_ctf_field_type_integer
*integer
;
431 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
436 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
437 ret
= integer
->declaration
.signedness
;
442 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
446 struct bt_ctf_field_type_integer
*integer
;
448 if (!type
|| type
->frozen
||
449 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
454 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
455 integer
->declaration
.signedness
= !!is_signed
;
460 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
461 struct bt_ctf_field_type
*type
)
463 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
464 struct bt_ctf_field_type_integer
*integer
;
466 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
470 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
471 ret
= integer
->declaration
.base
;
476 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
477 enum bt_ctf_integer_base base
)
481 if (!type
|| type
->frozen
||
482 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
488 case BT_CTF_INTEGER_BASE_BINARY
:
489 case BT_CTF_INTEGER_BASE_OCTAL
:
490 case BT_CTF_INTEGER_BASE_DECIMAL
:
491 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
493 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
494 struct bt_ctf_field_type_integer
, parent
);
495 integer
->declaration
.base
= base
;
505 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
506 struct bt_ctf_field_type
*type
)
508 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
509 struct bt_ctf_field_type_integer
*integer
;
511 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
515 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
516 ret
= integer
->declaration
.encoding
;
521 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
522 enum ctf_string_encoding encoding
)
525 struct bt_ctf_field_type_integer
*integer
;
527 if (!type
|| type
->frozen
||
528 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
529 (encoding
< CTF_STRING_NONE
) ||
530 (encoding
>= CTF_STRING_UNKNOWN
)) {
535 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
536 integer
->declaration
.encoding
= encoding
;
541 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
542 struct bt_ctf_field_type
*type
)
544 struct bt_ctf_field_type_integer
*integer
;
545 struct bt_ctf_clock
*clock
= NULL
;
551 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
552 clock
= integer
->mapped_clock
;
554 bt_ctf_clock_get(clock
);
560 int bt_ctf_field_type_integer_set_mapped_clock(
561 struct bt_ctf_field_type
*type
,
562 struct bt_ctf_clock
*clock
)
564 struct bt_ctf_field_type_integer
*integer
;
567 if (!type
|| type
->frozen
) {
572 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
573 if (integer
->mapped_clock
) {
574 bt_ctf_clock_put(integer
->mapped_clock
);
578 bt_ctf_clock_get(clock
);
581 integer
->mapped_clock
= clock
;
586 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
587 struct bt_ctf_field_type
*integer_container_type
)
589 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
591 if (!integer_container_type
) {
595 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
599 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
604 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
605 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
606 bt_ctf_field_type_get(integer_container_type
);
607 enumeration
->container
= integer_container_type
;
608 enumeration
->entries
= g_ptr_array_new_with_free_func(
609 (GDestroyNotify
)destroy_enumeration_mapping
);
610 bt_ctf_field_type_init(&enumeration
->parent
);
611 return &enumeration
->parent
;
617 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
618 struct bt_ctf_field_type
*type
)
620 struct bt_ctf_field_type
*container_type
= NULL
;
621 struct bt_ctf_field_type_enumeration
*enumeration_type
;
627 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
631 enumeration_type
= container_of(type
,
632 struct bt_ctf_field_type_enumeration
, parent
);
633 container_type
= enumeration_type
->container
;
634 bt_ctf_field_type_get(container_type
);
636 return container_type
;
639 int bt_ctf_field_type_enumeration_add_mapping(
640 struct bt_ctf_field_type
*type
, const char *string
,
641 int64_t range_start
, int64_t range_end
)
645 struct enumeration_mapping
*mapping
;
646 struct bt_ctf_field_type_enumeration
*enumeration
;
647 struct range_overlap_query query
;
648 char *escaped_string
;
650 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
652 (range_end
< range_start
)) {
657 if (!string
|| strlen(string
) == 0) {
662 escaped_string
= g_strescape(string
, NULL
);
663 if (!escaped_string
) {
668 mapping_name
= g_quark_from_string(escaped_string
);
669 query
= (struct range_overlap_query
) {
670 .range_start
._signed
= range_start
,
671 .range_end
._signed
= range_end
,
672 .mapping_name
= mapping_name
,
674 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
677 /* Check that the range does not overlap with one already present */
678 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
679 if (query
.overlaps
) {
684 mapping
= g_new(struct enumeration_mapping
, 1);
690 *mapping
= (struct enumeration_mapping
) {
691 .range_start
._signed
= range_start
,
692 .range_end
._signed
= range_end
, .string
= mapping_name
};
693 g_ptr_array_add(enumeration
->entries
, mapping
);
694 g_ptr_array_sort(enumeration
->entries
,
695 (GCompareFunc
)compare_enumeration_mappings_signed
);
697 free(escaped_string
);
702 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
703 struct bt_ctf_field_type
*type
, const char *string
,
704 uint64_t range_start
, uint64_t range_end
)
708 struct enumeration_mapping
*mapping
;
709 struct bt_ctf_field_type_enumeration
*enumeration
;
710 struct range_overlap_query query
;
711 char *escaped_string
;
713 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
715 (range_end
< range_start
)) {
720 if (!string
|| strlen(string
) == 0) {
725 escaped_string
= g_strescape(string
, NULL
);
726 if (!escaped_string
) {
731 mapping_name
= g_quark_from_string(escaped_string
);
732 query
= (struct range_overlap_query
) {
733 .range_start
._unsigned
= range_start
,
734 .range_end
._unsigned
= range_end
,
735 .mapping_name
= mapping_name
,
737 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
740 /* Check that the range does not overlap with one already present */
741 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
743 if (query
.overlaps
) {
748 mapping
= g_new(struct enumeration_mapping
, 1);
754 *mapping
= (struct enumeration_mapping
) {
755 .range_start
._unsigned
= range_start
,
756 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
757 g_ptr_array_add(enumeration
->entries
, mapping
);
758 g_ptr_array_sort(enumeration
->entries
,
759 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
761 free(escaped_string
);
766 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
767 struct bt_ctf_field_type_enumeration
*enumeration_type
,
770 const char *name
= NULL
;
771 struct range_overlap_query query
=
772 (struct range_overlap_query
) {
773 .range_start
._unsigned
= value
,
774 .range_end
._unsigned
= value
,
777 g_ptr_array_foreach(enumeration_type
->entries
,
778 check_ranges_overlap_unsigned
,
780 if (!query
.overlaps
) {
784 name
= g_quark_to_string(query
.mapping_name
);
789 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
790 struct bt_ctf_field_type_enumeration
*enumeration_type
,
793 const char *name
= NULL
;
794 struct range_overlap_query query
=
795 (struct range_overlap_query
) {
796 .range_start
._signed
= value
,
797 .range_end
._signed
= value
,
800 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
802 if (!query
.overlaps
) {
806 name
= g_quark_to_string(query
.mapping_name
);
811 int bt_ctf_field_type_enumeration_get_mapping_count(
812 struct bt_ctf_field_type
*type
)
815 struct bt_ctf_field_type_enumeration
*enumeration
;
817 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
822 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
824 ret
= (int) enumeration
->entries
->len
;
830 struct enumeration_mapping
*get_enumeration_mapping(
831 struct bt_ctf_field_type
*type
, int index
)
833 struct enumeration_mapping
*mapping
= NULL
;
834 struct bt_ctf_field_type_enumeration
*enumeration
;
836 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
838 if (index
>= enumeration
->entries
->len
) {
842 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
847 int bt_ctf_field_type_enumeration_get_mapping(
848 struct bt_ctf_field_type
*type
, int index
,
849 const char **string
, int64_t *range_start
, int64_t *range_end
)
851 struct enumeration_mapping
*mapping
;
854 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
855 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
860 mapping
= get_enumeration_mapping(type
, index
);
866 *string
= g_quark_to_string(mapping
->string
);
867 *range_start
= mapping
->range_start
._signed
;
868 *range_end
= mapping
->range_end
._signed
;
873 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
874 struct bt_ctf_field_type
*type
, int index
,
875 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
877 struct enumeration_mapping
*mapping
;
880 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
881 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
886 mapping
= get_enumeration_mapping(type
, index
);
892 *string
= g_quark_to_string(mapping
->string
);
893 *range_start
= mapping
->range_start
._unsigned
;
894 *range_end
= mapping
->range_end
._unsigned
;
899 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
900 struct bt_ctf_field_type
*type
, const char *name
)
903 struct bt_ctf_field_type_enumeration
*enumeration
;
906 if (!type
|| !name
||
907 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
912 name_quark
= g_quark_try_string(name
);
918 enumeration
= container_of(type
,
919 struct bt_ctf_field_type_enumeration
, parent
);
920 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
921 struct enumeration_mapping
*mapping
=
922 get_enumeration_mapping(type
, i
);
924 if (mapping
->string
== name_quark
) {
935 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
936 struct bt_ctf_field_type
*type
, int64_t value
)
938 struct bt_ctf_field_type_enumeration
*enumeration
;
941 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
946 enumeration
= container_of(type
,
947 struct bt_ctf_field_type_enumeration
, parent
);
948 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
949 struct enumeration_mapping
*mapping
=
950 get_enumeration_mapping(type
, i
);
952 if (value
>= mapping
->range_start
._signed
&&
953 value
<= mapping
->range_end
._signed
) {
964 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
965 struct bt_ctf_field_type
*type
, uint64_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
._unsigned
&&
982 value
<= mapping
->range_end
._unsigned
) {
993 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
995 struct bt_ctf_field_type_floating_point
*floating_point
=
996 g_new0(struct bt_ctf_field_type_floating_point
, 1);
998 if (!floating_point
) {
1002 floating_point
->declaration
.sign
= &floating_point
->sign
;
1003 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1004 floating_point
->declaration
.exp
= &floating_point
->exp
;
1005 floating_point
->sign
.len
= 1;
1006 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1007 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1008 floating_point
->declaration
.exp
->len
=
1009 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1010 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1011 floating_point
->sign
.p
.alignment
= 1;
1012 floating_point
->mantissa
.p
.alignment
= 1;
1013 floating_point
->exp
.p
.alignment
= 1;
1015 bt_ctf_field_type_init(&floating_point
->parent
);
1017 return floating_point
? &floating_point
->parent
: NULL
;
1020 int bt_ctf_field_type_floating_point_get_exponent_digits(
1021 struct bt_ctf_field_type
*type
)
1024 struct bt_ctf_field_type_floating_point
*floating_point
;
1026 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1031 floating_point
= container_of(type
,
1032 struct bt_ctf_field_type_floating_point
, parent
);
1033 ret
= (int) floating_point
->declaration
.exp
->len
;
1038 int bt_ctf_field_type_floating_point_set_exponent_digits(
1039 struct bt_ctf_field_type
*type
,
1040 unsigned int exponent_digits
)
1043 struct bt_ctf_field_type_floating_point
*floating_point
;
1045 if (!type
|| type
->frozen
||
1046 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1051 floating_point
= container_of(type
,
1052 struct bt_ctf_field_type_floating_point
, parent
);
1053 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1054 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1056 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1061 floating_point
->declaration
.exp
->len
= exponent_digits
;
1066 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1067 struct bt_ctf_field_type
*type
)
1070 struct bt_ctf_field_type_floating_point
*floating_point
;
1072 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1077 floating_point
= container_of(type
,
1078 struct bt_ctf_field_type_floating_point
, parent
);
1079 ret
= (int) floating_point
->mantissa
.len
+ 1;
1084 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1085 struct bt_ctf_field_type
*type
,
1086 unsigned int mantissa_digits
)
1089 struct bt_ctf_field_type_floating_point
*floating_point
;
1091 if (!type
|| type
->frozen
||
1092 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1097 floating_point
= container_of(type
,
1098 struct bt_ctf_field_type_floating_point
, parent
);
1100 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1101 (mantissa_digits
!= DBL_MANT_DIG
) &&
1102 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1107 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1112 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1114 struct bt_ctf_field_type_structure
*structure
=
1115 g_new0(struct bt_ctf_field_type_structure
, 1);
1121 structure
->parent
.declaration
= &structure
->declaration
.p
;
1122 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1123 structure
->fields
= g_ptr_array_new_with_free_func(
1124 (GDestroyNotify
)destroy_structure_field
);
1125 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1126 bt_ctf_field_type_init(&structure
->parent
);
1127 return &structure
->parent
;
1132 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1133 struct bt_ctf_field_type
*field_type
,
1134 const char *field_name
)
1137 struct bt_ctf_field_type_structure
*structure
;
1139 if (!type
|| !field_type
|| type
->frozen
||
1140 bt_ctf_validate_identifier(field_name
) ||
1141 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1142 bt_ctf_field_type_validate(field_type
)) {
1147 structure
= container_of(type
,
1148 struct bt_ctf_field_type_structure
, parent
);
1149 if (add_structure_field(structure
->fields
,
1150 structure
->field_name_to_index
, field_type
, field_name
)) {
1155 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1156 type
->declaration
->alignment
=
1157 field_type
->declaration
->alignment
;
1163 int bt_ctf_field_type_structure_get_field_count(
1164 struct bt_ctf_field_type
*type
)
1167 struct bt_ctf_field_type_structure
*structure
;
1169 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1174 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1176 ret
= (int) structure
->fields
->len
;
1181 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1182 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1185 struct bt_ctf_field_type_structure
*structure
;
1186 struct structure_field
*field
;
1189 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1190 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1195 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1197 if (index
>= structure
->fields
->len
) {
1202 field
= g_ptr_array_index(structure
->fields
, index
);
1203 *field_type
= field
->type
;
1204 bt_ctf_field_type_get(field
->type
);
1205 *field_name
= g_quark_to_string(field
->name
);
1210 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1211 struct bt_ctf_field_type
*type
,
1216 struct structure_field
*field
;
1217 struct bt_ctf_field_type_structure
*structure
;
1218 struct bt_ctf_field_type
*field_type
= NULL
;
1220 if (!type
|| !name
) {
1224 name_quark
= g_quark_try_string(name
);
1229 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1231 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1232 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1236 field
= structure
->fields
->pdata
[index
];
1237 field_type
= field
->type
;
1238 bt_ctf_field_type_get(field_type
);
1243 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1244 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1246 struct bt_ctf_field_type_variant
*variant
= NULL
;
1248 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1252 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1257 variant
->parent
.declaration
= &variant
->declaration
.p
;
1258 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1259 variant
->tag_name
= g_string_new(tag_name
);
1260 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1261 variant
->fields
= g_ptr_array_new_with_free_func(
1262 (GDestroyNotify
)destroy_structure_field
);
1264 bt_ctf_field_type_get(enum_tag
);
1265 variant
->tag
= container_of(enum_tag
,
1266 struct bt_ctf_field_type_enumeration
, parent
);
1269 bt_ctf_field_type_init(&variant
->parent
);
1270 return &variant
->parent
;
1275 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1276 struct bt_ctf_field_type
*type
)
1278 struct bt_ctf_field_type_variant
*variant
;
1279 struct bt_ctf_field_type
*tag_type
= NULL
;
1281 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1285 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1286 if (!variant
->tag
) {
1290 tag_type
= &variant
->tag
->parent
;
1291 bt_ctf_field_type_get(tag_type
);
1296 const char *bt_ctf_field_type_variant_get_tag_name(
1297 struct bt_ctf_field_type
*type
)
1299 struct bt_ctf_field_type_variant
*variant
;
1300 const char *tag_name
= NULL
;
1302 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1306 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1307 if (variant
->tag_name
->len
== 0) {
1311 tag_name
= variant
->tag_name
->str
;
1316 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1317 struct bt_ctf_field_type
*field_type
,
1318 const char *field_name
)
1322 struct bt_ctf_field_type_variant
*variant
;
1323 GQuark field_name_quark
= g_quark_from_string(field_name
);
1325 if (!type
|| !field_type
|| type
->frozen
||
1326 bt_ctf_validate_identifier(field_name
) ||
1327 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1328 bt_ctf_field_type_validate(field_type
)) {
1333 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1335 /* The user has explicitly provided a tag; validate against it. */
1339 /* Make sure this name is present in the enum tag */
1340 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1341 struct enumeration_mapping
*mapping
=
1342 g_ptr_array_index(variant
->tag
->entries
, i
);
1344 if (mapping
->string
== field_name_quark
) {
1351 /* Validation failed */
1357 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1358 field_type
, field_name
)) {
1366 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1367 struct bt_ctf_field_type
*type
,
1368 const char *field_name
)
1372 struct structure_field
*field
;
1373 struct bt_ctf_field_type_variant
*variant
;
1374 struct bt_ctf_field_type
*field_type
= NULL
;
1376 if (!type
|| !field_name
) {
1380 name_quark
= g_quark_try_string(field_name
);
1385 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1386 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1387 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1391 field
= g_ptr_array_index(variant
->fields
, index
);
1392 field_type
= field
->type
;
1393 bt_ctf_field_type_get(field_type
);
1398 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1399 struct bt_ctf_field_type
*type
,
1400 struct bt_ctf_field
*tag
)
1402 const char *enum_value
;
1403 struct bt_ctf_field_type
*field_type
= NULL
;
1405 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1409 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1414 /* Already increments field_type's reference count */
1415 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1421 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1424 struct bt_ctf_field_type_variant
*variant
;
1426 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1431 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1433 ret
= (int) variant
->fields
->len
;
1439 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1440 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1443 struct bt_ctf_field_type_variant
*variant
;
1444 struct structure_field
*field
;
1447 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1448 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1453 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1455 if (index
>= variant
->fields
->len
) {
1460 field
= g_ptr_array_index(variant
->fields
, index
);
1461 *field_type
= field
->type
;
1462 bt_ctf_field_type_get(field
->type
);
1463 *field_name
= g_quark_to_string(field
->name
);
1468 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1469 struct bt_ctf_field_type
*element_type
,
1470 unsigned int length
)
1472 struct bt_ctf_field_type_array
*array
= NULL
;
1474 if (!element_type
|| length
== 0 ||
1475 bt_ctf_field_type_validate(element_type
)) {
1479 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1484 array
->parent
.declaration
= &array
->declaration
.p
;
1485 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1487 bt_ctf_field_type_get(element_type
);
1488 array
->element_type
= element_type
;
1489 array
->length
= length
;
1490 bt_ctf_field_type_init(&array
->parent
);
1491 array
->parent
.declaration
->alignment
=
1492 element_type
->declaration
->alignment
;
1493 return &array
->parent
;
1498 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1499 struct bt_ctf_field_type
*type
)
1501 struct bt_ctf_field_type
*ret
= NULL
;
1502 struct bt_ctf_field_type_array
*array
;
1504 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1508 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1509 ret
= array
->element_type
;
1510 bt_ctf_field_type_get(ret
);
1515 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1518 struct bt_ctf_field_type_array
*array
;
1520 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1525 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1526 ret
= (int64_t) array
->length
;
1531 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1532 struct bt_ctf_field_type
*element_type
,
1533 const char *length_field_name
)
1535 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1537 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1538 bt_ctf_field_type_validate(element_type
)) {
1542 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1547 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1548 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1549 bt_ctf_field_type_get(element_type
);
1550 sequence
->element_type
= element_type
;
1551 sequence
->length_field_name
= g_string_new(length_field_name
);
1552 bt_ctf_field_type_init(&sequence
->parent
);
1553 sequence
->parent
.declaration
->alignment
=
1554 element_type
->declaration
->alignment
;
1555 return &sequence
->parent
;
1560 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1561 struct bt_ctf_field_type
*type
)
1563 struct bt_ctf_field_type
*ret
= NULL
;
1564 struct bt_ctf_field_type_sequence
*sequence
;
1566 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1570 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1572 ret
= sequence
->element_type
;
1573 bt_ctf_field_type_get(ret
);
1578 const char *bt_ctf_field_type_sequence_get_length_field_name(
1579 struct bt_ctf_field_type
*type
)
1581 const char *ret
= NULL
;
1582 struct bt_ctf_field_type_sequence
*sequence
;
1584 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1588 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1590 ret
= sequence
->length_field_name
->str
;
1595 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1597 struct bt_ctf_field_type_string
*string
=
1598 g_new0(struct bt_ctf_field_type_string
, 1);
1604 string
->parent
.declaration
= &string
->declaration
.p
;
1605 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1606 bt_ctf_field_type_init(&string
->parent
);
1607 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1608 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1609 return &string
->parent
;
1612 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1613 struct bt_ctf_field_type
*type
)
1615 struct bt_ctf_field_type_string
*string
;
1616 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1618 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1622 string
= container_of(type
, struct bt_ctf_field_type_string
,
1624 ret
= string
->declaration
.encoding
;
1629 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1630 enum ctf_string_encoding encoding
)
1633 struct bt_ctf_field_type_string
*string
;
1635 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1636 (encoding
!= CTF_STRING_UTF8
&&
1637 encoding
!= CTF_STRING_ASCII
)) {
1642 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1643 string
->declaration
.encoding
= encoding
;
1648 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1657 ret
= (int) type
->declaration
->alignment
;
1662 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1663 unsigned int alignment
)
1667 /* Alignment must be bit-aligned (1) or byte aligned */
1668 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1673 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1674 alignment
!= CHAR_BIT
) {
1679 type
->declaration
->alignment
= alignment
;
1685 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1686 struct bt_ctf_field_type
*type
)
1688 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1689 int internal_byte_order
= -1;
1695 switch (type
->declaration
->id
) {
1696 case CTF_TYPE_INTEGER
:
1698 struct bt_ctf_field_type_integer
*integer
= container_of(
1699 type
, struct bt_ctf_field_type_integer
, parent
);
1700 internal_byte_order
= integer
->declaration
.byte_order
;
1703 case CTF_TYPE_FLOAT
:
1705 struct bt_ctf_field_type_floating_point
*floating_point
=
1707 struct bt_ctf_field_type_floating_point
,
1709 internal_byte_order
= floating_point
->declaration
.byte_order
;
1716 switch (internal_byte_order
) {
1718 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1721 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1724 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1727 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1733 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1734 enum bt_ctf_byte_order byte_order
)
1737 int internal_byte_order
;
1738 enum ctf_type_id type_id
;
1740 if (!type
|| type
->frozen
) {
1745 type_id
= type
->declaration
->id
;
1746 switch (byte_order
) {
1747 case BT_CTF_BYTE_ORDER_NATIVE
:
1748 /* Leave unset. Will be initialized by parent. */
1749 internal_byte_order
= 0;
1751 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1752 internal_byte_order
= LITTLE_ENDIAN
;
1754 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1755 case BT_CTF_BYTE_ORDER_NETWORK
:
1756 internal_byte_order
= BIG_ENDIAN
;
1763 if (set_byte_order_funcs
[type_id
]) {
1764 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1770 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1771 struct bt_ctf_field_type
*type
)
1774 return CTF_TYPE_UNKNOWN
;
1777 return type
->declaration
->id
;
1780 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1786 bt_ctf_ref_get(&type
->ref_count
);
1789 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1795 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1799 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1809 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1810 struct bt_ctf_field_type_variant
*variant
,
1813 struct bt_ctf_field_type
*type
= NULL
;
1814 GQuark field_name_quark
;
1816 struct structure_field
*field_entry
;
1817 struct range_overlap_query query
= {
1818 .range_start
._signed
= tag_value
,
1819 .range_end
._signed
= tag_value
,
1820 .mapping_name
= 0, .overlaps
= 0};
1822 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1824 if (!query
.overlaps
) {
1828 field_name_quark
= query
.mapping_name
;
1829 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1830 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1834 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1835 type
= field_entry
->type
;
1841 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1842 struct bt_ctf_field_type_variant
*variant
,
1845 struct bt_ctf_field_type
*type
= NULL
;
1846 GQuark field_name_quark
;
1848 struct structure_field
*field_entry
;
1849 struct range_overlap_query query
= {
1850 .range_start
._unsigned
= tag_value
,
1851 .range_end
._unsigned
= tag_value
,
1852 .mapping_name
= 0, .overlaps
= 0};
1854 g_ptr_array_foreach(variant
->tag
->entries
,
1855 check_ranges_overlap_unsigned
,
1857 if (!query
.overlaps
) {
1861 field_name_quark
= query
.mapping_name
;
1862 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1863 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1867 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1868 type
= field_entry
->type
;
1874 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1875 struct metadata_context
*context
)
1879 if (!type
|| !context
) {
1884 ret
= type
->serialize(type
, context
);
1890 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1897 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1898 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1899 set_byte_order_funcs
[type
->declaration
->id
](type
,
1905 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
1907 struct bt_ctf_field_type
*copy
= NULL
;
1913 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
1919 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1921 struct bt_ctf_field_type_integer
*integer
;
1927 integer
= container_of(
1928 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1929 struct bt_ctf_field_type_integer
, parent
);
1930 if (integer
->mapped_clock
) {
1931 bt_ctf_clock_put(integer
->mapped_clock
);
1937 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1939 struct bt_ctf_field_type_enumeration
*enumeration
;
1945 enumeration
= container_of(
1946 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1947 struct bt_ctf_field_type_enumeration
, parent
);
1948 g_ptr_array_free(enumeration
->entries
, TRUE
);
1949 bt_ctf_field_type_put(enumeration
->container
);
1950 g_free(enumeration
);
1954 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1956 struct bt_ctf_field_type_floating_point
*floating_point
;
1962 floating_point
= container_of(
1963 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1964 struct bt_ctf_field_type_floating_point
, parent
);
1965 g_free(floating_point
);
1969 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1971 struct bt_ctf_field_type_structure
*structure
;
1977 structure
= container_of(
1978 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1979 struct bt_ctf_field_type_structure
, parent
);
1980 g_ptr_array_free(structure
->fields
, TRUE
);
1981 g_hash_table_destroy(structure
->field_name_to_index
);
1986 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
1988 struct bt_ctf_field_type_variant
*variant
;
1994 variant
= container_of(
1995 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1996 struct bt_ctf_field_type_variant
, parent
);
1997 g_ptr_array_free(variant
->fields
, TRUE
);
1998 g_hash_table_destroy(variant
->field_name_to_index
);
1999 g_string_free(variant
->tag_name
, TRUE
);
2000 bt_ctf_field_type_put(&variant
->tag
->parent
);
2005 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2007 struct bt_ctf_field_type_array
*array
;
2013 array
= container_of(
2014 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2015 struct bt_ctf_field_type_array
, parent
);
2016 bt_ctf_field_type_put(array
->element_type
);
2021 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2023 struct bt_ctf_field_type_sequence
*sequence
;
2029 sequence
= container_of(
2030 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2031 struct bt_ctf_field_type_sequence
, parent
);
2032 bt_ctf_field_type_put(sequence
->element_type
);
2033 g_string_free(sequence
->length_field_name
, TRUE
);
2038 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2040 struct bt_ctf_field_type_string
*string
;
2046 string
= container_of(
2047 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2048 struct bt_ctf_field_type_string
, parent
);
2053 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2059 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2061 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2062 type
, struct bt_ctf_field_type_enumeration
, parent
);
2064 generic_field_type_freeze(type
);
2065 bt_ctf_field_type_freeze(enumeration_type
->container
);
2069 void freeze_structure_field(struct structure_field
*field
)
2071 bt_ctf_field_type_freeze(field
->type
);
2075 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2077 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2078 type
, struct bt_ctf_field_type_structure
, parent
);
2080 generic_field_type_freeze(type
);
2081 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
2086 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2088 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2089 type
, struct bt_ctf_field_type_variant
, parent
);
2091 generic_field_type_freeze(type
);
2092 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
2097 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2099 struct bt_ctf_field_type_array
*array_type
= container_of(
2100 type
, struct bt_ctf_field_type_array
, parent
);
2102 generic_field_type_freeze(type
);
2103 bt_ctf_field_type_freeze(array_type
->element_type
);
2107 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2109 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2110 type
, struct bt_ctf_field_type_sequence
, parent
);
2112 generic_field_type_freeze(type
);
2113 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2117 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2119 const char *encoding_string
;
2122 case CTF_STRING_NONE
:
2123 encoding_string
= "none";
2125 case CTF_STRING_ASCII
:
2126 encoding_string
= "ASCII";
2128 case CTF_STRING_UTF8
:
2129 encoding_string
= "UTF8";
2132 encoding_string
= "unknown";
2136 return encoding_string
;
2140 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2142 const char *base_string
;
2145 case BT_CTF_INTEGER_BASE_DECIMAL
:
2146 base_string
= "decimal";
2148 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2149 base_string
= "hexadecimal";
2151 case BT_CTF_INTEGER_BASE_OCTAL
:
2152 base_string
= "octal";
2154 case BT_CTF_INTEGER_BASE_BINARY
:
2155 base_string
= "binary";
2158 base_string
= "unknown";
2166 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2167 struct metadata_context
*context
)
2169 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2170 struct bt_ctf_field_type_integer
, parent
);
2173 g_string_append_printf(context
->string
,
2174 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2175 integer
->declaration
.len
, type
->declaration
->alignment
,
2176 (integer
->declaration
.signedness
? "true" : "false"),
2177 get_encoding_string(integer
->declaration
.encoding
),
2178 get_integer_base_string(integer
->declaration
.base
),
2179 get_byte_order_string(integer
->declaration
.byte_order
));
2180 if (integer
->mapped_clock
) {
2181 const char *clock_name
= bt_ctf_clock_get_name(
2182 integer
->mapped_clock
);
2189 g_string_append_printf(context
->string
,
2190 "; map = clock.%s.value", clock_name
);
2193 g_string_append(context
->string
, "; }");
2199 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2200 struct metadata_context
*context
)
2204 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2205 struct bt_ctf_field_type_enumeration
, parent
);
2206 struct bt_ctf_field_type
*container_type
;
2207 int container_signed
;
2209 ret
= bt_ctf_field_type_validate(type
);
2214 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2215 if (!container_type
) {
2220 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2221 if (container_signed
< 0) {
2222 ret
= container_signed
;
2223 goto error_put_container_type
;
2226 g_string_append(context
->string
, "enum : ");
2227 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2229 goto error_put_container_type
;
2232 g_string_append(context
->string
, " { ");
2233 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2234 struct enumeration_mapping
*mapping
=
2235 enumeration
->entries
->pdata
[entry
];
2237 if (container_signed
) {
2238 if (mapping
->range_start
._signed
==
2239 mapping
->range_end
._signed
) {
2240 g_string_append_printf(context
->string
,
2241 "\"%s\" = %" PRId64
,
2242 g_quark_to_string(mapping
->string
),
2243 mapping
->range_start
._signed
);
2245 g_string_append_printf(context
->string
,
2246 "\"%s\" = %" PRId64
" ... %" PRId64
,
2247 g_quark_to_string(mapping
->string
),
2248 mapping
->range_start
._signed
,
2249 mapping
->range_end
._signed
);
2252 if (mapping
->range_start
._unsigned
==
2253 mapping
->range_end
._unsigned
) {
2254 g_string_append_printf(context
->string
,
2255 "\"%s\" = %" PRIu64
,
2256 g_quark_to_string(mapping
->string
),
2257 mapping
->range_start
._unsigned
);
2259 g_string_append_printf(context
->string
,
2260 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2261 g_quark_to_string(mapping
->string
),
2262 mapping
->range_start
._unsigned
,
2263 mapping
->range_end
._unsigned
);
2267 g_string_append(context
->string
,
2268 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2272 if (context
->field_name
->len
) {
2273 g_string_append_printf(context
->string
, " %s",
2274 context
->field_name
->str
);
2275 g_string_assign(context
->field_name
, "");
2277 error_put_container_type
:
2278 bt_ctf_field_type_put(container_type
);
2284 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2285 struct metadata_context
*context
)
2287 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2288 type
, struct bt_ctf_field_type_floating_point
, parent
);
2290 g_string_append_printf(context
->string
,
2291 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2292 floating_point
->declaration
.exp
->len
,
2293 floating_point
->declaration
.mantissa
->len
+ 1,
2294 get_byte_order_string(floating_point
->declaration
.byte_order
),
2295 type
->declaration
->alignment
);
2300 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2301 struct metadata_context
*context
)
2304 unsigned int indent
;
2306 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2307 struct bt_ctf_field_type_structure
, parent
);
2308 GString
*structure_field_name
= context
->field_name
;
2310 context
->field_name
= g_string_new("");
2312 context
->current_indentation_level
++;
2313 g_string_append(context
->string
, "struct {\n");
2315 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2316 struct structure_field
*field
;
2318 for (indent
= 0; indent
< context
->current_indentation_level
;
2320 g_string_append_c(context
->string
, '\t');
2323 field
= structure
->fields
->pdata
[i
];
2324 g_string_assign(context
->field_name
,
2325 g_quark_to_string(field
->name
));
2326 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2331 if (context
->field_name
->len
) {
2332 g_string_append_printf(context
->string
, " %s",
2333 context
->field_name
->str
);
2335 g_string_append(context
->string
, ";\n");
2338 context
->current_indentation_level
--;
2339 for (indent
= 0; indent
< context
->current_indentation_level
;
2341 g_string_append_c(context
->string
, '\t');
2344 g_string_append_printf(context
->string
, "} align(%zu)",
2345 type
->declaration
->alignment
);
2347 g_string_free(context
->field_name
, TRUE
);
2348 context
->field_name
= structure_field_name
;
2353 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2354 struct metadata_context
*context
)
2357 unsigned int indent
;
2359 struct bt_ctf_field_type_variant
*variant
= container_of(
2360 type
, struct bt_ctf_field_type_variant
, parent
);
2361 GString
*variant_field_name
= context
->field_name
;
2363 context
->field_name
= g_string_new("");
2364 if (variant
->tag_name
->len
> 0) {
2365 g_string_append_printf(context
->string
,
2366 "variant <%s> {\n", variant
->tag_name
->str
);
2368 g_string_append(context
->string
, "variant {\n");
2371 context
->current_indentation_level
++;
2372 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2373 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2375 g_string_assign(context
->field_name
,
2376 g_quark_to_string(field
->name
));
2377 for (indent
= 0; indent
< context
->current_indentation_level
;
2379 g_string_append_c(context
->string
, '\t');
2382 g_string_assign(context
->field_name
,
2383 g_quark_to_string(field
->name
));
2384 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2389 if (context
->field_name
->len
) {
2390 g_string_append_printf(context
->string
, " %s;",
2391 context
->field_name
->str
);
2394 g_string_append_c(context
->string
, '\n');
2397 context
->current_indentation_level
--;
2398 for (indent
= 0; indent
< context
->current_indentation_level
;
2400 g_string_append_c(context
->string
, '\t');
2403 g_string_append(context
->string
, "}");
2405 g_string_free(context
->field_name
, TRUE
);
2406 context
->field_name
= variant_field_name
;
2411 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2412 struct metadata_context
*context
)
2415 struct bt_ctf_field_type_array
*array
= container_of(type
,
2416 struct bt_ctf_field_type_array
, parent
);
2418 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2423 if (context
->field_name
->len
) {
2424 g_string_append_printf(context
->string
, " %s[%u]",
2425 context
->field_name
->str
, array
->length
);
2426 g_string_assign(context
->field_name
, "");
2428 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2435 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2436 struct metadata_context
*context
)
2439 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2440 type
, struct bt_ctf_field_type_sequence
, parent
);
2442 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2447 if (context
->field_name
->len
) {
2448 g_string_append_printf(context
->string
, " %s[%s]",
2449 context
->field_name
->str
,
2450 sequence
->length_field_name
->str
);
2451 g_string_assign(context
->field_name
, "");
2453 g_string_append_printf(context
->string
, "[%s]",
2454 sequence
->length_field_name
->str
);
2461 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2462 struct metadata_context
*context
)
2464 struct bt_ctf_field_type_string
*string
= container_of(
2465 type
, struct bt_ctf_field_type_string
, parent
);
2467 g_string_append_printf(context
->string
,
2468 "string { encoding = %s; }",
2469 get_encoding_string(string
->declaration
.encoding
));
2474 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2475 int byte_order
, int set_native
)
2477 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2478 struct bt_ctf_field_type_integer
, parent
);
2481 integer_type
->declaration
.byte_order
=
2482 integer_type
->declaration
.byte_order
== 0 ?
2483 byte_order
: integer_type
->declaration
.byte_order
;
2485 integer_type
->declaration
.byte_order
= byte_order
;
2490 void bt_ctf_field_type_enumeration_set_byte_order(
2491 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2493 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2494 struct bt_ctf_field_type_enumeration
, parent
);
2496 /* Safe to assume that container is an integer */
2497 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2498 byte_order
, set_native
);
2502 void bt_ctf_field_type_floating_point_set_byte_order(
2503 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2505 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2506 container_of(type
, struct bt_ctf_field_type_floating_point
,
2510 floating_point_type
->declaration
.byte_order
=
2511 floating_point_type
->declaration
.byte_order
== 0 ?
2513 floating_point_type
->declaration
.byte_order
;
2514 floating_point_type
->sign
.byte_order
=
2515 floating_point_type
->sign
.byte_order
== 0 ?
2516 byte_order
: floating_point_type
->sign
.byte_order
;
2517 floating_point_type
->mantissa
.byte_order
=
2518 floating_point_type
->mantissa
.byte_order
== 0 ?
2519 byte_order
: floating_point_type
->mantissa
.byte_order
;
2520 floating_point_type
->exp
.byte_order
=
2521 floating_point_type
->exp
.byte_order
== 0 ?
2522 byte_order
: floating_point_type
->exp
.byte_order
;
2524 floating_point_type
->declaration
.byte_order
= byte_order
;
2525 floating_point_type
->sign
.byte_order
= byte_order
;
2526 floating_point_type
->mantissa
.byte_order
= byte_order
;
2527 floating_point_type
->exp
.byte_order
= byte_order
;
2532 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2533 int byte_order
, int set_native
)
2536 struct bt_ctf_field_type_structure
*structure_type
=
2537 container_of(type
, struct bt_ctf_field_type_structure
,
2540 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2541 struct structure_field
*field
= g_ptr_array_index(
2542 structure_type
->fields
, i
);
2543 struct bt_ctf_field_type
*field_type
= field
->type
;
2545 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2546 set_byte_order_funcs
[field_type
->declaration
->id
](
2547 field_type
, byte_order
, set_native
);
2553 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2554 int byte_order
, int set_native
)
2557 struct bt_ctf_field_type_variant
*variant_type
=
2558 container_of(type
, struct bt_ctf_field_type_variant
,
2561 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2562 struct structure_field
*field
= g_ptr_array_index(
2563 variant_type
->fields
, i
);
2564 struct bt_ctf_field_type
*field_type
= field
->type
;
2566 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2567 set_byte_order_funcs
[field_type
->declaration
->id
](
2568 field_type
, byte_order
, set_native
);
2574 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2575 int byte_order
, int set_native
)
2577 struct bt_ctf_field_type_array
*array_type
=
2578 container_of(type
, struct bt_ctf_field_type_array
,
2581 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2582 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2583 array_type
->element_type
, byte_order
, set_native
);
2588 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2589 int byte_order
, int set_native
)
2591 struct bt_ctf_field_type_sequence
*sequence_type
=
2592 container_of(type
, struct bt_ctf_field_type_sequence
,
2595 if (set_byte_order_funcs
[
2596 sequence_type
->element_type
->declaration
->id
]) {
2597 set_byte_order_funcs
[
2598 sequence_type
->element_type
->declaration
->id
](
2599 sequence_type
->element_type
, byte_order
, set_native
);
2604 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2605 struct bt_ctf_field_type
*type
)
2607 struct bt_ctf_field_type
*copy
;
2608 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
2610 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
2611 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
2616 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
2618 copy_integer
->declaration
= integer
->declaration
;
2619 if (integer
->mapped_clock
) {
2620 bt_ctf_clock_get(integer
->mapped_clock
);
2621 copy_integer
->mapped_clock
= integer
->mapped_clock
;
2628 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
2629 struct bt_ctf_field_type
*type
)
2632 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
2633 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
2635 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
2638 /* Copy the source enumeration's container */
2639 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
2640 if (!copy_container
) {
2644 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
2648 copy_enumeration
= container_of(copy
,
2649 struct bt_ctf_field_type_enumeration
, parent
);
2651 /* Copy all enumaration entries */
2652 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
2653 struct enumeration_mapping
*mapping
= g_ptr_array_index(
2654 enumeration
->entries
, i
);
2655 struct enumeration_mapping
* copy_mapping
= g_new0(
2656 struct enumeration_mapping
, 1);
2658 if (!copy_mapping
) {
2662 *copy_mapping
= *mapping
;
2663 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
2666 copy_enumeration
->declaration
= enumeration
->declaration
;
2668 if (copy_container
) {
2669 bt_ctf_field_type_put(copy_container
);
2673 if (copy_container
) {
2674 bt_ctf_field_type_put(copy_container
);
2676 bt_ctf_field_type_put(copy
);
2681 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
2682 struct bt_ctf_field_type
*type
)
2684 struct bt_ctf_field_type
*copy
;
2685 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
2687 floating_point
= container_of(type
,
2688 struct bt_ctf_field_type_floating_point
, parent
);
2689 copy
= bt_ctf_field_type_floating_point_create();
2694 copy_float
= container_of(copy
,
2695 struct bt_ctf_field_type_floating_point
, parent
);
2696 copy_float
->declaration
= floating_point
->declaration
;
2697 copy_float
->sign
= floating_point
->sign
;
2698 copy_float
->mantissa
= floating_point
->mantissa
;
2699 copy_float
->exp
= floating_point
->exp
;
2705 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
2706 struct bt_ctf_field_type
*type
)
2709 GHashTableIter iter
;
2710 gpointer key
, value
;
2711 struct bt_ctf_field_type
*copy
;
2712 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
2714 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2716 copy
= bt_ctf_field_type_structure_create();
2721 copy_structure
= container_of(copy
,
2722 struct bt_ctf_field_type_structure
, parent
);
2724 /* Copy field_name_to_index */
2725 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
2726 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2727 g_hash_table_insert(copy_structure
->field_name_to_index
,
2731 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2732 struct structure_field
*entry
, *copy_entry
;
2733 struct bt_ctf_field_type
*copy_field
;
2735 copy_entry
= g_new0(struct structure_field
, 1);
2740 entry
= g_ptr_array_index(structure
->fields
, i
);
2741 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2747 copy_entry
->name
= entry
->name
;
2748 copy_entry
->type
= copy_field
;
2749 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
2752 copy_structure
->declaration
= structure
->declaration
;
2756 bt_ctf_field_type_put(copy
);
2761 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
2762 struct bt_ctf_field_type
*type
)
2765 GHashTableIter iter
;
2766 gpointer key
, value
;
2767 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
2768 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
2770 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2773 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
2779 copy
= bt_ctf_field_type_variant_create(copy_tag
,
2780 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
2785 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
2788 /* Copy field_name_to_index */
2789 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
2790 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2791 g_hash_table_insert(copy_variant
->field_name_to_index
,
2795 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2796 struct structure_field
*entry
, *copy_entry
;
2797 struct bt_ctf_field_type
*copy_field
;
2799 copy_entry
= g_new0(struct structure_field
, 1);
2804 entry
= g_ptr_array_index(variant
->fields
, i
);
2805 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2811 copy_entry
->name
= entry
->name
;
2812 copy_entry
->type
= copy_field
;
2813 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
2816 copy_variant
->declaration
= variant
->declaration
;
2819 bt_ctf_field_type_put(copy_tag
);
2825 bt_ctf_field_type_put(copy_tag
);
2828 bt_ctf_field_type_put(copy
);
2833 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
2834 struct bt_ctf_field_type
*type
)
2836 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2837 struct bt_ctf_field_type_array
*array
, *copy_array
;
2839 array
= container_of(type
, struct bt_ctf_field_type_array
,
2841 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
2842 if (!copy_element
) {
2846 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
2851 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
2853 copy_array
->declaration
= array
->declaration
;
2856 bt_ctf_field_type_put(copy_element
);
2863 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
2864 struct bt_ctf_field_type
*type
)
2866 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2867 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
2869 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2871 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
2872 if (!copy_element
) {
2876 copy
= bt_ctf_field_type_sequence_create(copy_element
,
2877 sequence
->length_field_name
->len
?
2878 sequence
->length_field_name
->str
: NULL
);
2883 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
2885 copy_sequence
->declaration
= sequence
->declaration
;
2888 bt_ctf_field_type_put(copy_element
);
2895 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
2896 struct bt_ctf_field_type
*type
)
2898 struct bt_ctf_field_type
*copy
;
2899 struct bt_ctf_field_type_string
*string
, *copy_string
;
2901 copy
= bt_ctf_field_type_string_create();
2906 string
= container_of(type
, struct bt_ctf_field_type_string
,
2908 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
2910 copy_string
->declaration
= string
->declaration
;