- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_STRUCT) {
- BT_LOGW("Invalid parameter: field's type is not a structure field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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)) {
- BT_LOGW("Invalid parameter: field type of field to set is different from the expected field type: "
- "struct-field-addr=%p, field-addr=%p, "
- "field-ft-addr=%p, expected-ft-addr=%p",
- field, value, value->type, expected_field_type);
- ret = -1;
- goto end;
- }
-
- if (!g_hash_table_lookup_extended(structure->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, "
- "field-ft-addr=%p, name=\"%s\"",
- field, field->type, value->type, name);
- ret = -1;
- 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;
-}
-
-struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
- uint64_t index)
-{
- struct bt_ctf_field *new_field = NULL;
- struct bt_ctf_field_type *field_type = NULL;
- struct bt_ctf_field_array *array;
-
- if (!field) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_ARRAY) {
- BT_LOGW("Invalid parameter: field's type is not an array field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- goto end;
- }
-
- array = container_of(field, struct bt_ctf_field_array, parent);
- if (index >= array->elements->len) {
- BT_LOGW("Invalid parameter: index is out of bounds: "
- "addr=%p, index=%" PRIu64 ", count=%u",
- field, 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;
- }
-
- /* We don't want to modify this field if it's frozen */
- if (field->frozen) {
- /*
- * Not logging a warning here because the user could
- * legitimately check if a array field is set with
- * this function: if the preconditions are satisfied,
- * a NULL return value means this.
- */
- BT_LOGV("Not creating a field because array field is frozen: "
- "array-field-addr=%p, index=%" PRIu64, field, index);
- 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;
-}
-
-struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
- uint64_t index)
-{
- struct bt_ctf_field *new_field = NULL;
- struct bt_ctf_field_type *field_type = NULL;
- struct bt_ctf_field_sequence *sequence;
-
- if (!field) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
- BT_LOGW("Invalid parameter: field's type is not a sequence field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- goto end;
- }
-
- sequence = container_of(field, struct bt_ctf_field_sequence, parent);
- if (!sequence->elements) {
- BT_LOGV("Sequence field's elements do not exist: addr=%p",
- field);
- goto end;
- }
-
- if (index >= sequence->elements->len) {
- BT_LOGW("Invalid parameter: index is out of bounds: "
- "addr=%p, index=%" PRIu64 ", count=%u",
- field, index, sequence->elements->len);
- goto end;
- }
-
- 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) {
- /*
- * Not logging a warning here because the user could
- * legitimately check if a sequence field is set with
- * this function: if the preconditions are satisfied,
- * a NULL return value means this.
- */
- BT_LOGV("Not creating a field because sequence field is frozen: "
- "sequence-field-addr=%p, index=%" PRIu64, field, index);
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (!tag_field) {
- BT_LOGW_STR("Invalid parameter: tag field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_VARIANT) {
- BT_LOGW("Invalid parameter: field's type is not a variant field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(tag_field->type) !=
- BT_CTF_FIELD_TYPE_ID_ENUM) {
- BT_LOGW("Invalid parameter: tag field's type is not an enumeration field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", tag_field,
- tag_field->type,
- bt_ctf_field_type_id_string(tag_field->type->id));
- 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) {
- BT_LOGW("Invalid parameter: tag field is invalid: "
- "variant-field-addr=%p, tag-field-addr=%p",
- field, tag_field);
- 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) {
- /*
- * Not logging a warning here because the user could
- * legitimately check if a variant field is set with
- * this function: if the preconditions are satisfied,
- * a NULL return value means this.
- */
- BT_LOGV("Not creating a field because variant field is frozen: "
- "variant-field-addr=%p, tag-field-addr=%p",
- field, tag_field);
- goto end;
- }
-
- field_type = bt_ctf_field_type_variant_get_field_type_signed(
- variant_type, tag_enum_value);
- if (!field_type) {
- BT_LOGW("Cannot get variant field type's field: "
- "variant-field-addr=%p, variant-ft-addr=%p, "
- "tag-value-signed=%" PRId64,
- field, variant_type, tag_enum_value);
- goto end;
- }
-
- new_field = bt_ctf_field_create(field_type);
- if (!new_field) {
- BT_LOGW("Cannot create field: "
- "variant-field-addr=%p, variant-ft-addr=%p, "
- "field-ft-addr=%p", field, field->type, field_type);
- 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_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(variant_field->type) !=
- BT_CTF_FIELD_TYPE_ID_VARIANT) {
- BT_LOGW("Invalid parameter: field's type is not a variant field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", variant_field,
- variant_field->type,
- bt_ctf_field_type_id_string(variant_field->type->id));
- 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_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(variant_field->type) !=
- BT_CTF_FIELD_TYPE_ID_VARIANT) {
- BT_LOGW("Invalid parameter: field's type is not a variant field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", variant_field,
- variant_field->type,
- bt_ctf_field_type_id_string(variant_field->type->id));
- 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_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_ENUM) {
- BT_LOGW("Invalid parameter: field's type is not an enumeration field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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) {
- /*
- * Not logging a warning here because the user
- * could legitimately check if an enumeration's
- * container field is set with this function: if
- * the preconditions are satisfied, a NULL
- * return value means this.
- */
- BT_LOGV("Not creating a field because enumeration field is frozen: "
- "enum-field-addr=%p", field);
- 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) {
- BT_LOGW("Invalid parameter: enumeration field has no container field: "
- "addr=%p", field);
- goto end;
- }
-
- container_type = bt_ctf_field_get_type(container);
- assert(container_type);
- 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) {
- BT_LOGW("Cannot get value from signed enumeration field's payload field: "
- "enum-field-addr=%p, payload-field-addr=%p",
- field, container);
- 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) {
- BT_LOGW("Cannot get value from unsigned enumeration field's payload field: "
- "enum-field-addr=%p, payload-field-addr=%p",
- field, container);
- 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);
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!field->payload_set) {
- BT_LOGV("Field's payload is not set: addr=%p", field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_INTEGER) {
- BT_LOGW("Invalid parameter: field's type is not an integer field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- ret = -1;
- goto end;
- }
-
- integer_type = container_of(field->type,
- struct bt_ctf_field_type_integer, parent);
- if (!integer_type->is_signed) {
- BT_LOGW("Invalid parameter: integer field's type is not signed: "
- "field-addr=%p, ft-addr=%p", field, field->type);
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_INTEGER) {
- BT_LOGW("Invalid parameter: field's type is not an integer field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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) {
- BT_LOGW("Invalid parameter: integer field's type is not signed: "
- "field-addr=%p, ft-addr=%p", field, field->type);
- 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) {
- BT_LOGW("Invalid parameter: value is out of bounds: "
- "addr=%p, value=%" PRId64 ", "
- "min-value=%" PRId64 ", max-value=%" PRId64,
- field, value, min_value, max_value);
- ret = -1;
- goto end;
- }
-
- integer->payload.signd = value;
- integer->parent.payload_set = true;
-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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!field->payload_set) {
- BT_LOGV("Field's payload is not set: addr=%p", field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_INTEGER) {
- BT_LOGW("Invalid parameter: field's type is not an integer field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- ret = -1;
- goto end;
- }
-
- integer_type = container_of(field->type,
- struct bt_ctf_field_type_integer, parent);
- if (integer_type->is_signed) {
- BT_LOGW("Invalid parameter: integer field's type is signed: "
- "field-addr=%p, ft-addr=%p", field, field->type);
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_INTEGER) {
- BT_LOGW("Invalid parameter: field's type is not an integer field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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) {
- BT_LOGW("Invalid parameter: integer field's type is signed: "
- "field-addr=%p, ft-addr=%p", field, field->type);
- ret = -1;
- goto end;
- }
-
- size = integer_type->size;
- max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1;
- if (value > max_value) {
- BT_LOGW("Invalid parameter: value is out of bounds: "
- "addr=%p, value=%" PRIu64 ", "
- "min-value=%" PRIu64 ", max-value=%" PRIu64,
- field, value, (uint64_t) 0, max_value);
- ret = -1;
- goto end;
- }
-
- integer->payload.unsignd = value;
- integer->parent.payload_set = true;
-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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!field->payload_set) {
- BT_LOGV("Field's payload is not set: addr=%p", field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_FLOAT) {
- BT_LOGW("Invalid parameter: field's type is not a floating point number field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_FLOAT) {
- BT_LOGW("Invalid parameter: field's type is not a floating point number field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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 = true;
-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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- goto end;
- }
-
- if (!field->payload_set) {
- BT_LOGV("Field's payload is not set: addr=%p", field);
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_STRING) {
- BT_LOGW("Invalid parameter: field's type is not a string field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_STRING) {
- BT_LOGW("Invalid parameter: field's type is not a string field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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 = true;
-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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_STRING) {
- BT_LOGW("Invalid parameter: field's type is not a string field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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 = true;
-
-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) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- ret = -1;
- goto end;
- }
-
- if (!value) {
- BT_LOGW_STR("Invalid parameter: value is NULL.");
- ret = -1;
- goto end;
- }
-
- if (field->frozen) {
- BT_LOGW("Invalid parameter: field is frozen: addr=%p",
- field);
- ret = -1;
- goto end;
- }
-
- if (bt_ctf_field_type_get_type_id(field->type) !=
- BT_CTF_FIELD_TYPE_ID_STRING) {
- BT_LOGW("Invalid parameter: field's type is not a string field type: "
- "field-addr=%p, ft-addr=%p, ft-id=%s", field,
- field->type,
- bt_ctf_field_type_id_string(field->type->id));
- 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 = true;
-
-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) {
- BT_LOGD_STR("Invalid parameter: field is NULL.");
- 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) {
- BT_LOGW("Invalid parameter: unknown field type ID: "
- "addr=%p, ft-addr=%p, ft-id=%d",
- field, field->type, type_id);
- 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) {
- BT_LOGD_STR("Invalid parameter: field is NULL.");
- 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) {
- BT_LOGW("Invalid parameter: unknown field type ID: "
- "addr=%p, ft-addr=%p, ft-id=%d",
- field, field->type, type_id);
- 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;
-
- assert(pos);
-
- if (!field) {
- BT_LOGD_STR("Invalid parameter: field is NULL.");
- 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) {
- BT_LOGW("Invalid parameter: unknown field type ID: "
- "addr=%p, ft-addr=%p, ft-id=%d",
- field, field->type, type_id);
- 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;
- }
-
- 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) {
- BT_LOGW("Invalid parameter: unknown field type ID: "
- "field-addr=%p, ft-addr=%p, ft-id=%d",
- field, field->type, type_id);
- goto end;
- }
-
- is_set = field_is_set_funcs[type_id](field);
-end:
- return is_set;
-}
-
-struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
-{
- int ret;
- struct bt_ctf_field *copy = NULL;
- enum bt_ctf_field_type_id type_id;
-
- if (!field) {
- BT_LOGW_STR("Invalid parameter: field is NULL.");
- 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) {
- BT_LOGW("Invalid parameter: unknown field type ID: "
- "field-addr=%p, ft-addr=%p, ft-id=%d",
- field, field->type, type_id);
- goto end;
- }
-
- copy = bt_ctf_field_create(field->type);
- if (!copy) {
- BT_LOGW("Cannot create field: ft-addr=%p", field->type);
- goto end;
- }
-
- copy->payload_set = field->payload_set;
- ret = field_copy_funcs[type_id](field, copy);
- if (ret) {
- bt_put(copy);
- copy = NULL;
- }
-end:
- return copy;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_integer *integer = g_new0(
- struct bt_ctf_field_integer, 1);
-
- BT_LOGD("Creating integer field object: ft-addr=%p", type);
-
- if (integer) {
- BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
- &integer->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one integer field.");
- }
-
- return integer ? &integer->parent : NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_enumeration_create(
- struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_enumeration *enumeration = g_new0(
- struct bt_ctf_field_enumeration, 1);
-
- BT_LOGD("Creating enumeration field object: ft-addr=%p", type);
-
- if (enumeration) {
- BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
- &enumeration->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one enumeration field.");
- }
-
- return enumeration ? &enumeration->parent : NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_floating_point_create(
- struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_floating_point *floating_point;
-
- BT_LOGD("Creating floating point number field object: ft-addr=%p", type);
- floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
-
- if (floating_point) {
- BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
- &floating_point->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one floating point number field.");
- }
-
- return floating_point ? &floating_point->parent : NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_structure_create(
- struct bt_ctf_field_type *type)
-{
- 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;
-
- BT_LOGD("Creating structure field object: ft-addr=%p", type);
-
- if (!structure) {
- BT_LOGE_STR("Failed to allocate one structure field.");
- 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;
- BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
- field, type);
-end:
- return field;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_variant *variant = g_new0(
- struct bt_ctf_field_variant, 1);
-
- BT_LOGD("Creating variant field object: ft-addr=%p", type);
-
- if (variant) {
- BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
- &variant->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one variant field.");
- }
-
- return variant ? &variant->parent : NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
-{
- 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;
-
- BT_LOGD("Creating array field object: ft-addr=%p", type);
- assert(type);
-
- if (!array) {
- BT_LOGE_STR("Failed to allocate one array field.");
- 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);
- BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
- &array->parent, type);
- return &array->parent;
-error:
- g_free(array);
- return NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_sequence_create(
- struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_sequence *sequence = g_new0(
- struct bt_ctf_field_sequence, 1);
-
- BT_LOGD("Creating sequence field object: ft-addr=%p", type);
-
- if (sequence) {
- BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
- &sequence->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one sequence field.");
- }
-
- return sequence ? &sequence->parent : NULL;
-}
-
-static
-struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
-{
- struct bt_ctf_field_string *string = g_new0(
- struct bt_ctf_field_string, 1);
-
- BT_LOGD("Creating string field object: ft-addr=%p", type);
-
- if (string) {
- BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
- &string->parent, type);
- } else {
- BT_LOGE_STR("Failed to allocate one string field.");
- }
-
- return string ? &string->parent : NULL;
-}
-
-static
-void bt_ctf_field_destroy(struct bt_object *obj)
-{
- struct bt_ctf_field *field;
- struct bt_ctf_field_type *type;
- enum bt_ctf_field_type_id type_id;
-
- field = container_of(obj, struct bt_ctf_field, base);
- type = field->type;
- type_id = bt_ctf_field_type_get_type_id(type);
- assert(type_id > BT_CTF_FIELD_TYPE_ID_UNKNOWN &&
- type_id < BT_CTF_NR_TYPE_IDS);
- field_destroy_funcs[type_id](field);
- BT_LOGD_STR("Putting field's type.");
- bt_put(type);
-}
-
-static
-void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_integer *integer;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying integer field object: addr=%p", field);
- integer = container_of(field, struct bt_ctf_field_integer, parent);
- g_free(integer);
-}
-
-static
-void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_enumeration *enumeration;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying enumeration field object: addr=%p", field);
- enumeration = container_of(field, struct bt_ctf_field_enumeration,
- parent);
- BT_LOGD_STR("Putting payload field.");
- bt_put(enumeration->payload);
- g_free(enumeration);
-}
-
-static
-void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_floating_point *floating_point;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying floating point number field object: addr=%p", field);
- floating_point = container_of(field, struct bt_ctf_field_floating_point,
- parent);
- g_free(floating_point);
-}
-
-static
-void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_structure *structure;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying structure field object: addr=%p", field);
- structure = container_of(field, struct bt_ctf_field_structure, parent);
- g_ptr_array_free(structure->fields, TRUE);
- g_free(structure);
-}
-
-static
-void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_variant *variant;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying variant field object: addr=%p", field);
- variant = container_of(field, struct bt_ctf_field_variant, parent);
- BT_LOGD_STR("Putting tag field.");
- bt_put(variant->tag);
- BT_LOGD_STR("Putting payload field.");
- bt_put(variant->payload);
- g_free(variant);
-}
-
-static
-void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_array *array;
-
- if (!field) {
- return;
- }
-
- BT_LOGD("Destroying array field object: addr=%p", field);
- array = container_of(field, struct bt_ctf_field_array, parent);
- g_ptr_array_free(array->elements, TRUE);
- g_free(array);
-}
-
-static
-void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
-{
- struct bt_ctf_field_sequence *sequence;