Make API CTF-agnostic
[babeltrace.git] / lib / ctf-writer / writer.c
index 2978cdfb78a02affa2f7b0e32193eb44225d10cd..c9909d9dfc42eb8b34424decc17cfc0983b5bbb6 100644 (file)
 #define BT_LOG_TAG "CTF-WRITER"
 #include <babeltrace/lib-logging-internal.h>
 
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/compat/uuid-internal.h>
+#include <babeltrace/compiler-internal.h>
 #include <babeltrace/ctf-writer/clock-internal.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
-#include <babeltrace/ctf-ir/field-types-internal.h>
-#include <babeltrace/ctf-ir/fields-internal.h>
+#include <babeltrace/ctf-writer/field-types-internal.h>
+#include <babeltrace/ctf-writer/fields-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
-#include <babeltrace/ctf-ir/stream-class-internal.h>
-#include <babeltrace/ctf-ir/stream-internal.h>
-#include <babeltrace/ctf-ir/trace-internal.h>
-#include <babeltrace/ref.h>
+#include <babeltrace/ctf-writer/stream-class-internal.h>
+#include <babeltrace/ctf-writer/stream-internal.h>
+#include <babeltrace/ctf-writer/trace-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
 #include <babeltrace/endian-internal.h>
-#include <babeltrace/compiler-internal.h>
-#include <babeltrace/compat/uuid-internal.h>
+#include <babeltrace/ref.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
-#include <errno.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <inttypes.h>
 
 static
 void bt_ctf_writer_destroy(struct bt_object *obj);
@@ -56,7 +57,6 @@ static
 int init_trace_packet_header(struct bt_ctf_trace *trace)
 {
        int ret = 0;
-       struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
        struct bt_ctf_field_type *_uint32_t =
                get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
        struct bt_ctf_field_type *_uint8_t =
@@ -89,7 +89,7 @@ int init_trace_packet_header(struct bt_ctf_trace *trace)
                goto end;
        }
 
-       ret = bt_ctf_trace_set_packet_header_type(trace,
+       ret = bt_ctf_trace_set_packet_header_field_type(trace,
                trace_packet_header_type);
        if (ret) {
                goto end;
@@ -98,8 +98,6 @@ end:
        bt_put(uuid_array_type);
        bt_put(_uint32_t);
        bt_put(_uint8_t);
-       bt_put(magic);
-       bt_put(uuid_array);
        bt_put(trace_packet_header_type);
        return ret;
 }
@@ -122,7 +120,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
 
        metadata_path = g_build_filename(path, "metadata", NULL);
 
-       bt_object_init(writer, bt_ctf_writer_destroy);
+       bt_object_init_shared(&writer->base, bt_ctf_writer_destroy);
        writer->path = g_string_new(path);
        if (!writer->path) {
                goto error_destroy;
@@ -150,13 +148,12 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error_destroy;
        }
 
-       writer->trace->is_created_by_writer = 1;
-       bt_object_set_parent(writer->trace, writer);
+       bt_object_set_parent(&writer->trace->common.base, &writer->base);
        bt_put(writer->trace);
 
        /* Default to little-endian */
        ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE);
-       assert(ret == 0);
+       BT_ASSERT(ret == 0);
 
        /* Create trace directory if necessary and open a metadata file */
        if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) {
@@ -198,7 +195,7 @@ void bt_ctf_writer_destroy(struct bt_object *obj)
                }
        }
 
-       bt_object_release(writer->trace);
+       bt_object_try_spec_release(&writer->trace->common.base);
        g_free(writer);
 }
 
@@ -259,7 +256,7 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
                }
        }
 
-       stream = bt_ctf_stream_create(stream_class, NULL);
+       stream = bt_ctf_stream_create_with_id(stream_class, NULL, -1ULL);
        if (!stream) {
                goto error;
        }
@@ -385,18 +382,70 @@ end:
        return ret;
 }
 
-void bt_ctf_writer_get(struct bt_ctf_writer *writer)
+BT_HIDDEN
+void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
 {
-       bt_get(writer);
+       writer->frozen = 1;
 }
 
-void bt_ctf_writer_put(struct bt_ctf_writer *writer)
+static
+const unsigned int field_type_aliases_alignments[] = {
+       [FIELD_TYPE_ALIAS_UINT5_T] = 1,
+       [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
+       [FIELD_TYPE_ALIAS_UINT27_T] = 1,
+       [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
+};
+
+static
+const unsigned int field_type_aliases_sizes[] = {
+       [FIELD_TYPE_ALIAS_UINT5_T] = 5,
+       [FIELD_TYPE_ALIAS_UINT8_T] = 8,
+       [FIELD_TYPE_ALIAS_UINT16_T] = 16,
+       [FIELD_TYPE_ALIAS_UINT27_T] = 27,
+       [FIELD_TYPE_ALIAS_UINT32_T] = 32,
+       [FIELD_TYPE_ALIAS_UINT64_T] = 64,
+};
+
+BT_HIDDEN
+struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
 {
-       bt_put(writer);
+       int ret;
+       unsigned int alignment, size;
+       struct bt_ctf_field_type *field_type = NULL;
+
+       if (alias >= NR_FIELD_TYPE_ALIAS) {
+               goto end;
+       }
+
+       alignment = field_type_aliases_alignments[alias];
+       size = field_type_aliases_sizes[alias];
+       field_type = bt_ctf_field_type_integer_create(size);
+       ret = bt_ctf_field_type_set_alignment(field_type, alignment);
+       if (ret) {
+               BT_PUT(field_type);
+       }
+end:
+       return field_type;
 }
 
 BT_HIDDEN
-void bt_ctf_writer_freeze(struct bt_ctf_writer *writer)
+const char *bt_ctf_get_byte_order_string(enum bt_ctf_byte_order byte_order)
 {
-       writer->frozen = 1;
+       const char *string;
+
+       switch (byte_order) {
+       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
+               string = "le";
+               break;
+       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
+               string = "be";
+               break;
+       case BT_CTF_BYTE_ORDER_NATIVE:
+               string = "native";
+               break;
+       default:
+               abort();
+       }
+
+       return string;
 }
This page took 0.027152 seconds and 4 git commands to generate.