From: Philippe Proulx Date: Tue, 9 Feb 2016 00:32:00 +0000 (-0500) Subject: ir: rename event-fields/event-types -> fields/field-types X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=2e33ac5abec88a4be03907091474324dfcc42e23 ir: rename event-fields/event-types -> fields/field-types Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/bindings/python/python-complements.c b/bindings/python/python-complements.c index b916b58c..08b7912f 100644 --- a/bindings/python/python-complements.c +++ b/bindings/python/python-complements.c @@ -26,9 +26,9 @@ */ #include "python-complements.h" -#include -#include -#include +#include +#include +#include #include #include #include diff --git a/formats/ctf/ir/Makefile.am b/formats/ctf/ir/Makefile.am index e9dc96ef..d483ff89 100644 --- a/formats/ctf/ir/Makefile.am +++ b/formats/ctf/ir/Makefile.am @@ -6,8 +6,8 @@ libctf_ir_la_SOURCES = \ attributes.c \ clock.c \ event.c \ - event-fields.c \ - event-types.c \ + fields.c \ + field-types.c \ field-path.c \ stream.c \ stream-class.c \ diff --git a/formats/ctf/ir/event-fields.c b/formats/ctf/ir/event-fields.c deleted file mode 100644 index 91e7e9c4..00000000 --- a/formats/ctf/ir/event-fields.c +++ /dev/null @@ -1,2203 +0,0 @@ -/* - * event-fields.c - * - * Babeltrace CTF IR - Event Fields - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include - -#define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) - -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 *); - -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 *); - -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 *); - -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 *); - -static -int bt_ctf_field_integer_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_structure_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_variant_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_array_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); -static -int bt_ctf_field_string_serialize(struct bt_ctf_field *, - struct ctf_stream_pos *); - -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 *); - -static -int increase_packet_size(struct ctf_stream_pos *pos); - -static -struct bt_ctf_field *(* const field_create_funcs[])( - struct bt_ctf_field_type *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_create, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_create, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_create, -}; - -static -void (* const field_destroy_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_destroy, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_destroy, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_destroy, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_destroy, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_destroy, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_destroy, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_destroy, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_destroy, -}; - -static -int (* const field_validate_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_generic_validate, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_validate, - [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_generic_validate, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_validate, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_validate, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_validate, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_validate, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_generic_validate, -}; - -static -int (* const field_reset_funcs[])(struct bt_ctf_field *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_generic_reset, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_reset, - [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_generic_reset, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_reset, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_reset, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_reset, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_reset, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_reset, -}; - -static -int (* const field_serialize_funcs[])(struct bt_ctf_field *, - struct ctf_stream_pos *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_serialize, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_serialize, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_floating_point_serialize, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_serialize, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_serialize, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_serialize, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_serialize, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_serialize, -}; - -static -int (* const field_copy_funcs[])(struct bt_ctf_field *, - struct bt_ctf_field *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_copy, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_copy, - [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_copy, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_copy, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_copy, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_copy, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_copy, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_copy, -}; - -struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field *field = NULL; - enum bt_ctf_type_id type_id; - int ret; - - if (!type) { - goto error; - } - - type_id = bt_ctf_field_type_get_type_id(type); - if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - goto error; - } - - /* Field class MUST be valid */ - ret = bt_ctf_field_type_validate(type); - - if (ret) { - /* Invalid */ - goto error; - } - - field = field_create_funcs[type_id](type); - if (!field) { - goto error; - } - - /* 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; -} - -void bt_ctf_field_get(struct bt_ctf_field *field) -{ - bt_get(field); -} - -void bt_ctf_field_put(struct bt_ctf_field *field) -{ - bt_put(field); -} - -struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) -{ - struct bt_ctf_field_type *ret = NULL; - - if (!field) { - goto end; - } - - ret = field->type; - bt_get(ret); -end: - return ret; -} - -enum bt_ctf_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) -{ - enum bt_ctf_type_id ret = BT_CTF_TYPE_ID_UNKNOWN; - - if (!field) { - goto end; - } - - ret = bt_ctf_field_type_get_type_id(field->type); -end: - return ret; -} - -int bt_ctf_field_is_integer(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_INTEGER; -} - -int bt_ctf_field_is_floating_point(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_FLOAT; -} - -int bt_ctf_field_is_enumeration(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_ENUM; -} - -int bt_ctf_field_is_string(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_STRING; -} - -int bt_ctf_field_is_structure(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_STRUCT; -} - -int bt_ctf_field_is_array(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_ARRAY; -} - -int bt_ctf_field_is_sequence(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_SEQUENCE; -} - -int bt_ctf_field_is_variant(struct bt_ctf_field *field) -{ - return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_VARIANT; -} - -struct bt_ctf_field *bt_ctf_field_sequence_get_length( - struct bt_ctf_field *field) -{ - struct bt_ctf_field *ret = NULL; - struct bt_ctf_field_sequence *sequence; - - if (!field) { - goto end; - } - - if (bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_TYPE_ID_SEQUENCE) { - goto end; - } - - 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) -{ - 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) { - ret = -1; - goto end; - } - if (bt_ctf_field_type_get_type_id(length_field->type) != - BT_CTF_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->declaration.signedness) { - ret = -1; - goto end; - } - - length = container_of(length_field, struct bt_ctf_field_integer, - parent); - sequence_length = length->definition.value._unsigned; - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - if (sequence->elements) { - g_ptr_array_free(sequence->elements, TRUE); - bt_put(sequence->length); - } - - sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); - if (!sequence->elements) { - ret = -1; - goto end; - } - - 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( - struct bt_ctf_field *field, const char *name) -{ - 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_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; - } - - new_field = bt_ctf_field_create(field_type); - if (!new_field) { - goto error; - } - - structure->fields->pdata[index] = new_field; -end: - bt_get(new_field); -error: - if (field_type) { - bt_put(field_type); - } - return new_field; -} - -struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( - struct bt_ctf_field *field, int index) -{ - 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_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; - } - - /* 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; - } - - structure->fields->pdata[index] = ret_field; -end: - bt_get(ret_field); -error: - bt_put(field_type); - return ret_field; -} - -BT_HIDDEN -int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, - const char *name, struct bt_ctf_field *value) -{ - 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 || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_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; -} - -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_ctf_field_type_get_type_id(field->type) != - BT_CTF_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; - } - - 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_ctf_field_type_get_type_id(field->type) != - BT_CTF_TYPE_ID_SEQUENCE) { - goto end; - } - - sequence = container_of(field, struct bt_ctf_field_sequence, parent); - if (!sequence->elements || sequence->elements->len <= index) { - 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; - } - - 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_TYPE_ID_VARIANT || - bt_ctf_field_type_get_type_id(tag_field->type) != - BT_CTF_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->definition.value._signed; - - /* - * 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->definition.value._signed; - - if (cur_tag_value == tag_enum_value) { - new_field = variant->payload; - bt_get(new_field); - 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_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) { - 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; -} - -const char *bt_ctf_field_enumeration_get_mapping_name( - struct bt_ctf_field *field) -{ - int ret; - const char *name = NULL; - 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 *enumeration_type = 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); - enumeration_type = container_of(field->type, - struct bt_ctf_field_type_enumeration, parent); - - if (!integer_type->declaration.signedness) { - uint64_t value; - ret = bt_ctf_field_unsigned_integer_get_value(container, - &value); - if (ret) { - goto error_put_container_type; - } - - name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned( - enumeration_type, value); - } else { - int64_t value; - ret = bt_ctf_field_signed_integer_get_value(container, - &value); - if (ret) { - goto error_put_container_type; - } - - name = bt_ctf_field_type_enumeration_get_mapping_name_signed( - enumeration_type, value); - } - -error_put_container_type: - bt_put(container_type); -error_put_container: - bt_put(container); -end: - return name; -} - -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->declaration.signedness) { - ret = -1; - goto end; - } - - integer = container_of(field, - struct bt_ctf_field_integer, parent); - *value = integer->definition.value._signed; -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_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->declaration.signedness) { - ret = -1; - goto end; - } - - size = integer_type->declaration.len; - min_value = -((int64_t)1 << (size - 1)); - max_value = ((int64_t)1 << (size - 1)) - 1; - if (value < min_value || value > max_value) { - ret = -1; - goto end; - } - - integer->definition.value._signed = 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->declaration.signedness) { - ret = -1; - goto end; - } - - integer = container_of(field, - struct bt_ctf_field_integer, parent); - *value = integer->definition.value._unsigned; -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_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->declaration.signedness) { - ret = -1; - goto end; - } - - size = integer_type->declaration.len; - max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; - if (value > max_value) { - ret = -1; - goto end; - } - - integer->definition.value._unsigned = 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->definition.value; -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_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); - floating_point->definition.value = 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_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 || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_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 || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_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 || - bt_ctf_field_type_get_type_id(field->type) != - BT_CTF_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_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_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_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_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 ctf_stream_pos *pos) -{ - int ret = 0; - enum bt_ctf_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_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - ret = -1; - goto end; - } - - ret = field_serialize_funcs[type_id](field, pos); -end: - return ret; -} - -struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) -{ - int ret; - struct bt_ctf_field *copy = NULL; - enum bt_ctf_type_id type_id; - - if (!field) { - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(field->type); - if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { - goto end; - } - - copy = bt_ctf_field_create(field->type); - if (!copy) { - 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_type_integer *integer_type = container_of(type, - struct bt_ctf_field_type_integer, parent); - struct bt_ctf_field_integer *integer = g_new0( - struct bt_ctf_field_integer, 1); - - if (integer) { - integer->definition.declaration = &integer_type->declaration; - } - - 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); - - 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; - struct bt_ctf_field_type_floating_point *floating_point_type; - - floating_point = g_new0(struct bt_ctf_field_floating_point, 1); - if (!floating_point) { - goto end; - } - - floating_point_type = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - floating_point->definition.declaration = container_of( - type->declaration, struct declaration_float, p); - - - floating_point->definition.sign = &floating_point->sign; - floating_point->sign.declaration = &floating_point_type->sign; - floating_point->definition.sign->p.declaration = - &floating_point_type->sign.p; - - floating_point->definition.mantissa = &floating_point->mantissa; - floating_point->mantissa.declaration = &floating_point_type->mantissa; - floating_point->definition.mantissa->p.declaration = - &floating_point_type->mantissa.p; - - floating_point->definition.exp = &floating_point->exp; - floating_point->exp.declaration = &floating_point_type->exp; - floating_point->definition.exp->p.declaration = - &floating_point_type->exp.p; - -end: - 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; - - if (!structure) { - 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; -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); - 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; - - 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; -} - -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); - 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); - 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_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); - if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - return; - } - - field_destroy_funcs[type_id](field); - bt_put(type); -} - -static -void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) -{ - struct bt_ctf_field_integer *integer; - - if (!field) { - return; - } - - 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; - } - - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - 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; - } - - 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; - } - - 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; - } - - variant = container_of(field, struct bt_ctf_field_variant, parent); - bt_put(variant->tag); - 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; - } - - 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; - - if (!field) { - return; - } - - 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 -void bt_ctf_field_string_destroy(struct bt_ctf_field *field) -{ - struct bt_ctf_field_string *string; - if (!field) { - return; - } - - string = container_of(field, struct bt_ctf_field_string, parent); - if (string->payload) { - g_string_free(string->payload, TRUE); - } - g_free(string); -} - -static -int bt_ctf_field_generic_validate(struct bt_ctf_field *field) -{ - return (field && field->payload_set) ? 0 : -1; -} - -static -int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) -{ - int ret; - struct bt_ctf_field_enumeration *enumeration; - - if (!field) { - ret = -1; - goto end; - } - - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_validate(enumeration->payload); -end: - return ret; -} - -static -int bt_ctf_field_structure_validate(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure; - - if (!field) { - ret = -1; - goto end; - } - - 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) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_variant_validate(struct bt_ctf_field *field) -{ - int ret = 0; - struct bt_ctf_field_variant *variant; - - if (!field) { - ret = -1; - goto end; - } - - variant = container_of(field, struct bt_ctf_field_variant, parent); - ret = bt_ctf_field_validate(variant->payload); -end: - return ret; -} - -static -int bt_ctf_field_array_validate(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_array *array; - - if (!field) { - ret = -1; - goto end; - } - - 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) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_sequence *sequence; - - if (!field) { - ret = -1; - goto end; - } - - 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) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_generic_reset(struct bt_ctf_field *field) -{ - int ret = 0; - - if (!field) { - ret = -1; - goto end; - } - - field->payload_set = 0; -end: - return ret; -} - -static -int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field) -{ - int ret = 0; - struct bt_ctf_field_enumeration *enumeration; - - if (!field) { - ret = -1; - goto end; - } - - enumeration = container_of(field, struct bt_ctf_field_enumeration, - parent); - if (!enumeration->payload) { - goto end; - } - - ret = bt_ctf_field_reset(enumeration->payload); -end: - return ret; -} - -static -int bt_ctf_field_structure_reset(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure; - - if (!field) { - ret = -1; - goto end; - } - - 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]; - - if (!member) { - /* - * Structure members are lazily initialized; skip if - * this member has not been allocated yet. - */ - continue; - } - - ret = bt_ctf_field_reset(member); - if (ret) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_variant_reset(struct bt_ctf_field *field) -{ - int ret = 0; - struct bt_ctf_field_variant *variant; - - if (!field) { - ret = -1; - goto end; - } - - variant = container_of(field, struct bt_ctf_field_variant, parent); - if (variant->payload) { - ret = bt_ctf_field_reset(variant->payload); - } -end: - return ret; -} - -static -int bt_ctf_field_array_reset(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_array *array; - - if (!field) { - ret = -1; - 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]; - - if (!member) { - /* - * Array elements are lazily initialized; skip if - * this member has not been allocated yet. - */ - continue; - } - - ret = bt_ctf_field_reset(member); - if (ret) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_sequence_reset(struct bt_ctf_field *field) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_sequence *sequence; - - if (!field) { - 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; - } - - 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) -{ - int ret = 0; - struct bt_ctf_field_string *string; - - if (!field) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_generic_reset(field); - if (ret) { - goto end; - } - - string = container_of(field, struct bt_ctf_field_string, parent); - if (string->payload) { - g_string_truncate(string->payload, 0); - } -end: - return ret; -} - -static -int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - int ret = 0; - struct bt_ctf_field_integer *integer = container_of(field, - struct bt_ctf_field_integer, parent); - -retry: - ret = ctf_integer_write(&pos->parent, &integer->definition.p); - 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; - } -end: - return ret; -} - -static -int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - struct bt_ctf_field_enumeration *enumeration = container_of( - field, struct bt_ctf_field_enumeration, parent); - - return bt_ctf_field_serialize(enumeration->payload, pos); -} - -static -int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - int ret = 0; - struct bt_ctf_field_floating_point *floating_point = container_of(field, - struct bt_ctf_field_floating_point, parent); - -retry: - ret = ctf_float_write(&pos->parent, &floating_point->definition.p); - 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; - } -end: - return ret; -} - -static -int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_structure *structure = container_of( - field, struct bt_ctf_field_structure, parent); - - while (!ctf_pos_access_ok(pos, - offset_align(pos->offset, - field->type->declaration->alignment))) { - ret = increase_packet_size(pos); - if (ret) { - goto end; - } - } - - if (!ctf_align_pos(pos, field->type->declaration->alignment)) { - ret = -1; - goto end; - } - - for (i = 0; i < structure->fields->len; i++) { - struct bt_ctf_field *field = g_ptr_array_index( - structure->fields, i); - - ret = bt_ctf_field_serialize(field, pos); - if (ret) { - break; - } - } -end: - return ret; -} - -static -int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - struct bt_ctf_field_variant *variant = container_of( - field, struct bt_ctf_field_variant, parent); - - return bt_ctf_field_serialize(variant->payload, pos); -} - -static -int bt_ctf_field_array_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_array *array = container_of( - field, struct bt_ctf_field_array, parent); - - for (i = 0; i < array->elements->len; i++) { - ret = bt_ctf_field_serialize( - g_ptr_array_index(array->elements, i), pos); - if (ret) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_sequence *sequence = container_of( - field, struct bt_ctf_field_sequence, parent); - - for (i = 0; i < sequence->elements->len; i++) { - ret = bt_ctf_field_serialize( - g_ptr_array_index(sequence->elements, i), pos); - if (ret) { - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_string_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos) -{ - size_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; - } - - ret = bt_ctf_field_integer_serialize(character, pos); - if (ret) { - 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); - - memcpy(&integer_dst->definition, &integer_src->definition, - sizeof(struct definition_integer)); - return 0; -} - -static -int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - int ret = 0; - struct bt_ctf_field_enumeration *enum_src, *enum_dst; - - enum_src = container_of(src, struct bt_ctf_field_enumeration, parent); - enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent); - - if (enum_src->payload) { - enum_dst->payload = bt_ctf_field_copy(enum_src->payload); - if (!enum_dst->payload) { - ret = -1; - goto end; - } - } -end: - return ret; -} - -static -int bt_ctf_field_floating_point_copy( - struct bt_ctf_field *src, struct bt_ctf_field *dst) -{ - 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); - - memcpy(&float_dst->definition, &float_src->definition, - sizeof(struct definition_float)); - memcpy(&float_dst->sign, &float_src->sign, - sizeof(struct definition_integer)); - memcpy(&float_dst->mantissa, &float_src->mantissa, - sizeof(struct definition_integer)); - memcpy(&float_dst->exp, &float_src->exp, - sizeof(struct definition_integer)); - return 0; -} - -static -int bt_ctf_field_structure_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - 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); - - 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; - - if (field) { - field_copy = bt_ctf_field_copy(field); - - if (!field_copy) { - ret = -1; - goto end; - } - } - - g_ptr_array_index(struct_dst->fields, i) = field_copy; - } -end: - return ret; -} - -static -int bt_ctf_field_variant_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - 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); - - 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; -} - -static -int bt_ctf_field_array_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - 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); - - 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; - - if (field) { - field_copy = bt_ctf_field_copy(field); - - if (!field_copy) { - ret = -1; - goto end; - } - } - - g_ptr_array_index(array_dst->elements, i) = field_copy; - } -end: - return ret; -} - -static -int bt_ctf_field_sequence_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - 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); - - 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; - - if (field) { - field_copy = bt_ctf_field_copy(field); - - if (!field_copy) { - ret = -1; - goto end; - } - } - - g_ptr_array_index(sequence_dst->elements, i) = field_copy; - } -end: - return ret; -} - -static -int bt_ctf_field_string_copy(struct bt_ctf_field *src, - struct bt_ctf_field *dst) -{ - 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; -} - -static -int increase_packet_size(struct ctf_stream_pos *pos) -{ - int ret; - - assert(pos); - ret = munmap_align(pos->base_mma); - if (ret) { - goto end; - } - - 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; - } - - 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; - } -end: - return ret; -} diff --git a/formats/ctf/ir/event-types.c b/formats/ctf/ir/event-types.c deleted file mode 100644 index eab0ef43..00000000 --- a/formats/ctf/ir/event-types.c +++ /dev/null @@ -1,4102 +0,0 @@ -/* - * event-types.c - * - * Babeltrace CTF IR - Event Types - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct range_overlap_query { - union { - uint64_t _unsigned; - int64_t _signed; - } range_start; - - union { - uint64_t _unsigned; - int64_t _signed; - } range_end; - int overlaps; - GQuark mapping_name; -}; - -static -void bt_ctf_field_type_destroy(struct bt_object *); -static -void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *); - -static -void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_destroy, - [BT_CTF_TYPE_ID_ENUM] = - bt_ctf_field_type_enumeration_destroy, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_type_floating_point_destroy, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_destroy, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_destroy, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_destroy, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_destroy, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_destroy, -}; - -static -void generic_field_type_freeze(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *); -static -void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *); - -static -type_freeze_func const type_freeze_funcs[] = { - [BT_CTF_TYPE_ID_INTEGER] = generic_field_type_freeze, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_freeze, - [BT_CTF_TYPE_ID_FLOAT] = generic_field_type_freeze, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_freeze, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_freeze, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_freeze, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_freeze, - [BT_CTF_TYPE_ID_STRING] = generic_field_type_freeze, -}; - -static -int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_floating_point_serialize( - struct bt_ctf_field_type *, struct metadata_context *); -static -int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *, - struct metadata_context *); -static -int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *, - struct metadata_context *); - -static -type_serialize_func const type_serialize_funcs[] = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_serialize, - [BT_CTF_TYPE_ID_ENUM] = - bt_ctf_field_type_enumeration_serialize, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_type_floating_point_serialize, - [BT_CTF_TYPE_ID_STRUCT] = - bt_ctf_field_type_structure_serialize, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_serialize, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_serialize, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_serialize, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_serialize, -}; - -static -void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); -static -void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); -static -void bt_ctf_field_type_floating_point_set_byte_order( - struct bt_ctf_field_type *, int byte_order, int set_native); -static -void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); -static -void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); -static -void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); -static -void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *, - int byte_order, int set_native); - -/* The set_native flag only set the byte order if it is set to native */ -static -void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *, - int byte_order, int set_native) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_set_byte_order, - [BT_CTF_TYPE_ID_ENUM] = - bt_ctf_field_type_enumeration_set_byte_order, - [BT_CTF_TYPE_ID_FLOAT] = - bt_ctf_field_type_floating_point_set_byte_order, - [BT_CTF_TYPE_ID_STRUCT] = - bt_ctf_field_type_structure_set_byte_order, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_set_byte_order, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_set_byte_order, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order, - [BT_CTF_TYPE_ID_STRING] = NULL, -}; - -static -struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_array_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( - struct bt_ctf_field_type *); -static -struct bt_ctf_field_type *bt_ctf_field_type_string_copy( - struct bt_ctf_field_type *); - -static -struct bt_ctf_field_type *(* const type_copy_funcs[])( - struct bt_ctf_field_type *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_copy, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_copy, - [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_copy, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_copy, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_copy, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_copy, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_copy, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_copy, -}; - -static -int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); -static -int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *, - struct bt_ctf_field_type *); - -static -int (* const type_compare_funcs[])(struct bt_ctf_field_type *, - struct bt_ctf_field_type *) = { - [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_compare, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_compare, - [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_compare, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_compare, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_compare, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_compare, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_compare, - [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_compare, -}; - -static -int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *); -static -int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *); -static -int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *); -static -int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *); -static -int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *); - -static -int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = { - [BT_CTF_TYPE_ID_INTEGER] = NULL, - [BT_CTF_TYPE_ID_FLOAT] = NULL, - [BT_CTF_TYPE_ID_STRING] = NULL, - [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_validate, - [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_validate, - [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_validate, - [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_validate, - [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_validate, -}; - -static -void destroy_enumeration_mapping(struct enumeration_mapping *mapping) -{ - g_free(mapping); -} - -static -void destroy_structure_field(struct structure_field *field) -{ - bt_put(field->type); - g_free(field); -} - -static -void check_ranges_overlap(gpointer element, gpointer query) -{ - struct enumeration_mapping *mapping = element; - struct range_overlap_query *overlap_query = query; - - if (mapping->range_start._signed <= overlap_query->range_end._signed - && overlap_query->range_start._signed <= - mapping->range_end._signed) { - overlap_query->overlaps = 1; - overlap_query->mapping_name = mapping->string; - } - - overlap_query->overlaps |= - mapping->string == overlap_query->mapping_name; -} - -static -void check_ranges_overlap_unsigned(gpointer element, gpointer query) -{ - struct enumeration_mapping *mapping = element; - struct range_overlap_query *overlap_query = query; - - if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned - && overlap_query->range_start._unsigned <= - mapping->range_end._unsigned) { - overlap_query->overlaps = 1; - overlap_query->mapping_name = mapping->string; - } - - overlap_query->overlaps |= - mapping->string == overlap_query->mapping_name; -} - -static -gint compare_enumeration_mappings_signed(struct enumeration_mapping **a, - struct enumeration_mapping **b) -{ - return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1; -} - -static -gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a, - struct enumeration_mapping **b) -{ - return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1; -} - -static -void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo) -{ - enum bt_ctf_type_id type_id = type->declaration->id; - - assert(type && (type_id > BT_CTF_TYPE_ID_UNKNOWN) && - (type_id < BT_CTF_NR_TYPE_IDS)); - - bt_object_init(type, bt_ctf_field_type_destroy); - type->freeze = type_freeze_funcs[type_id]; - type->serialize = type_serialize_funcs[type_id]; - - if (init_bo) { - int ret = bt_ctf_field_type_set_byte_order(type, - BT_CTF_BYTE_ORDER_NATIVE); - assert(!ret); - } - - type->declaration->alignment = 1; -} - -static -int add_structure_field(GPtrArray *fields, - GHashTable *field_name_to_index, - struct bt_ctf_field_type *field_type, - const char *field_name) -{ - int ret = 0; - GQuark name_quark = g_quark_from_string(field_name); - struct structure_field *field; - - /* Make sure structure does not contain a field of the same name */ - if (g_hash_table_lookup_extended(field_name_to_index, - GUINT_TO_POINTER(name_quark), NULL, NULL)) { - ret = -1; - goto end; - } - - field = g_new0(struct structure_field, 1); - if (!field) { - ret = -1; - goto end; - } - - bt_get(field_type); - field->name = name_quark; - field->type = field_type; - g_hash_table_insert(field_name_to_index, - (gpointer) (unsigned long) name_quark, - (gpointer) (unsigned long) fields->len); - g_ptr_array_add(fields, field); -end: - return ret; -} - -static -void bt_ctf_field_type_destroy(struct bt_object *obj) -{ - struct bt_ctf_field_type *type; - enum bt_ctf_type_id type_id; - - type = container_of(obj, struct bt_ctf_field_type, base); - type_id = type->declaration->id; - if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || - type_id >= BT_CTF_NR_TYPE_IDS) { - return; - } - - type_destroy_funcs[type_id](type); -} - -static -int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - - struct bt_ctf_field_type_enumeration *enumeration = - container_of(type, struct bt_ctf_field_type_enumeration, - parent); - struct bt_ctf_field_type *container_type = - bt_ctf_field_type_enumeration_get_container_type(type); - - if (!container_type) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_type_validate(container_type); - if (ret) { - goto end; - } - - /* Ensure enum has entries */ - ret = enumeration->entries->len ? 0 : -1; - -end: - BT_PUT(container_type); - return ret; -} - -static -int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type *element_type = NULL; - struct bt_ctf_field_type_sequence *sequence = - container_of(type, struct bt_ctf_field_type_sequence, - parent); - - /* Length field name should be set at this point */ - if (sequence->length_field_name->len == 0) { - ret = -1; - goto end; - } - - element_type = bt_ctf_field_type_sequence_get_element_type(type); - if (!element_type) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_type_validate(element_type); - -end: - BT_PUT(element_type); - - return ret; -} - -static -int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type *element_type = NULL; - - element_type = bt_ctf_field_type_array_get_element_type(type); - if (!element_type) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_type_validate(element_type); - -end: - BT_PUT(element_type); - - return ret; -} - -static -int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type *child_type = NULL; - int field_count = bt_ctf_field_type_structure_get_field_count(type); - int i; - - if (field_count < 0) { - ret = -1; - goto end; - } - - for (i = 0; i < field_count; ++i) { - ret = bt_ctf_field_type_structure_get_field(type, - NULL, &child_type, i); - if (ret) { - goto end; - } - - ret = bt_ctf_field_type_validate(child_type); - if (ret) { - goto end; - } - - BT_PUT(child_type); - } - -end: - BT_PUT(child_type); - - return ret; -} - -static -int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - int field_count; - struct bt_ctf_field_type *child_type = NULL; - struct bt_ctf_field_type_variant *variant = - container_of(type, struct bt_ctf_field_type_variant, - parent); - int i; - int tag_mappings_count; - - if (variant->tag_name->len == 0 || !variant->tag) { - ret = -1; - goto end; - } - - tag_mappings_count = - bt_ctf_field_type_enumeration_get_mapping_count( - (struct bt_ctf_field_type *) variant->tag); - - if (tag_mappings_count != variant->fields->len) { - ret = -1; - goto end; - } - - for (i = 0; i < tag_mappings_count; ++i) { - const char *label; - int64_t range_start, range_end; - struct bt_ctf_field_type *ft; - - ret = bt_ctf_field_type_enumeration_get_mapping( - (struct bt_ctf_field_type *) variant->tag, - i, &label, &range_start, &range_end); - if (ret) { - goto end; - } - if (!label) { - ret = -1; - goto end; - } - - ft = bt_ctf_field_type_variant_get_field_type_by_name( - type, label); - if (!ft) { - ret = -1; - goto end; - } - - BT_PUT(ft); - } - - field_count = bt_ctf_field_type_variant_get_field_count(type); - if (field_count < 0) { - ret = -1; - goto end; - } - - for (i = 0; i < field_count; ++i) { - ret = bt_ctf_field_type_variant_get_field(type, - NULL, &child_type, i); - if (ret) { - goto end; - } - - ret = bt_ctf_field_type_validate(child_type); - if (ret) { - goto end; - } - - BT_PUT(child_type); - } - -end: - BT_PUT(child_type); - - return ret; -} - -/* - * This function validates a given field type without considering - * where this field type is located. It only validates the properties - * of the given field type and the properties of its children if - * applicable. - */ -BT_HIDDEN -int bt_ctf_field_type_validate(struct bt_ctf_field_type *type) -{ - int ret = 0; - enum bt_ctf_type_id id = bt_ctf_field_type_get_type_id(type); - - if (!type) { - ret = -1; - goto end; - } - - if (type->valid) { - /* Already marked as valid */ - goto end; - } - - if (type_validate_funcs[id]) { - ret = type_validate_funcs[id](type); - } - - if (!ret && type->frozen) { - /* Field type is valid */ - type->valid = 1; - } - -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) -{ - struct bt_ctf_field_type_integer *integer = - g_new0(struct bt_ctf_field_type_integer, 1); - - if (!integer || size == 0 || size > 64) { - return NULL; - } - - integer->parent.declaration = &integer->declaration.p; - integer->parent.declaration->id = BT_CTF_TYPE_ID_INTEGER; - integer->declaration.len = size; - integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL; - integer->declaration.encoding = BT_CTF_STRING_ENCODING_NONE; - bt_ctf_field_type_init(&integer->parent, TRUE); - return &integer->parent; -} - -int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - ret = (int) integer->declaration.len; -end: - return ret; -} - -int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - ret = integer->declaration.signedness; -end: - return ret; -} - -int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type, - int is_signed) -{ - int ret = 0; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->frozen || - type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - integer->declaration.signedness = !!is_signed; -end: - return ret; -} - -enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( - struct bt_ctf_field_type *type) -{ - enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - ret = integer->declaration.base; -end: - return ret; -} - -int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type, - enum bt_ctf_integer_base base) -{ - int ret = 0; - - if (!type || type->frozen || - type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - ret = -1; - goto end; - } - - switch (base) { - case BT_CTF_INTEGER_BASE_BINARY: - case BT_CTF_INTEGER_BASE_OCTAL: - case BT_CTF_INTEGER_BASE_DECIMAL: - case BT_CTF_INTEGER_BASE_HEXADECIMAL: - { - struct bt_ctf_field_type_integer *integer = container_of(type, - struct bt_ctf_field_type_integer, parent); - integer->declaration.base = base; - break; - } - default: - ret = -1; - } -end: - return ret; -} - -enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( - struct bt_ctf_field_type *type) -{ - enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - ret = integer->declaration.encoding; -end: - return ret; -} - -int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type, - enum bt_ctf_string_encoding encoding) -{ - int ret = 0; - struct bt_ctf_field_type_integer *integer; - - if (!type || type->frozen || - (type->declaration->id != BT_CTF_TYPE_ID_INTEGER) || - (encoding < BT_CTF_STRING_ENCODING_NONE) || - (encoding >= BT_CTF_STRING_ENCODING_UNKNOWN)) { - ret = -1; - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - integer->declaration.encoding = encoding; -end: - return ret; -} - -struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_integer *integer; - struct bt_ctf_clock *clock = NULL; - - if (!type) { - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - clock = integer->mapped_clock; - bt_get(clock); -end: - return clock; -} - -int bt_ctf_field_type_integer_set_mapped_clock( - struct bt_ctf_field_type *type, - struct bt_ctf_clock *clock) -{ - struct bt_ctf_field_type_integer *integer; - int ret = 0; - - if (!type || type->frozen) { - ret = -1; - goto end; - } - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - bt_put(integer->mapped_clock); - bt_get(clock); - integer->mapped_clock = clock; -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( - struct bt_ctf_field_type *integer_container_type) -{ - struct bt_ctf_field_type_enumeration *enumeration = NULL; - - if (!integer_container_type) { - goto error; - } - - if (integer_container_type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { - goto error; - } - - enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1); - if (!enumeration) { - goto error; - } - - enumeration->parent.declaration = &enumeration->declaration.p; - enumeration->parent.declaration->id = BT_CTF_TYPE_ID_ENUM; - bt_get(integer_container_type); - enumeration->container = integer_container_type; - enumeration->entries = g_ptr_array_new_with_free_func( - (GDestroyNotify)destroy_enumeration_mapping); - bt_ctf_field_type_init(&enumeration->parent, FALSE); - return &enumeration->parent; -error: - g_free(enumeration); - return NULL; -} - -struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *container_type = NULL; - struct bt_ctf_field_type_enumeration *enumeration_type; - - if (!type) { - goto end; - } - - if (type->declaration->id != BT_CTF_TYPE_ID_ENUM) { - goto end; - } - - enumeration_type = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - container_type = enumeration_type->container; - bt_get(container_type); -end: - return container_type; -} - -int bt_ctf_field_type_enumeration_add_mapping( - struct bt_ctf_field_type *type, const char *string, - int64_t range_start, int64_t range_end) -{ - int ret = 0; - GQuark mapping_name; - struct enumeration_mapping *mapping; - struct bt_ctf_field_type_enumeration *enumeration; - struct range_overlap_query query; - char *escaped_string; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) || - type->frozen || - (range_end < range_start)) { - ret = -1; - goto end; - } - - if (!string || strlen(string) == 0) { - ret = -1; - goto end; - } - - escaped_string = g_strescape(string, NULL); - if (!escaped_string) { - ret = -1; - goto end; - } - - mapping_name = g_quark_from_string(escaped_string); - query = (struct range_overlap_query) { - .range_start._signed = range_start, - .range_end._signed = range_end, - .mapping_name = mapping_name, - .overlaps = 0 }; - enumeration = container_of(type, struct bt_ctf_field_type_enumeration, - parent); - - /* Check that the range does not overlap with one already present */ - g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query); - if (query.overlaps) { - ret = -1; - goto error_free; - } - - mapping = g_new(struct enumeration_mapping, 1); - if (!mapping) { - ret = -1; - goto error_free; - } - - *mapping = (struct enumeration_mapping) { - .range_start._signed = range_start, - .range_end._signed = range_end, .string = mapping_name}; - g_ptr_array_add(enumeration->entries, mapping); - g_ptr_array_sort(enumeration->entries, - (GCompareFunc)compare_enumeration_mappings_signed); -error_free: - free(escaped_string); -end: - return ret; -} - -int bt_ctf_field_type_enumeration_add_mapping_unsigned( - struct bt_ctf_field_type *type, const char *string, - uint64_t range_start, uint64_t range_end) -{ - int ret = 0; - GQuark mapping_name; - struct enumeration_mapping *mapping; - struct bt_ctf_field_type_enumeration *enumeration; - struct range_overlap_query query; - char *escaped_string; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) || - type->frozen || - (range_end < range_start)) { - ret = -1; - goto end; - } - - if (!string || strlen(string) == 0) { - ret = -1; - goto end; - } - - escaped_string = g_strescape(string, NULL); - if (!escaped_string) { - ret = -1; - goto end; - } - - mapping_name = g_quark_from_string(escaped_string); - query = (struct range_overlap_query) { - .range_start._unsigned = range_start, - .range_end._unsigned = range_end, - .mapping_name = mapping_name, - .overlaps = 0 }; - enumeration = container_of(type, struct bt_ctf_field_type_enumeration, - parent); - - /* Check that the range does not overlap with one already present */ - g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned, - &query); - if (query.overlaps) { - ret = -1; - goto error_free; - } - - mapping = g_new(struct enumeration_mapping, 1); - if (!mapping) { - ret = -1; - goto error_free; - } - - *mapping = (struct enumeration_mapping) { - .range_start._unsigned = range_start, - .range_end._unsigned = range_end, .string = mapping_name}; - g_ptr_array_add(enumeration->entries, mapping); - g_ptr_array_sort(enumeration->entries, - (GCompareFunc)compare_enumeration_mappings_unsigned); -error_free: - free(escaped_string); -end: - return ret; -} - -const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( - struct bt_ctf_field_type_enumeration *enumeration_type, - uint64_t value) -{ - const char *name = NULL; - struct range_overlap_query query = - (struct range_overlap_query) { - .range_start._unsigned = value, - .range_end._unsigned = value, - .overlaps = 0 }; - - g_ptr_array_foreach(enumeration_type->entries, - check_ranges_overlap_unsigned, - &query); - if (!query.overlaps) { - goto end; - } - - name = g_quark_to_string(query.mapping_name); -end: - return name; -} - -const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( - struct bt_ctf_field_type_enumeration *enumeration_type, - int64_t value) -{ - const char *name = NULL; - struct range_overlap_query query = - (struct range_overlap_query) { - .range_start._signed = value, - .range_end._signed = value, - .overlaps = 0 }; - - g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap, - &query); - if (!query.overlaps) { - goto end; - } - - name = g_quark_to_string(query.mapping_name); -end: - return name; -} - -int bt_ctf_field_type_enumeration_get_mapping_count( - struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_enumeration *enumeration; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - enumeration = container_of(type, struct bt_ctf_field_type_enumeration, - parent); - ret = (int) enumeration->entries->len; -end: - return ret; -} - -static inline -struct enumeration_mapping *get_enumeration_mapping( - struct bt_ctf_field_type *type, int index) -{ - struct enumeration_mapping *mapping = NULL; - struct bt_ctf_field_type_enumeration *enumeration; - - enumeration = container_of(type, struct bt_ctf_field_type_enumeration, - parent); - if (index >= enumeration->entries->len) { - goto end; - } - - mapping = g_ptr_array_index(enumeration->entries, index); -end: - return mapping; -} - -int bt_ctf_field_type_enumeration_get_mapping( - struct bt_ctf_field_type *type, int index, - const char **string, int64_t *range_start, int64_t *range_end) -{ - struct enumeration_mapping *mapping; - int ret = 0; - - if (!type || index < 0 || !string || !range_start || !range_end || - (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - mapping = get_enumeration_mapping(type, index); - if (!mapping) { - ret = -1; - goto end; - } - - *string = g_quark_to_string(mapping->string); - *range_start = mapping->range_start._signed; - *range_end = mapping->range_end._signed; -end: - return ret; -} - -int bt_ctf_field_type_enumeration_get_mapping_unsigned( - struct bt_ctf_field_type *type, int index, - const char **string, uint64_t *range_start, uint64_t *range_end) -{ - struct enumeration_mapping *mapping; - int ret = 0; - - if (!type || index < 0 || !string || !range_start || !range_end || - (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - mapping = get_enumeration_mapping(type, index); - if (!mapping) { - ret = -1; - goto end; - } - - *string = g_quark_to_string(mapping->string); - *range_start = mapping->range_start._unsigned; - *range_end = mapping->range_end._unsigned; -end: - return ret; -} - -int bt_ctf_field_type_enumeration_get_mapping_index_by_name( - struct bt_ctf_field_type *type, const char *name) -{ - GQuark name_quark; - struct bt_ctf_field_type_enumeration *enumeration; - int i, ret = 0; - - if (!type || !name || - (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - name_quark = g_quark_try_string(name); - if (!name_quark) { - ret = -1; - goto end; - } - - enumeration = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - for (i = 0; i < enumeration->entries->len; i++) { - struct enumeration_mapping *mapping = - get_enumeration_mapping(type, i); - - if (mapping->string == name_quark) { - ret = i; - goto end; - } - } - - ret = -1; -end: - return ret; -} - -int bt_ctf_field_type_enumeration_get_mapping_index_by_value( - struct bt_ctf_field_type *type, int64_t value) -{ - struct bt_ctf_field_type_enumeration *enumeration; - int i, ret = 0; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - enumeration = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - for (i = 0; i < enumeration->entries->len; i++) { - struct enumeration_mapping *mapping = - get_enumeration_mapping(type, i); - - if (value >= mapping->range_start._signed && - value <= mapping->range_end._signed) { - ret = i; - goto end; - } - } - - ret = -1; -end: - return ret; -} - -int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( - struct bt_ctf_field_type *type, uint64_t value) -{ - struct bt_ctf_field_type_enumeration *enumeration; - int i, ret = 0; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { - ret = -1; - goto end; - } - - enumeration = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - for (i = 0; i < enumeration->entries->len; i++) { - struct enumeration_mapping *mapping = - get_enumeration_mapping(type, i); - - if (value >= mapping->range_start._unsigned && - value <= mapping->range_end._unsigned) { - ret = i; - goto end; - } - } - - ret = -1; -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) -{ - struct bt_ctf_field_type_floating_point *floating_point = - g_new0(struct bt_ctf_field_type_floating_point, 1); - - if (!floating_point) { - goto end; - } - - floating_point->declaration.sign = &floating_point->sign; - floating_point->declaration.mantissa = &floating_point->mantissa; - floating_point->declaration.exp = &floating_point->exp; - floating_point->sign.len = 1; - floating_point->parent.declaration = &floating_point->declaration.p; - floating_point->parent.declaration->id = BT_CTF_TYPE_ID_FLOAT; - floating_point->declaration.exp->len = - sizeof(float) * CHAR_BIT - FLT_MANT_DIG; - floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1; - floating_point->sign.p.alignment = 1; - floating_point->mantissa.p.alignment = 1; - floating_point->exp.p.alignment = 1; - - bt_ctf_field_type_init(&floating_point->parent, TRUE); -end: - return floating_point ? &floating_point->parent : NULL; -} - -int bt_ctf_field_type_floating_point_get_exponent_digits( - struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_floating_point *floating_point; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { - ret = -1; - goto end; - } - - floating_point = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - ret = (int) floating_point->declaration.exp->len; -end: - return ret; -} - -int bt_ctf_field_type_floating_point_set_exponent_digits( - struct bt_ctf_field_type *type, - unsigned int exponent_digits) -{ - int ret = 0; - struct bt_ctf_field_type_floating_point *floating_point; - - if (!type || type->frozen || - (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { - ret = -1; - goto end; - } - - floating_point = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) && - (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) && - (exponent_digits != - sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) { - ret = -1; - goto end; - } - - floating_point->declaration.exp->len = exponent_digits; -end: - return ret; -} - -int bt_ctf_field_type_floating_point_get_mantissa_digits( - struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_floating_point *floating_point; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { - ret = -1; - goto end; - } - - floating_point = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - ret = (int) floating_point->mantissa.len + 1; -end: - return ret; -} - -int bt_ctf_field_type_floating_point_set_mantissa_digits( - struct bt_ctf_field_type *type, - unsigned int mantissa_digits) -{ - int ret = 0; - struct bt_ctf_field_type_floating_point *floating_point; - - if (!type || type->frozen || - (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { - ret = -1; - goto end; - } - - floating_point = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - - if ((mantissa_digits != FLT_MANT_DIG) && - (mantissa_digits != DBL_MANT_DIG) && - (mantissa_digits != LDBL_MANT_DIG)) { - ret = -1; - goto end; - } - - floating_point->declaration.mantissa->len = mantissa_digits - 1; -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) -{ - struct bt_ctf_field_type_structure *structure = - g_new0(struct bt_ctf_field_type_structure, 1); - - if (!structure) { - goto error; - } - - structure->parent.declaration = &structure->declaration.p; - structure->parent.declaration->id = BT_CTF_TYPE_ID_STRUCT; - structure->fields = g_ptr_array_new_with_free_func( - (GDestroyNotify)destroy_structure_field); - structure->field_name_to_index = g_hash_table_new(NULL, NULL); - bt_ctf_field_type_init(&structure->parent, TRUE); - return &structure->parent; -error: - return NULL; -} - -int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *field_type, - const char *field_name) -{ - int ret = 0; - struct bt_ctf_field_type_structure *structure; - - if (!type || !field_type || type->frozen || - bt_ctf_validate_identifier(field_name) || - (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { - ret = -1; - goto end; - } - - structure = container_of(type, - struct bt_ctf_field_type_structure, parent); - if (add_structure_field(structure->fields, - structure->field_name_to_index, field_type, field_name)) { - ret = -1; - goto end; - } -end: - return ret; -} - -int bt_ctf_field_type_structure_get_field_count( - struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_structure *structure; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { - ret = -1; - goto end; - } - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - ret = (int) structure->fields->len; -end: - return ret; -} - -int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type, - const char **field_name, struct bt_ctf_field_type **field_type, - int index) -{ - struct bt_ctf_field_type_structure *structure; - struct structure_field *field; - int ret = 0; - - if (!type || index < 0 || - (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { - ret = -1; - goto end; - } - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - if (index >= structure->fields->len) { - ret = -1; - goto end; - } - - field = g_ptr_array_index(structure->fields, index); - if (field_type) { - *field_type = field->type; - bt_get(field->type); - } - if (field_name) { - *field_name = g_quark_to_string(field->name); - } -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( - struct bt_ctf_field_type *type, - const char *name) -{ - size_t index; - GQuark name_quark; - struct structure_field *field; - struct bt_ctf_field_type_structure *structure; - struct bt_ctf_field_type *field_type = NULL; - - if (!type || !name) { - goto end; - } - - name_quark = g_quark_try_string(name); - if (!name_quark) { - goto end; - } - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - if (!g_hash_table_lookup_extended(structure->field_name_to_index, - GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { - goto end; - } - - field = structure->fields->pdata[index]; - field_type = field->type; - bt_get(field_type); -end: - return field_type; -} - -struct bt_ctf_field_type *bt_ctf_field_type_variant_create( - struct bt_ctf_field_type *enum_tag, const char *tag_name) -{ - struct bt_ctf_field_type_variant *variant = NULL; - - if (tag_name && bt_ctf_validate_identifier(tag_name)) { - goto error; - } - - variant = g_new0(struct bt_ctf_field_type_variant, 1); - if (!variant) { - goto error; - } - - variant->parent.declaration = &variant->declaration.p; - variant->parent.declaration->id = BT_CTF_TYPE_ID_VARIANT; - variant->tag_name = g_string_new(tag_name); - variant->field_name_to_index = g_hash_table_new(NULL, NULL); - variant->fields = g_ptr_array_new_with_free_func( - (GDestroyNotify) destroy_structure_field); - if (enum_tag) { - bt_get(enum_tag); - variant->tag = container_of(enum_tag, - struct bt_ctf_field_type_enumeration, parent); - } - - bt_ctf_field_type_init(&variant->parent, TRUE); - /* A variant's alignment is undefined */ - variant->parent.declaration->alignment = 0; - return &variant->parent; -error: - return NULL; -} - -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_variant *variant; - struct bt_ctf_field_type *tag_type = NULL; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, parent); - if (!variant->tag) { - goto end; - } - - tag_type = &variant->tag->parent; - bt_get(tag_type); -end: - return tag_type; -} - -const char *bt_ctf_field_type_variant_get_tag_name( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_variant *variant; - const char *tag_name = NULL; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, parent); - if (variant->tag_name->len == 0) { - goto end; - } - - tag_name = variant->tag_name->str; -end: - return tag_name; -} - -int bt_ctf_field_type_variant_set_tag_name( - struct bt_ctf_field_type *type, const char *name) -{ - int ret = 0; - struct bt_ctf_field_type_variant *variant; - - if (!type || type->frozen || - (type->declaration->id != BT_CTF_TYPE_ID_VARIANT) || - bt_ctf_validate_identifier(name)) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, parent); - g_string_assign(variant->tag_name, name); -end: - return ret; -} - -int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *field_type, - const char *field_name) -{ - size_t i; - int ret = 0; - struct bt_ctf_field_type_variant *variant; - GQuark field_name_quark = g_quark_from_string(field_name); - - if (!type || !field_type || type->frozen || - bt_ctf_validate_identifier(field_name) || - (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, parent); - - /* The user has explicitly provided a tag; validate against it. */ - if (variant->tag) { - int name_found = 0; - - /* Make sure this name is present in the enum tag */ - for (i = 0; i < variant->tag->entries->len; i++) { - struct enumeration_mapping *mapping = - g_ptr_array_index(variant->tag->entries, i); - - if (mapping->string == field_name_quark) { - name_found = 1; - break; - } - } - - if (!name_found) { - /* Validation failed */ - ret = -1; - goto end; - } - } - - if (add_structure_field(variant->fields, variant->field_name_to_index, - field_type, field_name)) { - ret = -1; - goto end; - } -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( - struct bt_ctf_field_type *type, - const char *field_name) -{ - size_t index; - GQuark name_quark; - struct structure_field *field; - struct bt_ctf_field_type_variant *variant; - struct bt_ctf_field_type *field_type = NULL; - - if (!type || !field_name) { - goto end; - } - - name_quark = g_quark_try_string(field_name); - if (!name_quark) { - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, parent); - if (!g_hash_table_lookup_extended(variant->field_name_to_index, - GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { - goto end; - } - - field = g_ptr_array_index(variant->fields, index); - field_type = field->type; - bt_get(field_type); -end: - return field_type; -} - -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( - struct bt_ctf_field_type *type, - struct bt_ctf_field *tag) -{ - const char *enum_value; - struct bt_ctf_field_type *field_type = NULL; - - if (!type || !tag || type->declaration->id != BT_CTF_TYPE_ID_VARIANT) { - goto end; - } - - enum_value = bt_ctf_field_enumeration_get_mapping_name(tag); - if (!enum_value) { - goto end; - } - - /* Already increments field_type's reference count */ - field_type = bt_ctf_field_type_variant_get_field_type_by_name( - type, enum_value); -end: - return field_type; -} - -int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type) -{ - int ret = 0; - struct bt_ctf_field_type_variant *variant; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - ret = (int) variant->fields->len; -end: - return ret; - -} - -int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type, - const char **field_name, struct bt_ctf_field_type **field_type, - int index) -{ - struct bt_ctf_field_type_variant *variant; - struct structure_field *field; - int ret = 0; - - if (!type || index < 0 || - (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - if (index >= variant->fields->len) { - ret = -1; - goto end; - } - - field = g_ptr_array_index(variant->fields, index); - if (field_type) { - *field_type = field->type; - bt_get(field->type); - } - if (field_name) { - *field_name = g_quark_to_string(field->name); - } -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_array_create( - struct bt_ctf_field_type *element_type, - unsigned int length) -{ - struct bt_ctf_field_type_array *array = NULL; - - if (!element_type || length == 0) { - goto error; - } - - array = g_new0(struct bt_ctf_field_type_array, 1); - if (!array) { - goto error; - } - - array->parent.declaration = &array->declaration.p; - array->parent.declaration->id = BT_CTF_TYPE_ID_ARRAY; - - bt_get(element_type); - array->element_type = element_type; - array->length = length; - bt_ctf_field_type_init(&array->parent, FALSE); - return &array->parent; -error: - return NULL; -} - -struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *ret = NULL; - struct bt_ctf_field_type_array *array; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { - goto end; - } - - array = container_of(type, struct bt_ctf_field_type_array, parent); - ret = array->element_type; - bt_get(ret); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *element_type) -{ - int ret = 0; - struct bt_ctf_field_type_array *array; - - if (!type || !element_type || - (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { - ret = -1; - goto end; - } - - array = container_of(type, struct bt_ctf_field_type_array, parent); - - if (array->element_type) { - BT_PUT(array->element_type); - } - - array->element_type = element_type; - bt_get(array->element_type); - -end: - return ret; -} - -int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type) -{ - int64_t ret; - struct bt_ctf_field_type_array *array; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { - ret = -1; - goto end; - } - - array = container_of(type, struct bt_ctf_field_type_array, parent); - ret = (int64_t) array->length; -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( - struct bt_ctf_field_type *element_type, - const char *length_field_name) -{ - struct bt_ctf_field_type_sequence *sequence = NULL; - - if (!element_type || bt_ctf_validate_identifier(length_field_name)) { - goto error; - } - - sequence = g_new0(struct bt_ctf_field_type_sequence, 1); - if (!sequence) { - goto error; - } - - sequence->parent.declaration = &sequence->declaration.p; - sequence->parent.declaration->id = BT_CTF_TYPE_ID_SEQUENCE; - bt_get(element_type); - sequence->element_type = element_type; - sequence->length_field_name = g_string_new(length_field_name); - bt_ctf_field_type_init(&sequence->parent, FALSE); - return &sequence->parent; -error: - return NULL; -} - -struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *ret = NULL; - struct bt_ctf_field_type_sequence *sequence; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { - goto end; - } - - sequence = container_of(type, struct bt_ctf_field_type_sequence, - parent); - ret = sequence->element_type; - bt_get(ret); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *element_type) -{ - int ret = 0; - struct bt_ctf_field_type_sequence *sequence; - - if (!type || !element_type || - (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { - ret = -1; - goto end; - } - - sequence = container_of(type, struct bt_ctf_field_type_sequence, parent); - - if (sequence->element_type) { - BT_PUT(sequence->element_type); - } - - sequence->element_type = element_type; - bt_get(sequence->element_type); - -end: - return ret; -} - -const char *bt_ctf_field_type_sequence_get_length_field_name( - struct bt_ctf_field_type *type) -{ - const char *ret = NULL; - struct bt_ctf_field_type_sequence *sequence; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { - goto end; - } - - sequence = container_of(type, struct bt_ctf_field_type_sequence, - parent); - ret = sequence->length_field_name->str; -end: - return ret; -} - -struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) -{ - struct bt_ctf_field_type_string *string = - g_new0(struct bt_ctf_field_type_string, 1); - - if (!string) { - return NULL; - } - - string->parent.declaration = &string->declaration.p; - string->parent.declaration->id = BT_CTF_TYPE_ID_STRING; - bt_ctf_field_type_init(&string->parent, TRUE); - string->declaration.encoding = BT_CTF_STRING_ENCODING_UTF8; - string->parent.declaration->alignment = CHAR_BIT; - return &string->parent; -} - -enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_string *string; - enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; - - if (!type || (type->declaration->id != BT_CTF_TYPE_ID_STRING)) { - goto end; - } - - string = container_of(type, struct bt_ctf_field_type_string, - parent); - ret = string->declaration.encoding; -end: - return ret; -} - -int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type, - enum bt_ctf_string_encoding encoding) -{ - int ret = 0; - struct bt_ctf_field_type_string *string; - - if (!type || type->declaration->id != BT_CTF_TYPE_ID_STRING || - (encoding != BT_CTF_STRING_ENCODING_UTF8 && - encoding != BT_CTF_STRING_ENCODING_ASCII)) { - ret = -1; - goto end; - } - - string = container_of(type, struct bt_ctf_field_type_string, parent); - string->declaration.encoding = encoding; -end: - return ret; -} - -int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type) -{ - int ret; - enum bt_ctf_type_id type_id; - - if (!type) { - ret = -1; - goto end; - } - - if (type->frozen) { - ret = (int) type->declaration->alignment; - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(type); - switch (type_id) { - case BT_CTF_TYPE_ID_SEQUENCE: - { - struct bt_ctf_field_type *element = - bt_ctf_field_type_sequence_get_element_type(type); - - if (!element) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_type_get_alignment(element); - bt_put(element); - break; - } - case BT_CTF_TYPE_ID_ARRAY: - { - struct bt_ctf_field_type *element = - bt_ctf_field_type_array_get_element_type(type); - - if (!element) { - ret = -1; - goto end; - } - - ret = bt_ctf_field_type_get_alignment(element); - bt_put(element); - break; - } - case BT_CTF_TYPE_ID_STRUCT: - { - int i, element_count; - - element_count = bt_ctf_field_type_structure_get_field_count( - type); - if (element_count < 0) { - ret = element_count; - goto end; - } - - for (i = 0; i < element_count; i++) { - struct bt_ctf_field_type *field; - int field_alignment; - - ret = bt_ctf_field_type_structure_get_field(type, NULL, - &field, i); - if (ret) { - goto end; - } - - assert(field); - field_alignment = bt_ctf_field_type_get_alignment( - field); - bt_put(field); - if (field_alignment < 0) { - ret = field_alignment; - goto end; - } - - type->declaration->alignment = MAX(field_alignment, - type->declaration->alignment); - } - ret = (int) type->declaration->alignment; - break; - } - case BT_CTF_TYPE_ID_UNKNOWN: - ret = -1; - break; - default: - ret = (int) type->declaration->alignment; - break; - } -end: - return ret; -} - -static inline -int is_power_of_two(unsigned int value) -{ - return ((value & (value - 1)) == 0) && value > 0; -} - -int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, - unsigned int alignment) -{ - int ret = 0; - enum bt_ctf_type_id type_id; - - /* Alignment must be a power of two */ - if (!type || type->frozen || !is_power_of_two(alignment)) { - ret = -1; - goto end; - } - - type_id = bt_ctf_field_type_get_type_id(type); - if (type_id == BT_CTF_TYPE_ID_UNKNOWN) { - ret = -1; - goto end; - } - - if (type->declaration->id == BT_CTF_TYPE_ID_STRING && - alignment != CHAR_BIT) { - ret = -1; - goto end; - } - - if (type_id == BT_CTF_TYPE_ID_VARIANT || - type_id == BT_CTF_TYPE_ID_SEQUENCE || - type_id == BT_CTF_TYPE_ID_ARRAY) { - /* Setting an alignment on these types makes no sense */ - ret = -1; - goto end; - } - - type->declaration->alignment = alignment; - ret = 0; -end: - return ret; -} - -enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( - struct bt_ctf_field_type *type) -{ - enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; - - if (!type) { - goto end; - } - - switch (type->declaration->id) { - case BT_CTF_TYPE_ID_INTEGER: - { - struct bt_ctf_field_type_integer *integer = container_of( - type, struct bt_ctf_field_type_integer, parent); - ret = integer->user_byte_order; - break; - } - case BT_CTF_TYPE_ID_FLOAT: - { - struct bt_ctf_field_type_floating_point *floating_point = - container_of(type, - struct bt_ctf_field_type_floating_point, - parent); - ret = floating_point->user_byte_order; - break; - } - default: - goto end; - } - - assert(ret == BT_CTF_BYTE_ORDER_NATIVE || - ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || - ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN || - ret == BT_CTF_BYTE_ORDER_NETWORK); - -end: - return ret; -} - -int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, - enum bt_ctf_byte_order byte_order) -{ - int ret = 0; - int internal_byte_order; - enum bt_ctf_type_id type_id; - - if (!type || type->frozen) { - ret = -1; - goto end; - } - - switch (byte_order) { - case BT_CTF_BYTE_ORDER_NATIVE: - /* Leave unset. Will be initialized by parent. */ - internal_byte_order = 0; - break; - case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: - internal_byte_order = LITTLE_ENDIAN; - break; - case BT_CTF_BYTE_ORDER_BIG_ENDIAN: - case BT_CTF_BYTE_ORDER_NETWORK: - internal_byte_order = BIG_ENDIAN; - break; - default: - ret = -1; - goto end; - } - - type_id = type->declaration->id; - if (set_byte_order_funcs[type_id]) { - set_byte_order_funcs[type_id](type, internal_byte_order, 0); - } -end: - return ret; -} - -enum bt_ctf_type_id bt_ctf_field_type_get_type_id( - struct bt_ctf_field_type *type) -{ - if (!type) { - return BT_CTF_TYPE_ID_UNKNOWN; - } - - return type->declaration->id; -} - -int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_INTEGER; -} - -int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_FLOAT; -} - -int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_ENUM; -} - -int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_STRING; -} - -int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_STRUCT; -} - -int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_ARRAY; -} - -int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_SEQUENCE; -} - -int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type) -{ - return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_VARIANT; -} - -void bt_ctf_field_type_get(struct bt_ctf_field_type *type) -{ - bt_get(type); -} - -void bt_ctf_field_type_put(struct bt_ctf_field_type *type) -{ - bt_put(type); -} - -BT_HIDDEN -void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type) -{ - if (!type) { - return; - } - - type->freeze(type); -} - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( - struct bt_ctf_field_type_variant *variant, - int64_t tag_value) -{ - struct bt_ctf_field_type *type = NULL; - GQuark field_name_quark; - gpointer index; - struct structure_field *field_entry; - struct range_overlap_query query = { - .range_start._signed = tag_value, - .range_end._signed = tag_value, - .mapping_name = 0, .overlaps = 0}; - - g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap, - &query); - if (!query.overlaps) { - goto end; - } - - field_name_quark = query.mapping_name; - if (!g_hash_table_lookup_extended(variant->field_name_to_index, - GUINT_TO_POINTER(field_name_quark), NULL, &index)) { - goto end; - } - - field_entry = g_ptr_array_index(variant->fields, (size_t) index); - type = field_entry->type; -end: - return type; -} - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( - struct bt_ctf_field_type_variant *variant, - uint64_t tag_value) -{ - struct bt_ctf_field_type *type = NULL; - GQuark field_name_quark; - gpointer index; - struct structure_field *field_entry; - struct range_overlap_query query = { - .range_start._unsigned = tag_value, - .range_end._unsigned = tag_value, - .mapping_name = 0, .overlaps = 0}; - - g_ptr_array_foreach(variant->tag->entries, - check_ranges_overlap_unsigned, - &query); - if (!query.overlaps) { - goto end; - } - - field_name_quark = query.mapping_name; - if (!g_hash_table_lookup_extended(variant->field_name_to_index, - GUINT_TO_POINTER(field_name_quark), NULL, &index)) { - goto end; - } - - field_entry = g_ptr_array_index(variant->fields, (size_t)index); - type = field_entry->type; -end: - return type; -} - -BT_HIDDEN -int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - int ret; - - if (!type || !context) { - ret = -1; - goto end; - } - - /* Make sure field type is valid before serializing it */ - ret = bt_ctf_field_type_validate(type); - - if (ret) { - goto end; - } - - ret = type->serialize(type, context); -end: - return ret; -} - -BT_HIDDEN -void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type, - int byte_order) -{ - if (!type) { - return; - } - - assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN); - if (set_byte_order_funcs[type->declaration->id]) { - set_byte_order_funcs[type->declaration->id](type, - byte_order, 1); - } -} - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy = NULL; - - if (!type) { - goto end; - } - - copy = type_copy_funcs[type->declaration->id](type); -end: - return copy; -} - -BT_HIDDEN -int bt_ctf_field_type_structure_get_field_name_index( - struct bt_ctf_field_type *type, const char *name) -{ - int ret; - size_t index; - GQuark name_quark; - struct bt_ctf_field_type_structure *structure; - - if (!type || !name || - bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_STRUCT) { - ret = -1; - goto end; - } - - name_quark = g_quark_try_string(name); - if (!name_quark) { - ret = -1; - goto end; - } - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - if (!g_hash_table_lookup_extended(structure->field_name_to_index, - GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { - ret = -1; - goto end; - } - ret = (int) index; -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *field, int index) -{ - int ret = 0; - struct bt_ctf_field_type_structure *structure; - - if (!type || !field || - bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_STRUCT) { - ret = -1; - goto end; - } - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - if (index < 0 || index >= structure->fields->len) { - ret = -1; - goto end; - } - - bt_get(field); - bt_put(((struct structure_field *) - g_ptr_array_index(structure->fields, index))->type); - ((struct structure_field *) structure->fields->pdata[index])->type = - field; -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_variant_get_field_name_index( - struct bt_ctf_field_type *type, const char *name) -{ - int ret; - size_t index; - GQuark name_quark; - struct bt_ctf_field_type_variant *variant; - - if (!type || !name || - bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_VARIANT) { - ret = -1; - goto end; - } - - name_quark = g_quark_try_string(name); - if (!name_quark) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - if (!g_hash_table_lookup_extended(variant->field_name_to_index, - GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { - ret = -1; - goto end; - } - ret = (int) index; -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_sequence_set_length_field_path( - struct bt_ctf_field_type *type, - struct bt_ctf_field_path *path) -{ - int ret = 0; - struct bt_ctf_field_type_sequence *sequence; - - if (!type || bt_ctf_field_type_get_type_id(type) != - BT_CTF_TYPE_ID_SEQUENCE) { - ret = -1; - goto end; - } - - sequence = container_of(type, struct bt_ctf_field_type_sequence, - parent); - bt_get(path); - BT_MOVE(sequence->length_field_path, path); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, - struct bt_ctf_field_path *path) -{ - int ret = 0; - struct bt_ctf_field_type_variant *variant; - - if (!type || bt_ctf_field_type_get_type_id(type) != - BT_CTF_TYPE_ID_VARIANT) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - bt_get(path); - BT_MOVE(variant->tag_field_path, path); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *tag) -{ - int ret = 0; - struct bt_ctf_field_type_variant *variant; - - if (!type || !tag || - bt_ctf_field_type_get_type_id(tag) != - BT_CTF_TYPE_ID_ENUM) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - bt_get(tag); - if (variant->tag) { - bt_put(&variant->tag->parent); - } - variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration, - parent); -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *field, int index) -{ - int ret = 0; - struct bt_ctf_field_type_variant *variant; - - if (!type || !field || - bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_VARIANT) { - ret = -1; - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - if (index < 0 || index >= variant->fields->len) { - ret = -1; - goto end; - } - - bt_get(field); - bt_put(((struct structure_field *) - g_ptr_array_index(variant->fields, index))->type); - ((struct structure_field *) variant->fields->pdata[index])->type = - field; -end: - return ret; -} - -static -void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_integer *integer = - (struct bt_ctf_field_type_integer *) type; - - if (!type) { - return; - } - - bt_put(integer->mapped_clock); - g_free(integer); -} - -static -void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_enumeration *enumeration = - (struct bt_ctf_field_type_enumeration *) type; - - if (!type) { - return; - } - - g_ptr_array_free(enumeration->entries, TRUE); - bt_put(enumeration->container); - g_free(enumeration); -} - -static -void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_floating_point *floating_point = - (struct bt_ctf_field_type_floating_point *) type; - - if (!type) { - return; - } - - g_free(floating_point); -} - -static -void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_structure *structure = - (struct bt_ctf_field_type_structure *) type; - - if (!type) { - return; - } - - g_ptr_array_free(structure->fields, TRUE); - g_hash_table_destroy(structure->field_name_to_index); - g_free(structure); -} - -static -void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_variant *variant = - (struct bt_ctf_field_type_variant *) type; - - if (!type) { - return; - } - - g_ptr_array_free(variant->fields, TRUE); - g_hash_table_destroy(variant->field_name_to_index); - g_string_free(variant->tag_name, TRUE); - bt_put(&variant->tag->parent); - BT_PUT(variant->tag_field_path); - g_free(variant); -} - -static -void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_array *array = - (struct bt_ctf_field_type_array *) type; - - if (!type) { - return; - } - - bt_put(array->element_type); - g_free(array); -} - -static -void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_sequence *sequence = - (struct bt_ctf_field_type_sequence *) type; - - if (!type) { - return; - } - - bt_put(sequence->element_type); - g_string_free(sequence->length_field_name, TRUE); - BT_PUT(sequence->length_field_path); - g_free(sequence); -} - -static -void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_string *string = - (struct bt_ctf_field_type_string *) type; - - if (!type) { - return; - } - - g_free(string); -} - -static -void generic_field_type_freeze(struct bt_ctf_field_type *type) -{ - type->frozen = 1; -} - -static -void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_enumeration *enumeration_type = container_of( - type, struct bt_ctf_field_type_enumeration, parent); - - generic_field_type_freeze(type); - bt_ctf_field_type_freeze(enumeration_type->container); -} - -static -void freeze_structure_field(struct structure_field *field) -{ - bt_ctf_field_type_freeze(field->type); -} - -static -void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_structure *structure_type = container_of( - type, struct bt_ctf_field_type_structure, parent); - - /* Cache the alignment */ - type->declaration->alignment = bt_ctf_field_type_get_alignment(type); - generic_field_type_freeze(type); - g_ptr_array_foreach(structure_type->fields, - (GFunc) freeze_structure_field, NULL); -} - -static -void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_variant *variant_type = container_of( - type, struct bt_ctf_field_type_variant, parent); - - generic_field_type_freeze(type); - g_ptr_array_foreach(variant_type->fields, - (GFunc) freeze_structure_field, NULL); -} - -static -void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_array *array_type = container_of( - type, struct bt_ctf_field_type_array, parent); - - /* Cache the alignment */ - type->declaration->alignment = bt_ctf_field_type_get_alignment(type); - generic_field_type_freeze(type); - bt_ctf_field_type_freeze(array_type->element_type); -} - -static -void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type_sequence *sequence_type = container_of( - type, struct bt_ctf_field_type_sequence, parent); - - /* Cache the alignment */ - type->declaration->alignment = bt_ctf_field_type_get_alignment(type); - generic_field_type_freeze(type); - bt_ctf_field_type_freeze(sequence_type->element_type); -} - -static -const char *get_encoding_string(enum bt_ctf_string_encoding encoding) -{ - const char *encoding_string; - - switch (encoding) { - case BT_CTF_STRING_ENCODING_NONE: - encoding_string = "none"; - break; - case BT_CTF_STRING_ENCODING_ASCII: - encoding_string = "ASCII"; - break; - case BT_CTF_STRING_ENCODING_UTF8: - encoding_string = "UTF8"; - break; - default: - encoding_string = "unknown"; - break; - } - - return encoding_string; -} - -static -const char *get_integer_base_string(enum bt_ctf_integer_base base) -{ - const char *base_string; - - switch (base) { - case BT_CTF_INTEGER_BASE_DECIMAL: - base_string = "decimal"; - break; - case BT_CTF_INTEGER_BASE_HEXADECIMAL: - base_string = "hexadecimal"; - break; - case BT_CTF_INTEGER_BASE_OCTAL: - base_string = "octal"; - break; - case BT_CTF_INTEGER_BASE_BINARY: - base_string = "binary"; - break; - default: - base_string = "unknown"; - break; - } - - return base_string; -} - -static -int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - struct bt_ctf_field_type_integer *integer = container_of(type, - struct bt_ctf_field_type_integer, parent); - int ret = 0; - - g_string_append_printf(context->string, - "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s", - integer->declaration.len, type->declaration->alignment, - (integer->declaration.signedness ? "true" : "false"), - get_encoding_string(integer->declaration.encoding), - get_integer_base_string(integer->declaration.base), - get_byte_order_string(integer->declaration.byte_order)); - if (integer->mapped_clock) { - const char *clock_name = bt_ctf_clock_get_name( - integer->mapped_clock); - - if (!clock_name) { - ret = -1; - goto end; - } - - g_string_append_printf(context->string, - "; map = clock.%s.value", clock_name); - } - - g_string_append(context->string, "; }"); -end: - return ret; -} - -static -int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - size_t entry; - int ret; - struct bt_ctf_field_type_enumeration *enumeration = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - struct bt_ctf_field_type *container_type; - int container_signed; - - container_type = bt_ctf_field_type_enumeration_get_container_type(type); - if (!container_type) { - ret = -1; - goto end; - } - - container_signed = bt_ctf_field_type_integer_get_signed(container_type); - if (container_signed < 0) { - ret = container_signed; - goto error_put_container_type; - } - - g_string_append(context->string, "enum : "); - ret = bt_ctf_field_type_serialize(enumeration->container, context); - if (ret) { - goto error_put_container_type; - } - - g_string_append(context->string, " { "); - for (entry = 0; entry < enumeration->entries->len; entry++) { - struct enumeration_mapping *mapping = - enumeration->entries->pdata[entry]; - - if (container_signed) { - if (mapping->range_start._signed == - mapping->range_end._signed) { - g_string_append_printf(context->string, - "\"%s\" = %" PRId64, - g_quark_to_string(mapping->string), - mapping->range_start._signed); - } else { - g_string_append_printf(context->string, - "\"%s\" = %" PRId64 " ... %" PRId64, - g_quark_to_string(mapping->string), - mapping->range_start._signed, - mapping->range_end._signed); - } - } else { - if (mapping->range_start._unsigned == - mapping->range_end._unsigned) { - g_string_append_printf(context->string, - "\"%s\" = %" PRIu64, - g_quark_to_string(mapping->string), - mapping->range_start._unsigned); - } else { - g_string_append_printf(context->string, - "\"%s\" = %" PRIu64 " ... %" PRIu64, - g_quark_to_string(mapping->string), - mapping->range_start._unsigned, - mapping->range_end._unsigned); - } - } - - g_string_append(context->string, - ((entry != (enumeration->entries->len - 1)) ? - ", " : " }")); - } - - if (context->field_name->len) { - g_string_append_printf(context->string, " %s", - context->field_name->str); - g_string_assign(context->field_name, ""); - } -error_put_container_type: - bt_put(container_type); -end: - return ret; -} - -static -int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - struct bt_ctf_field_type_floating_point *floating_point = container_of( - type, struct bt_ctf_field_type_floating_point, parent); - - g_string_append_printf(context->string, - "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }", - floating_point->declaration.exp->len, - floating_point->declaration.mantissa->len + 1, - get_byte_order_string(floating_point->declaration.byte_order), - type->declaration->alignment); - return 0; -} - -static -int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - size_t i; - unsigned int indent; - int ret = 0; - struct bt_ctf_field_type_structure *structure = container_of(type, - struct bt_ctf_field_type_structure, parent); - GString *structure_field_name = context->field_name; - - context->field_name = g_string_new(""); - - context->current_indentation_level++; - g_string_append(context->string, "struct {\n"); - - for (i = 0; i < structure->fields->len; i++) { - struct structure_field *field; - - for (indent = 0; indent < context->current_indentation_level; - indent++) { - g_string_append_c(context->string, '\t'); - } - - field = structure->fields->pdata[i]; - g_string_assign(context->field_name, - g_quark_to_string(field->name)); - ret = bt_ctf_field_type_serialize(field->type, context); - if (ret) { - goto end; - } - - if (context->field_name->len) { - g_string_append_printf(context->string, " %s", - context->field_name->str); - } - g_string_append(context->string, ";\n"); - } - - context->current_indentation_level--; - for (indent = 0; indent < context->current_indentation_level; - indent++) { - g_string_append_c(context->string, '\t'); - } - - g_string_append_printf(context->string, "} align(%zu)", - type->declaration->alignment); -end: - g_string_free(context->field_name, TRUE); - context->field_name = structure_field_name; - return ret; -} - -static -int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - size_t i; - unsigned int indent; - int ret = 0; - struct bt_ctf_field_type_variant *variant = container_of( - type, struct bt_ctf_field_type_variant, parent); - GString *variant_field_name = context->field_name; - - context->field_name = g_string_new(""); - if (variant->tag_name->len > 0) { - g_string_append_printf(context->string, - "variant <%s> {\n", variant->tag_name->str); - } else { - g_string_append(context->string, "variant {\n"); - } - - context->current_indentation_level++; - for (i = 0; i < variant->fields->len; i++) { - struct structure_field *field = variant->fields->pdata[i]; - - g_string_assign(context->field_name, - g_quark_to_string(field->name)); - for (indent = 0; indent < context->current_indentation_level; - indent++) { - g_string_append_c(context->string, '\t'); - } - - g_string_assign(context->field_name, - g_quark_to_string(field->name)); - ret = bt_ctf_field_type_serialize(field->type, context); - if (ret) { - goto end; - } - - if (context->field_name->len) { - g_string_append_printf(context->string, " %s;", - context->field_name->str); - } - - g_string_append_c(context->string, '\n'); - } - - context->current_indentation_level--; - for (indent = 0; indent < context->current_indentation_level; - indent++) { - g_string_append_c(context->string, '\t'); - } - - g_string_append(context->string, "}"); -end: - g_string_free(context->field_name, TRUE); - context->field_name = variant_field_name; - return ret; -} - -static -int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - int ret = 0; - struct bt_ctf_field_type_array *array = container_of(type, - struct bt_ctf_field_type_array, parent); - - ret = bt_ctf_field_type_serialize(array->element_type, context); - if (ret) { - goto end; - } - - if (context->field_name->len) { - g_string_append_printf(context->string, " %s[%u]", - context->field_name->str, array->length); - g_string_assign(context->field_name, ""); - } else { - g_string_append_printf(context->string, "[%u]", array->length); - } -end: - return ret; -} - -static -int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - int ret = 0; - struct bt_ctf_field_type_sequence *sequence = container_of( - type, struct bt_ctf_field_type_sequence, parent); - - ret = bt_ctf_field_type_serialize(sequence->element_type, context); - if (ret) { - goto end; - } - - if (context->field_name->len) { - g_string_append_printf(context->string, " %s[%s]", - context->field_name->str, - sequence->length_field_name->str); - g_string_assign(context->field_name, ""); - } else { - g_string_append_printf(context->string, "[%s]", - sequence->length_field_name->str); - } -end: - return ret; -} - -static -int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context) -{ - struct bt_ctf_field_type_string *string = container_of( - type, struct bt_ctf_field_type_string, parent); - - g_string_append_printf(context->string, - "string { encoding = %s; }", - get_encoding_string(string->declaration.encoding)); - return 0; -} - -static -enum bt_ctf_byte_order get_ctf_ir_byte_order(int byte_order) { - enum bt_ctf_byte_order ret; - - switch (byte_order) { - case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: - case LITTLE_ENDIAN: - ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; - break; - case BT_CTF_BYTE_ORDER_BIG_ENDIAN: - case BIG_ENDIAN: - ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; - break; - case BT_CTF_BYTE_ORDER_NETWORK: - ret = BT_CTF_BYTE_ORDER_NETWORK; - break; - case BT_CTF_BYTE_ORDER_NATIVE: - ret = BT_CTF_BYTE_ORDER_NATIVE; - break; - default: - ret = BT_CTF_BYTE_ORDER_UNKNOWN; - break; - } - - return ret; -} - -static -void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type, - int byte_order, int set_native) -{ - struct bt_ctf_field_type_integer *integer_type = container_of(type, - struct bt_ctf_field_type_integer, parent); - - if (set_native) { - if (integer_type->user_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { - /* - * User byte order is native, so we can set - * the real byte order. - */ - integer_type->declaration.byte_order = - byte_order; - } - } else { - integer_type->user_byte_order = - get_ctf_ir_byte_order(byte_order); - integer_type->declaration.byte_order = byte_order; - } -} - -static -void bt_ctf_field_type_enumeration_set_byte_order( - struct bt_ctf_field_type *type, int byte_order, int set_native) -{ - struct bt_ctf_field_type_enumeration *enum_type = container_of(type, - struct bt_ctf_field_type_enumeration, parent); - - /* Safe to assume that container is an integer */ - bt_ctf_field_type_integer_set_byte_order(enum_type->container, - byte_order, set_native); -} - -static -void bt_ctf_field_type_floating_point_set_byte_order( - struct bt_ctf_field_type *type, int byte_order, int set_native) -{ - struct bt_ctf_field_type_floating_point *floating_point_type = - container_of(type, struct bt_ctf_field_type_floating_point, - parent); - - if (set_native) { - if (floating_point_type->user_byte_order == - BT_CTF_BYTE_ORDER_NATIVE) { - /* - * User byte order is native, so we can set - * the real byte order. - */ - floating_point_type->declaration.byte_order = - byte_order; - floating_point_type->sign.byte_order = - byte_order; - floating_point_type->mantissa.byte_order = - byte_order; - floating_point_type->exp.byte_order = - byte_order; - } - } else { - floating_point_type->user_byte_order = - get_ctf_ir_byte_order(byte_order); - floating_point_type->declaration.byte_order = byte_order; - floating_point_type->sign.byte_order = byte_order; - floating_point_type->mantissa.byte_order = byte_order; - floating_point_type->exp.byte_order = byte_order; - } -} - -static -void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type, - int byte_order, int set_native) -{ - int i; - struct bt_ctf_field_type_structure *structure_type = - container_of(type, struct bt_ctf_field_type_structure, - parent); - - for (i = 0; i < structure_type->fields->len; i++) { - struct structure_field *field = g_ptr_array_index( - structure_type->fields, i); - struct bt_ctf_field_type *field_type = field->type; - - if (set_byte_order_funcs[field_type->declaration->id]) { - set_byte_order_funcs[field_type->declaration->id]( - field_type, byte_order, set_native); - } - } -} - -static -void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type, - int byte_order, int set_native) -{ - int i; - struct bt_ctf_field_type_variant *variant_type = - container_of(type, struct bt_ctf_field_type_variant, - parent); - - for (i = 0; i < variant_type->fields->len; i++) { - struct structure_field *field = g_ptr_array_index( - variant_type->fields, i); - struct bt_ctf_field_type *field_type = field->type; - - if (set_byte_order_funcs[field_type->declaration->id]) { - set_byte_order_funcs[field_type->declaration->id]( - field_type, byte_order, set_native); - } - } -} - -static -void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type, - int byte_order, int set_native) -{ - struct bt_ctf_field_type_array *array_type = - container_of(type, struct bt_ctf_field_type_array, - parent); - - if (set_byte_order_funcs[array_type->element_type->declaration->id]) { - set_byte_order_funcs[array_type->element_type->declaration->id]( - array_type->element_type, byte_order, set_native); - } -} - -static -void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type, - int byte_order, int set_native) -{ - struct bt_ctf_field_type_sequence *sequence_type = - container_of(type, struct bt_ctf_field_type_sequence, - parent); - - if (set_byte_order_funcs[ - sequence_type->element_type->declaration->id]) { - set_byte_order_funcs[ - sequence_type->element_type->declaration->id]( - sequence_type->element_type, byte_order, set_native); - } -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy; - struct bt_ctf_field_type_integer *integer, *copy_integer; - - integer = container_of(type, struct bt_ctf_field_type_integer, parent); - copy = bt_ctf_field_type_integer_create(integer->declaration.len); - if (!copy) { - goto end; - } - - copy_integer = container_of(copy, struct bt_ctf_field_type_integer, - parent); - copy_integer->declaration = integer->declaration; - if (integer->mapped_clock) { - bt_get(integer->mapped_clock); - copy_integer->mapped_clock = integer->mapped_clock; - } - - copy_integer->user_byte_order = integer->user_byte_order; - -end: - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( - struct bt_ctf_field_type *type) -{ - size_t i; - struct bt_ctf_field_type *copy = NULL, *copy_container; - struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration; - - enumeration = container_of(type, struct bt_ctf_field_type_enumeration, - parent); - - /* Copy the source enumeration's container */ - copy_container = bt_ctf_field_type_copy(enumeration->container); - if (!copy_container) { - goto end; - } - - copy = bt_ctf_field_type_enumeration_create(copy_container); - if (!copy) { - goto end; - } - copy_enumeration = container_of(copy, - struct bt_ctf_field_type_enumeration, parent); - - /* Copy all enumaration entries */ - for (i = 0; i < enumeration->entries->len; i++) { - struct enumeration_mapping *mapping = g_ptr_array_index( - enumeration->entries, i); - struct enumeration_mapping* copy_mapping = g_new0( - struct enumeration_mapping, 1); - - if (!copy_mapping) { - goto error; - } - - *copy_mapping = *mapping; - g_ptr_array_add(copy_enumeration->entries, copy_mapping); - } - - copy_enumeration->declaration = enumeration->declaration; -end: - bt_put(copy_container); - return copy; -error: - bt_put(copy_container); - BT_PUT(copy); - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy; - struct bt_ctf_field_type_floating_point *floating_point, *copy_float; - - floating_point = container_of(type, - struct bt_ctf_field_type_floating_point, parent); - copy = bt_ctf_field_type_floating_point_create(); - if (!copy) { - goto end; - } - - copy_float = container_of(copy, - struct bt_ctf_field_type_floating_point, parent); - copy_float->declaration = floating_point->declaration; - copy_float->sign = floating_point->sign; - copy_float->mantissa = floating_point->mantissa; - copy_float->exp = floating_point->exp; - copy_float->user_byte_order = floating_point->user_byte_order; - copy_float->declaration.sign = ©_float->sign; - copy_float->declaration.mantissa = ©_float->mantissa; - copy_float->declaration.exp = ©_float->exp; -end: - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( - struct bt_ctf_field_type *type) -{ - int i; - GHashTableIter iter; - gpointer key, value; - struct bt_ctf_field_type *copy; - struct bt_ctf_field_type_structure *structure, *copy_structure; - - structure = container_of(type, struct bt_ctf_field_type_structure, - parent); - copy = bt_ctf_field_type_structure_create(); - if (!copy) { - goto end; - } - - copy_structure = container_of(copy, - struct bt_ctf_field_type_structure, parent); - - /* Copy field_name_to_index */ - g_hash_table_iter_init(&iter, structure->field_name_to_index); - while (g_hash_table_iter_next (&iter, &key, &value)) { - g_hash_table_insert(copy_structure->field_name_to_index, - key, value); - } - - for (i = 0; i < structure->fields->len; i++) { - struct structure_field *entry, *copy_entry; - struct bt_ctf_field_type *copy_field; - - copy_entry = g_new0(struct structure_field, 1); - if (!copy_entry) { - goto error; - } - - entry = g_ptr_array_index(structure->fields, i); - copy_field = bt_ctf_field_type_copy(entry->type); - if (!copy_field) { - g_free(copy_entry); - goto error; - } - - copy_entry->name = entry->name; - copy_entry->type = copy_field; - g_ptr_array_add(copy_structure->fields, copy_entry); - } - - copy_structure->declaration = structure->declaration; -end: - return copy; -error: - BT_PUT(copy); - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( - struct bt_ctf_field_type *type) -{ - int i; - GHashTableIter iter; - gpointer key, value; - struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL; - struct bt_ctf_field_type_variant *variant, *copy_variant; - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - if (variant->tag) { - copy_tag = bt_ctf_field_type_copy(&variant->tag->parent); - if (!copy_tag) { - goto end; - } - } - - copy = bt_ctf_field_type_variant_create(copy_tag, - variant->tag_name->len ? variant->tag_name->str : NULL); - if (!copy) { - goto end; - } - - copy_variant = container_of(copy, struct bt_ctf_field_type_variant, - parent); - - /* Copy field_name_to_index */ - g_hash_table_iter_init(&iter, variant->field_name_to_index); - while (g_hash_table_iter_next (&iter, &key, &value)) { - g_hash_table_insert(copy_variant->field_name_to_index, - key, value); - } - - for (i = 0; i < variant->fields->len; i++) { - struct structure_field *entry, *copy_entry; - struct bt_ctf_field_type *copy_field; - - copy_entry = g_new0(struct structure_field, 1); - if (!copy_entry) { - goto error; - } - - entry = g_ptr_array_index(variant->fields, i); - copy_field = bt_ctf_field_type_copy(entry->type); - if (!copy_field) { - g_free(copy_entry); - goto error; - } - - copy_entry->name = entry->name; - copy_entry->type = copy_field; - g_ptr_array_add(copy_variant->fields, copy_entry); - } - - copy_variant->declaration = variant->declaration; - if (variant->tag_field_path) { - copy_variant->tag_field_path = bt_ctf_field_path_copy( - variant->tag_field_path); - if (!copy_variant->tag_field_path) { - goto error; - } - } -end: - bt_put(copy_tag); - return copy; -error: - bt_put(copy_tag); - BT_PUT(copy); - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_array_copy( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy = NULL, *copy_element; - struct bt_ctf_field_type_array *array, *copy_array; - - array = container_of(type, struct bt_ctf_field_type_array, - parent); - copy_element = bt_ctf_field_type_copy(array->element_type); - if (!copy_element) { - goto end; - } - - copy = bt_ctf_field_type_array_create(copy_element, array->length); - if (!copy) { - goto end; - } - - copy_array = container_of(copy, struct bt_ctf_field_type_array, - parent); - copy_array->declaration = array->declaration; -end: - bt_put(copy_element); - return copy; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy = NULL, *copy_element; - struct bt_ctf_field_type_sequence *sequence, *copy_sequence; - - sequence = container_of(type, struct bt_ctf_field_type_sequence, - parent); - copy_element = bt_ctf_field_type_copy(sequence->element_type); - if (!copy_element) { - goto end; - } - - copy = bt_ctf_field_type_sequence_create(copy_element, - sequence->length_field_name->len ? - sequence->length_field_name->str : NULL); - if (!copy) { - goto end; - } - - copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence, - parent); - copy_sequence->declaration = sequence->declaration; - if (sequence->length_field_path) { - copy_sequence->length_field_path = bt_ctf_field_path_copy( - sequence->length_field_path); - if (!copy_sequence->length_field_path) { - goto error; - } - } -end: - bt_put(copy_element); - return copy; -error: - BT_PUT(copy); - goto end; -} - -static -struct bt_ctf_field_type *bt_ctf_field_type_string_copy( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_type *copy; - struct bt_ctf_field_type_string *string, *copy_string; - - copy = bt_ctf_field_type_string_create(); - if (!copy) { - goto end; - } - - string = container_of(type, struct bt_ctf_field_type_string, - parent); - copy_string = container_of(type, struct bt_ctf_field_type_string, - parent); - copy_string->declaration = string->declaration; -end: - return copy; -} - -static -int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - struct bt_ctf_field_type_integer *integer_a; - struct bt_ctf_field_type_integer *integer_b; - struct declaration_integer *decl_a; - struct declaration_integer *decl_b; - - integer_a = container_of(type_a, struct bt_ctf_field_type_integer, - parent); - integer_b = container_of(type_b, struct bt_ctf_field_type_integer, - parent); - decl_a = &integer_a->declaration; - decl_b = &integer_b->declaration; - - /* Length */ - if (decl_a->len != decl_b->len) { - goto end; - } - - /* - * Compare user byte orders only, not the cached, - * real byte orders. - */ - if (integer_a->user_byte_order != integer_b->user_byte_order) { - goto end; - } - - /* Signedness */ - if (decl_a->signedness != decl_b->signedness) { - goto end; - } - - /* Base */ - if (decl_a->base != decl_b->base) { - goto end; - } - - /* Encoding */ - if (decl_a->encoding != decl_b->encoding) { - goto end; - } - - /* Mapped clock */ - if (integer_a->mapped_clock != integer_b->mapped_clock) { - goto end; - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - struct bt_ctf_field_type_floating_point *float_a; - struct bt_ctf_field_type_floating_point *float_b; - - float_a = container_of(type_a, - struct bt_ctf_field_type_floating_point, parent); - float_b = container_of(type_b, - struct bt_ctf_field_type_floating_point, parent); - - /* Sign length */ - if (float_a->sign.len != float_b->sign.len) { - goto end; - } - - /* Exponent length */ - if (float_a->exp.len != float_b->exp.len) { - goto end; - } - - /* Mantissa length */ - if (float_a->mantissa.len != float_b->mantissa.len) { - goto end; - } - - /* - * Compare user byte orders only, not the cached, - * real byte orders. - */ - if (float_a->user_byte_order != float_b->user_byte_order) { - goto end; - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int compare_enumeration_mappings(struct enumeration_mapping *mapping_a, - struct enumeration_mapping *mapping_b) -{ - int ret = 1; - - /* Label */ - if (mapping_a->string != mapping_b->string) { - goto end; - } - - /* Range start */ - if (mapping_a->range_start._unsigned != - mapping_b->range_start._unsigned) { - goto end; - } - - /* Range end */ - if (mapping_a->range_end._unsigned != - mapping_b->range_end._unsigned) { - goto end; - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - int i; - struct bt_ctf_field_type_enumeration *enum_a; - struct bt_ctf_field_type_enumeration *enum_b; - - enum_a = container_of(type_a, - struct bt_ctf_field_type_enumeration, parent); - enum_b = container_of(type_b, - struct bt_ctf_field_type_enumeration, parent); - - /* Container field type */ - ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container); - if (ret) { - goto end; - } - - ret = 1; - - /* Entries */ - if (enum_a->entries->len != enum_b->entries->len) { - goto end; - } - - for (i = 0; i < enum_a->entries->len; ++i) { - struct enumeration_mapping *mapping_a = - g_ptr_array_index(enum_a->entries, i); - struct enumeration_mapping *mapping_b = - g_ptr_array_index(enum_b->entries, i); - - if (compare_enumeration_mappings(mapping_a, mapping_b)) { - goto end; - } - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - struct bt_ctf_field_type_string *string_a; - struct bt_ctf_field_type_string *string_b; - - string_a = container_of(type_a, - struct bt_ctf_field_type_string, parent); - string_b = container_of(type_b, - struct bt_ctf_field_type_string, parent); - - /* Encoding */ - if (string_a->declaration.encoding != string_b->declaration.encoding) { - goto end; - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int compare_structure_fields(struct structure_field *field_a, - struct structure_field *field_b) -{ - int ret = 1; - - /* Label */ - if (field_a->name != field_b->name) { - goto end; - } - - /* Type */ - ret = bt_ctf_field_type_compare(field_a->type, field_b->type); - -end: - return ret; -} - -static -int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - int i; - struct bt_ctf_field_type_structure *struct_a; - struct bt_ctf_field_type_structure *struct_b; - - struct_a = container_of(type_a, - struct bt_ctf_field_type_structure, parent); - struct_b = container_of(type_b, - struct bt_ctf_field_type_structure, parent); - - /* Alignment */ - if (bt_ctf_field_type_get_alignment(type_a) != - bt_ctf_field_type_get_alignment(type_b)) { - goto end; - } - - /* Fields */ - if (struct_a->fields->len != struct_b->fields->len) { - goto end; - } - - for (i = 0; i < struct_a->fields->len; ++i) { - struct structure_field *field_a = - g_ptr_array_index(struct_a->fields, i); - struct structure_field *field_b = - g_ptr_array_index(struct_b->fields, i); - - ret = compare_structure_fields(field_a, field_b); - if (ret) { - goto end; - } - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - int i; - struct bt_ctf_field_type_variant *variant_a; - struct bt_ctf_field_type_variant *variant_b; - - variant_a = container_of(type_a, - struct bt_ctf_field_type_variant, parent); - variant_b = container_of(type_b, - struct bt_ctf_field_type_variant, parent); - - /* Tag name */ - if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) { - goto end; - } - - /* Tag type */ - ret = bt_ctf_field_type_compare( - (struct bt_ctf_field_type *) variant_a->tag, - (struct bt_ctf_field_type *) variant_b->tag); - if (ret) { - goto end; - } - - ret = 1; - - /* Fields */ - if (variant_a->fields->len != variant_b->fields->len) { - goto end; - } - - for (i = 0; i < variant_a->fields->len; ++i) { - struct structure_field *field_a = - g_ptr_array_index(variant_a->fields, i); - struct structure_field *field_b = - g_ptr_array_index(variant_b->fields, i); - - ret = compare_structure_fields(field_a, field_b); - if (ret) { - goto end; - } - } - - /* Equal */ - ret = 0; - -end: - return ret; -} - -static -int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - struct bt_ctf_field_type_array *array_a; - struct bt_ctf_field_type_array *array_b; - - array_a = container_of(type_a, - struct bt_ctf_field_type_array, parent); - array_b = container_of(type_b, - struct bt_ctf_field_type_array, parent); - - /* Length */ - if (array_a->length != array_b->length) { - goto end; - } - - /* Element type */ - ret = bt_ctf_field_type_compare(array_a->element_type, - array_b->element_type); - -end: - return ret; -} - -static -int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = -1; - struct bt_ctf_field_type_sequence *sequence_a; - struct bt_ctf_field_type_sequence *sequence_b; - - sequence_a = container_of(type_a, - struct bt_ctf_field_type_sequence, parent); - sequence_b = container_of(type_b, - struct bt_ctf_field_type_sequence, parent); - - /* Length name */ - if (strcmp(sequence_a->length_field_name->str, - sequence_b->length_field_name->str)) { - goto end; - } - - /* Element type */ - ret = bt_ctf_field_type_compare(sequence_a->element_type, - sequence_b->element_type); - -end: - return ret; -} - -int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b) -{ - int ret = 1; - - if (type_a == type_b) { - /* Same reference: equal (even if both are NULL) */ - ret = 0; - goto end; - } - - if (!type_a || !type_b) { - ret = -1; - goto end; - } - - if (type_a->declaration->id != type_b->declaration->id) { - /* Different type IDs */ - goto end; - } - - if (type_a->declaration->id == BT_CTF_TYPE_ID_UNKNOWN) { - /* Both have unknown type IDs */ - goto end; - } - - ret = type_compare_funcs[type_a->declaration->id](type_a, type_b); - -end: - return ret; -} - -BT_HIDDEN -int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *field_type) -{ - int field_count = -1; - enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); - - switch (type_id) { - case CTF_TYPE_STRUCT: - field_count = - bt_ctf_field_type_structure_get_field_count(field_type); - break; - case CTF_TYPE_VARIANT: - field_count = - bt_ctf_field_type_variant_get_field_count(field_type); - break; - case CTF_TYPE_ARRAY: - case CTF_TYPE_SEQUENCE: - /* - * Array and sequence types always contain a single member - * (the element type). - */ - field_count = 1; - break; - default: - break; - } - - return field_count; -} - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index( - struct bt_ctf_field_type *field_type, int index) -{ - struct bt_ctf_field_type *field = NULL; - enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); - - switch (type_id) { - case CTF_TYPE_STRUCT: - bt_ctf_field_type_structure_get_field(field_type, NULL, &field, - index); - break; - case CTF_TYPE_VARIANT: - bt_ctf_field_type_variant_get_field(field_type, NULL, - &field, index); - break; - case CTF_TYPE_ARRAY: - field = bt_ctf_field_type_array_get_element_type(field_type); - break; - case CTF_TYPE_SEQUENCE: - field = bt_ctf_field_type_sequence_get_element_type(field_type); - break; - default: - break; - } - - return field; -} - -BT_HIDDEN -int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *field_type, - const char *name) -{ - int field_index = -1; - enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); - - switch (type_id) { - case CTF_TYPE_STRUCT: - field_index = bt_ctf_field_type_structure_get_field_name_index( - field_type, name); - break; - case CTF_TYPE_VARIANT: - field_index = bt_ctf_field_type_variant_get_field_name_index( - field_type, name); - break; - default: - break; - } - - return field_index; -} - -struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_path *field_path = NULL; - struct bt_ctf_field_type_variant *variant; - - if (!type || !bt_ctf_field_type_is_variant(type)) { - goto end; - } - - variant = container_of(type, struct bt_ctf_field_type_variant, - parent); - field_path = bt_get(variant->tag_field_path); -end: - return field_path; -} - -struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( - struct bt_ctf_field_type *type) -{ - struct bt_ctf_field_path *field_path = NULL; - struct bt_ctf_field_type_sequence *sequence; - - if (!type || !bt_ctf_field_type_is_sequence(type)) { - goto end; - } - - sequence = container_of(type, struct bt_ctf_field_type_sequence, - parent); - field_path = bt_get(sequence->length_field_path); -end: - return field_path; -} diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index f9779e24..8ddf8034 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -26,11 +26,8 @@ * SOFTWARE. */ -#include -#include -#include -#include -#include +#include +#include #include #include #include diff --git a/formats/ctf/ir/field-path.c b/formats/ctf/ir/field-path.c index 44de8cd4..6c0b248a 100644 --- a/formats/ctf/ir/field-path.c +++ b/formats/ctf/ir/field-path.c @@ -25,7 +25,7 @@ * SOFTWARE. */ -#include +#include #include #include #include diff --git a/formats/ctf/ir/field-types.c b/formats/ctf/ir/field-types.c new file mode 100644 index 00000000..1613dd19 --- /dev/null +++ b/formats/ctf/ir/field-types.c @@ -0,0 +1,4101 @@ +/* + * field-types.c + * + * Babeltrace CTF IR - Event Types + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct range_overlap_query { + union { + uint64_t _unsigned; + int64_t _signed; + } range_start; + + union { + uint64_t _unsigned; + int64_t _signed; + } range_end; + int overlaps; + GQuark mapping_name; +}; + +static +void bt_ctf_field_type_destroy(struct bt_object *); +static +void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *); + +static +void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_destroy, + [BT_CTF_TYPE_ID_ENUM] = + bt_ctf_field_type_enumeration_destroy, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_type_floating_point_destroy, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_destroy, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_destroy, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_destroy, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_destroy, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_destroy, +}; + +static +void generic_field_type_freeze(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *); +static +void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *); + +static +type_freeze_func const type_freeze_funcs[] = { + [BT_CTF_TYPE_ID_INTEGER] = generic_field_type_freeze, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_freeze, + [BT_CTF_TYPE_ID_FLOAT] = generic_field_type_freeze, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_freeze, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_freeze, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_freeze, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_freeze, + [BT_CTF_TYPE_ID_STRING] = generic_field_type_freeze, +}; + +static +int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_floating_point_serialize( + struct bt_ctf_field_type *, struct metadata_context *); +static +int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *, + struct metadata_context *); +static +int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *, + struct metadata_context *); + +static +type_serialize_func const type_serialize_funcs[] = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_serialize, + [BT_CTF_TYPE_ID_ENUM] = + bt_ctf_field_type_enumeration_serialize, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_type_floating_point_serialize, + [BT_CTF_TYPE_ID_STRUCT] = + bt_ctf_field_type_structure_serialize, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_serialize, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_serialize, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_serialize, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_serialize, +}; + +static +void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); +static +void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); +static +void bt_ctf_field_type_floating_point_set_byte_order( + struct bt_ctf_field_type *, int byte_order, int set_native); +static +void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); +static +void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); +static +void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); +static +void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *, + int byte_order, int set_native); + +/* The set_native flag only set the byte order if it is set to native */ +static +void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *, + int byte_order, int set_native) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_set_byte_order, + [BT_CTF_TYPE_ID_ENUM] = + bt_ctf_field_type_enumeration_set_byte_order, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_type_floating_point_set_byte_order, + [BT_CTF_TYPE_ID_STRUCT] = + bt_ctf_field_type_structure_set_byte_order, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_set_byte_order, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_set_byte_order, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order, + [BT_CTF_TYPE_ID_STRING] = NULL, +}; + +static +struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_array_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( + struct bt_ctf_field_type *); +static +struct bt_ctf_field_type *bt_ctf_field_type_string_copy( + struct bt_ctf_field_type *); + +static +struct bt_ctf_field_type *(* const type_copy_funcs[])( + struct bt_ctf_field_type *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_copy, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_copy, + [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_copy, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_copy, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_copy, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_copy, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_copy, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_copy, +}; + +static +int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); +static +int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *, + struct bt_ctf_field_type *); + +static +int (* const type_compare_funcs[])(struct bt_ctf_field_type *, + struct bt_ctf_field_type *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_compare, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_compare, + [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_type_floating_point_compare, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_compare, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_compare, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_compare, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_compare, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_compare, +}; + +static +int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *); +static +int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *); +static +int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *); +static +int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *); +static +int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *); + +static +int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = { + [BT_CTF_TYPE_ID_INTEGER] = NULL, + [BT_CTF_TYPE_ID_FLOAT] = NULL, + [BT_CTF_TYPE_ID_STRING] = NULL, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_validate, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_validate, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_type_variant_validate, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_type_array_validate, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_type_sequence_validate, +}; + +static +void destroy_enumeration_mapping(struct enumeration_mapping *mapping) +{ + g_free(mapping); +} + +static +void destroy_structure_field(struct structure_field *field) +{ + bt_put(field->type); + g_free(field); +} + +static +void check_ranges_overlap(gpointer element, gpointer query) +{ + struct enumeration_mapping *mapping = element; + struct range_overlap_query *overlap_query = query; + + if (mapping->range_start._signed <= overlap_query->range_end._signed + && overlap_query->range_start._signed <= + mapping->range_end._signed) { + overlap_query->overlaps = 1; + overlap_query->mapping_name = mapping->string; + } + + overlap_query->overlaps |= + mapping->string == overlap_query->mapping_name; +} + +static +void check_ranges_overlap_unsigned(gpointer element, gpointer query) +{ + struct enumeration_mapping *mapping = element; + struct range_overlap_query *overlap_query = query; + + if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned + && overlap_query->range_start._unsigned <= + mapping->range_end._unsigned) { + overlap_query->overlaps = 1; + overlap_query->mapping_name = mapping->string; + } + + overlap_query->overlaps |= + mapping->string == overlap_query->mapping_name; +} + +static +gint compare_enumeration_mappings_signed(struct enumeration_mapping **a, + struct enumeration_mapping **b) +{ + return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1; +} + +static +gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a, + struct enumeration_mapping **b) +{ + return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1; +} + +static +void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo) +{ + enum bt_ctf_type_id type_id = type->declaration->id; + + assert(type && (type_id > BT_CTF_TYPE_ID_UNKNOWN) && + (type_id < BT_CTF_NR_TYPE_IDS)); + + bt_object_init(type, bt_ctf_field_type_destroy); + type->freeze = type_freeze_funcs[type_id]; + type->serialize = type_serialize_funcs[type_id]; + + if (init_bo) { + int ret = bt_ctf_field_type_set_byte_order(type, + BT_CTF_BYTE_ORDER_NATIVE); + assert(!ret); + } + + type->declaration->alignment = 1; +} + +static +int add_structure_field(GPtrArray *fields, + GHashTable *field_name_to_index, + struct bt_ctf_field_type *field_type, + const char *field_name) +{ + int ret = 0; + GQuark name_quark = g_quark_from_string(field_name); + struct structure_field *field; + + /* Make sure structure does not contain a field of the same name */ + if (g_hash_table_lookup_extended(field_name_to_index, + GUINT_TO_POINTER(name_quark), NULL, NULL)) { + ret = -1; + goto end; + } + + field = g_new0(struct structure_field, 1); + if (!field) { + ret = -1; + goto end; + } + + bt_get(field_type); + field->name = name_quark; + field->type = field_type; + g_hash_table_insert(field_name_to_index, + (gpointer) (unsigned long) name_quark, + (gpointer) (unsigned long) fields->len); + g_ptr_array_add(fields, field); +end: + return ret; +} + +static +void bt_ctf_field_type_destroy(struct bt_object *obj) +{ + struct bt_ctf_field_type *type; + enum bt_ctf_type_id type_id; + + type = container_of(obj, struct bt_ctf_field_type, base); + type_id = type->declaration->id; + if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || + type_id >= BT_CTF_NR_TYPE_IDS) { + return; + } + + type_destroy_funcs[type_id](type); +} + +static +int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + + struct bt_ctf_field_type_enumeration *enumeration = + container_of(type, struct bt_ctf_field_type_enumeration, + parent); + struct bt_ctf_field_type *container_type = + bt_ctf_field_type_enumeration_get_container_type(type); + + if (!container_type) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_type_validate(container_type); + if (ret) { + goto end; + } + + /* Ensure enum has entries */ + ret = enumeration->entries->len ? 0 : -1; + +end: + BT_PUT(container_type); + return ret; +} + +static +int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type *element_type = NULL; + struct bt_ctf_field_type_sequence *sequence = + container_of(type, struct bt_ctf_field_type_sequence, + parent); + + /* Length field name should be set at this point */ + if (sequence->length_field_name->len == 0) { + ret = -1; + goto end; + } + + element_type = bt_ctf_field_type_sequence_get_element_type(type); + if (!element_type) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_type_validate(element_type); + +end: + BT_PUT(element_type); + + return ret; +} + +static +int bt_ctf_field_type_array_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type *element_type = NULL; + + element_type = bt_ctf_field_type_array_get_element_type(type); + if (!element_type) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_type_validate(element_type); + +end: + BT_PUT(element_type); + + return ret; +} + +static +int bt_ctf_field_type_structure_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type *child_type = NULL; + int field_count = bt_ctf_field_type_structure_get_field_count(type); + int i; + + if (field_count < 0) { + ret = -1; + goto end; + } + + for (i = 0; i < field_count; ++i) { + ret = bt_ctf_field_type_structure_get_field(type, + NULL, &child_type, i); + if (ret) { + goto end; + } + + ret = bt_ctf_field_type_validate(child_type); + if (ret) { + goto end; + } + + BT_PUT(child_type); + } + +end: + BT_PUT(child_type); + + return ret; +} + +static +int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + int field_count; + struct bt_ctf_field_type *child_type = NULL; + struct bt_ctf_field_type_variant *variant = + container_of(type, struct bt_ctf_field_type_variant, + parent); + int i; + int tag_mappings_count; + + if (variant->tag_name->len == 0 || !variant->tag) { + ret = -1; + goto end; + } + + tag_mappings_count = + bt_ctf_field_type_enumeration_get_mapping_count( + (struct bt_ctf_field_type *) variant->tag); + + if (tag_mappings_count != variant->fields->len) { + ret = -1; + goto end; + } + + for (i = 0; i < tag_mappings_count; ++i) { + const char *label; + int64_t range_start, range_end; + struct bt_ctf_field_type *ft; + + ret = bt_ctf_field_type_enumeration_get_mapping( + (struct bt_ctf_field_type *) variant->tag, + i, &label, &range_start, &range_end); + if (ret) { + goto end; + } + if (!label) { + ret = -1; + goto end; + } + + ft = bt_ctf_field_type_variant_get_field_type_by_name( + type, label); + if (!ft) { + ret = -1; + goto end; + } + + BT_PUT(ft); + } + + field_count = bt_ctf_field_type_variant_get_field_count(type); + if (field_count < 0) { + ret = -1; + goto end; + } + + for (i = 0; i < field_count; ++i) { + ret = bt_ctf_field_type_variant_get_field(type, + NULL, &child_type, i); + if (ret) { + goto end; + } + + ret = bt_ctf_field_type_validate(child_type); + if (ret) { + goto end; + } + + BT_PUT(child_type); + } + +end: + BT_PUT(child_type); + + return ret; +} + +/* + * This function validates a given field type without considering + * where this field type is located. It only validates the properties + * of the given field type and the properties of its children if + * applicable. + */ +BT_HIDDEN +int bt_ctf_field_type_validate(struct bt_ctf_field_type *type) +{ + int ret = 0; + enum bt_ctf_type_id id = bt_ctf_field_type_get_type_id(type); + + if (!type) { + ret = -1; + goto end; + } + + if (type->valid) { + /* Already marked as valid */ + goto end; + } + + if (type_validate_funcs[id]) { + ret = type_validate_funcs[id](type); + } + + if (!ret && type->frozen) { + /* Field type is valid */ + type->valid = 1; + } + +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) +{ + struct bt_ctf_field_type_integer *integer = + g_new0(struct bt_ctf_field_type_integer, 1); + + if (!integer || size == 0 || size > 64) { + return NULL; + } + + integer->parent.declaration = &integer->declaration.p; + integer->parent.declaration->id = BT_CTF_TYPE_ID_INTEGER; + integer->declaration.len = size; + integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL; + integer->declaration.encoding = BT_CTF_STRING_ENCODING_NONE; + bt_ctf_field_type_init(&integer->parent, TRUE); + return &integer->parent; +} + +int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + ret = -1; + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + ret = (int) integer->declaration.len; +end: + return ret; +} + +int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + ret = -1; + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + ret = integer->declaration.signedness; +end: + return ret; +} + +int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type, + int is_signed) +{ + int ret = 0; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->frozen || + type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + ret = -1; + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + integer->declaration.signedness = !!is_signed; +end: + return ret; +} + +enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( + struct bt_ctf_field_type *type) +{ + enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + ret = integer->declaration.base; +end: + return ret; +} + +int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type, + enum bt_ctf_integer_base base) +{ + int ret = 0; + + if (!type || type->frozen || + type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + ret = -1; + goto end; + } + + switch (base) { + case BT_CTF_INTEGER_BASE_BINARY: + case BT_CTF_INTEGER_BASE_OCTAL: + case BT_CTF_INTEGER_BASE_DECIMAL: + case BT_CTF_INTEGER_BASE_HEXADECIMAL: + { + struct bt_ctf_field_type_integer *integer = container_of(type, + struct bt_ctf_field_type_integer, parent); + integer->declaration.base = base; + break; + } + default: + ret = -1; + } +end: + return ret; +} + +enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( + struct bt_ctf_field_type *type) +{ + enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + ret = integer->declaration.encoding; +end: + return ret; +} + +int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type, + enum bt_ctf_string_encoding encoding) +{ + int ret = 0; + struct bt_ctf_field_type_integer *integer; + + if (!type || type->frozen || + (type->declaration->id != BT_CTF_TYPE_ID_INTEGER) || + (encoding < BT_CTF_STRING_ENCODING_NONE) || + (encoding >= BT_CTF_STRING_ENCODING_UNKNOWN)) { + ret = -1; + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + integer->declaration.encoding = encoding; +end: + return ret; +} + +struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_integer *integer; + struct bt_ctf_clock *clock = NULL; + + if (!type) { + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + clock = integer->mapped_clock; + bt_get(clock); +end: + return clock; +} + +int bt_ctf_field_type_integer_set_mapped_clock( + struct bt_ctf_field_type *type, + struct bt_ctf_clock *clock) +{ + struct bt_ctf_field_type_integer *integer; + int ret = 0; + + if (!type || type->frozen) { + ret = -1; + goto end; + } + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + bt_put(integer->mapped_clock); + bt_get(clock); + integer->mapped_clock = clock; +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( + struct bt_ctf_field_type *integer_container_type) +{ + struct bt_ctf_field_type_enumeration *enumeration = NULL; + + if (!integer_container_type) { + goto error; + } + + if (integer_container_type->declaration->id != BT_CTF_TYPE_ID_INTEGER) { + goto error; + } + + enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1); + if (!enumeration) { + goto error; + } + + enumeration->parent.declaration = &enumeration->declaration.p; + enumeration->parent.declaration->id = BT_CTF_TYPE_ID_ENUM; + bt_get(integer_container_type); + enumeration->container = integer_container_type; + enumeration->entries = g_ptr_array_new_with_free_func( + (GDestroyNotify)destroy_enumeration_mapping); + bt_ctf_field_type_init(&enumeration->parent, FALSE); + return &enumeration->parent; +error: + g_free(enumeration); + return NULL; +} + +struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *container_type = NULL; + struct bt_ctf_field_type_enumeration *enumeration_type; + + if (!type) { + goto end; + } + + if (type->declaration->id != BT_CTF_TYPE_ID_ENUM) { + goto end; + } + + enumeration_type = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + container_type = enumeration_type->container; + bt_get(container_type); +end: + return container_type; +} + +int bt_ctf_field_type_enumeration_add_mapping( + struct bt_ctf_field_type *type, const char *string, + int64_t range_start, int64_t range_end) +{ + int ret = 0; + GQuark mapping_name; + struct enumeration_mapping *mapping; + struct bt_ctf_field_type_enumeration *enumeration; + struct range_overlap_query query; + char *escaped_string; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) || + type->frozen || + (range_end < range_start)) { + ret = -1; + goto end; + } + + if (!string || strlen(string) == 0) { + ret = -1; + goto end; + } + + escaped_string = g_strescape(string, NULL); + if (!escaped_string) { + ret = -1; + goto end; + } + + mapping_name = g_quark_from_string(escaped_string); + query = (struct range_overlap_query) { + .range_start._signed = range_start, + .range_end._signed = range_end, + .mapping_name = mapping_name, + .overlaps = 0 }; + enumeration = container_of(type, struct bt_ctf_field_type_enumeration, + parent); + + /* Check that the range does not overlap with one already present */ + g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query); + if (query.overlaps) { + ret = -1; + goto error_free; + } + + mapping = g_new(struct enumeration_mapping, 1); + if (!mapping) { + ret = -1; + goto error_free; + } + + *mapping = (struct enumeration_mapping) { + .range_start._signed = range_start, + .range_end._signed = range_end, .string = mapping_name}; + g_ptr_array_add(enumeration->entries, mapping); + g_ptr_array_sort(enumeration->entries, + (GCompareFunc)compare_enumeration_mappings_signed); +error_free: + free(escaped_string); +end: + return ret; +} + +int bt_ctf_field_type_enumeration_add_mapping_unsigned( + struct bt_ctf_field_type *type, const char *string, + uint64_t range_start, uint64_t range_end) +{ + int ret = 0; + GQuark mapping_name; + struct enumeration_mapping *mapping; + struct bt_ctf_field_type_enumeration *enumeration; + struct range_overlap_query query; + char *escaped_string; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) || + type->frozen || + (range_end < range_start)) { + ret = -1; + goto end; + } + + if (!string || strlen(string) == 0) { + ret = -1; + goto end; + } + + escaped_string = g_strescape(string, NULL); + if (!escaped_string) { + ret = -1; + goto end; + } + + mapping_name = g_quark_from_string(escaped_string); + query = (struct range_overlap_query) { + .range_start._unsigned = range_start, + .range_end._unsigned = range_end, + .mapping_name = mapping_name, + .overlaps = 0 }; + enumeration = container_of(type, struct bt_ctf_field_type_enumeration, + parent); + + /* Check that the range does not overlap with one already present */ + g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned, + &query); + if (query.overlaps) { + ret = -1; + goto error_free; + } + + mapping = g_new(struct enumeration_mapping, 1); + if (!mapping) { + ret = -1; + goto error_free; + } + + *mapping = (struct enumeration_mapping) { + .range_start._unsigned = range_start, + .range_end._unsigned = range_end, .string = mapping_name}; + g_ptr_array_add(enumeration->entries, mapping); + g_ptr_array_sort(enumeration->entries, + (GCompareFunc)compare_enumeration_mappings_unsigned); +error_free: + free(escaped_string); +end: + return ret; +} + +const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( + struct bt_ctf_field_type_enumeration *enumeration_type, + uint64_t value) +{ + const char *name = NULL; + struct range_overlap_query query = + (struct range_overlap_query) { + .range_start._unsigned = value, + .range_end._unsigned = value, + .overlaps = 0 }; + + g_ptr_array_foreach(enumeration_type->entries, + check_ranges_overlap_unsigned, + &query); + if (!query.overlaps) { + goto end; + } + + name = g_quark_to_string(query.mapping_name); +end: + return name; +} + +const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( + struct bt_ctf_field_type_enumeration *enumeration_type, + int64_t value) +{ + const char *name = NULL; + struct range_overlap_query query = + (struct range_overlap_query) { + .range_start._signed = value, + .range_end._signed = value, + .overlaps = 0 }; + + g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap, + &query); + if (!query.overlaps) { + goto end; + } + + name = g_quark_to_string(query.mapping_name); +end: + return name; +} + +int bt_ctf_field_type_enumeration_get_mapping_count( + struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_enumeration *enumeration; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + enumeration = container_of(type, struct bt_ctf_field_type_enumeration, + parent); + ret = (int) enumeration->entries->len; +end: + return ret; +} + +static inline +struct enumeration_mapping *get_enumeration_mapping( + struct bt_ctf_field_type *type, int index) +{ + struct enumeration_mapping *mapping = NULL; + struct bt_ctf_field_type_enumeration *enumeration; + + enumeration = container_of(type, struct bt_ctf_field_type_enumeration, + parent); + if (index >= enumeration->entries->len) { + goto end; + } + + mapping = g_ptr_array_index(enumeration->entries, index); +end: + return mapping; +} + +int bt_ctf_field_type_enumeration_get_mapping( + struct bt_ctf_field_type *type, int index, + const char **string, int64_t *range_start, int64_t *range_end) +{ + struct enumeration_mapping *mapping; + int ret = 0; + + if (!type || index < 0 || !string || !range_start || !range_end || + (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + mapping = get_enumeration_mapping(type, index); + if (!mapping) { + ret = -1; + goto end; + } + + *string = g_quark_to_string(mapping->string); + *range_start = mapping->range_start._signed; + *range_end = mapping->range_end._signed; +end: + return ret; +} + +int bt_ctf_field_type_enumeration_get_mapping_unsigned( + struct bt_ctf_field_type *type, int index, + const char **string, uint64_t *range_start, uint64_t *range_end) +{ + struct enumeration_mapping *mapping; + int ret = 0; + + if (!type || index < 0 || !string || !range_start || !range_end || + (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + mapping = get_enumeration_mapping(type, index); + if (!mapping) { + ret = -1; + goto end; + } + + *string = g_quark_to_string(mapping->string); + *range_start = mapping->range_start._unsigned; + *range_end = mapping->range_end._unsigned; +end: + return ret; +} + +int bt_ctf_field_type_enumeration_get_mapping_index_by_name( + struct bt_ctf_field_type *type, const char *name) +{ + GQuark name_quark; + struct bt_ctf_field_type_enumeration *enumeration; + int i, ret = 0; + + if (!type || !name || + (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + name_quark = g_quark_try_string(name); + if (!name_quark) { + ret = -1; + goto end; + } + + enumeration = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + for (i = 0; i < enumeration->entries->len; i++) { + struct enumeration_mapping *mapping = + get_enumeration_mapping(type, i); + + if (mapping->string == name_quark) { + ret = i; + goto end; + } + } + + ret = -1; +end: + return ret; +} + +int bt_ctf_field_type_enumeration_get_mapping_index_by_value( + struct bt_ctf_field_type *type, int64_t value) +{ + struct bt_ctf_field_type_enumeration *enumeration; + int i, ret = 0; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + enumeration = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + for (i = 0; i < enumeration->entries->len; i++) { + struct enumeration_mapping *mapping = + get_enumeration_mapping(type, i); + + if (value >= mapping->range_start._signed && + value <= mapping->range_end._signed) { + ret = i; + goto end; + } + } + + ret = -1; +end: + return ret; +} + +int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( + struct bt_ctf_field_type *type, uint64_t value) +{ + struct bt_ctf_field_type_enumeration *enumeration; + int i, ret = 0; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) { + ret = -1; + goto end; + } + + enumeration = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + for (i = 0; i < enumeration->entries->len; i++) { + struct enumeration_mapping *mapping = + get_enumeration_mapping(type, i); + + if (value >= mapping->range_start._unsigned && + value <= mapping->range_end._unsigned) { + ret = i; + goto end; + } + } + + ret = -1; +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) +{ + struct bt_ctf_field_type_floating_point *floating_point = + g_new0(struct bt_ctf_field_type_floating_point, 1); + + if (!floating_point) { + goto end; + } + + floating_point->declaration.sign = &floating_point->sign; + floating_point->declaration.mantissa = &floating_point->mantissa; + floating_point->declaration.exp = &floating_point->exp; + floating_point->sign.len = 1; + floating_point->parent.declaration = &floating_point->declaration.p; + floating_point->parent.declaration->id = BT_CTF_TYPE_ID_FLOAT; + floating_point->declaration.exp->len = + sizeof(float) * CHAR_BIT - FLT_MANT_DIG; + floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1; + floating_point->sign.p.alignment = 1; + floating_point->mantissa.p.alignment = 1; + floating_point->exp.p.alignment = 1; + + bt_ctf_field_type_init(&floating_point->parent, TRUE); +end: + return floating_point ? &floating_point->parent : NULL; +} + +int bt_ctf_field_type_floating_point_get_exponent_digits( + struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_floating_point *floating_point; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { + ret = -1; + goto end; + } + + floating_point = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + ret = (int) floating_point->declaration.exp->len; +end: + return ret; +} + +int bt_ctf_field_type_floating_point_set_exponent_digits( + struct bt_ctf_field_type *type, + unsigned int exponent_digits) +{ + int ret = 0; + struct bt_ctf_field_type_floating_point *floating_point; + + if (!type || type->frozen || + (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { + ret = -1; + goto end; + } + + floating_point = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) && + (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) && + (exponent_digits != + sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) { + ret = -1; + goto end; + } + + floating_point->declaration.exp->len = exponent_digits; +end: + return ret; +} + +int bt_ctf_field_type_floating_point_get_mantissa_digits( + struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_floating_point *floating_point; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { + ret = -1; + goto end; + } + + floating_point = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + ret = (int) floating_point->mantissa.len + 1; +end: + return ret; +} + +int bt_ctf_field_type_floating_point_set_mantissa_digits( + struct bt_ctf_field_type *type, + unsigned int mantissa_digits) +{ + int ret = 0; + struct bt_ctf_field_type_floating_point *floating_point; + + if (!type || type->frozen || + (type->declaration->id != BT_CTF_TYPE_ID_FLOAT)) { + ret = -1; + goto end; + } + + floating_point = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + + if ((mantissa_digits != FLT_MANT_DIG) && + (mantissa_digits != DBL_MANT_DIG) && + (mantissa_digits != LDBL_MANT_DIG)) { + ret = -1; + goto end; + } + + floating_point->declaration.mantissa->len = mantissa_digits - 1; +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) +{ + struct bt_ctf_field_type_structure *structure = + g_new0(struct bt_ctf_field_type_structure, 1); + + if (!structure) { + goto error; + } + + structure->parent.declaration = &structure->declaration.p; + structure->parent.declaration->id = BT_CTF_TYPE_ID_STRUCT; + structure->fields = g_ptr_array_new_with_free_func( + (GDestroyNotify)destroy_structure_field); + structure->field_name_to_index = g_hash_table_new(NULL, NULL); + bt_ctf_field_type_init(&structure->parent, TRUE); + return &structure->parent; +error: + return NULL; +} + +int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *field_type, + const char *field_name) +{ + int ret = 0; + struct bt_ctf_field_type_structure *structure; + + if (!type || !field_type || type->frozen || + bt_ctf_validate_identifier(field_name) || + (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { + ret = -1; + goto end; + } + + structure = container_of(type, + struct bt_ctf_field_type_structure, parent); + if (add_structure_field(structure->fields, + structure->field_name_to_index, field_type, field_name)) { + ret = -1; + goto end; + } +end: + return ret; +} + +int bt_ctf_field_type_structure_get_field_count( + struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_structure *structure; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { + ret = -1; + goto end; + } + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + ret = (int) structure->fields->len; +end: + return ret; +} + +int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type, + const char **field_name, struct bt_ctf_field_type **field_type, + int index) +{ + struct bt_ctf_field_type_structure *structure; + struct structure_field *field; + int ret = 0; + + if (!type || index < 0 || + (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) { + ret = -1; + goto end; + } + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + if (index >= structure->fields->len) { + ret = -1; + goto end; + } + + field = g_ptr_array_index(structure->fields, index); + if (field_type) { + *field_type = field->type; + bt_get(field->type); + } + if (field_name) { + *field_name = g_quark_to_string(field->name); + } +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( + struct bt_ctf_field_type *type, + const char *name) +{ + size_t index; + GQuark name_quark; + struct structure_field *field; + struct bt_ctf_field_type_structure *structure; + struct bt_ctf_field_type *field_type = NULL; + + if (!type || !name) { + goto end; + } + + name_quark = g_quark_try_string(name); + if (!name_quark) { + goto end; + } + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + if (!g_hash_table_lookup_extended(structure->field_name_to_index, + GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { + goto end; + } + + field = structure->fields->pdata[index]; + field_type = field->type; + bt_get(field_type); +end: + return field_type; +} + +struct bt_ctf_field_type *bt_ctf_field_type_variant_create( + struct bt_ctf_field_type *enum_tag, const char *tag_name) +{ + struct bt_ctf_field_type_variant *variant = NULL; + + if (tag_name && bt_ctf_validate_identifier(tag_name)) { + goto error; + } + + variant = g_new0(struct bt_ctf_field_type_variant, 1); + if (!variant) { + goto error; + } + + variant->parent.declaration = &variant->declaration.p; + variant->parent.declaration->id = BT_CTF_TYPE_ID_VARIANT; + variant->tag_name = g_string_new(tag_name); + variant->field_name_to_index = g_hash_table_new(NULL, NULL); + variant->fields = g_ptr_array_new_with_free_func( + (GDestroyNotify) destroy_structure_field); + if (enum_tag) { + bt_get(enum_tag); + variant->tag = container_of(enum_tag, + struct bt_ctf_field_type_enumeration, parent); + } + + bt_ctf_field_type_init(&variant->parent, TRUE); + /* A variant's alignment is undefined */ + variant->parent.declaration->alignment = 0; + return &variant->parent; +error: + return NULL; +} + +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_variant *variant; + struct bt_ctf_field_type *tag_type = NULL; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, parent); + if (!variant->tag) { + goto end; + } + + tag_type = &variant->tag->parent; + bt_get(tag_type); +end: + return tag_type; +} + +const char *bt_ctf_field_type_variant_get_tag_name( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_variant *variant; + const char *tag_name = NULL; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, parent); + if (variant->tag_name->len == 0) { + goto end; + } + + tag_name = variant->tag_name->str; +end: + return tag_name; +} + +int bt_ctf_field_type_variant_set_tag_name( + struct bt_ctf_field_type *type, const char *name) +{ + int ret = 0; + struct bt_ctf_field_type_variant *variant; + + if (!type || type->frozen || + (type->declaration->id != BT_CTF_TYPE_ID_VARIANT) || + bt_ctf_validate_identifier(name)) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, parent); + g_string_assign(variant->tag_name, name); +end: + return ret; +} + +int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *field_type, + const char *field_name) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_type_variant *variant; + GQuark field_name_quark = g_quark_from_string(field_name); + + if (!type || !field_type || type->frozen || + bt_ctf_validate_identifier(field_name) || + (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, parent); + + /* The user has explicitly provided a tag; validate against it. */ + if (variant->tag) { + int name_found = 0; + + /* Make sure this name is present in the enum tag */ + for (i = 0; i < variant->tag->entries->len; i++) { + struct enumeration_mapping *mapping = + g_ptr_array_index(variant->tag->entries, i); + + if (mapping->string == field_name_quark) { + name_found = 1; + break; + } + } + + if (!name_found) { + /* Validation failed */ + ret = -1; + goto end; + } + } + + if (add_structure_field(variant->fields, variant->field_name_to_index, + field_type, field_name)) { + ret = -1; + goto end; + } +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( + struct bt_ctf_field_type *type, + const char *field_name) +{ + size_t index; + GQuark name_quark; + struct structure_field *field; + struct bt_ctf_field_type_variant *variant; + struct bt_ctf_field_type *field_type = NULL; + + if (!type || !field_name) { + goto end; + } + + name_quark = g_quark_try_string(field_name); + if (!name_quark) { + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, parent); + if (!g_hash_table_lookup_extended(variant->field_name_to_index, + GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { + goto end; + } + + field = g_ptr_array_index(variant->fields, index); + field_type = field->type; + bt_get(field_type); +end: + return field_type; +} + +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( + struct bt_ctf_field_type *type, + struct bt_ctf_field *tag) +{ + const char *enum_value; + struct bt_ctf_field_type *field_type = NULL; + + if (!type || !tag || type->declaration->id != BT_CTF_TYPE_ID_VARIANT) { + goto end; + } + + enum_value = bt_ctf_field_enumeration_get_mapping_name(tag); + if (!enum_value) { + goto end; + } + + /* Already increments field_type's reference count */ + field_type = bt_ctf_field_type_variant_get_field_type_by_name( + type, enum_value); +end: + return field_type; +} + +int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type) +{ + int ret = 0; + struct bt_ctf_field_type_variant *variant; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + ret = (int) variant->fields->len; +end: + return ret; + +} + +int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type, + const char **field_name, struct bt_ctf_field_type **field_type, + int index) +{ + struct bt_ctf_field_type_variant *variant; + struct structure_field *field; + int ret = 0; + + if (!type || index < 0 || + (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + if (index >= variant->fields->len) { + ret = -1; + goto end; + } + + field = g_ptr_array_index(variant->fields, index); + if (field_type) { + *field_type = field->type; + bt_get(field->type); + } + if (field_name) { + *field_name = g_quark_to_string(field->name); + } +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_array_create( + struct bt_ctf_field_type *element_type, + unsigned int length) +{ + struct bt_ctf_field_type_array *array = NULL; + + if (!element_type || length == 0) { + goto error; + } + + array = g_new0(struct bt_ctf_field_type_array, 1); + if (!array) { + goto error; + } + + array->parent.declaration = &array->declaration.p; + array->parent.declaration->id = BT_CTF_TYPE_ID_ARRAY; + + bt_get(element_type); + array->element_type = element_type; + array->length = length; + bt_ctf_field_type_init(&array->parent, FALSE); + return &array->parent; +error: + return NULL; +} + +struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *ret = NULL; + struct bt_ctf_field_type_array *array; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { + goto end; + } + + array = container_of(type, struct bt_ctf_field_type_array, parent); + ret = array->element_type; + bt_get(ret); +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *element_type) +{ + int ret = 0; + struct bt_ctf_field_type_array *array; + + if (!type || !element_type || + (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { + ret = -1; + goto end; + } + + array = container_of(type, struct bt_ctf_field_type_array, parent); + + if (array->element_type) { + BT_PUT(array->element_type); + } + + array->element_type = element_type; + bt_get(array->element_type); + +end: + return ret; +} + +int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type) +{ + int64_t ret; + struct bt_ctf_field_type_array *array; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ARRAY)) { + ret = -1; + goto end; + } + + array = container_of(type, struct bt_ctf_field_type_array, parent); + ret = (int64_t) array->length; +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( + struct bt_ctf_field_type *element_type, + const char *length_field_name) +{ + struct bt_ctf_field_type_sequence *sequence = NULL; + + if (!element_type || bt_ctf_validate_identifier(length_field_name)) { + goto error; + } + + sequence = g_new0(struct bt_ctf_field_type_sequence, 1); + if (!sequence) { + goto error; + } + + sequence->parent.declaration = &sequence->declaration.p; + sequence->parent.declaration->id = BT_CTF_TYPE_ID_SEQUENCE; + bt_get(element_type); + sequence->element_type = element_type; + sequence->length_field_name = g_string_new(length_field_name); + bt_ctf_field_type_init(&sequence->parent, FALSE); + return &sequence->parent; +error: + return NULL; +} + +struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *ret = NULL; + struct bt_ctf_field_type_sequence *sequence; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { + goto end; + } + + sequence = container_of(type, struct bt_ctf_field_type_sequence, + parent); + ret = sequence->element_type; + bt_get(ret); +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *element_type) +{ + int ret = 0; + struct bt_ctf_field_type_sequence *sequence; + + if (!type || !element_type || + (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { + ret = -1; + goto end; + } + + sequence = container_of(type, struct bt_ctf_field_type_sequence, parent); + + if (sequence->element_type) { + BT_PUT(sequence->element_type); + } + + sequence->element_type = element_type; + bt_get(sequence->element_type); + +end: + return ret; +} + +const char *bt_ctf_field_type_sequence_get_length_field_name( + struct bt_ctf_field_type *type) +{ + const char *ret = NULL; + struct bt_ctf_field_type_sequence *sequence; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_SEQUENCE)) { + goto end; + } + + sequence = container_of(type, struct bt_ctf_field_type_sequence, + parent); + ret = sequence->length_field_name->str; +end: + return ret; +} + +struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) +{ + struct bt_ctf_field_type_string *string = + g_new0(struct bt_ctf_field_type_string, 1); + + if (!string) { + return NULL; + } + + string->parent.declaration = &string->declaration.p; + string->parent.declaration->id = BT_CTF_TYPE_ID_STRING; + bt_ctf_field_type_init(&string->parent, TRUE); + string->declaration.encoding = BT_CTF_STRING_ENCODING_UTF8; + string->parent.declaration->alignment = CHAR_BIT; + return &string->parent; +} + +enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_string *string; + enum bt_ctf_string_encoding ret = BT_CTF_STRING_ENCODING_UNKNOWN; + + if (!type || (type->declaration->id != BT_CTF_TYPE_ID_STRING)) { + goto end; + } + + string = container_of(type, struct bt_ctf_field_type_string, + parent); + ret = string->declaration.encoding; +end: + return ret; +} + +int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type, + enum bt_ctf_string_encoding encoding) +{ + int ret = 0; + struct bt_ctf_field_type_string *string; + + if (!type || type->declaration->id != BT_CTF_TYPE_ID_STRING || + (encoding != BT_CTF_STRING_ENCODING_UTF8 && + encoding != BT_CTF_STRING_ENCODING_ASCII)) { + ret = -1; + goto end; + } + + string = container_of(type, struct bt_ctf_field_type_string, parent); + string->declaration.encoding = encoding; +end: + return ret; +} + +int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type) +{ + int ret; + enum bt_ctf_type_id type_id; + + if (!type) { + ret = -1; + goto end; + } + + if (type->frozen) { + ret = (int) type->declaration->alignment; + goto end; + } + + type_id = bt_ctf_field_type_get_type_id(type); + switch (type_id) { + case BT_CTF_TYPE_ID_SEQUENCE: + { + struct bt_ctf_field_type *element = + bt_ctf_field_type_sequence_get_element_type(type); + + if (!element) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_type_get_alignment(element); + bt_put(element); + break; + } + case BT_CTF_TYPE_ID_ARRAY: + { + struct bt_ctf_field_type *element = + bt_ctf_field_type_array_get_element_type(type); + + if (!element) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_type_get_alignment(element); + bt_put(element); + break; + } + case BT_CTF_TYPE_ID_STRUCT: + { + int i, element_count; + + element_count = bt_ctf_field_type_structure_get_field_count( + type); + if (element_count < 0) { + ret = element_count; + goto end; + } + + for (i = 0; i < element_count; i++) { + struct bt_ctf_field_type *field; + int field_alignment; + + ret = bt_ctf_field_type_structure_get_field(type, NULL, + &field, i); + if (ret) { + goto end; + } + + assert(field); + field_alignment = bt_ctf_field_type_get_alignment( + field); + bt_put(field); + if (field_alignment < 0) { + ret = field_alignment; + goto end; + } + + type->declaration->alignment = MAX(field_alignment, + type->declaration->alignment); + } + ret = (int) type->declaration->alignment; + break; + } + case BT_CTF_TYPE_ID_UNKNOWN: + ret = -1; + break; + default: + ret = (int) type->declaration->alignment; + break; + } +end: + return ret; +} + +static inline +int is_power_of_two(unsigned int value) +{ + return ((value & (value - 1)) == 0) && value > 0; +} + +int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, + unsigned int alignment) +{ + int ret = 0; + enum bt_ctf_type_id type_id; + + /* Alignment must be a power of two */ + if (!type || type->frozen || !is_power_of_two(alignment)) { + ret = -1; + goto end; + } + + type_id = bt_ctf_field_type_get_type_id(type); + if (type_id == BT_CTF_TYPE_ID_UNKNOWN) { + ret = -1; + goto end; + } + + if (type->declaration->id == BT_CTF_TYPE_ID_STRING && + alignment != CHAR_BIT) { + ret = -1; + goto end; + } + + if (type_id == BT_CTF_TYPE_ID_VARIANT || + type_id == BT_CTF_TYPE_ID_SEQUENCE || + type_id == BT_CTF_TYPE_ID_ARRAY) { + /* Setting an alignment on these types makes no sense */ + ret = -1; + goto end; + } + + type->declaration->alignment = alignment; + ret = 0; +end: + return ret; +} + +enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( + struct bt_ctf_field_type *type) +{ + enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; + + if (!type) { + goto end; + } + + switch (type->declaration->id) { + case BT_CTF_TYPE_ID_INTEGER: + { + struct bt_ctf_field_type_integer *integer = container_of( + type, struct bt_ctf_field_type_integer, parent); + ret = integer->user_byte_order; + break; + } + case BT_CTF_TYPE_ID_FLOAT: + { + struct bt_ctf_field_type_floating_point *floating_point = + container_of(type, + struct bt_ctf_field_type_floating_point, + parent); + ret = floating_point->user_byte_order; + break; + } + default: + goto end; + } + + assert(ret == BT_CTF_BYTE_ORDER_NATIVE || + ret == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || + ret == BT_CTF_BYTE_ORDER_BIG_ENDIAN || + ret == BT_CTF_BYTE_ORDER_NETWORK); + +end: + return ret; +} + +int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, + enum bt_ctf_byte_order byte_order) +{ + int ret = 0; + int internal_byte_order; + enum bt_ctf_type_id type_id; + + if (!type || type->frozen) { + ret = -1; + goto end; + } + + switch (byte_order) { + case BT_CTF_BYTE_ORDER_NATIVE: + /* Leave unset. Will be initialized by parent. */ + internal_byte_order = 0; + break; + case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: + internal_byte_order = LITTLE_ENDIAN; + break; + case BT_CTF_BYTE_ORDER_BIG_ENDIAN: + case BT_CTF_BYTE_ORDER_NETWORK: + internal_byte_order = BIG_ENDIAN; + break; + default: + ret = -1; + goto end; + } + + type_id = type->declaration->id; + if (set_byte_order_funcs[type_id]) { + set_byte_order_funcs[type_id](type, internal_byte_order, 0); + } +end: + return ret; +} + +enum bt_ctf_type_id bt_ctf_field_type_get_type_id( + struct bt_ctf_field_type *type) +{ + if (!type) { + return BT_CTF_TYPE_ID_UNKNOWN; + } + + return type->declaration->id; +} + +int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_INTEGER; +} + +int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_FLOAT; +} + +int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_ENUM; +} + +int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_STRING; +} + +int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_STRUCT; +} + +int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_ARRAY; +} + +int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_SEQUENCE; +} + +int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type) +{ + return bt_ctf_field_type_get_type_id(type) == BT_CTF_TYPE_ID_VARIANT; +} + +void bt_ctf_field_type_get(struct bt_ctf_field_type *type) +{ + bt_get(type); +} + +void bt_ctf_field_type_put(struct bt_ctf_field_type *type) +{ + bt_put(type); +} + +BT_HIDDEN +void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type) +{ + if (!type) { + return; + } + + type->freeze(type); +} + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( + struct bt_ctf_field_type_variant *variant, + int64_t tag_value) +{ + struct bt_ctf_field_type *type = NULL; + GQuark field_name_quark; + gpointer index; + struct structure_field *field_entry; + struct range_overlap_query query = { + .range_start._signed = tag_value, + .range_end._signed = tag_value, + .mapping_name = 0, .overlaps = 0}; + + g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap, + &query); + if (!query.overlaps) { + goto end; + } + + field_name_quark = query.mapping_name; + if (!g_hash_table_lookup_extended(variant->field_name_to_index, + GUINT_TO_POINTER(field_name_quark), NULL, &index)) { + goto end; + } + + field_entry = g_ptr_array_index(variant->fields, (size_t) index); + type = field_entry->type; +end: + return type; +} + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( + struct bt_ctf_field_type_variant *variant, + uint64_t tag_value) +{ + struct bt_ctf_field_type *type = NULL; + GQuark field_name_quark; + gpointer index; + struct structure_field *field_entry; + struct range_overlap_query query = { + .range_start._unsigned = tag_value, + .range_end._unsigned = tag_value, + .mapping_name = 0, .overlaps = 0}; + + g_ptr_array_foreach(variant->tag->entries, + check_ranges_overlap_unsigned, + &query); + if (!query.overlaps) { + goto end; + } + + field_name_quark = query.mapping_name; + if (!g_hash_table_lookup_extended(variant->field_name_to_index, + GUINT_TO_POINTER(field_name_quark), NULL, &index)) { + goto end; + } + + field_entry = g_ptr_array_index(variant->fields, (size_t)index); + type = field_entry->type; +end: + return type; +} + +BT_HIDDEN +int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + int ret; + + if (!type || !context) { + ret = -1; + goto end; + } + + /* Make sure field type is valid before serializing it */ + ret = bt_ctf_field_type_validate(type); + + if (ret) { + goto end; + } + + ret = type->serialize(type, context); +end: + return ret; +} + +BT_HIDDEN +void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type, + int byte_order) +{ + if (!type) { + return; + } + + assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN); + if (set_byte_order_funcs[type->declaration->id]) { + set_byte_order_funcs[type->declaration->id](type, + byte_order, 1); + } +} + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy = NULL; + + if (!type) { + goto end; + } + + copy = type_copy_funcs[type->declaration->id](type); +end: + return copy; +} + +BT_HIDDEN +int bt_ctf_field_type_structure_get_field_name_index( + struct bt_ctf_field_type *type, const char *name) +{ + int ret; + size_t index; + GQuark name_quark; + struct bt_ctf_field_type_structure *structure; + + if (!type || !name || + bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_STRUCT) { + ret = -1; + goto end; + } + + name_quark = g_quark_try_string(name); + if (!name_quark) { + ret = -1; + goto end; + } + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + if (!g_hash_table_lookup_extended(structure->field_name_to_index, + GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { + ret = -1; + goto end; + } + ret = (int) index; +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *field, int index) +{ + int ret = 0; + struct bt_ctf_field_type_structure *structure; + + if (!type || !field || + bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_STRUCT) { + ret = -1; + goto end; + } + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + if (index < 0 || index >= structure->fields->len) { + ret = -1; + goto end; + } + + bt_get(field); + bt_put(((struct structure_field *) + g_ptr_array_index(structure->fields, index))->type); + ((struct structure_field *) structure->fields->pdata[index])->type = + field; +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_variant_get_field_name_index( + struct bt_ctf_field_type *type, const char *name) +{ + int ret; + size_t index; + GQuark name_quark; + struct bt_ctf_field_type_variant *variant; + + if (!type || !name || + bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_VARIANT) { + ret = -1; + goto end; + } + + name_quark = g_quark_try_string(name); + if (!name_quark) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + if (!g_hash_table_lookup_extended(variant->field_name_to_index, + GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { + ret = -1; + goto end; + } + ret = (int) index; +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_sequence_set_length_field_path( + struct bt_ctf_field_type *type, + struct bt_ctf_field_path *path) +{ + int ret = 0; + struct bt_ctf_field_type_sequence *sequence; + + if (!type || bt_ctf_field_type_get_type_id(type) != + BT_CTF_TYPE_ID_SEQUENCE) { + ret = -1; + goto end; + } + + sequence = container_of(type, struct bt_ctf_field_type_sequence, + parent); + bt_get(path); + BT_MOVE(sequence->length_field_path, path); +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, + struct bt_ctf_field_path *path) +{ + int ret = 0; + struct bt_ctf_field_type_variant *variant; + + if (!type || bt_ctf_field_type_get_type_id(type) != + BT_CTF_TYPE_ID_VARIANT) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + bt_get(path); + BT_MOVE(variant->tag_field_path, path); +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *tag) +{ + int ret = 0; + struct bt_ctf_field_type_variant *variant; + + if (!type || !tag || + bt_ctf_field_type_get_type_id(tag) != + BT_CTF_TYPE_ID_ENUM) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + bt_get(tag); + if (variant->tag) { + bt_put(&variant->tag->parent); + } + variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration, + parent); +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *field, int index) +{ + int ret = 0; + struct bt_ctf_field_type_variant *variant; + + if (!type || !field || + bt_ctf_field_type_get_type_id(type) != BT_CTF_TYPE_ID_VARIANT) { + ret = -1; + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + if (index < 0 || index >= variant->fields->len) { + ret = -1; + goto end; + } + + bt_get(field); + bt_put(((struct structure_field *) + g_ptr_array_index(variant->fields, index))->type); + ((struct structure_field *) variant->fields->pdata[index])->type = + field; +end: + return ret; +} + +static +void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_integer *integer = + (struct bt_ctf_field_type_integer *) type; + + if (!type) { + return; + } + + bt_put(integer->mapped_clock); + g_free(integer); +} + +static +void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_enumeration *enumeration = + (struct bt_ctf_field_type_enumeration *) type; + + if (!type) { + return; + } + + g_ptr_array_free(enumeration->entries, TRUE); + bt_put(enumeration->container); + g_free(enumeration); +} + +static +void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_floating_point *floating_point = + (struct bt_ctf_field_type_floating_point *) type; + + if (!type) { + return; + } + + g_free(floating_point); +} + +static +void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_structure *structure = + (struct bt_ctf_field_type_structure *) type; + + if (!type) { + return; + } + + g_ptr_array_free(structure->fields, TRUE); + g_hash_table_destroy(structure->field_name_to_index); + g_free(structure); +} + +static +void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_variant *variant = + (struct bt_ctf_field_type_variant *) type; + + if (!type) { + return; + } + + g_ptr_array_free(variant->fields, TRUE); + g_hash_table_destroy(variant->field_name_to_index); + g_string_free(variant->tag_name, TRUE); + bt_put(&variant->tag->parent); + BT_PUT(variant->tag_field_path); + g_free(variant); +} + +static +void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_array *array = + (struct bt_ctf_field_type_array *) type; + + if (!type) { + return; + } + + bt_put(array->element_type); + g_free(array); +} + +static +void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_sequence *sequence = + (struct bt_ctf_field_type_sequence *) type; + + if (!type) { + return; + } + + bt_put(sequence->element_type); + g_string_free(sequence->length_field_name, TRUE); + BT_PUT(sequence->length_field_path); + g_free(sequence); +} + +static +void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_string *string = + (struct bt_ctf_field_type_string *) type; + + if (!type) { + return; + } + + g_free(string); +} + +static +void generic_field_type_freeze(struct bt_ctf_field_type *type) +{ + type->frozen = 1; +} + +static +void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_enumeration *enumeration_type = container_of( + type, struct bt_ctf_field_type_enumeration, parent); + + generic_field_type_freeze(type); + bt_ctf_field_type_freeze(enumeration_type->container); +} + +static +void freeze_structure_field(struct structure_field *field) +{ + bt_ctf_field_type_freeze(field->type); +} + +static +void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_structure *structure_type = container_of( + type, struct bt_ctf_field_type_structure, parent); + + /* Cache the alignment */ + type->declaration->alignment = bt_ctf_field_type_get_alignment(type); + generic_field_type_freeze(type); + g_ptr_array_foreach(structure_type->fields, + (GFunc) freeze_structure_field, NULL); +} + +static +void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_variant *variant_type = container_of( + type, struct bt_ctf_field_type_variant, parent); + + generic_field_type_freeze(type); + g_ptr_array_foreach(variant_type->fields, + (GFunc) freeze_structure_field, NULL); +} + +static +void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_array *array_type = container_of( + type, struct bt_ctf_field_type_array, parent); + + /* Cache the alignment */ + type->declaration->alignment = bt_ctf_field_type_get_alignment(type); + generic_field_type_freeze(type); + bt_ctf_field_type_freeze(array_type->element_type); +} + +static +void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type_sequence *sequence_type = container_of( + type, struct bt_ctf_field_type_sequence, parent); + + /* Cache the alignment */ + type->declaration->alignment = bt_ctf_field_type_get_alignment(type); + generic_field_type_freeze(type); + bt_ctf_field_type_freeze(sequence_type->element_type); +} + +static +const char *get_encoding_string(enum bt_ctf_string_encoding encoding) +{ + const char *encoding_string; + + switch (encoding) { + case BT_CTF_STRING_ENCODING_NONE: + encoding_string = "none"; + break; + case BT_CTF_STRING_ENCODING_ASCII: + encoding_string = "ASCII"; + break; + case BT_CTF_STRING_ENCODING_UTF8: + encoding_string = "UTF8"; + break; + default: + encoding_string = "unknown"; + break; + } + + return encoding_string; +} + +static +const char *get_integer_base_string(enum bt_ctf_integer_base base) +{ + const char *base_string; + + switch (base) { + case BT_CTF_INTEGER_BASE_DECIMAL: + base_string = "decimal"; + break; + case BT_CTF_INTEGER_BASE_HEXADECIMAL: + base_string = "hexadecimal"; + break; + case BT_CTF_INTEGER_BASE_OCTAL: + base_string = "octal"; + break; + case BT_CTF_INTEGER_BASE_BINARY: + base_string = "binary"; + break; + default: + base_string = "unknown"; + break; + } + + return base_string; +} + +static +int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + struct bt_ctf_field_type_integer *integer = container_of(type, + struct bt_ctf_field_type_integer, parent); + int ret = 0; + + g_string_append_printf(context->string, + "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s", + integer->declaration.len, type->declaration->alignment, + (integer->declaration.signedness ? "true" : "false"), + get_encoding_string(integer->declaration.encoding), + get_integer_base_string(integer->declaration.base), + get_byte_order_string(integer->declaration.byte_order)); + if (integer->mapped_clock) { + const char *clock_name = bt_ctf_clock_get_name( + integer->mapped_clock); + + if (!clock_name) { + ret = -1; + goto end; + } + + g_string_append_printf(context->string, + "; map = clock.%s.value", clock_name); + } + + g_string_append(context->string, "; }"); +end: + return ret; +} + +static +int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + size_t entry; + int ret; + struct bt_ctf_field_type_enumeration *enumeration = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + struct bt_ctf_field_type *container_type; + int container_signed; + + container_type = bt_ctf_field_type_enumeration_get_container_type(type); + if (!container_type) { + ret = -1; + goto end; + } + + container_signed = bt_ctf_field_type_integer_get_signed(container_type); + if (container_signed < 0) { + ret = container_signed; + goto error_put_container_type; + } + + g_string_append(context->string, "enum : "); + ret = bt_ctf_field_type_serialize(enumeration->container, context); + if (ret) { + goto error_put_container_type; + } + + g_string_append(context->string, " { "); + for (entry = 0; entry < enumeration->entries->len; entry++) { + struct enumeration_mapping *mapping = + enumeration->entries->pdata[entry]; + + if (container_signed) { + if (mapping->range_start._signed == + mapping->range_end._signed) { + g_string_append_printf(context->string, + "\"%s\" = %" PRId64, + g_quark_to_string(mapping->string), + mapping->range_start._signed); + } else { + g_string_append_printf(context->string, + "\"%s\" = %" PRId64 " ... %" PRId64, + g_quark_to_string(mapping->string), + mapping->range_start._signed, + mapping->range_end._signed); + } + } else { + if (mapping->range_start._unsigned == + mapping->range_end._unsigned) { + g_string_append_printf(context->string, + "\"%s\" = %" PRIu64, + g_quark_to_string(mapping->string), + mapping->range_start._unsigned); + } else { + g_string_append_printf(context->string, + "\"%s\" = %" PRIu64 " ... %" PRIu64, + g_quark_to_string(mapping->string), + mapping->range_start._unsigned, + mapping->range_end._unsigned); + } + } + + g_string_append(context->string, + ((entry != (enumeration->entries->len - 1)) ? + ", " : " }")); + } + + if (context->field_name->len) { + g_string_append_printf(context->string, " %s", + context->field_name->str); + g_string_assign(context->field_name, ""); + } +error_put_container_type: + bt_put(container_type); +end: + return ret; +} + +static +int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + struct bt_ctf_field_type_floating_point *floating_point = container_of( + type, struct bt_ctf_field_type_floating_point, parent); + + g_string_append_printf(context->string, + "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }", + floating_point->declaration.exp->len, + floating_point->declaration.mantissa->len + 1, + get_byte_order_string(floating_point->declaration.byte_order), + type->declaration->alignment); + return 0; +} + +static +int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + size_t i; + unsigned int indent; + int ret = 0; + struct bt_ctf_field_type_structure *structure = container_of(type, + struct bt_ctf_field_type_structure, parent); + GString *structure_field_name = context->field_name; + + context->field_name = g_string_new(""); + + context->current_indentation_level++; + g_string_append(context->string, "struct {\n"); + + for (i = 0; i < structure->fields->len; i++) { + struct structure_field *field; + + for (indent = 0; indent < context->current_indentation_level; + indent++) { + g_string_append_c(context->string, '\t'); + } + + field = structure->fields->pdata[i]; + g_string_assign(context->field_name, + g_quark_to_string(field->name)); + ret = bt_ctf_field_type_serialize(field->type, context); + if (ret) { + goto end; + } + + if (context->field_name->len) { + g_string_append_printf(context->string, " %s", + context->field_name->str); + } + g_string_append(context->string, ";\n"); + } + + context->current_indentation_level--; + for (indent = 0; indent < context->current_indentation_level; + indent++) { + g_string_append_c(context->string, '\t'); + } + + g_string_append_printf(context->string, "} align(%zu)", + type->declaration->alignment); +end: + g_string_free(context->field_name, TRUE); + context->field_name = structure_field_name; + return ret; +} + +static +int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + size_t i; + unsigned int indent; + int ret = 0; + struct bt_ctf_field_type_variant *variant = container_of( + type, struct bt_ctf_field_type_variant, parent); + GString *variant_field_name = context->field_name; + + context->field_name = g_string_new(""); + if (variant->tag_name->len > 0) { + g_string_append_printf(context->string, + "variant <%s> {\n", variant->tag_name->str); + } else { + g_string_append(context->string, "variant {\n"); + } + + context->current_indentation_level++; + for (i = 0; i < variant->fields->len; i++) { + struct structure_field *field = variant->fields->pdata[i]; + + g_string_assign(context->field_name, + g_quark_to_string(field->name)); + for (indent = 0; indent < context->current_indentation_level; + indent++) { + g_string_append_c(context->string, '\t'); + } + + g_string_assign(context->field_name, + g_quark_to_string(field->name)); + ret = bt_ctf_field_type_serialize(field->type, context); + if (ret) { + goto end; + } + + if (context->field_name->len) { + g_string_append_printf(context->string, " %s;", + context->field_name->str); + } + + g_string_append_c(context->string, '\n'); + } + + context->current_indentation_level--; + for (indent = 0; indent < context->current_indentation_level; + indent++) { + g_string_append_c(context->string, '\t'); + } + + g_string_append(context->string, "}"); +end: + g_string_free(context->field_name, TRUE); + context->field_name = variant_field_name; + return ret; +} + +static +int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + int ret = 0; + struct bt_ctf_field_type_array *array = container_of(type, + struct bt_ctf_field_type_array, parent); + + ret = bt_ctf_field_type_serialize(array->element_type, context); + if (ret) { + goto end; + } + + if (context->field_name->len) { + g_string_append_printf(context->string, " %s[%u]", + context->field_name->str, array->length); + g_string_assign(context->field_name, ""); + } else { + g_string_append_printf(context->string, "[%u]", array->length); + } +end: + return ret; +} + +static +int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + int ret = 0; + struct bt_ctf_field_type_sequence *sequence = container_of( + type, struct bt_ctf_field_type_sequence, parent); + + ret = bt_ctf_field_type_serialize(sequence->element_type, context); + if (ret) { + goto end; + } + + if (context->field_name->len) { + g_string_append_printf(context->string, " %s[%s]", + context->field_name->str, + sequence->length_field_name->str); + g_string_assign(context->field_name, ""); + } else { + g_string_append_printf(context->string, "[%s]", + sequence->length_field_name->str); + } +end: + return ret; +} + +static +int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context) +{ + struct bt_ctf_field_type_string *string = container_of( + type, struct bt_ctf_field_type_string, parent); + + g_string_append_printf(context->string, + "string { encoding = %s; }", + get_encoding_string(string->declaration.encoding)); + return 0; +} + +static +enum bt_ctf_byte_order get_ctf_ir_byte_order(int byte_order) { + enum bt_ctf_byte_order ret; + + switch (byte_order) { + case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: + case LITTLE_ENDIAN: + ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; + break; + case BT_CTF_BYTE_ORDER_BIG_ENDIAN: + case BIG_ENDIAN: + ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; + break; + case BT_CTF_BYTE_ORDER_NETWORK: + ret = BT_CTF_BYTE_ORDER_NETWORK; + break; + case BT_CTF_BYTE_ORDER_NATIVE: + ret = BT_CTF_BYTE_ORDER_NATIVE; + break; + default: + ret = BT_CTF_BYTE_ORDER_UNKNOWN; + break; + } + + return ret; +} + +static +void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type, + int byte_order, int set_native) +{ + struct bt_ctf_field_type_integer *integer_type = container_of(type, + struct bt_ctf_field_type_integer, parent); + + if (set_native) { + if (integer_type->user_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { + /* + * User byte order is native, so we can set + * the real byte order. + */ + integer_type->declaration.byte_order = + byte_order; + } + } else { + integer_type->user_byte_order = + get_ctf_ir_byte_order(byte_order); + integer_type->declaration.byte_order = byte_order; + } +} + +static +void bt_ctf_field_type_enumeration_set_byte_order( + struct bt_ctf_field_type *type, int byte_order, int set_native) +{ + struct bt_ctf_field_type_enumeration *enum_type = container_of(type, + struct bt_ctf_field_type_enumeration, parent); + + /* Safe to assume that container is an integer */ + bt_ctf_field_type_integer_set_byte_order(enum_type->container, + byte_order, set_native); +} + +static +void bt_ctf_field_type_floating_point_set_byte_order( + struct bt_ctf_field_type *type, int byte_order, int set_native) +{ + struct bt_ctf_field_type_floating_point *floating_point_type = + container_of(type, struct bt_ctf_field_type_floating_point, + parent); + + if (set_native) { + if (floating_point_type->user_byte_order == + BT_CTF_BYTE_ORDER_NATIVE) { + /* + * User byte order is native, so we can set + * the real byte order. + */ + floating_point_type->declaration.byte_order = + byte_order; + floating_point_type->sign.byte_order = + byte_order; + floating_point_type->mantissa.byte_order = + byte_order; + floating_point_type->exp.byte_order = + byte_order; + } + } else { + floating_point_type->user_byte_order = + get_ctf_ir_byte_order(byte_order); + floating_point_type->declaration.byte_order = byte_order; + floating_point_type->sign.byte_order = byte_order; + floating_point_type->mantissa.byte_order = byte_order; + floating_point_type->exp.byte_order = byte_order; + } +} + +static +void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type, + int byte_order, int set_native) +{ + int i; + struct bt_ctf_field_type_structure *structure_type = + container_of(type, struct bt_ctf_field_type_structure, + parent); + + for (i = 0; i < structure_type->fields->len; i++) { + struct structure_field *field = g_ptr_array_index( + structure_type->fields, i); + struct bt_ctf_field_type *field_type = field->type; + + if (set_byte_order_funcs[field_type->declaration->id]) { + set_byte_order_funcs[field_type->declaration->id]( + field_type, byte_order, set_native); + } + } +} + +static +void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type, + int byte_order, int set_native) +{ + int i; + struct bt_ctf_field_type_variant *variant_type = + container_of(type, struct bt_ctf_field_type_variant, + parent); + + for (i = 0; i < variant_type->fields->len; i++) { + struct structure_field *field = g_ptr_array_index( + variant_type->fields, i); + struct bt_ctf_field_type *field_type = field->type; + + if (set_byte_order_funcs[field_type->declaration->id]) { + set_byte_order_funcs[field_type->declaration->id]( + field_type, byte_order, set_native); + } + } +} + +static +void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type, + int byte_order, int set_native) +{ + struct bt_ctf_field_type_array *array_type = + container_of(type, struct bt_ctf_field_type_array, + parent); + + if (set_byte_order_funcs[array_type->element_type->declaration->id]) { + set_byte_order_funcs[array_type->element_type->declaration->id]( + array_type->element_type, byte_order, set_native); + } +} + +static +void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type, + int byte_order, int set_native) +{ + struct bt_ctf_field_type_sequence *sequence_type = + container_of(type, struct bt_ctf_field_type_sequence, + parent); + + if (set_byte_order_funcs[ + sequence_type->element_type->declaration->id]) { + set_byte_order_funcs[ + sequence_type->element_type->declaration->id]( + sequence_type->element_type, byte_order, set_native); + } +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy; + struct bt_ctf_field_type_integer *integer, *copy_integer; + + integer = container_of(type, struct bt_ctf_field_type_integer, parent); + copy = bt_ctf_field_type_integer_create(integer->declaration.len); + if (!copy) { + goto end; + } + + copy_integer = container_of(copy, struct bt_ctf_field_type_integer, + parent); + copy_integer->declaration = integer->declaration; + if (integer->mapped_clock) { + bt_get(integer->mapped_clock); + copy_integer->mapped_clock = integer->mapped_clock; + } + + copy_integer->user_byte_order = integer->user_byte_order; + +end: + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( + struct bt_ctf_field_type *type) +{ + size_t i; + struct bt_ctf_field_type *copy = NULL, *copy_container; + struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration; + + enumeration = container_of(type, struct bt_ctf_field_type_enumeration, + parent); + + /* Copy the source enumeration's container */ + copy_container = bt_ctf_field_type_copy(enumeration->container); + if (!copy_container) { + goto end; + } + + copy = bt_ctf_field_type_enumeration_create(copy_container); + if (!copy) { + goto end; + } + copy_enumeration = container_of(copy, + struct bt_ctf_field_type_enumeration, parent); + + /* Copy all enumaration entries */ + for (i = 0; i < enumeration->entries->len; i++) { + struct enumeration_mapping *mapping = g_ptr_array_index( + enumeration->entries, i); + struct enumeration_mapping* copy_mapping = g_new0( + struct enumeration_mapping, 1); + + if (!copy_mapping) { + goto error; + } + + *copy_mapping = *mapping; + g_ptr_array_add(copy_enumeration->entries, copy_mapping); + } + + copy_enumeration->declaration = enumeration->declaration; +end: + bt_put(copy_container); + return copy; +error: + bt_put(copy_container); + BT_PUT(copy); + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy; + struct bt_ctf_field_type_floating_point *floating_point, *copy_float; + + floating_point = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + copy = bt_ctf_field_type_floating_point_create(); + if (!copy) { + goto end; + } + + copy_float = container_of(copy, + struct bt_ctf_field_type_floating_point, parent); + copy_float->declaration = floating_point->declaration; + copy_float->sign = floating_point->sign; + copy_float->mantissa = floating_point->mantissa; + copy_float->exp = floating_point->exp; + copy_float->user_byte_order = floating_point->user_byte_order; + copy_float->declaration.sign = ©_float->sign; + copy_float->declaration.mantissa = ©_float->mantissa; + copy_float->declaration.exp = ©_float->exp; +end: + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( + struct bt_ctf_field_type *type) +{ + int i; + GHashTableIter iter; + gpointer key, value; + struct bt_ctf_field_type *copy; + struct bt_ctf_field_type_structure *structure, *copy_structure; + + structure = container_of(type, struct bt_ctf_field_type_structure, + parent); + copy = bt_ctf_field_type_structure_create(); + if (!copy) { + goto end; + } + + copy_structure = container_of(copy, + struct bt_ctf_field_type_structure, parent); + + /* Copy field_name_to_index */ + g_hash_table_iter_init(&iter, structure->field_name_to_index); + while (g_hash_table_iter_next (&iter, &key, &value)) { + g_hash_table_insert(copy_structure->field_name_to_index, + key, value); + } + + for (i = 0; i < structure->fields->len; i++) { + struct structure_field *entry, *copy_entry; + struct bt_ctf_field_type *copy_field; + + copy_entry = g_new0(struct structure_field, 1); + if (!copy_entry) { + goto error; + } + + entry = g_ptr_array_index(structure->fields, i); + copy_field = bt_ctf_field_type_copy(entry->type); + if (!copy_field) { + g_free(copy_entry); + goto error; + } + + copy_entry->name = entry->name; + copy_entry->type = copy_field; + g_ptr_array_add(copy_structure->fields, copy_entry); + } + + copy_structure->declaration = structure->declaration; +end: + return copy; +error: + BT_PUT(copy); + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( + struct bt_ctf_field_type *type) +{ + int i; + GHashTableIter iter; + gpointer key, value; + struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL; + struct bt_ctf_field_type_variant *variant, *copy_variant; + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + if (variant->tag) { + copy_tag = bt_ctf_field_type_copy(&variant->tag->parent); + if (!copy_tag) { + goto end; + } + } + + copy = bt_ctf_field_type_variant_create(copy_tag, + variant->tag_name->len ? variant->tag_name->str : NULL); + if (!copy) { + goto end; + } + + copy_variant = container_of(copy, struct bt_ctf_field_type_variant, + parent); + + /* Copy field_name_to_index */ + g_hash_table_iter_init(&iter, variant->field_name_to_index); + while (g_hash_table_iter_next (&iter, &key, &value)) { + g_hash_table_insert(copy_variant->field_name_to_index, + key, value); + } + + for (i = 0; i < variant->fields->len; i++) { + struct structure_field *entry, *copy_entry; + struct bt_ctf_field_type *copy_field; + + copy_entry = g_new0(struct structure_field, 1); + if (!copy_entry) { + goto error; + } + + entry = g_ptr_array_index(variant->fields, i); + copy_field = bt_ctf_field_type_copy(entry->type); + if (!copy_field) { + g_free(copy_entry); + goto error; + } + + copy_entry->name = entry->name; + copy_entry->type = copy_field; + g_ptr_array_add(copy_variant->fields, copy_entry); + } + + copy_variant->declaration = variant->declaration; + if (variant->tag_field_path) { + copy_variant->tag_field_path = bt_ctf_field_path_copy( + variant->tag_field_path); + if (!copy_variant->tag_field_path) { + goto error; + } + } +end: + bt_put(copy_tag); + return copy; +error: + bt_put(copy_tag); + BT_PUT(copy); + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_array_copy( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy = NULL, *copy_element; + struct bt_ctf_field_type_array *array, *copy_array; + + array = container_of(type, struct bt_ctf_field_type_array, + parent); + copy_element = bt_ctf_field_type_copy(array->element_type); + if (!copy_element) { + goto end; + } + + copy = bt_ctf_field_type_array_create(copy_element, array->length); + if (!copy) { + goto end; + } + + copy_array = container_of(copy, struct bt_ctf_field_type_array, + parent); + copy_array->declaration = array->declaration; +end: + bt_put(copy_element); + return copy; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy = NULL, *copy_element; + struct bt_ctf_field_type_sequence *sequence, *copy_sequence; + + sequence = container_of(type, struct bt_ctf_field_type_sequence, + parent); + copy_element = bt_ctf_field_type_copy(sequence->element_type); + if (!copy_element) { + goto end; + } + + copy = bt_ctf_field_type_sequence_create(copy_element, + sequence->length_field_name->len ? + sequence->length_field_name->str : NULL); + if (!copy) { + goto end; + } + + copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence, + parent); + copy_sequence->declaration = sequence->declaration; + if (sequence->length_field_path) { + copy_sequence->length_field_path = bt_ctf_field_path_copy( + sequence->length_field_path); + if (!copy_sequence->length_field_path) { + goto error; + } + } +end: + bt_put(copy_element); + return copy; +error: + BT_PUT(copy); + goto end; +} + +static +struct bt_ctf_field_type *bt_ctf_field_type_string_copy( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_type *copy; + struct bt_ctf_field_type_string *string, *copy_string; + + copy = bt_ctf_field_type_string_create(); + if (!copy) { + goto end; + } + + string = container_of(type, struct bt_ctf_field_type_string, + parent); + copy_string = container_of(type, struct bt_ctf_field_type_string, + parent); + copy_string->declaration = string->declaration; +end: + return copy; +} + +static +int bt_ctf_field_type_integer_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + struct bt_ctf_field_type_integer *integer_a; + struct bt_ctf_field_type_integer *integer_b; + struct declaration_integer *decl_a; + struct declaration_integer *decl_b; + + integer_a = container_of(type_a, struct bt_ctf_field_type_integer, + parent); + integer_b = container_of(type_b, struct bt_ctf_field_type_integer, + parent); + decl_a = &integer_a->declaration; + decl_b = &integer_b->declaration; + + /* Length */ + if (decl_a->len != decl_b->len) { + goto end; + } + + /* + * Compare user byte orders only, not the cached, + * real byte orders. + */ + if (integer_a->user_byte_order != integer_b->user_byte_order) { + goto end; + } + + /* Signedness */ + if (decl_a->signedness != decl_b->signedness) { + goto end; + } + + /* Base */ + if (decl_a->base != decl_b->base) { + goto end; + } + + /* Encoding */ + if (decl_a->encoding != decl_b->encoding) { + goto end; + } + + /* Mapped clock */ + if (integer_a->mapped_clock != integer_b->mapped_clock) { + goto end; + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int bt_ctf_field_type_floating_point_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + struct bt_ctf_field_type_floating_point *float_a; + struct bt_ctf_field_type_floating_point *float_b; + + float_a = container_of(type_a, + struct bt_ctf_field_type_floating_point, parent); + float_b = container_of(type_b, + struct bt_ctf_field_type_floating_point, parent); + + /* Sign length */ + if (float_a->sign.len != float_b->sign.len) { + goto end; + } + + /* Exponent length */ + if (float_a->exp.len != float_b->exp.len) { + goto end; + } + + /* Mantissa length */ + if (float_a->mantissa.len != float_b->mantissa.len) { + goto end; + } + + /* + * Compare user byte orders only, not the cached, + * real byte orders. + */ + if (float_a->user_byte_order != float_b->user_byte_order) { + goto end; + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int compare_enumeration_mappings(struct enumeration_mapping *mapping_a, + struct enumeration_mapping *mapping_b) +{ + int ret = 1; + + /* Label */ + if (mapping_a->string != mapping_b->string) { + goto end; + } + + /* Range start */ + if (mapping_a->range_start._unsigned != + mapping_b->range_start._unsigned) { + goto end; + } + + /* Range end */ + if (mapping_a->range_end._unsigned != + mapping_b->range_end._unsigned) { + goto end; + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int bt_ctf_field_type_enumeration_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + int i; + struct bt_ctf_field_type_enumeration *enum_a; + struct bt_ctf_field_type_enumeration *enum_b; + + enum_a = container_of(type_a, + struct bt_ctf_field_type_enumeration, parent); + enum_b = container_of(type_b, + struct bt_ctf_field_type_enumeration, parent); + + /* Container field type */ + ret = bt_ctf_field_type_compare(enum_a->container, enum_b->container); + if (ret) { + goto end; + } + + ret = 1; + + /* Entries */ + if (enum_a->entries->len != enum_b->entries->len) { + goto end; + } + + for (i = 0; i < enum_a->entries->len; ++i) { + struct enumeration_mapping *mapping_a = + g_ptr_array_index(enum_a->entries, i); + struct enumeration_mapping *mapping_b = + g_ptr_array_index(enum_b->entries, i); + + if (compare_enumeration_mappings(mapping_a, mapping_b)) { + goto end; + } + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int bt_ctf_field_type_string_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + struct bt_ctf_field_type_string *string_a; + struct bt_ctf_field_type_string *string_b; + + string_a = container_of(type_a, + struct bt_ctf_field_type_string, parent); + string_b = container_of(type_b, + struct bt_ctf_field_type_string, parent); + + /* Encoding */ + if (string_a->declaration.encoding != string_b->declaration.encoding) { + goto end; + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int compare_structure_fields(struct structure_field *field_a, + struct structure_field *field_b) +{ + int ret = 1; + + /* Label */ + if (field_a->name != field_b->name) { + goto end; + } + + /* Type */ + ret = bt_ctf_field_type_compare(field_a->type, field_b->type); + +end: + return ret; +} + +static +int bt_ctf_field_type_structure_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + int i; + struct bt_ctf_field_type_structure *struct_a; + struct bt_ctf_field_type_structure *struct_b; + + struct_a = container_of(type_a, + struct bt_ctf_field_type_structure, parent); + struct_b = container_of(type_b, + struct bt_ctf_field_type_structure, parent); + + /* Alignment */ + if (bt_ctf_field_type_get_alignment(type_a) != + bt_ctf_field_type_get_alignment(type_b)) { + goto end; + } + + /* Fields */ + if (struct_a->fields->len != struct_b->fields->len) { + goto end; + } + + for (i = 0; i < struct_a->fields->len; ++i) { + struct structure_field *field_a = + g_ptr_array_index(struct_a->fields, i); + struct structure_field *field_b = + g_ptr_array_index(struct_b->fields, i); + + ret = compare_structure_fields(field_a, field_b); + if (ret) { + goto end; + } + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int bt_ctf_field_type_variant_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + int i; + struct bt_ctf_field_type_variant *variant_a; + struct bt_ctf_field_type_variant *variant_b; + + variant_a = container_of(type_a, + struct bt_ctf_field_type_variant, parent); + variant_b = container_of(type_b, + struct bt_ctf_field_type_variant, parent); + + /* Tag name */ + if (strcmp(variant_a->tag_name->str, variant_b->tag_name->str)) { + goto end; + } + + /* Tag type */ + ret = bt_ctf_field_type_compare( + (struct bt_ctf_field_type *) variant_a->tag, + (struct bt_ctf_field_type *) variant_b->tag); + if (ret) { + goto end; + } + + ret = 1; + + /* Fields */ + if (variant_a->fields->len != variant_b->fields->len) { + goto end; + } + + for (i = 0; i < variant_a->fields->len; ++i) { + struct structure_field *field_a = + g_ptr_array_index(variant_a->fields, i); + struct structure_field *field_b = + g_ptr_array_index(variant_b->fields, i); + + ret = compare_structure_fields(field_a, field_b); + if (ret) { + goto end; + } + } + + /* Equal */ + ret = 0; + +end: + return ret; +} + +static +int bt_ctf_field_type_array_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + struct bt_ctf_field_type_array *array_a; + struct bt_ctf_field_type_array *array_b; + + array_a = container_of(type_a, + struct bt_ctf_field_type_array, parent); + array_b = container_of(type_b, + struct bt_ctf_field_type_array, parent); + + /* Length */ + if (array_a->length != array_b->length) { + goto end; + } + + /* Element type */ + ret = bt_ctf_field_type_compare(array_a->element_type, + array_b->element_type); + +end: + return ret; +} + +static +int bt_ctf_field_type_sequence_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = -1; + struct bt_ctf_field_type_sequence *sequence_a; + struct bt_ctf_field_type_sequence *sequence_b; + + sequence_a = container_of(type_a, + struct bt_ctf_field_type_sequence, parent); + sequence_b = container_of(type_b, + struct bt_ctf_field_type_sequence, parent); + + /* Length name */ + if (strcmp(sequence_a->length_field_name->str, + sequence_b->length_field_name->str)) { + goto end; + } + + /* Element type */ + ret = bt_ctf_field_type_compare(sequence_a->element_type, + sequence_b->element_type); + +end: + return ret; +} + +int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b) +{ + int ret = 1; + + if (type_a == type_b) { + /* Same reference: equal (even if both are NULL) */ + ret = 0; + goto end; + } + + if (!type_a || !type_b) { + ret = -1; + goto end; + } + + if (type_a->declaration->id != type_b->declaration->id) { + /* Different type IDs */ + goto end; + } + + if (type_a->declaration->id == BT_CTF_TYPE_ID_UNKNOWN) { + /* Both have unknown type IDs */ + goto end; + } + + ret = type_compare_funcs[type_a->declaration->id](type_a, type_b); + +end: + return ret; +} + +BT_HIDDEN +int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *field_type) +{ + int field_count = -1; + enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); + + switch (type_id) { + case CTF_TYPE_STRUCT: + field_count = + bt_ctf_field_type_structure_get_field_count(field_type); + break; + case CTF_TYPE_VARIANT: + field_count = + bt_ctf_field_type_variant_get_field_count(field_type); + break; + case CTF_TYPE_ARRAY: + case CTF_TYPE_SEQUENCE: + /* + * Array and sequence types always contain a single member + * (the element type). + */ + field_count = 1; + break; + default: + break; + } + + return field_count; +} + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index( + struct bt_ctf_field_type *field_type, int index) +{ + struct bt_ctf_field_type *field = NULL; + enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); + + switch (type_id) { + case CTF_TYPE_STRUCT: + bt_ctf_field_type_structure_get_field(field_type, NULL, &field, + index); + break; + case CTF_TYPE_VARIANT: + bt_ctf_field_type_variant_get_field(field_type, NULL, + &field, index); + break; + case CTF_TYPE_ARRAY: + field = bt_ctf_field_type_array_get_element_type(field_type); + break; + case CTF_TYPE_SEQUENCE: + field = bt_ctf_field_type_sequence_get_element_type(field_type); + break; + default: + break; + } + + return field; +} + +BT_HIDDEN +int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *field_type, + const char *name) +{ + int field_index = -1; + enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(field_type); + + switch (type_id) { + case CTF_TYPE_STRUCT: + field_index = bt_ctf_field_type_structure_get_field_name_index( + field_type, name); + break; + case CTF_TYPE_VARIANT: + field_index = bt_ctf_field_type_variant_get_field_name_index( + field_type, name); + break; + default: + break; + } + + return field_index; +} + +struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_path *field_path = NULL; + struct bt_ctf_field_type_variant *variant; + + if (!type || !bt_ctf_field_type_is_variant(type)) { + goto end; + } + + variant = container_of(type, struct bt_ctf_field_type_variant, + parent); + field_path = bt_get(variant->tag_field_path); +end: + return field_path; +} + +struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( + struct bt_ctf_field_type *type) +{ + struct bt_ctf_field_path *field_path = NULL; + struct bt_ctf_field_type_sequence *sequence; + + if (!type || !bt_ctf_field_type_is_sequence(type)) { + goto end; + } + + sequence = container_of(type, struct bt_ctf_field_type_sequence, + parent); + field_path = bt_get(sequence->length_field_path); +end: + return field_path; +} diff --git a/formats/ctf/ir/fields.c b/formats/ctf/ir/fields.c new file mode 100644 index 00000000..93e9486c --- /dev/null +++ b/formats/ctf/ir/fields.c @@ -0,0 +1,2202 @@ +/* + * fields.c + * + * Babeltrace CTF IR - Event Fields + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +#define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) + +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 *); + +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 *); + +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 *); + +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 *); + +static +int bt_ctf_field_integer_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_structure_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_variant_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_array_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); +static +int bt_ctf_field_string_serialize(struct bt_ctf_field *, + struct ctf_stream_pos *); + +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 *); + +static +int increase_packet_size(struct ctf_stream_pos *pos); + +static +struct bt_ctf_field *(* const field_create_funcs[])( + struct bt_ctf_field_type *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_floating_point_create, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_create, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_create, +}; + +static +void (* const field_destroy_funcs[])(struct bt_ctf_field *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_destroy, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_destroy, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_floating_point_destroy, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_destroy, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_destroy, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_destroy, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_destroy, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_destroy, +}; + +static +int (* const field_validate_funcs[])(struct bt_ctf_field *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_generic_validate, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_validate, + [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_generic_validate, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_validate, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_validate, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_validate, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_validate, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_generic_validate, +}; + +static +int (* const field_reset_funcs[])(struct bt_ctf_field *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_generic_reset, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_reset, + [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_generic_reset, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_reset, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_reset, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_reset, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_reset, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_reset, +}; + +static +int (* const field_serialize_funcs[])(struct bt_ctf_field *, + struct ctf_stream_pos *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_serialize, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_serialize, + [BT_CTF_TYPE_ID_FLOAT] = + bt_ctf_field_floating_point_serialize, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_serialize, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_serialize, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_serialize, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_serialize, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_serialize, +}; + +static +int (* const field_copy_funcs[])(struct bt_ctf_field *, + struct bt_ctf_field *) = { + [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_integer_copy, + [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_enumeration_copy, + [BT_CTF_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_copy, + [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_structure_copy, + [BT_CTF_TYPE_ID_VARIANT] = bt_ctf_field_variant_copy, + [BT_CTF_TYPE_ID_ARRAY] = bt_ctf_field_array_copy, + [BT_CTF_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_copy, + [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_string_copy, +}; + +struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) +{ + struct bt_ctf_field *field = NULL; + enum bt_ctf_type_id type_id; + int ret; + + if (!type) { + goto error; + } + + type_id = bt_ctf_field_type_get_type_id(type); + if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || + type_id >= BT_CTF_NR_TYPE_IDS) { + goto error; + } + + /* Field class MUST be valid */ + ret = bt_ctf_field_type_validate(type); + + if (ret) { + /* Invalid */ + goto error; + } + + field = field_create_funcs[type_id](type); + if (!field) { + goto error; + } + + /* 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; +} + +void bt_ctf_field_get(struct bt_ctf_field *field) +{ + bt_get(field); +} + +void bt_ctf_field_put(struct bt_ctf_field *field) +{ + bt_put(field); +} + +struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) +{ + struct bt_ctf_field_type *ret = NULL; + + if (!field) { + goto end; + } + + ret = field->type; + bt_get(ret); +end: + return ret; +} + +enum bt_ctf_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) +{ + enum bt_ctf_type_id ret = BT_CTF_TYPE_ID_UNKNOWN; + + if (!field) { + goto end; + } + + ret = bt_ctf_field_type_get_type_id(field->type); +end: + return ret; +} + +int bt_ctf_field_is_integer(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_INTEGER; +} + +int bt_ctf_field_is_floating_point(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_FLOAT; +} + +int bt_ctf_field_is_enumeration(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_ENUM; +} + +int bt_ctf_field_is_string(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_STRING; +} + +int bt_ctf_field_is_structure(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_STRUCT; +} + +int bt_ctf_field_is_array(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_ARRAY; +} + +int bt_ctf_field_is_sequence(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_SEQUENCE; +} + +int bt_ctf_field_is_variant(struct bt_ctf_field *field) +{ + return bt_ctf_field_get_type_id(field) == BT_CTF_TYPE_ID_VARIANT; +} + +struct bt_ctf_field *bt_ctf_field_sequence_get_length( + struct bt_ctf_field *field) +{ + struct bt_ctf_field *ret = NULL; + struct bt_ctf_field_sequence *sequence; + + if (!field) { + goto end; + } + + if (bt_ctf_field_type_get_type_id(field->type) != + BT_CTF_TYPE_ID_SEQUENCE) { + goto end; + } + + 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) +{ + 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) { + ret = -1; + goto end; + } + if (bt_ctf_field_type_get_type_id(length_field->type) != + BT_CTF_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->declaration.signedness) { + ret = -1; + goto end; + } + + length = container_of(length_field, struct bt_ctf_field_integer, + parent); + sequence_length = length->definition.value._unsigned; + sequence = container_of(field, struct bt_ctf_field_sequence, parent); + if (sequence->elements) { + g_ptr_array_free(sequence->elements, TRUE); + bt_put(sequence->length); + } + + sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); + if (!sequence->elements) { + ret = -1; + goto end; + } + + 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( + struct bt_ctf_field *field, const char *name) +{ + 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_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; + } + + new_field = bt_ctf_field_create(field_type); + if (!new_field) { + goto error; + } + + structure->fields->pdata[index] = new_field; +end: + bt_get(new_field); +error: + if (field_type) { + bt_put(field_type); + } + return new_field; +} + +struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( + struct bt_ctf_field *field, int index) +{ + 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_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; + } + + /* 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; + } + + structure->fields->pdata[index] = ret_field; +end: + bt_get(ret_field); +error: + bt_put(field_type); + return ret_field; +} + +BT_HIDDEN +int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, + const char *name, struct bt_ctf_field *value) +{ + 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 || + bt_ctf_field_type_get_type_id(field->type) != + BT_CTF_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; +} + +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_ctf_field_type_get_type_id(field->type) != + BT_CTF_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; + } + + 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_ctf_field_type_get_type_id(field->type) != + BT_CTF_TYPE_ID_SEQUENCE) { + goto end; + } + + sequence = container_of(field, struct bt_ctf_field_sequence, parent); + if (!sequence->elements || sequence->elements->len <= index) { + 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; + } + + 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_TYPE_ID_VARIANT || + bt_ctf_field_type_get_type_id(tag_field->type) != + BT_CTF_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->definition.value._signed; + + /* + * 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->definition.value._signed; + + if (cur_tag_value == tag_enum_value) { + new_field = variant->payload; + bt_get(new_field); + 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_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) { + 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; +} + +const char *bt_ctf_field_enumeration_get_mapping_name( + struct bt_ctf_field *field) +{ + int ret; + const char *name = NULL; + 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 *enumeration_type = 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); + enumeration_type = container_of(field->type, + struct bt_ctf_field_type_enumeration, parent); + + if (!integer_type->declaration.signedness) { + uint64_t value; + ret = bt_ctf_field_unsigned_integer_get_value(container, + &value); + if (ret) { + goto error_put_container_type; + } + + name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned( + enumeration_type, value); + } else { + int64_t value; + ret = bt_ctf_field_signed_integer_get_value(container, + &value); + if (ret) { + goto error_put_container_type; + } + + name = bt_ctf_field_type_enumeration_get_mapping_name_signed( + enumeration_type, value); + } + +error_put_container_type: + bt_put(container_type); +error_put_container: + bt_put(container); +end: + return name; +} + +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->declaration.signedness) { + ret = -1; + goto end; + } + + integer = container_of(field, + struct bt_ctf_field_integer, parent); + *value = integer->definition.value._signed; +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_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->declaration.signedness) { + ret = -1; + goto end; + } + + size = integer_type->declaration.len; + min_value = -((int64_t)1 << (size - 1)); + max_value = ((int64_t)1 << (size - 1)) - 1; + if (value < min_value || value > max_value) { + ret = -1; + goto end; + } + + integer->definition.value._signed = 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->declaration.signedness) { + ret = -1; + goto end; + } + + integer = container_of(field, + struct bt_ctf_field_integer, parent); + *value = integer->definition.value._unsigned; +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_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->declaration.signedness) { + ret = -1; + goto end; + } + + size = integer_type->declaration.len; + max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; + if (value > max_value) { + ret = -1; + goto end; + } + + integer->definition.value._unsigned = 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->definition.value; +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_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); + floating_point->definition.value = 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_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 || + bt_ctf_field_type_get_type_id(field->type) != + BT_CTF_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 || + bt_ctf_field_type_get_type_id(field->type) != + BT_CTF_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 || + bt_ctf_field_type_get_type_id(field->type) != + BT_CTF_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_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_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_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_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 ctf_stream_pos *pos) +{ + int ret = 0; + enum bt_ctf_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_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { + ret = -1; + goto end; + } + + ret = field_serialize_funcs[type_id](field, pos); +end: + return ret; +} + +struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) +{ + int ret; + struct bt_ctf_field *copy = NULL; + enum bt_ctf_type_id type_id; + + if (!field) { + goto end; + } + + type_id = bt_ctf_field_type_get_type_id(field->type); + if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || type_id >= BT_CTF_NR_TYPE_IDS) { + goto end; + } + + copy = bt_ctf_field_create(field->type); + if (!copy) { + 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_type_integer *integer_type = container_of(type, + struct bt_ctf_field_type_integer, parent); + struct bt_ctf_field_integer *integer = g_new0( + struct bt_ctf_field_integer, 1); + + if (integer) { + integer->definition.declaration = &integer_type->declaration; + } + + 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); + + 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; + struct bt_ctf_field_type_floating_point *floating_point_type; + + floating_point = g_new0(struct bt_ctf_field_floating_point, 1); + if (!floating_point) { + goto end; + } + + floating_point_type = container_of(type, + struct bt_ctf_field_type_floating_point, parent); + floating_point->definition.declaration = container_of( + type->declaration, struct declaration_float, p); + + + floating_point->definition.sign = &floating_point->sign; + floating_point->sign.declaration = &floating_point_type->sign; + floating_point->definition.sign->p.declaration = + &floating_point_type->sign.p; + + floating_point->definition.mantissa = &floating_point->mantissa; + floating_point->mantissa.declaration = &floating_point_type->mantissa; + floating_point->definition.mantissa->p.declaration = + &floating_point_type->mantissa.p; + + floating_point->definition.exp = &floating_point->exp; + floating_point->exp.declaration = &floating_point_type->exp; + floating_point->definition.exp->p.declaration = + &floating_point_type->exp.p; + +end: + 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; + + if (!structure) { + 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; +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); + 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; + + 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; +} + +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); + 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); + 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_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); + if (type_id <= BT_CTF_TYPE_ID_UNKNOWN || + type_id >= BT_CTF_NR_TYPE_IDS) { + return; + } + + field_destroy_funcs[type_id](field); + bt_put(type); +} + +static +void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) +{ + struct bt_ctf_field_integer *integer; + + if (!field) { + return; + } + + 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; + } + + enumeration = container_of(field, struct bt_ctf_field_enumeration, + parent); + 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; + } + + 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; + } + + 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; + } + + variant = container_of(field, struct bt_ctf_field_variant, parent); + bt_put(variant->tag); + 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; + } + + 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; + + if (!field) { + return; + } + + 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 +void bt_ctf_field_string_destroy(struct bt_ctf_field *field) +{ + struct bt_ctf_field_string *string; + if (!field) { + return; + } + + string = container_of(field, struct bt_ctf_field_string, parent); + if (string->payload) { + g_string_free(string->payload, TRUE); + } + g_free(string); +} + +static +int bt_ctf_field_generic_validate(struct bt_ctf_field *field) +{ + return (field && field->payload_set) ? 0 : -1; +} + +static +int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) +{ + int ret; + struct bt_ctf_field_enumeration *enumeration; + + if (!field) { + ret = -1; + goto end; + } + + enumeration = container_of(field, struct bt_ctf_field_enumeration, + parent); + if (!enumeration->payload) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_validate(enumeration->payload); +end: + return ret; +} + +static +int bt_ctf_field_structure_validate(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_structure *structure; + + if (!field) { + ret = -1; + goto end; + } + + 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) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_variant_validate(struct bt_ctf_field *field) +{ + int ret = 0; + struct bt_ctf_field_variant *variant; + + if (!field) { + ret = -1; + goto end; + } + + variant = container_of(field, struct bt_ctf_field_variant, parent); + ret = bt_ctf_field_validate(variant->payload); +end: + return ret; +} + +static +int bt_ctf_field_array_validate(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_array *array; + + if (!field) { + ret = -1; + goto end; + } + + 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) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_sequence *sequence; + + if (!field) { + ret = -1; + goto end; + } + + 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) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_generic_reset(struct bt_ctf_field *field) +{ + int ret = 0; + + if (!field) { + ret = -1; + goto end; + } + + field->payload_set = 0; +end: + return ret; +} + +static +int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field) +{ + int ret = 0; + struct bt_ctf_field_enumeration *enumeration; + + if (!field) { + ret = -1; + goto end; + } + + enumeration = container_of(field, struct bt_ctf_field_enumeration, + parent); + if (!enumeration->payload) { + goto end; + } + + ret = bt_ctf_field_reset(enumeration->payload); +end: + return ret; +} + +static +int bt_ctf_field_structure_reset(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_structure *structure; + + if (!field) { + ret = -1; + goto end; + } + + 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]; + + if (!member) { + /* + * Structure members are lazily initialized; skip if + * this member has not been allocated yet. + */ + continue; + } + + ret = bt_ctf_field_reset(member); + if (ret) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_variant_reset(struct bt_ctf_field *field) +{ + int ret = 0; + struct bt_ctf_field_variant *variant; + + if (!field) { + ret = -1; + goto end; + } + + variant = container_of(field, struct bt_ctf_field_variant, parent); + if (variant->payload) { + ret = bt_ctf_field_reset(variant->payload); + } +end: + return ret; +} + +static +int bt_ctf_field_array_reset(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_array *array; + + if (!field) { + ret = -1; + 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]; + + if (!member) { + /* + * Array elements are lazily initialized; skip if + * this member has not been allocated yet. + */ + continue; + } + + ret = bt_ctf_field_reset(member); + if (ret) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_sequence_reset(struct bt_ctf_field *field) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_sequence *sequence; + + if (!field) { + 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; + } + + 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) +{ + int ret = 0; + struct bt_ctf_field_string *string; + + if (!field) { + ret = -1; + goto end; + } + + ret = bt_ctf_field_generic_reset(field); + if (ret) { + goto end; + } + + string = container_of(field, struct bt_ctf_field_string, parent); + if (string->payload) { + g_string_truncate(string->payload, 0); + } +end: + return ret; +} + +static +int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + int ret = 0; + struct bt_ctf_field_integer *integer = container_of(field, + struct bt_ctf_field_integer, parent); + +retry: + ret = ctf_integer_write(&pos->parent, &integer->definition.p); + 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; + } +end: + return ret; +} + +static +int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + struct bt_ctf_field_enumeration *enumeration = container_of( + field, struct bt_ctf_field_enumeration, parent); + + return bt_ctf_field_serialize(enumeration->payload, pos); +} + +static +int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + int ret = 0; + struct bt_ctf_field_floating_point *floating_point = container_of(field, + struct bt_ctf_field_floating_point, parent); + +retry: + ret = ctf_float_write(&pos->parent, &floating_point->definition.p); + 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; + } +end: + return ret; +} + +static +int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_structure *structure = container_of( + field, struct bt_ctf_field_structure, parent); + + while (!ctf_pos_access_ok(pos, + offset_align(pos->offset, + field->type->declaration->alignment))) { + ret = increase_packet_size(pos); + if (ret) { + goto end; + } + } + + if (!ctf_align_pos(pos, field->type->declaration->alignment)) { + ret = -1; + goto end; + } + + for (i = 0; i < structure->fields->len; i++) { + struct bt_ctf_field *field = g_ptr_array_index( + structure->fields, i); + + ret = bt_ctf_field_serialize(field, pos); + if (ret) { + break; + } + } +end: + return ret; +} + +static +int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + struct bt_ctf_field_variant *variant = container_of( + field, struct bt_ctf_field_variant, parent); + + return bt_ctf_field_serialize(variant->payload, pos); +} + +static +int bt_ctf_field_array_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_array *array = container_of( + field, struct bt_ctf_field_array, parent); + + for (i = 0; i < array->elements->len; i++) { + ret = bt_ctf_field_serialize( + g_ptr_array_index(array->elements, i), pos); + if (ret) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + size_t i; + int ret = 0; + struct bt_ctf_field_sequence *sequence = container_of( + field, struct bt_ctf_field_sequence, parent); + + for (i = 0; i < sequence->elements->len; i++) { + ret = bt_ctf_field_serialize( + g_ptr_array_index(sequence->elements, i), pos); + if (ret) { + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_string_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos) +{ + size_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; + } + + ret = bt_ctf_field_integer_serialize(character, pos); + if (ret) { + 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); + + memcpy(&integer_dst->definition, &integer_src->definition, + sizeof(struct definition_integer)); + return 0; +} + +static +int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + int ret = 0; + struct bt_ctf_field_enumeration *enum_src, *enum_dst; + + enum_src = container_of(src, struct bt_ctf_field_enumeration, parent); + enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent); + + if (enum_src->payload) { + enum_dst->payload = bt_ctf_field_copy(enum_src->payload); + if (!enum_dst->payload) { + ret = -1; + goto end; + } + } +end: + return ret; +} + +static +int bt_ctf_field_floating_point_copy( + struct bt_ctf_field *src, struct bt_ctf_field *dst) +{ + 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); + + memcpy(&float_dst->definition, &float_src->definition, + sizeof(struct definition_float)); + memcpy(&float_dst->sign, &float_src->sign, + sizeof(struct definition_integer)); + memcpy(&float_dst->mantissa, &float_src->mantissa, + sizeof(struct definition_integer)); + memcpy(&float_dst->exp, &float_src->exp, + sizeof(struct definition_integer)); + return 0; +} + +static +int bt_ctf_field_structure_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + 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); + + 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; + + if (field) { + field_copy = bt_ctf_field_copy(field); + + if (!field_copy) { + ret = -1; + goto end; + } + } + + g_ptr_array_index(struct_dst->fields, i) = field_copy; + } +end: + return ret; +} + +static +int bt_ctf_field_variant_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + 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); + + 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; +} + +static +int bt_ctf_field_array_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + 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); + + 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; + + if (field) { + field_copy = bt_ctf_field_copy(field); + + if (!field_copy) { + ret = -1; + goto end; + } + } + + g_ptr_array_index(array_dst->elements, i) = field_copy; + } +end: + return ret; +} + +static +int bt_ctf_field_sequence_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + 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); + + 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; + + if (field) { + field_copy = bt_ctf_field_copy(field); + + if (!field_copy) { + ret = -1; + goto end; + } + } + + g_ptr_array_index(sequence_dst->elements, i) = field_copy; + } +end: + return ret; +} + +static +int bt_ctf_field_string_copy(struct bt_ctf_field *src, + struct bt_ctf_field *dst) +{ + 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; +} + +static +int increase_packet_size(struct ctf_stream_pos *pos) +{ + int ret; + + assert(pos); + ret = munmap_align(pos->base_mma); + if (ret) { + goto end; + } + + 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; + } + + 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; + } +end: + return ret; +} diff --git a/formats/ctf/ir/resolve.c b/formats/ctf/ir/resolve.c index c9a56b79..fba3f03e 100644 --- a/formats/ctf/ir/resolve.c +++ b/formats/ctf/ir/resolve.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/formats/ctf/ir/stream-class.c b/formats/ctf/ir/stream-class.c index ead548a7..aceb5b77 100644 --- a/formats/ctf/ir/stream-class.c +++ b/formats/ctf/ir/stream-class.c @@ -30,8 +30,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/formats/ctf/ir/stream.c b/formats/ctf/ir/stream.c index 1ee583f0..97f17946 100644 --- a/formats/ctf/ir/stream.c +++ b/formats/ctf/ir/stream.c @@ -30,8 +30,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index 75a47952..de8055b8 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/formats/ctf/ir/validation.c b/formats/ctf/ir/validation.c index 6deaa7a0..789a6f57 100644 --- a/formats/ctf/ir/validation.c +++ b/formats/ctf/ir/validation.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/formats/ctf/writer/writer.c b/formats/ctf/writer/writer.c index 52c153d1..babe033b 100644 --- a/formats/ctf/writer/writer.c +++ b/formats/ctf/writer/writer.c @@ -28,8 +28,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/include/Makefile.am b/include/Makefile.am index fbb7fce6..751128a9 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -25,8 +25,8 @@ babeltracectfwriterinclude_HEADERS = \ babeltracectfirinclude_HEADERS = \ babeltrace/ctf-ir/clock.h \ - babeltrace/ctf-ir/event-fields.h \ - babeltrace/ctf-ir/event-types.h \ + babeltrace/ctf-ir/fields.h \ + babeltrace/ctf-ir/field-types.h \ babeltrace/ctf-ir/event.h \ babeltrace/ctf-ir/field-path.h \ babeltrace/ctf-ir/stream.h \ @@ -57,8 +57,8 @@ noinst_HEADERS = \ babeltrace/ctf/ctf-index.h \ babeltrace/ctf-writer/writer-internal.h \ babeltrace/ctf-ir/attributes-internal.h \ - babeltrace/ctf-ir/event-types-internal.h \ - babeltrace/ctf-ir/event-fields-internal.h \ + babeltrace/ctf-ir/field-types-internal.h \ + babeltrace/ctf-ir/fields-internal.h \ babeltrace/ctf-ir/event-internal.h \ babeltrace/ctf-ir/field-path-internal.h \ babeltrace/ctf-ir/clock-internal.h \ diff --git a/include/babeltrace/ctf-ir/event-fields-internal.h b/include/babeltrace/ctf-ir/event-fields-internal.h deleted file mode 100644 index 4dea4a79..00000000 --- a/include/babeltrace/ctf-ir/event-fields-internal.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H -#define BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H - -/* - * BabelTrace - CTF Writer: Event Fields internal - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include - -struct bt_ctf_field { - struct bt_object base; - struct bt_ctf_field_type *type; - int payload_set; -}; - -struct bt_ctf_field_integer { - struct bt_ctf_field parent; - struct definition_integer definition; -}; - -struct bt_ctf_field_enumeration { - struct bt_ctf_field parent; - struct bt_ctf_field *payload; -}; - -struct bt_ctf_field_floating_point { - struct bt_ctf_field parent; - struct definition_float definition; - struct definition_integer sign, mantissa, exp; -}; - -struct bt_ctf_field_structure { - struct bt_ctf_field parent; - GHashTable *field_name_to_index; - GPtrArray *fields; /* Array of pointers to struct bt_ctf_field */ -}; - -struct bt_ctf_field_variant { - struct bt_ctf_field parent; - struct bt_ctf_field *tag; - struct bt_ctf_field *payload; -}; - -struct bt_ctf_field_array { - struct bt_ctf_field parent; - GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */ -}; - -struct bt_ctf_field_sequence { - struct bt_ctf_field parent; - struct bt_ctf_field *length; - GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */ -}; - -struct bt_ctf_field_string { - struct bt_ctf_field parent; - GString *payload; -}; - -/* - * Set a field's value with an already allocated field instance. - */ -BT_HIDDEN -int bt_ctf_field_structure_set_field(struct bt_ctf_field *structure, - const char *name, struct bt_ctf_field *value); - -/* Validate that the field's payload is set (returns 0 if set). */ -BT_HIDDEN -int bt_ctf_field_validate(struct bt_ctf_field *field); - -/* Mark field payload as unset. */ -BT_HIDDEN -int bt_ctf_field_reset(struct bt_ctf_field *field); - -BT_HIDDEN -int bt_ctf_field_serialize(struct bt_ctf_field *field, - struct ctf_stream_pos *pos); - -#endif /* BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/event-fields.h b/include/babeltrace/ctf-ir/event-fields.h deleted file mode 100644 index 28d6fa5a..00000000 --- a/include/babeltrace/ctf-ir/event-fields.h +++ /dev/null @@ -1,486 +0,0 @@ -#ifndef BABELTRACE_CTF_IR_EVENT_FIELDS_H -#define BABELTRACE_CTF_IR_EVENT_FIELDS_H - -/* - * BabelTrace - CTF IR: Event Fields - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * The Common Trace Format (CTF) Specification is available at - * http://www.efficios.com/ctf - */ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct bt_ctf_event_class; -struct bt_ctf_event; -struct bt_ctf_field; -struct bt_ctf_field_type; - -/* - * bt_ctf_field_create: create an instance of a field. - * - * Allocate a new field of the type described by the bt_ctf_field_type - * structure.The creation of a field sets its reference count to 1. - * - * @param type Field type to be instanciated. - * - * Returns an allocated field on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_create( - struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_structure_get_field: get a structure's field. - * - * Get the structure's field corresponding to the provided field name. - * bt_ctf_field_put() must be called on the returned value. - * - * @param structure Structure field instance. - * @param name Name of the field in the provided structure. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_structure_get_field( - struct bt_ctf_field *structure, const char *name); - -/* - * bt_ctf_field_structure_get_field_by_index: get a structure's field by index. - * - * Get the structure's field corresponding to the provided field name. - * bt_ctf_field_put() must be called on the returned value. - * The indexes are the same as those provided for bt_ctf_field_type_structure. - * - * @param structure Structure field instance. - * @param index Index of the field in the provided structure. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( - struct bt_ctf_field *structure, int index); - -/* - * bt_ctf_field_array_get_field: get an array's field at position "index". - * - * Return the array's field at position "index". bt_ctf_field_put() must be - * called on the returned value. - * - * @param array Array field instance. - * @param index Position of the array's desired element. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_array_get_field( - struct bt_ctf_field *array, uint64_t index); - -/* - * bt_ctf_field_sequence_get_length: get a sequence's length. - * - * Get the sequence's length field. - * - * @param sequence Sequence field instance. - * - * Returns a field instance on success, NULL if a length was never set. - */ -extern struct bt_ctf_field *bt_ctf_field_sequence_get_length( - struct bt_ctf_field *sequence); - -/* - * bt_ctf_field_sequence_set_length: set a sequence's length. - * - * Set the sequence's length field. - * - * @param sequence Sequence field instance. - * @param length_field Unsigned integer field instance indicating the - * sequence's length. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, - struct bt_ctf_field *length_field); - -/* - * bt_ctf_field_sequence_get_field: get a sequence's field at position "index". - * - * Return the sequence's field at position "index". The sequence's length must - * have been set prior to calling this function using - * bt_ctf_field_sequence_set_length(). - * bt_ctf_field_put() must be called on the returned value. - * - * @param array Sequence field instance. - * @param index Position of the sequence's desired element. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_sequence_get_field( - struct bt_ctf_field *sequence, uint64_t index); - -/* - * bt_ctf_field_variant_get_field: get a variant's selected field. - * - * Return the variant's selected field. The "tag" field is the selector enum - * field. bt_ctf_field_put() must be called on the returned value. - * - * @param variant Variant field instance. - * @param tag Selector enumeration field. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_variant_get_field( - struct bt_ctf_field *variant, struct bt_ctf_field *tag); - -/* - * bt_ctf_field_variant_get_current_field: get the current selected field of a - * variant. - * - * Return the variant's current selected field. This function, unlike - * bt_ctf_field_variant_get_field(), does not create any field; it - * returns NULL if there's no current selected field yet. - * - * @param variant Variant field instance. - * - * Returns a field instance on success, NULL on error or when there's no - * current selected field. - */ -extern struct bt_ctf_field *bt_ctf_field_variant_get_current_field( - struct bt_ctf_field *variant); - -/* - * bt_ctf_field_enumeration_get_container: get an enumeration field's container. - * - * Return the enumeration's underlying container field (an integer). - * bt_ctf_field_put() must be called on the returned value. - * - * @param enumeration Enumeration field instance. - * - * Returns a field instance on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_enumeration_get_container( - struct bt_ctf_field *enumeration); - -/* - * bt_ctf_field_enumeration_get_mapping_name: get an enumeration field's mapping - * name. - * - * Return the enumeration's underlying container field (an integer). - * bt_ctf_field_put() must be called on the returned value. - * - * @param enumeration Enumeration field instance. - * - * Returns a field instance on success, NULL on error. - */ -extern const char *bt_ctf_field_enumeration_get_mapping_name( - struct bt_ctf_field *enumeration); - -/* - * bt_ctf_field_signed_integer_get_value: get a signed integer field's value - * - * Get a signed integer field's value. - * - * @param integer Signed integer field instance. - * @param value Pointer to a signed integer where the value will be stored. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *integer, - int64_t *value); - -/* - * bt_ctf_field_signed_integer_set_value: set a signed integer field's value - * - * Set a signed integer field's value. The value is checked to make sure it - * can be stored in the underlying field. - * - * @param integer Signed integer field instance. - * @param value Signed integer field value. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, - int64_t value); - -/* - * bt_ctf_field_unsigned_integer_get_value: get unsigned integer field's value - * - * Get an unsigned integer field's value. - * - * @param integer Unsigned integer field instance. - * @param value Pointer to an unsigned integer where the value will be stored. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *integer, - uint64_t *value); - -/* - * bt_ctf_field_unsigned_integer_set_value: set unsigned integer field's value - * - * Set an unsigned integer field's value. The value is checked to make sure it - * can be stored in the underlying field. - * - * @param integer Unsigned integer field instance. - * @param value Unsigned integer field value. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, - uint64_t value); - -/* - * bt_ctf_field_floating_point_get_value: get a floating point field's value - * - * Get a floating point field's value. - * - * @param floating_point Floating point field instance. - * @param value Pointer to a double where the value will be stored. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_floating_point_get_value( - struct bt_ctf_field *floating_point, double *value); - -/* - * bt_ctf_field_floating_point_set_value: set a floating point field's value - * - * Set a floating point field's value. The underlying type may not support the - * double's full precision. - * - * @param floating_point Floating point field instance. - * @param value Floating point field value. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_floating_point_set_value( - struct bt_ctf_field *floating_point, - double value); - -/* - * bt_ctf_field_string_get_value: get a string field's value - * - * Get a string field's value. - * - * @param string_field String field instance. - * - * Returns the string's value, NULL if unset. - */ -extern const char *bt_ctf_field_string_get_value( - struct bt_ctf_field *string_field); - -/* - * bt_ctf_field_string_set_value: set a string field's value - * - * Set a string field's value. - * - * @param string_field String field instance. - * @param value String field value (will be copied). - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_string_set_value(struct bt_ctf_field *string_field, - const char *value); - -/* - * bt_ctf_field_string_append: append a string to a string field's - * current value. - * - * Append a string to the current value of a string field. If the string - * field was never set using bt_ctf_field_string_set_value(), it is - * first set to an empty string, and then the concatenation happens. - * - * @param string_field String field instance. - * @param value String to append to the current string field's value. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_string_append(struct bt_ctf_field *string_field, - const char *value); - -/* - * bt_ctf_field_string_append_len: append a string of a given length to - * a string field's current value. - * - * Append a string of a given length to the current value of a string - * field. If the string field was never set using - * bt_ctf_field_string_set_value(), it is first set to an empty string, - * and then the concatenation happens. - * - * If a null byte is encountered before the given length, only the - * substring before the first null byte is appended. - * - * @param string_field String field instance. - * @param value String to append to the current string field's value. - * @param length Length of string value to append. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_string_append_len( - struct bt_ctf_field *string_field, const char *value, - unsigned int length); - -/* - * bt_ctf_field_get_type: get a field's type - * - * @param field Field intance. - * - * Returns a field type instance on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_get_type( - struct bt_ctf_field *field); - -/* - * bt_ctf_field_get_type_id: get a field's ctf_type_id. - * - * This is a helper function which avoids a call to - * bt_ctf_field_get_type(), followed by a call to - * bt_ctf_field_type_get_type_id(), followed by a call to - * bt_ctf_put(). - * - * @param field Field instance. - * - * Returns the field's ctf_type_id, CTF_TYPE_UNKNOWN on error. - */ -extern enum bt_ctf_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_integer: returns whether or not a given field - * is an integer type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is an integer type, 0 otherwise. - */ -extern int bt_ctf_field_is_integer(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_floating_point: returns whether or not a given field - * is a floating point number type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is a floating point number type, 0 otherwise. - */ -extern int bt_ctf_field_is_floating_point(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_enumeration: returns whether or not a given field - * is an enumeration type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is an enumeration type, 0 otherwise. - */ -extern int bt_ctf_field_is_enumeration(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_string: returns whether or not a given field - * is a string type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is a string type, 0 otherwise. - */ -extern int bt_ctf_field_is_string(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_structure: returns whether or not a given field - * is a structure type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is a structure type, 0 otherwise. - */ -extern int bt_ctf_field_is_structure(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_array: returns whether or not a given field - * is an array type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is an array type, 0 otherwise. - */ -extern int bt_ctf_field_is_array(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_sequence: returns whether or not a given field - * is a sequence type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is a sequence type, 0 otherwise. - */ -extern int bt_ctf_field_is_sequence(struct bt_ctf_field *field); - -/* - * bt_ctf_field_is_variant: returns whether or not a given field - * is a variant type. - * - * @param field Field instance. - * - * Returns 1 if the field instance is a variant type, 0 otherwise. - */ -extern int bt_ctf_field_is_variant(struct bt_ctf_field *field); - -/* - * bt_ctf_field_copy: get a field's deep copy. - * - * Get a field's deep copy. The created field copy shares the source's - * associated field types. - * - * On success, the returned copy has its reference count set to 1. - * - * @param field Field instance. - * - * Returns the field copy on success, NULL on error. - */ -extern struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field); - -/* - * bt_ctf_field_get and bt_ctf_field_put: increment and decrement the - * field's reference count. - * - * You may also use bt_ctf_get() and bt_ctf_put() with field objects. - * - * These functions ensure that the field won't be destroyed when it - * is in use. The same number of get and put (plus one extra put to - * release the initial reference done at creation) have to be done to - * destroy a field. - * - * When the field's reference count is decremented to 0 by a bt_ctf_field_put, - * the field is freed. - * - * @param field Field instance. - */ -extern void bt_ctf_field_get(struct bt_ctf_field *field); -extern void bt_ctf_field_put(struct bt_ctf_field *field); - -#ifdef __cplusplus -} -#endif - -#endif /* BABELTRACE_CTF_IR_EVENT_FIELDS_H */ diff --git a/include/babeltrace/ctf-ir/event-types-internal.h b/include/babeltrace/ctf-ir/event-types-internal.h deleted file mode 100644 index 8eaaeb88..00000000 --- a/include/babeltrace/ctf-ir/event-types-internal.h +++ /dev/null @@ -1,251 +0,0 @@ -#ifndef BABELTRACE_CTF_IR_EVENT_TYPES_INTERNAL_H -#define BABELTRACE_CTF_IR_EVENT_TYPES_INTERNAL_H - -/* - * BabelTrace - CTF IR: Event types internal - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef void (*type_freeze_func)(struct bt_ctf_field_type *); -typedef int (*type_serialize_func)(struct bt_ctf_field_type *, - struct metadata_context *); - -struct bt_ctf_field_type { - struct bt_object base; - struct bt_declaration *declaration; - type_freeze_func freeze; - type_serialize_func serialize; - /* - * A type can't be modified once it is added to an event or after a - * a field has been instanciated from it. - */ - int frozen; - - /* - * This flag indicates if the field type is valid. A valid - * field type is _always_ frozen. All the nested field types of - * a valid field type are also valid (and thus frozen). - */ - int valid; -}; - -struct bt_ctf_field_type_integer { - struct bt_ctf_field_type parent; - struct declaration_integer declaration; - struct bt_ctf_clock *mapped_clock; - - /* - * This is what the user sets and is never modified by internal - * code. - * - * This field must contain a `BT_CTF_BYTE_ORDER_*` value. - */ - enum bt_ctf_byte_order user_byte_order; -}; - -struct enumeration_mapping { - union { - uint64_t _unsigned; - int64_t _signed; - } range_start; - - union { - uint64_t _unsigned; - int64_t _signed; - } range_end; - GQuark string; -}; - -struct bt_ctf_field_type_enumeration { - struct bt_ctf_field_type parent; - struct bt_ctf_field_type *container; - GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */ - struct declaration_enum declaration; -}; - -struct bt_ctf_field_type_floating_point { - struct bt_ctf_field_type parent; - struct declaration_float declaration; - - /* - * The `declaration` field above contains 3 pointers pointing - * to the fields below. This avoids unnecessary dynamic - * allocations. - */ - struct declaration_integer sign; - struct declaration_integer mantissa; - struct declaration_integer exp; - - /* - * This is what the user sets and is never modified by internal - * code. - * - * This field must contain a `BT_CTF_BYTE_ORDER_*` value. - */ - enum bt_ctf_byte_order user_byte_order; -}; - -struct structure_field { - GQuark name; - struct bt_ctf_field_type *type; -}; - -struct bt_ctf_field_type_structure { - struct bt_ctf_field_type parent; - GHashTable *field_name_to_index; - GPtrArray *fields; /* Array of pointers to struct structure_field */ - struct declaration_struct declaration; -}; - -struct bt_ctf_field_type_variant { - struct bt_ctf_field_type parent; - GString *tag_name; - struct bt_ctf_field_type_enumeration *tag; - struct bt_ctf_field_path *tag_field_path; - GHashTable *field_name_to_index; - GPtrArray *fields; /* Array of pointers to struct structure_field */ - struct declaration_variant declaration; -}; - -struct bt_ctf_field_type_array { - struct bt_ctf_field_type parent; - struct bt_ctf_field_type *element_type; - unsigned int length; /* Number of elements */ - struct declaration_array declaration; -}; - -struct bt_ctf_field_type_sequence { - struct bt_ctf_field_type parent; - struct bt_ctf_field_type *element_type; - GString *length_field_name; - struct bt_ctf_field_path *length_field_path; - struct declaration_sequence declaration; -}; - -struct bt_ctf_field_type_string { - struct bt_ctf_field_type parent; - struct declaration_string declaration; -}; - -BT_HIDDEN -void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type); - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( - struct bt_ctf_field_type_variant *variant, int64_t tag_value); - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( - struct bt_ctf_field_type_variant *variant, uint64_t tag_value); - -BT_HIDDEN -int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, - struct metadata_context *context); - -BT_HIDDEN -int bt_ctf_field_type_validate(struct bt_ctf_field_type *type); - -BT_HIDDEN -const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( - struct bt_ctf_field_type_enumeration *enumeration_type, - uint64_t value); - -BT_HIDDEN -const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( - struct bt_ctf_field_type_enumeration *enumeration_type, - int64_t value); - -/* Override field type's byte order only if it is set to "native" */ -BT_HIDDEN -void bt_ctf_field_type_set_native_byte_order( - struct bt_ctf_field_type *type, int byte_order); - -/* Deep copy a field type */ -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_copy( - struct bt_ctf_field_type *type); - -BT_HIDDEN -int bt_ctf_field_type_structure_get_field_name_index( - struct bt_ctf_field_type *structure, const char *name); - -/* Replace an existing field's type in a structure */ -BT_HIDDEN -int bt_ctf_field_type_structure_set_field_index( - struct bt_ctf_field_type *structure, - struct bt_ctf_field_type *field, int index); - -BT_HIDDEN -int bt_ctf_field_type_variant_get_field_name_index( - struct bt_ctf_field_type *variant, const char *name); - -BT_HIDDEN -int bt_ctf_field_type_sequence_set_length_field_path( - struct bt_ctf_field_type *type, - struct bt_ctf_field_path *path); - -BT_HIDDEN -int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, - struct bt_ctf_field_path *path); - -BT_HIDDEN -int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type, - struct bt_ctf_field_type *tag_type); - -/* Replace an existing field's type in a variant */ -BT_HIDDEN -int bt_ctf_field_type_variant_set_field_index( - struct bt_ctf_field_type *variant, - struct bt_ctf_field_type *field, int index); - -BT_HIDDEN -int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array, - struct bt_ctf_field_type *element_type); - -BT_HIDDEN -int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array, - struct bt_ctf_field_type *element_type); - -BT_HIDDEN -int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type); - -BT_HIDDEN -struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index( - struct bt_ctf_field_type *type, int index); - -BT_HIDDEN -int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type, - const char *name); - -#endif /* BABELTRACE_CTF_IR_EVENT_TYPES_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/event-types.h b/include/babeltrace/ctf-ir/event-types.h deleted file mode 100644 index be6b7ea2..00000000 --- a/include/babeltrace/ctf-ir/event-types.h +++ /dev/null @@ -1,967 +0,0 @@ -#ifndef BABELTRACE_CTF_IR_EVENT_TYPES_H -#define BABELTRACE_CTF_IR_EVENT_TYPES_H - -/* - * BabelTrace - CTF IR: Event Types - * - * Copyright 2013, 2014 Jérémie Galarneau - * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * The Common Trace Format (CTF) Specification is available at - * http://www.efficios.com/ctf - */ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct bt_ctf_event_class; -struct bt_ctf_event; -struct bt_ctf_field_type; -struct bt_ctf_field; -struct bt_ctf_field_path; - -/* - * Babeltrace 1.x enumerations that were also used in CTF writer's API. - * They are left here for backward compatibility reasons, but - * enum bt_ctf_type_id and enum bt_ctf_string_encoding should be used - * in new code. Both new enumerations are compatible with their legacy - * counterpart. - */ -enum ctf_type_id { - CTF_TYPE_UNKNOWN = 0, - CTF_TYPE_INTEGER, - CTF_TYPE_FLOAT, - CTF_TYPE_ENUM, - CTF_TYPE_STRING, - CTF_TYPE_STRUCT, - CTF_TYPE_UNTAGGED_VARIANT, - CTF_TYPE_VARIANT, - CTF_TYPE_ARRAY, - CTF_TYPE_SEQUENCE, - NR_CTF_TYPES, -}; - -/* - * Old enum. - */ -enum ctf_string_encoding { - CTF_STRING_NONE = 0, - CTF_STRING_UTF8, - CTF_STRING_ASCII, - CTF_STRING_UNKNOWN, -}; - -enum bt_ctf_type_id { - BT_CTF_TYPE_ID_UNKNOWN = CTF_TYPE_UNKNOWN, - BT_CTF_TYPE_ID_INTEGER = CTF_TYPE_INTEGER, - BT_CTF_TYPE_ID_FLOAT = CTF_TYPE_FLOAT, - BT_CTF_TYPE_ID_ENUM = CTF_TYPE_ENUM, - BT_CTF_TYPE_ID_STRING = CTF_TYPE_STRING, - BT_CTF_TYPE_ID_STRUCT = CTF_TYPE_STRUCT, - BT_CTF_TYPE_ID_UNTAGGED_VARIANT = CTF_TYPE_UNTAGGED_VARIANT, - BT_CTF_TYPE_ID_VARIANT = CTF_TYPE_VARIANT, - BT_CTF_TYPE_ID_ARRAY = CTF_TYPE_ARRAY, - BT_CTF_TYPE_ID_SEQUENCE = CTF_TYPE_SEQUENCE, - BT_CTF_NR_TYPE_IDS, -}; - -enum bt_ctf_integer_base { - BT_CTF_INTEGER_BASE_UNKNOWN = -1, - BT_CTF_INTEGER_BASE_BINARY = 2, - BT_CTF_INTEGER_BASE_OCTAL = 8, - BT_CTF_INTEGER_BASE_DECIMAL = 10, - BT_CTF_INTEGER_BASE_HEXADECIMAL = 16, -}; - -enum bt_ctf_byte_order { - BT_CTF_BYTE_ORDER_UNKNOWN = -1, - /* - * Note that native, in the context of the CTF specification, is defined - * as "the byte order described in the trace" and does not mean that the - * host's endianness will be used. - */ - BT_CTF_BYTE_ORDER_NATIVE = 0, - BT_CTF_BYTE_ORDER_LITTLE_ENDIAN, - BT_CTF_BYTE_ORDER_BIG_ENDIAN, - BT_CTF_BYTE_ORDER_NETWORK, -}; - -enum bt_ctf_string_encoding { - BT_CTF_STRING_ENCODING_NONE = CTF_STRING_NONE, - BT_CTF_STRING_ENCODING_UTF8 = CTF_STRING_UTF8, - BT_CTF_STRING_ENCODING_ASCII = CTF_STRING_ASCII, - BT_CTF_STRING_ENCODING_UNKNOWN = CTF_STRING_UNKNOWN, -}; - -enum bt_ctf_scope { - BT_CTF_SCOPE_UNKNOWN = -1, - BT_CTF_SCOPE_ENV = 0, - BT_CTF_SCOPE_TRACE_PACKET_HEADER = 1, - BT_CTF_SCOPE_STREAM_PACKET_CONTEXT = 2, - BT_CTF_SCOPE_STREAM_EVENT_HEADER = 3, - BT_CTF_SCOPE_STREAM_EVENT_CONTEXT = 4, - BT_CTF_SCOPE_EVENT_CONTEXT = 5, - BT_CTF_SCOPE_EVENT_FIELDS = 6, -}; - -/* - * bt_ctf_field_type_integer_create: create an integer field type. - * - * Allocate a new integer field type of the given size. The creation of a field - * type sets its reference count to 1. - * - * @param size Integer field type size/length in bits. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_integer_create( - unsigned int size); - -/* - * bt_ctf_field_type_integer_get_size: get an integer type's size. - * - * Get an integer type's size. - * - * @param integer Integer type. - * - * Returns the integer type's size, a negative value on error. - */ -extern int bt_ctf_field_type_integer_get_size( - struct bt_ctf_field_type *integer); - -/* - * bt_ctf_field_type_integer_get_signed: get an integer type's signedness. - * - * Get an integer type's signedness attribute. - * - * @param integer Integer type. - * - * Returns the integer's signedness, a negative value on error. - */ -extern int bt_ctf_field_type_integer_get_signed( - struct bt_ctf_field_type *integer); - -/* - * bt_ctf_field_type_integer_set_signed: set an integer type's signedness. - * - * Set an integer type's signedness attribute. - * - * @param integer Integer type. - * @param is_signed Integer's signedness, defaults to FALSE. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_integer_set_signed( - struct bt_ctf_field_type *integer, int is_signed); - -/* - * bt_ctf_field_type_integer_get_base: get an integer type's base. - * - * Get an integer type's base used to pretty-print the resulting trace. - * - * @param integer Integer type. - * - * Returns the integer type's base on success, BT_CTF_INTEGER_BASE_UNKNOWN on - * error. - */ -extern enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( - struct bt_ctf_field_type *integer); - -/* - * bt_ctf_field_type_integer_set_base: set an integer type's base. - * - * Set an integer type's base used to pretty-print the resulting trace. - * - * @param integer Integer type. - * @param base Integer base, defaults to BT_CTF_INTEGER_BASE_DECIMAL. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, - enum bt_ctf_integer_base base); - -/* - * bt_ctf_field_type_integer_get_encoding: get an integer type's encoding. - * - * @param integer Integer type. - * - * Returns the string field's encoding on success, - * BT_CTF_STRING_ENCODING_UNKNOWN on error. - */ -extern enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( - struct bt_ctf_field_type *integer); - -/* - * bt_ctf_field_type_integer_set_encoding: set an integer type's encoding. - * - * An integer encoding may be set to signal that the integer must be printed as - * a text character. - * - * @param integer Integer type. - * @param encoding Integer output encoding, defaults to - * BT_CTF_STRING_ENCODING_NONE - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_integer_set_encoding( - struct bt_ctf_field_type *integer, - enum bt_ctf_string_encoding encoding); - -/** - * bt_ctf_field_type_integer_get_mapped_clock: get an integer type's mapped clock. - * - * @param integer Integer type. - * - * Returns the integer's mapped clock (if any), NULL on error. - */ -extern struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( - struct bt_ctf_field_type *integer); - -/** - * bt_ctf_field_type_integer_set_mapped_clock: set an integer type's mapped clock. - * - * @param integer Integer type. - * @param clock Clock to map. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_integer_set_mapped_clock( - struct bt_ctf_field_type *integer, - struct bt_ctf_clock *clock); - -/* - * bt_ctf_field_type_enumeration_create: create an enumeration field type. - * - * Allocate a new enumeration field type with the given underlying type. The - * creation of a field type sets its reference count to 1. - * The resulting enumeration will share the integer_container_type's ownership - * by increasing its reference count. - * - * @param integer_container_type Underlying integer type of the enumeration - * type. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( - struct bt_ctf_field_type *integer_container_type); - -/* - * bt_ctf_field_type_enumeration_get_container_type: get underlying container. - * - * Get the enumeration type's underlying integer container type. - * - * @param enumeration Enumeration type. - * - * Returns an allocated field type on success, NULL on error. - */ -extern -struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( - struct bt_ctf_field_type *enumeration); - -/* - * bt_ctf_field_type_enumeration_add_mapping: add an enumeration mapping. - * - * Add a mapping to the enumeration. The range's values are inclusive. - * - * @param enumeration Enumeration type. - * @param name Enumeration mapping name (will be copied). - * @param range_start Enumeration mapping range start. - * @param range_end Enumeration mapping range end. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_add_mapping( - struct bt_ctf_field_type *enumeration, const char *name, - int64_t range_start, int64_t range_end); - -/* - * bt_ctf_field_type_enumeration_add_mapping_unsigned: add an enumeration - * mapping. - * - * Add a mapping to the enumeration. The range's values are inclusive. - * - * @param enumeration Enumeration type. - * @param name Enumeration mapping name (will be copied). - * @param range_start Enumeration mapping range start. - * @param range_end Enumeration mapping range end. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_add_mapping_unsigned( - struct bt_ctf_field_type *enumeration, const char *name, - uint64_t range_start, uint64_t range_end); - -/* - * bt_ctf_field_type_enumeration_get_mapping_count: Get the number of mappings - * defined in the enumeration. - * - * @param enumeration Enumeration type. - * - * Returns the mapping count on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping_count( - struct bt_ctf_field_type *enumeration); - -/* - * bt_ctf_field_type_enumeration_get_mapping: get an enumeration mapping. - * - * @param enumeration Enumeration type. - * @param index Index of mapping. - * @param name Pointer where the mapping's name will be returned (valid for - * the lifetime of the enumeration). - * @param range_start Pointer where the enumeration mapping's range start will - * be returned. - * @param range_end Pointer where the enumeration mapping's range end will - * be returned. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping( - struct bt_ctf_field_type *enumeration, int index, - const char **name, int64_t *range_start, int64_t *range_end); - -/* - * bt_ctf_field_type_enumeration_get_mapping_unsigned: get a mapping. - * - * @param enumeration Enumeration type. - * @param index Index of mapping. - * @param name Pointer where the mapping's name will be returned (valid for - * the lifetime of the enumeration). - * @param range_start Pointer where the enumeration mapping's range start will - * be returned. - * @param range_end Pointer where the enumeration mapping's range end will - * be returned. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping_unsigned( - struct bt_ctf_field_type *enumeration, int index, - const char **name, uint64_t *range_start, - uint64_t *range_end); - -/* - * bt_ctf_field_type_enumeration_get_mapping_index_by_name: get an enumerations' - * mapping index by name. - * - * @param enumeration Enumeration type. - * @param name Mapping name. - * - * Returns mapping index on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping_index_by_name( - struct bt_ctf_field_type *enumeration, const char *name); - -/* - * bt_ctf_field_type_enumeration_get_mapping_index_by_value: get an - * enumerations' mapping index by value. - * - * @param enumeration Enumeration type. - * @param value Value. - * - * Returns mapping index on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping_index_by_value( - struct bt_ctf_field_type *enumeration, int64_t value); - -/* - * bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value: get an - * enumerations' mapping index by value. - * - * @param enumeration Enumeration type. - * @param value Value. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( - struct bt_ctf_field_type *enumeration, uint64_t value); - -/* - * bt_ctf_field_type_floating_point_create: create a floating point field type. - * - * Allocate a new floating point field type. The creation of a field type sets - * its reference count to 1. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void); - -/* - * bt_ctf_field_type_floating_point_get_exponent_digits: get exponent digit - * count. - * - * @param floating_point Floating point type. - * - * Returns the exponent digit count on success, a negative value on error. - */ -extern int bt_ctf_field_type_floating_point_get_exponent_digits( - struct bt_ctf_field_type *floating_point); - -/* - * bt_ctf_field_type_floating_point_set_exponent_digits: set exponent digit - * count. - * - * Set the number of exponent digits to use to store the floating point field. - * The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG. - * - * @param floating_point Floating point type. - * @param exponent_digits Number of digits to allocate to the exponent (defaults - * to FLT_EXP_DIG). - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_floating_point_set_exponent_digits( - struct bt_ctf_field_type *floating_point, - unsigned int exponent_digits); - -/* - * bt_ctf_field_type_floating_point_get_mantissa_digits: get mantissa digit - * count. - * - * @param floating_point Floating point type. - * - * Returns the mantissa digit count on success, a negative value on error. - */ -extern int bt_ctf_field_type_floating_point_get_mantissa_digits( - struct bt_ctf_field_type *floating_point); - -/* - * bt_ctf_field_type_floating_point_set_mantissa_digits: set mantissa digit - * count. - * - * Set the number of mantissa digits to use to store the floating point field. - * The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG. - * - * @param floating_point Floating point type. - * @param mantissa_digits Number of digits to allocate to the mantissa (defaults - * to FLT_MANT_DIG). - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_floating_point_set_mantissa_digits( - struct bt_ctf_field_type *floating_point, - unsigned int mantissa_digits); - -/* - * bt_ctf_field_type_structure_create: create a structure field type. - * - * Allocate a new structure field type. The creation of a field type sets - * its reference count to 1. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void); - -/* - * bt_ctf_field_type_structure_add_field: add a field to a structure. - * - * Add a field of type "field_type" to the structure. The structure will share - * field_type's ownership by increasing its reference count. - * - * @param structure Structure type. - * @param field_type Type of the field to add to the structure type. - * @param field_name Name of the structure's new field (will be copied). - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_structure_add_field( - struct bt_ctf_field_type *structure, - struct bt_ctf_field_type *field_type, - const char *field_name); - -/* - * bt_ctf_field_type_structure_get_field_count: Get the number of fields defined - * in the structure. - * - * @param structure Structure type. - * - * Returns the field count on success, a negative value on error. - */ -extern int bt_ctf_field_type_structure_get_field_count( - struct bt_ctf_field_type *structure); - -/* - * bt_ctf_field_type_structure_get_field: get a structure's field type and name. - * - * @param structure Structure type. - * @param field_type Pointer to a const char* where the field's name will - * be returned. - * @param field_type Pointer to a bt_ctf_field_type* where the field's type will - * be returned. - * @param index Index of field. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_structure_get_field( - struct bt_ctf_field_type *structure, - const char **field_name, struct bt_ctf_field_type **field_type, - int index); - -/* - * bt_ctf_field_type_structure_get_field_type_by_name: get a structure field's - * type by name. - * - * @param structure Structure type. - * @param field_name Name of the structure's field. - * - * Returns a field type instance on success, NULL on error. - */ -extern -struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( - struct bt_ctf_field_type *structure, const char *field_name); - -/* - * bt_ctf_field_type_variant_create: create a variant field type. - * - * Allocate a new variant field type. The creation of a field type sets - * its reference count to 1. tag_name must be the name of an enumeration - * field declared in the same scope as this variant. - * - * @param enum_tag Type of the variant's tag/selector (must be an enumeration). - * @param tag_name Name of the variant's tag/selector field (will be copied). - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_variant_create( - struct bt_ctf_field_type *enum_tag, const char *tag_name); - -/* - * bt_ctf_field_type_variant_get_tag_type: get a variant's tag type. - * - * @param variant Variant type. - * - * Returns a field type instance on success, NULL if unset. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( - struct bt_ctf_field_type *variant); - -/* - * bt_ctf_field_type_variant_get_tag_name: get a variant's tag name. - * - * @param variant Variant type. - * - * Returns the tag field's name, NULL if unset. - */ -extern const char *bt_ctf_field_type_variant_get_tag_name( - struct bt_ctf_field_type *variant); - -/* - * bt_ctf_field_type_variant_set_tag_name: set a variant's tag name. - * - * @param variant Variant type. - * @param name Tag field name. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_variant_set_tag_name( - struct bt_ctf_field_type *variant, const char *name); - -/* - * bt_ctf_field_type_variant_add_field: add a field to a variant. - * - * Add a field of type "field_type" to the variant. The variant will share - * field_type's ownership by increasing its reference count. The "field_name" - * will be copied. field_name must match a mapping in the tag/selector - * enumeration. - * - * @param variant Variant type. - * @param field_type Type of the variant type's new field. - * @param field_name Name of the variant type's new field (will be copied). - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_variant_add_field( - struct bt_ctf_field_type *variant, - struct bt_ctf_field_type *field_type, - const char *field_name); - -/* - * bt_ctf_field_type_variant_get_field_type_by_name: get variant field's type. - * - * @param structure Variant type. - * @param field_name Name of the variant's field. - * - * Returns a field type instance on success, NULL on error. - */ -extern -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( - struct bt_ctf_field_type *variant, const char *field_name); - -/* - * bt_ctf_field_type_variant_get_field_type_from_tag: get variant field's type. - * - * @param variant Variant type. - * @param tag Type tag (enum). - * - * Returns a field type instance on success, NULL on error. - */ -extern -struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( - struct bt_ctf_field_type *variant, struct bt_ctf_field *tag); - -/* - * bt_ctf_field_type_variant_get_field_count: Get the number of fields defined - * in the variant. - * - * @param variant Variant type. - * - * Returns the field count on success, a negative value on error. - */ -extern int bt_ctf_field_type_variant_get_field_count( - struct bt_ctf_field_type *variant); - -/* - * bt_ctf_field_type_variant_get_field: get a variant's field name and type. - * - * @param variant Variant type. - * @param field_type Pointer to a const char* where the field's name will - * be returned. - * @param field_type Pointer to a bt_ctf_field_type* where the field's type will - * be returned. - * @param index Index of field. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_variant_get_field( - struct bt_ctf_field_type *variant, const char **field_name, - struct bt_ctf_field_type **field_type, int index); - -/* - * bt_ctf_field_type_array_create: create an array field type. - * - * Allocate a new array field type. The creation of a field type sets - * its reference count to 1. - * - * @param element_type Array's element type. - * @oaram length Array type's length. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_array_create( - struct bt_ctf_field_type *element_type, unsigned int length); - -/* - * bt_ctf_field_type_array_get_element_type: get an array's element type. - * - * @param array Array type. - * - * Returns a field type instance on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( - struct bt_ctf_field_type *array); - -/* - * bt_ctf_field_type_array_get_length: get an array's length. - * - * @param array Array type. - * - * Returns the array's length on success, a negative value on error. - */ -extern int64_t bt_ctf_field_type_array_get_length( - struct bt_ctf_field_type *array); - -/* - * bt_ctf_field_type_sequence_create: create a sequence field type. - * - * Allocate a new sequence field type. The creation of a field type sets - * its reference count to 1. "length_field_name" must match an integer field - * declared in the same scope. - * - * @param element_type Sequence's element type. - * @param length_field_name Name of the sequence's length field (will be - * copied). - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( - struct bt_ctf_field_type *element_type, - const char *length_field_name); - -/* - * bt_ctf_field_type_sequence_get_element_type: get a sequence's element type. - * - * @param sequence Sequence type. - * - * Returns a field type instance on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( - struct bt_ctf_field_type *sequence); - -/* - * bt_ctf_field_type_sequence_get_length_field_name: get length field name. - * - * @param sequence Sequence type. - * - * Returns the sequence's length field on success, NULL on error. - */ -extern const char *bt_ctf_field_type_sequence_get_length_field_name( - struct bt_ctf_field_type *sequence); - -/* - * bt_ctf_field_type_string_create: create a string field type. - * - * Allocate a new string field type. The creation of a field type sets - * its reference count to 1. - * - * Returns an allocated field type on success, NULL on error. - */ -extern struct bt_ctf_field_type *bt_ctf_field_type_string_create(void); - -/* - * bt_ctf_field_type_string_get_encoding: get a string type's encoding. - * - * Get the string type's encoding. - * - * @param string_type String type. - * - * Returns the string's encoding on success, a BT_CTF_STRING_ENCODING_UNKNOWN - * on error. - */ -extern enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( - struct bt_ctf_field_type *string_type); - -/* - * bt_ctf_field_type_string_set_encoding: set a string type's encoding. - * - * Set the string type's encoding. - * - * @param string_type String type. - * @param encoding String field encoding, default BT_CTF_STRING_ENCODING_ASCII. - * Valid values are BT_CTF_STRING_ENCODING_ASCII and - * BT_CTF_STRING_ENCODING_UTF8. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_string_set_encoding( - struct bt_ctf_field_type *string_type, - enum bt_ctf_string_encoding encoding); - -/* - * bt_ctf_field_type_get_alignment: get a field type's alignment. - * - * Get the field type's alignment. - * - * @param type Field type. - * - * Returns the field type's alignment on success, a negative value on error and - * 0 if the alignment is undefined (as in the case of a variant). - */ -extern int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_set_alignment: set a field type's alignment. - * - * Set the field type's alignment. - * - * @param type Field type. - * @param alignment Type's alignment. Defaults to 1 (bit-aligned). However, - * some types, such as structures and string, may impose other alignment - * constraints. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, - unsigned int alignment); - -/* - * bt_ctf_field_type_get_byte_order: get a field type's byte order. - * - * @param type Field type. - * - * Returns the field type's byte order on success, a negative value on error. - */ -extern enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( - struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_set_byte_order: set a field type's byte order. - * - * Set the field type's byte order. - * - * @param type Field type. - * @param byte_order Field type's byte order. Defaults to - * BT_CTF_BYTE_ORDER_NATIVE; the trace's endianness. - * - * Returns 0 on success, a negative value on error. - */ -extern int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, - enum bt_ctf_byte_order byte_order); - -/* - * bt_ctf_field_type_variant_get_tag_field_path: get a variant's tag's field - * path. - * - * Get the variant's tag's field path. - * - * @param type Field type. - * - * Returns the field path on success, NULL on error or if no field path is set. - */ -extern struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( - struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_sequence_get_length_field_path: get a sequence's length's - * field path. - * - * Get the sequence's length's field path. - * - * @param type Field type. - * - * Returns the field path on success, NULL on error or if no field path is set. - */ -extern struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( - struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_compare: compare two field types recursively - * - * Compare two field types recursively. - * - * The registered tag field type of a variant field type is ignored: - * only the tag strings are compared. - * - * @param type_a Field type A. - * @param type_b Field type B. - * - * Returns 0 if both field types are semantically equivalent, a positive - * value if they are not equivalent, or a negative value on error. - */ -extern int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, - struct bt_ctf_field_type *type_b); - -/* - * bt_ctf_field_type_get_type_id: get a field type's bt_ctf_type_id. - * - * @param type Field type. - * - * Returns the field type's bt_ctf_type_id, CTF_TYPE_UNKNOWN on error. - */ -extern enum bt_ctf_type_id bt_ctf_field_type_get_type_id( - struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_integer: returns whether or not a given field - * type is an integer type. - * - * @param type Field type. - * - * Returns 1 if the field type is an integer type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_floating_point: returns whether or not a given field - * type is a floating point number type. - * - * @param type Field type. - * - * Returns 1 if the field type is a floating point number type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_enumeration: returns whether or not a given field - * type is an enumeration type. - * - * @param type Field type. - * - * Returns 1 if the field type is an enumeration type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_string: returns whether or not a given field - * type is a string type. - * - * @param type Field type. - * - * Returns 1 if the field type is a string type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_structure: returns whether or not a given field - * type is a structure type. - * - * @param type Field type. - * - * Returns 1 if the field type is a structure type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_array: returns whether or not a given field - * type is an array type. - * - * @param type Field type. - * - * Returns 1 if the field type is an array type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_sequence: returns whether or not a given field - * type is a sequence type. - * - * @param type Field type. - * - * Returns 1 if the field type is a sequence type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_is_variant: returns whether or not a given field - * type is a variant type. - * - * @param type Field type. - * - * Returns 1 if the field type is a variant type, 0 otherwise. - */ -extern int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type); - -/* - * bt_ctf_field_type_get and bt_ctf_field_type_put: increment and decrement - * the field type's reference count. - * - * You may also use bt_ctf_get() and bt_ctf_put() with field type objects. - * - * These functions ensure that the field type won't be destroyed while it - * is in use. The same number of get and put (plus one extra put to - * release the initial reference done at creation) have to be done to - * destroy a field type. - * - * When the field type's reference count is decremented to 0 by a - * bt_ctf_field_type_put, the field type is freed. - * - * @param type Field type. - */ -extern void bt_ctf_field_type_get(struct bt_ctf_field_type *type); -extern void bt_ctf_field_type_put(struct bt_ctf_field_type *type); - -#ifdef __cplusplus -} -#endif - -#endif /* BABELTRACE_CTF_IR_EVENT_TYPES_H */ diff --git a/include/babeltrace/ctf-ir/field-path.h b/include/babeltrace/ctf-ir/field-path.h index ffcaae00..40acfc8b 100644 --- a/include/babeltrace/ctf-ir/field-path.h +++ b/include/babeltrace/ctf-ir/field-path.h @@ -28,7 +28,7 @@ * http://www.efficios.com/ctf */ -#include +#include #ifdef __cplusplus extern "C" { diff --git a/include/babeltrace/ctf-ir/field-types-internal.h b/include/babeltrace/ctf-ir/field-types-internal.h new file mode 100644 index 00000000..d3a4b0c1 --- /dev/null +++ b/include/babeltrace/ctf-ir/field-types-internal.h @@ -0,0 +1,251 @@ +#ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H +#define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H + +/* + * BabelTrace - CTF IR: Event field types internal + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef void (*type_freeze_func)(struct bt_ctf_field_type *); +typedef int (*type_serialize_func)(struct bt_ctf_field_type *, + struct metadata_context *); + +struct bt_ctf_field_type { + struct bt_object base; + struct bt_declaration *declaration; + type_freeze_func freeze; + type_serialize_func serialize; + /* + * A type can't be modified once it is added to an event or after a + * a field has been instanciated from it. + */ + int frozen; + + /* + * This flag indicates if the field type is valid. A valid + * field type is _always_ frozen. All the nested field types of + * a valid field type are also valid (and thus frozen). + */ + int valid; +}; + +struct bt_ctf_field_type_integer { + struct bt_ctf_field_type parent; + struct declaration_integer declaration; + struct bt_ctf_clock *mapped_clock; + + /* + * This is what the user sets and is never modified by internal + * code. + * + * This field must contain a `BT_CTF_BYTE_ORDER_*` value. + */ + enum bt_ctf_byte_order user_byte_order; +}; + +struct enumeration_mapping { + union { + uint64_t _unsigned; + int64_t _signed; + } range_start; + + union { + uint64_t _unsigned; + int64_t _signed; + } range_end; + GQuark string; +}; + +struct bt_ctf_field_type_enumeration { + struct bt_ctf_field_type parent; + struct bt_ctf_field_type *container; + GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */ + struct declaration_enum declaration; +}; + +struct bt_ctf_field_type_floating_point { + struct bt_ctf_field_type parent; + struct declaration_float declaration; + + /* + * The `declaration` field above contains 3 pointers pointing + * to the fields below. This avoids unnecessary dynamic + * allocations. + */ + struct declaration_integer sign; + struct declaration_integer mantissa; + struct declaration_integer exp; + + /* + * This is what the user sets and is never modified by internal + * code. + * + * This field must contain a `BT_CTF_BYTE_ORDER_*` value. + */ + enum bt_ctf_byte_order user_byte_order; +}; + +struct structure_field { + GQuark name; + struct bt_ctf_field_type *type; +}; + +struct bt_ctf_field_type_structure { + struct bt_ctf_field_type parent; + GHashTable *field_name_to_index; + GPtrArray *fields; /* Array of pointers to struct structure_field */ + struct declaration_struct declaration; +}; + +struct bt_ctf_field_type_variant { + struct bt_ctf_field_type parent; + GString *tag_name; + struct bt_ctf_field_type_enumeration *tag; + struct bt_ctf_field_path *tag_field_path; + GHashTable *field_name_to_index; + GPtrArray *fields; /* Array of pointers to struct structure_field */ + struct declaration_variant declaration; +}; + +struct bt_ctf_field_type_array { + struct bt_ctf_field_type parent; + struct bt_ctf_field_type *element_type; + unsigned int length; /* Number of elements */ + struct declaration_array declaration; +}; + +struct bt_ctf_field_type_sequence { + struct bt_ctf_field_type parent; + struct bt_ctf_field_type *element_type; + GString *length_field_name; + struct bt_ctf_field_path *length_field_path; + struct declaration_sequence declaration; +}; + +struct bt_ctf_field_type_string { + struct bt_ctf_field_type parent; + struct declaration_string declaration; +}; + +BT_HIDDEN +void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type); + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( + struct bt_ctf_field_type_variant *variant, int64_t tag_value); + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( + struct bt_ctf_field_type_variant *variant, uint64_t tag_value); + +BT_HIDDEN +int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, + struct metadata_context *context); + +BT_HIDDEN +int bt_ctf_field_type_validate(struct bt_ctf_field_type *type); + +BT_HIDDEN +const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( + struct bt_ctf_field_type_enumeration *enumeration_type, + uint64_t value); + +BT_HIDDEN +const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( + struct bt_ctf_field_type_enumeration *enumeration_type, + int64_t value); + +/* Override field type's byte order only if it is set to "native" */ +BT_HIDDEN +void bt_ctf_field_type_set_native_byte_order( + struct bt_ctf_field_type *type, int byte_order); + +/* Deep copy a field type */ +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_copy( + struct bt_ctf_field_type *type); + +BT_HIDDEN +int bt_ctf_field_type_structure_get_field_name_index( + struct bt_ctf_field_type *structure, const char *name); + +/* Replace an existing field's type in a structure */ +BT_HIDDEN +int bt_ctf_field_type_structure_set_field_index( + struct bt_ctf_field_type *structure, + struct bt_ctf_field_type *field, int index); + +BT_HIDDEN +int bt_ctf_field_type_variant_get_field_name_index( + struct bt_ctf_field_type *variant, const char *name); + +BT_HIDDEN +int bt_ctf_field_type_sequence_set_length_field_path( + struct bt_ctf_field_type *type, + struct bt_ctf_field_path *path); + +BT_HIDDEN +int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, + struct bt_ctf_field_path *path); + +BT_HIDDEN +int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type, + struct bt_ctf_field_type *tag_type); + +/* Replace an existing field's type in a variant */ +BT_HIDDEN +int bt_ctf_field_type_variant_set_field_index( + struct bt_ctf_field_type *variant, + struct bt_ctf_field_type *field, int index); + +BT_HIDDEN +int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array, + struct bt_ctf_field_type *element_type); + +BT_HIDDEN +int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array, + struct bt_ctf_field_type *element_type); + +BT_HIDDEN +int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type); + +BT_HIDDEN +struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index( + struct bt_ctf_field_type *type, int index); + +BT_HIDDEN +int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type, + const char *name); + +#endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/field-types.h b/include/babeltrace/ctf-ir/field-types.h new file mode 100644 index 00000000..ce9ca72f --- /dev/null +++ b/include/babeltrace/ctf-ir/field-types.h @@ -0,0 +1,967 @@ +#ifndef BABELTRACE_CTF_IR_FIELD_TYPES_H +#define BABELTRACE_CTF_IR_FIELD_TYPES_H + +/* + * BabelTrace - CTF IR: Event field types + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * The Common Trace Format (CTF) Specification is available at + * http://www.efficios.com/ctf + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_ctf_event_class; +struct bt_ctf_event; +struct bt_ctf_field_type; +struct bt_ctf_field; +struct bt_ctf_field_path; + +/* + * Babeltrace 1.x enumerations that were also used in CTF writer's API. + * They are left here for backward compatibility reasons, but + * enum bt_ctf_type_id and enum bt_ctf_string_encoding should be used + * in new code. Both new enumerations are compatible with their legacy + * counterpart. + */ +enum ctf_type_id { + CTF_TYPE_UNKNOWN = 0, + CTF_TYPE_INTEGER, + CTF_TYPE_FLOAT, + CTF_TYPE_ENUM, + CTF_TYPE_STRING, + CTF_TYPE_STRUCT, + CTF_TYPE_UNTAGGED_VARIANT, + CTF_TYPE_VARIANT, + CTF_TYPE_ARRAY, + CTF_TYPE_SEQUENCE, + NR_CTF_TYPES, +}; + +/* + * Old enum. + */ +enum ctf_string_encoding { + CTF_STRING_NONE = 0, + CTF_STRING_UTF8, + CTF_STRING_ASCII, + CTF_STRING_UNKNOWN, +}; + +enum bt_ctf_type_id { + BT_CTF_TYPE_ID_UNKNOWN = CTF_TYPE_UNKNOWN, + BT_CTF_TYPE_ID_INTEGER = CTF_TYPE_INTEGER, + BT_CTF_TYPE_ID_FLOAT = CTF_TYPE_FLOAT, + BT_CTF_TYPE_ID_ENUM = CTF_TYPE_ENUM, + BT_CTF_TYPE_ID_STRING = CTF_TYPE_STRING, + BT_CTF_TYPE_ID_STRUCT = CTF_TYPE_STRUCT, + BT_CTF_TYPE_ID_UNTAGGED_VARIANT = CTF_TYPE_UNTAGGED_VARIANT, + BT_CTF_TYPE_ID_VARIANT = CTF_TYPE_VARIANT, + BT_CTF_TYPE_ID_ARRAY = CTF_TYPE_ARRAY, + BT_CTF_TYPE_ID_SEQUENCE = CTF_TYPE_SEQUENCE, + BT_CTF_NR_TYPE_IDS, +}; + +enum bt_ctf_integer_base { + BT_CTF_INTEGER_BASE_UNKNOWN = -1, + BT_CTF_INTEGER_BASE_BINARY = 2, + BT_CTF_INTEGER_BASE_OCTAL = 8, + BT_CTF_INTEGER_BASE_DECIMAL = 10, + BT_CTF_INTEGER_BASE_HEXADECIMAL = 16, +}; + +enum bt_ctf_byte_order { + BT_CTF_BYTE_ORDER_UNKNOWN = -1, + /* + * Note that native, in the context of the CTF specification, is defined + * as "the byte order described in the trace" and does not mean that the + * host's endianness will be used. + */ + BT_CTF_BYTE_ORDER_NATIVE = 0, + BT_CTF_BYTE_ORDER_LITTLE_ENDIAN, + BT_CTF_BYTE_ORDER_BIG_ENDIAN, + BT_CTF_BYTE_ORDER_NETWORK, +}; + +enum bt_ctf_string_encoding { + BT_CTF_STRING_ENCODING_NONE = CTF_STRING_NONE, + BT_CTF_STRING_ENCODING_UTF8 = CTF_STRING_UTF8, + BT_CTF_STRING_ENCODING_ASCII = CTF_STRING_ASCII, + BT_CTF_STRING_ENCODING_UNKNOWN = CTF_STRING_UNKNOWN, +}; + +enum bt_ctf_scope { + BT_CTF_SCOPE_UNKNOWN = -1, + BT_CTF_SCOPE_ENV = 0, + BT_CTF_SCOPE_TRACE_PACKET_HEADER = 1, + BT_CTF_SCOPE_STREAM_PACKET_CONTEXT = 2, + BT_CTF_SCOPE_STREAM_EVENT_HEADER = 3, + BT_CTF_SCOPE_STREAM_EVENT_CONTEXT = 4, + BT_CTF_SCOPE_EVENT_CONTEXT = 5, + BT_CTF_SCOPE_EVENT_FIELDS = 6, +}; + +/* + * bt_ctf_field_type_integer_create: create an integer field type. + * + * Allocate a new integer field type of the given size. The creation of a field + * type sets its reference count to 1. + * + * @param size Integer field type size/length in bits. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_integer_create( + unsigned int size); + +/* + * bt_ctf_field_type_integer_get_size: get an integer type's size. + * + * Get an integer type's size. + * + * @param integer Integer type. + * + * Returns the integer type's size, a negative value on error. + */ +extern int bt_ctf_field_type_integer_get_size( + struct bt_ctf_field_type *integer); + +/* + * bt_ctf_field_type_integer_get_signed: get an integer type's signedness. + * + * Get an integer type's signedness attribute. + * + * @param integer Integer type. + * + * Returns the integer's signedness, a negative value on error. + */ +extern int bt_ctf_field_type_integer_get_signed( + struct bt_ctf_field_type *integer); + +/* + * bt_ctf_field_type_integer_set_signed: set an integer type's signedness. + * + * Set an integer type's signedness attribute. + * + * @param integer Integer type. + * @param is_signed Integer's signedness, defaults to FALSE. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_integer_set_signed( + struct bt_ctf_field_type *integer, int is_signed); + +/* + * bt_ctf_field_type_integer_get_base: get an integer type's base. + * + * Get an integer type's base used to pretty-print the resulting trace. + * + * @param integer Integer type. + * + * Returns the integer type's base on success, BT_CTF_INTEGER_BASE_UNKNOWN on + * error. + */ +extern enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( + struct bt_ctf_field_type *integer); + +/* + * bt_ctf_field_type_integer_set_base: set an integer type's base. + * + * Set an integer type's base used to pretty-print the resulting trace. + * + * @param integer Integer type. + * @param base Integer base, defaults to BT_CTF_INTEGER_BASE_DECIMAL. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, + enum bt_ctf_integer_base base); + +/* + * bt_ctf_field_type_integer_get_encoding: get an integer type's encoding. + * + * @param integer Integer type. + * + * Returns the string field's encoding on success, + * BT_CTF_STRING_ENCODING_UNKNOWN on error. + */ +extern enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( + struct bt_ctf_field_type *integer); + +/* + * bt_ctf_field_type_integer_set_encoding: set an integer type's encoding. + * + * An integer encoding may be set to signal that the integer must be printed as + * a text character. + * + * @param integer Integer type. + * @param encoding Integer output encoding, defaults to + * BT_CTF_STRING_ENCODING_NONE + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_integer_set_encoding( + struct bt_ctf_field_type *integer, + enum bt_ctf_string_encoding encoding); + +/** + * bt_ctf_field_type_integer_get_mapped_clock: get an integer type's mapped clock. + * + * @param integer Integer type. + * + * Returns the integer's mapped clock (if any), NULL on error. + */ +extern struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( + struct bt_ctf_field_type *integer); + +/** + * bt_ctf_field_type_integer_set_mapped_clock: set an integer type's mapped clock. + * + * @param integer Integer type. + * @param clock Clock to map. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_integer_set_mapped_clock( + struct bt_ctf_field_type *integer, + struct bt_ctf_clock *clock); + +/* + * bt_ctf_field_type_enumeration_create: create an enumeration field type. + * + * Allocate a new enumeration field type with the given underlying type. The + * creation of a field type sets its reference count to 1. + * The resulting enumeration will share the integer_container_type's ownership + * by increasing its reference count. + * + * @param integer_container_type Underlying integer type of the enumeration + * type. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( + struct bt_ctf_field_type *integer_container_type); + +/* + * bt_ctf_field_type_enumeration_get_container_type: get underlying container. + * + * Get the enumeration type's underlying integer container type. + * + * @param enumeration Enumeration type. + * + * Returns an allocated field type on success, NULL on error. + */ +extern +struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( + struct bt_ctf_field_type *enumeration); + +/* + * bt_ctf_field_type_enumeration_add_mapping: add an enumeration mapping. + * + * Add a mapping to the enumeration. The range's values are inclusive. + * + * @param enumeration Enumeration type. + * @param name Enumeration mapping name (will be copied). + * @param range_start Enumeration mapping range start. + * @param range_end Enumeration mapping range end. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_add_mapping( + struct bt_ctf_field_type *enumeration, const char *name, + int64_t range_start, int64_t range_end); + +/* + * bt_ctf_field_type_enumeration_add_mapping_unsigned: add an enumeration + * mapping. + * + * Add a mapping to the enumeration. The range's values are inclusive. + * + * @param enumeration Enumeration type. + * @param name Enumeration mapping name (will be copied). + * @param range_start Enumeration mapping range start. + * @param range_end Enumeration mapping range end. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_add_mapping_unsigned( + struct bt_ctf_field_type *enumeration, const char *name, + uint64_t range_start, uint64_t range_end); + +/* + * bt_ctf_field_type_enumeration_get_mapping_count: Get the number of mappings + * defined in the enumeration. + * + * @param enumeration Enumeration type. + * + * Returns the mapping count on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping_count( + struct bt_ctf_field_type *enumeration); + +/* + * bt_ctf_field_type_enumeration_get_mapping: get an enumeration mapping. + * + * @param enumeration Enumeration type. + * @param index Index of mapping. + * @param name Pointer where the mapping's name will be returned (valid for + * the lifetime of the enumeration). + * @param range_start Pointer where the enumeration mapping's range start will + * be returned. + * @param range_end Pointer where the enumeration mapping's range end will + * be returned. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping( + struct bt_ctf_field_type *enumeration, int index, + const char **name, int64_t *range_start, int64_t *range_end); + +/* + * bt_ctf_field_type_enumeration_get_mapping_unsigned: get a mapping. + * + * @param enumeration Enumeration type. + * @param index Index of mapping. + * @param name Pointer where the mapping's name will be returned (valid for + * the lifetime of the enumeration). + * @param range_start Pointer where the enumeration mapping's range start will + * be returned. + * @param range_end Pointer where the enumeration mapping's range end will + * be returned. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping_unsigned( + struct bt_ctf_field_type *enumeration, int index, + const char **name, uint64_t *range_start, + uint64_t *range_end); + +/* + * bt_ctf_field_type_enumeration_get_mapping_index_by_name: get an enumerations' + * mapping index by name. + * + * @param enumeration Enumeration type. + * @param name Mapping name. + * + * Returns mapping index on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping_index_by_name( + struct bt_ctf_field_type *enumeration, const char *name); + +/* + * bt_ctf_field_type_enumeration_get_mapping_index_by_value: get an + * enumerations' mapping index by value. + * + * @param enumeration Enumeration type. + * @param value Value. + * + * Returns mapping index on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping_index_by_value( + struct bt_ctf_field_type *enumeration, int64_t value); + +/* + * bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value: get an + * enumerations' mapping index by value. + * + * @param enumeration Enumeration type. + * @param value Value. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( + struct bt_ctf_field_type *enumeration, uint64_t value); + +/* + * bt_ctf_field_type_floating_point_create: create a floating point field type. + * + * Allocate a new floating point field type. The creation of a field type sets + * its reference count to 1. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void); + +/* + * bt_ctf_field_type_floating_point_get_exponent_digits: get exponent digit + * count. + * + * @param floating_point Floating point type. + * + * Returns the exponent digit count on success, a negative value on error. + */ +extern int bt_ctf_field_type_floating_point_get_exponent_digits( + struct bt_ctf_field_type *floating_point); + +/* + * bt_ctf_field_type_floating_point_set_exponent_digits: set exponent digit + * count. + * + * Set the number of exponent digits to use to store the floating point field. + * The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG. + * + * @param floating_point Floating point type. + * @param exponent_digits Number of digits to allocate to the exponent (defaults + * to FLT_EXP_DIG). + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_floating_point_set_exponent_digits( + struct bt_ctf_field_type *floating_point, + unsigned int exponent_digits); + +/* + * bt_ctf_field_type_floating_point_get_mantissa_digits: get mantissa digit + * count. + * + * @param floating_point Floating point type. + * + * Returns the mantissa digit count on success, a negative value on error. + */ +extern int bt_ctf_field_type_floating_point_get_mantissa_digits( + struct bt_ctf_field_type *floating_point); + +/* + * bt_ctf_field_type_floating_point_set_mantissa_digits: set mantissa digit + * count. + * + * Set the number of mantissa digits to use to store the floating point field. + * The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG. + * + * @param floating_point Floating point type. + * @param mantissa_digits Number of digits to allocate to the mantissa (defaults + * to FLT_MANT_DIG). + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_floating_point_set_mantissa_digits( + struct bt_ctf_field_type *floating_point, + unsigned int mantissa_digits); + +/* + * bt_ctf_field_type_structure_create: create a structure field type. + * + * Allocate a new structure field type. The creation of a field type sets + * its reference count to 1. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void); + +/* + * bt_ctf_field_type_structure_add_field: add a field to a structure. + * + * Add a field of type "field_type" to the structure. The structure will share + * field_type's ownership by increasing its reference count. + * + * @param structure Structure type. + * @param field_type Type of the field to add to the structure type. + * @param field_name Name of the structure's new field (will be copied). + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_structure_add_field( + struct bt_ctf_field_type *structure, + struct bt_ctf_field_type *field_type, + const char *field_name); + +/* + * bt_ctf_field_type_structure_get_field_count: Get the number of fields defined + * in the structure. + * + * @param structure Structure type. + * + * Returns the field count on success, a negative value on error. + */ +extern int bt_ctf_field_type_structure_get_field_count( + struct bt_ctf_field_type *structure); + +/* + * bt_ctf_field_type_structure_get_field: get a structure's field type and name. + * + * @param structure Structure type. + * @param field_type Pointer to a const char* where the field's name will + * be returned. + * @param field_type Pointer to a bt_ctf_field_type* where the field's type will + * be returned. + * @param index Index of field. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_structure_get_field( + struct bt_ctf_field_type *structure, + const char **field_name, struct bt_ctf_field_type **field_type, + int index); + +/* + * bt_ctf_field_type_structure_get_field_type_by_name: get a structure field's + * type by name. + * + * @param structure Structure type. + * @param field_name Name of the structure's field. + * + * Returns a field type instance on success, NULL on error. + */ +extern +struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( + struct bt_ctf_field_type *structure, const char *field_name); + +/* + * bt_ctf_field_type_variant_create: create a variant field type. + * + * Allocate a new variant field type. The creation of a field type sets + * its reference count to 1. tag_name must be the name of an enumeration + * field declared in the same scope as this variant. + * + * @param enum_tag Type of the variant's tag/selector (must be an enumeration). + * @param tag_name Name of the variant's tag/selector field (will be copied). + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_variant_create( + struct bt_ctf_field_type *enum_tag, const char *tag_name); + +/* + * bt_ctf_field_type_variant_get_tag_type: get a variant's tag type. + * + * @param variant Variant type. + * + * Returns a field type instance on success, NULL if unset. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( + struct bt_ctf_field_type *variant); + +/* + * bt_ctf_field_type_variant_get_tag_name: get a variant's tag name. + * + * @param variant Variant type. + * + * Returns the tag field's name, NULL if unset. + */ +extern const char *bt_ctf_field_type_variant_get_tag_name( + struct bt_ctf_field_type *variant); + +/* + * bt_ctf_field_type_variant_set_tag_name: set a variant's tag name. + * + * @param variant Variant type. + * @param name Tag field name. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_variant_set_tag_name( + struct bt_ctf_field_type *variant, const char *name); + +/* + * bt_ctf_field_type_variant_add_field: add a field to a variant. + * + * Add a field of type "field_type" to the variant. The variant will share + * field_type's ownership by increasing its reference count. The "field_name" + * will be copied. field_name must match a mapping in the tag/selector + * enumeration. + * + * @param variant Variant type. + * @param field_type Type of the variant type's new field. + * @param field_name Name of the variant type's new field (will be copied). + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_variant_add_field( + struct bt_ctf_field_type *variant, + struct bt_ctf_field_type *field_type, + const char *field_name); + +/* + * bt_ctf_field_type_variant_get_field_type_by_name: get variant field's type. + * + * @param structure Variant type. + * @param field_name Name of the variant's field. + * + * Returns a field type instance on success, NULL on error. + */ +extern +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( + struct bt_ctf_field_type *variant, const char *field_name); + +/* + * bt_ctf_field_type_variant_get_field_type_from_tag: get variant field's type. + * + * @param variant Variant type. + * @param tag Type tag (enum). + * + * Returns a field type instance on success, NULL on error. + */ +extern +struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( + struct bt_ctf_field_type *variant, struct bt_ctf_field *tag); + +/* + * bt_ctf_field_type_variant_get_field_count: Get the number of fields defined + * in the variant. + * + * @param variant Variant type. + * + * Returns the field count on success, a negative value on error. + */ +extern int bt_ctf_field_type_variant_get_field_count( + struct bt_ctf_field_type *variant); + +/* + * bt_ctf_field_type_variant_get_field: get a variant's field name and type. + * + * @param variant Variant type. + * @param field_type Pointer to a const char* where the field's name will + * be returned. + * @param field_type Pointer to a bt_ctf_field_type* where the field's type will + * be returned. + * @param index Index of field. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_variant_get_field( + struct bt_ctf_field_type *variant, const char **field_name, + struct bt_ctf_field_type **field_type, int index); + +/* + * bt_ctf_field_type_array_create: create an array field type. + * + * Allocate a new array field type. The creation of a field type sets + * its reference count to 1. + * + * @param element_type Array's element type. + * @oaram length Array type's length. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_array_create( + struct bt_ctf_field_type *element_type, unsigned int length); + +/* + * bt_ctf_field_type_array_get_element_type: get an array's element type. + * + * @param array Array type. + * + * Returns a field type instance on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( + struct bt_ctf_field_type *array); + +/* + * bt_ctf_field_type_array_get_length: get an array's length. + * + * @param array Array type. + * + * Returns the array's length on success, a negative value on error. + */ +extern int64_t bt_ctf_field_type_array_get_length( + struct bt_ctf_field_type *array); + +/* + * bt_ctf_field_type_sequence_create: create a sequence field type. + * + * Allocate a new sequence field type. The creation of a field type sets + * its reference count to 1. "length_field_name" must match an integer field + * declared in the same scope. + * + * @param element_type Sequence's element type. + * @param length_field_name Name of the sequence's length field (will be + * copied). + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( + struct bt_ctf_field_type *element_type, + const char *length_field_name); + +/* + * bt_ctf_field_type_sequence_get_element_type: get a sequence's element type. + * + * @param sequence Sequence type. + * + * Returns a field type instance on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( + struct bt_ctf_field_type *sequence); + +/* + * bt_ctf_field_type_sequence_get_length_field_name: get length field name. + * + * @param sequence Sequence type. + * + * Returns the sequence's length field on success, NULL on error. + */ +extern const char *bt_ctf_field_type_sequence_get_length_field_name( + struct bt_ctf_field_type *sequence); + +/* + * bt_ctf_field_type_string_create: create a string field type. + * + * Allocate a new string field type. The creation of a field type sets + * its reference count to 1. + * + * Returns an allocated field type on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_type_string_create(void); + +/* + * bt_ctf_field_type_string_get_encoding: get a string type's encoding. + * + * Get the string type's encoding. + * + * @param string_type String type. + * + * Returns the string's encoding on success, a BT_CTF_STRING_ENCODING_UNKNOWN + * on error. + */ +extern enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( + struct bt_ctf_field_type *string_type); + +/* + * bt_ctf_field_type_string_set_encoding: set a string type's encoding. + * + * Set the string type's encoding. + * + * @param string_type String type. + * @param encoding String field encoding, default BT_CTF_STRING_ENCODING_ASCII. + * Valid values are BT_CTF_STRING_ENCODING_ASCII and + * BT_CTF_STRING_ENCODING_UTF8. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_string_set_encoding( + struct bt_ctf_field_type *string_type, + enum bt_ctf_string_encoding encoding); + +/* + * bt_ctf_field_type_get_alignment: get a field type's alignment. + * + * Get the field type's alignment. + * + * @param type Field type. + * + * Returns the field type's alignment on success, a negative value on error and + * 0 if the alignment is undefined (as in the case of a variant). + */ +extern int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_set_alignment: set a field type's alignment. + * + * Set the field type's alignment. + * + * @param type Field type. + * @param alignment Type's alignment. Defaults to 1 (bit-aligned). However, + * some types, such as structures and string, may impose other alignment + * constraints. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, + unsigned int alignment); + +/* + * bt_ctf_field_type_get_byte_order: get a field type's byte order. + * + * @param type Field type. + * + * Returns the field type's byte order on success, a negative value on error. + */ +extern enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( + struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_set_byte_order: set a field type's byte order. + * + * Set the field type's byte order. + * + * @param type Field type. + * @param byte_order Field type's byte order. Defaults to + * BT_CTF_BYTE_ORDER_NATIVE; the trace's endianness. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, + enum bt_ctf_byte_order byte_order); + +/* + * bt_ctf_field_type_variant_get_tag_field_path: get a variant's tag's field + * path. + * + * Get the variant's tag's field path. + * + * @param type Field type. + * + * Returns the field path on success, NULL on error or if no field path is set. + */ +extern struct bt_ctf_field_path *bt_ctf_field_type_variant_get_tag_field_path( + struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_sequence_get_length_field_path: get a sequence's length's + * field path. + * + * Get the sequence's length's field path. + * + * @param type Field type. + * + * Returns the field path on success, NULL on error or if no field path is set. + */ +extern struct bt_ctf_field_path *bt_ctf_field_type_sequence_get_length_field_path( + struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_compare: compare two field types recursively + * + * Compare two field types recursively. + * + * The registered tag field type of a variant field type is ignored: + * only the tag strings are compared. + * + * @param type_a Field type A. + * @param type_b Field type B. + * + * Returns 0 if both field types are semantically equivalent, a positive + * value if they are not equivalent, or a negative value on error. + */ +extern int bt_ctf_field_type_compare(struct bt_ctf_field_type *type_a, + struct bt_ctf_field_type *type_b); + +/* + * bt_ctf_field_type_get_type_id: get a field type's bt_ctf_type_id. + * + * @param type Field type. + * + * Returns the field type's bt_ctf_type_id, CTF_TYPE_UNKNOWN on error. + */ +extern enum bt_ctf_type_id bt_ctf_field_type_get_type_id( + struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_integer: returns whether or not a given field + * type is an integer type. + * + * @param type Field type. + * + * Returns 1 if the field type is an integer type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_integer(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_floating_point: returns whether or not a given field + * type is a floating point number type. + * + * @param type Field type. + * + * Returns 1 if the field type is a floating point number type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_floating_point(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_enumeration: returns whether or not a given field + * type is an enumeration type. + * + * @param type Field type. + * + * Returns 1 if the field type is an enumeration type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_enumeration(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_string: returns whether or not a given field + * type is a string type. + * + * @param type Field type. + * + * Returns 1 if the field type is a string type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_string(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_structure: returns whether or not a given field + * type is a structure type. + * + * @param type Field type. + * + * Returns 1 if the field type is a structure type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_structure(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_array: returns whether or not a given field + * type is an array type. + * + * @param type Field type. + * + * Returns 1 if the field type is an array type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_array(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_sequence: returns whether or not a given field + * type is a sequence type. + * + * @param type Field type. + * + * Returns 1 if the field type is a sequence type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_sequence(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_is_variant: returns whether or not a given field + * type is a variant type. + * + * @param type Field type. + * + * Returns 1 if the field type is a variant type, 0 otherwise. + */ +extern int bt_ctf_field_type_is_variant(struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_type_get and bt_ctf_field_type_put: increment and decrement + * the field type's reference count. + * + * You may also use bt_ctf_get() and bt_ctf_put() with field type objects. + * + * These functions ensure that the field type won't be destroyed while it + * is in use. The same number of get and put (plus one extra put to + * release the initial reference done at creation) have to be done to + * destroy a field type. + * + * When the field type's reference count is decremented to 0 by a + * bt_ctf_field_type_put, the field type is freed. + * + * @param type Field type. + */ +extern void bt_ctf_field_type_get(struct bt_ctf_field_type *type); +extern void bt_ctf_field_type_put(struct bt_ctf_field_type *type); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_CTF_IR_FIELD_TYPES_H */ diff --git a/include/babeltrace/ctf-ir/fields-internal.h b/include/babeltrace/ctf-ir/fields-internal.h new file mode 100644 index 00000000..bd95bd2e --- /dev/null +++ b/include/babeltrace/ctf-ir/fields-internal.h @@ -0,0 +1,105 @@ +#ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H +#define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H + +/* + * Babeltrace - CTF IR: Event Fields internal + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include + +struct bt_ctf_field { + struct bt_object base; + struct bt_ctf_field_type *type; + int payload_set; +}; + +struct bt_ctf_field_integer { + struct bt_ctf_field parent; + struct definition_integer definition; +}; + +struct bt_ctf_field_enumeration { + struct bt_ctf_field parent; + struct bt_ctf_field *payload; +}; + +struct bt_ctf_field_floating_point { + struct bt_ctf_field parent; + struct definition_float definition; + struct definition_integer sign, mantissa, exp; +}; + +struct bt_ctf_field_structure { + struct bt_ctf_field parent; + GHashTable *field_name_to_index; + GPtrArray *fields; /* Array of pointers to struct bt_ctf_field */ +}; + +struct bt_ctf_field_variant { + struct bt_ctf_field parent; + struct bt_ctf_field *tag; + struct bt_ctf_field *payload; +}; + +struct bt_ctf_field_array { + struct bt_ctf_field parent; + GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */ +}; + +struct bt_ctf_field_sequence { + struct bt_ctf_field parent; + struct bt_ctf_field *length; + GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */ +}; + +struct bt_ctf_field_string { + struct bt_ctf_field parent; + GString *payload; +}; + +/* + * Set a field's value with an already allocated field instance. + */ +BT_HIDDEN +int bt_ctf_field_structure_set_field(struct bt_ctf_field *structure, + const char *name, struct bt_ctf_field *value); + +/* Validate that the field's payload is set (returns 0 if set). */ +BT_HIDDEN +int bt_ctf_field_validate(struct bt_ctf_field *field); + +/* Mark field payload as unset. */ +BT_HIDDEN +int bt_ctf_field_reset(struct bt_ctf_field *field); + +BT_HIDDEN +int bt_ctf_field_serialize(struct bt_ctf_field *field, + struct ctf_stream_pos *pos); + +#endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/fields.h b/include/babeltrace/ctf-ir/fields.h new file mode 100644 index 00000000..1b1ac8c2 --- /dev/null +++ b/include/babeltrace/ctf-ir/fields.h @@ -0,0 +1,486 @@ +#ifndef BABELTRACE_CTF_IR_FIELDS_H +#define BABELTRACE_CTF_IR_FIELDS_H + +/* + * Babeltrace - CTF IR: Event Fields + * + * Copyright 2013, 2014 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * The Common Trace Format (CTF) Specification is available at + * http://www.efficios.com/ctf + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_ctf_event_class; +struct bt_ctf_event; +struct bt_ctf_field; +struct bt_ctf_field_type; + +/* + * bt_ctf_field_create: create an instance of a field. + * + * Allocate a new field of the type described by the bt_ctf_field_type + * structure.The creation of a field sets its reference count to 1. + * + * @param type Field type to be instanciated. + * + * Returns an allocated field on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_create( + struct bt_ctf_field_type *type); + +/* + * bt_ctf_field_structure_get_field: get a structure's field. + * + * Get the structure's field corresponding to the provided field name. + * bt_ctf_field_put() must be called on the returned value. + * + * @param structure Structure field instance. + * @param name Name of the field in the provided structure. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_structure_get_field( + struct bt_ctf_field *structure, const char *name); + +/* + * bt_ctf_field_structure_get_field_by_index: get a structure's field by index. + * + * Get the structure's field corresponding to the provided field name. + * bt_ctf_field_put() must be called on the returned value. + * The indexes are the same as those provided for bt_ctf_field_type_structure. + * + * @param structure Structure field instance. + * @param index Index of the field in the provided structure. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( + struct bt_ctf_field *structure, int index); + +/* + * bt_ctf_field_array_get_field: get an array's field at position "index". + * + * Return the array's field at position "index". bt_ctf_field_put() must be + * called on the returned value. + * + * @param array Array field instance. + * @param index Position of the array's desired element. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_array_get_field( + struct bt_ctf_field *array, uint64_t index); + +/* + * bt_ctf_field_sequence_get_length: get a sequence's length. + * + * Get the sequence's length field. + * + * @param sequence Sequence field instance. + * + * Returns a field instance on success, NULL if a length was never set. + */ +extern struct bt_ctf_field *bt_ctf_field_sequence_get_length( + struct bt_ctf_field *sequence); + +/* + * bt_ctf_field_sequence_set_length: set a sequence's length. + * + * Set the sequence's length field. + * + * @param sequence Sequence field instance. + * @param length_field Unsigned integer field instance indicating the + * sequence's length. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, + struct bt_ctf_field *length_field); + +/* + * bt_ctf_field_sequence_get_field: get a sequence's field at position "index". + * + * Return the sequence's field at position "index". The sequence's length must + * have been set prior to calling this function using + * bt_ctf_field_sequence_set_length(). + * bt_ctf_field_put() must be called on the returned value. + * + * @param array Sequence field instance. + * @param index Position of the sequence's desired element. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_sequence_get_field( + struct bt_ctf_field *sequence, uint64_t index); + +/* + * bt_ctf_field_variant_get_field: get a variant's selected field. + * + * Return the variant's selected field. The "tag" field is the selector enum + * field. bt_ctf_field_put() must be called on the returned value. + * + * @param variant Variant field instance. + * @param tag Selector enumeration field. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_variant_get_field( + struct bt_ctf_field *variant, struct bt_ctf_field *tag); + +/* + * bt_ctf_field_variant_get_current_field: get the current selected field of a + * variant. + * + * Return the variant's current selected field. This function, unlike + * bt_ctf_field_variant_get_field(), does not create any field; it + * returns NULL if there's no current selected field yet. + * + * @param variant Variant field instance. + * + * Returns a field instance on success, NULL on error or when there's no + * current selected field. + */ +extern struct bt_ctf_field *bt_ctf_field_variant_get_current_field( + struct bt_ctf_field *variant); + +/* + * bt_ctf_field_enumeration_get_container: get an enumeration field's container. + * + * Return the enumeration's underlying container field (an integer). + * bt_ctf_field_put() must be called on the returned value. + * + * @param enumeration Enumeration field instance. + * + * Returns a field instance on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_enumeration_get_container( + struct bt_ctf_field *enumeration); + +/* + * bt_ctf_field_enumeration_get_mapping_name: get an enumeration field's mapping + * name. + * + * Return the enumeration's underlying container field (an integer). + * bt_ctf_field_put() must be called on the returned value. + * + * @param enumeration Enumeration field instance. + * + * Returns a field instance on success, NULL on error. + */ +extern const char *bt_ctf_field_enumeration_get_mapping_name( + struct bt_ctf_field *enumeration); + +/* + * bt_ctf_field_signed_integer_get_value: get a signed integer field's value + * + * Get a signed integer field's value. + * + * @param integer Signed integer field instance. + * @param value Pointer to a signed integer where the value will be stored. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *integer, + int64_t *value); + +/* + * bt_ctf_field_signed_integer_set_value: set a signed integer field's value + * + * Set a signed integer field's value. The value is checked to make sure it + * can be stored in the underlying field. + * + * @param integer Signed integer field instance. + * @param value Signed integer field value. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, + int64_t value); + +/* + * bt_ctf_field_unsigned_integer_get_value: get unsigned integer field's value + * + * Get an unsigned integer field's value. + * + * @param integer Unsigned integer field instance. + * @param value Pointer to an unsigned integer where the value will be stored. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *integer, + uint64_t *value); + +/* + * bt_ctf_field_unsigned_integer_set_value: set unsigned integer field's value + * + * Set an unsigned integer field's value. The value is checked to make sure it + * can be stored in the underlying field. + * + * @param integer Unsigned integer field instance. + * @param value Unsigned integer field value. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, + uint64_t value); + +/* + * bt_ctf_field_floating_point_get_value: get a floating point field's value + * + * Get a floating point field's value. + * + * @param floating_point Floating point field instance. + * @param value Pointer to a double where the value will be stored. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_floating_point_get_value( + struct bt_ctf_field *floating_point, double *value); + +/* + * bt_ctf_field_floating_point_set_value: set a floating point field's value + * + * Set a floating point field's value. The underlying type may not support the + * double's full precision. + * + * @param floating_point Floating point field instance. + * @param value Floating point field value. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_floating_point_set_value( + struct bt_ctf_field *floating_point, + double value); + +/* + * bt_ctf_field_string_get_value: get a string field's value + * + * Get a string field's value. + * + * @param string_field String field instance. + * + * Returns the string's value, NULL if unset. + */ +extern const char *bt_ctf_field_string_get_value( + struct bt_ctf_field *string_field); + +/* + * bt_ctf_field_string_set_value: set a string field's value + * + * Set a string field's value. + * + * @param string_field String field instance. + * @param value String field value (will be copied). + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_string_set_value(struct bt_ctf_field *string_field, + const char *value); + +/* + * bt_ctf_field_string_append: append a string to a string field's + * current value. + * + * Append a string to the current value of a string field. If the string + * field was never set using bt_ctf_field_string_set_value(), it is + * first set to an empty string, and then the concatenation happens. + * + * @param string_field String field instance. + * @param value String to append to the current string field's value. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_string_append(struct bt_ctf_field *string_field, + const char *value); + +/* + * bt_ctf_field_string_append_len: append a string of a given length to + * a string field's current value. + * + * Append a string of a given length to the current value of a string + * field. If the string field was never set using + * bt_ctf_field_string_set_value(), it is first set to an empty string, + * and then the concatenation happens. + * + * If a null byte is encountered before the given length, only the + * substring before the first null byte is appended. + * + * @param string_field String field instance. + * @param value String to append to the current string field's value. + * @param length Length of string value to append. + * + * Returns 0 on success, a negative value on error. + */ +extern int bt_ctf_field_string_append_len( + struct bt_ctf_field *string_field, const char *value, + unsigned int length); + +/* + * bt_ctf_field_get_type: get a field's type + * + * @param field Field intance. + * + * Returns a field type instance on success, NULL on error. + */ +extern struct bt_ctf_field_type *bt_ctf_field_get_type( + struct bt_ctf_field *field); + +/* + * bt_ctf_field_get_type_id: get a field's ctf_type_id. + * + * This is a helper function which avoids a call to + * bt_ctf_field_get_type(), followed by a call to + * bt_ctf_field_type_get_type_id(), followed by a call to + * bt_ctf_put(). + * + * @param field Field instance. + * + * Returns the field's ctf_type_id, CTF_TYPE_UNKNOWN on error. + */ +extern enum bt_ctf_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_integer: returns whether or not a given field + * is an integer type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is an integer type, 0 otherwise. + */ +extern int bt_ctf_field_is_integer(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_floating_point: returns whether or not a given field + * is a floating point number type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is a floating point number type, 0 otherwise. + */ +extern int bt_ctf_field_is_floating_point(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_enumeration: returns whether or not a given field + * is an enumeration type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is an enumeration type, 0 otherwise. + */ +extern int bt_ctf_field_is_enumeration(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_string: returns whether or not a given field + * is a string type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is a string type, 0 otherwise. + */ +extern int bt_ctf_field_is_string(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_structure: returns whether or not a given field + * is a structure type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is a structure type, 0 otherwise. + */ +extern int bt_ctf_field_is_structure(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_array: returns whether or not a given field + * is an array type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is an array type, 0 otherwise. + */ +extern int bt_ctf_field_is_array(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_sequence: returns whether or not a given field + * is a sequence type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is a sequence type, 0 otherwise. + */ +extern int bt_ctf_field_is_sequence(struct bt_ctf_field *field); + +/* + * bt_ctf_field_is_variant: returns whether or not a given field + * is a variant type. + * + * @param field Field instance. + * + * Returns 1 if the field instance is a variant type, 0 otherwise. + */ +extern int bt_ctf_field_is_variant(struct bt_ctf_field *field); + +/* + * bt_ctf_field_copy: get a field's deep copy. + * + * Get a field's deep copy. The created field copy shares the source's + * associated field types. + * + * On success, the returned copy has its reference count set to 1. + * + * @param field Field instance. + * + * Returns the field copy on success, NULL on error. + */ +extern struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field); + +/* + * bt_ctf_field_get and bt_ctf_field_put: increment and decrement the + * field's reference count. + * + * You may also use bt_ctf_get() and bt_ctf_put() with field objects. + * + * These functions ensure that the field won't be destroyed when it + * is in use. The same number of get and put (plus one extra put to + * release the initial reference done at creation) have to be done to + * destroy a field. + * + * When the field's reference count is decremented to 0 by a bt_ctf_field_put, + * the field is freed. + * + * @param field Field instance. + */ +extern void bt_ctf_field_get(struct bt_ctf_field *field); +extern void bt_ctf_field_put(struct bt_ctf_field *field); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE_CTF_IR_FIELDS_H */ diff --git a/include/babeltrace/ctf-ir/resolve-internal.h b/include/babeltrace/ctf-ir/resolve-internal.h index bd35f628..5479d44f 100644 --- a/include/babeltrace/ctf-ir/resolve-internal.h +++ b/include/babeltrace/ctf-ir/resolve-internal.h @@ -29,8 +29,8 @@ * SOFTWARE. */ -#include -#include +#include +#include #include #include #include diff --git a/include/babeltrace/ctf-ir/trace-internal.h b/include/babeltrace/ctf-ir/trace-internal.h index e92eb7c1..eb8a2563 100644 --- a/include/babeltrace/ctf-ir/trace-internal.h +++ b/include/babeltrace/ctf-ir/trace-internal.h @@ -28,8 +28,8 @@ */ #include -#include -#include +#include +#include #include #include #include diff --git a/include/babeltrace/ctf-ir/trace.h b/include/babeltrace/ctf-ir/trace.h index 29b60980..407982b3 100644 --- a/include/babeltrace/ctf-ir/trace.h +++ b/include/babeltrace/ctf-ir/trace.h @@ -30,7 +30,7 @@ * http://www.efficios.com/ctf */ -#include +#include #include #include diff --git a/include/babeltrace/ctf-ir/validation-internal.h b/include/babeltrace/ctf-ir/validation-internal.h index 065b32ea..e5636377 100644 --- a/include/babeltrace/ctf-ir/validation-internal.h +++ b/include/babeltrace/ctf-ir/validation-internal.h @@ -25,7 +25,7 @@ * SOFTWARE. */ -#include +#include #include #include #include diff --git a/include/babeltrace/ctf-writer/event-fields.h b/include/babeltrace/ctf-writer/event-fields.h index b5d8ccbc..3ab3e39f 100644 --- a/include/babeltrace/ctf-writer/event-fields.h +++ b/include/babeltrace/ctf-writer/event-fields.h @@ -27,4 +27,4 @@ * http://www.efficios.com/ctf */ -#include +#include diff --git a/include/babeltrace/ctf-writer/event-types.h b/include/babeltrace/ctf-writer/event-types.h index b1f4b21a..ac00f7e3 100644 --- a/include/babeltrace/ctf-writer/event-types.h +++ b/include/babeltrace/ctf-writer/event-types.h @@ -27,4 +27,4 @@ * http://www.efficios.com/ctf */ -#include +#include diff --git a/include/babeltrace/ctf-writer/writer.h b/include/babeltrace/ctf-writer/writer.h index 1c94bcca..940736fe 100644 --- a/include/babeltrace/ctf-writer/writer.h +++ b/include/babeltrace/ctf-writer/writer.h @@ -30,7 +30,7 @@ * http://www.efficios.com/ctf */ -#include +#include #include #ifdef __cplusplus diff --git a/include/babeltrace/ctf/events-internal.h b/include/babeltrace/ctf/events-internal.h index ba64bc09..d227b4d4 100644 --- a/include/babeltrace/ctf/events-internal.h +++ b/include/babeltrace/ctf/events-internal.h @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include struct ctf_stream_definition; diff --git a/include/babeltrace/ctf/events.h b/include/babeltrace/ctf/events.h index 15ebe56f..134db8c0 100644 --- a/include/babeltrace/ctf/events.h +++ b/include/babeltrace/ctf/events.h @@ -34,12 +34,12 @@ #include #include #include -#include +#include /* * This header must exist in Babeltrace 2 because it contained * enumerations that were used in CTF writer's API. Those enumerations - * are now located in ctf-ir/event-types.h. + * are now located in ctf-ir/field-types.h. */ #endif /* _BABELTRACE_CTF_EVENTS_H */ diff --git a/tests/lib/test_bt_ctf_field_type_validation.c b/tests/lib/test_bt_ctf_field_type_validation.c index 6338a7b3..c626d4ba 100644 --- a/tests/lib/test_bt_ctf_field_type_validation.c +++ b/tests/lib/test_bt_ctf_field_type_validation.c @@ -19,7 +19,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include +#include #include #include #include