- 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_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_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;
-
- if (!field || bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_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;
-}
-
-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_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_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_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_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_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,