* SOFTWARE.
*/
-#include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-writer/writer.h>
-#include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/ctf-ir/clock-class.h>
+#include <babeltrace/ctf-ir/field-types.h>
#include <babeltrace/babeltrace-internal.h>
#include <babeltrace/object-internal.h>
#include <babeltrace/types.h>
-#include <babeltrace/ctf/events.h>
+#include <stdint.h>
#include <glib.h>
-typedef void (*type_freeze_func)(struct bt_ctf_field_type *);
-typedef int (*type_serialize_func)(struct bt_ctf_field_type *,
- struct metadata_context *);
-
-struct bt_ctf_field_type {
+#define BT_ASSERT_PRE_FT_IS_INT(_ft, _name) \
+ BT_ASSERT_PRE( \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
+ _name " is not an integer field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_UNSIGNED_INT(_ft, _name) \
+ BT_ASSERT_PRE( \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, \
+ _name " is not an unsigned integer field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_ENUM(_ft, _name) \
+ BT_ASSERT_PRE( \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
+ _name " is not an enumeration field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_ARRAY(_ft, _name) \
+ BT_ASSERT_PRE( \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_STATIC_ARRAY || \
+ ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_DYNAMIC_ARRAY, \
+ _name " is not an array field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_HAS_ID(_ft, _id, _name) \
+ BT_ASSERT_PRE(((struct bt_field_type *) (_ft))->id == (_id), \
+ _name " has the wrong ID: expected-id=%s, " \
+ "%![ft-]+F", bt_common_field_type_id_string(_id), (_ft))
+
+#define BT_ASSERT_PRE_FT_HOT(_ft, _name) \
+ BT_ASSERT_PRE_HOT((struct bt_field_type *) (_ft), \
+ (_name), ": %!+F", (_ft))
+
+#define BT_FIELD_TYPE_NAMED_FT_AT_INDEX(_ft, _index) \
+ (&g_array_index(((struct bt_field_type_named_field_types_container *) (_ft))->named_fts, \
+ struct bt_named_field_type, (_index)))
+
+#define BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(_ft, _index) \
+ (&g_array_index(((struct bt_field_type_enumeration *) (_ft))->mappings, \
+ struct bt_field_type_enumeration_mapping, (_index)))
+
+#define BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
+ (&g_array_index((_mapping)->ranges, \
+ struct bt_field_type_enumeration_mapping_range, (_index)))
+
+struct bt_field;
+struct bt_field_type;
+
+struct bt_field_type {
struct bt_object base;
- struct bt_declaration *declaration;
- type_freeze_func freeze;
- type_serialize_func serialize;
- /*
- * A type can't be modified once it is added to an event or after a
- * a field has been instanciated from it.
- */
- int frozen;
+ enum bt_field_type_id id;
+ bool frozen;
/*
- * This flag indicates if the field type is valid. A valid
- * field type is _always_ frozen. All the nested field types of
- * a valid field type are also valid (and thus frozen).
+ * Only used in developer mode, this flag indicates whether or
+ * not this field type is part of a trace.
*/
- int valid;
+ bool part_of_trace;
};
-struct bt_ctf_field_type_integer {
- struct bt_ctf_field_type parent;
- struct declaration_integer declaration;
- struct bt_ctf_clock *mapped_clock;
+struct bt_field_type_integer {
+ struct bt_field_type common;
/*
- * This is what the user sets and is never modified by internal
- * code.
+ * Value range of fields built from this integer field type:
+ * this is an equivalent integer size in bits. More formally,
+ * `range` is `n` in:
*
- * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
+ * Unsigned range: [0, 2^n - 1]
+ * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
*/
- enum bt_ctf_byte_order user_byte_order;
+ uint64_t range;
+
+ enum bt_field_type_integer_preferred_display_base base;
};
-struct enumeration_mapping {
+struct bt_field_type_enumeration_mapping_range {
union {
- uint64_t _unsigned;
- int64_t _signed;
- } range_start;
+ uint64_t u;
+ int64_t i;
+ } lower;
union {
- uint64_t _unsigned;
- int64_t _signed;
- } range_end;
- GQuark string;
+ uint64_t u;
+ int64_t i;
+ } upper;
};
-struct bt_ctf_field_type_enumeration {
- struct bt_ctf_field_type parent;
- struct bt_ctf_field_type *container;
- GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */
- struct declaration_enum declaration;
+struct bt_field_type_enumeration_mapping {
+ GString *label;
+
+ /* Array of `struct bt_field_type_enumeration_mapping_range` */
+ GArray *ranges;
};
-struct bt_ctf_field_type_floating_point {
- struct bt_ctf_field_type parent;
- struct declaration_float declaration;
+struct bt_field_type_enumeration {
+ struct bt_field_type_integer common;
- /*
- * The `declaration` field above contains 3 pointers pointing
- * to the fields below. This avoids unnecessary dynamic
- * allocations.
- */
- struct declaration_integer sign;
- struct declaration_integer mantissa;
- struct declaration_integer exp;
+ /* Array of `struct bt_field_type_enumeration_mapping *` */
+ GArray *mappings;
/*
- * This is what the user sets and is never modified by internal
- * code.
+ * This is an array of `const char *` which acts as a temporary
+ * (potentially growing) buffer for
+ * bt_field_type_unsigned_enumeration_get_mapping_labels_by_value()
+ * and
+ * bt_field_type_signed_enumeration_get_mapping_labels_by_value().
*
- * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
+ * The actual strings are owned by the mappings above.
*/
- enum bt_ctf_byte_order user_byte_order;
-};
-
-struct structure_field {
- GQuark name;
- struct bt_ctf_field_type *type;
-};
-
-struct bt_ctf_field_type_structure {
- struct bt_ctf_field_type parent;
- GHashTable *field_name_to_index;
- GPtrArray *fields; /* Array of pointers to struct structure_field */
- struct declaration_struct declaration;
+ GPtrArray *label_buf;
};
-struct bt_ctf_field_type_variant {
- struct bt_ctf_field_type parent;
- GString *tag_name;
- struct bt_ctf_field_type_enumeration *tag;
- struct bt_ctf_field_path *tag_field_path;
- GHashTable *field_name_to_index;
- GPtrArray *fields; /* Array of pointers to struct structure_field */
- struct declaration_variant declaration;
+struct bt_field_type_real {
+ struct bt_field_type common;
+ bool is_single_precision;
};
-struct bt_ctf_field_type_array {
- struct bt_ctf_field_type parent;
- struct bt_ctf_field_type *element_type;
- unsigned int length; /* Number of elements */
- struct declaration_array declaration;
+struct bt_field_type_string {
+ struct bt_field_type common;
};
-struct bt_ctf_field_type_sequence {
- struct bt_ctf_field_type parent;
- struct bt_ctf_field_type *element_type;
- GString *length_field_name;
- struct bt_ctf_field_path *length_field_path;
- struct declaration_sequence declaration;
-};
+/* A named field type is a (name, field type) pair */
+struct bt_named_field_type {
+ GString *name;
-struct bt_ctf_field_type_string {
- struct bt_ctf_field_type parent;
- struct declaration_string declaration;
+ /* Owned by this */
+ struct bt_field_type *ft;
};
-BT_HIDDEN
-void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
- struct bt_ctf_field_type_variant *variant, int64_t tag_value);
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
- struct bt_ctf_field_type_variant *variant, uint64_t tag_value);
+/*
+ * This is the base field type for a container of named field types.
+ * Structure and variant field types inherit this.
+ */
+struct bt_field_type_named_field_types_container {
+ struct bt_field_type common;
-BT_HIDDEN
-int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
- struct metadata_context *context);
+ /*
+ * Key: `const char *`, not owned by this (owned by named field
+ * type objects contained in `named_fts` below).
+ */
+ GHashTable *name_to_index;
-BT_HIDDEN
-int bt_ctf_field_type_validate(struct bt_ctf_field_type *type);
+ /* Array of `struct bt_named_field_type` */
+ GArray *named_fts;
+};
-BT_HIDDEN
-const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
- struct bt_ctf_field_type_enumeration *enumeration_type,
- uint64_t value);
+struct bt_field_type_structure {
+ struct bt_field_type_named_field_types_container common;
+};
-BT_HIDDEN
-const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
- struct bt_ctf_field_type_enumeration *enumeration_type,
- int64_t value);
+struct bt_field_type_array {
+ struct bt_field_type common;
-/* Override field type's byte order only if it is set to "native" */
-BT_HIDDEN
-void bt_ctf_field_type_set_native_byte_order(
- struct bt_ctf_field_type *type, int byte_order);
+ /* Owned by this */
+ struct bt_field_type *element_ft;
+};
-BT_HIDDEN
-int bt_ctf_field_type_structure_get_field_name_index(
- struct bt_ctf_field_type *structure, const char *name);
+struct bt_field_type_static_array {
+ struct bt_field_type_array common;
+ uint64_t length;
+};
-/* Replace an existing field's type in a structure */
-BT_HIDDEN
-int bt_ctf_field_type_structure_set_field_index(
- struct bt_ctf_field_type *structure,
- struct bt_ctf_field_type *field, int index);
+struct bt_field_type_dynamic_array {
+ struct bt_field_type_array common;
-BT_HIDDEN
-int bt_ctf_field_type_variant_get_field_name_index(
- struct bt_ctf_field_type *variant, const char *name);
+ /* Weak: never dereferenced, only use to find it elsewhere */
+ struct bt_field_type *length_ft;
-BT_HIDDEN
-int bt_ctf_field_type_sequence_set_length_field_path(
- struct bt_ctf_field_type *type,
- struct bt_ctf_field_path *path);
+ /* Owned by this */
+ struct bt_field_path *length_field_path;
+};
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
- struct bt_ctf_field_path *path);
+struct bt_field_type_variant {
+ struct bt_field_type_named_field_types_container common;
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
- struct bt_ctf_field_type *tag_type);
+ /* Weak: never dereferenced, only use to find it elsewhere */
+ struct bt_field_type *selector_ft;
-/* Replace an existing field's type in a variant */
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_field_index(
- struct bt_ctf_field_type *variant,
- struct bt_ctf_field_type *field, int index);
+ /* Owned by this */
+ struct bt_field_path *selector_field_path;
+};
-BT_HIDDEN
-int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array,
- struct bt_ctf_field_type *element_type);
+static inline
+bool bt_field_type_has_known_id(struct bt_field_type *ft)
+{
+ return ft->id >= BT_FIELD_TYPE_ID_UNSIGNED_INTEGER &&
+ ft->id <= BT_FIELD_TYPE_ID_VARIANT;
+}
BT_HIDDEN
-int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array,
- struct bt_ctf_field_type *element_type);
+void _bt_field_type_freeze(struct bt_field_type *field_type);
-BT_HIDDEN
-int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type);
+#ifdef BT_DEV_MODE
+# define bt_field_type_freeze _bt_field_type_freeze
+#else
+# define bt_field_type_freeze(_ft)
+#endif
+/*
+ * This function recursively marks `field_type` and its children as
+ * being part of a trace. This is used to validate that all field types
+ * are used at a single location within trace objects even if they are
+ * shared objects for other purposes.
+ */
BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
- struct bt_ctf_field_type *type, int index);
+void _bt_field_type_make_part_of_trace(struct bt_field_type *field_type);
-BT_HIDDEN
-int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type,
- const char *name);
+#ifdef BT_DEV_MODE
+# define bt_field_type_make_part_of_trace _bt_field_type_make_part_of_trace
+#else
+# define bt_field_type_make_part_of_trace(_ft) ((void) _ft)
+#endif
#endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */