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/ref.h>
33 #include <babeltrace/ctf-ir/clock.h>
34 #include <babeltrace/ctf-writer/writer-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/endian.h>
43 struct range_overlap_query
{
58 void bt_ctf_field_type_destroy(struct bt_object
*);
60 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
62 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
64 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
66 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
68 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
70 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
72 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
74 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
77 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
78 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 bt_ctf_field_type_enumeration_destroy
,
82 bt_ctf_field_type_floating_point_destroy
,
83 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_destroy
,
84 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_destroy
,
85 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_destroy
,
86 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
87 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_destroy
,
91 void generic_field_type_freeze(struct bt_ctf_field_type
*);
93 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
99 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
101 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
104 type_freeze_func
const type_freeze_funcs
[] = {
105 [CTF_TYPE_INTEGER
] = generic_field_type_freeze
,
106 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
107 [CTF_TYPE_FLOAT
] = generic_field_type_freeze
,
108 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_freeze
,
109 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_freeze
,
110 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_freeze
,
111 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
112 [CTF_TYPE_STRING
] = generic_field_type_freeze
,
116 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_floating_point_serialize(
123 struct bt_ctf_field_type
*, struct metadata_context
*);
125 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
141 type_serialize_func
const type_serialize_funcs
[] = {
142 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_serialize
,
144 bt_ctf_field_type_enumeration_serialize
,
146 bt_ctf_field_type_floating_point_serialize
,
148 bt_ctf_field_type_structure_serialize
,
149 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_serialize
,
150 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_serialize
,
151 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
152 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_serialize
,
156 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_floating_point_set_byte_order(
163 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
165 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 /* The set_native flag only set the byte order if it is set to native */
179 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
180 int byte_order
, int set_native
) = {
181 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
183 bt_ctf_field_type_enumeration_set_byte_order
,
185 bt_ctf_field_type_floating_point_set_byte_order
,
187 bt_ctf_field_type_structure_set_byte_order
,
188 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
189 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
190 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
191 [CTF_TYPE_STRING
] = NULL
,
195 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
217 struct bt_ctf_field_type
*);
220 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
221 struct bt_ctf_field_type
*) = {
222 [CTF_TYPE_INTEGER
] = bt_ctf_field_type_integer_copy
,
223 [CTF_TYPE_ENUM
] = bt_ctf_field_type_enumeration_copy
,
224 [CTF_TYPE_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
225 [CTF_TYPE_STRUCT
] = bt_ctf_field_type_structure_copy
,
226 [CTF_TYPE_VARIANT
] = bt_ctf_field_type_variant_copy
,
227 [CTF_TYPE_ARRAY
] = bt_ctf_field_type_array_copy
,
228 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
229 [CTF_TYPE_STRING
] = bt_ctf_field_type_string_copy
,
233 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
239 void destroy_structure_field(struct structure_field
*field
)
246 void check_ranges_overlap(gpointer element
, gpointer query
)
248 struct enumeration_mapping
*mapping
= element
;
249 struct range_overlap_query
*overlap_query
= query
;
251 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
252 && overlap_query
->range_start
._signed
<=
253 mapping
->range_end
._signed
) {
254 overlap_query
->overlaps
= 1;
255 overlap_query
->mapping_name
= mapping
->string
;
258 overlap_query
->overlaps
|=
259 mapping
->string
== overlap_query
->mapping_name
;
263 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
265 struct enumeration_mapping
*mapping
= element
;
266 struct range_overlap_query
*overlap_query
= query
;
268 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
269 && overlap_query
->range_start
._unsigned
<=
270 mapping
->range_end
._unsigned
) {
271 overlap_query
->overlaps
= 1;
272 overlap_query
->mapping_name
= mapping
->string
;
275 overlap_query
->overlaps
|=
276 mapping
->string
== overlap_query
->mapping_name
;
280 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
281 struct enumeration_mapping
**b
)
283 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
287 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
288 struct enumeration_mapping
**b
)
290 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
294 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
296 enum ctf_type_id type_id
= type
->declaration
->id
;
298 assert(type
&& (type_id
> CTF_TYPE_UNKNOWN
) &&
299 (type_id
< NR_CTF_TYPES
));
301 bt_object_init(type
, bt_ctf_field_type_destroy
);
302 type
->freeze
= type_freeze_funcs
[type_id
];
303 type
->serialize
= type_serialize_funcs
[type_id
];
306 int ret
= bt_ctf_field_type_set_byte_order(type
,
307 BT_CTF_BYTE_ORDER_NATIVE
);
311 type
->declaration
->alignment
= 1;
315 int add_structure_field(GPtrArray
*fields
,
316 GHashTable
*field_name_to_index
,
317 struct bt_ctf_field_type
*field_type
,
318 const char *field_name
)
321 GQuark name_quark
= g_quark_from_string(field_name
);
322 struct structure_field
*field
;
324 /* Make sure structure does not contain a field of the same name */
325 if (g_hash_table_lookup_extended(field_name_to_index
,
326 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
331 field
= g_new0(struct structure_field
, 1);
338 field
->name
= name_quark
;
339 field
->type
= field_type
;
340 g_hash_table_insert(field_name_to_index
,
341 (gpointer
) (unsigned long) name_quark
,
342 (gpointer
) (unsigned long) fields
->len
);
343 g_ptr_array_add(fields
, field
);
349 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
351 struct bt_ctf_field_type
*type
;
352 enum ctf_type_id type_id
;
354 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
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
](type
);
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;
385 case CTF_TYPE_SEQUENCE
:
387 struct bt_ctf_field_type_sequence
*sequence
=
388 container_of(type
, struct bt_ctf_field_type_sequence
,
391 /* length field name should be set at this point */
392 ret
= sequence
->length_field_name
->len
? 0 : -1;
395 case CTF_TYPE_VARIANT
:
397 struct bt_ctf_field_type_variant
*variant
=
398 container_of(type
, struct bt_ctf_field_type_variant
,
401 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
413 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
415 struct bt_ctf_field_type_integer
*integer
=
416 g_new0(struct bt_ctf_field_type_integer
, 1);
418 if (!integer
|| size
== 0 || size
> 64) {
422 integer
->parent
.declaration
= &integer
->declaration
.p
;
423 integer
->parent
.declaration
->id
= CTF_TYPE_INTEGER
;
424 integer
->declaration
.len
= size
;
425 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
426 integer
->declaration
.encoding
= CTF_STRING_NONE
;
427 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
428 return &integer
->parent
;
431 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
434 struct bt_ctf_field_type_integer
*integer
;
436 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
441 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
442 ret
= (int) integer
->declaration
.len
;
447 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
450 struct bt_ctf_field_type_integer
*integer
;
452 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
457 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
458 ret
= integer
->declaration
.signedness
;
463 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
467 struct bt_ctf_field_type_integer
*integer
;
469 if (!type
|| type
->frozen
||
470 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
475 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
476 integer
->declaration
.signedness
= !!is_signed
;
481 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
482 struct bt_ctf_field_type
*type
)
484 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
485 struct bt_ctf_field_type_integer
*integer
;
487 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
491 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
492 ret
= integer
->declaration
.base
;
497 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
498 enum bt_ctf_integer_base base
)
502 if (!type
|| type
->frozen
||
503 type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
509 case BT_CTF_INTEGER_BASE_BINARY
:
510 case BT_CTF_INTEGER_BASE_OCTAL
:
511 case BT_CTF_INTEGER_BASE_DECIMAL
:
512 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
514 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
515 struct bt_ctf_field_type_integer
, parent
);
516 integer
->declaration
.base
= base
;
526 enum ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
527 struct bt_ctf_field_type
*type
)
529 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
530 struct bt_ctf_field_type_integer
*integer
;
532 if (!type
|| type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
536 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
537 ret
= integer
->declaration
.encoding
;
542 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
543 enum ctf_string_encoding encoding
)
546 struct bt_ctf_field_type_integer
*integer
;
548 if (!type
|| type
->frozen
||
549 (type
->declaration
->id
!= CTF_TYPE_INTEGER
) ||
550 (encoding
< CTF_STRING_NONE
) ||
551 (encoding
>= CTF_STRING_UNKNOWN
)) {
556 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
557 integer
->declaration
.encoding
= encoding
;
562 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
563 struct bt_ctf_field_type
*type
)
565 struct bt_ctf_field_type_integer
*integer
;
566 struct bt_ctf_clock
*clock
= NULL
;
572 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
573 clock
= integer
->mapped_clock
;
579 int bt_ctf_field_type_integer_set_mapped_clock(
580 struct bt_ctf_field_type
*type
,
581 struct bt_ctf_clock
*clock
)
583 struct bt_ctf_field_type_integer
*integer
;
586 if (!type
|| type
->frozen
) {
591 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
592 bt_put(integer
->mapped_clock
);
594 integer
->mapped_clock
= clock
;
599 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
600 struct bt_ctf_field_type
*integer_container_type
)
602 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
604 if (!integer_container_type
) {
608 if (integer_container_type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
612 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
617 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
618 enumeration
->parent
.declaration
->id
= CTF_TYPE_ENUM
;
619 bt_get(integer_container_type
);
620 enumeration
->container
= integer_container_type
;
621 enumeration
->entries
= g_ptr_array_new_with_free_func(
622 (GDestroyNotify
)destroy_enumeration_mapping
);
623 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
624 return &enumeration
->parent
;
630 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
631 struct bt_ctf_field_type
*type
)
633 struct bt_ctf_field_type
*container_type
= NULL
;
634 struct bt_ctf_field_type_enumeration
*enumeration_type
;
640 if (type
->declaration
->id
!= CTF_TYPE_ENUM
) {
644 enumeration_type
= container_of(type
,
645 struct bt_ctf_field_type_enumeration
, parent
);
646 container_type
= enumeration_type
->container
;
647 bt_get(container_type
);
649 return container_type
;
652 int bt_ctf_field_type_enumeration_add_mapping(
653 struct bt_ctf_field_type
*type
, const char *string
,
654 int64_t range_start
, int64_t range_end
)
658 struct enumeration_mapping
*mapping
;
659 struct bt_ctf_field_type_enumeration
*enumeration
;
660 struct range_overlap_query query
;
661 char *escaped_string
;
663 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
665 (range_end
< range_start
)) {
670 if (!string
|| strlen(string
) == 0) {
675 escaped_string
= g_strescape(string
, NULL
);
676 if (!escaped_string
) {
681 mapping_name
= g_quark_from_string(escaped_string
);
682 query
= (struct range_overlap_query
) {
683 .range_start
._signed
= range_start
,
684 .range_end
._signed
= range_end
,
685 .mapping_name
= mapping_name
,
687 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
690 /* Check that the range does not overlap with one already present */
691 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
692 if (query
.overlaps
) {
697 mapping
= g_new(struct enumeration_mapping
, 1);
703 *mapping
= (struct enumeration_mapping
) {
704 .range_start
._signed
= range_start
,
705 .range_end
._signed
= range_end
, .string
= mapping_name
};
706 g_ptr_array_add(enumeration
->entries
, mapping
);
707 g_ptr_array_sort(enumeration
->entries
,
708 (GCompareFunc
)compare_enumeration_mappings_signed
);
710 free(escaped_string
);
715 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
716 struct bt_ctf_field_type
*type
, const char *string
,
717 uint64_t range_start
, uint64_t range_end
)
721 struct enumeration_mapping
*mapping
;
722 struct bt_ctf_field_type_enumeration
*enumeration
;
723 struct range_overlap_query query
;
724 char *escaped_string
;
726 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
) ||
728 (range_end
< range_start
)) {
733 if (!string
|| strlen(string
) == 0) {
738 escaped_string
= g_strescape(string
, NULL
);
739 if (!escaped_string
) {
744 mapping_name
= g_quark_from_string(escaped_string
);
745 query
= (struct range_overlap_query
) {
746 .range_start
._unsigned
= range_start
,
747 .range_end
._unsigned
= range_end
,
748 .mapping_name
= mapping_name
,
750 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
753 /* Check that the range does not overlap with one already present */
754 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
756 if (query
.overlaps
) {
761 mapping
= g_new(struct enumeration_mapping
, 1);
767 *mapping
= (struct enumeration_mapping
) {
768 .range_start
._unsigned
= range_start
,
769 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
770 g_ptr_array_add(enumeration
->entries
, mapping
);
771 g_ptr_array_sort(enumeration
->entries
,
772 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
774 free(escaped_string
);
779 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
780 struct bt_ctf_field_type_enumeration
*enumeration_type
,
783 const char *name
= NULL
;
784 struct range_overlap_query query
=
785 (struct range_overlap_query
) {
786 .range_start
._unsigned
= value
,
787 .range_end
._unsigned
= value
,
790 g_ptr_array_foreach(enumeration_type
->entries
,
791 check_ranges_overlap_unsigned
,
793 if (!query
.overlaps
) {
797 name
= g_quark_to_string(query
.mapping_name
);
802 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
803 struct bt_ctf_field_type_enumeration
*enumeration_type
,
806 const char *name
= NULL
;
807 struct range_overlap_query query
=
808 (struct range_overlap_query
) {
809 .range_start
._signed
= value
,
810 .range_end
._signed
= value
,
813 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
815 if (!query
.overlaps
) {
819 name
= g_quark_to_string(query
.mapping_name
);
824 int bt_ctf_field_type_enumeration_get_mapping_count(
825 struct bt_ctf_field_type
*type
)
828 struct bt_ctf_field_type_enumeration
*enumeration
;
830 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
835 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
837 ret
= (int) enumeration
->entries
->len
;
843 struct enumeration_mapping
*get_enumeration_mapping(
844 struct bt_ctf_field_type
*type
, int index
)
846 struct enumeration_mapping
*mapping
= NULL
;
847 struct bt_ctf_field_type_enumeration
*enumeration
;
849 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
851 if (index
>= enumeration
->entries
->len
) {
855 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
860 int bt_ctf_field_type_enumeration_get_mapping(
861 struct bt_ctf_field_type
*type
, int index
,
862 const char **string
, int64_t *range_start
, int64_t *range_end
)
864 struct enumeration_mapping
*mapping
;
867 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
868 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
873 mapping
= get_enumeration_mapping(type
, index
);
879 *string
= g_quark_to_string(mapping
->string
);
880 *range_start
= mapping
->range_start
._signed
;
881 *range_end
= mapping
->range_end
._signed
;
886 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
887 struct bt_ctf_field_type
*type
, int index
,
888 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
890 struct enumeration_mapping
*mapping
;
893 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
894 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
899 mapping
= get_enumeration_mapping(type
, index
);
905 *string
= g_quark_to_string(mapping
->string
);
906 *range_start
= mapping
->range_start
._unsigned
;
907 *range_end
= mapping
->range_end
._unsigned
;
912 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
913 struct bt_ctf_field_type
*type
, const char *name
)
916 struct bt_ctf_field_type_enumeration
*enumeration
;
919 if (!type
|| !name
||
920 (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
925 name_quark
= g_quark_try_string(name
);
931 enumeration
= container_of(type
,
932 struct bt_ctf_field_type_enumeration
, parent
);
933 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
934 struct enumeration_mapping
*mapping
=
935 get_enumeration_mapping(type
, i
);
937 if (mapping
->string
== name_quark
) {
948 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
949 struct bt_ctf_field_type
*type
, int64_t value
)
951 struct bt_ctf_field_type_enumeration
*enumeration
;
954 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
959 enumeration
= container_of(type
,
960 struct bt_ctf_field_type_enumeration
, parent
);
961 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
962 struct enumeration_mapping
*mapping
=
963 get_enumeration_mapping(type
, i
);
965 if (value
>= mapping
->range_start
._signed
&&
966 value
<= mapping
->range_end
._signed
) {
977 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
978 struct bt_ctf_field_type
*type
, uint64_t value
)
980 struct bt_ctf_field_type_enumeration
*enumeration
;
983 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ENUM
)) {
988 enumeration
= container_of(type
,
989 struct bt_ctf_field_type_enumeration
, parent
);
990 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
991 struct enumeration_mapping
*mapping
=
992 get_enumeration_mapping(type
, i
);
994 if (value
>= mapping
->range_start
._unsigned
&&
995 value
<= mapping
->range_end
._unsigned
) {
1006 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1008 struct bt_ctf_field_type_floating_point
*floating_point
=
1009 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1011 if (!floating_point
) {
1015 floating_point
->declaration
.sign
= &floating_point
->sign
;
1016 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1017 floating_point
->declaration
.exp
= &floating_point
->exp
;
1018 floating_point
->sign
.len
= 1;
1019 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1020 floating_point
->parent
.declaration
->id
= CTF_TYPE_FLOAT
;
1021 floating_point
->declaration
.exp
->len
=
1022 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1023 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1024 floating_point
->sign
.p
.alignment
= 1;
1025 floating_point
->mantissa
.p
.alignment
= 1;
1026 floating_point
->exp
.p
.alignment
= 1;
1028 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1030 return floating_point
? &floating_point
->parent
: NULL
;
1033 int bt_ctf_field_type_floating_point_get_exponent_digits(
1034 struct bt_ctf_field_type
*type
)
1037 struct bt_ctf_field_type_floating_point
*floating_point
;
1039 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1044 floating_point
= container_of(type
,
1045 struct bt_ctf_field_type_floating_point
, parent
);
1046 ret
= (int) floating_point
->declaration
.exp
->len
;
1051 int bt_ctf_field_type_floating_point_set_exponent_digits(
1052 struct bt_ctf_field_type
*type
,
1053 unsigned int exponent_digits
)
1056 struct bt_ctf_field_type_floating_point
*floating_point
;
1058 if (!type
|| type
->frozen
||
1059 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1064 floating_point
= container_of(type
,
1065 struct bt_ctf_field_type_floating_point
, parent
);
1066 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1067 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1069 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1074 floating_point
->declaration
.exp
->len
= exponent_digits
;
1079 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1080 struct bt_ctf_field_type
*type
)
1083 struct bt_ctf_field_type_floating_point
*floating_point
;
1085 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1090 floating_point
= container_of(type
,
1091 struct bt_ctf_field_type_floating_point
, parent
);
1092 ret
= (int) floating_point
->mantissa
.len
+ 1;
1097 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1098 struct bt_ctf_field_type
*type
,
1099 unsigned int mantissa_digits
)
1102 struct bt_ctf_field_type_floating_point
*floating_point
;
1104 if (!type
|| type
->frozen
||
1105 (type
->declaration
->id
!= CTF_TYPE_FLOAT
)) {
1110 floating_point
= container_of(type
,
1111 struct bt_ctf_field_type_floating_point
, parent
);
1113 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1114 (mantissa_digits
!= DBL_MANT_DIG
) &&
1115 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1120 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1125 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1127 struct bt_ctf_field_type_structure
*structure
=
1128 g_new0(struct bt_ctf_field_type_structure
, 1);
1134 structure
->parent
.declaration
= &structure
->declaration
.p
;
1135 structure
->parent
.declaration
->id
= CTF_TYPE_STRUCT
;
1136 structure
->fields
= g_ptr_array_new_with_free_func(
1137 (GDestroyNotify
)destroy_structure_field
);
1138 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1139 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1140 return &structure
->parent
;
1145 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1146 struct bt_ctf_field_type
*field_type
,
1147 const char *field_name
)
1150 struct bt_ctf_field_type_structure
*structure
;
1152 if (!type
|| !field_type
|| type
->frozen
||
1153 bt_ctf_validate_identifier(field_name
) ||
1154 (type
->declaration
->id
!= CTF_TYPE_STRUCT
) ||
1155 bt_ctf_field_type_validate(field_type
)) {
1160 structure
= container_of(type
,
1161 struct bt_ctf_field_type_structure
, parent
);
1162 if (add_structure_field(structure
->fields
,
1163 structure
->field_name_to_index
, field_type
, field_name
)) {
1171 int bt_ctf_field_type_structure_get_field_count(
1172 struct bt_ctf_field_type
*type
)
1175 struct bt_ctf_field_type_structure
*structure
;
1177 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1182 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1184 ret
= (int) structure
->fields
->len
;
1189 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1190 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1193 struct bt_ctf_field_type_structure
*structure
;
1194 struct structure_field
*field
;
1197 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_STRUCT
)) {
1202 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1204 if (index
>= structure
->fields
->len
) {
1209 field
= g_ptr_array_index(structure
->fields
, index
);
1211 *field_type
= field
->type
;
1212 bt_get(field
->type
);
1215 *field_name
= g_quark_to_string(field
->name
);
1221 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1222 struct bt_ctf_field_type
*type
,
1227 struct structure_field
*field
;
1228 struct bt_ctf_field_type_structure
*structure
;
1229 struct bt_ctf_field_type
*field_type
= NULL
;
1231 if (!type
|| !name
) {
1235 name_quark
= g_quark_try_string(name
);
1240 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1242 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1243 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1247 field
= structure
->fields
->pdata
[index
];
1248 field_type
= field
->type
;
1254 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1255 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1257 struct bt_ctf_field_type_variant
*variant
= NULL
;
1259 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1263 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1268 variant
->parent
.declaration
= &variant
->declaration
.p
;
1269 variant
->parent
.declaration
->id
= CTF_TYPE_VARIANT
;
1270 variant
->tag_name
= g_string_new(tag_name
);
1271 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1272 variant
->fields
= g_ptr_array_new_with_free_func(
1273 (GDestroyNotify
) destroy_structure_field
);
1276 variant
->tag
= container_of(enum_tag
,
1277 struct bt_ctf_field_type_enumeration
, parent
);
1280 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1281 /* A variant's alignment is undefined */
1282 variant
->parent
.declaration
->alignment
= 0;
1283 return &variant
->parent
;
1288 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1289 struct bt_ctf_field_type
*type
)
1291 struct bt_ctf_field_type_variant
*variant
;
1292 struct bt_ctf_field_type
*tag_type
= NULL
;
1294 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1298 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1299 if (!variant
->tag
) {
1303 tag_type
= &variant
->tag
->parent
;
1309 const char *bt_ctf_field_type_variant_get_tag_name(
1310 struct bt_ctf_field_type
*type
)
1312 struct bt_ctf_field_type_variant
*variant
;
1313 const char *tag_name
= NULL
;
1315 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1319 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1320 if (variant
->tag_name
->len
== 0) {
1324 tag_name
= variant
->tag_name
->str
;
1329 int bt_ctf_field_type_variant_set_tag_name(
1330 struct bt_ctf_field_type
*type
, const char *name
)
1333 struct bt_ctf_field_type_variant
*variant
;
1335 if (!type
|| type
->frozen
||
1336 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1337 bt_ctf_validate_identifier(name
)) {
1342 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1343 g_string_assign(variant
->tag_name
, name
);
1348 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1349 struct bt_ctf_field_type
*field_type
,
1350 const char *field_name
)
1354 struct bt_ctf_field_type_variant
*variant
;
1355 GQuark field_name_quark
= g_quark_from_string(field_name
);
1357 if (!type
|| !field_type
|| type
->frozen
||
1358 bt_ctf_validate_identifier(field_name
) ||
1359 (type
->declaration
->id
!= CTF_TYPE_VARIANT
) ||
1360 bt_ctf_field_type_validate(field_type
)) {
1365 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1367 /* The user has explicitly provided a tag; validate against it. */
1371 /* Make sure this name is present in the enum tag */
1372 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1373 struct enumeration_mapping
*mapping
=
1374 g_ptr_array_index(variant
->tag
->entries
, i
);
1376 if (mapping
->string
== field_name_quark
) {
1383 /* Validation failed */
1389 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1390 field_type
, field_name
)) {
1398 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1399 struct bt_ctf_field_type
*type
,
1400 const char *field_name
)
1404 struct structure_field
*field
;
1405 struct bt_ctf_field_type_variant
*variant
;
1406 struct bt_ctf_field_type
*field_type
= NULL
;
1408 if (!type
|| !field_name
) {
1412 name_quark
= g_quark_try_string(field_name
);
1417 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1418 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1419 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1423 field
= g_ptr_array_index(variant
->fields
, index
);
1424 field_type
= field
->type
;
1430 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1431 struct bt_ctf_field_type
*type
,
1432 struct bt_ctf_field
*tag
)
1434 const char *enum_value
;
1435 struct bt_ctf_field_type
*field_type
= NULL
;
1437 if (!type
|| !tag
|| type
->declaration
->id
!= CTF_TYPE_VARIANT
) {
1441 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1446 /* Already increments field_type's reference count */
1447 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1453 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1456 struct bt_ctf_field_type_variant
*variant
;
1458 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1463 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1465 ret
= (int) variant
->fields
->len
;
1471 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1472 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1475 struct bt_ctf_field_type_variant
*variant
;
1476 struct structure_field
*field
;
1479 if (!type
|| index
< 0 || (type
->declaration
->id
!= CTF_TYPE_VARIANT
)) {
1484 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1486 if (index
>= variant
->fields
->len
) {
1491 field
= g_ptr_array_index(variant
->fields
, index
);
1493 *field_type
= field
->type
;
1494 bt_get(field
->type
);
1497 *field_name
= g_quark_to_string(field
->name
);
1503 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1504 struct bt_ctf_field_type
*element_type
,
1505 unsigned int length
)
1507 struct bt_ctf_field_type_array
*array
= NULL
;
1509 if (!element_type
|| length
== 0 ||
1510 bt_ctf_field_type_validate(element_type
)) {
1514 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1519 array
->parent
.declaration
= &array
->declaration
.p
;
1520 array
->parent
.declaration
->id
= CTF_TYPE_ARRAY
;
1522 bt_get(element_type
);
1523 array
->element_type
= element_type
;
1524 array
->length
= length
;
1525 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1526 return &array
->parent
;
1531 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1532 struct bt_ctf_field_type
*type
)
1534 struct bt_ctf_field_type
*ret
= NULL
;
1535 struct bt_ctf_field_type_array
*array
;
1537 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1541 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1542 ret
= array
->element_type
;
1548 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1551 struct bt_ctf_field_type_array
*array
;
1553 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_ARRAY
)) {
1558 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1559 ret
= (int64_t) array
->length
;
1564 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1565 struct bt_ctf_field_type
*element_type
,
1566 const char *length_field_name
)
1568 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1570 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
) ||
1571 bt_ctf_field_type_validate(element_type
)) {
1575 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1580 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1581 sequence
->parent
.declaration
->id
= CTF_TYPE_SEQUENCE
;
1582 bt_get(element_type
);
1583 sequence
->element_type
= element_type
;
1584 sequence
->length_field_name
= g_string_new(length_field_name
);
1585 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1586 return &sequence
->parent
;
1591 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1592 struct bt_ctf_field_type
*type
)
1594 struct bt_ctf_field_type
*ret
= NULL
;
1595 struct bt_ctf_field_type_sequence
*sequence
;
1597 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1601 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1603 ret
= sequence
->element_type
;
1609 const char *bt_ctf_field_type_sequence_get_length_field_name(
1610 struct bt_ctf_field_type
*type
)
1612 const char *ret
= NULL
;
1613 struct bt_ctf_field_type_sequence
*sequence
;
1615 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_SEQUENCE
)) {
1619 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1621 ret
= sequence
->length_field_name
->str
;
1626 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1628 struct bt_ctf_field_type_string
*string
=
1629 g_new0(struct bt_ctf_field_type_string
, 1);
1635 string
->parent
.declaration
= &string
->declaration
.p
;
1636 string
->parent
.declaration
->id
= CTF_TYPE_STRING
;
1637 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1638 string
->declaration
.encoding
= CTF_STRING_UTF8
;
1639 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1640 return &string
->parent
;
1643 enum ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1644 struct bt_ctf_field_type
*type
)
1646 struct bt_ctf_field_type_string
*string
;
1647 enum ctf_string_encoding ret
= CTF_STRING_UNKNOWN
;
1649 if (!type
|| (type
->declaration
->id
!= CTF_TYPE_STRING
)) {
1653 string
= container_of(type
, struct bt_ctf_field_type_string
,
1655 ret
= string
->declaration
.encoding
;
1660 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1661 enum ctf_string_encoding encoding
)
1664 struct bt_ctf_field_type_string
*string
;
1666 if (!type
|| type
->declaration
->id
!= CTF_TYPE_STRING
||
1667 (encoding
!= CTF_STRING_UTF8
&&
1668 encoding
!= CTF_STRING_ASCII
)) {
1673 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1674 string
->declaration
.encoding
= encoding
;
1679 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1682 enum ctf_type_id type_id
;
1690 ret
= (int) type
->declaration
->alignment
;
1694 type_id
= bt_ctf_field_type_get_type_id(type
);
1696 case CTF_TYPE_SEQUENCE
:
1698 struct bt_ctf_field_type
*element
=
1699 bt_ctf_field_type_sequence_get_element_type(type
);
1706 ret
= bt_ctf_field_type_get_alignment(element
);
1710 case CTF_TYPE_ARRAY
:
1712 struct bt_ctf_field_type
*element
=
1713 bt_ctf_field_type_array_get_element_type(type
);
1720 ret
= bt_ctf_field_type_get_alignment(element
);
1724 case CTF_TYPE_STRUCT
:
1726 int i
, element_count
;
1728 element_count
= bt_ctf_field_type_structure_get_field_count(
1730 if (element_count
< 0) {
1731 ret
= element_count
;
1735 for (i
= 0; i
< element_count
; i
++) {
1736 struct bt_ctf_field_type
*field
;
1737 int field_alignment
;
1739 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
1746 field_alignment
= bt_ctf_field_type_get_alignment(
1749 if (field_alignment
< 0) {
1750 ret
= field_alignment
;
1754 type
->declaration
->alignment
= MAX(field_alignment
,
1755 type
->declaration
->alignment
);
1757 ret
= (int) type
->declaration
->alignment
;
1760 case CTF_TYPE_UNKNOWN
:
1764 ret
= (int) type
->declaration
->alignment
;
1772 int is_power_of_two(unsigned int value
)
1774 return ((value
& (value
- 1)) == 0) && value
> 0;
1777 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
1778 unsigned int alignment
)
1781 enum ctf_type_id type_id
;
1783 /* Alignment must be a power of two */
1784 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
1789 type_id
= bt_ctf_field_type_get_type_id(type
);
1790 if (type_id
== CTF_TYPE_UNKNOWN
) {
1795 if (type
->declaration
->id
== CTF_TYPE_STRING
&&
1796 alignment
!= CHAR_BIT
) {
1801 if (type_id
== CTF_TYPE_VARIANT
|| type_id
== CTF_TYPE_SEQUENCE
||
1802 type_id
== CTF_TYPE_ARRAY
) {
1803 /* Setting an alignment on these types makes no sense */
1808 type
->declaration
->alignment
= alignment
;
1814 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
1815 struct bt_ctf_field_type
*type
)
1817 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1818 int internal_byte_order
= -1;
1824 switch (type
->declaration
->id
) {
1825 case CTF_TYPE_INTEGER
:
1827 struct bt_ctf_field_type_integer
*integer
= container_of(
1828 type
, struct bt_ctf_field_type_integer
, parent
);
1829 internal_byte_order
= integer
->declaration
.byte_order
;
1832 case CTF_TYPE_FLOAT
:
1834 struct bt_ctf_field_type_floating_point
*floating_point
=
1836 struct bt_ctf_field_type_floating_point
,
1838 internal_byte_order
= floating_point
->declaration
.byte_order
;
1845 switch (internal_byte_order
) {
1847 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1850 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1853 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
1856 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1862 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
1863 enum bt_ctf_byte_order byte_order
)
1866 int internal_byte_order
;
1867 enum ctf_type_id type_id
;
1869 if (!type
|| type
->frozen
) {
1874 switch (byte_order
) {
1875 case BT_CTF_BYTE_ORDER_NATIVE
:
1876 /* Leave unset. Will be initialized by parent. */
1877 internal_byte_order
= 0;
1879 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
1880 internal_byte_order
= LITTLE_ENDIAN
;
1882 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
1883 case BT_CTF_BYTE_ORDER_NETWORK
:
1884 internal_byte_order
= BIG_ENDIAN
;
1891 type_id
= type
->declaration
->id
;
1892 if (set_byte_order_funcs
[type_id
]) {
1893 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
1899 enum ctf_type_id
bt_ctf_field_type_get_type_id(
1900 struct bt_ctf_field_type
*type
)
1903 return CTF_TYPE_UNKNOWN
;
1906 return type
->declaration
->id
;
1909 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
1911 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_INTEGER
;
1914 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
1916 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_FLOAT
;
1919 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
1921 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ENUM
;
1924 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
1926 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRING
;
1929 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
1931 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_STRUCT
;
1934 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
1936 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_ARRAY
;
1939 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
1941 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_SEQUENCE
;
1944 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
1946 return bt_ctf_field_type_get_type_id(type
) == CTF_TYPE_VARIANT
;
1949 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
1954 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
1960 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
1970 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
1971 struct bt_ctf_field_type_variant
*variant
,
1974 struct bt_ctf_field_type
*type
= NULL
;
1975 GQuark field_name_quark
;
1977 struct structure_field
*field_entry
;
1978 struct range_overlap_query query
= {
1979 .range_start
._signed
= tag_value
,
1980 .range_end
._signed
= tag_value
,
1981 .mapping_name
= 0, .overlaps
= 0};
1983 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
1985 if (!query
.overlaps
) {
1989 field_name_quark
= query
.mapping_name
;
1990 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1991 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
1995 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
1996 type
= field_entry
->type
;
2002 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2003 struct bt_ctf_field_type_variant
*variant
,
2006 struct bt_ctf_field_type
*type
= NULL
;
2007 GQuark field_name_quark
;
2009 struct structure_field
*field_entry
;
2010 struct range_overlap_query query
= {
2011 .range_start
._unsigned
= tag_value
,
2012 .range_end
._unsigned
= tag_value
,
2013 .mapping_name
= 0, .overlaps
= 0};
2015 g_ptr_array_foreach(variant
->tag
->entries
,
2016 check_ranges_overlap_unsigned
,
2018 if (!query
.overlaps
) {
2022 field_name_quark
= query
.mapping_name
;
2023 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2024 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2028 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2029 type
= field_entry
->type
;
2035 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2036 struct metadata_context
*context
)
2040 if (!type
|| !context
) {
2045 ret
= type
->serialize(type
, context
);
2051 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2058 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2059 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2060 set_byte_order_funcs
[type
->declaration
->id
](type
,
2066 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2068 struct bt_ctf_field_type
*copy
= NULL
;
2074 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2080 struct bt_ctf_field_path
*bt_ctf_field_path_create(void)
2082 struct bt_ctf_field_path
*field_path
= NULL
;
2084 field_path
= g_new0(struct bt_ctf_field_path
, 1);
2089 field_path
->root
= CTF_NODE_UNKNOWN
;
2090 field_path
->path_indexes
= g_array_new(TRUE
, FALSE
, sizeof(int));
2091 if (!field_path
->path_indexes
) {
2092 bt_ctf_field_path_destroy(field_path
);
2101 struct bt_ctf_field_path
*bt_ctf_field_path_copy(
2102 struct bt_ctf_field_path
*path
)
2104 struct bt_ctf_field_path
*new_path
= bt_ctf_field_path_create();
2110 new_path
->root
= path
->root
;
2111 g_array_insert_vals(new_path
->path_indexes
, 0,
2112 path
->path_indexes
->data
, path
->path_indexes
->len
);
2118 void bt_ctf_field_path_destroy(struct bt_ctf_field_path
*path
)
2124 if (path
->path_indexes
) {
2125 g_array_free(path
->path_indexes
, TRUE
);
2131 int bt_ctf_field_type_structure_get_field_name_index(
2132 struct bt_ctf_field_type
*type
, const char *name
)
2137 struct bt_ctf_field_type_structure
*structure
;
2139 if (!type
|| !name
||
2140 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2145 name_quark
= g_quark_try_string(name
);
2151 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2153 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2154 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2164 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2165 struct bt_ctf_field_type
*field
, int index
)
2168 struct bt_ctf_field_type_structure
*structure
;
2170 if (!type
|| !field
||
2171 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_STRUCT
) {
2176 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2178 if (index
< 0 || index
>= structure
->fields
->len
) {
2184 bt_put(((struct structure_field
*)
2185 g_ptr_array_index(structure
->fields
, index
))->type
);
2186 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2193 int bt_ctf_field_type_variant_get_field_name_index(
2194 struct bt_ctf_field_type
*type
, const char *name
)
2199 struct bt_ctf_field_type_variant
*variant
;
2201 if (!type
|| !name
||
2202 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2207 name_quark
= g_quark_try_string(name
);
2213 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2215 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2216 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2226 int bt_ctf_field_type_sequence_set_length_field_path(
2227 struct bt_ctf_field_type
*type
,
2228 struct bt_ctf_field_path
*path
)
2231 struct bt_ctf_field_type_sequence
*sequence
;
2233 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_SEQUENCE
) {
2238 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2240 if (sequence
->length_field_path
) {
2241 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2243 sequence
->length_field_path
= path
;
2249 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
2250 struct bt_ctf_field_type
*type
)
2252 struct bt_ctf_field_type_sequence
*sequence
;
2254 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2257 return sequence
->length_field_path
;
2261 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2262 struct bt_ctf_field_path
*path
)
2265 struct bt_ctf_field_type_variant
*variant
;
2267 if (!type
|| bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2272 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2274 if (variant
->tag_path
) {
2275 bt_ctf_field_path_destroy(variant
->tag_path
);
2277 variant
->tag_path
= path
;
2283 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
2284 struct bt_ctf_field_type
*type
)
2286 struct bt_ctf_field_type_variant
*variant
;
2288 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2291 return variant
->tag_path
;
2295 int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type
*type
,
2296 struct bt_ctf_field_type
*tag
)
2299 struct bt_ctf_field_type_variant
*variant
;
2301 if (!type
|| !tag
|| type
->frozen
||
2302 bt_ctf_field_type_get_type_id(tag
) != CTF_TYPE_ENUM
) {
2307 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2311 bt_put(&variant
->tag
->parent
);
2313 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2320 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2321 struct bt_ctf_field_type
*field
, int index
)
2324 struct bt_ctf_field_type_variant
*variant
;
2326 if (!type
|| !field
||
2327 bt_ctf_field_type_get_type_id(type
) != CTF_TYPE_VARIANT
) {
2332 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2334 if (index
< 0 || index
>= variant
->fields
->len
) {
2340 bt_put(((struct structure_field
*)
2341 g_ptr_array_index(variant
->fields
, index
))->type
);
2342 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2349 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2351 struct bt_ctf_field_type_integer
*integer
=
2352 (struct bt_ctf_field_type_integer
*) type
;
2358 bt_put(integer
->mapped_clock
);
2363 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2365 struct bt_ctf_field_type_enumeration
*enumeration
=
2366 (struct bt_ctf_field_type_enumeration
*) type
;
2372 g_ptr_array_free(enumeration
->entries
, TRUE
);
2373 bt_put(enumeration
->container
);
2374 g_free(enumeration
);
2378 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2380 struct bt_ctf_field_type_floating_point
*floating_point
=
2381 (struct bt_ctf_field_type_floating_point
*) type
;
2387 g_free(floating_point
);
2391 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2393 struct bt_ctf_field_type_structure
*structure
=
2394 (struct bt_ctf_field_type_structure
*) type
;
2400 g_ptr_array_free(structure
->fields
, TRUE
);
2401 g_hash_table_destroy(structure
->field_name_to_index
);
2406 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2408 struct bt_ctf_field_type_variant
*variant
=
2409 (struct bt_ctf_field_type_variant
*) type
;
2415 g_ptr_array_free(variant
->fields
, TRUE
);
2416 g_hash_table_destroy(variant
->field_name_to_index
);
2417 g_string_free(variant
->tag_name
, TRUE
);
2418 bt_put(&variant
->tag
->parent
);
2419 bt_ctf_field_path_destroy(variant
->tag_path
);
2424 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2426 struct bt_ctf_field_type_array
*array
=
2427 (struct bt_ctf_field_type_array
*) type
;
2433 bt_put(array
->element_type
);
2438 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2440 struct bt_ctf_field_type_sequence
*sequence
=
2441 (struct bt_ctf_field_type_sequence
*) type
;
2447 bt_put(sequence
->element_type
);
2448 g_string_free(sequence
->length_field_name
, TRUE
);
2449 bt_ctf_field_path_destroy(sequence
->length_field_path
);
2454 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2456 struct bt_ctf_field_type_string
*string
=
2457 (struct bt_ctf_field_type_string
*) type
;
2467 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2473 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2475 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2476 type
, struct bt_ctf_field_type_enumeration
, parent
);
2478 generic_field_type_freeze(type
);
2479 bt_ctf_field_type_freeze(enumeration_type
->container
);
2483 void freeze_structure_field(struct structure_field
*field
)
2485 bt_ctf_field_type_freeze(field
->type
);
2489 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2491 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2492 type
, struct bt_ctf_field_type_structure
, parent
);
2494 /* Cache the alignment */
2495 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2496 generic_field_type_freeze(type
);
2497 g_ptr_array_foreach(structure_type
->fields
,
2498 (GFunc
) freeze_structure_field
, NULL
);
2502 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2504 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2505 type
, struct bt_ctf_field_type_variant
, parent
);
2507 generic_field_type_freeze(type
);
2508 g_ptr_array_foreach(variant_type
->fields
,
2509 (GFunc
) freeze_structure_field
, NULL
);
2513 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2515 struct bt_ctf_field_type_array
*array_type
= container_of(
2516 type
, struct bt_ctf_field_type_array
, parent
);
2518 /* Cache the alignment */
2519 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2520 generic_field_type_freeze(type
);
2521 bt_ctf_field_type_freeze(array_type
->element_type
);
2525 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2527 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2528 type
, struct bt_ctf_field_type_sequence
, parent
);
2530 /* Cache the alignment */
2531 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2532 generic_field_type_freeze(type
);
2533 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2537 const char *get_encoding_string(enum ctf_string_encoding encoding
)
2539 const char *encoding_string
;
2542 case CTF_STRING_NONE
:
2543 encoding_string
= "none";
2545 case CTF_STRING_ASCII
:
2546 encoding_string
= "ASCII";
2548 case CTF_STRING_UTF8
:
2549 encoding_string
= "UTF8";
2552 encoding_string
= "unknown";
2556 return encoding_string
;
2560 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2562 const char *base_string
;
2565 case BT_CTF_INTEGER_BASE_DECIMAL
:
2566 base_string
= "decimal";
2568 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2569 base_string
= "hexadecimal";
2571 case BT_CTF_INTEGER_BASE_OCTAL
:
2572 base_string
= "octal";
2574 case BT_CTF_INTEGER_BASE_BINARY
:
2575 base_string
= "binary";
2578 base_string
= "unknown";
2586 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2587 struct metadata_context
*context
)
2589 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2590 struct bt_ctf_field_type_integer
, parent
);
2593 g_string_append_printf(context
->string
,
2594 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2595 integer
->declaration
.len
, type
->declaration
->alignment
,
2596 (integer
->declaration
.signedness
? "true" : "false"),
2597 get_encoding_string(integer
->declaration
.encoding
),
2598 get_integer_base_string(integer
->declaration
.base
),
2599 get_byte_order_string(integer
->declaration
.byte_order
));
2600 if (integer
->mapped_clock
) {
2601 const char *clock_name
= bt_ctf_clock_get_name(
2602 integer
->mapped_clock
);
2609 g_string_append_printf(context
->string
,
2610 "; map = clock.%s.value", clock_name
);
2613 g_string_append(context
->string
, "; }");
2619 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2620 struct metadata_context
*context
)
2624 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2625 struct bt_ctf_field_type_enumeration
, parent
);
2626 struct bt_ctf_field_type
*container_type
;
2627 int container_signed
;
2629 ret
= bt_ctf_field_type_validate(type
);
2634 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2635 if (!container_type
) {
2640 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2641 if (container_signed
< 0) {
2642 ret
= container_signed
;
2643 goto error_put_container_type
;
2646 g_string_append(context
->string
, "enum : ");
2647 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2649 goto error_put_container_type
;
2652 g_string_append(context
->string
, " { ");
2653 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2654 struct enumeration_mapping
*mapping
=
2655 enumeration
->entries
->pdata
[entry
];
2657 if (container_signed
) {
2658 if (mapping
->range_start
._signed
==
2659 mapping
->range_end
._signed
) {
2660 g_string_append_printf(context
->string
,
2661 "\"%s\" = %" PRId64
,
2662 g_quark_to_string(mapping
->string
),
2663 mapping
->range_start
._signed
);
2665 g_string_append_printf(context
->string
,
2666 "\"%s\" = %" PRId64
" ... %" PRId64
,
2667 g_quark_to_string(mapping
->string
),
2668 mapping
->range_start
._signed
,
2669 mapping
->range_end
._signed
);
2672 if (mapping
->range_start
._unsigned
==
2673 mapping
->range_end
._unsigned
) {
2674 g_string_append_printf(context
->string
,
2675 "\"%s\" = %" PRIu64
,
2676 g_quark_to_string(mapping
->string
),
2677 mapping
->range_start
._unsigned
);
2679 g_string_append_printf(context
->string
,
2680 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2681 g_quark_to_string(mapping
->string
),
2682 mapping
->range_start
._unsigned
,
2683 mapping
->range_end
._unsigned
);
2687 g_string_append(context
->string
,
2688 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2692 if (context
->field_name
->len
) {
2693 g_string_append_printf(context
->string
, " %s",
2694 context
->field_name
->str
);
2695 g_string_assign(context
->field_name
, "");
2697 error_put_container_type
:
2698 bt_put(container_type
);
2704 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2705 struct metadata_context
*context
)
2707 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2708 type
, struct bt_ctf_field_type_floating_point
, parent
);
2710 g_string_append_printf(context
->string
,
2711 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2712 floating_point
->declaration
.exp
->len
,
2713 floating_point
->declaration
.mantissa
->len
+ 1,
2714 get_byte_order_string(floating_point
->declaration
.byte_order
),
2715 type
->declaration
->alignment
);
2720 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2721 struct metadata_context
*context
)
2724 unsigned int indent
;
2726 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2727 struct bt_ctf_field_type_structure
, parent
);
2728 GString
*structure_field_name
= context
->field_name
;
2730 context
->field_name
= g_string_new("");
2732 context
->current_indentation_level
++;
2733 g_string_append(context
->string
, "struct {\n");
2735 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2736 struct structure_field
*field
;
2738 for (indent
= 0; indent
< context
->current_indentation_level
;
2740 g_string_append_c(context
->string
, '\t');
2743 field
= structure
->fields
->pdata
[i
];
2744 g_string_assign(context
->field_name
,
2745 g_quark_to_string(field
->name
));
2746 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2751 if (context
->field_name
->len
) {
2752 g_string_append_printf(context
->string
, " %s",
2753 context
->field_name
->str
);
2755 g_string_append(context
->string
, ";\n");
2758 context
->current_indentation_level
--;
2759 for (indent
= 0; indent
< context
->current_indentation_level
;
2761 g_string_append_c(context
->string
, '\t');
2764 g_string_append_printf(context
->string
, "} align(%zu)",
2765 type
->declaration
->alignment
);
2767 g_string_free(context
->field_name
, TRUE
);
2768 context
->field_name
= structure_field_name
;
2773 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2774 struct metadata_context
*context
)
2777 unsigned int indent
;
2779 struct bt_ctf_field_type_variant
*variant
= container_of(
2780 type
, struct bt_ctf_field_type_variant
, parent
);
2781 GString
*variant_field_name
= context
->field_name
;
2783 context
->field_name
= g_string_new("");
2784 if (variant
->tag_name
->len
> 0) {
2785 g_string_append_printf(context
->string
,
2786 "variant <%s> {\n", variant
->tag_name
->str
);
2788 g_string_append(context
->string
, "variant {\n");
2791 context
->current_indentation_level
++;
2792 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2793 struct structure_field
*field
= variant
->fields
->pdata
[i
];
2795 g_string_assign(context
->field_name
,
2796 g_quark_to_string(field
->name
));
2797 for (indent
= 0; indent
< context
->current_indentation_level
;
2799 g_string_append_c(context
->string
, '\t');
2802 g_string_assign(context
->field_name
,
2803 g_quark_to_string(field
->name
));
2804 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2809 if (context
->field_name
->len
) {
2810 g_string_append_printf(context
->string
, " %s;",
2811 context
->field_name
->str
);
2814 g_string_append_c(context
->string
, '\n');
2817 context
->current_indentation_level
--;
2818 for (indent
= 0; indent
< context
->current_indentation_level
;
2820 g_string_append_c(context
->string
, '\t');
2823 g_string_append(context
->string
, "}");
2825 g_string_free(context
->field_name
, TRUE
);
2826 context
->field_name
= variant_field_name
;
2831 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
2832 struct metadata_context
*context
)
2835 struct bt_ctf_field_type_array
*array
= container_of(type
,
2836 struct bt_ctf_field_type_array
, parent
);
2838 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
2843 if (context
->field_name
->len
) {
2844 g_string_append_printf(context
->string
, " %s[%u]",
2845 context
->field_name
->str
, array
->length
);
2846 g_string_assign(context
->field_name
, "");
2848 g_string_append_printf(context
->string
, "[%u]", array
->length
);
2855 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
2856 struct metadata_context
*context
)
2859 struct bt_ctf_field_type_sequence
*sequence
= container_of(
2860 type
, struct bt_ctf_field_type_sequence
, parent
);
2862 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
2867 if (context
->field_name
->len
) {
2868 g_string_append_printf(context
->string
, " %s[%s]",
2869 context
->field_name
->str
,
2870 sequence
->length_field_name
->str
);
2871 g_string_assign(context
->field_name
, "");
2873 g_string_append_printf(context
->string
, "[%s]",
2874 sequence
->length_field_name
->str
);
2881 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
2882 struct metadata_context
*context
)
2884 struct bt_ctf_field_type_string
*string
= container_of(
2885 type
, struct bt_ctf_field_type_string
, parent
);
2887 g_string_append_printf(context
->string
,
2888 "string { encoding = %s; }",
2889 get_encoding_string(string
->declaration
.encoding
));
2894 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
2895 int byte_order
, int set_native
)
2897 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
2898 struct bt_ctf_field_type_integer
, parent
);
2901 integer_type
->declaration
.byte_order
=
2902 integer_type
->declaration
.byte_order
== 0 ?
2903 byte_order
: integer_type
->declaration
.byte_order
;
2905 integer_type
->declaration
.byte_order
= byte_order
;
2910 void bt_ctf_field_type_enumeration_set_byte_order(
2911 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2913 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
2914 struct bt_ctf_field_type_enumeration
, parent
);
2916 /* Safe to assume that container is an integer */
2917 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
2918 byte_order
, set_native
);
2922 void bt_ctf_field_type_floating_point_set_byte_order(
2923 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
2925 struct bt_ctf_field_type_floating_point
*floating_point_type
=
2926 container_of(type
, struct bt_ctf_field_type_floating_point
,
2930 floating_point_type
->declaration
.byte_order
=
2931 floating_point_type
->declaration
.byte_order
== 0 ?
2933 floating_point_type
->declaration
.byte_order
;
2934 floating_point_type
->sign
.byte_order
=
2935 floating_point_type
->sign
.byte_order
== 0 ?
2936 byte_order
: floating_point_type
->sign
.byte_order
;
2937 floating_point_type
->mantissa
.byte_order
=
2938 floating_point_type
->mantissa
.byte_order
== 0 ?
2939 byte_order
: floating_point_type
->mantissa
.byte_order
;
2940 floating_point_type
->exp
.byte_order
=
2941 floating_point_type
->exp
.byte_order
== 0 ?
2942 byte_order
: floating_point_type
->exp
.byte_order
;
2944 floating_point_type
->declaration
.byte_order
= byte_order
;
2945 floating_point_type
->sign
.byte_order
= byte_order
;
2946 floating_point_type
->mantissa
.byte_order
= byte_order
;
2947 floating_point_type
->exp
.byte_order
= byte_order
;
2952 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
2953 int byte_order
, int set_native
)
2956 struct bt_ctf_field_type_structure
*structure_type
=
2957 container_of(type
, struct bt_ctf_field_type_structure
,
2960 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
2961 struct structure_field
*field
= g_ptr_array_index(
2962 structure_type
->fields
, i
);
2963 struct bt_ctf_field_type
*field_type
= field
->type
;
2965 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2966 set_byte_order_funcs
[field_type
->declaration
->id
](
2967 field_type
, byte_order
, set_native
);
2973 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
2974 int byte_order
, int set_native
)
2977 struct bt_ctf_field_type_variant
*variant_type
=
2978 container_of(type
, struct bt_ctf_field_type_variant
,
2981 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
2982 struct structure_field
*field
= g_ptr_array_index(
2983 variant_type
->fields
, i
);
2984 struct bt_ctf_field_type
*field_type
= field
->type
;
2986 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
2987 set_byte_order_funcs
[field_type
->declaration
->id
](
2988 field_type
, byte_order
, set_native
);
2994 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
2995 int byte_order
, int set_native
)
2997 struct bt_ctf_field_type_array
*array_type
=
2998 container_of(type
, struct bt_ctf_field_type_array
,
3001 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3002 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3003 array_type
->element_type
, byte_order
, set_native
);
3008 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3009 int byte_order
, int set_native
)
3011 struct bt_ctf_field_type_sequence
*sequence_type
=
3012 container_of(type
, struct bt_ctf_field_type_sequence
,
3015 if (set_byte_order_funcs
[
3016 sequence_type
->element_type
->declaration
->id
]) {
3017 set_byte_order_funcs
[
3018 sequence_type
->element_type
->declaration
->id
](
3019 sequence_type
->element_type
, byte_order
, set_native
);
3024 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3025 struct bt_ctf_field_type
*type
)
3027 struct bt_ctf_field_type
*copy
;
3028 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3030 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3031 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3036 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3038 copy_integer
->declaration
= integer
->declaration
;
3039 if (integer
->mapped_clock
) {
3040 bt_get(integer
->mapped_clock
);
3041 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3048 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3049 struct bt_ctf_field_type
*type
)
3052 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3053 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3055 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3058 /* Copy the source enumeration's container */
3059 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3060 if (!copy_container
) {
3064 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3068 copy_enumeration
= container_of(copy
,
3069 struct bt_ctf_field_type_enumeration
, parent
);
3071 /* Copy all enumaration entries */
3072 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3073 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3074 enumeration
->entries
, i
);
3075 struct enumeration_mapping
* copy_mapping
= g_new0(
3076 struct enumeration_mapping
, 1);
3078 if (!copy_mapping
) {
3082 *copy_mapping
= *mapping
;
3083 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3086 copy_enumeration
->declaration
= enumeration
->declaration
;
3088 bt_put(copy_container
);
3091 bt_put(copy_container
);
3097 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3098 struct bt_ctf_field_type
*type
)
3100 struct bt_ctf_field_type
*copy
;
3101 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3103 floating_point
= container_of(type
,
3104 struct bt_ctf_field_type_floating_point
, parent
);
3105 copy
= bt_ctf_field_type_floating_point_create();
3110 copy_float
= container_of(copy
,
3111 struct bt_ctf_field_type_floating_point
, parent
);
3112 copy_float
->declaration
= floating_point
->declaration
;
3113 copy_float
->sign
= floating_point
->sign
;
3114 copy_float
->mantissa
= floating_point
->mantissa
;
3115 copy_float
->exp
= floating_point
->exp
;
3121 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3122 struct bt_ctf_field_type
*type
)
3125 GHashTableIter iter
;
3126 gpointer key
, value
;
3127 struct bt_ctf_field_type
*copy
;
3128 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3130 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3132 copy
= bt_ctf_field_type_structure_create();
3137 copy_structure
= container_of(copy
,
3138 struct bt_ctf_field_type_structure
, parent
);
3140 /* Copy field_name_to_index */
3141 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3142 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3143 g_hash_table_insert(copy_structure
->field_name_to_index
,
3147 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3148 struct structure_field
*entry
, *copy_entry
;
3149 struct bt_ctf_field_type
*copy_field
;
3151 copy_entry
= g_new0(struct structure_field
, 1);
3156 entry
= g_ptr_array_index(structure
->fields
, i
);
3157 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3163 copy_entry
->name
= entry
->name
;
3164 copy_entry
->type
= copy_field
;
3165 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3168 copy_structure
->declaration
= structure
->declaration
;
3177 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3178 struct bt_ctf_field_type
*type
)
3181 GHashTableIter iter
;
3182 gpointer key
, value
;
3183 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3184 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3186 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3189 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3195 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3196 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3201 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3204 /* Copy field_name_to_index */
3205 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3206 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3207 g_hash_table_insert(copy_variant
->field_name_to_index
,
3211 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3212 struct structure_field
*entry
, *copy_entry
;
3213 struct bt_ctf_field_type
*copy_field
;
3215 copy_entry
= g_new0(struct structure_field
, 1);
3220 entry
= g_ptr_array_index(variant
->fields
, i
);
3221 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3227 copy_entry
->name
= entry
->name
;
3228 copy_entry
->type
= copy_field
;
3229 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3232 copy_variant
->declaration
= variant
->declaration
;
3233 if (variant
->tag_path
) {
3234 copy_variant
->tag_path
= bt_ctf_field_path_copy(
3236 if (!copy_variant
->tag_path
) {
3250 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3251 struct bt_ctf_field_type
*type
)
3253 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3254 struct bt_ctf_field_type_array
*array
, *copy_array
;
3256 array
= container_of(type
, struct bt_ctf_field_type_array
,
3258 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3259 if (!copy_element
) {
3263 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3268 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3270 copy_array
->declaration
= array
->declaration
;
3272 bt_put(copy_element
);
3277 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3278 struct bt_ctf_field_type
*type
)
3280 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3281 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3283 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3285 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3286 if (!copy_element
) {
3290 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3291 sequence
->length_field_name
->len
?
3292 sequence
->length_field_name
->str
: NULL
);
3297 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3299 copy_sequence
->declaration
= sequence
->declaration
;
3300 if (sequence
->length_field_path
) {
3301 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3302 sequence
->length_field_path
);
3303 if (!copy_sequence
->length_field_path
) {
3308 bt_put(copy_element
);
3316 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3317 struct bt_ctf_field_type
*type
)
3319 struct bt_ctf_field_type
*copy
;
3320 struct bt_ctf_field_type_string
*string
, *copy_string
;
3322 copy
= bt_ctf_field_type_string_create();
3327 string
= container_of(type
, struct bt_ctf_field_type_string
,
3329 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3331 copy_string
->declaration
= string
->declaration
;