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
)
1883 const char *enum_value
;
1884 struct bt_ctf_field_type
*field_type
= NULL
;
1885 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
1887 if (!type
|| !tag
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
) {
1891 iter
= bt_ctf_field_enumeration_get_mappings(tag
);
1896 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter
,
1902 field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
1909 int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type
*type
)
1912 struct bt_ctf_field_type_variant
*variant
;
1914 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1919 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1921 ret
= (int) variant
->fields
->len
;
1927 int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type
*type
,
1928 const char **field_name
, struct bt_ctf_field_type
**field_type
,
1931 struct bt_ctf_field_type_variant
*variant
;
1932 struct structure_field
*field
;
1935 if (!type
|| index
< 0 ||
1936 (type
->declaration
->id
!= BT_CTF_TYPE_ID_VARIANT
)) {
1941 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
1943 if (index
>= variant
->fields
->len
) {
1948 field
= g_ptr_array_index(variant
->fields
, index
);
1950 *field_type
= field
->type
;
1951 bt_get(field
->type
);
1954 *field_name
= g_quark_to_string(field
->name
);
1960 struct bt_ctf_field_type
*bt_ctf_field_type_array_create(
1961 struct bt_ctf_field_type
*element_type
,
1962 unsigned int length
)
1964 struct bt_ctf_field_type_array
*array
= NULL
;
1966 if (!element_type
|| length
== 0) {
1970 array
= g_new0(struct bt_ctf_field_type_array
, 1);
1975 array
->parent
.declaration
= &array
->declaration
.p
;
1976 array
->parent
.declaration
->id
= BT_CTF_TYPE_ID_ARRAY
;
1978 bt_get(element_type
);
1979 array
->element_type
= element_type
;
1980 array
->length
= length
;
1981 bt_ctf_field_type_init(&array
->parent
, FALSE
);
1982 return &array
->parent
;
1987 struct bt_ctf_field_type
*bt_ctf_field_type_array_get_element_type(
1988 struct bt_ctf_field_type
*type
)
1990 struct bt_ctf_field_type
*ret
= NULL
;
1991 struct bt_ctf_field_type_array
*array
;
1993 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
1997 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1998 ret
= array
->element_type
;
2005 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type
*type
,
2006 struct bt_ctf_field_type
*element_type
)
2009 struct bt_ctf_field_type_array
*array
;
2011 if (!type
|| !element_type
||
2012 (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2017 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2019 if (array
->element_type
) {
2020 BT_PUT(array
->element_type
);
2023 array
->element_type
= element_type
;
2024 bt_get(array
->element_type
);
2030 int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type
*type
)
2033 struct bt_ctf_field_type_array
*array
;
2035 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_ARRAY
)) {
2040 array
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
2041 ret
= (int64_t) array
->length
;
2046 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_create(
2047 struct bt_ctf_field_type
*element_type
,
2048 const char *length_field_name
)
2050 struct bt_ctf_field_type_sequence
*sequence
= NULL
;
2052 if (!element_type
|| bt_ctf_validate_identifier(length_field_name
)) {
2056 sequence
= g_new0(struct bt_ctf_field_type_sequence
, 1);
2061 sequence
->parent
.declaration
= &sequence
->declaration
.p
;
2062 sequence
->parent
.declaration
->id
= BT_CTF_TYPE_ID_SEQUENCE
;
2063 bt_get(element_type
);
2064 sequence
->element_type
= element_type
;
2065 sequence
->length_field_name
= g_string_new(length_field_name
);
2066 bt_ctf_field_type_init(&sequence
->parent
, FALSE
);
2067 return &sequence
->parent
;
2072 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_get_element_type(
2073 struct bt_ctf_field_type
*type
)
2075 struct bt_ctf_field_type
*ret
= NULL
;
2076 struct bt_ctf_field_type_sequence
*sequence
;
2078 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2082 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2084 ret
= sequence
->element_type
;
2091 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type
*type
,
2092 struct bt_ctf_field_type
*element_type
)
2095 struct bt_ctf_field_type_sequence
*sequence
;
2097 if (!type
|| !element_type
||
2098 (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2103 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
, parent
);
2105 if (sequence
->element_type
) {
2106 BT_PUT(sequence
->element_type
);
2109 sequence
->element_type
= element_type
;
2110 bt_get(sequence
->element_type
);
2116 const char *bt_ctf_field_type_sequence_get_length_field_name(
2117 struct bt_ctf_field_type
*type
)
2119 const char *ret
= NULL
;
2120 struct bt_ctf_field_type_sequence
*sequence
;
2122 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_SEQUENCE
)) {
2126 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2128 ret
= sequence
->length_field_name
->str
;
2133 struct bt_ctf_field_type
*bt_ctf_field_type_string_create(void)
2135 struct bt_ctf_field_type_string
*string
=
2136 g_new0(struct bt_ctf_field_type_string
, 1);
2142 string
->parent
.declaration
= &string
->declaration
.p
;
2143 string
->parent
.declaration
->id
= BT_CTF_TYPE_ID_STRING
;
2144 bt_ctf_field_type_init(&string
->parent
, TRUE
);
2145 string
->declaration
.encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2146 string
->parent
.declaration
->alignment
= CHAR_BIT
;
2147 return &string
->parent
;
2150 enum bt_ctf_string_encoding
bt_ctf_field_type_string_get_encoding(
2151 struct bt_ctf_field_type
*type
)
2153 struct bt_ctf_field_type_string
*string
;
2154 enum bt_ctf_string_encoding ret
= BT_CTF_STRING_ENCODING_UNKNOWN
;
2156 if (!type
|| (type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
)) {
2160 string
= container_of(type
, struct bt_ctf_field_type_string
,
2162 ret
= string
->declaration
.encoding
;
2167 int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type
*type
,
2168 enum bt_ctf_string_encoding encoding
)
2171 struct bt_ctf_field_type_string
*string
;
2173 if (!type
|| type
->declaration
->id
!= BT_CTF_TYPE_ID_STRING
||
2174 (encoding
!= BT_CTF_STRING_ENCODING_UTF8
&&
2175 encoding
!= BT_CTF_STRING_ENCODING_ASCII
)) {
2180 string
= container_of(type
, struct bt_ctf_field_type_string
, parent
);
2181 string
->declaration
.encoding
= encoding
;
2186 int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type
*type
)
2189 enum bt_ctf_type_id type_id
;
2197 ret
= (int) type
->declaration
->alignment
;
2201 type_id
= bt_ctf_field_type_get_type_id(type
);
2203 case BT_CTF_TYPE_ID_SEQUENCE
:
2205 struct bt_ctf_field_type
*element
=
2206 bt_ctf_field_type_sequence_get_element_type(type
);
2213 ret
= bt_ctf_field_type_get_alignment(element
);
2217 case BT_CTF_TYPE_ID_ARRAY
:
2219 struct bt_ctf_field_type
*element
=
2220 bt_ctf_field_type_array_get_element_type(type
);
2227 ret
= bt_ctf_field_type_get_alignment(element
);
2231 case BT_CTF_TYPE_ID_STRUCT
:
2233 int i
, element_count
;
2235 element_count
= bt_ctf_field_type_structure_get_field_count(
2237 if (element_count
< 0) {
2238 ret
= element_count
;
2242 for (i
= 0; i
< element_count
; i
++) {
2243 struct bt_ctf_field_type
*field
;
2244 int field_alignment
;
2246 ret
= bt_ctf_field_type_structure_get_field(type
, NULL
,
2253 field_alignment
= bt_ctf_field_type_get_alignment(
2256 if (field_alignment
< 0) {
2257 ret
= field_alignment
;
2261 type
->declaration
->alignment
= MAX(field_alignment
,
2262 type
->declaration
->alignment
);
2264 ret
= (int) type
->declaration
->alignment
;
2267 case BT_CTF_TYPE_ID_UNKNOWN
:
2271 ret
= (int) type
->declaration
->alignment
;
2279 int is_power_of_two(unsigned int value
)
2281 return ((value
& (value
- 1)) == 0) && value
> 0;
2284 int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type
*type
,
2285 unsigned int alignment
)
2288 enum bt_ctf_type_id type_id
;
2290 /* Alignment must be a power of two */
2291 if (!type
|| type
->frozen
|| !is_power_of_two(alignment
)) {
2296 type_id
= bt_ctf_field_type_get_type_id(type
);
2297 if (type_id
== BT_CTF_TYPE_ID_UNKNOWN
) {
2302 if (type
->declaration
->id
== BT_CTF_TYPE_ID_STRING
&&
2303 alignment
!= CHAR_BIT
) {
2308 if (type_id
== BT_CTF_TYPE_ID_VARIANT
||
2309 type_id
== BT_CTF_TYPE_ID_SEQUENCE
||
2310 type_id
== BT_CTF_TYPE_ID_ARRAY
) {
2311 /* Setting an alignment on these types makes no sense */
2316 type
->declaration
->alignment
= alignment
;
2322 enum bt_ctf_byte_order
bt_ctf_field_type_get_byte_order(
2323 struct bt_ctf_field_type
*type
)
2325 enum bt_ctf_byte_order ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
2331 switch (type
->declaration
->id
) {
2332 case BT_CTF_TYPE_ID_INTEGER
:
2334 struct bt_ctf_field_type_integer
*integer
= container_of(
2335 type
, struct bt_ctf_field_type_integer
, parent
);
2336 ret
= integer
->user_byte_order
;
2339 case BT_CTF_TYPE_ID_ENUM
:
2341 struct bt_ctf_field_type_enumeration
*enum_ft
= container_of(
2342 type
, struct bt_ctf_field_type_enumeration
, parent
);
2343 ret
= bt_ctf_field_type_get_byte_order(enum_ft
->container
);
2346 case BT_CTF_TYPE_ID_FLOAT
:
2348 struct bt_ctf_field_type_floating_point
*floating_point
=
2350 struct bt_ctf_field_type_floating_point
,
2352 ret
= floating_point
->user_byte_order
;
2359 assert(ret
== BT_CTF_BYTE_ORDER_NATIVE
||
2360 ret
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
2361 ret
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
||
2362 ret
== BT_CTF_BYTE_ORDER_NETWORK
);
2368 int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type
*type
,
2369 enum bt_ctf_byte_order byte_order
)
2372 int internal_byte_order
;
2373 enum bt_ctf_type_id type_id
;
2375 if (!type
|| type
->frozen
) {
2380 switch (byte_order
) {
2381 case BT_CTF_BYTE_ORDER_NATIVE
:
2382 /* Leave unset. Will be initialized by parent. */
2383 internal_byte_order
= 0;
2385 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
2386 internal_byte_order
= LITTLE_ENDIAN
;
2388 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
2389 case BT_CTF_BYTE_ORDER_NETWORK
:
2390 internal_byte_order
= BIG_ENDIAN
;
2397 type_id
= type
->declaration
->id
;
2398 if (set_byte_order_funcs
[type_id
]) {
2399 set_byte_order_funcs
[type_id
](type
, internal_byte_order
, 0);
2405 enum bt_ctf_type_id
bt_ctf_field_type_get_type_id(
2406 struct bt_ctf_field_type
*type
)
2409 return BT_CTF_TYPE_ID_UNKNOWN
;
2412 return type
->declaration
->id
;
2415 int bt_ctf_field_type_is_integer(struct bt_ctf_field_type
*type
)
2417 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_INTEGER
;
2420 int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type
*type
)
2422 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_FLOAT
;
2425 int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type
*type
)
2427 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ENUM
;
2430 int bt_ctf_field_type_is_string(struct bt_ctf_field_type
*type
)
2432 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRING
;
2435 int bt_ctf_field_type_is_structure(struct bt_ctf_field_type
*type
)
2437 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_STRUCT
;
2440 int bt_ctf_field_type_is_array(struct bt_ctf_field_type
*type
)
2442 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_ARRAY
;
2445 int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type
*type
)
2447 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_SEQUENCE
;
2450 int bt_ctf_field_type_is_variant(struct bt_ctf_field_type
*type
)
2452 return bt_ctf_field_type_get_type_id(type
) == BT_CTF_TYPE_ID_VARIANT
;
2455 void bt_ctf_field_type_get(struct bt_ctf_field_type
*type
)
2460 void bt_ctf_field_type_put(struct bt_ctf_field_type
*type
)
2466 void bt_ctf_field_type_freeze(struct bt_ctf_field_type
*type
)
2476 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_signed(
2477 struct bt_ctf_field_type_variant
*variant
,
2480 struct bt_ctf_field_type
*type
= NULL
;
2481 GQuark field_name_quark
;
2483 struct structure_field
*field_entry
;
2484 struct range_overlap_query query
= {
2485 .range_start
._signed
= tag_value
,
2486 .range_end
._signed
= tag_value
,
2491 g_ptr_array_foreach(variant
->tag
->entries
, check_ranges_overlap
,
2493 if (!query
.overlaps
) {
2497 field_name_quark
= query
.mapping_name
;
2498 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2499 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2503 field_entry
= g_ptr_array_index(variant
->fields
, (size_t) index
);
2504 type
= field_entry
->type
;
2510 struct bt_ctf_field_type
*bt_ctf_field_type_variant_get_field_type_unsigned(
2511 struct bt_ctf_field_type_variant
*variant
,
2514 struct bt_ctf_field_type
*type
= NULL
;
2515 GQuark field_name_quark
;
2517 struct structure_field
*field_entry
;
2518 struct range_overlap_query query
= {
2519 .range_start
._unsigned
= tag_value
,
2520 .range_end
._unsigned
= tag_value
,
2525 g_ptr_array_foreach(variant
->tag
->entries
,
2526 check_ranges_overlap_unsigned
,
2528 if (!query
.overlaps
) {
2532 field_name_quark
= query
.mapping_name
;
2533 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2534 GUINT_TO_POINTER(field_name_quark
), NULL
, &index
)) {
2538 field_entry
= g_ptr_array_index(variant
->fields
, (size_t)index
);
2539 type
= field_entry
->type
;
2545 int bt_ctf_field_type_serialize(struct bt_ctf_field_type
*type
,
2546 struct metadata_context
*context
)
2550 if (!type
|| !context
) {
2555 /* Make sure field type is valid before serializing it */
2556 ret
= bt_ctf_field_type_validate(type
);
2562 ret
= type
->serialize(type
, context
);
2568 void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type
*type
,
2575 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
2576 if (set_byte_order_funcs
[type
->declaration
->id
]) {
2577 set_byte_order_funcs
[type
->declaration
->id
](type
,
2582 struct bt_ctf_field_type
*bt_ctf_field_type_copy(struct bt_ctf_field_type
*type
)
2584 struct bt_ctf_field_type
*copy
= NULL
;
2590 copy
= type_copy_funcs
[type
->declaration
->id
](type
);
2596 int bt_ctf_field_type_structure_get_field_name_index(
2597 struct bt_ctf_field_type
*type
, const char *name
)
2602 struct bt_ctf_field_type_structure
*structure
;
2604 if (!type
|| !name
||
2605 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2610 name_quark
= g_quark_try_string(name
);
2616 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2618 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
2619 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2629 int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type
*type
,
2630 struct bt_ctf_field_type
*field
, int index
)
2633 struct bt_ctf_field_type_structure
*structure
;
2635 if (!type
|| !field
||
2636 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_STRUCT
) {
2641 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
2643 if (index
< 0 || index
>= structure
->fields
->len
) {
2649 bt_put(((struct structure_field
*)
2650 g_ptr_array_index(structure
->fields
, index
))->type
);
2651 ((struct structure_field
*) structure
->fields
->pdata
[index
])->type
=
2658 int bt_ctf_field_type_variant_get_field_name_index(
2659 struct bt_ctf_field_type
*type
, const char *name
)
2664 struct bt_ctf_field_type_variant
*variant
;
2666 if (!type
|| !name
||
2667 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2672 name_quark
= g_quark_try_string(name
);
2678 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2680 if (!g_hash_table_lookup_extended(variant
->field_name_to_index
,
2681 GUINT_TO_POINTER(name_quark
), NULL
, (gpointer
*)&index
)) {
2691 int bt_ctf_field_type_sequence_set_length_field_path(
2692 struct bt_ctf_field_type
*type
,
2693 struct bt_ctf_field_path
*path
)
2696 struct bt_ctf_field_type_sequence
*sequence
;
2698 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2699 BT_CTF_TYPE_ID_SEQUENCE
) {
2704 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
2707 BT_MOVE(sequence
->length_field_path
, path
);
2713 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type
*type
,
2714 struct bt_ctf_field_path
*path
)
2717 struct bt_ctf_field_type_variant
*variant
;
2719 if (!type
|| bt_ctf_field_type_get_type_id(type
) !=
2720 BT_CTF_TYPE_ID_VARIANT
) {
2725 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2728 BT_MOVE(variant
->tag_field_path
, path
);
2734 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type
*type
,
2735 struct bt_ctf_field_type
*tag
)
2738 struct bt_ctf_field_type_variant
*variant
;
2740 if (!type
|| !tag
||
2741 bt_ctf_field_type_get_type_id(tag
) !=
2742 BT_CTF_TYPE_ID_ENUM
) {
2747 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2751 bt_put(&variant
->tag
->parent
);
2753 variant
->tag
= container_of(tag
, struct bt_ctf_field_type_enumeration
,
2760 int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type
*type
,
2761 struct bt_ctf_field_type
*field
, int index
)
2764 struct bt_ctf_field_type_variant
*variant
;
2766 if (!type
|| !field
||
2767 bt_ctf_field_type_get_type_id(type
) != BT_CTF_TYPE_ID_VARIANT
) {
2772 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
2774 if (index
< 0 || index
>= variant
->fields
->len
) {
2780 bt_put(((struct structure_field
*)
2781 g_ptr_array_index(variant
->fields
, index
))->type
);
2782 ((struct structure_field
*) variant
->fields
->pdata
[index
])->type
=
2789 void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type
*type
)
2791 struct bt_ctf_field_type_integer
*integer
=
2792 (struct bt_ctf_field_type_integer
*) type
;
2798 bt_put(integer
->mapped_clock
);
2803 void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type
*type
)
2805 struct bt_ctf_field_type_enumeration
*enumeration
=
2806 (struct bt_ctf_field_type_enumeration
*) type
;
2812 g_ptr_array_free(enumeration
->entries
, TRUE
);
2813 bt_put(enumeration
->container
);
2814 g_free(enumeration
);
2818 void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type
*type
)
2820 struct bt_ctf_field_type_floating_point
*floating_point
=
2821 (struct bt_ctf_field_type_floating_point
*) type
;
2827 g_free(floating_point
);
2831 void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type
*type
)
2833 struct bt_ctf_field_type_structure
*structure
=
2834 (struct bt_ctf_field_type_structure
*) type
;
2840 g_ptr_array_free(structure
->fields
, TRUE
);
2841 g_hash_table_destroy(structure
->field_name_to_index
);
2846 void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type
*type
)
2848 struct bt_ctf_field_type_variant
*variant
=
2849 (struct bt_ctf_field_type_variant
*) type
;
2855 g_ptr_array_free(variant
->fields
, TRUE
);
2856 g_hash_table_destroy(variant
->field_name_to_index
);
2857 g_string_free(variant
->tag_name
, TRUE
);
2858 bt_put(&variant
->tag
->parent
);
2859 BT_PUT(variant
->tag_field_path
);
2864 void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type
*type
)
2866 struct bt_ctf_field_type_array
*array
=
2867 (struct bt_ctf_field_type_array
*) type
;
2873 bt_put(array
->element_type
);
2878 void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type
*type
)
2880 struct bt_ctf_field_type_sequence
*sequence
=
2881 (struct bt_ctf_field_type_sequence
*) type
;
2887 bt_put(sequence
->element_type
);
2888 g_string_free(sequence
->length_field_name
, TRUE
);
2889 BT_PUT(sequence
->length_field_path
);
2894 void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type
*type
)
2896 struct bt_ctf_field_type_string
*string
=
2897 (struct bt_ctf_field_type_string
*) type
;
2907 void generic_field_type_freeze(struct bt_ctf_field_type
*type
)
2913 void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type
*type
)
2915 struct bt_ctf_field_type_integer
*integer_type
= container_of(
2916 type
, struct bt_ctf_field_type_integer
, parent
);
2918 if (integer_type
->mapped_clock
) {
2919 bt_ctf_clock_class_freeze(integer_type
->mapped_clock
);
2922 generic_field_type_freeze(type
);
2926 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type
*type
)
2928 struct bt_ctf_field_type_enumeration
*enumeration_type
= container_of(
2929 type
, struct bt_ctf_field_type_enumeration
, parent
);
2931 set_enumeration_range_overlap(type
);
2933 generic_field_type_freeze(type
);
2934 bt_ctf_field_type_freeze(enumeration_type
->container
);
2938 void freeze_structure_field(struct structure_field
*field
)
2940 bt_ctf_field_type_freeze(field
->type
);
2944 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type
*type
)
2946 struct bt_ctf_field_type_structure
*structure_type
= container_of(
2947 type
, struct bt_ctf_field_type_structure
, parent
);
2949 /* Cache the alignment */
2950 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2951 generic_field_type_freeze(type
);
2952 g_ptr_array_foreach(structure_type
->fields
,
2953 (GFunc
) freeze_structure_field
, NULL
);
2957 void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type
*type
)
2959 struct bt_ctf_field_type_variant
*variant_type
= container_of(
2960 type
, struct bt_ctf_field_type_variant
, parent
);
2962 generic_field_type_freeze(type
);
2963 g_ptr_array_foreach(variant_type
->fields
,
2964 (GFunc
) freeze_structure_field
, NULL
);
2968 void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type
*type
)
2970 struct bt_ctf_field_type_array
*array_type
= container_of(
2971 type
, struct bt_ctf_field_type_array
, parent
);
2973 /* Cache the alignment */
2974 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2975 generic_field_type_freeze(type
);
2976 bt_ctf_field_type_freeze(array_type
->element_type
);
2980 void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type
*type
)
2982 struct bt_ctf_field_type_sequence
*sequence_type
= container_of(
2983 type
, struct bt_ctf_field_type_sequence
, parent
);
2985 /* Cache the alignment */
2986 type
->declaration
->alignment
= bt_ctf_field_type_get_alignment(type
);
2987 generic_field_type_freeze(type
);
2988 bt_ctf_field_type_freeze(sequence_type
->element_type
);
2992 const char *get_encoding_string(enum bt_ctf_string_encoding encoding
)
2994 const char *encoding_string
;
2997 case BT_CTF_STRING_ENCODING_NONE
:
2998 encoding_string
= "none";
3000 case BT_CTF_STRING_ENCODING_ASCII
:
3001 encoding_string
= "ASCII";
3003 case BT_CTF_STRING_ENCODING_UTF8
:
3004 encoding_string
= "UTF8";
3007 encoding_string
= "unknown";
3011 return encoding_string
;
3015 const char *get_integer_base_string(enum bt_ctf_integer_base base
)
3017 const char *base_string
;
3020 case BT_CTF_INTEGER_BASE_DECIMAL
:
3021 base_string
= "decimal";
3023 case BT_CTF_INTEGER_BASE_HEXADECIMAL
:
3024 base_string
= "hexadecimal";
3026 case BT_CTF_INTEGER_BASE_OCTAL
:
3027 base_string
= "octal";
3029 case BT_CTF_INTEGER_BASE_BINARY
:
3030 base_string
= "binary";
3033 base_string
= "unknown";
3041 int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type
*type
,
3042 struct metadata_context
*context
)
3044 struct bt_ctf_field_type_integer
*integer
= container_of(type
,
3045 struct bt_ctf_field_type_integer
, parent
);
3048 g_string_append_printf(context
->string
,
3049 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
3050 integer
->declaration
.len
, type
->declaration
->alignment
,
3051 (integer
->declaration
.signedness
? "true" : "false"),
3052 get_encoding_string(integer
->declaration
.encoding
),
3053 get_integer_base_string(integer
->declaration
.base
),
3054 get_byte_order_string(integer
->declaration
.byte_order
));
3055 if (integer
->mapped_clock
) {
3056 const char *clock_name
= bt_ctf_clock_class_get_name(
3057 integer
->mapped_clock
);
3064 g_string_append_printf(context
->string
,
3065 "; map = clock.%s.value", clock_name
);
3068 g_string_append(context
->string
, "; }");
3074 int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type
*type
,
3075 struct metadata_context
*context
)
3079 struct bt_ctf_field_type_enumeration
*enumeration
= container_of(type
,
3080 struct bt_ctf_field_type_enumeration
, parent
);
3081 struct bt_ctf_field_type
*container_type
;
3082 int container_signed
;
3084 container_type
= bt_ctf_field_type_enumeration_get_container_type(type
);
3085 if (!container_type
) {
3090 container_signed
= bt_ctf_field_type_integer_get_signed(container_type
);
3091 if (container_signed
< 0) {
3092 ret
= container_signed
;
3093 goto error_put_container_type
;
3096 g_string_append(context
->string
, "enum : ");
3097 ret
= bt_ctf_field_type_serialize(enumeration
->container
, context
);
3099 goto error_put_container_type
;
3102 g_string_append(context
->string
, " { ");
3103 for (entry
= 0; entry
< enumeration
->entries
->len
; entry
++) {
3104 struct enumeration_mapping
*mapping
=
3105 enumeration
->entries
->pdata
[entry
];
3107 if (container_signed
) {
3108 if (mapping
->range_start
._signed
==
3109 mapping
->range_end
._signed
) {
3110 g_string_append_printf(context
->string
,
3111 "\"%s\" = %" PRId64
,
3112 g_quark_to_string(mapping
->string
),
3113 mapping
->range_start
._signed
);
3115 g_string_append_printf(context
->string
,
3116 "\"%s\" = %" PRId64
" ... %" PRId64
,
3117 g_quark_to_string(mapping
->string
),
3118 mapping
->range_start
._signed
,
3119 mapping
->range_end
._signed
);
3122 if (mapping
->range_start
._unsigned
==
3123 mapping
->range_end
._unsigned
) {
3124 g_string_append_printf(context
->string
,
3125 "\"%s\" = %" PRIu64
,
3126 g_quark_to_string(mapping
->string
),
3127 mapping
->range_start
._unsigned
);
3129 g_string_append_printf(context
->string
,
3130 "\"%s\" = %" PRIu64
" ... %" PRIu64
,
3131 g_quark_to_string(mapping
->string
),
3132 mapping
->range_start
._unsigned
,
3133 mapping
->range_end
._unsigned
);
3137 g_string_append(context
->string
,
3138 ((entry
!= (enumeration
->entries
->len
- 1)) ?
3142 if (context
->field_name
->len
) {
3143 g_string_append_printf(context
->string
, " %s",
3144 context
->field_name
->str
);
3145 g_string_assign(context
->field_name
, "");
3147 error_put_container_type
:
3148 bt_put(container_type
);
3154 int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type
*type
,
3155 struct metadata_context
*context
)
3157 struct bt_ctf_field_type_floating_point
*floating_point
= container_of(
3158 type
, struct bt_ctf_field_type_floating_point
, parent
);
3160 g_string_append_printf(context
->string
,
3161 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
3162 floating_point
->declaration
.exp
->len
,
3163 floating_point
->declaration
.mantissa
->len
+ 1,
3164 get_byte_order_string(floating_point
->declaration
.byte_order
),
3165 type
->declaration
->alignment
);
3170 int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type
*type
,
3171 struct metadata_context
*context
)
3174 unsigned int indent
;
3176 struct bt_ctf_field_type_structure
*structure
= container_of(type
,
3177 struct bt_ctf_field_type_structure
, parent
);
3178 GString
*structure_field_name
= context
->field_name
;
3180 context
->field_name
= g_string_new("");
3182 context
->current_indentation_level
++;
3183 g_string_append(context
->string
, "struct {\n");
3185 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3186 struct structure_field
*field
;
3188 for (indent
= 0; indent
< context
->current_indentation_level
;
3190 g_string_append_c(context
->string
, '\t');
3193 field
= structure
->fields
->pdata
[i
];
3194 g_string_assign(context
->field_name
,
3195 g_quark_to_string(field
->name
));
3196 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3201 if (context
->field_name
->len
) {
3202 g_string_append_printf(context
->string
, " %s",
3203 context
->field_name
->str
);
3205 g_string_append(context
->string
, ";\n");
3208 context
->current_indentation_level
--;
3209 for (indent
= 0; indent
< context
->current_indentation_level
;
3211 g_string_append_c(context
->string
, '\t');
3214 g_string_append_printf(context
->string
, "} align(%zu)",
3215 type
->declaration
->alignment
);
3217 g_string_free(context
->field_name
, TRUE
);
3218 context
->field_name
= structure_field_name
;
3223 int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type
*type
,
3224 struct metadata_context
*context
)
3227 unsigned int indent
;
3229 struct bt_ctf_field_type_variant
*variant
= container_of(
3230 type
, struct bt_ctf_field_type_variant
, parent
);
3231 GString
*variant_field_name
= context
->field_name
;
3233 context
->field_name
= g_string_new("");
3234 if (variant
->tag_name
->len
> 0) {
3235 g_string_append_printf(context
->string
,
3236 "variant <%s> {\n", variant
->tag_name
->str
);
3238 g_string_append(context
->string
, "variant {\n");
3241 context
->current_indentation_level
++;
3242 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3243 struct structure_field
*field
= variant
->fields
->pdata
[i
];
3245 g_string_assign(context
->field_name
,
3246 g_quark_to_string(field
->name
));
3247 for (indent
= 0; indent
< context
->current_indentation_level
;
3249 g_string_append_c(context
->string
, '\t');
3252 g_string_assign(context
->field_name
,
3253 g_quark_to_string(field
->name
));
3254 ret
= bt_ctf_field_type_serialize(field
->type
, context
);
3259 if (context
->field_name
->len
) {
3260 g_string_append_printf(context
->string
, " %s;",
3261 context
->field_name
->str
);
3264 g_string_append_c(context
->string
, '\n');
3267 context
->current_indentation_level
--;
3268 for (indent
= 0; indent
< context
->current_indentation_level
;
3270 g_string_append_c(context
->string
, '\t');
3273 g_string_append(context
->string
, "}");
3275 g_string_free(context
->field_name
, TRUE
);
3276 context
->field_name
= variant_field_name
;
3281 int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type
*type
,
3282 struct metadata_context
*context
)
3285 struct bt_ctf_field_type_array
*array
= container_of(type
,
3286 struct bt_ctf_field_type_array
, parent
);
3288 ret
= bt_ctf_field_type_serialize(array
->element_type
, context
);
3293 if (context
->field_name
->len
) {
3294 g_string_append_printf(context
->string
, " %s[%u]",
3295 context
->field_name
->str
, array
->length
);
3296 g_string_assign(context
->field_name
, "");
3298 g_string_append_printf(context
->string
, "[%u]", array
->length
);
3305 int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type
*type
,
3306 struct metadata_context
*context
)
3309 struct bt_ctf_field_type_sequence
*sequence
= container_of(
3310 type
, struct bt_ctf_field_type_sequence
, parent
);
3312 ret
= bt_ctf_field_type_serialize(sequence
->element_type
, context
);
3317 if (context
->field_name
->len
) {
3318 g_string_append_printf(context
->string
, " %s[%s]",
3319 context
->field_name
->str
,
3320 sequence
->length_field_name
->str
);
3321 g_string_assign(context
->field_name
, "");
3323 g_string_append_printf(context
->string
, "[%s]",
3324 sequence
->length_field_name
->str
);
3331 int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type
*type
,
3332 struct metadata_context
*context
)
3334 struct bt_ctf_field_type_string
*string
= container_of(
3335 type
, struct bt_ctf_field_type_string
, parent
);
3337 g_string_append_printf(context
->string
,
3338 "string { encoding = %s; }",
3339 get_encoding_string(string
->declaration
.encoding
));
3344 enum bt_ctf_byte_order
get_ctf_ir_byte_order(int byte_order
) {
3345 enum bt_ctf_byte_order ret
;
3347 switch (byte_order
) {
3348 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
3350 ret
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
3352 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
3354 ret
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
3356 case BT_CTF_BYTE_ORDER_NETWORK
:
3357 ret
= BT_CTF_BYTE_ORDER_NETWORK
;
3359 case BT_CTF_BYTE_ORDER_NATIVE
:
3360 ret
= BT_CTF_BYTE_ORDER_NATIVE
;
3363 ret
= BT_CTF_BYTE_ORDER_UNKNOWN
;
3371 void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type
*type
,
3372 int byte_order
, int set_native
)
3374 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
3375 struct bt_ctf_field_type_integer
, parent
);
3378 if (integer_type
->user_byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
3380 * User byte order is native, so we can set
3381 * the real byte order.
3383 integer_type
->declaration
.byte_order
=
3387 integer_type
->user_byte_order
=
3388 get_ctf_ir_byte_order(byte_order
);
3389 integer_type
->declaration
.byte_order
= byte_order
;
3394 void bt_ctf_field_type_enumeration_set_byte_order(
3395 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3397 struct bt_ctf_field_type_enumeration
*enum_type
= container_of(type
,
3398 struct bt_ctf_field_type_enumeration
, parent
);
3400 /* Safe to assume that container is an integer */
3401 bt_ctf_field_type_integer_set_byte_order(enum_type
->container
,
3402 byte_order
, set_native
);
3406 void bt_ctf_field_type_floating_point_set_byte_order(
3407 struct bt_ctf_field_type
*type
, int byte_order
, int set_native
)
3409 struct bt_ctf_field_type_floating_point
*floating_point_type
=
3410 container_of(type
, struct bt_ctf_field_type_floating_point
,
3414 if (floating_point_type
->user_byte_order
==
3415 BT_CTF_BYTE_ORDER_NATIVE
) {
3417 * User byte order is native, so we can set
3418 * the real byte order.
3420 floating_point_type
->declaration
.byte_order
=
3422 floating_point_type
->sign
.byte_order
=
3424 floating_point_type
->mantissa
.byte_order
=
3426 floating_point_type
->exp
.byte_order
=
3430 floating_point_type
->user_byte_order
=
3431 get_ctf_ir_byte_order(byte_order
);
3432 floating_point_type
->declaration
.byte_order
= byte_order
;
3433 floating_point_type
->sign
.byte_order
= byte_order
;
3434 floating_point_type
->mantissa
.byte_order
= byte_order
;
3435 floating_point_type
->exp
.byte_order
= byte_order
;
3440 void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type
*type
,
3441 int byte_order
, int set_native
)
3444 struct bt_ctf_field_type_structure
*structure_type
=
3445 container_of(type
, struct bt_ctf_field_type_structure
,
3448 for (i
= 0; i
< structure_type
->fields
->len
; i
++) {
3449 struct structure_field
*field
= g_ptr_array_index(
3450 structure_type
->fields
, i
);
3451 struct bt_ctf_field_type
*field_type
= field
->type
;
3453 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3454 set_byte_order_funcs
[field_type
->declaration
->id
](
3455 field_type
, byte_order
, set_native
);
3461 void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type
*type
,
3462 int byte_order
, int set_native
)
3465 struct bt_ctf_field_type_variant
*variant_type
=
3466 container_of(type
, struct bt_ctf_field_type_variant
,
3469 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
3470 struct structure_field
*field
= g_ptr_array_index(
3471 variant_type
->fields
, i
);
3472 struct bt_ctf_field_type
*field_type
= field
->type
;
3474 if (set_byte_order_funcs
[field_type
->declaration
->id
]) {
3475 set_byte_order_funcs
[field_type
->declaration
->id
](
3476 field_type
, byte_order
, set_native
);
3482 void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type
*type
,
3483 int byte_order
, int set_native
)
3485 struct bt_ctf_field_type_array
*array_type
=
3486 container_of(type
, struct bt_ctf_field_type_array
,
3489 if (set_byte_order_funcs
[array_type
->element_type
->declaration
->id
]) {
3490 set_byte_order_funcs
[array_type
->element_type
->declaration
->id
](
3491 array_type
->element_type
, byte_order
, set_native
);
3496 void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type
*type
,
3497 int byte_order
, int set_native
)
3499 struct bt_ctf_field_type_sequence
*sequence_type
=
3500 container_of(type
, struct bt_ctf_field_type_sequence
,
3503 if (set_byte_order_funcs
[
3504 sequence_type
->element_type
->declaration
->id
]) {
3505 set_byte_order_funcs
[
3506 sequence_type
->element_type
->declaration
->id
](
3507 sequence_type
->element_type
, byte_order
, set_native
);
3512 struct bt_ctf_field_type
*bt_ctf_field_type_integer_copy(
3513 struct bt_ctf_field_type
*type
)
3515 struct bt_ctf_field_type
*copy
;
3516 struct bt_ctf_field_type_integer
*integer
, *copy_integer
;
3518 integer
= container_of(type
, struct bt_ctf_field_type_integer
, parent
);
3519 copy
= bt_ctf_field_type_integer_create(integer
->declaration
.len
);
3524 copy_integer
= container_of(copy
, struct bt_ctf_field_type_integer
,
3526 copy_integer
->declaration
= integer
->declaration
;
3527 if (integer
->mapped_clock
) {
3528 bt_get(integer
->mapped_clock
);
3529 copy_integer
->mapped_clock
= integer
->mapped_clock
;
3532 copy_integer
->user_byte_order
= integer
->user_byte_order
;
3539 struct bt_ctf_field_type
*bt_ctf_field_type_enumeration_copy(
3540 struct bt_ctf_field_type
*type
)
3543 struct bt_ctf_field_type
*copy
= NULL
, *copy_container
;
3544 struct bt_ctf_field_type_enumeration
*enumeration
, *copy_enumeration
;
3546 enumeration
= container_of(type
, struct bt_ctf_field_type_enumeration
,
3549 /* Copy the source enumeration's container */
3550 copy_container
= bt_ctf_field_type_copy(enumeration
->container
);
3551 if (!copy_container
) {
3555 copy
= bt_ctf_field_type_enumeration_create(copy_container
);
3559 copy_enumeration
= container_of(copy
,
3560 struct bt_ctf_field_type_enumeration
, parent
);
3562 /* Copy all enumaration entries */
3563 for (i
= 0; i
< enumeration
->entries
->len
; i
++) {
3564 struct enumeration_mapping
*mapping
= g_ptr_array_index(
3565 enumeration
->entries
, i
);
3566 struct enumeration_mapping
*copy_mapping
= g_new0(
3567 struct enumeration_mapping
, 1);
3569 if (!copy_mapping
) {
3573 *copy_mapping
= *mapping
;
3574 g_ptr_array_add(copy_enumeration
->entries
, copy_mapping
);
3577 copy_enumeration
->declaration
= enumeration
->declaration
;
3579 bt_put(copy_container
);
3582 bt_put(copy_container
);
3588 struct bt_ctf_field_type
*bt_ctf_field_type_floating_point_copy(
3589 struct bt_ctf_field_type
*type
)
3591 struct bt_ctf_field_type
*copy
;
3592 struct bt_ctf_field_type_floating_point
*floating_point
, *copy_float
;
3594 floating_point
= container_of(type
,
3595 struct bt_ctf_field_type_floating_point
, parent
);
3596 copy
= bt_ctf_field_type_floating_point_create();
3601 copy_float
= container_of(copy
,
3602 struct bt_ctf_field_type_floating_point
, parent
);
3603 copy_float
->declaration
= floating_point
->declaration
;
3604 copy_float
->sign
= floating_point
->sign
;
3605 copy_float
->mantissa
= floating_point
->mantissa
;
3606 copy_float
->exp
= floating_point
->exp
;
3607 copy_float
->user_byte_order
= floating_point
->user_byte_order
;
3608 copy_float
->declaration
.sign
= ©_float
->sign
;
3609 copy_float
->declaration
.mantissa
= ©_float
->mantissa
;
3610 copy_float
->declaration
.exp
= ©_float
->exp
;
3616 struct bt_ctf_field_type
*bt_ctf_field_type_structure_copy(
3617 struct bt_ctf_field_type
*type
)
3620 GHashTableIter iter
;
3621 gpointer key
, value
;
3622 struct bt_ctf_field_type
*copy
;
3623 struct bt_ctf_field_type_structure
*structure
, *copy_structure
;
3625 structure
= container_of(type
, struct bt_ctf_field_type_structure
,
3627 copy
= bt_ctf_field_type_structure_create();
3632 copy_structure
= container_of(copy
,
3633 struct bt_ctf_field_type_structure
, parent
);
3635 /* Copy field_name_to_index */
3636 g_hash_table_iter_init(&iter
, structure
->field_name_to_index
);
3637 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3638 g_hash_table_insert(copy_structure
->field_name_to_index
,
3642 for (i
= 0; i
< structure
->fields
->len
; i
++) {
3643 struct structure_field
*entry
, *copy_entry
;
3644 struct bt_ctf_field_type
*copy_field
;
3646 copy_entry
= g_new0(struct structure_field
, 1);
3651 entry
= g_ptr_array_index(structure
->fields
, i
);
3652 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3658 copy_entry
->name
= entry
->name
;
3659 copy_entry
->type
= copy_field
;
3660 g_ptr_array_add(copy_structure
->fields
, copy_entry
);
3663 copy_structure
->declaration
= structure
->declaration
;
3672 struct bt_ctf_field_type
*bt_ctf_field_type_variant_copy(
3673 struct bt_ctf_field_type
*type
)
3676 GHashTableIter iter
;
3677 gpointer key
, value
;
3678 struct bt_ctf_field_type
*copy
= NULL
, *copy_tag
= NULL
;
3679 struct bt_ctf_field_type_variant
*variant
, *copy_variant
;
3681 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
3684 copy_tag
= bt_ctf_field_type_copy(&variant
->tag
->parent
);
3690 copy
= bt_ctf_field_type_variant_create(copy_tag
,
3691 variant
->tag_name
->len
? variant
->tag_name
->str
: NULL
);
3696 copy_variant
= container_of(copy
, struct bt_ctf_field_type_variant
,
3699 /* Copy field_name_to_index */
3700 g_hash_table_iter_init(&iter
, variant
->field_name_to_index
);
3701 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
3702 g_hash_table_insert(copy_variant
->field_name_to_index
,
3706 for (i
= 0; i
< variant
->fields
->len
; i
++) {
3707 struct structure_field
*entry
, *copy_entry
;
3708 struct bt_ctf_field_type
*copy_field
;
3710 copy_entry
= g_new0(struct structure_field
, 1);
3715 entry
= g_ptr_array_index(variant
->fields
, i
);
3716 copy_field
= bt_ctf_field_type_copy(entry
->type
);
3722 copy_entry
->name
= entry
->name
;
3723 copy_entry
->type
= copy_field
;
3724 g_ptr_array_add(copy_variant
->fields
, copy_entry
);
3727 copy_variant
->declaration
= variant
->declaration
;
3728 if (variant
->tag_field_path
) {
3729 copy_variant
->tag_field_path
= bt_ctf_field_path_copy(
3730 variant
->tag_field_path
);
3731 if (!copy_variant
->tag_field_path
) {
3745 struct bt_ctf_field_type
*bt_ctf_field_type_array_copy(
3746 struct bt_ctf_field_type
*type
)
3748 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3749 struct bt_ctf_field_type_array
*array
, *copy_array
;
3751 array
= container_of(type
, struct bt_ctf_field_type_array
,
3753 copy_element
= bt_ctf_field_type_copy(array
->element_type
);
3754 if (!copy_element
) {
3758 copy
= bt_ctf_field_type_array_create(copy_element
, array
->length
);
3763 copy_array
= container_of(copy
, struct bt_ctf_field_type_array
,
3765 copy_array
->declaration
= array
->declaration
;
3767 bt_put(copy_element
);
3772 struct bt_ctf_field_type
*bt_ctf_field_type_sequence_copy(
3773 struct bt_ctf_field_type
*type
)
3775 struct bt_ctf_field_type
*copy
= NULL
, *copy_element
;
3776 struct bt_ctf_field_type_sequence
*sequence
, *copy_sequence
;
3778 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
3780 copy_element
= bt_ctf_field_type_copy(sequence
->element_type
);
3781 if (!copy_element
) {
3785 copy
= bt_ctf_field_type_sequence_create(copy_element
,
3786 sequence
->length_field_name
->len
?
3787 sequence
->length_field_name
->str
: NULL
);
3792 copy_sequence
= container_of(copy
, struct bt_ctf_field_type_sequence
,
3794 copy_sequence
->declaration
= sequence
->declaration
;
3795 if (sequence
->length_field_path
) {
3796 copy_sequence
->length_field_path
= bt_ctf_field_path_copy(
3797 sequence
->length_field_path
);
3798 if (!copy_sequence
->length_field_path
) {
3803 bt_put(copy_element
);
3811 struct bt_ctf_field_type
*bt_ctf_field_type_string_copy(
3812 struct bt_ctf_field_type
*type
)
3814 struct bt_ctf_field_type
*copy
;
3815 struct bt_ctf_field_type_string
*string
, *copy_string
;
3817 copy
= bt_ctf_field_type_string_create();
3822 string
= container_of(type
, struct bt_ctf_field_type_string
,
3824 copy_string
= container_of(type
, struct bt_ctf_field_type_string
,
3826 copy_string
->declaration
= string
->declaration
;
3832 int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type
*type_a
,
3833 struct bt_ctf_field_type
*type_b
)
3836 struct bt_ctf_field_type_integer
*integer_a
;
3837 struct bt_ctf_field_type_integer
*integer_b
;
3838 struct declaration_integer
*decl_a
;
3839 struct declaration_integer
*decl_b
;
3841 integer_a
= container_of(type_a
, struct bt_ctf_field_type_integer
,
3843 integer_b
= container_of(type_b
, struct bt_ctf_field_type_integer
,
3845 decl_a
= &integer_a
->declaration
;
3846 decl_b
= &integer_b
->declaration
;
3849 if (decl_a
->len
!= decl_b
->len
) {
3854 * Compare user byte orders only, not the cached,
3857 if (integer_a
->user_byte_order
!= integer_b
->user_byte_order
) {
3862 if (decl_a
->signedness
!= decl_b
->signedness
) {
3867 if (decl_a
->base
!= decl_b
->base
) {
3872 if (decl_a
->encoding
!= decl_b
->encoding
) {
3877 if (integer_a
->mapped_clock
!= integer_b
->mapped_clock
) {
3889 int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type
*type_a
,
3890 struct bt_ctf_field_type
*type_b
)
3893 struct bt_ctf_field_type_floating_point
*float_a
;
3894 struct bt_ctf_field_type_floating_point
*float_b
;
3896 float_a
= container_of(type_a
,
3897 struct bt_ctf_field_type_floating_point
, parent
);
3898 float_b
= container_of(type_b
,
3899 struct bt_ctf_field_type_floating_point
, parent
);
3902 if (float_a
->sign
.len
!= float_b
->sign
.len
) {
3906 /* Exponent length */
3907 if (float_a
->exp
.len
!= float_b
->exp
.len
) {
3911 /* Mantissa length */
3912 if (float_a
->mantissa
.len
!= float_b
->mantissa
.len
) {
3917 * Compare user byte orders only, not the cached,
3920 if (float_a
->user_byte_order
!= float_b
->user_byte_order
) {
3932 int compare_enumeration_mappings(struct enumeration_mapping
*mapping_a
,
3933 struct enumeration_mapping
*mapping_b
)
3938 if (mapping_a
->string
!= mapping_b
->string
) {
3943 if (mapping_a
->range_start
._unsigned
!=
3944 mapping_b
->range_start
._unsigned
) {
3949 if (mapping_a
->range_end
._unsigned
!=
3950 mapping_b
->range_end
._unsigned
) {
3962 int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type
*type_a
,
3963 struct bt_ctf_field_type
*type_b
)
3967 struct bt_ctf_field_type_enumeration
*enum_a
;
3968 struct bt_ctf_field_type_enumeration
*enum_b
;
3970 enum_a
= container_of(type_a
,
3971 struct bt_ctf_field_type_enumeration
, parent
);
3972 enum_b
= container_of(type_b
,
3973 struct bt_ctf_field_type_enumeration
, parent
);
3975 /* Container field type */
3976 ret
= bt_ctf_field_type_compare(enum_a
->container
, enum_b
->container
);
3984 if (enum_a
->entries
->len
!= enum_b
->entries
->len
) {
3988 for (i
= 0; i
< enum_a
->entries
->len
; ++i
) {
3989 struct enumeration_mapping
*mapping_a
=
3990 g_ptr_array_index(enum_a
->entries
, i
);
3991 struct enumeration_mapping
*mapping_b
=
3992 g_ptr_array_index(enum_b
->entries
, i
);
3994 if (compare_enumeration_mappings(mapping_a
, mapping_b
)) {
4007 int bt_ctf_field_type_string_compare(struct bt_ctf_field_type
*type_a
,
4008 struct bt_ctf_field_type
*type_b
)
4011 struct bt_ctf_field_type_string
*string_a
;
4012 struct bt_ctf_field_type_string
*string_b
;
4014 string_a
= container_of(type_a
,
4015 struct bt_ctf_field_type_string
, parent
);
4016 string_b
= container_of(type_b
,
4017 struct bt_ctf_field_type_string
, parent
);
4020 if (string_a
->declaration
.encoding
!= string_b
->declaration
.encoding
) {
4032 int compare_structure_fields(struct structure_field
*field_a
,
4033 struct structure_field
*field_b
)
4038 if (field_a
->name
!= field_b
->name
) {
4043 ret
= bt_ctf_field_type_compare(field_a
->type
, field_b
->type
);
4050 int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type
*type_a
,
4051 struct bt_ctf_field_type
*type_b
)
4055 struct bt_ctf_field_type_structure
*struct_a
;
4056 struct bt_ctf_field_type_structure
*struct_b
;
4058 struct_a
= container_of(type_a
,
4059 struct bt_ctf_field_type_structure
, parent
);
4060 struct_b
= container_of(type_b
,
4061 struct bt_ctf_field_type_structure
, parent
);
4064 if (bt_ctf_field_type_get_alignment(type_a
) !=
4065 bt_ctf_field_type_get_alignment(type_b
)) {
4070 if (struct_a
->fields
->len
!= struct_b
->fields
->len
) {
4074 for (i
= 0; i
< struct_a
->fields
->len
; ++i
) {
4075 struct structure_field
*field_a
=
4076 g_ptr_array_index(struct_a
->fields
, i
);
4077 struct structure_field
*field_b
=
4078 g_ptr_array_index(struct_b
->fields
, i
);
4080 ret
= compare_structure_fields(field_a
, field_b
);
4094 int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type
*type_a
,
4095 struct bt_ctf_field_type
*type_b
)
4099 struct bt_ctf_field_type_variant
*variant_a
;
4100 struct bt_ctf_field_type_variant
*variant_b
;
4102 variant_a
= container_of(type_a
,
4103 struct bt_ctf_field_type_variant
, parent
);
4104 variant_b
= container_of(type_b
,
4105 struct bt_ctf_field_type_variant
, parent
);
4108 if (strcmp(variant_a
->tag_name
->str
, variant_b
->tag_name
->str
)) {
4113 ret
= bt_ctf_field_type_compare(
4114 (struct bt_ctf_field_type
*) variant_a
->tag
,
4115 (struct bt_ctf_field_type
*) variant_b
->tag
);
4123 if (variant_a
->fields
->len
!= variant_b
->fields
->len
) {
4127 for (i
= 0; i
< variant_a
->fields
->len
; ++i
) {
4128 struct structure_field
*field_a
=
4129 g_ptr_array_index(variant_a
->fields
, i
);
4130 struct structure_field
*field_b
=
4131 g_ptr_array_index(variant_b
->fields
, i
);
4133 ret
= compare_structure_fields(field_a
, field_b
);
4147 int bt_ctf_field_type_array_compare(struct bt_ctf_field_type
*type_a
,
4148 struct bt_ctf_field_type
*type_b
)
4151 struct bt_ctf_field_type_array
*array_a
;
4152 struct bt_ctf_field_type_array
*array_b
;
4154 array_a
= container_of(type_a
,
4155 struct bt_ctf_field_type_array
, parent
);
4156 array_b
= container_of(type_b
,
4157 struct bt_ctf_field_type_array
, parent
);
4160 if (array_a
->length
!= array_b
->length
) {
4165 ret
= bt_ctf_field_type_compare(array_a
->element_type
,
4166 array_b
->element_type
);
4173 int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type
*type_a
,
4174 struct bt_ctf_field_type
*type_b
)
4177 struct bt_ctf_field_type_sequence
*sequence_a
;
4178 struct bt_ctf_field_type_sequence
*sequence_b
;
4180 sequence_a
= container_of(type_a
,
4181 struct bt_ctf_field_type_sequence
, parent
);
4182 sequence_b
= container_of(type_b
,
4183 struct bt_ctf_field_type_sequence
, parent
);
4186 if (strcmp(sequence_a
->length_field_name
->str
,
4187 sequence_b
->length_field_name
->str
)) {
4192 ret
= bt_ctf_field_type_compare(sequence_a
->element_type
,
4193 sequence_b
->element_type
);
4199 int bt_ctf_field_type_compare(struct bt_ctf_field_type
*type_a
,
4200 struct bt_ctf_field_type
*type_b
)
4204 if (type_a
== type_b
) {
4205 /* Same reference: equal (even if both are NULL) */
4210 if (!type_a
|| !type_b
) {
4215 if (type_a
->declaration
->id
!= type_b
->declaration
->id
) {
4216 /* Different type IDs */
4220 if (type_a
->declaration
->id
== BT_CTF_TYPE_ID_UNKNOWN
) {
4221 /* Both have unknown type IDs */
4225 ret
= type_compare_funcs
[type_a
->declaration
->id
](type_a
, type_b
);
4232 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type
*field_type
)
4234 int field_count
= -1;
4235 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4238 case CTF_TYPE_STRUCT
:
4240 bt_ctf_field_type_structure_get_field_count(field_type
);
4242 case CTF_TYPE_VARIANT
:
4244 bt_ctf_field_type_variant_get_field_count(field_type
);
4246 case CTF_TYPE_ARRAY
:
4247 case CTF_TYPE_SEQUENCE
:
4249 * Array and sequence types always contain a single member
4250 * (the element type).
4262 struct bt_ctf_field_type
*bt_ctf_field_type_get_field_at_index(
4263 struct bt_ctf_field_type
*field_type
, int index
)
4265 struct bt_ctf_field_type
*field
= NULL
;
4266 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4269 case CTF_TYPE_STRUCT
:
4270 bt_ctf_field_type_structure_get_field(field_type
, NULL
, &field
,
4273 case CTF_TYPE_VARIANT
:
4275 int ret
= bt_ctf_field_type_variant_get_field(field_type
, NULL
,
4283 case CTF_TYPE_ARRAY
:
4284 field
= bt_ctf_field_type_array_get_element_type(field_type
);
4286 case CTF_TYPE_SEQUENCE
:
4287 field
= bt_ctf_field_type_sequence_get_element_type(field_type
);
4297 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type
*field_type
,
4300 int field_index
= -1;
4301 enum ctf_type_id type_id
= bt_ctf_field_type_get_type_id(field_type
);
4304 case CTF_TYPE_STRUCT
:
4305 field_index
= bt_ctf_field_type_structure_get_field_name_index(
4308 case CTF_TYPE_VARIANT
:
4309 field_index
= bt_ctf_field_type_variant_get_field_name_index(
4319 struct bt_ctf_field_path
*bt_ctf_field_type_variant_get_tag_field_path(
4320 struct bt_ctf_field_type
*type
)
4322 struct bt_ctf_field_path
*field_path
= NULL
;
4323 struct bt_ctf_field_type_variant
*variant
;
4325 if (!type
|| !bt_ctf_field_type_is_variant(type
)) {
4329 variant
= container_of(type
, struct bt_ctf_field_type_variant
,
4331 field_path
= bt_get(variant
->tag_field_path
);
4336 struct bt_ctf_field_path
*bt_ctf_field_type_sequence_get_length_field_path(
4337 struct bt_ctf_field_type
*type
)
4339 struct bt_ctf_field_path
*field_path
= NULL
;
4340 struct bt_ctf_field_type_sequence
*sequence
;
4342 if (!type
|| !bt_ctf_field_type_is_sequence(type
)) {
4346 sequence
= container_of(type
, struct bt_ctf_field_type_sequence
,
4348 field_path
= bt_get(sequence
->length_field_path
);