4 * Babeltrace CTF IR - Event Types
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/field-path-internal.h>
31 #include <babeltrace/ctf-ir/utils.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/clock-class.h>
34 #include <babeltrace/ctf-ir/clock-class-internal.h>
35 #include <babeltrace/ctf-writer/writer-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/endian-internal.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
81 bt_ctf_field_type_enumeration_destroy
,
82 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
83 bt_ctf_field_type_floating_point_destroy
,
84 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
104 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
107 type_freeze_func
const type_freeze_funcs
[] = {
108 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
109 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
110 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
111 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
112 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
113 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
114 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
115 [BT_CTF_FIELD_TYPE_ID_STRING
] = generic_field_type_freeze
,
119 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_floating_point_serialize(
126 struct bt_ctf_field_type
*, struct metadata_context
*);
128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
144 type_serialize_func
const type_serialize_funcs
[] = {
145 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
146 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
147 bt_ctf_field_type_enumeration_serialize
,
148 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
149 bt_ctf_field_type_floating_point_serialize
,
150 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
151 bt_ctf_field_type_structure_serialize
,
152 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
153 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
154 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
155 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
159 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
160 enum bt_ctf_byte_order byte_order
);
162 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
163 enum bt_ctf_byte_order byte_order
);
165 void bt_ctf_field_type_floating_point_set_byte_order(
166 struct bt_ctf_field_type
*, enum bt_ctf_byte_order byte_order
);
168 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
169 enum bt_ctf_byte_order byte_order
);
171 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
172 enum bt_ctf_byte_order byte_order
);
174 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
175 enum bt_ctf_byte_order byte_order
);
177 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
178 enum bt_ctf_byte_order byte_order
);
181 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
182 enum bt_ctf_byte_order
) = {
183 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
184 [BT_CTF_FIELD_TYPE_ID_ENUM
] =
185 bt_ctf_field_type_enumeration_set_byte_order
,
186 [BT_CTF_FIELD_TYPE_ID_FLOAT
] =
187 bt_ctf_field_type_floating_point_set_byte_order
,
188 [BT_CTF_FIELD_TYPE_ID_STRUCT
] =
189 bt_ctf_field_type_structure_set_byte_order
,
190 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
191 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
192 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
193 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
197 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
198 struct bt_ctf_field_type
*);
200 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
201 struct bt_ctf_field_type
*);
203 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
204 struct bt_ctf_field_type
*);
206 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
207 struct bt_ctf_field_type
*);
209 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
210 struct bt_ctf_field_type
*);
212 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
213 struct bt_ctf_field_type
*);
215 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
216 struct bt_ctf_field_type
*);
218 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
219 struct bt_ctf_field_type
*);
222 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
223 struct bt_ctf_field_type
*) = {
224 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
225 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
226 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
227 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
228 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
229 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
230 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
231 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
235 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
236 struct bt_ctf_field_type
*);
238 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
239 struct bt_ctf_field_type
*);
241 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
242 struct bt_ctf_field_type
*);
244 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
245 struct bt_ctf_field_type
*);
247 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
248 struct bt_ctf_field_type
*);
250 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
251 struct bt_ctf_field_type
*);
253 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
254 struct bt_ctf_field_type
*);
256 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
257 struct bt_ctf_field_type
*);
260 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
261 struct bt_ctf_field_type
*) = {
262 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
263 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
264 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
265 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
266 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
267 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
268 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
269 [BT_CTF_FIELD_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
273 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
275 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
277 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
279 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
281 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
283 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
286 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
287 [BT_CTF_FIELD_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
288 [BT_CTF_FIELD_TYPE_ID_FLOAT
] = NULL
,
289 [BT_CTF_FIELD_TYPE_ID_STRING
] = NULL
,
290 [BT_CTF_FIELD_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
291 [BT_CTF_FIELD_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
292 [BT_CTF_FIELD_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
293 [BT_CTF_FIELD_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
294 [BT_CTF_FIELD_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
298 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
304 void destroy_structure_field(struct structure_field
*field
)
311 void check_ranges_overlap(gpointer element
, gpointer query
)
313 struct enumeration_mapping
*mapping
= element
;
314 struct range_overlap_query
*overlap_query
= query
;
316 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
317 && overlap_query
->range_start
._signed
<=
318 mapping
->range_end
._signed
) {
319 overlap_query
->overlaps
= 1;
320 overlap_query
->mapping_name
= mapping
->string
;
323 overlap_query
->overlaps
|=
324 mapping
->string
== overlap_query
->mapping_name
;
328 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
330 struct enumeration_mapping
*mapping
= element
;
331 struct range_overlap_query
*overlap_query
= query
;
333 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
334 && overlap_query
->range_start
._unsigned
<=
335 mapping
->range_end
._unsigned
) {
336 overlap_query
->overlaps
= 1;
337 overlap_query
->mapping_name
= mapping
->string
;
340 overlap_query
->overlaps
|=
341 mapping
->string
== overlap_query
->mapping_name
;
345 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
346 struct enumeration_mapping
**b
)
348 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
352 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
353 struct enumeration_mapping
**b
)
355 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
359 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, bool init_bo
)
361 assert(type
&& (type
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
) &&
362 (type
->id
< BT_CTF_NR_TYPE_IDS
));
364 bt_object_init(type
, bt_ctf_field_type_destroy
);
365 type
->freeze
= type_freeze_funcs
[type
->id
];
366 type
->serialize
= type_serialize_funcs
[type
->id
];
369 int ret
= bt_ctf_field_type_set_byte_order(type
,
370 BT_CTF_BYTE_ORDER_NATIVE
);
378 int add_structure_field(GPtrArray
*fields
,
379 GHashTable
*field_name_to_index
,
380 struct bt_ctf_field_type
*field_type
,
381 const char *field_name
)
384 GQuark name_quark
= g_quark_from_string(field_name
);
385 struct structure_field
*field
;
387 /* Make sure structure does not contain a field of the same name */
388 if (g_hash_table_lookup_extended(field_name_to_index
,
389 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
394 field
= g_new0(struct structure_field
, 1);
401 field
->name
= name_quark
;
402 field
->type
= field_type
;
403 g_hash_table_insert(field_name_to_index
,
404 GUINT_TO_POINTER(name_quark
),
405 GUINT_TO_POINTER(fields
->len
));
406 g_ptr_array_add(fields
, field
);
412 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
414 struct bt_ctf_field_type
*type
;
415 enum bt_ctf_field_type_id type_id
;
417 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
419 if (type_id
<= BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
420 type_id
>= BT_CTF_NR_TYPE_IDS
) {
424 type_destroy_funcs
[type_id
](type
);
428 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
432 struct bt_ctf_field_type_integer
*integer
=
433 container_of(type
, struct bt_ctf_field_type_integer
,
436 if (integer
->mapped_clock
&& integer
->is_signed
) {
446 struct enumeration_mapping
*get_enumeration_mapping(
447 struct bt_ctf_field_type
*type
, int index
)
449 struct enumeration_mapping
*mapping
= NULL
;
450 struct bt_ctf_field_type_enumeration
*enumeration
;
452 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
454 if (index
>= enumeration
->entries
->len
) {
458 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
464 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
465 * Only used when freezing an enumeration.
468 void set_enumeration_range_overlap(
469 struct bt_ctf_field_type
*type
)
472 struct bt_ctf_field_type
*container_type
;
473 struct bt_ctf_field_type_enumeration
*enumeration_type
;
476 enumeration_type
= container_of(type
,
477 struct bt_ctf_field_type_enumeration
, parent
);
479 len
= enumeration_type
->entries
->len
;
480 container_type
= enumeration_type
->container
;
481 is_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
483 for (i
= 0; i
< len
; i
++) {
484 for (j
= i
+ 1; j
< len
; j
++) {
485 struct enumeration_mapping
*mapping
[2];
487 mapping
[0] = get_enumeration_mapping(type
, i
);
488 mapping
[1] = get_enumeration_mapping(type
, j
);
490 if (mapping
[0]->range_start
._signed
491 <= mapping
[1]->range_end
._signed
492 && mapping
[0]->range_end
._signed
493 >= mapping
[1]->range_start
._signed
) {
494 enumeration_type
->has_overlapping_ranges
= true;
498 if (mapping
[0]->range_start
._unsigned
499 <= mapping
[1]->range_end
._unsigned
500 && mapping
[0]->range_end
._unsigned
501 >= mapping
[1]->range_start
._unsigned
) {
502 enumeration_type
->has_overlapping_ranges
= true;
511 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
515 struct bt_ctf_field_type_enumeration
*enumeration
=
516 container_of(type
, struct bt_ctf_field_type_enumeration
,
518 struct bt_ctf_field_type
*container_type
=
519 bt_ctf_field_type_enumeration_get_container_type(type
);
521 if (!container_type
) {
526 ret
= bt_ctf_field_type_validate(container_type
);
531 /* Ensure enum has entries */
532 ret
= enumeration
->entries
->len
? 0 : -1;
535 BT_PUT(container_type
);
540 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
543 struct bt_ctf_field_type
*element_type
= NULL
;
544 struct bt_ctf_field_type_sequence
*sequence
=
545 container_of(type
, struct bt_ctf_field_type_sequence
,
548 /* Length field name should be set at this point */
549 if (sequence
->length_field_name
->len
== 0) {
554 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
560 ret
= bt_ctf_field_type_validate(element_type
);
563 BT_PUT(element_type
);
569 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
572 struct bt_ctf_field_type
*element_type
= NULL
;
574 element_type
= bt_ctf_field_type_array_get_element_type(type
);
580 ret
= bt_ctf_field_type_validate(element_type
);
583 BT_PUT(element_type
);
589 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
592 struct bt_ctf_field_type
*child_type
= NULL
;
593 int64_t field_count
= bt_ctf_field_type_structure_get_field_count(type
);
596 if (field_count
< 0) {
601 for (i
= 0; i
< field_count
; ++i
) {
602 ret
= bt_ctf_field_type_structure_get_field_by_index(type
,
603 NULL
, &child_type
, i
);
608 ret
= bt_ctf_field_type_validate(child_type
);
623 bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
624 struct bt_ctf_field_type_enumeration
*enumeration_type
)
626 if (!enumeration_type
->parent
.frozen
) {
627 set_enumeration_range_overlap(&enumeration_type
->parent
);
629 return enumeration_type
->has_overlapping_ranges
;
633 int bt_ctf_field_type_enumeration_get_mapping_name(
634 struct bt_ctf_field_type
*enum_field_type
,
636 const char **mapping_name
)
639 struct enumeration_mapping
*mapping
;
641 if (!enum_field_type
|| index
< 0) {
646 mapping
= get_enumeration_mapping(enum_field_type
, index
);
653 *mapping_name
= g_quark_to_string(mapping
->string
);
660 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
664 struct bt_ctf_field_type
*child_type
= NULL
;
665 struct bt_ctf_field_type_variant
*variant
=
666 container_of(type
, struct bt_ctf_field_type_variant
,
669 int64_t tag_mappings_count
;
671 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
676 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
683 bt_ctf_field_type_enumeration_get_mapping_count(
684 (struct bt_ctf_field_type
*) variant
->tag
);
686 if (tag_mappings_count
!= variant
->fields
->len
) {
691 for (i
= 0; i
< tag_mappings_count
; ++i
) {
693 struct bt_ctf_field_type
*ft
;
695 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
696 (struct bt_ctf_field_type
*) variant
->tag
,
706 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
716 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
717 if (field_count
< 0) {
722 for (i
= 0; i
< field_count
; ++i
) {
723 ret
= bt_ctf_field_type_variant_get_field_by_index(type
,
724 NULL
, &child_type
, i
);
729 ret
= bt_ctf_field_type_validate(child_type
);
744 * This function validates a given field type without considering
745 * where this field type is located. It only validates the properties
746 * of the given field type and the properties of its children if
750 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
753 enum bt_ctf_field_type_id id
= bt_ctf_field_type_get_type_id(type
);
761 /* Already marked as valid */
765 if (type_validate_funcs
[id
]) {
766 ret
= type_validate_funcs
[id
](type
);
769 if (!ret
&& type
->frozen
) {
770 /* Field type is valid */
778 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
780 struct bt_ctf_field_type_integer
*integer
=
781 g_new0(struct bt_ctf_field_type_integer
, 1);
783 if (!integer
|| size
== 0 || size
> 64) {
787 integer
->parent
.id
= BT_CTF_FIELD_TYPE_ID_INTEGER
;
788 integer
->size
= size
;
789 integer
->base
= BT_CTF_INTEGER_BASE_DECIMAL
;
790 integer
->encoding
= BT_CTF_STRING_ENCODING_NONE
;
791 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
792 return &integer
->parent
;
795 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
798 struct bt_ctf_field_type_integer
*integer
;
800 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
805 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
806 ret
= (int) integer
->size
;
811 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
814 struct bt_ctf_field_type_integer
*integer
;
816 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
821 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
822 ret
= integer
->is_signed
;
827 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
831 struct bt_ctf_field_type_integer
*integer
;
833 if (!type
|| type
->frozen
||
834 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
839 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
840 integer
->is_signed
= !!is_signed
;
845 int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type
*type
,
849 struct bt_ctf_field_type_integer
*integer
;
851 if (!type
|| type
->frozen
||
852 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
||
853 !size
|| size
> 64) {
858 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
859 integer
->size
= size
;
864 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
865 struct bt_ctf_field_type
*type
)
867 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
868 struct bt_ctf_field_type_integer
*integer
;
870 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
874 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
880 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
881 enum bt_ctf_integer_base base
)
885 if (!type
|| type
->frozen
||
886 type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
892 case BT_CTF_INTEGER_BASE_BINARY
:
893 case BT_CTF_INTEGER_BASE_OCTAL
:
894 case BT_CTF_INTEGER_BASE_DECIMAL
:
895 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
897 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
898 struct bt_ctf_field_type_integer
, parent
);
899 integer
->base
= base
;
909 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
910 struct bt_ctf_field_type
*type
)
912 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
913 struct bt_ctf_field_type_integer
*integer
;
915 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
919 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
920 ret
= integer
->encoding
;
925 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
926 enum bt_ctf_string_encoding encoding
)
929 struct bt_ctf_field_type_integer
*integer
;
931 if (!type
|| type
->frozen
||
932 (type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) ||
933 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
934 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
939 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
940 integer
->encoding
= encoding
;
945 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
946 struct bt_ctf_field_type
*type
)
948 struct bt_ctf_field_type_integer
*integer
;
949 struct bt_ctf_clock_class
*clock_class
= NULL
;
955 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
956 clock_class
= integer
->mapped_clock
;
962 int bt_ctf_field_type_integer_set_mapped_clock_class(
963 struct bt_ctf_field_type
*type
,
964 struct bt_ctf_clock_class
*clock_class
)
966 struct bt_ctf_field_type_integer
*integer
;
969 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
974 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
975 bt_put(integer
->mapped_clock
);
976 integer
->mapped_clock
= bt_get(clock_class
);
982 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
984 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
986 struct bt_ctf_field_type_enumeration_mapping_iterator
,
989 bt_put(&iter
->enumeration_type
->parent
);
994 struct bt_ctf_field_type_enumeration_mapping_iterator
*
995 bt_ctf_field_type_enumeration_find_mappings_type(
996 struct bt_ctf_field_type
*type
,
997 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
999 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1000 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1002 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1006 enumeration_type
= container_of(type
,
1007 struct bt_ctf_field_type_enumeration
, parent
);
1008 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
1013 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
1015 iter
->enumeration_type
= enumeration_type
;
1017 iter
->type
= iterator_type
;
1022 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1023 bt_ctf_field_type_enumeration_find_mappings_by_name(
1024 struct bt_ctf_field_type
*type
, const char *name
)
1026 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1028 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1029 type
, ITERATOR_BY_NAME
);
1034 iter
->u
.name_quark
= g_quark_try_string(name
);
1035 if (!iter
->u
.name_quark
) {
1039 /* Advance iterator to first entry, or leave index at -1. */
1040 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1041 /* No entry found. */
1051 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1052 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1054 struct bt_ctf_field_type_enumeration
*enumeration
;
1055 struct bt_ctf_field_type
*type
;
1056 int i
, ret
= 0, len
;
1058 enumeration
= iter
->enumeration_type
;
1059 type
= &enumeration
->parent
;
1060 len
= enumeration
->entries
->len
;
1061 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1062 struct enumeration_mapping
*mapping
=
1063 get_enumeration_mapping(type
, i
);
1065 switch (iter
->type
) {
1066 case ITERATOR_BY_NAME
:
1067 if (mapping
->string
== iter
->u
.name_quark
) {
1072 case ITERATOR_BY_SIGNED_VALUE
:
1074 int64_t value
= iter
->u
.signed_value
;
1076 if (value
>= mapping
->range_start
._signed
&&
1077 value
<= mapping
->range_end
._signed
) {
1083 case ITERATOR_BY_UNSIGNED_VALUE
:
1085 uint64_t value
= iter
->u
.unsigned_value
;
1087 if (value
>= mapping
->range_start
._unsigned
&&
1088 value
<= mapping
->range_end
._unsigned
) {
1104 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1105 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1106 struct bt_ctf_field_type
*type
, int64_t value
)
1108 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1110 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1111 type
, ITERATOR_BY_SIGNED_VALUE
);
1116 if (bt_ctf_field_type_integer_get_signed(
1117 iter
->enumeration_type
->container
) != 1) {
1120 iter
->u
.signed_value
= value
;
1122 /* Advance iterator to first entry, or leave index at -1. */
1123 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1124 /* No entry found. */
1134 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1135 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1136 struct bt_ctf_field_type
*type
, uint64_t value
)
1138 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1140 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1141 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1146 if (bt_ctf_field_type_integer_get_signed(
1147 iter
->enumeration_type
->container
) != 0) {
1150 iter
->u
.unsigned_value
= value
;
1152 /* Advance iterator to first entry, or leave index at -1. */
1153 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1154 /* No entry found. */
1164 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1165 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1166 const char **mapping_name
, int64_t *range_begin
,
1176 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1177 &iter
->enumeration_type
->parent
, iter
->index
,
1178 mapping_name
, range_begin
, range_end
);
1183 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1184 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1185 const char **mapping_name
, uint64_t *range_begin
,
1186 uint64_t *range_end
)
1195 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1196 &iter
->enumeration_type
->parent
, iter
->index
,
1197 mapping_name
, range_begin
, range_end
);
1202 int bt_ctf_field_type_enumeration_get_mapping_signed(
1203 struct bt_ctf_field_type
*enum_field_type
,
1204 uint64_t index
, const char **mapping_name
, int64_t *range_begin
,
1208 struct enumeration_mapping
*mapping
;
1210 if (!enum_field_type
) {
1215 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1222 *mapping_name
= g_quark_to_string(mapping
->string
);
1226 *range_begin
= mapping
->range_start
._signed
;
1230 *range_end
= mapping
->range_end
._signed
;
1236 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1237 struct bt_ctf_field_type
*enum_field_type
,
1239 const char **mapping_name
, uint64_t *range_begin
,
1240 uint64_t *range_end
)
1243 struct enumeration_mapping
*mapping
;
1245 if (!enum_field_type
) {
1250 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1257 *mapping_name
= g_quark_to_string(mapping
->string
);
1261 *range_begin
= mapping
->range_start
._unsigned
;
1265 *range_end
= mapping
->range_end
._unsigned
;
1271 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1272 struct bt_ctf_field_type
*integer_container_type
)
1274 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1276 if (!integer_container_type
) {
1280 if (integer_container_type
->id
!= BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1284 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1289 enumeration
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ENUM
;
1290 bt_get(integer_container_type
);
1291 enumeration
->container
= integer_container_type
;
1292 enumeration
->entries
= g_ptr_array_new_with_free_func(
1293 (GDestroyNotify
)destroy_enumeration_mapping
);
1294 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1295 return &enumeration
->parent
;
1297 g_free(enumeration
);
1301 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1302 struct bt_ctf_field_type
*type
)
1304 struct bt_ctf_field_type
*container_type
= NULL
;
1305 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1311 if (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) {
1315 enumeration_type
= container_of(type
,
1316 struct bt_ctf_field_type_enumeration
, parent
);
1317 container_type
= enumeration_type
->container
;
1318 bt_get(container_type
);
1320 return container_type
;
1323 int bt_ctf_field_type_enumeration_add_mapping(
1324 struct bt_ctf_field_type
*type
, const char *string
,
1325 int64_t range_start
, int64_t range_end
)
1328 GQuark mapping_name
;
1329 struct enumeration_mapping
*mapping
;
1330 struct bt_ctf_field_type_enumeration
*enumeration
;
1331 char *escaped_string
;
1333 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1335 (range_end
< range_start
)) {
1340 if (!string
|| strlen(string
) == 0) {
1345 escaped_string
= g_strescape(string
, NULL
);
1346 if (!escaped_string
) {
1351 mapping
= g_new(struct enumeration_mapping
, 1);
1356 mapping_name
= g_quark_from_string(escaped_string
);
1357 *mapping
= (struct enumeration_mapping
) {
1358 .range_start
._signed
= range_start
,
1359 .range_end
._signed
= range_end
,
1360 .string
= mapping_name
,
1362 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1364 g_ptr_array_add(enumeration
->entries
, mapping
);
1365 g_ptr_array_sort(enumeration
->entries
,
1366 (GCompareFunc
)compare_enumeration_mappings_signed
);
1368 free(escaped_string
);
1373 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1374 struct bt_ctf_field_type
*type
, const char *string
,
1375 uint64_t range_start
, uint64_t range_end
)
1378 GQuark mapping_name
;
1379 struct enumeration_mapping
*mapping
;
1380 struct bt_ctf_field_type_enumeration
*enumeration
;
1381 char *escaped_string
;
1383 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
) ||
1385 (range_end
< range_start
)) {
1390 if (!string
|| strlen(string
) == 0) {
1395 escaped_string
= g_strescape(string
, NULL
);
1396 if (!escaped_string
) {
1401 mapping
= g_new(struct enumeration_mapping
, 1);
1406 mapping_name
= g_quark_from_string(escaped_string
);
1407 *mapping
= (struct enumeration_mapping
) {
1408 .range_start
._unsigned
= range_start
,
1409 .range_end
._unsigned
= range_end
,
1410 .string
= mapping_name
,
1412 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1414 g_ptr_array_add(enumeration
->entries
, mapping
);
1415 g_ptr_array_sort(enumeration
->entries
,
1416 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1418 free(escaped_string
);
1423 int64_t bt_ctf_field_type_enumeration_get_mapping_count(
1424 struct bt_ctf_field_type
*type
)
1427 struct bt_ctf_field_type_enumeration
*enumeration
;
1429 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ENUM
)) {
1434 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1436 ret
= (int64_t) enumeration
->entries
->len
;
1441 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1443 struct bt_ctf_field_type_floating_point
*floating_point
=
1444 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1446 if (!floating_point
) {
1450 floating_point
->parent
.id
= BT_CTF_FIELD_TYPE_ID_FLOAT
;
1451 floating_point
->exp_dig
= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1452 floating_point
->mant_dig
= FLT_MANT_DIG
;
1453 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1455 return floating_point
? &floating_point
->parent
: NULL
;
1458 int bt_ctf_field_type_floating_point_get_exponent_digits(
1459 struct bt_ctf_field_type
*type
)
1462 struct bt_ctf_field_type_floating_point
*floating_point
;
1464 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1469 floating_point
= container_of(type
,
1470 struct bt_ctf_field_type_floating_point
, parent
);
1471 ret
= (int) floating_point
->exp_dig
;
1476 int bt_ctf_field_type_floating_point_set_exponent_digits(
1477 struct bt_ctf_field_type
*type
,
1478 unsigned int exponent_digits
)
1481 struct bt_ctf_field_type_floating_point
*floating_point
;
1483 if (!type
|| type
->frozen
||
1484 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1489 floating_point
= container_of(type
,
1490 struct bt_ctf_field_type_floating_point
, parent
);
1491 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1492 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1494 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1499 floating_point
->exp_dig
= exponent_digits
;
1504 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1505 struct bt_ctf_field_type
*type
)
1508 struct bt_ctf_field_type_floating_point
*floating_point
;
1510 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1515 floating_point
= container_of(type
,
1516 struct bt_ctf_field_type_floating_point
, parent
);
1517 ret
= (int) floating_point
->mant_dig
;
1522 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1523 struct bt_ctf_field_type
*type
,
1524 unsigned int mantissa_digits
)
1527 struct bt_ctf_field_type_floating_point
*floating_point
;
1529 if (!type
|| type
->frozen
||
1530 (type
->id
!= BT_CTF_FIELD_TYPE_ID_FLOAT
)) {
1535 floating_point
= container_of(type
,
1536 struct bt_ctf_field_type_floating_point
, parent
);
1538 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1539 (mantissa_digits
!= DBL_MANT_DIG
) &&
1540 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1545 floating_point
->mant_dig
= mantissa_digits
;
1550 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1552 struct bt_ctf_field_type_structure
*structure
=
1553 g_new0(struct bt_ctf_field_type_structure
, 1);
1559 structure
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRUCT
;
1560 structure
->fields
= g_ptr_array_new_with_free_func(
1561 (GDestroyNotify
)destroy_structure_field
);
1562 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1563 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1564 return &structure
->parent
;
1569 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1570 struct bt_ctf_field_type
*field_type
,
1571 const char *field_name
)
1574 struct bt_ctf_field_type_structure
*structure
;
1577 * TODO: check that `field_type` does not contain `type`,
1580 if (!type
|| !field_type
|| type
->frozen
||
1581 bt_ctf_validate_identifier(field_name
) ||
1582 (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) ||
1583 type
== field_type
) {
1588 structure
= container_of(type
,
1589 struct bt_ctf_field_type_structure
, parent
);
1590 if (add_structure_field(structure
->fields
,
1591 structure
->field_name_to_index
, field_type
, field_name
)) {
1599 int64_t bt_ctf_field_type_structure_get_field_count(
1600 struct bt_ctf_field_type
*type
)
1603 struct bt_ctf_field_type_structure
*structure
;
1605 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
)) {
1610 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1612 ret
= (int64_t) structure
->fields
->len
;
1617 int bt_ctf_field_type_structure_get_field_by_index(
1618 struct bt_ctf_field_type
*type
,
1619 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1622 struct bt_ctf_field_type_structure
*structure
;
1623 struct structure_field
*field
;
1626 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRUCT
) {
1631 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1633 if (index
>= structure
->fields
->len
) {
1638 field
= g_ptr_array_index(structure
->fields
, index
);
1640 *field_type
= field
->type
;
1641 bt_get(field
->type
);
1644 *field_name
= g_quark_to_string(field
->name
);
1650 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1651 struct bt_ctf_field_type
*type
,
1656 struct structure_field
*field
;
1657 struct bt_ctf_field_type_structure
*structure
;
1658 struct bt_ctf_field_type
*field_type
= NULL
;
1660 if (!type
|| !name
) {
1664 name_quark
= g_quark_try_string(name
);
1669 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1671 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1672 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1676 field
= structure
->fields
->pdata
[index
];
1677 field_type
= field
->type
;
1683 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1684 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1686 struct bt_ctf_field_type_variant
*variant
= NULL
;
1688 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1692 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1697 variant
->parent
.id
= BT_CTF_FIELD_TYPE_ID_VARIANT
;
1698 variant
->tag_name
= g_string_new(tag_name
);
1699 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1700 variant
->fields
= g_ptr_array_new_with_free_func(
1701 (GDestroyNotify
) destroy_structure_field
);
1704 variant
->tag
= container_of(enum_tag
,
1705 struct bt_ctf_field_type_enumeration
, parent
);
1708 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1709 /* A variant's alignment is undefined */
1710 variant
->parent
.alignment
= 0;
1711 return &variant
->parent
;
1716 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1717 struct bt_ctf_field_type
*type
)
1719 struct bt_ctf_field_type_variant
*variant
;
1720 struct bt_ctf_field_type
*tag_type
= NULL
;
1722 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1726 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1727 if (!variant
->tag
) {
1731 tag_type
= &variant
->tag
->parent
;
1737 const char *bt_ctf_field_type_variant_get_tag_name(
1738 struct bt_ctf_field_type
*type
)
1740 struct bt_ctf_field_type_variant
*variant
;
1741 const char *tag_name
= NULL
;
1743 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1747 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1748 if (variant
->tag_name
->len
== 0) {
1752 tag_name
= variant
->tag_name
->str
;
1757 int bt_ctf_field_type_variant_set_tag_name(
1758 struct bt_ctf_field_type
*type
, const char *name
)
1761 struct bt_ctf_field_type_variant
*variant
;
1763 if (!type
|| type
->frozen
||
1764 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1765 bt_ctf_validate_identifier(name
)) {
1770 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1771 g_string_assign(variant
->tag_name
, name
);
1776 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1777 struct bt_ctf_field_type
*field_type
,
1778 const char *field_name
)
1782 struct bt_ctf_field_type_variant
*variant
;
1783 GQuark field_name_quark
= g_quark_from_string(field_name
);
1786 * TODO: check that `field_type` does not contain `type`,
1789 if (!type
|| !field_type
|| type
->frozen
||
1790 bt_ctf_validate_identifier(field_name
) ||
1791 (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) ||
1792 type
== field_type
) {
1797 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1799 /* The user has explicitly provided a tag; validate against it. */
1803 /* Make sure this name is present in the enum tag */
1804 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1805 struct enumeration_mapping
*mapping
=
1806 g_ptr_array_index(variant
->tag
->entries
, i
);
1808 if (mapping
->string
== field_name_quark
) {
1815 /* Validation failed */
1821 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1822 field_type
, field_name
)) {
1830 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1831 struct bt_ctf_field_type
*type
,
1832 const char *field_name
)
1836 struct structure_field
*field
;
1837 struct bt_ctf_field_type_variant
*variant
;
1838 struct bt_ctf_field_type
*field_type
= NULL
;
1840 if (!type
|| !field_name
) {
1844 name_quark
= g_quark_try_string(field_name
);
1849 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1850 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1851 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1855 field
= g_ptr_array_index(variant
->fields
, index
);
1856 field_type
= field
->type
;
1862 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1863 struct bt_ctf_field_type
*type
,
1864 struct bt_ctf_field
*tag
)
1867 const char *enum_value
;
1868 struct bt_ctf_field_type
*field_type
= NULL
;
1869 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1871 if (!type
|| !tag
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1875 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1880 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
,
1881 &enum_value
, NULL
, NULL
);
1886 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1893 int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1896 struct bt_ctf_field_type_variant
*variant
;
1898 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
)) {
1903 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1905 ret
= (int64_t) variant
->fields
->len
;
1911 int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type
*type
,
1912 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1915 struct bt_ctf_field_type_variant
*variant
;
1916 struct structure_field
*field
;
1919 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_VARIANT
) {
1924 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1926 if (index
>= variant
->fields
->len
) {
1931 field
= g_ptr_array_index(variant
->fields
, index
);
1933 *field_type
= field
->type
;
1934 bt_get(field
->type
);
1937 *field_name
= g_quark_to_string(field
->name
);
1943 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1944 struct bt_ctf_field_type
*element_type
,
1945 unsigned int length
)
1947 struct bt_ctf_field_type_array
*array
= NULL
;
1949 if (!element_type
|| length
== 0) {
1953 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1958 array
->parent
.id
= BT_CTF_FIELD_TYPE_ID_ARRAY
;
1959 bt_get(element_type
);
1960 array
->element_type
= element_type
;
1961 array
->length
= length
;
1962 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1963 return &array
->parent
;
1968 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1969 struct bt_ctf_field_type
*type
)
1971 struct bt_ctf_field_type
*ret
= NULL
;
1972 struct bt_ctf_field_type_array
*array
;
1974 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
1978 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1979 ret
= array
->element_type
;
1986 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1987 struct bt_ctf_field_type
*element_type
)
1990 struct bt_ctf_field_type_array
*array
;
1992 if (!type
|| !element_type
||
1993 (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
1998 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2000 if (array
->element_type
) {
2001 BT_PUT(array
->element_type
);
2004 array
->element_type
= element_type
;
2005 bt_get(array
->element_type
);
2011 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2014 struct bt_ctf_field_type_array
*array
;
2016 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_ARRAY
)) {
2021 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2022 ret
= (int64_t) array
->length
;
2027 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2028 struct bt_ctf_field_type
*element_type
,
2029 const char *length_field_name
)
2031 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2033 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2037 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2042 sequence
->parent
.id
= BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2043 bt_get(element_type
);
2044 sequence
->element_type
= element_type
;
2045 sequence
->length_field_name
= g_string_new(length_field_name
);
2046 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2047 return &sequence
->parent
;
2052 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2053 struct bt_ctf_field_type
*type
)
2055 struct bt_ctf_field_type
*ret
= NULL
;
2056 struct bt_ctf_field_type_sequence
*sequence
;
2058 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2062 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2064 ret
= sequence
->element_type
;
2071 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2072 struct bt_ctf_field_type
*element_type
)
2075 struct bt_ctf_field_type_sequence
*sequence
;
2077 if (!type
|| !element_type
||
2078 (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2083 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2085 if (sequence
->element_type
) {
2086 BT_PUT(sequence
->element_type
);
2089 sequence
->element_type
= element_type
;
2090 bt_get(sequence
->element_type
);
2096 const char *bt_ctf_field_type_sequence_get_length_field_name(
2097 struct bt_ctf_field_type
*type
)
2099 const char *ret
= NULL
;
2100 struct bt_ctf_field_type_sequence
*sequence
;
2102 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_SEQUENCE
)) {
2106 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2108 ret
= sequence
->length_field_name
->str
;
2113 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2115 struct bt_ctf_field_type_string
*string
=
2116 g_new0(struct bt_ctf_field_type_string
, 1);
2122 string
->parent
.id
= BT_CTF_FIELD_TYPE_ID_STRING
;
2123 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2124 string
->encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2125 string
->parent
.alignment
= CHAR_BIT
;
2126 return &string
->parent
;
2129 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2130 struct bt_ctf_field_type
*type
)
2132 struct bt_ctf_field_type_string
*string
;
2133 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2135 if (!type
|| (type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
)) {
2139 string
= container_of(type
, struct bt_ctf_field_type_string
,
2141 ret
= string
->encoding
;
2146 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2147 enum bt_ctf_string_encoding encoding
)
2150 struct bt_ctf_field_type_string
*string
;
2152 if (!type
|| type
->id
!= BT_CTF_FIELD_TYPE_ID_STRING
||
2153 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2154 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2159 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2160 string
->encoding
= encoding
;
2165 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2168 enum bt_ctf_field_type_id type_id
;
2176 ret
= (int) type
->alignment
;
2180 type_id
= bt_ctf_field_type_get_type_id(type
);
2182 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
2184 struct bt_ctf_field_type
*element
=
2185 bt_ctf_field_type_sequence_get_element_type(type
);
2192 ret
= bt_ctf_field_type_get_alignment(element
);
2196 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
2198 struct bt_ctf_field_type
*element
=
2199 bt_ctf_field_type_array_get_element_type(type
);
2206 ret
= bt_ctf_field_type_get_alignment(element
);
2210 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
2212 int64_t i
, element_count
;
2214 element_count
= bt_ctf_field_type_structure_get_field_count(
2216 if (element_count
< 0) {
2217 ret
= element_count
;
2221 for (i
= 0; i
< element_count
; i
++) {
2222 struct bt_ctf_field_type
*field
;
2223 int field_alignment
;
2225 ret
= bt_ctf_field_type_structure_get_field_by_index(
2226 type
, NULL
, &field
, i
);
2232 field_alignment
= bt_ctf_field_type_get_alignment(
2235 if (field_alignment
< 0) {
2236 ret
= field_alignment
;
2240 type
->alignment
= MAX(field_alignment
, type
->alignment
);
2242 ret
= (int) type
->alignment
;
2245 case BT_CTF_FIELD_TYPE_ID_UNKNOWN
:
2249 ret
= (int) type
->alignment
;
2257 int is_power_of_two(unsigned int value
)
2259 return ((value
& (value
- 1)) == 0) && value
> 0;
2262 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2263 unsigned int alignment
)
2266 enum bt_ctf_field_type_id type_id
;
2268 /* Alignment must be a power of two */
2269 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2274 type_id
= bt_ctf_field_type_get_type_id(type
);
2275 if (type_id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
2280 if (type
->id
== BT_CTF_FIELD_TYPE_ID_STRING
&&
2281 alignment
!= CHAR_BIT
) {
2286 if (type_id
== BT_CTF_FIELD_TYPE_ID_VARIANT
||
2287 type_id
== BT_CTF_FIELD_TYPE_ID_SEQUENCE
||
2288 type_id
== BT_CTF_FIELD_TYPE_ID_ARRAY
) {
2289 /* Setting an alignment on these types makes no sense */
2294 type
->alignment
= alignment
;
2300 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2301 struct bt_ctf_field_type
*type
)
2303 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2310 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
2312 struct bt_ctf_field_type_integer
*integer
= container_of(
2313 type
, struct bt_ctf_field_type_integer
, parent
);
2314 ret
= integer
->user_byte_order
;
2317 case BT_CTF_FIELD_TYPE_ID_ENUM
:
2319 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2320 type
, struct bt_ctf_field_type_enumeration
, parent
);
2321 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2324 case BT_CTF_FIELD_TYPE_ID_FLOAT
:
2326 struct bt_ctf_field_type_floating_point
*floating_point
=
2328 struct bt_ctf_field_type_floating_point
,
2330 ret
= floating_point
->user_byte_order
;
2337 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2338 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2339 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2340 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2346 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2347 enum bt_ctf_byte_order byte_order
)
2351 if (!type
|| type
->frozen
) {
2356 if (byte_order
!= BT_CTF_BYTE_ORDER_NATIVE
&&
2357 byte_order
!= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
&&
2358 byte_order
!= BT_CTF_BYTE_ORDER_BIG_ENDIAN
&&
2359 byte_order
!= BT_CTF_BYTE_ORDER_NETWORK
) {
2364 if (set_byte_order_funcs
[type
->id
]) {
2365 set_byte_order_funcs
[type
->id
](type
, byte_order
);
2371 enum bt_ctf_field_type_id
bt_ctf_field_type_get_type_id(
2372 struct bt_ctf_field_type
*type
)
2375 return BT_CTF_FIELD_TYPE_ID_UNKNOWN
;
2381 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2383 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
;
2386 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2388 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_FLOAT
;
2391 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2393 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ENUM
;
2396 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2398 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRING
;
2401 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2403 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
;
2406 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2408 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_ARRAY
;
2411 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2413 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_SEQUENCE
;
2416 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2418 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_FIELD_TYPE_ID_VARIANT
;
2421 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2426 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2432 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2442 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2443 struct bt_ctf_field_type_variant
*variant
,
2446 struct bt_ctf_field_type
*type
= NULL
;
2447 GQuark field_name_quark
;
2449 struct structure_field
*field_entry
;
2450 struct range_overlap_query query
= {
2451 .range_start
._signed
= tag_value
,
2452 .range_end
._signed
= tag_value
,
2457 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2459 if (!query
.overlaps
) {
2463 field_name_quark
= query
.mapping_name
;
2464 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2465 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2469 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2470 type
= field_entry
->type
;
2476 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2477 struct bt_ctf_field_type_variant
*variant
,
2480 struct bt_ctf_field_type
*type
= NULL
;
2481 GQuark field_name_quark
;
2483 struct structure_field
*field_entry
;
2484 struct range_overlap_query query
= {
2485 .range_start
._unsigned
= tag_value
,
2486 .range_end
._unsigned
= tag_value
,
2491 g_ptr_array_foreach(variant
->tag
->entries
,
2492 check_ranges_overlap_unsigned
,
2494 if (!query
.overlaps
) {
2498 field_name_quark
= query
.mapping_name
;
2499 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2500 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2504 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2505 type
= field_entry
->type
;
2511 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2512 struct metadata_context
*context
)
2516 if (!type
|| !context
) {
2521 /* Make sure field type is valid before serializing it */
2522 ret
= bt_ctf_field_type_validate(type
);
2528 ret
= type
->serialize(type
, context
);
2533 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2535 struct bt_ctf_field_type
*copy
= NULL
;
2541 copy
= type_copy_funcs
[type
->id
](type
);
2542 copy
->alignment
= type
->alignment
;
2548 int bt_ctf_field_type_structure_get_field_name_index(
2549 struct bt_ctf_field_type
*type
, const char *name
)
2554 struct bt_ctf_field_type_structure
*structure
;
2556 if (!type
|| !name
||
2557 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2562 name_quark
= g_quark_try_string(name
);
2568 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2570 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2571 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2581 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2582 struct bt_ctf_field_type
*field
, int index
)
2585 struct bt_ctf_field_type_structure
*structure
;
2587 if (!type
|| !field
||
2588 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_STRUCT
) {
2593 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2595 if (index
< 0 || index
>= structure
->fields
->len
) {
2601 bt_put(((struct structure_field
*)
2602 g_ptr_array_index(structure
->fields
, index
))->type
);
2603 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2610 int bt_ctf_field_type_variant_get_field_name_index(
2611 struct bt_ctf_field_type
*type
, const char *name
)
2616 struct bt_ctf_field_type_variant
*variant
;
2618 if (!type
|| !name
||
2619 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2624 name_quark
= g_quark_try_string(name
);
2630 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2632 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2633 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2643 int bt_ctf_field_type_sequence_set_length_field_path(
2644 struct bt_ctf_field_type
*type
,
2645 struct bt_ctf_field_path
*path
)
2648 struct bt_ctf_field_type_sequence
*sequence
;
2650 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2651 BT_CTF_FIELD_TYPE_ID_SEQUENCE
) {
2656 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2659 BT_MOVE(sequence
->length_field_path
, path
);
2665 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2666 struct bt_ctf_field_path
*path
)
2669 struct bt_ctf_field_type_variant
*variant
;
2671 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2672 BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2677 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2680 BT_MOVE(variant
->tag_field_path
, path
);
2686 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2687 struct bt_ctf_field_type
*tag
)
2690 struct bt_ctf_field_type_variant
*variant
;
2692 if (!type
|| !tag
||
2693 bt_ctf_field_type_get_type_id(tag
) !=
2694 BT_CTF_FIELD_TYPE_ID_ENUM
) {
2699 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2703 bt_put(&variant
->tag
->parent
);
2705 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2712 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2713 struct bt_ctf_field_type
*field
, int index
)
2716 struct bt_ctf_field_type_variant
*variant
;
2718 if (!type
|| !field
||
2719 bt_ctf_field_type_get_type_id(type
) != BT_CTF_FIELD_TYPE_ID_VARIANT
) {
2724 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2726 if (index
< 0 || index
>= variant
->fields
->len
) {
2732 bt_put(((struct structure_field
*)
2733 g_ptr_array_index(variant
->fields
, index
))->type
);
2734 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2741 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2743 struct bt_ctf_field_type_integer
*integer
=
2744 (struct bt_ctf_field_type_integer
*) type
;
2750 bt_put(integer
->mapped_clock
);
2755 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2757 struct bt_ctf_field_type_enumeration
*enumeration
=
2758 (struct bt_ctf_field_type_enumeration
*) type
;
2764 g_ptr_array_free(enumeration
->entries
, TRUE
);
2765 bt_put(enumeration
->container
);
2766 g_free(enumeration
);
2770 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2772 struct bt_ctf_field_type_floating_point
*floating_point
=
2773 (struct bt_ctf_field_type_floating_point
*) type
;
2779 g_free(floating_point
);
2783 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2785 struct bt_ctf_field_type_structure
*structure
=
2786 (struct bt_ctf_field_type_structure
*) type
;
2792 g_ptr_array_free(structure
->fields
, TRUE
);
2793 g_hash_table_destroy(structure
->field_name_to_index
);
2798 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2800 struct bt_ctf_field_type_variant
*variant
=
2801 (struct bt_ctf_field_type_variant
*) type
;
2807 g_ptr_array_free(variant
->fields
, TRUE
);
2808 g_hash_table_destroy(variant
->field_name_to_index
);
2809 g_string_free(variant
->tag_name
, TRUE
);
2810 bt_put(&variant
->tag
->parent
);
2811 BT_PUT(variant
->tag_field_path
);
2816 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2818 struct bt_ctf_field_type_array
*array
=
2819 (struct bt_ctf_field_type_array
*) type
;
2825 bt_put(array
->element_type
);
2830 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2832 struct bt_ctf_field_type_sequence
*sequence
=
2833 (struct bt_ctf_field_type_sequence
*) type
;
2839 bt_put(sequence
->element_type
);
2840 g_string_free(sequence
->length_field_name
, TRUE
);
2841 BT_PUT(sequence
->length_field_path
);
2846 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2848 struct bt_ctf_field_type_string
*string
=
2849 (struct bt_ctf_field_type_string
*) type
;
2859 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2865 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2867 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2868 type
, struct bt_ctf_field_type_integer
, parent
);
2870 if (integer_type
->mapped_clock
) {
2871 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2874 generic_field_type_freeze(type
);
2878 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2880 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2881 type
, struct bt_ctf_field_type_enumeration
, parent
);
2883 set_enumeration_range_overlap(type
);
2885 generic_field_type_freeze(type
);
2886 bt_ctf_field_type_freeze(enumeration_type
->container
);
2890 void freeze_structure_field(struct structure_field
*field
)
2892 bt_ctf_field_type_freeze(field
->type
);
2896 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2898 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2899 type
, struct bt_ctf_field_type_structure
, parent
);
2901 /* Cache the alignment */
2902 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2903 generic_field_type_freeze(type
);
2904 g_ptr_array_foreach(structure_type
->fields
,
2905 (GFunc
) freeze_structure_field
, NULL
);
2909 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2911 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2912 type
, struct bt_ctf_field_type_variant
, parent
);
2914 generic_field_type_freeze(type
);
2915 g_ptr_array_foreach(variant_type
->fields
,
2916 (GFunc
) freeze_structure_field
, NULL
);
2920 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2922 struct bt_ctf_field_type_array
*array_type
= container_of(
2923 type
, struct bt_ctf_field_type_array
, parent
);
2925 /* Cache the alignment */
2926 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2927 generic_field_type_freeze(type
);
2928 bt_ctf_field_type_freeze(array_type
->element_type
);
2932 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2934 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2935 type
, struct bt_ctf_field_type_sequence
, parent
);
2937 /* Cache the alignment */
2938 type
->alignment
= bt_ctf_field_type_get_alignment(type
);
2939 generic_field_type_freeze(type
);
2940 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2944 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2946 const char *encoding_string
;
2949 case BT_CTF_STRING_ENCODING_NONE
:
2950 encoding_string
= "none";
2952 case BT_CTF_STRING_ENCODING_ASCII
:
2953 encoding_string
= "ASCII";
2955 case BT_CTF_STRING_ENCODING_UTF8
:
2956 encoding_string
= "UTF8";
2959 encoding_string
= "unknown";
2963 return encoding_string
;
2967 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
2969 const char *base_string
;
2972 case BT_CTF_INTEGER_BASE_DECIMAL
:
2973 base_string
= "decimal";
2975 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
2976 base_string
= "hexadecimal";
2978 case BT_CTF_INTEGER_BASE_OCTAL
:
2979 base_string
= "octal";
2981 case BT_CTF_INTEGER_BASE_BINARY
:
2982 base_string
= "binary";
2985 base_string
= "unknown";
2993 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
2994 struct metadata_context
*context
)
2996 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
2997 struct bt_ctf_field_type_integer
, parent
);
3000 g_string_append_printf(context
->string
,
3001 "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s",
3002 integer
->size
, type
->alignment
,
3003 (integer
->is_signed
? "true" : "false"),
3004 get_encoding_string(integer
->encoding
),
3005 get_integer_base_string(integer
->base
),
3006 get_byte_order_string(integer
->user_byte_order
));
3007 if (integer
->mapped_clock
) {
3008 const char *clock_name
= bt_ctf_clock_class_get_name(
3009 integer
->mapped_clock
);
3016 g_string_append_printf(context
->string
,
3017 "; map = clock.%s.value", clock_name
);
3020 g_string_append(context
->string
, "; }");
3026 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3027 struct metadata_context
*context
)
3031 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3032 struct bt_ctf_field_type_enumeration
, parent
);
3033 struct bt_ctf_field_type
*container_type
;
3034 int container_signed
;
3036 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3037 if (!container_type
) {
3042 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3043 if (container_signed
< 0) {
3044 ret
= container_signed
;
3045 goto error_put_container_type
;
3048 g_string_append(context
->string
, "enum : ");
3049 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3051 goto error_put_container_type
;
3054 g_string_append(context
->string
, " { ");
3055 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3056 struct enumeration_mapping
*mapping
=
3057 enumeration
->entries
->pdata
[entry
];
3059 if (container_signed
) {
3060 if (mapping
->range_start
._signed
==
3061 mapping
->range_end
._signed
) {
3062 g_string_append_printf(context
->string
,
3063 "\"%s\" = %" PRId64
,
3064 g_quark_to_string(mapping
->string
),
3065 mapping
->range_start
._signed
);
3067 g_string_append_printf(context
->string
,
3068 "\"%s\" = %" PRId64
" ... %" PRId64
,
3069 g_quark_to_string(mapping
->string
),
3070 mapping
->range_start
._signed
,
3071 mapping
->range_end
._signed
);
3074 if (mapping
->range_start
._unsigned
==
3075 mapping
->range_end
._unsigned
) {
3076 g_string_append_printf(context
->string
,
3077 "\"%s\" = %" PRIu64
,
3078 g_quark_to_string(mapping
->string
),
3079 mapping
->range_start
._unsigned
);
3081 g_string_append_printf(context
->string
,
3082 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3083 g_quark_to_string(mapping
->string
),
3084 mapping
->range_start
._unsigned
,
3085 mapping
->range_end
._unsigned
);
3089 g_string_append(context
->string
,
3090 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3094 if (context
->field_name
->len
) {
3095 g_string_append_printf(context
->string
, " %s",
3096 context
->field_name
->str
);
3097 g_string_assign(context
->field_name
, "");
3099 error_put_container_type
:
3100 bt_put(container_type
);
3106 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3107 struct metadata_context
*context
)
3109 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3110 type
, struct bt_ctf_field_type_floating_point
, parent
);
3112 g_string_append_printf(context
->string
,
3113 "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }",
3114 floating_point
->exp_dig
,
3115 floating_point
->mant_dig
,
3116 get_byte_order_string(floating_point
->user_byte_order
),
3122 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3123 struct metadata_context
*context
)
3126 unsigned int indent
;
3128 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3129 struct bt_ctf_field_type_structure
, parent
);
3130 GString
*structure_field_name
= context
->field_name
;
3132 context
->field_name
= g_string_new("");
3134 context
->current_indentation_level
++;
3135 g_string_append(context
->string
, "struct {\n");
3137 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3138 struct structure_field
*field
;
3140 for (indent
= 0; indent
< context
->current_indentation_level
;
3142 g_string_append_c(context
->string
, '\t');
3145 field
= structure
->fields
->pdata
[i
];
3146 g_string_assign(context
->field_name
,
3147 g_quark_to_string(field
->name
));
3148 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3153 if (context
->field_name
->len
) {
3154 g_string_append_printf(context
->string
, " %s",
3155 context
->field_name
->str
);
3157 g_string_append(context
->string
, ";\n");
3160 context
->current_indentation_level
--;
3161 for (indent
= 0; indent
< context
->current_indentation_level
;
3163 g_string_append_c(context
->string
, '\t');
3166 g_string_append_printf(context
->string
, "} align(%u)",
3169 g_string_free(context
->field_name
, TRUE
);
3170 context
->field_name
= structure_field_name
;
3175 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3176 struct metadata_context
*context
)
3179 unsigned int indent
;
3181 struct bt_ctf_field_type_variant
*variant
= container_of(
3182 type
, struct bt_ctf_field_type_variant
, parent
);
3183 GString
*variant_field_name
= context
->field_name
;
3185 context
->field_name
= g_string_new("");
3186 if (variant
->tag_name
->len
> 0) {
3187 g_string_append_printf(context
->string
,
3188 "variant <%s> {\n", variant
->tag_name
->str
);
3190 g_string_append(context
->string
, "variant {\n");
3193 context
->current_indentation_level
++;
3194 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3195 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3197 g_string_assign(context
->field_name
,
3198 g_quark_to_string(field
->name
));
3199 for (indent
= 0; indent
< context
->current_indentation_level
;
3201 g_string_append_c(context
->string
, '\t');
3204 g_string_assign(context
->field_name
,
3205 g_quark_to_string(field
->name
));
3206 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3211 if (context
->field_name
->len
) {
3212 g_string_append_printf(context
->string
, " %s;",
3213 context
->field_name
->str
);
3216 g_string_append_c(context
->string
, '\n');
3219 context
->current_indentation_level
--;
3220 for (indent
= 0; indent
< context
->current_indentation_level
;
3222 g_string_append_c(context
->string
, '\t');
3225 g_string_append(context
->string
, "}");
3227 g_string_free(context
->field_name
, TRUE
);
3228 context
->field_name
= variant_field_name
;
3233 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3234 struct metadata_context
*context
)
3237 struct bt_ctf_field_type_array
*array
= container_of(type
,
3238 struct bt_ctf_field_type_array
, parent
);
3240 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3245 if (context
->field_name
->len
) {
3246 g_string_append_printf(context
->string
, " %s[%u]",
3247 context
->field_name
->str
, array
->length
);
3248 g_string_assign(context
->field_name
, "");
3250 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3257 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3258 struct metadata_context
*context
)
3261 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3262 type
, struct bt_ctf_field_type_sequence
, parent
);
3264 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3269 if (context
->field_name
->len
) {
3270 g_string_append_printf(context
->string
, " %s[%s]",
3271 context
->field_name
->str
,
3272 sequence
->length_field_name
->str
);
3273 g_string_assign(context
->field_name
, "");
3275 g_string_append_printf(context
->string
, "[%s]",
3276 sequence
->length_field_name
->str
);
3283 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3284 struct metadata_context
*context
)
3286 struct bt_ctf_field_type_string
*string
= container_of(
3287 type
, struct bt_ctf_field_type_string
, parent
);
3289 g_string_append_printf(context
->string
,
3290 "string { encoding = %s; }",
3291 get_encoding_string(string
->encoding
));
3296 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3297 enum bt_ctf_byte_order byte_order
)
3299 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3300 struct bt_ctf_field_type_integer
, parent
);
3302 integer_type
->user_byte_order
= byte_order
;
3306 void bt_ctf_field_type_enumeration_set_byte_order(
3307 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3309 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3310 struct bt_ctf_field_type_enumeration
, parent
);
3312 /* Safe to assume that container is an integer */
3313 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3318 void bt_ctf_field_type_floating_point_set_byte_order(
3319 struct bt_ctf_field_type
*type
, enum bt_ctf_byte_order byte_order
)
3321 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3322 container_of(type
, struct bt_ctf_field_type_floating_point
,
3325 floating_point_type
->user_byte_order
= byte_order
;
3329 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3330 enum bt_ctf_byte_order byte_order
)
3333 struct bt_ctf_field_type_structure
*structure_type
=
3334 container_of(type
, struct bt_ctf_field_type_structure
,
3337 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3338 struct structure_field
*field
= g_ptr_array_index(
3339 structure_type
->fields
, i
);
3340 struct bt_ctf_field_type
*field_type
= field
->type
;
3342 if (set_byte_order_funcs
[field_type
->id
]) {
3343 set_byte_order_funcs
[field_type
->id
](
3344 field_type
, byte_order
);
3350 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3351 enum bt_ctf_byte_order byte_order
)
3354 struct bt_ctf_field_type_variant
*variant_type
=
3355 container_of(type
, struct bt_ctf_field_type_variant
,
3358 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3359 struct structure_field
*field
= g_ptr_array_index(
3360 variant_type
->fields
, i
);
3361 struct bt_ctf_field_type
*field_type
= field
->type
;
3363 if (set_byte_order_funcs
[field_type
->id
]) {
3364 set_byte_order_funcs
[field_type
->id
](
3365 field_type
, byte_order
);
3371 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3372 enum bt_ctf_byte_order byte_order
)
3374 struct bt_ctf_field_type_array
*array_type
=
3375 container_of(type
, struct bt_ctf_field_type_array
,
3378 if (set_byte_order_funcs
[array_type
->element_type
->id
]) {
3379 set_byte_order_funcs
[array_type
->element_type
->id
](
3380 array_type
->element_type
, byte_order
);
3385 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3386 enum bt_ctf_byte_order byte_order
)
3388 struct bt_ctf_field_type_sequence
*sequence_type
=
3389 container_of(type
, struct bt_ctf_field_type_sequence
,
3392 if (set_byte_order_funcs
[
3393 sequence_type
->element_type
->id
]) {
3394 set_byte_order_funcs
[
3395 sequence_type
->element_type
->id
](
3396 sequence_type
->element_type
, byte_order
);
3401 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3402 struct bt_ctf_field_type
*type
)
3404 struct bt_ctf_field_type
*copy
;
3405 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3407 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3408 copy
= bt_ctf_field_type_integer_create(integer
->size
);
3413 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3415 copy_integer
->mapped_clock
= bt_get(integer
->mapped_clock
);
3416 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3417 copy_integer
->is_signed
= integer
->is_signed
;
3418 copy_integer
->size
= integer
->size
;
3419 copy_integer
->base
= integer
->base
;
3420 copy_integer
->encoding
= integer
->encoding
;
3427 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3428 struct bt_ctf_field_type
*type
)
3431 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3432 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3434 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3437 /* Copy the source enumeration's container */
3438 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3439 if (!copy_container
) {
3443 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3447 copy_enumeration
= container_of(copy
,
3448 struct bt_ctf_field_type_enumeration
, parent
);
3450 /* Copy all enumaration entries */
3451 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3452 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3453 enumeration
->entries
, i
);
3454 struct enumeration_mapping
*copy_mapping
= g_new0(
3455 struct enumeration_mapping
, 1);
3457 if (!copy_mapping
) {
3461 *copy_mapping
= *mapping
;
3462 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3466 bt_put(copy_container
);
3469 bt_put(copy_container
);
3475 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3476 struct bt_ctf_field_type
*type
)
3478 struct bt_ctf_field_type
*copy
;
3479 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3481 floating_point
= container_of(type
,
3482 struct bt_ctf_field_type_floating_point
, parent
);
3483 copy
= bt_ctf_field_type_floating_point_create();
3488 copy_float
= container_of(copy
,
3489 struct bt_ctf_field_type_floating_point
, parent
);
3490 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3491 copy_float
->exp_dig
= floating_point
->exp_dig
;
3492 copy_float
->mant_dig
= floating_point
->mant_dig
;
3498 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3499 struct bt_ctf_field_type
*type
)
3502 GHashTableIter iter
;
3503 gpointer key
, value
;
3504 struct bt_ctf_field_type
*copy
;
3505 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3507 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3509 copy
= bt_ctf_field_type_structure_create();
3514 copy_structure
= container_of(copy
,
3515 struct bt_ctf_field_type_structure
, parent
);
3517 /* Copy field_name_to_index */
3518 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3519 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3520 g_hash_table_insert(copy_structure
->field_name_to_index
,
3524 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3525 struct structure_field
*entry
, *copy_entry
;
3526 struct bt_ctf_field_type
*copy_field
;
3528 copy_entry
= g_new0(struct structure_field
, 1);
3533 entry
= g_ptr_array_index(structure
->fields
, i
);
3534 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3540 copy_entry
->name
= entry
->name
;
3541 copy_entry
->type
= copy_field
;
3542 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3553 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3554 struct bt_ctf_field_type
*type
)
3557 GHashTableIter iter
;
3558 gpointer key
, value
;
3559 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3560 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3562 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3565 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3571 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3572 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3577 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3580 /* Copy field_name_to_index */
3581 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3582 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3583 g_hash_table_insert(copy_variant
->field_name_to_index
,
3587 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3588 struct structure_field
*entry
, *copy_entry
;
3589 struct bt_ctf_field_type
*copy_field
;
3591 copy_entry
= g_new0(struct structure_field
, 1);
3596 entry
= g_ptr_array_index(variant
->fields
, i
);
3597 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3603 copy_entry
->name
= entry
->name
;
3604 copy_entry
->type
= copy_field
;
3605 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3608 if (variant
->tag_field_path
) {
3609 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3610 variant
->tag_field_path
);
3611 if (!copy_variant
->tag_field_path
) {
3625 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3626 struct bt_ctf_field_type
*type
)
3628 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3629 struct bt_ctf_field_type_array
*array
;
3631 array
= container_of(type
, struct bt_ctf_field_type_array
,
3633 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3634 if (!copy_element
) {
3638 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3643 bt_put(copy_element
);
3648 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3649 struct bt_ctf_field_type
*type
)
3651 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3652 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3654 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3656 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3657 if (!copy_element
) {
3661 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3662 sequence
->length_field_name
->len
?
3663 sequence
->length_field_name
->str
: NULL
);
3668 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3670 if (sequence
->length_field_path
) {
3671 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3672 sequence
->length_field_path
);
3673 if (!copy_sequence
->length_field_path
) {
3678 bt_put(copy_element
);
3686 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3687 struct bt_ctf_field_type
*type
)
3689 struct bt_ctf_field_type
*copy
;
3690 struct bt_ctf_field_type_string
*string
;
3692 copy
= bt_ctf_field_type_string_create();
3697 string
= container_of(type
, struct bt_ctf_field_type_string
,
3704 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3705 struct bt_ctf_field_type
*type_b
)
3708 struct bt_ctf_field_type_integer
*int_type_a
;
3709 struct bt_ctf_field_type_integer
*int_type_b
;
3711 int_type_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3713 int_type_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3717 if (int_type_a
->size
!= int_type_b
->size
) {
3722 if (int_type_a
->user_byte_order
!= int_type_b
->user_byte_order
) {
3727 if (int_type_a
->is_signed
!= int_type_b
->is_signed
) {
3732 if (int_type_a
->base
!= int_type_b
->base
) {
3737 if (int_type_a
->encoding
!= int_type_b
->encoding
) {
3742 if (int_type_a
->mapped_clock
!= int_type_b
->mapped_clock
) {
3754 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3755 struct bt_ctf_field_type
*type_b
)
3758 struct bt_ctf_field_type_floating_point
*float_a
;
3759 struct bt_ctf_field_type_floating_point
*float_b
;
3761 float_a
= container_of(type_a
,
3762 struct bt_ctf_field_type_floating_point
, parent
);
3763 float_b
= container_of(type_b
,
3764 struct bt_ctf_field_type_floating_point
, parent
);
3767 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3771 /* Exponent length */
3772 if (float_a
->exp_dig
!= float_b
->exp_dig
) {
3776 /* Mantissa length */
3777 if (float_a
->mant_dig
!= float_b
->mant_dig
) {
3789 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3790 struct enumeration_mapping
*mapping_b
)
3795 if (mapping_a
->string
!= mapping_b
->string
) {
3800 if (mapping_a
->range_start
._unsigned
!=
3801 mapping_b
->range_start
._unsigned
) {
3806 if (mapping_a
->range_end
._unsigned
!=
3807 mapping_b
->range_end
._unsigned
) {
3819 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3820 struct bt_ctf_field_type
*type_b
)
3824 struct bt_ctf_field_type_enumeration
*enum_a
;
3825 struct bt_ctf_field_type_enumeration
*enum_b
;
3827 enum_a
= container_of(type_a
,
3828 struct bt_ctf_field_type_enumeration
, parent
);
3829 enum_b
= container_of(type_b
,
3830 struct bt_ctf_field_type_enumeration
, parent
);
3832 /* Container field type */
3833 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3841 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3845 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3846 struct enumeration_mapping
*mapping_a
=
3847 g_ptr_array_index(enum_a
->entries
, i
);
3848 struct enumeration_mapping
*mapping_b
=
3849 g_ptr_array_index(enum_b
->entries
, i
);
3851 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3864 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3865 struct bt_ctf_field_type
*type_b
)
3868 struct bt_ctf_field_type_string
*string_a
;
3869 struct bt_ctf_field_type_string
*string_b
;
3871 string_a
= container_of(type_a
,
3872 struct bt_ctf_field_type_string
, parent
);
3873 string_b
= container_of(type_b
,
3874 struct bt_ctf_field_type_string
, parent
);
3877 if (string_a
->encoding
!= string_b
->encoding
) {
3889 int compare_structure_fields(struct structure_field
*field_a
,
3890 struct structure_field
*field_b
)
3895 if (field_a
->name
!= field_b
->name
) {
3900 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
3907 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
3908 struct bt_ctf_field_type
*type_b
)
3912 struct bt_ctf_field_type_structure
*struct_a
;
3913 struct bt_ctf_field_type_structure
*struct_b
;
3915 struct_a
= container_of(type_a
,
3916 struct bt_ctf_field_type_structure
, parent
);
3917 struct_b
= container_of(type_b
,
3918 struct bt_ctf_field_type_structure
, parent
);
3921 if (bt_ctf_field_type_get_alignment(type_a
) !=
3922 bt_ctf_field_type_get_alignment(type_b
)) {
3927 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
3931 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
3932 struct structure_field
*field_a
=
3933 g_ptr_array_index(struct_a
->fields
, i
);
3934 struct structure_field
*field_b
=
3935 g_ptr_array_index(struct_b
->fields
, i
);
3937 ret
= compare_structure_fields(field_a
, field_b
);
3951 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
3952 struct bt_ctf_field_type
*type_b
)
3956 struct bt_ctf_field_type_variant
*variant_a
;
3957 struct bt_ctf_field_type_variant
*variant_b
;
3959 variant_a
= container_of(type_a
,
3960 struct bt_ctf_field_type_variant
, parent
);
3961 variant_b
= container_of(type_b
,
3962 struct bt_ctf_field_type_variant
, parent
);
3965 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
3970 ret
= bt_ctf_field_type_compare(
3971 (struct bt_ctf_field_type
*) variant_a
->tag
,
3972 (struct bt_ctf_field_type
*) variant_b
->tag
);
3980 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
3984 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
3985 struct structure_field
*field_a
=
3986 g_ptr_array_index(variant_a
->fields
, i
);
3987 struct structure_field
*field_b
=
3988 g_ptr_array_index(variant_b
->fields
, i
);
3990 ret
= compare_structure_fields(field_a
, field_b
);
4004 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4005 struct bt_ctf_field_type
*type_b
)
4008 struct bt_ctf_field_type_array
*array_a
;
4009 struct bt_ctf_field_type_array
*array_b
;
4011 array_a
= container_of(type_a
,
4012 struct bt_ctf_field_type_array
, parent
);
4013 array_b
= container_of(type_b
,
4014 struct bt_ctf_field_type_array
, parent
);
4017 if (array_a
->length
!= array_b
->length
) {
4022 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4023 array_b
->element_type
);
4030 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4031 struct bt_ctf_field_type
*type_b
)
4034 struct bt_ctf_field_type_sequence
*sequence_a
;
4035 struct bt_ctf_field_type_sequence
*sequence_b
;
4037 sequence_a
= container_of(type_a
,
4038 struct bt_ctf_field_type_sequence
, parent
);
4039 sequence_b
= container_of(type_b
,
4040 struct bt_ctf_field_type_sequence
, parent
);
4043 if (strcmp(sequence_a
->length_field_name
->str
,
4044 sequence_b
->length_field_name
->str
)) {
4049 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4050 sequence_b
->element_type
);
4056 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4057 struct bt_ctf_field_type
*type_b
)
4061 if (type_a
== type_b
) {
4062 /* Same reference: equal (even if both are NULL) */
4067 if (!type_a
|| !type_b
) {
4072 if (type_a
->id
!= type_b
->id
) {
4073 /* Different type IDs */
4077 if (type_a
->id
== BT_CTF_FIELD_TYPE_ID_UNKNOWN
) {
4078 /* Both have unknown type IDs */
4082 ret
= type_compare_funcs
[type_a
->id
](type_a
, type_b
);
4089 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4091 int64_t field_count
= -1;
4092 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4095 case CTF_TYPE_STRUCT
:
4097 bt_ctf_field_type_structure_get_field_count(field_type
);
4099 case CTF_TYPE_VARIANT
:
4101 bt_ctf_field_type_variant_get_field_count(field_type
);
4103 case CTF_TYPE_ARRAY
:
4104 case CTF_TYPE_SEQUENCE
:
4106 * Array and sequence types always contain a single member
4107 * (the element type).
4119 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4120 struct bt_ctf_field_type
*field_type
, int index
)
4122 struct bt_ctf_field_type
*field
= NULL
;
4123 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4126 case CTF_TYPE_STRUCT
:
4127 bt_ctf_field_type_structure_get_field_by_index(field_type
,
4128 NULL
, &field
, index
);
4130 case CTF_TYPE_VARIANT
:
4132 int ret
= bt_ctf_field_type_variant_get_field_by_index(
4133 field_type
, NULL
, &field
, index
);
4140 case CTF_TYPE_ARRAY
:
4141 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4143 case CTF_TYPE_SEQUENCE
:
4144 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4154 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4157 int field_index
= -1;
4158 enum bt_ctf_field_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4161 case CTF_TYPE_STRUCT
:
4162 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4165 case CTF_TYPE_VARIANT
:
4166 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4176 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4177 struct bt_ctf_field_type
*type
)
4179 struct bt_ctf_field_path
*field_path
= NULL
;
4180 struct bt_ctf_field_type_variant
*variant
;
4182 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4186 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4188 field_path
= bt_get(variant
->tag_field_path
);
4193 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4194 struct bt_ctf_field_type
*type
)
4196 struct bt_ctf_field_path
*field_path
= NULL
;
4197 struct bt_ctf_field_type_sequence
*sequence
;
4199 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4203 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4205 field_path
= bt_get(sequence
->length_field_path
);