4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/clock-class.h>
34 #include <babeltrace/ctf-ir/clock-class-internal.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/endian-internal.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
81 bt_ctf_field_type_enumeration_destroy
,
82 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
83 bt_ctf_field_type_floating_point_destroy
,
84 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
104 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
107 type_freeze_func
const type_freeze_funcs
[] = {
108 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
109 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
110 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
111 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
112 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
113 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
114 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
115 [BT_CTF_FIELD_TYPE_ID_STRING
] = generic_field_type_freeze
,
119 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_floating_point_serialize(
126 struct bt_ctf_field_type
*, struct metadata_context
*);
128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
144 type_serialize_func
const type_serialize_funcs
[] = {
145 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
146 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
147 bt_ctf_field_type_enumeration_serialize
,
148 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
149 bt_ctf_field_type_floating_point_serialize
,
150 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
151 bt_ctf_field_type_structure_serialize
,
152 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
153 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
154 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
155 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
159 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
160 enum bt_ctf_byte_order byte_order
);
162 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
163 enum bt_ctf_byte_order byte_order
);
165 void bt_ctf_field_type_floating_point_set_byte_order(
166 struct bt_ctf_field_type
*, enum bt_ctf_byte_order byte_order
);
168 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
169 enum bt_ctf_byte_order byte_order
);
171 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
172 enum bt_ctf_byte_order byte_order
);
174 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
175 enum bt_ctf_byte_order byte_order
);
177 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
178 enum bt_ctf_byte_order byte_order
);
181 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
182 enum bt_ctf_byte_order
) = {
183 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
184 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
185 bt_ctf_field_type_enumeration_set_byte_order
,
186 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
187 bt_ctf_field_type_floating_point_set_byte_order
,
188 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
189 bt_ctf_field_type_structure_set_byte_order
,
190 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
191 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
192 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
193 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
197 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
198 struct bt_ctf_field_type
*);
200 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
201 struct bt_ctf_field_type
*);
203 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
204 struct bt_ctf_field_type
*);
206 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
207 struct bt_ctf_field_type
*);
209 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
210 struct bt_ctf_field_type
*);
212 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
213 struct bt_ctf_field_type
*);
215 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
216 struct bt_ctf_field_type
*);
218 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
219 struct bt_ctf_field_type
*);
222 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
223 struct bt_ctf_field_type
*) = {
224 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
225 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
226 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
227 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
228 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
229 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
230 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
231 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
235 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
236 struct bt_ctf_field_type
*);
238 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
239 struct bt_ctf_field_type
*);
241 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
242 struct bt_ctf_field_type
*);
244 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
245 struct bt_ctf_field_type
*);
247 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
248 struct bt_ctf_field_type
*);
250 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
251 struct bt_ctf_field_type
*);
253 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
254 struct bt_ctf_field_type
*);
256 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
257 struct bt_ctf_field_type
*);
260 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
261 struct bt_ctf_field_type
*) = {
262 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
263 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
264 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
265 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
266 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
267 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
268 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
269 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
273 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
275 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
277 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
279 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
281 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
283 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
286 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
287 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
288 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = NULL
,
289 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
290 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
291 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
292 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
293 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
294 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
298 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
304 void destroy_structure_field(struct structure_field
*field
)
311 void check_ranges_overlap(gpointer element
, gpointer query
)
313 struct enumeration_mapping
*mapping
= element
;
314 struct range_overlap_query
*overlap_query
= query
;
316 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
317 && overlap_query
->range_start
._signed
<=
318 mapping
->range_end
._signed
) {
319 overlap_query
->overlaps
= 1;
320 overlap_query
->mapping_name
= mapping
->string
;
323 overlap_query
->overlaps
|=
324 mapping
->string
== overlap_query
->mapping_name
;
328 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
330 struct enumeration_mapping
*mapping
= element
;
331 struct range_overlap_query
*overlap_query
= query
;
333 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
334 && overlap_query
->range_start
._unsigned
<=
335 mapping
->range_end
._unsigned
) {
336 overlap_query
->overlaps
= 1;
337 overlap_query
->mapping_name
= mapping
->string
;
340 overlap_query
->overlaps
|=
341 mapping
->string
== overlap_query
->mapping_name
;
345 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
346 struct enumeration_mapping
**b
)
348 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
352 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
353 struct enumeration_mapping
**b
)
355 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
359 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, bool init_bo
)
361 assert(type
&& (type
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
) &&
362 (type
->id
< BT_CTF_NR_TYPE_IDS
));
364 bt_object_init(type
, bt_ctf_field_type_destroy
);
365 type
->freeze
= type_freeze_funcs
[type
->id
];
366 type
->serialize
= type_serialize_funcs
[type
->id
];
369 int ret
= bt_ctf_field_type_set_byte_order(type
,
370 BT_CTF_BYTE_ORDER_NATIVE
);
378 int add_structure_field(GPtrArray
*fields
,
379 GHashTable
*field_name_to_index
,
380 struct bt_ctf_field_type
*field_type
,
381 const char *field_name
)
384 GQuark name_quark
= g_quark_from_string(field_name
);
385 struct structure_field
*field
;
387 /* Make sure structure does not contain a field of the same name */
388 if (g_hash_table_lookup_extended(field_name_to_index
,
389 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
394 field
= g_new0(struct structure_field
, 1);
401 field
->name
= name_quark
;
402 field
->type
= field_type
;
403 g_hash_table_insert(field_name_to_index
,
404 GUINT_TO_POINTER(name_quark
),
405 GUINT_TO_POINTER(fields
->len
));
406 g_ptr_array_add(fields
, field
);
412 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
414 struct bt_ctf_field_type
*type
;
415 enum bt_ctf_field_type_id type_id
;
417 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
419 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
420 type_id
>= BT_CTF_NR_TYPE_IDS
) {
424 type_destroy_funcs
[type_id
](type
);
428 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
432 struct bt_ctf_field_type_integer
*integer
=
433 container_of(type
, struct bt_ctf_field_type_integer
,
436 if (integer
->mapped_clock
&& integer
->is_signed
) {
446 struct enumeration_mapping
*get_enumeration_mapping(
447 struct bt_ctf_field_type
*type
, int index
)
449 struct enumeration_mapping
*mapping
= NULL
;
450 struct bt_ctf_field_type_enumeration
*enumeration
;
452 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
454 if (index
>= enumeration
->entries
->len
) {
458 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
464 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
465 * Only used when freezing an enumeration.
468 void set_enumeration_range_overlap(
469 struct bt_ctf_field_type
*type
)
472 struct bt_ctf_field_type
*container_type
;
473 struct bt_ctf_field_type_enumeration
*enumeration_type
;
476 enumeration_type
= container_of(type
,
477 struct bt_ctf_field_type_enumeration
, parent
);
479 len
= enumeration_type
->entries
->len
;
480 container_type
= enumeration_type
->container
;
481 is_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
483 for (i
= 0; i
< len
; i
++) {
484 for (j
= i
+ 1; j
< len
; j
++) {
485 struct enumeration_mapping
*mapping
[2];
487 mapping
[0] = get_enumeration_mapping(type
, i
);
488 mapping
[1] = get_enumeration_mapping(type
, j
);
490 if (mapping
[0]->range_start
._signed
491 <= mapping
[1]->range_end
._signed
492 && mapping
[0]->range_end
._signed
493 >= mapping
[1]->range_start
._signed
) {
494 enumeration_type
->has_overlapping_ranges
= true;
498 if (mapping
[0]->range_start
._unsigned
499 <= mapping
[1]->range_end
._unsigned
500 && mapping
[0]->range_end
._unsigned
501 >= mapping
[1]->range_start
._unsigned
) {
502 enumeration_type
->has_overlapping_ranges
= true;
511 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
515 struct bt_ctf_field_type_enumeration
*enumeration
=
516 container_of(type
, struct bt_ctf_field_type_enumeration
,
518 struct bt_ctf_field_type
*container_type
=
519 bt_ctf_field_type_enumeration_get_container_type(type
);
521 if (!container_type
) {
526 ret
= bt_ctf_field_type_validate(container_type
);
531 /* Ensure enum has entries */
532 ret
= enumeration
->entries
->len
? 0 : -1;
535 BT_PUT(container_type
);
540 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
543 struct bt_ctf_field_type
*element_type
= NULL
;
544 struct bt_ctf_field_type_sequence
*sequence
=
545 container_of(type
, struct bt_ctf_field_type_sequence
,
548 /* Length field name should be set at this point */
549 if (sequence
->length_field_name
->len
== 0) {
554 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
560 ret
= bt_ctf_field_type_validate(element_type
);
563 BT_PUT(element_type
);
569 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
572 struct bt_ctf_field_type
*element_type
= NULL
;
574 element_type
= bt_ctf_field_type_array_get_element_type(type
);
580 ret
= bt_ctf_field_type_validate(element_type
);
583 BT_PUT(element_type
);
589 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
592 struct bt_ctf_field_type
*child_type
= NULL
;
593 int64_t field_count
= bt_ctf_field_type_structure_get_field_count(type
);
596 if (field_count
< 0) {
601 for (i
= 0; i
< field_count
; ++i
) {
602 ret
= bt_ctf_field_type_structure_get_field_by_index(type
,
603 NULL
, &child_type
, i
);
608 ret
= bt_ctf_field_type_validate(child_type
);
623 bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
624 struct bt_ctf_field_type_enumeration
*enumeration_type
)
626 if (!enumeration_type
->parent
.frozen
) {
627 set_enumeration_range_overlap(&enumeration_type
->parent
);
629 return enumeration_type
->has_overlapping_ranges
;
633 int bt_ctf_field_type_enumeration_get_mapping_name(
634 struct bt_ctf_field_type
*enum_field_type
,
636 const char **mapping_name
)
639 struct enumeration_mapping
*mapping
;
641 if (!enum_field_type
|| index
< 0) {
646 mapping
= get_enumeration_mapping(enum_field_type
, index
);
653 *mapping_name
= g_quark_to_string(mapping
->string
);
660 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
664 struct bt_ctf_field_type
*child_type
= NULL
;
665 struct bt_ctf_field_type_variant
*variant
=
666 container_of(type
, struct bt_ctf_field_type_variant
,
669 int64_t tag_mappings_count
;
671 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
676 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
683 bt_ctf_field_type_enumeration_get_mapping_count(
684 (struct bt_ctf_field_type
*) variant
->tag
);
687 * Validate that each mapping found in the tag has a name which
688 * is also the name of a field in this variant field type.
690 * The opposite is accepted: variant FT fields which cannot be
691 * selected because the variant FT tag has no mapping named as
692 * such. This scenario, while not ideal, cannot cause any error.
694 for (i
= 0; i
< tag_mappings_count
; ++i
) {
696 struct bt_ctf_field_type
*ft
;
698 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
699 (struct bt_ctf_field_type
*) variant
->tag
,
709 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
719 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
720 if (field_count
< 0) {
725 for (i
= 0; i
< field_count
; ++i
) {
726 ret
= bt_ctf_field_type_variant_get_field_by_index(type
,
727 NULL
, &child_type
, i
);
732 ret
= bt_ctf_field_type_validate(child_type
);
747 * This function validates a given field type without considering
748 * where this field type is located. It only validates the properties
749 * of the given field type and the properties of its children if
753 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
756 enum bt_ctf_field_type_id id
= bt_ctf_field_type_get_type_id(type
);
764 /* Already marked as valid */
768 if (type_validate_funcs
[id
]) {
769 ret
= type_validate_funcs
[id
](type
);
772 if (!ret
&& type
->frozen
) {
773 /* Field type is valid */
781 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
783 struct bt_ctf_field_type_integer
*integer
=
784 g_new0(struct bt_ctf_field_type_integer
, 1);
786 if (!integer
|| size
== 0 || size
> 64) {
790 integer
->parent
.id
= BT_CTF_FIELD_TYPE_ID_INTEGER
;
791 integer
->size
= size
;
792 integer
->base
= BT_CTF_INTEGER_BASE_DECIMAL
;
793 integer
->encoding
= BT_CTF_STRING_ENCODING_NONE
;
794 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
795 return &integer
->parent
;
798 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
801 struct bt_ctf_field_type_integer
*integer
;
803 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
808 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
809 ret
= (int) integer
->size
;
814 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
817 struct bt_ctf_field_type_integer
*integer
;
819 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
824 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
825 ret
= integer
->is_signed
;
830 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
834 struct bt_ctf_field_type_integer
*integer
;
836 if (!type
|| type
->frozen
||
837 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
842 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
843 integer
->is_signed
= !!is_signed
;
848 int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type
*type
,
852 struct bt_ctf_field_type_integer
*integer
;
854 if (!type
|| type
->frozen
||
855 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
856 !size
|| size
> 64) {
861 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
862 integer
->size
= size
;
867 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
868 struct bt_ctf_field_type
*type
)
870 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
871 struct bt_ctf_field_type_integer
*integer
;
873 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
877 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
883 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
884 enum bt_ctf_integer_base base
)
888 if (!type
|| type
->frozen
||
889 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
895 case BT_CTF_INTEGER_BASE_BINARY
:
896 case BT_CTF_INTEGER_BASE_OCTAL
:
897 case BT_CTF_INTEGER_BASE_DECIMAL
:
898 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
900 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
901 struct bt_ctf_field_type_integer
, parent
);
902 integer
->base
= base
;
912 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
913 struct bt_ctf_field_type
*type
)
915 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
916 struct bt_ctf_field_type_integer
*integer
;
918 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
922 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
923 ret
= integer
->encoding
;
928 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
929 enum bt_ctf_string_encoding encoding
)
932 struct bt_ctf_field_type_integer
*integer
;
934 if (!type
|| type
->frozen
||
935 (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) ||
936 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
937 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
942 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
943 integer
->encoding
= encoding
;
948 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
949 struct bt_ctf_field_type
*type
)
951 struct bt_ctf_field_type_integer
*integer
;
952 struct bt_ctf_clock_class
*clock_class
= NULL
;
958 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
959 clock_class
= integer
->mapped_clock
;
965 int bt_ctf_field_type_integer_set_mapped_clock_class(
966 struct bt_ctf_field_type
*type
,
967 struct bt_ctf_clock_class
*clock_class
)
969 struct bt_ctf_field_type_integer
*integer
;
972 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
977 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
978 bt_put(integer
->mapped_clock
);
979 integer
->mapped_clock
= bt_get(clock_class
);
985 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
987 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
989 struct bt_ctf_field_type_enumeration_mapping_iterator
,
992 bt_put(&iter
->enumeration_type
->parent
);
997 struct bt_ctf_field_type_enumeration_mapping_iterator
*
998 bt_ctf_field_type_enumeration_find_mappings_type(
999 struct bt_ctf_field_type
*type
,
1000 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
1002 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1003 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1005 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1009 enumeration_type
= container_of(type
,
1010 struct bt_ctf_field_type_enumeration
, parent
);
1011 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
1016 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
1018 iter
->enumeration_type
= enumeration_type
;
1020 iter
->type
= iterator_type
;
1025 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1026 bt_ctf_field_type_enumeration_find_mappings_by_name(
1027 struct bt_ctf_field_type
*type
, const char *name
)
1029 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1031 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1032 type
, ITERATOR_BY_NAME
);
1037 iter
->u
.name_quark
= g_quark_try_string(name
);
1038 if (!iter
->u
.name_quark
) {
1042 /* Advance iterator to first entry, or leave index at -1. */
1043 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1044 /* No entry found. */
1054 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1055 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1057 struct bt_ctf_field_type_enumeration
*enumeration
;
1058 struct bt_ctf_field_type
*type
;
1059 int i
, ret
= 0, len
;
1061 enumeration
= iter
->enumeration_type
;
1062 type
= &enumeration
->parent
;
1063 len
= enumeration
->entries
->len
;
1064 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1065 struct enumeration_mapping
*mapping
=
1066 get_enumeration_mapping(type
, i
);
1068 switch (iter
->type
) {
1069 case ITERATOR_BY_NAME
:
1070 if (mapping
->string
== iter
->u
.name_quark
) {
1075 case ITERATOR_BY_SIGNED_VALUE
:
1077 int64_t value
= iter
->u
.signed_value
;
1079 if (value
>= mapping
->range_start
._signed
&&
1080 value
<= mapping
->range_end
._signed
) {
1086 case ITERATOR_BY_UNSIGNED_VALUE
:
1088 uint64_t value
= iter
->u
.unsigned_value
;
1090 if (value
>= mapping
->range_start
._unsigned
&&
1091 value
<= mapping
->range_end
._unsigned
) {
1107 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1108 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1109 struct bt_ctf_field_type
*type
, int64_t value
)
1111 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1113 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1114 type
, ITERATOR_BY_SIGNED_VALUE
);
1119 if (bt_ctf_field_type_integer_get_signed(
1120 iter
->enumeration_type
->container
) != 1) {
1123 iter
->u
.signed_value
= value
;
1125 /* Advance iterator to first entry, or leave index at -1. */
1126 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1127 /* No entry found. */
1137 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1138 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1139 struct bt_ctf_field_type
*type
, uint64_t value
)
1141 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1143 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1144 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1149 if (bt_ctf_field_type_integer_get_signed(
1150 iter
->enumeration_type
->container
) != 0) {
1153 iter
->u
.unsigned_value
= value
;
1155 /* Advance iterator to first entry, or leave index at -1. */
1156 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1157 /* No entry found. */
1167 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1168 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1169 const char **mapping_name
, int64_t *range_begin
,
1179 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1180 &iter
->enumeration_type
->parent
, iter
->index
,
1181 mapping_name
, range_begin
, range_end
);
1186 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1187 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1188 const char **mapping_name
, uint64_t *range_begin
,
1189 uint64_t *range_end
)
1198 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1199 &iter
->enumeration_type
->parent
, iter
->index
,
1200 mapping_name
, range_begin
, range_end
);
1205 int bt_ctf_field_type_enumeration_get_mapping_signed(
1206 struct bt_ctf_field_type
*enum_field_type
,
1207 uint64_t index
, const char **mapping_name
, int64_t *range_begin
,
1211 struct enumeration_mapping
*mapping
;
1213 if (!enum_field_type
) {
1218 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1225 *mapping_name
= g_quark_to_string(mapping
->string
);
1229 *range_begin
= mapping
->range_start
._signed
;
1233 *range_end
= mapping
->range_end
._signed
;
1239 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1240 struct bt_ctf_field_type
*enum_field_type
,
1242 const char **mapping_name
, uint64_t *range_begin
,
1243 uint64_t *range_end
)
1246 struct enumeration_mapping
*mapping
;
1248 if (!enum_field_type
) {
1253 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1260 *mapping_name
= g_quark_to_string(mapping
->string
);
1264 *range_begin
= mapping
->range_start
._unsigned
;
1268 *range_end
= mapping
->range_end
._unsigned
;
1274 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1275 struct bt_ctf_field_type
*integer_container_type
)
1277 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1279 if (!integer_container_type
) {
1283 if (integer_container_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1287 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1292 enumeration
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ENUM
;
1293 bt_get(integer_container_type
);
1294 enumeration
->container
= integer_container_type
;
1295 enumeration
->entries
= g_ptr_array_new_with_free_func(
1296 (GDestroyNotify
)destroy_enumeration_mapping
);
1297 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1298 return &enumeration
->parent
;
1300 g_free(enumeration
);
1304 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1305 struct bt_ctf_field_type
*type
)
1307 struct bt_ctf_field_type
*container_type
= NULL
;
1308 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1314 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1318 enumeration_type
= container_of(type
,
1319 struct bt_ctf_field_type_enumeration
, parent
);
1320 container_type
= enumeration_type
->container
;
1321 bt_get(container_type
);
1323 return container_type
;
1326 int bt_ctf_field_type_enumeration_add_mapping(
1327 struct bt_ctf_field_type
*type
, const char *string
,
1328 int64_t range_start
, int64_t range_end
)
1331 GQuark mapping_name
;
1332 struct enumeration_mapping
*mapping
;
1333 struct bt_ctf_field_type_enumeration
*enumeration
;
1334 char *escaped_string
;
1336 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1338 (range_end
< range_start
)) {
1343 if (!string
|| strlen(string
) == 0) {
1348 escaped_string
= g_strescape(string
, NULL
);
1349 if (!escaped_string
) {
1354 mapping
= g_new(struct enumeration_mapping
, 1);
1359 mapping_name
= g_quark_from_string(escaped_string
);
1360 *mapping
= (struct enumeration_mapping
) {
1361 .range_start
._signed
= range_start
,
1362 .range_end
._signed
= range_end
,
1363 .string
= mapping_name
,
1365 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1367 g_ptr_array_add(enumeration
->entries
, mapping
);
1368 g_ptr_array_sort(enumeration
->entries
,
1369 (GCompareFunc
)compare_enumeration_mappings_signed
);
1371 free(escaped_string
);
1376 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1377 struct bt_ctf_field_type
*type
, const char *string
,
1378 uint64_t range_start
, uint64_t range_end
)
1381 GQuark mapping_name
;
1382 struct enumeration_mapping
*mapping
;
1383 struct bt_ctf_field_type_enumeration
*enumeration
;
1384 char *escaped_string
;
1386 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1388 (range_end
< range_start
)) {
1393 if (!string
|| strlen(string
) == 0) {
1398 escaped_string
= g_strescape(string
, NULL
);
1399 if (!escaped_string
) {
1404 mapping
= g_new(struct enumeration_mapping
, 1);
1409 mapping_name
= g_quark_from_string(escaped_string
);
1410 *mapping
= (struct enumeration_mapping
) {
1411 .range_start
._unsigned
= range_start
,
1412 .range_end
._unsigned
= range_end
,
1413 .string
= mapping_name
,
1415 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1417 g_ptr_array_add(enumeration
->entries
, mapping
);
1418 g_ptr_array_sort(enumeration
->entries
,
1419 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1421 free(escaped_string
);
1426 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
1427 struct bt_ctf_field_type
*type
)
1430 struct bt_ctf_field_type_enumeration
*enumeration
;
1432 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1437 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1439 ret
= (int64_t) enumeration
->entries
->len
;
1444 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1446 struct bt_ctf_field_type_floating_point
*floating_point
=
1447 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1449 if (!floating_point
) {
1453 floating_point
->parent
.id
= BT_CTF_FIELD_TYPE_ID_FLOAT
;
1454 floating_point
->exp_dig
= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1455 floating_point
->mant_dig
= FLT_MANT_DIG
;
1456 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1458 return floating_point
? &floating_point
->parent
: NULL
;
1461 int bt_ctf_field_type_floating_point_get_exponent_digits(
1462 struct bt_ctf_field_type
*type
)
1465 struct bt_ctf_field_type_floating_point
*floating_point
;
1467 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1472 floating_point
= container_of(type
,
1473 struct bt_ctf_field_type_floating_point
, parent
);
1474 ret
= (int) floating_point
->exp_dig
;
1479 int bt_ctf_field_type_floating_point_set_exponent_digits(
1480 struct bt_ctf_field_type
*type
,
1481 unsigned int exponent_digits
)
1484 struct bt_ctf_field_type_floating_point
*floating_point
;
1486 if (!type
|| type
->frozen
||
1487 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1492 floating_point
= container_of(type
,
1493 struct bt_ctf_field_type_floating_point
, parent
);
1494 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1495 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1497 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1502 floating_point
->exp_dig
= exponent_digits
;
1507 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1508 struct bt_ctf_field_type
*type
)
1511 struct bt_ctf_field_type_floating_point
*floating_point
;
1513 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1518 floating_point
= container_of(type
,
1519 struct bt_ctf_field_type_floating_point
, parent
);
1520 ret
= (int) floating_point
->mant_dig
;
1525 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1526 struct bt_ctf_field_type
*type
,
1527 unsigned int mantissa_digits
)
1530 struct bt_ctf_field_type_floating_point
*floating_point
;
1532 if (!type
|| type
->frozen
||
1533 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1538 floating_point
= container_of(type
,
1539 struct bt_ctf_field_type_floating_point
, parent
);
1541 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1542 (mantissa_digits
!= DBL_MANT_DIG
) &&
1543 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1548 floating_point
->mant_dig
= mantissa_digits
;
1553 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1555 struct bt_ctf_field_type_structure
*structure
=
1556 g_new0(struct bt_ctf_field_type_structure
, 1);
1562 structure
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRUCT
;
1563 structure
->fields
= g_ptr_array_new_with_free_func(
1564 (GDestroyNotify
)destroy_structure_field
);
1565 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1566 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1567 return &structure
->parent
;
1572 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1573 struct bt_ctf_field_type
*field_type
,
1574 const char *field_name
)
1577 struct bt_ctf_field_type_structure
*structure
;
1580 * TODO: check that `field_type` does not contain `type`,
1583 if (!type
|| !field_type
|| type
->frozen
||
1584 bt_ctf_validate_identifier(field_name
) ||
1585 (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) ||
1586 type
== field_type
) {
1591 structure
= container_of(type
,
1592 struct bt_ctf_field_type_structure
, parent
);
1593 if (add_structure_field(structure
->fields
,
1594 structure
->field_name_to_index
, field_type
, field_name
)) {
1602 int64_t bt_ctf_field_type_structure_get_field_count(
1603 struct bt_ctf_field_type
*type
)
1606 struct bt_ctf_field_type_structure
*structure
;
1608 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
)) {
1613 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1615 ret
= (int64_t) structure
->fields
->len
;
1620 int bt_ctf_field_type_structure_get_field_by_index(
1621 struct bt_ctf_field_type
*type
,
1622 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1625 struct bt_ctf_field_type_structure
*structure
;
1626 struct structure_field
*field
;
1629 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1634 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1636 if (index
>= structure
->fields
->len
) {
1641 field
= g_ptr_array_index(structure
->fields
, index
);
1643 *field_type
= field
->type
;
1644 bt_get(field
->type
);
1647 *field_name
= g_quark_to_string(field
->name
);
1653 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1654 struct bt_ctf_field_type
*type
,
1659 struct structure_field
*field
;
1660 struct bt_ctf_field_type_structure
*structure
;
1661 struct bt_ctf_field_type
*field_type
= NULL
;
1663 if (!type
|| !name
) {
1667 name_quark
= g_quark_try_string(name
);
1672 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1674 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1675 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1679 field
= structure
->fields
->pdata
[index
];
1680 field_type
= field
->type
;
1686 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1687 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1689 struct bt_ctf_field_type_variant
*variant
= NULL
;
1691 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1695 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1700 variant
->parent
.id
= BT_CTF_FIELD_TYPE_ID_VARIANT
;
1701 variant
->tag_name
= g_string_new(tag_name
);
1702 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1703 variant
->fields
= g_ptr_array_new_with_free_func(
1704 (GDestroyNotify
) destroy_structure_field
);
1707 variant
->tag
= container_of(enum_tag
,
1708 struct bt_ctf_field_type_enumeration
, parent
);
1711 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1712 /* A variant's alignment is undefined */
1713 variant
->parent
.alignment
= 0;
1714 return &variant
->parent
;
1719 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1720 struct bt_ctf_field_type
*type
)
1722 struct bt_ctf_field_type_variant
*variant
;
1723 struct bt_ctf_field_type
*tag_type
= NULL
;
1725 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1729 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1730 if (!variant
->tag
) {
1734 tag_type
= &variant
->tag
->parent
;
1740 const char *bt_ctf_field_type_variant_get_tag_name(
1741 struct bt_ctf_field_type
*type
)
1743 struct bt_ctf_field_type_variant
*variant
;
1744 const char *tag_name
= NULL
;
1746 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1750 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1751 if (variant
->tag_name
->len
== 0) {
1755 tag_name
= variant
->tag_name
->str
;
1760 int bt_ctf_field_type_variant_set_tag_name(
1761 struct bt_ctf_field_type
*type
, const char *name
)
1764 struct bt_ctf_field_type_variant
*variant
;
1766 if (!type
|| type
->frozen
||
1767 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1768 bt_ctf_validate_identifier(name
)) {
1773 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1774 g_string_assign(variant
->tag_name
, name
);
1779 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1780 struct bt_ctf_field_type
*field_type
,
1781 const char *field_name
)
1785 struct bt_ctf_field_type_variant
*variant
;
1786 GQuark field_name_quark
= g_quark_from_string(field_name
);
1789 * TODO: check that `field_type` does not contain `type`,
1792 if (!type
|| !field_type
|| type
->frozen
||
1793 bt_ctf_validate_identifier(field_name
) ||
1794 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1795 type
== field_type
) {
1800 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1802 /* The user has explicitly provided a tag; validate against it. */
1806 /* Make sure this name is present in the enum tag */
1807 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1808 struct enumeration_mapping
*mapping
=
1809 g_ptr_array_index(variant
->tag
->entries
, i
);
1811 if (mapping
->string
== field_name_quark
) {
1818 /* Validation failed */
1824 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1825 field_type
, field_name
)) {
1833 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1834 struct bt_ctf_field_type
*type
,
1835 const char *field_name
)
1839 struct structure_field
*field
;
1840 struct bt_ctf_field_type_variant
*variant
;
1841 struct bt_ctf_field_type
*field_type
= NULL
;
1843 if (!type
|| !field_name
) {
1847 name_quark
= g_quark_try_string(field_name
);
1852 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1853 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1854 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1858 field
= g_ptr_array_index(variant
->fields
, index
);
1859 field_type
= field
->type
;
1865 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1866 struct bt_ctf_field_type
*type
,
1867 struct bt_ctf_field
*tag
)
1870 const char *enum_value
;
1871 struct bt_ctf_field_type
*field_type
= NULL
;
1872 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1874 if (!type
|| !tag
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1878 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1883 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
,
1884 &enum_value
, NULL
, NULL
);
1889 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1896 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1899 struct bt_ctf_field_type_variant
*variant
;
1901 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1906 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1908 ret
= (int64_t) variant
->fields
->len
;
1914 int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type
*type
,
1915 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1918 struct bt_ctf_field_type_variant
*variant
;
1919 struct structure_field
*field
;
1922 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1927 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1929 if (index
>= variant
->fields
->len
) {
1934 field
= g_ptr_array_index(variant
->fields
, index
);
1936 *field_type
= field
->type
;
1937 bt_get(field
->type
);
1940 *field_name
= g_quark_to_string(field
->name
);
1946 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1947 struct bt_ctf_field_type
*element_type
,
1948 unsigned int length
)
1950 struct bt_ctf_field_type_array
*array
= NULL
;
1952 if (!element_type
|| length
== 0) {
1956 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1961 array
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ARRAY
;
1962 bt_get(element_type
);
1963 array
->element_type
= element_type
;
1964 array
->length
= length
;
1965 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1966 return &array
->parent
;
1971 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1972 struct bt_ctf_field_type
*type
)
1974 struct bt_ctf_field_type
*ret
= NULL
;
1975 struct bt_ctf_field_type_array
*array
;
1977 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
1981 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1982 ret
= array
->element_type
;
1989 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1990 struct bt_ctf_field_type
*element_type
)
1993 struct bt_ctf_field_type_array
*array
;
1995 if (!type
|| !element_type
||
1996 (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
2001 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2003 if (array
->element_type
) {
2004 BT_PUT(array
->element_type
);
2007 array
->element_type
= element_type
;
2008 bt_get(array
->element_type
);
2014 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2017 struct bt_ctf_field_type_array
*array
;
2019 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
2024 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2025 ret
= (int64_t) array
->length
;
2030 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2031 struct bt_ctf_field_type
*element_type
,
2032 const char *length_field_name
)
2034 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2036 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2040 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2045 sequence
->parent
.id
= BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2046 bt_get(element_type
);
2047 sequence
->element_type
= element_type
;
2048 sequence
->length_field_name
= g_string_new(length_field_name
);
2049 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2050 return &sequence
->parent
;
2055 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2056 struct bt_ctf_field_type
*type
)
2058 struct bt_ctf_field_type
*ret
= NULL
;
2059 struct bt_ctf_field_type_sequence
*sequence
;
2061 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2065 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2067 ret
= sequence
->element_type
;
2074 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2075 struct bt_ctf_field_type
*element_type
)
2078 struct bt_ctf_field_type_sequence
*sequence
;
2080 if (!type
|| !element_type
||
2081 (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2086 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2088 if (sequence
->element_type
) {
2089 BT_PUT(sequence
->element_type
);
2092 sequence
->element_type
= element_type
;
2093 bt_get(sequence
->element_type
);
2099 const char *bt_ctf_field_type_sequence_get_length_field_name(
2100 struct bt_ctf_field_type
*type
)
2102 const char *ret
= NULL
;
2103 struct bt_ctf_field_type_sequence
*sequence
;
2105 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2109 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2111 ret
= sequence
->length_field_name
->str
;
2116 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2118 struct bt_ctf_field_type_string
*string
=
2119 g_new0(struct bt_ctf_field_type_string
, 1);
2125 string
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRING
;
2126 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2127 string
->encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2128 string
->parent
.alignment
= CHAR_BIT
;
2129 return &string
->parent
;
2132 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2133 struct bt_ctf_field_type
*type
)
2135 struct bt_ctf_field_type_string
*string
;
2136 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2138 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
)) {
2142 string
= container_of(type
, struct bt_ctf_field_type_string
,
2144 ret
= string
->encoding
;
2149 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2150 enum bt_ctf_string_encoding encoding
)
2153 struct bt_ctf_field_type_string
*string
;
2155 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
||
2156 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2157 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2162 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2163 string
->encoding
= encoding
;
2168 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2171 enum bt_ctf_field_type_id type_id
;
2179 ret
= (int) type
->alignment
;
2183 type_id
= bt_ctf_field_type_get_type_id(type
);
2185 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
2187 struct bt_ctf_field_type
*element
=
2188 bt_ctf_field_type_sequence_get_element_type(type
);
2195 ret
= bt_ctf_field_type_get_alignment(element
);
2199 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
2201 struct bt_ctf_field_type
*element
=
2202 bt_ctf_field_type_array_get_element_type(type
);
2209 ret
= bt_ctf_field_type_get_alignment(element
);
2213 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
2215 int64_t i
, element_count
;
2217 element_count
= bt_ctf_field_type_structure_get_field_count(
2219 if (element_count
< 0) {
2220 ret
= element_count
;
2224 for (i
= 0; i
< element_count
; i
++) {
2225 struct bt_ctf_field_type
*field
;
2226 int field_alignment
;
2228 ret
= bt_ctf_field_type_structure_get_field_by_index(
2229 type
, NULL
, &field
, i
);
2235 field_alignment
= bt_ctf_field_type_get_alignment(
2238 if (field_alignment
< 0) {
2239 ret
= field_alignment
;
2243 type
->alignment
= MAX(field_alignment
, type
->alignment
);
2245 ret
= (int) type
->alignment
;
2248 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
2252 ret
= (int) type
->alignment
;
2260 int is_power_of_two(unsigned int value
)
2262 return ((value
& (value
- 1)) == 0) && value
> 0;
2265 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2266 unsigned int alignment
)
2269 enum bt_ctf_field_type_id type_id
;
2271 /* Alignment must be a power of two */
2272 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2277 type_id
= bt_ctf_field_type_get_type_id(type
);
2278 if (type_id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
2283 if (type
->id
== BT_CTF_FIELD_TYPE_ID_STRING
&&
2284 alignment
!= CHAR_BIT
) {
2289 if (type_id
== BT_CTF_FIELD_TYPE_ID_VARIANT
||
2290 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
||
2291 type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2292 /* Setting an alignment on these types makes no sense */
2297 type
->alignment
= alignment
;
2303 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2304 struct bt_ctf_field_type
*type
)
2306 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2313 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2315 struct bt_ctf_field_type_integer
*integer
= container_of(
2316 type
, struct bt_ctf_field_type_integer
, parent
);
2317 ret
= integer
->user_byte_order
;
2320 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2322 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2323 type
, struct bt_ctf_field_type_enumeration
, parent
);
2324 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2327 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
2329 struct bt_ctf_field_type_floating_point
*floating_point
=
2331 struct bt_ctf_field_type_floating_point
,
2333 ret
= floating_point
->user_byte_order
;
2340 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2341 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2342 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2343 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2349 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2350 enum bt_ctf_byte_order byte_order
)
2354 if (!type
|| type
->frozen
) {
2359 if (byte_order
!= BT_CTF_BYTE_ORDER_NATIVE
&&
2360 byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
2361 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
2362 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
2367 if (set_byte_order_funcs
[type
->id
]) {
2368 set_byte_order_funcs
[type
->id
](type
, byte_order
);
2374 enum bt_ctf_field_type_id
bt_ctf_field_type_get_type_id(
2375 struct bt_ctf_field_type
*type
)
2378 return BT_CTF_FIELD_TYPE_ID_UNKNOWN
;
2384 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2386 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
;
2389 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2391 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_FLOAT
;
2394 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2396 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ENUM
;
2399 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2401 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRING
;
2404 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2406 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
;
2409 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2411 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ARRAY
;
2414 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2416 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2419 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2421 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_VARIANT
;
2424 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2429 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2435 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2445 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2446 struct bt_ctf_field_type_variant
*variant
,
2449 struct bt_ctf_field_type
*type
= NULL
;
2450 GQuark field_name_quark
;
2452 struct structure_field
*field_entry
;
2453 struct range_overlap_query query
= {
2454 .range_start
._signed
= tag_value
,
2455 .range_end
._signed
= tag_value
,
2460 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2462 if (!query
.overlaps
) {
2466 field_name_quark
= query
.mapping_name
;
2467 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2468 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2472 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2473 type
= field_entry
->type
;
2479 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2480 struct bt_ctf_field_type_variant
*variant
,
2483 struct bt_ctf_field_type
*type
= NULL
;
2484 GQuark field_name_quark
;
2486 struct structure_field
*field_entry
;
2487 struct range_overlap_query query
= {
2488 .range_start
._unsigned
= tag_value
,
2489 .range_end
._unsigned
= tag_value
,
2494 g_ptr_array_foreach(variant
->tag
->entries
,
2495 check_ranges_overlap_unsigned
,
2497 if (!query
.overlaps
) {
2501 field_name_quark
= query
.mapping_name
;
2502 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2503 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2507 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2508 type
= field_entry
->type
;
2514 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2515 struct metadata_context
*context
)
2519 if (!type
|| !context
) {
2524 /* Make sure field type is valid before serializing it */
2525 ret
= bt_ctf_field_type_validate(type
);
2531 ret
= type
->serialize(type
, context
);
2536 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2538 struct bt_ctf_field_type
*copy
= NULL
;
2544 copy
= type_copy_funcs
[type
->id
](type
);
2545 copy
->alignment
= type
->alignment
;
2551 int bt_ctf_field_type_structure_get_field_name_index(
2552 struct bt_ctf_field_type
*type
, const char *name
)
2557 struct bt_ctf_field_type_structure
*structure
;
2559 if (!type
|| !name
||
2560 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2565 name_quark
= g_quark_try_string(name
);
2571 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2573 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2574 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2584 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2585 struct bt_ctf_field_type
*field
, int index
)
2588 struct bt_ctf_field_type_structure
*structure
;
2590 if (!type
|| !field
||
2591 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2596 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2598 if (index
< 0 || index
>= structure
->fields
->len
) {
2604 bt_put(((struct structure_field
*)
2605 g_ptr_array_index(structure
->fields
, index
))->type
);
2606 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2613 int bt_ctf_field_type_variant_get_field_name_index(
2614 struct bt_ctf_field_type
*type
, const char *name
)
2619 struct bt_ctf_field_type_variant
*variant
;
2621 if (!type
|| !name
||
2622 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2627 name_quark
= g_quark_try_string(name
);
2633 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2635 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2636 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2646 int bt_ctf_field_type_sequence_set_length_field_path(
2647 struct bt_ctf_field_type
*type
,
2648 struct bt_ctf_field_path
*path
)
2651 struct bt_ctf_field_type_sequence
*sequence
;
2653 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2654 BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2659 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2662 BT_MOVE(sequence
->length_field_path
, path
);
2668 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2669 struct bt_ctf_field_path
*path
)
2672 struct bt_ctf_field_type_variant
*variant
;
2674 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2675 BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2680 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2683 BT_MOVE(variant
->tag_field_path
, path
);
2689 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2690 struct bt_ctf_field_type
*tag
)
2693 struct bt_ctf_field_type_variant
*variant
;
2695 if (!type
|| !tag
||
2696 bt_ctf_field_type_get_type_id(tag
) !=
2697 BT_CTF_FIELD_TYPE_ID_ENUM
) {
2702 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2706 bt_put(&variant
->tag
->parent
);
2708 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2715 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2716 struct bt_ctf_field_type
*field
, int index
)
2719 struct bt_ctf_field_type_variant
*variant
;
2721 if (!type
|| !field
||
2722 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2727 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2729 if (index
< 0 || index
>= variant
->fields
->len
) {
2735 bt_put(((struct structure_field
*)
2736 g_ptr_array_index(variant
->fields
, index
))->type
);
2737 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2744 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2746 struct bt_ctf_field_type_integer
*integer
=
2747 (struct bt_ctf_field_type_integer
*) type
;
2753 bt_put(integer
->mapped_clock
);
2758 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2760 struct bt_ctf_field_type_enumeration
*enumeration
=
2761 (struct bt_ctf_field_type_enumeration
*) type
;
2767 g_ptr_array_free(enumeration
->entries
, TRUE
);
2768 bt_put(enumeration
->container
);
2769 g_free(enumeration
);
2773 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2775 struct bt_ctf_field_type_floating_point
*floating_point
=
2776 (struct bt_ctf_field_type_floating_point
*) type
;
2782 g_free(floating_point
);
2786 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2788 struct bt_ctf_field_type_structure
*structure
=
2789 (struct bt_ctf_field_type_structure
*) type
;
2795 g_ptr_array_free(structure
->fields
, TRUE
);
2796 g_hash_table_destroy(structure
->field_name_to_index
);
2801 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2803 struct bt_ctf_field_type_variant
*variant
=
2804 (struct bt_ctf_field_type_variant
*) type
;
2810 g_ptr_array_free(variant
->fields
, TRUE
);
2811 g_hash_table_destroy(variant
->field_name_to_index
);
2812 g_string_free(variant
->tag_name
, TRUE
);
2813 bt_put(&variant
->tag
->parent
);
2814 BT_PUT(variant
->tag_field_path
);
2819 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2821 struct bt_ctf_field_type_array
*array
=
2822 (struct bt_ctf_field_type_array
*) type
;
2828 bt_put(array
->element_type
);
2833 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2835 struct bt_ctf_field_type_sequence
*sequence
=
2836 (struct bt_ctf_field_type_sequence
*) type
;
2842 bt_put(sequence
->element_type
);
2843 g_string_free(sequence
->length_field_name
, TRUE
);
2844 BT_PUT(sequence
->length_field_path
);
2849 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2851 struct bt_ctf_field_type_string
*string
=
2852 (struct bt_ctf_field_type_string
*) type
;
2862 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2868 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2870 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2871 type
, struct bt_ctf_field_type_integer
, parent
);
2873 if (integer_type
->mapped_clock
) {
2874 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2877 generic_field_type_freeze(type
);
2881 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2883 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2884 type
, struct bt_ctf_field_type_enumeration
, parent
);
2886 set_enumeration_range_overlap(type
);
2888 generic_field_type_freeze(type
);
2889 bt_ctf_field_type_freeze(enumeration_type
->container
);
2893 void freeze_structure_field(struct structure_field
*field
)
2895 bt_ctf_field_type_freeze(field
->type
);
2899 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2901 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2902 type
, struct bt_ctf_field_type_structure
, parent
);
2904 /* Cache the alignment */
2905 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2906 generic_field_type_freeze(type
);
2907 g_ptr_array_foreach(structure_type
->fields
,
2908 (GFunc
) freeze_structure_field
, NULL
);
2912 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2914 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2915 type
, struct bt_ctf_field_type_variant
, parent
);
2917 generic_field_type_freeze(type
);
2918 g_ptr_array_foreach(variant_type
->fields
,
2919 (GFunc
) freeze_structure_field
, NULL
);
2923 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2925 struct bt_ctf_field_type_array
*array_type
= container_of(
2926 type
, struct bt_ctf_field_type_array
, parent
);
2928 /* Cache the alignment */
2929 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2930 generic_field_type_freeze(type
);
2931 bt_ctf_field_type_freeze(array_type
->element_type
);
2935 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2937 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2938 type
, struct bt_ctf_field_type_sequence
, parent
);
2940 /* Cache the alignment */
2941 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2942 generic_field_type_freeze(type
);
2943 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2947 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2949 const char *encoding_string
;
2952 case BT_CTF_STRING_ENCODING_NONE
:
2953 encoding_string
= "none";
2955 case BT_CTF_STRING_ENCODING_ASCII
:
2956 encoding_string
= "ASCII";
2958 case BT_CTF_STRING_ENCODING_UTF8
:
2959 encoding_string
= "UTF8";
2962 encoding_string
= "unknown";
2966 return encoding_string
;
2970 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2972 const char *base_string
;
2975 case BT_CTF_INTEGER_BASE_DECIMAL
:
2976 base_string
= "decimal";
2978 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2979 base_string
= "hexadecimal";
2981 case BT_CTF_INTEGER_BASE_OCTAL
:
2982 base_string
= "octal";
2984 case BT_CTF_INTEGER_BASE_BINARY
:
2985 base_string
= "binary";
2988 base_string
= "unknown";
2996 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2997 struct metadata_context
*context
)
2999 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3000 struct bt_ctf_field_type_integer
, parent
);
3003 g_string_append_printf(context
->string
,
3004 "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s",
3005 integer
->size
, type
->alignment
,
3006 (integer
->is_signed
? "true" : "false"),
3007 get_encoding_string(integer
->encoding
),
3008 get_integer_base_string(integer
->base
),
3009 get_byte_order_string(integer
->user_byte_order
));
3010 if (integer
->mapped_clock
) {
3011 const char *clock_name
= bt_ctf_clock_class_get_name(
3012 integer
->mapped_clock
);
3019 g_string_append_printf(context
->string
,
3020 "; map = clock.%s.value", clock_name
);
3023 g_string_append(context
->string
, "; }");
3029 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3030 struct metadata_context
*context
)
3034 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3035 struct bt_ctf_field_type_enumeration
, parent
);
3036 struct bt_ctf_field_type
*container_type
;
3037 int container_signed
;
3039 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3040 if (!container_type
) {
3045 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3046 if (container_signed
< 0) {
3047 ret
= container_signed
;
3048 goto error_put_container_type
;
3051 g_string_append(context
->string
, "enum : ");
3052 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3054 goto error_put_container_type
;
3057 g_string_append(context
->string
, " { ");
3058 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3059 struct enumeration_mapping
*mapping
=
3060 enumeration
->entries
->pdata
[entry
];
3062 if (container_signed
) {
3063 if (mapping
->range_start
._signed
==
3064 mapping
->range_end
._signed
) {
3065 g_string_append_printf(context
->string
,
3066 "\"%s\" = %" PRId64
,
3067 g_quark_to_string(mapping
->string
),
3068 mapping
->range_start
._signed
);
3070 g_string_append_printf(context
->string
,
3071 "\"%s\" = %" PRId64
" ... %" PRId64
,
3072 g_quark_to_string(mapping
->string
),
3073 mapping
->range_start
._signed
,
3074 mapping
->range_end
._signed
);
3077 if (mapping
->range_start
._unsigned
==
3078 mapping
->range_end
._unsigned
) {
3079 g_string_append_printf(context
->string
,
3080 "\"%s\" = %" PRIu64
,
3081 g_quark_to_string(mapping
->string
),
3082 mapping
->range_start
._unsigned
);
3084 g_string_append_printf(context
->string
,
3085 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3086 g_quark_to_string(mapping
->string
),
3087 mapping
->range_start
._unsigned
,
3088 mapping
->range_end
._unsigned
);
3092 g_string_append(context
->string
,
3093 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3097 if (context
->field_name
->len
) {
3098 g_string_append_printf(context
->string
, " %s",
3099 context
->field_name
->str
);
3100 g_string_assign(context
->field_name
, "");
3102 error_put_container_type
:
3103 bt_put(container_type
);
3109 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3110 struct metadata_context
*context
)
3112 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3113 type
, struct bt_ctf_field_type_floating_point
, parent
);
3115 g_string_append_printf(context
->string
,
3116 "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }",
3117 floating_point
->exp_dig
,
3118 floating_point
->mant_dig
,
3119 get_byte_order_string(floating_point
->user_byte_order
),
3125 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3126 struct metadata_context
*context
)
3129 unsigned int indent
;
3131 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3132 struct bt_ctf_field_type_structure
, parent
);
3133 GString
*structure_field_name
= context
->field_name
;
3135 context
->field_name
= g_string_new("");
3137 context
->current_indentation_level
++;
3138 g_string_append(context
->string
, "struct {\n");
3140 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3141 struct structure_field
*field
;
3143 for (indent
= 0; indent
< context
->current_indentation_level
;
3145 g_string_append_c(context
->string
, '\t');
3148 field
= structure
->fields
->pdata
[i
];
3149 g_string_assign(context
->field_name
,
3150 g_quark_to_string(field
->name
));
3151 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3156 if (context
->field_name
->len
) {
3157 g_string_append_printf(context
->string
, " %s",
3158 context
->field_name
->str
);
3160 g_string_append(context
->string
, ";\n");
3163 context
->current_indentation_level
--;
3164 for (indent
= 0; indent
< context
->current_indentation_level
;
3166 g_string_append_c(context
->string
, '\t');
3169 g_string_append_printf(context
->string
, "} align(%u)",
3172 g_string_free(context
->field_name
, TRUE
);
3173 context
->field_name
= structure_field_name
;
3178 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3179 struct metadata_context
*context
)
3182 unsigned int indent
;
3184 struct bt_ctf_field_type_variant
*variant
= container_of(
3185 type
, struct bt_ctf_field_type_variant
, parent
);
3186 GString
*variant_field_name
= context
->field_name
;
3188 context
->field_name
= g_string_new("");
3189 if (variant
->tag_name
->len
> 0) {
3190 g_string_append_printf(context
->string
,
3191 "variant <%s> {\n", variant
->tag_name
->str
);
3193 g_string_append(context
->string
, "variant {\n");
3196 context
->current_indentation_level
++;
3197 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3198 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3200 g_string_assign(context
->field_name
,
3201 g_quark_to_string(field
->name
));
3202 for (indent
= 0; indent
< context
->current_indentation_level
;
3204 g_string_append_c(context
->string
, '\t');
3207 g_string_assign(context
->field_name
,
3208 g_quark_to_string(field
->name
));
3209 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3214 if (context
->field_name
->len
) {
3215 g_string_append_printf(context
->string
, " %s;",
3216 context
->field_name
->str
);
3219 g_string_append_c(context
->string
, '\n');
3222 context
->current_indentation_level
--;
3223 for (indent
= 0; indent
< context
->current_indentation_level
;
3225 g_string_append_c(context
->string
, '\t');
3228 g_string_append(context
->string
, "}");
3230 g_string_free(context
->field_name
, TRUE
);
3231 context
->field_name
= variant_field_name
;
3236 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3237 struct metadata_context
*context
)
3240 struct bt_ctf_field_type_array
*array
= container_of(type
,
3241 struct bt_ctf_field_type_array
, parent
);
3243 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3248 if (context
->field_name
->len
) {
3249 g_string_append_printf(context
->string
, " %s[%u]",
3250 context
->field_name
->str
, array
->length
);
3251 g_string_assign(context
->field_name
, "");
3253 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3260 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3261 struct metadata_context
*context
)
3264 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3265 type
, struct bt_ctf_field_type_sequence
, parent
);
3267 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3272 if (context
->field_name
->len
) {
3273 g_string_append_printf(context
->string
, " %s[%s]",
3274 context
->field_name
->str
,
3275 sequence
->length_field_name
->str
);
3276 g_string_assign(context
->field_name
, "");
3278 g_string_append_printf(context
->string
, "[%s]",
3279 sequence
->length_field_name
->str
);
3286 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3287 struct metadata_context
*context
)
3289 struct bt_ctf_field_type_string
*string
= container_of(
3290 type
, struct bt_ctf_field_type_string
, parent
);
3292 g_string_append_printf(context
->string
,
3293 "string { encoding = %s; }",
3294 get_encoding_string(string
->encoding
));
3299 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3300 enum bt_ctf_byte_order byte_order
)
3302 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3303 struct bt_ctf_field_type_integer
, parent
);
3305 integer_type
->user_byte_order
= byte_order
;
3309 void bt_ctf_field_type_enumeration_set_byte_order(
3310 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3312 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3313 struct bt_ctf_field_type_enumeration
, parent
);
3315 /* Safe to assume that container is an integer */
3316 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3321 void bt_ctf_field_type_floating_point_set_byte_order(
3322 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3324 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3325 container_of(type
, struct bt_ctf_field_type_floating_point
,
3328 floating_point_type
->user_byte_order
= byte_order
;
3332 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3333 enum bt_ctf_byte_order byte_order
)
3336 struct bt_ctf_field_type_structure
*structure_type
=
3337 container_of(type
, struct bt_ctf_field_type_structure
,
3340 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3341 struct structure_field
*field
= g_ptr_array_index(
3342 structure_type
->fields
, i
);
3343 struct bt_ctf_field_type
*field_type
= field
->type
;
3345 if (set_byte_order_funcs
[field_type
->id
]) {
3346 set_byte_order_funcs
[field_type
->id
](
3347 field_type
, byte_order
);
3353 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3354 enum bt_ctf_byte_order byte_order
)
3357 struct bt_ctf_field_type_variant
*variant_type
=
3358 container_of(type
, struct bt_ctf_field_type_variant
,
3361 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3362 struct structure_field
*field
= g_ptr_array_index(
3363 variant_type
->fields
, i
);
3364 struct bt_ctf_field_type
*field_type
= field
->type
;
3366 if (set_byte_order_funcs
[field_type
->id
]) {
3367 set_byte_order_funcs
[field_type
->id
](
3368 field_type
, byte_order
);
3374 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3375 enum bt_ctf_byte_order byte_order
)
3377 struct bt_ctf_field_type_array
*array_type
=
3378 container_of(type
, struct bt_ctf_field_type_array
,
3381 if (set_byte_order_funcs
[array_type
->element_type
->id
]) {
3382 set_byte_order_funcs
[array_type
->element_type
->id
](
3383 array_type
->element_type
, byte_order
);
3388 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3389 enum bt_ctf_byte_order byte_order
)
3391 struct bt_ctf_field_type_sequence
*sequence_type
=
3392 container_of(type
, struct bt_ctf_field_type_sequence
,
3395 if (set_byte_order_funcs
[
3396 sequence_type
->element_type
->id
]) {
3397 set_byte_order_funcs
[
3398 sequence_type
->element_type
->id
](
3399 sequence_type
->element_type
, byte_order
);
3404 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3405 struct bt_ctf_field_type
*type
)
3407 struct bt_ctf_field_type
*copy
;
3408 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3410 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3411 copy
= bt_ctf_field_type_integer_create(integer
->size
);
3416 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3418 copy_integer
->mapped_clock
= bt_get(integer
->mapped_clock
);
3419 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3420 copy_integer
->is_signed
= integer
->is_signed
;
3421 copy_integer
->size
= integer
->size
;
3422 copy_integer
->base
= integer
->base
;
3423 copy_integer
->encoding
= integer
->encoding
;
3430 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3431 struct bt_ctf_field_type
*type
)
3434 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3435 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3437 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3440 /* Copy the source enumeration's container */
3441 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3442 if (!copy_container
) {
3446 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3450 copy_enumeration
= container_of(copy
,
3451 struct bt_ctf_field_type_enumeration
, parent
);
3453 /* Copy all enumaration entries */
3454 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3455 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3456 enumeration
->entries
, i
);
3457 struct enumeration_mapping
*copy_mapping
= g_new0(
3458 struct enumeration_mapping
, 1);
3460 if (!copy_mapping
) {
3464 *copy_mapping
= *mapping
;
3465 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3469 bt_put(copy_container
);
3472 bt_put(copy_container
);
3478 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3479 struct bt_ctf_field_type
*type
)
3481 struct bt_ctf_field_type
*copy
;
3482 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3484 floating_point
= container_of(type
,
3485 struct bt_ctf_field_type_floating_point
, parent
);
3486 copy
= bt_ctf_field_type_floating_point_create();
3491 copy_float
= container_of(copy
,
3492 struct bt_ctf_field_type_floating_point
, parent
);
3493 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3494 copy_float
->exp_dig
= floating_point
->exp_dig
;
3495 copy_float
->mant_dig
= floating_point
->mant_dig
;
3501 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3502 struct bt_ctf_field_type
*type
)
3505 GHashTableIter iter
;
3506 gpointer key
, value
;
3507 struct bt_ctf_field_type
*copy
;
3508 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3510 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3512 copy
= bt_ctf_field_type_structure_create();
3517 copy_structure
= container_of(copy
,
3518 struct bt_ctf_field_type_structure
, parent
);
3520 /* Copy field_name_to_index */
3521 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3522 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3523 g_hash_table_insert(copy_structure
->field_name_to_index
,
3527 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3528 struct structure_field
*entry
, *copy_entry
;
3529 struct bt_ctf_field_type
*copy_field
;
3531 copy_entry
= g_new0(struct structure_field
, 1);
3536 entry
= g_ptr_array_index(structure
->fields
, i
);
3537 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3543 copy_entry
->name
= entry
->name
;
3544 copy_entry
->type
= copy_field
;
3545 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3556 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3557 struct bt_ctf_field_type
*type
)
3560 GHashTableIter iter
;
3561 gpointer key
, value
;
3562 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3563 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3565 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3568 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3574 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3575 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3580 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3583 /* Copy field_name_to_index */
3584 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3585 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3586 g_hash_table_insert(copy_variant
->field_name_to_index
,
3590 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3591 struct structure_field
*entry
, *copy_entry
;
3592 struct bt_ctf_field_type
*copy_field
;
3594 copy_entry
= g_new0(struct structure_field
, 1);
3599 entry
= g_ptr_array_index(variant
->fields
, i
);
3600 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3606 copy_entry
->name
= entry
->name
;
3607 copy_entry
->type
= copy_field
;
3608 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3611 if (variant
->tag_field_path
) {
3612 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3613 variant
->tag_field_path
);
3614 if (!copy_variant
->tag_field_path
) {
3628 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3629 struct bt_ctf_field_type
*type
)
3631 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3632 struct bt_ctf_field_type_array
*array
;
3634 array
= container_of(type
, struct bt_ctf_field_type_array
,
3636 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3637 if (!copy_element
) {
3641 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3646 bt_put(copy_element
);
3651 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3652 struct bt_ctf_field_type
*type
)
3654 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3655 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3657 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3659 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3660 if (!copy_element
) {
3664 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3665 sequence
->length_field_name
->len
?
3666 sequence
->length_field_name
->str
: NULL
);
3671 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3673 if (sequence
->length_field_path
) {
3674 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3675 sequence
->length_field_path
);
3676 if (!copy_sequence
->length_field_path
) {
3681 bt_put(copy_element
);
3689 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3690 struct bt_ctf_field_type
*type
)
3692 struct bt_ctf_field_type
*copy
;
3693 struct bt_ctf_field_type_string
*string
;
3695 copy
= bt_ctf_field_type_string_create();
3700 string
= container_of(type
, struct bt_ctf_field_type_string
,
3707 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3708 struct bt_ctf_field_type
*type_b
)
3711 struct bt_ctf_field_type_integer
*int_type_a
;
3712 struct bt_ctf_field_type_integer
*int_type_b
;
3714 int_type_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3716 int_type_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3720 if (int_type_a
->size
!= int_type_b
->size
) {
3725 if (int_type_a
->user_byte_order
!= int_type_b
->user_byte_order
) {
3730 if (int_type_a
->is_signed
!= int_type_b
->is_signed
) {
3735 if (int_type_a
->base
!= int_type_b
->base
) {
3740 if (int_type_a
->encoding
!= int_type_b
->encoding
) {
3745 if (int_type_a
->mapped_clock
!= int_type_b
->mapped_clock
) {
3757 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3758 struct bt_ctf_field_type
*type_b
)
3761 struct bt_ctf_field_type_floating_point
*float_a
;
3762 struct bt_ctf_field_type_floating_point
*float_b
;
3764 float_a
= container_of(type_a
,
3765 struct bt_ctf_field_type_floating_point
, parent
);
3766 float_b
= container_of(type_b
,
3767 struct bt_ctf_field_type_floating_point
, parent
);
3770 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3774 /* Exponent length */
3775 if (float_a
->exp_dig
!= float_b
->exp_dig
) {
3779 /* Mantissa length */
3780 if (float_a
->mant_dig
!= float_b
->mant_dig
) {
3792 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3793 struct enumeration_mapping
*mapping_b
)
3798 if (mapping_a
->string
!= mapping_b
->string
) {
3803 if (mapping_a
->range_start
._unsigned
!=
3804 mapping_b
->range_start
._unsigned
) {
3809 if (mapping_a
->range_end
._unsigned
!=
3810 mapping_b
->range_end
._unsigned
) {
3822 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3823 struct bt_ctf_field_type
*type_b
)
3827 struct bt_ctf_field_type_enumeration
*enum_a
;
3828 struct bt_ctf_field_type_enumeration
*enum_b
;
3830 enum_a
= container_of(type_a
,
3831 struct bt_ctf_field_type_enumeration
, parent
);
3832 enum_b
= container_of(type_b
,
3833 struct bt_ctf_field_type_enumeration
, parent
);
3835 /* Container field type */
3836 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3844 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3848 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3849 struct enumeration_mapping
*mapping_a
=
3850 g_ptr_array_index(enum_a
->entries
, i
);
3851 struct enumeration_mapping
*mapping_b
=
3852 g_ptr_array_index(enum_b
->entries
, i
);
3854 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3867 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3868 struct bt_ctf_field_type
*type_b
)
3871 struct bt_ctf_field_type_string
*string_a
;
3872 struct bt_ctf_field_type_string
*string_b
;
3874 string_a
= container_of(type_a
,
3875 struct bt_ctf_field_type_string
, parent
);
3876 string_b
= container_of(type_b
,
3877 struct bt_ctf_field_type_string
, parent
);
3880 if (string_a
->encoding
!= string_b
->encoding
) {
3892 int compare_structure_fields(struct structure_field
*field_a
,
3893 struct structure_field
*field_b
)
3898 if (field_a
->name
!= field_b
->name
) {
3903 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3910 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3911 struct bt_ctf_field_type
*type_b
)
3915 struct bt_ctf_field_type_structure
*struct_a
;
3916 struct bt_ctf_field_type_structure
*struct_b
;
3918 struct_a
= container_of(type_a
,
3919 struct bt_ctf_field_type_structure
, parent
);
3920 struct_b
= container_of(type_b
,
3921 struct bt_ctf_field_type_structure
, parent
);
3924 if (bt_ctf_field_type_get_alignment(type_a
) !=
3925 bt_ctf_field_type_get_alignment(type_b
)) {
3930 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3934 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3935 struct structure_field
*field_a
=
3936 g_ptr_array_index(struct_a
->fields
, i
);
3937 struct structure_field
*field_b
=
3938 g_ptr_array_index(struct_b
->fields
, i
);
3940 ret
= compare_structure_fields(field_a
, field_b
);
3954 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3955 struct bt_ctf_field_type
*type_b
)
3959 struct bt_ctf_field_type_variant
*variant_a
;
3960 struct bt_ctf_field_type_variant
*variant_b
;
3962 variant_a
= container_of(type_a
,
3963 struct bt_ctf_field_type_variant
, parent
);
3964 variant_b
= container_of(type_b
,
3965 struct bt_ctf_field_type_variant
, parent
);
3968 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3973 ret
= bt_ctf_field_type_compare(
3974 (struct bt_ctf_field_type
*) variant_a
->tag
,
3975 (struct bt_ctf_field_type
*) variant_b
->tag
);
3983 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3987 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3988 struct structure_field
*field_a
=
3989 g_ptr_array_index(variant_a
->fields
, i
);
3990 struct structure_field
*field_b
=
3991 g_ptr_array_index(variant_b
->fields
, i
);
3993 ret
= compare_structure_fields(field_a
, field_b
);
4007 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4008 struct bt_ctf_field_type
*type_b
)
4011 struct bt_ctf_field_type_array
*array_a
;
4012 struct bt_ctf_field_type_array
*array_b
;
4014 array_a
= container_of(type_a
,
4015 struct bt_ctf_field_type_array
, parent
);
4016 array_b
= container_of(type_b
,
4017 struct bt_ctf_field_type_array
, parent
);
4020 if (array_a
->length
!= array_b
->length
) {
4025 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4026 array_b
->element_type
);
4033 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4034 struct bt_ctf_field_type
*type_b
)
4037 struct bt_ctf_field_type_sequence
*sequence_a
;
4038 struct bt_ctf_field_type_sequence
*sequence_b
;
4040 sequence_a
= container_of(type_a
,
4041 struct bt_ctf_field_type_sequence
, parent
);
4042 sequence_b
= container_of(type_b
,
4043 struct bt_ctf_field_type_sequence
, parent
);
4046 if (strcmp(sequence_a
->length_field_name
->str
,
4047 sequence_b
->length_field_name
->str
)) {
4052 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4053 sequence_b
->element_type
);
4059 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4060 struct bt_ctf_field_type
*type_b
)
4064 if (type_a
== type_b
) {
4065 /* Same reference: equal (even if both are NULL) */
4070 if (!type_a
|| !type_b
) {
4075 if (type_a
->id
!= type_b
->id
) {
4076 /* Different type IDs */
4080 if (type_a
->id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
4081 /* Both have unknown type IDs */
4085 ret
= type_compare_funcs
[type_a
->id
](type_a
, type_b
);
4092 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4094 int64_t field_count
= -1;
4095 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4098 case CTF_TYPE_STRUCT
:
4100 bt_ctf_field_type_structure_get_field_count(field_type
);
4102 case CTF_TYPE_VARIANT
:
4104 bt_ctf_field_type_variant_get_field_count(field_type
);
4106 case CTF_TYPE_ARRAY
:
4107 case CTF_TYPE_SEQUENCE
:
4109 * Array and sequence types always contain a single member
4110 * (the element type).
4122 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4123 struct bt_ctf_field_type
*field_type
, int index
)
4125 struct bt_ctf_field_type
*field
= NULL
;
4126 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4129 case CTF_TYPE_STRUCT
:
4130 bt_ctf_field_type_structure_get_field_by_index(field_type
,
4131 NULL
, &field
, index
);
4133 case CTF_TYPE_VARIANT
:
4135 int ret
= bt_ctf_field_type_variant_get_field_by_index(
4136 field_type
, NULL
, &field
, index
);
4143 case CTF_TYPE_ARRAY
:
4144 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4146 case CTF_TYPE_SEQUENCE
:
4147 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4157 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4160 int field_index
= -1;
4161 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4164 case CTF_TYPE_STRUCT
:
4165 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4168 case CTF_TYPE_VARIANT
:
4169 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4179 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4180 struct bt_ctf_field_type
*type
)
4182 struct bt_ctf_field_path
*field_path
= NULL
;
4183 struct bt_ctf_field_type_variant
*variant
;
4185 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4189 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4191 field_path
= bt_get(variant
->tag_field_path
);
4196 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4197 struct bt_ctf_field_type
*type
)
4199 struct bt_ctf_field_path
*field_path
= NULL
;
4200 struct bt_ctf_field_type_sequence
*sequence
;
4202 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4206 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4208 field_path
= bt_get(sequence
->length_field_path
);