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-writer/writer-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/endian.h>
43 struct range_overlap_query
{
58 void bt_ctf_field_type_destroy(struct bt_object
*);
60 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
62 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
64 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
66 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
68 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
70 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
72 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
74 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
77 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
78 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
79 [BT_CTF_TYPE_ID_ENUM
] =
80 bt_ctf_field_type_enumeration_destroy
,
81 [BT_CTF_TYPE_ID_FLOAT
] =
82 bt_ctf_field_type_floating_point_destroy
,
83 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
84 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
85 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
86 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
87 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
91 void generic_field_type_freeze(struct bt_ctf_field_type
*);
93 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
95 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
97 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
99 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
101 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
104 type_freeze_func
const type_freeze_funcs
[] = {
105 [BT_CTF_TYPE_ID_INTEGER
] = generic_field_type_freeze
,
106 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
107 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
108 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
109 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
110 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
111 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
112 [BT_CTF_TYPE_ID_STRING
] = generic_field_type_freeze
,
116 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
117 struct metadata_context
*);
119 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_floating_point_serialize(
123 struct bt_ctf_field_type
*, struct metadata_context
*);
125 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
126 struct metadata_context
*);
128 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
141 type_serialize_func
const type_serialize_funcs
[] = {
142 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
143 [BT_CTF_TYPE_ID_ENUM
] =
144 bt_ctf_field_type_enumeration_serialize
,
145 [BT_CTF_TYPE_ID_FLOAT
] =
146 bt_ctf_field_type_floating_point_serialize
,
147 [BT_CTF_TYPE_ID_STRUCT
] =
148 bt_ctf_field_type_structure_serialize
,
149 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
150 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
151 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
152 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
156 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
157 int byte_order
, int set_native
);
159 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_floating_point_set_byte_order(
163 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
165 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
166 int byte_order
, int set_native
);
168 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 /* The set_native flag only set the byte order if it is set to native */
179 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
180 int byte_order
, int set_native
) = {
181 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
182 [BT_CTF_TYPE_ID_ENUM
] =
183 bt_ctf_field_type_enumeration_set_byte_order
,
184 [BT_CTF_TYPE_ID_FLOAT
] =
185 bt_ctf_field_type_floating_point_set_byte_order
,
186 [BT_CTF_TYPE_ID_STRUCT
] =
187 bt_ctf_field_type_structure_set_byte_order
,
188 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
189 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
190 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
191 [BT_CTF_TYPE_ID_STRING
] = NULL
,
195 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
196 struct bt_ctf_field_type
*);
198 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
217 struct bt_ctf_field_type
*);
220 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
221 struct bt_ctf_field_type
*) = {
222 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
223 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
224 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
225 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
226 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
227 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
228 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
229 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
233 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
234 struct bt_ctf_field_type
*);
236 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
237 struct bt_ctf_field_type
*);
239 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
240 struct bt_ctf_field_type
*);
242 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
243 struct bt_ctf_field_type
*);
245 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
246 struct bt_ctf_field_type
*);
248 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
249 struct bt_ctf_field_type
*);
251 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
252 struct bt_ctf_field_type
*);
254 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
255 struct bt_ctf_field_type
*);
258 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
259 struct bt_ctf_field_type
*) = {
260 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
261 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
262 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
263 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
264 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
265 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
266 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
267 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
271 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
273 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
275 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
277 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
279 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
282 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
283 [BT_CTF_TYPE_ID_INTEGER
] = NULL
,
284 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
285 [BT_CTF_TYPE_ID_STRING
] = NULL
,
286 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
287 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
288 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
289 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
290 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
294 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
300 void destroy_structure_field(struct structure_field
*field
)
307 void check_ranges_overlap(gpointer element
, gpointer query
)
309 struct enumeration_mapping
*mapping
= element
;
310 struct range_overlap_query
*overlap_query
= query
;
312 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
313 && overlap_query
->range_start
._signed
<=
314 mapping
->range_end
._signed
) {
315 overlap_query
->overlaps
= 1;
316 overlap_query
->mapping_name
= mapping
->string
;
319 overlap_query
->overlaps
|=
320 mapping
->string
== overlap_query
->mapping_name
;
324 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
326 struct enumeration_mapping
*mapping
= element
;
327 struct range_overlap_query
*overlap_query
= query
;
329 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
330 && overlap_query
->range_start
._unsigned
<=
331 mapping
->range_end
._unsigned
) {
332 overlap_query
->overlaps
= 1;
333 overlap_query
->mapping_name
= mapping
->string
;
336 overlap_query
->overlaps
|=
337 mapping
->string
== overlap_query
->mapping_name
;
341 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
342 struct enumeration_mapping
**b
)
344 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
348 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
349 struct enumeration_mapping
**b
)
351 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
355 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
357 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
359 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
360 (type_id
< BT_CTF_NR_TYPE_IDS
));
362 bt_object_init(type
, bt_ctf_field_type_destroy
);
363 type
->freeze
= type_freeze_funcs
[type_id
];
364 type
->serialize
= type_serialize_funcs
[type_id
];
367 int ret
= bt_ctf_field_type_set_byte_order(type
,
368 BT_CTF_BYTE_ORDER_NATIVE
);
372 type
->declaration
->alignment
= 1;
376 int add_structure_field(GPtrArray
*fields
,
377 GHashTable
*field_name_to_index
,
378 struct bt_ctf_field_type
*field_type
,
379 const char *field_name
)
382 GQuark name_quark
= g_quark_from_string(field_name
);
383 struct structure_field
*field
;
385 /* Make sure structure does not contain a field of the same name */
386 if (g_hash_table_lookup_extended(field_name_to_index
,
387 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
392 field
= g_new0(struct structure_field
, 1);
399 field
->name
= name_quark
;
400 field
->type
= field_type
;
401 g_hash_table_insert(field_name_to_index
,
402 (gpointer
) (unsigned long) name_quark
,
403 (gpointer
) (unsigned long) fields
->len
);
404 g_ptr_array_add(fields
, field
);
410 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
412 struct bt_ctf_field_type
*type
;
413 enum bt_ctf_type_id type_id
;
415 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
416 type_id
= type
->declaration
->id
;
417 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
418 type_id
>= BT_CTF_NR_TYPE_IDS
) {
422 type_destroy_funcs
[type_id
](type
);
426 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
430 struct bt_ctf_field_type_enumeration
*enumeration
=
431 container_of(type
, struct bt_ctf_field_type_enumeration
,
433 struct bt_ctf_field_type
*container_type
=
434 bt_ctf_field_type_enumeration_get_container_type(type
);
436 if (!container_type
) {
441 ret
= bt_ctf_field_type_validate(container_type
);
446 /* Ensure enum has entries */
447 ret
= enumeration
->entries
->len
? 0 : -1;
450 BT_PUT(container_type
);
455 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
458 struct bt_ctf_field_type
*element_type
= NULL
;
459 struct bt_ctf_field_type_sequence
*sequence
=
460 container_of(type
, struct bt_ctf_field_type_sequence
,
463 /* Length field name should be set at this point */
464 if (sequence
->length_field_name
->len
== 0) {
469 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
475 ret
= bt_ctf_field_type_validate(element_type
);
478 BT_PUT(element_type
);
484 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
487 struct bt_ctf_field_type
*element_type
= NULL
;
489 element_type
= bt_ctf_field_type_array_get_element_type(type
);
495 ret
= bt_ctf_field_type_validate(element_type
);
498 BT_PUT(element_type
);
504 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
507 struct bt_ctf_field_type
*child_type
= NULL
;
508 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
511 if (field_count
< 0) {
516 for (i
= 0; i
< field_count
; ++i
) {
517 ret
= bt_ctf_field_type_structure_get_field(type
,
518 NULL
, &child_type
, i
);
523 ret
= bt_ctf_field_type_validate(child_type
);
538 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
542 struct bt_ctf_field_type
*child_type
= NULL
;
543 struct bt_ctf_field_type_variant
*variant
=
544 container_of(type
, struct bt_ctf_field_type_variant
,
547 int tag_mappings_count
;
549 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
555 bt_ctf_field_type_enumeration_get_mapping_count(
556 (struct bt_ctf_field_type
*) variant
->tag
);
558 if (tag_mappings_count
!= variant
->fields
->len
) {
563 for (i
= 0; i
< tag_mappings_count
; ++i
) {
565 int64_t range_start
, range_end
;
566 struct bt_ctf_field_type
*ft
;
568 ret
= bt_ctf_field_type_enumeration_get_mapping(
569 (struct bt_ctf_field_type
*) variant
->tag
,
570 i
, &label
, &range_start
, &range_end
);
579 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
589 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
590 if (field_count
< 0) {
595 for (i
= 0; i
< field_count
; ++i
) {
596 ret
= bt_ctf_field_type_variant_get_field(type
,
597 NULL
, &child_type
, i
);
602 ret
= bt_ctf_field_type_validate(child_type
);
617 * This function validates a given field type without considering
618 * where this field type is located. It only validates the properties
619 * of the given field type and the properties of its children if
623 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
626 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
634 /* Already marked as valid */
638 if (type_validate_funcs
[id
]) {
639 ret
= type_validate_funcs
[id
](type
);
642 if (!ret
&& type
->frozen
) {
643 /* Field type is valid */
651 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
653 struct bt_ctf_field_type_integer
*integer
=
654 g_new0(struct bt_ctf_field_type_integer
, 1);
656 if (!integer
|| size
== 0 || size
> 64) {
660 integer
->parent
.declaration
= &integer
->declaration
.p
;
661 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
662 integer
->declaration
.len
= size
;
663 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
664 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
665 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
666 return &integer
->parent
;
669 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
672 struct bt_ctf_field_type_integer
*integer
;
674 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
679 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
680 ret
= (int) integer
->declaration
.len
;
685 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
688 struct bt_ctf_field_type_integer
*integer
;
690 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
695 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
696 ret
= integer
->declaration
.signedness
;
701 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
705 struct bt_ctf_field_type_integer
*integer
;
707 if (!type
|| type
->frozen
||
708 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
713 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
714 integer
->declaration
.signedness
= !!is_signed
;
719 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
720 struct bt_ctf_field_type
*type
)
722 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
723 struct bt_ctf_field_type_integer
*integer
;
725 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
729 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
730 ret
= integer
->declaration
.base
;
735 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
736 enum bt_ctf_integer_base base
)
740 if (!type
|| type
->frozen
||
741 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
747 case BT_CTF_INTEGER_BASE_BINARY
:
748 case BT_CTF_INTEGER_BASE_OCTAL
:
749 case BT_CTF_INTEGER_BASE_DECIMAL
:
750 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
752 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
753 struct bt_ctf_field_type_integer
, parent
);
754 integer
->declaration
.base
= base
;
764 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
765 struct bt_ctf_field_type
*type
)
767 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
768 struct bt_ctf_field_type_integer
*integer
;
770 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
774 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
775 ret
= integer
->declaration
.encoding
;
780 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
781 enum bt_ctf_string_encoding encoding
)
784 struct bt_ctf_field_type_integer
*integer
;
786 if (!type
|| type
->frozen
||
787 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
788 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
789 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
794 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
795 integer
->declaration
.encoding
= encoding
;
800 struct bt_ctf_clock
*bt_ctf_field_type_integer_get_mapped_clock(
801 struct bt_ctf_field_type
*type
)
803 struct bt_ctf_field_type_integer
*integer
;
804 struct bt_ctf_clock
*clock
= NULL
;
810 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
811 clock
= integer
->mapped_clock
;
817 int bt_ctf_field_type_integer_set_mapped_clock(
818 struct bt_ctf_field_type
*type
,
819 struct bt_ctf_clock
*clock
)
821 struct bt_ctf_field_type_integer
*integer
;
824 if (!type
|| type
->frozen
) {
829 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
830 bt_put(integer
->mapped_clock
);
832 integer
->mapped_clock
= clock
;
837 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
838 struct bt_ctf_field_type
*integer_container_type
)
840 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
842 if (!integer_container_type
) {
846 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
850 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
855 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
856 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
857 bt_get(integer_container_type
);
858 enumeration
->container
= integer_container_type
;
859 enumeration
->entries
= g_ptr_array_new_with_free_func(
860 (GDestroyNotify
)destroy_enumeration_mapping
);
861 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
862 return &enumeration
->parent
;
868 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
869 struct bt_ctf_field_type
*type
)
871 struct bt_ctf_field_type
*container_type
= NULL
;
872 struct bt_ctf_field_type_enumeration
*enumeration_type
;
878 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
882 enumeration_type
= container_of(type
,
883 struct bt_ctf_field_type_enumeration
, parent
);
884 container_type
= enumeration_type
->container
;
885 bt_get(container_type
);
887 return container_type
;
890 int bt_ctf_field_type_enumeration_add_mapping(
891 struct bt_ctf_field_type
*type
, const char *string
,
892 int64_t range_start
, int64_t range_end
)
896 struct enumeration_mapping
*mapping
;
897 struct bt_ctf_field_type_enumeration
*enumeration
;
898 struct range_overlap_query query
;
899 char *escaped_string
;
901 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
903 (range_end
< range_start
)) {
908 if (!string
|| strlen(string
) == 0) {
913 escaped_string
= g_strescape(string
, NULL
);
914 if (!escaped_string
) {
919 mapping_name
= g_quark_from_string(escaped_string
);
920 query
= (struct range_overlap_query
) {
921 .range_start
._signed
= range_start
,
922 .range_end
._signed
= range_end
,
923 .mapping_name
= mapping_name
,
925 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
928 /* Check that the range does not overlap with one already present */
929 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap
, &query
);
930 if (query
.overlaps
) {
935 mapping
= g_new(struct enumeration_mapping
, 1);
941 *mapping
= (struct enumeration_mapping
) {
942 .range_start
._signed
= range_start
,
943 .range_end
._signed
= range_end
, .string
= mapping_name
};
944 g_ptr_array_add(enumeration
->entries
, mapping
);
945 g_ptr_array_sort(enumeration
->entries
,
946 (GCompareFunc
)compare_enumeration_mappings_signed
);
948 free(escaped_string
);
953 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
954 struct bt_ctf_field_type
*type
, const char *string
,
955 uint64_t range_start
, uint64_t range_end
)
959 struct enumeration_mapping
*mapping
;
960 struct bt_ctf_field_type_enumeration
*enumeration
;
961 struct range_overlap_query query
;
962 char *escaped_string
;
964 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
966 (range_end
< range_start
)) {
971 if (!string
|| strlen(string
) == 0) {
976 escaped_string
= g_strescape(string
, NULL
);
977 if (!escaped_string
) {
982 mapping_name
= g_quark_from_string(escaped_string
);
983 query
= (struct range_overlap_query
) {
984 .range_start
._unsigned
= range_start
,
985 .range_end
._unsigned
= range_end
,
986 .mapping_name
= mapping_name
,
988 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
991 /* Check that the range does not overlap with one already present */
992 g_ptr_array_foreach(enumeration
->entries
, check_ranges_overlap_unsigned
,
994 if (query
.overlaps
) {
999 mapping
= g_new(struct enumeration_mapping
, 1);
1005 *mapping
= (struct enumeration_mapping
) {
1006 .range_start
._unsigned
= range_start
,
1007 .range_end
._unsigned
= range_end
, .string
= mapping_name
};
1008 g_ptr_array_add(enumeration
->entries
, mapping
);
1009 g_ptr_array_sort(enumeration
->entries
,
1010 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1012 free(escaped_string
);
1017 const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
1018 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1021 const char *name
= NULL
;
1022 struct range_overlap_query query
=
1023 (struct range_overlap_query
) {
1024 .range_start
._unsigned
= value
,
1025 .range_end
._unsigned
= value
,
1028 g_ptr_array_foreach(enumeration_type
->entries
,
1029 check_ranges_overlap_unsigned
,
1031 if (!query
.overlaps
) {
1035 name
= g_quark_to_string(query
.mapping_name
);
1040 const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
1041 struct bt_ctf_field_type_enumeration
*enumeration_type
,
1044 const char *name
= NULL
;
1045 struct range_overlap_query query
=
1046 (struct range_overlap_query
) {
1047 .range_start
._signed
= value
,
1048 .range_end
._signed
= value
,
1051 g_ptr_array_foreach(enumeration_type
->entries
, check_ranges_overlap
,
1053 if (!query
.overlaps
) {
1057 name
= g_quark_to_string(query
.mapping_name
);
1062 int bt_ctf_field_type_enumeration_get_mapping_count(
1063 struct bt_ctf_field_type
*type
)
1066 struct bt_ctf_field_type_enumeration
*enumeration
;
1068 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1073 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1075 ret
= (int) enumeration
->entries
->len
;
1081 struct enumeration_mapping
*get_enumeration_mapping(
1082 struct bt_ctf_field_type
*type
, int index
)
1084 struct enumeration_mapping
*mapping
= NULL
;
1085 struct bt_ctf_field_type_enumeration
*enumeration
;
1087 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1089 if (index
>= enumeration
->entries
->len
) {
1093 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
1098 int bt_ctf_field_type_enumeration_get_mapping(
1099 struct bt_ctf_field_type
*type
, int index
,
1100 const char **string
, int64_t *range_start
, int64_t *range_end
)
1102 struct enumeration_mapping
*mapping
;
1105 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1106 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1111 mapping
= get_enumeration_mapping(type
, index
);
1117 *string
= g_quark_to_string(mapping
->string
);
1118 *range_start
= mapping
->range_start
._signed
;
1119 *range_end
= mapping
->range_end
._signed
;
1124 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1125 struct bt_ctf_field_type
*type
, int index
,
1126 const char **string
, uint64_t *range_start
, uint64_t *range_end
)
1128 struct enumeration_mapping
*mapping
;
1131 if (!type
|| index
< 0 || !string
|| !range_start
|| !range_end
||
1132 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1137 mapping
= get_enumeration_mapping(type
, index
);
1143 *string
= g_quark_to_string(mapping
->string
);
1144 *range_start
= mapping
->range_start
._unsigned
;
1145 *range_end
= mapping
->range_end
._unsigned
;
1150 int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
1151 struct bt_ctf_field_type
*type
, const char *name
)
1154 struct bt_ctf_field_type_enumeration
*enumeration
;
1157 if (!type
|| !name
||
1158 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1163 name_quark
= g_quark_try_string(name
);
1169 enumeration
= container_of(type
,
1170 struct bt_ctf_field_type_enumeration
, parent
);
1171 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1172 struct enumeration_mapping
*mapping
=
1173 get_enumeration_mapping(type
, i
);
1175 if (mapping
->string
== name_quark
) {
1186 int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
1187 struct bt_ctf_field_type
*type
, int64_t value
)
1189 struct bt_ctf_field_type_enumeration
*enumeration
;
1192 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1197 enumeration
= container_of(type
,
1198 struct bt_ctf_field_type_enumeration
, parent
);
1199 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1200 struct enumeration_mapping
*mapping
=
1201 get_enumeration_mapping(type
, i
);
1203 if (value
>= mapping
->range_start
._signed
&&
1204 value
<= mapping
->range_end
._signed
) {
1215 int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
1216 struct bt_ctf_field_type
*type
, uint64_t value
)
1218 struct bt_ctf_field_type_enumeration
*enumeration
;
1221 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1226 enumeration
= container_of(type
,
1227 struct bt_ctf_field_type_enumeration
, parent
);
1228 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
1229 struct enumeration_mapping
*mapping
=
1230 get_enumeration_mapping(type
, i
);
1232 if (value
>= mapping
->range_start
._unsigned
&&
1233 value
<= mapping
->range_end
._unsigned
) {
1244 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1246 struct bt_ctf_field_type_floating_point
*floating_point
=
1247 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1249 if (!floating_point
) {
1253 floating_point
->declaration
.sign
= &floating_point
->sign
;
1254 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1255 floating_point
->declaration
.exp
= &floating_point
->exp
;
1256 floating_point
->sign
.len
= 1;
1257 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1258 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1259 floating_point
->declaration
.exp
->len
=
1260 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1261 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1262 floating_point
->sign
.p
.alignment
= 1;
1263 floating_point
->mantissa
.p
.alignment
= 1;
1264 floating_point
->exp
.p
.alignment
= 1;
1266 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1268 return floating_point
? &floating_point
->parent
: NULL
;
1271 int bt_ctf_field_type_floating_point_get_exponent_digits(
1272 struct bt_ctf_field_type
*type
)
1275 struct bt_ctf_field_type_floating_point
*floating_point
;
1277 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1282 floating_point
= container_of(type
,
1283 struct bt_ctf_field_type_floating_point
, parent
);
1284 ret
= (int) floating_point
->declaration
.exp
->len
;
1289 int bt_ctf_field_type_floating_point_set_exponent_digits(
1290 struct bt_ctf_field_type
*type
,
1291 unsigned int exponent_digits
)
1294 struct bt_ctf_field_type_floating_point
*floating_point
;
1296 if (!type
|| type
->frozen
||
1297 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1302 floating_point
= container_of(type
,
1303 struct bt_ctf_field_type_floating_point
, parent
);
1304 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1305 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1307 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1312 floating_point
->declaration
.exp
->len
= exponent_digits
;
1317 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1318 struct bt_ctf_field_type
*type
)
1321 struct bt_ctf_field_type_floating_point
*floating_point
;
1323 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1328 floating_point
= container_of(type
,
1329 struct bt_ctf_field_type_floating_point
, parent
);
1330 ret
= (int) floating_point
->mantissa
.len
+ 1;
1335 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1336 struct bt_ctf_field_type
*type
,
1337 unsigned int mantissa_digits
)
1340 struct bt_ctf_field_type_floating_point
*floating_point
;
1342 if (!type
|| type
->frozen
||
1343 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1348 floating_point
= container_of(type
,
1349 struct bt_ctf_field_type_floating_point
, parent
);
1351 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1352 (mantissa_digits
!= DBL_MANT_DIG
) &&
1353 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1358 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1363 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1365 struct bt_ctf_field_type_structure
*structure
=
1366 g_new0(struct bt_ctf_field_type_structure
, 1);
1372 structure
->parent
.declaration
= &structure
->declaration
.p
;
1373 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1374 structure
->fields
= g_ptr_array_new_with_free_func(
1375 (GDestroyNotify
)destroy_structure_field
);
1376 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1377 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1378 return &structure
->parent
;
1383 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1384 struct bt_ctf_field_type
*field_type
,
1385 const char *field_name
)
1388 struct bt_ctf_field_type_structure
*structure
;
1390 if (!type
|| !field_type
|| type
->frozen
||
1391 bt_ctf_validate_identifier(field_name
) ||
1392 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1397 structure
= container_of(type
,
1398 struct bt_ctf_field_type_structure
, parent
);
1399 if (add_structure_field(structure
->fields
,
1400 structure
->field_name_to_index
, field_type
, field_name
)) {
1408 int bt_ctf_field_type_structure_get_field_count(
1409 struct bt_ctf_field_type
*type
)
1412 struct bt_ctf_field_type_structure
*structure
;
1414 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1419 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1421 ret
= (int) structure
->fields
->len
;
1426 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1427 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1430 struct bt_ctf_field_type_structure
*structure
;
1431 struct structure_field
*field
;
1434 if (!type
|| index
< 0 ||
1435 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1440 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1442 if (index
>= structure
->fields
->len
) {
1447 field
= g_ptr_array_index(structure
->fields
, index
);
1449 *field_type
= field
->type
;
1450 bt_get(field
->type
);
1453 *field_name
= g_quark_to_string(field
->name
);
1459 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1460 struct bt_ctf_field_type
*type
,
1465 struct structure_field
*field
;
1466 struct bt_ctf_field_type_structure
*structure
;
1467 struct bt_ctf_field_type
*field_type
= NULL
;
1469 if (!type
|| !name
) {
1473 name_quark
= g_quark_try_string(name
);
1478 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1480 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1481 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1485 field
= structure
->fields
->pdata
[index
];
1486 field_type
= field
->type
;
1492 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1493 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1495 struct bt_ctf_field_type_variant
*variant
= NULL
;
1497 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1501 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1506 variant
->parent
.declaration
= &variant
->declaration
.p
;
1507 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1508 variant
->tag_name
= g_string_new(tag_name
);
1509 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1510 variant
->fields
= g_ptr_array_new_with_free_func(
1511 (GDestroyNotify
) destroy_structure_field
);
1514 variant
->tag
= container_of(enum_tag
,
1515 struct bt_ctf_field_type_enumeration
, parent
);
1518 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1519 /* A variant's alignment is undefined */
1520 variant
->parent
.declaration
->alignment
= 0;
1521 return &variant
->parent
;
1526 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1527 struct bt_ctf_field_type
*type
)
1529 struct bt_ctf_field_type_variant
*variant
;
1530 struct bt_ctf_field_type
*tag_type
= NULL
;
1532 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1536 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1537 if (!variant
->tag
) {
1541 tag_type
= &variant
->tag
->parent
;
1547 const char *bt_ctf_field_type_variant_get_tag_name(
1548 struct bt_ctf_field_type
*type
)
1550 struct bt_ctf_field_type_variant
*variant
;
1551 const char *tag_name
= NULL
;
1553 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1557 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1558 if (variant
->tag_name
->len
== 0) {
1562 tag_name
= variant
->tag_name
->str
;
1567 int bt_ctf_field_type_variant_set_tag_name(
1568 struct bt_ctf_field_type
*type
, const char *name
)
1571 struct bt_ctf_field_type_variant
*variant
;
1573 if (!type
|| type
->frozen
||
1574 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1575 bt_ctf_validate_identifier(name
)) {
1580 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1581 g_string_assign(variant
->tag_name
, name
);
1586 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1587 struct bt_ctf_field_type
*field_type
,
1588 const char *field_name
)
1592 struct bt_ctf_field_type_variant
*variant
;
1593 GQuark field_name_quark
= g_quark_from_string(field_name
);
1595 if (!type
|| !field_type
|| type
->frozen
||
1596 bt_ctf_validate_identifier(field_name
) ||
1597 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1602 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1604 /* The user has explicitly provided a tag; validate against it. */
1608 /* Make sure this name is present in the enum tag */
1609 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1610 struct enumeration_mapping
*mapping
=
1611 g_ptr_array_index(variant
->tag
->entries
, i
);
1613 if (mapping
->string
== field_name_quark
) {
1620 /* Validation failed */
1626 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1627 field_type
, field_name
)) {
1635 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1636 struct bt_ctf_field_type
*type
,
1637 const char *field_name
)
1641 struct structure_field
*field
;
1642 struct bt_ctf_field_type_variant
*variant
;
1643 struct bt_ctf_field_type
*field_type
= NULL
;
1645 if (!type
|| !field_name
) {
1649 name_quark
= g_quark_try_string(field_name
);
1654 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1655 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1656 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1660 field
= g_ptr_array_index(variant
->fields
, index
);
1661 field_type
= field
->type
;
1667 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1668 struct bt_ctf_field_type
*type
,
1669 struct bt_ctf_field
*tag
)
1671 const char *enum_value
;
1672 struct bt_ctf_field_type
*field_type
= NULL
;
1674 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1678 enum_value
= bt_ctf_field_enumeration_get_mapping_name(tag
);
1683 /* Already increments field_type's reference count */
1684 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1690 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1693 struct bt_ctf_field_type_variant
*variant
;
1695 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1700 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1702 ret
= (int) variant
->fields
->len
;
1708 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1709 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1712 struct bt_ctf_field_type_variant
*variant
;
1713 struct structure_field
*field
;
1716 if (!type
|| index
< 0 ||
1717 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1722 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1724 if (index
>= variant
->fields
->len
) {
1729 field
= g_ptr_array_index(variant
->fields
, index
);
1731 *field_type
= field
->type
;
1732 bt_get(field
->type
);
1735 *field_name
= g_quark_to_string(field
->name
);
1741 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1742 struct bt_ctf_field_type
*element_type
,
1743 unsigned int length
)
1745 struct bt_ctf_field_type_array
*array
= NULL
;
1747 if (!element_type
|| length
== 0) {
1751 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1756 array
->parent
.declaration
= &array
->declaration
.p
;
1757 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1759 bt_get(element_type
);
1760 array
->element_type
= element_type
;
1761 array
->length
= length
;
1762 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1763 return &array
->parent
;
1768 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1769 struct bt_ctf_field_type
*type
)
1771 struct bt_ctf_field_type
*ret
= NULL
;
1772 struct bt_ctf_field_type_array
*array
;
1774 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1778 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1779 ret
= array
->element_type
;
1786 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1787 struct bt_ctf_field_type
*element_type
)
1790 struct bt_ctf_field_type_array
*array
;
1792 if (!type
|| !element_type
||
1793 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1798 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1800 if (array
->element_type
) {
1801 BT_PUT(array
->element_type
);
1804 array
->element_type
= element_type
;
1805 bt_get(array
->element_type
);
1811 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
1814 struct bt_ctf_field_type_array
*array
;
1816 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1821 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1822 ret
= (int64_t) array
->length
;
1827 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
1828 struct bt_ctf_field_type
*element_type
,
1829 const char *length_field_name
)
1831 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
1833 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
1837 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
1842 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
1843 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
1844 bt_get(element_type
);
1845 sequence
->element_type
= element_type
;
1846 sequence
->length_field_name
= g_string_new(length_field_name
);
1847 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
1848 return &sequence
->parent
;
1853 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
1854 struct bt_ctf_field_type
*type
)
1856 struct bt_ctf_field_type
*ret
= NULL
;
1857 struct bt_ctf_field_type_sequence
*sequence
;
1859 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1863 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1865 ret
= sequence
->element_type
;
1872 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
1873 struct bt_ctf_field_type
*element_type
)
1876 struct bt_ctf_field_type_sequence
*sequence
;
1878 if (!type
|| !element_type
||
1879 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1884 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
1886 if (sequence
->element_type
) {
1887 BT_PUT(sequence
->element_type
);
1890 sequence
->element_type
= element_type
;
1891 bt_get(sequence
->element_type
);
1897 const char *bt_ctf_field_type_sequence_get_length_field_name(
1898 struct bt_ctf_field_type
*type
)
1900 const char *ret
= NULL
;
1901 struct bt_ctf_field_type_sequence
*sequence
;
1903 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
1907 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
1909 ret
= sequence
->length_field_name
->str
;
1914 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
1916 struct bt_ctf_field_type_string
*string
=
1917 g_new0(struct bt_ctf_field_type_string
, 1);
1923 string
->parent
.declaration
= &string
->declaration
.p
;
1924 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
1925 bt_ctf_field_type_init(&string
->parent
, TRUE
);
1926 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
1927 string
->parent
.declaration
->alignment
= CHAR_BIT
;
1928 return &string
->parent
;
1931 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
1932 struct bt_ctf_field_type
*type
)
1934 struct bt_ctf_field_type_string
*string
;
1935 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
1937 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
1941 string
= container_of(type
, struct bt_ctf_field_type_string
,
1943 ret
= string
->declaration
.encoding
;
1948 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
1949 enum bt_ctf_string_encoding encoding
)
1952 struct bt_ctf_field_type_string
*string
;
1954 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
1955 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
1956 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
1961 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
1962 string
->declaration
.encoding
= encoding
;
1967 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
1970 enum bt_ctf_type_id type_id
;
1978 ret
= (int) type
->declaration
->alignment
;
1982 type_id
= bt_ctf_field_type_get_type_id(type
);
1984 case BT_CTF_TYPE_ID_SEQUENCE
:
1986 struct bt_ctf_field_type
*element
=
1987 bt_ctf_field_type_sequence_get_element_type(type
);
1994 ret
= bt_ctf_field_type_get_alignment(element
);
1998 case BT_CTF_TYPE_ID_ARRAY
:
2000 struct bt_ctf_field_type
*element
=
2001 bt_ctf_field_type_array_get_element_type(type
);
2008 ret
= bt_ctf_field_type_get_alignment(element
);
2012 case BT_CTF_TYPE_ID_STRUCT
:
2014 int i
, element_count
;
2016 element_count
= bt_ctf_field_type_structure_get_field_count(
2018 if (element_count
< 0) {
2019 ret
= element_count
;
2023 for (i
= 0; i
< element_count
; i
++) {
2024 struct bt_ctf_field_type
*field
;
2025 int field_alignment
;
2027 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2034 field_alignment
= bt_ctf_field_type_get_alignment(
2037 if (field_alignment
< 0) {
2038 ret
= field_alignment
;
2042 type
->declaration
->alignment
= MAX(field_alignment
,
2043 type
->declaration
->alignment
);
2045 ret
= (int) type
->declaration
->alignment
;
2048 case BT_CTF_TYPE_ID_UNKNOWN
:
2052 ret
= (int) type
->declaration
->alignment
;
2060 int is_power_of_two(unsigned int value
)
2062 return ((value
& (value
- 1)) == 0) && value
> 0;
2065 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2066 unsigned int alignment
)
2069 enum bt_ctf_type_id type_id
;
2071 /* Alignment must be a power of two */
2072 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2077 type_id
= bt_ctf_field_type_get_type_id(type
);
2078 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2083 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2084 alignment
!= CHAR_BIT
) {
2089 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2090 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2091 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2092 /* Setting an alignment on these types makes no sense */
2097 type
->declaration
->alignment
= alignment
;
2103 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2104 struct bt_ctf_field_type
*type
)
2106 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2112 switch (type
->declaration
->id
) {
2113 case BT_CTF_TYPE_ID_INTEGER
:
2115 struct bt_ctf_field_type_integer
*integer
= container_of(
2116 type
, struct bt_ctf_field_type_integer
, parent
);
2117 ret
= integer
->user_byte_order
;
2120 case BT_CTF_TYPE_ID_FLOAT
:
2122 struct bt_ctf_field_type_floating_point
*floating_point
=
2124 struct bt_ctf_field_type_floating_point
,
2126 ret
= floating_point
->user_byte_order
;
2133 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2134 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2135 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2136 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2142 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2143 enum bt_ctf_byte_order byte_order
)
2146 int internal_byte_order
;
2147 enum bt_ctf_type_id type_id
;
2149 if (!type
|| type
->frozen
) {
2154 switch (byte_order
) {
2155 case BT_CTF_BYTE_ORDER_NATIVE
:
2156 /* Leave unset. Will be initialized by parent. */
2157 internal_byte_order
= 0;
2159 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2160 internal_byte_order
= LITTLE_ENDIAN
;
2162 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2163 case BT_CTF_BYTE_ORDER_NETWORK
:
2164 internal_byte_order
= BIG_ENDIAN
;
2171 type_id
= type
->declaration
->id
;
2172 if (set_byte_order_funcs
[type_id
]) {
2173 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2179 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2180 struct bt_ctf_field_type
*type
)
2183 return BT_CTF_TYPE_ID_UNKNOWN
;
2186 return type
->declaration
->id
;
2189 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2191 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2194 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2196 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2199 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2201 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2204 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2206 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2209 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2211 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2214 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2216 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2219 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2221 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2224 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2226 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2229 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2234 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2240 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2250 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2251 struct bt_ctf_field_type_variant
*variant
,
2254 struct bt_ctf_field_type
*type
= NULL
;
2255 GQuark field_name_quark
;
2257 struct structure_field
*field_entry
;
2258 struct range_overlap_query query
= {
2259 .range_start
._signed
= tag_value
,
2260 .range_end
._signed
= tag_value
,
2261 .mapping_name
= 0, .overlaps
= 0};
2263 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2265 if (!query
.overlaps
) {
2269 field_name_quark
= query
.mapping_name
;
2270 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2271 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2275 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2276 type
= field_entry
->type
;
2282 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2283 struct bt_ctf_field_type_variant
*variant
,
2286 struct bt_ctf_field_type
*type
= NULL
;
2287 GQuark field_name_quark
;
2289 struct structure_field
*field_entry
;
2290 struct range_overlap_query query
= {
2291 .range_start
._unsigned
= tag_value
,
2292 .range_end
._unsigned
= tag_value
,
2293 .mapping_name
= 0, .overlaps
= 0};
2295 g_ptr_array_foreach(variant
->tag
->entries
,
2296 check_ranges_overlap_unsigned
,
2298 if (!query
.overlaps
) {
2302 field_name_quark
= query
.mapping_name
;
2303 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2304 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2308 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2309 type
= field_entry
->type
;
2315 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2316 struct metadata_context
*context
)
2320 if (!type
|| !context
) {
2325 /* Make sure field type is valid before serializing it */
2326 ret
= bt_ctf_field_type_validate(type
);
2332 ret
= type
->serialize(type
, context
);
2338 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2345 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2346 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2347 set_byte_order_funcs
[type
->declaration
->id
](type
,
2353 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2355 struct bt_ctf_field_type
*copy
= NULL
;
2361 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2367 int bt_ctf_field_type_structure_get_field_name_index(
2368 struct bt_ctf_field_type
*type
, const char *name
)
2373 struct bt_ctf_field_type_structure
*structure
;
2375 if (!type
|| !name
||
2376 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2381 name_quark
= g_quark_try_string(name
);
2387 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2389 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2390 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2400 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2401 struct bt_ctf_field_type
*field
, int index
)
2404 struct bt_ctf_field_type_structure
*structure
;
2406 if (!type
|| !field
||
2407 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2412 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2414 if (index
< 0 || index
>= structure
->fields
->len
) {
2420 bt_put(((struct structure_field
*)
2421 g_ptr_array_index(structure
->fields
, index
))->type
);
2422 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2429 int bt_ctf_field_type_variant_get_field_name_index(
2430 struct bt_ctf_field_type
*type
, const char *name
)
2435 struct bt_ctf_field_type_variant
*variant
;
2437 if (!type
|| !name
||
2438 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2443 name_quark
= g_quark_try_string(name
);
2449 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2451 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2452 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2462 int bt_ctf_field_type_sequence_set_length_field_path(
2463 struct bt_ctf_field_type
*type
,
2464 struct bt_ctf_field_path
*path
)
2467 struct bt_ctf_field_type_sequence
*sequence
;
2469 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2470 BT_CTF_TYPE_ID_SEQUENCE
) {
2475 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2478 BT_MOVE(sequence
->length_field_path
, path
);
2484 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2485 struct bt_ctf_field_path
*path
)
2488 struct bt_ctf_field_type_variant
*variant
;
2490 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2491 BT_CTF_TYPE_ID_VARIANT
) {
2496 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2499 BT_MOVE(variant
->tag_field_path
, path
);
2505 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2506 struct bt_ctf_field_type
*tag
)
2509 struct bt_ctf_field_type_variant
*variant
;
2511 if (!type
|| !tag
||
2512 bt_ctf_field_type_get_type_id(tag
) !=
2513 BT_CTF_TYPE_ID_ENUM
) {
2518 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2522 bt_put(&variant
->tag
->parent
);
2524 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2531 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2532 struct bt_ctf_field_type
*field
, int index
)
2535 struct bt_ctf_field_type_variant
*variant
;
2537 if (!type
|| !field
||
2538 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2543 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2545 if (index
< 0 || index
>= variant
->fields
->len
) {
2551 bt_put(((struct structure_field
*)
2552 g_ptr_array_index(variant
->fields
, index
))->type
);
2553 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2560 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2562 struct bt_ctf_field_type_integer
*integer
=
2563 (struct bt_ctf_field_type_integer
*) type
;
2569 bt_put(integer
->mapped_clock
);
2574 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2576 struct bt_ctf_field_type_enumeration
*enumeration
=
2577 (struct bt_ctf_field_type_enumeration
*) type
;
2583 g_ptr_array_free(enumeration
->entries
, TRUE
);
2584 bt_put(enumeration
->container
);
2585 g_free(enumeration
);
2589 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2591 struct bt_ctf_field_type_floating_point
*floating_point
=
2592 (struct bt_ctf_field_type_floating_point
*) type
;
2598 g_free(floating_point
);
2602 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2604 struct bt_ctf_field_type_structure
*structure
=
2605 (struct bt_ctf_field_type_structure
*) type
;
2611 g_ptr_array_free(structure
->fields
, TRUE
);
2612 g_hash_table_destroy(structure
->field_name_to_index
);
2617 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2619 struct bt_ctf_field_type_variant
*variant
=
2620 (struct bt_ctf_field_type_variant
*) type
;
2626 g_ptr_array_free(variant
->fields
, TRUE
);
2627 g_hash_table_destroy(variant
->field_name_to_index
);
2628 g_string_free(variant
->tag_name
, TRUE
);
2629 bt_put(&variant
->tag
->parent
);
2630 BT_PUT(variant
->tag_field_path
);
2635 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2637 struct bt_ctf_field_type_array
*array
=
2638 (struct bt_ctf_field_type_array
*) type
;
2644 bt_put(array
->element_type
);
2649 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2651 struct bt_ctf_field_type_sequence
*sequence
=
2652 (struct bt_ctf_field_type_sequence
*) type
;
2658 bt_put(sequence
->element_type
);
2659 g_string_free(sequence
->length_field_name
, TRUE
);
2660 BT_PUT(sequence
->length_field_path
);
2665 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2667 struct bt_ctf_field_type_string
*string
=
2668 (struct bt_ctf_field_type_string
*) type
;
2678 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2684 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2686 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2687 type
, struct bt_ctf_field_type_enumeration
, parent
);
2689 generic_field_type_freeze(type
);
2690 bt_ctf_field_type_freeze(enumeration_type
->container
);
2694 void freeze_structure_field(struct structure_field
*field
)
2696 bt_ctf_field_type_freeze(field
->type
);
2700 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2702 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2703 type
, struct bt_ctf_field_type_structure
, parent
);
2705 /* Cache the alignment */
2706 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2707 generic_field_type_freeze(type
);
2708 g_ptr_array_foreach(structure_type
->fields
,
2709 (GFunc
) freeze_structure_field
, NULL
);
2713 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2715 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2716 type
, struct bt_ctf_field_type_variant
, parent
);
2718 generic_field_type_freeze(type
);
2719 g_ptr_array_foreach(variant_type
->fields
,
2720 (GFunc
) freeze_structure_field
, NULL
);
2724 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2726 struct bt_ctf_field_type_array
*array_type
= container_of(
2727 type
, struct bt_ctf_field_type_array
, parent
);
2729 /* Cache the alignment */
2730 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2731 generic_field_type_freeze(type
);
2732 bt_ctf_field_type_freeze(array_type
->element_type
);
2736 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2738 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2739 type
, struct bt_ctf_field_type_sequence
, parent
);
2741 /* Cache the alignment */
2742 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2743 generic_field_type_freeze(type
);
2744 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2748 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2750 const char *encoding_string
;
2753 case BT_CTF_STRING_ENCODING_NONE
:
2754 encoding_string
= "none";
2756 case BT_CTF_STRING_ENCODING_ASCII
:
2757 encoding_string
= "ASCII";
2759 case BT_CTF_STRING_ENCODING_UTF8
:
2760 encoding_string
= "UTF8";
2763 encoding_string
= "unknown";
2767 return encoding_string
;
2771 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2773 const char *base_string
;
2776 case BT_CTF_INTEGER_BASE_DECIMAL
:
2777 base_string
= "decimal";
2779 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2780 base_string
= "hexadecimal";
2782 case BT_CTF_INTEGER_BASE_OCTAL
:
2783 base_string
= "octal";
2785 case BT_CTF_INTEGER_BASE_BINARY
:
2786 base_string
= "binary";
2789 base_string
= "unknown";
2797 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2798 struct metadata_context
*context
)
2800 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2801 struct bt_ctf_field_type_integer
, parent
);
2804 g_string_append_printf(context
->string
,
2805 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
2806 integer
->declaration
.len
, type
->declaration
->alignment
,
2807 (integer
->declaration
.signedness
? "true" : "false"),
2808 get_encoding_string(integer
->declaration
.encoding
),
2809 get_integer_base_string(integer
->declaration
.base
),
2810 get_byte_order_string(integer
->declaration
.byte_order
));
2811 if (integer
->mapped_clock
) {
2812 const char *clock_name
= bt_ctf_clock_get_name(
2813 integer
->mapped_clock
);
2820 g_string_append_printf(context
->string
,
2821 "; map = clock.%s.value", clock_name
);
2824 g_string_append(context
->string
, "; }");
2830 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
2831 struct metadata_context
*context
)
2835 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
2836 struct bt_ctf_field_type_enumeration
, parent
);
2837 struct bt_ctf_field_type
*container_type
;
2838 int container_signed
;
2840 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
2841 if (!container_type
) {
2846 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
2847 if (container_signed
< 0) {
2848 ret
= container_signed
;
2849 goto error_put_container_type
;
2852 g_string_append(context
->string
, "enum : ");
2853 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
2855 goto error_put_container_type
;
2858 g_string_append(context
->string
, " { ");
2859 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
2860 struct enumeration_mapping
*mapping
=
2861 enumeration
->entries
->pdata
[entry
];
2863 if (container_signed
) {
2864 if (mapping
->range_start
._signed
==
2865 mapping
->range_end
._signed
) {
2866 g_string_append_printf(context
->string
,
2867 "\"%s\" = %" PRId64
,
2868 g_quark_to_string(mapping
->string
),
2869 mapping
->range_start
._signed
);
2871 g_string_append_printf(context
->string
,
2872 "\"%s\" = %" PRId64
" ... %" PRId64
,
2873 g_quark_to_string(mapping
->string
),
2874 mapping
->range_start
._signed
,
2875 mapping
->range_end
._signed
);
2878 if (mapping
->range_start
._unsigned
==
2879 mapping
->range_end
._unsigned
) {
2880 g_string_append_printf(context
->string
,
2881 "\"%s\" = %" PRIu64
,
2882 g_quark_to_string(mapping
->string
),
2883 mapping
->range_start
._unsigned
);
2885 g_string_append_printf(context
->string
,
2886 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
2887 g_quark_to_string(mapping
->string
),
2888 mapping
->range_start
._unsigned
,
2889 mapping
->range_end
._unsigned
);
2893 g_string_append(context
->string
,
2894 ((entry
!= (enumeration
->entries
->len
- 1)) ?
2898 if (context
->field_name
->len
) {
2899 g_string_append_printf(context
->string
, " %s",
2900 context
->field_name
->str
);
2901 g_string_assign(context
->field_name
, "");
2903 error_put_container_type
:
2904 bt_put(container_type
);
2910 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
2911 struct metadata_context
*context
)
2913 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
2914 type
, struct bt_ctf_field_type_floating_point
, parent
);
2916 g_string_append_printf(context
->string
,
2917 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2918 floating_point
->declaration
.exp
->len
,
2919 floating_point
->declaration
.mantissa
->len
+ 1,
2920 get_byte_order_string(floating_point
->declaration
.byte_order
),
2921 type
->declaration
->alignment
);
2926 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
2927 struct metadata_context
*context
)
2930 unsigned int indent
;
2932 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
2933 struct bt_ctf_field_type_structure
, parent
);
2934 GString
*structure_field_name
= context
->field_name
;
2936 context
->field_name
= g_string_new("");
2938 context
->current_indentation_level
++;
2939 g_string_append(context
->string
, "struct {\n");
2941 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2942 struct structure_field
*field
;
2944 for (indent
= 0; indent
< context
->current_indentation_level
;
2946 g_string_append_c(context
->string
, '\t');
2949 field
= structure
->fields
->pdata
[i
];
2950 g_string_assign(context
->field_name
,
2951 g_quark_to_string(field
->name
));
2952 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
2957 if (context
->field_name
->len
) {
2958 g_string_append_printf(context
->string
, " %s",
2959 context
->field_name
->str
);
2961 g_string_append(context
->string
, ";\n");
2964 context
->current_indentation_level
--;
2965 for (indent
= 0; indent
< context
->current_indentation_level
;
2967 g_string_append_c(context
->string
, '\t');
2970 g_string_append_printf(context
->string
, "} align(%zu)",
2971 type
->declaration
->alignment
);
2973 g_string_free(context
->field_name
, TRUE
);
2974 context
->field_name
= structure_field_name
;
2979 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
2980 struct metadata_context
*context
)
2983 unsigned int indent
;
2985 struct bt_ctf_field_type_variant
*variant
= container_of(
2986 type
, struct bt_ctf_field_type_variant
, parent
);
2987 GString
*variant_field_name
= context
->field_name
;
2989 context
->field_name
= g_string_new("");
2990 if (variant
->tag_name
->len
> 0) {
2991 g_string_append_printf(context
->string
,
2992 "variant <%s> {\n", variant
->tag_name
->str
);
2994 g_string_append(context
->string
, "variant {\n");
2997 context
->current_indentation_level
++;
2998 for (i
= 0; i
< variant
->fields
->len
; i
++) {
2999 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3001 g_string_assign(context
->field_name
,
3002 g_quark_to_string(field
->name
));
3003 for (indent
= 0; indent
< context
->current_indentation_level
;
3005 g_string_append_c(context
->string
, '\t');
3008 g_string_assign(context
->field_name
,
3009 g_quark_to_string(field
->name
));
3010 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3015 if (context
->field_name
->len
) {
3016 g_string_append_printf(context
->string
, " %s;",
3017 context
->field_name
->str
);
3020 g_string_append_c(context
->string
, '\n');
3023 context
->current_indentation_level
--;
3024 for (indent
= 0; indent
< context
->current_indentation_level
;
3026 g_string_append_c(context
->string
, '\t');
3029 g_string_append(context
->string
, "}");
3031 g_string_free(context
->field_name
, TRUE
);
3032 context
->field_name
= variant_field_name
;
3037 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3038 struct metadata_context
*context
)
3041 struct bt_ctf_field_type_array
*array
= container_of(type
,
3042 struct bt_ctf_field_type_array
, parent
);
3044 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3049 if (context
->field_name
->len
) {
3050 g_string_append_printf(context
->string
, " %s[%u]",
3051 context
->field_name
->str
, array
->length
);
3052 g_string_assign(context
->field_name
, "");
3054 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3061 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3062 struct metadata_context
*context
)
3065 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3066 type
, struct bt_ctf_field_type_sequence
, parent
);
3068 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3073 if (context
->field_name
->len
) {
3074 g_string_append_printf(context
->string
, " %s[%s]",
3075 context
->field_name
->str
,
3076 sequence
->length_field_name
->str
);
3077 g_string_assign(context
->field_name
, "");
3079 g_string_append_printf(context
->string
, "[%s]",
3080 sequence
->length_field_name
->str
);
3087 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3088 struct metadata_context
*context
)
3090 struct bt_ctf_field_type_string
*string
= container_of(
3091 type
, struct bt_ctf_field_type_string
, parent
);
3093 g_string_append_printf(context
->string
,
3094 "string { encoding = %s; }",
3095 get_encoding_string(string
->declaration
.encoding
));
3100 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3101 enum bt_ctf_byte_order ret
;
3103 switch (byte_order
) {
3104 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3106 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3108 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3110 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3112 case BT_CTF_BYTE_ORDER_NETWORK
:
3113 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3115 case BT_CTF_BYTE_ORDER_NATIVE
:
3116 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3119 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3127 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3128 int byte_order
, int set_native
)
3130 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3131 struct bt_ctf_field_type_integer
, parent
);
3134 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3136 * User byte order is native, so we can set
3137 * the real byte order.
3139 integer_type
->declaration
.byte_order
=
3143 integer_type
->user_byte_order
=
3144 get_ctf_ir_byte_order(byte_order
);
3145 integer_type
->declaration
.byte_order
= byte_order
;
3150 void bt_ctf_field_type_enumeration_set_byte_order(
3151 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3153 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3154 struct bt_ctf_field_type_enumeration
, parent
);
3156 /* Safe to assume that container is an integer */
3157 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3158 byte_order
, set_native
);
3162 void bt_ctf_field_type_floating_point_set_byte_order(
3163 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3165 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3166 container_of(type
, struct bt_ctf_field_type_floating_point
,
3170 if (floating_point_type
->user_byte_order
==
3171 BT_CTF_BYTE_ORDER_NATIVE
) {
3173 * User byte order is native, so we can set
3174 * the real byte order.
3176 floating_point_type
->declaration
.byte_order
=
3178 floating_point_type
->sign
.byte_order
=
3180 floating_point_type
->mantissa
.byte_order
=
3182 floating_point_type
->exp
.byte_order
=
3186 floating_point_type
->user_byte_order
=
3187 get_ctf_ir_byte_order(byte_order
);
3188 floating_point_type
->declaration
.byte_order
= byte_order
;
3189 floating_point_type
->sign
.byte_order
= byte_order
;
3190 floating_point_type
->mantissa
.byte_order
= byte_order
;
3191 floating_point_type
->exp
.byte_order
= byte_order
;
3196 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3197 int byte_order
, int set_native
)
3200 struct bt_ctf_field_type_structure
*structure_type
=
3201 container_of(type
, struct bt_ctf_field_type_structure
,
3204 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3205 struct structure_field
*field
= g_ptr_array_index(
3206 structure_type
->fields
, i
);
3207 struct bt_ctf_field_type
*field_type
= field
->type
;
3209 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3210 set_byte_order_funcs
[field_type
->declaration
->id
](
3211 field_type
, byte_order
, set_native
);
3217 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3218 int byte_order
, int set_native
)
3221 struct bt_ctf_field_type_variant
*variant_type
=
3222 container_of(type
, struct bt_ctf_field_type_variant
,
3225 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3226 struct structure_field
*field
= g_ptr_array_index(
3227 variant_type
->fields
, i
);
3228 struct bt_ctf_field_type
*field_type
= field
->type
;
3230 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3231 set_byte_order_funcs
[field_type
->declaration
->id
](
3232 field_type
, byte_order
, set_native
);
3238 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3239 int byte_order
, int set_native
)
3241 struct bt_ctf_field_type_array
*array_type
=
3242 container_of(type
, struct bt_ctf_field_type_array
,
3245 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3246 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3247 array_type
->element_type
, byte_order
, set_native
);
3252 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3253 int byte_order
, int set_native
)
3255 struct bt_ctf_field_type_sequence
*sequence_type
=
3256 container_of(type
, struct bt_ctf_field_type_sequence
,
3259 if (set_byte_order_funcs
[
3260 sequence_type
->element_type
->declaration
->id
]) {
3261 set_byte_order_funcs
[
3262 sequence_type
->element_type
->declaration
->id
](
3263 sequence_type
->element_type
, byte_order
, set_native
);
3268 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3269 struct bt_ctf_field_type
*type
)
3271 struct bt_ctf_field_type
*copy
;
3272 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3274 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3275 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3280 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3282 copy_integer
->declaration
= integer
->declaration
;
3283 if (integer
->mapped_clock
) {
3284 bt_get(integer
->mapped_clock
);
3285 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3288 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3295 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3296 struct bt_ctf_field_type
*type
)
3299 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3300 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3302 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3305 /* Copy the source enumeration's container */
3306 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3307 if (!copy_container
) {
3311 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3315 copy_enumeration
= container_of(copy
,
3316 struct bt_ctf_field_type_enumeration
, parent
);
3318 /* Copy all enumaration entries */
3319 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3320 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3321 enumeration
->entries
, i
);
3322 struct enumeration_mapping
* copy_mapping
= g_new0(
3323 struct enumeration_mapping
, 1);
3325 if (!copy_mapping
) {
3329 *copy_mapping
= *mapping
;
3330 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3333 copy_enumeration
->declaration
= enumeration
->declaration
;
3335 bt_put(copy_container
);
3338 bt_put(copy_container
);
3344 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3345 struct bt_ctf_field_type
*type
)
3347 struct bt_ctf_field_type
*copy
;
3348 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3350 floating_point
= container_of(type
,
3351 struct bt_ctf_field_type_floating_point
, parent
);
3352 copy
= bt_ctf_field_type_floating_point_create();
3357 copy_float
= container_of(copy
,
3358 struct bt_ctf_field_type_floating_point
, parent
);
3359 copy_float
->declaration
= floating_point
->declaration
;
3360 copy_float
->sign
= floating_point
->sign
;
3361 copy_float
->mantissa
= floating_point
->mantissa
;
3362 copy_float
->exp
= floating_point
->exp
;
3363 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3364 copy_float
->declaration
.sign
= ©_float
->sign
;
3365 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3366 copy_float
->declaration
.exp
= ©_float
->exp
;
3372 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3373 struct bt_ctf_field_type
*type
)
3376 GHashTableIter iter
;
3377 gpointer key
, value
;
3378 struct bt_ctf_field_type
*copy
;
3379 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3381 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3383 copy
= bt_ctf_field_type_structure_create();
3388 copy_structure
= container_of(copy
,
3389 struct bt_ctf_field_type_structure
, parent
);
3391 /* Copy field_name_to_index */
3392 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3393 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3394 g_hash_table_insert(copy_structure
->field_name_to_index
,
3398 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3399 struct structure_field
*entry
, *copy_entry
;
3400 struct bt_ctf_field_type
*copy_field
;
3402 copy_entry
= g_new0(struct structure_field
, 1);
3407 entry
= g_ptr_array_index(structure
->fields
, i
);
3408 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3414 copy_entry
->name
= entry
->name
;
3415 copy_entry
->type
= copy_field
;
3416 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3419 copy_structure
->declaration
= structure
->declaration
;
3428 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3429 struct bt_ctf_field_type
*type
)
3432 GHashTableIter iter
;
3433 gpointer key
, value
;
3434 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3435 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3437 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3440 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3446 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3447 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3452 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3455 /* Copy field_name_to_index */
3456 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3457 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3458 g_hash_table_insert(copy_variant
->field_name_to_index
,
3462 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3463 struct structure_field
*entry
, *copy_entry
;
3464 struct bt_ctf_field_type
*copy_field
;
3466 copy_entry
= g_new0(struct structure_field
, 1);
3471 entry
= g_ptr_array_index(variant
->fields
, i
);
3472 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3478 copy_entry
->name
= entry
->name
;
3479 copy_entry
->type
= copy_field
;
3480 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3483 copy_variant
->declaration
= variant
->declaration
;
3484 if (variant
->tag_field_path
) {
3485 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3486 variant
->tag_field_path
);
3487 if (!copy_variant
->tag_field_path
) {
3501 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3502 struct bt_ctf_field_type
*type
)
3504 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3505 struct bt_ctf_field_type_array
*array
, *copy_array
;
3507 array
= container_of(type
, struct bt_ctf_field_type_array
,
3509 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3510 if (!copy_element
) {
3514 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3519 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3521 copy_array
->declaration
= array
->declaration
;
3523 bt_put(copy_element
);
3528 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3529 struct bt_ctf_field_type
*type
)
3531 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3532 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3534 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3536 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3537 if (!copy_element
) {
3541 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3542 sequence
->length_field_name
->len
?
3543 sequence
->length_field_name
->str
: NULL
);
3548 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3550 copy_sequence
->declaration
= sequence
->declaration
;
3551 if (sequence
->length_field_path
) {
3552 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3553 sequence
->length_field_path
);
3554 if (!copy_sequence
->length_field_path
) {
3559 bt_put(copy_element
);
3567 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3568 struct bt_ctf_field_type
*type
)
3570 struct bt_ctf_field_type
*copy
;
3571 struct bt_ctf_field_type_string
*string
, *copy_string
;
3573 copy
= bt_ctf_field_type_string_create();
3578 string
= container_of(type
, struct bt_ctf_field_type_string
,
3580 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3582 copy_string
->declaration
= string
->declaration
;
3588 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3589 struct bt_ctf_field_type
*type_b
)
3592 struct bt_ctf_field_type_integer
*integer_a
;
3593 struct bt_ctf_field_type_integer
*integer_b
;
3594 struct declaration_integer
*decl_a
;
3595 struct declaration_integer
*decl_b
;
3597 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3599 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3601 decl_a
= &integer_a
->declaration
;
3602 decl_b
= &integer_b
->declaration
;
3605 if (decl_a
->len
!= decl_b
->len
) {
3610 * Compare user byte orders only, not the cached,
3613 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3618 if (decl_a
->signedness
!= decl_b
->signedness
) {
3623 if (decl_a
->base
!= decl_b
->base
) {
3628 if (decl_a
->encoding
!= decl_b
->encoding
) {
3633 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3645 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3646 struct bt_ctf_field_type
*type_b
)
3649 struct bt_ctf_field_type_floating_point
*float_a
;
3650 struct bt_ctf_field_type_floating_point
*float_b
;
3652 float_a
= container_of(type_a
,
3653 struct bt_ctf_field_type_floating_point
, parent
);
3654 float_b
= container_of(type_b
,
3655 struct bt_ctf_field_type_floating_point
, parent
);
3658 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3662 /* Exponent length */
3663 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3667 /* Mantissa length */
3668 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3673 * Compare user byte orders only, not the cached,
3676 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3688 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3689 struct enumeration_mapping
*mapping_b
)
3694 if (mapping_a
->string
!= mapping_b
->string
) {
3699 if (mapping_a
->range_start
._unsigned
!=
3700 mapping_b
->range_start
._unsigned
) {
3705 if (mapping_a
->range_end
._unsigned
!=
3706 mapping_b
->range_end
._unsigned
) {
3718 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3719 struct bt_ctf_field_type
*type_b
)
3723 struct bt_ctf_field_type_enumeration
*enum_a
;
3724 struct bt_ctf_field_type_enumeration
*enum_b
;
3726 enum_a
= container_of(type_a
,
3727 struct bt_ctf_field_type_enumeration
, parent
);
3728 enum_b
= container_of(type_b
,
3729 struct bt_ctf_field_type_enumeration
, parent
);
3731 /* Container field type */
3732 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3740 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3744 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3745 struct enumeration_mapping
*mapping_a
=
3746 g_ptr_array_index(enum_a
->entries
, i
);
3747 struct enumeration_mapping
*mapping_b
=
3748 g_ptr_array_index(enum_b
->entries
, i
);
3750 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3763 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3764 struct bt_ctf_field_type
*type_b
)
3767 struct bt_ctf_field_type_string
*string_a
;
3768 struct bt_ctf_field_type_string
*string_b
;
3770 string_a
= container_of(type_a
,
3771 struct bt_ctf_field_type_string
, parent
);
3772 string_b
= container_of(type_b
,
3773 struct bt_ctf_field_type_string
, parent
);
3776 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
3788 int compare_structure_fields(struct structure_field
*field_a
,
3789 struct structure_field
*field_b
)
3794 if (field_a
->name
!= field_b
->name
) {
3799 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3806 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3807 struct bt_ctf_field_type
*type_b
)
3811 struct bt_ctf_field_type_structure
*struct_a
;
3812 struct bt_ctf_field_type_structure
*struct_b
;
3814 struct_a
= container_of(type_a
,
3815 struct bt_ctf_field_type_structure
, parent
);
3816 struct_b
= container_of(type_b
,
3817 struct bt_ctf_field_type_structure
, parent
);
3820 if (bt_ctf_field_type_get_alignment(type_a
) !=
3821 bt_ctf_field_type_get_alignment(type_b
)) {
3826 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3830 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3831 struct structure_field
*field_a
=
3832 g_ptr_array_index(struct_a
->fields
, i
);
3833 struct structure_field
*field_b
=
3834 g_ptr_array_index(struct_b
->fields
, i
);
3836 ret
= compare_structure_fields(field_a
, field_b
);
3850 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3851 struct bt_ctf_field_type
*type_b
)
3855 struct bt_ctf_field_type_variant
*variant_a
;
3856 struct bt_ctf_field_type_variant
*variant_b
;
3858 variant_a
= container_of(type_a
,
3859 struct bt_ctf_field_type_variant
, parent
);
3860 variant_b
= container_of(type_b
,
3861 struct bt_ctf_field_type_variant
, parent
);
3864 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3869 ret
= bt_ctf_field_type_compare(
3870 (struct bt_ctf_field_type
*) variant_a
->tag
,
3871 (struct bt_ctf_field_type
*) variant_b
->tag
);
3879 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3883 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3884 struct structure_field
*field_a
=
3885 g_ptr_array_index(variant_a
->fields
, i
);
3886 struct structure_field
*field_b
=
3887 g_ptr_array_index(variant_b
->fields
, i
);
3889 ret
= compare_structure_fields(field_a
, field_b
);
3903 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
3904 struct bt_ctf_field_type
*type_b
)
3907 struct bt_ctf_field_type_array
*array_a
;
3908 struct bt_ctf_field_type_array
*array_b
;
3910 array_a
= container_of(type_a
,
3911 struct bt_ctf_field_type_array
, parent
);
3912 array_b
= container_of(type_b
,
3913 struct bt_ctf_field_type_array
, parent
);
3916 if (array_a
->length
!= array_b
->length
) {
3921 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
3922 array_b
->element_type
);
3929 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
3930 struct bt_ctf_field_type
*type_b
)
3933 struct bt_ctf_field_type_sequence
*sequence_a
;
3934 struct bt_ctf_field_type_sequence
*sequence_b
;
3936 sequence_a
= container_of(type_a
,
3937 struct bt_ctf_field_type_sequence
, parent
);
3938 sequence_b
= container_of(type_b
,
3939 struct bt_ctf_field_type_sequence
, parent
);
3942 if (strcmp(sequence_a
->length_field_name
->str
,
3943 sequence_b
->length_field_name
->str
)) {
3948 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
3949 sequence_b
->element_type
);
3955 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
3956 struct bt_ctf_field_type
*type_b
)
3960 if (type_a
== type_b
) {
3961 /* Same reference: equal (even if both are NULL) */
3966 if (!type_a
|| !type_b
) {
3971 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
3972 /* Different type IDs */
3976 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
3977 /* Both have unknown type IDs */
3981 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
3988 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
3990 int field_count
= -1;
3991 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
3994 case CTF_TYPE_STRUCT
:
3996 bt_ctf_field_type_structure_get_field_count(field_type
);
3998 case CTF_TYPE_VARIANT
:
4000 bt_ctf_field_type_variant_get_field_count(field_type
);
4002 case CTF_TYPE_ARRAY
:
4003 case CTF_TYPE_SEQUENCE
:
4005 * Array and sequence types always contain a single member
4006 * (the element type).
4018 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4019 struct bt_ctf_field_type
*field_type
, int index
)
4021 struct bt_ctf_field_type
*field
= NULL
;
4022 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4025 case CTF_TYPE_STRUCT
:
4026 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4029 case CTF_TYPE_VARIANT
:
4030 bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4033 case CTF_TYPE_ARRAY
:
4034 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4036 case CTF_TYPE_SEQUENCE
:
4037 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4047 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4050 int field_index
= -1;
4051 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4054 case CTF_TYPE_STRUCT
:
4055 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4058 case CTF_TYPE_VARIANT
:
4059 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4069 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4070 struct bt_ctf_field_type
*type
)
4072 struct bt_ctf_field_path
*field_path
= NULL
;
4073 struct bt_ctf_field_type_variant
*variant
;
4075 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4079 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4081 field_path
= bt_get(variant
->tag_field_path
);
4086 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4087 struct bt_ctf_field_type
*type
)
4089 struct bt_ctf_field_path
*field_path
= NULL
;
4090 struct bt_ctf_field_type_sequence
*sequence
;
4092 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4096 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4098 field_path
= bt_get(sequence
->length_field_path
);