Split the CTF-Writer implementation in IR and Writer parts
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 15 Apr 2014 20:56:21 +0000 (16:56 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 27 Jun 2014 20:07:25 +0000 (16:07 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
14 files changed:
configure.ac
formats/ctf/Makefile.am
formats/ctf/ir/Makefile.am [new file with mode: 0644]
formats/ctf/ir/clock.c [new file with mode: 0644]
formats/ctf/ir/event-fields.c [new file with mode: 0644]
formats/ctf/ir/event-types.c [new file with mode: 0644]
formats/ctf/ir/event.c [new file with mode: 0644]
formats/ctf/ir/stream-class.c [new file with mode: 0644]
formats/ctf/writer/Makefile.am
formats/ctf/writer/clock.c [deleted file]
formats/ctf/writer/event-fields.c [deleted file]
formats/ctf/writer/event-types.c [deleted file]
formats/ctf/writer/event.c [deleted file]
formats/ctf/writer/stream.c

index aefa8a59984e0b847b2cef494aff29c33be90de5..c9a25e0905f18c575528b94024a2b16684a13e16 100644 (file)
@@ -178,6 +178,7 @@ AC_CONFIG_FILES([
        formats/lttng-live/Makefile
        formats/ctf/metadata/Makefile
        formats/ctf/writer/Makefile
+       formats/ctf/ir/Makefile
        converter/Makefile
        doc/Makefile
        lib/Makefile
index 5d8a29701eccfc4063fc71f0b699e1edbfe550f8..7d97f8e6c23b574336059744827f427b2ab38c5e 100644 (file)
@@ -1,6 +1,6 @@
 AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
 
-SUBDIRS = types metadata writer .
+SUBDIRS = types metadata writer ir .
 
 lib_LTLIBRARIES = libbabeltrace-ctf.la
 
@@ -20,4 +20,5 @@ libbabeltrace_ctf_la_LIBADD = \
        types/libctf-types.la \
        metadata/libctf-parser.la \
        metadata/libctf-ast.la \
-       writer/libctf-writer.la
+       writer/libctf-writer.la \
+       ir/libctf-ir.la
diff --git a/formats/ctf/ir/Makefile.am b/formats/ctf/ir/Makefile.am
new file mode 100644 (file)
index 0000000..0247445
--- /dev/null
@@ -0,0 +1,20 @@
+AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
+
+noinst_LTLIBRARIES = libctf-ir.la
+
+libctf_ir_la_SOURCES = \
+       clock.c \
+       event.c \
+       event-fields.c \
+       event-types.c \
+       stream-class.c
+
+libctf_ir_la_LIBADD = \
+       $(top_builddir)/lib/libbabeltrace.la
+
+if BABELTRACE_BUILD_WITH_LIBUUID
+libctf_ir_la_LIBADD += -luuid
+endif
+if BABELTRACE_BUILD_WITH_LIBC_UUID
+libctf_ir_la_LIBADD += -lc
+endif
diff --git a/formats/ctf/ir/clock.c b/formats/ctf/ir/clock.c
new file mode 100644 (file)
index 0000000..d8df956
--- /dev/null
@@ -0,0 +1,264 @@
+/*
+ * clock.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+#include <inttypes.h>
+
+static
+void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
+
+struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
+{
+       struct bt_ctf_clock *clock = NULL;
+
+       if (validate_identifier(name)) {
+               goto error;
+       }
+
+       clock = g_new0(struct bt_ctf_clock, 1);
+       if (!clock) {
+               goto error;
+       }
+
+       clock->name = g_string_new(name);
+       if (!clock->name) {
+               goto error_destroy;
+       }
+
+       clock->description = g_string_new(NULL);
+       if (!clock->description) {
+               goto error_destroy;
+       }
+
+       clock->precision = 1;
+       clock->frequency = 1000000000;
+       uuid_generate(clock->uuid);
+       bt_ctf_ref_init(&clock->ref_count);
+       return clock;
+error_destroy:
+       bt_ctf_clock_destroy(&clock->ref_count);
+error:
+       clock = NULL;
+       return clock;
+}
+
+int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
+{
+       int ret = 0;
+
+       if (!clock || !desc || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->description = g_string_assign(clock->description, desc);
+       ret = clock->description ? 0 : -1;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->frequency = freq;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->precision = precision;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->offset_s = offset_s;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->offset = offset;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->absolute = !!is_absolute;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+{
+       int ret = 0;
+
+       /* Timestamps are strictly monotonic */
+       if (!clock || time < clock->time) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->time = time;
+end:
+       return ret;
+}
+
+void bt_ctf_clock_get(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       bt_ctf_ref_get(&clock->ref_count);
+}
+
+void bt_ctf_clock_put(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+}
+
+BT_HIDDEN
+void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       clock->frozen = 1;
+}
+
+BT_HIDDEN
+void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
+               struct metadata_context *context)
+{
+       unsigned char *uuid;
+
+       if (!clock || !context) {
+               return;
+       }
+
+       uuid = clock->uuid;
+       g_string_append(context->string, "clock {\n");
+       g_string_append_printf(context->string, "\tname = %s;\n",
+               clock->name->str);
+       g_string_append_printf(context->string,
+               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
+               uuid[0], uuid[1], uuid[2], uuid[3],
+               uuid[4], uuid[5], uuid[6], uuid[7],
+               uuid[8], uuid[9], uuid[10], uuid[11],
+               uuid[12], uuid[13], uuid[14], uuid[15]);
+       if (clock->description->len) {
+               g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
+                       clock->description->str);
+       }
+
+       g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
+               clock->frequency);
+       g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
+               clock->precision);
+       g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
+               clock->offset_s);
+       g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
+               clock->offset);
+       g_string_append_printf(context->string, "\tabsolute = %s;\n",
+               clock->absolute ? "TRUE" : "FALSE");
+       g_string_append(context->string, "};\n\n");
+}
+
+BT_HIDDEN
+uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+{
+       return clock ? clock->time : 0;
+}
+
+static
+void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_clock *clock;
+
+       if (!ref) {
+               return;
+       }
+
+       clock = container_of(ref, struct bt_ctf_clock, ref_count);
+       if (clock->name) {
+               g_string_free(clock->name, TRUE);
+       }
+
+       if (clock->description) {
+               g_string_free(clock->description, TRUE);
+       }
+
+       g_free(clock);
+}
diff --git a/formats/ctf/ir/event-fields.c b/formats/ctf/ir/event-fields.c
new file mode 100644 (file)
index 0000000..17616a5
--- /dev/null
@@ -0,0 +1,1257 @@
+/*
+ * event-fields.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-ir/event-fields-internal.h>
+#include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/compiler.h>
+
+#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_ctf_ref *);
+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 *field);
+static
+int bt_ctf_field_structure_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_variant_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_array_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_sequence_validate(struct bt_ctf_field *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 increase_packet_size(struct ctf_stream_pos *pos);
+
+static
+struct bt_ctf_field *(*field_create_funcs[])(
+               struct bt_ctf_field_type *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_create,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_create,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_create,
+};
+
+static
+void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_destroy,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_destroy,
+};
+
+static
+int (*field_validate_funcs[])(struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
+       [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate,
+       [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
+};
+
+static
+int (*field_serialize_funcs[])(struct bt_ctf_field *,
+               struct ctf_stream_pos *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_serialize,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
+};
+
+struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field *field = NULL;
+       enum ctf_type_id type_id;
+
+       if (!type) {
+               goto error;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES ||
+               bt_ctf_field_type_validate(type)) {
+               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_ctf_field_type_get(type);
+       bt_ctf_ref_init(&field->ref_count);
+       field->type = type;
+error:
+       return field;
+}
+
+void bt_ctf_field_get(struct bt_ctf_field *field)
+{
+       if (field) {
+               bt_ctf_ref_get(&field->ref_count);
+       }
+}
+
+void bt_ctf_field_put(struct bt_ctf_field *field)
+{
+       if (field) {
+               bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
+       }
+}
+
+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) !=
+               CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       length_type = container_of(length_field->type,
+               struct bt_ctf_field_type_integer, parent);
+       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_ctf_field_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_ctf_field_put);
+       g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
+       bt_ctf_field_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_structure *structure_type;
+       struct bt_ctf_field_type *field_type;
+       size_t index;
+
+       if (!field || !name ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRUCT) {
+               goto error;
+       }
+
+       field_quark = g_quark_from_string(name);
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       structure_type = container_of(field->type,
+               struct bt_ctf_field_type_structure, parent);
+       field_type = bt_ctf_field_type_structure_get_type(structure_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_ctf_field_get(new_field);
+error:
+       return new_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_structure *structure_type;
+       struct bt_ctf_field_type *expected_field_type;
+       size_t index;
+
+       if (!field || !name || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRUCT) {
+               ret = -1;
+               goto end;
+       }
+
+       field_quark = g_quark_from_string(name);
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       structure_type = container_of(field->type,
+               struct bt_ctf_field_type_structure, parent);
+       expected_field_type = bt_ctf_field_type_structure_get_type(
+               structure_type, name);
+       if (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_ctf_field_put(structure->fields->pdata[index]);
+       }
+
+       structure->fields->pdata[index] = value;
+       bt_ctf_field_get(value);
+end:
+       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_array *array;
+       struct bt_ctf_field_type_array *array_type;
+       struct bt_ctf_field_type *field_type;
+
+       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_ARRAY) {
+               goto end;
+       }
+
+       array = container_of(field, struct bt_ctf_field_array, parent);
+       if (index >= array->elements->len) {
+               goto end;
+       }
+
+       array_type = container_of(field->type, struct bt_ctf_field_type_array,
+               parent);
+       field_type = bt_ctf_field_type_array_get_element_type(array_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);
+       bt_ctf_field_get(new_field);
+       array->elements->pdata[(size_t)index] = new_field;
+end:
+       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_sequence *sequence;
+       struct bt_ctf_field_type_sequence *sequence_type;
+       struct bt_ctf_field_type *field_type;
+
+       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_SEQUENCE) {
+               goto end;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       if (!sequence->elements || sequence->elements->len <= index) {
+               goto end;
+       }
+
+       sequence_type = container_of(field->type,
+               struct bt_ctf_field_type_sequence, parent);
+       field_type = bt_ctf_field_type_sequence_get_element_type(sequence_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);
+       bt_ctf_field_get(new_field);
+       sequence->elements->pdata[(size_t)index] = new_field;
+end:
+       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) !=
+                       CTF_TYPE_VARIANT ||
+               bt_ctf_field_type_get_type_id(tag_field->type) !=
+                       CTF_TYPE_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(variant->tag)) {
+               goto end;
+       }
+
+       tag_enum_value = tag_enum_integer->definition.value._signed;
+       field_type = bt_ctf_field_type_variant_get_field_type(variant_type,
+               tag_enum_value);
+       if (!field_type) {
+               goto end;
+       }
+
+       new_field = bt_ctf_field_create(field_type);
+       if (!new_field) {
+               goto end;
+       }
+
+       bt_ctf_field_put(variant->tag);
+       bt_ctf_field_put(variant->payload);
+       bt_ctf_field_get(new_field);
+       bt_ctf_field_get(tag_field);
+       variant->tag = tag_field;
+       variant->payload = new_field;
+end:
+       bt_ctf_field_put(tag_enum);
+       return new_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) {
+               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_ctf_field_get(container);
+end:
+       return container;
+}
+
+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) !=
+                       CTF_TYPE_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_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) !=
+                       CTF_TYPE_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_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) !=
+                       CTF_TYPE_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;
+}
+
+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) !=
+                       CTF_TYPE_STRING) {
+               ret = -1;
+               goto end;
+       }
+
+       string = container_of(field, struct bt_ctf_field_string, parent);
+       if (string->payload) {
+               g_string_free(string->payload, TRUE);
+       }
+
+       string->payload = g_string_new(value);
+       string->parent.payload_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_field_validate(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       enum 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 <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = field_validate_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 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 <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = field_serialize_funcs[type_id](field, pos);
+end:
+       return ret;
+}
+
+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 || !structure_type->fields->len) {
+               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_ctf_ref *ref)
+{
+       struct bt_ctf_field *field;
+       struct bt_ctf_field_type *type;
+       enum ctf_type_id type_id;
+
+       if (!ref) {
+               return;
+       }
+
+       field = container_of(ref, struct bt_ctf_field, ref_count);
+       type = field->type;
+       type_id = bt_ctf_field_type_get_type_id(type);
+       if (type_id <= CTF_TYPE_UNKNOWN ||
+               type_id >= NR_CTF_TYPES) {
+               return;
+       }
+
+       field_destroy_funcs[type_id](field);
+       if (type) {
+               bt_ctf_field_type_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_ctf_field_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_ctf_field_put(variant->tag);
+       bt_ctf_field_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);
+       g_ptr_array_free(sequence->elements, TRUE);
+       bt_ctf_field_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);
+       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_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_ctf_field_put(character);
+       bt_ctf_field_type_put(character_type);
+       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;
+       ret = posix_fallocate(pos->fd, pos->mmap_offset,
+               pos->packet_size / CHAR_BIT);
+       if (ret) {
+               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
new file mode 100644 (file)
index 0000000..0c8eddc
--- /dev/null
@@ -0,0 +1,1445 @@
+/*
+ * event-types.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+#include <babeltrace/endian.h>
+#include <float.h>
+#include <inttypes.h>
+#include <stdlib.h>
+
+struct range_overlap_query {
+       int64_t range_start, range_end;
+       int overlaps;
+       GQuark mapping_name;
+};
+
+static
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *);
+
+static
+void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
+       [CTF_TYPE_ENUM] =
+               bt_ctf_field_type_enumeration_destroy,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_destroy,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
+       [CTF_TYPE_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[] = {
+       [CTF_TYPE_INTEGER] = generic_field_type_freeze,
+       [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze,
+       [CTF_TYPE_FLOAT] = generic_field_type_freeze,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
+       [CTF_TYPE_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[] = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize,
+       [CTF_TYPE_ENUM] =
+               bt_ctf_field_type_enumeration_serialize,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_serialize,
+       [CTF_TYPE_STRUCT] =
+               bt_ctf_field_type_structure_serialize,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
+       [CTF_TYPE_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);
+static
+void bt_ctf_field_type_floating_point_set_byte_order(
+               struct bt_ctf_field_type *, int byte_order);
+
+static
+void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
+       int) = {
+       [CTF_TYPE_INTEGER] =
+               bt_ctf_field_type_integer_set_byte_order,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_set_byte_order,
+       [CTF_TYPE_ENUM ... CTF_TYPE_SEQUENCE] = NULL,
+};
+
+
+static
+void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
+{
+       g_free(mapping);
+}
+
+static
+void destroy_structure_field(struct structure_field *field)
+{
+       if (field->type) {
+               bt_ctf_field_type_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 <= overlap_query->range_end
+               && overlap_query->range_start <= mapping->range_end) {
+               overlap_query->overlaps = 1;
+               overlap_query->mapping_name = mapping->string;
+       }
+
+       overlap_query->overlaps |=
+               mapping->string == overlap_query->mapping_name;
+}
+
+static
+void bt_ctf_field_type_init(struct bt_ctf_field_type *type)
+{
+       enum ctf_type_id type_id = type->declaration->id;
+       int ret;
+
+       assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
+               (type_id < NR_CTF_TYPES));
+
+       bt_ctf_ref_init(&type->ref_count);
+       type->freeze = type_freeze_funcs[type_id];
+       type->serialize = type_serialize_funcs[type_id];
+       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_ctf_field_type_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);
+       bt_ctf_field_type_freeze(field_type);
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_field_type_validate(struct bt_ctf_field_type *type)
+{
+       int ret = 0;
+
+       if (!type) {
+               ret = -1;
+               goto end;
+       }
+
+       if (type->declaration->id == CTF_TYPE_ENUM) {
+               struct bt_ctf_field_type_enumeration *enumeration =
+                       container_of(type, struct bt_ctf_field_type_enumeration,
+                       parent);
+
+               ret = enumeration->entries->len ? 0 : -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 > 64) {
+               return NULL;
+       }
+
+       integer->parent.declaration = &integer->declaration.p;
+       integer->parent.declaration->id = CTF_TYPE_INTEGER;
+       integer->declaration.len = size;
+       integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL;
+       integer->declaration.encoding = CTF_STRING_NONE;
+       bt_ctf_field_type_init(&integer->parent);
+       return &integer->parent;
+}
+
+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 != CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(type, struct bt_ctf_field_type_integer, parent);
+       if (is_signed && integer->declaration.len <= 1) {
+               ret = -1;
+               goto end;
+       }
+
+       integer->declaration.signedness = !!is_signed;
+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 != CTF_TYPE_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;
+}
+
+int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
+               enum ctf_string_encoding encoding)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_integer *integer;
+
+       if (!type || type->frozen ||
+               (type->declaration->id != CTF_TYPE_INTEGER) ||
+               (encoding < CTF_STRING_NONE) ||
+               (encoding >= CTF_STRING_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_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;
+       }
+
+       enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
+       if (!enumeration) {
+               goto error;
+       }
+
+       enumeration->parent.declaration = &enumeration->declaration.p;
+       enumeration->parent.declaration->id = CTF_TYPE_ENUM;
+       bt_ctf_field_type_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);
+       return &enumeration->parent;
+error:
+       g_free(enumeration);
+       return NULL;
+}
+
+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 != CTF_TYPE_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 = range_start,
+               .range_end = 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 = range_start,
+               .range_end = range_end, .string = mapping_name};
+       g_ptr_array_add(enumeration->entries, mapping);
+error_free:
+       free(escaped_string);
+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 = CTF_TYPE_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);
+end:
+       return floating_point ? &floating_point->parent : NULL;
+}
+
+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 != CTF_TYPE_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_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 != CTF_TYPE_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 = CTF_TYPE_STRUCT;
+       bt_ctf_field_type_init(&structure->parent);
+       structure->fields = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)destroy_structure_field);
+       structure->field_name_to_index = g_hash_table_new(NULL, NULL);
+       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 ||
+               validate_identifier(field_name) ||
+               (type->declaration->id != CTF_TYPE_STRUCT) ||
+               bt_ctf_field_type_validate(field_type)) {
+               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;
+       }
+
+       if (type->declaration->alignment < field_type->declaration->alignment) {
+               type->declaration->alignment =
+                       field_type->declaration->alignment;
+       }
+end:
+       return ret;
+}
+
+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 (!enum_tag || validate_identifier(tag_name) ||
+               (enum_tag->declaration->id != CTF_TYPE_ENUM)) {
+               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 = CTF_TYPE_VARIANT;
+       variant->tag_name = g_string_new(tag_name);
+       bt_ctf_field_type_init(&variant->parent);
+       variant->field_name_to_index = g_hash_table_new(NULL, NULL);
+       variant->fields = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)destroy_structure_field);
+       bt_ctf_field_type_get(enum_tag);
+       variant->tag = container_of(enum_tag,
+               struct bt_ctf_field_type_enumeration, parent);
+       return &variant->parent;
+error:
+       return NULL;
+}
+
+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;
+       int name_found = 0;
+       struct bt_ctf_field_type_variant *variant;
+       GQuark field_name_quark = g_quark_from_string(field_name);
+
+       if (!type || !field_type || type->frozen ||
+               validate_identifier(field_name) ||
+               (type->declaration->id != CTF_TYPE_VARIANT) ||
+               bt_ctf_field_type_validate(field_type)) {
+               ret = -1;
+               goto end;
+       }
+
+       variant = container_of(type, struct bt_ctf_field_type_variant, parent);
+       /* 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 || 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_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 ||
+               bt_ctf_field_type_validate(element_type)) {
+               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 = CTF_TYPE_ARRAY;
+       bt_ctf_field_type_init(&array->parent);
+       bt_ctf_field_type_get(element_type);
+       array->element_type = element_type;
+       array->length = length;
+       array->parent.declaration->alignment =
+               element_type->declaration->alignment;
+       return &array->parent;
+error:
+       return NULL;
+}
+
+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 || validate_identifier(length_field_name) ||
+               bt_ctf_field_type_validate(element_type)) {
+               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 = CTF_TYPE_SEQUENCE;
+       bt_ctf_field_type_init(&sequence->parent);
+       bt_ctf_field_type_get(element_type);
+       sequence->element_type = element_type;
+       sequence->length_field_name = g_string_new(length_field_name);
+       sequence->parent.declaration->alignment =
+               element_type->declaration->alignment;
+       return &sequence->parent;
+error:
+       return NULL;
+}
+
+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 = CTF_TYPE_STRING;
+       bt_ctf_field_type_init(&string->parent);
+       string->declaration.encoding = CTF_STRING_UTF8;
+       string->parent.declaration->alignment = CHAR_BIT;
+       return &string->parent;
+}
+
+int bt_ctf_field_type_string_set_encoding(
+               struct bt_ctf_field_type *type,
+               enum ctf_string_encoding encoding)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_string *string;
+
+       if (!type || type->declaration->id != CTF_TYPE_STRING ||
+               (encoding != CTF_STRING_UTF8 &&
+               encoding != CTF_STRING_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_set_alignment(struct bt_ctf_field_type *type,
+               unsigned int alignment)
+{
+       int ret = 0;
+
+       /* Alignment must be bit-aligned (1) or byte aligned */
+       if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) {
+               ret = -1;
+               goto end;
+       }
+
+       if (type->declaration->id == CTF_TYPE_STRING &&
+               alignment != CHAR_BIT) {
+               ret = -1;
+               goto end;
+       }
+
+       type->declaration->alignment = alignment;
+       ret = 0;
+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 ctf_type_id type_id;
+
+       if (!type || type->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = type->declaration->id;
+       switch (byte_order) {
+       case BT_CTF_BYTE_ORDER_NATIVE:
+               internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN ?
+                       LITTLE_ENDIAN : BIG_ENDIAN);
+               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;
+       }
+
+       if (set_byte_order_funcs[type_id]) {
+               set_byte_order_funcs[type_id](type, internal_byte_order);
+       }
+end:
+       return ret;
+}
+
+void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return;
+       }
+
+       bt_ctf_ref_get(&type->ref_count);
+}
+
+void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
+{
+       enum ctf_type_id type_id;
+
+       if (!type) {
+               return;
+       }
+
+       type_id = type->declaration->id;
+       assert(type_id > CTF_TYPE_UNKNOWN && type_id < NR_CTF_TYPES);
+       bt_ctf_ref_put(&type->ref_count, type_destroy_funcs[type_id]);
+}
+
+BT_HIDDEN
+void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return;
+       }
+
+       type->freeze(type);
+}
+
+BT_HIDDEN
+enum ctf_type_id bt_ctf_field_type_get_type_id(
+               struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return CTF_TYPE_UNKNOWN;
+       }
+
+       return type->declaration->id;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_structure_get_type(
+               struct bt_ctf_field_type_structure *structure,
+               const char *name)
+{
+       struct bt_ctf_field_type *type = NULL;
+       struct structure_field *field;
+       GQuark name_quark = g_quark_try_string(name);
+       size_t index;
+
+       if (!name_quark) {
+               goto end;
+       }
+
+       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];
+       type = field->type;
+end:
+       return type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
+               struct bt_ctf_field_type_array *array)
+{
+       assert(array);
+       return array->element_type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
+               struct bt_ctf_field_type_sequence *sequence)
+{
+       assert(sequence);
+       return sequence->element_type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type(
+               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 = tag_value,
+               .range_end = 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
+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;
+       }
+
+       ret = type->serialize(type, context);
+end:
+       return ret;
+}
+
+static
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_integer *integer;
+
+       if (!ref) {
+               return;
+       }
+
+       integer = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_integer, parent);
+       g_free(integer);
+}
+
+static
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_enumeration *enumeration;
+
+       if (!ref) {
+               return;
+       }
+
+       enumeration = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_enumeration, parent);
+       g_ptr_array_free(enumeration->entries, TRUE);
+       bt_ctf_field_type_put(enumeration->container);
+       g_free(enumeration);
+}
+
+static
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_floating_point *floating_point;
+
+       if (!ref) {
+               return;
+       }
+
+       floating_point = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_floating_point, parent);
+       g_free(floating_point);
+}
+
+static
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_structure *structure;
+
+       if (!ref) {
+               return;
+       }
+
+       structure = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_structure, parent);
+       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_ref *ref)
+{
+       struct bt_ctf_field_type_variant *variant;
+
+       if (!ref) {
+               return;
+       }
+
+       variant = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_variant, parent);
+       g_ptr_array_free(variant->fields, TRUE);
+       g_hash_table_destroy(variant->field_name_to_index);
+       g_string_free(variant->tag_name, TRUE);
+       bt_ctf_field_type_put(&variant->tag->parent);
+       g_free(variant);
+}
+
+static
+void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_array *array;
+
+       if (!ref) {
+               return;
+       }
+
+       array = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_array, parent);
+       bt_ctf_field_type_put(array->element_type);
+       g_free(array);
+}
+
+static
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_sequence *sequence;
+
+       if (!ref) {
+               return;
+       }
+
+       sequence = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_sequence, parent);
+       bt_ctf_field_type_put(sequence->element_type);
+       g_string_free(sequence->length_field_name, TRUE);
+       g_free(sequence);
+}
+
+static
+void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_string *string;
+
+       if (!ref) {
+               return;
+       }
+
+       string = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_string, parent);
+       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);
+
+       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);
+
+       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);
+
+       generic_field_type_freeze(type);
+       bt_ctf_field_type_freeze(sequence_type->element_type);
+}
+
+static
+const char *get_encoding_string(enum ctf_string_encoding encoding)
+{
+       const char *encoding_string;
+
+       switch (encoding) {
+       case CTF_STRING_NONE:
+               encoding_string = "none";
+               break;
+       case CTF_STRING_ASCII:
+               encoding_string = "ASCII";
+               break;
+       case CTF_STRING_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);
+
+       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));
+       return 0;
+}
+
+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);
+
+       ret = bt_ctf_field_type_validate(type);
+       if (ret) {
+               goto end;
+       }
+
+       g_string_append(context->string, "enum : ");
+       ret = bt_ctf_field_type_serialize(enumeration->container, context);
+       if (ret) {
+               goto end;
+       }
+
+       g_string_append(context->string, " { ");
+       for (entry = 0; entry < enumeration->entries->len; entry++) {
+               struct enumeration_mapping *mapping =
+                       enumeration->entries->pdata[entry];
+
+               if (mapping->range_start == mapping->range_end) {
+                       g_string_append_printf(context->string,
+                               "\"%s\" = %" PRId64,
+                               g_quark_to_string(mapping->string),
+                               mapping->range_start);
+               } else {
+                       g_string_append_printf(context->string,
+                               "\"%s\" = %" PRId64 " ... %" PRId64,
+                               g_quark_to_string(mapping->string),
+                               mapping->range_start, mapping->range_end);
+               }
+
+               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, "");
+       }
+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("");
+       g_string_append_printf(context->string,
+               "variant <%s> {\n", variant->tag_name->str);
+       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
+void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
+               int byte_order)
+{
+       struct bt_ctf_field_type_integer *integer_type = container_of(type,
+               struct bt_ctf_field_type_integer, parent);
+
+       integer_type->declaration.byte_order = byte_order;
+}
+
+static
+void bt_ctf_field_type_floating_point_set_byte_order(
+               struct bt_ctf_field_type *type, int byte_order)
+{
+       struct bt_ctf_field_type_floating_point *floating_point_type =
+               container_of(type, struct bt_ctf_field_type_floating_point,
+               parent);
+
+       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;
+}
diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c
new file mode 100644 (file)
index 0000000..c62317d
--- /dev/null
@@ -0,0 +1,368 @@
+/*
+ * event.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-ir/event-fields-internal.h>
+#include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/event-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+
+static
+void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref);
+static
+void bt_ctf_event_destroy(struct bt_ctf_ref *ref);
+
+struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
+{
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (validate_identifier(name)) {
+               goto end;
+       }
+
+       event_class = g_new0(struct bt_ctf_event_class, 1);
+       if (!event_class) {
+               goto end;
+       }
+
+       bt_ctf_ref_init(&event_class->ref_count);
+       event_class->name = g_quark_from_string(name);
+end:
+       return event_class;
+}
+
+int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
+               struct bt_ctf_field_type *type,
+               const char *name)
+{
+       int ret = 0;
+
+       if (!event_class || !type || validate_identifier(name) ||
+               event_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!event_class->fields) {
+               event_class->fields = bt_ctf_field_type_structure_create();
+               if (!event_class->fields) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_class->fields,
+               type, name);
+end:
+       return ret;
+}
+
+void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
+{
+       if (!event_class) {
+               return;
+       }
+
+       bt_ctf_ref_get(&event_class->ref_count);
+}
+
+void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
+{
+       if (!event_class) {
+               return;
+       }
+
+       bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy);
+}
+
+struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
+{
+       struct bt_ctf_event *event = NULL;
+
+       if (!event_class) {
+               goto end;
+       }
+
+       event = g_new0(struct bt_ctf_event, 1);
+       if (!event) {
+               goto end;
+       }
+
+       bt_ctf_ref_init(&event->ref_count);
+       bt_ctf_event_class_get(event_class);
+       bt_ctf_event_class_freeze(event_class);
+       event->event_class = event_class;
+       event->context_payload = bt_ctf_field_create(event_class->context);
+       event->fields_payload = bt_ctf_field_create(event_class->fields);
+end:
+       return event;
+}
+
+int bt_ctf_event_set_payload(struct bt_ctf_event *event,
+               const char *name,
+               struct bt_ctf_field *value)
+{
+       int ret = 0;
+
+       if (!event || !value || validate_identifier(name)) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_structure_set_field(event->fields_payload,
+               name, value);
+end:
+       return ret;
+}
+
+
+struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
+               const char *name)
+{
+       struct bt_ctf_field *field = NULL;
+
+       if (!event || !name) {
+               goto end;
+       }
+
+       field = bt_ctf_field_structure_get_field(event->fields_payload, name);
+end:
+       return field;
+}
+
+void bt_ctf_event_get(struct bt_ctf_event *event)
+{
+       if (!event) {
+               return;
+       }
+
+       bt_ctf_ref_get(&event->ref_count);
+}
+
+void bt_ctf_event_put(struct bt_ctf_event *event)
+{
+       if (!event) {
+               return;
+       }
+
+       bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy);
+}
+
+static
+void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_event_class *event_class;
+
+       if (!ref) {
+               return;
+       }
+
+       event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
+       bt_ctf_field_type_put(event_class->context);
+       bt_ctf_field_type_put(event_class->fields);
+       g_free(event_class);
+}
+
+static
+void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_event *event;
+
+       if (!ref) {
+               return;
+       }
+
+       event = container_of(ref, struct bt_ctf_event,
+               ref_count);
+       bt_ctf_event_class_put(event->event_class);
+       bt_ctf_field_put(event->context_payload);
+       bt_ctf_field_put(event->fields_payload);
+       g_free(event);
+}
+
+BT_HIDDEN
+void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
+{
+       assert(event_class);
+       event_class->frozen = 1;
+       bt_ctf_field_type_freeze(event_class->context);
+       bt_ctf_field_type_freeze(event_class->fields);
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
+               uint32_t id)
+{
+       int ret = 0;
+
+       if (event_class->id_set && id != event_class->id) {
+               ret = -1;
+               goto end;
+       }
+
+       event_class->id = id;
+       event_class->id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+uint32_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
+{
+       assert(event_class);
+       return event_class->id;
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
+               uint32_t id)
+{
+       int ret = 0;
+
+       assert(event_class);
+       if (event_class->stream_id_set && id != event_class->stream_id) {
+               ret = -1;
+               goto end;
+       }
+
+       event_class->stream_id = id;
+       event_class->stream_id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
+               struct metadata_context *context)
+{
+       int ret = 0;
+
+       assert(event_class);
+       assert(context);
+       context->current_indentation_level = 1;
+       g_string_assign(context->field_name, "");
+       g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRIu32 ";\n",
+               g_quark_to_string(event_class->name),
+               event_class->id,
+               event_class->stream_id);
+
+       if (event_class->context) {
+               g_string_append(context->string, "\tcontext := ");
+               ret = bt_ctf_field_type_serialize(event_class->context,
+                       context);
+               if (ret) {
+                       goto end;
+               }
+               g_string_append(context->string, ";\n");
+       }
+
+       if (event_class->fields) {
+               g_string_append(context->string, "\tfields := ");
+               ret = bt_ctf_field_type_serialize(event_class->fields, context);
+               if (ret) {
+                       goto end;
+               }
+               g_string_append(context->string, ";\n");
+       }
+
+       g_string_append(context->string, "};\n\n");
+end:
+       context->current_indentation_level = 0;
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_validate(struct bt_ctf_event *event)
+{
+       /* Make sure each field's payload has been set */
+       int ret;
+
+       assert(event);
+       ret = bt_ctf_field_validate(event->fields_payload);
+       if (ret) {
+               goto end;
+       }
+
+       if (event->event_class->context) {
+               ret = bt_ctf_field_validate(event->context_payload);
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_serialize(struct bt_ctf_event *event,
+               struct ctf_stream_pos *pos)
+{
+       int ret = 0;
+
+       assert(event);
+       assert(pos);
+       if (event->context_payload) {
+               ret = bt_ctf_field_serialize(event->context_payload, pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       if (event->fields_payload) {
+               ret = bt_ctf_field_serialize(event->fields_payload, pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_set_timestamp(struct bt_ctf_event *event,
+               uint64_t timestamp)
+{
+       int ret = 0;
+
+       assert(event);
+       if (event->timestamp) {
+               ret = -1;
+               goto end;
+       }
+
+       event->timestamp = timestamp;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event)
+{
+       assert(event);
+       return event->timestamp;
+}
diff --git a/formats/ctf/ir/stream-class.c b/formats/ctf/ir/stream-class.c
new file mode 100644 (file)
index 0000000..3122496
--- /dev/null
@@ -0,0 +1,416 @@
+/*
+ * stream.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-ir/event-internal.h>
+#include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/event-fields-internal.h>
+#include <babeltrace/ctf-writer/stream.h>
+#include <babeltrace/ctf-ir/stream-class-internal.h>
+#include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/compiler.h>
+#include <babeltrace/align.h>
+
+static
+void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
+static
+int init_event_header(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order);
+static
+int init_packet_context(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order);
+
+struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
+{
+       struct bt_ctf_stream_class *stream_class = NULL;
+
+       if (!name || !strlen(name)) {
+               goto error;
+       }
+
+       stream_class = g_new0(struct bt_ctf_stream_class, 1);
+       if (!stream_class) {
+               goto error;
+       }
+
+       stream_class->name = g_string_new(name);
+       stream_class->event_classes = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_event_class_put);
+       if (!stream_class->event_classes) {
+               goto error_destroy;
+       }
+
+       bt_ctf_ref_init(&stream_class->ref_count);
+       return stream_class;
+
+error_destroy:
+       bt_ctf_stream_class_destroy(&stream_class->ref_count);
+       stream_class = NULL;
+error:
+       return stream_class;
+}
+
+int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_clock *clock)
+{
+       int ret = 0;
+
+       if (!stream_class || !clock || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (stream_class->clock) {
+               bt_ctf_clock_put(stream_class->clock);
+       }
+
+       stream_class->clock = clock;
+       bt_ctf_clock_get(clock);
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_add_event_class(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_event_class *event_class)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_class) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Check for duplicate event classes */
+       struct search_query query = { .value = event_class, .found = 0 };
+       g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
+       if (query.found) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_event_class_set_id(event_class,
+               stream_class->next_event_id++)) {
+               /* The event is already associated to a stream class */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_event_class_get(event_class);
+       g_ptr_array_add(stream_class->event_classes, event_class);
+end:
+       return ret;
+}
+
+void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       bt_ctf_ref_get(&stream_class->ref_count);
+}
+
+void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
+}
+
+BT_HIDDEN
+void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       stream_class->frozen = 1;
+       bt_ctf_clock_freeze(stream_class->clock);
+       g_ptr_array_foreach(stream_class->event_classes,
+               (GFunc)bt_ctf_event_class_freeze, NULL);
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
+       uint32_t id)
+{
+       int ret = 0;
+
+       if (!stream_class ||
+               (stream_class->id_set && (id != stream_class->id))) {
+               ret = -1;
+               goto end;
+       }
+
+       stream_class->id = id;
+       stream_class->id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
+       enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+
+       ret = init_packet_context(stream_class, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = init_event_header(stream_class, byte_order);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
+               struct metadata_context *context)
+{
+       int ret = 0;
+       size_t i;
+
+       g_string_assign(context->field_name, "");
+       context->current_indentation_level = 1;
+       if (!stream_class->id_set) {
+               ret = -1;
+               goto end;
+       }
+
+       g_string_append_printf(context->string,
+               "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
+               stream_class->id);
+       ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
+               context);
+       if (ret) {
+               goto end;
+       }
+
+       g_string_append(context->string, ";\n\n\tpacket.context := ");
+       ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
+               context);
+       if (ret) {
+               goto end;
+       }
+
+       if (stream_class->event_context_type) {
+               g_string_append(context->string, ";\n\n\tevent.context := ");
+               ret = bt_ctf_field_type_serialize(
+                       stream_class->event_context_type, context);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       g_string_append(context->string, ";\n};\n\n");
+
+       /* Assign this stream's ID to every event and serialize them */
+       g_ptr_array_foreach(stream_class->event_classes,
+               (GFunc) bt_ctf_event_class_set_stream_id,
+               GUINT_TO_POINTER(stream_class->id));
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *event_class =
+                       stream_class->event_classes->pdata[i];
+
+               ret = bt_ctf_event_class_set_stream_id(event_class,
+                       stream_class->id);
+               if (ret) {
+                       goto end;
+               }
+
+               ret = bt_ctf_event_class_serialize(event_class, context);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       context->current_indentation_level = 0;
+       return ret;
+}
+
+static
+void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_stream_class *stream_class;
+
+       if (!ref) {
+               return;
+       }
+
+       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
+       bt_ctf_clock_put(stream_class->clock);
+
+       if (stream_class->event_classes) {
+               g_ptr_array_free(stream_class->event_classes, TRUE);
+       }
+
+       if (stream_class->name) {
+               g_string_free(stream_class->name, TRUE);
+       }
+
+       bt_ctf_field_type_put(stream_class->event_header_type);
+       bt_ctf_field_put(stream_class->event_header);
+       bt_ctf_field_type_put(stream_class->packet_context_type);
+       bt_ctf_field_put(stream_class->packet_context);
+       bt_ctf_field_type_put(stream_class->event_context_type);
+       bt_ctf_field_put(stream_class->event_context);
+       g_free(stream_class);
+}
+
+static
+int init_event_header(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *event_header_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *_uint32_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
+       struct bt_ctf_field_type *_uint64_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
+
+       if (!event_header_type) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_header_type,
+               _uint32_t, "id");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_header_type,
+               _uint64_t, "timestamp");
+       if (ret) {
+               goto end;
+       }
+
+       stream_class->event_header_type = event_header_type;
+       stream_class->event_header = bt_ctf_field_create(
+               stream_class->event_header_type);
+       if (!stream_class->event_header) {
+               ret = -1;
+       }
+end:
+       if (ret) {
+               bt_ctf_field_type_put(event_header_type);
+       }
+
+       bt_ctf_field_type_put(_uint32_t);
+       bt_ctf_field_type_put(_uint64_t);
+       return ret;
+}
+
+static
+int init_packet_context(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *packet_context_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *_uint64_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
+
+       if (!packet_context_type) {
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * We create a stream packet context as proposed in the CTF
+        * specification.
+        */
+       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "timestamp_begin");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "timestamp_end");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "content_size");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "packet_size");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "events_discarded");
+       if (ret) {
+               goto end;
+       }
+
+       stream_class->packet_context_type = packet_context_type;
+       stream_class->packet_context = bt_ctf_field_create(packet_context_type);
+       if (!stream_class->packet_context) {
+               ret = -1;
+       }
+end:
+       if (ret) {
+               bt_ctf_field_type_put(packet_context_type);
+               goto end;
+       }
+
+       bt_ctf_field_type_put(_uint64_t);
+       return ret;
+}
index 84ac03d5cc8cb9d8ae80963f57348474e7c98ffb..6f791f555c888f324a3c7b77030a3812a4f36f1a 100644 (file)
@@ -4,11 +4,7 @@ noinst_LTLIBRARIES = libctf-writer.la
 
 libctf_writer_la_SOURCES = \
        writer.c \
