X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fctf-ir%2Ffields.c;h=3ebe6ebf18b25805bf9bf23393e63e599b8c5f00;hb=cb6f1f7dfb7938a4738a7f3ca3886334fbceb1a3;hp=173aa3358d05f6afb0f2e00e64dffba38aa90327;hpb=c14d64fa6621ca3fb666837721ea628af4cd8b3d;p=babeltrace.git diff --git a/lib/ctf-ir/fields.c b/lib/ctf-ir/fields.c index 173aa335..3ebe6ebf 100644 --- a/lib/ctf-ir/fields.c +++ b/lib/ctf-ir/fields.c @@ -26,2078 +26,1351 @@ * SOFTWARE. */ +#define BT_LOG_TAG "FIELDS" +#include + +#include +#include #include #include -#include #include #include #include #include #include +#include +#include -#define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) +#define BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(_field, _name) \ + BT_ASSERT_PRE((_field)->type->id == BT_FIELD_TYPE_ID_INTEGER || \ + (_field)->type->id == BT_FIELD_TYPE_ID_ENUM, \ + _name " is not an integer or an enumeration field: " \ + "%!+f", (_field)) static -struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_enumeration_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_floating_point_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_structure_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_variant_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_array_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_sequence_create( - struct bt_ctf_field_type *); -static -struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); +int bt_field_generic_validate(struct bt_field *field); static -void bt_ctf_field_destroy(struct bt_object *); -static -void bt_ctf_field_integer_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_structure_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_variant_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_array_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_sequence_destroy(struct bt_ctf_field *); -static -void bt_ctf_field_string_destroy(struct bt_ctf_field *); +int bt_field_structure_validate_recursive(struct bt_field *field); static -int bt_ctf_field_generic_validate(struct bt_ctf_field *); -static -int bt_ctf_field_structure_validate(struct bt_ctf_field *); -static -int bt_ctf_field_variant_validate(struct bt_ctf_field *); -static -int bt_ctf_field_enumeration_validate(struct bt_ctf_field *); -static -int bt_ctf_field_array_validate(struct bt_ctf_field *); -static -int bt_ctf_field_sequence_validate(struct bt_ctf_field *); +int bt_field_variant_validate_recursive(struct bt_field *field); static -int bt_ctf_field_generic_reset(struct bt_ctf_field *); -static -int bt_ctf_field_structure_reset(struct bt_ctf_field *); -static -int bt_ctf_field_variant_reset(struct bt_ctf_field *); -static -int bt_ctf_field_enumeration_reset(struct bt_ctf_field *); -static -int bt_ctf_field_array_reset(struct bt_ctf_field *); -static -int bt_ctf_field_sequence_reset(struct bt_ctf_field *); -static -int bt_ctf_field_string_reset(struct bt_ctf_field *); +int bt_field_array_validate_recursive(struct bt_field *field); static -int bt_ctf_field_integer_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_structure_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_variant_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_array_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); -static -int bt_ctf_field_string_serialize(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order); +int bt_field_sequence_validate_recursive(struct bt_field *field); static -int bt_ctf_field_integer_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_enumeration_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_floating_point_copy(struct bt_ctf_field *, - struct bt_ctf_field *); -static -int bt_ctf_field_structure_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_variant_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_array_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_sequence_copy(struct bt_ctf_field *, struct bt_ctf_field *); -static -int bt_ctf_field_string_copy(struct bt_ctf_field *, struct bt_ctf_field *); +void bt_field_generic_reset(struct bt_field *field); static -void generic_field_freeze(struct bt_ctf_field *); -static -void bt_ctf_field_enumeration_freeze(struct bt_ctf_field *); -static -void bt_ctf_field_structure_freeze(struct bt_ctf_field *); -static -void bt_ctf_field_variant_freeze(struct bt_ctf_field *); -static -void bt_ctf_field_array_freeze(struct bt_ctf_field *); -static -void bt_ctf_field_sequence_freeze(struct bt_ctf_field *); +void bt_field_structure_reset_recursive(struct bt_field *field); static -bt_bool bt_ctf_field_generic_is_set(struct bt_ctf_field *); +void bt_field_variant_reset_recursive(struct bt_field *field); + static -bt_bool bt_ctf_field_structure_is_set(struct bt_ctf_field *); +void bt_field_array_reset_recursive(struct bt_field *field); + static -bt_bool bt_ctf_field_variant_is_set(struct bt_ctf_field *); +void bt_field_sequence_reset_recursive(struct bt_field *field); + static -bt_bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field *); +void bt_field_generic_set_is_frozen(struct bt_field *field, + bool is_frozen); + static -bt_bool bt_ctf_field_array_is_set(struct bt_ctf_field *); +void bt_field_structure_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen); + static -bt_bool bt_ctf_field_sequence_is_set(struct bt_ctf_field *); +void bt_field_variant_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen); static -int increase_packet_size(struct bt_ctf_stream_pos *pos); +void bt_field_array_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen); static -struct bt_ctf_field *(* const field_create_funcs[])( - struct bt_ctf_field_type *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_create, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, -}; +void bt_field_sequence_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen); static -void (* const field_destroy_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_destroy, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_destroy, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_destroy, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_destroy, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_destroy, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_destroy, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_destroy, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_destroy, -}; +bt_bool bt_field_generic_is_set(struct bt_field *field); static -int (* const field_validate_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_generic_validate, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_validate, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_generic_validate, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_validate, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_validate, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_validate, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_validate, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_generic_validate, -}; +bt_bool bt_field_structure_is_set_recursive( + struct bt_field *field); static -int (* const field_reset_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_generic_reset, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_reset, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_generic_reset, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_reset, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_reset, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_reset, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_reset, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_reset, -}; +bt_bool bt_field_variant_is_set_recursive(struct bt_field *field); static -int (* const field_serialize_funcs[])(struct bt_ctf_field *, - struct bt_ctf_stream_pos *, enum bt_ctf_byte_order) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_serialize, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_serialize, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_serialize, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_serialize, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_serialize, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_serialize, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_serialize, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_serialize, -}; +bt_bool bt_field_array_is_set_recursive(struct bt_field *field); static -int (* const field_copy_funcs[])(struct bt_ctf_field *, - struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_copy, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_copy, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_copy, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_copy, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_copy, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_copy, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_copy, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_copy, +bt_bool bt_field_sequence_is_set_recursive(struct bt_field *field); + +static struct bt_field_methods bt_field_integer_methods = { + .set_is_frozen = bt_field_generic_set_is_frozen, + .validate = bt_field_generic_validate, + .is_set = bt_field_generic_is_set, + .reset = bt_field_generic_reset, }; -static -void (* const field_freeze_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = generic_field_freeze, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = generic_field_freeze, - [BT_CTF_FIELD_TYPE_ID_STRING] = generic_field_freeze, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_freeze, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_freeze, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_freeze, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_freeze, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_freeze, +static struct bt_field_methods bt_field_floating_point_methods = { + .set_is_frozen = bt_field_generic_set_is_frozen, + .validate = bt_field_generic_validate, + .is_set = bt_field_generic_is_set, + .reset = bt_field_generic_reset, }; -static -bt_bool (* const field_is_set_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_generic_is_set, - [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_is_set, - [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_generic_is_set, - [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_is_set, - [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_is_set, - [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_is_set, - [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_is_set, - [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_generic_is_set, +static struct bt_field_methods bt_field_enumeration_methods = { + .set_is_frozen = bt_field_generic_set_is_frozen, + .validate = bt_field_generic_validate, + .is_set = bt_field_generic_is_set, + .reset = bt_field_generic_reset, }; -struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field *field = NULL; - enum bt_ctf_field_type_id type_id; - int ret; +static struct bt_field_methods bt_field_string_methods = { + .set_is_frozen = bt_field_generic_set_is_frozen, + .validate = bt_field_generic_validate, + .is_set = bt_field_generic_is_set, + .reset = bt_field_generic_reset, +}; - if (!type) { - goto error; - } +static struct bt_field_methods bt_field_structure_methods = { + .set_is_frozen = bt_field_structure_set_is_frozen_recursive, + .validate = bt_field_structure_validate_recursive, + .is_set = bt_field_structure_is_set_recursive, + .reset = bt_field_structure_reset_recursive, +}; - type_id = bt_ctf_field_type_get_type_id(type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - goto error; - } +static struct bt_field_methods bt_field_sequence_methods = { + .set_is_frozen = bt_field_sequence_set_is_frozen_recursive, + .validate = bt_field_sequence_validate_recursive, + .is_set = bt_field_sequence_is_set_recursive, + .reset = bt_field_sequence_reset_recursive, +}; - /* Field class MUST be valid */ - ret = bt_ctf_field_type_validate(type); +static struct bt_field_methods bt_field_array_methods = { + .set_is_frozen = bt_field_array_set_is_frozen_recursive, + .validate = bt_field_array_validate_recursive, + .is_set = bt_field_array_is_set_recursive, + .reset = bt_field_array_reset_recursive, +}; - if (ret) { - /* Invalid */ - goto error; - } +static struct bt_field_methods bt_field_variant_methods = { + .set_is_frozen = bt_field_variant_set_is_frozen_recursive, + .validate = bt_field_variant_validate_recursive, + .is_set = bt_field_variant_is_set_recursive, + .reset = bt_field_variant_reset_recursive, +}; - field = field_create_funcs[type_id](type); - if (!field) { - goto error; - } +static +struct bt_field *bt_field_integer_create(struct bt_field_type *); - /* The type's declaration can't change after this point */ - bt_ctf_field_type_freeze(type); - bt_get(type); - bt_object_init(field, bt_ctf_field_destroy); - field->type = type; -error: - return field; -} +static +struct bt_field *bt_field_enumeration_create(struct bt_field_type *); -void bt_ctf_field_get(struct bt_ctf_field *field) -{ - bt_get(field); -} +static +struct bt_field *bt_field_floating_point_create(struct bt_field_type *); -void bt_ctf_field_put(struct bt_ctf_field *field) -{ - bt_put(field); -} +static +struct bt_field *bt_field_structure_create(struct bt_field_type *); -struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) -{ - struct bt_ctf_field_type *ret = NULL; +static +struct bt_field *bt_field_variant_create(struct bt_field_type *); - if (!field) { - goto end; - } +static +struct bt_field *bt_field_array_create(struct bt_field_type *); - ret = field->type; - bt_get(ret); -end: - return ret; -} +static +struct bt_field *bt_field_sequence_create(struct bt_field_type *); -enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) -{ - enum bt_ctf_field_type_id ret = BT_CTF_FIELD_TYPE_ID_UNKNOWN; +static +struct bt_field *bt_field_string_create(struct bt_field_type *); - if (!field) { - goto end; - } +static +struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = { + [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_create, + [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_create, + [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_create, + [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_create, + [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_create, + [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_create, + [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_create, + [BT_FIELD_TYPE_ID_STRING] = bt_field_string_create, +}; - ret = bt_ctf_field_type_get_type_id(field->type); -end: - return ret; -} +static +void bt_field_integer_destroy(struct bt_field *field); -bt_bool bt_ctf_field_is_integer(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_INTEGER; -} +static +void bt_field_enumeration_destroy(struct bt_field *field); -bt_bool bt_ctf_field_is_floating_point(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_FLOAT; -} +static +void bt_field_floating_point_destroy(struct bt_field *field); -bt_bool bt_ctf_field_is_enumeration(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_ENUM; -} +static +void bt_field_structure_destroy_recursive(struct bt_field *field); -bt_bool bt_ctf_field_is_string(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_STRING; -} +static +void bt_field_variant_destroy_recursive(struct bt_field *field); -bt_bool bt_ctf_field_is_structure(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_STRUCT; -} +static +void bt_field_array_destroy_recursive(struct bt_field *field); -bt_bool bt_ctf_field_is_array(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_ARRAY; -} +static +void bt_field_sequence_destroy_recursive(struct bt_field *field); -bt_bool bt_ctf_field_is_sequence(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_SEQUENCE; -} +static +void bt_field_string_destroy(struct bt_field *field); -bt_bool bt_ctf_field_is_variant(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_FIELD_TYPE_ID_VARIANT; -} +static +void (* const field_destroy_funcs[])(struct bt_field *) = { + [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_destroy, + [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_destroy, + [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_destroy, + [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_destroy_recursive, + [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_destroy_recursive, + [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_destroy_recursive, + [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_destroy_recursive, + [BT_FIELD_TYPE_ID_STRING] = bt_field_string_destroy, +}; -struct bt_ctf_field *bt_ctf_field_sequence_get_length( - struct bt_ctf_field *field) +BT_ASSERT_PRE_FUNC +static inline bool value_is_in_range_signed(unsigned int size, int64_t value) { - struct bt_ctf_field *ret = NULL; - struct bt_ctf_field_sequence *sequence; - - if (!field) { - goto end; - } + bool ret = true; + int64_t min_value, max_value; - if (bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_SEQUENCE) { - goto end; + min_value = -(1ULL << (size - 1)); + max_value = (1ULL << (size - 1)) - 1; + if (value < min_value || value > max_value) { + BT_LOGF("Value is out of bounds: value=%" PRId64 ", " + "min-value=%" PRId64 ", max-value=%" PRId64, + value, min_value, max_value); + ret = false; } - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - ret = sequence->length; - bt_get(ret); -end: return ret; } -int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, - struct bt_ctf_field *length_field) +BT_ASSERT_PRE_FUNC +static inline bool value_is_in_range_unsigned(unsigned int size, uint64_t value) { - int ret = 0; - struct bt_ctf_field_type_integer *length_type; - struct bt_ctf_field_integer *length; - struct bt_ctf_field_sequence *sequence; - uint64_t sequence_length; - - if (!field || !length_field || field->frozen) { - ret = -1; - goto end; - } - if (bt_ctf_field_type_get_type_id(length_field->type) != - BT_CTF_FIELD_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - length_type = container_of(length_field->type, - struct bt_ctf_field_type_integer, parent); - /* The length field must be unsigned */ - if (length_type->is_signed) { - ret = -1; - goto end; - } - - length = container_of(length_field, struct bt_ctf_field_integer, - parent); - sequence_length = length->payload.unsignd; - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - if (sequence->elements) { - g_ptr_array_free(sequence->elements, TRUE); - bt_put(sequence->length); - } + bool ret = true; + int64_t max_value; - sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); - if (!sequence->elements) { - ret = -1; - goto end; + max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1; + if (value > max_value) { + BT_LOGF("Value is out of bounds: value=%" PRIu64 ", " + "max-value=%" PRIu64, + value, max_value); + ret = false; } - g_ptr_array_set_free_func(sequence->elements, - (GDestroyNotify) bt_put); - g_ptr_array_set_size(sequence->elements, (size_t) sequence_length); - bt_get(length_field); - sequence->length = length_field; -end: return ret; } -struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( - struct bt_ctf_field *field, const char *name) +BT_HIDDEN +struct bt_field *bt_field_create_recursive(struct bt_field_type *type) { - struct bt_ctf_field *new_field = NULL; - GQuark field_quark; - struct bt_ctf_field_structure *structure; - struct bt_ctf_field_type *field_type = NULL; - size_t index; - - if (!field || !name || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRUCT) { - goto error; - } - - field_quark = g_quark_from_string(name); - structure = container_of(field, struct bt_ctf_field_structure, parent); - field_type = - bt_ctf_field_type_structure_get_field_type_by_name(field->type, - name); - if (!g_hash_table_lookup_extended(structure->field_name_to_index, - GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) { - goto error; - } - - if (structure->fields->pdata[index]) { - new_field = structure->fields->pdata[index]; - goto end; - } + struct bt_field *field = NULL; + enum bt_field_type_id type_id; - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { + BT_ASSERT_PRE_NON_NULL(type, "Field type"); + BT_ASSERT(bt_field_type_has_known_id((void *) type)); + BT_ASSERT_PRE(bt_field_type_validate((void *) type) == 0, + "Field type is invalid: %!+F", type); + type_id = bt_field_type_get_type_id(type); + field = field_create_funcs[type_id](type); + if (!field) { goto end; } - new_field = bt_ctf_field_create(field_type); - if (!new_field) { - goto error; - } + bt_field_type_freeze(type); - structure->fields->pdata[index] = new_field; end: - bt_get(new_field); -error: - if (field_type) { - bt_put(field_type); - } - return new_field; + return field; } -struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( - struct bt_ctf_field *field, uint64_t index) +struct bt_field_type *bt_field_borrow_type(struct bt_field *field) { - int ret; - const char *field_name; - struct bt_ctf_field_structure *structure; - struct bt_ctf_field_type *structure_type; - struct bt_ctf_field_type *field_type = NULL; - struct bt_ctf_field *ret_field = NULL; - - if (!field || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRUCT) { - goto end; - } - - structure = container_of(field, struct bt_ctf_field_structure, parent); - if (index >= structure->fields->len) { - goto error; - } - - ret_field = structure->fields->pdata[index]; - if (ret_field) { - goto end; - } - - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { - goto end; - } - - /* Field has not been instanciated yet, create it */ - structure_type = bt_ctf_field_get_type(field); - if (!structure_type) { - goto error; - } - - ret = bt_ctf_field_type_structure_get_field(structure_type, - &field_name, &field_type, index); - bt_put(structure_type); - if (ret) { - goto error; - } - - ret_field = bt_ctf_field_create(field_type); - if (!ret_field) { - goto error; - } + struct bt_field_type *ret = NULL; - structure->fields->pdata[index] = ret_field; -end: - bt_get(ret_field); -error: - bt_put(field_type); - return ret_field; + BT_ASSERT_PRE_NON_NULL(field, "Field"); + ret = field->type; + return ret; } -int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, - const char *name, struct bt_ctf_field *value) +enum bt_field_type_id bt_field_get_type_id(struct bt_field *field) { - int ret = 0; - GQuark field_quark; - struct bt_ctf_field_structure *structure; - struct bt_ctf_field_type *expected_field_type = NULL; - size_t index; - - if (!field || !name || !value || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRUCT) { - ret = -1; - goto end; - } - - field_quark = g_quark_from_string(name); - structure = container_of(field, struct bt_ctf_field_structure, parent); - expected_field_type = - bt_ctf_field_type_structure_get_field_type_by_name(field->type, - name); - - if (bt_ctf_field_type_compare(expected_field_type, value->type)) { - ret = -1; - goto end; - } - - if (!g_hash_table_lookup_extended(structure->field_name_to_index, - GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) { - goto end; - } - - if (structure->fields->pdata[index]) { - bt_put(structure->fields->pdata[index]); - } - - structure->fields->pdata[index] = value; - bt_get(value); -end: - if (expected_field_type) { - bt_put(expected_field_type); - } - return ret; + BT_ASSERT_PRE_NON_NULL(field, "Field"); + return field->type->id; } -struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field, - uint64_t index) +int64_t bt_field_sequence_get_length(struct bt_field *field) { - struct bt_ctf_field *new_field = NULL; - struct bt_ctf_field_type *field_type = NULL; - struct bt_ctf_field_array *array; - - if (!field || bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_ARRAY) { - goto end; - } - - array = container_of(field, struct bt_ctf_field_array, parent); - if (index >= array->elements->len) { - goto end; - } - - field_type = bt_ctf_field_type_array_get_element_type(field->type); - if (array->elements->pdata[(size_t)index]) { - new_field = array->elements->pdata[(size_t)index]; - goto end; - } + struct bt_field_sequence *sequence = (void *) field; - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { - goto end; - } - - new_field = bt_ctf_field_create(field_type); - array->elements->pdata[(size_t)index] = new_field; -end: - if (field_type) { - bt_put(field_type); - } - if (new_field) { - bt_get(new_field); - } - return new_field; + BT_ASSERT_PRE_NON_NULL(field, "Sequence field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_SEQUENCE, + "Field"); + return (int64_t) sequence->length; } -struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field, - uint64_t index) +int bt_field_sequence_set_length(struct bt_field *field, uint64_t length) { - struct bt_ctf_field *new_field = NULL; - struct bt_ctf_field_type *field_type = NULL; - struct bt_ctf_field_sequence *sequence; + int ret = 0; + struct bt_field_sequence *sequence = (void *) field; - if (!field || bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_SEQUENCE) { - goto end; - } + BT_ASSERT_PRE_NON_NULL(field, "Sequence field"); + BT_ASSERT_PRE(((int64_t) length) >= 0, + "Invalid sequence length (too large): length=%" PRId64, + length); + BT_ASSERT_PRE_FIELD_HOT(field, "Sequence field"); - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - if (!sequence->elements || index >= sequence->elements->len) { - goto end; - } + if (unlikely(length > sequence->elements->len)) { + /* Make more room */ + struct bt_field_type_sequence *sequence_ft; + uint64_t cur_len = sequence->elements->len; + uint64_t i; - field_type = bt_ctf_field_type_sequence_get_element_type(field->type); - if (sequence->elements->pdata[(size_t) index]) { - new_field = sequence->elements->pdata[(size_t) index]; - goto end; - } - - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { - goto end; - } - - new_field = bt_ctf_field_create(field_type); - sequence->elements->pdata[(size_t) index] = new_field; -end: - if (field_type) { - bt_put(field_type); - } - if (new_field) { - bt_get(new_field); - } - return new_field; -} - -struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, - struct bt_ctf_field *tag_field) -{ - struct bt_ctf_field *new_field = NULL; - struct bt_ctf_field_variant *variant; - struct bt_ctf_field_type_variant *variant_type; - struct bt_ctf_field_type *field_type; - struct bt_ctf_field *tag_enum = NULL; - struct bt_ctf_field_integer *tag_enum_integer; - int64_t tag_enum_value; - - if (!field || !tag_field || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_VARIANT || - bt_ctf_field_type_get_type_id(tag_field->type) != - BT_CTF_FIELD_TYPE_ID_ENUM) { - goto end; - } - - variant = container_of(field, struct bt_ctf_field_variant, parent); - variant_type = container_of(field->type, - struct bt_ctf_field_type_variant, parent); - tag_enum = bt_ctf_field_enumeration_get_container(tag_field); - if (!tag_enum) { - goto end; - } - - tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer, - parent); - - if (bt_ctf_field_validate(tag_field) < 0) { - goto end; - } - - tag_enum_value = tag_enum_integer->payload.signd; - - /* - * If the variant currently has a tag and a payload, and if the - * requested tag value is the same as the current one, return - * the current payload instead of creating a fresh one. - */ - if (variant->tag && variant->payload) { - struct bt_ctf_field *cur_tag_container = NULL; - struct bt_ctf_field_integer *cur_tag_enum_integer; - int64_t cur_tag_value; - - cur_tag_container = - bt_ctf_field_enumeration_get_container(variant->tag); - assert(cur_tag_container); - cur_tag_enum_integer = container_of(cur_tag_container, - struct bt_ctf_field_integer, parent); - bt_put(cur_tag_container); - cur_tag_value = cur_tag_enum_integer->payload.signd; - - if (cur_tag_value == tag_enum_value) { - new_field = variant->payload; - bt_get(new_field); - goto end; - } - } - - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { - goto end; - } - - field_type = bt_ctf_field_type_variant_get_field_type_signed( - variant_type, tag_enum_value); - if (!field_type) { - goto end; - } - - new_field = bt_ctf_field_create(field_type); - if (!new_field) { - goto end; - } - - bt_put(variant->tag); - bt_put(variant->payload); - bt_get(new_field); - bt_get(tag_field); - variant->tag = tag_field; - variant->payload = new_field; -end: - bt_put(tag_enum); - return new_field; -} - -struct bt_ctf_field *bt_ctf_field_variant_get_current_field( - struct bt_ctf_field *variant_field) -{ - struct bt_ctf_field *current_field = NULL; - struct bt_ctf_field_variant *variant; - - if (!variant_field || - bt_ctf_field_type_get_type_id(variant_field->type) != - BT_CTF_FIELD_TYPE_ID_VARIANT) { - goto end; - } - - variant = container_of(variant_field, struct bt_ctf_field_variant, - parent); - - if (variant->payload) { - current_field = variant->payload; - bt_get(current_field); - goto end; - } - -end: - return current_field; -} - -struct bt_ctf_field *bt_ctf_field_variant_get_tag( - struct bt_ctf_field *variant_field) -{ - struct bt_ctf_field *tag = NULL; - struct bt_ctf_field_variant *variant; - - if (!variant_field || - bt_ctf_field_type_get_type_id(variant_field->type) != - BT_CTF_FIELD_TYPE_ID_VARIANT) { - goto end; - } - - variant = container_of(variant_field, struct bt_ctf_field_variant, - parent); - if (variant->tag) { - tag = bt_get(variant->tag); - } -end: - return tag; -} - -struct bt_ctf_field *bt_ctf_field_enumeration_get_container( - struct bt_ctf_field *field) -{ - struct bt_ctf_field *container = NULL; - struct bt_ctf_field_enumeration *enumeration; + g_ptr_array_set_size(sequence->elements, length); + sequence_ft = (void *) sequence->common.type; - if (!field || bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_ENUM) { - goto end; - } - - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - /* We don't want to modify this field if it's frozen */ - if (field->frozen) { - goto end; - } - - struct bt_ctf_field_type_enumeration *enumeration_type = - container_of(field->type, - struct bt_ctf_field_type_enumeration, parent); - enumeration->payload = - bt_ctf_field_create(enumeration_type->container); - } - - container = enumeration->payload; - bt_get(container); -end: - return container; -} + for (i = cur_len; i < sequence->elements->len; i++) { + struct bt_field *elem_field = + bt_field_create_recursive( + sequence_ft->element_ft); -struct bt_ctf_field_type_enumeration_mapping_iterator * -bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field *field) -{ - int ret; - struct bt_ctf_field *container = NULL; - struct bt_ctf_field_type *container_type = NULL; - struct bt_ctf_field_type_integer *integer_type = NULL; - struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; - - container = bt_ctf_field_enumeration_get_container(field); - if (!container) { - goto end; - } - - container_type = bt_ctf_field_get_type(container); - if (!container_type) { - goto error_put_container; - } - - integer_type = container_of(container_type, - struct bt_ctf_field_type_integer, parent); - - if (!integer_type->is_signed) { - uint64_t value; - - ret = bt_ctf_field_unsigned_integer_get_value(container, - &value); - if (ret) { - goto error_put_container_type; - } - iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value( - field->type, value); - } else { - int64_t value; - - ret = bt_ctf_field_signed_integer_get_value(container, - &value); - if (ret) { - goto error_put_container_type; - } - iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value( - field->type, value); - } - -error_put_container_type: - bt_put(container_type); -error_put_container: - bt_put(container); -end: - return iter; -} - -int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field, - int64_t *value) -{ - int ret = 0; - struct bt_ctf_field_integer *integer; - struct bt_ctf_field_type_integer *integer_type; - - if (!field || !value || !field->payload_set || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer_type = container_of(field->type, - struct bt_ctf_field_type_integer, parent); - if (!integer_type->is_signed) { - ret = -1; - goto end; - } - - integer = container_of(field, - struct bt_ctf_field_integer, parent); - *value = integer->payload.signd; -end: - return ret; -} - -int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field, - int64_t value) -{ - int ret = 0; - struct bt_ctf_field_integer *integer; - struct bt_ctf_field_type_integer *integer_type; - unsigned int size; - int64_t min_value, max_value; - - if (!field || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer = container_of(field, struct bt_ctf_field_integer, parent); - integer_type = container_of(field->type, - struct bt_ctf_field_type_integer, parent); - if (!integer_type->is_signed) { - ret = -1; - goto end; - } - - size = integer_type->size; - min_value = -(1ULL << (size - 1)); - max_value = (1ULL << (size - 1)) - 1; - if (value < min_value || value > max_value) { - ret = -1; - goto end; - } - - integer->payload.signd = value; - integer->parent.payload_set = 1; -end: - return ret; -} - -int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field, - uint64_t *value) -{ - int ret = 0; - struct bt_ctf_field_integer *integer; - struct bt_ctf_field_type_integer *integer_type; - - if (!field || !value || !field->payload_set || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer_type = container_of(field->type, - struct bt_ctf_field_type_integer, parent); - if (integer_type->is_signed) { - ret = -1; - goto end; - } - - integer = container_of(field, - struct bt_ctf_field_integer, parent); - *value = integer->payload.unsignd; -end: - return ret; -} - -int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field, - uint64_t value) -{ - int ret = 0; - struct bt_ctf_field_integer *integer; - struct bt_ctf_field_type_integer *integer_type; - unsigned int size; - uint64_t max_value; - - if (!field || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer = container_of(field, struct bt_ctf_field_integer, parent); - integer_type = container_of(field->type, - struct bt_ctf_field_type_integer, parent); - if (integer_type->is_signed) { - ret = -1; - goto end; - } - - size = integer_type->size; - max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1; - if (value > max_value) { - ret = -1; - goto end; - } - - integer->payload.unsignd = value; - integer->parent.payload_set = 1; -end: - return ret; -} - -int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, - double *value) -{ - int ret = 0; - struct bt_ctf_field_floating_point *floating_point; - - if (!field || !value || !field->payload_set || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_FLOAT) { - ret = -1; - goto end; - } - - floating_point = container_of(field, - struct bt_ctf_field_floating_point, parent); - *value = floating_point->payload; -end: - return ret; -} - -int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, - double value) -{ - int ret = 0; - struct bt_ctf_field_floating_point *floating_point; - - if (!field || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_FLOAT) { - ret = -1; - goto end; - } - floating_point = container_of(field, struct bt_ctf_field_floating_point, - parent); - floating_point->payload = value; - floating_point->parent.payload_set = 1; -end: - return ret; -} - -const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) -{ - const char *ret = NULL; - struct bt_ctf_field_string *string; - - if (!field || !field->payload_set || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRING) { - goto end; - } - - string = container_of(field, - struct bt_ctf_field_string, parent); - ret = string->payload->str; -end: - return ret; -} - -int bt_ctf_field_string_set_value(struct bt_ctf_field *field, - const char *value) -{ - int ret = 0; - struct bt_ctf_field_string *string; - - if (!field || !value || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRING) { - ret = -1; - goto end; - } - - string = container_of(field, struct bt_ctf_field_string, parent); - if (string->payload) { - g_string_assign(string->payload, value); - } else { - string->payload = g_string_new(value); - } - - string->parent.payload_set = 1; -end: - return ret; -} - -int bt_ctf_field_string_append(struct bt_ctf_field *field, - const char *value) -{ - int ret = 0; - struct bt_ctf_field_string *string_field; - - if (!field || !value || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRING) { - ret = -1; - goto end; - } - - string_field = container_of(field, struct bt_ctf_field_string, parent); - - if (string_field->payload) { - g_string_append(string_field->payload, value); - } else { - string_field->payload = g_string_new(value); - } - - string_field->parent.payload_set = 1; - -end: - return ret; -} - -int bt_ctf_field_string_append_len(struct bt_ctf_field *field, - const char *value, unsigned int length) -{ - int i; - int ret = 0; - unsigned int effective_length = length; - struct bt_ctf_field_string *string_field; - - if (!field || !value || field->frozen || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_FIELD_TYPE_ID_STRING) { - ret = -1; - goto end; - } - - string_field = container_of(field, struct bt_ctf_field_string, parent); - - /* make sure no null bytes are appended */ - for (i = 0; i < length; ++i) { - if (value[i] == '\0') { - effective_length = i; - break; - } - } - - if (string_field->payload) { - g_string_append_len(string_field->payload, value, - effective_length); - } else { - string_field->payload = g_string_new_len(value, - effective_length); - } - - string_field->parent.payload_set = 1; - -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_validate(struct bt_ctf_field *field) -{ - int ret = 0; - enum bt_ctf_field_type_id type_id; - - if (!field) { - ret = -1; - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - ret = -1; - goto end; - } - - ret = field_validate_funcs[type_id](field); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_reset(struct bt_ctf_field *field) -{ - int ret = 0; - enum bt_ctf_field_type_id type_id; - - if (!field) { - ret = -1; - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - ret = -1; - goto end; - } - - ret = field_reset_funcs[type_id](field); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) -{ - int ret = 0; - enum bt_ctf_field_type_id type_id; - - if (!field || !pos) { - ret = -1; - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - ret = -1; - goto end; - } - - ret = field_serialize_funcs[type_id](field, pos, native_byte_order); -end: - return ret; -} - - -BT_HIDDEN -bt_bool bt_ctf_field_is_set(struct bt_ctf_field *field) -{ - bt_bool is_set = BT_FALSE; - enum bt_ctf_field_type_id type_id; - - if (!field) { - goto end; - } + if (!elem_field) { + ret = -1; + goto end; + } - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - goto end; + BT_ASSERT(!sequence->elements->pdata[i]); + sequence->elements->pdata[i] = elem_field; + } } - is_set = field_is_set_funcs[type_id](field); + sequence->length = length; + end: - return is_set; + return ret; } -struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) +struct bt_field *bt_field_structure_borrow_field_by_index( + struct bt_field *field, uint64_t index) { - int ret; - struct bt_ctf_field *copy = NULL; - enum bt_ctf_field_type_id type_id; + struct bt_field_structure *structure = (void *) field; - if (!field) { - goto end; - } + BT_ASSERT_PRE_NON_NULL(field, "Structure field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRUCT, "Field"); + BT_ASSERT_PRE(index < structure->fields->len, + "Index is out of bound: %![struct-field-]+f, " + "index=%" PRIu64 ", count=%u", field, index, + structure->fields->len); + return structure->fields->pdata[index]; +} - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - goto end; +struct bt_field *bt_field_structure_borrow_field_by_name( + struct bt_field *field, const char *name) +{ + struct bt_field *ret = NULL; + GQuark field_quark; + struct bt_field_type_structure *structure_ft; + struct bt_field_structure *structure = (void *) field; + size_t index; + GHashTable *field_name_to_index; + + BT_ASSERT_PRE_NON_NULL(field, "Structure field"); + BT_ASSERT_PRE_NON_NULL(name, "Field name"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRUCT, "Field"); + structure_ft = (void *) field->type; + field_name_to_index = structure_ft->field_name_to_index; + field_quark = g_quark_from_string(name); + if (!g_hash_table_lookup_extended(field_name_to_index, + GUINT_TO_POINTER(field_quark), + NULL, (gpointer *) &index)) { + BT_LOGV("Invalid parameter: no such field in structure field's type: " + "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"", + field, field->type, name); + goto error; } - copy = bt_ctf_field_create(field->type); - if (!copy) { - goto end; - } + ret = structure->fields->pdata[index]; + BT_ASSERT(ret); - copy->payload_set = field->payload_set; - ret = field_copy_funcs[type_id](field, copy); - if (ret) { - bt_put(copy); - copy = NULL; - } -end: - return copy; +error: + return ret; } -static -struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) +struct bt_field *bt_field_array_borrow_field( + struct bt_field *field, uint64_t index) { - struct bt_ctf_field_integer *integer = g_new0( - struct bt_ctf_field_integer, 1); + struct bt_field_array *array = (void *) field; - return integer ? &integer->parent : NULL; + BT_ASSERT_PRE_NON_NULL(field, "Array field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_ARRAY, + "Field"); + BT_ASSERT_PRE(index < array->elements->len, + "Index is out of bound: %![array-field-]+f, " + "index=%" PRIu64 ", count=%u", field, + index, array->elements->len); + return array->elements->pdata[(size_t) index]; } -static -struct bt_ctf_field *bt_ctf_field_enumeration_create( - struct bt_ctf_field_type *type) +struct bt_field *bt_field_sequence_borrow_field( + struct bt_field *field, uint64_t index) { - struct bt_ctf_field_enumeration *enumeration = g_new0( - struct bt_ctf_field_enumeration, 1); + struct bt_field_sequence *sequence = (void *) field; - return enumeration ? &enumeration->parent : NULL; + BT_ASSERT_PRE_NON_NULL(field, "Sequence field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_SEQUENCE, + "Field"); + BT_ASSERT_PRE(index < sequence->length, + "Index is out of bound: %![seq-field-]+f, " + "index=%" PRIu64 ", count=%u", field, index, + sequence->elements->len); + return sequence->elements->pdata[(size_t) index]; } -static -struct bt_ctf_field *bt_ctf_field_floating_point_create( - struct bt_ctf_field_type *type) +struct bt_field *bt_field_variant_borrow_current_field( + struct bt_field *field) { - struct bt_ctf_field_floating_point *floating_point; + struct bt_field_variant *variant = (void *) field; - floating_point = g_new0(struct bt_ctf_field_floating_point, 1); - return floating_point ? &floating_point->parent : NULL; + BT_ASSERT_PRE_NON_NULL(field, "Variant field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_VARIANT, "Field"); + BT_ASSERT_PRE(variant->current_field, + "Variant field has no current field: %!+f", field); + return variant->current_field; } -static -struct bt_ctf_field *bt_ctf_field_structure_create( - struct bt_ctf_field_type *type) +static inline +int bt_field_variant_set_tag(struct bt_field *field, + uint64_t tag_uval, bool is_signed) { - struct bt_ctf_field_type_structure *structure_type = container_of(type, - struct bt_ctf_field_type_structure, parent); - struct bt_ctf_field_structure *structure = g_new0( - struct bt_ctf_field_structure, 1); - struct bt_ctf_field *field = NULL; + int ret = 0; + int64_t choice_index; + struct bt_field_variant *variant = (void *) field; - if (!structure) { + BT_ASSERT_PRE_NON_NULL(field, "Variant field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_VARIANT, "Field"); + + /* Find matching index in variant field's type */ + choice_index = bt_field_type_variant_find_choice_index( + field->type, tag_uval, is_signed); + if (choice_index < 0) { + ret = -1; goto end; } - structure->field_name_to_index = structure_type->field_name_to_index; - structure->fields = g_ptr_array_new_with_free_func( - (GDestroyNotify)bt_ctf_field_put); - g_ptr_array_set_size(structure->fields, - g_hash_table_size(structure->field_name_to_index)); - field = &structure->parent; + /* Select corresponding field */ + BT_ASSERT(choice_index < variant->fields->len); + variant->current_field = variant->fields->pdata[choice_index]; + variant->tag_value.u = tag_uval; + end: - return field; + return ret; } -static -struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) +int bt_field_variant_set_tag_signed(struct bt_field *variant_field, + int64_t tag) { - struct bt_ctf_field_variant *variant = g_new0( - struct bt_ctf_field_variant, 1); - return variant ? &variant->parent : NULL; + return bt_field_variant_set_tag((void *) variant_field, + (uint64_t) tag, true); } -static -struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) +int bt_field_variant_set_tag_unsigned(struct bt_field *variant_field, + uint64_t tag) { - struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1); - struct bt_ctf_field_type_array *array_type; - unsigned int array_length; - - if (!array || !type) { - goto error; - } - - array_type = container_of(type, struct bt_ctf_field_type_array, parent); - array_length = array_type->length; - array->elements = g_ptr_array_sized_new(array_length); - if (!array->elements) { - goto error; - } - - g_ptr_array_set_free_func(array->elements, - (GDestroyNotify)bt_ctf_field_put); - g_ptr_array_set_size(array->elements, array_length); - return &array->parent; -error: - g_free(array); - return NULL; + return bt_field_variant_set_tag((void *) variant_field, + (uint64_t) tag, false); } -static -struct bt_ctf_field *bt_ctf_field_sequence_create( - struct bt_ctf_field_type *type) +int bt_field_variant_get_tag_signed(struct bt_field *field, + int64_t *tag) { - struct bt_ctf_field_sequence *sequence = g_new0( - struct bt_ctf_field_sequence, 1); - return sequence ? &sequence->parent : NULL; + struct bt_field_variant *variant = (void *) field; + + BT_ASSERT_PRE_NON_NULL(field, "Variant field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_VARIANT, "Field"); + BT_ASSERT_PRE(variant->current_field, + "Variant field has no current field: %!+f", field); + *tag = variant->tag_value.i; + return 0; } -static -struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) +int bt_field_variant_get_tag_unsigned(struct bt_field *field, + uint64_t *tag) { - struct bt_ctf_field_string *string = g_new0( - struct bt_ctf_field_string, 1); - return string ? &string->parent : NULL; + struct bt_field_variant *variant = (void *) field; + + BT_ASSERT_PRE_NON_NULL(field, "Variant field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_VARIANT, "Field"); + BT_ASSERT_PRE(variant->current_field, + "Variant field has no current field: %!+f", field); + *tag = variant->tag_value.u; + return 0; } -static -void bt_ctf_field_destroy(struct bt_object *obj) +struct bt_field_type_enumeration_mapping_iterator * +bt_field_enumeration_get_mappings(struct bt_field *field) { - struct bt_ctf_field *field; - struct bt_ctf_field_type *type; - enum bt_ctf_field_type_id type_id; + struct bt_field_enumeration *enum_field = (void *) field; + struct bt_field_type_enumeration *enum_type = NULL; + struct bt_field_type_integer *integer_type = NULL; + struct bt_field_type_enumeration_mapping_iterator *iter = NULL; - field = container_of(obj, struct bt_ctf_field, base); - type = field->type; - type_id = bt_ctf_field_type_get_type_id(type); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - return; + BT_ASSERT(field); + BT_ASSERT(field->type->id == BT_FIELD_TYPE_ID_ENUM); + BT_ASSERT(field->payload_set); + BT_ASSERT_PRE_NON_NULL(field, "Enumeration field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID((struct bt_field *) field, + BT_FIELD_TYPE_ID_ENUM, "Field"); + BT_ASSERT_PRE_FIELD_IS_SET((struct bt_field *) field, + "Enumeration field"); + enum_type = (void *) field->type; + integer_type = enum_type->container_ft; + + if (!integer_type->is_signed) { + iter = bt_field_type_enumeration_unsigned_find_mappings_by_value( + field->type, + enum_field->common.payload.unsignd); + } else { + iter = bt_field_type_enumeration_signed_find_mappings_by_value( + field->type, + enum_field->common.payload.signd); } - field_destroy_funcs[type_id](field); - bt_put(type); + return iter; } -static -void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) +BT_ASSERT_PRE_FUNC +static inline +struct bt_field_type_integer *get_int_enum_int_ft( + struct bt_field *field) { - struct bt_ctf_field_integer *integer; + struct bt_field_integer *int_field = (void *) field; + struct bt_field_type_integer *int_ft = NULL; - if (!field) { - return; + if (int_field->common.type->id == BT_FIELD_TYPE_ID_INTEGER) { + int_ft = (void *) int_field->common.type; + } else if (int_field->common.type->id == BT_FIELD_TYPE_ID_ENUM) { + struct bt_field_type_enumeration *enum_ft = + (void *) int_field->common.type; + int_ft = enum_ft->container_ft; + } else { + abort(); } - integer = container_of(field, struct bt_ctf_field_integer, parent); - g_free(integer); + BT_ASSERT(int_ft); + return int_ft; } -static -void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field) +int bt_field_integer_signed_get_value(struct bt_field *field, int64_t *value) { - struct bt_ctf_field_enumeration *enumeration; + struct bt_field_integer *integer = (void *) field; - if (!field) { - return; - } + BT_ASSERT_PRE_NON_NULL(field, "Integer/enumeration field"); + BT_ASSERT_PRE_NON_NULL(value, "Value"); + BT_ASSERT_PRE_FIELD_IS_SET(field, + "Integer/enumeration field"); + BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(field, "Field"); + BT_ASSERT_PRE(bt_field_type_integer_is_signed( + (void *) get_int_enum_int_ft(field)), + "Field's type is unsigned: %!+f", field); + *value = integer->payload.signd; + return 0; +} - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - bt_put(enumeration->payload); - g_free(enumeration); +int bt_field_integer_signed_set_value(struct bt_field *field, int64_t value) +{ + int ret = 0; + struct bt_field_integer *integer = (void *) field; + + BT_ASSERT_PRE_NON_NULL(field, "Integer field"); + BT_ASSERT_PRE_FIELD_HOT(field, "Integer field"); + BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(field, "Field"); + BT_ASSERT_PRE(bt_field_type_integer_is_signed( + (void *) get_int_enum_int_ft(field)), + "Field's type is unsigned: %!+f", field); + BT_ASSERT_PRE(value_is_in_range_signed( + get_int_enum_int_ft(field)->size, value), + "Value is out of bounds: value=%" PRId64 ", %![field-]+f", + value, field); + integer->payload.signd = value; + bt_field_set(field, true); + return ret; } -static -void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) +int bt_field_integer_unsigned_get_value(struct bt_field *field, uint64_t *value) { - struct bt_ctf_field_floating_point *floating_point; + struct bt_field_integer *integer = (void *) field; - if (!field) { - return; - } + BT_ASSERT_PRE_NON_NULL(field, "Integer field"); + BT_ASSERT_PRE_NON_NULL(value, "Value"); + BT_ASSERT_PRE_FIELD_IS_SET(field, "Integer field"); + BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(field, "Field"); + BT_ASSERT_PRE(!bt_field_type_integer_is_signed( + (void *) get_int_enum_int_ft(field)), + "Field's type is signed: %!+f", field); + *value = integer->payload.unsignd; + return 0; +} - floating_point = container_of(field, struct bt_ctf_field_floating_point, - parent); - g_free(floating_point); +int bt_field_integer_unsigned_set_value(struct bt_field *field, + uint64_t value) +{ + struct bt_field_integer *integer = (void *) field; + + BT_ASSERT_PRE_NON_NULL(field, "Integer field"); + BT_ASSERT_PRE_FIELD_HOT(field, "Integer field"); + BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(field, "Field"); + BT_ASSERT_PRE(!bt_field_type_integer_is_signed( + (void *) get_int_enum_int_ft(field)), + "Field's type is signed: %!+f", field); + BT_ASSERT_PRE(value_is_in_range_unsigned( + get_int_enum_int_ft(field)->size, value), + "Value is out of bounds: value=%" PRIu64 ", %![field-]+f", + value, field); + integer->payload.unsignd = value; + bt_field_set(field, true); + return 0; } -static -void bt_ctf_field_structure_destroy(struct bt_ctf_field *field) +int bt_field_floating_point_get_value(struct bt_field *field, + double *value) { - struct bt_ctf_field_structure *structure; + struct bt_field_floating_point *floating_point = (void *) field; - if (!field) { - return; - } + BT_ASSERT_PRE_NON_NULL(field, "Floating point number field"); + BT_ASSERT_PRE_NON_NULL(value, "Value"); + BT_ASSERT_PRE_FIELD_IS_SET(field, "Floating point number field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_FLOAT, "Field"); + *value = floating_point->payload; + return 0; +} - structure = container_of(field, struct bt_ctf_field_structure, parent); - g_ptr_array_free(structure->fields, TRUE); - g_free(structure); +int bt_field_floating_point_set_value(struct bt_field *field, + double value) +{ + struct bt_field_floating_point *floating_point = (void *) field; + + BT_ASSERT_PRE_NON_NULL(field, "Floating point number field"); + BT_ASSERT_PRE_FIELD_HOT(field, "Floating point number field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_FLOAT, "Field"); + floating_point->payload = value; + bt_field_set(field, true); + return 0; } -static -void bt_ctf_field_variant_destroy(struct bt_ctf_field *field) +const char *bt_field_string_get_value(struct bt_field *field) { - struct bt_ctf_field_variant *variant; + struct bt_field_string *string = (void *) field; - if (!field) { - return; - } + BT_ASSERT_PRE_NON_NULL(field, "String field"); + BT_ASSERT_PRE_FIELD_IS_SET(field, "String field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRING, "Field"); + return (const char *) string->buf->data; +} - variant = container_of(field, struct bt_ctf_field_variant, parent); - bt_put(variant->tag); - bt_put(variant->payload); - g_free(variant); +int bt_field_string_set_value(struct bt_field *field, const char *value) +{ + BT_ASSERT_PRE_NON_NULL(field, "String field"); + BT_ASSERT_PRE_NON_NULL(value, "Value"); + BT_ASSERT_PRE_FIELD_HOT(field, "String field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRING, "Field"); + bt_field_string_clear(field); + return bt_field_string_append_len(field, + value, strlen(value)); } -static -void bt_ctf_field_array_destroy(struct bt_ctf_field *field) +int bt_field_string_append(struct bt_field *field, const char *value) { - struct bt_ctf_field_array *array; + BT_ASSERT_PRE_NON_NULL(value, "Value"); + return bt_field_string_append_len(field, value, + strlen(value)); +} - if (!field) { - return; +int bt_field_string_append_len(struct bt_field *field, + const char *value, unsigned int length) +{ + struct bt_field_string *string_field = (void *) field; + char *data; + size_t new_size; + + BT_ASSERT_PRE_NON_NULL(field, "String field"); + BT_ASSERT_PRE_NON_NULL(value, "Value"); + BT_ASSERT_PRE_FIELD_HOT(field, "String field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRING, "Field"); + + /* Make sure no null bytes are appended */ + BT_ASSERT_PRE(memchr(value, '\0', length) == NULL, + "String value to append contains a null character: " + "partial-value=\"%.32s\", length=%u", value, length); + + new_size = string_field->size + length; + + if (unlikely(new_size + 1 > string_field->buf->len)) { + g_array_set_size(string_field->buf, new_size + 1); } - array = container_of(field, struct bt_ctf_field_array, parent); - g_ptr_array_free(array->elements, TRUE); - g_free(array); + data = string_field->buf->data; + memcpy(data + string_field->size, value, length); + ((char *) string_field->buf->data)[new_size] = '\0'; + string_field->size = new_size; + bt_field_set(field, true); + return 0; } -static -void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field) +int bt_field_string_clear(struct bt_field *field) { - struct bt_ctf_field_sequence *sequence; + struct bt_field_string *string_field = (void *) field; - if (!field) { - return; - } + BT_ASSERT_PRE_NON_NULL(field, "String field"); + BT_ASSERT_PRE_FIELD_HOT(field, "String field"); + BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(field, + BT_FIELD_TYPE_ID_STRING, "Field"); + string_field->size = 0; + bt_field_set(field, true); + return 0; +} - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - if (sequence->elements) { - g_ptr_array_free(sequence->elements, TRUE); - } - bt_put(sequence->length); - g_free(sequence); +static inline +void bt_field_finalize(struct bt_field *field) +{ + BT_ASSERT(field); + BT_LOGD_STR("Putting field's type."); + bt_put(field->type); } static -void bt_ctf_field_string_destroy(struct bt_ctf_field *field) +void bt_field_integer_destroy(struct bt_field *field) { - struct bt_ctf_field_string *string; - if (!field) { - return; - } + BT_ASSERT(field); + BT_LOGD("Destroying integer field object: addr=%p", field); + bt_field_finalize(field); + g_free(field); +} - string = container_of(field, struct bt_ctf_field_string, parent); - if (string->payload) { - g_string_free(string->payload, TRUE); - } - g_free(string); +static +void bt_field_floating_point_destroy(struct bt_field *field) +{ + BT_ASSERT(field); + BT_LOGD("Destroying floating point field object: addr=%p", field); + bt_field_finalize(field); + g_free(field); } static -int bt_ctf_field_generic_validate(struct bt_ctf_field *field) +void bt_field_enumeration_destroy(struct bt_field *field) { - return (field && field->payload_set) ? 0 : -1; + BT_LOGD("Destroying enumeration field object: addr=%p", field); + bt_field_finalize((void *) field); + g_free(field); } static -int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) +void bt_field_structure_destroy_recursive(struct bt_field *field) { - int ret; - struct bt_ctf_field_enumeration *enumeration; + struct bt_field_structure *structure = (void *) field; - if (!field) { - ret = -1; - goto end; - } + BT_ASSERT(field); + BT_LOGD("Destroying structure field object: addr=%p", field); + bt_field_finalize(field); - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - ret = -1; - goto end; + if (structure->fields) { + g_ptr_array_free(structure->fields, TRUE); } - ret = bt_ctf_field_validate(enumeration->payload); -end: - return ret; + g_free(field); } static -int bt_ctf_field_structure_validate(struct bt_ctf_field *field) +void bt_field_variant_destroy_recursive(struct bt_field *field) { - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure; + struct bt_field_variant *variant = (void *) field; - if (!field) { - ret = -1; - goto end; - } + BT_ASSERT(field); + BT_LOGD("Destroying variant field object: addr=%p", field); + bt_field_finalize(field); - structure = container_of(field, struct bt_ctf_field_structure, parent); - for (i = 0; i < structure->fields->len; i++) { - ret = bt_ctf_field_validate(structure->fields->pdata[i]); - if (ret) { - const char *name; - struct bt_ctf_field_type *field_type = - bt_ctf_field_get_type(field); - - (void) bt_ctf_field_type_structure_get_field(field_type, - &name, NULL, i); - fprintf(stderr, "Field %s failed validation\n", - name ? name : "NULL"); - bt_put(field_type); - goto end; - } + if (variant->fields) { + g_ptr_array_free(variant->fields, TRUE); } -end: - return ret; + + g_free(field); } static -int bt_ctf_field_variant_validate(struct bt_ctf_field *field) +void bt_field_array_destroy_recursive(struct bt_field *field) { - int ret = 0; - struct bt_ctf_field_variant *variant; + struct bt_field_array *array = (void *) field; - if (!field) { - ret = -1; - goto end; + BT_ASSERT(field); + BT_LOGD("Destroying array field object: addr=%p", field); + bt_field_finalize(field); + + if (array->elements) { + g_ptr_array_free(array->elements, TRUE); } - variant = container_of(field, struct bt_ctf_field_variant, parent); - ret = bt_ctf_field_validate(variant->payload); -end: - return ret; + g_free(field); } static -int bt_ctf_field_array_validate(struct bt_ctf_field *field) +void bt_field_sequence_destroy_recursive(struct bt_field *field) { - size_t i; - int ret = 0; - struct bt_ctf_field_array *array; + struct bt_field_sequence *sequence = (void *) field; - if (!field) { - ret = -1; - goto end; - } + BT_ASSERT(field); + BT_LOGD("Destroying sequence field object: addr=%p", field); + bt_field_finalize(field); - array = container_of(field, struct bt_ctf_field_array, parent); - for (i = 0; i < array->elements->len; i++) { - ret = bt_ctf_field_validate(array->elements->pdata[i]); - if (ret) { - fprintf(stderr, "Failed to validate array field #%zu\n", i); - goto end; - } + if (sequence->elements) { + g_ptr_array_free(sequence->elements, TRUE); } -end: - return ret; + g_free(field); } static -int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) +void bt_field_string_destroy(struct bt_field *field) { - size_t i; - int ret = 0; - struct bt_ctf_field_sequence *sequence; + struct bt_field_string *string = (void *) field; - if (!field) { - ret = -1; - goto end; + BT_LOGD("Destroying string field object: addr=%p", field); + BT_ASSERT(field); + bt_field_finalize(field); + + if (string->buf) { + g_array_free(string->buf, TRUE); } - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - for (i = 0; i < sequence->elements->len; i++) { - ret = bt_ctf_field_validate(sequence->elements->pdata[i]); - if (ret) { - fprintf(stderr, "Failed to validate sequence field #%zu\n", i); - goto end; - } + g_free(field); +} + +BT_HIDDEN +void bt_field_destroy_recursive(struct bt_field *field) +{ + if (!field) { + return; } -end: - return ret; + + BT_ASSERT(bt_field_type_has_known_id((void *) field->type)); + field_destroy_funcs[field->type->id](field); +} + +static inline +void bt_field_initialize(struct bt_field *field, + struct bt_field_type *ft, + struct bt_field_methods *methods) +{ + BT_ASSERT(field); + BT_ASSERT(ft); + bt_object_init_unique(&field->base); + field->methods = methods; + field->type = bt_get(ft); } static -int bt_ctf_field_generic_reset(struct bt_ctf_field *field) +struct bt_field *bt_field_integer_create(struct bt_field_type *type) { - int ret = 0; + struct bt_field_integer *integer = + g_new0(struct bt_field_integer, 1); - if (!field) { - ret = -1; - goto end; + BT_LOGD("Creating integer field object: ft-addr=%p", type); + + if (integer) { + bt_field_initialize((void *) integer, (void *) type, + &bt_field_integer_methods); + BT_LOGD("Created integer field object: addr=%p, ft-addr=%p", + integer, type); + } else { + BT_LOGE_STR("Failed to allocate one integer field."); } - field->payload_set = 0; -end: - return ret; + return (void *) integer; } static -int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field) +struct bt_field *bt_field_enumeration_create(struct bt_field_type *type) { - int ret = 0; - struct bt_ctf_field_enumeration *enumeration; + struct bt_field_enumeration *enumeration = g_new0( + struct bt_field_enumeration, 1); - if (!field) { - ret = -1; - goto end; - } + BT_LOGD("Creating enumeration field object: ft-addr=%p", type); - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - goto end; + if (enumeration) { + bt_field_initialize((void *) enumeration, + (void *) type, &bt_field_enumeration_methods); + BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p", + enumeration, type); + } else { + BT_LOGE_STR("Failed to allocate one enumeration field."); } - ret = bt_ctf_field_reset(enumeration->payload); -end: - return ret; + return (void *) enumeration; } static -int bt_ctf_field_structure_reset(struct bt_ctf_field *field) +struct bt_field *bt_field_floating_point_create(struct bt_field_type *type) { - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure; + struct bt_field_floating_point *floating_point; - if (!field) { - ret = -1; - goto end; + BT_LOGD("Creating floating point number field object: ft-addr=%p", type); + floating_point = g_new0(struct bt_field_floating_point, 1); + + if (floating_point) { + bt_field_initialize((void *) floating_point, + (void *) type, &bt_field_floating_point_methods); + BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p", + floating_point, type); + } else { + BT_LOGE_STR("Failed to allocate one floating point number field."); } - structure = container_of(field, struct bt_ctf_field_structure, parent); - for (i = 0; i < structure->fields->len; i++) { - struct bt_ctf_field *member = structure->fields->pdata[i]; + return (void *) floating_point; +} - if (!member) { - /* - * Structure members are lazily initialized; skip if - * this member has not been allocated yet. - */ - continue; - } +static inline +int bt_field_structure_initialize(struct bt_field *field, + struct bt_field_type *type, + struct bt_field_methods *methods, + bt_field_create_func field_create_func, + GDestroyNotify field_release_func) +{ + int ret = 0; + struct bt_field_type_structure *structure_type = (void *) type; + struct bt_field_structure *structure = (void *) field; + size_t i; - ret = bt_ctf_field_reset(member); - if (ret) { + BT_LOGD("Initializing structure field object: ft-addr=%p", type); + bt_field_initialize(field, type, methods); + structure->fields = g_ptr_array_new_with_free_func(field_release_func); + g_ptr_array_set_size(structure->fields, structure_type->fields->len); + + /* Create all fields contained in the structure field. */ + for (i = 0; i < structure_type->fields->len; i++) { + struct bt_field *field; + struct bt_field_type_structure_field *struct_field = + BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX( + structure_type, i); + field = field_create_func(struct_field->type); + if (!field) { + BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu", + g_quark_to_string(struct_field->name), i); + ret = -1; goto end; } + + g_ptr_array_index(structure->fields, i) = field; } + + BT_LOGD("Initialized structure field object: addr=%p, ft-addr=%p", + field, type); + end: return ret; } static -int bt_ctf_field_variant_reset(struct bt_ctf_field *field) +struct bt_field *bt_field_structure_create(struct bt_field_type *type) { - int ret = 0; - struct bt_ctf_field_variant *variant; + struct bt_field_structure *structure = g_new0( + struct bt_field_structure, 1); + int iret; - if (!field) { - ret = -1; + BT_LOGD("Creating structure field object: ft-addr=%p", type); + + if (!structure) { + BT_LOGE_STR("Failed to allocate one structure field."); goto end; } - variant = container_of(field, struct bt_ctf_field_variant, parent); - if (variant->payload) { - ret = bt_ctf_field_reset(variant->payload); + iret = bt_field_structure_initialize((void *) structure, + (void *) type, &bt_field_structure_methods, + (bt_field_create_func) bt_field_create_recursive, + (GDestroyNotify) bt_field_destroy_recursive); + if (iret) { + BT_PUT(structure); + goto end; } + + BT_LOGD("Created structure field object: addr=%p, ft-addr=%p", + structure, type); + end: - return ret; + return (void *) structure; } -static -int bt_ctf_field_array_reset(struct bt_ctf_field *field) +static inline +int bt_field_variant_initialize(struct bt_field *field, + struct bt_field_type *type, + struct bt_field_methods *methods, + bt_field_create_func field_create_func, + GDestroyNotify field_release_func) { - size_t i; int ret = 0; - struct bt_ctf_field_array *array; + struct bt_field_type_variant *variant_type = (void *) type; + struct bt_field_variant *variant = (void *) field; + size_t i; - if (!field) { - ret = -1; + BT_LOGD("Initializing variant field object: ft-addr=%p", type); + bt_field_initialize(field, type, methods); + ret = bt_field_type_variant_update_choices(type); + if (ret) { + BT_LOGE("Cannot update variant field type choices: " + "ret=%d", ret); goto end; } - array = container_of(field, struct bt_ctf_field_array, parent); - for (i = 0; i < array->elements->len; i++) { - struct bt_ctf_field *member = array->elements->pdata[i]; + variant->fields = g_ptr_array_new_with_free_func(field_release_func); + g_ptr_array_set_size(variant->fields, variant_type->choices->len); - if (!member) { - /* - * Array elements are lazily initialized; skip if - * this member has not been allocated yet. - */ - continue; - } + /* Create all fields contained in the variant field. */ + for (i = 0; i < variant_type->choices->len; i++) { + struct bt_field *field; + struct bt_field_type_variant_choice *var_choice = + BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX( + variant_type, i); - ret = bt_ctf_field_reset(member); - if (ret) { + field = field_create_func(var_choice->type); + if (!field) { + BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu", + g_quark_to_string(var_choice->name), i); + ret = -1; goto end; } + + g_ptr_array_index(variant->fields, i) = field; } + + BT_LOGD("Initialized variant field object: addr=%p, ft-addr=%p", + field, type); + end: return ret; } -static -int bt_ctf_field_sequence_reset(struct bt_ctf_field *field) +static inline +int bt_field_string_initialize(struct bt_field *field, + struct bt_field_type *type, + struct bt_field_methods *methods) { - size_t i; int ret = 0; - struct bt_ctf_field_sequence *sequence; + struct bt_field_string *string = (void *) field; - if (!field) { + BT_LOGD("Initializing string field object: ft-addr=%p", type); + bt_field_initialize(field, type, methods); + string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1); + if (!string->buf) { ret = -1; goto end; } - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - for (i = 0; i < sequence->elements->len; i++) { - struct bt_ctf_field *member = sequence->elements->pdata[i]; - - if (!member) { - /* - * Sequence elements are lazily initialized; skip if - * this member has not been allocated yet. - */ - continue; - } + g_array_index(string->buf, char, 0) = '\0'; + BT_LOGD("Initialized string field object: addr=%p, ft-addr=%p", + field, type); - ret = bt_ctf_field_reset(member); - if (ret) { - goto end; - } - } end: return ret; } static -int bt_ctf_field_string_reset(struct bt_ctf_field *field) +struct bt_field *bt_field_variant_create(struct bt_field_type *type) { - int ret = 0; - struct bt_ctf_field_string *string; + struct bt_field_variant *variant = g_new0( + struct bt_field_variant, 1); + int iret; - if (!field) { - ret = -1; + BT_LOGD("Creating variant field object: ft-addr=%p", type); + + if (!variant) { + BT_LOGE_STR("Failed to allocate one variant field."); goto end; } - ret = bt_ctf_field_generic_reset(field); - if (ret) { + iret = bt_field_variant_initialize((void *) variant, + (void *) type, &bt_field_variant_methods, + (bt_field_create_func) bt_field_create_recursive, + (GDestroyNotify) bt_field_destroy_recursive); + if (iret) { + BT_PUT(variant); goto end; } - string = container_of(field, struct bt_ctf_field_string, parent); - if (string->payload) { - g_string_truncate(string->payload, 0); - } + BT_LOGD("Created variant field object: addr=%p, ft-addr=%p", + variant, type); + end: - return ret; + return (void *) variant; } -static -int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +static inline +int bt_field_array_initialize(struct bt_field *field, + struct bt_field_type *type, + struct bt_field_methods *methods, + bt_field_create_func field_create_func, + GDestroyNotify field_destroy_func) { + struct bt_field_type_array *array_type = (void *) type; + struct bt_field_array *array = (void *) field; + unsigned int array_length; int ret = 0; - struct bt_ctf_field_integer *integer = container_of(field, - struct bt_ctf_field_integer, parent); + uint64_t i; - if (!bt_ctf_field_generic_is_set(field)) { + BT_LOGD("Initializing array field object: ft-addr=%p", type); + BT_ASSERT(type); + bt_field_initialize(field, type, methods); + array_length = array_type->length; + array->elements = g_ptr_array_sized_new(array_length); + if (!array->elements) { ret = -1; goto end; } -retry: - ret = bt_ctf_field_integer_write(integer, pos, native_byte_order); - if (ret == -EFAULT) { - /* - * The field is too large to fit in the current packet's - * remaining space. Bump the packet size and retry. - */ - ret = increase_packet_size(pos); - if (ret) { + + g_ptr_array_set_free_func(array->elements, field_destroy_func); + g_ptr_array_set_size(array->elements, array_length); + + for (i = 0; i < array_length; i++) { + array->elements->pdata[i] = field_create_func( + array_type->element_ft); + if (!array->elements->pdata[i]) { + ret = -1; goto end; } - goto retry; } + + BT_LOGD("Initialized array field object: addr=%p, ft-addr=%p", + field, type); + end: return ret; } static -int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +struct bt_field *bt_field_array_create(struct bt_field_type *type) { - struct bt_ctf_field_enumeration *enumeration = container_of( - field, struct bt_ctf_field_enumeration, parent); + struct bt_field_array *array = + g_new0(struct bt_field_array, 1); + int ret; + + BT_LOGD("Creating array field object: ft-addr=%p", type); + BT_ASSERT(type); + + if (!array) { + BT_LOGE_STR("Failed to allocate one array field."); + goto end; + } + + ret = bt_field_array_initialize((void *) array, + (void *) type, &bt_field_array_methods, + (bt_field_create_func) bt_field_create_recursive, + (GDestroyNotify) bt_field_destroy_recursive); + if (ret) { + BT_PUT(array); + goto end; + } - return bt_ctf_field_serialize(enumeration->payload, pos, - native_byte_order); + BT_LOGD("Created array field object: addr=%p, ft-addr=%p", + array, type); + +end: + return (void *) array; } -static -int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +static inline +int bt_field_sequence_initialize(struct bt_field *field, + struct bt_field_type *type, + struct bt_field_methods *methods, + GDestroyNotify field_destroy_func) { + struct bt_field_sequence *sequence = (void *) field; int ret = 0; - struct bt_ctf_field_floating_point *floating_point = container_of(field, - struct bt_ctf_field_floating_point, parent); - if (!bt_ctf_field_generic_is_set(field)) { + BT_LOGD("Initializing sequence field object: ft-addr=%p", type); + BT_ASSERT(type); + bt_field_initialize(field, type, methods); + sequence->elements = g_ptr_array_new(); + if (!sequence->elements) { ret = -1; goto end; } -retry: - ret = bt_ctf_field_floating_point_write(floating_point, pos, - native_byte_order); - if (ret == -EFAULT) { - /* - * The field is too large to fit in the current packet's - * remaining space. Bump the packet size and retry. - */ - ret = increase_packet_size(pos); - if (ret) { - goto end; - } - goto retry; - } + + g_ptr_array_set_free_func(sequence->elements, field_destroy_func); + BT_LOGD("Initialized sequence field object: addr=%p, ft-addr=%p", + field, type); + end: return ret; } static -int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +struct bt_field *bt_field_sequence_create(struct bt_field_type *type) { - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure = container_of( - field, struct bt_ctf_field_structure, parent); + struct bt_field_sequence *sequence = + g_new0(struct bt_field_sequence, 1); + int ret; - while (!bt_ctf_stream_pos_access_ok(pos, - offset_align(pos->offset, field->type->alignment))) { - ret = increase_packet_size(pos); - if (ret) { - goto end; - } + BT_LOGD("Creating sequence field object: ft-addr=%p", type); + BT_ASSERT(type); + + if (!sequence) { + BT_LOGE_STR("Failed to allocate one sequence field."); + goto end; } - if (!bt_ctf_stream_pos_align(pos, field->type->alignment)) { - ret = -1; + ret = bt_field_sequence_initialize((void *) sequence, + (void *) type, &bt_field_sequence_methods, + (GDestroyNotify) bt_field_destroy_recursive); + if (ret) { + BT_PUT(sequence); goto end; } - for (i = 0; i < structure->fields->len; i++) { - struct bt_ctf_field *member = g_ptr_array_index( - structure->fields, i); + BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p", + sequence, type); - ret = bt_ctf_field_serialize(member, pos, native_byte_order); - if (ret) { - const char *name; - struct bt_ctf_field_type *structure_type = - bt_ctf_field_get_type(field); - - (void) bt_ctf_field_type_structure_get_field( - structure_type, &name, NULL, i); - fprintf(stderr, "Field %s failed to serialize\n", - name ? name : "NULL"); - bt_put(structure_type); - break; - } - } end: - return ret; + return (void *) sequence; } static -int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +struct bt_field *bt_field_string_create(struct bt_field_type *type) { - struct bt_ctf_field_variant *variant = container_of( - field, struct bt_ctf_field_variant, parent); + struct bt_field_string *string = g_new0( + struct bt_field_string, 1); - return bt_ctf_field_serialize(variant->payload, pos, - native_byte_order); + BT_LOGD("Creating string field object: ft-addr=%p", type); + + if (string) { + bt_field_string_initialize((void *) string, + (void *) type, &bt_field_string_methods); + BT_LOGD("Created string field object: addr=%p, ft-addr=%p", + string, type); + } else { + BT_LOGE_STR("Failed to allocate one string field."); + } + + return (void *) string; } static -int bt_ctf_field_array_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +int bt_field_generic_validate(struct bt_field *field) { - size_t i; + return (field && field->payload_set) ? 0 : -1; +} + +static +int bt_field_structure_validate_recursive(struct bt_field *field) +{ + int64_t i; int ret = 0; - struct bt_ctf_field_array *array = container_of( - field, struct bt_ctf_field_array, parent); + struct bt_field_structure *structure = (void *) field; + + BT_ASSERT(field); + + for (i = 0; i < structure->fields->len; i++) { + ret = bt_field_validate_recursive( + (void *) structure->fields->pdata[i]); - for (i = 0; i < array->elements->len; i++) { - ret = bt_ctf_field_serialize( - g_ptr_array_index(array->elements, i), pos, - native_byte_order); if (ret) { - fprintf(stderr, "Failed to serialize array element #%zu\n", i); + int this_ret; + const char *name; + + this_ret = bt_field_type_structure_borrow_field_by_index( + field->type, &name, NULL, i); + BT_ASSERT(this_ret == 0); + BT_ASSERT_PRE_MSG("Invalid structure field's field: " + "%![struct-field-]+f, field-name=\"%s\", " + "index=%" PRId64 ", %![field-]+f", + field, name, i, structure->fields->pdata[i]); goto end; } } + end: return ret; } static -int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +int bt_field_variant_validate_recursive(struct bt_field *field) { - size_t i; int ret = 0; - struct bt_ctf_field_sequence *sequence = container_of( - field, struct bt_ctf_field_sequence, parent); + struct bt_field_variant *variant = (void *) field; - for (i = 0; i < sequence->elements->len; i++) { - ret = bt_ctf_field_serialize( - g_ptr_array_index(sequence->elements, i), pos, - native_byte_order); - if (ret) { - fprintf(stderr, "Failed to serialize sequence element #%zu\n", i); - goto end; - } + BT_ASSERT(field); + + if (!variant->current_field) { + ret = -1; + goto end; } + + ret = bt_field_validate_recursive(variant->current_field); + end: return ret; } static -int bt_ctf_field_string_serialize(struct bt_ctf_field *field, - struct bt_ctf_stream_pos *pos, - enum bt_ctf_byte_order native_byte_order) +int bt_field_array_validate_recursive(struct bt_field *field) { - size_t i; + int64_t i; int ret = 0; - struct bt_ctf_field_string *string = container_of(field, - struct bt_ctf_field_string, parent); - struct bt_ctf_field_type *character_type = - get_field_type(FIELD_TYPE_ALIAS_UINT8_T); - struct bt_ctf_field *character = bt_ctf_field_create(character_type); - - for (i = 0; i < string->payload->len + 1; i++) { - ret = bt_ctf_field_unsigned_integer_set_value(character, - (uint64_t) string->payload->str[i]); - if (ret) { - goto end; - } + struct bt_field_array *array = (void *) field; + + BT_ASSERT(field); - ret = bt_ctf_field_integer_serialize(character, pos, - native_byte_order); + for (i = 0; i < array->elements->len; i++) { + ret = bt_field_validate_recursive((void *) array->elements->pdata[i]); if (ret) { + BT_ASSERT_PRE_MSG("Invalid array field's element field: " + "%![array-field-]+f, " PRId64 ", " + "%![elem-field-]+f", + field, i, array->elements->pdata[i]); goto end; } } + end: - bt_put(character); - bt_put(character_type); return ret; } static -int bt_ctf_field_integer_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - struct bt_ctf_field_integer *integer_src, *integer_dst; - - integer_src = container_of(src, struct bt_ctf_field_integer, parent); - integer_dst = container_of(dst, struct bt_ctf_field_integer, parent); - integer_dst->payload = integer_src->payload; - return 0; -} - -static -int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +int bt_field_sequence_validate_recursive(struct bt_field *field) { + size_t i; int ret = 0; - struct bt_ctf_field_enumeration *enum_src, *enum_dst; + struct bt_field_sequence *sequence = (void *) field; - enum_src = container_of(src, struct bt_ctf_field_enumeration, parent); - enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent); + BT_ASSERT(field); - if (enum_src->payload) { - enum_dst->payload = bt_ctf_field_copy(enum_src->payload); - if (!enum_dst->payload) { - ret = -1; + for (i = 0; i < sequence->elements->len; i++) { + ret = bt_field_validate_recursive( + (void *) sequence->elements->pdata[i]); + if (ret) { + BT_ASSERT_PRE_MSG("Invalid sequence field's element field: " + "%![seq-field-]+f, " PRId64 ", " + "%![elem-field-]+f", + field, i, sequence->elements->pdata[i]); goto end; } } @@ -2106,427 +1379,283 @@ end: } static -int bt_ctf_field_floating_point_copy( - struct bt_ctf_field *src, struct bt_ctf_field *dst) +void bt_field_generic_reset(struct bt_field *field) { - struct bt_ctf_field_floating_point *float_src, *float_dst; - - float_src = container_of(src, struct bt_ctf_field_floating_point, - parent); - float_dst = container_of(dst, struct bt_ctf_field_floating_point, - parent); - float_dst->payload = float_src->payload; - return 0; + BT_ASSERT(field); + field->payload_set = false; } static -int bt_ctf_field_structure_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +void bt_field_structure_reset_recursive(struct bt_field *field) { - int ret = 0, i; - struct bt_ctf_field_structure *struct_src, *struct_dst; - - struct_src = container_of(src, struct bt_ctf_field_structure, parent); - struct_dst = container_of(dst, struct bt_ctf_field_structure, parent); - - /* This field_name_to_index HT is owned by the structure field type */ - struct_dst->field_name_to_index = struct_src->field_name_to_index; - g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len); + int64_t i; + struct bt_field_structure *structure = (void *) field; - for (i = 0; i < struct_src->fields->len; i++) { - struct bt_ctf_field *field = - g_ptr_array_index(struct_src->fields, i); - struct bt_ctf_field *field_copy = NULL; + BT_ASSERT(field); - if (field) { - field_copy = bt_ctf_field_copy(field); + for (i = 0; i < structure->fields->len; i++) { + struct bt_field *member = structure->fields->pdata[i]; - if (!field_copy) { - ret = -1; - goto end; - } + if (!member) { + /* + * Structure members are lazily initialized; + * skip if this member has not been allocated + * yet. + */ + continue; } - g_ptr_array_index(struct_dst->fields, i) = field_copy; + bt_field_reset_recursive(member); } -end: - return ret; } static -int bt_ctf_field_variant_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +void bt_field_variant_reset_recursive(struct bt_field *field) { - int ret = 0; - struct bt_ctf_field_variant *variant_src, *variant_dst; - - variant_src = container_of(src, struct bt_ctf_field_variant, parent); - variant_dst = container_of(dst, struct bt_ctf_field_variant, parent); + struct bt_field_variant *variant = (void *) field; - if (variant_src->tag) { - variant_dst->tag = bt_ctf_field_copy(variant_src->tag); - if (!variant_dst->tag) { - ret = -1; - goto end; - } - } - if (variant_src->payload) { - variant_dst->payload = bt_ctf_field_copy(variant_src->payload); - if (!variant_dst->payload) { - ret = -1; - goto end; - } - } -end: - return ret; + BT_ASSERT(field); + variant->current_field = NULL; } static -int bt_ctf_field_array_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +void bt_field_array_reset_recursive(struct bt_field *field) { - int ret = 0, i; - struct bt_ctf_field_array *array_src, *array_dst; - - array_src = container_of(src, struct bt_ctf_field_array, parent); - array_dst = container_of(dst, struct bt_ctf_field_array, parent); + size_t i; + struct bt_field_array *array = (void *) field; - g_ptr_array_set_size(array_dst->elements, array_src->elements->len); - for (i = 0; i < array_src->elements->len; i++) { - struct bt_ctf_field *field = - g_ptr_array_index(array_src->elements, i); - struct bt_ctf_field *field_copy = NULL; + BT_ASSERT(field); - if (field) { - field_copy = bt_ctf_field_copy(field); + for (i = 0; i < array->elements->len; i++) { + struct bt_field *member = array->elements->pdata[i]; - if (!field_copy) { - ret = -1; - goto end; - } + if (!member) { + /* + * Array elements are lazily initialized; skip + * if this member has not been allocated yet. + */ + continue; } - g_ptr_array_index(array_dst->elements, i) = field_copy; + bt_field_reset_recursive(member); } -end: - return ret; } static -int bt_ctf_field_sequence_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +void bt_field_sequence_reset_recursive(struct bt_field *field) { - int ret = 0, i; - struct bt_ctf_field_sequence *sequence_src, *sequence_dst; - struct bt_ctf_field *src_length; - struct bt_ctf_field *dst_length; - - sequence_src = container_of(src, struct bt_ctf_field_sequence, parent); - sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent); - - src_length = bt_ctf_field_sequence_get_length(src); - - if (!src_length) { - /* no length set yet: keep destination sequence empty */ - goto end; - } - - /* copy source length */ - dst_length = bt_ctf_field_copy(src_length); - bt_put(src_length); - - if (!dst_length) { - ret = -1; - goto end; - } - - /* this will initialize the destination sequence's internal array */ - ret = bt_ctf_field_sequence_set_length(dst, dst_length); - bt_put(dst_length); - - if (ret) { - goto end; - } - - assert(sequence_dst->elements->len == sequence_src->elements->len); + struct bt_field_sequence *sequence = (void *) field; + uint64_t i; - for (i = 0; i < sequence_src->elements->len; i++) { - struct bt_ctf_field *field = - g_ptr_array_index(sequence_src->elements, i); - struct bt_ctf_field *field_copy = NULL; + BT_ASSERT(field); - if (field) { - field_copy = bt_ctf_field_copy(field); - - if (!field_copy) { - ret = -1; - goto end; - } + for (i = 0; i < sequence->elements->len; i++) { + if (sequence->elements->pdata[i]) { + bt_field_reset_recursive( + sequence->elements->pdata[i]); } - - g_ptr_array_index(sequence_dst->elements, i) = field_copy; } -end: - return ret; + + sequence->length = 0; } static -int bt_ctf_field_string_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) +void bt_field_generic_set_is_frozen(struct bt_field *field, + bool is_frozen) { - int ret = 0; - struct bt_ctf_field_string *string_src, *string_dst; - - string_src = container_of(src, struct bt_ctf_field_string, parent); - string_dst = container_of(dst, struct bt_ctf_field_string, parent); - - if (string_src->payload) { - string_dst->payload = g_string_new(string_src->payload->str); - if (!string_dst->payload) { - ret = -1; - goto end; - } - } -end: - return ret; + field->frozen = is_frozen; } static -int increase_packet_size(struct bt_ctf_stream_pos *pos) +void bt_field_structure_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen) { - int ret; + uint64_t i; + struct bt_field_structure *structure_field = (void *) field; - assert(pos); - ret = munmap_align(pos->base_mma); - if (ret) { - goto end; - } + BT_LOGD("Freezing structure field object: addr=%p", field); - pos->packet_size += PACKET_LEN_INCREMENT; - do { - ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, - pos->packet_size / CHAR_BIT); - } while (ret == EINTR); - if (ret) { - errno = EINTR; - ret = -1; - goto end; - } + for (i = 0; i < structure_field->fields->len; i++) { + struct bt_field *struct_field = + g_ptr_array_index(structure_field->fields, i); - pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, - pos->flags, pos->fd, pos->mmap_offset); - if (pos->base_mma == MAP_FAILED) { - ret = -1; + BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64, + struct_field, i); + bt_field_set_is_frozen_recursive(struct_field, + is_frozen); } -end: - return ret; -} -static -void generic_field_freeze(struct bt_ctf_field *field) -{ - field->frozen = 1; + bt_field_generic_set_is_frozen(field, is_frozen); } static -void bt_ctf_field_enumeration_freeze(struct bt_ctf_field *field) +void bt_field_variant_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen) { - struct bt_ctf_field_enumeration *enum_field = - container_of(field, struct bt_ctf_field_enumeration, parent); + uint64_t i; + struct bt_field_variant *variant_field = (void *) field; - bt_ctf_field_freeze(enum_field->payload); - generic_field_freeze(field); -} - -static -void bt_ctf_field_structure_freeze(struct bt_ctf_field *field) -{ - int i; - struct bt_ctf_field_structure *structure_field = - container_of(field, struct bt_ctf_field_structure, parent); + BT_LOGD("Freezing variant field object: addr=%p", field); - for (i = 0; i < structure_field->fields->len; i++) { - struct bt_ctf_field *field = - g_ptr_array_index(structure_field->fields, i); + for (i = 0; i < variant_field->fields->len; i++) { + struct bt_field *var_field = + g_ptr_array_index(variant_field->fields, i); - bt_ctf_field_freeze(field); + BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64, + var_field, i); + bt_field_set_is_frozen_recursive(var_field, is_frozen); } - generic_field_freeze(field); + bt_field_generic_set_is_frozen(field, is_frozen); } static -void bt_ctf_field_variant_freeze(struct bt_ctf_field *field) +void bt_field_array_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen) { - struct bt_ctf_field_variant *variant_field = - container_of(field, struct bt_ctf_field_variant, parent); - - bt_ctf_field_freeze(variant_field->tag); - bt_ctf_field_freeze(variant_field->payload); - generic_field_freeze(field); -} + int64_t i; + struct bt_field_array *array_field = (void *) field; -static -void bt_ctf_field_array_freeze(struct bt_ctf_field *field) -{ - int i; - struct bt_ctf_field_array *array_field = - container_of(field, struct bt_ctf_field_array, parent); + BT_LOGD("Freezing array field object: addr=%p", field); for (i = 0; i < array_field->elements->len; i++) { - struct bt_ctf_field *field = + struct bt_field *elem_field = g_ptr_array_index(array_field->elements, i); - bt_ctf_field_freeze(field); + BT_LOGD("Freezing array field object's element field: " + "element-field-addr=%p, index=%" PRId64, + elem_field, i); + bt_field_set_is_frozen_recursive(elem_field, is_frozen); } - generic_field_freeze(field); + bt_field_generic_set_is_frozen(field, is_frozen); } static -void bt_ctf_field_sequence_freeze(struct bt_ctf_field *field) +void bt_field_sequence_set_is_frozen_recursive( + struct bt_field *field, bool is_frozen) { - int i; - struct bt_ctf_field_sequence *sequence_field = - container_of(field, struct bt_ctf_field_sequence, parent); + int64_t i; + struct bt_field_sequence *sequence_field = (void *) field; - bt_ctf_field_freeze(sequence_field->length); + BT_LOGD("Freezing sequence field object: addr=%p", field); - for (i = 0; i < sequence_field->elements->len; i++) { - struct bt_ctf_field *field = + for (i = 0; i < sequence_field->length; i++) { + struct bt_field *elem_field = g_ptr_array_index(sequence_field->elements, i); - bt_ctf_field_freeze(field); + BT_LOGD("Freezing sequence field object's element field: " + "element-field-addr=%p, index=%" PRId64, + elem_field, i); + bt_field_set_is_frozen_recursive(elem_field, is_frozen); } - generic_field_freeze(field); + bt_field_generic_set_is_frozen(field, is_frozen); } BT_HIDDEN -void bt_ctf_field_freeze(struct bt_ctf_field *field) +void _bt_field_set_is_frozen_recursive(struct bt_field *field, + bool is_frozen) { - enum bt_ctf_field_type_id type_id; - if (!field) { goto end; } - type_id = bt_ctf_field_get_type_id(field); - if (type_id <= BT_CTF_FIELD_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - goto end; - } + BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d", + field, is_frozen); + BT_ASSERT(bt_field_type_has_known_id(field->type)); + BT_ASSERT(field->methods->set_is_frozen); + field->methods->set_is_frozen(field, is_frozen); - field_freeze_funcs[type_id](field); end: return; } static -bt_bool bt_ctf_field_generic_is_set(struct bt_ctf_field *field) +bt_bool bt_field_generic_is_set(struct bt_field *field) { return field && field->payload_set; } static -bt_bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field *field) -{ - bt_bool is_set = BT_FALSE; - struct bt_ctf_field_enumeration *enumeration; - - if (!field) { - goto end; - } - - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - goto end; - } - - is_set = bt_ctf_field_is_set(enumeration->payload); -end: - return is_set; -} - -static -bt_bool bt_ctf_field_structure_is_set(struct bt_ctf_field *field) +bt_bool bt_field_structure_is_set_recursive( + struct bt_field *field) { bt_bool is_set = BT_FALSE; size_t i; - struct bt_ctf_field_structure *structure; + struct bt_field_structure *structure = (void *) field; - if (!field) { - goto end; - } + BT_ASSERT(field); - structure = container_of(field, struct bt_ctf_field_structure, parent); for (i = 0; i < structure->fields->len; i++) { - is_set = bt_ctf_field_is_set(structure->fields->pdata[i]); + is_set = bt_field_is_set_recursive( + structure->fields->pdata[i]); if (!is_set) { goto end; } } + end: return is_set; } static -bt_bool bt_ctf_field_variant_is_set(struct bt_ctf_field *field) +bt_bool bt_field_variant_is_set_recursive(struct bt_field *field) { + struct bt_field_variant *variant = (void *) field; bt_bool is_set = BT_FALSE; - struct bt_ctf_field_variant *variant; - if (!field) { - goto end; + BT_ASSERT(field); + + if (variant->current_field) { + is_set = bt_field_is_set_recursive( + variant->current_field); } - variant = container_of(field, struct bt_ctf_field_variant, parent); - is_set = bt_ctf_field_is_set(variant->payload); -end: return is_set; } static -bt_bool bt_ctf_field_array_is_set(struct bt_ctf_field *field) +bt_bool bt_field_array_is_set_recursive(struct bt_field *field) { size_t i; bt_bool is_set = BT_FALSE; - struct bt_ctf_field_array *array; + struct bt_field_array *array = (void *) field; - if (!field) { - goto end; - } + BT_ASSERT(field); - array = container_of(field, struct bt_ctf_field_array, parent); for (i = 0; i < array->elements->len; i++) { - is_set = bt_ctf_field_is_set(array->elements->pdata[i]); + is_set = bt_field_is_set_recursive(array->elements->pdata[i]); if (!is_set) { goto end; } } + end: return is_set; } static -bt_bool bt_ctf_field_sequence_is_set(struct bt_ctf_field *field) +bt_bool bt_field_sequence_is_set_recursive(struct bt_field *field) { size_t i; bt_bool is_set = BT_FALSE; - struct bt_ctf_field_sequence *sequence; + struct bt_field_sequence *sequence = (void *) field; - if (!field) { + BT_ASSERT(field); + + if (!sequence->elements) { goto end; } - sequence = container_of(field, struct bt_ctf_field_sequence, parent); for (i = 0; i < sequence->elements->len; i++) { - is_set = bt_ctf_field_validate(sequence->elements->pdata[i]); + is_set = bt_field_is_set_recursive( + sequence->elements->pdata[i]); if (!is_set) { goto end; } } + end: return is_set; }