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
, bt_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
= BT_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
= BT_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 bt_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 bt_bool
bt_ctf_field_type_integer_is_signed(
831 struct bt_ctf_field_type
*int_field_type
)
833 return bt_ctf_field_type_integer_get_signed(int_field_type
) ?
837 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
841 struct bt_ctf_field_type_integer
*integer
;
843 if (!type
|| type
->frozen
||
844 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
849 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
850 integer
->is_signed
= !!is_signed
;
855 int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type
*type
,
859 struct bt_ctf_field_type_integer
*integer
;
861 if (!type
|| type
->frozen
||
862 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
863 !size
|| size
> 64) {
868 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
869 integer
->size
= size
;
874 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
875 struct bt_ctf_field_type
*type
)
877 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
878 struct bt_ctf_field_type_integer
*integer
;
880 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
884 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
890 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
891 enum bt_ctf_integer_base base
)
895 if (!type
|| type
->frozen
||
896 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
902 case BT_CTF_INTEGER_BASE_BINARY
:
903 case BT_CTF_INTEGER_BASE_OCTAL
:
904 case BT_CTF_INTEGER_BASE_DECIMAL
:
905 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
907 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
908 struct bt_ctf_field_type_integer
, parent
);
909 integer
->base
= base
;
919 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
920 struct bt_ctf_field_type
*type
)
922 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
923 struct bt_ctf_field_type_integer
*integer
;
925 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
929 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
930 ret
= integer
->encoding
;
935 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
936 enum bt_ctf_string_encoding encoding
)
939 struct bt_ctf_field_type_integer
*integer
;
941 if (!type
|| type
->frozen
||
942 (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) ||
943 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
944 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
949 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
950 integer
->encoding
= encoding
;
955 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
956 struct bt_ctf_field_type
*type
)
958 struct bt_ctf_field_type_integer
*integer
;
959 struct bt_ctf_clock_class
*clock_class
= NULL
;
965 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
966 clock_class
= integer
->mapped_clock
;
972 int bt_ctf_field_type_integer_set_mapped_clock_class(
973 struct bt_ctf_field_type
*type
,
974 struct bt_ctf_clock_class
*clock_class
)
976 struct bt_ctf_field_type_integer
*integer
;
979 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
984 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
985 bt_put(integer
->mapped_clock
);
986 integer
->mapped_clock
= bt_get(clock_class
);
992 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
994 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
996 struct bt_ctf_field_type_enumeration_mapping_iterator
,
999 bt_put(&iter
->enumeration_type
->parent
);
1004 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1005 bt_ctf_field_type_enumeration_find_mappings_type(
1006 struct bt_ctf_field_type
*type
,
1007 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
1009 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1010 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1012 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1016 enumeration_type
= container_of(type
,
1017 struct bt_ctf_field_type_enumeration
, parent
);
1018 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
1023 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
1025 iter
->enumeration_type
= enumeration_type
;
1027 iter
->type
= iterator_type
;
1032 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1033 bt_ctf_field_type_enumeration_find_mappings_by_name(
1034 struct bt_ctf_field_type
*type
, const char *name
)
1036 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1038 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1039 type
, ITERATOR_BY_NAME
);
1044 iter
->u
.name_quark
= g_quark_try_string(name
);
1045 if (!iter
->u
.name_quark
) {
1049 /* Advance iterator to first entry, or leave index at -1. */
1050 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1051 /* No entry found. */
1061 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1062 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1064 struct bt_ctf_field_type_enumeration
*enumeration
;
1065 struct bt_ctf_field_type
*type
;
1066 int i
, ret
= 0, len
;
1068 enumeration
= iter
->enumeration_type
;
1069 type
= &enumeration
->parent
;
1070 len
= enumeration
->entries
->len
;
1071 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1072 struct enumeration_mapping
*mapping
=
1073 get_enumeration_mapping(type
, i
);
1075 switch (iter
->type
) {
1076 case ITERATOR_BY_NAME
:
1077 if (mapping
->string
== iter
->u
.name_quark
) {
1082 case ITERATOR_BY_SIGNED_VALUE
:
1084 int64_t value
= iter
->u
.signed_value
;
1086 if (value
>= mapping
->range_start
._signed
&&
1087 value
<= mapping
->range_end
._signed
) {
1093 case ITERATOR_BY_UNSIGNED_VALUE
:
1095 uint64_t value
= iter
->u
.unsigned_value
;
1097 if (value
>= mapping
->range_start
._unsigned
&&
1098 value
<= mapping
->range_end
._unsigned
) {
1114 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1115 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1116 struct bt_ctf_field_type
*type
, int64_t value
)
1118 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1120 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1121 type
, ITERATOR_BY_SIGNED_VALUE
);
1126 if (bt_ctf_field_type_integer_get_signed(
1127 iter
->enumeration_type
->container
) != 1) {
1130 iter
->u
.signed_value
= value
;
1132 /* Advance iterator to first entry, or leave index at -1. */
1133 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1134 /* No entry found. */
1144 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1145 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1146 struct bt_ctf_field_type
*type
, uint64_t value
)
1148 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1150 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1151 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1156 if (bt_ctf_field_type_integer_get_signed(
1157 iter
->enumeration_type
->container
) != 0) {
1160 iter
->u
.unsigned_value
= value
;
1162 /* Advance iterator to first entry, or leave index at -1. */
1163 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1164 /* No entry found. */
1174 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1175 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1176 const char **mapping_name
, int64_t *range_begin
,
1186 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1187 &iter
->enumeration_type
->parent
, iter
->index
,
1188 mapping_name
, range_begin
, range_end
);
1193 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1194 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1195 const char **mapping_name
, uint64_t *range_begin
,
1196 uint64_t *range_end
)
1205 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1206 &iter
->enumeration_type
->parent
, iter
->index
,
1207 mapping_name
, range_begin
, range_end
);
1212 int bt_ctf_field_type_enumeration_get_mapping_signed(
1213 struct bt_ctf_field_type
*enum_field_type
,
1214 uint64_t index
, const char **mapping_name
, int64_t *range_begin
,
1218 struct enumeration_mapping
*mapping
;
1220 if (!enum_field_type
) {
1225 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1232 *mapping_name
= g_quark_to_string(mapping
->string
);
1236 *range_begin
= mapping
->range_start
._signed
;
1240 *range_end
= mapping
->range_end
._signed
;
1246 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1247 struct bt_ctf_field_type
*enum_field_type
,
1249 const char **mapping_name
, uint64_t *range_begin
,
1250 uint64_t *range_end
)
1253 struct enumeration_mapping
*mapping
;
1255 if (!enum_field_type
) {
1260 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1267 *mapping_name
= g_quark_to_string(mapping
->string
);
1271 *range_begin
= mapping
->range_start
._unsigned
;
1275 *range_end
= mapping
->range_end
._unsigned
;
1281 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1282 struct bt_ctf_field_type
*integer_container_type
)
1284 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1286 if (!integer_container_type
) {
1290 if (integer_container_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1294 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1299 enumeration
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ENUM
;
1300 bt_get(integer_container_type
);
1301 enumeration
->container
= integer_container_type
;
1302 enumeration
->entries
= g_ptr_array_new_with_free_func(
1303 (GDestroyNotify
)destroy_enumeration_mapping
);
1304 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1305 return &enumeration
->parent
;
1307 g_free(enumeration
);
1311 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1312 struct bt_ctf_field_type
*type
)
1314 struct bt_ctf_field_type
*container_type
= NULL
;
1315 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1321 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1325 enumeration_type
= container_of(type
,
1326 struct bt_ctf_field_type_enumeration
, parent
);
1327 container_type
= enumeration_type
->container
;
1328 bt_get(container_type
);
1330 return container_type
;
1333 int bt_ctf_field_type_enumeration_add_mapping(
1334 struct bt_ctf_field_type
*type
, const char *string
,
1335 int64_t range_start
, int64_t range_end
)
1338 GQuark mapping_name
;
1339 struct enumeration_mapping
*mapping
;
1340 struct bt_ctf_field_type_enumeration
*enumeration
;
1341 char *escaped_string
;
1343 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1345 (range_end
< range_start
)) {
1350 if (!string
|| strlen(string
) == 0) {
1355 escaped_string
= g_strescape(string
, NULL
);
1356 if (!escaped_string
) {
1361 mapping
= g_new(struct enumeration_mapping
, 1);
1366 mapping_name
= g_quark_from_string(escaped_string
);
1367 *mapping
= (struct enumeration_mapping
) {
1368 .range_start
._signed
= range_start
,
1369 .range_end
._signed
= range_end
,
1370 .string
= mapping_name
,
1372 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1374 g_ptr_array_add(enumeration
->entries
, mapping
);
1375 g_ptr_array_sort(enumeration
->entries
,
1376 (GCompareFunc
)compare_enumeration_mappings_signed
);
1378 free(escaped_string
);
1383 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1384 struct bt_ctf_field_type
*type
, const char *string
,
1385 uint64_t range_start
, uint64_t range_end
)
1388 GQuark mapping_name
;
1389 struct enumeration_mapping
*mapping
;
1390 struct bt_ctf_field_type_enumeration
*enumeration
;
1391 char *escaped_string
;
1393 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1395 (range_end
< range_start
)) {
1400 if (!string
|| strlen(string
) == 0) {
1405 escaped_string
= g_strescape(string
, NULL
);
1406 if (!escaped_string
) {
1411 mapping
= g_new(struct enumeration_mapping
, 1);
1416 mapping_name
= g_quark_from_string(escaped_string
);
1417 *mapping
= (struct enumeration_mapping
) {
1418 .range_start
._unsigned
= range_start
,
1419 .range_end
._unsigned
= range_end
,
1420 .string
= mapping_name
,
1422 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1424 g_ptr_array_add(enumeration
->entries
, mapping
);
1425 g_ptr_array_sort(enumeration
->entries
,
1426 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1428 free(escaped_string
);
1433 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
1434 struct bt_ctf_field_type
*type
)
1437 struct bt_ctf_field_type_enumeration
*enumeration
;
1439 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1444 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1446 ret
= (int64_t) enumeration
->entries
->len
;
1451 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1453 struct bt_ctf_field_type_floating_point
*floating_point
=
1454 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1456 if (!floating_point
) {
1460 floating_point
->parent
.id
= BT_CTF_FIELD_TYPE_ID_FLOAT
;
1461 floating_point
->exp_dig
= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1462 floating_point
->mant_dig
= FLT_MANT_DIG
;
1463 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1465 return floating_point
? &floating_point
->parent
: NULL
;
1468 int bt_ctf_field_type_floating_point_get_exponent_digits(
1469 struct bt_ctf_field_type
*type
)
1472 struct bt_ctf_field_type_floating_point
*floating_point
;
1474 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1479 floating_point
= container_of(type
,
1480 struct bt_ctf_field_type_floating_point
, parent
);
1481 ret
= (int) floating_point
->exp_dig
;
1486 int bt_ctf_field_type_floating_point_set_exponent_digits(
1487 struct bt_ctf_field_type
*type
,
1488 unsigned int exponent_digits
)
1491 struct bt_ctf_field_type_floating_point
*floating_point
;
1493 if (!type
|| type
->frozen
||
1494 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1499 floating_point
= container_of(type
,
1500 struct bt_ctf_field_type_floating_point
, parent
);
1501 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1502 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1504 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1509 floating_point
->exp_dig
= exponent_digits
;
1514 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1515 struct bt_ctf_field_type
*type
)
1518 struct bt_ctf_field_type_floating_point
*floating_point
;
1520 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1525 floating_point
= container_of(type
,
1526 struct bt_ctf_field_type_floating_point
, parent
);
1527 ret
= (int) floating_point
->mant_dig
;
1532 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1533 struct bt_ctf_field_type
*type
,
1534 unsigned int mantissa_digits
)
1537 struct bt_ctf_field_type_floating_point
*floating_point
;
1539 if (!type
|| type
->frozen
||
1540 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1545 floating_point
= container_of(type
,
1546 struct bt_ctf_field_type_floating_point
, parent
);
1548 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1549 (mantissa_digits
!= DBL_MANT_DIG
) &&
1550 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1555 floating_point
->mant_dig
= mantissa_digits
;
1560 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1562 struct bt_ctf_field_type_structure
*structure
=
1563 g_new0(struct bt_ctf_field_type_structure
, 1);
1569 structure
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRUCT
;
1570 structure
->fields
= g_ptr_array_new_with_free_func(
1571 (GDestroyNotify
)destroy_structure_field
);
1572 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1573 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1574 return &structure
->parent
;
1579 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1580 struct bt_ctf_field_type
*field_type
,
1581 const char *field_name
)
1584 struct bt_ctf_field_type_structure
*structure
;
1587 * TODO: check that `field_type` does not contain `type`,
1590 if (!type
|| !field_type
|| type
->frozen
||
1591 bt_ctf_validate_identifier(field_name
) ||
1592 (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) ||
1593 type
== field_type
) {
1598 structure
= container_of(type
,
1599 struct bt_ctf_field_type_structure
, parent
);
1600 if (add_structure_field(structure
->fields
,
1601 structure
->field_name_to_index
, field_type
, field_name
)) {
1609 int64_t bt_ctf_field_type_structure_get_field_count(
1610 struct bt_ctf_field_type
*type
)
1613 struct bt_ctf_field_type_structure
*structure
;
1615 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
)) {
1620 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1622 ret
= (int64_t) structure
->fields
->len
;
1627 int bt_ctf_field_type_structure_get_field_by_index(
1628 struct bt_ctf_field_type
*type
,
1629 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1632 struct bt_ctf_field_type_structure
*structure
;
1633 struct structure_field
*field
;
1636 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1641 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1643 if (index
>= structure
->fields
->len
) {
1648 field
= g_ptr_array_index(structure
->fields
, index
);
1650 *field_type
= field
->type
;
1651 bt_get(field
->type
);
1654 *field_name
= g_quark_to_string(field
->name
);
1660 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1661 struct bt_ctf_field_type
*type
,
1666 struct structure_field
*field
;
1667 struct bt_ctf_field_type_structure
*structure
;
1668 struct bt_ctf_field_type
*field_type
= NULL
;
1670 if (!type
|| !name
) {
1674 name_quark
= g_quark_try_string(name
);
1679 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1681 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1682 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1686 field
= structure
->fields
->pdata
[index
];
1687 field_type
= field
->type
;
1693 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1694 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1696 struct bt_ctf_field_type_variant
*variant
= NULL
;
1698 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1702 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1707 variant
->parent
.id
= BT_CTF_FIELD_TYPE_ID_VARIANT
;
1708 variant
->tag_name
= g_string_new(tag_name
);
1709 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1710 variant
->fields
= g_ptr_array_new_with_free_func(
1711 (GDestroyNotify
) destroy_structure_field
);
1714 variant
->tag
= container_of(enum_tag
,
1715 struct bt_ctf_field_type_enumeration
, parent
);
1718 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1719 /* A variant's alignment is undefined */
1720 variant
->parent
.alignment
= 0;
1721 return &variant
->parent
;
1726 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1727 struct bt_ctf_field_type
*type
)
1729 struct bt_ctf_field_type_variant
*variant
;
1730 struct bt_ctf_field_type
*tag_type
= NULL
;
1732 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1736 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1737 if (!variant
->tag
) {
1741 tag_type
= &variant
->tag
->parent
;
1747 const char *bt_ctf_field_type_variant_get_tag_name(
1748 struct bt_ctf_field_type
*type
)
1750 struct bt_ctf_field_type_variant
*variant
;
1751 const char *tag_name
= NULL
;
1753 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1757 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1758 if (variant
->tag_name
->len
== 0) {
1762 tag_name
= variant
->tag_name
->str
;
1767 int bt_ctf_field_type_variant_set_tag_name(
1768 struct bt_ctf_field_type
*type
, const char *name
)
1771 struct bt_ctf_field_type_variant
*variant
;
1773 if (!type
|| type
->frozen
||
1774 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1775 bt_ctf_validate_identifier(name
)) {
1780 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1781 g_string_assign(variant
->tag_name
, name
);
1786 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1787 struct bt_ctf_field_type
*field_type
,
1788 const char *field_name
)
1792 struct bt_ctf_field_type_variant
*variant
;
1793 GQuark field_name_quark
= g_quark_from_string(field_name
);
1796 * TODO: check that `field_type` does not contain `type`,
1799 if (!type
|| !field_type
|| type
->frozen
||
1800 bt_ctf_validate_identifier(field_name
) ||
1801 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1802 type
== field_type
) {
1807 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1809 /* The user has explicitly provided a tag; validate against it. */
1813 /* Make sure this name is present in the enum tag */
1814 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1815 struct enumeration_mapping
*mapping
=
1816 g_ptr_array_index(variant
->tag
->entries
, i
);
1818 if (mapping
->string
== field_name_quark
) {
1825 /* Validation failed */
1831 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1832 field_type
, field_name
)) {
1840 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1841 struct bt_ctf_field_type
*type
,
1842 const char *field_name
)
1846 struct structure_field
*field
;
1847 struct bt_ctf_field_type_variant
*variant
;
1848 struct bt_ctf_field_type
*field_type
= NULL
;
1850 if (!type
|| !field_name
) {
1854 name_quark
= g_quark_try_string(field_name
);
1859 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1860 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1861 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1865 field
= g_ptr_array_index(variant
->fields
, index
);
1866 field_type
= field
->type
;
1872 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1873 struct bt_ctf_field_type
*type
,
1874 struct bt_ctf_field
*tag
)
1877 const char *enum_value
;
1878 struct bt_ctf_field_type
*field_type
= NULL
;
1879 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1881 if (!type
|| !tag
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1885 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1890 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
,
1891 &enum_value
, NULL
, NULL
);
1896 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1903 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1906 struct bt_ctf_field_type_variant
*variant
;
1908 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1913 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1915 ret
= (int64_t) variant
->fields
->len
;
1921 int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type
*type
,
1922 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1925 struct bt_ctf_field_type_variant
*variant
;
1926 struct structure_field
*field
;
1929 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1934 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1936 if (index
>= variant
->fields
->len
) {
1941 field
= g_ptr_array_index(variant
->fields
, index
);
1943 *field_type
= field
->type
;
1944 bt_get(field
->type
);
1947 *field_name
= g_quark_to_string(field
->name
);
1953 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1954 struct bt_ctf_field_type
*element_type
,
1955 unsigned int length
)
1957 struct bt_ctf_field_type_array
*array
= NULL
;
1959 if (!element_type
|| length
== 0) {
1963 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1968 array
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ARRAY
;
1969 bt_get(element_type
);
1970 array
->element_type
= element_type
;
1971 array
->length
= length
;
1972 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1973 return &array
->parent
;
1978 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1979 struct bt_ctf_field_type
*type
)
1981 struct bt_ctf_field_type
*ret
= NULL
;
1982 struct bt_ctf_field_type_array
*array
;
1984 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
1988 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1989 ret
= array
->element_type
;
1996 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1997 struct bt_ctf_field_type
*element_type
)
2000 struct bt_ctf_field_type_array
*array
;
2002 if (!type
|| !element_type
||
2003 (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
2008 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2010 if (array
->element_type
) {
2011 BT_PUT(array
->element_type
);
2014 array
->element_type
= element_type
;
2015 bt_get(array
->element_type
);
2021 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2024 struct bt_ctf_field_type_array
*array
;
2026 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
2031 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2032 ret
= (int64_t) array
->length
;
2037 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2038 struct bt_ctf_field_type
*element_type
,
2039 const char *length_field_name
)
2041 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2043 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2047 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2052 sequence
->parent
.id
= BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2053 bt_get(element_type
);
2054 sequence
->element_type
= element_type
;
2055 sequence
->length_field_name
= g_string_new(length_field_name
);
2056 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2057 return &sequence
->parent
;
2062 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2063 struct bt_ctf_field_type
*type
)
2065 struct bt_ctf_field_type
*ret
= NULL
;
2066 struct bt_ctf_field_type_sequence
*sequence
;
2068 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2072 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2074 ret
= sequence
->element_type
;
2081 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2082 struct bt_ctf_field_type
*element_type
)
2085 struct bt_ctf_field_type_sequence
*sequence
;
2087 if (!type
|| !element_type
||
2088 (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2093 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2095 if (sequence
->element_type
) {
2096 BT_PUT(sequence
->element_type
);
2099 sequence
->element_type
= element_type
;
2100 bt_get(sequence
->element_type
);
2106 const char *bt_ctf_field_type_sequence_get_length_field_name(
2107 struct bt_ctf_field_type
*type
)
2109 const char *ret
= NULL
;
2110 struct bt_ctf_field_type_sequence
*sequence
;
2112 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2116 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2118 ret
= sequence
->length_field_name
->str
;
2123 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2125 struct bt_ctf_field_type_string
*string
=
2126 g_new0(struct bt_ctf_field_type_string
, 1);
2132 string
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRING
;
2133 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2134 string
->encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2135 string
->parent
.alignment
= CHAR_BIT
;
2136 return &string
->parent
;
2139 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2140 struct bt_ctf_field_type
*type
)
2142 struct bt_ctf_field_type_string
*string
;
2143 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2145 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
)) {
2149 string
= container_of(type
, struct bt_ctf_field_type_string
,
2151 ret
= string
->encoding
;
2156 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2157 enum bt_ctf_string_encoding encoding
)
2160 struct bt_ctf_field_type_string
*string
;
2162 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
||
2163 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2164 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2169 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2170 string
->encoding
= encoding
;
2175 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2178 enum bt_ctf_field_type_id type_id
;
2186 ret
= (int) type
->alignment
;
2190 type_id
= bt_ctf_field_type_get_type_id(type
);
2192 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
2194 struct bt_ctf_field_type
*element
=
2195 bt_ctf_field_type_sequence_get_element_type(type
);
2202 ret
= bt_ctf_field_type_get_alignment(element
);
2206 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
2208 struct bt_ctf_field_type
*element
=
2209 bt_ctf_field_type_array_get_element_type(type
);
2216 ret
= bt_ctf_field_type_get_alignment(element
);
2220 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
2222 int64_t i
, element_count
;
2224 element_count
= bt_ctf_field_type_structure_get_field_count(
2226 if (element_count
< 0) {
2227 ret
= element_count
;
2231 for (i
= 0; i
< element_count
; i
++) {
2232 struct bt_ctf_field_type
*field
;
2233 int field_alignment
;
2235 ret
= bt_ctf_field_type_structure_get_field_by_index(
2236 type
, NULL
, &field
, i
);
2242 field_alignment
= bt_ctf_field_type_get_alignment(
2245 if (field_alignment
< 0) {
2246 ret
= field_alignment
;
2250 type
->alignment
= MAX(field_alignment
, type
->alignment
);
2252 ret
= (int) type
->alignment
;
2255 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
2259 ret
= (int) type
->alignment
;
2267 int is_power_of_two(unsigned int value
)
2269 return ((value
& (value
- 1)) == 0) && value
> 0;
2272 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2273 unsigned int alignment
)
2276 enum bt_ctf_field_type_id type_id
;
2278 /* Alignment must be a power of two */
2279 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2284 type_id
= bt_ctf_field_type_get_type_id(type
);
2285 if (type_id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
2290 if (type
->id
== BT_CTF_FIELD_TYPE_ID_STRING
&&
2291 alignment
!= CHAR_BIT
) {
2296 if (type_id
== BT_CTF_FIELD_TYPE_ID_VARIANT
||
2297 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
||
2298 type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2299 /* Setting an alignment on these types makes no sense */
2304 type
->alignment
= alignment
;
2310 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2311 struct bt_ctf_field_type
*type
)
2313 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2320 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2322 struct bt_ctf_field_type_integer
*integer
= container_of(
2323 type
, struct bt_ctf_field_type_integer
, parent
);
2324 ret
= integer
->user_byte_order
;
2327 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2329 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2330 type
, struct bt_ctf_field_type_enumeration
, parent
);
2331 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2334 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
2336 struct bt_ctf_field_type_floating_point
*floating_point
=
2338 struct bt_ctf_field_type_floating_point
,
2340 ret
= floating_point
->user_byte_order
;
2347 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2348 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2349 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2350 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2356 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2357 enum bt_ctf_byte_order byte_order
)
2361 if (!type
|| type
->frozen
) {
2366 if (byte_order
!= BT_CTF_BYTE_ORDER_NATIVE
&&
2367 byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
2368 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
2369 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
2374 if (set_byte_order_funcs
[type
->id
]) {
2375 set_byte_order_funcs
[type
->id
](type
, byte_order
);
2381 enum bt_ctf_field_type_id
bt_ctf_field_type_get_type_id(
2382 struct bt_ctf_field_type
*type
)
2385 return BT_CTF_FIELD_TYPE_ID_UNKNOWN
;
2391 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2393 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
;
2396 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2398 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_FLOAT
;
2401 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2403 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ENUM
;
2406 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2408 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRING
;
2411 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2413 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
;
2416 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2418 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ARRAY
;
2421 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2423 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2426 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2428 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_VARIANT
;
2431 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2436 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2442 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2452 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2453 struct bt_ctf_field_type_variant
*variant
,
2456 struct bt_ctf_field_type
*type
= NULL
;
2457 GQuark field_name_quark
;
2459 struct structure_field
*field_entry
;
2460 struct range_overlap_query query
= {
2461 .range_start
._signed
= tag_value
,
2462 .range_end
._signed
= tag_value
,
2467 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2469 if (!query
.overlaps
) {
2473 field_name_quark
= query
.mapping_name
;
2474 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2475 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2479 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2480 type
= field_entry
->type
;
2486 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2487 struct bt_ctf_field_type_variant
*variant
,
2490 struct bt_ctf_field_type
*type
= NULL
;
2491 GQuark field_name_quark
;
2493 struct structure_field
*field_entry
;
2494 struct range_overlap_query query
= {
2495 .range_start
._unsigned
= tag_value
,
2496 .range_end
._unsigned
= tag_value
,
2501 g_ptr_array_foreach(variant
->tag
->entries
,
2502 check_ranges_overlap_unsigned
,
2504 if (!query
.overlaps
) {
2508 field_name_quark
= query
.mapping_name
;
2509 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2510 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2514 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2515 type
= field_entry
->type
;
2521 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2522 struct metadata_context
*context
)
2526 if (!type
|| !context
) {
2531 /* Make sure field type is valid before serializing it */
2532 ret
= bt_ctf_field_type_validate(type
);
2538 ret
= type
->serialize(type
, context
);
2543 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2545 struct bt_ctf_field_type
*copy
= NULL
;
2551 copy
= type_copy_funcs
[type
->id
](type
);
2552 copy
->alignment
= type
->alignment
;
2558 int bt_ctf_field_type_structure_get_field_name_index(
2559 struct bt_ctf_field_type
*type
, const char *name
)
2564 struct bt_ctf_field_type_structure
*structure
;
2566 if (!type
|| !name
||
2567 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2572 name_quark
= g_quark_try_string(name
);
2578 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2580 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2581 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2591 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2592 struct bt_ctf_field_type
*field
, int index
)
2595 struct bt_ctf_field_type_structure
*structure
;
2597 if (!type
|| !field
||
2598 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2603 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2605 if (index
< 0 || index
>= structure
->fields
->len
) {
2611 bt_put(((struct structure_field
*)
2612 g_ptr_array_index(structure
->fields
, index
))->type
);
2613 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2620 int bt_ctf_field_type_variant_get_field_name_index(
2621 struct bt_ctf_field_type
*type
, const char *name
)
2626 struct bt_ctf_field_type_variant
*variant
;
2628 if (!type
|| !name
||
2629 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2634 name_quark
= g_quark_try_string(name
);
2640 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2642 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2643 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2653 int bt_ctf_field_type_sequence_set_length_field_path(
2654 struct bt_ctf_field_type
*type
,
2655 struct bt_ctf_field_path
*path
)
2658 struct bt_ctf_field_type_sequence
*sequence
;
2660 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2661 BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2666 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2669 BT_MOVE(sequence
->length_field_path
, path
);
2675 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2676 struct bt_ctf_field_path
*path
)
2679 struct bt_ctf_field_type_variant
*variant
;
2681 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2682 BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2687 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2690 BT_MOVE(variant
->tag_field_path
, path
);
2696 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2697 struct bt_ctf_field_type
*tag
)
2700 struct bt_ctf_field_type_variant
*variant
;
2702 if (!type
|| !tag
||
2703 bt_ctf_field_type_get_type_id(tag
) !=
2704 BT_CTF_FIELD_TYPE_ID_ENUM
) {
2709 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2713 bt_put(&variant
->tag
->parent
);
2715 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2722 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2723 struct bt_ctf_field_type
*field
, int index
)
2726 struct bt_ctf_field_type_variant
*variant
;
2728 if (!type
|| !field
||
2729 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2734 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2736 if (index
< 0 || index
>= variant
->fields
->len
) {
2742 bt_put(((struct structure_field
*)
2743 g_ptr_array_index(variant
->fields
, index
))->type
);
2744 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2751 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2753 struct bt_ctf_field_type_integer
*integer
=
2754 (struct bt_ctf_field_type_integer
*) type
;
2760 bt_put(integer
->mapped_clock
);
2765 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2767 struct bt_ctf_field_type_enumeration
*enumeration
=
2768 (struct bt_ctf_field_type_enumeration
*) type
;
2774 g_ptr_array_free(enumeration
->entries
, TRUE
);
2775 bt_put(enumeration
->container
);
2776 g_free(enumeration
);
2780 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2782 struct bt_ctf_field_type_floating_point
*floating_point
=
2783 (struct bt_ctf_field_type_floating_point
*) type
;
2789 g_free(floating_point
);
2793 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2795 struct bt_ctf_field_type_structure
*structure
=
2796 (struct bt_ctf_field_type_structure
*) type
;
2802 g_ptr_array_free(structure
->fields
, TRUE
);
2803 g_hash_table_destroy(structure
->field_name_to_index
);
2808 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2810 struct bt_ctf_field_type_variant
*variant
=
2811 (struct bt_ctf_field_type_variant
*) type
;
2817 g_ptr_array_free(variant
->fields
, TRUE
);
2818 g_hash_table_destroy(variant
->field_name_to_index
);
2819 g_string_free(variant
->tag_name
, TRUE
);
2820 bt_put(&variant
->tag
->parent
);
2821 BT_PUT(variant
->tag_field_path
);
2826 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2828 struct bt_ctf_field_type_array
*array
=
2829 (struct bt_ctf_field_type_array
*) type
;
2835 bt_put(array
->element_type
);
2840 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2842 struct bt_ctf_field_type_sequence
*sequence
=
2843 (struct bt_ctf_field_type_sequence
*) type
;
2849 bt_put(sequence
->element_type
);
2850 g_string_free(sequence
->length_field_name
, TRUE
);
2851 BT_PUT(sequence
->length_field_path
);
2856 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2858 struct bt_ctf_field_type_string
*string
=
2859 (struct bt_ctf_field_type_string
*) type
;
2869 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2875 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2877 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2878 type
, struct bt_ctf_field_type_integer
, parent
);
2880 if (integer_type
->mapped_clock
) {
2881 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2884 generic_field_type_freeze(type
);
2888 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2890 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2891 type
, struct bt_ctf_field_type_enumeration
, parent
);
2893 set_enumeration_range_overlap(type
);
2895 generic_field_type_freeze(type
);
2896 bt_ctf_field_type_freeze(enumeration_type
->container
);
2900 void freeze_structure_field(struct structure_field
*field
)
2902 bt_ctf_field_type_freeze(field
->type
);
2906 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2908 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2909 type
, struct bt_ctf_field_type_structure
, parent
);
2911 /* Cache the alignment */
2912 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2913 generic_field_type_freeze(type
);
2914 g_ptr_array_foreach(structure_type
->fields
,
2915 (GFunc
) freeze_structure_field
, NULL
);
2919 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2921 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2922 type
, struct bt_ctf_field_type_variant
, parent
);
2924 generic_field_type_freeze(type
);
2925 g_ptr_array_foreach(variant_type
->fields
,
2926 (GFunc
) freeze_structure_field
, NULL
);
2930 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2932 struct bt_ctf_field_type_array
*array_type
= container_of(
2933 type
, struct bt_ctf_field_type_array
, parent
);
2935 /* Cache the alignment */
2936 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2937 generic_field_type_freeze(type
);
2938 bt_ctf_field_type_freeze(array_type
->element_type
);
2942 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2944 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2945 type
, struct bt_ctf_field_type_sequence
, parent
);
2947 /* Cache the alignment */
2948 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2949 generic_field_type_freeze(type
);
2950 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2954 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2956 const char *encoding_string
;
2959 case BT_CTF_STRING_ENCODING_NONE
:
2960 encoding_string
= "none";
2962 case BT_CTF_STRING_ENCODING_ASCII
:
2963 encoding_string
= "ASCII";
2965 case BT_CTF_STRING_ENCODING_UTF8
:
2966 encoding_string
= "UTF8";
2969 encoding_string
= "unknown";
2973 return encoding_string
;
2977 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2979 const char *base_string
;
2982 case BT_CTF_INTEGER_BASE_DECIMAL
:
2983 base_string
= "decimal";
2985 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2986 base_string
= "hexadecimal";
2988 case BT_CTF_INTEGER_BASE_OCTAL
:
2989 base_string
= "octal";
2991 case BT_CTF_INTEGER_BASE_BINARY
:
2992 base_string
= "binary";
2995 base_string
= "unknown";
3003 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3004 struct metadata_context
*context
)
3006 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3007 struct bt_ctf_field_type_integer
, parent
);
3010 g_string_append_printf(context
->string
,
3011 "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s",
3012 integer
->size
, type
->alignment
,
3013 (integer
->is_signed
? "true" : "false"),
3014 get_encoding_string(integer
->encoding
),
3015 get_integer_base_string(integer
->base
),
3016 get_byte_order_string(integer
->user_byte_order
));
3017 if (integer
->mapped_clock
) {
3018 const char *clock_name
= bt_ctf_clock_class_get_name(
3019 integer
->mapped_clock
);
3026 g_string_append_printf(context
->string
,
3027 "; map = clock.%s.value", clock_name
);
3030 g_string_append(context
->string
, "; }");
3036 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3037 struct metadata_context
*context
)
3041 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3042 struct bt_ctf_field_type_enumeration
, parent
);
3043 struct bt_ctf_field_type
*container_type
;
3044 int container_signed
;
3046 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3047 if (!container_type
) {
3052 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3053 if (container_signed
< 0) {
3054 ret
= container_signed
;
3055 goto error_put_container_type
;
3058 g_string_append(context
->string
, "enum : ");
3059 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3061 goto error_put_container_type
;
3064 g_string_append(context
->string
, " { ");
3065 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3066 struct enumeration_mapping
*mapping
=
3067 enumeration
->entries
->pdata
[entry
];
3069 if (container_signed
) {
3070 if (mapping
->range_start
._signed
==
3071 mapping
->range_end
._signed
) {
3072 g_string_append_printf(context
->string
,
3073 "\"%s\" = %" PRId64
,
3074 g_quark_to_string(mapping
->string
),
3075 mapping
->range_start
._signed
);
3077 g_string_append_printf(context
->string
,
3078 "\"%s\" = %" PRId64
" ... %" PRId64
,
3079 g_quark_to_string(mapping
->string
),
3080 mapping
->range_start
._signed
,
3081 mapping
->range_end
._signed
);
3084 if (mapping
->range_start
._unsigned
==
3085 mapping
->range_end
._unsigned
) {
3086 g_string_append_printf(context
->string
,
3087 "\"%s\" = %" PRIu64
,
3088 g_quark_to_string(mapping
->string
),
3089 mapping
->range_start
._unsigned
);
3091 g_string_append_printf(context
->string
,
3092 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3093 g_quark_to_string(mapping
->string
),
3094 mapping
->range_start
._unsigned
,
3095 mapping
->range_end
._unsigned
);
3099 g_string_append(context
->string
,
3100 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3104 if (context
->field_name
->len
) {
3105 g_string_append_printf(context
->string
, " %s",
3106 context
->field_name
->str
);
3107 g_string_assign(context
->field_name
, "");
3109 error_put_container_type
:
3110 bt_put(container_type
);
3116 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3117 struct metadata_context
*context
)
3119 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3120 type
, struct bt_ctf_field_type_floating_point
, parent
);
3122 g_string_append_printf(context
->string
,
3123 "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }",
3124 floating_point
->exp_dig
,
3125 floating_point
->mant_dig
,
3126 get_byte_order_string(floating_point
->user_byte_order
),
3132 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3133 struct metadata_context
*context
)
3136 unsigned int indent
;
3138 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3139 struct bt_ctf_field_type_structure
, parent
);
3140 GString
*structure_field_name
= context
->field_name
;
3142 context
->field_name
= g_string_new("");
3144 context
->current_indentation_level
++;
3145 g_string_append(context
->string
, "struct {\n");
3147 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3148 struct structure_field
*field
;
3150 for (indent
= 0; indent
< context
->current_indentation_level
;
3152 g_string_append_c(context
->string
, '\t');
3155 field
= structure
->fields
->pdata
[i
];
3156 g_string_assign(context
->field_name
,
3157 g_quark_to_string(field
->name
));
3158 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3163 if (context
->field_name
->len
) {
3164 g_string_append_printf(context
->string
, " %s",
3165 context
->field_name
->str
);
3167 g_string_append(context
->string
, ";\n");
3170 context
->current_indentation_level
--;
3171 for (indent
= 0; indent
< context
->current_indentation_level
;
3173 g_string_append_c(context
->string
, '\t');
3176 g_string_append_printf(context
->string
, "} align(%u)",
3179 g_string_free(context
->field_name
, TRUE
);
3180 context
->field_name
= structure_field_name
;
3185 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3186 struct metadata_context
*context
)
3189 unsigned int indent
;
3191 struct bt_ctf_field_type_variant
*variant
= container_of(
3192 type
, struct bt_ctf_field_type_variant
, parent
);
3193 GString
*variant_field_name
= context
->field_name
;
3195 context
->field_name
= g_string_new("");
3196 if (variant
->tag_name
->len
> 0) {
3197 g_string_append_printf(context
->string
,
3198 "variant <%s> {\n", variant
->tag_name
->str
);
3200 g_string_append(context
->string
, "variant {\n");
3203 context
->current_indentation_level
++;
3204 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3205 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3207 g_string_assign(context
->field_name
,
3208 g_quark_to_string(field
->name
));
3209 for (indent
= 0; indent
< context
->current_indentation_level
;
3211 g_string_append_c(context
->string
, '\t');
3214 g_string_assign(context
->field_name
,
3215 g_quark_to_string(field
->name
));
3216 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3221 if (context
->field_name
->len
) {
3222 g_string_append_printf(context
->string
, " %s;",
3223 context
->field_name
->str
);
3226 g_string_append_c(context
->string
, '\n');
3229 context
->current_indentation_level
--;
3230 for (indent
= 0; indent
< context
->current_indentation_level
;
3232 g_string_append_c(context
->string
, '\t');
3235 g_string_append(context
->string
, "}");
3237 g_string_free(context
->field_name
, TRUE
);
3238 context
->field_name
= variant_field_name
;
3243 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3244 struct metadata_context
*context
)
3247 struct bt_ctf_field_type_array
*array
= container_of(type
,
3248 struct bt_ctf_field_type_array
, parent
);
3250 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3255 if (context
->field_name
->len
) {
3256 g_string_append_printf(context
->string
, " %s[%u]",
3257 context
->field_name
->str
, array
->length
);
3258 g_string_assign(context
->field_name
, "");
3260 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3267 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3268 struct metadata_context
*context
)
3271 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3272 type
, struct bt_ctf_field_type_sequence
, parent
);
3274 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3279 if (context
->field_name
->len
) {
3280 g_string_append_printf(context
->string
, " %s[%s]",
3281 context
->field_name
->str
,
3282 sequence
->length_field_name
->str
);
3283 g_string_assign(context
->field_name
, "");
3285 g_string_append_printf(context
->string
, "[%s]",
3286 sequence
->length_field_name
->str
);
3293 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3294 struct metadata_context
*context
)
3296 struct bt_ctf_field_type_string
*string
= container_of(
3297 type
, struct bt_ctf_field_type_string
, parent
);
3299 g_string_append_printf(context
->string
,
3300 "string { encoding = %s; }",
3301 get_encoding_string(string
->encoding
));
3306 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3307 enum bt_ctf_byte_order byte_order
)
3309 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3310 struct bt_ctf_field_type_integer
, parent
);
3312 integer_type
->user_byte_order
= byte_order
;
3316 void bt_ctf_field_type_enumeration_set_byte_order(
3317 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3319 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3320 struct bt_ctf_field_type_enumeration
, parent
);
3322 /* Safe to assume that container is an integer */
3323 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3328 void bt_ctf_field_type_floating_point_set_byte_order(
3329 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3331 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3332 container_of(type
, struct bt_ctf_field_type_floating_point
,
3335 floating_point_type
->user_byte_order
= byte_order
;
3339 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3340 enum bt_ctf_byte_order byte_order
)
3343 struct bt_ctf_field_type_structure
*structure_type
=
3344 container_of(type
, struct bt_ctf_field_type_structure
,
3347 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3348 struct structure_field
*field
= g_ptr_array_index(
3349 structure_type
->fields
, i
);
3350 struct bt_ctf_field_type
*field_type
= field
->type
;
3352 if (set_byte_order_funcs
[field_type
->id
]) {
3353 set_byte_order_funcs
[field_type
->id
](
3354 field_type
, byte_order
);
3360 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3361 enum bt_ctf_byte_order byte_order
)
3364 struct bt_ctf_field_type_variant
*variant_type
=
3365 container_of(type
, struct bt_ctf_field_type_variant
,
3368 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3369 struct structure_field
*field
= g_ptr_array_index(
3370 variant_type
->fields
, i
);
3371 struct bt_ctf_field_type
*field_type
= field
->type
;
3373 if (set_byte_order_funcs
[field_type
->id
]) {
3374 set_byte_order_funcs
[field_type
->id
](
3375 field_type
, byte_order
);
3381 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3382 enum bt_ctf_byte_order byte_order
)
3384 struct bt_ctf_field_type_array
*array_type
=
3385 container_of(type
, struct bt_ctf_field_type_array
,
3388 if (set_byte_order_funcs
[array_type
->element_type
->id
]) {
3389 set_byte_order_funcs
[array_type
->element_type
->id
](
3390 array_type
->element_type
, byte_order
);
3395 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3396 enum bt_ctf_byte_order byte_order
)
3398 struct bt_ctf_field_type_sequence
*sequence_type
=
3399 container_of(type
, struct bt_ctf_field_type_sequence
,
3402 if (set_byte_order_funcs
[
3403 sequence_type
->element_type
->id
]) {
3404 set_byte_order_funcs
[
3405 sequence_type
->element_type
->id
](
3406 sequence_type
->element_type
, byte_order
);
3411 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3412 struct bt_ctf_field_type
*type
)
3414 struct bt_ctf_field_type
*copy
;
3415 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3417 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3418 copy
= bt_ctf_field_type_integer_create(integer
->size
);
3423 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3425 copy_integer
->mapped_clock
= bt_get(integer
->mapped_clock
);
3426 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3427 copy_integer
->is_signed
= integer
->is_signed
;
3428 copy_integer
->size
= integer
->size
;
3429 copy_integer
->base
= integer
->base
;
3430 copy_integer
->encoding
= integer
->encoding
;
3437 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3438 struct bt_ctf_field_type
*type
)
3441 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3442 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3444 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3447 /* Copy the source enumeration's container */
3448 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3449 if (!copy_container
) {
3453 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3457 copy_enumeration
= container_of(copy
,
3458 struct bt_ctf_field_type_enumeration
, parent
);
3460 /* Copy all enumaration entries */
3461 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3462 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3463 enumeration
->entries
, i
);
3464 struct enumeration_mapping
*copy_mapping
= g_new0(
3465 struct enumeration_mapping
, 1);
3467 if (!copy_mapping
) {
3471 *copy_mapping
= *mapping
;
3472 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3476 bt_put(copy_container
);
3479 bt_put(copy_container
);
3485 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3486 struct bt_ctf_field_type
*type
)
3488 struct bt_ctf_field_type
*copy
;
3489 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3491 floating_point
= container_of(type
,
3492 struct bt_ctf_field_type_floating_point
, parent
);
3493 copy
= bt_ctf_field_type_floating_point_create();
3498 copy_float
= container_of(copy
,
3499 struct bt_ctf_field_type_floating_point
, parent
);
3500 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3501 copy_float
->exp_dig
= floating_point
->exp_dig
;
3502 copy_float
->mant_dig
= floating_point
->mant_dig
;
3508 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3509 struct bt_ctf_field_type
*type
)
3512 GHashTableIter iter
;
3513 gpointer key
, value
;
3514 struct bt_ctf_field_type
*copy
;
3515 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3517 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3519 copy
= bt_ctf_field_type_structure_create();
3524 copy_structure
= container_of(copy
,
3525 struct bt_ctf_field_type_structure
, parent
);
3527 /* Copy field_name_to_index */
3528 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3529 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3530 g_hash_table_insert(copy_structure
->field_name_to_index
,
3534 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3535 struct structure_field
*entry
, *copy_entry
;
3536 struct bt_ctf_field_type
*copy_field
;
3538 copy_entry
= g_new0(struct structure_field
, 1);
3543 entry
= g_ptr_array_index(structure
->fields
, i
);
3544 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3550 copy_entry
->name
= entry
->name
;
3551 copy_entry
->type
= copy_field
;
3552 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3563 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3564 struct bt_ctf_field_type
*type
)
3567 GHashTableIter iter
;
3568 gpointer key
, value
;
3569 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3570 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3572 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3575 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3581 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3582 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3587 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3590 /* Copy field_name_to_index */
3591 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3592 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3593 g_hash_table_insert(copy_variant
->field_name_to_index
,
3597 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3598 struct structure_field
*entry
, *copy_entry
;
3599 struct bt_ctf_field_type
*copy_field
;
3601 copy_entry
= g_new0(struct structure_field
, 1);
3606 entry
= g_ptr_array_index(variant
->fields
, i
);
3607 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3613 copy_entry
->name
= entry
->name
;
3614 copy_entry
->type
= copy_field
;
3615 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3618 if (variant
->tag_field_path
) {
3619 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3620 variant
->tag_field_path
);
3621 if (!copy_variant
->tag_field_path
) {
3635 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3636 struct bt_ctf_field_type
*type
)
3638 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3639 struct bt_ctf_field_type_array
*array
;
3641 array
= container_of(type
, struct bt_ctf_field_type_array
,
3643 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3644 if (!copy_element
) {
3648 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3653 bt_put(copy_element
);
3658 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3659 struct bt_ctf_field_type
*type
)
3661 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3662 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3664 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3666 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3667 if (!copy_element
) {
3671 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3672 sequence
->length_field_name
->len
?
3673 sequence
->length_field_name
->str
: NULL
);
3678 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3680 if (sequence
->length_field_path
) {
3681 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3682 sequence
->length_field_path
);
3683 if (!copy_sequence
->length_field_path
) {
3688 bt_put(copy_element
);
3696 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3697 struct bt_ctf_field_type
*type
)
3699 struct bt_ctf_field_type
*copy
;
3700 struct bt_ctf_field_type_string
*string
;
3702 copy
= bt_ctf_field_type_string_create();
3707 string
= container_of(type
, struct bt_ctf_field_type_string
,
3714 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3715 struct bt_ctf_field_type
*type_b
)
3718 struct bt_ctf_field_type_integer
*int_type_a
;
3719 struct bt_ctf_field_type_integer
*int_type_b
;
3721 int_type_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3723 int_type_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3727 if (int_type_a
->size
!= int_type_b
->size
) {
3732 if (int_type_a
->user_byte_order
!= int_type_b
->user_byte_order
) {
3737 if (int_type_a
->is_signed
!= int_type_b
->is_signed
) {
3742 if (int_type_a
->base
!= int_type_b
->base
) {
3747 if (int_type_a
->encoding
!= int_type_b
->encoding
) {
3752 if (int_type_a
->mapped_clock
!= int_type_b
->mapped_clock
) {
3764 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3765 struct bt_ctf_field_type
*type_b
)
3768 struct bt_ctf_field_type_floating_point
*float_a
;
3769 struct bt_ctf_field_type_floating_point
*float_b
;
3771 float_a
= container_of(type_a
,
3772 struct bt_ctf_field_type_floating_point
, parent
);
3773 float_b
= container_of(type_b
,
3774 struct bt_ctf_field_type_floating_point
, parent
);
3777 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3781 /* Exponent length */
3782 if (float_a
->exp_dig
!= float_b
->exp_dig
) {
3786 /* Mantissa length */
3787 if (float_a
->mant_dig
!= float_b
->mant_dig
) {
3799 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3800 struct enumeration_mapping
*mapping_b
)
3805 if (mapping_a
->string
!= mapping_b
->string
) {
3810 if (mapping_a
->range_start
._unsigned
!=
3811 mapping_b
->range_start
._unsigned
) {
3816 if (mapping_a
->range_end
._unsigned
!=
3817 mapping_b
->range_end
._unsigned
) {
3829 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3830 struct bt_ctf_field_type
*type_b
)
3834 struct bt_ctf_field_type_enumeration
*enum_a
;
3835 struct bt_ctf_field_type_enumeration
*enum_b
;
3837 enum_a
= container_of(type_a
,
3838 struct bt_ctf_field_type_enumeration
, parent
);
3839 enum_b
= container_of(type_b
,
3840 struct bt_ctf_field_type_enumeration
, parent
);
3842 /* Container field type */
3843 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3851 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3855 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3856 struct enumeration_mapping
*mapping_a
=
3857 g_ptr_array_index(enum_a
->entries
, i
);
3858 struct enumeration_mapping
*mapping_b
=
3859 g_ptr_array_index(enum_b
->entries
, i
);
3861 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3874 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3875 struct bt_ctf_field_type
*type_b
)
3878 struct bt_ctf_field_type_string
*string_a
;
3879 struct bt_ctf_field_type_string
*string_b
;
3881 string_a
= container_of(type_a
,
3882 struct bt_ctf_field_type_string
, parent
);
3883 string_b
= container_of(type_b
,
3884 struct bt_ctf_field_type_string
, parent
);
3887 if (string_a
->encoding
!= string_b
->encoding
) {
3899 int compare_structure_fields(struct structure_field
*field_a
,
3900 struct structure_field
*field_b
)
3905 if (field_a
->name
!= field_b
->name
) {
3910 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3917 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3918 struct bt_ctf_field_type
*type_b
)
3922 struct bt_ctf_field_type_structure
*struct_a
;
3923 struct bt_ctf_field_type_structure
*struct_b
;
3925 struct_a
= container_of(type_a
,
3926 struct bt_ctf_field_type_structure
, parent
);
3927 struct_b
= container_of(type_b
,
3928 struct bt_ctf_field_type_structure
, parent
);
3931 if (bt_ctf_field_type_get_alignment(type_a
) !=
3932 bt_ctf_field_type_get_alignment(type_b
)) {
3937 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3941 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3942 struct structure_field
*field_a
=
3943 g_ptr_array_index(struct_a
->fields
, i
);
3944 struct structure_field
*field_b
=
3945 g_ptr_array_index(struct_b
->fields
, i
);
3947 ret
= compare_structure_fields(field_a
, field_b
);
3961 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3962 struct bt_ctf_field_type
*type_b
)
3966 struct bt_ctf_field_type_variant
*variant_a
;
3967 struct bt_ctf_field_type_variant
*variant_b
;
3969 variant_a
= container_of(type_a
,
3970 struct bt_ctf_field_type_variant
, parent
);
3971 variant_b
= container_of(type_b
,
3972 struct bt_ctf_field_type_variant
, parent
);
3975 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3980 ret
= bt_ctf_field_type_compare(
3981 (struct bt_ctf_field_type
*) variant_a
->tag
,
3982 (struct bt_ctf_field_type
*) variant_b
->tag
);
3990 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3994 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3995 struct structure_field
*field_a
=
3996 g_ptr_array_index(variant_a
->fields
, i
);
3997 struct structure_field
*field_b
=
3998 g_ptr_array_index(variant_b
->fields
, i
);
4000 ret
= compare_structure_fields(field_a
, field_b
);
4014 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4015 struct bt_ctf_field_type
*type_b
)
4018 struct bt_ctf_field_type_array
*array_a
;
4019 struct bt_ctf_field_type_array
*array_b
;
4021 array_a
= container_of(type_a
,
4022 struct bt_ctf_field_type_array
, parent
);
4023 array_b
= container_of(type_b
,
4024 struct bt_ctf_field_type_array
, parent
);
4027 if (array_a
->length
!= array_b
->length
) {
4032 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4033 array_b
->element_type
);
4040 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4041 struct bt_ctf_field_type
*type_b
)
4044 struct bt_ctf_field_type_sequence
*sequence_a
;
4045 struct bt_ctf_field_type_sequence
*sequence_b
;
4047 sequence_a
= container_of(type_a
,
4048 struct bt_ctf_field_type_sequence
, parent
);
4049 sequence_b
= container_of(type_b
,
4050 struct bt_ctf_field_type_sequence
, parent
);
4053 if (strcmp(sequence_a
->length_field_name
->str
,
4054 sequence_b
->length_field_name
->str
)) {
4059 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4060 sequence_b
->element_type
);
4066 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4067 struct bt_ctf_field_type
*type_b
)
4071 if (type_a
== type_b
) {
4072 /* Same reference: equal (even if both are NULL) */
4077 if (!type_a
|| !type_b
) {
4082 if (type_a
->id
!= type_b
->id
) {
4083 /* Different type IDs */
4087 if (type_a
->id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
4088 /* Both have unknown type IDs */
4092 ret
= type_compare_funcs
[type_a
->id
](type_a
, type_b
);
4099 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4101 int64_t field_count
= -1;
4102 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4105 case CTF_TYPE_STRUCT
:
4107 bt_ctf_field_type_structure_get_field_count(field_type
);
4109 case CTF_TYPE_VARIANT
:
4111 bt_ctf_field_type_variant_get_field_count(field_type
);
4113 case CTF_TYPE_ARRAY
:
4114 case CTF_TYPE_SEQUENCE
:
4116 * Array and sequence types always contain a single member
4117 * (the element type).
4129 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4130 struct bt_ctf_field_type
*field_type
, int index
)
4132 struct bt_ctf_field_type
*field
= NULL
;
4133 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4136 case CTF_TYPE_STRUCT
:
4137 bt_ctf_field_type_structure_get_field_by_index(field_type
,
4138 NULL
, &field
, index
);
4140 case CTF_TYPE_VARIANT
:
4142 int ret
= bt_ctf_field_type_variant_get_field_by_index(
4143 field_type
, NULL
, &field
, index
);
4150 case CTF_TYPE_ARRAY
:
4151 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4153 case CTF_TYPE_SEQUENCE
:
4154 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4164 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4167 int field_index
= -1;
4168 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4171 case CTF_TYPE_STRUCT
:
4172 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4175 case CTF_TYPE_VARIANT
:
4176 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4186 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4187 struct bt_ctf_field_type
*type
)
4189 struct bt_ctf_field_path
*field_path
= NULL
;
4190 struct bt_ctf_field_type_variant
*variant
;
4192 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4196 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4198 field_path
= bt_get(variant
->tag_field_path
);
4203 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4204 struct bt_ctf_field_type
*type
)
4206 struct bt_ctf_field_path
*field_path
= NULL
;
4207 struct bt_ctf_field_type_sequence
*sequence
;
4209 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4213 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4215 field_path
= bt_get(sequence
->length_field_path
);