-       clock.c \
        stream.c \
-       event-types.c \
-       event-fields.c \
-       event.c \
        functor.c
 
 libctf_writer_la_LIBADD = \
diff --git a/formats/ctf/writer/clock.c b/formats/ctf/writer/clock.c
deleted file mode 100644 (file)
index d8df956..0000000
+++ /dev/null
@@ -1,264 +0,0 @@
-/*
- * clock.c
- *
- * Babeltrace CTF Writer
- *
- * Copyright 2013 EfficiOS Inc.
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * 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 <babeltrace/ctf-writer/clock.h>
-#include <babeltrace/ctf-ir/clock-internal.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
-#include <babeltrace/compiler.h>
-#include <inttypes.h>
-
-static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
-
-struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
-{
-       struct bt_ctf_clock *clock = NULL;
-
-       if (validate_identifier(name)) {
-               goto error;
-       }
-
-       clock = g_new0(struct bt_ctf_clock, 1);
-       if (!clock) {
-               goto error;
-       }
-
-       clock->name = g_string_new(name);
-       if (!clock->name) {
-               goto error_destroy;
-       }
-
-       clock->description = g_string_new(NULL);
-       if (!clock->description) {
-               goto error_destroy;
-       }
-
-       clock->precision = 1;
-       clock->frequency = 1000000000;
-       uuid_generate(clock->uuid);
-       bt_ctf_ref_init(&clock->ref_count);
-       return clock;
-error_destroy:
-       bt_ctf_clock_destroy(&clock->ref_count);
-error:
-       clock = NULL;
-       return clock;
-}
-
-int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
-{
-       int ret = 0;
-
-       if (!clock || !desc || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->description = g_string_assign(clock->description, desc);
-       ret = clock->description ? 0 : -1;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
-{
-       int ret = 0;
-
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->frequency = freq;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
-{
-       int ret = 0;
-
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->precision = precision;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
-{
-       int ret = 0;
-
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->offset_s = offset_s;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
-{
-       int ret = 0;
-
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->offset = offset;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
-{
-       int ret = 0;
-
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->absolute = !!is_absolute;
-end:
-       return ret;
-}
-
-int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
-{
-       int ret = 0;
-
-       /* Timestamps are strictly monotonic */
-       if (!clock || time < clock->time) {
-               ret = -1;
-               goto end;
-       }
-
-       clock->time = time;
-end:
-       return ret;
-}
-
-void bt_ctf_clock_get(struct bt_ctf_clock *clock)
-{
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_get(&clock->ref_count);
-}
-
-void bt_ctf_clock_put(struct bt_ctf_clock *clock)
-{
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
-}
-
-BT_HIDDEN
-void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
-{
-       if (!clock) {
-               return;
-       }
-
-       clock->frozen = 1;
-}
-
-BT_HIDDEN
-void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
-               struct metadata_context *context)
-{
-       unsigned char *uuid;
-
-       if (!clock || !context) {
-               return;
-       }
-
-       uuid = clock->uuid;
-       g_string_append(context->string, "clock {\n");
-       g_string_append_printf(context->string, "\tname = %s;\n",
-               clock->name->str);
-       g_string_append_printf(context->string,
-               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
-               uuid[0], uuid[1], uuid[2], uuid[3],
-               uuid[4], uuid[5], uuid[6], uuid[7],
-               uuid[8], uuid[9], uuid[10], uuid[11],
-               uuid[12], uuid[13], uuid[14], uuid[15]);
-       if (clock->description->len) {
-               g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
-                       clock->description->str);
-       }
-
-       g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
-               clock->frequency);
-       g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
-               clock->precision);
-       g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
-               clock->offset_s);
-       g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
-               clock->offset);
-       g_string_append_printf(context->string, "\tabsolute = %s;\n",
-               clock->absolute ? "TRUE" : "FALSE");
-       g_string_append(context->string, "};\n\n");
-}
-
-BT_HIDDEN
-uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
-{
-       return clock ? clock->time : 0;
-}
-
-static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_clock *clock;
-
-       if (!ref) {
-               return;
-       }
-
-       clock = container_of(ref, struct bt_ctf_clock, ref_count);
-       if (clock->name) {
-               g_string_free(clock->name, TRUE);
-       }
-
-       if (clock->description) {
-               g_string_free(clock->description, TRUE);
-       }
-
-       g_free(clock);
-}
diff --git a/formats/ctf/writer/event-fields.c b/formats/ctf/writer/event-fields.c
deleted file mode 100644 (file)
index 17616a5..0000000
+++ /dev/null
@@ -1,1257 +0,0 @@
-/*
- * event-fields.c
- *
- * Babeltrace CTF Writer
- *
- * Copyright 2013 EfficiOS Inc.
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * 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 <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-ir/event-fields-internal.h>
-#include <babeltrace/ctf-ir/event-types-internal.h>
-#include <babeltrace/compiler.h>
-
-#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_ctf_ref *);
-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 *field);
-static
-int bt_ctf_field_structure_validate(struct bt_ctf_field *field);
-static
-int bt_ctf_field_variant_validate(struct bt_ctf_field *field);
-static
-int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field);
-static
-int bt_ctf_field_array_validate(struct bt_ctf_field *field);
-static
-int bt_ctf_field_sequence_validate(struct bt_ctf_field *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 increase_packet_size(struct ctf_stream_pos *pos);
-
-static
-struct bt_ctf_field *(*field_create_funcs[])(
-               struct bt_ctf_field_type *) = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
-       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_floating_point_create,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_array_create,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create,
-       [CTF_TYPE_STRING] = bt_ctf_field_string_create,
-};
-
-static
-void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
-       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_floating_point_destroy,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy,
-       [CTF_TYPE_STRING] = bt_ctf_field_string_destroy,
-};
-
-static
-int (*field_validate_funcs[])(struct bt_ctf_field *) = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
-       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
-       [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate,
-       [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
-};
-
-static
-int (*field_serialize_funcs[])(struct bt_ctf_field *,
-               struct ctf_stream_pos *) = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
-       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_floating_point_serialize,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize,
-       [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
-};
-
-struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
-{
-       struct bt_ctf_field *field = NULL;
-       enum ctf_type_id type_id;
-
-       if (!type) {
-               goto error;
-       }
-
-       type_id = bt_ctf_field_type_get_type_id(type);
-       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES ||
-               bt_ctf_field_type_validate(type)) {
-               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_ctf_field_type_get(type);
-       bt_ctf_ref_init(&field->ref_count);
-       field->type = type;
-error:
-       return field;
-}
-
-void bt_ctf_field_get(struct bt_ctf_field *field)
-{
-       if (field) {
-               bt_ctf_ref_get(&field->ref_count);
-       }
-}
-
-void bt_ctf_field_put(struct bt_ctf_field *field)
-{
-       if (field) {
-               bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
-       }
-}
-
-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) !=
-               CTF_TYPE_INTEGER) {
-               ret = -1;
-               goto end;
-       }
-
-       length_type = container_of(length_field->type,
-               struct bt_ctf_field_type_integer, parent);
-       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_ctf_field_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_ctf_field_put);
-       g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
-       bt_ctf_field_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_structure *structure_type;
-       struct bt_ctf_field_type *field_type;
-       size_t index;
-
-       if (!field || !name ||
-               bt_ctf_field_type_get_type_id(field->type) !=
-                       CTF_TYPE_STRUCT) {
-               goto error;
-       }
-
-       field_quark = g_quark_from_string(name);
-       structure = container_of(field, struct bt_ctf_field_structure, parent);
-       structure_type = container_of(field->type,
-               struct bt_ctf_field_type_structure, parent);
-       field_type = bt_ctf_field_type_structure_get_type(structure_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_ctf_field_get(new_field);
-error:
-       return new_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_structure *structure_type;
-       struct bt_ctf_field_type *expected_field_type;
-       size_t index;
-
-       if (!field || !name || !value ||
-               bt_ctf_field_type_get_type_id(field->type) !=
-                       CTF_TYPE_STRUCT) {
-               ret = -1;
-               goto end;
-       }
-
-       field_quark = g_quark_from_string(name);
-       structure = container_of(field, struct bt_ctf_field_structure, parent);
-       structure_type = container_of(field->type,
-               struct bt_ctf_field_type_structure, parent);
-       expected_field_type = bt_ctf_field_type_structure_get_type(
-               structure_type, name);
-       if (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_ctf_field_put(structure->fields->pdata[index]);
-       }
-
-       structure->fields->pdata[index] = value;
-       bt_ctf_field_get(value);
-end:
-       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_array *array;
-       struct bt_ctf_field_type_array *array_type;
-       struct bt_ctf_field_type *field_type;
-
-       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
-               CTF_TYPE_ARRAY) {
-               goto end;
-       }
-
-       array = container_of(field, struct bt_ctf_field_array, parent);
-       if (index >= array->elements->len) {
-               goto end;
-       }
-
-       array_type = container_of(field->type, struct bt_ctf_field_type_array,
-               parent);
-       field_type = bt_ctf_field_type_array_get_element_type(array_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);
-       bt_ctf_field_get(new_field);
-       array->elements->pdata[(size_t)index] = new_field;
-end:
-       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_sequence *sequence;
-       struct bt_ctf_field_type_sequence *sequence_type;
-       struct bt_ctf_field_type *field_type;
-
-       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
-               CTF_TYPE_SEQUENCE) {
-               goto end;
-       }
-
-       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
-       if (!sequence->elements || sequence->elements->len <= index) {
-               goto end;
-       }
-
-       sequence_type = container_of(field->type,
-               struct bt_ctf_field_type_sequence, parent);
-       field_type = bt_ctf_field_type_sequence_get_element_type(sequence_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);
-       bt_ctf_field_get(new_field);
-       sequence->elements->pdata[(size_t)index] = new_field;
-end:
-       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) !=
-                       CTF_TYPE_VARIANT ||
-               bt_ctf_field_type_get_type_id(tag_field->type) !=
-                       CTF_TYPE_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(variant->tag)) {
-               goto end;
-       }
-
-       tag_enum_value = tag_enum_integer->definition.value._signed;
-       field_type = bt_ctf_field_type_variant_get_field_type(variant_type,
-               tag_enum_value);
-       if (!field_type) {
-               goto end;
-       }
-
-       new_field = bt_ctf_field_create(field_type);
-       if (!new_field) {
-               goto end;
-       }
-
-       bt_ctf_field_put(variant->tag);
-       bt_ctf_field_put(variant->payload);
-       bt_ctf_field_get(new_field);
-       bt_ctf_field_get(tag_field);
-       variant->tag = tag_field;
-       variant->payload = new_field;
-end:
-       bt_ctf_field_put(tag_enum);
-       return new_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) {
-               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_ctf_field_get(container);
-end:
-       return container;
-}
-
-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) !=
-                       CTF_TYPE_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_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) !=
-                       CTF_TYPE_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_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) !=
-                       CTF_TYPE_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;
-}
-
-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) !=
-                       CTF_TYPE_STRING) {
-               ret = -1;
-               goto end;
-       }
-
-       string = container_of(field, struct bt_ctf_field_string, parent);
-       if (string->payload) {
-               g_string_free(string->payload, TRUE);
-       }
-
-       string->payload = g_string_new(value);
-       string->parent.payload_set = 1;
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_field_validate(struct bt_ctf_field *field)
-{
-       int ret = 0;
-       enum 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 <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = field_validate_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 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 <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = field_serialize_funcs[type_id](field, pos);
-end:
-       return ret;
-}
-
-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 || !structure_type->fields->len) {
-               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_ctf_ref *ref)
-{
-       struct bt_ctf_field *field;
-       struct bt_ctf_field_type *type;
-       enum ctf_type_id type_id;
-
-       if (!ref) {
-               return;
-       }
-
-       field = container_of(ref, struct bt_ctf_field, ref_count);
-       type = field->type;
-       type_id = bt_ctf_field_type_get_type_id(type);
-       if (type_id <= CTF_TYPE_UNKNOWN ||
-               type_id >= NR_CTF_TYPES) {
-               return;
-       }
-
-       field_destroy_funcs[type_id](field);
-       if (type) {
-               bt_ctf_field_type_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_ctf_field_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_ctf_field_put(variant->tag);
-       bt_ctf_field_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);
-       g_ptr_array_free(sequence->elements, TRUE);
-       bt_ctf_field_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);
-       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_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_ctf_field_put(character);
-       bt_ctf_field_type_put(character_type);
-       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;
-       ret = posix_fallocate(pos->fd, pos->mmap_offset,
-               pos->packet_size / CHAR_BIT);
-       if (ret) {
-               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/writer/event-types.c b/formats/ctf/writer/event-types.c
deleted file mode 100644 (file)
index 0c8eddc..0000000
+++ /dev/null
@@ -1,1445 +0,0 @@
-/*
- * event-types.c
- *
- * Babeltrace CTF Writer
- *
- * Copyright 2013 EfficiOS Inc.
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * 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 <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-ir/event-types-internal.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
-#include <babeltrace/compiler.h>
-#include <babeltrace/endian.h>
-#include <float.h>
-#include <inttypes.h>
-#include <stdlib.h>
-
-struct range_overlap_query {
-       int64_t range_start, range_end;
-       int overlaps;
-       GQuark mapping_name;
-};
-
-static
-void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *);
-static
-void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *);
-
-static
-void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
-       [CTF_TYPE_ENUM] =
-               bt_ctf_field_type_enumeration_destroy,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_type_floating_point_destroy,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
-       [CTF_TYPE_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[] = {
-       [CTF_TYPE_INTEGER] = generic_field_type_freeze,
-       [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze,
-       [CTF_TYPE_FLOAT] = generic_field_type_freeze,
-       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
-       [CTF_TYPE_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[] = {
-       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize,
-       [CTF_TYPE_ENUM] =
-               bt_ctf_field_type_enumeration_serialize,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_type_floating_point_serialize,
-       [CTF_TYPE_STRUCT] =
-               bt_ctf_field_type_structure_serialize,
-       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize,
-       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize,
-       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
-       [CTF_TYPE_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);
-static
-void bt_ctf_field_type_floating_point_set_byte_order(
-               struct bt_ctf_field_type *, int byte_order);
-
-static
-void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
-       int) = {
-       [CTF_TYPE_INTEGER] =
-               bt_ctf_field_type_integer_set_byte_order,
-       [CTF_TYPE_FLOAT] =
-               bt_ctf_field_type_floating_point_set_byte_order,
-       [CTF_TYPE_ENUM ... CTF_TYPE_SEQUENCE] = NULL,
-};
-
-
-static
-void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
-{
-       g_free(mapping);
-}
-
-static
-void destroy_structure_field(struct structure_field *field)
-{
-       if (field->type) {
-               bt_ctf_field_type_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 <= overlap_query->range_end
-               && overlap_query->range_start <= mapping->range_end) {
-               overlap_query->overlaps = 1;
-               overlap_query->mapping_name = mapping->string;
-       }
-
-       overlap_query->overlaps |=
-               mapping->string == overlap_query->mapping_name;
-}
-
-static
-void bt_ctf_field_type_init(struct bt_ctf_field_type *type)
-{
-       enum ctf_type_id type_id = type->declaration->id;
-       int ret;
-
-       assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
-               (type_id < NR_CTF_TYPES));
-
-       bt_ctf_ref_init(&type->ref_count);
-       type->freeze = type_freeze_funcs[type_id];
-       type->serialize = type_serialize_funcs[type_id];
-       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_ctf_field_type_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);
-       bt_ctf_field_type_freeze(field_type);
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_field_type_validate(struct bt_ctf_field_type *type)
-{
-       int ret = 0;
-
-       if (!type) {
-               ret = -1;
-               goto end;
-       }
-
-       if (type->declaration->id == CTF_TYPE_ENUM) {
-               struct bt_ctf_field_type_enumeration *enumeration =
-                       container_of(type, struct bt_ctf_field_type_enumeration,
-                       parent);
-
-               ret = enumeration->entries->len ? 0 : -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 > 64) {
-               return NULL;
-       }
-
-       integer->parent.declaration = &integer->declaration.p;
-       integer->parent.declaration->id = CTF_TYPE_INTEGER;
-       integer->declaration.len = size;
-       integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL;
-       integer->declaration.encoding = CTF_STRING_NONE;
-       bt_ctf_field_type_init(&integer->parent);
-       return &integer->parent;
-}
-
-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 != CTF_TYPE_INTEGER) {
-               ret = -1;
-               goto end;
-       }
-
-       integer = container_of(type, struct bt_ctf_field_type_integer, parent);
-       if (is_signed && integer->declaration.len <= 1) {
-               ret = -1;
-               goto end;
-       }
-
-       integer->declaration.signedness = !!is_signed;
-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 != CTF_TYPE_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;
-}
-
-int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
-               enum ctf_string_encoding encoding)
-{
-       int ret = 0;
-       struct bt_ctf_field_type_integer *integer;
-
-       if (!type || type->frozen ||
-               (type->declaration->id != CTF_TYPE_INTEGER) ||
-               (encoding < CTF_STRING_NONE) ||
-               (encoding >= CTF_STRING_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_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;
-       }
-
-       enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
-       if (!enumeration) {
-               goto error;
-       }
-
-       enumeration->parent.declaration = &enumeration->declaration.p;
-       enumeration->parent.declaration->id = CTF_TYPE_ENUM;
-       bt_ctf_field_type_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);
-       return &enumeration->parent;
-error:
-       g_free(enumeration);
-       return NULL;
-}
-
-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 != CTF_TYPE_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 = range_start,
-               .range_end = 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 = range_start,
-               .range_end = range_end, .string = mapping_name};
-       g_ptr_array_add(enumeration->entries, mapping);
-error_free:
-       free(escaped_string);
-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 = CTF_TYPE_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);
-end:
-       return floating_point ? &floating_point->parent : NULL;
-}
-
-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 != CTF_TYPE_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_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 != CTF_TYPE_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 = CTF_TYPE_STRUCT;
-       bt_ctf_field_type_init(&structure->parent);
-       structure->fields = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)destroy_structure_field);
-       structure->field_name_to_index = g_hash_table_new(NULL, NULL);
-       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 ||
-               validate_identifier(field_name) ||
-               (type->declaration->id != CTF_TYPE_STRUCT) ||
-               bt_ctf_field_type_validate(field_type)) {
-               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;
-       }
-
-       if (type->declaration->alignment < field_type->declaration->alignment) {
-               type->declaration->alignment =
-                       field_type->declaration->alignment;
-       }
-end:
-       return ret;
-}
-
-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 (!enum_tag || validate_identifier(tag_name) ||
-               (enum_tag->declaration->id != CTF_TYPE_ENUM)) {
-               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 = CTF_TYPE_VARIANT;
-       variant->tag_name = g_string_new(tag_name);
-       bt_ctf_field_type_init(&variant->parent);
-       variant->field_name_to_index = g_hash_table_new(NULL, NULL);
-       variant->fields = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)destroy_structure_field);
-       bt_ctf_field_type_get(enum_tag);
-       variant->tag = container_of(enum_tag,
-               struct bt_ctf_field_type_enumeration, parent);
-       return &variant->parent;
-error:
-       return NULL;
-}
-
-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;
-       int name_found = 0;
-       struct bt_ctf_field_type_variant *variant;
-       GQuark field_name_quark = g_quark_from_string(field_name);
-
-       if (!type || !field_type || type->frozen ||
-               validate_identifier(field_name) ||
-               (type->declaration->id != CTF_TYPE_VARIANT) ||
-               bt_ctf_field_type_validate(field_type)) {
-               ret = -1;
-               goto end;
-       }
-
-       variant = container_of(type, struct bt_ctf_field_type_variant, parent);
-       /* 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 || 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_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 ||
-               bt_ctf_field_type_validate(element_type)) {
-               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 = CTF_TYPE_ARRAY;
-       bt_ctf_field_type_init(&array->parent);
-       bt_ctf_field_type_get(element_type);
-       array->element_type = element_type;
-       array->length = length;
-       array->parent.declaration->alignment =
-               element_type->declaration->alignment;
-       return &array->parent;
-error:
-       return NULL;
-}
-
-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 || validate_identifier(length_field_name) ||
-               bt_ctf_field_type_validate(element_type)) {
-               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 = CTF_TYPE_SEQUENCE;
-       bt_ctf_field_type_init(&sequence->parent);
-       bt_ctf_field_type_get(element_type);
-       sequence->element_type = element_type;
-       sequence->length_field_name = g_string_new(length_field_name);
-       sequence->parent.declaration->alignment =
-               element_type->declaration->alignment;
-       return &sequence->parent;
-error:
-       return NULL;
-}
-
-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 = CTF_TYPE_STRING;
-       bt_ctf_field_type_init(&string->parent);
-       string->declaration.encoding = CTF_STRING_UTF8;
-       string->parent.declaration->alignment = CHAR_BIT;
-       return &string->parent;
-}
-
-int bt_ctf_field_type_string_set_encoding(
-               struct bt_ctf_field_type *type,
-               enum ctf_string_encoding encoding)
-{
-       int ret = 0;
-       struct bt_ctf_field_type_string *string;
-
-       if (!type || type->declaration->id != CTF_TYPE_STRING ||
-               (encoding != CTF_STRING_UTF8 &&
-               encoding != CTF_STRING_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_set_alignment(struct bt_ctf_field_type *type,
-               unsigned int alignment)
-{
-       int ret = 0;
-
-       /* Alignment must be bit-aligned (1) or byte aligned */
-       if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) {
-               ret = -1;
-               goto end;
-       }
-
-       if (type->declaration->id == CTF_TYPE_STRING &&
-               alignment != CHAR_BIT) {
-               ret = -1;
-               goto end;
-       }
-
-       type->declaration->alignment = alignment;
-       ret = 0;
-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 ctf_type_id type_id;
-
-       if (!type || type->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       type_id = type->declaration->id;
-       switch (byte_order) {
-       case BT_CTF_BYTE_ORDER_NATIVE:
-               internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN ?
-                       LITTLE_ENDIAN : BIG_ENDIAN);
-               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;
-       }
-
-       if (set_byte_order_funcs[type_id]) {
-               set_byte_order_funcs[type_id](type, internal_byte_order);
-       }
-end:
-       return ret;
-}
-
-void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
-{
-       if (!type) {
-               return;
-       }
-
-       bt_ctf_ref_get(&type->ref_count);
-}
-
-void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
-{
-       enum ctf_type_id type_id;
-
-       if (!type) {
-               return;
-       }
-
-       type_id = type->declaration->id;
-       assert(type_id > CTF_TYPE_UNKNOWN && type_id < NR_CTF_TYPES);
-       bt_ctf_ref_put(&type->ref_count, type_destroy_funcs[type_id]);
-}
-
-BT_HIDDEN
-void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
-{
-       if (!type) {
-               return;
-       }
-
-       type->freeze(type);
-}
-
-BT_HIDDEN
-enum ctf_type_id bt_ctf_field_type_get_type_id(
-               struct bt_ctf_field_type *type)
-{
-       if (!type) {
-               return CTF_TYPE_UNKNOWN;
-       }
-
-       return type->declaration->id;
-}
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_structure_get_type(
-               struct bt_ctf_field_type_structure *structure,
-               const char *name)
-{
-       struct bt_ctf_field_type *type = NULL;
-       struct structure_field *field;
-       GQuark name_quark = g_quark_try_string(name);
-       size_t index;
-
-       if (!name_quark) {
-               goto end;
-       }
-
-       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];
-       type = field->type;
-end:
-       return type;
-}
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
-               struct bt_ctf_field_type_array *array)
-{
-       assert(array);
-       return array->element_type;
-}
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
-               struct bt_ctf_field_type_sequence *sequence)
-{
-       assert(sequence);
-       return sequence->element_type;
-}
-
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type(
-               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 = tag_value,
-               .range_end = 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
-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;
-       }
-
-       ret = type->serialize(type, context);
-end:
-       return ret;
-}
-
-static
-void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_integer *integer;
-
-       if (!ref) {
-               return;
-       }
-
-       integer = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_integer, parent);
-       g_free(integer);
-}
-
-static
-void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_enumeration *enumeration;
-
-       if (!ref) {
-               return;
-       }
-
-       enumeration = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_enumeration, parent);
-       g_ptr_array_free(enumeration->entries, TRUE);
-       bt_ctf_field_type_put(enumeration->container);
-       g_free(enumeration);
-}
-
-static
-void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_floating_point *floating_point;
-
-       if (!ref) {
-               return;
-       }
-
-       floating_point = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_floating_point, parent);
-       g_free(floating_point);
-}
-
-static
-void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_structure *structure;
-
-       if (!ref) {
-               return;
-       }
-
-       structure = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_structure, parent);
-       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_ref *ref)
-{
-       struct bt_ctf_field_type_variant *variant;
-
-       if (!ref) {
-               return;
-       }
-
-       variant = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_variant, parent);
-       g_ptr_array_free(variant->fields, TRUE);
-       g_hash_table_destroy(variant->field_name_to_index);
-       g_string_free(variant->tag_name, TRUE);
-       bt_ctf_field_type_put(&variant->tag->parent);
-       g_free(variant);
-}
-
-static
-void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_array *array;
-
-       if (!ref) {
-               return;
-       }
-
-       array = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_array, parent);
-       bt_ctf_field_type_put(array->element_type);
-       g_free(array);
-}
-
-static
-void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_sequence *sequence;
-
-       if (!ref) {
-               return;
-       }
-
-       sequence = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_sequence, parent);
-       bt_ctf_field_type_put(sequence->element_type);
-       g_string_free(sequence->length_field_name, TRUE);
-       g_free(sequence);
-}
-
-static
-void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_field_type_string *string;
-
-       if (!ref) {
-               return;
-       }
-
-       string = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_string, parent);
-       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);
-
-       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);
-
-       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);
-
-       generic_field_type_freeze(type);
-       bt_ctf_field_type_freeze(sequence_type->element_type);
-}
-
-static
-const char *get_encoding_string(enum ctf_string_encoding encoding)
-{
-       const char *encoding_string;
-
-       switch (encoding) {
-       case CTF_STRING_NONE:
-               encoding_string = "none";
-               break;
-       case CTF_STRING_ASCII:
-               encoding_string = "ASCII";
-               break;
-       case CTF_STRING_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);
-
-       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));
-       return 0;
-}
-
-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);
-
-       ret = bt_ctf_field_type_validate(type);
-       if (ret) {
-               goto end;
-       }
-
-       g_string_append(context->string, "enum : ");
-       ret = bt_ctf_field_type_serialize(enumeration->container, context);
-       if (ret) {
-               goto end;
-       }
-
-       g_string_append(context->string, " { ");
-       for (entry = 0; entry < enumeration->entries->len; entry++) {
-               struct enumeration_mapping *mapping =
-                       enumeration->entries->pdata[entry];
-
-               if (mapping->range_start == mapping->range_end) {
-                       g_string_append_printf(context->string,
-                               "\"%s\" = %" PRId64,
-                               g_quark_to_string(mapping->string),
-                               mapping->range_start);
-               } else {
-                       g_string_append_printf(context->string,
-                               "\"%s\" = %" PRId64 " ... %" PRId64,
-                               g_quark_to_string(mapping->string),
-                               mapping->range_start, mapping->range_end);
-               }
-
-               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, "");
-       }
-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("");
-       g_string_append_printf(context->string,
-               "variant <%s> {\n", variant->tag_name->str);
-       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
-void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
-               int byte_order)
-{
-       struct bt_ctf_field_type_integer *integer_type = container_of(type,
-               struct bt_ctf_field_type_integer, parent);
-
-       integer_type->declaration.byte_order = byte_order;
-}
-
-static
-void bt_ctf_field_type_floating_point_set_byte_order(
-               struct bt_ctf_field_type *type, int byte_order)
-{
-       struct bt_ctf_field_type_floating_point *floating_point_type =
-               container_of(type, struct bt_ctf_field_type_floating_point,
-               parent);
-
-       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;
-}
diff --git a/formats/ctf/writer/event.c b/formats/ctf/writer/event.c
deleted file mode 100644 (file)
index c62317d..0000000
+++ /dev/null
@@ -1,368 +0,0 @@
-/*
- * event.c
- *
- * Babeltrace CTF Writer
- *
- * Copyright 2013 EfficiOS Inc.
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * 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 <babeltrace/ctf-writer/event.h>
-#include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-ir/event-fields-internal.h>
-#include <babeltrace/ctf-ir/event-types-internal.h>
-#include <babeltrace/ctf-ir/event-internal.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
-#include <babeltrace/compiler.h>
-
-static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref);
-static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref);
-
-struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
-{
-       struct bt_ctf_event_class *event_class = NULL;
-
-       if (validate_identifier(name)) {
-               goto end;
-       }
-
-       event_class = g_new0(struct bt_ctf_event_class, 1);
-       if (!event_class) {
-               goto end;
-       }
-
-       bt_ctf_ref_init(&event_class->ref_count);
-       event_class->name = g_quark_from_string(name);
-end:
-       return event_class;
-}
-
-int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
-               struct bt_ctf_field_type *type,
-               const char *name)
-{
-       int ret = 0;
-
-       if (!event_class || !type || validate_identifier(name) ||
-               event_class->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       if (!event_class->fields) {
-               event_class->fields = bt_ctf_field_type_structure_create();
-               if (!event_class->fields) {
-                       ret = -1;
-                       goto end;
-               }
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(event_class->fields,
-               type, name);
-end:
-       return ret;
-}
-
-void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
-{
-       if (!event_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event_class->ref_count);
-}
-
-void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
-{
-       if (!event_class) {
-               return;
-       }
-
-       bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy);
-}
-
-struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
-{
-       struct bt_ctf_event *event = NULL;
-
-       if (!event_class) {
-               goto end;
-       }
-
-       event = g_new0(struct bt_ctf_event, 1);
-       if (!event) {
-               goto end;
-       }
-
-       bt_ctf_ref_init(&event->ref_count);
-       bt_ctf_event_class_get(event_class);
-       bt_ctf_event_class_freeze(event_class);
-       event->event_class = event_class;
-       event->context_payload = bt_ctf_field_create(event_class->context);
-       event->fields_payload = bt_ctf_field_create(event_class->fields);
-end:
-       return event;
-}
-
-int bt_ctf_event_set_payload(struct bt_ctf_event *event,
-               const char *name,
-               struct bt_ctf_field *value)
-{
-       int ret = 0;
-
-       if (!event || !value || validate_identifier(name)) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = bt_ctf_field_structure_set_field(event->fields_payload,
-               name, value);
-end:
-       return ret;
-}
-
-
-struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
-               const char *name)
-{
-       struct bt_ctf_field *field = NULL;
-
-       if (!event || !name) {
-               goto end;
-       }
-
-       field = bt_ctf_field_structure_get_field(event->fields_payload, name);
-end:
-       return field;
-}
-
-void bt_ctf_event_get(struct bt_ctf_event *event)
-{
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event->ref_count);
-}
-
-void bt_ctf_event_put(struct bt_ctf_event *event)
-{
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy);
-}
-
-static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_event_class *event_class;
-
-       if (!ref) {
-               return;
-       }
-
-       event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
-       bt_ctf_field_type_put(event_class->context);
-       bt_ctf_field_type_put(event_class->fields);
-       g_free(event_class);
-}
-
-static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_event *event;
-
-       if (!ref) {
-               return;
-       }
-
-       event = container_of(ref, struct bt_ctf_event,
-               ref_count);
-       bt_ctf_event_class_put(event->event_class);
-       bt_ctf_field_put(event->context_payload);
-       bt_ctf_field_put(event->fields_payload);
-       g_free(event);
-}
-
-BT_HIDDEN
-void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
-{
-       assert(event_class);
-       event_class->frozen = 1;
-       bt_ctf_field_type_freeze(event_class->context);
-       bt_ctf_field_type_freeze(event_class->fields);
-}
-
-BT_HIDDEN
-int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
-               uint32_t id)
-{
-       int ret = 0;
-
-       if (event_class->id_set && id != event_class->id) {
-               ret = -1;
-               goto end;
-       }
-
-       event_class->id = id;
-       event_class->id_set = 1;
-end:
-       return ret;
-}
-
-BT_HIDDEN
-uint32_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
-{
-       assert(event_class);
-       return event_class->id;
-}
-
-BT_HIDDEN
-int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
-               uint32_t id)
-{
-       int ret = 0;
-
-       assert(event_class);
-       if (event_class->stream_id_set && id != event_class->stream_id) {
-               ret = -1;
-               goto end;
-       }
-
-       event_class->stream_id = id;
-       event_class->stream_id_set = 1;
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
-               struct metadata_context *context)
-{
-       int ret = 0;
-
-       assert(event_class);
-       assert(context);
-       context->current_indentation_level = 1;
-       g_string_assign(context->field_name, "");
-       g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRIu32 ";\n",
-               g_quark_to_string(event_class->name),
-               event_class->id,
-               event_class->stream_id);
-
-       if (event_class->context) {
-               g_string_append(context->string, "\tcontext := ");
-               ret = bt_ctf_field_type_serialize(event_class->context,
-                       context);
-               if (ret) {
-                       goto end;
-               }
-               g_string_append(context->string, ";\n");
-       }
-
-       if (event_class->fields) {
-               g_string_append(context->string, "\tfields := ");
-               ret = bt_ctf_field_type_serialize(event_class->fields, context);
-               if (ret) {
-                       goto end;
-               }
-               g_string_append(context->string, ";\n");
-       }
-
-       g_string_append(context->string, "};\n\n");
-end:
-       context->current_indentation_level = 0;
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_event_validate(struct bt_ctf_event *event)
-{
-       /* Make sure each field's payload has been set */
-       int ret;
-
-       assert(event);
-       ret = bt_ctf_field_validate(event->fields_payload);
-       if (ret) {
-               goto end;
-       }
-
-       if (event->event_class->context) {
-               ret = bt_ctf_field_validate(event->context_payload);
-       }
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_event_serialize(struct bt_ctf_event *event,
-               struct ctf_stream_pos *pos)
-{
-       int ret = 0;
-
-       assert(event);
-       assert(pos);
-       if (event->context_payload) {
-               ret = bt_ctf_field_serialize(event->context_payload, pos);
-               if (ret) {
-                       goto end;
-               }
-       }
-
-       if (event->fields_payload) {
-               ret = bt_ctf_field_serialize(event->fields_payload, pos);
-               if (ret) {
-                       goto end;
-               }
-       }
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_event_set_timestamp(struct bt_ctf_event *event,
-               uint64_t timestamp)
-{
-       int ret = 0;
-
-       assert(event);
-       if (event->timestamp) {
-               ret = -1;
-               goto end;
-       }
-
-       event->timestamp = timestamp;
-end:
-       return ret;
-}
-
-BT_HIDDEN
-uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event)
-{
-       assert(event);
-       return event->timestamp;
-}
index 71a8212eddb2f30839f7ba730de306d4571e9fed..1076dd3eebdc4c0bc6fefa2159fce8e58bc7f2c9 100644 (file)
 static
 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
