1 #ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
5 * BabelTrace - CTF IR: Event field types internal
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/clock-class.h>
32 #include <babeltrace/ctf-ir/field-types.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/types.h>
39 #define BT_ASSERT_PRE_FT_COMMON_HAS_ID(_ft, _type_id, _name) \
40 BT_ASSERT_PRE(((struct bt_field_type_common *) (_ft))->id == (_type_id), \
41 _name " has the wrong type ID: expected-type-id=%s, " \
42 "%![ft-]+_F", bt_common_field_type_id_string(_type_id), (_ft))
44 #define BT_ASSERT_PRE_FT_HOT(_ft, _name) \
45 BT_ASSERT_PRE_HOT((_ft), (_name), ": +%!+_F", (_ft))
47 #define BT_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(_ft, _index) \
48 (&g_array_index(((struct bt_field_type_common_structure *) (_ft))->fields, \
49 struct bt_field_type_common_structure_field, (_index)))
51 #define BT_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(_ft, _index) \
52 (&g_array_index(((struct bt_field_type_common_variant *) (_ft))->choices, \
53 struct bt_field_type_common_variant_choice, (_index)))
55 struct bt_field_common
;
56 struct bt_field_type_common
;
59 typedef void (*bt_field_type_common_method_freeze
)(
60 struct bt_field_type_common
*);
61 typedef int (*bt_field_type_common_method_validate
)(
62 struct bt_field_type_common
*);
63 typedef void (*bt_field_type_common_method_set_byte_order
)(
64 struct bt_field_type_common
*, enum bt_byte_order
);
65 typedef struct bt_field_type_common
*(*bt_field_type_common_method_copy
)(
66 struct bt_field_type_common
*);
67 typedef int (*bt_field_type_common_method_compare
)(
68 struct bt_field_type_common
*,
69 struct bt_field_type_common
*);
71 struct bt_field_type_common_methods
{
72 bt_field_type_common_method_freeze freeze
;
73 bt_field_type_common_method_validate validate
;
74 bt_field_type_common_method_set_byte_order set_byte_order
;
75 bt_field_type_common_method_copy copy
;
76 bt_field_type_common_method_compare compare
;
79 struct bt_field_type_common
{
80 struct bt_object base
;
81 enum bt_field_type_id id
;
82 unsigned int alignment
;
85 struct bt_field_type_common_methods
*methods
;
88 * A type can't be modified once it is added to an event or after a
89 * a field has been instanciated from it.
94 * This flag indicates if the field type is valid. A valid
95 * field type is _always_ frozen. All the nested field types of
96 * a valid field type are also valid (and thus frozen).
101 * Specialized data for either CTF IR or CTF writer APIs.
102 * Having this here ensures that:
104 * * The type-specific common data is always found at the same
105 * offset when the common API has a `struct
106 * bt_field_type_common *` so that you can cast it to `struct
107 * bt_field_type_common_integer *` for example and access the
108 * common integer field type fields.
110 * * The specific CTF IR and CTF writer APIs can access their
111 * specific field type fields in this union at an offset known
112 * at build time. This avoids a pointer to specific data so
113 * that all the fields, common or specific, of a CTF IR
114 * integer field type or of a CTF writer integer field type,
115 * for example, are contained within the same contiguous block
122 void *serialize_func
;
127 struct bt_field_type_common_integer
{
128 struct bt_field_type_common common
;
131 struct bt_clock_class
*mapped_clock_class
;
133 enum bt_byte_order user_byte_order
;
136 enum bt_integer_base base
;
137 enum bt_string_encoding encoding
;
140 struct enumeration_mapping
{
152 struct bt_field_type_common_enumeration
{
153 struct bt_field_type_common common
;
156 struct bt_field_type_common_integer
*container_ft
;
158 /* Array of `struct enumeration_mapping *`, owned by this */
161 /* Only set during validation */
162 bt_bool has_overlapping_ranges
;
165 enum bt_field_type_enumeration_mapping_iterator_type
{
167 ITERATOR_BY_SIGNED_VALUE
,
168 ITERATOR_BY_UNSIGNED_VALUE
,
171 struct bt_field_type_enumeration_mapping_iterator
{
172 struct bt_object base
;
175 struct bt_field_type_common_enumeration
*enumeration_ft
;
177 enum bt_field_type_enumeration_mapping_iterator_type type
;
181 int64_t signed_value
;
182 uint64_t unsigned_value
;
186 struct bt_field_type_common_floating_point
{
187 struct bt_field_type_common common
;
188 enum bt_byte_order user_byte_order
;
189 unsigned int exp_dig
;
190 unsigned int mant_dig
;
193 struct bt_field_type_common_structure_field
{
197 struct bt_field_type_common
*type
;
200 struct bt_field_type_common_structure
{
201 struct bt_field_type_common common
;
202 GHashTable
*field_name_to_index
;
205 * Array of `struct bt_field_type_common_structure_field`,
211 struct bt_field_type_common_variant_choice_range
{
222 struct bt_field_type_common_variant_choice
{
226 struct bt_field_type_common
*type
;
228 /* Array of `struct bt_field_type_common_variant_choice_range` */
232 struct bt_field_type_common_variant
{
233 struct bt_field_type_common common
;
235 bool choices_up_to_date
;
238 struct bt_field_type_common_enumeration
*tag_ft
;
241 struct bt_field_path
*tag_field_path
;
243 GHashTable
*choice_name_to_index
;
246 * Array of `struct bt_field_type_common_variant_choice`,
251 struct bt_field_type_common_array
{
252 struct bt_field_type_common common
;
255 struct bt_field_type_common
*element_ft
;
260 struct bt_field_type_common_sequence
{
261 struct bt_field_type_common common
;
264 struct bt_field_type_common
*element_ft
;
266 GString
*length_field_name
;
269 struct bt_field_path
*length_field_path
;
272 struct bt_field_type_common_string
{
273 struct bt_field_type_common common
;
274 enum bt_string_encoding encoding
;
278 # define bt_field_type_freeze _bt_field_type_freeze
279 # define bt_field_type_common_freeze _bt_field_type_common_freeze
281 # define bt_field_type_freeze(_ft)
282 # define bt_field_type_common_freeze(_ft)
285 typedef struct bt_field_common
*(* bt_field_common_create_func
)(
286 struct bt_field_type_common
*);
289 void bt_field_type_common_initialize(struct bt_field_type_common
*ft
,
290 bool init_bo
, bt_object_release_func release_func
,
291 struct bt_field_type_common_methods
*methods
);
294 void bt_field_type_common_integer_initialize(
295 struct bt_field_type_common
*ft
,
296 unsigned int size
, bt_object_release_func release_func
,
297 struct bt_field_type_common_methods
*methods
);
300 void bt_field_type_common_floating_point_initialize(
301 struct bt_field_type_common
*ft
,
302 bt_object_release_func release_func
,
303 struct bt_field_type_common_methods
*methods
);
306 void bt_field_type_common_enumeration_initialize(
307 struct bt_field_type_common
*ft
,
308 struct bt_field_type_common
*container_ft
,
309 bt_object_release_func release_func
,
310 struct bt_field_type_common_methods
*methods
);
313 void bt_field_type_common_string_initialize(
314 struct bt_field_type_common
*ft
,
315 bt_object_release_func release_func
,
316 struct bt_field_type_common_methods
*methods
);
319 void bt_field_type_common_structure_initialize(
320 struct bt_field_type_common
*ft
,
321 bt_object_release_func release_func
,
322 struct bt_field_type_common_methods
*methods
);
325 void bt_field_type_common_array_initialize(
326 struct bt_field_type_common
*ft
,
327 struct bt_field_type_common
*element_ft
,
328 unsigned int length
, bt_object_release_func release_func
,
329 struct bt_field_type_common_methods
*methods
);
332 void bt_field_type_common_sequence_initialize(
333 struct bt_field_type_common
*ft
,
334 struct bt_field_type_common
*element_ft
,
335 const char *length_field_name
,
336 bt_object_release_func release_func
,
337 struct bt_field_type_common_methods
*methods
);
340 void bt_field_type_common_variant_initialize(
341 struct bt_field_type_common
*ft
,
342 struct bt_field_type_common
*tag_ft
,
343 const char *tag_name
,
344 bt_object_release_func release_func
,
345 struct bt_field_type_common_methods
*methods
);
348 void bt_field_type_common_integer_destroy(struct bt_object
*obj
);
351 void bt_field_type_common_floating_point_destroy(struct bt_object
*obj
);
354 void bt_field_type_common_enumeration_destroy_recursive(struct bt_object
*obj
);
357 void bt_field_type_common_string_destroy(struct bt_object
*obj
);
360 void bt_field_type_common_structure_destroy_recursive(struct bt_object
*obj
);
363 void bt_field_type_common_array_destroy_recursive(struct bt_object
*obj
);
366 void bt_field_type_common_sequence_destroy_recursive(struct bt_object
*obj
);
369 void bt_field_type_common_variant_destroy_recursive(struct bt_object
*obj
);
372 int bt_field_type_common_integer_validate(struct bt_field_type_common
*ft
);
375 int bt_field_type_common_enumeration_validate_recursive(
376 struct bt_field_type_common
*ft
);
379 int bt_field_type_common_sequence_validate_recursive(
380 struct bt_field_type_common
*ft
);
383 int bt_field_type_common_array_validate_recursive(
384 struct bt_field_type_common
*ft
);
387 int bt_field_type_common_structure_validate_recursive(
388 struct bt_field_type_common
*ft
);
391 int bt_field_type_common_variant_validate_recursive(
392 struct bt_field_type_common
*type
);
395 int bt_field_type_common_validate(struct bt_field_type_common
*ft
);
398 int bt_field_type_common_integer_get_size(struct bt_field_type_common
*ft
);
401 bt_bool
bt_field_type_common_integer_is_signed(struct bt_field_type_common
*ft
);
404 int bt_field_type_common_integer_set_is_signed(struct bt_field_type_common
*ft
,
408 int bt_field_type_common_integer_set_size(struct bt_field_type_common
*ft
,
412 enum bt_integer_base
bt_field_type_common_integer_get_base(
413 struct bt_field_type_common
*ft
);
416 int bt_field_type_common_integer_set_base(struct bt_field_type_common
*ft
,
417 enum bt_integer_base base
);
420 enum bt_string_encoding
bt_field_type_common_integer_get_encoding(
421 struct bt_field_type_common
*ft
);
424 int bt_field_type_common_integer_set_encoding(struct bt_field_type_common
*ft
,
425 enum bt_string_encoding encoding
);
428 struct bt_clock_class
*bt_field_type_common_integer_borrow_mapped_clock_class(
429 struct bt_field_type_common
*ft
);
432 int bt_field_type_common_integer_set_mapped_clock_class_no_check_frozen(
433 struct bt_field_type_common
*ft
,
434 struct bt_clock_class
*clock_class
);
437 int bt_field_type_common_integer_set_mapped_clock_class(
438 struct bt_field_type_common
*ft
,
439 struct bt_clock_class
*clock_class
);
442 struct bt_field_type_enumeration_mapping_iterator
*
443 bt_field_type_common_enumeration_find_mappings_by_name(
444 struct bt_field_type_common
*ft
, const char *name
);
447 struct bt_field_type_enumeration_mapping_iterator
*
448 bt_field_type_common_enumeration_signed_find_mappings_by_value(
449 struct bt_field_type_common
*ft
, int64_t value
);
452 struct bt_field_type_enumeration_mapping_iterator
*
453 bt_field_type_common_enumeration_unsigned_find_mappings_by_value(
454 struct bt_field_type_common
*ft
, uint64_t value
);
457 int bt_field_type_common_enumeration_signed_get_mapping_by_index(
458 struct bt_field_type_common
*ft
, uint64_t index
,
459 const char **mapping_name
, int64_t *range_begin
,
463 int bt_field_type_common_enumeration_unsigned_get_mapping_by_index(
464 struct bt_field_type_common
*ft
, uint64_t index
,
465 const char **mapping_name
, uint64_t *range_begin
,
466 uint64_t *range_end
);
469 struct bt_field_type_common
*
470 bt_field_type_common_enumeration_borrow_container_field_type(
471 struct bt_field_type_common
*ft
);
474 int bt_field_type_common_enumeration_signed_add_mapping(
475 struct bt_field_type_common
*ft
, const char *string
,
476 int64_t range_start
, int64_t range_end
);
479 int bt_field_type_common_enumeration_unsigned_add_mapping(
480 struct bt_field_type_common
*ft
, const char *string
,
481 uint64_t range_start
, uint64_t range_end
);
484 int64_t bt_field_type_common_enumeration_get_mapping_count(
485 struct bt_field_type_common
*ft
);
488 int bt_field_type_common_floating_point_get_exponent_digits(
489 struct bt_field_type_common
*ft
);
492 int bt_field_type_common_floating_point_set_exponent_digits(
493 struct bt_field_type_common
*ft
,
494 unsigned int exponent_digits
);
497 int bt_field_type_common_floating_point_get_mantissa_digits(
498 struct bt_field_type_common
*type
);
501 int bt_field_type_common_floating_point_set_mantissa_digits(
502 struct bt_field_type_common
*ft
, unsigned int mantissa_digits
);
505 int bt_field_type_common_structure_replace_field(
506 struct bt_field_type_common
*ft
,
507 const char *field_name
,
508 struct bt_field_type_common
*field_type
);
511 int bt_field_type_common_structure_add_field(struct bt_field_type_common
*ft
,
512 struct bt_field_type_common
*field_type
,
513 const char *field_name
);
516 int64_t bt_field_type_common_structure_get_field_count(
517 struct bt_field_type_common
*ft
);
520 int bt_field_type_common_structure_borrow_field_by_index(
521 struct bt_field_type_common
*ft
,
522 const char **field_name
,
523 struct bt_field_type_common
**field_type
, uint64_t index
);
526 struct bt_field_type_common
*
527 bt_field_type_common_structure_borrow_field_type_by_name(
528 struct bt_field_type_common
*ft
, const char *name
);
531 struct bt_field_type_common
*
532 bt_field_type_common_variant_borrow_tag_field_type(
533 struct bt_field_type_common
*ft
);
536 const char *bt_field_type_common_variant_get_tag_name(
537 struct bt_field_type_common
*ft
);
540 int bt_field_type_common_variant_set_tag_name(
541 struct bt_field_type_common
*ft
, const char *name
);
544 int bt_field_type_common_variant_add_field(struct bt_field_type_common
*ft
,
545 struct bt_field_type_common
*field_type
,
546 const char *field_name
);
549 int bt_field_type_common_variant_update_choices(
550 struct bt_field_type_common
*ft
);
553 struct bt_field_type_common
*
554 bt_field_type_common_variant_borrow_field_type_by_name(
555 struct bt_field_type_common
*ft
,
556 const char *field_name
);
559 int64_t bt_field_type_common_variant_get_field_count(
560 struct bt_field_type_common
*ft
);
563 int bt_field_type_common_variant_borrow_field_by_index(
564 struct bt_field_type_common
*ft
,
565 const char **field_name
,
566 struct bt_field_type_common
**field_type
, uint64_t index
);
569 struct bt_field_type_common
*
570 bt_field_type_common_array_borrow_element_field_type(
571 struct bt_field_type_common
*ft
);
574 int bt_field_type_common_array_set_element_field_type(
575 struct bt_field_type_common
*ft
,
576 struct bt_field_type_common
*element_ft
);
579 int64_t bt_field_type_common_array_get_length(struct bt_field_type_common
*ft
);
582 struct bt_field_type_common
*
583 bt_field_type_common_sequence_borrow_element_field_type(
584 struct bt_field_type_common
*ft
);
587 int bt_field_type_common_sequence_set_element_field_type(
588 struct bt_field_type_common
*ft
,
589 struct bt_field_type_common
*element_ft
);
592 const char *bt_field_type_common_sequence_get_length_field_name(
593 struct bt_field_type_common
*ft
);
596 enum bt_string_encoding
bt_field_type_common_string_get_encoding(
597 struct bt_field_type_common
*ft
);
600 int bt_field_type_common_string_set_encoding(struct bt_field_type_common
*ft
,
601 enum bt_string_encoding encoding
);
604 int bt_field_type_common_get_alignment(struct bt_field_type_common
*type
);
607 int bt_field_type_common_set_alignment(struct bt_field_type_common
*ft
,
608 unsigned int alignment
);
611 enum bt_byte_order
bt_field_type_common_get_byte_order(
612 struct bt_field_type_common
*ft
);
615 int bt_field_type_common_set_byte_order(struct bt_field_type_common
*ft
,
616 enum bt_byte_order byte_order
);
619 enum bt_field_type_id
bt_field_type_common_get_type_id(
620 struct bt_field_type_common
*ft
);
623 void _bt_field_type_common_freeze(struct bt_field_type_common
*ft
);
626 void _bt_field_type_freeze(struct bt_field_type
*ft
);
629 struct bt_field_type_common
*
630 bt_field_type_common_variant_borrow_field_type_signed(
631 struct bt_field_type_common_variant
*var_ft
,
635 struct bt_field_type_common
*
636 bt_field_type_common_variant_borrow_field_type_unsigned(
637 struct bt_field_type_common_variant
*var_ft
,
641 struct bt_field_type_common
*bt_field_type_common_copy(
642 struct bt_field_type_common
*ft
);
645 int bt_field_type_common_structure_get_field_name_index(
646 struct bt_field_type_common
*ft
, const char *name
);
649 int bt_field_type_common_variant_get_field_name_index(
650 struct bt_field_type_common
*ft
, const char *name
);
653 int bt_field_type_common_sequence_set_length_field_path(
654 struct bt_field_type_common
*ft
, struct bt_field_path
*path
);
657 int bt_field_type_common_variant_set_tag_field_path(
658 struct bt_field_type_common
*ft
,
659 struct bt_field_path
*path
);
662 int bt_field_type_common_variant_set_tag_field_type(
663 struct bt_field_type_common
*ft
,
664 struct bt_field_type_common
*tag_ft
);
667 void bt_field_type_common_generic_freeze(struct bt_field_type_common
*ft
);
670 void bt_field_type_common_enumeration_freeze_recursive(
671 struct bt_field_type_common
*ft
);
674 void bt_field_type_common_structure_freeze_recursive(
675 struct bt_field_type_common
*ft
);
678 void bt_field_type_common_variant_freeze_recursive(
679 struct bt_field_type_common
*ft
);
682 void bt_field_type_common_array_freeze_recursive(
683 struct bt_field_type_common
*ft
);
686 void bt_field_type_common_sequence_freeze_recursive(
687 struct bt_field_type_common
*type
);
690 void bt_field_type_common_integer_set_byte_order(
691 struct bt_field_type_common
*ft
, enum bt_byte_order byte_order
);
694 void bt_field_type_common_enumeration_set_byte_order_recursive(
695 struct bt_field_type_common
*ft
, enum bt_byte_order byte_order
);
698 void bt_field_type_common_floating_point_set_byte_order(
699 struct bt_field_type_common
*ft
, enum bt_byte_order byte_order
);
702 void bt_field_type_common_structure_set_byte_order_recursive(
703 struct bt_field_type_common
*ft
,
704 enum bt_byte_order byte_order
);
707 void bt_field_type_common_variant_set_byte_order_recursive(
708 struct bt_field_type_common
*ft
,
709 enum bt_byte_order byte_order
);
712 void bt_field_type_common_array_set_byte_order_recursive(
713 struct bt_field_type_common
*ft
,
714 enum bt_byte_order byte_order
);
717 void bt_field_type_common_sequence_set_byte_order_recursive(
718 struct bt_field_type_common
*ft
,
719 enum bt_byte_order byte_order
);
722 int bt_field_type_common_integer_compare(struct bt_field_type_common
*ft_a
,
723 struct bt_field_type_common
*ft_b
);
726 int bt_field_type_common_floating_point_compare(
727 struct bt_field_type_common
*ft_a
,
728 struct bt_field_type_common
*ft_b
);
731 int bt_field_type_common_enumeration_compare_recursive(
732 struct bt_field_type_common
*ft_a
,
733 struct bt_field_type_common
*ft_b
);
736 int bt_field_type_common_string_compare(struct bt_field_type_common
*ft_a
,
737 struct bt_field_type_common
*ft_b
);
740 int bt_field_type_common_structure_compare_recursive(
741 struct bt_field_type_common
*ft_a
,
742 struct bt_field_type_common
*ft_b
);
745 int bt_field_type_common_variant_compare_recursive(
746 struct bt_field_type_common
*ft_a
,
747 struct bt_field_type_common
*ft_b
);
750 int bt_field_type_common_array_compare_recursive(
751 struct bt_field_type_common
*ft_a
,
752 struct bt_field_type_common
*ft_b
);
755 int bt_field_type_common_sequence_compare_recursive(
756 struct bt_field_type_common
*ft_a
,
757 struct bt_field_type_common
*ft_b
);
760 int bt_field_type_common_compare(struct bt_field_type_common
*ft_a
,
761 struct bt_field_type_common
*ft_b
);
764 int64_t bt_field_type_common_get_field_count(struct bt_field_type_common
*ft
);
767 struct bt_field_type_common
*bt_field_type_common_borrow_field_at_index(
768 struct bt_field_type_common
*ft
, int index
);
771 int bt_field_type_common_get_field_index(struct bt_field_type_common
*ft
,
775 struct bt_field_path
*bt_field_type_common_variant_borrow_tag_field_path(
776 struct bt_field_type_common
*ft
);
779 struct bt_field_path
*bt_field_type_common_sequence_borrow_length_field_path(
780 struct bt_field_type_common
*ft
);
783 int bt_field_type_common_validate_single_clock_class(
784 struct bt_field_type_common
*ft
,
785 struct bt_clock_class
**expected_clock_class
);
788 int64_t bt_field_type_common_variant_find_choice_index(
789 struct bt_field_type_common
*ft
, uint64_t uval
,
792 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */