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.h>
39 #include <babeltrace/endian.h>
44 struct range_overlap_query
{
59 void bt_ctf_field_type_destroy(struct bt_object
*);
61 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*);
63 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*);
65 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*);
67 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*);
69 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*);
71 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*);
73 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*);
75 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*);
78 void (* const type_destroy_funcs
[])(struct bt_ctf_field_type
*) = {
79 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_destroy
,
80 [BT_CTF_TYPE_ID_ENUM
] =
81 bt_ctf_field_type_enumeration_destroy
,
82 [BT_CTF_TYPE_ID_FLOAT
] =
83 bt_ctf_field_type_floating_point_destroy
,
84 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_destroy
,
85 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_destroy
,
86 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_destroy
,
87 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_destroy
,
88 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_destroy
,
92 void generic_field_type_freeze(struct bt_ctf_field_type
*);
94 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*);
96 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*);
98 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*);
100 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*);
102 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*);
104 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*);
107 type_freeze_func
const type_freeze_funcs
[] = {
108 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_freeze
,
109 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_freeze
,
110 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_type_freeze
,
111 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_freeze
,
112 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_freeze
,
113 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_freeze
,
114 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_freeze
,
115 [BT_CTF_TYPE_ID_STRING
] = generic_field_type_freeze
,
119 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*,
120 struct metadata_context
*);
122 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*,
123 struct metadata_context
*);
125 int bt_ctf_field_type_floating_point_serialize(
126 struct bt_ctf_field_type
*, struct metadata_context
*);
128 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*,
129 struct metadata_context
*);
131 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*,
132 struct metadata_context
*);
134 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*,
135 struct metadata_context
*);
137 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*,
138 struct metadata_context
*);
140 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*,
141 struct metadata_context
*);
144 type_serialize_func
const type_serialize_funcs
[] = {
145 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_serialize
,
146 [BT_CTF_TYPE_ID_ENUM
] =
147 bt_ctf_field_type_enumeration_serialize
,
148 [BT_CTF_TYPE_ID_FLOAT
] =
149 bt_ctf_field_type_floating_point_serialize
,
150 [BT_CTF_TYPE_ID_STRUCT
] =
151 bt_ctf_field_type_structure_serialize
,
152 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_serialize
,
153 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_serialize
,
154 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_serialize
,
155 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_serialize
,
159 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*,
160 int byte_order
, int set_native
);
162 void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type
*,
163 int byte_order
, int set_native
);
165 void bt_ctf_field_type_floating_point_set_byte_order(
166 struct bt_ctf_field_type
*, int byte_order
, int set_native
);
168 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*,
169 int byte_order
, int set_native
);
171 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*,
172 int byte_order
, int set_native
);
174 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*,
175 int byte_order
, int set_native
);
177 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*,
178 int byte_order
, int set_native
);
180 /* The set_native flag only set the byte order if it is set to native */
182 void (* const set_byte_order_funcs
[])(struct bt_ctf_field_type
*,
183 int byte_order
, int set_native
) = {
184 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_set_byte_order
,
185 [BT_CTF_TYPE_ID_ENUM
] =
186 bt_ctf_field_type_enumeration_set_byte_order
,
187 [BT_CTF_TYPE_ID_FLOAT
] =
188 bt_ctf_field_type_floating_point_set_byte_order
,
189 [BT_CTF_TYPE_ID_STRUCT
] =
190 bt_ctf_field_type_structure_set_byte_order
,
191 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_set_byte_order
,
192 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_set_byte_order
,
193 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_set_byte_order
,
194 [BT_CTF_TYPE_ID_STRING
] = NULL
,
198 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
199 struct bt_ctf_field_type
*);
201 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
202 struct bt_ctf_field_type
*);
204 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
205 struct bt_ctf_field_type
*);
207 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
208 struct bt_ctf_field_type
*);
210 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
211 struct bt_ctf_field_type
*);
213 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
214 struct bt_ctf_field_type
*);
216 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
217 struct bt_ctf_field_type
*);
219 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
220 struct bt_ctf_field_type
*);
223 struct bt_ctf_field_type
*(* const type_copy_funcs
[])(
224 struct bt_ctf_field_type
*) = {
225 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_copy
,
226 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_copy
,
227 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_copy
,
228 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_copy
,
229 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_copy
,
230 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_copy
,
231 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_copy
,
232 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_copy
,
236 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*,
237 struct bt_ctf_field_type
*);
239 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*,
240 struct bt_ctf_field_type
*);
242 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*,
243 struct bt_ctf_field_type
*);
245 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*,
246 struct bt_ctf_field_type
*);
248 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*,
249 struct bt_ctf_field_type
*);
251 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*,
252 struct bt_ctf_field_type
*);
254 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*,
255 struct bt_ctf_field_type
*);
257 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*,
258 struct bt_ctf_field_type
*);
261 int (* const type_compare_funcs
[])(struct bt_ctf_field_type
*,
262 struct bt_ctf_field_type
*) = {
263 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_compare
,
264 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_compare
,
265 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_type_floating_point_compare
,
266 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_compare
,
267 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_compare
,
268 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_compare
,
269 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_compare
,
270 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_type_string_compare
,
274 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*);
276 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*);
278 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*);
280 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*);
282 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*);
284 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*);
287 int (* const type_validate_funcs
[])(struct bt_ctf_field_type
*) = {
288 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_type_integer_validate
,
289 [BT_CTF_TYPE_ID_FLOAT
] = NULL
,
290 [BT_CTF_TYPE_ID_STRING
] = NULL
,
291 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_type_enumeration_validate
,
292 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_type_structure_validate
,
293 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_type_variant_validate
,
294 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_type_array_validate
,
295 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_type_sequence_validate
,
299 void destroy_enumeration_mapping(struct enumeration_mapping
*mapping
)
305 void destroy_structure_field(struct structure_field
*field
)
312 void check_ranges_overlap(gpointer element
, gpointer query
)
314 struct enumeration_mapping
*mapping
= element
;
315 struct range_overlap_query
*overlap_query
= query
;
317 if (mapping
->range_start
._signed
<= overlap_query
->range_end
._signed
318 && overlap_query
->range_start
._signed
<=
319 mapping
->range_end
._signed
) {
320 overlap_query
->overlaps
= 1;
321 overlap_query
->mapping_name
= mapping
->string
;
324 overlap_query
->overlaps
|=
325 mapping
->string
== overlap_query
->mapping_name
;
329 void check_ranges_overlap_unsigned(gpointer element
, gpointer query
)
331 struct enumeration_mapping
*mapping
= element
;
332 struct range_overlap_query
*overlap_query
= query
;
334 if (mapping
->range_start
._unsigned
<= overlap_query
->range_end
._unsigned
335 && overlap_query
->range_start
._unsigned
<=
336 mapping
->range_end
._unsigned
) {
337 overlap_query
->overlaps
= 1;
338 overlap_query
->mapping_name
= mapping
->string
;
341 overlap_query
->overlaps
|=
342 mapping
->string
== overlap_query
->mapping_name
;
346 gint
compare_enumeration_mappings_signed(struct enumeration_mapping
**a
,
347 struct enumeration_mapping
**b
)
349 return ((*a
)->range_start
._signed
< (*b
)->range_start
._signed
) ? -1 : 1;
353 gint
compare_enumeration_mappings_unsigned(struct enumeration_mapping
**a
,
354 struct enumeration_mapping
**b
)
356 return ((*a
)->range_start
._unsigned
< (*b
)->range_start
._unsigned
) ? -1 : 1;
360 void bt_ctf_field_type_init(struct bt_ctf_field_type
*type
, int init_bo
)
362 enum bt_ctf_type_id type_id
= type
->declaration
->id
;
364 assert(type
&& (type_id
> BT_CTF_TYPE_ID_UNKNOWN
) &&
365 (type_id
< BT_CTF_NR_TYPE_IDS
));
367 bt_object_init(type
, bt_ctf_field_type_destroy
);
368 type
->freeze
= type_freeze_funcs
[type_id
];
369 type
->serialize
= type_serialize_funcs
[type_id
];
372 int ret
= bt_ctf_field_type_set_byte_order(type
,
373 BT_CTF_BYTE_ORDER_NATIVE
);
377 type
->declaration
->alignment
= 1;
381 int add_structure_field(GPtrArray
*fields
,
382 GHashTable
*field_name_to_index
,
383 struct bt_ctf_field_type
*field_type
,
384 const char *field_name
)
387 GQuark name_quark
= g_quark_from_string(field_name
);
388 struct structure_field
*field
;
390 /* Make sure structure does not contain a field of the same name */
391 if (g_hash_table_lookup_extended(field_name_to_index
,
392 GUINT_TO_POINTER(name_quark
), NULL
, NULL
)) {
397 field
= g_new0(struct structure_field
, 1);
404 field
->name
= name_quark
;
405 field
->type
= field_type
;
406 g_hash_table_insert(field_name_to_index
,
407 GUINT_TO_POINTER(name_quark
),
408 GUINT_TO_POINTER(fields
->len
));
409 g_ptr_array_add(fields
, field
);
415 void bt_ctf_field_type_destroy(struct bt_object
*obj
)
417 struct bt_ctf_field_type
*type
;
418 enum bt_ctf_type_id type_id
;
420 type
= container_of(obj
, struct bt_ctf_field_type
, base
);
421 type_id
= type
->declaration
->id
;
422 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
423 type_id
>= BT_CTF_NR_TYPE_IDS
) {
427 type_destroy_funcs
[type_id
](type
);
431 int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type
*type
)
435 struct bt_ctf_field_type_integer
*integer
=
436 container_of(type
, struct bt_ctf_field_type_integer
,
439 if (integer
->mapped_clock
&& integer
->declaration
.signedness
) {
449 struct enumeration_mapping
*get_enumeration_mapping(
450 struct bt_ctf_field_type
*type
, int index
)
452 struct enumeration_mapping
*mapping
= NULL
;
453 struct bt_ctf_field_type_enumeration
*enumeration
;
455 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
457 if (index
>= enumeration
->entries
->len
) {
461 mapping
= g_ptr_array_index(enumeration
->entries
, index
);
467 * Note: This algorithm is O(n^2) vs number of enumeration mappings.
468 * Only used when freezing an enumeration.
471 void set_enumeration_range_overlap(
472 struct bt_ctf_field_type
*type
)
475 struct bt_ctf_field_type
*container_type
;
476 struct bt_ctf_field_type_enumeration
*enumeration_type
;
479 enumeration_type
= container_of(type
,
480 struct bt_ctf_field_type_enumeration
, parent
);
482 len
= enumeration_type
->entries
->len
;
483 container_type
= enumeration_type
->container
;
484 is_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
486 for (i
= 0; i
< len
; i
++) {
487 for (j
= i
+ 1; j
< len
; j
++) {
488 struct enumeration_mapping
*mapping
[2];
490 mapping
[0] = get_enumeration_mapping(type
, i
);
491 mapping
[1] = get_enumeration_mapping(type
, j
);
493 if (mapping
[0]->range_start
._signed
494 <= mapping
[1]->range_end
._signed
495 && mapping
[0]->range_end
._signed
496 >= mapping
[1]->range_start
._signed
) {
497 enumeration_type
->has_overlapping_ranges
= true;
501 if (mapping
[0]->range_start
._unsigned
502 <= mapping
[1]->range_end
._unsigned
503 && mapping
[0]->range_end
._unsigned
504 >= mapping
[1]->range_start
._unsigned
) {
505 enumeration_type
->has_overlapping_ranges
= true;
514 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type
*type
)
518 struct bt_ctf_field_type_enumeration
*enumeration
=
519 container_of(type
, struct bt_ctf_field_type_enumeration
,
521 struct bt_ctf_field_type
*container_type
=
522 bt_ctf_field_type_enumeration_get_container_type(type
);
524 if (!container_type
) {
529 ret
= bt_ctf_field_type_validate(container_type
);
534 /* Ensure enum has entries */
535 ret
= enumeration
->entries
->len
? 0 : -1;
538 BT_PUT(container_type
);
543 int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type
*type
)
546 struct bt_ctf_field_type
*element_type
= NULL
;
547 struct bt_ctf_field_type_sequence
*sequence
=
548 container_of(type
, struct bt_ctf_field_type_sequence
,
551 /* Length field name should be set at this point */
552 if (sequence
->length_field_name
->len
== 0) {
557 element_type
= bt_ctf_field_type_sequence_get_element_type(type
);
563 ret
= bt_ctf_field_type_validate(element_type
);
566 BT_PUT(element_type
);
572 int bt_ctf_field_type_array_validate(struct bt_ctf_field_type
*type
)
575 struct bt_ctf_field_type
*element_type
= NULL
;
577 element_type
= bt_ctf_field_type_array_get_element_type(type
);
583 ret
= bt_ctf_field_type_validate(element_type
);
586 BT_PUT(element_type
);
592 int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type
*type
)
595 struct bt_ctf_field_type
*child_type
= NULL
;
596 int field_count
= bt_ctf_field_type_structure_get_field_count(type
);
599 if (field_count
< 0) {
604 for (i
= 0; i
< field_count
; ++i
) {
605 ret
= bt_ctf_field_type_structure_get_field(type
,
606 NULL
, &child_type
, i
);
611 ret
= bt_ctf_field_type_validate(child_type
);
626 bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
627 struct bt_ctf_field_type_enumeration
*enumeration_type
)
629 if (!enumeration_type
->parent
.frozen
) {
630 set_enumeration_range_overlap(&enumeration_type
->parent
);
632 return enumeration_type
->has_overlapping_ranges
;
636 int bt_ctf_field_type_enumeration_get_mapping_name(
637 struct bt_ctf_field_type
*enum_field_type
,
639 const char **mapping_name
)
642 struct enumeration_mapping
*mapping
;
644 if (!enum_field_type
|| index
< 0) {
649 mapping
= get_enumeration_mapping(enum_field_type
, index
);
656 *mapping_name
= g_quark_to_string(mapping
->string
);
663 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type
*type
)
667 struct bt_ctf_field_type
*child_type
= NULL
;
668 struct bt_ctf_field_type_variant
*variant
=
669 container_of(type
, struct bt_ctf_field_type_variant
,
672 int tag_mappings_count
;
674 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
679 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
686 bt_ctf_field_type_enumeration_get_mapping_count(
687 (struct bt_ctf_field_type
*) variant
->tag
);
689 if (tag_mappings_count
!= variant
->fields
->len
) {
694 for (i
= 0; i
< tag_mappings_count
; ++i
) {
696 struct bt_ctf_field_type
*ft
;
698 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
699 (struct bt_ctf_field_type
*) variant
->tag
,
709 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
719 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
720 if (field_count
< 0) {
725 for (i
= 0; i
< field_count
; ++i
) {
726 ret
= bt_ctf_field_type_variant_get_field(type
,
727 NULL
, &child_type
, i
);
732 ret
= bt_ctf_field_type_validate(child_type
);
747 * This function validates a given field type without considering
748 * where this field type is located. It only validates the properties
749 * of the given field type and the properties of its children if
753 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
756 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
764 /* Already marked as valid */
768 if (type_validate_funcs
[id
]) {
769 ret
= type_validate_funcs
[id
](type
);
772 if (!ret
&& type
->frozen
) {
773 /* Field type is valid */
781 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
783 struct bt_ctf_field_type_integer
*integer
=
784 g_new0(struct bt_ctf_field_type_integer
, 1);
786 if (!integer
|| size
== 0 || size
> 64) {
790 integer
->parent
.declaration
= &integer
->declaration
.p
;
791 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
792 integer
->declaration
.len
= size
;
793 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
794 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
795 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
796 return &integer
->parent
;
799 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
802 struct bt_ctf_field_type_integer
*integer
;
804 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
809 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
810 ret
= (int) integer
->declaration
.len
;
815 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
818 struct bt_ctf_field_type_integer
*integer
;
820 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
825 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
826 ret
= integer
->declaration
.signedness
;
831 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
835 struct bt_ctf_field_type_integer
*integer
;
837 if (!type
|| type
->frozen
||
838 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
843 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
844 integer
->declaration
.signedness
= !!is_signed
;
849 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
850 struct bt_ctf_field_type
*type
)
852 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
853 struct bt_ctf_field_type_integer
*integer
;
855 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
859 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
860 ret
= integer
->declaration
.base
;
865 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
866 enum bt_ctf_integer_base base
)
870 if (!type
|| type
->frozen
||
871 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
877 case BT_CTF_INTEGER_BASE_BINARY
:
878 case BT_CTF_INTEGER_BASE_OCTAL
:
879 case BT_CTF_INTEGER_BASE_DECIMAL
:
880 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
882 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
883 struct bt_ctf_field_type_integer
, parent
);
884 integer
->declaration
.base
= base
;
894 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
895 struct bt_ctf_field_type
*type
)
897 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
898 struct bt_ctf_field_type_integer
*integer
;
900 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
904 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
905 ret
= integer
->declaration
.encoding
;
910 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
911 enum bt_ctf_string_encoding encoding
)
914 struct bt_ctf_field_type_integer
*integer
;
916 if (!type
|| type
->frozen
||
917 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
918 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
919 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
924 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
925 integer
->declaration
.encoding
= encoding
;
930 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
931 struct bt_ctf_field_type
*type
)
933 struct bt_ctf_field_type_integer
*integer
;
934 struct bt_ctf_clock_class
*clock_class
= NULL
;
940 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
941 clock_class
= integer
->mapped_clock
;
947 int bt_ctf_field_type_integer_set_mapped_clock_class(
948 struct bt_ctf_field_type
*type
,
949 struct bt_ctf_clock_class
*clock_class
)
951 struct bt_ctf_field_type_integer
*integer
;
954 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
959 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
960 bt_put(integer
->mapped_clock
);
961 integer
->mapped_clock
= bt_get(clock_class
);
967 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
969 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
971 struct bt_ctf_field_type_enumeration_mapping_iterator
,
974 bt_put(&iter
->enumeration_type
->parent
);
979 struct bt_ctf_field_type_enumeration_mapping_iterator
*
980 bt_ctf_field_type_enumeration_find_mappings_type(
981 struct bt_ctf_field_type
*type
,
982 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
984 struct bt_ctf_field_type_enumeration
*enumeration_type
;
985 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
987 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
991 enumeration_type
= container_of(type
,
992 struct bt_ctf_field_type_enumeration
, parent
);
993 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
998 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
1000 iter
->enumeration_type
= enumeration_type
;
1002 iter
->type
= iterator_type
;
1007 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1008 bt_ctf_field_type_enumeration_find_mappings_by_name(
1009 struct bt_ctf_field_type
*type
, const char *name
)
1011 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1013 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1014 type
, ITERATOR_BY_NAME
);
1019 iter
->u
.name_quark
= g_quark_try_string(name
);
1020 if (!iter
->u
.name_quark
) {
1024 /* Advance iterator to first entry, or leave index at -1. */
1025 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1026 /* No entry found. */
1036 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1037 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1039 struct bt_ctf_field_type_enumeration
*enumeration
;
1040 struct bt_ctf_field_type
*type
;
1041 int i
, ret
= 0, len
;
1043 enumeration
= iter
->enumeration_type
;
1044 type
= &enumeration
->parent
;
1045 len
= enumeration
->entries
->len
;
1046 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1047 struct enumeration_mapping
*mapping
=
1048 get_enumeration_mapping(type
, i
);
1050 switch (iter
->type
) {
1051 case ITERATOR_BY_NAME
:
1052 if (mapping
->string
== iter
->u
.name_quark
) {
1057 case ITERATOR_BY_SIGNED_VALUE
:
1059 int64_t value
= iter
->u
.signed_value
;
1061 if (value
>= mapping
->range_start
._signed
&&
1062 value
<= mapping
->range_end
._signed
) {
1068 case ITERATOR_BY_UNSIGNED_VALUE
:
1070 uint64_t value
= iter
->u
.unsigned_value
;
1072 if (value
>= mapping
->range_start
._unsigned
&&
1073 value
<= mapping
->range_end
._unsigned
) {
1089 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1090 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1091 struct bt_ctf_field_type
*type
, int64_t value
)
1093 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1095 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1096 type
, ITERATOR_BY_SIGNED_VALUE
);
1101 if (bt_ctf_field_type_integer_get_signed(
1102 iter
->enumeration_type
->container
) != 1) {
1105 iter
->u
.signed_value
= value
;
1107 /* Advance iterator to first entry, or leave index at -1. */
1108 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1109 /* No entry found. */
1119 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1120 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1121 struct bt_ctf_field_type
*type
, uint64_t value
)
1123 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1125 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1126 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1131 if (bt_ctf_field_type_integer_get_signed(
1132 iter
->enumeration_type
->container
) != 0) {
1135 iter
->u
.unsigned_value
= value
;
1137 /* Advance iterator to first entry, or leave index at -1. */
1138 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1139 /* No entry found. */
1149 int bt_ctf_field_type_enumeration_mapping_iterator_get_name(
1150 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1151 const char **mapping_name
)
1160 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
1161 &iter
->enumeration_type
->parent
, iter
->index
,
1167 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1168 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1169 const char **mapping_name
, int64_t *range_begin
,
1179 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1180 &iter
->enumeration_type
->parent
, iter
->index
,
1181 mapping_name
, range_begin
, range_end
);
1186 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1187 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1188 const char **mapping_name
, uint64_t *range_begin
,
1189 uint64_t *range_end
)
1198 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1199 &iter
->enumeration_type
->parent
, iter
->index
,
1200 mapping_name
, range_begin
, range_end
);
1205 int bt_ctf_field_type_enumeration_get_mapping_signed(
1206 struct bt_ctf_field_type
*enum_field_type
,
1208 const char **mapping_name
, int64_t *range_begin
,
1212 struct enumeration_mapping
*mapping
;
1214 if (!enum_field_type
|| index
< 0) {
1219 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1226 *mapping_name
= g_quark_to_string(mapping
->string
);
1230 *range_begin
= mapping
->range_start
._signed
;
1234 *range_end
= mapping
->range_end
._signed
;
1240 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1241 struct bt_ctf_field_type
*enum_field_type
,
1243 const char **mapping_name
, uint64_t *range_begin
,
1244 uint64_t *range_end
)
1247 struct enumeration_mapping
*mapping
;
1249 if (!enum_field_type
|| index
< 0) {
1254 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1261 *mapping_name
= g_quark_to_string(mapping
->string
);
1265 *range_begin
= mapping
->range_start
._unsigned
;
1269 *range_end
= mapping
->range_end
._unsigned
;
1275 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1276 struct bt_ctf_field_type
*integer_container_type
)
1278 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1280 if (!integer_container_type
) {
1284 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
1288 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1293 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
1294 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
1295 bt_get(integer_container_type
);
1296 enumeration
->container
= integer_container_type
;
1297 enumeration
->entries
= g_ptr_array_new_with_free_func(
1298 (GDestroyNotify
)destroy_enumeration_mapping
);
1299 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1300 return &enumeration
->parent
;
1302 g_free(enumeration
);
1306 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1307 struct bt_ctf_field_type
*type
)
1309 struct bt_ctf_field_type
*container_type
= NULL
;
1310 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1316 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
1320 enumeration_type
= container_of(type
,
1321 struct bt_ctf_field_type_enumeration
, parent
);
1322 container_type
= enumeration_type
->container
;
1323 bt_get(container_type
);
1325 return container_type
;
1328 int bt_ctf_field_type_enumeration_add_mapping(
1329 struct bt_ctf_field_type
*type
, const char *string
,
1330 int64_t range_start
, int64_t range_end
)
1333 GQuark mapping_name
;
1334 struct enumeration_mapping
*mapping
;
1335 struct bt_ctf_field_type_enumeration
*enumeration
;
1336 char *escaped_string
;
1338 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1340 (range_end
< range_start
)) {
1345 if (!string
|| strlen(string
) == 0) {
1350 escaped_string
= g_strescape(string
, NULL
);
1351 if (!escaped_string
) {
1356 mapping
= g_new(struct enumeration_mapping
, 1);
1361 mapping_name
= g_quark_from_string(escaped_string
);
1362 *mapping
= (struct enumeration_mapping
) {
1363 .range_start
._signed
= range_start
,
1364 .range_end
._signed
= range_end
,
1365 .string
= mapping_name
,
1367 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1369 g_ptr_array_add(enumeration
->entries
, mapping
);
1370 g_ptr_array_sort(enumeration
->entries
,
1371 (GCompareFunc
)compare_enumeration_mappings_signed
);
1373 free(escaped_string
);
1378 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1379 struct bt_ctf_field_type
*type
, const char *string
,
1380 uint64_t range_start
, uint64_t range_end
)
1383 GQuark mapping_name
;
1384 struct enumeration_mapping
*mapping
;
1385 struct bt_ctf_field_type_enumeration
*enumeration
;
1386 char *escaped_string
;
1388 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1390 (range_end
< range_start
)) {
1395 if (!string
|| strlen(string
) == 0) {
1400 escaped_string
= g_strescape(string
, NULL
);
1401 if (!escaped_string
) {
1406 mapping
= g_new(struct enumeration_mapping
, 1);
1411 mapping_name
= g_quark_from_string(escaped_string
);
1412 *mapping
= (struct enumeration_mapping
) {
1413 .range_start
._unsigned
= range_start
,
1414 .range_end
._unsigned
= range_end
,
1415 .string
= mapping_name
,
1417 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1419 g_ptr_array_add(enumeration
->entries
, mapping
);
1420 g_ptr_array_sort(enumeration
->entries
,
1421 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1423 free(escaped_string
);
1428 int bt_ctf_field_type_enumeration_get_mapping_count(
1429 struct bt_ctf_field_type
*type
)
1432 struct bt_ctf_field_type_enumeration
*enumeration
;
1434 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1439 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1441 ret
= (int) enumeration
->entries
->len
;
1446 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1448 struct bt_ctf_field_type_floating_point
*floating_point
=
1449 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1451 if (!floating_point
) {
1455 floating_point
->declaration
.sign
= &floating_point
->sign
;
1456 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1457 floating_point
->declaration
.exp
= &floating_point
->exp
;
1458 floating_point
->sign
.len
= 1;
1459 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1460 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1461 floating_point
->declaration
.exp
->len
=
1462 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1463 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1464 floating_point
->sign
.p
.alignment
= 1;
1465 floating_point
->mantissa
.p
.alignment
= 1;
1466 floating_point
->exp
.p
.alignment
= 1;
1468 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1470 return floating_point
? &floating_point
->parent
: NULL
;
1473 int bt_ctf_field_type_floating_point_get_exponent_digits(
1474 struct bt_ctf_field_type
*type
)
1477 struct bt_ctf_field_type_floating_point
*floating_point
;
1479 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1484 floating_point
= container_of(type
,
1485 struct bt_ctf_field_type_floating_point
, parent
);
1486 ret
= (int) floating_point
->declaration
.exp
->len
;
1491 int bt_ctf_field_type_floating_point_set_exponent_digits(
1492 struct bt_ctf_field_type
*type
,
1493 unsigned int exponent_digits
)
1496 struct bt_ctf_field_type_floating_point
*floating_point
;
1498 if (!type
|| type
->frozen
||
1499 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1504 floating_point
= container_of(type
,
1505 struct bt_ctf_field_type_floating_point
, parent
);
1506 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1507 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1509 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1514 floating_point
->declaration
.exp
->len
= exponent_digits
;
1519 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1520 struct bt_ctf_field_type
*type
)
1523 struct bt_ctf_field_type_floating_point
*floating_point
;
1525 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1530 floating_point
= container_of(type
,
1531 struct bt_ctf_field_type_floating_point
, parent
);
1532 ret
= (int) floating_point
->mantissa
.len
+ 1;
1537 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1538 struct bt_ctf_field_type
*type
,
1539 unsigned int mantissa_digits
)
1542 struct bt_ctf_field_type_floating_point
*floating_point
;
1544 if (!type
|| type
->frozen
||
1545 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1550 floating_point
= container_of(type
,
1551 struct bt_ctf_field_type_floating_point
, parent
);
1553 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1554 (mantissa_digits
!= DBL_MANT_DIG
) &&
1555 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1560 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1565 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1567 struct bt_ctf_field_type_structure
*structure
=
1568 g_new0(struct bt_ctf_field_type_structure
, 1);
1574 structure
->parent
.declaration
= &structure
->declaration
.p
;
1575 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1576 structure
->fields
= g_ptr_array_new_with_free_func(
1577 (GDestroyNotify
)destroy_structure_field
);
1578 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1579 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1580 return &structure
->parent
;
1585 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1586 struct bt_ctf_field_type
*field_type
,
1587 const char *field_name
)
1590 struct bt_ctf_field_type_structure
*structure
;
1593 * TODO: check that `field_type` does not contain `type`,
1596 if (!type
|| !field_type
|| type
->frozen
||
1597 bt_ctf_validate_identifier(field_name
) ||
1598 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
) ||
1599 type
== field_type
) {
1604 structure
= container_of(type
,
1605 struct bt_ctf_field_type_structure
, parent
);
1606 if (add_structure_field(structure
->fields
,
1607 structure
->field_name_to_index
, field_type
, field_name
)) {
1615 int bt_ctf_field_type_structure_get_field_count(
1616 struct bt_ctf_field_type
*type
)
1619 struct bt_ctf_field_type_structure
*structure
;
1621 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1626 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1628 ret
= (int) structure
->fields
->len
;
1633 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1634 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1637 struct bt_ctf_field_type_structure
*structure
;
1638 struct structure_field
*field
;
1641 if (!type
|| index
< 0 ||
1642 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1647 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1649 if (index
>= structure
->fields
->len
) {
1654 field
= g_ptr_array_index(structure
->fields
, index
);
1656 *field_type
= field
->type
;
1657 bt_get(field
->type
);
1660 *field_name
= g_quark_to_string(field
->name
);
1666 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1667 struct bt_ctf_field_type
*type
,
1672 struct structure_field
*field
;
1673 struct bt_ctf_field_type_structure
*structure
;
1674 struct bt_ctf_field_type
*field_type
= NULL
;
1676 if (!type
|| !name
) {
1680 name_quark
= g_quark_try_string(name
);
1685 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1687 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1688 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1692 field
= structure
->fields
->pdata
[index
];
1693 field_type
= field
->type
;
1699 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1700 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1702 struct bt_ctf_field_type_variant
*variant
= NULL
;
1704 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1708 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1713 variant
->parent
.declaration
= &variant
->declaration
.p
;
1714 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1715 variant
->tag_name
= g_string_new(tag_name
);
1716 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1717 variant
->fields
= g_ptr_array_new_with_free_func(
1718 (GDestroyNotify
) destroy_structure_field
);
1721 variant
->tag
= container_of(enum_tag
,
1722 struct bt_ctf_field_type_enumeration
, parent
);
1725 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1726 /* A variant's alignment is undefined */
1727 variant
->parent
.declaration
->alignment
= 0;
1728 return &variant
->parent
;
1733 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1734 struct bt_ctf_field_type
*type
)
1736 struct bt_ctf_field_type_variant
*variant
;
1737 struct bt_ctf_field_type
*tag_type
= NULL
;
1739 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1743 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1744 if (!variant
->tag
) {
1748 tag_type
= &variant
->tag
->parent
;
1754 const char *bt_ctf_field_type_variant_get_tag_name(
1755 struct bt_ctf_field_type
*type
)
1757 struct bt_ctf_field_type_variant
*variant
;
1758 const char *tag_name
= NULL
;
1760 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1764 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1765 if (variant
->tag_name
->len
== 0) {
1769 tag_name
= variant
->tag_name
->str
;
1774 int bt_ctf_field_type_variant_set_tag_name(
1775 struct bt_ctf_field_type
*type
, const char *name
)
1778 struct bt_ctf_field_type_variant
*variant
;
1780 if (!type
|| type
->frozen
||
1781 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1782 bt_ctf_validate_identifier(name
)) {
1787 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1788 g_string_assign(variant
->tag_name
, name
);
1793 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1794 struct bt_ctf_field_type
*field_type
,
1795 const char *field_name
)
1799 struct bt_ctf_field_type_variant
*variant
;
1800 GQuark field_name_quark
= g_quark_from_string(field_name
);
1803 * TODO: check that `field_type` does not contain `type`,
1806 if (!type
|| !field_type
|| type
->frozen
||
1807 bt_ctf_validate_identifier(field_name
) ||
1808 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1809 type
== field_type
) {
1814 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1816 /* The user has explicitly provided a tag; validate against it. */
1820 /* Make sure this name is present in the enum tag */
1821 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1822 struct enumeration_mapping
*mapping
=
1823 g_ptr_array_index(variant
->tag
->entries
, i
);
1825 if (mapping
->string
== field_name_quark
) {
1832 /* Validation failed */
1838 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1839 field_type
, field_name
)) {
1847 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1848 struct bt_ctf_field_type
*type
,
1849 const char *field_name
)
1853 struct structure_field
*field
;
1854 struct bt_ctf_field_type_variant
*variant
;
1855 struct bt_ctf_field_type
*field_type
= NULL
;
1857 if (!type
|| !field_name
) {
1861 name_quark
= g_quark_try_string(field_name
);
1866 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1867 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1868 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1872 field
= g_ptr_array_index(variant
->fields
, index
);
1873 field_type
= field
->type
;
1879 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1880 struct bt_ctf_field_type
*type
,
1881 struct bt_ctf_field
*tag
)
1884 const char *enum_value
;
1885 struct bt_ctf_field_type
*field_type
= NULL
;
1886 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1888 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1892 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1897 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter
,
1903 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1910 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1913 struct bt_ctf_field_type_variant
*variant
;
1915 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1920 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1922 ret
= (int) variant
->fields
->len
;
1928 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1929 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1932 struct bt_ctf_field_type_variant
*variant
;
1933 struct structure_field
*field
;
1936 if (!type
|| index
< 0 ||
1937 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1942 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1944 if (index
>= variant
->fields
->len
) {
1949 field
= g_ptr_array_index(variant
->fields
, index
);
1951 *field_type
= field
->type
;
1952 bt_get(field
->type
);
1955 *field_name
= g_quark_to_string(field
->name
);
1961 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1962 struct bt_ctf_field_type
*element_type
,
1963 unsigned int length
)
1965 struct bt_ctf_field_type_array
*array
= NULL
;
1967 if (!element_type
|| length
== 0) {
1971 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1976 array
->parent
.declaration
= &array
->declaration
.p
;
1977 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1979 bt_get(element_type
);
1980 array
->element_type
= element_type
;
1981 array
->length
= length
;
1982 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1983 return &array
->parent
;
1988 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1989 struct bt_ctf_field_type
*type
)
1991 struct bt_ctf_field_type
*ret
= NULL
;
1992 struct bt_ctf_field_type_array
*array
;
1994 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1998 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1999 ret
= array
->element_type
;
2006 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
2007 struct bt_ctf_field_type
*element_type
)
2010 struct bt_ctf_field_type_array
*array
;
2012 if (!type
|| !element_type
||
2013 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2018 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2020 if (array
->element_type
) {
2021 BT_PUT(array
->element_type
);
2024 array
->element_type
= element_type
;
2025 bt_get(array
->element_type
);
2031 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2034 struct bt_ctf_field_type_array
*array
;
2036 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2041 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2042 ret
= (int64_t) array
->length
;
2047 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2048 struct bt_ctf_field_type
*element_type
,
2049 const char *length_field_name
)
2051 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2053 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2057 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2062 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
2063 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
2064 bt_get(element_type
);
2065 sequence
->element_type
= element_type
;
2066 sequence
->length_field_name
= g_string_new(length_field_name
);
2067 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2068 return &sequence
->parent
;
2073 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2074 struct bt_ctf_field_type
*type
)
2076 struct bt_ctf_field_type
*ret
= NULL
;
2077 struct bt_ctf_field_type_sequence
*sequence
;
2079 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2083 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2085 ret
= sequence
->element_type
;
2092 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2093 struct bt_ctf_field_type
*element_type
)
2096 struct bt_ctf_field_type_sequence
*sequence
;
2098 if (!type
|| !element_type
||
2099 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2104 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2106 if (sequence
->element_type
) {
2107 BT_PUT(sequence
->element_type
);
2110 sequence
->element_type
= element_type
;
2111 bt_get(sequence
->element_type
);
2117 const char *bt_ctf_field_type_sequence_get_length_field_name(
2118 struct bt_ctf_field_type
*type
)
2120 const char *ret
= NULL
;
2121 struct bt_ctf_field_type_sequence
*sequence
;
2123 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2127 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2129 ret
= sequence
->length_field_name
->str
;
2134 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2136 struct bt_ctf_field_type_string
*string
=
2137 g_new0(struct bt_ctf_field_type_string
, 1);
2143 string
->parent
.declaration
= &string
->declaration
.p
;
2144 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
2145 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2146 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2147 string
->parent
.declaration
->alignment
= CHAR_BIT
;
2148 return &string
->parent
;
2151 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2152 struct bt_ctf_field_type
*type
)
2154 struct bt_ctf_field_type_string
*string
;
2155 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2157 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
2161 string
= container_of(type
, struct bt_ctf_field_type_string
,
2163 ret
= string
->declaration
.encoding
;
2168 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2169 enum bt_ctf_string_encoding encoding
)
2172 struct bt_ctf_field_type_string
*string
;
2174 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2175 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2176 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2181 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2182 string
->declaration
.encoding
= encoding
;
2187 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2190 enum bt_ctf_type_id type_id
;
2198 ret
= (int) type
->declaration
->alignment
;
2202 type_id
= bt_ctf_field_type_get_type_id(type
);
2204 case BT_CTF_TYPE_ID_SEQUENCE
:
2206 struct bt_ctf_field_type
*element
=
2207 bt_ctf_field_type_sequence_get_element_type(type
);
2214 ret
= bt_ctf_field_type_get_alignment(element
);
2218 case BT_CTF_TYPE_ID_ARRAY
:
2220 struct bt_ctf_field_type
*element
=
2221 bt_ctf_field_type_array_get_element_type(type
);
2228 ret
= bt_ctf_field_type_get_alignment(element
);
2232 case BT_CTF_TYPE_ID_STRUCT
:
2234 int i
, element_count
;
2236 element_count
= bt_ctf_field_type_structure_get_field_count(
2238 if (element_count
< 0) {
2239 ret
= element_count
;
2243 for (i
= 0; i
< element_count
; i
++) {
2244 struct bt_ctf_field_type
*field
;
2245 int field_alignment
;
2247 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2254 field_alignment
= bt_ctf_field_type_get_alignment(
2257 if (field_alignment
< 0) {
2258 ret
= field_alignment
;
2262 type
->declaration
->alignment
= MAX(field_alignment
,
2263 type
->declaration
->alignment
);
2265 ret
= (int) type
->declaration
->alignment
;
2268 case BT_CTF_TYPE_ID_UNKNOWN
:
2272 ret
= (int) type
->declaration
->alignment
;
2280 int is_power_of_two(unsigned int value
)
2282 return ((value
& (value
- 1)) == 0) && value
> 0;
2285 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2286 unsigned int alignment
)
2289 enum bt_ctf_type_id type_id
;
2291 /* Alignment must be a power of two */
2292 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2297 type_id
= bt_ctf_field_type_get_type_id(type
);
2298 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2303 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2304 alignment
!= CHAR_BIT
) {
2309 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2310 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2311 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2312 /* Setting an alignment on these types makes no sense */
2317 type
->declaration
->alignment
= alignment
;
2323 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2324 struct bt_ctf_field_type
*type
)
2326 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2332 switch (type
->declaration
->id
) {
2333 case BT_CTF_TYPE_ID_INTEGER
:
2335 struct bt_ctf_field_type_integer
*integer
= container_of(
2336 type
, struct bt_ctf_field_type_integer
, parent
);
2337 ret
= integer
->user_byte_order
;
2340 case BT_CTF_TYPE_ID_ENUM
:
2342 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2343 type
, struct bt_ctf_field_type_enumeration
, parent
);
2344 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2347 case BT_CTF_TYPE_ID_FLOAT
:
2349 struct bt_ctf_field_type_floating_point
*floating_point
=
2351 struct bt_ctf_field_type_floating_point
,
2353 ret
= floating_point
->user_byte_order
;
2360 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2361 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2362 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2363 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2369 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2370 enum bt_ctf_byte_order byte_order
)
2373 int internal_byte_order
;
2374 enum bt_ctf_type_id type_id
;
2376 if (!type
|| type
->frozen
) {
2381 switch (byte_order
) {
2382 case BT_CTF_BYTE_ORDER_NATIVE
:
2383 /* Leave unset. Will be initialized by parent. */
2384 internal_byte_order
= 0;
2386 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2387 internal_byte_order
= LITTLE_ENDIAN
;
2389 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2390 case BT_CTF_BYTE_ORDER_NETWORK
:
2391 internal_byte_order
= BIG_ENDIAN
;
2398 type_id
= type
->declaration
->id
;
2399 if (set_byte_order_funcs
[type_id
]) {
2400 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2406 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2407 struct bt_ctf_field_type
*type
)
2410 return BT_CTF_TYPE_ID_UNKNOWN
;
2413 return type
->declaration
->id
;
2416 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2418 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2421 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2423 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2426 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2428 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2431 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2433 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2436 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2438 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2441 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2443 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2446 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2448 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2451 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2453 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2456 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2461 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2467 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2477 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2478 struct bt_ctf_field_type_variant
*variant
,
2481 struct bt_ctf_field_type
*type
= NULL
;
2482 GQuark field_name_quark
;
2484 struct structure_field
*field_entry
;
2485 struct range_overlap_query query
= {
2486 .range_start
._signed
= tag_value
,
2487 .range_end
._signed
= tag_value
,
2492 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
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 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2512 struct bt_ctf_field_type_variant
*variant
,
2515 struct bt_ctf_field_type
*type
= NULL
;
2516 GQuark field_name_quark
;
2518 struct structure_field
*field_entry
;
2519 struct range_overlap_query query
= {
2520 .range_start
._unsigned
= tag_value
,
2521 .range_end
._unsigned
= tag_value
,
2526 g_ptr_array_foreach(variant
->tag
->entries
,
2527 check_ranges_overlap_unsigned
,
2529 if (!query
.overlaps
) {
2533 field_name_quark
= query
.mapping_name
;
2534 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2535 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2539 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2540 type
= field_entry
->type
;
2546 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2547 struct metadata_context
*context
)
2551 if (!type
|| !context
) {
2556 /* Make sure field type is valid before serializing it */
2557 ret
= bt_ctf_field_type_validate(type
);
2563 ret
= type
->serialize(type
, context
);
2569 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2576 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2577 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2578 set_byte_order_funcs
[type
->declaration
->id
](type
,
2583 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2585 struct bt_ctf_field_type
*copy
= NULL
;
2591 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2597 int bt_ctf_field_type_structure_get_field_name_index(
2598 struct bt_ctf_field_type
*type
, const char *name
)
2603 struct bt_ctf_field_type_structure
*structure
;
2605 if (!type
|| !name
||
2606 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2611 name_quark
= g_quark_try_string(name
);
2617 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2619 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2620 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2630 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2631 struct bt_ctf_field_type
*field
, int index
)
2634 struct bt_ctf_field_type_structure
*structure
;
2636 if (!type
|| !field
||
2637 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2642 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2644 if (index
< 0 || index
>= structure
->fields
->len
) {
2650 bt_put(((struct structure_field
*)
2651 g_ptr_array_index(structure
->fields
, index
))->type
);
2652 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2659 int bt_ctf_field_type_variant_get_field_name_index(
2660 struct bt_ctf_field_type
*type
, const char *name
)
2665 struct bt_ctf_field_type_variant
*variant
;
2667 if (!type
|| !name
||
2668 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2673 name_quark
= g_quark_try_string(name
);
2679 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2681 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2682 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2692 int bt_ctf_field_type_sequence_set_length_field_path(
2693 struct bt_ctf_field_type
*type
,
2694 struct bt_ctf_field_path
*path
)
2697 struct bt_ctf_field_type_sequence
*sequence
;
2699 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2700 BT_CTF_TYPE_ID_SEQUENCE
) {
2705 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2708 BT_MOVE(sequence
->length_field_path
, path
);
2714 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2715 struct bt_ctf_field_path
*path
)
2718 struct bt_ctf_field_type_variant
*variant
;
2720 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2721 BT_CTF_TYPE_ID_VARIANT
) {
2726 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2729 BT_MOVE(variant
->tag_field_path
, path
);
2735 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2736 struct bt_ctf_field_type
*tag
)
2739 struct bt_ctf_field_type_variant
*variant
;
2741 if (!type
|| !tag
||
2742 bt_ctf_field_type_get_type_id(tag
) !=
2743 BT_CTF_TYPE_ID_ENUM
) {
2748 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2752 bt_put(&variant
->tag
->parent
);
2754 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2761 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2762 struct bt_ctf_field_type
*field
, int index
)
2765 struct bt_ctf_field_type_variant
*variant
;
2767 if (!type
|| !field
||
2768 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2773 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2775 if (index
< 0 || index
>= variant
->fields
->len
) {
2781 bt_put(((struct structure_field
*)
2782 g_ptr_array_index(variant
->fields
, index
))->type
);
2783 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2790 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2792 struct bt_ctf_field_type_integer
*integer
=
2793 (struct bt_ctf_field_type_integer
*) type
;
2799 bt_put(integer
->mapped_clock
);
2804 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2806 struct bt_ctf_field_type_enumeration
*enumeration
=
2807 (struct bt_ctf_field_type_enumeration
*) type
;
2813 g_ptr_array_free(enumeration
->entries
, TRUE
);
2814 bt_put(enumeration
->container
);
2815 g_free(enumeration
);
2819 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2821 struct bt_ctf_field_type_floating_point
*floating_point
=
2822 (struct bt_ctf_field_type_floating_point
*) type
;
2828 g_free(floating_point
);
2832 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2834 struct bt_ctf_field_type_structure
*structure
=
2835 (struct bt_ctf_field_type_structure
*) type
;
2841 g_ptr_array_free(structure
->fields
, TRUE
);
2842 g_hash_table_destroy(structure
->field_name_to_index
);
2847 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2849 struct bt_ctf_field_type_variant
*variant
=
2850 (struct bt_ctf_field_type_variant
*) type
;
2856 g_ptr_array_free(variant
->fields
, TRUE
);
2857 g_hash_table_destroy(variant
->field_name_to_index
);
2858 g_string_free(variant
->tag_name
, TRUE
);
2859 bt_put(&variant
->tag
->parent
);
2860 BT_PUT(variant
->tag_field_path
);
2865 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2867 struct bt_ctf_field_type_array
*array
=
2868 (struct bt_ctf_field_type_array
*) type
;
2874 bt_put(array
->element_type
);
2879 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2881 struct bt_ctf_field_type_sequence
*sequence
=
2882 (struct bt_ctf_field_type_sequence
*) type
;
2888 bt_put(sequence
->element_type
);
2889 g_string_free(sequence
->length_field_name
, TRUE
);
2890 BT_PUT(sequence
->length_field_path
);
2895 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2897 struct bt_ctf_field_type_string
*string
=
2898 (struct bt_ctf_field_type_string
*) type
;
2908 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2914 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2916 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2917 type
, struct bt_ctf_field_type_integer
, parent
);
2919 if (integer_type
->mapped_clock
) {
2920 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2923 generic_field_type_freeze(type
);
2927 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2929 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2930 type
, struct bt_ctf_field_type_enumeration
, parent
);
2932 set_enumeration_range_overlap(type
);
2934 generic_field_type_freeze(type
);
2935 bt_ctf_field_type_freeze(enumeration_type
->container
);
2939 void freeze_structure_field(struct structure_field
*field
)
2941 bt_ctf_field_type_freeze(field
->type
);
2945 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2947 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2948 type
, struct bt_ctf_field_type_structure
, parent
);
2950 /* Cache the alignment */
2951 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2952 generic_field_type_freeze(type
);
2953 g_ptr_array_foreach(structure_type
->fields
,
2954 (GFunc
) freeze_structure_field
, NULL
);
2958 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2960 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2961 type
, struct bt_ctf_field_type_variant
, parent
);
2963 generic_field_type_freeze(type
);
2964 g_ptr_array_foreach(variant_type
->fields
,
2965 (GFunc
) freeze_structure_field
, NULL
);
2969 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2971 struct bt_ctf_field_type_array
*array_type
= container_of(
2972 type
, struct bt_ctf_field_type_array
, parent
);
2974 /* Cache the alignment */
2975 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2976 generic_field_type_freeze(type
);
2977 bt_ctf_field_type_freeze(array_type
->element_type
);
2981 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2983 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2984 type
, struct bt_ctf_field_type_sequence
, parent
);
2986 /* Cache the alignment */
2987 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2988 generic_field_type_freeze(type
);
2989 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2993 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2995 const char *encoding_string
;
2998 case BT_CTF_STRING_ENCODING_NONE
:
2999 encoding_string
= "none";
3001 case BT_CTF_STRING_ENCODING_ASCII
:
3002 encoding_string
= "ASCII";
3004 case BT_CTF_STRING_ENCODING_UTF8
:
3005 encoding_string
= "UTF8";
3008 encoding_string
= "unknown";
3012 return encoding_string
;
3016 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
3018 const char *base_string
;
3021 case BT_CTF_INTEGER_BASE_DECIMAL
:
3022 base_string
= "decimal";
3024 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
3025 base_string
= "hexadecimal";
3027 case BT_CTF_INTEGER_BASE_OCTAL
:
3028 base_string
= "octal";
3030 case BT_CTF_INTEGER_BASE_BINARY
:
3031 base_string
= "binary";
3034 base_string
= "unknown";
3042 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3043 struct metadata_context
*context
)
3045 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3046 struct bt_ctf_field_type_integer
, parent
);
3049 g_string_append_printf(context
->string
,
3050 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
3051 integer
->declaration
.len
, type
->declaration
->alignment
,
3052 (integer
->declaration
.signedness
? "true" : "false"),
3053 get_encoding_string(integer
->declaration
.encoding
),
3054 get_integer_base_string(integer
->declaration
.base
),
3055 get_byte_order_string(integer
->declaration
.byte_order
));
3056 if (integer
->mapped_clock
) {
3057 const char *clock_name
= bt_ctf_clock_class_get_name(
3058 integer
->mapped_clock
);
3065 g_string_append_printf(context
->string
,
3066 "; map = clock.%s.value", clock_name
);
3069 g_string_append(context
->string
, "; }");
3075 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3076 struct metadata_context
*context
)
3080 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3081 struct bt_ctf_field_type_enumeration
, parent
);
3082 struct bt_ctf_field_type
*container_type
;
3083 int container_signed
;
3085 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3086 if (!container_type
) {
3091 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3092 if (container_signed
< 0) {
3093 ret
= container_signed
;
3094 goto error_put_container_type
;
3097 g_string_append(context
->string
, "enum : ");
3098 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3100 goto error_put_container_type
;
3103 g_string_append(context
->string
, " { ");
3104 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3105 struct enumeration_mapping
*mapping
=
3106 enumeration
->entries
->pdata
[entry
];
3108 if (container_signed
) {
3109 if (mapping
->range_start
._signed
==
3110 mapping
->range_end
._signed
) {
3111 g_string_append_printf(context
->string
,
3112 "\"%s\" = %" PRId64
,
3113 g_quark_to_string(mapping
->string
),
3114 mapping
->range_start
._signed
);
3116 g_string_append_printf(context
->string
,
3117 "\"%s\" = %" PRId64
" ... %" PRId64
,
3118 g_quark_to_string(mapping
->string
),
3119 mapping
->range_start
._signed
,
3120 mapping
->range_end
._signed
);
3123 if (mapping
->range_start
._unsigned
==
3124 mapping
->range_end
._unsigned
) {
3125 g_string_append_printf(context
->string
,
3126 "\"%s\" = %" PRIu64
,
3127 g_quark_to_string(mapping
->string
),
3128 mapping
->range_start
._unsigned
);
3130 g_string_append_printf(context
->string
,
3131 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3132 g_quark_to_string(mapping
->string
),
3133 mapping
->range_start
._unsigned
,
3134 mapping
->range_end
._unsigned
);
3138 g_string_append(context
->string
,
3139 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3143 if (context
->field_name
->len
) {
3144 g_string_append_printf(context
->string
, " %s",
3145 context
->field_name
->str
);
3146 g_string_assign(context
->field_name
, "");
3148 error_put_container_type
:
3149 bt_put(container_type
);
3155 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3156 struct metadata_context
*context
)
3158 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3159 type
, struct bt_ctf_field_type_floating_point
, parent
);
3161 g_string_append_printf(context
->string
,
3162 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
3163 floating_point
->declaration
.exp
->len
,
3164 floating_point
->declaration
.mantissa
->len
+ 1,
3165 get_byte_order_string(floating_point
->declaration
.byte_order
),
3166 type
->declaration
->alignment
);
3171 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3172 struct metadata_context
*context
)
3175 unsigned int indent
;
3177 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3178 struct bt_ctf_field_type_structure
, parent
);
3179 GString
*structure_field_name
= context
->field_name
;
3181 context
->field_name
= g_string_new("");
3183 context
->current_indentation_level
++;
3184 g_string_append(context
->string
, "struct {\n");
3186 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3187 struct structure_field
*field
;
3189 for (indent
= 0; indent
< context
->current_indentation_level
;
3191 g_string_append_c(context
->string
, '\t');
3194 field
= structure
->fields
->pdata
[i
];
3195 g_string_assign(context
->field_name
,
3196 g_quark_to_string(field
->name
));
3197 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3202 if (context
->field_name
->len
) {
3203 g_string_append_printf(context
->string
, " %s",
3204 context
->field_name
->str
);
3206 g_string_append(context
->string
, ";\n");
3209 context
->current_indentation_level
--;
3210 for (indent
= 0; indent
< context
->current_indentation_level
;
3212 g_string_append_c(context
->string
, '\t');
3215 g_string_append_printf(context
->string
, "} align(%zu)",
3216 type
->declaration
->alignment
);
3218 g_string_free(context
->field_name
, TRUE
);
3219 context
->field_name
= structure_field_name
;
3224 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3225 struct metadata_context
*context
)
3228 unsigned int indent
;
3230 struct bt_ctf_field_type_variant
*variant
= container_of(
3231 type
, struct bt_ctf_field_type_variant
, parent
);
3232 GString
*variant_field_name
= context
->field_name
;
3234 context
->field_name
= g_string_new("");
3235 if (variant
->tag_name
->len
> 0) {
3236 g_string_append_printf(context
->string
,
3237 "variant <%s> {\n", variant
->tag_name
->str
);
3239 g_string_append(context
->string
, "variant {\n");
3242 context
->current_indentation_level
++;
3243 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3244 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3246 g_string_assign(context
->field_name
,
3247 g_quark_to_string(field
->name
));
3248 for (indent
= 0; indent
< context
->current_indentation_level
;
3250 g_string_append_c(context
->string
, '\t');
3253 g_string_assign(context
->field_name
,
3254 g_quark_to_string(field
->name
));
3255 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3260 if (context
->field_name
->len
) {
3261 g_string_append_printf(context
->string
, " %s;",
3262 context
->field_name
->str
);
3265 g_string_append_c(context
->string
, '\n');
3268 context
->current_indentation_level
--;
3269 for (indent
= 0; indent
< context
->current_indentation_level
;
3271 g_string_append_c(context
->string
, '\t');
3274 g_string_append(context
->string
, "}");
3276 g_string_free(context
->field_name
, TRUE
);
3277 context
->field_name
= variant_field_name
;
3282 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3283 struct metadata_context
*context
)
3286 struct bt_ctf_field_type_array
*array
= container_of(type
,
3287 struct bt_ctf_field_type_array
, parent
);
3289 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3294 if (context
->field_name
->len
) {
3295 g_string_append_printf(context
->string
, " %s[%u]",
3296 context
->field_name
->str
, array
->length
);
3297 g_string_assign(context
->field_name
, "");
3299 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3306 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3307 struct metadata_context
*context
)
3310 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3311 type
, struct bt_ctf_field_type_sequence
, parent
);
3313 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3318 if (context
->field_name
->len
) {
3319 g_string_append_printf(context
->string
, " %s[%s]",
3320 context
->field_name
->str
,
3321 sequence
->length_field_name
->str
);
3322 g_string_assign(context
->field_name
, "");
3324 g_string_append_printf(context
->string
, "[%s]",
3325 sequence
->length_field_name
->str
);
3332 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3333 struct metadata_context
*context
)
3335 struct bt_ctf_field_type_string
*string
= container_of(
3336 type
, struct bt_ctf_field_type_string
, parent
);
3338 g_string_append_printf(context
->string
,
3339 "string { encoding = %s; }",
3340 get_encoding_string(string
->declaration
.encoding
));
3345 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3346 enum bt_ctf_byte_order ret
;
3348 switch (byte_order
) {
3349 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3351 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3353 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3355 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3357 case BT_CTF_BYTE_ORDER_NETWORK
:
3358 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3360 case BT_CTF_BYTE_ORDER_NATIVE
:
3361 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3364 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3372 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3373 int byte_order
, int set_native
)
3375 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3376 struct bt_ctf_field_type_integer
, parent
);
3379 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3381 * User byte order is native, so we can set
3382 * the real byte order.
3384 integer_type
->declaration
.byte_order
=
3388 integer_type
->user_byte_order
=
3389 get_ctf_ir_byte_order(byte_order
);
3390 integer_type
->declaration
.byte_order
= byte_order
;
3395 void bt_ctf_field_type_enumeration_set_byte_order(
3396 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3398 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3399 struct bt_ctf_field_type_enumeration
, parent
);
3401 /* Safe to assume that container is an integer */
3402 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3403 byte_order
, set_native
);
3407 void bt_ctf_field_type_floating_point_set_byte_order(
3408 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3410 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3411 container_of(type
, struct bt_ctf_field_type_floating_point
,
3415 if (floating_point_type
->user_byte_order
==
3416 BT_CTF_BYTE_ORDER_NATIVE
) {
3418 * User byte order is native, so we can set
3419 * the real byte order.
3421 floating_point_type
->declaration
.byte_order
=
3423 floating_point_type
->sign
.byte_order
=
3425 floating_point_type
->mantissa
.byte_order
=
3427 floating_point_type
->exp
.byte_order
=
3431 floating_point_type
->user_byte_order
=
3432 get_ctf_ir_byte_order(byte_order
);
3433 floating_point_type
->declaration
.byte_order
= byte_order
;
3434 floating_point_type
->sign
.byte_order
= byte_order
;
3435 floating_point_type
->mantissa
.byte_order
= byte_order
;
3436 floating_point_type
->exp
.byte_order
= byte_order
;
3441 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3442 int byte_order
, int set_native
)
3445 struct bt_ctf_field_type_structure
*structure_type
=
3446 container_of(type
, struct bt_ctf_field_type_structure
,
3449 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3450 struct structure_field
*field
= g_ptr_array_index(
3451 structure_type
->fields
, i
);
3452 struct bt_ctf_field_type
*field_type
= field
->type
;
3454 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3455 set_byte_order_funcs
[field_type
->declaration
->id
](
3456 field_type
, byte_order
, set_native
);
3462 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3463 int byte_order
, int set_native
)
3466 struct bt_ctf_field_type_variant
*variant_type
=
3467 container_of(type
, struct bt_ctf_field_type_variant
,
3470 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3471 struct structure_field
*field
= g_ptr_array_index(
3472 variant_type
->fields
, i
);
3473 struct bt_ctf_field_type
*field_type
= field
->type
;
3475 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3476 set_byte_order_funcs
[field_type
->declaration
->id
](
3477 field_type
, byte_order
, set_native
);
3483 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3484 int byte_order
, int set_native
)
3486 struct bt_ctf_field_type_array
*array_type
=
3487 container_of(type
, struct bt_ctf_field_type_array
,
3490 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3491 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3492 array_type
->element_type
, byte_order
, set_native
);
3497 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3498 int byte_order
, int set_native
)
3500 struct bt_ctf_field_type_sequence
*sequence_type
=
3501 container_of(type
, struct bt_ctf_field_type_sequence
,
3504 if (set_byte_order_funcs
[
3505 sequence_type
->element_type
->declaration
->id
]) {
3506 set_byte_order_funcs
[
3507 sequence_type
->element_type
->declaration
->id
](
3508 sequence_type
->element_type
, byte_order
, set_native
);
3513 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3514 struct bt_ctf_field_type
*type
)
3516 struct bt_ctf_field_type
*copy
;
3517 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3519 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3520 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3525 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3527 copy_integer
->declaration
= integer
->declaration
;
3528 if (integer
->mapped_clock
) {
3529 bt_get(integer
->mapped_clock
);
3530 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3533 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3540 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3541 struct bt_ctf_field_type
*type
)
3544 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3545 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3547 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3550 /* Copy the source enumeration's container */
3551 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3552 if (!copy_container
) {
3556 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3560 copy_enumeration
= container_of(copy
,
3561 struct bt_ctf_field_type_enumeration
, parent
);
3563 /* Copy all enumaration entries */
3564 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3565 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3566 enumeration
->entries
, i
);
3567 struct enumeration_mapping
*copy_mapping
= g_new0(
3568 struct enumeration_mapping
, 1);
3570 if (!copy_mapping
) {
3574 *copy_mapping
= *mapping
;
3575 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3578 copy_enumeration
->declaration
= enumeration
->declaration
;
3580 bt_put(copy_container
);
3583 bt_put(copy_container
);
3589 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3590 struct bt_ctf_field_type
*type
)
3592 struct bt_ctf_field_type
*copy
;
3593 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3595 floating_point
= container_of(type
,
3596 struct bt_ctf_field_type_floating_point
, parent
);
3597 copy
= bt_ctf_field_type_floating_point_create();
3602 copy_float
= container_of(copy
,
3603 struct bt_ctf_field_type_floating_point
, parent
);
3604 copy_float
->declaration
= floating_point
->declaration
;
3605 copy_float
->sign
= floating_point
->sign
;
3606 copy_float
->mantissa
= floating_point
->mantissa
;
3607 copy_float
->exp
= floating_point
->exp
;
3608 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3609 copy_float
->declaration
.sign
= ©_float
->sign
;
3610 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3611 copy_float
->declaration
.exp
= ©_float
->exp
;
3617 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3618 struct bt_ctf_field_type
*type
)
3621 GHashTableIter iter
;
3622 gpointer key
, value
;
3623 struct bt_ctf_field_type
*copy
;
3624 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3626 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3628 copy
= bt_ctf_field_type_structure_create();
3633 copy_structure
= container_of(copy
,
3634 struct bt_ctf_field_type_structure
, parent
);
3636 /* Copy field_name_to_index */
3637 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3638 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3639 g_hash_table_insert(copy_structure
->field_name_to_index
,
3643 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3644 struct structure_field
*entry
, *copy_entry
;
3645 struct bt_ctf_field_type
*copy_field
;
3647 copy_entry
= g_new0(struct structure_field
, 1);
3652 entry
= g_ptr_array_index(structure
->fields
, i
);
3653 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3659 copy_entry
->name
= entry
->name
;
3660 copy_entry
->type
= copy_field
;
3661 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3664 copy_structure
->declaration
= structure
->declaration
;
3673 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3674 struct bt_ctf_field_type
*type
)
3677 GHashTableIter iter
;
3678 gpointer key
, value
;
3679 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3680 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3682 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3685 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3691 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3692 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3697 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3700 /* Copy field_name_to_index */
3701 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3702 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3703 g_hash_table_insert(copy_variant
->field_name_to_index
,
3707 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3708 struct structure_field
*entry
, *copy_entry
;
3709 struct bt_ctf_field_type
*copy_field
;
3711 copy_entry
= g_new0(struct structure_field
, 1);
3716 entry
= g_ptr_array_index(variant
->fields
, i
);
3717 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3723 copy_entry
->name
= entry
->name
;
3724 copy_entry
->type
= copy_field
;
3725 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3728 copy_variant
->declaration
= variant
->declaration
;
3729 if (variant
->tag_field_path
) {
3730 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3731 variant
->tag_field_path
);
3732 if (!copy_variant
->tag_field_path
) {
3746 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3747 struct bt_ctf_field_type
*type
)
3749 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3750 struct bt_ctf_field_type_array
*array
, *copy_array
;
3752 array
= container_of(type
, struct bt_ctf_field_type_array
,
3754 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3755 if (!copy_element
) {
3759 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3764 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3766 copy_array
->declaration
= array
->declaration
;
3768 bt_put(copy_element
);
3773 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3774 struct bt_ctf_field_type
*type
)
3776 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3777 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3779 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3781 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3782 if (!copy_element
) {
3786 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3787 sequence
->length_field_name
->len
?
3788 sequence
->length_field_name
->str
: NULL
);
3793 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3795 copy_sequence
->declaration
= sequence
->declaration
;
3796 if (sequence
->length_field_path
) {
3797 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3798 sequence
->length_field_path
);
3799 if (!copy_sequence
->length_field_path
) {
3804 bt_put(copy_element
);
3812 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3813 struct bt_ctf_field_type
*type
)
3815 struct bt_ctf_field_type
*copy
;
3816 struct bt_ctf_field_type_string
*string
, *copy_string
;
3818 copy
= bt_ctf_field_type_string_create();
3823 string
= container_of(type
, struct bt_ctf_field_type_string
,
3825 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3827 copy_string
->declaration
= string
->declaration
;
3833 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3834 struct bt_ctf_field_type
*type_b
)
3837 struct bt_ctf_field_type_integer
*integer_a
;
3838 struct bt_ctf_field_type_integer
*integer_b
;
3839 struct declaration_integer
*decl_a
;
3840 struct declaration_integer
*decl_b
;
3842 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3844 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3846 decl_a
= &integer_a
->declaration
;
3847 decl_b
= &integer_b
->declaration
;
3850 if (decl_a
->len
!= decl_b
->len
) {
3855 * Compare user byte orders only, not the cached,
3858 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3863 if (decl_a
->signedness
!= decl_b
->signedness
) {
3868 if (decl_a
->base
!= decl_b
->base
) {
3873 if (decl_a
->encoding
!= decl_b
->encoding
) {
3878 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3890 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3891 struct bt_ctf_field_type
*type_b
)
3894 struct bt_ctf_field_type_floating_point
*float_a
;
3895 struct bt_ctf_field_type_floating_point
*float_b
;
3897 float_a
= container_of(type_a
,
3898 struct bt_ctf_field_type_floating_point
, parent
);
3899 float_b
= container_of(type_b
,
3900 struct bt_ctf_field_type_floating_point
, parent
);
3903 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3907 /* Exponent length */
3908 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3912 /* Mantissa length */
3913 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3918 * Compare user byte orders only, not the cached,
3921 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3933 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3934 struct enumeration_mapping
*mapping_b
)
3939 if (mapping_a
->string
!= mapping_b
->string
) {
3944 if (mapping_a
->range_start
._unsigned
!=
3945 mapping_b
->range_start
._unsigned
) {
3950 if (mapping_a
->range_end
._unsigned
!=
3951 mapping_b
->range_end
._unsigned
) {
3963 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3964 struct bt_ctf_field_type
*type_b
)
3968 struct bt_ctf_field_type_enumeration
*enum_a
;
3969 struct bt_ctf_field_type_enumeration
*enum_b
;
3971 enum_a
= container_of(type_a
,
3972 struct bt_ctf_field_type_enumeration
, parent
);
3973 enum_b
= container_of(type_b
,
3974 struct bt_ctf_field_type_enumeration
, parent
);
3976 /* Container field type */
3977 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3985 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3989 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3990 struct enumeration_mapping
*mapping_a
=
3991 g_ptr_array_index(enum_a
->entries
, i
);
3992 struct enumeration_mapping
*mapping_b
=
3993 g_ptr_array_index(enum_b
->entries
, i
);
3995 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
4008 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
4009 struct bt_ctf_field_type
*type_b
)
4012 struct bt_ctf_field_type_string
*string_a
;
4013 struct bt_ctf_field_type_string
*string_b
;
4015 string_a
= container_of(type_a
,
4016 struct bt_ctf_field_type_string
, parent
);
4017 string_b
= container_of(type_b
,
4018 struct bt_ctf_field_type_string
, parent
);
4021 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
4033 int compare_structure_fields(struct structure_field
*field_a
,
4034 struct structure_field
*field_b
)
4039 if (field_a
->name
!= field_b
->name
) {
4044 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
4051 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
4052 struct bt_ctf_field_type
*type_b
)
4056 struct bt_ctf_field_type_structure
*struct_a
;
4057 struct bt_ctf_field_type_structure
*struct_b
;
4059 struct_a
= container_of(type_a
,
4060 struct bt_ctf_field_type_structure
, parent
);
4061 struct_b
= container_of(type_b
,
4062 struct bt_ctf_field_type_structure
, parent
);
4065 if (bt_ctf_field_type_get_alignment(type_a
) !=
4066 bt_ctf_field_type_get_alignment(type_b
)) {
4071 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4075 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4076 struct structure_field
*field_a
=
4077 g_ptr_array_index(struct_a
->fields
, i
);
4078 struct structure_field
*field_b
=
4079 g_ptr_array_index(struct_b
->fields
, i
);
4081 ret
= compare_structure_fields(field_a
, field_b
);
4095 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4096 struct bt_ctf_field_type
*type_b
)
4100 struct bt_ctf_field_type_variant
*variant_a
;
4101 struct bt_ctf_field_type_variant
*variant_b
;
4103 variant_a
= container_of(type_a
,
4104 struct bt_ctf_field_type_variant
, parent
);
4105 variant_b
= container_of(type_b
,
4106 struct bt_ctf_field_type_variant
, parent
);
4109 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4114 ret
= bt_ctf_field_type_compare(
4115 (struct bt_ctf_field_type
*) variant_a
->tag
,
4116 (struct bt_ctf_field_type
*) variant_b
->tag
);
4124 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
4128 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
4129 struct structure_field
*field_a
=
4130 g_ptr_array_index(variant_a
->fields
, i
);
4131 struct structure_field
*field_b
=
4132 g_ptr_array_index(variant_b
->fields
, i
);
4134 ret
= compare_structure_fields(field_a
, field_b
);
4148 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4149 struct bt_ctf_field_type
*type_b
)
4152 struct bt_ctf_field_type_array
*array_a
;
4153 struct bt_ctf_field_type_array
*array_b
;
4155 array_a
= container_of(type_a
,
4156 struct bt_ctf_field_type_array
, parent
);
4157 array_b
= container_of(type_b
,
4158 struct bt_ctf_field_type_array
, parent
);
4161 if (array_a
->length
!= array_b
->length
) {
4166 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4167 array_b
->element_type
);
4174 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4175 struct bt_ctf_field_type
*type_b
)
4178 struct bt_ctf_field_type_sequence
*sequence_a
;
4179 struct bt_ctf_field_type_sequence
*sequence_b
;
4181 sequence_a
= container_of(type_a
,
4182 struct bt_ctf_field_type_sequence
, parent
);
4183 sequence_b
= container_of(type_b
,
4184 struct bt_ctf_field_type_sequence
, parent
);
4187 if (strcmp(sequence_a
->length_field_name
->str
,
4188 sequence_b
->length_field_name
->str
)) {
4193 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4194 sequence_b
->element_type
);
4200 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4201 struct bt_ctf_field_type
*type_b
)
4205 if (type_a
== type_b
) {
4206 /* Same reference: equal (even if both are NULL) */
4211 if (!type_a
|| !type_b
) {
4216 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4217 /* Different type IDs */
4221 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4222 /* Both have unknown type IDs */
4226 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4233 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4235 int field_count
= -1;
4236 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4239 case CTF_TYPE_STRUCT
:
4241 bt_ctf_field_type_structure_get_field_count(field_type
);
4243 case CTF_TYPE_VARIANT
:
4245 bt_ctf_field_type_variant_get_field_count(field_type
);
4247 case CTF_TYPE_ARRAY
:
4248 case CTF_TYPE_SEQUENCE
:
4250 * Array and sequence types always contain a single member
4251 * (the element type).
4263 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4264 struct bt_ctf_field_type
*field_type
, int index
)
4266 struct bt_ctf_field_type
*field
= NULL
;
4267 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4270 case CTF_TYPE_STRUCT
:
4271 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4274 case CTF_TYPE_VARIANT
:
4276 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4284 case CTF_TYPE_ARRAY
:
4285 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4287 case CTF_TYPE_SEQUENCE
:
4288 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4298 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4301 int field_index
= -1;
4302 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4305 case CTF_TYPE_STRUCT
:
4306 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4309 case CTF_TYPE_VARIANT
:
4310 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4320 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4321 struct bt_ctf_field_type
*type
)
4323 struct bt_ctf_field_path
*field_path
= NULL
;
4324 struct bt_ctf_field_type_variant
*variant
;
4326 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4330 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4332 field_path
= bt_get(variant
->tag_field_path
);
4337 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4338 struct bt_ctf_field_type
*type
)
4340 struct bt_ctf_field_path
*field_path
= NULL
;
4341 struct bt_ctf_field_type_sequence
*sequence
;
4343 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4347 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4349 field_path
= bt_get(sequence
->length_field_path
);