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-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/clock.h>
34 #include <babeltrace/ctf-ir/clock-internal.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_integer_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
104 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
107 type_freeze_func
const type_freeze_funcs
[] = {
108 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
109 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
110 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
111 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
112 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
113 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
114 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
115 [BT_CTF_TYPE_ID_STRING
] = generic_field_type_freeze
,
119 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_floating_point_serialize(
126 struct bt_ctf_field_type
*, struct metadata_context
*);
128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
144 type_serialize_func
const type_serialize_funcs
[] = {
145 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
146 [BT_CTF_TYPE_ID_ENUM
] =
147 bt_ctf_field_type_enumeration_serialize
,
148 [BT_CTF_TYPE_ID_FLOAT
] =
149 bt_ctf_field_type_floating_point_serialize
,
150 [BT_CTF_TYPE_ID_STRUCT
] =
151 bt_ctf_field_type_structure_serialize
,
152 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
153 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
154 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
155 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
159 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_floating_point_set_byte_order(
166 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
168 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
178 int byte_order
, int set_native
);
180 /* The set_native flag only set the byte order if it is set to native */
182 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
183 int byte_order
, int set_native
) = {
184 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
185 [BT_CTF_TYPE_ID_ENUM
] =
186 bt_ctf_field_type_enumeration_set_byte_order
,
187 [BT_CTF_TYPE_ID_FLOAT
] =
188 bt_ctf_field_type_floating_point_set_byte_order
,
189 [BT_CTF_TYPE_ID_STRUCT
] =
190 bt_ctf_field_type_structure_set_byte_order
,
191 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
192 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
193 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
194 [BT_CTF_TYPE_ID_STRING
] = NULL
,
198 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
217 struct bt_ctf_field_type
*);
219 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
220 struct bt_ctf_field_type
*);
223 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
224 struct bt_ctf_field_type
*) = {
225 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
226 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
227 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
228 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
229 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
230 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
231 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
232 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
236 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
237 struct bt_ctf_field_type
*);
239 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
240 struct bt_ctf_field_type
*);
242 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
243 struct bt_ctf_field_type
*);
245 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
246 struct bt_ctf_field_type
*);
248 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
249 struct bt_ctf_field_type
*);
251 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
252 struct bt_ctf_field_type
*);
254 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
255 struct bt_ctf_field_type
*);
257 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
258 struct bt_ctf_field_type
*);
261 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
262 struct bt_ctf_field_type
*) = {
263 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
264 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
265 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
266 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
267 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
268 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
269 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
270 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
274 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
282 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
284 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
287 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
288 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
289 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
290 [BT_CTF_TYPE_ID_STRING
] = NULL
,
291 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
292 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
293 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
294 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
295 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
299 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
305 void destroy_structure_field(struct structure_field
*field
)
312 void check_ranges_overlap(gpointer element
, gpointer query
)
314 struct enumeration_mapping
*mapping
= element
;
315 struct range_overlap_query
*overlap_query
= query
;
317 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
318 && overlap_query
->range_start
._signed
<=
319 mapping
->range_end
._signed
) {
320 overlap_query
->overlaps
= 1;
321 overlap_query
->mapping_name
= mapping
->string
;
324 overlap_query
->overlaps
|=
325 mapping
->string
== overlap_query
->mapping_name
;
329 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
331 struct enumeration_mapping
*mapping
= element
;
332 struct range_overlap_query
*overlap_query
= query
;
334 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
335 && overlap_query
->range_start
._unsigned
<=
336 mapping
->range_end
._unsigned
) {
337 overlap_query
->overlaps
= 1;
338 overlap_query
->mapping_name
= mapping
->string
;
341 overlap_query
->overlaps
|=
342 mapping
->string
== overlap_query
->mapping_name
;
346 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
347 struct enumeration_mapping
**b
)
349 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
353 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
354 struct enumeration_mapping
**b
)
356 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
360 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
362 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
364 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
365 (type_id
< BT_CTF_NR_TYPE_IDS
));
367 bt_object_init(type
, bt_ctf_field_type_destroy
);
368 type
->freeze
= type_freeze_funcs
[type_id
];
369 type
->serialize
= type_serialize_funcs
[type_id
];
372 int ret
= bt_ctf_field_type_set_byte_order(type
,
373 BT_CTF_BYTE_ORDER_NATIVE
);
377 type
->declaration
->alignment
= 1;
381 int add_structure_field(GPtrArray
*fields
,
382 GHashTable
*field_name_to_index
,
383 struct bt_ctf_field_type
*field_type
,
384 const char *field_name
)
387 GQuark name_quark
= g_quark_from_string(field_name
);
388 struct structure_field
*field
;
390 /* Make sure structure does not contain a field of the same name */
391 if (g_hash_table_lookup_extended(field_name_to_index
,
392 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
397 field
= g_new0(struct structure_field
, 1);
404 field
->name
= name_quark
;
405 field
->type
= field_type
;
406 g_hash_table_insert(field_name_to_index
,
407 GUINT_TO_POINTER(name_quark
),
408 GUINT_TO_POINTER(fields
->len
));
409 g_ptr_array_add(fields
, field
);
415 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
417 struct bt_ctf_field_type
*type
;
418 enum bt_ctf_type_id type_id
;
420 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
421 type_id
= type
->declaration
->id
;
422 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
423 type_id
>= BT_CTF_NR_TYPE_IDS
) {
427 type_destroy_funcs
[type_id
](type
);
431 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
435 struct bt_ctf_field_type_integer
*integer
=
436 container_of(type
, struct bt_ctf_field_type_integer
,
439 if (integer
->mapped_clock
&& integer
->declaration
.signedness
) {
449 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
453 struct bt_ctf_field_type_enumeration
*enumeration
=
454 container_of(type
, struct bt_ctf_field_type_enumeration
,
456 struct bt_ctf_field_type
*container_type
=
457 bt_ctf_field_type_enumeration_get_container_type(type
);
459 if (!container_type
) {
464 ret
= bt_ctf_field_type_validate(container_type
);
469 /* Ensure enum has entries */
470 ret
= enumeration
->entries
->len
? 0 : -1;
473 BT_PUT(container_type
);
478 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
481 struct bt_ctf_field_type
*element_type
= NULL
;
482 struct bt_ctf_field_type_sequence
*sequence
=
483 container_of(type
, struct bt_ctf_field_type_sequence
,
486 /* Length field name should be set at this point */
487 if (sequence
->length_field_name
->len
== 0) {
492 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
498 ret
= bt_ctf_field_type_validate(element_type
);
501 BT_PUT(element_type
);
507 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
510 struct bt_ctf_field_type
*element_type
= NULL
;
512 element_type
= bt_ctf_field_type_array_get_element_type(type
);
518 ret
= bt_ctf_field_type_validate(element_type
);
521 BT_PUT(element_type
);
527 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
530 struct bt_ctf_field_type
*child_type
= NULL
;
531 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
534 if (field_count
< 0) {
539 for (i
= 0; i
< field_count
; ++i
) {
540 ret
= bt_ctf_field_type_structure_get_field(type
,
541 NULL
, &child_type
, i
);
546 ret
= bt_ctf_field_type_validate(child_type
);
561 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
565 struct bt_ctf_field_type
*child_type
= NULL
;
566 struct bt_ctf_field_type_variant
*variant
=
567 container_of(type
, struct bt_ctf_field_type_variant
,
570 int tag_mappings_count
;
572 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
578 bt_ctf_field_type_enumeration_get_mapping_count(
579 (struct bt_ctf_field_type
*) variant
->tag
);
581 if (tag_mappings_count
!= variant
->fields
->len
) {
586 for (i
= 0; i
< tag_mappings_count
; ++i
) {
588 int64_t range_start
, range_end
;
589 struct bt_ctf_field_type
*ft
;
591 ret
= bt_ctf_field_type_enumeration_get_mapping(
592 (struct bt_ctf_field_type
*) variant
->tag
,
593 i
, &label
, &range_start
, &range_end
);
602 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
612 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
613 if (field_count
< 0) {
618 for (i
= 0; i
< field_count
; ++i
) {
619 ret
= bt_ctf_field_type_variant_get_field(type
,
620 NULL
, &child_type
, i
);
625 ret
= bt_ctf_field_type_validate(child_type
);
640 * This function validates a given field type without considering
641 * where this field type is located. It only validates the properties
642 * of the given field type and the properties of its children if
646 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
649 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
657 /* Already marked as valid */
661 if (type_validate_funcs
[id
]) {
662 ret
= type_validate_funcs
[id
](type
);
665 if (!ret
&& type
->frozen
) {
666 /* Field type is valid */
674 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
676 struct bt_ctf_field_type_integer
*integer
=
677 g_new0(struct bt_ctf_field_type_integer
, 1);
679 if (!integer
|| size
== 0 || size
> 64) {
683 integer
->parent
.declaration
= &integer
->declaration
.p
;
684 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
685 integer
->declaration
.len
= size
;
686 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
687 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
688 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
689 return &integer
->parent
;
692 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
695 struct bt_ctf_field_type_integer
*integer
;
697 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
702 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
703 ret
= (int) integer
->declaration
.len
;
708 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
711 struct bt_ctf_field_type_integer
*integer
;
713 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
718 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
719 ret
= integer
->declaration
.signedness
;
724 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
728 struct bt_ctf_field_type_integer
*integer
;
730 if (!type
|| type
->frozen
||
731 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
736 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
737 integer
->declaration
.signedness
= !!is_signed
;
742 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
743 struct bt_ctf_field_type
*type
)
745 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
746 struct bt_ctf_field_type_integer
*integer
;
748 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
752 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
753 ret
= integer
->declaration
.base
;
758 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
759 enum bt_ctf_integer_base base
)
763 if (!type
|| type
->frozen
||
764 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
770 case BT_CTF_INTEGER_BASE_BINARY
:
771 case BT_CTF_INTEGER_BASE_OCTAL
:
772 case BT_CTF_INTEGER_BASE_DECIMAL
:
773 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
775 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
776 struct bt_ctf_field_type_integer
, parent
);
777 integer
->declaration
.base
= base
;
787 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
788 struct bt_ctf_field_type
*type
)
790 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
791 struct bt_ctf_field_type_integer
*integer
;
793 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
797 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
798 ret
= integer
->declaration
.encoding
;
803 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
804 enum bt_ctf_string_encoding encoding
)
807 struct bt_ctf_field_type_integer
*integer
;
809 if (!type
|| type
->frozen
||
810 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
811 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
812 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
817 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
818 integer
->declaration
.encoding
= encoding
;
823 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
824 struct bt_ctf_field_type
*type
)
826 struct bt_ctf_field_type_integer
*integer
;
827 struct bt_ctf_clock
*clock
= NULL
;
833 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
834 clock
= integer
->mapped_clock
;
840 int bt_ctf_field_type_integer_set_mapped_clock(
841 struct bt_ctf_field_type
*type
,
842 struct bt_ctf_clock
*clock
)
844 struct bt_ctf_field_type_integer
*integer
;
847 if (!type
|| type
->frozen
|| !bt_ctf_clock_is_valid(clock
)) {
852 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
853 bt_put(integer
->mapped_clock
);
855 integer
->mapped_clock
= clock
;
860 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
861 struct bt_ctf_field_type
*integer_container_type
)
863 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
865 if (!integer_container_type
) {
869 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
873 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
878 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
879 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
880 bt_get(integer_container_type
);
881 enumeration
->container
= integer_container_type
;
882 enumeration
->entries
= g_ptr_array_new_with_free_func(
883 (GDestroyNotify
)destroy_enumeration_mapping
);
884 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
885 return &enumeration
->parent
;
891 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
892 struct bt_ctf_field_type
*type
)
894 struct bt_ctf_field_type
*container_type
= NULL
;
895 struct bt_ctf_field_type_enumeration
*enumeration_type
;
901 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
905 enumeration_type
= container_of(type
,
906 struct bt_ctf_field_type_enumeration
, parent
);
907 container_type
= enumeration_type
->container
;
908 bt_get(container_type
);
910 return container_type
;
913 int bt_ctf_field_type_enumeration_add_mapping(
914 struct bt_ctf_field_type
*type
, const char *string
,
915 int64_t range_start
, int64_t range_end
)
919 struct enumeration_mapping
*mapping
;
920 struct bt_ctf_field_type_enumeration
*enumeration
;
921 struct range_overlap_query query
;
922 char *escaped_string
;
924 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
926 (range_end
< range_start
)) {
931 if (!string
|| strlen(string
) == 0) {
936 escaped_string
= g_strescape(string
, NULL
);
937 if (!escaped_string
) {
942 mapping_name
= g_quark_from_string(escaped_string
);
943 query
= (struct range_overlap_query
) {
944 .range_start
._signed
= range_start
,
945 .range_end
._signed
= range_end
,
946 .mapping_name
= mapping_name
,
948 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
951 /* Check that the range does not overlap with one already present */
952 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
953 if (query
.overlaps
) {
958 mapping
= g_new(struct enumeration_mapping
, 1);
964 *mapping
= (struct enumeration_mapping
) {
965 .range_start
._signed
= range_start
,
966 .range_end
._signed
= range_end
, .string
= mapping_name
};
967 g_ptr_array_add(enumeration
->entries
, mapping
);
968 g_ptr_array_sort(enumeration
->entries
,
969 (GCompareFunc
)compare_enumeration_mappings_signed
);
971 free(escaped_string
);
976 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
977 struct bt_ctf_field_type
*type
, const char *string
,
978 uint64_t range_start
, uint64_t range_end
)
982 struct enumeration_mapping
*mapping
;
983 struct bt_ctf_field_type_enumeration
*enumeration
;
984 struct range_overlap_query query
;
985 char *escaped_string
;
987 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
989 (range_end
< range_start
)) {
994 if (!string
|| strlen(string
) == 0) {
999 escaped_string
= g_strescape(string
, NULL
);
1000 if (!escaped_string
) {
1005 mapping_name
= g_quark_from_string(escaped_string
);
1006 query
= (struct range_overlap_query
) {
1007 .range_start
._unsigned
= range_start
,
1008 .range_end
._unsigned
= range_end
,
1009 .mapping_name
= mapping_name
,
1011 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1014 /* Check that the range does not overlap with one already present */
1015 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
1017 if (query
.overlaps
) {
1022 mapping
= g_new(struct enumeration_mapping
, 1);
1028 *mapping
= (struct enumeration_mapping
) {
1029 .range_start
._unsigned
= range_start
,
1030 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1031 g_ptr_array_add(enumeration
->entries
, mapping
);
1032 g_ptr_array_sort(enumeration
->entries
,
1033 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1035 free(escaped_string
);
1040 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1041 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1044 const char *name
= NULL
;
1045 struct range_overlap_query query
=
1046 (struct range_overlap_query
) {
1047 .range_start
._unsigned
= value
,
1048 .range_end
._unsigned
= value
,
1051 g_ptr_array_foreach(enumeration_type
->entries
,
1052 check_ranges_overlap_unsigned
,
1054 if (!query
.overlaps
) {
1058 name
= g_quark_to_string(query
.mapping_name
);
1063 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1064 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1067 const char *name
= NULL
;
1068 struct range_overlap_query query
=
1069 (struct range_overlap_query
) {
1070 .range_start
._signed
= value
,
1071 .range_end
._signed
= value
,
1074 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1076 if (!query
.overlaps
) {
1080 name
= g_quark_to_string(query
.mapping_name
);
1085 int bt_ctf_field_type_enumeration_get_mapping_count(
1086 struct bt_ctf_field_type
*type
)
1089 struct bt_ctf_field_type_enumeration
*enumeration
;
1091 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1096 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1098 ret
= (int) enumeration
->entries
->len
;
1104 struct enumeration_mapping
*get_enumeration_mapping(
1105 struct bt_ctf_field_type
*type
, int index
)
1107 struct enumeration_mapping
*mapping
= NULL
;
1108 struct bt_ctf_field_type_enumeration
*enumeration
;
1110 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1112 if (index
>= enumeration
->entries
->len
) {
1116 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1121 int bt_ctf_field_type_enumeration_get_mapping(
1122 struct bt_ctf_field_type
*type
, int index
,
1123 const char **string
, int64_t *range_start
, int64_t *range_end
)
1125 struct enumeration_mapping
*mapping
;
1128 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1129 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1134 mapping
= get_enumeration_mapping(type
, index
);
1140 *string
= g_quark_to_string(mapping
->string
);
1141 *range_start
= mapping
->range_start
._signed
;
1142 *range_end
= mapping
->range_end
._signed
;
1147 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1148 struct bt_ctf_field_type
*type
, int index
,
1149 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1151 struct enumeration_mapping
*mapping
;
1154 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1155 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1160 mapping
= get_enumeration_mapping(type
, index
);
1166 *string
= g_quark_to_string(mapping
->string
);
1167 *range_start
= mapping
->range_start
._unsigned
;
1168 *range_end
= mapping
->range_end
._unsigned
;
1173 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1174 struct bt_ctf_field_type
*type
, const char *name
)
1177 struct bt_ctf_field_type_enumeration
*enumeration
;
1180 if (!type
|| !name
||
1181 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1186 name_quark
= g_quark_try_string(name
);
1192 enumeration
= container_of(type
,
1193 struct bt_ctf_field_type_enumeration
, parent
);
1194 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1195 struct enumeration_mapping
*mapping
=
1196 get_enumeration_mapping(type
, i
);
1198 if (mapping
->string
== name_quark
) {
1209 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1210 struct bt_ctf_field_type
*type
, int64_t value
)
1212 struct bt_ctf_field_type_enumeration
*enumeration
;
1215 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1220 enumeration
= container_of(type
,
1221 struct bt_ctf_field_type_enumeration
, parent
);
1222 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1223 struct enumeration_mapping
*mapping
=
1224 get_enumeration_mapping(type
, i
);
1226 if (value
>= mapping
->range_start
._signed
&&
1227 value
<= mapping
->range_end
._signed
) {
1238 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1239 struct bt_ctf_field_type
*type
, uint64_t value
)
1241 struct bt_ctf_field_type_enumeration
*enumeration
;
1244 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1249 enumeration
= container_of(type
,
1250 struct bt_ctf_field_type_enumeration
, parent
);
1251 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1252 struct enumeration_mapping
*mapping
=
1253 get_enumeration_mapping(type
, i
);
1255 if (value
>= mapping
->range_start
._unsigned
&&
1256 value
<= mapping
->range_end
._unsigned
) {
1267 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1269 struct bt_ctf_field_type_floating_point
*floating_point
=
1270 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1272 if (!floating_point
) {
1276 floating_point
->declaration
.sign
= &floating_point
->sign
;
1277 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1278 floating_point
->declaration
.exp
= &floating_point
->exp
;
1279 floating_point
->sign
.len
= 1;
1280 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1281 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1282 floating_point
->declaration
.exp
->len
=
1283 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1284 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1285 floating_point
->sign
.p
.alignment
= 1;
1286 floating_point
->mantissa
.p
.alignment
= 1;
1287 floating_point
->exp
.p
.alignment
= 1;
1289 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1291 return floating_point
? &floating_point
->parent
: NULL
;
1294 int bt_ctf_field_type_floating_point_get_exponent_digits(
1295 struct bt_ctf_field_type
*type
)
1298 struct bt_ctf_field_type_floating_point
*floating_point
;
1300 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1305 floating_point
= container_of(type
,
1306 struct bt_ctf_field_type_floating_point
, parent
);
1307 ret
= (int) floating_point
->declaration
.exp
->len
;
1312 int bt_ctf_field_type_floating_point_set_exponent_digits(
1313 struct bt_ctf_field_type
*type
,
1314 unsigned int exponent_digits
)
1317 struct bt_ctf_field_type_floating_point
*floating_point
;
1319 if (!type
|| type
->frozen
||
1320 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1325 floating_point
= container_of(type
,
1326 struct bt_ctf_field_type_floating_point
, parent
);
1327 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1328 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1330 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1335 floating_point
->declaration
.exp
->len
= exponent_digits
;
1340 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1341 struct bt_ctf_field_type
*type
)
1344 struct bt_ctf_field_type_floating_point
*floating_point
;
1346 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1351 floating_point
= container_of(type
,
1352 struct bt_ctf_field_type_floating_point
, parent
);
1353 ret
= (int) floating_point
->mantissa
.len
+ 1;
1358 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1359 struct bt_ctf_field_type
*type
,
1360 unsigned int mantissa_digits
)
1363 struct bt_ctf_field_type_floating_point
*floating_point
;
1365 if (!type
|| type
->frozen
||
1366 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1371 floating_point
= container_of(type
,
1372 struct bt_ctf_field_type_floating_point
, parent
);
1374 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1375 (mantissa_digits
!= DBL_MANT_DIG
) &&
1376 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1381 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1386 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1388 struct bt_ctf_field_type_structure
*structure
=
1389 g_new0(struct bt_ctf_field_type_structure
, 1);
1395 structure
->parent
.declaration
= &structure
->declaration
.p
;
1396 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1397 structure
->fields
= g_ptr_array_new_with_free_func(
1398 (GDestroyNotify
)destroy_structure_field
);
1399 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1400 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1401 return &structure
->parent
;
1406 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1407 struct bt_ctf_field_type
*field_type
,
1408 const char *field_name
)
1411 struct bt_ctf_field_type_structure
*structure
;
1414 * TODO: check that `field_type` does not contain `type`,
1417 if (!type
|| !field_type
|| type
->frozen
||
1418 bt_ctf_validate_identifier(field_name
) ||
1419 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
) ||
1420 type
== field_type
) {
1425 structure
= container_of(type
,
1426 struct bt_ctf_field_type_structure
, parent
);
1427 if (add_structure_field(structure
->fields
,
1428 structure
->field_name_to_index
, field_type
, field_name
)) {
1436 int bt_ctf_field_type_structure_get_field_count(
1437 struct bt_ctf_field_type
*type
)
1440 struct bt_ctf_field_type_structure
*structure
;
1442 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1447 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1449 ret
= (int) structure
->fields
->len
;
1454 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1455 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1458 struct bt_ctf_field_type_structure
*structure
;
1459 struct structure_field
*field
;
1462 if (!type
|| index
< 0 ||
1463 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1468 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1470 if (index
>= structure
->fields
->len
) {
1475 field
= g_ptr_array_index(structure
->fields
, index
);
1477 *field_type
= field
->type
;
1478 bt_get(field
->type
);
1481 *field_name
= g_quark_to_string(field
->name
);
1487 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1488 struct bt_ctf_field_type
*type
,
1493 struct structure_field
*field
;
1494 struct bt_ctf_field_type_structure
*structure
;
1495 struct bt_ctf_field_type
*field_type
= NULL
;
1497 if (!type
|| !name
) {
1501 name_quark
= g_quark_try_string(name
);
1506 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1508 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1509 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1513 field
= structure
->fields
->pdata
[index
];
1514 field_type
= field
->type
;
1520 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1521 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1523 struct bt_ctf_field_type_variant
*variant
= NULL
;
1525 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1529 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1534 variant
->parent
.declaration
= &variant
->declaration
.p
;
1535 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1536 variant
->tag_name
= g_string_new(tag_name
);
1537 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1538 variant
->fields
= g_ptr_array_new_with_free_func(
1539 (GDestroyNotify
) destroy_structure_field
);
1542 variant
->tag
= container_of(enum_tag
,
1543 struct bt_ctf_field_type_enumeration
, parent
);
1546 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1547 /* A variant's alignment is undefined */
1548 variant
->parent
.declaration
->alignment
= 0;
1549 return &variant
->parent
;
1554 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1555 struct bt_ctf_field_type
*type
)
1557 struct bt_ctf_field_type_variant
*variant
;
1558 struct bt_ctf_field_type
*tag_type
= NULL
;
1560 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1564 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1565 if (!variant
->tag
) {
1569 tag_type
= &variant
->tag
->parent
;
1575 const char *bt_ctf_field_type_variant_get_tag_name(
1576 struct bt_ctf_field_type
*type
)
1578 struct bt_ctf_field_type_variant
*variant
;
1579 const char *tag_name
= NULL
;
1581 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1585 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1586 if (variant
->tag_name
->len
== 0) {
1590 tag_name
= variant
->tag_name
->str
;
1595 int bt_ctf_field_type_variant_set_tag_name(
1596 struct bt_ctf_field_type
*type
, const char *name
)
1599 struct bt_ctf_field_type_variant
*variant
;
1601 if (!type
|| type
->frozen
||
1602 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1603 bt_ctf_validate_identifier(name
)) {
1608 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1609 g_string_assign(variant
->tag_name
, name
);
1614 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1615 struct bt_ctf_field_type
*field_type
,
1616 const char *field_name
)
1620 struct bt_ctf_field_type_variant
*variant
;
1621 GQuark field_name_quark
= g_quark_from_string(field_name
);
1624 * TODO: check that `field_type` does not contain `type`,
1627 if (!type
|| !field_type
|| type
->frozen
||
1628 bt_ctf_validate_identifier(field_name
) ||
1629 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1630 type
== field_type
) {
1635 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1637 /* The user has explicitly provided a tag; validate against it. */
1641 /* Make sure this name is present in the enum tag */
1642 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1643 struct enumeration_mapping
*mapping
=
1644 g_ptr_array_index(variant
->tag
->entries
, i
);
1646 if (mapping
->string
== field_name_quark
) {
1653 /* Validation failed */
1659 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1660 field_type
, field_name
)) {
1668 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1669 struct bt_ctf_field_type
*type
,
1670 const char *field_name
)
1674 struct structure_field
*field
;
1675 struct bt_ctf_field_type_variant
*variant
;
1676 struct bt_ctf_field_type
*field_type
= NULL
;
1678 if (!type
|| !field_name
) {
1682 name_quark
= g_quark_try_string(field_name
);
1687 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1688 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1689 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1693 field
= g_ptr_array_index(variant
->fields
, index
);
1694 field_type
= field
->type
;
1700 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1701 struct bt_ctf_field_type
*type
,
1702 struct bt_ctf_field
*tag
)
1704 const char *enum_value
;
1705 struct bt_ctf_field_type
*field_type
= NULL
;
1707 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1711 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1716 /* Already increments field_type's reference count */
1717 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1723 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1726 struct bt_ctf_field_type_variant
*variant
;
1728 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1733 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1735 ret
= (int) variant
->fields
->len
;
1741 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1742 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1745 struct bt_ctf_field_type_variant
*variant
;
1746 struct structure_field
*field
;
1749 if (!type
|| index
< 0 ||
1750 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1755 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1757 if (index
>= variant
->fields
->len
) {
1762 field
= g_ptr_array_index(variant
->fields
, index
);
1764 *field_type
= field
->type
;
1765 bt_get(field
->type
);
1768 *field_name
= g_quark_to_string(field
->name
);
1774 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1775 struct bt_ctf_field_type
*element_type
,
1776 unsigned int length
)
1778 struct bt_ctf_field_type_array
*array
= NULL
;
1780 if (!element_type
|| length
== 0) {
1784 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1789 array
->parent
.declaration
= &array
->declaration
.p
;
1790 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1792 bt_get(element_type
);
1793 array
->element_type
= element_type
;
1794 array
->length
= length
;
1795 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1796 return &array
->parent
;
1801 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1802 struct bt_ctf_field_type
*type
)
1804 struct bt_ctf_field_type
*ret
= NULL
;
1805 struct bt_ctf_field_type_array
*array
;
1807 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1811 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1812 ret
= array
->element_type
;
1819 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1820 struct bt_ctf_field_type
*element_type
)
1823 struct bt_ctf_field_type_array
*array
;
1825 if (!type
|| !element_type
||
1826 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1831 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1833 if (array
->element_type
) {
1834 BT_PUT(array
->element_type
);
1837 array
->element_type
= element_type
;
1838 bt_get(array
->element_type
);
1844 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1847 struct bt_ctf_field_type_array
*array
;
1849 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1854 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1855 ret
= (int64_t) array
->length
;
1860 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1861 struct bt_ctf_field_type
*element_type
,
1862 const char *length_field_name
)
1864 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1866 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1870 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1875 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1876 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1877 bt_get(element_type
);
1878 sequence
->element_type
= element_type
;
1879 sequence
->length_field_name
= g_string_new(length_field_name
);
1880 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1881 return &sequence
->parent
;
1886 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1887 struct bt_ctf_field_type
*type
)
1889 struct bt_ctf_field_type
*ret
= NULL
;
1890 struct bt_ctf_field_type_sequence
*sequence
;
1892 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1896 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1898 ret
= sequence
->element_type
;
1905 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1906 struct bt_ctf_field_type
*element_type
)
1909 struct bt_ctf_field_type_sequence
*sequence
;
1911 if (!type
|| !element_type
||
1912 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1917 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1919 if (sequence
->element_type
) {
1920 BT_PUT(sequence
->element_type
);
1923 sequence
->element_type
= element_type
;
1924 bt_get(sequence
->element_type
);
1930 const char *bt_ctf_field_type_sequence_get_length_field_name(
1931 struct bt_ctf_field_type
*type
)
1933 const char *ret
= NULL
;
1934 struct bt_ctf_field_type_sequence
*sequence
;
1936 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1940 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1942 ret
= sequence
->length_field_name
->str
;
1947 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1949 struct bt_ctf_field_type_string
*string
=
1950 g_new0(struct bt_ctf_field_type_string
, 1);
1956 string
->parent
.declaration
= &string
->declaration
.p
;
1957 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
1958 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1959 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1960 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1961 return &string
->parent
;
1964 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1965 struct bt_ctf_field_type
*type
)
1967 struct bt_ctf_field_type_string
*string
;
1968 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1970 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
1974 string
= container_of(type
, struct bt_ctf_field_type_string
,
1976 ret
= string
->declaration
.encoding
;
1981 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1982 enum bt_ctf_string_encoding encoding
)
1985 struct bt_ctf_field_type_string
*string
;
1987 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
1988 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
1989 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
1994 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1995 string
->declaration
.encoding
= encoding
;
2000 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2003 enum bt_ctf_type_id type_id
;
2011 ret
= (int) type
->declaration
->alignment
;
2015 type_id
= bt_ctf_field_type_get_type_id(type
);
2017 case BT_CTF_TYPE_ID_SEQUENCE
:
2019 struct bt_ctf_field_type
*element
=
2020 bt_ctf_field_type_sequence_get_element_type(type
);
2027 ret
= bt_ctf_field_type_get_alignment(element
);
2031 case BT_CTF_TYPE_ID_ARRAY
:
2033 struct bt_ctf_field_type
*element
=
2034 bt_ctf_field_type_array_get_element_type(type
);
2041 ret
= bt_ctf_field_type_get_alignment(element
);
2045 case BT_CTF_TYPE_ID_STRUCT
:
2047 int i
, element_count
;
2049 element_count
= bt_ctf_field_type_structure_get_field_count(
2051 if (element_count
< 0) {
2052 ret
= element_count
;
2056 for (i
= 0; i
< element_count
; i
++) {
2057 struct bt_ctf_field_type
*field
;
2058 int field_alignment
;
2060 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2067 field_alignment
= bt_ctf_field_type_get_alignment(
2070 if (field_alignment
< 0) {
2071 ret
= field_alignment
;
2075 type
->declaration
->alignment
= MAX(field_alignment
,
2076 type
->declaration
->alignment
);
2078 ret
= (int) type
->declaration
->alignment
;
2081 case BT_CTF_TYPE_ID_UNKNOWN
:
2085 ret
= (int) type
->declaration
->alignment
;
2093 int is_power_of_two(unsigned int value
)
2095 return ((value
& (value
- 1)) == 0) && value
> 0;
2098 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2099 unsigned int alignment
)
2102 enum bt_ctf_type_id type_id
;
2104 /* Alignment must be a power of two */
2105 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2110 type_id
= bt_ctf_field_type_get_type_id(type
);
2111 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2116 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2117 alignment
!= CHAR_BIT
) {
2122 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2123 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2124 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2125 /* Setting an alignment on these types makes no sense */
2130 type
->declaration
->alignment
= alignment
;
2136 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2137 struct bt_ctf_field_type
*type
)
2139 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2145 switch (type
->declaration
->id
) {
2146 case BT_CTF_TYPE_ID_INTEGER
:
2148 struct bt_ctf_field_type_integer
*integer
= container_of(
2149 type
, struct bt_ctf_field_type_integer
, parent
);
2150 ret
= integer
->user_byte_order
;
2153 case BT_CTF_TYPE_ID_ENUM
:
2155 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2156 type
, struct bt_ctf_field_type_enumeration
, parent
);
2157 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2160 case BT_CTF_TYPE_ID_FLOAT
:
2162 struct bt_ctf_field_type_floating_point
*floating_point
=
2164 struct bt_ctf_field_type_floating_point
,
2166 ret
= floating_point
->user_byte_order
;
2173 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2174 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2175 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2176 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2182 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2183 enum bt_ctf_byte_order byte_order
)
2186 int internal_byte_order
;
2187 enum bt_ctf_type_id type_id
;
2189 if (!type
|| type
->frozen
) {
2194 switch (byte_order
) {
2195 case BT_CTF_BYTE_ORDER_NATIVE
:
2196 /* Leave unset. Will be initialized by parent. */
2197 internal_byte_order
= 0;
2199 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2200 internal_byte_order
= LITTLE_ENDIAN
;
2202 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2203 case BT_CTF_BYTE_ORDER_NETWORK
:
2204 internal_byte_order
= BIG_ENDIAN
;
2211 type_id
= type
->declaration
->id
;
2212 if (set_byte_order_funcs
[type_id
]) {
2213 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2219 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2220 struct bt_ctf_field_type
*type
)
2223 return BT_CTF_TYPE_ID_UNKNOWN
;
2226 return type
->declaration
->id
;
2229 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2231 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2234 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2236 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2239 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2241 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2244 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2246 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2249 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2251 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2254 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2256 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2259 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2261 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2264 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2266 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2269 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2274 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2280 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2290 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2291 struct bt_ctf_field_type_variant
*variant
,
2294 struct bt_ctf_field_type
*type
= NULL
;
2295 GQuark field_name_quark
;
2297 struct structure_field
*field_entry
;
2298 struct range_overlap_query query
= {
2299 .range_start
._signed
= tag_value
,
2300 .range_end
._signed
= tag_value
,
2301 .mapping_name
= 0, .overlaps
= 0};
2303 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2305 if (!query
.overlaps
) {
2309 field_name_quark
= query
.mapping_name
;
2310 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2311 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2315 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2316 type
= field_entry
->type
;
2322 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2323 struct bt_ctf_field_type_variant
*variant
,
2326 struct bt_ctf_field_type
*type
= NULL
;
2327 GQuark field_name_quark
;
2329 struct structure_field
*field_entry
;
2330 struct range_overlap_query query
= {
2331 .range_start
._unsigned
= tag_value
,
2332 .range_end
._unsigned
= tag_value
,
2333 .mapping_name
= 0, .overlaps
= 0};
2335 g_ptr_array_foreach(variant
->tag
->entries
,
2336 check_ranges_overlap_unsigned
,
2338 if (!query
.overlaps
) {
2342 field_name_quark
= query
.mapping_name
;
2343 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2344 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2348 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2349 type
= field_entry
->type
;
2355 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2356 struct metadata_context
*context
)
2360 if (!type
|| !context
) {
2365 /* Make sure field type is valid before serializing it */
2366 ret
= bt_ctf_field_type_validate(type
);
2372 ret
= type
->serialize(type
, context
);
2378 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2385 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2386 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2387 set_byte_order_funcs
[type
->declaration
->id
](type
,
2392 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2394 struct bt_ctf_field_type
*copy
= NULL
;
2400 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2406 int bt_ctf_field_type_structure_get_field_name_index(
2407 struct bt_ctf_field_type
*type
, const char *name
)
2412 struct bt_ctf_field_type_structure
*structure
;
2414 if (!type
|| !name
||
2415 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2420 name_quark
= g_quark_try_string(name
);
2426 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2428 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2429 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2439 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2440 struct bt_ctf_field_type
*field
, int index
)
2443 struct bt_ctf_field_type_structure
*structure
;
2445 if (!type
|| !field
||
2446 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2451 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2453 if (index
< 0 || index
>= structure
->fields
->len
) {
2459 bt_put(((struct structure_field
*)
2460 g_ptr_array_index(structure
->fields
, index
))->type
);
2461 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2468 int bt_ctf_field_type_variant_get_field_name_index(
2469 struct bt_ctf_field_type
*type
, const char *name
)
2474 struct bt_ctf_field_type_variant
*variant
;
2476 if (!type
|| !name
||
2477 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2482 name_quark
= g_quark_try_string(name
);
2488 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2490 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2491 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2501 int bt_ctf_field_type_sequence_set_length_field_path(
2502 struct bt_ctf_field_type
*type
,
2503 struct bt_ctf_field_path
*path
)
2506 struct bt_ctf_field_type_sequence
*sequence
;
2508 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2509 BT_CTF_TYPE_ID_SEQUENCE
) {
2514 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2517 BT_MOVE(sequence
->length_field_path
, path
);
2523 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2524 struct bt_ctf_field_path
*path
)
2527 struct bt_ctf_field_type_variant
*variant
;
2529 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2530 BT_CTF_TYPE_ID_VARIANT
) {
2535 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2538 BT_MOVE(variant
->tag_field_path
, path
);
2544 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2545 struct bt_ctf_field_type
*tag
)
2548 struct bt_ctf_field_type_variant
*variant
;
2550 if (!type
|| !tag
||
2551 bt_ctf_field_type_get_type_id(tag
) !=
2552 BT_CTF_TYPE_ID_ENUM
) {
2557 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2561 bt_put(&variant
->tag
->parent
);
2563 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2570 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2571 struct bt_ctf_field_type
*field
, int index
)
2574 struct bt_ctf_field_type_variant
*variant
;
2576 if (!type
|| !field
||
2577 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2582 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2584 if (index
< 0 || index
>= variant
->fields
->len
) {
2590 bt_put(((struct structure_field
*)
2591 g_ptr_array_index(variant
->fields
, index
))->type
);
2592 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2599 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2601 struct bt_ctf_field_type_integer
*integer
=
2602 (struct bt_ctf_field_type_integer
*) type
;
2608 bt_put(integer
->mapped_clock
);
2613 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2615 struct bt_ctf_field_type_enumeration
*enumeration
=
2616 (struct bt_ctf_field_type_enumeration
*) type
;
2622 g_ptr_array_free(enumeration
->entries
, TRUE
);
2623 bt_put(enumeration
->container
);
2624 g_free(enumeration
);
2628 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2630 struct bt_ctf_field_type_floating_point
*floating_point
=
2631 (struct bt_ctf_field_type_floating_point
*) type
;
2637 g_free(floating_point
);
2641 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2643 struct bt_ctf_field_type_structure
*structure
=
2644 (struct bt_ctf_field_type_structure
*) type
;
2650 g_ptr_array_free(structure
->fields
, TRUE
);
2651 g_hash_table_destroy(structure
->field_name_to_index
);
2656 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2658 struct bt_ctf_field_type_variant
*variant
=
2659 (struct bt_ctf_field_type_variant
*) type
;
2665 g_ptr_array_free(variant
->fields
, TRUE
);
2666 g_hash_table_destroy(variant
->field_name_to_index
);
2667 g_string_free(variant
->tag_name
, TRUE
);
2668 bt_put(&variant
->tag
->parent
);
2669 BT_PUT(variant
->tag_field_path
);
2674 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2676 struct bt_ctf_field_type_array
*array
=
2677 (struct bt_ctf_field_type_array
*) type
;
2683 bt_put(array
->element_type
);
2688 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2690 struct bt_ctf_field_type_sequence
*sequence
=
2691 (struct bt_ctf_field_type_sequence
*) type
;
2697 bt_put(sequence
->element_type
);
2698 g_string_free(sequence
->length_field_name
, TRUE
);
2699 BT_PUT(sequence
->length_field_path
);
2704 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2706 struct bt_ctf_field_type_string
*string
=
2707 (struct bt_ctf_field_type_string
*) type
;
2717 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2723 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2725 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2726 type
, struct bt_ctf_field_type_integer
, parent
);
2728 if (integer_type
->mapped_clock
) {
2729 bt_ctf_clock_freeze(integer_type
->mapped_clock
);
2732 generic_field_type_freeze(type
);
2736 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2738 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2739 type
, struct bt_ctf_field_type_enumeration
, parent
);
2741 generic_field_type_freeze(type
);
2742 bt_ctf_field_type_freeze(enumeration_type
->container
);
2746 void freeze_structure_field(struct structure_field
*field
)
2748 bt_ctf_field_type_freeze(field
->type
);
2752 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2754 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2755 type
, struct bt_ctf_field_type_structure
, parent
);
2757 /* Cache the alignment */
2758 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2759 generic_field_type_freeze(type
);
2760 g_ptr_array_foreach(structure_type
->fields
,
2761 (GFunc
) freeze_structure_field
, NULL
);
2765 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2767 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2768 type
, struct bt_ctf_field_type_variant
, parent
);
2770 generic_field_type_freeze(type
);
2771 g_ptr_array_foreach(variant_type
->fields
,
2772 (GFunc
) freeze_structure_field
, NULL
);
2776 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2778 struct bt_ctf_field_type_array
*array_type
= container_of(
2779 type
, struct bt_ctf_field_type_array
, parent
);
2781 /* Cache the alignment */
2782 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2783 generic_field_type_freeze(type
);
2784 bt_ctf_field_type_freeze(array_type
->element_type
);
2788 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2790 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2791 type
, struct bt_ctf_field_type_sequence
, parent
);
2793 /* Cache the alignment */
2794 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2795 generic_field_type_freeze(type
);
2796 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2800 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2802 const char *encoding_string
;
2805 case BT_CTF_STRING_ENCODING_NONE
:
2806 encoding_string
= "none";
2808 case BT_CTF_STRING_ENCODING_ASCII
:
2809 encoding_string
= "ASCII";
2811 case BT_CTF_STRING_ENCODING_UTF8
:
2812 encoding_string
= "UTF8";
2815 encoding_string
= "unknown";
2819 return encoding_string
;
2823 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2825 const char *base_string
;
2828 case BT_CTF_INTEGER_BASE_DECIMAL
:
2829 base_string
= "decimal";
2831 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2832 base_string
= "hexadecimal";
2834 case BT_CTF_INTEGER_BASE_OCTAL
:
2835 base_string
= "octal";
2837 case BT_CTF_INTEGER_BASE_BINARY
:
2838 base_string
= "binary";
2841 base_string
= "unknown";
2849 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2850 struct metadata_context
*context
)
2852 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2853 struct bt_ctf_field_type_integer
, parent
);
2856 g_string_append_printf(context
->string
,
2857 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2858 integer
->declaration
.len
, type
->declaration
->alignment
,
2859 (integer
->declaration
.signedness
? "true" : "false"),
2860 get_encoding_string(integer
->declaration
.encoding
),
2861 get_integer_base_string(integer
->declaration
.base
),
2862 get_byte_order_string(integer
->declaration
.byte_order
));
2863 if (integer
->mapped_clock
) {
2864 const char *clock_name
= bt_ctf_clock_get_name(
2865 integer
->mapped_clock
);
2872 g_string_append_printf(context
->string
,
2873 "; map = clock.%s.value", clock_name
);
2876 g_string_append(context
->string
, "; }");
2882 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2883 struct metadata_context
*context
)
2887 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2888 struct bt_ctf_field_type_enumeration
, parent
);
2889 struct bt_ctf_field_type
*container_type
;
2890 int container_signed
;
2892 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2893 if (!container_type
) {
2898 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2899 if (container_signed
< 0) {
2900 ret
= container_signed
;
2901 goto error_put_container_type
;
2904 g_string_append(context
->string
, "enum : ");
2905 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2907 goto error_put_container_type
;
2910 g_string_append(context
->string
, " { ");
2911 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2912 struct enumeration_mapping
*mapping
=
2913 enumeration
->entries
->pdata
[entry
];
2915 if (container_signed
) {
2916 if (mapping
->range_start
._signed
==
2917 mapping
->range_end
._signed
) {
2918 g_string_append_printf(context
->string
,
2919 "\"%s\" = %" PRId64
,
2920 g_quark_to_string(mapping
->string
),
2921 mapping
->range_start
._signed
);
2923 g_string_append_printf(context
->string
,
2924 "\"%s\" = %" PRId64
" ... %" PRId64
,
2925 g_quark_to_string(mapping
->string
),
2926 mapping
->range_start
._signed
,
2927 mapping
->range_end
._signed
);
2930 if (mapping
->range_start
._unsigned
==
2931 mapping
->range_end
._unsigned
) {
2932 g_string_append_printf(context
->string
,
2933 "\"%s\" = %" PRIu64
,
2934 g_quark_to_string(mapping
->string
),
2935 mapping
->range_start
._unsigned
);
2937 g_string_append_printf(context
->string
,
2938 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2939 g_quark_to_string(mapping
->string
),
2940 mapping
->range_start
._unsigned
,
2941 mapping
->range_end
._unsigned
);
2945 g_string_append(context
->string
,
2946 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2950 if (context
->field_name
->len
) {
2951 g_string_append_printf(context
->string
, " %s",
2952 context
->field_name
->str
);
2953 g_string_assign(context
->field_name
, "");
2955 error_put_container_type
:
2956 bt_put(container_type
);
2962 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2963 struct metadata_context
*context
)
2965 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2966 type
, struct bt_ctf_field_type_floating_point
, parent
);
2968 g_string_append_printf(context
->string
,
2969 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2970 floating_point
->declaration
.exp
->len
,
2971 floating_point
->declaration
.mantissa
->len
+ 1,
2972 get_byte_order_string(floating_point
->declaration
.byte_order
),
2973 type
->declaration
->alignment
);
2978 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2979 struct metadata_context
*context
)
2982 unsigned int indent
;
2984 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2985 struct bt_ctf_field_type_structure
, parent
);
2986 GString
*structure_field_name
= context
->field_name
;
2988 context
->field_name
= g_string_new("");
2990 context
->current_indentation_level
++;
2991 g_string_append(context
->string
, "struct {\n");
2993 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2994 struct structure_field
*field
;
2996 for (indent
= 0; indent
< context
->current_indentation_level
;
2998 g_string_append_c(context
->string
, '\t');
3001 field
= structure
->fields
->pdata
[i
];
3002 g_string_assign(context
->field_name
,
3003 g_quark_to_string(field
->name
));
3004 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3009 if (context
->field_name
->len
) {
3010 g_string_append_printf(context
->string
, " %s",
3011 context
->field_name
->str
);
3013 g_string_append(context
->string
, ";\n");
3016 context
->current_indentation_level
--;
3017 for (indent
= 0; indent
< context
->current_indentation_level
;
3019 g_string_append_c(context
->string
, '\t');
3022 g_string_append_printf(context
->string
, "} align(%zu)",
3023 type
->declaration
->alignment
);
3025 g_string_free(context
->field_name
, TRUE
);
3026 context
->field_name
= structure_field_name
;
3031 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3032 struct metadata_context
*context
)
3035 unsigned int indent
;
3037 struct bt_ctf_field_type_variant
*variant
= container_of(
3038 type
, struct bt_ctf_field_type_variant
, parent
);
3039 GString
*variant_field_name
= context
->field_name
;
3041 context
->field_name
= g_string_new("");
3042 if (variant
->tag_name
->len
> 0) {
3043 g_string_append_printf(context
->string
,
3044 "variant <%s> {\n", variant
->tag_name
->str
);
3046 g_string_append(context
->string
, "variant {\n");
3049 context
->current_indentation_level
++;
3050 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3051 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3053 g_string_assign(context
->field_name
,
3054 g_quark_to_string(field
->name
));
3055 for (indent
= 0; indent
< context
->current_indentation_level
;
3057 g_string_append_c(context
->string
, '\t');
3060 g_string_assign(context
->field_name
,
3061 g_quark_to_string(field
->name
));
3062 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3067 if (context
->field_name
->len
) {
3068 g_string_append_printf(context
->string
, " %s;",
3069 context
->field_name
->str
);
3072 g_string_append_c(context
->string
, '\n');
3075 context
->current_indentation_level
--;
3076 for (indent
= 0; indent
< context
->current_indentation_level
;
3078 g_string_append_c(context
->string
, '\t');
3081 g_string_append(context
->string
, "}");
3083 g_string_free(context
->field_name
, TRUE
);
3084 context
->field_name
= variant_field_name
;
3089 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3090 struct metadata_context
*context
)
3093 struct bt_ctf_field_type_array
*array
= container_of(type
,
3094 struct bt_ctf_field_type_array
, parent
);
3096 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3101 if (context
->field_name
->len
) {
3102 g_string_append_printf(context
->string
, " %s[%u]",
3103 context
->field_name
->str
, array
->length
);
3104 g_string_assign(context
->field_name
, "");
3106 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3113 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3114 struct metadata_context
*context
)
3117 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3118 type
, struct bt_ctf_field_type_sequence
, parent
);
3120 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3125 if (context
->field_name
->len
) {
3126 g_string_append_printf(context
->string
, " %s[%s]",
3127 context
->field_name
->str
,
3128 sequence
->length_field_name
->str
);
3129 g_string_assign(context
->field_name
, "");
3131 g_string_append_printf(context
->string
, "[%s]",
3132 sequence
->length_field_name
->str
);
3139 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3140 struct metadata_context
*context
)
3142 struct bt_ctf_field_type_string
*string
= container_of(
3143 type
, struct bt_ctf_field_type_string
, parent
);
3145 g_string_append_printf(context
->string
,
3146 "string { encoding = %s; }",
3147 get_encoding_string(string
->declaration
.encoding
));
3152 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3153 enum bt_ctf_byte_order ret
;
3155 switch (byte_order
) {
3156 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3158 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3160 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3162 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3164 case BT_CTF_BYTE_ORDER_NETWORK
:
3165 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3167 case BT_CTF_BYTE_ORDER_NATIVE
:
3168 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3171 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3179 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3180 int byte_order
, int set_native
)
3182 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3183 struct bt_ctf_field_type_integer
, parent
);
3186 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3188 * User byte order is native, so we can set
3189 * the real byte order.
3191 integer_type
->declaration
.byte_order
=
3195 integer_type
->user_byte_order
=
3196 get_ctf_ir_byte_order(byte_order
);
3197 integer_type
->declaration
.byte_order
= byte_order
;
3202 void bt_ctf_field_type_enumeration_set_byte_order(
3203 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3205 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3206 struct bt_ctf_field_type_enumeration
, parent
);
3208 /* Safe to assume that container is an integer */
3209 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3210 byte_order
, set_native
);
3214 void bt_ctf_field_type_floating_point_set_byte_order(
3215 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3217 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3218 container_of(type
, struct bt_ctf_field_type_floating_point
,
3222 if (floating_point_type
->user_byte_order
==
3223 BT_CTF_BYTE_ORDER_NATIVE
) {
3225 * User byte order is native, so we can set
3226 * the real byte order.
3228 floating_point_type
->declaration
.byte_order
=
3230 floating_point_type
->sign
.byte_order
=
3232 floating_point_type
->mantissa
.byte_order
=
3234 floating_point_type
->exp
.byte_order
=
3238 floating_point_type
->user_byte_order
=
3239 get_ctf_ir_byte_order(byte_order
);
3240 floating_point_type
->declaration
.byte_order
= byte_order
;
3241 floating_point_type
->sign
.byte_order
= byte_order
;
3242 floating_point_type
->mantissa
.byte_order
= byte_order
;
3243 floating_point_type
->exp
.byte_order
= byte_order
;
3248 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3249 int byte_order
, int set_native
)
3252 struct bt_ctf_field_type_structure
*structure_type
=
3253 container_of(type
, struct bt_ctf_field_type_structure
,
3256 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3257 struct structure_field
*field
= g_ptr_array_index(
3258 structure_type
->fields
, i
);
3259 struct bt_ctf_field_type
*field_type
= field
->type
;
3261 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3262 set_byte_order_funcs
[field_type
->declaration
->id
](
3263 field_type
, byte_order
, set_native
);
3269 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3270 int byte_order
, int set_native
)
3273 struct bt_ctf_field_type_variant
*variant_type
=
3274 container_of(type
, struct bt_ctf_field_type_variant
,
3277 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3278 struct structure_field
*field
= g_ptr_array_index(
3279 variant_type
->fields
, i
);
3280 struct bt_ctf_field_type
*field_type
= field
->type
;
3282 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3283 set_byte_order_funcs
[field_type
->declaration
->id
](
3284 field_type
, byte_order
, set_native
);
3290 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3291 int byte_order
, int set_native
)
3293 struct bt_ctf_field_type_array
*array_type
=
3294 container_of(type
, struct bt_ctf_field_type_array
,
3297 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3298 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3299 array_type
->element_type
, byte_order
, set_native
);
3304 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3305 int byte_order
, int set_native
)
3307 struct bt_ctf_field_type_sequence
*sequence_type
=
3308 container_of(type
, struct bt_ctf_field_type_sequence
,
3311 if (set_byte_order_funcs
[
3312 sequence_type
->element_type
->declaration
->id
]) {
3313 set_byte_order_funcs
[
3314 sequence_type
->element_type
->declaration
->id
](
3315 sequence_type
->element_type
, byte_order
, set_native
);
3320 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3321 struct bt_ctf_field_type
*type
)
3323 struct bt_ctf_field_type
*copy
;
3324 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3326 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3327 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3332 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3334 copy_integer
->declaration
= integer
->declaration
;
3335 if (integer
->mapped_clock
) {
3336 bt_get(integer
->mapped_clock
);
3337 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3340 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3347 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3348 struct bt_ctf_field_type
*type
)
3351 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3352 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3354 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3357 /* Copy the source enumeration's container */
3358 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3359 if (!copy_container
) {
3363 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3367 copy_enumeration
= container_of(copy
,
3368 struct bt_ctf_field_type_enumeration
, parent
);
3370 /* Copy all enumaration entries */
3371 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3372 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3373 enumeration
->entries
, i
);
3374 struct enumeration_mapping
* copy_mapping
= g_new0(
3375 struct enumeration_mapping
, 1);
3377 if (!copy_mapping
) {
3381 *copy_mapping
= *mapping
;
3382 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3385 copy_enumeration
->declaration
= enumeration
->declaration
;
3387 bt_put(copy_container
);
3390 bt_put(copy_container
);
3396 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3397 struct bt_ctf_field_type
*type
)
3399 struct bt_ctf_field_type
*copy
;
3400 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3402 floating_point
= container_of(type
,
3403 struct bt_ctf_field_type_floating_point
, parent
);
3404 copy
= bt_ctf_field_type_floating_point_create();
3409 copy_float
= container_of(copy
,
3410 struct bt_ctf_field_type_floating_point
, parent
);
3411 copy_float
->declaration
= floating_point
->declaration
;
3412 copy_float
->sign
= floating_point
->sign
;
3413 copy_float
->mantissa
= floating_point
->mantissa
;
3414 copy_float
->exp
= floating_point
->exp
;
3415 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3416 copy_float
->declaration
.sign
= ©_float
->sign
;
3417 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3418 copy_float
->declaration
.exp
= ©_float
->exp
;
3424 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3425 struct bt_ctf_field_type
*type
)
3428 GHashTableIter iter
;
3429 gpointer key
, value
;
3430 struct bt_ctf_field_type
*copy
;
3431 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3433 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3435 copy
= bt_ctf_field_type_structure_create();
3440 copy_structure
= container_of(copy
,
3441 struct bt_ctf_field_type_structure
, parent
);
3443 /* Copy field_name_to_index */
3444 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3445 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3446 g_hash_table_insert(copy_structure
->field_name_to_index
,
3450 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3451 struct structure_field
*entry
, *copy_entry
;
3452 struct bt_ctf_field_type
*copy_field
;
3454 copy_entry
= g_new0(struct structure_field
, 1);
3459 entry
= g_ptr_array_index(structure
->fields
, i
);
3460 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3466 copy_entry
->name
= entry
->name
;
3467 copy_entry
->type
= copy_field
;
3468 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3471 copy_structure
->declaration
= structure
->declaration
;
3480 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3481 struct bt_ctf_field_type
*type
)
3484 GHashTableIter iter
;
3485 gpointer key
, value
;
3486 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3487 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3489 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3492 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3498 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3499 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3504 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3507 /* Copy field_name_to_index */
3508 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3509 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3510 g_hash_table_insert(copy_variant
->field_name_to_index
,
3514 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3515 struct structure_field
*entry
, *copy_entry
;
3516 struct bt_ctf_field_type
*copy_field
;
3518 copy_entry
= g_new0(struct structure_field
, 1);
3523 entry
= g_ptr_array_index(variant
->fields
, i
);
3524 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3530 copy_entry
->name
= entry
->name
;
3531 copy_entry
->type
= copy_field
;
3532 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3535 copy_variant
->declaration
= variant
->declaration
;
3536 if (variant
->tag_field_path
) {
3537 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3538 variant
->tag_field_path
);
3539 if (!copy_variant
->tag_field_path
) {
3553 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3554 struct bt_ctf_field_type
*type
)
3556 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3557 struct bt_ctf_field_type_array
*array
, *copy_array
;
3559 array
= container_of(type
, struct bt_ctf_field_type_array
,
3561 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3562 if (!copy_element
) {
3566 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3571 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3573 copy_array
->declaration
= array
->declaration
;
3575 bt_put(copy_element
);
3580 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3581 struct bt_ctf_field_type
*type
)
3583 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3584 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3586 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3588 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3589 if (!copy_element
) {
3593 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3594 sequence
->length_field_name
->len
?
3595 sequence
->length_field_name
->str
: NULL
);
3600 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3602 copy_sequence
->declaration
= sequence
->declaration
;
3603 if (sequence
->length_field_path
) {
3604 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3605 sequence
->length_field_path
);
3606 if (!copy_sequence
->length_field_path
) {
3611 bt_put(copy_element
);
3619 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3620 struct bt_ctf_field_type
*type
)
3622 struct bt_ctf_field_type
*copy
;
3623 struct bt_ctf_field_type_string
*string
, *copy_string
;
3625 copy
= bt_ctf_field_type_string_create();
3630 string
= container_of(type
, struct bt_ctf_field_type_string
,
3632 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3634 copy_string
->declaration
= string
->declaration
;
3640 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3641 struct bt_ctf_field_type
*type_b
)
3644 struct bt_ctf_field_type_integer
*integer_a
;
3645 struct bt_ctf_field_type_integer
*integer_b
;
3646 struct declaration_integer
*decl_a
;
3647 struct declaration_integer
*decl_b
;
3649 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3651 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3653 decl_a
= &integer_a
->declaration
;
3654 decl_b
= &integer_b
->declaration
;
3657 if (decl_a
->len
!= decl_b
->len
) {
3662 * Compare user byte orders only, not the cached,
3665 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3670 if (decl_a
->signedness
!= decl_b
->signedness
) {
3675 if (decl_a
->base
!= decl_b
->base
) {
3680 if (decl_a
->encoding
!= decl_b
->encoding
) {
3685 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3697 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3698 struct bt_ctf_field_type
*type_b
)
3701 struct bt_ctf_field_type_floating_point
*float_a
;
3702 struct bt_ctf_field_type_floating_point
*float_b
;
3704 float_a
= container_of(type_a
,
3705 struct bt_ctf_field_type_floating_point
, parent
);
3706 float_b
= container_of(type_b
,
3707 struct bt_ctf_field_type_floating_point
, parent
);
3710 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3714 /* Exponent length */
3715 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3719 /* Mantissa length */
3720 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3725 * Compare user byte orders only, not the cached,
3728 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3740 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3741 struct enumeration_mapping
*mapping_b
)
3746 if (mapping_a
->string
!= mapping_b
->string
) {
3751 if (mapping_a
->range_start
._unsigned
!=
3752 mapping_b
->range_start
._unsigned
) {
3757 if (mapping_a
->range_end
._unsigned
!=
3758 mapping_b
->range_end
._unsigned
) {
3770 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3771 struct bt_ctf_field_type
*type_b
)
3775 struct bt_ctf_field_type_enumeration
*enum_a
;
3776 struct bt_ctf_field_type_enumeration
*enum_b
;
3778 enum_a
= container_of(type_a
,
3779 struct bt_ctf_field_type_enumeration
, parent
);
3780 enum_b
= container_of(type_b
,
3781 struct bt_ctf_field_type_enumeration
, parent
);
3783 /* Container field type */
3784 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3792 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3796 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3797 struct enumeration_mapping
*mapping_a
=
3798 g_ptr_array_index(enum_a
->entries
, i
);
3799 struct enumeration_mapping
*mapping_b
=
3800 g_ptr_array_index(enum_b
->entries
, i
);
3802 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3815 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3816 struct bt_ctf_field_type
*type_b
)
3819 struct bt_ctf_field_type_string
*string_a
;
3820 struct bt_ctf_field_type_string
*string_b
;
3822 string_a
= container_of(type_a
,
3823 struct bt_ctf_field_type_string
, parent
);
3824 string_b
= container_of(type_b
,
3825 struct bt_ctf_field_type_string
, parent
);
3828 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3840 int compare_structure_fields(struct structure_field
*field_a
,
3841 struct structure_field
*field_b
)
3846 if (field_a
->name
!= field_b
->name
) {
3851 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3858 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3859 struct bt_ctf_field_type
*type_b
)
3863 struct bt_ctf_field_type_structure
*struct_a
;
3864 struct bt_ctf_field_type_structure
*struct_b
;
3866 struct_a
= container_of(type_a
,
3867 struct bt_ctf_field_type_structure
, parent
);
3868 struct_b
= container_of(type_b
,
3869 struct bt_ctf_field_type_structure
, parent
);
3872 if (bt_ctf_field_type_get_alignment(type_a
) !=
3873 bt_ctf_field_type_get_alignment(type_b
)) {
3878 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3882 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3883 struct structure_field
*field_a
=
3884 g_ptr_array_index(struct_a
->fields
, i
);
3885 struct structure_field
*field_b
=
3886 g_ptr_array_index(struct_b
->fields
, i
);
3888 ret
= compare_structure_fields(field_a
, field_b
);
3902 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3903 struct bt_ctf_field_type
*type_b
)
3907 struct bt_ctf_field_type_variant
*variant_a
;
3908 struct bt_ctf_field_type_variant
*variant_b
;
3910 variant_a
= container_of(type_a
,
3911 struct bt_ctf_field_type_variant
, parent
);
3912 variant_b
= container_of(type_b
,
3913 struct bt_ctf_field_type_variant
, parent
);
3916 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3921 ret
= bt_ctf_field_type_compare(
3922 (struct bt_ctf_field_type
*) variant_a
->tag
,
3923 (struct bt_ctf_field_type
*) variant_b
->tag
);
3931 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3935 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3936 struct structure_field
*field_a
=
3937 g_ptr_array_index(variant_a
->fields
, i
);
3938 struct structure_field
*field_b
=
3939 g_ptr_array_index(variant_b
->fields
, i
);
3941 ret
= compare_structure_fields(field_a
, field_b
);
3955 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3956 struct bt_ctf_field_type
*type_b
)
3959 struct bt_ctf_field_type_array
*array_a
;
3960 struct bt_ctf_field_type_array
*array_b
;
3962 array_a
= container_of(type_a
,
3963 struct bt_ctf_field_type_array
, parent
);
3964 array_b
= container_of(type_b
,
3965 struct bt_ctf_field_type_array
, parent
);
3968 if (array_a
->length
!= array_b
->length
) {
3973 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3974 array_b
->element_type
);
3981 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3982 struct bt_ctf_field_type
*type_b
)
3985 struct bt_ctf_field_type_sequence
*sequence_a
;
3986 struct bt_ctf_field_type_sequence
*sequence_b
;
3988 sequence_a
= container_of(type_a
,
3989 struct bt_ctf_field_type_sequence
, parent
);
3990 sequence_b
= container_of(type_b
,
3991 struct bt_ctf_field_type_sequence
, parent
);
3994 if (strcmp(sequence_a
->length_field_name
->str
,
3995 sequence_b
->length_field_name
->str
)) {
4000 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4001 sequence_b
->element_type
);
4007 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4008 struct bt_ctf_field_type
*type_b
)
4012 if (type_a
== type_b
) {
4013 /* Same reference: equal (even if both are NULL) */
4018 if (!type_a
|| !type_b
) {
4023 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4024 /* Different type IDs */
4028 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4029 /* Both have unknown type IDs */
4033 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4040 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4042 int field_count
= -1;
4043 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4046 case CTF_TYPE_STRUCT
:
4048 bt_ctf_field_type_structure_get_field_count(field_type
);
4050 case CTF_TYPE_VARIANT
:
4052 bt_ctf_field_type_variant_get_field_count(field_type
);
4054 case CTF_TYPE_ARRAY
:
4055 case CTF_TYPE_SEQUENCE
:
4057 * Array and sequence types always contain a single member
4058 * (the element type).
4070 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4071 struct bt_ctf_field_type
*field_type
, int index
)
4073 struct bt_ctf_field_type
*field
= NULL
;
4074 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4077 case CTF_TYPE_STRUCT
:
4078 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4081 case CTF_TYPE_VARIANT
:
4083 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4091 case CTF_TYPE_ARRAY
:
4092 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4094 case CTF_TYPE_SEQUENCE
:
4095 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4105 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4108 int field_index
= -1;
4109 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4112 case CTF_TYPE_STRUCT
:
4113 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4116 case CTF_TYPE_VARIANT
:
4117 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4127 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4128 struct bt_ctf_field_type
*type
)
4130 struct bt_ctf_field_path
*field_path
= NULL
;
4131 struct bt_ctf_field_type_variant
*variant
;
4133 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4137 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4139 field_path
= bt_get(variant
->tag_field_path
);
4144 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4145 struct bt_ctf_field_type
*type
)
4147 struct bt_ctf_field_path
*field_path
= NULL
;
4148 struct bt_ctf_field_type_sequence
*sequence
;
4150 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4154 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4156 field_path
= bt_get(sequence
->length_field_path
);