-static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
-static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
-static
 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
 
-struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
-{
-       struct bt_ctf_stream_class *stream_class = NULL;
-
-       if (!name || !strlen(name)) {
-               goto error;
-       }
-
-       stream_class = g_new0(struct bt_ctf_stream_class, 1);
-       if (!stream_class) {
-               goto error;
-       }
-
-       stream_class->name = g_string_new(name);
-       stream_class->event_classes = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_event_class_put);
-       if (!stream_class->event_classes) {
-               goto error_destroy;
-       }
-
-       bt_ctf_ref_init(&stream_class->ref_count);
-       return stream_class;
-
-error_destroy:
-       bt_ctf_stream_class_destroy(&stream_class->ref_count);
-       stream_class = NULL;
-error:
-       return stream_class;
-}
-
-int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
-               struct bt_ctf_clock *clock)
-{
-       int ret = 0;
-
-       if (!stream_class || !clock || stream_class->frozen) {
-               ret = -1;
-               goto end;
-       }
-
-       if (stream_class->clock) {
-               bt_ctf_clock_put(stream_class->clock);
-       }
-
-       stream_class->clock = clock;
-       bt_ctf_clock_get(clock);
-end:
-       return ret;
-}
-
-int bt_ctf_stream_class_add_event_class(
-               struct bt_ctf_stream_class *stream_class,
-               struct bt_ctf_event_class *event_class)
-{
-       int ret = 0;
-
-       if (!stream_class || !event_class) {
-               ret = -1;
-               goto end;
-       }
-
-       /* Check for duplicate event classes */
-       struct search_query query = { .value = event_class, .found = 0 };
-       g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
-       if (query.found) {
-               ret = -1;
-               goto end;
-       }
-
-       if (bt_ctf_event_class_set_id(event_class,
-               stream_class->next_event_id++)) {
-               /* The event is already associated to a stream class */
-               ret = -1;
-               goto end;
-       }
-
-       bt_ctf_event_class_get(event_class);
-       g_ptr_array_add(stream_class->event_classes, event_class);
-end:
-       return ret;
-}
-
-void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
-{
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&stream_class->ref_count);
-}
-
-void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
-{
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
-}
-
-BT_HIDDEN
-void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
-{
-       if (!stream_class) {
-               return;
-       }
-
-       stream_class->frozen = 1;
-       bt_ctf_clock_freeze(stream_class->clock);
-       g_ptr_array_foreach(stream_class->event_classes,
-               (GFunc)bt_ctf_event_class_freeze, NULL);
-}
-
-BT_HIDDEN
-int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
-       uint32_t id)
-{
-       int ret = 0;
-
-       if (!stream_class ||
-               (stream_class->id_set && (id != stream_class->id))) {
-               ret = -1;
-               goto end;
-       }
-
-       stream_class->id = id;
-       stream_class->id_set = 1;
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
-       enum bt_ctf_byte_order byte_order)
-{
-       int ret = 0;
-
-       ret = init_packet_context(stream_class, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = init_event_header(stream_class, byte_order);
-       if (ret) {
-               goto end;
-       }
-end:
-       return ret;
-}
-
-BT_HIDDEN
-int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
-               struct metadata_context *context)
-{
-       int ret = 0;
-       size_t i;
-
-       g_string_assign(context->field_name, "");
-       context->current_indentation_level = 1;
-       if (!stream_class->id_set) {
-               ret = -1;
-               goto end;
-       }
-
-       g_string_append_printf(context->string,
-               "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
-               stream_class->id);
-       ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
-               context);
-       if (ret) {
-               goto end;
-       }
-
-       g_string_append(context->string, ";\n\n\tpacket.context := ");
-       ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
-               context);
-       if (ret) {
-               goto end;
-       }
-
-       if (stream_class->event_context_type) {
-               g_string_append(context->string, ";\n\n\tevent.context := ");
-               ret = bt_ctf_field_type_serialize(
-                       stream_class->event_context_type, context);
-               if (ret) {
-                       goto end;
-               }
-       }
-
-       g_string_append(context->string, ";\n};\n\n");
-
-       /* Assign this stream's ID to every event and serialize them */
-       g_ptr_array_foreach(stream_class->event_classes,
-               (GFunc) bt_ctf_event_class_set_stream_id,
-               GUINT_TO_POINTER(stream_class->id));
-       for (i = 0; i < stream_class->event_classes->len; i++) {
-               struct bt_ctf_event_class *event_class =
-                       stream_class->event_classes->pdata[i];
-
-               ret = bt_ctf_event_class_set_stream_id(event_class,
-                       stream_class->id);
-               if (ret) {
-                       goto end;
-               }
-
-               ret = bt_ctf_event_class_serialize(event_class, context);
-               if (ret) {
-                       goto end;
-               }
-       }
-end:
-       context->current_indentation_level = 0;
-       return ret;
-}
-
 BT_HIDDEN
 struct bt_ctf_stream *bt_ctf_stream_create(
                struct bt_ctf_stream_class *stream_class)
