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 if (type
->alias_name
) {
362 g_string_free(type
->alias_name
, TRUE
);
364 type_destroy_funcs
[type_id
](ref
);
368 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
377 switch (type
->declaration
->id
) {
380 struct bt_ctf_field_type_enumeration
*enumeration
=
381 container_of(type
, struct bt_ctf_field_type_enumeration
,
384 /* Ensure enum has entries */
385 ret
= enumeration
->entries
->len
? 0 : -1;
395 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
397 struct bt_ctf_field_type_integer
*integer
=
398 g_new0(struct bt_ctf_field_type_integer
, 1);
400 if (!integer
|| size
== 0 || size
> 64) {
404 integer
->parent
.declaration
= &integer
->declaration
.p
;
405 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
406 integer
->declaration
.len
= size
;
407 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
408 integer
->declaration
.encoding
= CTF_STRING_NONE
;
409 bt_ctf_field_type_init(&integer
->parent
);
410 return &integer
->parent
;
413 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
416 struct bt_ctf_field_type_integer
*integer
;
418 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
423 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
424 ret
= (int) integer
->declaration
.len
;
429 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
432 struct bt_ctf_field_type_integer
*integer
;
434 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
439 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
440 ret
= integer
->declaration
.signedness
;
445 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
449 struct bt_ctf_field_type_integer
*integer
;
451 if (!type
|| type
->frozen
||
452 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
457 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
458 integer
->declaration
.signedness
= !!is_signed
;
463 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
464 struct bt_ctf_field_type
*type
)
466 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
467 struct bt_ctf_field_type_integer
*integer
;
469 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
473 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
474 ret
= integer
->declaration
.base
;
479 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
480 enum bt_ctf_integer_base base
)
484 if (!type
|| type
->frozen
||
485 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
491 case BT_CTF_INTEGER_BASE_BINARY
:
492 case BT_CTF_INTEGER_BASE_OCTAL
:
493 case BT_CTF_INTEGER_BASE_DECIMAL
:
494 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
496 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
497 struct bt_ctf_field_type_integer
, parent
);
498 integer
->declaration
.base
= base
;
508 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
509 struct bt_ctf_field_type
*type
)
511 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
512 struct bt_ctf_field_type_integer
*integer
;
514 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
518 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
519 ret
= integer
->declaration
.encoding
;
524 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
525 enum ctf_string_encoding encoding
)
528 struct bt_ctf_field_type_integer
*integer
;
530 if (!type
|| type
->frozen
||
531 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
532 (encoding
< CTF_STRING_NONE
) ||
533 (encoding
>= CTF_STRING_UNKNOWN
)) {
538 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
539 integer
->declaration
.encoding
= encoding
;
544 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
545 struct bt_ctf_field_type
*type
)
547 struct bt_ctf_field_type_integer
*integer
;
548 struct bt_ctf_clock
*clock
= NULL
;
554 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
555 clock
= integer
->mapped_clock
;
557 bt_ctf_clock_get(clock
);
563 int bt_ctf_field_type_integer_set_mapped_clock(
564 struct bt_ctf_field_type
*type
,
565 struct bt_ctf_clock
*clock
)
567 struct bt_ctf_field_type_integer
*integer
;
570 if (!type
|| type
->frozen
) {
575 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
576 if (integer
->mapped_clock
) {
577 bt_ctf_clock_put(integer
->mapped_clock
);
581 bt_ctf_clock_get(clock
);
584 integer
->mapped_clock
= clock
;
589 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
590 struct bt_ctf_field_type
*integer_container_type
)
592 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
594 if (!integer_container_type
) {
598 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
602 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
607 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
608 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
609 bt_ctf_field_type_get(integer_container_type
);
610 enumeration
->container
= integer_container_type
;
611 enumeration
->entries
= g_ptr_array_new_with_free_func(
612 (GDestroyNotify
)destroy_enumeration_mapping
);
613 bt_ctf_field_type_init(&enumeration
->parent
);
614 return &enumeration
->parent
;
620 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
621 struct bt_ctf_field_type
*type
)
623 struct bt_ctf_field_type
*container_type
= NULL
;
624 struct bt_ctf_field_type_enumeration
*enumeration_type
;
630 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
634 enumeration_type
= container_of(type
,
635 struct bt_ctf_field_type_enumeration
, parent
);
636 container_type
= enumeration_type
->container
;
637 bt_ctf_field_type_get(container_type
);
639 return container_type
;
642 int bt_ctf_field_type_enumeration_add_mapping(
643 struct bt_ctf_field_type
*type
, const char *string
,
644 int64_t range_start
, int64_t range_end
)
648 struct enumeration_mapping
*mapping
;
649 struct bt_ctf_field_type_enumeration
*enumeration
;
650 struct range_overlap_query query
;
651 char *escaped_string
;
653 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
655 (range_end
< range_start
)) {
660 if (!string
|| strlen(string
) == 0) {
665 escaped_string
= g_strescape(string
, NULL
);
666 if (!escaped_string
) {
671 mapping_name
= g_quark_from_string(escaped_string
);
672 query
= (struct range_overlap_query
) {
673 .range_start
._signed
= range_start
,
674 .range_end
._signed
= range_end
,
675 .mapping_name
= mapping_name
,
677 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
680 /* Check that the range does not overlap with one already present */
681 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
682 if (query
.overlaps
) {
687 mapping
= g_new(struct enumeration_mapping
, 1);
693 *mapping
= (struct enumeration_mapping
) {
694 .range_start
._signed
= range_start
,
695 .range_end
._signed
= range_end
, .string
= mapping_name
};
696 g_ptr_array_add(enumeration
->entries
, mapping
);
697 g_ptr_array_sort(enumeration
->entries
,
698 (GCompareFunc
)compare_enumeration_mappings_signed
);
700 free(escaped_string
);
705 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
706 struct bt_ctf_field_type
*type
, const char *string
,
707 uint64_t range_start
, uint64_t range_end
)
711 struct enumeration_mapping
*mapping
;
712 struct bt_ctf_field_type_enumeration
*enumeration
;
713 struct range_overlap_query query
;
714 char *escaped_string
;
716 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
718 (range_end
< range_start
)) {
723 if (!string
|| strlen(string
) == 0) {
728 escaped_string
= g_strescape(string
, NULL
);
729 if (!escaped_string
) {
734 mapping_name
= g_quark_from_string(escaped_string
);
735 query
= (struct range_overlap_query
) {
736 .range_start
._unsigned
= range_start
,
737 .range_end
._unsigned
= range_end
,
738 .mapping_name
= mapping_name
,
740 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
743 /* Check that the range does not overlap with one already present */
744 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
746 if (query
.overlaps
) {
751 mapping
= g_new(struct enumeration_mapping
, 1);
757 *mapping
= (struct enumeration_mapping
) {
758 .range_start
._unsigned
= range_start
,
759 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
760 g_ptr_array_add(enumeration
->entries
, mapping
);
761 g_ptr_array_sort(enumeration
->entries
,
762 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
764 free(escaped_string
);
769 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
770 struct bt_ctf_field_type_enumeration
*enumeration_type
,
773 const char *name
= NULL
;
774 struct range_overlap_query query
=
775 (struct range_overlap_query
) {
776 .range_start
._unsigned
= value
,
777 .range_end
._unsigned
= value
,
780 g_ptr_array_foreach(enumeration_type
->entries
,
781 check_ranges_overlap_unsigned
,
783 if (!query
.overlaps
) {
787 name
= g_quark_to_string(query
.mapping_name
);
792 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
793 struct bt_ctf_field_type_enumeration
*enumeration_type
,
796 const char *name
= NULL
;
797 struct range_overlap_query query
=
798 (struct range_overlap_query
) {
799 .range_start
._signed
= value
,
800 .range_end
._signed
= value
,
803 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
805 if (!query
.overlaps
) {
809 name
= g_quark_to_string(query
.mapping_name
);
814 int bt_ctf_field_type_enumeration_get_mapping_count(
815 struct bt_ctf_field_type
*type
)
818 struct bt_ctf_field_type_enumeration
*enumeration
;
820 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
825 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
827 ret
= (int) enumeration
->entries
->len
;
833 struct enumeration_mapping
*get_enumeration_mapping(
834 struct bt_ctf_field_type
*type
, int index
)
836 struct enumeration_mapping
*mapping
= NULL
;
837 struct bt_ctf_field_type_enumeration
*enumeration
;
839 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
841 if (index
>= enumeration
->entries
->len
) {
845 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
850 int bt_ctf_field_type_enumeration_get_mapping(
851 struct bt_ctf_field_type
*type
, int index
,
852 const char **string
, int64_t *range_start
, int64_t *range_end
)
854 struct enumeration_mapping
*mapping
;
857 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
858 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
863 mapping
= get_enumeration_mapping(type
, index
);
869 *string
= g_quark_to_string(mapping
->string
);
870 *range_start
= mapping
->range_start
._signed
;
871 *range_end
= mapping
->range_end
._signed
;
876 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
877 struct bt_ctf_field_type
*type
, int index
,
878 const char **string
, uint64_t *range_start
, uint64_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
._unsigned
;
897 *range_end
= mapping
->range_end
._unsigned
;
902 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
903 struct bt_ctf_field_type
*type
, const char *name
)
906 struct bt_ctf_field_type_enumeration
*enumeration
;
909 if (!type
|| !name
||
910 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
915 name_quark
= g_quark_try_string(name
);
921 enumeration
= container_of(type
,
922 struct bt_ctf_field_type_enumeration
, parent
);
923 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
924 struct enumeration_mapping
*mapping
=
925 get_enumeration_mapping(type
, i
);
927 if (mapping
->string
== name_quark
) {
938 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
939 struct bt_ctf_field_type
*type
, int64_t value
)
941 struct bt_ctf_field_type_enumeration
*enumeration
;
944 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
949 enumeration
= container_of(type
,
950 struct bt_ctf_field_type_enumeration
, parent
);
951 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
952 struct enumeration_mapping
*mapping
=
953 get_enumeration_mapping(type
, i
);
955 if (value
>= mapping
->range_start
._signed
&&
956 value
<= mapping
->range_end
._signed
) {
967 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
968 struct bt_ctf_field_type
*type
, uint64_t value
)
970 struct bt_ctf_field_type_enumeration
*enumeration
;
973 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
978 enumeration
= container_of(type
,
979 struct bt_ctf_field_type_enumeration
, parent
);
980 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
981 struct enumeration_mapping
*mapping
=
982 get_enumeration_mapping(type
, i
);
984 if (value
>= mapping
->range_start
._unsigned
&&
985 value
<= mapping
->range_end
._unsigned
) {
996 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
998 struct bt_ctf_field_type_floating_point
*floating_point
=
999 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1001 if (!floating_point
) {
1005 floating_point
->declaration
.sign
= &floating_point
->sign
;
1006 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1007 floating_point
->declaration
.exp
= &floating_point
->exp
;
1008 floating_point
->sign
.len
= 1;
1009 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1010 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1011 floating_point
->declaration
.exp
->len
=
1012 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1013 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1014 floating_point
->sign
.p
.alignment
= 1;
1015 floating_point
->mantissa
.p
.alignment
= 1;
1016 floating_point
->exp
.p
.alignment
= 1;
1018 bt_ctf_field_type_init(&floating_point
->parent
);
1020 return floating_point
? &floating_point
->parent
: NULL
;
1023 int bt_ctf_field_type_floating_point_get_exponent_digits(
1024 struct bt_ctf_field_type
*type
)
1027 struct bt_ctf_field_type_floating_point
*floating_point
;
1029 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1034 floating_point
= container_of(type
,
1035 struct bt_ctf_field_type_floating_point
, parent
);
1036 ret
= (int) floating_point
->declaration
.exp
->len
;
1041 int bt_ctf_field_type_floating_point_set_exponent_digits(
1042 struct bt_ctf_field_type
*type
,
1043 unsigned int exponent_digits
)
1046 struct bt_ctf_field_type_floating_point
*floating_point
;
1048 if (!type
|| type
->frozen
||
1049 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1054 floating_point
= container_of(type
,
1055 struct bt_ctf_field_type_floating_point
, parent
);
1056 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1057 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1059 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1064 floating_point
->declaration
.exp
->len
= exponent_digits
;
1069 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1070 struct bt_ctf_field_type
*type
)
1073 struct bt_ctf_field_type_floating_point
*floating_point
;
1075 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1080 floating_point
= container_of(type
,
1081 struct bt_ctf_field_type_floating_point
, parent
);
1082 ret
= (int) floating_point
->mantissa
.len
+ 1;
1087 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1088 struct bt_ctf_field_type
*type
,
1089 unsigned int mantissa_digits
)
1092 struct bt_ctf_field_type_floating_point
*floating_point
;
1094 if (!type
|| type
->frozen
||
1095 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1100 floating_point
= container_of(type
,
1101 struct bt_ctf_field_type_floating_point
, parent
);
1103 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1104 (mantissa_digits
!= DBL_MANT_DIG
) &&
1105 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1110 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1115 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1117 struct bt_ctf_field_type_structure
*structure
=
1118 g_new0(struct bt_ctf_field_type_structure
, 1);
1124 structure
->parent
.declaration
= &structure
->declaration
.p
;
1125 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1126 structure
->fields
= g_ptr_array_new_with_free_func(
1127 (GDestroyNotify
)destroy_structure_field
);
1128 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1129 bt_ctf_field_type_init(&structure
->parent
);
1130 return &structure
->parent
;
1135 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1136 struct bt_ctf_field_type
*field_type
,
1137 const char *field_name
)
1140 struct bt_ctf_field_type_structure
*structure
;
1142 if (!type
|| !field_type
|| type
->frozen
||
1143 bt_ctf_validate_identifier(field_name
) ||
1144 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1145 bt_ctf_field_type_validate(field_type
)) {
1150 structure
= container_of(type
,
1151 struct bt_ctf_field_type_structure
, parent
);
1152 if (add_structure_field(structure
->fields
,
1153 structure
->field_name_to_index
, field_type
, field_name
)) {
1158 if (type
->declaration
->alignment
< field_type
->declaration
->alignment
) {
1159 type
->declaration
->alignment
=
1160 field_type
->declaration
->alignment
;
1166 int bt_ctf_field_type_structure_get_field_count(
1167 struct bt_ctf_field_type
*type
)
1170 struct bt_ctf_field_type_structure
*structure
;
1172 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1177 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1179 ret
= (int) structure
->fields
->len
;
1184 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1185 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1188 struct bt_ctf_field_type_structure
*structure
;
1189 struct structure_field
*field
;
1192 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1193 (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1198 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1200 if (index
>= structure
->fields
->len
) {
1205 field
= g_ptr_array_index(structure
->fields
, index
);
1206 *field_type
= field
->type
;
1207 bt_ctf_field_type_get(field
->type
);
1208 *field_name
= g_quark_to_string(field
->name
);
1213 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1214 struct bt_ctf_field_type
*type
,
1219 struct structure_field
*field
;
1220 struct bt_ctf_field_type_structure
*structure
;
1221 struct bt_ctf_field_type
*field_type
= NULL
;
1223 if (!type
|| !name
) {
1227 name_quark
= g_quark_try_string(name
);
1232 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1234 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1235 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1239 field
= structure
->fields
->pdata
[index
];
1240 field_type
= field
->type
;
1241 bt_ctf_field_type_get(field_type
);
1246 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1247 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1249 struct bt_ctf_field_type_variant
*variant
= NULL
;
1251 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1255 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1260 variant
->parent
.declaration
= &variant
->declaration
.p
;
1261 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1262 variant
->tag_name
= g_string_new(tag_name
);
1263 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1264 variant
->fields
= g_ptr_array_new_with_free_func(
1265 (GDestroyNotify
)destroy_structure_field
);
1267 bt_ctf_field_type_get(enum_tag
);
1268 variant
->tag
= container_of(enum_tag
,
1269 struct bt_ctf_field_type_enumeration
, parent
);
1272 bt_ctf_field_type_init(&variant
->parent
);
1273 return &variant
->parent
;
1278 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1279 struct bt_ctf_field_type
*type
)
1281 struct bt_ctf_field_type_variant
*variant
;
1282 struct bt_ctf_field_type
*tag_type
= NULL
;
1284 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1288 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1289 if (!variant
->tag
) {
1293 tag_type
= &variant
->tag
->parent
;
1294 bt_ctf_field_type_get(tag_type
);
1299 const char *bt_ctf_field_type_variant_get_tag_name(
1300 struct bt_ctf_field_type
*type
)
1302 struct bt_ctf_field_type_variant
*variant
;
1303 const char *tag_name
= NULL
;
1305 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1309 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1310 if (variant
->tag_name
->len
== 0) {
1314 tag_name
= variant
->tag_name
->str
;
1319 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1320 struct bt_ctf_field_type
*field_type
,
1321 const char *field_name
)
1325 struct bt_ctf_field_type_variant
*variant
;
1326 GQuark field_name_quark
= g_quark_from_string(field_name
);
1328 if (!type
|| !field_type
|| type
->frozen
||
1329 bt_ctf_validate_identifier(field_name
) ||
1330 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1331 bt_ctf_field_type_validate(field_type
)) {
1336 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1338 /* The user has explicitly provided a tag; validate against it. */
1342 /* Make sure this name is present in the enum tag */
1343 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1344 struct enumeration_mapping
*mapping
=
1345 g_ptr_array_index(variant
->tag
->entries
, i
);
1347 if (mapping
->string
== field_name_quark
) {
1354 /* Validation failed */
1360 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1361 field_type
, field_name
)) {
1369 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1370 struct bt_ctf_field_type
*type
,
1371 const char *field_name
)
1375 struct structure_field
*field
;
1376 struct bt_ctf_field_type_variant
*variant
;
1377 struct bt_ctf_field_type
*field_type
= NULL
;
1379 if (!type
|| !field_name
) {
1383 name_quark
= g_quark_try_string(field_name
);
1388 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1389 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1390 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1394 field
= g_ptr_array_index(variant
->fields
, index
);
1395 field_type
= field
->type
;
1396 bt_ctf_field_type_get(field_type
);
1401 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1402 struct bt_ctf_field_type
*type
,
1403 struct bt_ctf_field
*tag
)
1405 const char *enum_value
;
1406 struct bt_ctf_field_type
*field_type
= NULL
;
1408 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1412 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1417 /* Already increments field_type's reference count */
1418 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1424 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1427 struct bt_ctf_field_type_variant
*variant
;
1429 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1434 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1436 ret
= (int) variant
->fields
->len
;
1442 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1443 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1446 struct bt_ctf_field_type_variant
*variant
;
1447 struct structure_field
*field
;
1450 if (!type
|| index
< 0 || !field_name
|| !field_type
||
1451 (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1456 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1458 if (index
>= variant
->fields
->len
) {
1463 field
= g_ptr_array_index(variant
->fields
, index
);
1464 *field_type
= field
->type
;
1465 bt_ctf_field_type_get(field
->type
);
1466 *field_name
= g_quark_to_string(field
->name
);
1471 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1472 struct bt_ctf_field_type
*element_type
,
1473 unsigned int length
)
1475 struct bt_ctf_field_type_array
*array
= NULL
;
1477 if (!element_type
|| length
== 0 ||
1478 bt_ctf_field_type_validate(element_type
)) {
1482 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1487 array
->parent
.declaration
= &array
->declaration
.p
;
1488 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1490 bt_ctf_field_type_get(element_type
);
1491 array
->element_type
= element_type
;
1492 array
->length
= length
;
1493 bt_ctf_field_type_init(&array
->parent
);
1494 array
->parent
.declaration
->alignment
=
1495 element_type
->declaration
->alignment
;
1496 return &array
->parent
;
1501 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1502 struct bt_ctf_field_type
*type
)
1504 struct bt_ctf_field_type
*ret
= NULL
;
1505 struct bt_ctf_field_type_array
*array
;
1507 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1511 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1512 ret
= array
->element_type
;
1513 bt_ctf_field_type_get(ret
);
1518 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1521 struct bt_ctf_field_type_array
*array
;
1523 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1528 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1529 ret
= (int64_t) array
->length
;
1534 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1535 struct bt_ctf_field_type
*element_type
,
1536 const char *length_field_name
)
1538 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1540 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1541 bt_ctf_field_type_validate(element_type
)) {
1545 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1550 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1551 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1552 bt_ctf_field_type_get(element_type
);
1553 sequence
->element_type
= element_type
;
1554 sequence
->length_field_name
= g_string_new(length_field_name
);
1555 bt_ctf_field_type_init(&sequence
->parent
);
1556 sequence
->parent
.declaration
->alignment
=
1557 element_type
->declaration
->alignment
;
1558 return &sequence
->parent
;
1563 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1564 struct bt_ctf_field_type
*type
)
1566 struct bt_ctf_field_type
*ret
= NULL
;
1567 struct bt_ctf_field_type_sequence
*sequence
;
1569 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1573 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1575 ret
= sequence
->element_type
;
1576 bt_ctf_field_type_get(ret
);
1581 const char *bt_ctf_field_type_sequence_get_length_field_name(
1582 struct bt_ctf_field_type
*type
)
1584 const char *ret
= NULL
;
1585 struct bt_ctf_field_type_sequence
*sequence
;
1587 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1591 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1593 ret
= sequence
->length_field_name
->str
;
1598 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1600 struct bt_ctf_field_type_string
*string
=
1601 g_new0(struct bt_ctf_field_type_string
, 1);
1607 string
->parent
.declaration
= &string
->declaration
.p
;
1608 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1609 bt_ctf_field_type_init(&string
->parent
);
1610 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1611 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1612 return &string
->parent
;
1615 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1616 struct bt_ctf_field_type
*type
)
1618 struct bt_ctf_field_type_string
*string
;
1619 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1621 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1625 string
= container_of(type
, struct bt_ctf_field_type_string
,
1627 ret
= string
->declaration
.encoding
;
1632 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1633 enum ctf_string_encoding encoding
)
1636 struct bt_ctf_field_type_string
*string
;
1638 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1639 (encoding
!= CTF_STRING_UTF8
&&
1640 encoding
!= CTF_STRING_ASCII
)) {
1645 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1646 string
->declaration
.encoding
= encoding
;
1651 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1660 ret
= (int) type
->declaration
->alignment
;
1665 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1666 unsigned int alignment
)
1670 /* Alignment must be bit-aligned (1) or byte aligned */
1671 if (!type
|| type
->frozen
|| (alignment
!= 1 && (alignment
& 0x7))) {
1676 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1677 alignment
!= CHAR_BIT
) {
1682 type
->declaration
->alignment
= alignment
;
1688 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1689 struct bt_ctf_field_type
*type
)
1691 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1692 int internal_byte_order
= -1;
1698 switch (type
->declaration
->id
) {
1699 case CTF_TYPE_INTEGER
:
1701 struct bt_ctf_field_type_integer
*integer
= container_of(
1702 type
, struct bt_ctf_field_type_integer
, parent
);
1703 internal_byte_order
= integer
->declaration
.byte_order
;
1706 case CTF_TYPE_FLOAT
:
1708 struct bt_ctf_field_type_floating_point
*floating_point
=
1710 struct bt_ctf_field_type_floating_point
,
1712 internal_byte_order
= floating_point
->declaration
.byte_order
;
1719 switch (internal_byte_order
) {
1721 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1724 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1727 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1730 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1736 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1737 enum bt_ctf_byte_order byte_order
)
1740 int internal_byte_order
;
1741 enum ctf_type_id type_id
;
1743 if (!type
|| type
->frozen
) {
1748 type_id
= type
->declaration
->id
;
1749 switch (byte_order
) {
1750 case BT_CTF_BYTE_ORDER_NATIVE
:
1751 /* Leave unset. Will be initialized by parent. */
1752 internal_byte_order
= 0;
1754 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1755 internal_byte_order
= LITTLE_ENDIAN
;
1757 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1758 case BT_CTF_BYTE_ORDER_NETWORK
:
1759 internal_byte_order
= BIG_ENDIAN
;
1766 if (set_byte_order_funcs
[type_id
]) {
1767 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1773 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1774 struct bt_ctf_field_type
*type
)
1777 return CTF_TYPE_UNKNOWN
;
1780 return type
->declaration
->id
;
1783 const char *bt_ctf_field_type_get_alias_name(
1784 struct bt_ctf_field_type
*type
)
1786 const char *name
= NULL
;
1788 if (!type
|| !type
->alias_name
) {
1792 name
= type
->alias_name
->str
;
1797 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1803 bt_ctf_ref_get(&type
->ref_count
);
1806 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1812 bt_ctf_ref_put(&type
->ref_count
, bt_ctf_field_type_destroy
);
1816 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1826 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1827 struct bt_ctf_field_type_variant
*variant
,
1830 struct bt_ctf_field_type
*type
= NULL
;
1831 GQuark field_name_quark
;
1833 struct structure_field
*field_entry
;
1834 struct range_overlap_query query
= {
1835 .range_start
._signed
= tag_value
,
1836 .range_end
._signed
= tag_value
,
1837 .mapping_name
= 0, .overlaps
= 0};
1839 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1841 if (!query
.overlaps
) {
1845 field_name_quark
= query
.mapping_name
;
1846 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1847 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1851 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1852 type
= field_entry
->type
;
1858 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
1859 struct bt_ctf_field_type_variant
*variant
,
1862 struct bt_ctf_field_type
*type
= NULL
;
1863 GQuark field_name_quark
;
1865 struct structure_field
*field_entry
;
1866 struct range_overlap_query query
= {
1867 .range_start
._unsigned
= tag_value
,
1868 .range_end
._unsigned
= tag_value
,
1869 .mapping_name
= 0, .overlaps
= 0};
1871 g_ptr_array_foreach(variant
->tag
->entries
,
1872 check_ranges_overlap_unsigned
,
1874 if (!query
.overlaps
) {
1878 field_name_quark
= query
.mapping_name
;
1879 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1880 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1884 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
1885 type
= field_entry
->type
;
1891 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
1892 struct metadata_context
*context
)
1896 if (!type
|| !context
) {
1901 ret
= type
->serialize(type
, context
);
1907 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
1914 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
1915 if (set_byte_order_funcs
[type
->declaration
->id
]) {
1916 set_byte_order_funcs
[type
->declaration
->id
](type
,
1922 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
1924 struct bt_ctf_field_type
*copy
= NULL
;
1930 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
1936 void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref
*ref
)
1938 struct bt_ctf_field_type_integer
*integer
;
1944 integer
= container_of(
1945 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1946 struct bt_ctf_field_type_integer
, parent
);
1947 if (integer
->mapped_clock
) {
1948 bt_ctf_clock_put(integer
->mapped_clock
);
1954 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref
*ref
)
1956 struct bt_ctf_field_type_enumeration
*enumeration
;
1962 enumeration
= container_of(
1963 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1964 struct bt_ctf_field_type_enumeration
, parent
);
1965 g_ptr_array_free(enumeration
->entries
, TRUE
);
1966 bt_ctf_field_type_put(enumeration
->container
);
1967 g_free(enumeration
);
1971 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref
*ref
)
1973 struct bt_ctf_field_type_floating_point
*floating_point
;
1979 floating_point
= container_of(
1980 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1981 struct bt_ctf_field_type_floating_point
, parent
);
1982 g_free(floating_point
);
1986 void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref
*ref
)
1988 struct bt_ctf_field_type_structure
*structure
;
1994 structure
= container_of(
1995 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
1996 struct bt_ctf_field_type_structure
, parent
);
1997 g_ptr_array_free(structure
->fields
, TRUE
);
1998 g_hash_table_destroy(structure
->field_name_to_index
);
2003 void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref
*ref
)
2005 struct bt_ctf_field_type_variant
*variant
;
2011 variant
= container_of(
2012 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2013 struct bt_ctf_field_type_variant
, parent
);
2014 g_ptr_array_free(variant
->fields
, TRUE
);
2015 g_hash_table_destroy(variant
->field_name_to_index
);
2016 g_string_free(variant
->tag_name
, TRUE
);
2017 bt_ctf_field_type_put(&variant
->tag
->parent
);
2022 void bt_ctf_field_type_array_destroy(struct bt_ctf_ref
*ref
)
2024 struct bt_ctf_field_type_array
*array
;
2030 array
= container_of(
2031 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2032 struct bt_ctf_field_type_array
, parent
);
2033 bt_ctf_field_type_put(array
->element_type
);
2038 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref
*ref
)
2040 struct bt_ctf_field_type_sequence
*sequence
;
2046 sequence
= container_of(
2047 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2048 struct bt_ctf_field_type_sequence
, parent
);
2049 bt_ctf_field_type_put(sequence
->element_type
);
2050 g_string_free(sequence
->length_field_name
, TRUE
);
2055 void bt_ctf_field_type_string_destroy(struct bt_ctf_ref
*ref
)
2057 struct bt_ctf_field_type_string
*string
;
2063 string
= container_of(
2064 container_of(ref
, struct bt_ctf_field_type
, ref_count
),
2065 struct bt_ctf_field_type_string
, parent
);
2070 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2076 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2078 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2079 type
, struct bt_ctf_field_type_enumeration
, parent
);
2081 generic_field_type_freeze(type
);
2082 bt_ctf_field_type_freeze(enumeration_type
->container
);
2086 void freeze_structure_field(struct structure_field
*field
)
2088 bt_ctf_field_type_freeze(field
->type
);
2092 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2094 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2095 type
, struct bt_ctf_field_type_structure
, parent
);
2097 generic_field_type_freeze(type
);
2098 g_ptr_array_foreach(structure_type
->fields
, (GFunc
)freeze_structure_field
,
2103 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2105 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2106 type
, struct bt_ctf_field_type_variant
, parent
);
2108 generic_field_type_freeze(type
);
2109 g_ptr_array_foreach(variant_type
->fields
, (GFunc
)freeze_structure_field
,
2114 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2116 struct bt_ctf_field_type_array
*array_type
= container_of(
2117 type
, struct bt_ctf_field_type_array
, parent
);
2119 generic_field_type_freeze(type
);
2120 bt_ctf_field_type_freeze(array_type
->element_type
);
2124 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2126 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2127 type
, struct bt_ctf_field_type_sequence
, parent
);
2129 generic_field_type_freeze(type
);
2130 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2134 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2136 const char *encoding_string
;
2139 case CTF_STRING_NONE
:
2140 encoding_string
= "none";
2142 case CTF_STRING_ASCII
:
2143 encoding_string
= "ASCII";
2145 case CTF_STRING_UTF8
:
2146 encoding_string
= "UTF8";
2149 encoding_string
= "unknown";
2153 return encoding_string
;
2157 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2159 const char *base_string
;
2162 case BT_CTF_INTEGER_BASE_DECIMAL
:
2163 base_string
= "decimal";
2165 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2166 base_string
= "hexadecimal";
2168 case BT_CTF_INTEGER_BASE_OCTAL
:
2169 base_string
= "octal";
2171 case BT_CTF_INTEGER_BASE_BINARY
:
2172 base_string
= "binary";
2175 base_string
= "unknown";
2183 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2184 struct metadata_context
*context
)
2186 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2187 struct bt_ctf_field_type_integer
, parent
);
2190 g_string_append_printf(context
->string
,
2191 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2192 integer
->declaration
.len
, type
->declaration
->alignment
,
2193 (integer
->declaration
.signedness
? "true" : "false"),
2194 get_encoding_string(integer
->declaration
.encoding
),
2195 get_integer_base_string(integer
->declaration
.base
),
2196 get_byte_order_string(integer
->declaration
.byte_order
));
2197 if (integer
->mapped_clock
) {
2198 const char *clock_name
= bt_ctf_clock_get_name(
2199 integer
->mapped_clock
);
2206 g_string_append_printf(context
->string
,
2207 "; map = clock.%s.value", clock_name
);
2210 g_string_append(context
->string
, "; }");
2216 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2217 struct metadata_context
*context
)
2221 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2222 struct bt_ctf_field_type_enumeration
, parent
);
2223 struct bt_ctf_field_type
*container_type
;
2224 int container_signed
;
2226 ret
= bt_ctf_field_type_validate(type
);
2231 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2232 if (!container_type
) {
2237 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2238 if (container_signed
< 0) {
2239 ret
= container_signed
;
2240 goto error_put_container_type
;
2243 g_string_append(context
->string
, "enum : ");
2244 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2246 goto error_put_container_type
;
2249 g_string_append(context
->string
, " { ");
2250 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2251 struct enumeration_mapping
*mapping
=
2252 enumeration
->entries
->pdata
[entry
];
2254 if (container_signed
) {
2255 if (mapping
->range_start
._signed
==
2256 mapping
->range_end
._signed
) {
2257 g_string_append_printf(context
->string
,
2258 "\"%s\" = %" PRId64
,
2259 g_quark_to_string(mapping
->string
),
2260 mapping
->range_start
._signed
);
2262 g_string_append_printf(context
->string
,
2263 "\"%s\" = %" PRId64
" ... %" PRId64
,
2264 g_quark_to_string(mapping
->string
),
2265 mapping
->range_start
._signed
,
2266 mapping
->range_end
._signed
);
2269 if (mapping
->range_start
._unsigned
==
2270 mapping
->range_end
._unsigned
) {
2271 g_string_append_printf(context
->string
,
2272 "\"%s\" = %" PRIu64
,
2273 g_quark_to_string(mapping
->string
),
2274 mapping
->range_start
._unsigned
);
2276 g_string_append_printf(context
->string
,
2277 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2278 g_quark_to_string(mapping
->string
),
2279 mapping
->range_start
._unsigned
,
2280 mapping
->range_end
._unsigned
);
2284 g_string_append(context
->string
,
2285 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2289 if (context
->field_name
->len
) {
2290 g_string_append_printf(context
->string
, " %s",
2291 context
->field_name
->str
);
2292 g_string_assign(context
->field_name
, "");
2294 error_put_container_type
:
2295 bt_ctf_field_type_put(container_type
);
2301 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2302 struct metadata_context
*context
)
2304 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2305 type
, struct bt_ctf_field_type_floating_point
, parent
);
2307 g_string_append_printf(context
->string
,
2308 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2309 floating_point
->declaration
.exp
->len
,
2310 floating_point
->declaration
.mantissa
->len
+ 1,
2311 get_byte_order_string(floating_point
->declaration
.byte_order
),
2312 type
->declaration
->alignment
);
2317 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2318 struct metadata_context
*context
)
2321 unsigned int indent
;
2323 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2324 struct bt_ctf_field_type_structure
, parent
);
2325 GString
*structure_field_name
= context
->field_name
;
2327 context
->field_name
= g_string_new("");
2329 context
->current_indentation_level
++;
2330 g_string_append(context
->string
, "struct {\n");
2332 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2333 struct structure_field
*field
;
2335 for (indent
= 0; indent
< context
->current_indentation_level
;
2337 g_string_append_c(context
->string
, '\t');
2340 field
= structure
->fields
->pdata
[i
];
2341 g_string_assign(context
->field_name
,
2342 g_quark_to_string(field
->name
));
2343 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2348 if (context
->field_name
->len
) {
2349 g_string_append_printf(context
->string
, " %s",
2350 context
->field_name
->str
);
2352 g_string_append(context
->string
, ";\n");
2355 context
->current_indentation_level
--;
2356 for (indent
= 0; indent
< context
->current_indentation_level
;
2358 g_string_append_c(context
->string
, '\t');
2361 g_string_append_printf(context
->string
, "} align(%zu)",
2362 type
->declaration
->alignment
);
2364 g_string_free(context
->field_name
, TRUE
);
2365 context
->field_name
= structure_field_name
;
2370 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2371 struct metadata_context
*context
)
2374 unsigned int indent
;
2376 struct bt_ctf_field_type_variant
*variant
= container_of(
2377 type
, struct bt_ctf_field_type_variant
, parent
);
2378 GString
*variant_field_name
= context
->field_name
;
2380 context
->field_name
= g_string_new("");
2381 if (variant
->tag_name
->len
> 0) {
2382 g_string_append_printf(context
->string
,
2383 "variant <%s> {\n", variant
->tag_name
->str
);
2385 g_string_append(context
->string
, "variant {\n");
2388 context
->current_indentation_level
++;
2389 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2390 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2392 g_string_assign(context
->field_name
,
2393 g_quark_to_string(field
->name
));
2394 for (indent
= 0; indent
< context
->current_indentation_level
;
2396 g_string_append_c(context
->string
, '\t');
2399 g_string_assign(context
->field_name
,
2400 g_quark_to_string(field
->name
));
2401 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2406 if (context
->field_name
->len
) {
2407 g_string_append_printf(context
->string
, " %s;",
2408 context
->field_name
->str
);
2411 g_string_append_c(context
->string
, '\n');
2414 context
->current_indentation_level
--;
2415 for (indent
= 0; indent
< context
->current_indentation_level
;
2417 g_string_append_c(context
->string
, '\t');
2420 g_string_append(context
->string
, "}");
2422 g_string_free(context
->field_name
, TRUE
);
2423 context
->field_name
= variant_field_name
;
2428 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2429 struct metadata_context
*context
)
2432 struct bt_ctf_field_type_array
*array
= container_of(type
,
2433 struct bt_ctf_field_type_array
, parent
);
2435 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2440 if (context
->field_name
->len
) {
2441 g_string_append_printf(context
->string
, " %s[%u]",
2442 context
->field_name
->str
, array
->length
);
2443 g_string_assign(context
->field_name
, "");
2445 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2452 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2453 struct metadata_context
*context
)
2456 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2457 type
, struct bt_ctf_field_type_sequence
, parent
);
2459 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2464 if (context
->field_name
->len
) {
2465 g_string_append_printf(context
->string
, " %s[%s]",
2466 context
->field_name
->str
,
2467 sequence
->length_field_name
->str
);
2468 g_string_assign(context
->field_name
, "");
2470 g_string_append_printf(context
->string
, "[%s]",
2471 sequence
->length_field_name
->str
);
2478 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2479 struct metadata_context
*context
)
2481 struct bt_ctf_field_type_string
*string
= container_of(
2482 type
, struct bt_ctf_field_type_string
, parent
);
2484 g_string_append_printf(context
->string
,
2485 "string { encoding = %s; }",
2486 get_encoding_string(string
->declaration
.encoding
));
2491 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2492 int byte_order
, int set_native
)
2494 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2495 struct bt_ctf_field_type_integer
, parent
);
2498 integer_type
->declaration
.byte_order
=
2499 integer_type
->declaration
.byte_order
== 0 ?
2500 byte_order
: integer_type
->declaration
.byte_order
;
2502 integer_type
->declaration
.byte_order
= byte_order
;
2507 void bt_ctf_field_type_enumeration_set_byte_order(
2508 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2510 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2511 struct bt_ctf_field_type_enumeration
, parent
);
2513 /* Safe to assume that container is an integer */
2514 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2515 byte_order
, set_native
);
2519 void bt_ctf_field_type_floating_point_set_byte_order(
2520 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2522 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2523 container_of(type
, struct bt_ctf_field_type_floating_point
,
2527 floating_point_type
->declaration
.byte_order
=
2528 floating_point_type
->declaration
.byte_order
== 0 ?
2530 floating_point_type
->declaration
.byte_order
;
2531 floating_point_type
->sign
.byte_order
=
2532 floating_point_type
->sign
.byte_order
== 0 ?
2533 byte_order
: floating_point_type
->sign
.byte_order
;
2534 floating_point_type
->mantissa
.byte_order
=
2535 floating_point_type
->mantissa
.byte_order
== 0 ?
2536 byte_order
: floating_point_type
->mantissa
.byte_order
;
2537 floating_point_type
->exp
.byte_order
=
2538 floating_point_type
->exp
.byte_order
== 0 ?
2539 byte_order
: floating_point_type
->exp
.byte_order
;
2541 floating_point_type
->declaration
.byte_order
= byte_order
;
2542 floating_point_type
->sign
.byte_order
= byte_order
;
2543 floating_point_type
->mantissa
.byte_order
= byte_order
;
2544 floating_point_type
->exp
.byte_order
= byte_order
;
2549 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2550 int byte_order
, int set_native
)
2553 struct bt_ctf_field_type_structure
*structure_type
=
2554 container_of(type
, struct bt_ctf_field_type_structure
,
2557 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2558 struct structure_field
*field
= g_ptr_array_index(
2559 structure_type
->fields
, i
);
2560 struct bt_ctf_field_type
*field_type
= field
->type
;
2562 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2563 set_byte_order_funcs
[field_type
->declaration
->id
](
2564 field_type
, byte_order
, set_native
);
2570 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2571 int byte_order
, int set_native
)
2574 struct bt_ctf_field_type_variant
*variant_type
=
2575 container_of(type
, struct bt_ctf_field_type_variant
,
2578 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2579 struct structure_field
*field
= g_ptr_array_index(
2580 variant_type
->fields
, i
);
2581 struct bt_ctf_field_type
*field_type
= field
->type
;
2583 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2584 set_byte_order_funcs
[field_type
->declaration
->id
](
2585 field_type
, byte_order
, set_native
);
2591 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2592 int byte_order
, int set_native
)
2594 struct bt_ctf_field_type_array
*array_type
=
2595 container_of(type
, struct bt_ctf_field_type_array
,
2598 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
2599 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
2600 array_type
->element_type
, byte_order
, set_native
);
2605 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
2606 int byte_order
, int set_native
)
2608 struct bt_ctf_field_type_sequence
*sequence_type
=
2609 container_of(type
, struct bt_ctf_field_type_sequence
,
2612 if (set_byte_order_funcs
[
2613 sequence_type
->element_type
->declaration
->id
]) {
2614 set_byte_order_funcs
[
2615 sequence_type
->element_type
->declaration
->id
](
2616 sequence_type
->element_type
, byte_order
, set_native
);
2621 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
2622 struct bt_ctf_field_type
*type
)
2624 struct bt_ctf_field_type
*copy
;
2625 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
2627 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
2628 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
2633 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
2635 copy_integer
->declaration
= integer
->declaration
;
2636 if (integer
->mapped_clock
) {
2637 bt_ctf_clock_get(integer
->mapped_clock
);
2638 copy_integer
->mapped_clock
= integer
->mapped_clock
;
2645 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
2646 struct bt_ctf_field_type
*type
)
2649 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
2650 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
2652 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
2655 /* Copy the source enumeration's container */
2656 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
2657 if (!copy_container
) {
2661 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
2665 copy_enumeration
= container_of(copy
,
2666 struct bt_ctf_field_type_enumeration
, parent
);
2668 /* Copy all enumaration entries */
2669 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
2670 struct enumeration_mapping
*mapping
= g_ptr_array_index(
2671 enumeration
->entries
, i
);
2672 struct enumeration_mapping
* copy_mapping
= g_new0(
2673 struct enumeration_mapping
, 1);
2675 if (!copy_mapping
) {
2679 *copy_mapping
= *mapping
;
2680 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
2683 copy_enumeration
->declaration
= enumeration
->declaration
;
2685 if (copy_container
) {
2686 bt_ctf_field_type_put(copy_container
);
2690 if (copy_container
) {
2691 bt_ctf_field_type_put(copy_container
);
2693 bt_ctf_field_type_put(copy
);
2698 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
2699 struct bt_ctf_field_type
*type
)
2701 struct bt_ctf_field_type
*copy
;
2702 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
2704 floating_point
= container_of(type
,
2705 struct bt_ctf_field_type_floating_point
, parent
);
2706 copy
= bt_ctf_field_type_floating_point_create();
2711 copy_float
= container_of(copy
,
2712 struct bt_ctf_field_type_floating_point
, parent
);
2713 copy_float
->declaration
= floating_point
->declaration
;
2714 copy_float
->sign
= floating_point
->sign
;
2715 copy_float
->mantissa
= floating_point
->mantissa
;
2716 copy_float
->exp
= floating_point
->exp
;
2722 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
2723 struct bt_ctf_field_type
*type
)
2726 GHashTableIter iter
;
2727 gpointer key
, value
;
2728 struct bt_ctf_field_type
*copy
;
2729 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
2731 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2733 copy
= bt_ctf_field_type_structure_create();
2738 copy_structure
= container_of(copy
,
2739 struct bt_ctf_field_type_structure
, parent
);
2741 /* Copy field_name_to_index */
2742 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
2743 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2744 g_hash_table_insert(copy_structure
->field_name_to_index
,
2748 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2749 struct structure_field
*entry
, *copy_entry
;
2750 struct bt_ctf_field_type
*copy_field
;
2752 copy_entry
= g_new0(struct structure_field
, 1);
2757 entry
= g_ptr_array_index(structure
->fields
, i
);
2758 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2764 copy_entry
->name
= entry
->name
;
2765 copy_entry
->type
= copy_field
;
2766 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
2769 copy_structure
->declaration
= structure
->declaration
;
2773 bt_ctf_field_type_put(copy
);
2778 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
2779 struct bt_ctf_field_type
*type
)
2782 GHashTableIter iter
;
2783 gpointer key
, value
;
2784 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
2785 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
2787 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2790 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
2796 copy
= bt_ctf_field_type_variant_create(copy_tag
,
2797 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
2802 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
2805 /* Copy field_name_to_index */
2806 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
2807 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2808 g_hash_table_insert(copy_variant
->field_name_to_index
,
2812 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2813 struct structure_field
*entry
, *copy_entry
;
2814 struct bt_ctf_field_type
*copy_field
;
2816 copy_entry
= g_new0(struct structure_field
, 1);
2821 entry
= g_ptr_array_index(variant
->fields
, i
);
2822 copy_field
= bt_ctf_field_type_copy(entry
->type
);
2828 copy_entry
->name
= entry
->name
;
2829 copy_entry
->type
= copy_field
;
2830 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
2833 copy_variant
->declaration
= variant
->declaration
;
2836 bt_ctf_field_type_put(copy_tag
);
2842 bt_ctf_field_type_put(copy_tag
);
2845 bt_ctf_field_type_put(copy
);
2850 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
2851 struct bt_ctf_field_type
*type
)
2853 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2854 struct bt_ctf_field_type_array
*array
, *copy_array
;
2856 array
= container_of(type
, struct bt_ctf_field_type_array
,
2858 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
2859 if (!copy_element
) {
2863 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
2868 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
2870 copy_array
->declaration
= array
->declaration
;
2873 bt_ctf_field_type_put(copy_element
);
2880 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
2881 struct bt_ctf_field_type
*type
)
2883 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
2884 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
2886 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2888 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
2889 if (!copy_element
) {
2893 copy
= bt_ctf_field_type_sequence_create(copy_element
,
2894 sequence
->length_field_name
->len
?
2895 sequence
->length_field_name
->str
: NULL
);
2900 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
2902 copy_sequence
->declaration
= sequence
->declaration
;
2905 bt_ctf_field_type_put(copy_element
);
2912 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
2913 struct bt_ctf_field_type
*type
)
2915 struct bt_ctf_field_type
*copy
;
2916 struct bt_ctf_field_type_string
*string
, *copy_string
;
2918 copy
= bt_ctf_field_type_string_create();
2923 string
= container_of(type
, struct bt_ctf_field_type_string
,
2925 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
2927 copy_string
->declaration
= string
->declaration
;