ir: disable clock value accessors in non-writer mode
[babeltrace.git] / formats / ctf / ir / trace.c
index 660a2ed5c6abf438687345aa0083b3dbb6667f71..9cce8b6f95c91eaa49af9384075416bf0d80609f 100644 (file)
@@ -136,43 +136,6 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
        g_free(trace);
 }
 
-struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
-               struct bt_ctf_stream_class *stream_class)
-{
-       int ret;
-       int stream_class_found = 0;
-       size_t i;
-       struct bt_ctf_stream *stream = NULL;
-
-       if (!trace || !stream_class) {
-               goto error;
-       }
-
-       for (i = 0; i < trace->stream_classes->len; i++) {
-               if (trace->stream_classes->pdata[i] == stream_class) {
-                       stream_class_found = 1;
-               }
-       }
-
-       if (!stream_class_found) {
-               ret = bt_ctf_trace_add_stream_class(trace, stream_class);
-               if (ret) {
-                       goto error;
-               }
-       }
-
-       stream = bt_ctf_stream_create(stream_class, trace);
-       if (!stream) {
-               goto error;
-       }
-
-       g_ptr_array_add(trace->streams, stream);
-       return stream;
-error:
-        BT_PUT(stream);
-       return stream;
-}
-
 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
                const char *name, struct bt_value *value)
 {
@@ -388,6 +351,15 @@ int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
        bt_get(clock);
        g_ptr_array_add(trace->clocks, clock);
 
+       if (!trace->is_created_by_writer) {
+               /*
+                * Non-writer mode trace: disable clock value functions
+                * because clock values are per-stream in that
+                * situation.
+                */
+               clock->has_value = 0;
+       }
+
        if (trace->frozen) {
                bt_ctf_clock_freeze(clock);
        }
@@ -440,6 +412,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        struct bt_ctf_field_type *event_header_type = NULL;
        struct bt_ctf_field_type *stream_event_ctx_type = NULL;
        int event_class_count;
+       struct bt_ctf_clock *clock_to_add_to_trace = NULL;
 
        if (!trace || !stream_class) {
                ret = -1;
@@ -459,6 +432,32 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                }
        }
 
+       /*
+        * If the stream class has a clock, register this clock to this
+        * trace if not already done.
+        */
+       if (stream_class->clock) {
+               const char *clock_name =
+                       bt_ctf_clock_get_name(stream_class->clock);
+               struct bt_ctf_clock *trace_clock;
+
+               assert(clock_name);
+               trace_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
+               bt_put(trace_clock);
+               if (trace_clock) {
+                       if (trace_clock != stream_class->clock) {
+                               /*
+                                * Error: two different clocks in the
+                                * trace would share the same name.
+                                */
+                               ret = -1;
+                               goto end;
+                       }
+               } else {
+                       clock_to_add_to_trace = bt_get(stream_class->clock);
+               }
+       }
+
        /*
         * We're about to freeze both the trace and the stream class.
         * Also, each event class contained in this stream class are
@@ -621,6 +620,13 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                trace->byte_order);
        bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
 
+       /* Add stream class's clock if it exists */
+       if (clock_to_add_to_trace) {
+               int add_clock_ret =
+                       bt_ctf_trace_add_clock(trace, clock_to_add_to_trace);
+               assert(add_clock_ret == 0);
+       }
+
        /*
         * Freeze the trace and the stream class.
         */
@@ -641,6 +647,7 @@ end:
 
        g_free(ec_validation_outputs);
        bt_ctf_validation_output_put_types(&trace_sc_validation_output);
+       BT_PUT(clock_to_add_to_trace);
        assert(!packet_header_type);
        assert(!packet_context_type);
        assert(!event_header_type);
This page took 0.027511 seconds and 4 git commands to generate.