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/field-path-internal.h>
32 #include <babeltrace/ctf-ir/utils.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/endian.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 [BT_CTF_TYPE_ID_ENUM
] =
81 bt_ctf_field_type_enumeration_destroy
,
82 [BT_CTF_TYPE_ID_FLOAT
] =
83 bt_ctf_field_type_floating_point_destroy
,
84 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
105 type_freeze_func
const type_freeze_funcs
[] = {
106 [BT_CTF_TYPE_ID_INTEGER
] = generic_field_type_freeze
,
107 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
108 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
109 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
110 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
111 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
112 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
113 [BT_CTF_TYPE_ID_STRING
] = generic_field_type_freeze
,
117 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
118 struct metadata_context
*);
120 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
121 struct metadata_context
*);
123 int bt_ctf_field_type_floating_point_serialize(
124 struct bt_ctf_field_type
*, struct metadata_context
*);
126 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
127 struct metadata_context
*);
129 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
130 struct metadata_context
*);
132 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
133 struct metadata_context
*);
135 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
136 struct metadata_context
*);
138 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
139 struct metadata_context
*);
142 type_serialize_func
const type_serialize_funcs
[] = {
143 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
144 [BT_CTF_TYPE_ID_ENUM
] =
145 bt_ctf_field_type_enumeration_serialize
,
146 [BT_CTF_TYPE_ID_FLOAT
] =
147 bt_ctf_field_type_floating_point_serialize
,
148 [BT_CTF_TYPE_ID_STRUCT
] =
149 bt_ctf_field_type_structure_serialize
,
150 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
151 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
152 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
153 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
157 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
158 int byte_order
, int set_native
);
160 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
161 int byte_order
, int set_native
);
163 void bt_ctf_field_type_floating_point_set_byte_order(
164 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
166 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
167 int byte_order
, int set_native
);
169 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
170 int byte_order
, int set_native
);
172 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
173 int byte_order
, int set_native
);
175 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
176 int byte_order
, int set_native
);
178 /* The set_native flag only set the byte order if it is set to native */
180 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
181 int byte_order
, int set_native
) = {
182 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
183 [BT_CTF_TYPE_ID_ENUM
] =
184 bt_ctf_field_type_enumeration_set_byte_order
,
185 [BT_CTF_TYPE_ID_FLOAT
] =
186 bt_ctf_field_type_floating_point_set_byte_order
,
187 [BT_CTF_TYPE_ID_STRUCT
] =
188 bt_ctf_field_type_structure_set_byte_order
,
189 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
190 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
191 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
192 [BT_CTF_TYPE_ID_STRING
] = NULL
,
196 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
197 struct bt_ctf_field_type
*);
199 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
200 struct bt_ctf_field_type
*);
202 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
203 struct bt_ctf_field_type
*);
205 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
206 struct bt_ctf_field_type
*);
208 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
209 struct bt_ctf_field_type
*);
211 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
212 struct bt_ctf_field_type
*);
214 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
215 struct bt_ctf_field_type
*);
217 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
218 struct bt_ctf_field_type
*);
221 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
222 struct bt_ctf_field_type
*) = {
223 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
224 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
225 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
226 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
227 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
228 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
229 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
230 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
234 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
235 struct bt_ctf_field_type
*);
237 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
238 struct bt_ctf_field_type
*);
240 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
241 struct bt_ctf_field_type
*);
243 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
244 struct bt_ctf_field_type
*);
246 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
247 struct bt_ctf_field_type
*);
249 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
250 struct bt_ctf_field_type
*);
252 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
253 struct bt_ctf_field_type
*);
255 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
256 struct bt_ctf_field_type
*);
259 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
260 struct bt_ctf_field_type
*) = {
261 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
262 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
263 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
264 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
265 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
266 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
267 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
268 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
272 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
274 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
283 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
284 [BT_CTF_TYPE_ID_INTEGER
] = NULL
,
285 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
286 [BT_CTF_TYPE_ID_STRING
] = NULL
,
287 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
288 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
289 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
290 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
291 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
295 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
301 void destroy_structure_field(struct structure_field
*field
)
308 void check_ranges_overlap(gpointer element
, gpointer query
)
310 struct enumeration_mapping
*mapping
= element
;
311 struct range_overlap_query
*overlap_query
= query
;
313 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
314 && overlap_query
->range_start
._signed
<=
315 mapping
->range_end
._signed
) {
316 overlap_query
->overlaps
= 1;
317 overlap_query
->mapping_name
= mapping
->string
;
320 overlap_query
->overlaps
|=
321 mapping
->string
== overlap_query
->mapping_name
;
325 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
327 struct enumeration_mapping
*mapping
= element
;
328 struct range_overlap_query
*overlap_query
= query
;
330 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
331 && overlap_query
->range_start
._unsigned
<=
332 mapping
->range_end
._unsigned
) {
333 overlap_query
->overlaps
= 1;
334 overlap_query
->mapping_name
= mapping
->string
;
337 overlap_query
->overlaps
|=
338 mapping
->string
== overlap_query
->mapping_name
;
342 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
343 struct enumeration_mapping
**b
)
345 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
349 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
350 struct enumeration_mapping
**b
)
352 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
356 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
358 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
360 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
361 (type_id
< BT_CTF_NR_TYPE_IDS
));
363 bt_object_init(type
, bt_ctf_field_type_destroy
);
364 type
->freeze
= type_freeze_funcs
[type_id
];
365 type
->serialize
= type_serialize_funcs
[type_id
];
368 int ret
= bt_ctf_field_type_set_byte_order(type
,
369 BT_CTF_BYTE_ORDER_NATIVE
);
373 type
->declaration
->alignment
= 1;
377 int add_structure_field(GPtrArray
*fields
,
378 GHashTable
*field_name_to_index
,
379 struct bt_ctf_field_type
*field_type
,
380 const char *field_name
)
383 GQuark name_quark
= g_quark_from_string(field_name
);
384 struct structure_field
*field
;
386 /* Make sure structure does not contain a field of the same name */
387 if (g_hash_table_lookup_extended(field_name_to_index
,
388 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
393 field
= g_new0(struct structure_field
, 1);
400 field
->name
= name_quark
;
401 field
->type
= field_type
;
402 g_hash_table_insert(field_name_to_index
,
403 (gpointer
) (unsigned long) name_quark
,
404 (gpointer
) (unsigned long) fields
->len
);
405 g_ptr_array_add(fields
, field
);
411 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
413 struct bt_ctf_field_type
*type
;
414 enum bt_ctf_type_id type_id
;
416 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
417 type_id
= type
->declaration
->id
;
418 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
419 type_id
>= BT_CTF_NR_TYPE_IDS
) {
423 type_destroy_funcs
[type_id
](type
);
427 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
431 struct bt_ctf_field_type_enumeration
*enumeration
=
432 container_of(type
, struct bt_ctf_field_type_enumeration
,
434 struct bt_ctf_field_type
*container_type
=
435 bt_ctf_field_type_enumeration_get_container_type(type
);
437 if (!container_type
) {
442 ret
= bt_ctf_field_type_validate(container_type
);
447 /* Ensure enum has entries */
448 ret
= enumeration
->entries
->len
? 0 : -1;
451 BT_PUT(container_type
);
456 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
459 struct bt_ctf_field_type
*element_type
= NULL
;
460 struct bt_ctf_field_type_sequence
*sequence
=
461 container_of(type
, struct bt_ctf_field_type_sequence
,
464 /* Length field name should be set at this point */
465 if (sequence
->length_field_name
->len
== 0) {
470 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
476 ret
= bt_ctf_field_type_validate(element_type
);
479 BT_PUT(element_type
);
485 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
488 struct bt_ctf_field_type
*element_type
= NULL
;
490 element_type
= bt_ctf_field_type_array_get_element_type(type
);
496 ret
= bt_ctf_field_type_validate(element_type
);
499 BT_PUT(element_type
);
505 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
508 struct bt_ctf_field_type
*child_type
= NULL
;
509 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
512 if (field_count
< 0) {
517 for (i
= 0; i
< field_count
; ++i
) {
518 ret
= bt_ctf_field_type_structure_get_field(type
,
519 NULL
, &child_type
, i
);
524 ret
= bt_ctf_field_type_validate(child_type
);
539 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
543 struct bt_ctf_field_type
*child_type
= NULL
;
544 struct bt_ctf_field_type_variant
*variant
=
545 container_of(type
, struct bt_ctf_field_type_variant
,
548 int tag_mappings_count
;
550 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
556 bt_ctf_field_type_enumeration_get_mapping_count(
557 (struct bt_ctf_field_type
*) variant
->tag
);
559 if (tag_mappings_count
!= variant
->fields
->len
) {
564 for (i
= 0; i
< tag_mappings_count
; ++i
) {
566 int64_t range_start
, range_end
;
567 struct bt_ctf_field_type
*ft
;
569 ret
= bt_ctf_field_type_enumeration_get_mapping(
570 (struct bt_ctf_field_type
*) variant
->tag
,
571 i
, &label
, &range_start
, &range_end
);
580 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
590 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
591 if (field_count
< 0) {
596 for (i
= 0; i
< field_count
; ++i
) {
597 ret
= bt_ctf_field_type_variant_get_field(type
,
598 NULL
, &child_type
, i
);
603 ret
= bt_ctf_field_type_validate(child_type
);
618 * This function validates a given field type without considering
619 * where this field type is located. It only validates the properties
620 * of the given field type and the properties of its children if
624 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
627 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
635 /* Already marked as valid */
639 if (type_validate_funcs
[id
]) {
640 ret
= type_validate_funcs
[id
](type
);
643 if (!ret
&& type
->frozen
) {
644 /* Field type is valid */
652 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
654 struct bt_ctf_field_type_integer
*integer
=
655 g_new0(struct bt_ctf_field_type_integer
, 1);
657 if (!integer
|| size
== 0 || size
> 64) {
661 integer
->parent
.declaration
= &integer
->declaration
.p
;
662 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
663 integer
->declaration
.len
= size
;
664 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
665 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
666 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
667 return &integer
->parent
;
670 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
673 struct bt_ctf_field_type_integer
*integer
;
675 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
680 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
681 ret
= (int) integer
->declaration
.len
;
686 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
689 struct bt_ctf_field_type_integer
*integer
;
691 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
696 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
697 ret
= integer
->declaration
.signedness
;
702 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
706 struct bt_ctf_field_type_integer
*integer
;
708 if (!type
|| type
->frozen
||
709 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
714 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
715 integer
->declaration
.signedness
= !!is_signed
;
720 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
721 struct bt_ctf_field_type
*type
)
723 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
724 struct bt_ctf_field_type_integer
*integer
;
726 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
730 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
731 ret
= integer
->declaration
.base
;
736 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
737 enum bt_ctf_integer_base base
)
741 if (!type
|| type
->frozen
||
742 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
748 case BT_CTF_INTEGER_BASE_BINARY
:
749 case BT_CTF_INTEGER_BASE_OCTAL
:
750 case BT_CTF_INTEGER_BASE_DECIMAL
:
751 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
753 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
754 struct bt_ctf_field_type_integer
, parent
);
755 integer
->declaration
.base
= base
;
765 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
766 struct bt_ctf_field_type
*type
)
768 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
769 struct bt_ctf_field_type_integer
*integer
;
771 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
775 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
776 ret
= integer
->declaration
.encoding
;
781 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
782 enum bt_ctf_string_encoding encoding
)
785 struct bt_ctf_field_type_integer
*integer
;
787 if (!type
|| type
->frozen
||
788 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
789 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
790 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
795 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
796 integer
->declaration
.encoding
= encoding
;
801 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
802 struct bt_ctf_field_type
*type
)
804 struct bt_ctf_field_type_integer
*integer
;
805 struct bt_ctf_clock
*clock
= NULL
;
811 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
812 clock
= integer
->mapped_clock
;
818 int bt_ctf_field_type_integer_set_mapped_clock(
819 struct bt_ctf_field_type
*type
,
820 struct bt_ctf_clock
*clock
)
822 struct bt_ctf_field_type_integer
*integer
;
825 if (!type
|| type
->frozen
) {
830 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
831 bt_put(integer
->mapped_clock
);
833 integer
->mapped_clock
= clock
;
838 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
839 struct bt_ctf_field_type
*integer_container_type
)
841 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
843 if (!integer_container_type
) {
847 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
851 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
856 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
857 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
858 bt_get(integer_container_type
);
859 enumeration
->container
= integer_container_type
;
860 enumeration
->entries
= g_ptr_array_new_with_free_func(
861 (GDestroyNotify
)destroy_enumeration_mapping
);
862 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
863 return &enumeration
->parent
;
869 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
870 struct bt_ctf_field_type
*type
)
872 struct bt_ctf_field_type
*container_type
= NULL
;
873 struct bt_ctf_field_type_enumeration
*enumeration_type
;
879 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
883 enumeration_type
= container_of(type
,
884 struct bt_ctf_field_type_enumeration
, parent
);
885 container_type
= enumeration_type
->container
;
886 bt_get(container_type
);
888 return container_type
;
891 int bt_ctf_field_type_enumeration_add_mapping(
892 struct bt_ctf_field_type
*type
, const char *string
,
893 int64_t range_start
, int64_t range_end
)
897 struct enumeration_mapping
*mapping
;
898 struct bt_ctf_field_type_enumeration
*enumeration
;
899 struct range_overlap_query query
;
900 char *escaped_string
;
902 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
904 (range_end
< range_start
)) {
909 if (!string
|| strlen(string
) == 0) {
914 escaped_string
= g_strescape(string
, NULL
);
915 if (!escaped_string
) {
920 mapping_name
= g_quark_from_string(escaped_string
);
921 query
= (struct range_overlap_query
) {
922 .range_start
._signed
= range_start
,
923 .range_end
._signed
= range_end
,
924 .mapping_name
= mapping_name
,
926 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
929 /* Check that the range does not overlap with one already present */
930 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
931 if (query
.overlaps
) {
936 mapping
= g_new(struct enumeration_mapping
, 1);
942 *mapping
= (struct enumeration_mapping
) {
943 .range_start
._signed
= range_start
,
944 .range_end
._signed
= range_end
, .string
= mapping_name
};
945 g_ptr_array_add(enumeration
->entries
, mapping
);
946 g_ptr_array_sort(enumeration
->entries
,
947 (GCompareFunc
)compare_enumeration_mappings_signed
);
949 free(escaped_string
);
954 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
955 struct bt_ctf_field_type
*type
, const char *string
,
956 uint64_t range_start
, uint64_t range_end
)
960 struct enumeration_mapping
*mapping
;
961 struct bt_ctf_field_type_enumeration
*enumeration
;
962 struct range_overlap_query query
;
963 char *escaped_string
;
965 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
967 (range_end
< range_start
)) {
972 if (!string
|| strlen(string
) == 0) {
977 escaped_string
= g_strescape(string
, NULL
);
978 if (!escaped_string
) {
983 mapping_name
= g_quark_from_string(escaped_string
);
984 query
= (struct range_overlap_query
) {
985 .range_start
._unsigned
= range_start
,
986 .range_end
._unsigned
= range_end
,
987 .mapping_name
= mapping_name
,
989 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
992 /* Check that the range does not overlap with one already present */
993 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
995 if (query
.overlaps
) {
1000 mapping
= g_new(struct enumeration_mapping
, 1);
1006 *mapping
= (struct enumeration_mapping
) {
1007 .range_start
._unsigned
= range_start
,
1008 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1009 g_ptr_array_add(enumeration
->entries
, mapping
);
1010 g_ptr_array_sort(enumeration
->entries
,
1011 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1013 free(escaped_string
);
1018 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1019 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1022 const char *name
= NULL
;
1023 struct range_overlap_query query
=
1024 (struct range_overlap_query
) {
1025 .range_start
._unsigned
= value
,
1026 .range_end
._unsigned
= value
,
1029 g_ptr_array_foreach(enumeration_type
->entries
,
1030 check_ranges_overlap_unsigned
,
1032 if (!query
.overlaps
) {
1036 name
= g_quark_to_string(query
.mapping_name
);
1041 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1042 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1045 const char *name
= NULL
;
1046 struct range_overlap_query query
=
1047 (struct range_overlap_query
) {
1048 .range_start
._signed
= value
,
1049 .range_end
._signed
= value
,
1052 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1054 if (!query
.overlaps
) {
1058 name
= g_quark_to_string(query
.mapping_name
);
1063 int bt_ctf_field_type_enumeration_get_mapping_count(
1064 struct bt_ctf_field_type
*type
)
1067 struct bt_ctf_field_type_enumeration
*enumeration
;
1069 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1074 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1076 ret
= (int) enumeration
->entries
->len
;
1082 struct enumeration_mapping
*get_enumeration_mapping(
1083 struct bt_ctf_field_type
*type
, int index
)
1085 struct enumeration_mapping
*mapping
= NULL
;
1086 struct bt_ctf_field_type_enumeration
*enumeration
;
1088 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1090 if (index
>= enumeration
->entries
->len
) {
1094 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1099 int bt_ctf_field_type_enumeration_get_mapping(
1100 struct bt_ctf_field_type
*type
, int index
,
1101 const char **string
, int64_t *range_start
, int64_t *range_end
)
1103 struct enumeration_mapping
*mapping
;
1106 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1107 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1112 mapping
= get_enumeration_mapping(type
, index
);
1118 *string
= g_quark_to_string(mapping
->string
);
1119 *range_start
= mapping
->range_start
._signed
;
1120 *range_end
= mapping
->range_end
._signed
;
1125 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1126 struct bt_ctf_field_type
*type
, int index
,
1127 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1129 struct enumeration_mapping
*mapping
;
1132 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1133 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1138 mapping
= get_enumeration_mapping(type
, index
);
1144 *string
= g_quark_to_string(mapping
->string
);
1145 *range_start
= mapping
->range_start
._unsigned
;
1146 *range_end
= mapping
->range_end
._unsigned
;
1151 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1152 struct bt_ctf_field_type
*type
, const char *name
)
1155 struct bt_ctf_field_type_enumeration
*enumeration
;
1158 if (!type
|| !name
||
1159 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1164 name_quark
= g_quark_try_string(name
);
1170 enumeration
= container_of(type
,
1171 struct bt_ctf_field_type_enumeration
, parent
);
1172 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1173 struct enumeration_mapping
*mapping
=
1174 get_enumeration_mapping(type
, i
);
1176 if (mapping
->string
== name_quark
) {
1187 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1188 struct bt_ctf_field_type
*type
, int64_t value
)
1190 struct bt_ctf_field_type_enumeration
*enumeration
;
1193 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1198 enumeration
= container_of(type
,
1199 struct bt_ctf_field_type_enumeration
, parent
);
1200 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1201 struct enumeration_mapping
*mapping
=
1202 get_enumeration_mapping(type
, i
);
1204 if (value
>= mapping
->range_start
._signed
&&
1205 value
<= mapping
->range_end
._signed
) {
1216 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1217 struct bt_ctf_field_type
*type
, uint64_t value
)
1219 struct bt_ctf_field_type_enumeration
*enumeration
;
1222 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1227 enumeration
= container_of(type
,
1228 struct bt_ctf_field_type_enumeration
, parent
);
1229 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1230 struct enumeration_mapping
*mapping
=
1231 get_enumeration_mapping(type
, i
);
1233 if (value
>= mapping
->range_start
._unsigned
&&
1234 value
<= mapping
->range_end
._unsigned
) {
1245 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1247 struct bt_ctf_field_type_floating_point
*floating_point
=
1248 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1250 if (!floating_point
) {
1254 floating_point
->declaration
.sign
= &floating_point
->sign
;
1255 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1256 floating_point
->declaration
.exp
= &floating_point
->exp
;
1257 floating_point
->sign
.len
= 1;
1258 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1259 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1260 floating_point
->declaration
.exp
->len
=
1261 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1262 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1263 floating_point
->sign
.p
.alignment
= 1;
1264 floating_point
->mantissa
.p
.alignment
= 1;
1265 floating_point
->exp
.p
.alignment
= 1;
1267 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1269 return floating_point
? &floating_point
->parent
: NULL
;
1272 int bt_ctf_field_type_floating_point_get_exponent_digits(
1273 struct bt_ctf_field_type
*type
)
1276 struct bt_ctf_field_type_floating_point
*floating_point
;
1278 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1283 floating_point
= container_of(type
,
1284 struct bt_ctf_field_type_floating_point
, parent
);
1285 ret
= (int) floating_point
->declaration
.exp
->len
;
1290 int bt_ctf_field_type_floating_point_set_exponent_digits(
1291 struct bt_ctf_field_type
*type
,
1292 unsigned int exponent_digits
)
1295 struct bt_ctf_field_type_floating_point
*floating_point
;
1297 if (!type
|| type
->frozen
||
1298 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1303 floating_point
= container_of(type
,
1304 struct bt_ctf_field_type_floating_point
, parent
);
1305 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1306 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1308 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1313 floating_point
->declaration
.exp
->len
= exponent_digits
;
1318 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1319 struct bt_ctf_field_type
*type
)
1322 struct bt_ctf_field_type_floating_point
*floating_point
;
1324 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1329 floating_point
= container_of(type
,
1330 struct bt_ctf_field_type_floating_point
, parent
);
1331 ret
= (int) floating_point
->mantissa
.len
+ 1;
1336 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1337 struct bt_ctf_field_type
*type
,
1338 unsigned int mantissa_digits
)
1341 struct bt_ctf_field_type_floating_point
*floating_point
;
1343 if (!type
|| type
->frozen
||
1344 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1349 floating_point
= container_of(type
,
1350 struct bt_ctf_field_type_floating_point
, parent
);
1352 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1353 (mantissa_digits
!= DBL_MANT_DIG
) &&
1354 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1359 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1364 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1366 struct bt_ctf_field_type_structure
*structure
=
1367 g_new0(struct bt_ctf_field_type_structure
, 1);
1373 structure
->parent
.declaration
= &structure
->declaration
.p
;
1374 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1375 structure
->fields
= g_ptr_array_new_with_free_func(
1376 (GDestroyNotify
)destroy_structure_field
);
1377 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1378 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1379 return &structure
->parent
;
1384 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1385 struct bt_ctf_field_type
*field_type
,
1386 const char *field_name
)
1389 struct bt_ctf_field_type_structure
*structure
;
1391 if (!type
|| !field_type
|| type
->frozen
||
1392 bt_ctf_validate_identifier(field_name
) ||
1393 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1398 structure
= container_of(type
,
1399 struct bt_ctf_field_type_structure
, parent
);
1400 if (add_structure_field(structure
->fields
,
1401 structure
->field_name_to_index
, field_type
, field_name
)) {
1409 int bt_ctf_field_type_structure_get_field_count(
1410 struct bt_ctf_field_type
*type
)
1413 struct bt_ctf_field_type_structure
*structure
;
1415 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1420 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1422 ret
= (int) structure
->fields
->len
;
1427 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1428 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1431 struct bt_ctf_field_type_structure
*structure
;
1432 struct structure_field
*field
;
1435 if (!type
|| index
< 0 ||
1436 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1441 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1443 if (index
>= structure
->fields
->len
) {
1448 field
= g_ptr_array_index(structure
->fields
, index
);
1450 *field_type
= field
->type
;
1451 bt_get(field
->type
);
1454 *field_name
= g_quark_to_string(field
->name
);
1460 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1461 struct bt_ctf_field_type
*type
,
1466 struct structure_field
*field
;
1467 struct bt_ctf_field_type_structure
*structure
;
1468 struct bt_ctf_field_type
*field_type
= NULL
;
1470 if (!type
|| !name
) {
1474 name_quark
= g_quark_try_string(name
);
1479 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1481 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1482 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1486 field
= structure
->fields
->pdata
[index
];
1487 field_type
= field
->type
;
1493 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1494 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1496 struct bt_ctf_field_type_variant
*variant
= NULL
;
1498 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1502 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1507 variant
->parent
.declaration
= &variant
->declaration
.p
;
1508 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1509 variant
->tag_name
= g_string_new(tag_name
);
1510 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1511 variant
->fields
= g_ptr_array_new_with_free_func(
1512 (GDestroyNotify
) destroy_structure_field
);
1515 variant
->tag
= container_of(enum_tag
,
1516 struct bt_ctf_field_type_enumeration
, parent
);
1519 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1520 /* A variant's alignment is undefined */
1521 variant
->parent
.declaration
->alignment
= 0;
1522 return &variant
->parent
;
1527 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1528 struct bt_ctf_field_type
*type
)
1530 struct bt_ctf_field_type_variant
*variant
;
1531 struct bt_ctf_field_type
*tag_type
= NULL
;
1533 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1537 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1538 if (!variant
->tag
) {
1542 tag_type
= &variant
->tag
->parent
;
1548 const char *bt_ctf_field_type_variant_get_tag_name(
1549 struct bt_ctf_field_type
*type
)
1551 struct bt_ctf_field_type_variant
*variant
;
1552 const char *tag_name
= NULL
;
1554 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1558 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1559 if (variant
->tag_name
->len
== 0) {
1563 tag_name
= variant
->tag_name
->str
;
1568 int bt_ctf_field_type_variant_set_tag_name(
1569 struct bt_ctf_field_type
*type
, const char *name
)
1572 struct bt_ctf_field_type_variant
*variant
;
1574 if (!type
|| type
->frozen
||
1575 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1576 bt_ctf_validate_identifier(name
)) {
1581 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1582 g_string_assign(variant
->tag_name
, name
);
1587 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1588 struct bt_ctf_field_type
*field_type
,
1589 const char *field_name
)
1593 struct bt_ctf_field_type_variant
*variant
;
1594 GQuark field_name_quark
= g_quark_from_string(field_name
);
1596 if (!type
|| !field_type
|| type
->frozen
||
1597 bt_ctf_validate_identifier(field_name
) ||
1598 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1603 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1605 /* The user has explicitly provided a tag; validate against it. */
1609 /* Make sure this name is present in the enum tag */
1610 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1611 struct enumeration_mapping
*mapping
=
1612 g_ptr_array_index(variant
->tag
->entries
, i
);
1614 if (mapping
->string
== field_name_quark
) {
1621 /* Validation failed */
1627 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1628 field_type
, field_name
)) {
1636 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1637 struct bt_ctf_field_type
*type
,
1638 const char *field_name
)
1642 struct structure_field
*field
;
1643 struct bt_ctf_field_type_variant
*variant
;
1644 struct bt_ctf_field_type
*field_type
= NULL
;
1646 if (!type
|| !field_name
) {
1650 name_quark
= g_quark_try_string(field_name
);
1655 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1656 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1657 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1661 field
= g_ptr_array_index(variant
->fields
, index
);
1662 field_type
= field
->type
;
1668 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1669 struct bt_ctf_field_type
*type
,
1670 struct bt_ctf_field
*tag
)
1672 const char *enum_value
;
1673 struct bt_ctf_field_type
*field_type
= NULL
;
1675 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1679 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1684 /* Already increments field_type's reference count */
1685 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1691 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1694 struct bt_ctf_field_type_variant
*variant
;
1696 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1701 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1703 ret
= (int) variant
->fields
->len
;
1709 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1710 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1713 struct bt_ctf_field_type_variant
*variant
;
1714 struct structure_field
*field
;
1717 if (!type
|| index
< 0 ||
1718 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1723 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1725 if (index
>= variant
->fields
->len
) {
1730 field
= g_ptr_array_index(variant
->fields
, index
);
1732 *field_type
= field
->type
;
1733 bt_get(field
->type
);
1736 *field_name
= g_quark_to_string(field
->name
);
1742 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1743 struct bt_ctf_field_type
*element_type
,
1744 unsigned int length
)
1746 struct bt_ctf_field_type_array
*array
= NULL
;
1748 if (!element_type
|| length
== 0) {
1752 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1757 array
->parent
.declaration
= &array
->declaration
.p
;
1758 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1760 bt_get(element_type
);
1761 array
->element_type
= element_type
;
1762 array
->length
= length
;
1763 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1764 return &array
->parent
;
1769 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1770 struct bt_ctf_field_type
*type
)
1772 struct bt_ctf_field_type
*ret
= NULL
;
1773 struct bt_ctf_field_type_array
*array
;
1775 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1779 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1780 ret
= array
->element_type
;
1787 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1788 struct bt_ctf_field_type
*element_type
)
1791 struct bt_ctf_field_type_array
*array
;
1793 if (!type
|| !element_type
||
1794 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1799 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1801 if (array
->element_type
) {
1802 BT_PUT(array
->element_type
);
1805 array
->element_type
= element_type
;
1806 bt_get(array
->element_type
);
1812 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1815 struct bt_ctf_field_type_array
*array
;
1817 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1822 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1823 ret
= (int64_t) array
->length
;
1828 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1829 struct bt_ctf_field_type
*element_type
,
1830 const char *length_field_name
)
1832 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1834 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1838 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1843 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1844 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1845 bt_get(element_type
);
1846 sequence
->element_type
= element_type
;
1847 sequence
->length_field_name
= g_string_new(length_field_name
);
1848 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1849 return &sequence
->parent
;
1854 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1855 struct bt_ctf_field_type
*type
)
1857 struct bt_ctf_field_type
*ret
= NULL
;
1858 struct bt_ctf_field_type_sequence
*sequence
;
1860 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1864 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1866 ret
= sequence
->element_type
;
1873 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1874 struct bt_ctf_field_type
*element_type
)
1877 struct bt_ctf_field_type_sequence
*sequence
;
1879 if (!type
|| !element_type
||
1880 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1885 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1887 if (sequence
->element_type
) {
1888 BT_PUT(sequence
->element_type
);
1891 sequence
->element_type
= element_type
;
1892 bt_get(sequence
->element_type
);
1898 const char *bt_ctf_field_type_sequence_get_length_field_name(
1899 struct bt_ctf_field_type
*type
)
1901 const char *ret
= NULL
;
1902 struct bt_ctf_field_type_sequence
*sequence
;
1904 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1908 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1910 ret
= sequence
->length_field_name
->str
;
1915 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1917 struct bt_ctf_field_type_string
*string
=
1918 g_new0(struct bt_ctf_field_type_string
, 1);
1924 string
->parent
.declaration
= &string
->declaration
.p
;
1925 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
1926 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1927 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1928 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1929 return &string
->parent
;
1932 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1933 struct bt_ctf_field_type
*type
)
1935 struct bt_ctf_field_type_string
*string
;
1936 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1938 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
1942 string
= container_of(type
, struct bt_ctf_field_type_string
,
1944 ret
= string
->declaration
.encoding
;
1949 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1950 enum bt_ctf_string_encoding encoding
)
1953 struct bt_ctf_field_type_string
*string
;
1955 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
1956 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
1957 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
1962 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1963 string
->declaration
.encoding
= encoding
;
1968 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1971 enum bt_ctf_type_id type_id
;
1979 ret
= (int) type
->declaration
->alignment
;
1983 type_id
= bt_ctf_field_type_get_type_id(type
);
1985 case BT_CTF_TYPE_ID_SEQUENCE
:
1987 struct bt_ctf_field_type
*element
=
1988 bt_ctf_field_type_sequence_get_element_type(type
);
1995 ret
= bt_ctf_field_type_get_alignment(element
);
1999 case BT_CTF_TYPE_ID_ARRAY
:
2001 struct bt_ctf_field_type
*element
=
2002 bt_ctf_field_type_array_get_element_type(type
);
2009 ret
= bt_ctf_field_type_get_alignment(element
);
2013 case BT_CTF_TYPE_ID_STRUCT
:
2015 int i
, element_count
;
2017 element_count
= bt_ctf_field_type_structure_get_field_count(
2019 if (element_count
< 0) {
2020 ret
= element_count
;
2024 for (i
= 0; i
< element_count
; i
++) {
2025 struct bt_ctf_field_type
*field
;
2026 int field_alignment
;
2028 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2035 field_alignment
= bt_ctf_field_type_get_alignment(
2038 if (field_alignment
< 0) {
2039 ret
= field_alignment
;
2043 type
->declaration
->alignment
= MAX(field_alignment
,
2044 type
->declaration
->alignment
);
2046 ret
= (int) type
->declaration
->alignment
;
2049 case BT_CTF_TYPE_ID_UNKNOWN
:
2053 ret
= (int) type
->declaration
->alignment
;
2061 int is_power_of_two(unsigned int value
)
2063 return ((value
& (value
- 1)) == 0) && value
> 0;
2066 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2067 unsigned int alignment
)
2070 enum bt_ctf_type_id type_id
;
2072 /* Alignment must be a power of two */
2073 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2078 type_id
= bt_ctf_field_type_get_type_id(type
);
2079 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2084 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2085 alignment
!= CHAR_BIT
) {
2090 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2091 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2092 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2093 /* Setting an alignment on these types makes no sense */
2098 type
->declaration
->alignment
= alignment
;
2104 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2105 struct bt_ctf_field_type
*type
)
2107 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2113 switch (type
->declaration
->id
) {
2114 case BT_CTF_TYPE_ID_INTEGER
:
2116 struct bt_ctf_field_type_integer
*integer
= container_of(
2117 type
, struct bt_ctf_field_type_integer
, parent
);
2118 ret
= integer
->user_byte_order
;
2121 case BT_CTF_TYPE_ID_FLOAT
:
2123 struct bt_ctf_field_type_floating_point
*floating_point
=
2125 struct bt_ctf_field_type_floating_point
,
2127 ret
= floating_point
->user_byte_order
;
2134 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2135 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2136 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2137 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2143 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2144 enum bt_ctf_byte_order byte_order
)
2147 int internal_byte_order
;
2148 enum bt_ctf_type_id type_id
;
2150 if (!type
|| type
->frozen
) {
2155 switch (byte_order
) {
2156 case BT_CTF_BYTE_ORDER_NATIVE
:
2157 /* Leave unset. Will be initialized by parent. */
2158 internal_byte_order
= 0;
2160 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2161 internal_byte_order
= LITTLE_ENDIAN
;
2163 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2164 case BT_CTF_BYTE_ORDER_NETWORK
:
2165 internal_byte_order
= BIG_ENDIAN
;
2172 type_id
= type
->declaration
->id
;
2173 if (set_byte_order_funcs
[type_id
]) {
2174 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2180 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2181 struct bt_ctf_field_type
*type
)
2184 return BT_CTF_TYPE_ID_UNKNOWN
;
2187 return type
->declaration
->id
;
2190 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2192 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2195 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2197 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2200 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2202 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2205 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2207 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2210 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2212 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2215 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2217 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2220 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2222 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2225 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2227 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2230 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2235 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2241 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2251 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2252 struct bt_ctf_field_type_variant
*variant
,
2255 struct bt_ctf_field_type
*type
= NULL
;
2256 GQuark field_name_quark
;
2258 struct structure_field
*field_entry
;
2259 struct range_overlap_query query
= {
2260 .range_start
._signed
= tag_value
,
2261 .range_end
._signed
= tag_value
,
2262 .mapping_name
= 0, .overlaps
= 0};
2264 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2266 if (!query
.overlaps
) {
2270 field_name_quark
= query
.mapping_name
;
2271 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2272 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2276 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2277 type
= field_entry
->type
;
2283 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2284 struct bt_ctf_field_type_variant
*variant
,
2287 struct bt_ctf_field_type
*type
= NULL
;
2288 GQuark field_name_quark
;
2290 struct structure_field
*field_entry
;
2291 struct range_overlap_query query
= {
2292 .range_start
._unsigned
= tag_value
,
2293 .range_end
._unsigned
= tag_value
,
2294 .mapping_name
= 0, .overlaps
= 0};
2296 g_ptr_array_foreach(variant
->tag
->entries
,
2297 check_ranges_overlap_unsigned
,
2299 if (!query
.overlaps
) {
2303 field_name_quark
= query
.mapping_name
;
2304 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2305 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2309 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2310 type
= field_entry
->type
;
2316 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2317 struct metadata_context
*context
)
2321 if (!type
|| !context
) {
2326 /* Make sure field type is valid before serializing it */
2327 ret
= bt_ctf_field_type_validate(type
);
2333 ret
= type
->serialize(type
, context
);
2339 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2346 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2347 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2348 set_byte_order_funcs
[type
->declaration
->id
](type
,
2354 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2356 struct bt_ctf_field_type
*copy
= NULL
;
2362 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2368 int bt_ctf_field_type_structure_get_field_name_index(
2369 struct bt_ctf_field_type
*type
, const char *name
)
2374 struct bt_ctf_field_type_structure
*structure
;
2376 if (!type
|| !name
||
2377 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2382 name_quark
= g_quark_try_string(name
);
2388 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2390 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2391 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2401 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2402 struct bt_ctf_field_type
*field
, int index
)
2405 struct bt_ctf_field_type_structure
*structure
;
2407 if (!type
|| !field
||
2408 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2413 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2415 if (index
< 0 || index
>= structure
->fields
->len
) {
2421 bt_put(((struct structure_field
*)
2422 g_ptr_array_index(structure
->fields
, index
))->type
);
2423 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2430 int bt_ctf_field_type_variant_get_field_name_index(
2431 struct bt_ctf_field_type
*type
, const char *name
)
2436 struct bt_ctf_field_type_variant
*variant
;
2438 if (!type
|| !name
||
2439 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2444 name_quark
= g_quark_try_string(name
);
2450 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2452 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2453 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2463 int bt_ctf_field_type_sequence_set_length_field_path(
2464 struct bt_ctf_field_type
*type
,
2465 struct bt_ctf_field_path
*path
)
2468 struct bt_ctf_field_type_sequence
*sequence
;
2470 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2471 BT_CTF_TYPE_ID_SEQUENCE
) {
2476 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2479 BT_MOVE(sequence
->length_field_path
, path
);
2485 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2486 struct bt_ctf_field_path
*path
)
2489 struct bt_ctf_field_type_variant
*variant
;
2491 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2492 BT_CTF_TYPE_ID_VARIANT
) {
2497 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2500 BT_MOVE(variant
->tag_field_path
, path
);
2506 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2507 struct bt_ctf_field_type
*tag
)
2510 struct bt_ctf_field_type_variant
*variant
;
2512 if (!type
|| !tag
||
2513 bt_ctf_field_type_get_type_id(tag
) !=
2514 BT_CTF_TYPE_ID_ENUM
) {
2519 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2523 bt_put(&variant
->tag
->parent
);
2525 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2532 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2533 struct bt_ctf_field_type
*field
, int index
)
2536 struct bt_ctf_field_type_variant
*variant
;
2538 if (!type
|| !field
||
2539 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2544 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2546 if (index
< 0 || index
>= variant
->fields
->len
) {
2552 bt_put(((struct structure_field
*)
2553 g_ptr_array_index(variant
->fields
, index
))->type
);
2554 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2561 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2563 struct bt_ctf_field_type_integer
*integer
=
2564 (struct bt_ctf_field_type_integer
*) type
;
2570 bt_put(integer
->mapped_clock
);
2575 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2577 struct bt_ctf_field_type_enumeration
*enumeration
=
2578 (struct bt_ctf_field_type_enumeration
*) type
;
2584 g_ptr_array_free(enumeration
->entries
, TRUE
);
2585 bt_put(enumeration
->container
);
2586 g_free(enumeration
);
2590 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2592 struct bt_ctf_field_type_floating_point
*floating_point
=
2593 (struct bt_ctf_field_type_floating_point
*) type
;
2599 g_free(floating_point
);
2603 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2605 struct bt_ctf_field_type_structure
*structure
=
2606 (struct bt_ctf_field_type_structure
*) type
;
2612 g_ptr_array_free(structure
->fields
, TRUE
);
2613 g_hash_table_destroy(structure
->field_name_to_index
);
2618 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2620 struct bt_ctf_field_type_variant
*variant
=
2621 (struct bt_ctf_field_type_variant
*) type
;
2627 g_ptr_array_free(variant
->fields
, TRUE
);
2628 g_hash_table_destroy(variant
->field_name_to_index
);
2629 g_string_free(variant
->tag_name
, TRUE
);
2630 bt_put(&variant
->tag
->parent
);
2631 BT_PUT(variant
->tag_field_path
);
2636 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2638 struct bt_ctf_field_type_array
*array
=
2639 (struct bt_ctf_field_type_array
*) type
;
2645 bt_put(array
->element_type
);
2650 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2652 struct bt_ctf_field_type_sequence
*sequence
=
2653 (struct bt_ctf_field_type_sequence
*) type
;
2659 bt_put(sequence
->element_type
);
2660 g_string_free(sequence
->length_field_name
, TRUE
);
2661 BT_PUT(sequence
->length_field_path
);
2666 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2668 struct bt_ctf_field_type_string
*string
=
2669 (struct bt_ctf_field_type_string
*) type
;
2679 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2685 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2687 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2688 type
, struct bt_ctf_field_type_enumeration
, parent
);
2690 generic_field_type_freeze(type
);
2691 bt_ctf_field_type_freeze(enumeration_type
->container
);
2695 void freeze_structure_field(struct structure_field
*field
)
2697 bt_ctf_field_type_freeze(field
->type
);
2701 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2703 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2704 type
, struct bt_ctf_field_type_structure
, parent
);
2706 /* Cache the alignment */
2707 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2708 generic_field_type_freeze(type
);
2709 g_ptr_array_foreach(structure_type
->fields
,
2710 (GFunc
) freeze_structure_field
, NULL
);
2714 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2716 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2717 type
, struct bt_ctf_field_type_variant
, parent
);
2719 generic_field_type_freeze(type
);
2720 g_ptr_array_foreach(variant_type
->fields
,
2721 (GFunc
) freeze_structure_field
, NULL
);
2725 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2727 struct bt_ctf_field_type_array
*array_type
= container_of(
2728 type
, struct bt_ctf_field_type_array
, parent
);
2730 /* Cache the alignment */
2731 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2732 generic_field_type_freeze(type
);
2733 bt_ctf_field_type_freeze(array_type
->element_type
);
2737 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2739 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2740 type
, struct bt_ctf_field_type_sequence
, parent
);
2742 /* Cache the alignment */
2743 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2744 generic_field_type_freeze(type
);
2745 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2749 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2751 const char *encoding_string
;
2754 case BT_CTF_STRING_ENCODING_NONE
:
2755 encoding_string
= "none";
2757 case BT_CTF_STRING_ENCODING_ASCII
:
2758 encoding_string
= "ASCII";
2760 case BT_CTF_STRING_ENCODING_UTF8
:
2761 encoding_string
= "UTF8";
2764 encoding_string
= "unknown";
2768 return encoding_string
;
2772 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2774 const char *base_string
;
2777 case BT_CTF_INTEGER_BASE_DECIMAL
:
2778 base_string
= "decimal";
2780 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2781 base_string
= "hexadecimal";
2783 case BT_CTF_INTEGER_BASE_OCTAL
:
2784 base_string
= "octal";
2786 case BT_CTF_INTEGER_BASE_BINARY
:
2787 base_string
= "binary";
2790 base_string
= "unknown";
2798 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2799 struct metadata_context
*context
)
2801 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2802 struct bt_ctf_field_type_integer
, parent
);
2805 g_string_append_printf(context
->string
,
2806 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2807 integer
->declaration
.len
, type
->declaration
->alignment
,
2808 (integer
->declaration
.signedness
? "true" : "false"),
2809 get_encoding_string(integer
->declaration
.encoding
),
2810 get_integer_base_string(integer
->declaration
.base
),
2811 get_byte_order_string(integer
->declaration
.byte_order
));
2812 if (integer
->mapped_clock
) {
2813 const char *clock_name
= bt_ctf_clock_get_name(
2814 integer
->mapped_clock
);
2821 g_string_append_printf(context
->string
,
2822 "; map = clock.%s.value", clock_name
);
2825 g_string_append(context
->string
, "; }");
2831 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2832 struct metadata_context
*context
)
2836 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2837 struct bt_ctf_field_type_enumeration
, parent
);
2838 struct bt_ctf_field_type
*container_type
;
2839 int container_signed
;
2841 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2842 if (!container_type
) {
2847 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2848 if (container_signed
< 0) {
2849 ret
= container_signed
;
2850 goto error_put_container_type
;
2853 g_string_append(context
->string
, "enum : ");
2854 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2856 goto error_put_container_type
;
2859 g_string_append(context
->string
, " { ");
2860 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2861 struct enumeration_mapping
*mapping
=
2862 enumeration
->entries
->pdata
[entry
];
2864 if (container_signed
) {
2865 if (mapping
->range_start
._signed
==
2866 mapping
->range_end
._signed
) {
2867 g_string_append_printf(context
->string
,
2868 "\"%s\" = %" PRId64
,
2869 g_quark_to_string(mapping
->string
),
2870 mapping
->range_start
._signed
);
2872 g_string_append_printf(context
->string
,
2873 "\"%s\" = %" PRId64
" ... %" PRId64
,
2874 g_quark_to_string(mapping
->string
),
2875 mapping
->range_start
._signed
,
2876 mapping
->range_end
._signed
);
2879 if (mapping
->range_start
._unsigned
==
2880 mapping
->range_end
._unsigned
) {
2881 g_string_append_printf(context
->string
,
2882 "\"%s\" = %" PRIu64
,
2883 g_quark_to_string(mapping
->string
),
2884 mapping
->range_start
._unsigned
);
2886 g_string_append_printf(context
->string
,
2887 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2888 g_quark_to_string(mapping
->string
),
2889 mapping
->range_start
._unsigned
,
2890 mapping
->range_end
._unsigned
);
2894 g_string_append(context
->string
,
2895 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2899 if (context
->field_name
->len
) {
2900 g_string_append_printf(context
->string
, " %s",
2901 context
->field_name
->str
);
2902 g_string_assign(context
->field_name
, "");
2904 error_put_container_type
:
2905 bt_put(container_type
);
2911 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2912 struct metadata_context
*context
)
2914 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2915 type
, struct bt_ctf_field_type_floating_point
, parent
);
2917 g_string_append_printf(context
->string
,
2918 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2919 floating_point
->declaration
.exp
->len
,
2920 floating_point
->declaration
.mantissa
->len
+ 1,
2921 get_byte_order_string(floating_point
->declaration
.byte_order
),
2922 type
->declaration
->alignment
);
2927 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2928 struct metadata_context
*context
)
2931 unsigned int indent
;
2933 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2934 struct bt_ctf_field_type_structure
, parent
);
2935 GString
*structure_field_name
= context
->field_name
;
2937 context
->field_name
= g_string_new("");
2939 context
->current_indentation_level
++;
2940 g_string_append(context
->string
, "struct {\n");
2942 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2943 struct structure_field
*field
;
2945 for (indent
= 0; indent
< context
->current_indentation_level
;
2947 g_string_append_c(context
->string
, '\t');
2950 field
= structure
->fields
->pdata
[i
];
2951 g_string_assign(context
->field_name
,
2952 g_quark_to_string(field
->name
));
2953 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2958 if (context
->field_name
->len
) {
2959 g_string_append_printf(context
->string
, " %s",
2960 context
->field_name
->str
);
2962 g_string_append(context
->string
, ";\n");
2965 context
->current_indentation_level
--;
2966 for (indent
= 0; indent
< context
->current_indentation_level
;
2968 g_string_append_c(context
->string
, '\t');
2971 g_string_append_printf(context
->string
, "} align(%zu)",
2972 type
->declaration
->alignment
);
2974 g_string_free(context
->field_name
, TRUE
);
2975 context
->field_name
= structure_field_name
;
2980 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2981 struct metadata_context
*context
)
2984 unsigned int indent
;
2986 struct bt_ctf_field_type_variant
*variant
= container_of(
2987 type
, struct bt_ctf_field_type_variant
, parent
);
2988 GString
*variant_field_name
= context
->field_name
;
2990 context
->field_name
= g_string_new("");
2991 if (variant
->tag_name
->len
> 0) {
2992 g_string_append_printf(context
->string
,
2993 "variant <%s> {\n", variant
->tag_name
->str
);
2995 g_string_append(context
->string
, "variant {\n");
2998 context
->current_indentation_level
++;
2999 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3000 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3002 g_string_assign(context
->field_name
,
3003 g_quark_to_string(field
->name
));
3004 for (indent
= 0; indent
< context
->current_indentation_level
;
3006 g_string_append_c(context
->string
, '\t');
3009 g_string_assign(context
->field_name
,
3010 g_quark_to_string(field
->name
));
3011 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3016 if (context
->field_name
->len
) {
3017 g_string_append_printf(context
->string
, " %s;",
3018 context
->field_name
->str
);
3021 g_string_append_c(context
->string
, '\n');
3024 context
->current_indentation_level
--;
3025 for (indent
= 0; indent
< context
->current_indentation_level
;
3027 g_string_append_c(context
->string
, '\t');
3030 g_string_append(context
->string
, "}");
3032 g_string_free(context
->field_name
, TRUE
);
3033 context
->field_name
= variant_field_name
;
3038 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3039 struct metadata_context
*context
)
3042 struct bt_ctf_field_type_array
*array
= container_of(type
,
3043 struct bt_ctf_field_type_array
, parent
);
3045 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3050 if (context
->field_name
->len
) {
3051 g_string_append_printf(context
->string
, " %s[%u]",
3052 context
->field_name
->str
, array
->length
);
3053 g_string_assign(context
->field_name
, "");
3055 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3062 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3063 struct metadata_context
*context
)
3066 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3067 type
, struct bt_ctf_field_type_sequence
, parent
);
3069 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3074 if (context
->field_name
->len
) {
3075 g_string_append_printf(context
->string
, " %s[%s]",
3076 context
->field_name
->str
,
3077 sequence
->length_field_name
->str
);
3078 g_string_assign(context
->field_name
, "");
3080 g_string_append_printf(context
->string
, "[%s]",
3081 sequence
->length_field_name
->str
);
3088 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3089 struct metadata_context
*context
)
3091 struct bt_ctf_field_type_string
*string
= container_of(
3092 type
, struct bt_ctf_field_type_string
, parent
);
3094 g_string_append_printf(context
->string
,
3095 "string { encoding = %s; }",
3096 get_encoding_string(string
->declaration
.encoding
));
3101 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3102 enum bt_ctf_byte_order ret
;
3104 switch (byte_order
) {
3105 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3107 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3109 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3111 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3113 case BT_CTF_BYTE_ORDER_NETWORK
:
3114 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3116 case BT_CTF_BYTE_ORDER_NATIVE
:
3117 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3120 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3128 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3129 int byte_order
, int set_native
)
3131 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3132 struct bt_ctf_field_type_integer
, parent
);
3135 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3137 * User byte order is native, so we can set
3138 * the real byte order.
3140 integer_type
->declaration
.byte_order
=
3144 integer_type
->user_byte_order
=
3145 get_ctf_ir_byte_order(byte_order
);
3146 integer_type
->declaration
.byte_order
= byte_order
;
3151 void bt_ctf_field_type_enumeration_set_byte_order(
3152 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3154 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3155 struct bt_ctf_field_type_enumeration
, parent
);
3157 /* Safe to assume that container is an integer */
3158 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3159 byte_order
, set_native
);
3163 void bt_ctf_field_type_floating_point_set_byte_order(
3164 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3166 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3167 container_of(type
, struct bt_ctf_field_type_floating_point
,
3171 if (floating_point_type
->user_byte_order
==
3172 BT_CTF_BYTE_ORDER_NATIVE
) {
3174 * User byte order is native, so we can set
3175 * the real byte order.
3177 floating_point_type
->declaration
.byte_order
=
3179 floating_point_type
->sign
.byte_order
=
3181 floating_point_type
->mantissa
.byte_order
=
3183 floating_point_type
->exp
.byte_order
=
3187 floating_point_type
->user_byte_order
=
3188 get_ctf_ir_byte_order(byte_order
);
3189 floating_point_type
->declaration
.byte_order
= byte_order
;
3190 floating_point_type
->sign
.byte_order
= byte_order
;
3191 floating_point_type
->mantissa
.byte_order
= byte_order
;
3192 floating_point_type
->exp
.byte_order
= byte_order
;
3197 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3198 int byte_order
, int set_native
)
3201 struct bt_ctf_field_type_structure
*structure_type
=
3202 container_of(type
, struct bt_ctf_field_type_structure
,
3205 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3206 struct structure_field
*field
= g_ptr_array_index(
3207 structure_type
->fields
, i
);
3208 struct bt_ctf_field_type
*field_type
= field
->type
;
3210 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3211 set_byte_order_funcs
[field_type
->declaration
->id
](
3212 field_type
, byte_order
, set_native
);
3218 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3219 int byte_order
, int set_native
)
3222 struct bt_ctf_field_type_variant
*variant_type
=
3223 container_of(type
, struct bt_ctf_field_type_variant
,
3226 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3227 struct structure_field
*field
= g_ptr_array_index(
3228 variant_type
->fields
, i
);
3229 struct bt_ctf_field_type
*field_type
= field
->type
;
3231 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3232 set_byte_order_funcs
[field_type
->declaration
->id
](
3233 field_type
, byte_order
, set_native
);
3239 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3240 int byte_order
, int set_native
)
3242 struct bt_ctf_field_type_array
*array_type
=
3243 container_of(type
, struct bt_ctf_field_type_array
,
3246 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3247 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3248 array_type
->element_type
, byte_order
, set_native
);
3253 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3254 int byte_order
, int set_native
)
3256 struct bt_ctf_field_type_sequence
*sequence_type
=
3257 container_of(type
, struct bt_ctf_field_type_sequence
,
3260 if (set_byte_order_funcs
[
3261 sequence_type
->element_type
->declaration
->id
]) {
3262 set_byte_order_funcs
[
3263 sequence_type
->element_type
->declaration
->id
](
3264 sequence_type
->element_type
, byte_order
, set_native
);
3269 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3270 struct bt_ctf_field_type
*type
)
3272 struct bt_ctf_field_type
*copy
;
3273 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3275 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3276 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3281 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3283 copy_integer
->declaration
= integer
->declaration
;
3284 if (integer
->mapped_clock
) {
3285 bt_get(integer
->mapped_clock
);
3286 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3289 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3296 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3297 struct bt_ctf_field_type
*type
)
3300 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3301 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3303 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3306 /* Copy the source enumeration's container */
3307 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3308 if (!copy_container
) {
3312 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3316 copy_enumeration
= container_of(copy
,
3317 struct bt_ctf_field_type_enumeration
, parent
);
3319 /* Copy all enumaration entries */
3320 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3321 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3322 enumeration
->entries
, i
);
3323 struct enumeration_mapping
* copy_mapping
= g_new0(
3324 struct enumeration_mapping
, 1);
3326 if (!copy_mapping
) {
3330 *copy_mapping
= *mapping
;
3331 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3334 copy_enumeration
->declaration
= enumeration
->declaration
;
3336 bt_put(copy_container
);
3339 bt_put(copy_container
);
3345 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3346 struct bt_ctf_field_type
*type
)
3348 struct bt_ctf_field_type
*copy
;
3349 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3351 floating_point
= container_of(type
,
3352 struct bt_ctf_field_type_floating_point
, parent
);
3353 copy
= bt_ctf_field_type_floating_point_create();
3358 copy_float
= container_of(copy
,
3359 struct bt_ctf_field_type_floating_point
, parent
);
3360 copy_float
->declaration
= floating_point
->declaration
;
3361 copy_float
->sign
= floating_point
->sign
;
3362 copy_float
->mantissa
= floating_point
->mantissa
;
3363 copy_float
->exp
= floating_point
->exp
;
3364 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3365 copy_float
->declaration
.sign
= ©_float
->sign
;
3366 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3367 copy_float
->declaration
.exp
= ©_float
->exp
;
3373 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3374 struct bt_ctf_field_type
*type
)
3377 GHashTableIter iter
;
3378 gpointer key
, value
;
3379 struct bt_ctf_field_type
*copy
;
3380 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3382 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3384 copy
= bt_ctf_field_type_structure_create();
3389 copy_structure
= container_of(copy
,
3390 struct bt_ctf_field_type_structure
, parent
);
3392 /* Copy field_name_to_index */
3393 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3394 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3395 g_hash_table_insert(copy_structure
->field_name_to_index
,
3399 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3400 struct structure_field
*entry
, *copy_entry
;
3401 struct bt_ctf_field_type
*copy_field
;
3403 copy_entry
= g_new0(struct structure_field
, 1);
3408 entry
= g_ptr_array_index(structure
->fields
, i
);
3409 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3415 copy_entry
->name
= entry
->name
;
3416 copy_entry
->type
= copy_field
;
3417 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3420 copy_structure
->declaration
= structure
->declaration
;
3429 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3430 struct bt_ctf_field_type
*type
)
3433 GHashTableIter iter
;
3434 gpointer key
, value
;
3435 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3436 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3438 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3441 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3447 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3448 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3453 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3456 /* Copy field_name_to_index */
3457 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3458 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3459 g_hash_table_insert(copy_variant
->field_name_to_index
,
3463 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3464 struct structure_field
*entry
, *copy_entry
;
3465 struct bt_ctf_field_type
*copy_field
;
3467 copy_entry
= g_new0(struct structure_field
, 1);
3472 entry
= g_ptr_array_index(variant
->fields
, i
);
3473 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3479 copy_entry
->name
= entry
->name
;
3480 copy_entry
->type
= copy_field
;
3481 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3484 copy_variant
->declaration
= variant
->declaration
;
3485 if (variant
->tag_field_path
) {
3486 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3487 variant
->tag_field_path
);
3488 if (!copy_variant
->tag_field_path
) {
3502 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3503 struct bt_ctf_field_type
*type
)
3505 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3506 struct bt_ctf_field_type_array
*array
, *copy_array
;
3508 array
= container_of(type
, struct bt_ctf_field_type_array
,
3510 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3511 if (!copy_element
) {
3515 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3520 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3522 copy_array
->declaration
= array
->declaration
;
3524 bt_put(copy_element
);
3529 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3530 struct bt_ctf_field_type
*type
)
3532 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3533 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3535 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3537 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3538 if (!copy_element
) {
3542 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3543 sequence
->length_field_name
->len
?
3544 sequence
->length_field_name
->str
: NULL
);
3549 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3551 copy_sequence
->declaration
= sequence
->declaration
;
3552 if (sequence
->length_field_path
) {
3553 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3554 sequence
->length_field_path
);
3555 if (!copy_sequence
->length_field_path
) {
3560 bt_put(copy_element
);
3568 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3569 struct bt_ctf_field_type
*type
)
3571 struct bt_ctf_field_type
*copy
;
3572 struct bt_ctf_field_type_string
*string
, *copy_string
;
3574 copy
= bt_ctf_field_type_string_create();
3579 string
= container_of(type
, struct bt_ctf_field_type_string
,
3581 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3583 copy_string
->declaration
= string
->declaration
;
3589 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3590 struct bt_ctf_field_type
*type_b
)
3593 struct bt_ctf_field_type_integer
*integer_a
;
3594 struct bt_ctf_field_type_integer
*integer_b
;
3595 struct declaration_integer
*decl_a
;
3596 struct declaration_integer
*decl_b
;
3598 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3600 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3602 decl_a
= &integer_a
->declaration
;
3603 decl_b
= &integer_b
->declaration
;
3606 if (decl_a
->len
!= decl_b
->len
) {
3611 * Compare user byte orders only, not the cached,
3614 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3619 if (decl_a
->signedness
!= decl_b
->signedness
) {
3624 if (decl_a
->base
!= decl_b
->base
) {
3629 if (decl_a
->encoding
!= decl_b
->encoding
) {
3634 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3646 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3647 struct bt_ctf_field_type
*type_b
)
3650 struct bt_ctf_field_type_floating_point
*float_a
;
3651 struct bt_ctf_field_type_floating_point
*float_b
;
3653 float_a
= container_of(type_a
,
3654 struct bt_ctf_field_type_floating_point
, parent
);
3655 float_b
= container_of(type_b
,
3656 struct bt_ctf_field_type_floating_point
, parent
);
3659 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3663 /* Exponent length */
3664 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3668 /* Mantissa length */
3669 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3674 * Compare user byte orders only, not the cached,
3677 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3689 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3690 struct enumeration_mapping
*mapping_b
)
3695 if (mapping_a
->string
!= mapping_b
->string
) {
3700 if (mapping_a
->range_start
._unsigned
!=
3701 mapping_b
->range_start
._unsigned
) {
3706 if (mapping_a
->range_end
._unsigned
!=
3707 mapping_b
->range_end
._unsigned
) {
3719 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3720 struct bt_ctf_field_type
*type_b
)
3724 struct bt_ctf_field_type_enumeration
*enum_a
;
3725 struct bt_ctf_field_type_enumeration
*enum_b
;
3727 enum_a
= container_of(type_a
,
3728 struct bt_ctf_field_type_enumeration
, parent
);
3729 enum_b
= container_of(type_b
,
3730 struct bt_ctf_field_type_enumeration
, parent
);
3732 /* Container field type */
3733 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3741 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3745 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3746 struct enumeration_mapping
*mapping_a
=
3747 g_ptr_array_index(enum_a
->entries
, i
);
3748 struct enumeration_mapping
*mapping_b
=
3749 g_ptr_array_index(enum_b
->entries
, i
);
3751 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3764 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3765 struct bt_ctf_field_type
*type_b
)
3768 struct bt_ctf_field_type_string
*string_a
;
3769 struct bt_ctf_field_type_string
*string_b
;
3771 string_a
= container_of(type_a
,
3772 struct bt_ctf_field_type_string
, parent
);
3773 string_b
= container_of(type_b
,
3774 struct bt_ctf_field_type_string
, parent
);
3777 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3789 int compare_structure_fields(struct structure_field
*field_a
,
3790 struct structure_field
*field_b
)
3795 if (field_a
->name
!= field_b
->name
) {
3800 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3807 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3808 struct bt_ctf_field_type
*type_b
)
3812 struct bt_ctf_field_type_structure
*struct_a
;
3813 struct bt_ctf_field_type_structure
*struct_b
;
3815 struct_a
= container_of(type_a
,
3816 struct bt_ctf_field_type_structure
, parent
);
3817 struct_b
= container_of(type_b
,
3818 struct bt_ctf_field_type_structure
, parent
);
3821 if (bt_ctf_field_type_get_alignment(type_a
) !=
3822 bt_ctf_field_type_get_alignment(type_b
)) {
3827 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3831 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3832 struct structure_field
*field_a
=
3833 g_ptr_array_index(struct_a
->fields
, i
);
3834 struct structure_field
*field_b
=
3835 g_ptr_array_index(struct_b
->fields
, i
);
3837 ret
= compare_structure_fields(field_a
, field_b
);
3851 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3852 struct bt_ctf_field_type
*type_b
)
3856 struct bt_ctf_field_type_variant
*variant_a
;
3857 struct bt_ctf_field_type_variant
*variant_b
;
3859 variant_a
= container_of(type_a
,
3860 struct bt_ctf_field_type_variant
, parent
);
3861 variant_b
= container_of(type_b
,
3862 struct bt_ctf_field_type_variant
, parent
);
3865 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3870 ret
= bt_ctf_field_type_compare(
3871 (struct bt_ctf_field_type
*) variant_a
->tag
,
3872 (struct bt_ctf_field_type
*) variant_b
->tag
);
3880 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3884 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3885 struct structure_field
*field_a
=
3886 g_ptr_array_index(variant_a
->fields
, i
);
3887 struct structure_field
*field_b
=
3888 g_ptr_array_index(variant_b
->fields
, i
);
3890 ret
= compare_structure_fields(field_a
, field_b
);
3904 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3905 struct bt_ctf_field_type
*type_b
)
3908 struct bt_ctf_field_type_array
*array_a
;
3909 struct bt_ctf_field_type_array
*array_b
;
3911 array_a
= container_of(type_a
,
3912 struct bt_ctf_field_type_array
, parent
);
3913 array_b
= container_of(type_b
,
3914 struct bt_ctf_field_type_array
, parent
);
3917 if (array_a
->length
!= array_b
->length
) {
3922 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3923 array_b
->element_type
);
3930 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3931 struct bt_ctf_field_type
*type_b
)
3934 struct bt_ctf_field_type_sequence
*sequence_a
;
3935 struct bt_ctf_field_type_sequence
*sequence_b
;
3937 sequence_a
= container_of(type_a
,
3938 struct bt_ctf_field_type_sequence
, parent
);
3939 sequence_b
= container_of(type_b
,
3940 struct bt_ctf_field_type_sequence
, parent
);
3943 if (strcmp(sequence_a
->length_field_name
->str
,
3944 sequence_b
->length_field_name
->str
)) {
3949 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
3950 sequence_b
->element_type
);
3956 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
3957 struct bt_ctf_field_type
*type_b
)
3961 if (type_a
== type_b
) {
3962 /* Same reference: equal (even if both are NULL) */
3967 if (!type_a
|| !type_b
) {
3972 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
3973 /* Different type IDs */
3977 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
3978 /* Both have unknown type IDs */
3982 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
3989 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
3991 int field_count
= -1;
3992 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
3995 case CTF_TYPE_STRUCT
:
3997 bt_ctf_field_type_structure_get_field_count(field_type
);
3999 case CTF_TYPE_VARIANT
:
4001 bt_ctf_field_type_variant_get_field_count(field_type
);
4003 case CTF_TYPE_ARRAY
:
4004 case CTF_TYPE_SEQUENCE
:
4006 * Array and sequence types always contain a single member
4007 * (the element type).
4019 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4020 struct bt_ctf_field_type
*field_type
, int index
)
4022 struct bt_ctf_field_type
*field
= NULL
;
4023 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4026 case CTF_TYPE_STRUCT
:
4027 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4030 case CTF_TYPE_VARIANT
:
4031 bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4034 case CTF_TYPE_ARRAY
:
4035 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4037 case CTF_TYPE_SEQUENCE
:
4038 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4048 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4051 int field_index
= -1;
4052 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4055 case CTF_TYPE_STRUCT
:
4056 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4059 case CTF_TYPE_VARIANT
:
4060 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4070 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4071 struct bt_ctf_field_type
*type
)
4073 struct bt_ctf_field_path
*field_path
= NULL
;
4074 struct bt_ctf_field_type_variant
*variant
;
4076 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4080 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4082 field_path
= bt_get(variant
->tag_field_path
);
4087 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4088 struct bt_ctf_field_type
*type
)
4090 struct bt_ctf_field_path
*field_path
= NULL
;
4091 struct bt_ctf_field_type_sequence
*sequence
;
4093 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4097 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4099 field_path
= bt_get(sequence
->length_field_path
);