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-class.h>
34 #include <babeltrace/ctf-ir/clock-class-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 bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
562 struct bt_ctf_field_type_enumeration
*enumeration_type
)
564 return enumeration_type
->has_overlapping_ranges
;
568 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
572 struct bt_ctf_field_type
*child_type
= NULL
;
573 struct bt_ctf_field_type_variant
*variant
=
574 container_of(type
, struct bt_ctf_field_type_variant
,
577 int tag_mappings_count
;
579 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
584 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
591 bt_ctf_field_type_enumeration_get_mapping_count(
592 (struct bt_ctf_field_type
*) variant
->tag
);
594 if (tag_mappings_count
!= variant
->fields
->len
) {
599 for (i
= 0; i
< tag_mappings_count
; ++i
) {
601 struct bt_ctf_field_type
*ft
;
603 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
604 (struct bt_ctf_field_type
*) variant
->tag
,
614 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
624 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
625 if (field_count
< 0) {
630 for (i
= 0; i
< field_count
; ++i
) {
631 ret
= bt_ctf_field_type_variant_get_field(type
,
632 NULL
, &child_type
, i
);
637 ret
= bt_ctf_field_type_validate(child_type
);
652 * This function validates a given field type without considering
653 * where this field type is located. It only validates the properties
654 * of the given field type and the properties of its children if
658 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
661 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
669 /* Already marked as valid */
673 if (type_validate_funcs
[id
]) {
674 ret
= type_validate_funcs
[id
](type
);
677 if (!ret
&& type
->frozen
) {
678 /* Field type is valid */
686 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
688 struct bt_ctf_field_type_integer
*integer
=
689 g_new0(struct bt_ctf_field_type_integer
, 1);
691 if (!integer
|| size
== 0 || size
> 64) {
695 integer
->parent
.declaration
= &integer
->declaration
.p
;
696 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
697 integer
->declaration
.len
= size
;
698 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
699 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
700 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
701 return &integer
->parent
;
704 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
707 struct bt_ctf_field_type_integer
*integer
;
709 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
714 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
715 ret
= (int) integer
->declaration
.len
;
720 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
723 struct bt_ctf_field_type_integer
*integer
;
725 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
730 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
731 ret
= integer
->declaration
.signedness
;
736 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
740 struct bt_ctf_field_type_integer
*integer
;
742 if (!type
|| type
->frozen
||
743 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
748 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
749 integer
->declaration
.signedness
= !!is_signed
;
754 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
755 struct bt_ctf_field_type
*type
)
757 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
758 struct bt_ctf_field_type_integer
*integer
;
760 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
764 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
765 ret
= integer
->declaration
.base
;
770 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
771 enum bt_ctf_integer_base base
)
775 if (!type
|| type
->frozen
||
776 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
782 case BT_CTF_INTEGER_BASE_BINARY
:
783 case BT_CTF_INTEGER_BASE_OCTAL
:
784 case BT_CTF_INTEGER_BASE_DECIMAL
:
785 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
787 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
788 struct bt_ctf_field_type_integer
, parent
);
789 integer
->declaration
.base
= base
;
799 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
800 struct bt_ctf_field_type
*type
)
802 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
803 struct bt_ctf_field_type_integer
*integer
;
805 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
809 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
810 ret
= integer
->declaration
.encoding
;
815 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
816 enum bt_ctf_string_encoding encoding
)
819 struct bt_ctf_field_type_integer
*integer
;
821 if (!type
|| type
->frozen
||
822 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
823 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
824 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
829 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
830 integer
->declaration
.encoding
= encoding
;
835 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
836 struct bt_ctf_field_type
*type
)
838 struct bt_ctf_field_type_integer
*integer
;
839 struct bt_ctf_clock_class
*clock_class
= NULL
;
845 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
846 clock_class
= integer
->mapped_clock
;
852 int bt_ctf_field_type_integer_set_mapped_clock_class(
853 struct bt_ctf_field_type
*type
,
854 struct bt_ctf_clock_class
*clock_class
)
856 struct bt_ctf_field_type_integer
*integer
;
859 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
864 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
865 bt_put(integer
->mapped_clock
);
866 integer
->mapped_clock
= bt_get(clock_class
);
872 struct enumeration_mapping
*get_enumeration_mapping(
873 struct bt_ctf_field_type
*type
, int index
)
875 struct enumeration_mapping
*mapping
= NULL
;
876 struct bt_ctf_field_type_enumeration
*enumeration
;
878 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
880 if (index
>= enumeration
->entries
->len
) {
884 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
889 static void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
891 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
893 struct bt_ctf_field_type_enumeration_mapping_iterator
,
896 bt_put(&iter
->enumeration_type
->parent
);
900 int bt_ctf_field_type_enumeration_mapping_iterator_next(
901 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
903 struct bt_ctf_field_type_enumeration
*enumeration
;
904 struct bt_ctf_field_type
*type
;
907 enumeration
= iter
->enumeration_type
;
908 type
= &enumeration
->parent
;
909 len
= enumeration
->entries
->len
;
910 for (i
= iter
->index
+ 1; i
< len
; i
++) {
911 struct enumeration_mapping
*mapping
=
912 get_enumeration_mapping(type
, i
);
914 switch (iter
->kind
) {
915 case ITERATOR_BY_NAME
:
916 if (mapping
->string
== iter
->u
.name_quark
) {
921 case ITERATOR_BY_SIGNED_VALUE
:
923 int64_t value
= iter
->u
.signed_value
;
925 if (value
>= mapping
->range_start
._signed
&&
926 value
<= mapping
->range_end
._signed
) {
932 case ITERATOR_BY_UNSIGNED_VALUE
:
934 uint64_t value
= iter
->u
.unsigned_value
;
936 if (value
>= mapping
->range_start
._unsigned
&&
937 value
<= mapping
->range_end
._unsigned
) {
953 static struct bt_ctf_field_type_enumeration_mapping_iterator
*
954 bt_ctf_field_type_enumeration_find_mappings_kind(
955 struct bt_ctf_field_type
*type
,
956 enum bt_ctf_field_type_enumeration_mapping_iterator_kind kind
)
958 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
959 struct bt_ctf_field_type_enumeration
*enumeration_type
;
961 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
964 enumeration_type
= container_of(type
,
965 struct bt_ctf_field_type_enumeration
, parent
);
966 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
970 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
972 iter
->enumeration_type
= enumeration_type
;
978 struct bt_ctf_field_type_enumeration_mapping_iterator
*
979 bt_ctf_field_type_enumeration_find_mappings_by_name(
980 struct bt_ctf_field_type
*type
,
983 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
985 iter
= bt_ctf_field_type_enumeration_find_mappings_kind(
986 type
, ITERATOR_BY_NAME
);
990 iter
->u
.name_quark
= g_quark_try_string(name
);
991 if (!iter
->u
.name_quark
) {
994 /* Advance iterator to first entry, or leave index at -1. */
995 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
996 /* No entry found. */
1006 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1007 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1008 struct bt_ctf_field_type
*type
,
1011 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1013 iter
= bt_ctf_field_type_enumeration_find_mappings_kind(
1014 type
, ITERATOR_BY_SIGNED_VALUE
);
1018 if (bt_ctf_field_type_integer_get_signed(
1019 iter
->enumeration_type
->container
) != 1) {
1022 iter
->u
.signed_value
= value
;
1023 /* Advance iterator to first entry, or leave index at -1. */
1024 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1025 /* No entry found. */
1035 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1036 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1037 struct bt_ctf_field_type
*type
,
1040 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1042 iter
= bt_ctf_field_type_enumeration_find_mappings_kind(
1043 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1047 if (bt_ctf_field_type_integer_get_signed(
1048 iter
->enumeration_type
->container
) != 0) {
1051 iter
->u
.unsigned_value
= value
;
1052 /* Advance iterator to first entry, or leave index at -1. */
1053 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1054 /* No entry found. */
1064 int bt_ctf_field_type_enumeration_get_mapping_name(
1065 struct bt_ctf_field_type
*enum_field_type
,
1067 const char **mapping_name
)
1069 struct enumeration_mapping
*mapping
;
1071 if (!enum_field_type
|| index
< 0) {
1074 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1079 *mapping_name
= g_quark_to_string(mapping
->string
);
1084 int bt_ctf_field_type_enumeration_mapping_iterator_get_name(
1085 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1086 const char **mapping_name
)
1091 return bt_ctf_field_type_enumeration_get_mapping_name(
1092 &iter
->enumeration_type
->parent
, iter
->index
,
1096 int bt_ctf_field_type_enumeration_get_mapping_signed(
1097 struct bt_ctf_field_type
*enum_field_type
,
1099 const char **mapping_name
, int64_t *range_begin
,
1102 struct enumeration_mapping
*mapping
;
1104 if (!enum_field_type
|| index
< 0) {
1107 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1112 *mapping_name
= g_quark_to_string(mapping
->string
);
1115 *range_begin
= mapping
->range_start
._signed
;
1118 *range_end
= mapping
->range_end
._signed
;
1123 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1124 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1125 const char **mapping_name
, int64_t *range_begin
,
1131 return bt_ctf_field_type_enumeration_get_mapping_signed(
1132 &iter
->enumeration_type
->parent
, iter
->index
,
1133 mapping_name
, range_begin
, range_end
);
1136 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1137 struct bt_ctf_field_type
*enum_field_type
,
1139 const char **mapping_name
, uint64_t *range_begin
,
1140 uint64_t *range_end
)
1142 struct enumeration_mapping
*mapping
;
1144 if (!enum_field_type
|| index
< 0) {
1147 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1152 *mapping_name
= g_quark_to_string(mapping
->string
);
1155 *range_begin
= mapping
->range_start
._unsigned
;
1158 *range_end
= mapping
->range_end
._unsigned
;
1163 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1164 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1165 const char **mapping_name
, uint64_t *range_begin
,
1166 uint64_t *range_end
)
1171 return bt_ctf_field_type_enumeration_get_mapping_unsigned(
1172 &iter
->enumeration_type
->parent
, iter
->index
,
1173 mapping_name
, range_begin
, range_end
);
1176 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1177 struct bt_ctf_field_type
*integer_container_type
)
1179 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1181 if (!integer_container_type
) {
1185 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
1189 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1194 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
1195 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
1196 bt_get(integer_container_type
);
1197 enumeration
->container
= integer_container_type
;
1198 enumeration
->entries
= g_ptr_array_new_with_free_func(
1199 (GDestroyNotify
)destroy_enumeration_mapping
);
1200 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1201 return &enumeration
->parent
;
1203 g_free(enumeration
);
1207 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1208 struct bt_ctf_field_type
*type
)
1210 struct bt_ctf_field_type
*container_type
= NULL
;
1211 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1217 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
1221 enumeration_type
= container_of(type
,
1222 struct bt_ctf_field_type_enumeration
, parent
);
1223 container_type
= enumeration_type
->container
;
1224 bt_get(container_type
);
1226 return container_type
;
1229 int bt_ctf_field_type_enumeration_add_mapping(
1230 struct bt_ctf_field_type
*type
, const char *string
,
1231 int64_t range_start
, int64_t range_end
)
1234 GQuark mapping_name
;
1235 struct enumeration_mapping
*mapping
;
1236 struct bt_ctf_field_type_enumeration
*enumeration
;
1237 char *escaped_string
;
1239 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1241 (range_end
< range_start
)) {
1246 if (!string
|| strlen(string
) == 0) {
1251 escaped_string
= g_strescape(string
, NULL
);
1252 if (!escaped_string
) {
1257 mapping
= g_new(struct enumeration_mapping
, 1);
1262 mapping_name
= g_quark_from_string(escaped_string
);
1263 *mapping
= (struct enumeration_mapping
) {
1264 .range_start
._signed
= range_start
,
1265 .range_end
._signed
= range_end
,
1266 .string
= mapping_name
,
1268 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1270 g_ptr_array_add(enumeration
->entries
, mapping
);
1271 g_ptr_array_sort(enumeration
->entries
,
1272 (GCompareFunc
)compare_enumeration_mappings_signed
);
1274 free(escaped_string
);
1279 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1280 struct bt_ctf_field_type
*type
, const char *string
,
1281 uint64_t range_start
, uint64_t range_end
)
1284 GQuark mapping_name
;
1285 struct enumeration_mapping
*mapping
;
1286 struct bt_ctf_field_type_enumeration
*enumeration
;
1287 char *escaped_string
;
1289 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1291 (range_end
< range_start
)) {
1296 if (!string
|| strlen(string
) == 0) {
1301 escaped_string
= g_strescape(string
, NULL
);
1302 if (!escaped_string
) {
1307 mapping
= g_new(struct enumeration_mapping
, 1);
1312 mapping_name
= g_quark_from_string(escaped_string
);
1313 *mapping
= (struct enumeration_mapping
) {
1314 .range_start
._unsigned
= range_start
,
1315 .range_end
._unsigned
= range_end
,
1316 .string
= mapping_name
,
1318 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1320 g_ptr_array_add(enumeration
->entries
, mapping
);
1321 g_ptr_array_sort(enumeration
->entries
,
1322 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1324 free(escaped_string
);
1329 int bt_ctf_field_type_enumeration_get_mapping_count(
1330 struct bt_ctf_field_type
*type
)
1333 struct bt_ctf_field_type_enumeration
*enumeration
;
1335 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1340 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1342 ret
= (int) enumeration
->entries
->len
;
1347 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1349 struct bt_ctf_field_type_floating_point
*floating_point
=
1350 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1352 if (!floating_point
) {
1356 floating_point
->declaration
.sign
= &floating_point
->sign
;
1357 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1358 floating_point
->declaration
.exp
= &floating_point
->exp
;
1359 floating_point
->sign
.len
= 1;
1360 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1361 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1362 floating_point
->declaration
.exp
->len
=
1363 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1364 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1365 floating_point
->sign
.p
.alignment
= 1;
1366 floating_point
->mantissa
.p
.alignment
= 1;
1367 floating_point
->exp
.p
.alignment
= 1;
1369 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1371 return floating_point
? &floating_point
->parent
: NULL
;
1374 int bt_ctf_field_type_floating_point_get_exponent_digits(
1375 struct bt_ctf_field_type
*type
)
1378 struct bt_ctf_field_type_floating_point
*floating_point
;
1380 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1385 floating_point
= container_of(type
,
1386 struct bt_ctf_field_type_floating_point
, parent
);
1387 ret
= (int) floating_point
->declaration
.exp
->len
;
1392 int bt_ctf_field_type_floating_point_set_exponent_digits(
1393 struct bt_ctf_field_type
*type
,
1394 unsigned int exponent_digits
)
1397 struct bt_ctf_field_type_floating_point
*floating_point
;
1399 if (!type
|| type
->frozen
||
1400 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1405 floating_point
= container_of(type
,
1406 struct bt_ctf_field_type_floating_point
, parent
);
1407 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1408 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1410 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1415 floating_point
->declaration
.exp
->len
= exponent_digits
;
1420 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1421 struct bt_ctf_field_type
*type
)
1424 struct bt_ctf_field_type_floating_point
*floating_point
;
1426 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1431 floating_point
= container_of(type
,
1432 struct bt_ctf_field_type_floating_point
, parent
);
1433 ret
= (int) floating_point
->mantissa
.len
+ 1;
1438 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1439 struct bt_ctf_field_type
*type
,
1440 unsigned int mantissa_digits
)
1443 struct bt_ctf_field_type_floating_point
*floating_point
;
1445 if (!type
|| type
->frozen
||
1446 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1451 floating_point
= container_of(type
,
1452 struct bt_ctf_field_type_floating_point
, parent
);
1454 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1455 (mantissa_digits
!= DBL_MANT_DIG
) &&
1456 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1461 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1466 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1468 struct bt_ctf_field_type_structure
*structure
=
1469 g_new0(struct bt_ctf_field_type_structure
, 1);
1475 structure
->parent
.declaration
= &structure
->declaration
.p
;
1476 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1477 structure
->fields
= g_ptr_array_new_with_free_func(
1478 (GDestroyNotify
)destroy_structure_field
);
1479 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1480 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1481 return &structure
->parent
;
1486 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1487 struct bt_ctf_field_type
*field_type
,
1488 const char *field_name
)
1491 struct bt_ctf_field_type_structure
*structure
;
1494 * TODO: check that `field_type` does not contain `type`,
1497 if (!type
|| !field_type
|| type
->frozen
||
1498 bt_ctf_validate_identifier(field_name
) ||
1499 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
) ||
1500 type
== field_type
) {
1505 structure
= container_of(type
,
1506 struct bt_ctf_field_type_structure
, parent
);
1507 if (add_structure_field(structure
->fields
,
1508 structure
->field_name_to_index
, field_type
, field_name
)) {
1516 int bt_ctf_field_type_structure_get_field_count(
1517 struct bt_ctf_field_type
*type
)
1520 struct bt_ctf_field_type_structure
*structure
;
1522 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1527 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1529 ret
= (int) structure
->fields
->len
;
1534 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1535 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1538 struct bt_ctf_field_type_structure
*structure
;
1539 struct structure_field
*field
;
1542 if (!type
|| index
< 0 ||
1543 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1548 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1550 if (index
>= structure
->fields
->len
) {
1555 field
= g_ptr_array_index(structure
->fields
, index
);
1557 *field_type
= field
->type
;
1558 bt_get(field
->type
);
1561 *field_name
= g_quark_to_string(field
->name
);
1567 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1568 struct bt_ctf_field_type
*type
,
1573 struct structure_field
*field
;
1574 struct bt_ctf_field_type_structure
*structure
;
1575 struct bt_ctf_field_type
*field_type
= NULL
;
1577 if (!type
|| !name
) {
1581 name_quark
= g_quark_try_string(name
);
1586 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1588 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1589 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1593 field
= structure
->fields
->pdata
[index
];
1594 field_type
= field
->type
;
1600 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1601 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1603 struct bt_ctf_field_type_variant
*variant
= NULL
;
1605 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1609 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1614 variant
->parent
.declaration
= &variant
->declaration
.p
;
1615 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1616 variant
->tag_name
= g_string_new(tag_name
);
1617 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1618 variant
->fields
= g_ptr_array_new_with_free_func(
1619 (GDestroyNotify
) destroy_structure_field
);
1622 variant
->tag
= container_of(enum_tag
,
1623 struct bt_ctf_field_type_enumeration
, parent
);
1626 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1627 /* A variant's alignment is undefined */
1628 variant
->parent
.declaration
->alignment
= 0;
1629 return &variant
->parent
;
1634 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1635 struct bt_ctf_field_type
*type
)
1637 struct bt_ctf_field_type_variant
*variant
;
1638 struct bt_ctf_field_type
*tag_type
= NULL
;
1640 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1644 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1645 if (!variant
->tag
) {
1649 tag_type
= &variant
->tag
->parent
;
1655 const char *bt_ctf_field_type_variant_get_tag_name(
1656 struct bt_ctf_field_type
*type
)
1658 struct bt_ctf_field_type_variant
*variant
;
1659 const char *tag_name
= NULL
;
1661 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1665 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1666 if (variant
->tag_name
->len
== 0) {
1670 tag_name
= variant
->tag_name
->str
;
1675 int bt_ctf_field_type_variant_set_tag_name(
1676 struct bt_ctf_field_type
*type
, const char *name
)
1679 struct bt_ctf_field_type_variant
*variant
;
1681 if (!type
|| type
->frozen
||
1682 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1683 bt_ctf_validate_identifier(name
)) {
1688 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1689 g_string_assign(variant
->tag_name
, name
);
1694 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1695 struct bt_ctf_field_type
*field_type
,
1696 const char *field_name
)
1700 struct bt_ctf_field_type_variant
*variant
;
1701 GQuark field_name_quark
= g_quark_from_string(field_name
);
1704 * TODO: check that `field_type` does not contain `type`,
1707 if (!type
|| !field_type
|| type
->frozen
||
1708 bt_ctf_validate_identifier(field_name
) ||
1709 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1710 type
== field_type
) {
1715 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1717 /* The user has explicitly provided a tag; validate against it. */
1721 /* Make sure this name is present in the enum tag */
1722 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1723 struct enumeration_mapping
*mapping
=
1724 g_ptr_array_index(variant
->tag
->entries
, i
);
1726 if (mapping
->string
== field_name_quark
) {
1733 /* Validation failed */
1739 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1740 field_type
, field_name
)) {
1748 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1749 struct bt_ctf_field_type
*type
,
1750 const char *field_name
)
1754 struct structure_field
*field
;
1755 struct bt_ctf_field_type_variant
*variant
;
1756 struct bt_ctf_field_type
*field_type
= NULL
;
1758 if (!type
|| !field_name
) {
1762 name_quark
= g_quark_try_string(field_name
);
1767 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1768 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1769 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1773 field
= g_ptr_array_index(variant
->fields
, index
);
1774 field_type
= field
->type
;
1780 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1781 struct bt_ctf_field_type
*type
,
1782 struct bt_ctf_field
*tag
)
1784 const char *enum_value
;
1785 struct bt_ctf_field_type
*field_type
= NULL
;
1787 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1791 enum_value
= bt_ctf_field_enumeration_get_single_mapping_name(tag
);
1796 /* Already increments field_type's reference count */
1797 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1803 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1806 struct bt_ctf_field_type_variant
*variant
;
1808 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1813 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1815 ret
= (int) variant
->fields
->len
;
1821 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1822 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1825 struct bt_ctf_field_type_variant
*variant
;
1826 struct structure_field
*field
;
1829 if (!type
|| index
< 0 ||
1830 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1835 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1837 if (index
>= variant
->fields
->len
) {
1842 field
= g_ptr_array_index(variant
->fields
, index
);
1844 *field_type
= field
->type
;
1845 bt_get(field
->type
);
1848 *field_name
= g_quark_to_string(field
->name
);
1854 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1855 struct bt_ctf_field_type
*element_type
,
1856 unsigned int length
)
1858 struct bt_ctf_field_type_array
*array
= NULL
;
1860 if (!element_type
|| length
== 0) {
1864 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1869 array
->parent
.declaration
= &array
->declaration
.p
;
1870 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1872 bt_get(element_type
);
1873 array
->element_type
= element_type
;
1874 array
->length
= length
;
1875 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1876 return &array
->parent
;
1881 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1882 struct bt_ctf_field_type
*type
)
1884 struct bt_ctf_field_type
*ret
= NULL
;
1885 struct bt_ctf_field_type_array
*array
;
1887 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1891 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1892 ret
= array
->element_type
;
1899 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1900 struct bt_ctf_field_type
*element_type
)
1903 struct bt_ctf_field_type_array
*array
;
1905 if (!type
|| !element_type
||
1906 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1911 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1913 if (array
->element_type
) {
1914 BT_PUT(array
->element_type
);
1917 array
->element_type
= element_type
;
1918 bt_get(array
->element_type
);
1924 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1927 struct bt_ctf_field_type_array
*array
;
1929 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1934 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1935 ret
= (int64_t) array
->length
;
1940 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1941 struct bt_ctf_field_type
*element_type
,
1942 const char *length_field_name
)
1944 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1946 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1950 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1955 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1956 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1957 bt_get(element_type
);
1958 sequence
->element_type
= element_type
;
1959 sequence
->length_field_name
= g_string_new(length_field_name
);
1960 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1961 return &sequence
->parent
;
1966 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1967 struct bt_ctf_field_type
*type
)
1969 struct bt_ctf_field_type
*ret
= NULL
;
1970 struct bt_ctf_field_type_sequence
*sequence
;
1972 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1976 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1978 ret
= sequence
->element_type
;
1985 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1986 struct bt_ctf_field_type
*element_type
)
1989 struct bt_ctf_field_type_sequence
*sequence
;
1991 if (!type
|| !element_type
||
1992 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1997 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1999 if (sequence
->element_type
) {
2000 BT_PUT(sequence
->element_type
);
2003 sequence
->element_type
= element_type
;
2004 bt_get(sequence
->element_type
);
2010 const char *bt_ctf_field_type_sequence_get_length_field_name(
2011 struct bt_ctf_field_type
*type
)
2013 const char *ret
= NULL
;
2014 struct bt_ctf_field_type_sequence
*sequence
;
2016 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2020 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2022 ret
= sequence
->length_field_name
->str
;
2027 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2029 struct bt_ctf_field_type_string
*string
=
2030 g_new0(struct bt_ctf_field_type_string
, 1);
2036 string
->parent
.declaration
= &string
->declaration
.p
;
2037 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
2038 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2039 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2040 string
->parent
.declaration
->alignment
= CHAR_BIT
;
2041 return &string
->parent
;
2044 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2045 struct bt_ctf_field_type
*type
)
2047 struct bt_ctf_field_type_string
*string
;
2048 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2050 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
2054 string
= container_of(type
, struct bt_ctf_field_type_string
,
2056 ret
= string
->declaration
.encoding
;
2061 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2062 enum bt_ctf_string_encoding encoding
)
2065 struct bt_ctf_field_type_string
*string
;
2067 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2068 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2069 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2074 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2075 string
->declaration
.encoding
= encoding
;
2080 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2083 enum bt_ctf_type_id type_id
;
2091 ret
= (int) type
->declaration
->alignment
;
2095 type_id
= bt_ctf_field_type_get_type_id(type
);
2097 case BT_CTF_TYPE_ID_SEQUENCE
:
2099 struct bt_ctf_field_type
*element
=
2100 bt_ctf_field_type_sequence_get_element_type(type
);
2107 ret
= bt_ctf_field_type_get_alignment(element
);
2111 case BT_CTF_TYPE_ID_ARRAY
:
2113 struct bt_ctf_field_type
*element
=
2114 bt_ctf_field_type_array_get_element_type(type
);
2121 ret
= bt_ctf_field_type_get_alignment(element
);
2125 case BT_CTF_TYPE_ID_STRUCT
:
2127 int i
, element_count
;
2129 element_count
= bt_ctf_field_type_structure_get_field_count(
2131 if (element_count
< 0) {
2132 ret
= element_count
;
2136 for (i
= 0; i
< element_count
; i
++) {
2137 struct bt_ctf_field_type
*field
;
2138 int field_alignment
;
2140 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2147 field_alignment
= bt_ctf_field_type_get_alignment(
2150 if (field_alignment
< 0) {
2151 ret
= field_alignment
;
2155 type
->declaration
->alignment
= MAX(field_alignment
,
2156 type
->declaration
->alignment
);
2158 ret
= (int) type
->declaration
->alignment
;
2161 case BT_CTF_TYPE_ID_UNKNOWN
:
2165 ret
= (int) type
->declaration
->alignment
;
2173 int is_power_of_two(unsigned int value
)
2175 return ((value
& (value
- 1)) == 0) && value
> 0;
2178 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2179 unsigned int alignment
)
2182 enum bt_ctf_type_id type_id
;
2184 /* Alignment must be a power of two */
2185 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2190 type_id
= bt_ctf_field_type_get_type_id(type
);
2191 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2196 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2197 alignment
!= CHAR_BIT
) {
2202 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2203 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2204 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2205 /* Setting an alignment on these types makes no sense */
2210 type
->declaration
->alignment
= alignment
;
2216 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2217 struct bt_ctf_field_type
*type
)
2219 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2225 switch (type
->declaration
->id
) {
2226 case BT_CTF_TYPE_ID_INTEGER
:
2228 struct bt_ctf_field_type_integer
*integer
= container_of(
2229 type
, struct bt_ctf_field_type_integer
, parent
);
2230 ret
= integer
->user_byte_order
;
2233 case BT_CTF_TYPE_ID_ENUM
:
2235 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2236 type
, struct bt_ctf_field_type_enumeration
, parent
);
2237 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2240 case BT_CTF_TYPE_ID_FLOAT
:
2242 struct bt_ctf_field_type_floating_point
*floating_point
=
2244 struct bt_ctf_field_type_floating_point
,
2246 ret
= floating_point
->user_byte_order
;
2253 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2254 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2255 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2256 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2262 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2263 enum bt_ctf_byte_order byte_order
)
2266 int internal_byte_order
;
2267 enum bt_ctf_type_id type_id
;
2269 if (!type
|| type
->frozen
) {
2274 switch (byte_order
) {
2275 case BT_CTF_BYTE_ORDER_NATIVE
:
2276 /* Leave unset. Will be initialized by parent. */
2277 internal_byte_order
= 0;
2279 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2280 internal_byte_order
= LITTLE_ENDIAN
;
2282 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2283 case BT_CTF_BYTE_ORDER_NETWORK
:
2284 internal_byte_order
= BIG_ENDIAN
;
2291 type_id
= type
->declaration
->id
;
2292 if (set_byte_order_funcs
[type_id
]) {
2293 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2299 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2300 struct bt_ctf_field_type
*type
)
2303 return BT_CTF_TYPE_ID_UNKNOWN
;
2306 return type
->declaration
->id
;
2309 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2311 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2314 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2316 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2319 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2321 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2324 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2326 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2329 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2331 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2334 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2336 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2339 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2341 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2344 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2346 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2349 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2354 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2360 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2370 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2371 struct bt_ctf_field_type_variant
*variant
,
2374 struct bt_ctf_field_type
*type
= NULL
;
2375 GQuark field_name_quark
;
2377 struct structure_field
*field_entry
;
2378 struct range_overlap_query query
= {
2379 .range_start
._signed
= tag_value
,
2380 .range_end
._signed
= tag_value
,
2385 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2387 if (!query
.overlaps
) {
2391 field_name_quark
= query
.mapping_name
;
2392 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2393 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2397 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2398 type
= field_entry
->type
;
2404 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2405 struct bt_ctf_field_type_variant
*variant
,
2408 struct bt_ctf_field_type
*type
= NULL
;
2409 GQuark field_name_quark
;
2411 struct structure_field
*field_entry
;
2412 struct range_overlap_query query
= {
2413 .range_start
._unsigned
= tag_value
,
2414 .range_end
._unsigned
= tag_value
,
2419 g_ptr_array_foreach(variant
->tag
->entries
,
2420 check_ranges_overlap_unsigned
,
2422 if (!query
.overlaps
) {
2426 field_name_quark
= query
.mapping_name
;
2427 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2428 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2432 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2433 type
= field_entry
->type
;
2439 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2440 struct metadata_context
*context
)
2444 if (!type
|| !context
) {
2449 /* Make sure field type is valid before serializing it */
2450 ret
= bt_ctf_field_type_validate(type
);
2456 ret
= type
->serialize(type
, context
);
2462 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2469 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2470 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2471 set_byte_order_funcs
[type
->declaration
->id
](type
,
2476 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2478 struct bt_ctf_field_type
*copy
= NULL
;
2484 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2490 int bt_ctf_field_type_structure_get_field_name_index(
2491 struct bt_ctf_field_type
*type
, const char *name
)
2496 struct bt_ctf_field_type_structure
*structure
;
2498 if (!type
|| !name
||
2499 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2504 name_quark
= g_quark_try_string(name
);
2510 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2512 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2513 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2523 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2524 struct bt_ctf_field_type
*field
, int index
)
2527 struct bt_ctf_field_type_structure
*structure
;
2529 if (!type
|| !field
||
2530 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2535 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2537 if (index
< 0 || index
>= structure
->fields
->len
) {
2543 bt_put(((struct structure_field
*)
2544 g_ptr_array_index(structure
->fields
, index
))->type
);
2545 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2552 int bt_ctf_field_type_variant_get_field_name_index(
2553 struct bt_ctf_field_type
*type
, const char *name
)
2558 struct bt_ctf_field_type_variant
*variant
;
2560 if (!type
|| !name
||
2561 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2566 name_quark
= g_quark_try_string(name
);
2572 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2574 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2575 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2585 int bt_ctf_field_type_sequence_set_length_field_path(
2586 struct bt_ctf_field_type
*type
,
2587 struct bt_ctf_field_path
*path
)
2590 struct bt_ctf_field_type_sequence
*sequence
;
2592 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2593 BT_CTF_TYPE_ID_SEQUENCE
) {
2598 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2601 BT_MOVE(sequence
->length_field_path
, path
);
2607 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2608 struct bt_ctf_field_path
*path
)
2611 struct bt_ctf_field_type_variant
*variant
;
2613 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2614 BT_CTF_TYPE_ID_VARIANT
) {
2619 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2622 BT_MOVE(variant
->tag_field_path
, path
);
2628 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2629 struct bt_ctf_field_type
*tag
)
2632 struct bt_ctf_field_type_variant
*variant
;
2634 if (!type
|| !tag
||
2635 bt_ctf_field_type_get_type_id(tag
) !=
2636 BT_CTF_TYPE_ID_ENUM
) {
2641 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2645 bt_put(&variant
->tag
->parent
);
2647 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2654 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2655 struct bt_ctf_field_type
*field
, int index
)
2658 struct bt_ctf_field_type_variant
*variant
;
2660 if (!type
|| !field
||
2661 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2666 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2668 if (index
< 0 || index
>= variant
->fields
->len
) {
2674 bt_put(((struct structure_field
*)
2675 g_ptr_array_index(variant
->fields
, index
))->type
);
2676 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2683 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2685 struct bt_ctf_field_type_integer
*integer
=
2686 (struct bt_ctf_field_type_integer
*) type
;
2692 bt_put(integer
->mapped_clock
);
2697 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2699 struct bt_ctf_field_type_enumeration
*enumeration
=
2700 (struct bt_ctf_field_type_enumeration
*) type
;
2706 g_ptr_array_free(enumeration
->entries
, TRUE
);
2707 bt_put(enumeration
->container
);
2708 g_free(enumeration
);
2712 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2714 struct bt_ctf_field_type_floating_point
*floating_point
=
2715 (struct bt_ctf_field_type_floating_point
*) type
;
2721 g_free(floating_point
);
2725 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2727 struct bt_ctf_field_type_structure
*structure
=
2728 (struct bt_ctf_field_type_structure
*) type
;
2734 g_ptr_array_free(structure
->fields
, TRUE
);
2735 g_hash_table_destroy(structure
->field_name_to_index
);
2740 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2742 struct bt_ctf_field_type_variant
*variant
=
2743 (struct bt_ctf_field_type_variant
*) type
;
2749 g_ptr_array_free(variant
->fields
, TRUE
);
2750 g_hash_table_destroy(variant
->field_name_to_index
);
2751 g_string_free(variant
->tag_name
, TRUE
);
2752 bt_put(&variant
->tag
->parent
);
2753 BT_PUT(variant
->tag_field_path
);
2758 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2760 struct bt_ctf_field_type_array
*array
=
2761 (struct bt_ctf_field_type_array
*) type
;
2767 bt_put(array
->element_type
);
2772 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2774 struct bt_ctf_field_type_sequence
*sequence
=
2775 (struct bt_ctf_field_type_sequence
*) type
;
2781 bt_put(sequence
->element_type
);
2782 g_string_free(sequence
->length_field_name
, TRUE
);
2783 BT_PUT(sequence
->length_field_path
);
2788 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2790 struct bt_ctf_field_type_string
*string
=
2791 (struct bt_ctf_field_type_string
*) type
;
2801 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2807 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2809 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2810 type
, struct bt_ctf_field_type_integer
, parent
);
2812 if (integer_type
->mapped_clock
) {
2813 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2816 generic_field_type_freeze(type
);
2820 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
2821 * Only used when freezing an enumeration.
2824 void enumeration_range_overlap_check(struct bt_ctf_field_type
*type
,
2825 struct bt_ctf_field_type_enumeration
*enumeration_type
)
2827 int i
, j
, len
= enumeration_type
->entries
->len
;
2828 struct bt_ctf_field_type
*container_type
;
2831 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2832 assert(container_type
);
2833 is_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2834 assert(is_signed
>= 0);
2836 for (i
= 0; i
< len
; i
++) {
2837 for (j
= i
+ 1; j
< len
; j
++) {
2838 struct enumeration_mapping
*mapping
[2];
2840 mapping
[0] = get_enumeration_mapping(type
, i
);
2841 mapping
[1] = get_enumeration_mapping(type
, j
);
2843 if (mapping
[0]->range_start
._signed
2844 <= mapping
[1]->range_end
._signed
2845 && mapping
[0]->range_end
._signed
2846 >= mapping
[1]->range_start
._signed
) {
2847 enumeration_type
->has_overlapping_ranges
= true;
2851 if (mapping
[0]->range_start
._unsigned
2852 <= mapping
[1]->range_end
._unsigned
2853 && mapping
[0]->range_end
._unsigned
2854 >= mapping
[1]->range_start
._unsigned
) {
2855 enumeration_type
->has_overlapping_ranges
= true;
2861 assert(!enumeration_type
->has_overlapping_ranges
);
2865 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2867 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2868 type
, struct bt_ctf_field_type_enumeration
, parent
);
2870 enumeration_range_overlap_check(type
, enumeration_type
);
2871 generic_field_type_freeze(type
);
2872 bt_ctf_field_type_freeze(enumeration_type
->container
);
2876 void freeze_structure_field(struct structure_field
*field
)
2878 bt_ctf_field_type_freeze(field
->type
);
2882 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2884 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2885 type
, struct bt_ctf_field_type_structure
, parent
);
2887 /* Cache the alignment */
2888 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2889 generic_field_type_freeze(type
);
2890 g_ptr_array_foreach(structure_type
->fields
,
2891 (GFunc
) freeze_structure_field
, NULL
);
2895 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2897 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2898 type
, struct bt_ctf_field_type_variant
, parent
);
2900 generic_field_type_freeze(type
);
2901 g_ptr_array_foreach(variant_type
->fields
,
2902 (GFunc
) freeze_structure_field
, NULL
);
2906 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2908 struct bt_ctf_field_type_array
*array_type
= container_of(
2909 type
, struct bt_ctf_field_type_array
, parent
);
2911 /* Cache the alignment */
2912 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2913 generic_field_type_freeze(type
);
2914 bt_ctf_field_type_freeze(array_type
->element_type
);
2918 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2920 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2921 type
, struct bt_ctf_field_type_sequence
, parent
);
2923 /* Cache the alignment */
2924 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2925 generic_field_type_freeze(type
);
2926 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2930 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2932 const char *encoding_string
;
2935 case BT_CTF_STRING_ENCODING_NONE
:
2936 encoding_string
= "none";
2938 case BT_CTF_STRING_ENCODING_ASCII
:
2939 encoding_string
= "ASCII";
2941 case BT_CTF_STRING_ENCODING_UTF8
:
2942 encoding_string
= "UTF8";
2945 encoding_string
= "unknown";
2949 return encoding_string
;
2953 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2955 const char *base_string
;
2958 case BT_CTF_INTEGER_BASE_DECIMAL
:
2959 base_string
= "decimal";
2961 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2962 base_string
= "hexadecimal";
2964 case BT_CTF_INTEGER_BASE_OCTAL
:
2965 base_string
= "octal";
2967 case BT_CTF_INTEGER_BASE_BINARY
:
2968 base_string
= "binary";
2971 base_string
= "unknown";
2979 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2980 struct metadata_context
*context
)
2982 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2983 struct bt_ctf_field_type_integer
, parent
);
2986 g_string_append_printf(context
->string
,
2987 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2988 integer
->declaration
.len
, type
->declaration
->alignment
,
2989 (integer
->declaration
.signedness
? "true" : "false"),
2990 get_encoding_string(integer
->declaration
.encoding
),
2991 get_integer_base_string(integer
->declaration
.base
),
2992 get_byte_order_string(integer
->declaration
.byte_order
));
2993 if (integer
->mapped_clock
) {
2994 const char *clock_name
= bt_ctf_clock_class_get_name(
2995 integer
->mapped_clock
);
3002 g_string_append_printf(context
->string
,
3003 "; map = clock.%s.value", clock_name
);
3006 g_string_append(context
->string
, "; }");
3012 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3013 struct metadata_context
*context
)
3017 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3018 struct bt_ctf_field_type_enumeration
, parent
);
3019 struct bt_ctf_field_type
*container_type
;
3020 int container_signed
;
3022 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3023 if (!container_type
) {
3028 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3029 if (container_signed
< 0) {
3030 ret
= container_signed
;
3031 goto error_put_container_type
;
3034 g_string_append(context
->string
, "enum : ");
3035 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3037 goto error_put_container_type
;
3040 g_string_append(context
->string
, " { ");
3041 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3042 struct enumeration_mapping
*mapping
=
3043 enumeration
->entries
->pdata
[entry
];
3045 if (container_signed
) {
3046 if (mapping
->range_start
._signed
==
3047 mapping
->range_end
._signed
) {
3048 g_string_append_printf(context
->string
,
3049 "\"%s\" = %" PRId64
,
3050 g_quark_to_string(mapping
->string
),
3051 mapping
->range_start
._signed
);
3053 g_string_append_printf(context
->string
,
3054 "\"%s\" = %" PRId64
" ... %" PRId64
,
3055 g_quark_to_string(mapping
->string
),
3056 mapping
->range_start
._signed
,
3057 mapping
->range_end
._signed
);
3060 if (mapping
->range_start
._unsigned
==
3061 mapping
->range_end
._unsigned
) {
3062 g_string_append_printf(context
->string
,
3063 "\"%s\" = %" PRIu64
,
3064 g_quark_to_string(mapping
->string
),
3065 mapping
->range_start
._unsigned
);
3067 g_string_append_printf(context
->string
,
3068 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3069 g_quark_to_string(mapping
->string
),
3070 mapping
->range_start
._unsigned
,
3071 mapping
->range_end
._unsigned
);
3075 g_string_append(context
->string
,
3076 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3080 if (context
->field_name
->len
) {
3081 g_string_append_printf(context
->string
, " %s",
3082 context
->field_name
->str
);
3083 g_string_assign(context
->field_name
, "");
3085 error_put_container_type
:
3086 bt_put(container_type
);
3092 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3093 struct metadata_context
*context
)
3095 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3096 type
, struct bt_ctf_field_type_floating_point
, parent
);
3098 g_string_append_printf(context
->string
,
3099 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
3100 floating_point
->declaration
.exp
->len
,
3101 floating_point
->declaration
.mantissa
->len
+ 1,
3102 get_byte_order_string(floating_point
->declaration
.byte_order
),
3103 type
->declaration
->alignment
);
3108 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3109 struct metadata_context
*context
)
3112 unsigned int indent
;
3114 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3115 struct bt_ctf_field_type_structure
, parent
);
3116 GString
*structure_field_name
= context
->field_name
;
3118 context
->field_name
= g_string_new("");
3120 context
->current_indentation_level
++;
3121 g_string_append(context
->string
, "struct {\n");
3123 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3124 struct structure_field
*field
;
3126 for (indent
= 0; indent
< context
->current_indentation_level
;
3128 g_string_append_c(context
->string
, '\t');
3131 field
= structure
->fields
->pdata
[i
];
3132 g_string_assign(context
->field_name
,
3133 g_quark_to_string(field
->name
));
3134 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3139 if (context
->field_name
->len
) {
3140 g_string_append_printf(context
->string
, " %s",
3141 context
->field_name
->str
);
3143 g_string_append(context
->string
, ";\n");
3146 context
->current_indentation_level
--;
3147 for (indent
= 0; indent
< context
->current_indentation_level
;
3149 g_string_append_c(context
->string
, '\t');
3152 g_string_append_printf(context
->string
, "} align(%zu)",
3153 type
->declaration
->alignment
);
3155 g_string_free(context
->field_name
, TRUE
);
3156 context
->field_name
= structure_field_name
;
3161 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3162 struct metadata_context
*context
)
3165 unsigned int indent
;
3167 struct bt_ctf_field_type_variant
*variant
= container_of(
3168 type
, struct bt_ctf_field_type_variant
, parent
);
3169 GString
*variant_field_name
= context
->field_name
;
3171 context
->field_name
= g_string_new("");
3172 if (variant
->tag_name
->len
> 0) {
3173 g_string_append_printf(context
->string
,
3174 "variant <%s> {\n", variant
->tag_name
->str
);
3176 g_string_append(context
->string
, "variant {\n");
3179 context
->current_indentation_level
++;
3180 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3181 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3183 g_string_assign(context
->field_name
,
3184 g_quark_to_string(field
->name
));
3185 for (indent
= 0; indent
< context
->current_indentation_level
;
3187 g_string_append_c(context
->string
, '\t');
3190 g_string_assign(context
->field_name
,
3191 g_quark_to_string(field
->name
));
3192 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3197 if (context
->field_name
->len
) {
3198 g_string_append_printf(context
->string
, " %s;",
3199 context
->field_name
->str
);
3202 g_string_append_c(context
->string
, '\n');
3205 context
->current_indentation_level
--;
3206 for (indent
= 0; indent
< context
->current_indentation_level
;
3208 g_string_append_c(context
->string
, '\t');
3211 g_string_append(context
->string
, "}");
3213 g_string_free(context
->field_name
, TRUE
);
3214 context
->field_name
= variant_field_name
;
3219 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3220 struct metadata_context
*context
)
3223 struct bt_ctf_field_type_array
*array
= container_of(type
,
3224 struct bt_ctf_field_type_array
, parent
);
3226 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3231 if (context
->field_name
->len
) {
3232 g_string_append_printf(context
->string
, " %s[%u]",
3233 context
->field_name
->str
, array
->length
);
3234 g_string_assign(context
->field_name
, "");
3236 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3243 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3244 struct metadata_context
*context
)
3247 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3248 type
, struct bt_ctf_field_type_sequence
, parent
);
3250 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3255 if (context
->field_name
->len
) {
3256 g_string_append_printf(context
->string
, " %s[%s]",
3257 context
->field_name
->str
,
3258 sequence
->length_field_name
->str
);
3259 g_string_assign(context
->field_name
, "");
3261 g_string_append_printf(context
->string
, "[%s]",
3262 sequence
->length_field_name
->str
);
3269 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3270 struct metadata_context
*context
)
3272 struct bt_ctf_field_type_string
*string
= container_of(
3273 type
, struct bt_ctf_field_type_string
, parent
);
3275 g_string_append_printf(context
->string
,
3276 "string { encoding = %s; }",
3277 get_encoding_string(string
->declaration
.encoding
));
3282 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3283 enum bt_ctf_byte_order ret
;
3285 switch (byte_order
) {
3286 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3288 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3290 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3292 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3294 case BT_CTF_BYTE_ORDER_NETWORK
:
3295 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3297 case BT_CTF_BYTE_ORDER_NATIVE
:
3298 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3301 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3309 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3310 int byte_order
, int set_native
)
3312 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3313 struct bt_ctf_field_type_integer
, parent
);
3316 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3318 * User byte order is native, so we can set
3319 * the real byte order.
3321 integer_type
->declaration
.byte_order
=
3325 integer_type
->user_byte_order
=
3326 get_ctf_ir_byte_order(byte_order
);
3327 integer_type
->declaration
.byte_order
= byte_order
;
3332 void bt_ctf_field_type_enumeration_set_byte_order(
3333 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3335 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3336 struct bt_ctf_field_type_enumeration
, parent
);
3338 /* Safe to assume that container is an integer */
3339 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3340 byte_order
, set_native
);
3344 void bt_ctf_field_type_floating_point_set_byte_order(
3345 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3347 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3348 container_of(type
, struct bt_ctf_field_type_floating_point
,
3352 if (floating_point_type
->user_byte_order
==
3353 BT_CTF_BYTE_ORDER_NATIVE
) {
3355 * User byte order is native, so we can set
3356 * the real byte order.
3358 floating_point_type
->declaration
.byte_order
=
3360 floating_point_type
->sign
.byte_order
=
3362 floating_point_type
->mantissa
.byte_order
=
3364 floating_point_type
->exp
.byte_order
=
3368 floating_point_type
->user_byte_order
=
3369 get_ctf_ir_byte_order(byte_order
);
3370 floating_point_type
->declaration
.byte_order
= byte_order
;
3371 floating_point_type
->sign
.byte_order
= byte_order
;
3372 floating_point_type
->mantissa
.byte_order
= byte_order
;
3373 floating_point_type
->exp
.byte_order
= byte_order
;
3378 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3379 int byte_order
, int set_native
)
3382 struct bt_ctf_field_type_structure
*structure_type
=
3383 container_of(type
, struct bt_ctf_field_type_structure
,
3386 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3387 struct structure_field
*field
= g_ptr_array_index(
3388 structure_type
->fields
, i
);
3389 struct bt_ctf_field_type
*field_type
= field
->type
;
3391 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3392 set_byte_order_funcs
[field_type
->declaration
->id
](
3393 field_type
, byte_order
, set_native
);
3399 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3400 int byte_order
, int set_native
)
3403 struct bt_ctf_field_type_variant
*variant_type
=
3404 container_of(type
, struct bt_ctf_field_type_variant
,
3407 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3408 struct structure_field
*field
= g_ptr_array_index(
3409 variant_type
->fields
, i
);
3410 struct bt_ctf_field_type
*field_type
= field
->type
;
3412 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3413 set_byte_order_funcs
[field_type
->declaration
->id
](
3414 field_type
, byte_order
, set_native
);
3420 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3421 int byte_order
, int set_native
)
3423 struct bt_ctf_field_type_array
*array_type
=
3424 container_of(type
, struct bt_ctf_field_type_array
,
3427 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3428 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3429 array_type
->element_type
, byte_order
, set_native
);
3434 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3435 int byte_order
, int set_native
)
3437 struct bt_ctf_field_type_sequence
*sequence_type
=
3438 container_of(type
, struct bt_ctf_field_type_sequence
,
3441 if (set_byte_order_funcs
[
3442 sequence_type
->element_type
->declaration
->id
]) {
3443 set_byte_order_funcs
[
3444 sequence_type
->element_type
->declaration
->id
](
3445 sequence_type
->element_type
, byte_order
, set_native
);
3450 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3451 struct bt_ctf_field_type
*type
)
3453 struct bt_ctf_field_type
*copy
;
3454 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3456 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3457 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3462 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3464 copy_integer
->declaration
= integer
->declaration
;
3465 if (integer
->mapped_clock
) {
3466 bt_get(integer
->mapped_clock
);
3467 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3470 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3477 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3478 struct bt_ctf_field_type
*type
)
3481 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3482 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3484 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3487 /* Copy the source enumeration's container */
3488 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3489 if (!copy_container
) {
3493 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3497 copy_enumeration
= container_of(copy
,
3498 struct bt_ctf_field_type_enumeration
, parent
);
3500 /* Copy all enumaration entries */
3501 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3502 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3503 enumeration
->entries
, i
);
3504 struct enumeration_mapping
*copy_mapping
= g_new0(
3505 struct enumeration_mapping
, 1);
3507 if (!copy_mapping
) {
3511 *copy_mapping
= *mapping
;
3512 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3515 copy_enumeration
->declaration
= enumeration
->declaration
;
3517 bt_put(copy_container
);
3520 bt_put(copy_container
);
3526 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3527 struct bt_ctf_field_type
*type
)
3529 struct bt_ctf_field_type
*copy
;
3530 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3532 floating_point
= container_of(type
,
3533 struct bt_ctf_field_type_floating_point
, parent
);
3534 copy
= bt_ctf_field_type_floating_point_create();
3539 copy_float
= container_of(copy
,
3540 struct bt_ctf_field_type_floating_point
, parent
);
3541 copy_float
->declaration
= floating_point
->declaration
;
3542 copy_float
->sign
= floating_point
->sign
;
3543 copy_float
->mantissa
= floating_point
->mantissa
;
3544 copy_float
->exp
= floating_point
->exp
;
3545 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3546 copy_float
->declaration
.sign
= ©_float
->sign
;
3547 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3548 copy_float
->declaration
.exp
= ©_float
->exp
;
3554 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3555 struct bt_ctf_field_type
*type
)
3558 GHashTableIter iter
;
3559 gpointer key
, value
;
3560 struct bt_ctf_field_type
*copy
;
3561 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3563 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3565 copy
= bt_ctf_field_type_structure_create();
3570 copy_structure
= container_of(copy
,
3571 struct bt_ctf_field_type_structure
, parent
);
3573 /* Copy field_name_to_index */
3574 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3575 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3576 g_hash_table_insert(copy_structure
->field_name_to_index
,
3580 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3581 struct structure_field
*entry
, *copy_entry
;
3582 struct bt_ctf_field_type
*copy_field
;
3584 copy_entry
= g_new0(struct structure_field
, 1);
3589 entry
= g_ptr_array_index(structure
->fields
, i
);
3590 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3596 copy_entry
->name
= entry
->name
;
3597 copy_entry
->type
= copy_field
;
3598 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3601 copy_structure
->declaration
= structure
->declaration
;
3610 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3611 struct bt_ctf_field_type
*type
)
3614 GHashTableIter iter
;
3615 gpointer key
, value
;
3616 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3617 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3619 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3622 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3628 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3629 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3634 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3637 /* Copy field_name_to_index */
3638 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3639 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3640 g_hash_table_insert(copy_variant
->field_name_to_index
,
3644 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3645 struct structure_field
*entry
, *copy_entry
;
3646 struct bt_ctf_field_type
*copy_field
;
3648 copy_entry
= g_new0(struct structure_field
, 1);
3653 entry
= g_ptr_array_index(variant
->fields
, i
);
3654 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3660 copy_entry
->name
= entry
->name
;
3661 copy_entry
->type
= copy_field
;
3662 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3665 copy_variant
->declaration
= variant
->declaration
;
3666 if (variant
->tag_field_path
) {
3667 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3668 variant
->tag_field_path
);
3669 if (!copy_variant
->tag_field_path
) {
3683 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3684 struct bt_ctf_field_type
*type
)
3686 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3687 struct bt_ctf_field_type_array
*array
, *copy_array
;
3689 array
= container_of(type
, struct bt_ctf_field_type_array
,
3691 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3692 if (!copy_element
) {
3696 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3701 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3703 copy_array
->declaration
= array
->declaration
;
3705 bt_put(copy_element
);
3710 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3711 struct bt_ctf_field_type
*type
)
3713 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3714 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3716 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3718 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3719 if (!copy_element
) {
3723 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3724 sequence
->length_field_name
->len
?
3725 sequence
->length_field_name
->str
: NULL
);
3730 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3732 copy_sequence
->declaration
= sequence
->declaration
;
3733 if (sequence
->length_field_path
) {
3734 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3735 sequence
->length_field_path
);
3736 if (!copy_sequence
->length_field_path
) {
3741 bt_put(copy_element
);
3749 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3750 struct bt_ctf_field_type
*type
)
3752 struct bt_ctf_field_type
*copy
;
3753 struct bt_ctf_field_type_string
*string
, *copy_string
;
3755 copy
= bt_ctf_field_type_string_create();
3760 string
= container_of(type
, struct bt_ctf_field_type_string
,
3762 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3764 copy_string
->declaration
= string
->declaration
;
3770 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3771 struct bt_ctf_field_type
*type_b
)
3774 struct bt_ctf_field_type_integer
*integer_a
;
3775 struct bt_ctf_field_type_integer
*integer_b
;
3776 struct declaration_integer
*decl_a
;
3777 struct declaration_integer
*decl_b
;
3779 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3781 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3783 decl_a
= &integer_a
->declaration
;
3784 decl_b
= &integer_b
->declaration
;
3787 if (decl_a
->len
!= decl_b
->len
) {
3792 * Compare user byte orders only, not the cached,
3795 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3800 if (decl_a
->signedness
!= decl_b
->signedness
) {
3805 if (decl_a
->base
!= decl_b
->base
) {
3810 if (decl_a
->encoding
!= decl_b
->encoding
) {
3815 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3827 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3828 struct bt_ctf_field_type
*type_b
)
3831 struct bt_ctf_field_type_floating_point
*float_a
;
3832 struct bt_ctf_field_type_floating_point
*float_b
;
3834 float_a
= container_of(type_a
,
3835 struct bt_ctf_field_type_floating_point
, parent
);
3836 float_b
= container_of(type_b
,
3837 struct bt_ctf_field_type_floating_point
, parent
);
3840 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3844 /* Exponent length */
3845 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3849 /* Mantissa length */
3850 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3855 * Compare user byte orders only, not the cached,
3858 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3870 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3871 struct enumeration_mapping
*mapping_b
)
3876 if (mapping_a
->string
!= mapping_b
->string
) {
3881 if (mapping_a
->range_start
._unsigned
!=
3882 mapping_b
->range_start
._unsigned
) {
3887 if (mapping_a
->range_end
._unsigned
!=
3888 mapping_b
->range_end
._unsigned
) {
3900 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3901 struct bt_ctf_field_type
*type_b
)
3905 struct bt_ctf_field_type_enumeration
*enum_a
;
3906 struct bt_ctf_field_type_enumeration
*enum_b
;
3908 enum_a
= container_of(type_a
,
3909 struct bt_ctf_field_type_enumeration
, parent
);
3910 enum_b
= container_of(type_b
,
3911 struct bt_ctf_field_type_enumeration
, parent
);
3913 /* Container field type */
3914 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3922 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3926 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3927 struct enumeration_mapping
*mapping_a
=
3928 g_ptr_array_index(enum_a
->entries
, i
);
3929 struct enumeration_mapping
*mapping_b
=
3930 g_ptr_array_index(enum_b
->entries
, i
);
3932 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3945 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3946 struct bt_ctf_field_type
*type_b
)
3949 struct bt_ctf_field_type_string
*string_a
;
3950 struct bt_ctf_field_type_string
*string_b
;
3952 string_a
= container_of(type_a
,
3953 struct bt_ctf_field_type_string
, parent
);
3954 string_b
= container_of(type_b
,
3955 struct bt_ctf_field_type_string
, parent
);
3958 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3970 int compare_structure_fields(struct structure_field
*field_a
,
3971 struct structure_field
*field_b
)
3976 if (field_a
->name
!= field_b
->name
) {
3981 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3988 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3989 struct bt_ctf_field_type
*type_b
)
3993 struct bt_ctf_field_type_structure
*struct_a
;
3994 struct bt_ctf_field_type_structure
*struct_b
;
3996 struct_a
= container_of(type_a
,
3997 struct bt_ctf_field_type_structure
, parent
);
3998 struct_b
= container_of(type_b
,
3999 struct bt_ctf_field_type_structure
, parent
);
4002 if (bt_ctf_field_type_get_alignment(type_a
) !=
4003 bt_ctf_field_type_get_alignment(type_b
)) {
4008 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4012 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4013 struct structure_field
*field_a
=
4014 g_ptr_array_index(struct_a
->fields
, i
);
4015 struct structure_field
*field_b
=
4016 g_ptr_array_index(struct_b
->fields
, i
);
4018 ret
= compare_structure_fields(field_a
, field_b
);
4032 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4033 struct bt_ctf_field_type
*type_b
)
4037 struct bt_ctf_field_type_variant
*variant_a
;
4038 struct bt_ctf_field_type_variant
*variant_b
;
4040 variant_a
= container_of(type_a
,
4041 struct bt_ctf_field_type_variant
, parent
);
4042 variant_b
= container_of(type_b
,
4043 struct bt_ctf_field_type_variant
, parent
);
4046 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4051 ret
= bt_ctf_field_type_compare(
4052 (struct bt_ctf_field_type
*) variant_a
->tag
,
4053 (struct bt_ctf_field_type
*) variant_b
->tag
);
4061 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
4065 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
4066 struct structure_field
*field_a
=
4067 g_ptr_array_index(variant_a
->fields
, i
);
4068 struct structure_field
*field_b
=
4069 g_ptr_array_index(variant_b
->fields
, i
);
4071 ret
= compare_structure_fields(field_a
, field_b
);
4085 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4086 struct bt_ctf_field_type
*type_b
)
4089 struct bt_ctf_field_type_array
*array_a
;
4090 struct bt_ctf_field_type_array
*array_b
;
4092 array_a
= container_of(type_a
,
4093 struct bt_ctf_field_type_array
, parent
);
4094 array_b
= container_of(type_b
,
4095 struct bt_ctf_field_type_array
, parent
);
4098 if (array_a
->length
!= array_b
->length
) {
4103 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4104 array_b
->element_type
);
4111 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4112 struct bt_ctf_field_type
*type_b
)
4115 struct bt_ctf_field_type_sequence
*sequence_a
;
4116 struct bt_ctf_field_type_sequence
*sequence_b
;
4118 sequence_a
= container_of(type_a
,
4119 struct bt_ctf_field_type_sequence
, parent
);
4120 sequence_b
= container_of(type_b
,
4121 struct bt_ctf_field_type_sequence
, parent
);
4124 if (strcmp(sequence_a
->length_field_name
->str
,
4125 sequence_b
->length_field_name
->str
)) {
4130 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4131 sequence_b
->element_type
);
4137 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4138 struct bt_ctf_field_type
*type_b
)
4142 if (type_a
== type_b
) {
4143 /* Same reference: equal (even if both are NULL) */
4148 if (!type_a
|| !type_b
) {
4153 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4154 /* Different type IDs */
4158 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4159 /* Both have unknown type IDs */
4163 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4170 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4172 int field_count
= -1;
4173 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4176 case CTF_TYPE_STRUCT
:
4178 bt_ctf_field_type_structure_get_field_count(field_type
);
4180 case CTF_TYPE_VARIANT
:
4182 bt_ctf_field_type_variant_get_field_count(field_type
);
4184 case CTF_TYPE_ARRAY
:
4185 case CTF_TYPE_SEQUENCE
:
4187 * Array and sequence types always contain a single member
4188 * (the element type).
4200 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4201 struct bt_ctf_field_type
*field_type
, int index
)
4203 struct bt_ctf_field_type
*field
= NULL
;
4204 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4207 case CTF_TYPE_STRUCT
:
4208 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4211 case CTF_TYPE_VARIANT
:
4213 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4221 case CTF_TYPE_ARRAY
:
4222 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4224 case CTF_TYPE_SEQUENCE
:
4225 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4235 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4238 int field_index
= -1;
4239 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4242 case CTF_TYPE_STRUCT
:
4243 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4246 case CTF_TYPE_VARIANT
:
4247 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4257 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4258 struct bt_ctf_field_type
*type
)
4260 struct bt_ctf_field_path
*field_path
= NULL
;
4261 struct bt_ctf_field_type_variant
*variant
;
4263 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4267 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4269 field_path
= bt_get(variant
->tag_field_path
);
4274 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4275 struct bt_ctf_field_type
*type
)
4277 struct bt_ctf_field_path
*field_path
= NULL
;
4278 struct bt_ctf_field_type_sequence
*sequence
;
4280 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4284 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4286 field_path
= bt_get(sequence
->length_field_path
);