@@ -531,159 +308,6 @@ void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
        g_free(stream);
 }
 
-static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
-{
-       struct bt_ctf_stream_class *stream_class;
-
-       if (!ref) {
-               return;
-       }
-
-       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
-       bt_ctf_clock_put(stream_class->clock);
-
-       if (stream_class->event_classes) {
-               g_ptr_array_free(stream_class->event_classes, TRUE);
-       }
-
-       if (stream_class->name) {
-               g_string_free(stream_class->name, TRUE);
-       }
-
-       bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_put(stream_class->event_header);
-       bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_put(stream_class->packet_context);
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_put(stream_class->event_context);
-       g_free(stream_class);
-}
-
-static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
-{
-       int ret = 0;
-       struct bt_ctf_field_type *event_header_type =
-               bt_ctf_field_type_structure_create();
-       struct bt_ctf_field_type *_uint32_t =
-               get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
-       struct bt_ctf_field_type *_uint64_t =
-               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
-
-       if (!event_header_type) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(event_header_type,
-               _uint32_t, "id");
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(event_header_type,
-               _uint64_t, "timestamp");
-       if (ret) {
-               goto end;
-       }
-
-       stream_class->event_header_type = event_header_type;
-       stream_class->event_header = bt_ctf_field_create(
-               stream_class->event_header_type);
-       if (!stream_class->event_header) {
-               ret = -1;
-       }
-end:
-       if (ret) {
-               bt_ctf_field_type_put(event_header_type);
-       }
-
-       bt_ctf_field_type_put(_uint32_t);
-       bt_ctf_field_type_put(_uint64_t);
-       return ret;
-}
-
-static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
-{
-       int ret = 0;
-       struct bt_ctf_field_type *packet_context_type =
-               bt_ctf_field_type_structure_create();
-       struct bt_ctf_field_type *_uint64_t =
-               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
-
-       if (!packet_context_type) {
-               ret = -1;
-               goto end;
-       }
-
-       /*
-        * We create a stream packet context as proposed in the CTF
-        * specification.
-        */
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "timestamp_begin");
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "timestamp_end");
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "content_size");
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "packet_size");
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
-               _uint64_t, "events_discarded");
-       if (ret) {
-               goto end;
-       }
-
-       stream_class->packet_context_type = packet_context_type;
-       stream_class->packet_context = bt_ctf_field_create(packet_context_type);
-       if (!stream_class->packet_context) {
-               ret = -1;
-       }
-end:
-       if (ret) {
-               bt_ctf_field_type_put(packet_context_type);
-               goto end;
-       }
-
-       bt_ctf_field_type_put(_uint64_t);
-       return ret;
-}
-
 static
 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
                uint64_t value)
This page took 0.094254 seconds and 4 git commands to generate.