4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/clock.h>
34 #include <babeltrace/ctf-ir/clock-internal.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/endian.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 [BT_CTF_TYPE_ID_ENUM
] =
81 bt_ctf_field_type_enumeration_destroy
,
82 [BT_CTF_TYPE_ID_FLOAT
] =
83 bt_ctf_field_type_floating_point_destroy
,
84 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
104 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
107 type_freeze_func
const type_freeze_funcs
[] = {
108 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
109 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
110 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
111 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
112 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
113 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
114 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
115 [BT_CTF_TYPE_ID_STRING
] = generic_field_type_freeze
,
119 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_floating_point_serialize(
126 struct bt_ctf_field_type
*, struct metadata_context
*);
128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
144 type_serialize_func
const type_serialize_funcs
[] = {
145 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
146 [BT_CTF_TYPE_ID_ENUM
] =
147 bt_ctf_field_type_enumeration_serialize
,
148 [BT_CTF_TYPE_ID_FLOAT
] =
149 bt_ctf_field_type_floating_point_serialize
,
150 [BT_CTF_TYPE_ID_STRUCT
] =
151 bt_ctf_field_type_structure_serialize
,
152 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
153 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
154 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
155 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
159 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_floating_point_set_byte_order(
166 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
168 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
178 int byte_order
, int set_native
);
180 /* The set_native flag only set the byte order if it is set to native */
182 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
183 int byte_order
, int set_native
) = {
184 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
185 [BT_CTF_TYPE_ID_ENUM
] =
186 bt_ctf_field_type_enumeration_set_byte_order
,
187 [BT_CTF_TYPE_ID_FLOAT
] =
188 bt_ctf_field_type_floating_point_set_byte_order
,
189 [BT_CTF_TYPE_ID_STRUCT
] =
190 bt_ctf_field_type_structure_set_byte_order
,
191 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
192 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
193 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
194 [BT_CTF_TYPE_ID_STRING
] = NULL
,
198 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
217 struct bt_ctf_field_type
*);
219 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
220 struct bt_ctf_field_type
*);
223 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
224 struct bt_ctf_field_type
*) = {
225 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
226 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
227 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
228 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
229 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
230 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
231 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
232 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
236 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
237 struct bt_ctf_field_type
*);
239 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
240 struct bt_ctf_field_type
*);
242 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
243 struct bt_ctf_field_type
*);
245 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
246 struct bt_ctf_field_type
*);
248 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
249 struct bt_ctf_field_type
*);
251 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
252 struct bt_ctf_field_type
*);
254 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
255 struct bt_ctf_field_type
*);
257 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
258 struct bt_ctf_field_type
*);
261 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
262 struct bt_ctf_field_type
*) = {
263 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
264 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
265 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
266 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
267 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
268 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
269 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
270 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
274 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
282 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
284 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
287 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
288 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
289 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
290 [BT_CTF_TYPE_ID_STRING
] = NULL
,
291 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
292 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
293 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
294 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
295 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
299 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
305 void destroy_structure_field(struct structure_field
*field
)
312 void check_ranges_overlap(gpointer element
, gpointer query
)
314 struct enumeration_mapping
*mapping
= element
;
315 struct range_overlap_query
*overlap_query
= query
;
317 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
318 && overlap_query
->range_start
._signed
<=
319 mapping
->range_end
._signed
) {
320 overlap_query
->overlaps
= 1;
321 overlap_query
->mapping_name
= mapping
->string
;
324 overlap_query
->overlaps
|=
325 mapping
->string
== overlap_query
->mapping_name
;
329 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
331 struct enumeration_mapping
*mapping
= element
;
332 struct range_overlap_query
*overlap_query
= query
;
334 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
335 && overlap_query
->range_start
._unsigned
<=
336 mapping
->range_end
._unsigned
) {
337 overlap_query
->overlaps
= 1;
338 overlap_query
->mapping_name
= mapping
->string
;
341 overlap_query
->overlaps
|=
342 mapping
->string
== overlap_query
->mapping_name
;
346 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
347 struct enumeration_mapping
**b
)
349 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
353 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
354 struct enumeration_mapping
**b
)
356 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
360 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
362 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
364 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
365 (type_id
< BT_CTF_NR_TYPE_IDS
));
367 bt_object_init(type
, bt_ctf_field_type_destroy
);
368 type
->freeze
= type_freeze_funcs
[type_id
];
369 type
->serialize
= type_serialize_funcs
[type_id
];
372 int ret
= bt_ctf_field_type_set_byte_order(type
,
373 BT_CTF_BYTE_ORDER_NATIVE
);
377 type
->declaration
->alignment
= 1;
381 int add_structure_field(GPtrArray
*fields
,
382 GHashTable
*field_name_to_index
,
383 struct bt_ctf_field_type
*field_type
,
384 const char *field_name
)
387 GQuark name_quark
= g_quark_from_string(field_name
);
388 struct structure_field
*field
;
390 /* Make sure structure does not contain a field of the same name */
391 if (g_hash_table_lookup_extended(field_name_to_index
,
392 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
397 field
= g_new0(struct structure_field
, 1);
404 field
->name
= name_quark
;
405 field
->type
= field_type
;
406 g_hash_table_insert(field_name_to_index
,
407 (gpointer
) (unsigned long) name_quark
,
408 (gpointer
) (unsigned long) fields
->len
);
409 g_ptr_array_add(fields
, field
);
415 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
417 struct bt_ctf_field_type
*type
;
418 enum bt_ctf_type_id type_id
;
420 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
421 type_id
= type
->declaration
->id
;
422 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
423 type_id
>= BT_CTF_NR_TYPE_IDS
) {
427 type_destroy_funcs
[type_id
](type
);
431 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
435 struct bt_ctf_field_type_integer
*integer
=
436 container_of(type
, struct bt_ctf_field_type_integer
,
439 if (integer
->mapped_clock
&& integer
->declaration
.signedness
) {
449 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
453 struct bt_ctf_field_type_enumeration
*enumeration
=
454 container_of(type
, struct bt_ctf_field_type_enumeration
,
456 struct bt_ctf_field_type
*container_type
=
457 bt_ctf_field_type_enumeration_get_container_type(type
);
459 if (!container_type
) {
464 ret
= bt_ctf_field_type_validate(container_type
);
469 /* Ensure enum has entries */
470 ret
= enumeration
->entries
->len
? 0 : -1;
473 BT_PUT(container_type
);
478 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
481 struct bt_ctf_field_type
*element_type
= NULL
;
482 struct bt_ctf_field_type_sequence
*sequence
=
483 container_of(type
, struct bt_ctf_field_type_sequence
,
486 /* Length field name should be set at this point */
487 if (sequence
->length_field_name
->len
== 0) {
492 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
498 ret
= bt_ctf_field_type_validate(element_type
);
501 BT_PUT(element_type
);
507 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
510 struct bt_ctf_field_type
*element_type
= NULL
;
512 element_type
= bt_ctf_field_type_array_get_element_type(type
);
518 ret
= bt_ctf_field_type_validate(element_type
);
521 BT_PUT(element_type
);
527 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
530 struct bt_ctf_field_type
*child_type
= NULL
;
531 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
534 if (field_count
< 0) {
539 for (i
= 0; i
< field_count
; ++i
) {
540 ret
= bt_ctf_field_type_structure_get_field(type
,
541 NULL
, &child_type
, i
);
546 ret
= bt_ctf_field_type_validate(child_type
);
561 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
565 struct bt_ctf_field_type
*child_type
= NULL
;
566 struct bt_ctf_field_type_variant
*variant
=
567 container_of(type
, struct bt_ctf_field_type_variant
,
570 int tag_mappings_count
;
572 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
578 bt_ctf_field_type_enumeration_get_mapping_count(
579 (struct bt_ctf_field_type
*) variant
->tag
);
581 if (tag_mappings_count
!= variant
->fields
->len
) {
586 for (i
= 0; i
< tag_mappings_count
; ++i
) {
588 int64_t range_start
, range_end
;
589 struct bt_ctf_field_type
*ft
;
591 ret
= bt_ctf_field_type_enumeration_get_mapping(
592 (struct bt_ctf_field_type
*) variant
->tag
,
593 i
, &label
, &range_start
, &range_end
);
602 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
612 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
613 if (field_count
< 0) {
618 for (i
= 0; i
< field_count
; ++i
) {
619 ret
= bt_ctf_field_type_variant_get_field(type
,
620 NULL
, &child_type
, i
);
625 ret
= bt_ctf_field_type_validate(child_type
);
640 * This function validates a given field type without considering
641 * where this field type is located. It only validates the properties
642 * of the given field type and the properties of its children if
646 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
649 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
657 /* Already marked as valid */
661 if (type_validate_funcs
[id
]) {
662 ret
= type_validate_funcs
[id
](type
);
665 if (!ret
&& type
->frozen
) {
666 /* Field type is valid */
674 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
676 struct bt_ctf_field_type_integer
*integer
=
677 g_new0(struct bt_ctf_field_type_integer
, 1);
679 if (!integer
|| size
== 0 || size
> 64) {
683 integer
->parent
.declaration
= &integer
->declaration
.p
;
684 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
685 integer
->declaration
.len
= size
;
686 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
687 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
688 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
689 return &integer
->parent
;
693 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
696 struct bt_ctf_field_type_integer
*integer
;
698 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
703 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
704 ret
= (int) integer
->declaration
.len
;
709 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
712 struct bt_ctf_field_type_integer
*integer
;
714 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
719 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
720 ret
= integer
->declaration
.signedness
;
725 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
729 struct bt_ctf_field_type_integer
*integer
;
731 if (!type
|| type
->frozen
||
732 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
737 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
738 integer
->declaration
.signedness
= !!is_signed
;
744 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
745 struct bt_ctf_field_type
*type
)
747 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
748 struct bt_ctf_field_type_integer
*integer
;
750 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
754 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
755 ret
= integer
->declaration
.base
;
760 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
761 enum bt_ctf_integer_base base
)
765 if (!type
|| type
->frozen
||
766 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
772 case BT_CTF_INTEGER_BASE_BINARY
:
773 case BT_CTF_INTEGER_BASE_OCTAL
:
774 case BT_CTF_INTEGER_BASE_DECIMAL
:
775 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
777 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
778 struct bt_ctf_field_type_integer
, parent
);
779 integer
->declaration
.base
= base
;
790 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
791 struct bt_ctf_field_type
*type
)
793 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
794 struct bt_ctf_field_type_integer
*integer
;
796 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
800 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
801 ret
= integer
->declaration
.encoding
;
806 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
807 enum bt_ctf_string_encoding encoding
)
810 struct bt_ctf_field_type_integer
*integer
;
812 if (!type
|| type
->frozen
||
813 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
814 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
815 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
820 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
821 integer
->declaration
.encoding
= encoding
;
827 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
828 struct bt_ctf_field_type
*type
)
830 struct bt_ctf_field_type_integer
*integer
;
831 struct bt_ctf_clock
*clock
= NULL
;
837 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
838 clock
= integer
->mapped_clock
;
845 int bt_ctf_field_type_integer_set_mapped_clock(
846 struct bt_ctf_field_type
*type
,
847 struct bt_ctf_clock
*clock
)
849 struct bt_ctf_field_type_integer
*integer
;
852 if (!type
|| type
->frozen
) {
857 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
858 bt_put(integer
->mapped_clock
);
860 integer
->mapped_clock
= clock
;
865 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
866 struct bt_ctf_field_type
*integer_container_type
)
868 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
870 if (!integer_container_type
) {
874 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
878 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
883 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
884 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
885 bt_get(integer_container_type
);
886 enumeration
->container
= integer_container_type
;
887 enumeration
->entries
= g_ptr_array_new_with_free_func(
888 (GDestroyNotify
)destroy_enumeration_mapping
);
889 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
890 return &enumeration
->parent
;
897 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
898 struct bt_ctf_field_type
*type
)
900 struct bt_ctf_field_type
*container_type
= NULL
;
901 struct bt_ctf_field_type_enumeration
*enumeration_type
;
907 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
911 enumeration_type
= container_of(type
,
912 struct bt_ctf_field_type_enumeration
, parent
);
913 container_type
= enumeration_type
->container
;
914 bt_get(container_type
);
916 return container_type
;
919 int bt_ctf_field_type_enumeration_add_mapping(
920 struct bt_ctf_field_type
*type
, const char *string
,
921 int64_t range_start
, int64_t range_end
)
925 struct enumeration_mapping
*mapping
;
926 struct bt_ctf_field_type_enumeration
*enumeration
;
927 struct range_overlap_query query
;
928 char *escaped_string
;
930 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
932 (range_end
< range_start
)) {
937 if (!string
|| strlen(string
) == 0) {
942 escaped_string
= g_strescape(string
, NULL
);
943 if (!escaped_string
) {
948 mapping_name
= g_quark_from_string(escaped_string
);
949 query
= (struct range_overlap_query
) {
950 .range_start
._signed
= range_start
,
951 .range_end
._signed
= range_end
,
952 .mapping_name
= mapping_name
,
954 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
957 /* Check that the range does not overlap with one already present */
958 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
959 if (query
.overlaps
) {
964 mapping
= g_new(struct enumeration_mapping
, 1);
970 *mapping
= (struct enumeration_mapping
) {
971 .range_start
._signed
= range_start
,
972 .range_end
._signed
= range_end
, .string
= mapping_name
};
973 g_ptr_array_add(enumeration
->entries
, mapping
);
974 g_ptr_array_sort(enumeration
->entries
,
975 (GCompareFunc
)compare_enumeration_mappings_signed
);
977 free(escaped_string
);
983 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
984 struct bt_ctf_field_type
*type
, const char *string
,
985 uint64_t range_start
, uint64_t range_end
)
989 struct enumeration_mapping
*mapping
;
990 struct bt_ctf_field_type_enumeration
*enumeration
;
991 struct range_overlap_query query
;
992 char *escaped_string
;
994 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
996 (range_end
< range_start
)) {
1001 if (!string
|| strlen(string
) == 0) {
1006 escaped_string
= g_strescape(string
, NULL
);
1007 if (!escaped_string
) {
1012 mapping_name
= g_quark_from_string(escaped_string
);
1013 query
= (struct range_overlap_query
) {
1014 .range_start
._unsigned
= range_start
,
1015 .range_end
._unsigned
= range_end
,
1016 .mapping_name
= mapping_name
,
1018 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1021 /* Check that the range does not overlap with one already present */
1022 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
1024 if (query
.overlaps
) {
1029 mapping
= g_new(struct enumeration_mapping
, 1);
1035 *mapping
= (struct enumeration_mapping
) {
1036 .range_start
._unsigned
= range_start
,
1037 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1038 g_ptr_array_add(enumeration
->entries
, mapping
);
1039 g_ptr_array_sort(enumeration
->entries
,
1040 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1042 free(escaped_string
);
1048 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1049 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1052 const char *name
= NULL
;
1053 struct range_overlap_query query
=
1054 (struct range_overlap_query
) {
1055 .range_start
._unsigned
= value
,
1056 .range_end
._unsigned
= value
,
1059 g_ptr_array_foreach(enumeration_type
->entries
,
1060 check_ranges_overlap_unsigned
,
1062 if (!query
.overlaps
) {
1066 name
= g_quark_to_string(query
.mapping_name
);
1071 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1072 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1075 const char *name
= NULL
;
1076 struct range_overlap_query query
=
1077 (struct range_overlap_query
) {
1078 .range_start
._signed
= value
,
1079 .range_end
._signed
= value
,
1082 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1084 if (!query
.overlaps
) {
1088 name
= g_quark_to_string(query
.mapping_name
);
1093 int bt_ctf_field_type_enumeration_get_mapping_count(
1094 struct bt_ctf_field_type
*type
)
1097 struct bt_ctf_field_type_enumeration
*enumeration
;
1099 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1104 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1106 ret
= (int) enumeration
->entries
->len
;
1112 struct enumeration_mapping
*get_enumeration_mapping(
1113 struct bt_ctf_field_type
*type
, int index
)
1115 struct enumeration_mapping
*mapping
= NULL
;
1116 struct bt_ctf_field_type_enumeration
*enumeration
;
1118 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1120 if (index
>= enumeration
->entries
->len
) {
1124 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1130 int bt_ctf_field_type_enumeration_get_mapping(
1131 struct bt_ctf_field_type
*type
, int index
,
1132 const char **string
, int64_t *range_start
, int64_t *range_end
)
1134 struct enumeration_mapping
*mapping
;
1137 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1138 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1143 mapping
= get_enumeration_mapping(type
, index
);
1149 *string
= g_quark_to_string(mapping
->string
);
1150 *range_start
= mapping
->range_start
._signed
;
1151 *range_end
= mapping
->range_end
._signed
;
1157 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1158 struct bt_ctf_field_type
*type
, int index
,
1159 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1161 struct enumeration_mapping
*mapping
;
1164 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1165 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1170 mapping
= get_enumeration_mapping(type
, index
);
1176 *string
= g_quark_to_string(mapping
->string
);
1177 *range_start
= mapping
->range_start
._unsigned
;
1178 *range_end
= mapping
->range_end
._unsigned
;
1184 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1185 struct bt_ctf_field_type
*type
, const char *name
)
1188 struct bt_ctf_field_type_enumeration
*enumeration
;
1191 if (!type
|| !name
||
1192 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1197 name_quark
= g_quark_try_string(name
);
1203 enumeration
= container_of(type
,
1204 struct bt_ctf_field_type_enumeration
, parent
);
1205 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1206 struct enumeration_mapping
*mapping
=
1207 get_enumeration_mapping(type
, i
);
1209 if (mapping
->string
== name_quark
) {
1221 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1222 struct bt_ctf_field_type
*type
, int64_t value
)
1224 struct bt_ctf_field_type_enumeration
*enumeration
;
1227 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1232 enumeration
= container_of(type
,
1233 struct bt_ctf_field_type_enumeration
, parent
);
1234 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1235 struct enumeration_mapping
*mapping
=
1236 get_enumeration_mapping(type
, i
);
1238 if (value
>= mapping
->range_start
._signed
&&
1239 value
<= mapping
->range_end
._signed
) {
1251 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1252 struct bt_ctf_field_type
*type
, uint64_t value
)
1254 struct bt_ctf_field_type_enumeration
*enumeration
;
1257 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1262 enumeration
= container_of(type
,
1263 struct bt_ctf_field_type_enumeration
, parent
);
1264 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1265 struct enumeration_mapping
*mapping
=
1266 get_enumeration_mapping(type
, i
);
1268 if (value
>= mapping
->range_start
._unsigned
&&
1269 value
<= mapping
->range_end
._unsigned
) {
1280 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1282 struct bt_ctf_field_type_floating_point
*floating_point
=
1283 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1285 if (!floating_point
) {
1289 floating_point
->declaration
.sign
= &floating_point
->sign
;
1290 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1291 floating_point
->declaration
.exp
= &floating_point
->exp
;
1292 floating_point
->sign
.len
= 1;
1293 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1294 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1295 floating_point
->declaration
.exp
->len
=
1296 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1297 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1298 floating_point
->sign
.p
.alignment
= 1;
1299 floating_point
->mantissa
.p
.alignment
= 1;
1300 floating_point
->exp
.p
.alignment
= 1;
1302 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1304 return floating_point
? &floating_point
->parent
: NULL
;
1308 int bt_ctf_field_type_floating_point_get_exponent_digits(
1309 struct bt_ctf_field_type
*type
)
1312 struct bt_ctf_field_type_floating_point
*floating_point
;
1314 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1319 floating_point
= container_of(type
,
1320 struct bt_ctf_field_type_floating_point
, parent
);
1321 ret
= (int) floating_point
->declaration
.exp
->len
;
1326 int bt_ctf_field_type_floating_point_set_exponent_digits(
1327 struct bt_ctf_field_type
*type
,
1328 unsigned int exponent_digits
)
1331 struct bt_ctf_field_type_floating_point
*floating_point
;
1333 if (!type
|| type
->frozen
||
1334 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1339 floating_point
= container_of(type
,
1340 struct bt_ctf_field_type_floating_point
, parent
);
1341 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1342 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1344 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1349 floating_point
->declaration
.exp
->len
= exponent_digits
;
1355 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1356 struct bt_ctf_field_type
*type
)
1359 struct bt_ctf_field_type_floating_point
*floating_point
;
1361 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1366 floating_point
= container_of(type
,
1367 struct bt_ctf_field_type_floating_point
, parent
);
1368 ret
= (int) floating_point
->mantissa
.len
+ 1;
1373 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1374 struct bt_ctf_field_type
*type
,
1375 unsigned int mantissa_digits
)
1378 struct bt_ctf_field_type_floating_point
*floating_point
;
1380 if (!type
|| type
->frozen
||
1381 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1386 floating_point
= container_of(type
,
1387 struct bt_ctf_field_type_floating_point
, parent
);
1389 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1390 (mantissa_digits
!= DBL_MANT_DIG
) &&
1391 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1396 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1401 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1403 struct bt_ctf_field_type_structure
*structure
=
1404 g_new0(struct bt_ctf_field_type_structure
, 1);
1410 structure
->parent
.declaration
= &structure
->declaration
.p
;
1411 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1412 structure
->fields
= g_ptr_array_new_with_free_func(
1413 (GDestroyNotify
)destroy_structure_field
);
1414 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1415 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1416 return &structure
->parent
;
1421 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1422 struct bt_ctf_field_type
*field_type
,
1423 const char *field_name
)
1426 struct bt_ctf_field_type_structure
*structure
;
1428 if (!type
|| !field_type
|| type
->frozen
||
1429 bt_ctf_validate_identifier(field_name
) ||
1430 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1435 structure
= container_of(type
,
1436 struct bt_ctf_field_type_structure
, parent
);
1437 if (add_structure_field(structure
->fields
,
1438 structure
->field_name_to_index
, field_type
, field_name
)) {
1447 int bt_ctf_field_type_structure_get_field_count(
1448 struct bt_ctf_field_type
*type
)
1451 struct bt_ctf_field_type_structure
*structure
;
1453 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1458 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1460 ret
= (int) structure
->fields
->len
;
1465 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1466 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1469 struct bt_ctf_field_type_structure
*structure
;
1470 struct structure_field
*field
;
1473 if (!type
|| index
< 0 ||
1474 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1479 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1481 if (index
>= structure
->fields
->len
) {
1486 field
= g_ptr_array_index(structure
->fields
, index
);
1488 *field_type
= field
->type
;
1489 bt_get(field
->type
);
1492 *field_name
= g_quark_to_string(field
->name
);
1499 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1500 struct bt_ctf_field_type
*type
,
1505 struct structure_field
*field
;
1506 struct bt_ctf_field_type_structure
*structure
;
1507 struct bt_ctf_field_type
*field_type
= NULL
;
1509 if (!type
|| !name
) {
1513 name_quark
= g_quark_try_string(name
);
1518 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1520 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1521 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1525 field
= structure
->fields
->pdata
[index
];
1526 field_type
= field
->type
;
1532 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1533 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1535 struct bt_ctf_field_type_variant
*variant
= NULL
;
1537 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1541 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1546 variant
->parent
.declaration
= &variant
->declaration
.p
;
1547 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1548 variant
->tag_name
= g_string_new(tag_name
);
1549 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1550 variant
->fields
= g_ptr_array_new_with_free_func(
1551 (GDestroyNotify
) destroy_structure_field
);
1554 variant
->tag
= container_of(enum_tag
,
1555 struct bt_ctf_field_type_enumeration
, parent
);
1558 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1559 /* A variant's alignment is undefined */
1560 variant
->parent
.declaration
->alignment
= 0;
1561 return &variant
->parent
;
1567 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1568 struct bt_ctf_field_type
*type
)
1570 struct bt_ctf_field_type_variant
*variant
;
1571 struct bt_ctf_field_type
*tag_type
= NULL
;
1573 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1577 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1578 if (!variant
->tag
) {
1582 tag_type
= &variant
->tag
->parent
;
1589 const char *bt_ctf_field_type_variant_get_tag_name(
1590 struct bt_ctf_field_type
*type
)
1592 struct bt_ctf_field_type_variant
*variant
;
1593 const char *tag_name
= NULL
;
1595 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1599 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1600 if (variant
->tag_name
->len
== 0) {
1604 tag_name
= variant
->tag_name
->str
;
1610 int bt_ctf_field_type_variant_set_tag_name(
1611 struct bt_ctf_field_type
*type
, const char *name
)
1614 struct bt_ctf_field_type_variant
*variant
;
1616 if (!type
|| type
->frozen
||
1617 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1618 bt_ctf_validate_identifier(name
)) {
1623 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1624 g_string_assign(variant
->tag_name
, name
);
1629 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1630 struct bt_ctf_field_type
*field_type
,
1631 const char *field_name
)
1635 struct bt_ctf_field_type_variant
*variant
;
1636 GQuark field_name_quark
= g_quark_from_string(field_name
);
1638 if (!type
|| !field_type
|| type
->frozen
||
1639 bt_ctf_validate_identifier(field_name
) ||
1640 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1645 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1647 /* The user has explicitly provided a tag; validate against it. */
1651 /* Make sure this name is present in the enum tag */
1652 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1653 struct enumeration_mapping
*mapping
=
1654 g_ptr_array_index(variant
->tag
->entries
, i
);
1656 if (mapping
->string
== field_name_quark
) {
1663 /* Validation failed */
1669 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1670 field_type
, field_name
)) {
1679 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1680 struct bt_ctf_field_type
*type
,
1681 const char *field_name
)
1685 struct structure_field
*field
;
1686 struct bt_ctf_field_type_variant
*variant
;
1687 struct bt_ctf_field_type
*field_type
= NULL
;
1689 if (!type
|| !field_name
) {
1693 name_quark
= g_quark_try_string(field_name
);
1698 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1699 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1700 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1704 field
= g_ptr_array_index(variant
->fields
, index
);
1705 field_type
= field
->type
;
1712 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1713 struct bt_ctf_field_type
*type
,
1714 struct bt_ctf_field
*tag
)
1716 const char *enum_value
;
1717 struct bt_ctf_field_type
*field_type
= NULL
;
1719 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1723 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1728 /* Already increments field_type's reference count */
1729 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1735 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1738 struct bt_ctf_field_type_variant
*variant
;
1740 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1745 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1747 ret
= (int) variant
->fields
->len
;
1754 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1755 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1758 struct bt_ctf_field_type_variant
*variant
;
1759 struct structure_field
*field
;
1762 if (!type
|| index
< 0 ||
1763 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1768 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1770 if (index
>= variant
->fields
->len
) {
1775 field
= g_ptr_array_index(variant
->fields
, index
);
1777 *field_type
= field
->type
;
1778 bt_get(field
->type
);
1781 *field_name
= g_quark_to_string(field
->name
);
1787 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1788 struct bt_ctf_field_type
*element_type
,
1789 unsigned int length
)
1791 struct bt_ctf_field_type_array
*array
= NULL
;
1793 if (!element_type
|| length
== 0) {
1797 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1802 array
->parent
.declaration
= &array
->declaration
.p
;
1803 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1805 bt_get(element_type
);
1806 array
->element_type
= element_type
;
1807 array
->length
= length
;
1808 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1809 return &array
->parent
;
1815 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1816 struct bt_ctf_field_type
*type
)
1818 struct bt_ctf_field_type
*ret
= NULL
;
1819 struct bt_ctf_field_type_array
*array
;
1821 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1825 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1826 ret
= array
->element_type
;
1833 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1834 struct bt_ctf_field_type
*element_type
)
1837 struct bt_ctf_field_type_array
*array
;
1839 if (!type
|| !element_type
||
1840 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1845 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1847 if (array
->element_type
) {
1848 BT_PUT(array
->element_type
);
1851 array
->element_type
= element_type
;
1852 bt_get(array
->element_type
);
1859 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1862 struct bt_ctf_field_type_array
*array
;
1864 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1869 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1870 ret
= (int64_t) array
->length
;
1875 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1876 struct bt_ctf_field_type
*element_type
,
1877 const char *length_field_name
)
1879 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1881 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1885 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1890 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1891 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1892 bt_get(element_type
);
1893 sequence
->element_type
= element_type
;
1894 sequence
->length_field_name
= g_string_new(length_field_name
);
1895 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1896 return &sequence
->parent
;
1902 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1903 struct bt_ctf_field_type
*type
)
1905 struct bt_ctf_field_type
*ret
= NULL
;
1906 struct bt_ctf_field_type_sequence
*sequence
;
1908 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1912 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1914 ret
= sequence
->element_type
;
1921 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1922 struct bt_ctf_field_type
*element_type
)
1925 struct bt_ctf_field_type_sequence
*sequence
;
1927 if (!type
|| !element_type
||
1928 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1933 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1935 if (sequence
->element_type
) {
1936 BT_PUT(sequence
->element_type
);
1939 sequence
->element_type
= element_type
;
1940 bt_get(sequence
->element_type
);
1947 const char *bt_ctf_field_type_sequence_get_length_field_name(
1948 struct bt_ctf_field_type
*type
)
1950 const char *ret
= NULL
;
1951 struct bt_ctf_field_type_sequence
*sequence
;
1953 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1957 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1959 ret
= sequence
->length_field_name
->str
;
1964 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1966 struct bt_ctf_field_type_string
*string
=
1967 g_new0(struct bt_ctf_field_type_string
, 1);
1973 string
->parent
.declaration
= &string
->declaration
.p
;
1974 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
1975 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1976 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1977 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1978 return &string
->parent
;
1982 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1983 struct bt_ctf_field_type
*type
)
1985 struct bt_ctf_field_type_string
*string
;
1986 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1988 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
1992 string
= container_of(type
, struct bt_ctf_field_type_string
,
1994 ret
= string
->declaration
.encoding
;
1999 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2000 enum bt_ctf_string_encoding encoding
)
2003 struct bt_ctf_field_type_string
*string
;
2005 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2006 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2007 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2012 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2013 string
->declaration
.encoding
= encoding
;
2019 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2022 enum bt_ctf_type_id type_id
;
2030 ret
= (int) type
->declaration
->alignment
;
2034 type_id
= bt_ctf_field_type_get_type_id(type
);
2036 case BT_CTF_TYPE_ID_SEQUENCE
:
2038 struct bt_ctf_field_type
*element
=
2039 bt_ctf_field_type_sequence_get_element_type(type
);
2046 ret
= bt_ctf_field_type_get_alignment(element
);
2050 case BT_CTF_TYPE_ID_ARRAY
:
2052 struct bt_ctf_field_type
*element
=
2053 bt_ctf_field_type_array_get_element_type(type
);
2060 ret
= bt_ctf_field_type_get_alignment(element
);
2064 case BT_CTF_TYPE_ID_STRUCT
:
2066 int i
, element_count
;
2068 element_count
= bt_ctf_field_type_structure_get_field_count(
2070 if (element_count
< 0) {
2071 ret
= element_count
;
2075 for (i
= 0; i
< element_count
; i
++) {
2076 struct bt_ctf_field_type
*field
;
2077 int field_alignment
;
2079 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2086 field_alignment
= bt_ctf_field_type_get_alignment(
2089 if (field_alignment
< 0) {
2090 ret
= field_alignment
;
2094 type
->declaration
->alignment
= MAX(field_alignment
,
2095 type
->declaration
->alignment
);
2097 ret
= (int) type
->declaration
->alignment
;
2100 case BT_CTF_TYPE_ID_UNKNOWN
:
2104 ret
= (int) type
->declaration
->alignment
;
2112 int is_power_of_two(unsigned int value
)
2114 return ((value
& (value
- 1)) == 0) && value
> 0;
2117 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2118 unsigned int alignment
)
2121 enum bt_ctf_type_id type_id
;
2123 /* Alignment must be a power of two */
2124 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2129 type_id
= bt_ctf_field_type_get_type_id(type
);
2130 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2135 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2136 alignment
!= CHAR_BIT
) {
2141 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2142 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2143 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2144 /* Setting an alignment on these types makes no sense */
2149 type
->declaration
->alignment
= alignment
;
2156 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2157 struct bt_ctf_field_type
*type
)
2159 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2165 switch (type
->declaration
->id
) {
2166 case BT_CTF_TYPE_ID_INTEGER
:
2168 struct bt_ctf_field_type_integer
*integer
= container_of(
2169 type
, struct bt_ctf_field_type_integer
, parent
);
2170 ret
= integer
->user_byte_order
;
2173 case BT_CTF_TYPE_ID_FLOAT
:
2175 struct bt_ctf_field_type_floating_point
*floating_point
=
2177 struct bt_ctf_field_type_floating_point
,
2179 ret
= floating_point
->user_byte_order
;
2186 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2187 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2188 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2189 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2195 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2196 enum bt_ctf_byte_order byte_order
)
2199 int internal_byte_order
;
2200 enum bt_ctf_type_id type_id
;
2202 if (!type
|| type
->frozen
) {
2207 switch (byte_order
) {
2208 case BT_CTF_BYTE_ORDER_NATIVE
:
2209 /* Leave unset. Will be initialized by parent. */
2210 internal_byte_order
= 0;
2212 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2213 internal_byte_order
= LITTLE_ENDIAN
;
2215 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2216 case BT_CTF_BYTE_ORDER_NETWORK
:
2217 internal_byte_order
= BIG_ENDIAN
;
2224 type_id
= type
->declaration
->id
;
2225 if (set_byte_order_funcs
[type_id
]) {
2226 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2233 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2234 struct bt_ctf_field_type
*type
)
2237 return BT_CTF_TYPE_ID_UNKNOWN
;
2240 return type
->declaration
->id
;
2244 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2246 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2250 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2252 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2256 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2258 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2262 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2264 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2268 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2270 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2274 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2276 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2280 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2282 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2286 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2288 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2291 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2296 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2302 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2312 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2313 struct bt_ctf_field_type_variant
*variant
,
2316 struct bt_ctf_field_type
*type
= NULL
;
2317 GQuark field_name_quark
;
2319 struct structure_field
*field_entry
;
2320 struct range_overlap_query query
= {
2321 .range_start
._signed
= tag_value
,
2322 .range_end
._signed
= tag_value
,
2323 .mapping_name
= 0, .overlaps
= 0};
2325 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2327 if (!query
.overlaps
) {
2331 field_name_quark
= query
.mapping_name
;
2332 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2333 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2337 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2338 type
= field_entry
->type
;
2344 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2345 struct bt_ctf_field_type_variant
*variant
,
2348 struct bt_ctf_field_type
*type
= NULL
;
2349 GQuark field_name_quark
;
2351 struct structure_field
*field_entry
;
2352 struct range_overlap_query query
= {
2353 .range_start
._unsigned
= tag_value
,
2354 .range_end
._unsigned
= tag_value
,
2355 .mapping_name
= 0, .overlaps
= 0};
2357 g_ptr_array_foreach(variant
->tag
->entries
,
2358 check_ranges_overlap_unsigned
,
2360 if (!query
.overlaps
) {
2364 field_name_quark
= query
.mapping_name
;
2365 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2366 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2370 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2371 type
= field_entry
->type
;
2377 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2378 struct metadata_context
*context
)
2382 if (!type
|| !context
) {
2387 /* Make sure field type is valid before serializing it */
2388 ret
= bt_ctf_field_type_validate(type
);
2394 ret
= type
->serialize(type
, context
);
2400 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2407 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2408 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2409 set_byte_order_funcs
[type
->declaration
->id
](type
,
2415 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2417 struct bt_ctf_field_type
*copy
= NULL
;
2423 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2429 int bt_ctf_field_type_structure_get_field_name_index(
2430 struct bt_ctf_field_type
*type
, const char *name
)
2435 struct bt_ctf_field_type_structure
*structure
;
2437 if (!type
|| !name
||
2438 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2443 name_quark
= g_quark_try_string(name
);
2449 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2451 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2452 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2462 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2463 struct bt_ctf_field_type
*field
, int index
)
2466 struct bt_ctf_field_type_structure
*structure
;
2468 if (!type
|| !field
||
2469 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2474 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2476 if (index
< 0 || index
>= structure
->fields
->len
) {
2482 bt_put(((struct structure_field
*)
2483 g_ptr_array_index(structure
->fields
, index
))->type
);
2484 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2491 int bt_ctf_field_type_variant_get_field_name_index(
2492 struct bt_ctf_field_type
*type
, const char *name
)
2497 struct bt_ctf_field_type_variant
*variant
;
2499 if (!type
|| !name
||
2500 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2505 name_quark
= g_quark_try_string(name
);
2511 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2513 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2514 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2524 int bt_ctf_field_type_sequence_set_length_field_path(
2525 struct bt_ctf_field_type
*type
,
2526 struct bt_ctf_field_path
*path
)
2529 struct bt_ctf_field_type_sequence
*sequence
;
2531 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2532 BT_CTF_TYPE_ID_SEQUENCE
) {
2537 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2540 BT_MOVE(sequence
->length_field_path
, path
);
2546 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2547 struct bt_ctf_field_path
*path
)
2550 struct bt_ctf_field_type_variant
*variant
;
2552 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2553 BT_CTF_TYPE_ID_VARIANT
) {
2558 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2561 BT_MOVE(variant
->tag_field_path
, path
);
2567 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2568 struct bt_ctf_field_type
*tag
)
2571 struct bt_ctf_field_type_variant
*variant
;
2573 if (!type
|| !tag
||
2574 bt_ctf_field_type_get_type_id(tag
) !=
2575 BT_CTF_TYPE_ID_ENUM
) {
2580 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2584 bt_put(&variant
->tag
->parent
);
2586 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2593 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2594 struct bt_ctf_field_type
*field
, int index
)
2597 struct bt_ctf_field_type_variant
*variant
;
2599 if (!type
|| !field
||
2600 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2605 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2607 if (index
< 0 || index
>= variant
->fields
->len
) {
2613 bt_put(((struct structure_field
*)
2614 g_ptr_array_index(variant
->fields
, index
))->type
);
2615 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2622 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2624 struct bt_ctf_field_type_integer
*integer
=
2625 (struct bt_ctf_field_type_integer
*) type
;
2631 bt_put(integer
->mapped_clock
);
2636 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2638 struct bt_ctf_field_type_enumeration
*enumeration
=
2639 (struct bt_ctf_field_type_enumeration
*) type
;
2645 g_ptr_array_free(enumeration
->entries
, TRUE
);
2646 bt_put(enumeration
->container
);
2647 g_free(enumeration
);
2651 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2653 struct bt_ctf_field_type_floating_point
*floating_point
=
2654 (struct bt_ctf_field_type_floating_point
*) type
;
2660 g_free(floating_point
);
2664 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2666 struct bt_ctf_field_type_structure
*structure
=
2667 (struct bt_ctf_field_type_structure
*) type
;
2673 g_ptr_array_free(structure
->fields
, TRUE
);
2674 g_hash_table_destroy(structure
->field_name_to_index
);
2679 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2681 struct bt_ctf_field_type_variant
*variant
=
2682 (struct bt_ctf_field_type_variant
*) type
;
2688 g_ptr_array_free(variant
->fields
, TRUE
);
2689 g_hash_table_destroy(variant
->field_name_to_index
);
2690 g_string_free(variant
->tag_name
, TRUE
);
2691 bt_put(&variant
->tag
->parent
);
2692 BT_PUT(variant
->tag_field_path
);
2697 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2699 struct bt_ctf_field_type_array
*array
=
2700 (struct bt_ctf_field_type_array
*) type
;
2706 bt_put(array
->element_type
);
2711 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2713 struct bt_ctf_field_type_sequence
*sequence
=
2714 (struct bt_ctf_field_type_sequence
*) type
;
2720 bt_put(sequence
->element_type
);
2721 g_string_free(sequence
->length_field_name
, TRUE
);
2722 BT_PUT(sequence
->length_field_path
);
2727 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2729 struct bt_ctf_field_type_string
*string
=
2730 (struct bt_ctf_field_type_string
*) type
;
2740 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2746 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2748 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2749 type
, struct bt_ctf_field_type_integer
, parent
);
2751 if (integer_type
->mapped_clock
) {
2752 bt_ctf_clock_freeze(integer_type
->mapped_clock
);
2755 generic_field_type_freeze(type
);
2759 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2761 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2762 type
, struct bt_ctf_field_type_enumeration
, parent
);
2764 generic_field_type_freeze(type
);
2765 bt_ctf_field_type_freeze(enumeration_type
->container
);
2769 void freeze_structure_field(struct structure_field
*field
)
2771 bt_ctf_field_type_freeze(field
->type
);
2775 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2777 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2778 type
, struct bt_ctf_field_type_structure
, parent
);
2780 /* Cache the alignment */
2781 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2782 generic_field_type_freeze(type
);
2783 g_ptr_array_foreach(structure_type
->fields
,
2784 (GFunc
) freeze_structure_field
, NULL
);
2788 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2790 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2791 type
, struct bt_ctf_field_type_variant
, parent
);
2793 generic_field_type_freeze(type
);
2794 g_ptr_array_foreach(variant_type
->fields
,
2795 (GFunc
) freeze_structure_field
, NULL
);
2799 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2801 struct bt_ctf_field_type_array
*array_type
= container_of(
2802 type
, struct bt_ctf_field_type_array
, parent
);
2804 /* Cache the alignment */
2805 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2806 generic_field_type_freeze(type
);
2807 bt_ctf_field_type_freeze(array_type
->element_type
);
2811 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2813 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2814 type
, struct bt_ctf_field_type_sequence
, parent
);
2816 /* Cache the alignment */
2817 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2818 generic_field_type_freeze(type
);
2819 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2823 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2825 const char *encoding_string
;
2828 case BT_CTF_STRING_ENCODING_NONE
:
2829 encoding_string
= "none";
2831 case BT_CTF_STRING_ENCODING_ASCII
:
2832 encoding_string
= "ASCII";
2834 case BT_CTF_STRING_ENCODING_UTF8
:
2835 encoding_string
= "UTF8";
2838 encoding_string
= "unknown";
2842 return encoding_string
;
2846 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2848 const char *base_string
;
2851 case BT_CTF_INTEGER_BASE_DECIMAL
:
2852 base_string
= "decimal";
2854 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2855 base_string
= "hexadecimal";
2857 case BT_CTF_INTEGER_BASE_OCTAL
:
2858 base_string
= "octal";
2860 case BT_CTF_INTEGER_BASE_BINARY
:
2861 base_string
= "binary";
2864 base_string
= "unknown";
2872 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2873 struct metadata_context
*context
)
2875 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2876 struct bt_ctf_field_type_integer
, parent
);
2879 g_string_append_printf(context
->string
,
2880 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2881 integer
->declaration
.len
, type
->declaration
->alignment
,
2882 (integer
->declaration
.signedness
? "true" : "false"),
2883 get_encoding_string(integer
->declaration
.encoding
),
2884 get_integer_base_string(integer
->declaration
.base
),
2885 get_byte_order_string(integer
->declaration
.byte_order
));
2886 if (integer
->mapped_clock
) {
2887 const char *clock_name
= bt_ctf_clock_get_name(
2888 integer
->mapped_clock
);
2895 g_string_append_printf(context
->string
,
2896 "; map = clock.%s.value", clock_name
);
2899 g_string_append(context
->string
, "; }");
2905 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2906 struct metadata_context
*context
)
2910 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2911 struct bt_ctf_field_type_enumeration
, parent
);
2912 struct bt_ctf_field_type
*container_type
;
2913 int container_signed
;
2915 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2916 if (!container_type
) {
2921 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2922 if (container_signed
< 0) {
2923 ret
= container_signed
;
2924 goto error_put_container_type
;
2927 g_string_append(context
->string
, "enum : ");
2928 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2930 goto error_put_container_type
;
2933 g_string_append(context
->string
, " { ");
2934 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2935 struct enumeration_mapping
*mapping
=
2936 enumeration
->entries
->pdata
[entry
];
2938 if (container_signed
) {
2939 if (mapping
->range_start
._signed
==
2940 mapping
->range_end
._signed
) {
2941 g_string_append_printf(context
->string
,
2942 "\"%s\" = %" PRId64
,
2943 g_quark_to_string(mapping
->string
),
2944 mapping
->range_start
._signed
);
2946 g_string_append_printf(context
->string
,
2947 "\"%s\" = %" PRId64
" ... %" PRId64
,
2948 g_quark_to_string(mapping
->string
),
2949 mapping
->range_start
._signed
,
2950 mapping
->range_end
._signed
);
2953 if (mapping
->range_start
._unsigned
==
2954 mapping
->range_end
._unsigned
) {
2955 g_string_append_printf(context
->string
,
2956 "\"%s\" = %" PRIu64
,
2957 g_quark_to_string(mapping
->string
),
2958 mapping
->range_start
._unsigned
);
2960 g_string_append_printf(context
->string
,
2961 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2962 g_quark_to_string(mapping
->string
),
2963 mapping
->range_start
._unsigned
,
2964 mapping
->range_end
._unsigned
);
2968 g_string_append(context
->string
,
2969 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2973 if (context
->field_name
->len
) {
2974 g_string_append_printf(context
->string
, " %s",
2975 context
->field_name
->str
);
2976 g_string_assign(context
->field_name
, "");
2978 error_put_container_type
:
2979 bt_put(container_type
);
2985 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2986 struct metadata_context
*context
)
2988 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2989 type
, struct bt_ctf_field_type_floating_point
, parent
);
2991 g_string_append_printf(context
->string
,
2992 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2993 floating_point
->declaration
.exp
->len
,
2994 floating_point
->declaration
.mantissa
->len
+ 1,
2995 get_byte_order_string(floating_point
->declaration
.byte_order
),
2996 type
->declaration
->alignment
);
3001 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3002 struct metadata_context
*context
)
3005 unsigned int indent
;
3007 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3008 struct bt_ctf_field_type_structure
, parent
);
3009 GString
*structure_field_name
= context
->field_name
;
3011 context
->field_name
= g_string_new("");
3013 context
->current_indentation_level
++;
3014 g_string_append(context
->string
, "struct {\n");
3016 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3017 struct structure_field
*field
;
3019 for (indent
= 0; indent
< context
->current_indentation_level
;
3021 g_string_append_c(context
->string
, '\t');
3024 field
= structure
->fields
->pdata
[i
];
3025 g_string_assign(context
->field_name
,
3026 g_quark_to_string(field
->name
));
3027 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3032 if (context
->field_name
->len
) {
3033 g_string_append_printf(context
->string
, " %s",
3034 context
->field_name
->str
);
3036 g_string_append(context
->string
, ";\n");
3039 context
->current_indentation_level
--;
3040 for (indent
= 0; indent
< context
->current_indentation_level
;
3042 g_string_append_c(context
->string
, '\t');
3045 g_string_append_printf(context
->string
, "} align(%zu)",
3046 type
->declaration
->alignment
);
3048 g_string_free(context
->field_name
, TRUE
);
3049 context
->field_name
= structure_field_name
;
3054 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3055 struct metadata_context
*context
)
3058 unsigned int indent
;
3060 struct bt_ctf_field_type_variant
*variant
= container_of(
3061 type
, struct bt_ctf_field_type_variant
, parent
);
3062 GString
*variant_field_name
= context
->field_name
;
3064 context
->field_name
= g_string_new("");
3065 if (variant
->tag_name
->len
> 0) {
3066 g_string_append_printf(context
->string
,
3067 "variant <%s> {\n", variant
->tag_name
->str
);
3069 g_string_append(context
->string
, "variant {\n");
3072 context
->current_indentation_level
++;
3073 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3074 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3076 g_string_assign(context
->field_name
,
3077 g_quark_to_string(field
->name
));
3078 for (indent
= 0; indent
< context
->current_indentation_level
;
3080 g_string_append_c(context
->string
, '\t');
3083 g_string_assign(context
->field_name
,
3084 g_quark_to_string(field
->name
));
3085 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3090 if (context
->field_name
->len
) {
3091 g_string_append_printf(context
->string
, " %s;",
3092 context
->field_name
->str
);
3095 g_string_append_c(context
->string
, '\n');
3098 context
->current_indentation_level
--;
3099 for (indent
= 0; indent
< context
->current_indentation_level
;
3101 g_string_append_c(context
->string
, '\t');
3104 g_string_append(context
->string
, "}");
3106 g_string_free(context
->field_name
, TRUE
);
3107 context
->field_name
= variant_field_name
;
3112 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3113 struct metadata_context
*context
)
3116 struct bt_ctf_field_type_array
*array
= container_of(type
,
3117 struct bt_ctf_field_type_array
, parent
);
3119 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3124 if (context
->field_name
->len
) {
3125 g_string_append_printf(context
->string
, " %s[%u]",
3126 context
->field_name
->str
, array
->length
);
3127 g_string_assign(context
->field_name
, "");
3129 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3136 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3137 struct metadata_context
*context
)
3140 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3141 type
, struct bt_ctf_field_type_sequence
, parent
);
3143 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3148 if (context
->field_name
->len
) {
3149 g_string_append_printf(context
->string
, " %s[%s]",
3150 context
->field_name
->str
,
3151 sequence
->length_field_name
->str
);
3152 g_string_assign(context
->field_name
, "");
3154 g_string_append_printf(context
->string
, "[%s]",
3155 sequence
->length_field_name
->str
);
3162 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3163 struct metadata_context
*context
)
3165 struct bt_ctf_field_type_string
*string
= container_of(
3166 type
, struct bt_ctf_field_type_string
, parent
);
3168 g_string_append_printf(context
->string
,
3169 "string { encoding = %s; }",
3170 get_encoding_string(string
->declaration
.encoding
));
3175 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3176 enum bt_ctf_byte_order ret
;
3178 switch (byte_order
) {
3179 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3181 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3183 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3185 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3187 case BT_CTF_BYTE_ORDER_NETWORK
:
3188 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3190 case BT_CTF_BYTE_ORDER_NATIVE
:
3191 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3194 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3202 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3203 int byte_order
, int set_native
)
3205 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3206 struct bt_ctf_field_type_integer
, parent
);
3209 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3211 * User byte order is native, so we can set
3212 * the real byte order.
3214 integer_type
->declaration
.byte_order
=
3218 integer_type
->user_byte_order
=
3219 get_ctf_ir_byte_order(byte_order
);
3220 integer_type
->declaration
.byte_order
= byte_order
;
3225 void bt_ctf_field_type_enumeration_set_byte_order(
3226 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3228 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3229 struct bt_ctf_field_type_enumeration
, parent
);
3231 /* Safe to assume that container is an integer */
3232 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3233 byte_order
, set_native
);
3237 void bt_ctf_field_type_floating_point_set_byte_order(
3238 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3240 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3241 container_of(type
, struct bt_ctf_field_type_floating_point
,
3245 if (floating_point_type
->user_byte_order
==
3246 BT_CTF_BYTE_ORDER_NATIVE
) {
3248 * User byte order is native, so we can set
3249 * the real byte order.
3251 floating_point_type
->declaration
.byte_order
=
3253 floating_point_type
->sign
.byte_order
=
3255 floating_point_type
->mantissa
.byte_order
=
3257 floating_point_type
->exp
.byte_order
=
3261 floating_point_type
->user_byte_order
=
3262 get_ctf_ir_byte_order(byte_order
);
3263 floating_point_type
->declaration
.byte_order
= byte_order
;
3264 floating_point_type
->sign
.byte_order
= byte_order
;
3265 floating_point_type
->mantissa
.byte_order
= byte_order
;
3266 floating_point_type
->exp
.byte_order
= byte_order
;
3271 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3272 int byte_order
, int set_native
)
3275 struct bt_ctf_field_type_structure
*structure_type
=
3276 container_of(type
, struct bt_ctf_field_type_structure
,
3279 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3280 struct structure_field
*field
= g_ptr_array_index(
3281 structure_type
->fields
, i
);
3282 struct bt_ctf_field_type
*field_type
= field
->type
;
3284 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3285 set_byte_order_funcs
[field_type
->declaration
->id
](
3286 field_type
, byte_order
, set_native
);
3292 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3293 int byte_order
, int set_native
)
3296 struct bt_ctf_field_type_variant
*variant_type
=
3297 container_of(type
, struct bt_ctf_field_type_variant
,
3300 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3301 struct structure_field
*field
= g_ptr_array_index(
3302 variant_type
->fields
, i
);
3303 struct bt_ctf_field_type
*field_type
= field
->type
;
3305 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3306 set_byte_order_funcs
[field_type
->declaration
->id
](
3307 field_type
, byte_order
, set_native
);
3313 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3314 int byte_order
, int set_native
)
3316 struct bt_ctf_field_type_array
*array_type
=
3317 container_of(type
, struct bt_ctf_field_type_array
,
3320 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3321 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3322 array_type
->element_type
, byte_order
, set_native
);
3327 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3328 int byte_order
, int set_native
)
3330 struct bt_ctf_field_type_sequence
*sequence_type
=
3331 container_of(type
, struct bt_ctf_field_type_sequence
,
3334 if (set_byte_order_funcs
[
3335 sequence_type
->element_type
->declaration
->id
]) {
3336 set_byte_order_funcs
[
3337 sequence_type
->element_type
->declaration
->id
](
3338 sequence_type
->element_type
, byte_order
, set_native
);
3343 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3344 struct bt_ctf_field_type
*type
)
3346 struct bt_ctf_field_type
*copy
;
3347 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3349 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3350 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3355 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3357 copy_integer
->declaration
= integer
->declaration
;
3358 if (integer
->mapped_clock
) {
3359 bt_get(integer
->mapped_clock
);
3360 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3363 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3370 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3371 struct bt_ctf_field_type
*type
)
3374 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3375 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3377 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3380 /* Copy the source enumeration's container */
3381 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3382 if (!copy_container
) {
3386 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3390 copy_enumeration
= container_of(copy
,
3391 struct bt_ctf_field_type_enumeration
, parent
);
3393 /* Copy all enumaration entries */
3394 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3395 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3396 enumeration
->entries
, i
);
3397 struct enumeration_mapping
* copy_mapping
= g_new0(
3398 struct enumeration_mapping
, 1);
3400 if (!copy_mapping
) {
3404 *copy_mapping
= *mapping
;
3405 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3408 copy_enumeration
->declaration
= enumeration
->declaration
;
3410 bt_put(copy_container
);
3413 bt_put(copy_container
);
3419 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3420 struct bt_ctf_field_type
*type
)
3422 struct bt_ctf_field_type
*copy
;
3423 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3425 floating_point
= container_of(type
,
3426 struct bt_ctf_field_type_floating_point
, parent
);
3427 copy
= bt_ctf_field_type_floating_point_create();
3432 copy_float
= container_of(copy
,
3433 struct bt_ctf_field_type_floating_point
, parent
);
3434 copy_float
->declaration
= floating_point
->declaration
;
3435 copy_float
->sign
= floating_point
->sign
;
3436 copy_float
->mantissa
= floating_point
->mantissa
;
3437 copy_float
->exp
= floating_point
->exp
;
3438 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3439 copy_float
->declaration
.sign
= ©_float
->sign
;
3440 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3441 copy_float
->declaration
.exp
= ©_float
->exp
;
3447 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3448 struct bt_ctf_field_type
*type
)
3451 GHashTableIter iter
;
3452 gpointer key
, value
;
3453 struct bt_ctf_field_type
*copy
;
3454 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3456 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3458 copy
= bt_ctf_field_type_structure_create();
3463 copy_structure
= container_of(copy
,
3464 struct bt_ctf_field_type_structure
, parent
);
3466 /* Copy field_name_to_index */
3467 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3468 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3469 g_hash_table_insert(copy_structure
->field_name_to_index
,
3473 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3474 struct structure_field
*entry
, *copy_entry
;
3475 struct bt_ctf_field_type
*copy_field
;
3477 copy_entry
= g_new0(struct structure_field
, 1);
3482 entry
= g_ptr_array_index(structure
->fields
, i
);
3483 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3489 copy_entry
->name
= entry
->name
;
3490 copy_entry
->type
= copy_field
;
3491 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3494 copy_structure
->declaration
= structure
->declaration
;
3503 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3504 struct bt_ctf_field_type
*type
)
3507 GHashTableIter iter
;
3508 gpointer key
, value
;
3509 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3510 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3512 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3515 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3521 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3522 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3527 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3530 /* Copy field_name_to_index */
3531 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3532 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3533 g_hash_table_insert(copy_variant
->field_name_to_index
,
3537 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3538 struct structure_field
*entry
, *copy_entry
;
3539 struct bt_ctf_field_type
*copy_field
;
3541 copy_entry
= g_new0(struct structure_field
, 1);
3546 entry
= g_ptr_array_index(variant
->fields
, i
);
3547 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3553 copy_entry
->name
= entry
->name
;
3554 copy_entry
->type
= copy_field
;
3555 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3558 copy_variant
->declaration
= variant
->declaration
;
3559 if (variant
->tag_field_path
) {
3560 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3561 variant
->tag_field_path
);
3562 if (!copy_variant
->tag_field_path
) {
3576 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3577 struct bt_ctf_field_type
*type
)
3579 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3580 struct bt_ctf_field_type_array
*array
, *copy_array
;
3582 array
= container_of(type
, struct bt_ctf_field_type_array
,
3584 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3585 if (!copy_element
) {
3589 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3594 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3596 copy_array
->declaration
= array
->declaration
;
3598 bt_put(copy_element
);
3603 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3604 struct bt_ctf_field_type
*type
)
3606 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3607 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3609 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3611 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3612 if (!copy_element
) {
3616 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3617 sequence
->length_field_name
->len
?
3618 sequence
->length_field_name
->str
: NULL
);
3623 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3625 copy_sequence
->declaration
= sequence
->declaration
;
3626 if (sequence
->length_field_path
) {
3627 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3628 sequence
->length_field_path
);
3629 if (!copy_sequence
->length_field_path
) {
3634 bt_put(copy_element
);
3642 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3643 struct bt_ctf_field_type
*type
)
3645 struct bt_ctf_field_type
*copy
;
3646 struct bt_ctf_field_type_string
*string
, *copy_string
;
3648 copy
= bt_ctf_field_type_string_create();
3653 string
= container_of(type
, struct bt_ctf_field_type_string
,
3655 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3657 copy_string
->declaration
= string
->declaration
;
3663 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3664 struct bt_ctf_field_type
*type_b
)
3667 struct bt_ctf_field_type_integer
*integer_a
;
3668 struct bt_ctf_field_type_integer
*integer_b
;
3669 struct declaration_integer
*decl_a
;
3670 struct declaration_integer
*decl_b
;
3672 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3674 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3676 decl_a
= &integer_a
->declaration
;
3677 decl_b
= &integer_b
->declaration
;
3680 if (decl_a
->len
!= decl_b
->len
) {
3685 * Compare user byte orders only, not the cached,
3688 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3693 if (decl_a
->signedness
!= decl_b
->signedness
) {
3698 if (decl_a
->base
!= decl_b
->base
) {
3703 if (decl_a
->encoding
!= decl_b
->encoding
) {
3708 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3720 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3721 struct bt_ctf_field_type
*type_b
)
3724 struct bt_ctf_field_type_floating_point
*float_a
;
3725 struct bt_ctf_field_type_floating_point
*float_b
;
3727 float_a
= container_of(type_a
,
3728 struct bt_ctf_field_type_floating_point
, parent
);
3729 float_b
= container_of(type_b
,
3730 struct bt_ctf_field_type_floating_point
, parent
);
3733 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3737 /* Exponent length */
3738 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3742 /* Mantissa length */
3743 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3748 * Compare user byte orders only, not the cached,
3751 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3763 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3764 struct enumeration_mapping
*mapping_b
)
3769 if (mapping_a
->string
!= mapping_b
->string
) {
3774 if (mapping_a
->range_start
._unsigned
!=
3775 mapping_b
->range_start
._unsigned
) {
3780 if (mapping_a
->range_end
._unsigned
!=
3781 mapping_b
->range_end
._unsigned
) {
3793 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3794 struct bt_ctf_field_type
*type_b
)
3798 struct bt_ctf_field_type_enumeration
*enum_a
;
3799 struct bt_ctf_field_type_enumeration
*enum_b
;
3801 enum_a
= container_of(type_a
,
3802 struct bt_ctf_field_type_enumeration
, parent
);
3803 enum_b
= container_of(type_b
,
3804 struct bt_ctf_field_type_enumeration
, parent
);
3806 /* Container field type */
3807 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3815 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3819 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3820 struct enumeration_mapping
*mapping_a
=
3821 g_ptr_array_index(enum_a
->entries
, i
);
3822 struct enumeration_mapping
*mapping_b
=
3823 g_ptr_array_index(enum_b
->entries
, i
);
3825 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3838 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3839 struct bt_ctf_field_type
*type_b
)
3842 struct bt_ctf_field_type_string
*string_a
;
3843 struct bt_ctf_field_type_string
*string_b
;
3845 string_a
= container_of(type_a
,
3846 struct bt_ctf_field_type_string
, parent
);
3847 string_b
= container_of(type_b
,
3848 struct bt_ctf_field_type_string
, parent
);
3851 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3863 int compare_structure_fields(struct structure_field
*field_a
,
3864 struct structure_field
*field_b
)
3869 if (field_a
->name
!= field_b
->name
) {
3874 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3881 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3882 struct bt_ctf_field_type
*type_b
)
3886 struct bt_ctf_field_type_structure
*struct_a
;
3887 struct bt_ctf_field_type_structure
*struct_b
;
3889 struct_a
= container_of(type_a
,
3890 struct bt_ctf_field_type_structure
, parent
);
3891 struct_b
= container_of(type_b
,
3892 struct bt_ctf_field_type_structure
, parent
);
3895 if (bt_ctf_field_type_get_alignment(type_a
) !=
3896 bt_ctf_field_type_get_alignment(type_b
)) {
3901 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3905 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3906 struct structure_field
*field_a
=
3907 g_ptr_array_index(struct_a
->fields
, i
);
3908 struct structure_field
*field_b
=
3909 g_ptr_array_index(struct_b
->fields
, i
);
3911 ret
= compare_structure_fields(field_a
, field_b
);
3925 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3926 struct bt_ctf_field_type
*type_b
)
3930 struct bt_ctf_field_type_variant
*variant_a
;
3931 struct bt_ctf_field_type_variant
*variant_b
;
3933 variant_a
= container_of(type_a
,
3934 struct bt_ctf_field_type_variant
, parent
);
3935 variant_b
= container_of(type_b
,
3936 struct bt_ctf_field_type_variant
, parent
);
3939 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3944 ret
= bt_ctf_field_type_compare(
3945 (struct bt_ctf_field_type
*) variant_a
->tag
,
3946 (struct bt_ctf_field_type
*) variant_b
->tag
);
3954 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3958 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3959 struct structure_field
*field_a
=
3960 g_ptr_array_index(variant_a
->fields
, i
);
3961 struct structure_field
*field_b
=
3962 g_ptr_array_index(variant_b
->fields
, i
);
3964 ret
= compare_structure_fields(field_a
, field_b
);
3978 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3979 struct bt_ctf_field_type
*type_b
)
3982 struct bt_ctf_field_type_array
*array_a
;
3983 struct bt_ctf_field_type_array
*array_b
;
3985 array_a
= container_of(type_a
,
3986 struct bt_ctf_field_type_array
, parent
);
3987 array_b
= container_of(type_b
,
3988 struct bt_ctf_field_type_array
, parent
);
3991 if (array_a
->length
!= array_b
->length
) {
3996 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3997 array_b
->element_type
);
4004 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4005 struct bt_ctf_field_type
*type_b
)
4008 struct bt_ctf_field_type_sequence
*sequence_a
;
4009 struct bt_ctf_field_type_sequence
*sequence_b
;
4011 sequence_a
= container_of(type_a
,
4012 struct bt_ctf_field_type_sequence
, parent
);
4013 sequence_b
= container_of(type_b
,
4014 struct bt_ctf_field_type_sequence
, parent
);
4017 if (strcmp(sequence_a
->length_field_name
->str
,
4018 sequence_b
->length_field_name
->str
)) {
4023 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4024 sequence_b
->element_type
);
4031 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4032 struct bt_ctf_field_type
*type_b
)
4036 if (type_a
== type_b
) {
4037 /* Same reference: equal (even if both are NULL) */
4042 if (!type_a
|| !type_b
) {
4047 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4048 /* Different type IDs */
4052 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4053 /* Both have unknown type IDs */
4057 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4064 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4066 int field_count
= -1;
4067 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4070 case CTF_TYPE_STRUCT
:
4072 bt_ctf_field_type_structure_get_field_count(field_type
);
4074 case CTF_TYPE_VARIANT
:
4076 bt_ctf_field_type_variant_get_field_count(field_type
);
4078 case CTF_TYPE_ARRAY
:
4079 case CTF_TYPE_SEQUENCE
:
4081 * Array and sequence types always contain a single member
4082 * (the element type).
4094 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4095 struct bt_ctf_field_type
*field_type
, int index
)
4097 struct bt_ctf_field_type
*field
= NULL
;
4098 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4101 case CTF_TYPE_STRUCT
:
4102 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4105 case CTF_TYPE_VARIANT
:
4107 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4115 case CTF_TYPE_ARRAY
:
4116 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4118 case CTF_TYPE_SEQUENCE
:
4119 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4129 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4132 int field_index
= -1;
4133 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4136 case CTF_TYPE_STRUCT
:
4137 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4140 case CTF_TYPE_VARIANT
:
4141 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4152 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4153 struct bt_ctf_field_type
*type
)
4155 struct bt_ctf_field_path
*field_path
= NULL
;
4156 struct bt_ctf_field_type_variant
*variant
;
4158 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4162 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4164 field_path
= bt_get(variant
->tag_field_path
);
4170 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4171 struct bt_ctf_field_type
*type
)
4173 struct bt_ctf_field_path
*field_path
= NULL
;
4174 struct bt_ctf_field_type_sequence
*sequence
;
4176 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4180 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4182 field_path
= bt_get(sequence
->length_field_path
);