Unify reference counting using a common bt_object base
[babeltrace.git] / formats / ctf / writer / writer.c
index 7b876617a4bc29dd65605fac43dca8b9020c50f2..f8a36778e39b3f1382cea2fc66bedc09f483ae1f 100644 (file)
@@ -33,6 +33,7 @@
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
+#include <babeltrace/ref.h>
 #include <babeltrace/compiler.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>
 
 static
-void bt_ctf_writer_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_writer_destroy(struct bt_object *obj);
+
 static
 int create_stream_file(struct bt_ctf_writer *writer,
                struct bt_ctf_stream *stream);
-static
-void stream_flush_cb(struct bt_ctf_stream *stream,
-               struct bt_ctf_writer *writer);
 
 struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
 {
@@ -64,7 +63,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error;
        }
 
-       bt_ctf_ref_init(&writer->ref_count);
+       bt_object_init(writer, bt_ctf_writer_destroy);
        writer->path = g_string_new(path);
        if (!writer->path) {
                goto error_destroy;
@@ -95,21 +94,16 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
 
 error_destroy:
        unlinkat(writer->trace_dir_fd, "metadata", 0);
-       bt_ctf_writer_destroy(&writer->ref_count);
-       writer = NULL;
+       BT_PUT(writer);
 error:
        return writer;
 }
 
-void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_writer_destroy(struct bt_object *obj)
 {
        struct bt_ctf_writer *writer;
 
-       if (!ref) {
-               return;
-       }
-
-       writer = container_of(ref, struct bt_ctf_writer, ref_count);
+       writer = container_of(obj, struct bt_ctf_writer, base);
        bt_ctf_writer_flush_metadata(writer);
        if (writer->path) {
                g_string_free(writer->path, TRUE);
@@ -127,10 +121,24 @@ void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
                }
        }
 
-       bt_ctf_trace_put(writer->trace);
+       bt_put(writer->trace);
        g_free(writer);
 }
 
+struct bt_ctf_trace *bt_ctf_writer_get_trace(struct bt_ctf_writer *writer)
+{
+       struct bt_ctf_trace *trace = NULL;
+
+       if (!writer) {
+               goto end;
+       }
+
+       trace = writer->trace;
+       bt_get(trace);
+end:
+       return trace;
+}
+
 struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
                struct bt_ctf_stream_class *stream_class)
 {
@@ -151,14 +159,12 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
                goto error;
        }
 
-       bt_ctf_stream_set_flush_callback(stream, (flush_func)stream_flush_cb,
-               writer);
        writer->frozen = 1;
        return stream;
 
 error:
-       bt_ctf_stream_put(stream);
-       return NULL;
+        BT_PUT(stream);
+       return stream;
 }
 
 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
@@ -171,7 +177,7 @@ int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
                goto end;
        }
 
-       ret = bt_ctf_trace_add_environment_field(writer->trace,
+       ret = bt_ctf_trace_set_environment_field_string(writer->trace,
                name, value);
 end:
        return ret;
@@ -258,20 +264,12 @@ end:
 
 void bt_ctf_writer_get(struct bt_ctf_writer *writer)
 {
-       if (!writer) {
-               return;
-       }
-
-       bt_ctf_ref_get(&writer->ref_count);
+       bt_get(writer);
 }
 
 void bt_ctf_writer_put(struct bt_ctf_writer *writer)
 {
-       if (!writer) {
-               return;
-       }
-
-       bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy);
+       bt_put(writer);
 }
 
 static
@@ -281,30 +279,23 @@ int create_stream_file(struct bt_ctf_writer *writer,
        int fd;
        GString *filename = g_string_new(stream->stream_class->name->str);
 
+       if (stream->stream_class->name->len == 0) {
+               int64_t ret;
+
+               ret = bt_ctf_stream_class_get_id(stream->stream_class);
+               if (ret < 0) {
+                       fd = -1;
+                       goto error;
+               }
+
+               g_string_printf(filename, "stream_%" PRId64, ret);
+       }
+
        g_string_append_printf(filename, "_%" PRIu32, stream->id);
        fd = openat(writer->trace_dir_fd, filename->str,
                O_RDWR | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+error:
        g_string_free(filename, TRUE);
        return fd;
 }
-
-static
-void stream_flush_cb(struct bt_ctf_stream *stream, struct bt_ctf_writer *writer)
-{
-       struct bt_ctf_field *stream_id;
-
-       /* Start a new packet in the stream */
-       if (stream->flushed_packet_count) {
-               /* ctf_init_pos has already initialized the first packet */
-               ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
-       }
-
-       stream_id = bt_ctf_field_structure_get_field(
-               writer->trace->trace_packet_header, "stream_id");
-       bt_ctf_field_unsigned_integer_set_value(stream_id, stream->stream_class->id);
-       bt_ctf_field_put(stream_id);
-
-       /* Write the trace_packet_header */
-       bt_ctf_field_serialize(writer->trace->trace_packet_header, &stream->pos);
-}
This page took 0.0252 seconds and 4 git commands to generate.