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_signed(
1150 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1151 const char **mapping_name
, int64_t *range_begin
,
1161 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1162 &iter
->enumeration_type
->parent
, iter
->index
,
1163 mapping_name
, range_begin
, range_end
);
1168 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1169 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1170 const char **mapping_name
, uint64_t *range_begin
,
1171 uint64_t *range_end
)
1180 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1181 &iter
->enumeration_type
->parent
, iter
->index
,
1182 mapping_name
, range_begin
, range_end
);
1187 int bt_ctf_field_type_enumeration_get_mapping_signed(
1188 struct bt_ctf_field_type
*enum_field_type
,
1190 const char **mapping_name
, int64_t *range_begin
,
1194 struct enumeration_mapping
*mapping
;
1196 if (!enum_field_type
|| index
< 0) {
1201 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1208 *mapping_name
= g_quark_to_string(mapping
->string
);
1212 *range_begin
= mapping
->range_start
._signed
;
1216 *range_end
= mapping
->range_end
._signed
;
1222 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1223 struct bt_ctf_field_type
*enum_field_type
,
1225 const char **mapping_name
, uint64_t *range_begin
,
1226 uint64_t *range_end
)
1229 struct enumeration_mapping
*mapping
;
1231 if (!enum_field_type
|| index
< 0) {
1236 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1243 *mapping_name
= g_quark_to_string(mapping
->string
);
1247 *range_begin
= mapping
->range_start
._unsigned
;
1251 *range_end
= mapping
->range_end
._unsigned
;
1257 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1258 struct bt_ctf_field_type
*integer_container_type
)
1260 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1262 if (!integer_container_type
) {
1266 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
1270 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1275 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
1276 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
1277 bt_get(integer_container_type
);
1278 enumeration
->container
= integer_container_type
;
1279 enumeration
->entries
= g_ptr_array_new_with_free_func(
1280 (GDestroyNotify
)destroy_enumeration_mapping
);
1281 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1282 return &enumeration
->parent
;
1284 g_free(enumeration
);
1288 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1289 struct bt_ctf_field_type
*type
)
1291 struct bt_ctf_field_type
*container_type
= NULL
;
1292 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1298 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
1302 enumeration_type
= container_of(type
,
1303 struct bt_ctf_field_type_enumeration
, parent
);
1304 container_type
= enumeration_type
->container
;
1305 bt_get(container_type
);
1307 return container_type
;
1310 int bt_ctf_field_type_enumeration_add_mapping(
1311 struct bt_ctf_field_type
*type
, const char *string
,
1312 int64_t range_start
, int64_t range_end
)
1315 GQuark mapping_name
;
1316 struct enumeration_mapping
*mapping
;
1317 struct bt_ctf_field_type_enumeration
*enumeration
;
1318 char *escaped_string
;
1320 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1322 (range_end
< range_start
)) {
1327 if (!string
|| strlen(string
) == 0) {
1332 escaped_string
= g_strescape(string
, NULL
);
1333 if (!escaped_string
) {
1338 mapping
= g_new(struct enumeration_mapping
, 1);
1343 mapping_name
= g_quark_from_string(escaped_string
);
1344 *mapping
= (struct enumeration_mapping
) {
1345 .range_start
._signed
= range_start
,
1346 .range_end
._signed
= range_end
,
1347 .string
= mapping_name
,
1349 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1351 g_ptr_array_add(enumeration
->entries
, mapping
);
1352 g_ptr_array_sort(enumeration
->entries
,
1353 (GCompareFunc
)compare_enumeration_mappings_signed
);
1355 free(escaped_string
);
1360 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1361 struct bt_ctf_field_type
*type
, const char *string
,
1362 uint64_t range_start
, uint64_t range_end
)
1365 GQuark mapping_name
;
1366 struct enumeration_mapping
*mapping
;
1367 struct bt_ctf_field_type_enumeration
*enumeration
;
1368 char *escaped_string
;
1370 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1372 (range_end
< range_start
)) {
1377 if (!string
|| strlen(string
) == 0) {
1382 escaped_string
= g_strescape(string
, NULL
);
1383 if (!escaped_string
) {
1388 mapping
= g_new(struct enumeration_mapping
, 1);
1393 mapping_name
= g_quark_from_string(escaped_string
);
1394 *mapping
= (struct enumeration_mapping
) {
1395 .range_start
._unsigned
= range_start
,
1396 .range_end
._unsigned
= range_end
,
1397 .string
= mapping_name
,
1399 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1401 g_ptr_array_add(enumeration
->entries
, mapping
);
1402 g_ptr_array_sort(enumeration
->entries
,
1403 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1405 free(escaped_string
);
1410 int bt_ctf_field_type_enumeration_get_mapping_count(
1411 struct bt_ctf_field_type
*type
)
1414 struct bt_ctf_field_type_enumeration
*enumeration
;
1416 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1421 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1423 ret
= (int) enumeration
->entries
->len
;
1428 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1430 struct bt_ctf_field_type_floating_point
*floating_point
=
1431 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1433 if (!floating_point
) {
1437 floating_point
->declaration
.sign
= &floating_point
->sign
;
1438 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1439 floating_point
->declaration
.exp
= &floating_point
->exp
;
1440 floating_point
->sign
.len
= 1;
1441 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1442 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1443 floating_point
->declaration
.exp
->len
=
1444 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1445 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1446 floating_point
->sign
.p
.alignment
= 1;
1447 floating_point
->mantissa
.p
.alignment
= 1;
1448 floating_point
->exp
.p
.alignment
= 1;
1450 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1452 return floating_point
? &floating_point
->parent
: NULL
;
1455 int bt_ctf_field_type_floating_point_get_exponent_digits(
1456 struct bt_ctf_field_type
*type
)
1459 struct bt_ctf_field_type_floating_point
*floating_point
;
1461 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1466 floating_point
= container_of(type
,
1467 struct bt_ctf_field_type_floating_point
, parent
);
1468 ret
= (int) floating_point
->declaration
.exp
->len
;
1473 int bt_ctf_field_type_floating_point_set_exponent_digits(
1474 struct bt_ctf_field_type
*type
,
1475 unsigned int exponent_digits
)
1478 struct bt_ctf_field_type_floating_point
*floating_point
;
1480 if (!type
|| type
->frozen
||
1481 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1486 floating_point
= container_of(type
,
1487 struct bt_ctf_field_type_floating_point
, parent
);
1488 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1489 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1491 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1496 floating_point
->declaration
.exp
->len
= exponent_digits
;
1501 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1502 struct bt_ctf_field_type
*type
)
1505 struct bt_ctf_field_type_floating_point
*floating_point
;
1507 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1512 floating_point
= container_of(type
,
1513 struct bt_ctf_field_type_floating_point
, parent
);
1514 ret
= (int) floating_point
->mantissa
.len
+ 1;
1519 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1520 struct bt_ctf_field_type
*type
,
1521 unsigned int mantissa_digits
)
1524 struct bt_ctf_field_type_floating_point
*floating_point
;
1526 if (!type
|| type
->frozen
||
1527 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1532 floating_point
= container_of(type
,
1533 struct bt_ctf_field_type_floating_point
, parent
);
1535 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1536 (mantissa_digits
!= DBL_MANT_DIG
) &&
1537 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1542 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1547 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1549 struct bt_ctf_field_type_structure
*structure
=
1550 g_new0(struct bt_ctf_field_type_structure
, 1);
1556 structure
->parent
.declaration
= &structure
->declaration
.p
;
1557 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1558 structure
->fields
= g_ptr_array_new_with_free_func(
1559 (GDestroyNotify
)destroy_structure_field
);
1560 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1561 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1562 return &structure
->parent
;
1567 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1568 struct bt_ctf_field_type
*field_type
,
1569 const char *field_name
)
1572 struct bt_ctf_field_type_structure
*structure
;
1575 * TODO: check that `field_type` does not contain `type`,
1578 if (!type
|| !field_type
|| type
->frozen
||
1579 bt_ctf_validate_identifier(field_name
) ||
1580 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
) ||
1581 type
== field_type
) {
1586 structure
= container_of(type
,
1587 struct bt_ctf_field_type_structure
, parent
);
1588 if (add_structure_field(structure
->fields
,
1589 structure
->field_name_to_index
, field_type
, field_name
)) {
1597 int bt_ctf_field_type_structure_get_field_count(
1598 struct bt_ctf_field_type
*type
)
1601 struct bt_ctf_field_type_structure
*structure
;
1603 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1608 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1610 ret
= (int) structure
->fields
->len
;
1615 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1616 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1619 struct bt_ctf_field_type_structure
*structure
;
1620 struct structure_field
*field
;
1623 if (!type
|| index
< 0 ||
1624 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1629 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1631 if (index
>= structure
->fields
->len
) {
1636 field
= g_ptr_array_index(structure
->fields
, index
);
1638 *field_type
= field
->type
;
1639 bt_get(field
->type
);
1642 *field_name
= g_quark_to_string(field
->name
);
1648 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1649 struct bt_ctf_field_type
*type
,
1654 struct structure_field
*field
;
1655 struct bt_ctf_field_type_structure
*structure
;
1656 struct bt_ctf_field_type
*field_type
= NULL
;
1658 if (!type
|| !name
) {
1662 name_quark
= g_quark_try_string(name
);
1667 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1669 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1670 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1674 field
= structure
->fields
->pdata
[index
];
1675 field_type
= field
->type
;
1681 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1682 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1684 struct bt_ctf_field_type_variant
*variant
= NULL
;
1686 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1690 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1695 variant
->parent
.declaration
= &variant
->declaration
.p
;
1696 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1697 variant
->tag_name
= g_string_new(tag_name
);
1698 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1699 variant
->fields
= g_ptr_array_new_with_free_func(
1700 (GDestroyNotify
) destroy_structure_field
);
1703 variant
->tag
= container_of(enum_tag
,
1704 struct bt_ctf_field_type_enumeration
, parent
);
1707 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1708 /* A variant's alignment is undefined */
1709 variant
->parent
.declaration
->alignment
= 0;
1710 return &variant
->parent
;
1715 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1716 struct bt_ctf_field_type
*type
)
1718 struct bt_ctf_field_type_variant
*variant
;
1719 struct bt_ctf_field_type
*tag_type
= NULL
;
1721 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1725 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1726 if (!variant
->tag
) {
1730 tag_type
= &variant
->tag
->parent
;
1736 const char *bt_ctf_field_type_variant_get_tag_name(
1737 struct bt_ctf_field_type
*type
)
1739 struct bt_ctf_field_type_variant
*variant
;
1740 const char *tag_name
= NULL
;
1742 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1746 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1747 if (variant
->tag_name
->len
== 0) {
1751 tag_name
= variant
->tag_name
->str
;
1756 int bt_ctf_field_type_variant_set_tag_name(
1757 struct bt_ctf_field_type
*type
, const char *name
)
1760 struct bt_ctf_field_type_variant
*variant
;
1762 if (!type
|| type
->frozen
||
1763 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1764 bt_ctf_validate_identifier(name
)) {
1769 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1770 g_string_assign(variant
->tag_name
, name
);
1775 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1776 struct bt_ctf_field_type
*field_type
,
1777 const char *field_name
)
1781 struct bt_ctf_field_type_variant
*variant
;
1782 GQuark field_name_quark
= g_quark_from_string(field_name
);
1785 * TODO: check that `field_type` does not contain `type`,
1788 if (!type
|| !field_type
|| type
->frozen
||
1789 bt_ctf_validate_identifier(field_name
) ||
1790 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1791 type
== field_type
) {
1796 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1798 /* The user has explicitly provided a tag; validate against it. */
1802 /* Make sure this name is present in the enum tag */
1803 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1804 struct enumeration_mapping
*mapping
=
1805 g_ptr_array_index(variant
->tag
->entries
, i
);
1807 if (mapping
->string
== field_name_quark
) {
1814 /* Validation failed */
1820 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1821 field_type
, field_name
)) {
1829 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1830 struct bt_ctf_field_type
*type
,
1831 const char *field_name
)
1835 struct structure_field
*field
;
1836 struct bt_ctf_field_type_variant
*variant
;
1837 struct bt_ctf_field_type
*field_type
= NULL
;
1839 if (!type
|| !field_name
) {
1843 name_quark
= g_quark_try_string(field_name
);
1848 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1849 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1850 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1854 field
= g_ptr_array_index(variant
->fields
, index
);
1855 field_type
= field
->type
;
1861 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1862 struct bt_ctf_field_type
*type
,
1863 struct bt_ctf_field
*tag
)
1866 const char *enum_value
;
1867 struct bt_ctf_field_type
*field_type
= NULL
;
1868 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1870 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1874 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1879 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
,
1880 &enum_value
, NULL
, NULL
);
1885 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1892 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1895 struct bt_ctf_field_type_variant
*variant
;
1897 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1902 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1904 ret
= (int) variant
->fields
->len
;
1910 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1911 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1914 struct bt_ctf_field_type_variant
*variant
;
1915 struct structure_field
*field
;
1918 if (!type
|| index
< 0 ||
1919 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1924 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1926 if (index
>= variant
->fields
->len
) {
1931 field
= g_ptr_array_index(variant
->fields
, index
);
1933 *field_type
= field
->type
;
1934 bt_get(field
->type
);
1937 *field_name
= g_quark_to_string(field
->name
);
1943 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1944 struct bt_ctf_field_type
*element_type
,
1945 unsigned int length
)
1947 struct bt_ctf_field_type_array
*array
= NULL
;
1949 if (!element_type
|| length
== 0) {
1953 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1958 array
->parent
.declaration
= &array
->declaration
.p
;
1959 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1961 bt_get(element_type
);
1962 array
->element_type
= element_type
;
1963 array
->length
= length
;
1964 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1965 return &array
->parent
;
1970 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1971 struct bt_ctf_field_type
*type
)
1973 struct bt_ctf_field_type
*ret
= NULL
;
1974 struct bt_ctf_field_type_array
*array
;
1976 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1980 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1981 ret
= array
->element_type
;
1988 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1989 struct bt_ctf_field_type
*element_type
)
1992 struct bt_ctf_field_type_array
*array
;
1994 if (!type
|| !element_type
||
1995 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2000 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2002 if (array
->element_type
) {
2003 BT_PUT(array
->element_type
);
2006 array
->element_type
= element_type
;
2007 bt_get(array
->element_type
);
2013 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2016 struct bt_ctf_field_type_array
*array
;
2018 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2023 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2024 ret
= (int64_t) array
->length
;
2029 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2030 struct bt_ctf_field_type
*element_type
,
2031 const char *length_field_name
)
2033 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2035 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2039 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2044 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
2045 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
2046 bt_get(element_type
);
2047 sequence
->element_type
= element_type
;
2048 sequence
->length_field_name
= g_string_new(length_field_name
);
2049 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2050 return &sequence
->parent
;
2055 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2056 struct bt_ctf_field_type
*type
)
2058 struct bt_ctf_field_type
*ret
= NULL
;
2059 struct bt_ctf_field_type_sequence
*sequence
;
2061 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2065 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2067 ret
= sequence
->element_type
;
2074 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2075 struct bt_ctf_field_type
*element_type
)
2078 struct bt_ctf_field_type_sequence
*sequence
;
2080 if (!type
|| !element_type
||
2081 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2086 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2088 if (sequence
->element_type
) {
2089 BT_PUT(sequence
->element_type
);
2092 sequence
->element_type
= element_type
;
2093 bt_get(sequence
->element_type
);
2099 const char *bt_ctf_field_type_sequence_get_length_field_name(
2100 struct bt_ctf_field_type
*type
)
2102 const char *ret
= NULL
;
2103 struct bt_ctf_field_type_sequence
*sequence
;
2105 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2109 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2111 ret
= sequence
->length_field_name
->str
;
2116 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2118 struct bt_ctf_field_type_string
*string
=
2119 g_new0(struct bt_ctf_field_type_string
, 1);
2125 string
->parent
.declaration
= &string
->declaration
.p
;
2126 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
2127 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2128 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2129 string
->parent
.declaration
->alignment
= CHAR_BIT
;
2130 return &string
->parent
;
2133 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2134 struct bt_ctf_field_type
*type
)
2136 struct bt_ctf_field_type_string
*string
;
2137 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2139 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
2143 string
= container_of(type
, struct bt_ctf_field_type_string
,
2145 ret
= string
->declaration
.encoding
;
2150 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2151 enum bt_ctf_string_encoding encoding
)
2154 struct bt_ctf_field_type_string
*string
;
2156 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2157 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2158 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2163 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2164 string
->declaration
.encoding
= encoding
;
2169 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2172 enum bt_ctf_type_id type_id
;
2180 ret
= (int) type
->declaration
->alignment
;
2184 type_id
= bt_ctf_field_type_get_type_id(type
);
2186 case BT_CTF_TYPE_ID_SEQUENCE
:
2188 struct bt_ctf_field_type
*element
=
2189 bt_ctf_field_type_sequence_get_element_type(type
);
2196 ret
= bt_ctf_field_type_get_alignment(element
);
2200 case BT_CTF_TYPE_ID_ARRAY
:
2202 struct bt_ctf_field_type
*element
=
2203 bt_ctf_field_type_array_get_element_type(type
);
2210 ret
= bt_ctf_field_type_get_alignment(element
);
2214 case BT_CTF_TYPE_ID_STRUCT
:
2216 int i
, element_count
;
2218 element_count
= bt_ctf_field_type_structure_get_field_count(
2220 if (element_count
< 0) {
2221 ret
= element_count
;
2225 for (i
= 0; i
< element_count
; i
++) {
2226 struct bt_ctf_field_type
*field
;
2227 int field_alignment
;
2229 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2236 field_alignment
= bt_ctf_field_type_get_alignment(
2239 if (field_alignment
< 0) {
2240 ret
= field_alignment
;
2244 type
->declaration
->alignment
= MAX(field_alignment
,
2245 type
->declaration
->alignment
);
2247 ret
= (int) type
->declaration
->alignment
;
2250 case BT_CTF_TYPE_ID_UNKNOWN
:
2254 ret
= (int) type
->declaration
->alignment
;
2262 int is_power_of_two(unsigned int value
)
2264 return ((value
& (value
- 1)) == 0) && value
> 0;
2267 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2268 unsigned int alignment
)
2271 enum bt_ctf_type_id type_id
;
2273 /* Alignment must be a power of two */
2274 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2279 type_id
= bt_ctf_field_type_get_type_id(type
);
2280 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2285 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2286 alignment
!= CHAR_BIT
) {
2291 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2292 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2293 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2294 /* Setting an alignment on these types makes no sense */
2299 type
->declaration
->alignment
= alignment
;
2305 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2306 struct bt_ctf_field_type
*type
)
2308 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2314 switch (type
->declaration
->id
) {
2315 case BT_CTF_TYPE_ID_INTEGER
:
2317 struct bt_ctf_field_type_integer
*integer
= container_of(
2318 type
, struct bt_ctf_field_type_integer
, parent
);
2319 ret
= integer
->user_byte_order
;
2322 case BT_CTF_TYPE_ID_ENUM
:
2324 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2325 type
, struct bt_ctf_field_type_enumeration
, parent
);
2326 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2329 case BT_CTF_TYPE_ID_FLOAT
:
2331 struct bt_ctf_field_type_floating_point
*floating_point
=
2333 struct bt_ctf_field_type_floating_point
,
2335 ret
= floating_point
->user_byte_order
;
2342 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2343 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2344 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2345 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2351 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2352 enum bt_ctf_byte_order byte_order
)
2355 int internal_byte_order
;
2356 enum bt_ctf_type_id type_id
;
2358 if (!type
|| type
->frozen
) {
2363 switch (byte_order
) {
2364 case BT_CTF_BYTE_ORDER_NATIVE
:
2365 /* Leave unset. Will be initialized by parent. */
2366 internal_byte_order
= 0;
2368 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2369 internal_byte_order
= LITTLE_ENDIAN
;
2371 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2372 case BT_CTF_BYTE_ORDER_NETWORK
:
2373 internal_byte_order
= BIG_ENDIAN
;
2380 type_id
= type
->declaration
->id
;
2381 if (set_byte_order_funcs
[type_id
]) {
2382 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2388 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2389 struct bt_ctf_field_type
*type
)
2392 return BT_CTF_TYPE_ID_UNKNOWN
;
2395 return type
->declaration
->id
;
2398 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2400 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2403 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2405 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2408 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2410 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2413 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2415 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2418 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2420 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2423 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2425 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2428 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2430 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2433 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2435 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2438 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2443 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2449 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2459 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2460 struct bt_ctf_field_type_variant
*variant
,
2463 struct bt_ctf_field_type
*type
= NULL
;
2464 GQuark field_name_quark
;
2466 struct structure_field
*field_entry
;
2467 struct range_overlap_query query
= {
2468 .range_start
._signed
= tag_value
,
2469 .range_end
._signed
= tag_value
,
2474 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2476 if (!query
.overlaps
) {
2480 field_name_quark
= query
.mapping_name
;
2481 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2482 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2486 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2487 type
= field_entry
->type
;
2493 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2494 struct bt_ctf_field_type_variant
*variant
,
2497 struct bt_ctf_field_type
*type
= NULL
;
2498 GQuark field_name_quark
;
2500 struct structure_field
*field_entry
;
2501 struct range_overlap_query query
= {
2502 .range_start
._unsigned
= tag_value
,
2503 .range_end
._unsigned
= tag_value
,
2508 g_ptr_array_foreach(variant
->tag
->entries
,
2509 check_ranges_overlap_unsigned
,
2511 if (!query
.overlaps
) {
2515 field_name_quark
= query
.mapping_name
;
2516 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2517 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2521 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2522 type
= field_entry
->type
;
2528 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2529 struct metadata_context
*context
)
2533 if (!type
|| !context
) {
2538 /* Make sure field type is valid before serializing it */
2539 ret
= bt_ctf_field_type_validate(type
);
2545 ret
= type
->serialize(type
, context
);
2551 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2558 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2559 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2560 set_byte_order_funcs
[type
->declaration
->id
](type
,
2565 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2567 struct bt_ctf_field_type
*copy
= NULL
;
2573 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2579 int bt_ctf_field_type_structure_get_field_name_index(
2580 struct bt_ctf_field_type
*type
, const char *name
)
2585 struct bt_ctf_field_type_structure
*structure
;
2587 if (!type
|| !name
||
2588 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2593 name_quark
= g_quark_try_string(name
);
2599 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2601 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2602 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2612 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2613 struct bt_ctf_field_type
*field
, int index
)
2616 struct bt_ctf_field_type_structure
*structure
;
2618 if (!type
|| !field
||
2619 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2624 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2626 if (index
< 0 || index
>= structure
->fields
->len
) {
2632 bt_put(((struct structure_field
*)
2633 g_ptr_array_index(structure
->fields
, index
))->type
);
2634 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2641 int bt_ctf_field_type_variant_get_field_name_index(
2642 struct bt_ctf_field_type
*type
, const char *name
)
2647 struct bt_ctf_field_type_variant
*variant
;
2649 if (!type
|| !name
||
2650 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2655 name_quark
= g_quark_try_string(name
);
2661 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2663 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2664 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2674 int bt_ctf_field_type_sequence_set_length_field_path(
2675 struct bt_ctf_field_type
*type
,
2676 struct bt_ctf_field_path
*path
)
2679 struct bt_ctf_field_type_sequence
*sequence
;
2681 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2682 BT_CTF_TYPE_ID_SEQUENCE
) {
2687 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2690 BT_MOVE(sequence
->length_field_path
, path
);
2696 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2697 struct bt_ctf_field_path
*path
)
2700 struct bt_ctf_field_type_variant
*variant
;
2702 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2703 BT_CTF_TYPE_ID_VARIANT
) {
2708 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2711 BT_MOVE(variant
->tag_field_path
, path
);
2717 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2718 struct bt_ctf_field_type
*tag
)
2721 struct bt_ctf_field_type_variant
*variant
;
2723 if (!type
|| !tag
||
2724 bt_ctf_field_type_get_type_id(tag
) !=
2725 BT_CTF_TYPE_ID_ENUM
) {
2730 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2734 bt_put(&variant
->tag
->parent
);
2736 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2743 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2744 struct bt_ctf_field_type
*field
, int index
)
2747 struct bt_ctf_field_type_variant
*variant
;
2749 if (!type
|| !field
||
2750 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2755 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2757 if (index
< 0 || index
>= variant
->fields
->len
) {
2763 bt_put(((struct structure_field
*)
2764 g_ptr_array_index(variant
->fields
, index
))->type
);
2765 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2772 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2774 struct bt_ctf_field_type_integer
*integer
=
2775 (struct bt_ctf_field_type_integer
*) type
;
2781 bt_put(integer
->mapped_clock
);
2786 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2788 struct bt_ctf_field_type_enumeration
*enumeration
=
2789 (struct bt_ctf_field_type_enumeration
*) type
;
2795 g_ptr_array_free(enumeration
->entries
, TRUE
);
2796 bt_put(enumeration
->container
);
2797 g_free(enumeration
);
2801 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2803 struct bt_ctf_field_type_floating_point
*floating_point
=
2804 (struct bt_ctf_field_type_floating_point
*) type
;
2810 g_free(floating_point
);
2814 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2816 struct bt_ctf_field_type_structure
*structure
=
2817 (struct bt_ctf_field_type_structure
*) type
;
2823 g_ptr_array_free(structure
->fields
, TRUE
);
2824 g_hash_table_destroy(structure
->field_name_to_index
);
2829 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2831 struct bt_ctf_field_type_variant
*variant
=
2832 (struct bt_ctf_field_type_variant
*) type
;
2838 g_ptr_array_free(variant
->fields
, TRUE
);
2839 g_hash_table_destroy(variant
->field_name_to_index
);
2840 g_string_free(variant
->tag_name
, TRUE
);
2841 bt_put(&variant
->tag
->parent
);
2842 BT_PUT(variant
->tag_field_path
);
2847 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2849 struct bt_ctf_field_type_array
*array
=
2850 (struct bt_ctf_field_type_array
*) type
;
2856 bt_put(array
->element_type
);
2861 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2863 struct bt_ctf_field_type_sequence
*sequence
=
2864 (struct bt_ctf_field_type_sequence
*) type
;
2870 bt_put(sequence
->element_type
);
2871 g_string_free(sequence
->length_field_name
, TRUE
);
2872 BT_PUT(sequence
->length_field_path
);
2877 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2879 struct bt_ctf_field_type_string
*string
=
2880 (struct bt_ctf_field_type_string
*) type
;
2890 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2896 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2898 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2899 type
, struct bt_ctf_field_type_integer
, parent
);
2901 if (integer_type
->mapped_clock
) {
2902 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2905 generic_field_type_freeze(type
);
2909 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2911 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2912 type
, struct bt_ctf_field_type_enumeration
, parent
);
2914 set_enumeration_range_overlap(type
);
2916 generic_field_type_freeze(type
);
2917 bt_ctf_field_type_freeze(enumeration_type
->container
);
2921 void freeze_structure_field(struct structure_field
*field
)
2923 bt_ctf_field_type_freeze(field
->type
);
2927 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2929 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2930 type
, struct bt_ctf_field_type_structure
, parent
);
2932 /* Cache the alignment */
2933 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2934 generic_field_type_freeze(type
);
2935 g_ptr_array_foreach(structure_type
->fields
,
2936 (GFunc
) freeze_structure_field
, NULL
);
2940 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2942 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2943 type
, struct bt_ctf_field_type_variant
, parent
);
2945 generic_field_type_freeze(type
);
2946 g_ptr_array_foreach(variant_type
->fields
,
2947 (GFunc
) freeze_structure_field
, NULL
);
2951 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2953 struct bt_ctf_field_type_array
*array_type
= container_of(
2954 type
, struct bt_ctf_field_type_array
, parent
);
2956 /* Cache the alignment */
2957 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2958 generic_field_type_freeze(type
);
2959 bt_ctf_field_type_freeze(array_type
->element_type
);
2963 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2965 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2966 type
, struct bt_ctf_field_type_sequence
, parent
);
2968 /* Cache the alignment */
2969 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2970 generic_field_type_freeze(type
);
2971 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2975 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2977 const char *encoding_string
;
2980 case BT_CTF_STRING_ENCODING_NONE
:
2981 encoding_string
= "none";
2983 case BT_CTF_STRING_ENCODING_ASCII
:
2984 encoding_string
= "ASCII";
2986 case BT_CTF_STRING_ENCODING_UTF8
:
2987 encoding_string
= "UTF8";
2990 encoding_string
= "unknown";
2994 return encoding_string
;
2998 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
3000 const char *base_string
;
3003 case BT_CTF_INTEGER_BASE_DECIMAL
:
3004 base_string
= "decimal";
3006 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
3007 base_string
= "hexadecimal";
3009 case BT_CTF_INTEGER_BASE_OCTAL
:
3010 base_string
= "octal";
3012 case BT_CTF_INTEGER_BASE_BINARY
:
3013 base_string
= "binary";
3016 base_string
= "unknown";
3024 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3025 struct metadata_context
*context
)
3027 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3028 struct bt_ctf_field_type_integer
, parent
);
3031 g_string_append_printf(context
->string
,
3032 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
3033 integer
->declaration
.len
, type
->declaration
->alignment
,
3034 (integer
->declaration
.signedness
? "true" : "false"),
3035 get_encoding_string(integer
->declaration
.encoding
),
3036 get_integer_base_string(integer
->declaration
.base
),
3037 get_byte_order_string(integer
->declaration
.byte_order
));
3038 if (integer
->mapped_clock
) {
3039 const char *clock_name
= bt_ctf_clock_class_get_name(
3040 integer
->mapped_clock
);
3047 g_string_append_printf(context
->string
,
3048 "; map = clock.%s.value", clock_name
);
3051 g_string_append(context
->string
, "; }");
3057 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3058 struct metadata_context
*context
)
3062 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3063 struct bt_ctf_field_type_enumeration
, parent
);
3064 struct bt_ctf_field_type
*container_type
;
3065 int container_signed
;
3067 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3068 if (!container_type
) {
3073 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3074 if (container_signed
< 0) {
3075 ret
= container_signed
;
3076 goto error_put_container_type
;
3079 g_string_append(context
->string
, "enum : ");
3080 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3082 goto error_put_container_type
;
3085 g_string_append(context
->string
, " { ");
3086 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3087 struct enumeration_mapping
*mapping
=
3088 enumeration
->entries
->pdata
[entry
];
3090 if (container_signed
) {
3091 if (mapping
->range_start
._signed
==
3092 mapping
->range_end
._signed
) {
3093 g_string_append_printf(context
->string
,
3094 "\"%s\" = %" PRId64
,
3095 g_quark_to_string(mapping
->string
),
3096 mapping
->range_start
._signed
);
3098 g_string_append_printf(context
->string
,
3099 "\"%s\" = %" PRId64
" ... %" PRId64
,
3100 g_quark_to_string(mapping
->string
),
3101 mapping
->range_start
._signed
,
3102 mapping
->range_end
._signed
);
3105 if (mapping
->range_start
._unsigned
==
3106 mapping
->range_end
._unsigned
) {
3107 g_string_append_printf(context
->string
,
3108 "\"%s\" = %" PRIu64
,
3109 g_quark_to_string(mapping
->string
),
3110 mapping
->range_start
._unsigned
);
3112 g_string_append_printf(context
->string
,
3113 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3114 g_quark_to_string(mapping
->string
),
3115 mapping
->range_start
._unsigned
,
3116 mapping
->range_end
._unsigned
);
3120 g_string_append(context
->string
,
3121 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3125 if (context
->field_name
->len
) {
3126 g_string_append_printf(context
->string
, " %s",
3127 context
->field_name
->str
);
3128 g_string_assign(context
->field_name
, "");
3130 error_put_container_type
:
3131 bt_put(container_type
);
3137 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3138 struct metadata_context
*context
)
3140 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3141 type
, struct bt_ctf_field_type_floating_point
, parent
);
3143 g_string_append_printf(context
->string
,
3144 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
3145 floating_point
->declaration
.exp
->len
,
3146 floating_point
->declaration
.mantissa
->len
+ 1,
3147 get_byte_order_string(floating_point
->declaration
.byte_order
),
3148 type
->declaration
->alignment
);
3153 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3154 struct metadata_context
*context
)
3157 unsigned int indent
;
3159 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3160 struct bt_ctf_field_type_structure
, parent
);
3161 GString
*structure_field_name
= context
->field_name
;
3163 context
->field_name
= g_string_new("");
3165 context
->current_indentation_level
++;
3166 g_string_append(context
->string
, "struct {\n");
3168 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3169 struct structure_field
*field
;
3171 for (indent
= 0; indent
< context
->current_indentation_level
;
3173 g_string_append_c(context
->string
, '\t');
3176 field
= structure
->fields
->pdata
[i
];
3177 g_string_assign(context
->field_name
,
3178 g_quark_to_string(field
->name
));
3179 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3184 if (context
->field_name
->len
) {
3185 g_string_append_printf(context
->string
, " %s",
3186 context
->field_name
->str
);
3188 g_string_append(context
->string
, ";\n");
3191 context
->current_indentation_level
--;
3192 for (indent
= 0; indent
< context
->current_indentation_level
;
3194 g_string_append_c(context
->string
, '\t');
3197 g_string_append_printf(context
->string
, "} align(%zu)",
3198 type
->declaration
->alignment
);
3200 g_string_free(context
->field_name
, TRUE
);
3201 context
->field_name
= structure_field_name
;
3206 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3207 struct metadata_context
*context
)
3210 unsigned int indent
;
3212 struct bt_ctf_field_type_variant
*variant
= container_of(
3213 type
, struct bt_ctf_field_type_variant
, parent
);
3214 GString
*variant_field_name
= context
->field_name
;
3216 context
->field_name
= g_string_new("");
3217 if (variant
->tag_name
->len
> 0) {
3218 g_string_append_printf(context
->string
,
3219 "variant <%s> {\n", variant
->tag_name
->str
);
3221 g_string_append(context
->string
, "variant {\n");
3224 context
->current_indentation_level
++;
3225 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3226 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3228 g_string_assign(context
->field_name
,
3229 g_quark_to_string(field
->name
));
3230 for (indent
= 0; indent
< context
->current_indentation_level
;
3232 g_string_append_c(context
->string
, '\t');
3235 g_string_assign(context
->field_name
,
3236 g_quark_to_string(field
->name
));
3237 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3242 if (context
->field_name
->len
) {
3243 g_string_append_printf(context
->string
, " %s;",
3244 context
->field_name
->str
);
3247 g_string_append_c(context
->string
, '\n');
3250 context
->current_indentation_level
--;
3251 for (indent
= 0; indent
< context
->current_indentation_level
;
3253 g_string_append_c(context
->string
, '\t');
3256 g_string_append(context
->string
, "}");
3258 g_string_free(context
->field_name
, TRUE
);
3259 context
->field_name
= variant_field_name
;
3264 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3265 struct metadata_context
*context
)
3268 struct bt_ctf_field_type_array
*array
= container_of(type
,
3269 struct bt_ctf_field_type_array
, parent
);
3271 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3276 if (context
->field_name
->len
) {
3277 g_string_append_printf(context
->string
, " %s[%u]",
3278 context
->field_name
->str
, array
->length
);
3279 g_string_assign(context
->field_name
, "");
3281 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3288 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3289 struct metadata_context
*context
)
3292 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3293 type
, struct bt_ctf_field_type_sequence
, parent
);
3295 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3300 if (context
->field_name
->len
) {
3301 g_string_append_printf(context
->string
, " %s[%s]",
3302 context
->field_name
->str
,
3303 sequence
->length_field_name
->str
);
3304 g_string_assign(context
->field_name
, "");
3306 g_string_append_printf(context
->string
, "[%s]",
3307 sequence
->length_field_name
->str
);
3314 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3315 struct metadata_context
*context
)
3317 struct bt_ctf_field_type_string
*string
= container_of(
3318 type
, struct bt_ctf_field_type_string
, parent
);
3320 g_string_append_printf(context
->string
,
3321 "string { encoding = %s; }",
3322 get_encoding_string(string
->declaration
.encoding
));
3327 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3328 enum bt_ctf_byte_order ret
;
3330 switch (byte_order
) {
3331 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3333 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3335 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3337 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3339 case BT_CTF_BYTE_ORDER_NETWORK
:
3340 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3342 case BT_CTF_BYTE_ORDER_NATIVE
:
3343 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3346 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3354 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3355 int byte_order
, int set_native
)
3357 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3358 struct bt_ctf_field_type_integer
, parent
);
3361 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3363 * User byte order is native, so we can set
3364 * the real byte order.
3366 integer_type
->declaration
.byte_order
=
3370 integer_type
->user_byte_order
=
3371 get_ctf_ir_byte_order(byte_order
);
3372 integer_type
->declaration
.byte_order
= byte_order
;
3377 void bt_ctf_field_type_enumeration_set_byte_order(
3378 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3380 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3381 struct bt_ctf_field_type_enumeration
, parent
);
3383 /* Safe to assume that container is an integer */
3384 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3385 byte_order
, set_native
);
3389 void bt_ctf_field_type_floating_point_set_byte_order(
3390 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3392 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3393 container_of(type
, struct bt_ctf_field_type_floating_point
,
3397 if (floating_point_type
->user_byte_order
==
3398 BT_CTF_BYTE_ORDER_NATIVE
) {
3400 * User byte order is native, so we can set
3401 * the real byte order.
3403 floating_point_type
->declaration
.byte_order
=
3405 floating_point_type
->sign
.byte_order
=
3407 floating_point_type
->mantissa
.byte_order
=
3409 floating_point_type
->exp
.byte_order
=
3413 floating_point_type
->user_byte_order
=
3414 get_ctf_ir_byte_order(byte_order
);
3415 floating_point_type
->declaration
.byte_order
= byte_order
;
3416 floating_point_type
->sign
.byte_order
= byte_order
;
3417 floating_point_type
->mantissa
.byte_order
= byte_order
;
3418 floating_point_type
->exp
.byte_order
= byte_order
;
3423 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3424 int byte_order
, int set_native
)
3427 struct bt_ctf_field_type_structure
*structure_type
=
3428 container_of(type
, struct bt_ctf_field_type_structure
,
3431 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3432 struct structure_field
*field
= g_ptr_array_index(
3433 structure_type
->fields
, i
);
3434 struct bt_ctf_field_type
*field_type
= field
->type
;
3436 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3437 set_byte_order_funcs
[field_type
->declaration
->id
](
3438 field_type
, byte_order
, set_native
);
3444 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3445 int byte_order
, int set_native
)
3448 struct bt_ctf_field_type_variant
*variant_type
=
3449 container_of(type
, struct bt_ctf_field_type_variant
,
3452 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3453 struct structure_field
*field
= g_ptr_array_index(
3454 variant_type
->fields
, i
);
3455 struct bt_ctf_field_type
*field_type
= field
->type
;
3457 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3458 set_byte_order_funcs
[field_type
->declaration
->id
](
3459 field_type
, byte_order
, set_native
);
3465 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3466 int byte_order
, int set_native
)
3468 struct bt_ctf_field_type_array
*array_type
=
3469 container_of(type
, struct bt_ctf_field_type_array
,
3472 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3473 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3474 array_type
->element_type
, byte_order
, set_native
);
3479 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3480 int byte_order
, int set_native
)
3482 struct bt_ctf_field_type_sequence
*sequence_type
=
3483 container_of(type
, struct bt_ctf_field_type_sequence
,
3486 if (set_byte_order_funcs
[
3487 sequence_type
->element_type
->declaration
->id
]) {
3488 set_byte_order_funcs
[
3489 sequence_type
->element_type
->declaration
->id
](
3490 sequence_type
->element_type
, byte_order
, set_native
);
3495 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3496 struct bt_ctf_field_type
*type
)
3498 struct bt_ctf_field_type
*copy
;
3499 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3501 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3502 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3507 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3509 copy_integer
->declaration
= integer
->declaration
;
3510 if (integer
->mapped_clock
) {
3511 bt_get(integer
->mapped_clock
);
3512 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3515 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3522 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3523 struct bt_ctf_field_type
*type
)
3526 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3527 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3529 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3532 /* Copy the source enumeration's container */
3533 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3534 if (!copy_container
) {
3538 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3542 copy_enumeration
= container_of(copy
,
3543 struct bt_ctf_field_type_enumeration
, parent
);
3545 /* Copy all enumaration entries */
3546 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3547 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3548 enumeration
->entries
, i
);
3549 struct enumeration_mapping
*copy_mapping
= g_new0(
3550 struct enumeration_mapping
, 1);
3552 if (!copy_mapping
) {
3556 *copy_mapping
= *mapping
;
3557 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3560 copy_enumeration
->declaration
= enumeration
->declaration
;
3562 bt_put(copy_container
);
3565 bt_put(copy_container
);
3571 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3572 struct bt_ctf_field_type
*type
)
3574 struct bt_ctf_field_type
*copy
;
3575 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3577 floating_point
= container_of(type
,
3578 struct bt_ctf_field_type_floating_point
, parent
);
3579 copy
= bt_ctf_field_type_floating_point_create();
3584 copy_float
= container_of(copy
,
3585 struct bt_ctf_field_type_floating_point
, parent
);
3586 copy_float
->declaration
= floating_point
->declaration
;
3587 copy_float
->sign
= floating_point
->sign
;
3588 copy_float
->mantissa
= floating_point
->mantissa
;
3589 copy_float
->exp
= floating_point
->exp
;
3590 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3591 copy_float
->declaration
.sign
= ©_float
->sign
;
3592 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3593 copy_float
->declaration
.exp
= ©_float
->exp
;
3599 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3600 struct bt_ctf_field_type
*type
)
3603 GHashTableIter iter
;
3604 gpointer key
, value
;
3605 struct bt_ctf_field_type
*copy
;
3606 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3608 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3610 copy
= bt_ctf_field_type_structure_create();
3615 copy_structure
= container_of(copy
,
3616 struct bt_ctf_field_type_structure
, parent
);
3618 /* Copy field_name_to_index */
3619 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3620 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3621 g_hash_table_insert(copy_structure
->field_name_to_index
,
3625 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3626 struct structure_field
*entry
, *copy_entry
;
3627 struct bt_ctf_field_type
*copy_field
;
3629 copy_entry
= g_new0(struct structure_field
, 1);
3634 entry
= g_ptr_array_index(structure
->fields
, i
);
3635 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3641 copy_entry
->name
= entry
->name
;
3642 copy_entry
->type
= copy_field
;
3643 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3646 copy_structure
->declaration
= structure
->declaration
;
3655 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3656 struct bt_ctf_field_type
*type
)
3659 GHashTableIter iter
;
3660 gpointer key
, value
;
3661 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3662 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3664 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3667 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3673 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3674 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3679 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3682 /* Copy field_name_to_index */
3683 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3684 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3685 g_hash_table_insert(copy_variant
->field_name_to_index
,
3689 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3690 struct structure_field
*entry
, *copy_entry
;
3691 struct bt_ctf_field_type
*copy_field
;
3693 copy_entry
= g_new0(struct structure_field
, 1);
3698 entry
= g_ptr_array_index(variant
->fields
, i
);
3699 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3705 copy_entry
->name
= entry
->name
;
3706 copy_entry
->type
= copy_field
;
3707 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3710 copy_variant
->declaration
= variant
->declaration
;
3711 if (variant
->tag_field_path
) {
3712 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3713 variant
->tag_field_path
);
3714 if (!copy_variant
->tag_field_path
) {
3728 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3729 struct bt_ctf_field_type
*type
)
3731 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3732 struct bt_ctf_field_type_array
*array
, *copy_array
;
3734 array
= container_of(type
, struct bt_ctf_field_type_array
,
3736 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3737 if (!copy_element
) {
3741 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3746 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3748 copy_array
->declaration
= array
->declaration
;
3750 bt_put(copy_element
);
3755 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3756 struct bt_ctf_field_type
*type
)
3758 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3759 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3761 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3763 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3764 if (!copy_element
) {
3768 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3769 sequence
->length_field_name
->len
?
3770 sequence
->length_field_name
->str
: NULL
);
3775 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3777 copy_sequence
->declaration
= sequence
->declaration
;
3778 if (sequence
->length_field_path
) {
3779 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3780 sequence
->length_field_path
);
3781 if (!copy_sequence
->length_field_path
) {
3786 bt_put(copy_element
);
3794 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3795 struct bt_ctf_field_type
*type
)
3797 struct bt_ctf_field_type
*copy
;
3798 struct bt_ctf_field_type_string
*string
, *copy_string
;
3800 copy
= bt_ctf_field_type_string_create();
3805 string
= container_of(type
, struct bt_ctf_field_type_string
,
3807 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3809 copy_string
->declaration
= string
->declaration
;
3815 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3816 struct bt_ctf_field_type
*type_b
)
3819 struct bt_ctf_field_type_integer
*integer_a
;
3820 struct bt_ctf_field_type_integer
*integer_b
;
3821 struct declaration_integer
*decl_a
;
3822 struct declaration_integer
*decl_b
;
3824 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3826 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3828 decl_a
= &integer_a
->declaration
;
3829 decl_b
= &integer_b
->declaration
;
3832 if (decl_a
->len
!= decl_b
->len
) {
3837 * Compare user byte orders only, not the cached,
3840 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3845 if (decl_a
->signedness
!= decl_b
->signedness
) {
3850 if (decl_a
->base
!= decl_b
->base
) {
3855 if (decl_a
->encoding
!= decl_b
->encoding
) {
3860 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3872 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3873 struct bt_ctf_field_type
*type_b
)
3876 struct bt_ctf_field_type_floating_point
*float_a
;
3877 struct bt_ctf_field_type_floating_point
*float_b
;
3879 float_a
= container_of(type_a
,
3880 struct bt_ctf_field_type_floating_point
, parent
);
3881 float_b
= container_of(type_b
,
3882 struct bt_ctf_field_type_floating_point
, parent
);
3885 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3889 /* Exponent length */
3890 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3894 /* Mantissa length */
3895 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3900 * Compare user byte orders only, not the cached,
3903 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3915 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3916 struct enumeration_mapping
*mapping_b
)
3921 if (mapping_a
->string
!= mapping_b
->string
) {
3926 if (mapping_a
->range_start
._unsigned
!=
3927 mapping_b
->range_start
._unsigned
) {
3932 if (mapping_a
->range_end
._unsigned
!=
3933 mapping_b
->range_end
._unsigned
) {
3945 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3946 struct bt_ctf_field_type
*type_b
)
3950 struct bt_ctf_field_type_enumeration
*enum_a
;
3951 struct bt_ctf_field_type_enumeration
*enum_b
;
3953 enum_a
= container_of(type_a
,
3954 struct bt_ctf_field_type_enumeration
, parent
);
3955 enum_b
= container_of(type_b
,
3956 struct bt_ctf_field_type_enumeration
, parent
);
3958 /* Container field type */
3959 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3967 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3971 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3972 struct enumeration_mapping
*mapping_a
=
3973 g_ptr_array_index(enum_a
->entries
, i
);
3974 struct enumeration_mapping
*mapping_b
=
3975 g_ptr_array_index(enum_b
->entries
, i
);
3977 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3990 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
3991 struct bt_ctf_field_type
*type_b
)
3994 struct bt_ctf_field_type_string
*string_a
;
3995 struct bt_ctf_field_type_string
*string_b
;
3997 string_a
= container_of(type_a
,
3998 struct bt_ctf_field_type_string
, parent
);
3999 string_b
= container_of(type_b
,
4000 struct bt_ctf_field_type_string
, parent
);
4003 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
4015 int compare_structure_fields(struct structure_field
*field_a
,
4016 struct structure_field
*field_b
)
4021 if (field_a
->name
!= field_b
->name
) {
4026 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
4033 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
4034 struct bt_ctf_field_type
*type_b
)
4038 struct bt_ctf_field_type_structure
*struct_a
;
4039 struct bt_ctf_field_type_structure
*struct_b
;
4041 struct_a
= container_of(type_a
,
4042 struct bt_ctf_field_type_structure
, parent
);
4043 struct_b
= container_of(type_b
,
4044 struct bt_ctf_field_type_structure
, parent
);
4047 if (bt_ctf_field_type_get_alignment(type_a
) !=
4048 bt_ctf_field_type_get_alignment(type_b
)) {
4053 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4057 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4058 struct structure_field
*field_a
=
4059 g_ptr_array_index(struct_a
->fields
, i
);
4060 struct structure_field
*field_b
=
4061 g_ptr_array_index(struct_b
->fields
, i
);
4063 ret
= compare_structure_fields(field_a
, field_b
);
4077 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4078 struct bt_ctf_field_type
*type_b
)
4082 struct bt_ctf_field_type_variant
*variant_a
;
4083 struct bt_ctf_field_type_variant
*variant_b
;
4085 variant_a
= container_of(type_a
,
4086 struct bt_ctf_field_type_variant
, parent
);
4087 variant_b
= container_of(type_b
,
4088 struct bt_ctf_field_type_variant
, parent
);
4091 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4096 ret
= bt_ctf_field_type_compare(
4097 (struct bt_ctf_field_type
*) variant_a
->tag
,
4098 (struct bt_ctf_field_type
*) variant_b
->tag
);
4106 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
4110 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
4111 struct structure_field
*field_a
=
4112 g_ptr_array_index(variant_a
->fields
, i
);
4113 struct structure_field
*field_b
=
4114 g_ptr_array_index(variant_b
->fields
, i
);
4116 ret
= compare_structure_fields(field_a
, field_b
);
4130 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4131 struct bt_ctf_field_type
*type_b
)
4134 struct bt_ctf_field_type_array
*array_a
;
4135 struct bt_ctf_field_type_array
*array_b
;
4137 array_a
= container_of(type_a
,
4138 struct bt_ctf_field_type_array
, parent
);
4139 array_b
= container_of(type_b
,
4140 struct bt_ctf_field_type_array
, parent
);
4143 if (array_a
->length
!= array_b
->length
) {
4148 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4149 array_b
->element_type
);
4156 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4157 struct bt_ctf_field_type
*type_b
)
4160 struct bt_ctf_field_type_sequence
*sequence_a
;
4161 struct bt_ctf_field_type_sequence
*sequence_b
;
4163 sequence_a
= container_of(type_a
,
4164 struct bt_ctf_field_type_sequence
, parent
);
4165 sequence_b
= container_of(type_b
,
4166 struct bt_ctf_field_type_sequence
, parent
);
4169 if (strcmp(sequence_a
->length_field_name
->str
,
4170 sequence_b
->length_field_name
->str
)) {
4175 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4176 sequence_b
->element_type
);
4182 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4183 struct bt_ctf_field_type
*type_b
)
4187 if (type_a
== type_b
) {
4188 /* Same reference: equal (even if both are NULL) */
4193 if (!type_a
|| !type_b
) {
4198 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4199 /* Different type IDs */
4203 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4204 /* Both have unknown type IDs */
4208 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4215 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4217 int field_count
= -1;
4218 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4221 case CTF_TYPE_STRUCT
:
4223 bt_ctf_field_type_structure_get_field_count(field_type
);
4225 case CTF_TYPE_VARIANT
:
4227 bt_ctf_field_type_variant_get_field_count(field_type
);
4229 case CTF_TYPE_ARRAY
:
4230 case CTF_TYPE_SEQUENCE
:
4232 * Array and sequence types always contain a single member
4233 * (the element type).
4245 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4246 struct bt_ctf_field_type
*field_type
, int index
)
4248 struct bt_ctf_field_type
*field
= NULL
;
4249 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4252 case CTF_TYPE_STRUCT
:
4253 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4256 case CTF_TYPE_VARIANT
:
4258 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4266 case CTF_TYPE_ARRAY
:
4267 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4269 case CTF_TYPE_SEQUENCE
:
4270 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4280 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4283 int field_index
= -1;
4284 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4287 case CTF_TYPE_STRUCT
:
4288 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4291 case CTF_TYPE_VARIANT
:
4292 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4302 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4303 struct bt_ctf_field_type
*type
)
4305 struct bt_ctf_field_path
*field_path
= NULL
;
4306 struct bt_ctf_field_type_variant
*variant
;
4308 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4312 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4314 field_path
= bt_get(variant
->tag_field_path
);
4319 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4320 struct bt_ctf_field_type
*type
)
4322 struct bt_ctf_field_path
*field_path
= NULL
;
4323 struct bt_ctf_field_type_sequence
*sequence
;
4325 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4329 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4331 field_path
= bt_get(sequence
->length_field_path
);