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_variant_validate(struct bt_ctf_field_type
*type
)
640 struct bt_ctf_field_type
*child_type
= NULL
;
641 struct bt_ctf_field_type_variant
*variant
=
642 container_of(type
, struct bt_ctf_field_type_variant
,
645 int tag_mappings_count
;
647 if (variant
->tag_name
->len
== 0 || !variant
->tag
) {
652 if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
659 bt_ctf_field_type_enumeration_get_mapping_count(
660 (struct bt_ctf_field_type
*) variant
->tag
);
662 if (tag_mappings_count
!= variant
->fields
->len
) {
667 for (i
= 0; i
< tag_mappings_count
; ++i
) {
669 struct bt_ctf_field_type
*ft
;
671 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
672 (struct bt_ctf_field_type
*) variant
->tag
,
682 ft
= bt_ctf_field_type_variant_get_field_type_by_name(
692 field_count
= bt_ctf_field_type_variant_get_field_count(type
);
693 if (field_count
< 0) {
698 for (i
= 0; i
< field_count
; ++i
) {
699 ret
= bt_ctf_field_type_variant_get_field(type
,
700 NULL
, &child_type
, i
);
705 ret
= bt_ctf_field_type_validate(child_type
);
720 * This function validates a given field type without considering
721 * where this field type is located. It only validates the properties
722 * of the given field type and the properties of its children if
726 int bt_ctf_field_type_validate(struct bt_ctf_field_type
*type
)
729 enum bt_ctf_type_id id
= bt_ctf_field_type_get_type_id(type
);
737 /* Already marked as valid */
741 if (type_validate_funcs
[id
]) {
742 ret
= type_validate_funcs
[id
](type
);
745 if (!ret
&& type
->frozen
) {
746 /* Field type is valid */
754 struct bt_ctf_field_type
*bt_ctf_field_type_integer_create(unsigned int size
)
756 struct bt_ctf_field_type_integer
*integer
=
757 g_new0(struct bt_ctf_field_type_integer
, 1);
759 if (!integer
|| size
== 0 || size
> 64) {
763 integer
->parent
.declaration
= &integer
->declaration
.p
;
764 integer
->parent
.declaration
->id
= BT_CTF_TYPE_ID_INTEGER
;
765 integer
->declaration
.len
= size
;
766 integer
->declaration
.base
= BT_CTF_INTEGER_BASE_DECIMAL
;
767 integer
->declaration
.encoding
= BT_CTF_STRING_ENCODING_NONE
;
768 bt_ctf_field_type_init(&integer
->parent
, TRUE
);
769 return &integer
->parent
;
772 int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type
*type
)
775 struct bt_ctf_field_type_integer
*integer
;
777 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
782 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
783 ret
= (int) integer
->declaration
.len
;
788 int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type
*type
)
791 struct bt_ctf_field_type_integer
*integer
;
793 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
798 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
799 ret
= integer
->declaration
.signedness
;
804 int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type
*type
,
808 struct bt_ctf_field_type_integer
*integer
;
810 if (!type
|| type
->frozen
||
811 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
816 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
817 integer
->declaration
.signedness
= !!is_signed
;
822 enum bt_ctf_integer_base
bt_ctf_field_type_integer_get_base(
823 struct bt_ctf_field_type
*type
)
825 enum bt_ctf_integer_base ret
= BT_CTF_INTEGER_BASE_UNKNOWN
;
826 struct bt_ctf_field_type_integer
*integer
;
828 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
832 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
833 ret
= integer
->declaration
.base
;
838 int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type
*type
,
839 enum bt_ctf_integer_base base
)
843 if (!type
|| type
->frozen
||
844 type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
850 case BT_CTF_INTEGER_BASE_BINARY
:
851 case BT_CTF_INTEGER_BASE_OCTAL
:
852 case BT_CTF_INTEGER_BASE_DECIMAL
:
853 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
855 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
856 struct bt_ctf_field_type_integer
, parent
);
857 integer
->declaration
.base
= base
;
867 enum bt_ctf_string_encoding
bt_ctf_field_type_integer_get_encoding(
868 struct bt_ctf_field_type
*type
)
870 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
871 struct bt_ctf_field_type_integer
*integer
;
873 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
877 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
878 ret
= integer
->declaration
.encoding
;
883 int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type
*type
,
884 enum bt_ctf_string_encoding encoding
)
887 struct bt_ctf_field_type_integer
*integer
;
889 if (!type
|| type
->frozen
||
890 (type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) ||
891 (encoding
< BT_CTF_STRING_ENCODING_NONE
) ||
892 (encoding
>= BT_CTF_STRING_ENCODING_UNKNOWN
)) {
897 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
898 integer
->declaration
.encoding
= encoding
;
903 struct bt_ctf_clock_class
*bt_ctf_field_type_integer_get_mapped_clock_class(
904 struct bt_ctf_field_type
*type
)
906 struct bt_ctf_field_type_integer
*integer
;
907 struct bt_ctf_clock_class
*clock_class
= NULL
;
913 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
914 clock_class
= integer
->mapped_clock
;
920 int bt_ctf_field_type_integer_set_mapped_clock_class(
921 struct bt_ctf_field_type
*type
,
922 struct bt_ctf_clock_class
*clock_class
)
924 struct bt_ctf_field_type_integer
*integer
;
927 if (!type
|| type
->frozen
|| !bt_ctf_clock_class_is_valid(clock_class
)) {
932 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
933 bt_put(integer
->mapped_clock
);
934 integer
->mapped_clock
= bt_get(clock_class
);
940 void bt_ctf_field_type_enum_iter_destroy(struct bt_object
*obj
)
942 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
=
944 struct bt_ctf_field_type_enumeration_mapping_iterator
,
947 bt_put(&iter
->enumeration_type
->parent
);
952 struct bt_ctf_field_type_enumeration_mapping_iterator
*
953 bt_ctf_field_type_enumeration_find_mappings_type(
954 struct bt_ctf_field_type
*type
,
955 enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type
)
957 struct bt_ctf_field_type_enumeration
*enumeration_type
;
958 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
960 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
964 enumeration_type
= container_of(type
,
965 struct bt_ctf_field_type_enumeration
, parent
);
966 iter
= g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator
, 1);
971 bt_object_init(&iter
->base
, bt_ctf_field_type_enum_iter_destroy
);
973 iter
->enumeration_type
= enumeration_type
;
975 iter
->type
= iterator_type
;
980 struct bt_ctf_field_type_enumeration_mapping_iterator
*
981 bt_ctf_field_type_enumeration_find_mappings_by_name(
982 struct bt_ctf_field_type
*type
, const char *name
)
984 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
986 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
987 type
, ITERATOR_BY_NAME
);
992 iter
->u
.name_quark
= g_quark_try_string(name
);
993 if (!iter
->u
.name_quark
) {
997 /* Advance iterator to first entry, or leave index at -1. */
998 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
999 /* No entry found. */
1009 int bt_ctf_field_type_enumeration_mapping_iterator_next(
1010 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
)
1012 struct bt_ctf_field_type_enumeration
*enumeration
;
1013 struct bt_ctf_field_type
*type
;
1014 int i
, ret
= 0, len
;
1016 enumeration
= iter
->enumeration_type
;
1017 type
= &enumeration
->parent
;
1018 len
= enumeration
->entries
->len
;
1019 for (i
= iter
->index
+ 1; i
< len
; i
++) {
1020 struct enumeration_mapping
*mapping
=
1021 get_enumeration_mapping(type
, i
);
1023 switch (iter
->type
) {
1024 case ITERATOR_BY_NAME
:
1025 if (mapping
->string
== iter
->u
.name_quark
) {
1030 case ITERATOR_BY_SIGNED_VALUE
:
1032 int64_t value
= iter
->u
.signed_value
;
1034 if (value
>= mapping
->range_start
._signed
&&
1035 value
<= mapping
->range_end
._signed
) {
1041 case ITERATOR_BY_UNSIGNED_VALUE
:
1043 uint64_t value
= iter
->u
.unsigned_value
;
1045 if (value
>= mapping
->range_start
._unsigned
&&
1046 value
<= mapping
->range_end
._unsigned
) {
1062 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1063 bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
1064 struct bt_ctf_field_type
*type
, int64_t value
)
1066 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1068 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1069 type
, ITERATOR_BY_SIGNED_VALUE
);
1074 if (bt_ctf_field_type_integer_get_signed(
1075 iter
->enumeration_type
->container
) != 1) {
1078 iter
->u
.signed_value
= value
;
1080 /* Advance iterator to first entry, or leave index at -1. */
1081 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1082 /* No entry found. */
1092 struct bt_ctf_field_type_enumeration_mapping_iterator
*
1093 bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
1094 struct bt_ctf_field_type
*type
, uint64_t value
)
1096 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
;
1098 iter
= bt_ctf_field_type_enumeration_find_mappings_type(
1099 type
, ITERATOR_BY_UNSIGNED_VALUE
);
1104 if (bt_ctf_field_type_integer_get_signed(
1105 iter
->enumeration_type
->container
) != 0) {
1108 iter
->u
.unsigned_value
= value
;
1110 /* Advance iterator to first entry, or leave index at -1. */
1111 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter
)) {
1112 /* No entry found. */
1122 int bt_ctf_field_type_enumeration_get_mapping_name(
1123 struct bt_ctf_field_type
*enum_field_type
,
1125 const char **mapping_name
)
1128 struct enumeration_mapping
*mapping
;
1130 if (!enum_field_type
|| index
< 0) {
1135 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1142 *mapping_name
= g_quark_to_string(mapping
->string
);
1148 int bt_ctf_field_type_enumeration_mapping_iterator_get_name(
1149 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1150 const char **mapping_name
)
1159 ret
= bt_ctf_field_type_enumeration_get_mapping_name(
1160 &iter
->enumeration_type
->parent
, iter
->index
,
1166 int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1167 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1168 const char **mapping_name
, int64_t *range_begin
,
1178 ret
= bt_ctf_field_type_enumeration_get_mapping_signed(
1179 &iter
->enumeration_type
->parent
, iter
->index
,
1180 mapping_name
, range_begin
, range_end
);
1185 int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
1186 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
,
1187 const char **mapping_name
, uint64_t *range_begin
,
1188 uint64_t *range_end
)
1197 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(
1198 &iter
->enumeration_type
->parent
, iter
->index
,
1199 mapping_name
, range_begin
, range_end
);
1204 int bt_ctf_field_type_enumeration_get_mapping_signed(
1205 struct bt_ctf_field_type
*enum_field_type
,
1207 const char **mapping_name
, int64_t *range_begin
,
1211 struct enumeration_mapping
*mapping
;
1213 if (!enum_field_type
|| index
< 0) {
1218 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1225 *mapping_name
= g_quark_to_string(mapping
->string
);
1229 *range_begin
= mapping
->range_start
._signed
;
1233 *range_end
= mapping
->range_end
._signed
;
1239 int bt_ctf_field_type_enumeration_get_mapping_unsigned(
1240 struct bt_ctf_field_type
*enum_field_type
,
1242 const char **mapping_name
, uint64_t *range_begin
,
1243 uint64_t *range_end
)
1246 struct enumeration_mapping
*mapping
;
1248 if (!enum_field_type
|| index
< 0) {
1253 mapping
= get_enumeration_mapping(enum_field_type
, index
);
1260 *mapping_name
= g_quark_to_string(mapping
->string
);
1264 *range_begin
= mapping
->range_start
._unsigned
;
1268 *range_end
= mapping
->range_end
._unsigned
;
1274 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_create(
1275 struct bt_ctf_field_type
*integer_container_type
)
1277 struct bt_ctf_field_type_enumeration
*enumeration
= NULL
;
1279 if (!integer_container_type
) {
1283 if (integer_container_type
->declaration
->id
!= BT_CTF_TYPE_ID_INTEGER
) {
1287 enumeration
= g_new0(struct bt_ctf_field_type_enumeration
, 1);
1292 enumeration
->parent
.declaration
= &enumeration
->declaration
.p
;
1293 enumeration
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ENUM
;
1294 bt_get(integer_container_type
);
1295 enumeration
->container
= integer_container_type
;
1296 enumeration
->entries
= g_ptr_array_new_with_free_func(
1297 (GDestroyNotify
)destroy_enumeration_mapping
);
1298 bt_ctf_field_type_init(&enumeration
->parent
, FALSE
);
1299 return &enumeration
->parent
;
1301 g_free(enumeration
);
1305 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_get_container_type(
1306 struct bt_ctf_field_type
*type
)
1308 struct bt_ctf_field_type
*container_type
= NULL
;
1309 struct bt_ctf_field_type_enumeration
*enumeration_type
;
1315 if (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) {
1319 enumeration_type
= container_of(type
,
1320 struct bt_ctf_field_type_enumeration
, parent
);
1321 container_type
= enumeration_type
->container
;
1322 bt_get(container_type
);
1324 return container_type
;
1327 int bt_ctf_field_type_enumeration_add_mapping(
1328 struct bt_ctf_field_type
*type
, const char *string
,
1329 int64_t range_start
, int64_t range_end
)
1332 GQuark mapping_name
;
1333 struct enumeration_mapping
*mapping
;
1334 struct bt_ctf_field_type_enumeration
*enumeration
;
1335 char *escaped_string
;
1337 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1339 (range_end
< range_start
)) {
1344 if (!string
|| strlen(string
) == 0) {
1349 escaped_string
= g_strescape(string
, NULL
);
1350 if (!escaped_string
) {
1355 mapping
= g_new(struct enumeration_mapping
, 1);
1360 mapping_name
= g_quark_from_string(escaped_string
);
1361 *mapping
= (struct enumeration_mapping
) {
1362 .range_start
._signed
= range_start
,
1363 .range_end
._signed
= range_end
,
1364 .string
= mapping_name
,
1366 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1368 g_ptr_array_add(enumeration
->entries
, mapping
);
1369 g_ptr_array_sort(enumeration
->entries
,
1370 (GCompareFunc
)compare_enumeration_mappings_signed
);
1372 free(escaped_string
);
1377 int bt_ctf_field_type_enumeration_add_mapping_unsigned(
1378 struct bt_ctf_field_type
*type
, const char *string
,
1379 uint64_t range_start
, uint64_t range_end
)
1382 GQuark mapping_name
;
1383 struct enumeration_mapping
*mapping
;
1384 struct bt_ctf_field_type_enumeration
*enumeration
;
1385 char *escaped_string
;
1387 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
) ||
1389 (range_end
< range_start
)) {
1394 if (!string
|| strlen(string
) == 0) {
1399 escaped_string
= g_strescape(string
, NULL
);
1400 if (!escaped_string
) {
1405 mapping
= g_new(struct enumeration_mapping
, 1);
1410 mapping_name
= g_quark_from_string(escaped_string
);
1411 *mapping
= (struct enumeration_mapping
) {
1412 .range_start
._unsigned
= range_start
,
1413 .range_end
._unsigned
= range_end
,
1414 .string
= mapping_name
,
1416 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1418 g_ptr_array_add(enumeration
->entries
, mapping
);
1419 g_ptr_array_sort(enumeration
->entries
,
1420 (GCompareFunc
)compare_enumeration_mappings_unsigned
);
1422 free(escaped_string
);
1427 int bt_ctf_field_type_enumeration_get_mapping_count(
1428 struct bt_ctf_field_type
*type
)
1431 struct bt_ctf_field_type_enumeration
*enumeration
;
1433 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ENUM
)) {
1438 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
1440 ret
= (int) enumeration
->entries
->len
;
1445 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_create(void)
1447 struct bt_ctf_field_type_floating_point
*floating_point
=
1448 g_new0(struct bt_ctf_field_type_floating_point
, 1);
1450 if (!floating_point
) {
1454 floating_point
->declaration
.sign
= &floating_point
->sign
;
1455 floating_point
->declaration
.mantissa
= &floating_point
->mantissa
;
1456 floating_point
->declaration
.exp
= &floating_point
->exp
;
1457 floating_point
->sign
.len
= 1;
1458 floating_point
->parent
.declaration
= &floating_point
->declaration
.p
;
1459 floating_point
->parent
.declaration
->id
= BT_CTF_TYPE_ID_FLOAT
;
1460 floating_point
->declaration
.exp
->len
=
1461 sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
;
1462 floating_point
->declaration
.mantissa
->len
= FLT_MANT_DIG
- 1;
1463 floating_point
->sign
.p
.alignment
= 1;
1464 floating_point
->mantissa
.p
.alignment
= 1;
1465 floating_point
->exp
.p
.alignment
= 1;
1467 bt_ctf_field_type_init(&floating_point
->parent
, TRUE
);
1469 return floating_point
? &floating_point
->parent
: NULL
;
1472 int bt_ctf_field_type_floating_point_get_exponent_digits(
1473 struct bt_ctf_field_type
*type
)
1476 struct bt_ctf_field_type_floating_point
*floating_point
;
1478 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1483 floating_point
= container_of(type
,
1484 struct bt_ctf_field_type_floating_point
, parent
);
1485 ret
= (int) floating_point
->declaration
.exp
->len
;
1490 int bt_ctf_field_type_floating_point_set_exponent_digits(
1491 struct bt_ctf_field_type
*type
,
1492 unsigned int exponent_digits
)
1495 struct bt_ctf_field_type_floating_point
*floating_point
;
1497 if (!type
|| type
->frozen
||
1498 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1503 floating_point
= container_of(type
,
1504 struct bt_ctf_field_type_floating_point
, parent
);
1505 if ((exponent_digits
!= sizeof(float) * CHAR_BIT
- FLT_MANT_DIG
) &&
1506 (exponent_digits
!= sizeof(double) * CHAR_BIT
- DBL_MANT_DIG
) &&
1508 sizeof(long double) * CHAR_BIT
- LDBL_MANT_DIG
)) {
1513 floating_point
->declaration
.exp
->len
= exponent_digits
;
1518 int bt_ctf_field_type_floating_point_get_mantissa_digits(
1519 struct bt_ctf_field_type
*type
)
1522 struct bt_ctf_field_type_floating_point
*floating_point
;
1524 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1529 floating_point
= container_of(type
,
1530 struct bt_ctf_field_type_floating_point
, parent
);
1531 ret
= (int) floating_point
->mantissa
.len
+ 1;
1536 int bt_ctf_field_type_floating_point_set_mantissa_digits(
1537 struct bt_ctf_field_type
*type
,
1538 unsigned int mantissa_digits
)
1541 struct bt_ctf_field_type_floating_point
*floating_point
;
1543 if (!type
|| type
->frozen
||
1544 (type
->declaration
->id
!= BT_CTF_TYPE_ID_FLOAT
)) {
1549 floating_point
= container_of(type
,
1550 struct bt_ctf_field_type_floating_point
, parent
);
1552 if ((mantissa_digits
!= FLT_MANT_DIG
) &&
1553 (mantissa_digits
!= DBL_MANT_DIG
) &&
1554 (mantissa_digits
!= LDBL_MANT_DIG
)) {
1559 floating_point
->declaration
.mantissa
->len
= mantissa_digits
- 1;
1564 struct bt_ctf_field_type
*bt_ctf_field_type_structure_create(void)
1566 struct bt_ctf_field_type_structure
*structure
=
1567 g_new0(struct bt_ctf_field_type_structure
, 1);
1573 structure
->parent
.declaration
= &structure
->declaration
.p
;
1574 structure
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRUCT
;
1575 structure
->fields
= g_ptr_array_new_with_free_func(
1576 (GDestroyNotify
)destroy_structure_field
);
1577 structure
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1578 bt_ctf_field_type_init(&structure
->parent
, TRUE
);
1579 return &structure
->parent
;
1584 int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type
*type
,
1585 struct bt_ctf_field_type
*field_type
,
1586 const char *field_name
)
1589 struct bt_ctf_field_type_structure
*structure
;
1592 * TODO: check that `field_type` does not contain `type`,
1595 if (!type
|| !field_type
|| type
->frozen
||
1596 bt_ctf_validate_identifier(field_name
) ||
1597 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
) ||
1598 type
== field_type
) {
1603 structure
= container_of(type
,
1604 struct bt_ctf_field_type_structure
, parent
);
1605 if (add_structure_field(structure
->fields
,
1606 structure
->field_name_to_index
, field_type
, field_name
)) {
1614 int bt_ctf_field_type_structure_get_field_count(
1615 struct bt_ctf_field_type
*type
)
1618 struct bt_ctf_field_type_structure
*structure
;
1620 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1625 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1627 ret
= (int) structure
->fields
->len
;
1632 int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type
*type
,
1633 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1636 struct bt_ctf_field_type_structure
*structure
;
1637 struct structure_field
*field
;
1640 if (!type
|| index
< 0 ||
1641 (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRUCT
)) {
1646 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1648 if (index
>= structure
->fields
->len
) {
1653 field
= g_ptr_array_index(structure
->fields
, index
);
1655 *field_type
= field
->type
;
1656 bt_get(field
->type
);
1659 *field_name
= g_quark_to_string(field
->name
);
1665 struct bt_ctf_field_type
*bt_ctf_field_type_structure_get_field_type_by_name(
1666 struct bt_ctf_field_type
*type
,
1671 struct structure_field
*field
;
1672 struct bt_ctf_field_type_structure
*structure
;
1673 struct bt_ctf_field_type
*field_type
= NULL
;
1675 if (!type
|| !name
) {
1679 name_quark
= g_quark_try_string(name
);
1684 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
1686 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
1687 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1691 field
= structure
->fields
->pdata
[index
];
1692 field_type
= field
->type
;
1698 struct bt_ctf_field_type
*bt_ctf_field_type_variant_create(
1699 struct bt_ctf_field_type
*enum_tag
, const char *tag_name
)
1701 struct bt_ctf_field_type_variant
*variant
= NULL
;
1703 if (tag_name
&& bt_ctf_validate_identifier(tag_name
)) {
1707 variant
= g_new0(struct bt_ctf_field_type_variant
, 1);
1712 variant
->parent
.declaration
= &variant
->declaration
.p
;
1713 variant
->parent
.declaration
->id
= BT_CTF_TYPE_ID_VARIANT
;
1714 variant
->tag_name
= g_string_new(tag_name
);
1715 variant
->field_name_to_index
= g_hash_table_new(NULL
, NULL
);
1716 variant
->fields
= g_ptr_array_new_with_free_func(
1717 (GDestroyNotify
) destroy_structure_field
);
1720 variant
->tag
= container_of(enum_tag
,
1721 struct bt_ctf_field_type_enumeration
, parent
);
1724 bt_ctf_field_type_init(&variant
->parent
, TRUE
);
1725 /* A variant's alignment is undefined */
1726 variant
->parent
.declaration
->alignment
= 0;
1727 return &variant
->parent
;
1732 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_tag_type(
1733 struct bt_ctf_field_type
*type
)
1735 struct bt_ctf_field_type_variant
*variant
;
1736 struct bt_ctf_field_type
*tag_type
= NULL
;
1738 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1742 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1743 if (!variant
->tag
) {
1747 tag_type
= &variant
->tag
->parent
;
1753 const char *bt_ctf_field_type_variant_get_tag_name(
1754 struct bt_ctf_field_type
*type
)
1756 struct bt_ctf_field_type_variant
*variant
;
1757 const char *tag_name
= NULL
;
1759 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1763 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1764 if (variant
->tag_name
->len
== 0) {
1768 tag_name
= variant
->tag_name
->str
;
1773 int bt_ctf_field_type_variant_set_tag_name(
1774 struct bt_ctf_field_type
*type
, const char *name
)
1777 struct bt_ctf_field_type_variant
*variant
;
1779 if (!type
|| type
->frozen
||
1780 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1781 bt_ctf_validate_identifier(name
)) {
1786 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1787 g_string_assign(variant
->tag_name
, name
);
1792 int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type
*type
,
1793 struct bt_ctf_field_type
*field_type
,
1794 const char *field_name
)
1798 struct bt_ctf_field_type_variant
*variant
;
1799 GQuark field_name_quark
= g_quark_from_string(field_name
);
1802 * TODO: check that `field_type` does not contain `type`,
1805 if (!type
|| !field_type
|| type
->frozen
||
1806 bt_ctf_validate_identifier(field_name
) ||
1807 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) ||
1808 type
== field_type
) {
1813 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1815 /* The user has explicitly provided a tag; validate against it. */
1819 /* Make sure this name is present in the enum tag */
1820 for (i
= 0; i
< variant
->tag
->entries
->len
; i
++) {
1821 struct enumeration_mapping
*mapping
=
1822 g_ptr_array_index(variant
->tag
->entries
, i
);
1824 if (mapping
->string
== field_name_quark
) {
1831 /* Validation failed */
1837 if (add_structure_field(variant
->fields
, variant
->field_name_to_index
,
1838 field_type
, field_name
)) {
1846 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_by_name(
1847 struct bt_ctf_field_type
*type
,
1848 const char *field_name
)
1852 struct structure_field
*field
;
1853 struct bt_ctf_field_type_variant
*variant
;
1854 struct bt_ctf_field_type
*field_type
= NULL
;
1856 if (!type
|| !field_name
) {
1860 name_quark
= g_quark_try_string(field_name
);
1865 variant
= container_of(type
, struct bt_ctf_field_type_variant
, parent
);
1866 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
1867 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
1871 field
= g_ptr_array_index(variant
->fields
, index
);
1872 field_type
= field
->type
;
1878 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_from_tag(
1879 struct bt_ctf_field_type
*type
,
1880 struct bt_ctf_field
*tag
)
1882 const char *enum_value
;
1883 struct bt_ctf_field_type
*field_type
= NULL
;
1885 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1889 enum_value
= bt_ctf_field_enumeration_get_single_mapping_name(tag
);
1894 /* Already increments field_type's reference count */
1895 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1901 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1904 struct bt_ctf_field_type_variant
*variant
;
1906 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1911 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1913 ret
= (int) variant
->fields
->len
;
1919 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1920 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1923 struct bt_ctf_field_type_variant
*variant
;
1924 struct structure_field
*field
;
1927 if (!type
|| index
< 0 ||
1928 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1933 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1935 if (index
>= variant
->fields
->len
) {
1940 field
= g_ptr_array_index(variant
->fields
, index
);
1942 *field_type
= field
->type
;
1943 bt_get(field
->type
);
1946 *field_name
= g_quark_to_string(field
->name
);
1952 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1953 struct bt_ctf_field_type
*element_type
,
1954 unsigned int length
)
1956 struct bt_ctf_field_type_array
*array
= NULL
;
1958 if (!element_type
|| length
== 0) {
1962 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1967 array
->parent
.declaration
= &array
->declaration
.p
;
1968 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1970 bt_get(element_type
);
1971 array
->element_type
= element_type
;
1972 array
->length
= length
;
1973 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1974 return &array
->parent
;
1979 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1980 struct bt_ctf_field_type
*type
)
1982 struct bt_ctf_field_type
*ret
= NULL
;
1983 struct bt_ctf_field_type_array
*array
;
1985 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1989 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1990 ret
= array
->element_type
;
1997 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
1998 struct bt_ctf_field_type
*element_type
)
2001 struct bt_ctf_field_type_array
*array
;
2003 if (!type
|| !element_type
||
2004 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2009 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2011 if (array
->element_type
) {
2012 BT_PUT(array
->element_type
);
2015 array
->element_type
= element_type
;
2016 bt_get(array
->element_type
);
2022 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2025 struct bt_ctf_field_type_array
*array
;
2027 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2032 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2033 ret
= (int64_t) array
->length
;
2038 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2039 struct bt_ctf_field_type
*element_type
,
2040 const char *length_field_name
)
2042 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2044 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2048 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2053 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
2054 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
2055 bt_get(element_type
);
2056 sequence
->element_type
= element_type
;
2057 sequence
->length_field_name
= g_string_new(length_field_name
);
2058 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2059 return &sequence
->parent
;
2064 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2065 struct bt_ctf_field_type
*type
)
2067 struct bt_ctf_field_type
*ret
= NULL
;
2068 struct bt_ctf_field_type_sequence
*sequence
;
2070 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2074 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2076 ret
= sequence
->element_type
;
2083 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2084 struct bt_ctf_field_type
*element_type
)
2087 struct bt_ctf_field_type_sequence
*sequence
;
2089 if (!type
|| !element_type
||
2090 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2095 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2097 if (sequence
->element_type
) {
2098 BT_PUT(sequence
->element_type
);
2101 sequence
->element_type
= element_type
;
2102 bt_get(sequence
->element_type
);
2108 const char *bt_ctf_field_type_sequence_get_length_field_name(
2109 struct bt_ctf_field_type
*type
)
2111 const char *ret
= NULL
;
2112 struct bt_ctf_field_type_sequence
*sequence
;
2114 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2118 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2120 ret
= sequence
->length_field_name
->str
;
2125 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2127 struct bt_ctf_field_type_string
*string
=
2128 g_new0(struct bt_ctf_field_type_string
, 1);
2134 string
->parent
.declaration
= &string
->declaration
.p
;
2135 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
2136 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2137 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2138 string
->parent
.declaration
->alignment
= CHAR_BIT
;
2139 return &string
->parent
;
2142 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2143 struct bt_ctf_field_type
*type
)
2145 struct bt_ctf_field_type_string
*string
;
2146 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2148 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
2152 string
= container_of(type
, struct bt_ctf_field_type_string
,
2154 ret
= string
->declaration
.encoding
;
2159 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2160 enum bt_ctf_string_encoding encoding
)
2163 struct bt_ctf_field_type_string
*string
;
2165 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2166 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2167 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2172 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2173 string
->declaration
.encoding
= encoding
;
2178 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2181 enum bt_ctf_type_id type_id
;
2189 ret
= (int) type
->declaration
->alignment
;
2193 type_id
= bt_ctf_field_type_get_type_id(type
);
2195 case BT_CTF_TYPE_ID_SEQUENCE
:
2197 struct bt_ctf_field_type
*element
=
2198 bt_ctf_field_type_sequence_get_element_type(type
);
2205 ret
= bt_ctf_field_type_get_alignment(element
);
2209 case BT_CTF_TYPE_ID_ARRAY
:
2211 struct bt_ctf_field_type
*element
=
2212 bt_ctf_field_type_array_get_element_type(type
);
2219 ret
= bt_ctf_field_type_get_alignment(element
);
2223 case BT_CTF_TYPE_ID_STRUCT
:
2225 int i
, element_count
;
2227 element_count
= bt_ctf_field_type_structure_get_field_count(
2229 if (element_count
< 0) {
2230 ret
= element_count
;
2234 for (i
= 0; i
< element_count
; i
++) {
2235 struct bt_ctf_field_type
*field
;
2236 int field_alignment
;
2238 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2245 field_alignment
= bt_ctf_field_type_get_alignment(
2248 if (field_alignment
< 0) {
2249 ret
= field_alignment
;
2253 type
->declaration
->alignment
= MAX(field_alignment
,
2254 type
->declaration
->alignment
);
2256 ret
= (int) type
->declaration
->alignment
;
2259 case BT_CTF_TYPE_ID_UNKNOWN
:
2263 ret
= (int) type
->declaration
->alignment
;
2271 int is_power_of_two(unsigned int value
)
2273 return ((value
& (value
- 1)) == 0) && value
> 0;
2276 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2277 unsigned int alignment
)
2280 enum bt_ctf_type_id type_id
;
2282 /* Alignment must be a power of two */
2283 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2288 type_id
= bt_ctf_field_type_get_type_id(type
);
2289 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2294 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2295 alignment
!= CHAR_BIT
) {
2300 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2301 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2302 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2303 /* Setting an alignment on these types makes no sense */
2308 type
->declaration
->alignment
= alignment
;
2314 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2315 struct bt_ctf_field_type
*type
)
2317 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2323 switch (type
->declaration
->id
) {
2324 case BT_CTF_TYPE_ID_INTEGER
:
2326 struct bt_ctf_field_type_integer
*integer
= container_of(
2327 type
, struct bt_ctf_field_type_integer
, parent
);
2328 ret
= integer
->user_byte_order
;
2331 case BT_CTF_TYPE_ID_ENUM
:
2333 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2334 type
, struct bt_ctf_field_type_enumeration
, parent
);
2335 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2338 case BT_CTF_TYPE_ID_FLOAT
:
2340 struct bt_ctf_field_type_floating_point
*floating_point
=
2342 struct bt_ctf_field_type_floating_point
,
2344 ret
= floating_point
->user_byte_order
;
2351 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2352 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2353 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2354 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2360 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2361 enum bt_ctf_byte_order byte_order
)
2364 int internal_byte_order
;
2365 enum bt_ctf_type_id type_id
;
2367 if (!type
|| type
->frozen
) {
2372 switch (byte_order
) {
2373 case BT_CTF_BYTE_ORDER_NATIVE
:
2374 /* Leave unset. Will be initialized by parent. */
2375 internal_byte_order
= 0;
2377 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2378 internal_byte_order
= LITTLE_ENDIAN
;
2380 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2381 case BT_CTF_BYTE_ORDER_NETWORK
:
2382 internal_byte_order
= BIG_ENDIAN
;
2389 type_id
= type
->declaration
->id
;
2390 if (set_byte_order_funcs
[type_id
]) {
2391 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2397 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2398 struct bt_ctf_field_type
*type
)
2401 return BT_CTF_TYPE_ID_UNKNOWN
;
2404 return type
->declaration
->id
;
2407 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2409 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2412 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2414 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2417 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2419 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2422 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2424 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2427 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2429 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2432 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2434 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2437 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2439 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2442 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2444 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2447 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2452 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2458 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2468 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2469 struct bt_ctf_field_type_variant
*variant
,
2472 struct bt_ctf_field_type
*type
= NULL
;
2473 GQuark field_name_quark
;
2475 struct structure_field
*field_entry
;
2476 struct range_overlap_query query
= {
2477 .range_start
._signed
= tag_value
,
2478 .range_end
._signed
= tag_value
,
2483 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2485 if (!query
.overlaps
) {
2489 field_name_quark
= query
.mapping_name
;
2490 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2491 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2495 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2496 type
= field_entry
->type
;
2502 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2503 struct bt_ctf_field_type_variant
*variant
,
2506 struct bt_ctf_field_type
*type
= NULL
;
2507 GQuark field_name_quark
;
2509 struct structure_field
*field_entry
;
2510 struct range_overlap_query query
= {
2511 .range_start
._unsigned
= tag_value
,
2512 .range_end
._unsigned
= tag_value
,
2517 g_ptr_array_foreach(variant
->tag
->entries
,
2518 check_ranges_overlap_unsigned
,
2520 if (!query
.overlaps
) {
2524 field_name_quark
= query
.mapping_name
;
2525 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2526 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2530 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2531 type
= field_entry
->type
;
2537 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2538 struct metadata_context
*context
)
2542 if (!type
|| !context
) {
2547 /* Make sure field type is valid before serializing it */
2548 ret
= bt_ctf_field_type_validate(type
);
2554 ret
= type
->serialize(type
, context
);
2560 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2567 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2568 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2569 set_byte_order_funcs
[type
->declaration
->id
](type
,
2574 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2576 struct bt_ctf_field_type
*copy
= NULL
;
2582 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2588 int bt_ctf_field_type_structure_get_field_name_index(
2589 struct bt_ctf_field_type
*type
, const char *name
)
2594 struct bt_ctf_field_type_structure
*structure
;
2596 if (!type
|| !name
||
2597 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2602 name_quark
= g_quark_try_string(name
);
2608 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2610 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2611 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2621 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2622 struct bt_ctf_field_type
*field
, int index
)
2625 struct bt_ctf_field_type_structure
*structure
;
2627 if (!type
|| !field
||
2628 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2633 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2635 if (index
< 0 || index
>= structure
->fields
->len
) {
2641 bt_put(((struct structure_field
*)
2642 g_ptr_array_index(structure
->fields
, index
))->type
);
2643 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2650 int bt_ctf_field_type_variant_get_field_name_index(
2651 struct bt_ctf_field_type
*type
, const char *name
)
2656 struct bt_ctf_field_type_variant
*variant
;
2658 if (!type
|| !name
||
2659 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2664 name_quark
= g_quark_try_string(name
);
2670 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2672 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2673 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2683 int bt_ctf_field_type_sequence_set_length_field_path(
2684 struct bt_ctf_field_type
*type
,
2685 struct bt_ctf_field_path
*path
)
2688 struct bt_ctf_field_type_sequence
*sequence
;
2690 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2691 BT_CTF_TYPE_ID_SEQUENCE
) {
2696 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2699 BT_MOVE(sequence
->length_field_path
, path
);
2705 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2706 struct bt_ctf_field_path
*path
)
2709 struct bt_ctf_field_type_variant
*variant
;
2711 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2712 BT_CTF_TYPE_ID_VARIANT
) {
2717 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2720 BT_MOVE(variant
->tag_field_path
, path
);
2726 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2727 struct bt_ctf_field_type
*tag
)
2730 struct bt_ctf_field_type_variant
*variant
;
2732 if (!type
|| !tag
||
2733 bt_ctf_field_type_get_type_id(tag
) !=
2734 BT_CTF_TYPE_ID_ENUM
) {
2739 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2743 bt_put(&variant
->tag
->parent
);
2745 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2752 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2753 struct bt_ctf_field_type
*field
, int index
)
2756 struct bt_ctf_field_type_variant
*variant
;
2758 if (!type
|| !field
||
2759 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2764 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2766 if (index
< 0 || index
>= variant
->fields
->len
) {
2772 bt_put(((struct structure_field
*)
2773 g_ptr_array_index(variant
->fields
, index
))->type
);
2774 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2781 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2783 struct bt_ctf_field_type_integer
*integer
=
2784 (struct bt_ctf_field_type_integer
*) type
;
2790 bt_put(integer
->mapped_clock
);
2795 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2797 struct bt_ctf_field_type_enumeration
*enumeration
=
2798 (struct bt_ctf_field_type_enumeration
*) type
;
2804 g_ptr_array_free(enumeration
->entries
, TRUE
);
2805 bt_put(enumeration
->container
);
2806 g_free(enumeration
);
2810 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2812 struct bt_ctf_field_type_floating_point
*floating_point
=
2813 (struct bt_ctf_field_type_floating_point
*) type
;
2819 g_free(floating_point
);
2823 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2825 struct bt_ctf_field_type_structure
*structure
=
2826 (struct bt_ctf_field_type_structure
*) type
;
2832 g_ptr_array_free(structure
->fields
, TRUE
);
2833 g_hash_table_destroy(structure
->field_name_to_index
);
2838 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2840 struct bt_ctf_field_type_variant
*variant
=
2841 (struct bt_ctf_field_type_variant
*) type
;
2847 g_ptr_array_free(variant
->fields
, TRUE
);
2848 g_hash_table_destroy(variant
->field_name_to_index
);
2849 g_string_free(variant
->tag_name
, TRUE
);
2850 bt_put(&variant
->tag
->parent
);
2851 BT_PUT(variant
->tag_field_path
);
2856 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2858 struct bt_ctf_field_type_array
*array
=
2859 (struct bt_ctf_field_type_array
*) type
;
2865 bt_put(array
->element_type
);
2870 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2872 struct bt_ctf_field_type_sequence
*sequence
=
2873 (struct bt_ctf_field_type_sequence
*) type
;
2879 bt_put(sequence
->element_type
);
2880 g_string_free(sequence
->length_field_name
, TRUE
);
2881 BT_PUT(sequence
->length_field_path
);
2886 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2888 struct bt_ctf_field_type_string
*string
=
2889 (struct bt_ctf_field_type_string
*) type
;
2899 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2905 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2907 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2908 type
, struct bt_ctf_field_type_integer
, parent
);
2910 if (integer_type
->mapped_clock
) {
2911 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2914 generic_field_type_freeze(type
);
2918 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2920 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2921 type
, struct bt_ctf_field_type_enumeration
, parent
);
2923 set_enumeration_range_overlap(type
);
2925 generic_field_type_freeze(type
);
2926 bt_ctf_field_type_freeze(enumeration_type
->container
);
2930 void freeze_structure_field(struct structure_field
*field
)
2932 bt_ctf_field_type_freeze(field
->type
);
2936 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2938 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2939 type
, struct bt_ctf_field_type_structure
, parent
);
2941 /* Cache the alignment */
2942 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2943 generic_field_type_freeze(type
);
2944 g_ptr_array_foreach(structure_type
->fields
,
2945 (GFunc
) freeze_structure_field
, NULL
);
2949 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2951 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2952 type
, struct bt_ctf_field_type_variant
, parent
);
2954 generic_field_type_freeze(type
);
2955 g_ptr_array_foreach(variant_type
->fields
,
2956 (GFunc
) freeze_structure_field
, NULL
);
2960 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2962 struct bt_ctf_field_type_array
*array_type
= container_of(
2963 type
, struct bt_ctf_field_type_array
, parent
);
2965 /* Cache the alignment */
2966 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2967 generic_field_type_freeze(type
);
2968 bt_ctf_field_type_freeze(array_type
->element_type
);
2972 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2974 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2975 type
, struct bt_ctf_field_type_sequence
, parent
);
2977 /* Cache the alignment */
2978 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2979 generic_field_type_freeze(type
);
2980 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2984 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2986 const char *encoding_string
;
2989 case BT_CTF_STRING_ENCODING_NONE
:
2990 encoding_string
= "none";
2992 case BT_CTF_STRING_ENCODING_ASCII
:
2993 encoding_string
= "ASCII";
2995 case BT_CTF_STRING_ENCODING_UTF8
:
2996 encoding_string
= "UTF8";
2999 encoding_string
= "unknown";
3003 return encoding_string
;
3007 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
3009 const char *base_string
;
3012 case BT_CTF_INTEGER_BASE_DECIMAL
:
3013 base_string
= "decimal";
3015 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
3016 base_string
= "hexadecimal";
3018 case BT_CTF_INTEGER_BASE_OCTAL
:
3019 base_string
= "octal";
3021 case BT_CTF_INTEGER_BASE_BINARY
:
3022 base_string
= "binary";
3025 base_string
= "unknown";
3033 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3034 struct metadata_context
*context
)
3036 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3037 struct bt_ctf_field_type_integer
, parent
);
3040 g_string_append_printf(context
->string
,
3041 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
3042 integer
->declaration
.len
, type
->declaration
->alignment
,
3043 (integer
->declaration
.signedness
? "true" : "false"),
3044 get_encoding_string(integer
->declaration
.encoding
),
3045 get_integer_base_string(integer
->declaration
.base
),
3046 get_byte_order_string(integer
->declaration
.byte_order
));
3047 if (integer
->mapped_clock
) {
3048 const char *clock_name
= bt_ctf_clock_class_get_name(
3049 integer
->mapped_clock
);
3056 g_string_append_printf(context
->string
,
3057 "; map = clock.%s.value", clock_name
);
3060 g_string_append(context
->string
, "; }");
3066 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3067 struct metadata_context
*context
)
3071 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3072 struct bt_ctf_field_type_enumeration
, parent
);
3073 struct bt_ctf_field_type
*container_type
;
3074 int container_signed
;
3076 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3077 if (!container_type
) {
3082 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3083 if (container_signed
< 0) {
3084 ret
= container_signed
;
3085 goto error_put_container_type
;
3088 g_string_append(context
->string
, "enum : ");
3089 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3091 goto error_put_container_type
;
3094 g_string_append(context
->string
, " { ");
3095 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3096 struct enumeration_mapping
*mapping
=
3097 enumeration
->entries
->pdata
[entry
];
3099 if (container_signed
) {
3100 if (mapping
->range_start
._signed
==
3101 mapping
->range_end
._signed
) {
3102 g_string_append_printf(context
->string
,
3103 "\"%s\" = %" PRId64
,
3104 g_quark_to_string(mapping
->string
),
3105 mapping
->range_start
._signed
);
3107 g_string_append_printf(context
->string
,
3108 "\"%s\" = %" PRId64
" ... %" PRId64
,
3109 g_quark_to_string(mapping
->string
),
3110 mapping
->range_start
._signed
,
3111 mapping
->range_end
._signed
);
3114 if (mapping
->range_start
._unsigned
==
3115 mapping
->range_end
._unsigned
) {
3116 g_string_append_printf(context
->string
,
3117 "\"%s\" = %" PRIu64
,
3118 g_quark_to_string(mapping
->string
),
3119 mapping
->range_start
._unsigned
);
3121 g_string_append_printf(context
->string
,
3122 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3123 g_quark_to_string(mapping
->string
),
3124 mapping
->range_start
._unsigned
,
3125 mapping
->range_end
._unsigned
);
3129 g_string_append(context
->string
,
3130 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3134 if (context
->field_name
->len
) {
3135 g_string_append_printf(context
->string
, " %s",
3136 context
->field_name
->str
);
3137 g_string_assign(context
->field_name
, "");
3139 error_put_container_type
:
3140 bt_put(container_type
);
3146 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3147 struct metadata_context
*context
)
3149 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3150 type
, struct bt_ctf_field_type_floating_point
, parent
);
3152 g_string_append_printf(context
->string
,
3153 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
3154 floating_point
->declaration
.exp
->len
,
3155 floating_point
->declaration
.mantissa
->len
+ 1,
3156 get_byte_order_string(floating_point
->declaration
.byte_order
),
3157 type
->declaration
->alignment
);
3162 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3163 struct metadata_context
*context
)
3166 unsigned int indent
;
3168 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3169 struct bt_ctf_field_type_structure
, parent
);
3170 GString
*structure_field_name
= context
->field_name
;
3172 context
->field_name
= g_string_new("");
3174 context
->current_indentation_level
++;
3175 g_string_append(context
->string
, "struct {\n");
3177 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3178 struct structure_field
*field
;
3180 for (indent
= 0; indent
< context
->current_indentation_level
;
3182 g_string_append_c(context
->string
, '\t');
3185 field
= structure
->fields
->pdata
[i
];
3186 g_string_assign(context
->field_name
,
3187 g_quark_to_string(field
->name
));
3188 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3193 if (context
->field_name
->len
) {
3194 g_string_append_printf(context
->string
, " %s",
3195 context
->field_name
->str
);
3197 g_string_append(context
->string
, ";\n");
3200 context
->current_indentation_level
--;
3201 for (indent
= 0; indent
< context
->current_indentation_level
;
3203 g_string_append_c(context
->string
, '\t');
3206 g_string_append_printf(context
->string
, "} align(%zu)",
3207 type
->declaration
->alignment
);
3209 g_string_free(context
->field_name
, TRUE
);
3210 context
->field_name
= structure_field_name
;
3215 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3216 struct metadata_context
*context
)
3219 unsigned int indent
;
3221 struct bt_ctf_field_type_variant
*variant
= container_of(
3222 type
, struct bt_ctf_field_type_variant
, parent
);
3223 GString
*variant_field_name
= context
->field_name
;
3225 context
->field_name
= g_string_new("");
3226 if (variant
->tag_name
->len
> 0) {
3227 g_string_append_printf(context
->string
,
3228 "variant <%s> {\n", variant
->tag_name
->str
);
3230 g_string_append(context
->string
, "variant {\n");
3233 context
->current_indentation_level
++;
3234 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3235 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3237 g_string_assign(context
->field_name
,
3238 g_quark_to_string(field
->name
));
3239 for (indent
= 0; indent
< context
->current_indentation_level
;
3241 g_string_append_c(context
->string
, '\t');
3244 g_string_assign(context
->field_name
,
3245 g_quark_to_string(field
->name
));
3246 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3251 if (context
->field_name
->len
) {
3252 g_string_append_printf(context
->string
, " %s;",
3253 context
->field_name
->str
);
3256 g_string_append_c(context
->string
, '\n');
3259 context
->current_indentation_level
--;
3260 for (indent
= 0; indent
< context
->current_indentation_level
;
3262 g_string_append_c(context
->string
, '\t');
3265 g_string_append(context
->string
, "}");
3267 g_string_free(context
->field_name
, TRUE
);
3268 context
->field_name
= variant_field_name
;
3273 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3274 struct metadata_context
*context
)
3277 struct bt_ctf_field_type_array
*array
= container_of(type
,
3278 struct bt_ctf_field_type_array
, parent
);
3280 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3285 if (context
->field_name
->len
) {
3286 g_string_append_printf(context
->string
, " %s[%u]",
3287 context
->field_name
->str
, array
->length
);
3288 g_string_assign(context
->field_name
, "");
3290 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3297 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3298 struct metadata_context
*context
)
3301 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3302 type
, struct bt_ctf_field_type_sequence
, parent
);
3304 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3309 if (context
->field_name
->len
) {
3310 g_string_append_printf(context
->string
, " %s[%s]",
3311 context
->field_name
->str
,
3312 sequence
->length_field_name
->str
);
3313 g_string_assign(context
->field_name
, "");
3315 g_string_append_printf(context
->string
, "[%s]",
3316 sequence
->length_field_name
->str
);
3323 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3324 struct metadata_context
*context
)
3326 struct bt_ctf_field_type_string
*string
= container_of(
3327 type
, struct bt_ctf_field_type_string
, parent
);
3329 g_string_append_printf(context
->string
,
3330 "string { encoding = %s; }",
3331 get_encoding_string(string
->declaration
.encoding
));
3336 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3337 enum bt_ctf_byte_order ret
;
3339 switch (byte_order
) {
3340 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3342 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3344 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3346 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3348 case BT_CTF_BYTE_ORDER_NETWORK
:
3349 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3351 case BT_CTF_BYTE_ORDER_NATIVE
:
3352 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3355 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3363 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3364 int byte_order
, int set_native
)
3366 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3367 struct bt_ctf_field_type_integer
, parent
);
3370 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3372 * User byte order is native, so we can set
3373 * the real byte order.
3375 integer_type
->declaration
.byte_order
=
3379 integer_type
->user_byte_order
=
3380 get_ctf_ir_byte_order(byte_order
);
3381 integer_type
->declaration
.byte_order
= byte_order
;
3386 void bt_ctf_field_type_enumeration_set_byte_order(
3387 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3389 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3390 struct bt_ctf_field_type_enumeration
, parent
);
3392 /* Safe to assume that container is an integer */
3393 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3394 byte_order
, set_native
);
3398 void bt_ctf_field_type_floating_point_set_byte_order(
3399 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3401 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3402 container_of(type
, struct bt_ctf_field_type_floating_point
,
3406 if (floating_point_type
->user_byte_order
==
3407 BT_CTF_BYTE_ORDER_NATIVE
) {
3409 * User byte order is native, so we can set
3410 * the real byte order.
3412 floating_point_type
->declaration
.byte_order
=
3414 floating_point_type
->sign
.byte_order
=
3416 floating_point_type
->mantissa
.byte_order
=
3418 floating_point_type
->exp
.byte_order
=
3422 floating_point_type
->user_byte_order
=
3423 get_ctf_ir_byte_order(byte_order
);
3424 floating_point_type
->declaration
.byte_order
= byte_order
;
3425 floating_point_type
->sign
.byte_order
= byte_order
;
3426 floating_point_type
->mantissa
.byte_order
= byte_order
;
3427 floating_point_type
->exp
.byte_order
= byte_order
;
3432 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3433 int byte_order
, int set_native
)
3436 struct bt_ctf_field_type_structure
*structure_type
=
3437 container_of(type
, struct bt_ctf_field_type_structure
,
3440 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3441 struct structure_field
*field
= g_ptr_array_index(
3442 structure_type
->fields
, i
);
3443 struct bt_ctf_field_type
*field_type
= field
->type
;
3445 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3446 set_byte_order_funcs
[field_type
->declaration
->id
](
3447 field_type
, byte_order
, set_native
);
3453 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3454 int byte_order
, int set_native
)
3457 struct bt_ctf_field_type_variant
*variant_type
=
3458 container_of(type
, struct bt_ctf_field_type_variant
,
3461 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3462 struct structure_field
*field
= g_ptr_array_index(
3463 variant_type
->fields
, i
);
3464 struct bt_ctf_field_type
*field_type
= field
->type
;
3466 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3467 set_byte_order_funcs
[field_type
->declaration
->id
](
3468 field_type
, byte_order
, set_native
);
3474 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3475 int byte_order
, int set_native
)
3477 struct bt_ctf_field_type_array
*array_type
=
3478 container_of(type
, struct bt_ctf_field_type_array
,
3481 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3482 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3483 array_type
->element_type
, byte_order
, set_native
);
3488 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3489 int byte_order
, int set_native
)
3491 struct bt_ctf_field_type_sequence
*sequence_type
=
3492 container_of(type
, struct bt_ctf_field_type_sequence
,
3495 if (set_byte_order_funcs
[
3496 sequence_type
->element_type
->declaration
->id
]) {
3497 set_byte_order_funcs
[
3498 sequence_type
->element_type
->declaration
->id
](
3499 sequence_type
->element_type
, byte_order
, set_native
);
3504 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3505 struct bt_ctf_field_type
*type
)
3507 struct bt_ctf_field_type
*copy
;
3508 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3510 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3511 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3516 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3518 copy_integer
->declaration
= integer
->declaration
;
3519 if (integer
->mapped_clock
) {
3520 bt_get(integer
->mapped_clock
);
3521 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3524 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3531 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3532 struct bt_ctf_field_type
*type
)
3535 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3536 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3538 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3541 /* Copy the source enumeration's container */
3542 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3543 if (!copy_container
) {
3547 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3551 copy_enumeration
= container_of(copy
,
3552 struct bt_ctf_field_type_enumeration
, parent
);
3554 /* Copy all enumaration entries */
3555 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3556 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3557 enumeration
->entries
, i
);
3558 struct enumeration_mapping
*copy_mapping
= g_new0(
3559 struct enumeration_mapping
, 1);
3561 if (!copy_mapping
) {
3565 *copy_mapping
= *mapping
;
3566 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3569 copy_enumeration
->declaration
= enumeration
->declaration
;
3571 bt_put(copy_container
);
3574 bt_put(copy_container
);
3580 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3581 struct bt_ctf_field_type
*type
)
3583 struct bt_ctf_field_type
*copy
;
3584 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3586 floating_point
= container_of(type
,
3587 struct bt_ctf_field_type_floating_point
, parent
);
3588 copy
= bt_ctf_field_type_floating_point_create();
3593 copy_float
= container_of(copy
,
3594 struct bt_ctf_field_type_floating_point
, parent
);
3595 copy_float
->declaration
= floating_point
->declaration
;
3596 copy_float
->sign
= floating_point
->sign
;
3597 copy_float
->mantissa
= floating_point
->mantissa
;
3598 copy_float
->exp
= floating_point
->exp
;
3599 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3600 copy_float
->declaration
.sign
= ©_float
->sign
;
3601 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3602 copy_float
->declaration
.exp
= ©_float
->exp
;
3608 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3609 struct bt_ctf_field_type
*type
)
3612 GHashTableIter iter
;
3613 gpointer key
, value
;
3614 struct bt_ctf_field_type
*copy
;
3615 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3617 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3619 copy
= bt_ctf_field_type_structure_create();
3624 copy_structure
= container_of(copy
,
3625 struct bt_ctf_field_type_structure
, parent
);
3627 /* Copy field_name_to_index */
3628 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3629 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3630 g_hash_table_insert(copy_structure
->field_name_to_index
,
3634 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3635 struct structure_field
*entry
, *copy_entry
;
3636 struct bt_ctf_field_type
*copy_field
;
3638 copy_entry
= g_new0(struct structure_field
, 1);
3643 entry
= g_ptr_array_index(structure
->fields
, i
);
3644 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3650 copy_entry
->name
= entry
->name
;
3651 copy_entry
->type
= copy_field
;
3652 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3655 copy_structure
->declaration
= structure
->declaration
;
3664 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3665 struct bt_ctf_field_type
*type
)
3668 GHashTableIter iter
;
3669 gpointer key
, value
;
3670 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3671 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3673 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3676 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3682 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3683 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3688 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3691 /* Copy field_name_to_index */
3692 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3693 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3694 g_hash_table_insert(copy_variant
->field_name_to_index
,
3698 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3699 struct structure_field
*entry
, *copy_entry
;
3700 struct bt_ctf_field_type
*copy_field
;
3702 copy_entry
= g_new0(struct structure_field
, 1);
3707 entry
= g_ptr_array_index(variant
->fields
, i
);
3708 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3714 copy_entry
->name
= entry
->name
;
3715 copy_entry
->type
= copy_field
;
3716 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3719 copy_variant
->declaration
= variant
->declaration
;
3720 if (variant
->tag_field_path
) {
3721 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3722 variant
->tag_field_path
);
3723 if (!copy_variant
->tag_field_path
) {
3737 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3738 struct bt_ctf_field_type
*type
)
3740 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3741 struct bt_ctf_field_type_array
*array
, *copy_array
;
3743 array
= container_of(type
, struct bt_ctf_field_type_array
,
3745 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3746 if (!copy_element
) {
3750 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3755 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3757 copy_array
->declaration
= array
->declaration
;
3759 bt_put(copy_element
);
3764 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3765 struct bt_ctf_field_type
*type
)
3767 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3768 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3770 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3772 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3773 if (!copy_element
) {
3777 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3778 sequence
->length_field_name
->len
?
3779 sequence
->length_field_name
->str
: NULL
);
3784 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3786 copy_sequence
->declaration
= sequence
->declaration
;
3787 if (sequence
->length_field_path
) {
3788 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3789 sequence
->length_field_path
);
3790 if (!copy_sequence
->length_field_path
) {
3795 bt_put(copy_element
);
3803 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3804 struct bt_ctf_field_type
*type
)
3806 struct bt_ctf_field_type
*copy
;
3807 struct bt_ctf_field_type_string
*string
, *copy_string
;
3809 copy
= bt_ctf_field_type_string_create();
3814 string
= container_of(type
, struct bt_ctf_field_type_string
,
3816 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3818 copy_string
->declaration
= string
->declaration
;
3824 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3825 struct bt_ctf_field_type
*type_b
)
3828 struct bt_ctf_field_type_integer
*integer_a
;
3829 struct bt_ctf_field_type_integer
*integer_b
;
3830 struct declaration_integer
*decl_a
;
3831 struct declaration_integer
*decl_b
;
3833 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3835 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3837 decl_a
= &integer_a
->declaration
;
3838 decl_b
= &integer_b
->declaration
;
3841 if (decl_a
->len
!= decl_b
->len
) {
3846 * Compare user byte orders only, not the cached,
3849 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3854 if (decl_a
->signedness
!= decl_b
->signedness
) {
3859 if (decl_a
->base
!= decl_b
->base
) {
3864 if (decl_a
->encoding
!= decl_b
->encoding
) {
3869 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3881 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3882 struct bt_ctf_field_type
*type_b
)
3885 struct bt_ctf_field_type_floating_point
*float_a
;
3886 struct bt_ctf_field_type_floating_point
*float_b
;
3888 float_a
= container_of(type_a
,
3889 struct bt_ctf_field_type_floating_point
, parent
);
3890 float_b
= container_of(type_b
,
3891 struct bt_ctf_field_type_floating_point
, parent
);
3894 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3898 /* Exponent length */
3899 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3903 /* Mantissa length */
3904 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3909 * Compare user byte orders only, not the cached,
3912 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3924 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3925 struct enumeration_mapping
*mapping_b
)
3930 if (mapping_a
->string
!= mapping_b
->string
) {
3935 if (mapping_a
->range_start
._unsigned
!=
3936 mapping_b
->range_start
._unsigned
) {
3941 if (mapping_a
->range_end
._unsigned
!=
3942 mapping_b
->range_end
._unsigned
) {
3954 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3955 struct bt_ctf_field_type
*type_b
)
3959 struct bt_ctf_field_type_enumeration
*enum_a
;
3960 struct bt_ctf_field_type_enumeration
*enum_b
;
3962 enum_a
= container_of(type_a
,
3963 struct bt_ctf_field_type_enumeration
, parent
);
3964 enum_b
= container_of(type_b
,
3965 struct bt_ctf_field_type_enumeration
, parent
);
3967 /* Container field type */
3968 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3976 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3980 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3981 struct enumeration_mapping
*mapping_a
=
3982 g_ptr_array_index(enum_a
->entries
, i
);
3983 struct enumeration_mapping
*mapping_b
=
3984 g_ptr_array_index(enum_b
->entries
, i
);
3986 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
3999 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
4000 struct bt_ctf_field_type
*type_b
)
4003 struct bt_ctf_field_type_string
*string_a
;
4004 struct bt_ctf_field_type_string
*string_b
;
4006 string_a
= container_of(type_a
,
4007 struct bt_ctf_field_type_string
, parent
);
4008 string_b
= container_of(type_b
,
4009 struct bt_ctf_field_type_string
, parent
);
4012 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
4024 int compare_structure_fields(struct structure_field
*field_a
,
4025 struct structure_field
*field_b
)
4030 if (field_a
->name
!= field_b
->name
) {
4035 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
4042 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
4043 struct bt_ctf_field_type
*type_b
)
4047 struct bt_ctf_field_type_structure
*struct_a
;
4048 struct bt_ctf_field_type_structure
*struct_b
;
4050 struct_a
= container_of(type_a
,
4051 struct bt_ctf_field_type_structure
, parent
);
4052 struct_b
= container_of(type_b
,
4053 struct bt_ctf_field_type_structure
, parent
);
4056 if (bt_ctf_field_type_get_alignment(type_a
) !=
4057 bt_ctf_field_type_get_alignment(type_b
)) {
4062 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4066 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4067 struct structure_field
*field_a
=
4068 g_ptr_array_index(struct_a
->fields
, i
);
4069 struct structure_field
*field_b
=
4070 g_ptr_array_index(struct_b
->fields
, i
);
4072 ret
= compare_structure_fields(field_a
, field_b
);
4086 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4087 struct bt_ctf_field_type
*type_b
)
4091 struct bt_ctf_field_type_variant
*variant_a
;
4092 struct bt_ctf_field_type_variant
*variant_b
;
4094 variant_a
= container_of(type_a
,
4095 struct bt_ctf_field_type_variant
, parent
);
4096 variant_b
= container_of(type_b
,
4097 struct bt_ctf_field_type_variant
, parent
);
4100 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4105 ret
= bt_ctf_field_type_compare(
4106 (struct bt_ctf_field_type
*) variant_a
->tag
,
4107 (struct bt_ctf_field_type
*) variant_b
->tag
);
4115 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
4119 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
4120 struct structure_field
*field_a
=
4121 g_ptr_array_index(variant_a
->fields
, i
);
4122 struct structure_field
*field_b
=
4123 g_ptr_array_index(variant_b
->fields
, i
);
4125 ret
= compare_structure_fields(field_a
, field_b
);
4139 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4140 struct bt_ctf_field_type
*type_b
)
4143 struct bt_ctf_field_type_array
*array_a
;
4144 struct bt_ctf_field_type_array
*array_b
;
4146 array_a
= container_of(type_a
,
4147 struct bt_ctf_field_type_array
, parent
);
4148 array_b
= container_of(type_b
,
4149 struct bt_ctf_field_type_array
, parent
);
4152 if (array_a
->length
!= array_b
->length
) {
4157 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4158 array_b
->element_type
);
4165 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4166 struct bt_ctf_field_type
*type_b
)
4169 struct bt_ctf_field_type_sequence
*sequence_a
;
4170 struct bt_ctf_field_type_sequence
*sequence_b
;
4172 sequence_a
= container_of(type_a
,
4173 struct bt_ctf_field_type_sequence
, parent
);
4174 sequence_b
= container_of(type_b
,
4175 struct bt_ctf_field_type_sequence
, parent
);
4178 if (strcmp(sequence_a
->length_field_name
->str
,
4179 sequence_b
->length_field_name
->str
)) {
4184 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4185 sequence_b
->element_type
);
4191 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4192 struct bt_ctf_field_type
*type_b
)
4196 if (type_a
== type_b
) {
4197 /* Same reference: equal (even if both are NULL) */
4202 if (!type_a
|| !type_b
) {
4207 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4208 /* Different type IDs */
4212 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4213 /* Both have unknown type IDs */
4217 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4224 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4226 int field_count
= -1;
4227 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4230 case CTF_TYPE_STRUCT
:
4232 bt_ctf_field_type_structure_get_field_count(field_type
);
4234 case CTF_TYPE_VARIANT
:
4236 bt_ctf_field_type_variant_get_field_count(field_type
);
4238 case CTF_TYPE_ARRAY
:
4239 case CTF_TYPE_SEQUENCE
:
4241 * Array and sequence types always contain a single member
4242 * (the element type).
4254 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4255 struct bt_ctf_field_type
*field_type
, int index
)
4257 struct bt_ctf_field_type
*field
= NULL
;
4258 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4261 case CTF_TYPE_STRUCT
:
4262 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4265 case CTF_TYPE_VARIANT
:
4267 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4275 case CTF_TYPE_ARRAY
:
4276 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4278 case CTF_TYPE_SEQUENCE
:
4279 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4289 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4292 int field_index
= -1;
4293 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4296 case CTF_TYPE_STRUCT
:
4297 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4300 case CTF_TYPE_VARIANT
:
4301 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4311 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4312 struct bt_ctf_field_type
*type
)
4314 struct bt_ctf_field_path
*field_path
= NULL
;
4315 struct bt_ctf_field_type_variant
*variant
;
4317 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4321 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4323 field_path
= bt_get(variant
->tag_field_path
);
4328 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4329 struct bt_ctf_field_type
*type
)
4331 struct bt_ctf_field_path
*field_path
= NULL
;
4332 struct bt_ctf_field_type_sequence
*sequence
;
4334 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4338 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4340 field_path
= bt_get(sequence
->length_field_path
);