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_enumeration_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
282 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
285 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
286 [BT_CTF_TYPE_ID_INTEGER
] = NULL
,
287 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
288 [BT_CTF_TYPE_ID_STRING
] = NULL
,
289 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
290 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
291 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
292 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
293 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
297 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
303 void destroy_structure_field(struct structure_field
*field
)
310 void check_ranges_overlap(gpointer element
, gpointer query
)
312 struct enumeration_mapping
*mapping
= element
;
313 struct range_overlap_query
*overlap_query
= query
;
315 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
316 && overlap_query
->range_start
._signed
<=
317 mapping
->range_end
._signed
) {
318 overlap_query
->overlaps
= 1;
319 overlap_query
->mapping_name
= mapping
->string
;
322 overlap_query
->overlaps
|=
323 mapping
->string
== overlap_query
->mapping_name
;
327 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
329 struct enumeration_mapping
*mapping
= element
;
330 struct range_overlap_query
*overlap_query
= query
;
332 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
333 && overlap_query
->range_start
._unsigned
<=
334 mapping
->range_end
._unsigned
) {
335 overlap_query
->overlaps
= 1;
336 overlap_query
->mapping_name
= mapping
->string
;
339 overlap_query
->overlaps
|=
340 mapping
->string
== overlap_query
->mapping_name
;
344 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
345 struct enumeration_mapping
**b
)
347 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
351 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
352 struct enumeration_mapping
**b
)
354 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
358 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
360 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
362 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
363 (type_id
< BT_CTF_NR_TYPE_IDS
));
365 bt_object_init(type
, bt_ctf_field_type_destroy
);
366 type
->freeze
= type_freeze_funcs
[type_id
];
367 type
->serialize
= type_serialize_funcs
[type_id
];
370 int ret
= bt_ctf_field_type_set_byte_order(type
,
371 BT_CTF_BYTE_ORDER_NATIVE
);
375 type
->declaration
->alignment
= 1;
379 int add_structure_field(GPtrArray
*fields
,
380 GHashTable
*field_name_to_index
,
381 struct bt_ctf_field_type
*field_type
,
382 const char *field_name
)
385 GQuark name_quark
= g_quark_from_string(field_name
);
386 struct structure_field
*field
;
388 /* Make sure structure does not contain a field of the same name */
389 if (g_hash_table_lookup_extended(field_name_to_index
,
390 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
395 field
= g_new0(struct structure_field
, 1);
402 field
->name
= name_quark
;
403 field
->type
= field_type
;
404 g_hash_table_insert(field_name_to_index
,
405 (gpointer
) (unsigned long) name_quark
,
406 (gpointer
) (unsigned long) fields
->len
);
407 g_ptr_array_add(fields
, field
);
413 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
415 struct bt_ctf_field_type
*type
;
416 enum bt_ctf_type_id type_id
;
418 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
419 type_id
= type
->declaration
->id
;
420 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
421 type_id
>= BT_CTF_NR_TYPE_IDS
) {
425 type_destroy_funcs
[type_id
](type
);
429 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
433 struct bt_ctf_field_type_enumeration
*enumeration
=
434 container_of(type
, struct bt_ctf_field_type_enumeration
,
436 struct bt_ctf_field_type
*container_type
=
437 bt_ctf_field_type_enumeration_get_container_type(type
);
439 if (!container_type
) {
444 ret
= bt_ctf_field_type_validate(container_type
);
449 /* Ensure enum has entries */
450 ret
= enumeration
->entries
->len
? 0 : -1;
453 BT_PUT(container_type
);
458 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
461 struct bt_ctf_field_type
*element_type
= NULL
;
462 struct bt_ctf_field_type_sequence
*sequence
=
463 container_of(type
, struct bt_ctf_field_type_sequence
,
466 /* Length field name should be set at this point */
467 if (sequence
->length_field_name
->len
== 0) {
472 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
478 ret
= bt_ctf_field_type_validate(element_type
);
481 BT_PUT(element_type
);
487 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
490 struct bt_ctf_field_type
*element_type
= NULL
;
492 element_type
= bt_ctf_field_type_array_get_element_type(type
);
498 ret
= bt_ctf_field_type_validate(element_type
);
501 BT_PUT(element_type
);
507 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
510 struct bt_ctf_field_type
*child_type
= NULL
;
511 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
514 if (field_count
< 0) {
519 for (i
= 0; i
< field_count
; ++i
) {
520 ret
= bt_ctf_field_type_structure_get_field(type
,
521 NULL
, &child_type
, i
);
526 ret
= bt_ctf_field_type_validate(child_type
);
541 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
545 struct bt_ctf_field_type
*child_type
= NULL
;
546 struct bt_ctf_field_type_variant
*variant
=
547 container_of(type
, struct bt_ctf_field_type_variant
,
550 int tag_mappings_count
;
552 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
558 bt_ctf_field_type_enumeration_get_mapping_count(
559 (struct bt_ctf_field_type
*) variant
->tag
);
561 if (tag_mappings_count
!= variant
->fields
->len
) {
566 for (i
= 0; i
< tag_mappings_count
; ++i
) {
568 int64_t range_start
, range_end
;
569 struct bt_ctf_field_type
*ft
;
571 ret
= bt_ctf_field_type_enumeration_get_mapping(
572 (struct bt_ctf_field_type
*) variant
->tag
,
573 i
, &label
, &range_start
, &range_end
);
582 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
592 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
593 if (field_count
< 0) {
598 for (i
= 0; i
< field_count
; ++i
) {
599 ret
= bt_ctf_field_type_variant_get_field(type
,
600 NULL
, &child_type
, i
);
605 ret
= bt_ctf_field_type_validate(child_type
);
620 * This function validates a given field type without considering
621 * where this field type is located. It only validates the properties
622 * of the given field type and the properties of its children if
626 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
629 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
637 /* Already marked as valid */
641 if (type_validate_funcs
[id
]) {
642 ret
= type_validate_funcs
[id
](type
);
645 if (!ret
&& type
->frozen
) {
646 /* Field type is valid */
654 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
656 struct bt_ctf_field_type_integer
*integer
=
657 g_new0(struct bt_ctf_field_type_integer
, 1);
659 if (!integer
|| size
== 0 || size
> 64) {
663 integer
->parent
.declaration
= &integer
->declaration
.p
;
664 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
665 integer
->declaration
.len
= size
;
666 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
667 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
668 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
669 return &integer
->parent
;
672 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
675 struct bt_ctf_field_type_integer
*integer
;
677 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
682 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
683 ret
= (int) integer
->declaration
.len
;
688 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
691 struct bt_ctf_field_type_integer
*integer
;
693 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
698 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
699 ret
= integer
->declaration
.signedness
;
704 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
708 struct bt_ctf_field_type_integer
*integer
;
710 if (!type
|| type
->frozen
||
711 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
716 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
717 integer
->declaration
.signedness
= !!is_signed
;
722 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
723 struct bt_ctf_field_type
*type
)
725 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
726 struct bt_ctf_field_type_integer
*integer
;
728 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
732 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
733 ret
= integer
->declaration
.base
;
738 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
739 enum bt_ctf_integer_base base
)
743 if (!type
|| type
->frozen
||
744 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
750 case BT_CTF_INTEGER_BASE_BINARY
:
751 case BT_CTF_INTEGER_BASE_OCTAL
:
752 case BT_CTF_INTEGER_BASE_DECIMAL
:
753 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
755 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
756 struct bt_ctf_field_type_integer
, parent
);
757 integer
->declaration
.base
= base
;
767 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
768 struct bt_ctf_field_type
*type
)
770 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
771 struct bt_ctf_field_type_integer
*integer
;
773 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
777 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
778 ret
= integer
->declaration
.encoding
;
783 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
784 enum bt_ctf_string_encoding encoding
)
787 struct bt_ctf_field_type_integer
*integer
;
789 if (!type
|| type
->frozen
||
790 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
791 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
792 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
797 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
798 integer
->declaration
.encoding
= encoding
;
803 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
804 struct bt_ctf_field_type
*type
)
806 struct bt_ctf_field_type_integer
*integer
;
807 struct bt_ctf_clock
*clock
= NULL
;
813 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
814 clock
= integer
->mapped_clock
;
820 int bt_ctf_field_type_integer_set_mapped_clock(
821 struct bt_ctf_field_type
*type
,
822 struct bt_ctf_clock
*clock
)
824 struct bt_ctf_field_type_integer
*integer
;
827 if (!type
|| type
->frozen
) {
832 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
833 bt_put(integer
->mapped_clock
);
835 integer
->mapped_clock
= clock
;
840 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
841 struct bt_ctf_field_type
*integer_container_type
)
843 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
845 if (!integer_container_type
) {
849 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
853 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
858 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
859 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
860 bt_get(integer_container_type
);
861 enumeration
->container
= integer_container_type
;
862 enumeration
->entries
= g_ptr_array_new_with_free_func(
863 (GDestroyNotify
)destroy_enumeration_mapping
);
864 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
865 return &enumeration
->parent
;
871 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
872 struct bt_ctf_field_type
*type
)
874 struct bt_ctf_field_type
*container_type
= NULL
;
875 struct bt_ctf_field_type_enumeration
*enumeration_type
;
881 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
885 enumeration_type
= container_of(type
,
886 struct bt_ctf_field_type_enumeration
, parent
);
887 container_type
= enumeration_type
->container
;
888 bt_get(container_type
);
890 return container_type
;
893 int bt_ctf_field_type_enumeration_add_mapping(
894 struct bt_ctf_field_type
*type
, const char *string
,
895 int64_t range_start
, int64_t range_end
)
899 struct enumeration_mapping
*mapping
;
900 struct bt_ctf_field_type_enumeration
*enumeration
;
901 struct range_overlap_query query
;
902 char *escaped_string
;
904 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
906 (range_end
< range_start
)) {
911 if (!string
|| strlen(string
) == 0) {
916 escaped_string
= g_strescape(string
, NULL
);
917 if (!escaped_string
) {
922 mapping_name
= g_quark_from_string(escaped_string
);
923 query
= (struct range_overlap_query
) {
924 .range_start
._signed
= range_start
,
925 .range_end
._signed
= range_end
,
926 .mapping_name
= mapping_name
,
928 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
931 /* Check that the range does not overlap with one already present */
932 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
933 if (query
.overlaps
) {
938 mapping
= g_new(struct enumeration_mapping
, 1);
944 *mapping
= (struct enumeration_mapping
) {
945 .range_start
._signed
= range_start
,
946 .range_end
._signed
= range_end
, .string
= mapping_name
};
947 g_ptr_array_add(enumeration
->entries
, mapping
);
948 g_ptr_array_sort(enumeration
->entries
,
949 (GCompareFunc
)compare_enumeration_mappings_signed
);
951 free(escaped_string
);
956 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
957 struct bt_ctf_field_type
*type
, const char *string
,
958 uint64_t range_start
, uint64_t range_end
)
962 struct enumeration_mapping
*mapping
;
963 struct bt_ctf_field_type_enumeration
*enumeration
;
964 struct range_overlap_query query
;
965 char *escaped_string
;
967 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
969 (range_end
< range_start
)) {
974 if (!string
|| strlen(string
) == 0) {
979 escaped_string
= g_strescape(string
, NULL
);
980 if (!escaped_string
) {
985 mapping_name
= g_quark_from_string(escaped_string
);
986 query
= (struct range_overlap_query
) {
987 .range_start
._unsigned
= range_start
,
988 .range_end
._unsigned
= range_end
,
989 .mapping_name
= mapping_name
,
991 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
994 /* Check that the range does not overlap with one already present */
995 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
997 if (query
.overlaps
) {
1002 mapping
= g_new(struct enumeration_mapping
, 1);
1008 *mapping
= (struct enumeration_mapping
) {
1009 .range_start
._unsigned
= range_start
,
1010 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1011 g_ptr_array_add(enumeration
->entries
, mapping
);
1012 g_ptr_array_sort(enumeration
->entries
,
1013 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1015 free(escaped_string
);
1020 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1021 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1024 const char *name
= NULL
;
1025 struct range_overlap_query query
=
1026 (struct range_overlap_query
) {
1027 .range_start
._unsigned
= value
,
1028 .range_end
._unsigned
= value
,
1031 g_ptr_array_foreach(enumeration_type
->entries
,
1032 check_ranges_overlap_unsigned
,
1034 if (!query
.overlaps
) {
1038 name
= g_quark_to_string(query
.mapping_name
);
1043 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1044 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1047 const char *name
= NULL
;
1048 struct range_overlap_query query
=
1049 (struct range_overlap_query
) {
1050 .range_start
._signed
= value
,
1051 .range_end
._signed
= value
,
1054 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1056 if (!query
.overlaps
) {
1060 name
= g_quark_to_string(query
.mapping_name
);
1065 int bt_ctf_field_type_enumeration_get_mapping_count(
1066 struct bt_ctf_field_type
*type
)
1069 struct bt_ctf_field_type_enumeration
*enumeration
;
1071 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1076 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1078 ret
= (int) enumeration
->entries
->len
;
1084 struct enumeration_mapping
*get_enumeration_mapping(
1085 struct bt_ctf_field_type
*type
, int index
)
1087 struct enumeration_mapping
*mapping
= NULL
;
1088 struct bt_ctf_field_type_enumeration
*enumeration
;
1090 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1092 if (index
>= enumeration
->entries
->len
) {
1096 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1101 int bt_ctf_field_type_enumeration_get_mapping(
1102 struct bt_ctf_field_type
*type
, int index
,
1103 const char **string
, int64_t *range_start
, int64_t *range_end
)
1105 struct enumeration_mapping
*mapping
;
1108 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1109 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1114 mapping
= get_enumeration_mapping(type
, index
);
1120 *string
= g_quark_to_string(mapping
->string
);
1121 *range_start
= mapping
->range_start
._signed
;
1122 *range_end
= mapping
->range_end
._signed
;
1127 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1128 struct bt_ctf_field_type
*type
, int index
,
1129 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1131 struct enumeration_mapping
*mapping
;
1134 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1135 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1140 mapping
= get_enumeration_mapping(type
, index
);
1146 *string
= g_quark_to_string(mapping
->string
);
1147 *range_start
= mapping
->range_start
._unsigned
;
1148 *range_end
= mapping
->range_end
._unsigned
;
1153 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1154 struct bt_ctf_field_type
*type
, const char *name
)
1157 struct bt_ctf_field_type_enumeration
*enumeration
;
1160 if (!type
|| !name
||
1161 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1166 name_quark
= g_quark_try_string(name
);
1172 enumeration
= container_of(type
,
1173 struct bt_ctf_field_type_enumeration
, parent
);
1174 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1175 struct enumeration_mapping
*mapping
=
1176 get_enumeration_mapping(type
, i
);
1178 if (mapping
->string
== name_quark
) {
1189 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1190 struct bt_ctf_field_type
*type
, int64_t value
)
1192 struct bt_ctf_field_type_enumeration
*enumeration
;
1195 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1200 enumeration
= container_of(type
,
1201 struct bt_ctf_field_type_enumeration
, parent
);
1202 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1203 struct enumeration_mapping
*mapping
=
1204 get_enumeration_mapping(type
, i
);
1206 if (value
>= mapping
->range_start
._signed
&&
1207 value
<= mapping
->range_end
._signed
) {
1218 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1219 struct bt_ctf_field_type
*type
, uint64_t value
)
1221 struct bt_ctf_field_type_enumeration
*enumeration
;
1224 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1229 enumeration
= container_of(type
,
1230 struct bt_ctf_field_type_enumeration
, parent
);
1231 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1232 struct enumeration_mapping
*mapping
=
1233 get_enumeration_mapping(type
, i
);
1235 if (value
>= mapping
->range_start
._unsigned
&&
1236 value
<= mapping
->range_end
._unsigned
) {
1247 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1249 struct bt_ctf_field_type_floating_point
*floating_point
=
1250 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1252 if (!floating_point
) {
1256 floating_point
->declaration
.sign
= &floating_point
->sign
;
1257 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1258 floating_point
->declaration
.exp
= &floating_point
->exp
;
1259 floating_point
->sign
.len
= 1;
1260 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1261 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1262 floating_point
->declaration
.exp
->len
=
1263 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1264 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1265 floating_point
->sign
.p
.alignment
= 1;
1266 floating_point
->mantissa
.p
.alignment
= 1;
1267 floating_point
->exp
.p
.alignment
= 1;
1269 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1271 return floating_point
? &floating_point
->parent
: NULL
;
1274 int bt_ctf_field_type_floating_point_get_exponent_digits(
1275 struct bt_ctf_field_type
*type
)
1278 struct bt_ctf_field_type_floating_point
*floating_point
;
1280 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1285 floating_point
= container_of(type
,
1286 struct bt_ctf_field_type_floating_point
, parent
);
1287 ret
= (int) floating_point
->declaration
.exp
->len
;
1292 int bt_ctf_field_type_floating_point_set_exponent_digits(
1293 struct bt_ctf_field_type
*type
,
1294 unsigned int exponent_digits
)
1297 struct bt_ctf_field_type_floating_point
*floating_point
;
1299 if (!type
|| type
->frozen
||
1300 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1305 floating_point
= container_of(type
,
1306 struct bt_ctf_field_type_floating_point
, parent
);
1307 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1308 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1310 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1315 floating_point
->declaration
.exp
->len
= exponent_digits
;
1320 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1321 struct bt_ctf_field_type
*type
)
1324 struct bt_ctf_field_type_floating_point
*floating_point
;
1326 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1331 floating_point
= container_of(type
,
1332 struct bt_ctf_field_type_floating_point
, parent
);
1333 ret
= (int) floating_point
->mantissa
.len
+ 1;
1338 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1339 struct bt_ctf_field_type
*type
,
1340 unsigned int mantissa_digits
)
1343 struct bt_ctf_field_type_floating_point
*floating_point
;
1345 if (!type
|| type
->frozen
||
1346 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1351 floating_point
= container_of(type
,
1352 struct bt_ctf_field_type_floating_point
, parent
);
1354 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1355 (mantissa_digits
!= DBL_MANT_DIG
) &&
1356 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1361 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1366 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1368 struct bt_ctf_field_type_structure
*structure
=
1369 g_new0(struct bt_ctf_field_type_structure
, 1);
1375 structure
->parent
.declaration
= &structure
->declaration
.p
;
1376 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1377 structure
->fields
= g_ptr_array_new_with_free_func(
1378 (GDestroyNotify
)destroy_structure_field
);
1379 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1380 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1381 return &structure
->parent
;
1386 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1387 struct bt_ctf_field_type
*field_type
,
1388 const char *field_name
)
1391 struct bt_ctf_field_type_structure
*structure
;
1393 if (!type
|| !field_type
|| type
->frozen
||
1394 bt_ctf_validate_identifier(field_name
) ||
1395 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1400 structure
= container_of(type
,
1401 struct bt_ctf_field_type_structure
, parent
);
1402 if (add_structure_field(structure
->fields
,
1403 structure
->field_name_to_index
, field_type
, field_name
)) {
1411 int bt_ctf_field_type_structure_get_field_count(
1412 struct bt_ctf_field_type
*type
)
1415 struct bt_ctf_field_type_structure
*structure
;
1417 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1422 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1424 ret
= (int) structure
->fields
->len
;
1429 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1430 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1433 struct bt_ctf_field_type_structure
*structure
;
1434 struct structure_field
*field
;
1437 if (!type
|| index
< 0 ||
1438 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1443 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1445 if (index
>= structure
->fields
->len
) {
1450 field
= g_ptr_array_index(structure
->fields
, index
);
1452 *field_type
= field
->type
;
1453 bt_get(field
->type
);
1456 *field_name
= g_quark_to_string(field
->name
);
1462 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1463 struct bt_ctf_field_type
*type
,
1468 struct structure_field
*field
;
1469 struct bt_ctf_field_type_structure
*structure
;
1470 struct bt_ctf_field_type
*field_type
= NULL
;
1472 if (!type
|| !name
) {
1476 name_quark
= g_quark_try_string(name
);
1481 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1483 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1484 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1488 field
= structure
->fields
->pdata
[index
];
1489 field_type
= field
->type
;
1495 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1496 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1498 struct bt_ctf_field_type_variant
*variant
= NULL
;
1500 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1504 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1509 variant
->parent
.declaration
= &variant
->declaration
.p
;
1510 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1511 variant
->tag_name
= g_string_new(tag_name
);
1512 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1513 variant
->fields
= g_ptr_array_new_with_free_func(
1514 (GDestroyNotify
) destroy_structure_field
);
1517 variant
->tag
= container_of(enum_tag
,
1518 struct bt_ctf_field_type_enumeration
, parent
);
1521 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1522 /* A variant's alignment is undefined */
1523 variant
->parent
.declaration
->alignment
= 0;
1524 return &variant
->parent
;
1529 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1530 struct bt_ctf_field_type
*type
)
1532 struct bt_ctf_field_type_variant
*variant
;
1533 struct bt_ctf_field_type
*tag_type
= NULL
;
1535 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1539 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1540 if (!variant
->tag
) {
1544 tag_type
= &variant
->tag
->parent
;
1550 const char *bt_ctf_field_type_variant_get_tag_name(
1551 struct bt_ctf_field_type
*type
)
1553 struct bt_ctf_field_type_variant
*variant
;
1554 const char *tag_name
= NULL
;
1556 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1560 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1561 if (variant
->tag_name
->len
== 0) {
1565 tag_name
= variant
->tag_name
->str
;
1570 int bt_ctf_field_type_variant_set_tag_name(
1571 struct bt_ctf_field_type
*type
, const char *name
)
1574 struct bt_ctf_field_type_variant
*variant
;
1576 if (!type
|| type
->frozen
||
1577 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1578 bt_ctf_validate_identifier(name
)) {
1583 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1584 g_string_assign(variant
->tag_name
, name
);
1589 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1590 struct bt_ctf_field_type
*field_type
,
1591 const char *field_name
)
1595 struct bt_ctf_field_type_variant
*variant
;
1596 GQuark field_name_quark
= g_quark_from_string(field_name
);
1598 if (!type
|| !field_type
|| type
->frozen
||
1599 bt_ctf_validate_identifier(field_name
) ||
1600 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1605 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1607 /* The user has explicitly provided a tag; validate against it. */
1611 /* Make sure this name is present in the enum tag */
1612 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1613 struct enumeration_mapping
*mapping
=
1614 g_ptr_array_index(variant
->tag
->entries
, i
);
1616 if (mapping
->string
== field_name_quark
) {
1623 /* Validation failed */
1629 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1630 field_type
, field_name
)) {
1638 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1639 struct bt_ctf_field_type
*type
,
1640 const char *field_name
)
1644 struct structure_field
*field
;
1645 struct bt_ctf_field_type_variant
*variant
;
1646 struct bt_ctf_field_type
*field_type
= NULL
;
1648 if (!type
|| !field_name
) {
1652 name_quark
= g_quark_try_string(field_name
);
1657 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1658 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1659 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1663 field
= g_ptr_array_index(variant
->fields
, index
);
1664 field_type
= field
->type
;
1670 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1671 struct bt_ctf_field_type
*type
,
1672 struct bt_ctf_field
*tag
)
1674 const char *enum_value
;
1675 struct bt_ctf_field_type
*field_type
= NULL
;
1677 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1681 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1686 /* Already increments field_type's reference count */
1687 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1693 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1696 struct bt_ctf_field_type_variant
*variant
;
1698 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1703 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1705 ret
= (int) variant
->fields
->len
;
1711 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1712 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1715 struct bt_ctf_field_type_variant
*variant
;
1716 struct structure_field
*field
;
1719 if (!type
|| index
< 0 ||
1720 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1725 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1727 if (index
>= variant
->fields
->len
) {
1732 field
= g_ptr_array_index(variant
->fields
, index
);
1734 *field_type
= field
->type
;
1735 bt_get(field
->type
);
1738 *field_name
= g_quark_to_string(field
->name
);
1744 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1745 struct bt_ctf_field_type
*element_type
,
1746 unsigned int length
)
1748 struct bt_ctf_field_type_array
*array
= NULL
;
1750 if (!element_type
|| length
== 0) {
1754 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1759 array
->parent
.declaration
= &array
->declaration
.p
;
1760 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1762 bt_get(element_type
);
1763 array
->element_type
= element_type
;
1764 array
->length
= length
;
1765 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1766 return &array
->parent
;
1771 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1772 struct bt_ctf_field_type
*type
)
1774 struct bt_ctf_field_type
*ret
= NULL
;
1775 struct bt_ctf_field_type_array
*array
;
1777 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1781 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1782 ret
= array
->element_type
;
1789 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1790 struct bt_ctf_field_type
*element_type
)
1793 struct bt_ctf_field_type_array
*array
;
1795 if (!type
|| !element_type
||
1796 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1801 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1803 if (array
->element_type
) {
1804 BT_PUT(array
->element_type
);
1807 array
->element_type
= element_type
;
1808 bt_get(array
->element_type
);
1814 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1817 struct bt_ctf_field_type_array
*array
;
1819 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1824 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1825 ret
= (int64_t) array
->length
;
1830 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1831 struct bt_ctf_field_type
*element_type
,
1832 const char *length_field_name
)
1834 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1836 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1840 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1845 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1846 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1847 bt_get(element_type
);
1848 sequence
->element_type
= element_type
;
1849 sequence
->length_field_name
= g_string_new(length_field_name
);
1850 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1851 return &sequence
->parent
;
1856 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1857 struct bt_ctf_field_type
*type
)
1859 struct bt_ctf_field_type
*ret
= NULL
;
1860 struct bt_ctf_field_type_sequence
*sequence
;
1862 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1866 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1868 ret
= sequence
->element_type
;
1875 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1876 struct bt_ctf_field_type
*element_type
)
1879 struct bt_ctf_field_type_sequence
*sequence
;
1881 if (!type
|| !element_type
||
1882 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1887 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1889 if (sequence
->element_type
) {
1890 BT_PUT(sequence
->element_type
);
1893 sequence
->element_type
= element_type
;
1894 bt_get(sequence
->element_type
);
1900 const char *bt_ctf_field_type_sequence_get_length_field_name(
1901 struct bt_ctf_field_type
*type
)
1903 const char *ret
= NULL
;
1904 struct bt_ctf_field_type_sequence
*sequence
;
1906 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1910 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1912 ret
= sequence
->length_field_name
->str
;
1917 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1919 struct bt_ctf_field_type_string
*string
=
1920 g_new0(struct bt_ctf_field_type_string
, 1);
1926 string
->parent
.declaration
= &string
->declaration
.p
;
1927 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
1928 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1929 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1930 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1931 return &string
->parent
;
1934 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1935 struct bt_ctf_field_type
*type
)
1937 struct bt_ctf_field_type_string
*string
;
1938 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1940 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
1944 string
= container_of(type
, struct bt_ctf_field_type_string
,
1946 ret
= string
->declaration
.encoding
;
1951 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1952 enum bt_ctf_string_encoding encoding
)
1955 struct bt_ctf_field_type_string
*string
;
1957 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
1958 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
1959 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
1964 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1965 string
->declaration
.encoding
= encoding
;
1970 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1973 enum bt_ctf_type_id type_id
;
1981 ret
= (int) type
->declaration
->alignment
;
1985 type_id
= bt_ctf_field_type_get_type_id(type
);
1987 case BT_CTF_TYPE_ID_SEQUENCE
:
1989 struct bt_ctf_field_type
*element
=
1990 bt_ctf_field_type_sequence_get_element_type(type
);
1997 ret
= bt_ctf_field_type_get_alignment(element
);
2001 case BT_CTF_TYPE_ID_ARRAY
:
2003 struct bt_ctf_field_type
*element
=
2004 bt_ctf_field_type_array_get_element_type(type
);
2011 ret
= bt_ctf_field_type_get_alignment(element
);
2015 case BT_CTF_TYPE_ID_STRUCT
:
2017 int i
, element_count
;
2019 element_count
= bt_ctf_field_type_structure_get_field_count(
2021 if (element_count
< 0) {
2022 ret
= element_count
;
2026 for (i
= 0; i
< element_count
; i
++) {
2027 struct bt_ctf_field_type
*field
;
2028 int field_alignment
;
2030 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2037 field_alignment
= bt_ctf_field_type_get_alignment(
2040 if (field_alignment
< 0) {
2041 ret
= field_alignment
;
2045 type
->declaration
->alignment
= MAX(field_alignment
,
2046 type
->declaration
->alignment
);
2048 ret
= (int) type
->declaration
->alignment
;
2051 case BT_CTF_TYPE_ID_UNKNOWN
:
2055 ret
= (int) type
->declaration
->alignment
;
2063 int is_power_of_two(unsigned int value
)
2065 return ((value
& (value
- 1)) == 0) && value
> 0;
2068 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2069 unsigned int alignment
)
2072 enum bt_ctf_type_id type_id
;
2074 /* Alignment must be a power of two */
2075 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2080 type_id
= bt_ctf_field_type_get_type_id(type
);
2081 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2086 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2087 alignment
!= CHAR_BIT
) {
2092 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2093 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2094 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2095 /* Setting an alignment on these types makes no sense */
2100 type
->declaration
->alignment
= alignment
;
2106 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2107 struct bt_ctf_field_type
*type
)
2109 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2115 switch (type
->declaration
->id
) {
2116 case BT_CTF_TYPE_ID_INTEGER
:
2118 struct bt_ctf_field_type_integer
*integer
= container_of(
2119 type
, struct bt_ctf_field_type_integer
, parent
);
2120 ret
= integer
->user_byte_order
;
2123 case BT_CTF_TYPE_ID_FLOAT
:
2125 struct bt_ctf_field_type_floating_point
*floating_point
=
2127 struct bt_ctf_field_type_floating_point
,
2129 ret
= floating_point
->user_byte_order
;
2136 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2137 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2138 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2139 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2145 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2146 enum bt_ctf_byte_order byte_order
)
2149 int internal_byte_order
;
2150 enum bt_ctf_type_id type_id
;
2152 if (!type
|| type
->frozen
) {
2157 switch (byte_order
) {
2158 case BT_CTF_BYTE_ORDER_NATIVE
:
2159 /* Leave unset. Will be initialized by parent. */
2160 internal_byte_order
= 0;
2162 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2163 internal_byte_order
= LITTLE_ENDIAN
;
2165 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2166 case BT_CTF_BYTE_ORDER_NETWORK
:
2167 internal_byte_order
= BIG_ENDIAN
;
2174 type_id
= type
->declaration
->id
;
2175 if (set_byte_order_funcs
[type_id
]) {
2176 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2182 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2183 struct bt_ctf_field_type
*type
)
2186 return BT_CTF_TYPE_ID_UNKNOWN
;
2189 return type
->declaration
->id
;
2192 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2194 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2197 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2199 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2202 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2204 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2207 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2209 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2212 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2214 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2217 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2219 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2222 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2224 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2227 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2229 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2232 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2237 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2243 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2253 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2254 struct bt_ctf_field_type_variant
*variant
,
2257 struct bt_ctf_field_type
*type
= NULL
;
2258 GQuark field_name_quark
;
2260 struct structure_field
*field_entry
;
2261 struct range_overlap_query query
= {
2262 .range_start
._signed
= tag_value
,
2263 .range_end
._signed
= tag_value
,
2264 .mapping_name
= 0, .overlaps
= 0};
2266 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2268 if (!query
.overlaps
) {
2272 field_name_quark
= query
.mapping_name
;
2273 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2274 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2278 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2279 type
= field_entry
->type
;
2285 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2286 struct bt_ctf_field_type_variant
*variant
,
2289 struct bt_ctf_field_type
*type
= NULL
;
2290 GQuark field_name_quark
;
2292 struct structure_field
*field_entry
;
2293 struct range_overlap_query query
= {
2294 .range_start
._unsigned
= tag_value
,
2295 .range_end
._unsigned
= tag_value
,
2296 .mapping_name
= 0, .overlaps
= 0};
2298 g_ptr_array_foreach(variant
->tag
->entries
,
2299 check_ranges_overlap_unsigned
,
2301 if (!query
.overlaps
) {
2305 field_name_quark
= query
.mapping_name
;
2306 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2307 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2311 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2312 type
= field_entry
->type
;
2318 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2319 struct metadata_context
*context
)
2323 if (!type
|| !context
) {
2328 /* Make sure field type is valid before serializing it */
2329 ret
= bt_ctf_field_type_validate(type
);
2335 ret
= type
->serialize(type
, context
);
2341 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2348 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2349 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2350 set_byte_order_funcs
[type
->declaration
->id
](type
,
2356 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2358 struct bt_ctf_field_type
*copy
= NULL
;
2364 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2370 int bt_ctf_field_type_structure_get_field_name_index(
2371 struct bt_ctf_field_type
*type
, const char *name
)
2376 struct bt_ctf_field_type_structure
*structure
;
2378 if (!type
|| !name
||
2379 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2384 name_quark
= g_quark_try_string(name
);
2390 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2392 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2393 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2403 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2404 struct bt_ctf_field_type
*field
, int index
)
2407 struct bt_ctf_field_type_structure
*structure
;
2409 if (!type
|| !field
||
2410 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2415 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2417 if (index
< 0 || index
>= structure
->fields
->len
) {
2423 bt_put(((struct structure_field
*)
2424 g_ptr_array_index(structure
->fields
, index
))->type
);
2425 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2432 int bt_ctf_field_type_variant_get_field_name_index(
2433 struct bt_ctf_field_type
*type
, const char *name
)
2438 struct bt_ctf_field_type_variant
*variant
;
2440 if (!type
|| !name
||
2441 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2446 name_quark
= g_quark_try_string(name
);
2452 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2454 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2455 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2465 int bt_ctf_field_type_sequence_set_length_field_path(
2466 struct bt_ctf_field_type
*type
,
2467 struct bt_ctf_field_path
*path
)
2470 struct bt_ctf_field_type_sequence
*sequence
;
2472 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2473 BT_CTF_TYPE_ID_SEQUENCE
) {
2478 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2481 BT_MOVE(sequence
->length_field_path
, path
);
2487 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2488 struct bt_ctf_field_path
*path
)
2491 struct bt_ctf_field_type_variant
*variant
;
2493 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2494 BT_CTF_TYPE_ID_VARIANT
) {
2499 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2502 BT_MOVE(variant
->tag_field_path
, path
);
2508 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2509 struct bt_ctf_field_type
*tag
)
2512 struct bt_ctf_field_type_variant
*variant
;
2514 if (!type
|| !tag
||
2515 bt_ctf_field_type_get_type_id(tag
) !=
2516 BT_CTF_TYPE_ID_ENUM
) {
2521 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2525 bt_put(&variant
->tag
->parent
);
2527 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2534 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2535 struct bt_ctf_field_type
*field
, int index
)
2538 struct bt_ctf_field_type_variant
*variant
;
2540 if (!type
|| !field
||
2541 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2546 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2548 if (index
< 0 || index
>= variant
->fields
->len
) {
2554 bt_put(((struct structure_field
*)
2555 g_ptr_array_index(variant
->fields
, index
))->type
);
2556 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2563 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2565 struct bt_ctf_field_type_integer
*integer
=
2566 (struct bt_ctf_field_type_integer
*) type
;
2572 bt_put(integer
->mapped_clock
);
2577 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2579 struct bt_ctf_field_type_enumeration
*enumeration
=
2580 (struct bt_ctf_field_type_enumeration
*) type
;
2586 g_ptr_array_free(enumeration
->entries
, TRUE
);
2587 bt_put(enumeration
->container
);
2588 g_free(enumeration
);
2592 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2594 struct bt_ctf_field_type_floating_point
*floating_point
=
2595 (struct bt_ctf_field_type_floating_point
*) type
;
2601 g_free(floating_point
);
2605 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2607 struct bt_ctf_field_type_structure
*structure
=
2608 (struct bt_ctf_field_type_structure
*) type
;
2614 g_ptr_array_free(structure
->fields
, TRUE
);
2615 g_hash_table_destroy(structure
->field_name_to_index
);
2620 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2622 struct bt_ctf_field_type_variant
*variant
=
2623 (struct bt_ctf_field_type_variant
*) type
;
2629 g_ptr_array_free(variant
->fields
, TRUE
);
2630 g_hash_table_destroy(variant
->field_name_to_index
);
2631 g_string_free(variant
->tag_name
, TRUE
);
2632 bt_put(&variant
->tag
->parent
);
2633 BT_PUT(variant
->tag_field_path
);
2638 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2640 struct bt_ctf_field_type_array
*array
=
2641 (struct bt_ctf_field_type_array
*) type
;
2647 bt_put(array
->element_type
);
2652 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2654 struct bt_ctf_field_type_sequence
*sequence
=
2655 (struct bt_ctf_field_type_sequence
*) type
;
2661 bt_put(sequence
->element_type
);
2662 g_string_free(sequence
->length_field_name
, TRUE
);
2663 BT_PUT(sequence
->length_field_path
);
2668 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2670 struct bt_ctf_field_type_string
*string
=
2671 (struct bt_ctf_field_type_string
*) type
;
2681 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2687 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2689 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2690 type
, struct bt_ctf_field_type_integer
, parent
);
2692 if (integer_type
->mapped_clock
) {
2693 bt_ctf_clock_freeze(integer_type
->mapped_clock
);
2696 generic_field_type_freeze(type
);
2700 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2702 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2703 type
, struct bt_ctf_field_type_enumeration
, parent
);
2705 generic_field_type_freeze(type
);
2706 bt_ctf_field_type_freeze(enumeration_type
->container
);
2710 void freeze_structure_field(struct structure_field
*field
)
2712 bt_ctf_field_type_freeze(field
->type
);
2716 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2718 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2719 type
, struct bt_ctf_field_type_structure
, parent
);
2721 /* Cache the alignment */
2722 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2723 generic_field_type_freeze(type
);
2724 g_ptr_array_foreach(structure_type
->fields
,
2725 (GFunc
) freeze_structure_field
, NULL
);
2729 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2731 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2732 type
, struct bt_ctf_field_type_variant
, parent
);
2734 generic_field_type_freeze(type
);
2735 g_ptr_array_foreach(variant_type
->fields
,
2736 (GFunc
) freeze_structure_field
, NULL
);
2740 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2742 struct bt_ctf_field_type_array
*array_type
= container_of(
2743 type
, struct bt_ctf_field_type_array
, parent
);
2745 /* Cache the alignment */
2746 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2747 generic_field_type_freeze(type
);
2748 bt_ctf_field_type_freeze(array_type
->element_type
);
2752 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2754 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2755 type
, struct bt_ctf_field_type_sequence
, parent
);
2757 /* Cache the alignment */
2758 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2759 generic_field_type_freeze(type
);
2760 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2764 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2766 const char *encoding_string
;
2769 case BT_CTF_STRING_ENCODING_NONE
:
2770 encoding_string
= "none";
2772 case BT_CTF_STRING_ENCODING_ASCII
:
2773 encoding_string
= "ASCII";
2775 case BT_CTF_STRING_ENCODING_UTF8
:
2776 encoding_string
= "UTF8";
2779 encoding_string
= "unknown";
2783 return encoding_string
;
2787 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2789 const char *base_string
;
2792 case BT_CTF_INTEGER_BASE_DECIMAL
:
2793 base_string
= "decimal";
2795 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2796 base_string
= "hexadecimal";
2798 case BT_CTF_INTEGER_BASE_OCTAL
:
2799 base_string
= "octal";
2801 case BT_CTF_INTEGER_BASE_BINARY
:
2802 base_string
= "binary";
2805 base_string
= "unknown";
2813 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2814 struct metadata_context
*context
)
2816 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2817 struct bt_ctf_field_type_integer
, parent
);
2820 g_string_append_printf(context
->string
,
2821 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2822 integer
->declaration
.len
, type
->declaration
->alignment
,
2823 (integer
->declaration
.signedness
? "true" : "false"),
2824 get_encoding_string(integer
->declaration
.encoding
),
2825 get_integer_base_string(integer
->declaration
.base
),
2826 get_byte_order_string(integer
->declaration
.byte_order
));
2827 if (integer
->mapped_clock
) {
2828 const char *clock_name
= bt_ctf_clock_get_name(
2829 integer
->mapped_clock
);
2836 g_string_append_printf(context
->string
,
2837 "; map = clock.%s.value", clock_name
);
2840 g_string_append(context
->string
, "; }");
2846 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2847 struct metadata_context
*context
)
2851 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2852 struct bt_ctf_field_type_enumeration
, parent
);
2853 struct bt_ctf_field_type
*container_type
;
2854 int container_signed
;
2856 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2857 if (!container_type
) {
2862 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2863 if (container_signed
< 0) {
2864 ret
= container_signed
;
2865 goto error_put_container_type
;
2868 g_string_append(context
->string
, "enum : ");
2869 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2871 goto error_put_container_type
;
2874 g_string_append(context
->string
, " { ");
2875 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2876 struct enumeration_mapping
*mapping
=
2877 enumeration
->entries
->pdata
[entry
];
2879 if (container_signed
) {
2880 if (mapping
->range_start
._signed
==
2881 mapping
->range_end
._signed
) {
2882 g_string_append_printf(context
->string
,
2883 "\"%s\" = %" PRId64
,
2884 g_quark_to_string(mapping
->string
),
2885 mapping
->range_start
._signed
);
2887 g_string_append_printf(context
->string
,
2888 "\"%s\" = %" PRId64
" ... %" PRId64
,
2889 g_quark_to_string(mapping
->string
),
2890 mapping
->range_start
._signed
,
2891 mapping
->range_end
._signed
);
2894 if (mapping
->range_start
._unsigned
==
2895 mapping
->range_end
._unsigned
) {
2896 g_string_append_printf(context
->string
,
2897 "\"%s\" = %" PRIu64
,
2898 g_quark_to_string(mapping
->string
),
2899 mapping
->range_start
._unsigned
);
2901 g_string_append_printf(context
->string
,
2902 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2903 g_quark_to_string(mapping
->string
),
2904 mapping
->range_start
._unsigned
,
2905 mapping
->range_end
._unsigned
);
2909 g_string_append(context
->string
,
2910 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2914 if (context
->field_name
->len
) {
2915 g_string_append_printf(context
->string
, " %s",
2916 context
->field_name
->str
);
2917 g_string_assign(context
->field_name
, "");
2919 error_put_container_type
:
2920 bt_put(container_type
);
2926 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2927 struct metadata_context
*context
)
2929 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2930 type
, struct bt_ctf_field_type_floating_point
, parent
);
2932 g_string_append_printf(context
->string
,
2933 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2934 floating_point
->declaration
.exp
->len
,
2935 floating_point
->declaration
.mantissa
->len
+ 1,
2936 get_byte_order_string(floating_point
->declaration
.byte_order
),
2937 type
->declaration
->alignment
);
2942 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2943 struct metadata_context
*context
)
2946 unsigned int indent
;
2948 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2949 struct bt_ctf_field_type_structure
, parent
);
2950 GString
*structure_field_name
= context
->field_name
;
2952 context
->field_name
= g_string_new("");
2954 context
->current_indentation_level
++;
2955 g_string_append(context
->string
, "struct {\n");
2957 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2958 struct structure_field
*field
;
2960 for (indent
= 0; indent
< context
->current_indentation_level
;
2962 g_string_append_c(context
->string
, '\t');
2965 field
= structure
->fields
->pdata
[i
];
2966 g_string_assign(context
->field_name
,
2967 g_quark_to_string(field
->name
));
2968 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2973 if (context
->field_name
->len
) {
2974 g_string_append_printf(context
->string
, " %s",
2975 context
->field_name
->str
);
2977 g_string_append(context
->string
, ";\n");
2980 context
->current_indentation_level
--;
2981 for (indent
= 0; indent
< context
->current_indentation_level
;
2983 g_string_append_c(context
->string
, '\t');
2986 g_string_append_printf(context
->string
, "} align(%zu)",
2987 type
->declaration
->alignment
);
2989 g_string_free(context
->field_name
, TRUE
);
2990 context
->field_name
= structure_field_name
;
2995 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2996 struct metadata_context
*context
)
2999 unsigned int indent
;
3001 struct bt_ctf_field_type_variant
*variant
= container_of(
3002 type
, struct bt_ctf_field_type_variant
, parent
);
3003 GString
*variant_field_name
= context
->field_name
;
3005 context
->field_name
= g_string_new("");
3006 if (variant
->tag_name
->len
> 0) {
3007 g_string_append_printf(context
->string
,
3008 "variant <%s> {\n", variant
->tag_name
->str
);
3010 g_string_append(context
->string
, "variant {\n");
3013 context
->current_indentation_level
++;
3014 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3015 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3017 g_string_assign(context
->field_name
,
3018 g_quark_to_string(field
->name
));
3019 for (indent
= 0; indent
< context
->current_indentation_level
;
3021 g_string_append_c(context
->string
, '\t');
3024 g_string_assign(context
->field_name
,
3025 g_quark_to_string(field
->name
));
3026 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3031 if (context
->field_name
->len
) {
3032 g_string_append_printf(context
->string
, " %s;",
3033 context
->field_name
->str
);
3036 g_string_append_c(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(context
->string
, "}");
3047 g_string_free(context
->field_name
, TRUE
);
3048 context
->field_name
= variant_field_name
;
3053 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3054 struct metadata_context
*context
)
3057 struct bt_ctf_field_type_array
*array
= container_of(type
,
3058 struct bt_ctf_field_type_array
, parent
);
3060 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3065 if (context
->field_name
->len
) {
3066 g_string_append_printf(context
->string
, " %s[%u]",
3067 context
->field_name
->str
, array
->length
);
3068 g_string_assign(context
->field_name
, "");
3070 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3077 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3078 struct metadata_context
*context
)
3081 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3082 type
, struct bt_ctf_field_type_sequence
, parent
);
3084 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3089 if (context
->field_name
->len
) {
3090 g_string_append_printf(context
->string
, " %s[%s]",
3091 context
->field_name
->str
,
3092 sequence
->length_field_name
->str
);
3093 g_string_assign(context
->field_name
, "");
3095 g_string_append_printf(context
->string
, "[%s]",
3096 sequence
->length_field_name
->str
);
3103 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3104 struct metadata_context
*context
)
3106 struct bt_ctf_field_type_string
*string
= container_of(
3107 type
, struct bt_ctf_field_type_string
, parent
);
3109 g_string_append_printf(context
->string
,
3110 "string { encoding = %s; }",
3111 get_encoding_string(string
->declaration
.encoding
));
3116 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3117 enum bt_ctf_byte_order ret
;
3119 switch (byte_order
) {
3120 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3122 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3124 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3126 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3128 case BT_CTF_BYTE_ORDER_NETWORK
:
3129 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3131 case BT_CTF_BYTE_ORDER_NATIVE
:
3132 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3135 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3143 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3144 int byte_order
, int set_native
)
3146 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3147 struct bt_ctf_field_type_integer
, parent
);
3150 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3152 * User byte order is native, so we can set
3153 * the real byte order.
3155 integer_type
->declaration
.byte_order
=
3159 integer_type
->user_byte_order
=
3160 get_ctf_ir_byte_order(byte_order
);
3161 integer_type
->declaration
.byte_order
= byte_order
;
3166 void bt_ctf_field_type_enumeration_set_byte_order(
3167 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3169 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3170 struct bt_ctf_field_type_enumeration
, parent
);
3172 /* Safe to assume that container is an integer */
3173 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3174 byte_order
, set_native
);
3178 void bt_ctf_field_type_floating_point_set_byte_order(
3179 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3181 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3182 container_of(type
, struct bt_ctf_field_type_floating_point
,
3186 if (floating_point_type
->user_byte_order
==
3187 BT_CTF_BYTE_ORDER_NATIVE
) {
3189 * User byte order is native, so we can set
3190 * the real byte order.
3192 floating_point_type
->declaration
.byte_order
=
3194 floating_point_type
->sign
.byte_order
=
3196 floating_point_type
->mantissa
.byte_order
=
3198 floating_point_type
->exp
.byte_order
=
3202 floating_point_type
->user_byte_order
=
3203 get_ctf_ir_byte_order(byte_order
);
3204 floating_point_type
->declaration
.byte_order
= byte_order
;
3205 floating_point_type
->sign
.byte_order
= byte_order
;
3206 floating_point_type
->mantissa
.byte_order
= byte_order
;
3207 floating_point_type
->exp
.byte_order
= byte_order
;
3212 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3213 int byte_order
, int set_native
)
3216 struct bt_ctf_field_type_structure
*structure_type
=
3217 container_of(type
, struct bt_ctf_field_type_structure
,
3220 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3221 struct structure_field
*field
= g_ptr_array_index(
3222 structure_type
->fields
, i
);
3223 struct bt_ctf_field_type
*field_type
= field
->type
;
3225 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3226 set_byte_order_funcs
[field_type
->declaration
->id
](
3227 field_type
, byte_order
, set_native
);
3233 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3234 int byte_order
, int set_native
)
3237 struct bt_ctf_field_type_variant
*variant_type
=
3238 container_of(type
, struct bt_ctf_field_type_variant
,
3241 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3242 struct structure_field
*field
= g_ptr_array_index(
3243 variant_type
->fields
, i
);
3244 struct bt_ctf_field_type
*field_type
= field
->type
;
3246 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3247 set_byte_order_funcs
[field_type
->declaration
->id
](
3248 field_type
, byte_order
, set_native
);
3254 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3255 int byte_order
, int set_native
)
3257 struct bt_ctf_field_type_array
*array_type
=
3258 container_of(type
, struct bt_ctf_field_type_array
,
3261 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3262 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3263 array_type
->element_type
, byte_order
, set_native
);
3268 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3269 int byte_order
, int set_native
)
3271 struct bt_ctf_field_type_sequence
*sequence_type
=
3272 container_of(type
, struct bt_ctf_field_type_sequence
,
3275 if (set_byte_order_funcs
[
3276 sequence_type
->element_type
->declaration
->id
]) {
3277 set_byte_order_funcs
[
3278 sequence_type
->element_type
->declaration
->id
](
3279 sequence_type
->element_type
, byte_order
, set_native
);
3284 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3285 struct bt_ctf_field_type
*type
)
3287 struct bt_ctf_field_type
*copy
;
3288 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3290 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3291 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3296 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3298 copy_integer
->declaration
= integer
->declaration
;
3299 if (integer
->mapped_clock
) {
3300 bt_get(integer
->mapped_clock
);
3301 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3304 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3311 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3312 struct bt_ctf_field_type
*type
)
3315 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3316 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3318 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3321 /* Copy the source enumeration's container */
3322 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3323 if (!copy_container
) {
3327 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3331 copy_enumeration
= container_of(copy
,
3332 struct bt_ctf_field_type_enumeration
, parent
);
3334 /* Copy all enumaration entries */
3335 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3336 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3337 enumeration
->entries
, i
);
3338 struct enumeration_mapping
* copy_mapping
= g_new0(
3339 struct enumeration_mapping
, 1);
3341 if (!copy_mapping
) {
3345 *copy_mapping
= *mapping
;
3346 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3349 copy_enumeration
->declaration
= enumeration
->declaration
;
3351 bt_put(copy_container
);
3354 bt_put(copy_container
);
3360 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3361 struct bt_ctf_field_type
*type
)
3363 struct bt_ctf_field_type
*copy
;
3364 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3366 floating_point
= container_of(type
,
3367 struct bt_ctf_field_type_floating_point
, parent
);
3368 copy
= bt_ctf_field_type_floating_point_create();
3373 copy_float
= container_of(copy
,
3374 struct bt_ctf_field_type_floating_point
, parent
);
3375 copy_float
->declaration
= floating_point
->declaration
;
3376 copy_float
->sign
= floating_point
->sign
;
3377 copy_float
->mantissa
= floating_point
->mantissa
;
3378 copy_float
->exp
= floating_point
->exp
;
3379 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3380 copy_float
->declaration
.sign
= ©_float
->sign
;
3381 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3382 copy_float
->declaration
.exp
= ©_float
->exp
;
3388 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3389 struct bt_ctf_field_type
*type
)
3392 GHashTableIter iter
;
3393 gpointer key
, value
;
3394 struct bt_ctf_field_type
*copy
;
3395 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3397 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3399 copy
= bt_ctf_field_type_structure_create();
3404 copy_structure
= container_of(copy
,
3405 struct bt_ctf_field_type_structure
, parent
);
3407 /* Copy field_name_to_index */
3408 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3409 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3410 g_hash_table_insert(copy_structure
->field_name_to_index
,
3414 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3415 struct structure_field
*entry
, *copy_entry
;
3416 struct bt_ctf_field_type
*copy_field
;
3418 copy_entry
= g_new0(struct structure_field
, 1);
3423 entry
= g_ptr_array_index(structure
->fields
, i
);
3424 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3430 copy_entry
->name
= entry
->name
;
3431 copy_entry
->type
= copy_field
;
3432 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3435 copy_structure
->declaration
= structure
->declaration
;
3444 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3445 struct bt_ctf_field_type
*type
)
3448 GHashTableIter iter
;
3449 gpointer key
, value
;
3450 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3451 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3453 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3456 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3462 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3463 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3468 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3471 /* Copy field_name_to_index */
3472 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3473 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3474 g_hash_table_insert(copy_variant
->field_name_to_index
,
3478 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3479 struct structure_field
*entry
, *copy_entry
;
3480 struct bt_ctf_field_type
*copy_field
;
3482 copy_entry
= g_new0(struct structure_field
, 1);
3487 entry
= g_ptr_array_index(variant
->fields
, i
);
3488 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3494 copy_entry
->name
= entry
->name
;
3495 copy_entry
->type
= copy_field
;
3496 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3499 copy_variant
->declaration
= variant
->declaration
;
3500 if (variant
->tag_field_path
) {
3501 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3502 variant
->tag_field_path
);
3503 if (!copy_variant
->tag_field_path
) {
3517 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3518 struct bt_ctf_field_type
*type
)
3520 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3521 struct bt_ctf_field_type_array
*array
, *copy_array
;
3523 array
= container_of(type
, struct bt_ctf_field_type_array
,
3525 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3526 if (!copy_element
) {
3530 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3535 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3537 copy_array
->declaration
= array
->declaration
;
3539 bt_put(copy_element
);
3544 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3545 struct bt_ctf_field_type
*type
)
3547 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3548 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3550 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3552 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3553 if (!copy_element
) {
3557 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3558 sequence
->length_field_name
->len
?
3559 sequence
->length_field_name
->str
: NULL
);
3564 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3566 copy_sequence
->declaration
= sequence
->declaration
;
3567 if (sequence
->length_field_path
) {
3568 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3569 sequence
->length_field_path
);
3570 if (!copy_sequence
->length_field_path
) {
3575 bt_put(copy_element
);
3583 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3584 struct bt_ctf_field_type
*type
)
3586 struct bt_ctf_field_type
*copy
;
3587 struct bt_ctf_field_type_string
*string
, *copy_string
;
3589 copy
= bt_ctf_field_type_string_create();
3594 string
= container_of(type
, struct bt_ctf_field_type_string
,
3596 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3598 copy_string
->declaration
= string
->declaration
;
3604 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3605 struct bt_ctf_field_type
*type_b
)
3608 struct bt_ctf_field_type_integer
*integer_a
;
3609 struct bt_ctf_field_type_integer
*integer_b
;
3610 struct declaration_integer
*decl_a
;
3611 struct declaration_integer
*decl_b
;
3613 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3615 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3617 decl_a
= &integer_a
->declaration
;
3618 decl_b
= &integer_b
->declaration
;
3621 if (decl_a
->len
!= decl_b
->len
) {
3626 * Compare user byte orders only, not the cached,
3629 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3634 if (decl_a
->signedness
!= decl_b
->signedness
) {
3639 if (decl_a
->base
!= decl_b
->base
) {
3644 if (decl_a
->encoding
!= decl_b
->encoding
) {
3649 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3661 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3662 struct bt_ctf_field_type
*type_b
)
3665 struct bt_ctf_field_type_floating_point
*float_a
;
3666 struct bt_ctf_field_type_floating_point
*float_b
;
3668 float_a
= container_of(type_a
,
3669 struct bt_ctf_field_type_floating_point
, parent
);
3670 float_b
= container_of(type_b
,
3671 struct bt_ctf_field_type_floating_point
, parent
);
3674 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3678 /* Exponent length */
3679 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3683 /* Mantissa length */
3684 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3689 * Compare user byte orders only, not the cached,
3692 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3704 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3705 struct enumeration_mapping
*mapping_b
)
3710 if (mapping_a
->string
!= mapping_b
->string
) {
3715 if (mapping_a
->range_start
._unsigned
!=
3716 mapping_b
->range_start
._unsigned
) {
3721 if (mapping_a
->range_end
._unsigned
!=
3722 mapping_b
->range_end
._unsigned
) {
3734 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3735 struct bt_ctf_field_type
*type_b
)
3739 struct bt_ctf_field_type_enumeration
*enum_a
;
3740 struct bt_ctf_field_type_enumeration
*enum_b
;
3742 enum_a
= container_of(type_a
,
3743 struct bt_ctf_field_type_enumeration
, parent
);
3744 enum_b
= container_of(type_b
,
3745 struct bt_ctf_field_type_enumeration
, parent
);
3747 /* Container field type */
3748 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3756 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3760 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3761 struct enumeration_mapping
*mapping_a
=
3762 g_ptr_array_index(enum_a
->entries
, i
);
3763 struct enumeration_mapping
*mapping_b
=
3764 g_ptr_array_index(enum_b
->entries
, i
);
3766 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3779 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3780 struct bt_ctf_field_type
*type_b
)
3783 struct bt_ctf_field_type_string
*string_a
;
3784 struct bt_ctf_field_type_string
*string_b
;
3786 string_a
= container_of(type_a
,
3787 struct bt_ctf_field_type_string
, parent
);
3788 string_b
= container_of(type_b
,
3789 struct bt_ctf_field_type_string
, parent
);
3792 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3804 int compare_structure_fields(struct structure_field
*field_a
,
3805 struct structure_field
*field_b
)
3810 if (field_a
->name
!= field_b
->name
) {
3815 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3822 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3823 struct bt_ctf_field_type
*type_b
)
3827 struct bt_ctf_field_type_structure
*struct_a
;
3828 struct bt_ctf_field_type_structure
*struct_b
;
3830 struct_a
= container_of(type_a
,
3831 struct bt_ctf_field_type_structure
, parent
);
3832 struct_b
= container_of(type_b
,
3833 struct bt_ctf_field_type_structure
, parent
);
3836 if (bt_ctf_field_type_get_alignment(type_a
) !=
3837 bt_ctf_field_type_get_alignment(type_b
)) {
3842 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3846 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3847 struct structure_field
*field_a
=
3848 g_ptr_array_index(struct_a
->fields
, i
);
3849 struct structure_field
*field_b
=
3850 g_ptr_array_index(struct_b
->fields
, i
);
3852 ret
= compare_structure_fields(field_a
, field_b
);
3866 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3867 struct bt_ctf_field_type
*type_b
)
3871 struct bt_ctf_field_type_variant
*variant_a
;
3872 struct bt_ctf_field_type_variant
*variant_b
;
3874 variant_a
= container_of(type_a
,
3875 struct bt_ctf_field_type_variant
, parent
);
3876 variant_b
= container_of(type_b
,
3877 struct bt_ctf_field_type_variant
, parent
);
3880 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3885 ret
= bt_ctf_field_type_compare(
3886 (struct bt_ctf_field_type
*) variant_a
->tag
,
3887 (struct bt_ctf_field_type
*) variant_b
->tag
);
3895 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3899 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3900 struct structure_field
*field_a
=
3901 g_ptr_array_index(variant_a
->fields
, i
);
3902 struct structure_field
*field_b
=
3903 g_ptr_array_index(variant_b
->fields
, i
);
3905 ret
= compare_structure_fields(field_a
, field_b
);
3919 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3920 struct bt_ctf_field_type
*type_b
)
3923 struct bt_ctf_field_type_array
*array_a
;
3924 struct bt_ctf_field_type_array
*array_b
;
3926 array_a
= container_of(type_a
,
3927 struct bt_ctf_field_type_array
, parent
);
3928 array_b
= container_of(type_b
,
3929 struct bt_ctf_field_type_array
, parent
);
3932 if (array_a
->length
!= array_b
->length
) {
3937 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3938 array_b
->element_type
);
3945 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3946 struct bt_ctf_field_type
*type_b
)
3949 struct bt_ctf_field_type_sequence
*sequence_a
;
3950 struct bt_ctf_field_type_sequence
*sequence_b
;
3952 sequence_a
= container_of(type_a
,
3953 struct bt_ctf_field_type_sequence
, parent
);
3954 sequence_b
= container_of(type_b
,
3955 struct bt_ctf_field_type_sequence
, parent
);
3958 if (strcmp(sequence_a
->length_field_name
->str
,
3959 sequence_b
->length_field_name
->str
)) {
3964 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
3965 sequence_b
->element_type
);
3971 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
3972 struct bt_ctf_field_type
*type_b
)
3976 if (type_a
== type_b
) {
3977 /* Same reference: equal (even if both are NULL) */
3982 if (!type_a
|| !type_b
) {
3987 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
3988 /* Different type IDs */
3992 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
3993 /* Both have unknown type IDs */
3997 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4004 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4006 int field_count
= -1;
4007 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4010 case CTF_TYPE_STRUCT
:
4012 bt_ctf_field_type_structure_get_field_count(field_type
);
4014 case CTF_TYPE_VARIANT
:
4016 bt_ctf_field_type_variant_get_field_count(field_type
);
4018 case CTF_TYPE_ARRAY
:
4019 case CTF_TYPE_SEQUENCE
:
4021 * Array and sequence types always contain a single member
4022 * (the element type).
4034 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4035 struct bt_ctf_field_type
*field_type
, int index
)
4037 struct bt_ctf_field_type
*field
= NULL
;
4038 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4041 case CTF_TYPE_STRUCT
:
4042 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4045 case CTF_TYPE_VARIANT
:
4046 bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4049 case CTF_TYPE_ARRAY
:
4050 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4052 case CTF_TYPE_SEQUENCE
:
4053 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4063 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4066 int field_index
= -1;
4067 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4070 case CTF_TYPE_STRUCT
:
4071 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4074 case CTF_TYPE_VARIANT
:
4075 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4085 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4086 struct bt_ctf_field_type
*type
)
4088 struct bt_ctf_field_path
*field_path
= NULL
;
4089 struct bt_ctf_field_type_variant
*variant
;
4091 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4095 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4097 field_path
= bt_get(variant
->tag_field_path
);
4102 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4103 struct bt_ctf_field_type
*type
)
4105 struct bt_ctf_field_path
*field_path
= NULL
;
4106 struct bt_ctf_field_type_sequence
*sequence
;
4108 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4112 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4114 field_path
= bt_get(sequence
->length_field_path
);