Add trace moficiation notification handler interface
[babeltrace.git] / formats / ctf / ir / trace.c
index 221e84e43b2b02f37e85f577cdbe0100aed7d5cf..c4889b79b44fb2f4ef318f387434bfad800ca79f 100644 (file)
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
 
+struct notification_handler {
+       bt_ctf_notification_cb func;
+       void *data;
+};
+
 static
 void bt_ctf_trace_destroy(struct bt_object *obj);
 static
@@ -136,43 +141,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)
 {
@@ -387,6 +355,19 @@ 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);
+       }
 end:
        return ret;
 }
@@ -436,6 +417,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;
@@ -455,6 +437,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
@@ -510,7 +518,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        }
 
        /* Validate each event class individually */
-       for (i = 0; i < event_class_count; ++i) {
+       for (i = 0; i < event_class_count; i++) {
                struct bt_ctf_event_class *event_class =
                        bt_ctf_stream_class_get_event_class(stream_class, i);
                struct bt_ctf_field_type *event_context_type = NULL;
@@ -593,7 +601,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
         */
        bt_ctf_validation_output_put_types(&trace_sc_validation_output);
 
-       for (i = 0; i < event_class_count; ++i) {
+       for (i = 0; i < event_class_count; i++) {
                struct bt_ctf_event_class *event_class =
                        bt_ctf_stream_class_get_event_class(stream_class, i);
 
@@ -617,6 +625,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.
         */
@@ -628,7 +643,7 @@ end:
                bt_object_set_parent(stream_class, NULL);
 
                if (ec_validation_outputs) {
-                       for (i = 0; i < event_class_count; ++i) {
+                       for (i = 0; i < event_class_count; i++) {
                                bt_ctf_validation_output_put_types(
                                        &ec_validation_outputs[i]);
                        }
@@ -637,6 +652,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);
@@ -684,7 +700,7 @@ struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
                goto end;
        }
 
-       for (i = 0; i < trace->stream_classes->len; ++i) {
+       for (i = 0; i < trace->stream_classes->len; i++) {
                struct bt_ctf_stream_class *stream_class_candidate;
 
                stream_class_candidate =
@@ -712,7 +728,7 @@ struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
                goto end;
        }
 
-       for (i = 0; i < trace->clocks->len; ++i) {
+       for (i = 0; i < trace->clocks->len; i++) {
                struct bt_ctf_clock *cur_clk =
                        g_ptr_array_index(trace->clocks, i);
                const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
@@ -803,7 +819,7 @@ void append_env_metadata(struct bt_ctf_trace *trace,
 
        g_string_append(context->string, "env {\n");
 
-       for (i = 0; i < env_size; ++i) {
+       for (i = 0; i < env_size; i++) {
                struct bt_value *env_field_value_obj = NULL;
                const char *entry_name;
 
@@ -1016,20 +1032,31 @@ end:
        return ret;
 }
 
-void bt_ctf_trace_get(struct bt_ctf_trace *trace)
+int bt_ctf_trace_add_notification_handler_cb(struct bt_ctf_trace *trace,
+               bt_ctf_notification_cb handler, void *handler_data)
 {
-       bt_get(trace);
-}
+       int ret = 0;
+       struct notification_handler *handler_wrapper =
+                       g_new0(struct notification_handler, 1);
 
-void bt_ctf_trace_put(struct bt_ctf_trace *trace)
-{
-       bt_put(trace);
+       if (!trace || !handler || !handler_wrapper) {
+               ret = -1;
+               goto error;
+       }
 
+       handler_wrapper->func = handler;
+       handler_wrapper->data = handler_data;
+       g_ptr_array_add(trace->notification_handlers, handler_wrapper);
+       return ret;
+error:
+       g_free(handler_wrapper);
+       return ret;
 }
 
 BT_HIDDEN
 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
 {
+       int ret;
        unsigned int alignment, size;
        struct bt_ctf_field_type *field_type = NULL;
 
@@ -1040,7 +1067,10 @@ struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
        alignment = field_type_aliases_alignments[alias];
        size = field_type_aliases_sizes[alias];
        field_type = bt_ctf_field_type_integer_create(size);
-       bt_ctf_field_type_set_alignment(field_type, alignment);
+       ret = bt_ctf_field_type_set_alignment(field_type, alignment);
+       if (ret) {
+               BT_PUT(field_type);
+       }
 end:
        return field_type;
 }
@@ -1048,8 +1078,18 @@ end:
 static
 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
 {
+       int i;
+
        bt_ctf_field_type_freeze(trace->packet_header_type);
        bt_ctf_attributes_freeze(trace->environment);
+
+       for (i = 0; i < trace->clocks->len; i++) {
+               struct bt_ctf_clock *clock =
+                       g_ptr_array_index(trace->clocks, i);
+
+               bt_ctf_clock_freeze(clock);
+       }
+
        trace->frozen = 1;
 }
 
This page took 0.028168 seconds and 4 git commands to generate.