Fix: ensure that a stream class is not associated to a trace
[babeltrace.git] / formats / ctf / ir / trace.c
index 2cbfdb4e0e878ef1938fb3729a081d49918ba55e..55967275a84d56fea4202c06b96caee745db697f 100644 (file)
@@ -48,8 +48,8 @@
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
 
-struct notification_handler {
-       bt_ctf_notification_cb func;
+struct listener_wrapper {
+       bt_ctf_listener_cb listener;
        void *data;
 };
 
@@ -111,6 +111,12 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
                goto error;
        }
 
+       trace->listeners = g_ptr_array_new_with_free_func(
+                       (GDestroyNotify) g_free);
+       if (!trace->listeners) {
+               goto error;
+       }
+
        return trace;
 
 error:
@@ -139,6 +145,10 @@ void bt_ctf_trace_destroy(struct bt_object *obj)
                g_ptr_array_free(trace->stream_classes, TRUE);
        }
 
+       if (trace->listeners) {
+               g_ptr_array_free(trace->listeners, TRUE);
+       }
+
        bt_put(trace->packet_header_type);
        g_free(trace);
 }
@@ -343,7 +353,7 @@ int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
        int ret = 0;
        struct search_query query = { .value = clock, .found = 0 };
 
-       if (!trace || !clock) {
+       if (!trace || !bt_ctf_clock_is_valid(clock)) {
                ret = -1;
                goto end;
        }
@@ -420,12 +430,20 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        struct bt_ctf_field_type *stream_event_ctx_type = NULL;
        int event_class_count;
        struct bt_ctf_clock *clock_to_add_to_trace = NULL;
+       struct bt_ctf_trace *current_parent_trace = NULL;
 
        if (!trace || !stream_class) {
                ret = -1;
                goto end;
        }
 
+       current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
+       if (current_parent_trace) {
+               /* Stream class is already associated to a trace, abort. */
+               ret = -1;
+               goto end;
+       }
+
        event_class_count =
                bt_ctf_stream_class_get_event_class_count(stream_class);
        assert(event_class_count >= 0);
@@ -640,6 +658,9 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
        bt_ctf_stream_class_freeze(stream_class);
        bt_ctf_trace_freeze(trace);
 
+       /* Notifiy listeners of the trace's schema modification. */
+       bt_ctf_stream_class_visit(stream_class,
+                       bt_ctf_trace_element_modification, trace);
 end:
        if (ret) {
                bt_object_set_parent(stream_class, NULL);
@@ -655,6 +676,7 @@ end:
        g_free(ec_validation_outputs);
        bt_ctf_validation_output_put_types(&trace_sc_validation_output);
        BT_PUT(clock_to_add_to_trace);
+       bt_put(current_parent_trace);
        assert(!packet_header_type);
        assert(!packet_context_type);
        assert(!event_header_type);
@@ -1073,60 +1095,70 @@ end:
 }
 
 static
-int ir_visitor(struct bt_ctf_ir_element *element, void *data)
+int invoke_listener(struct bt_ctf_ir_element *element, void *data)
 {
-       int ret = 0;
+       struct listener_wrapper *listener_wrapper = data;
 
-       switch (element->type) {
-       case BT_CTF_IR_TYPE_TRACE:
-       {
-               break;
-       }
-       case BT_CTF_IR_TYPE_STREAM_CLASS:
-       {
-               break;
-       }
-       case BT_CTF_IR_TYPE_EVENT_CLASS:
-       {
-               break;
-       }
-       default:
-               assert(0);
-               ret = -1;
-               goto end;
-       }
-end:
-       return ret;
+       listener_wrapper->listener(element, listener_wrapper->data);
+       return 0;
 }
 
-int bt_ctf_trace_add_notification_handler_cb(struct bt_ctf_trace *trace,
-               bt_ctf_notification_cb handler, void *handler_data)
+int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
+               bt_ctf_listener_cb listener, void *listener_data)
 {
        int ret = 0;
-       struct notification_handler *handler_wrapper =
-                       g_new0(struct notification_handler, 1);
+       struct listener_wrapper *listener_wrapper =
+                       g_new0(struct listener_wrapper, 1);
 
-       if (!trace || !handler || !handler_wrapper) {
+       if (!trace || !listener || !listener_wrapper) {
                ret = -1;
                goto error;
        }
 
-       handler_wrapper->func = handler;
-       handler_wrapper->data = handler_data;
+       listener_wrapper->listener = listener;
+       listener_wrapper->data = listener_data;
 
-       /* Emit notifications describing the current schema. */
-       ret = bt_ctf_trace_visit(trace, ir_visitor, handler_wrapper);
+       /* Visit the current schema. */
+       ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
        if (ret) {
                goto error;
        }
 
-       g_ptr_array_add(trace->notification_handlers, handler_wrapper);
+       /*
+        * Add listener to the array of callbacks which will be invoked on
+        * schema changes.
+        */
+       g_ptr_array_add(trace->listeners, listener_wrapper);
        return ret;
 error:
-       g_free(handler_wrapper);
+       g_free(listener_wrapper);
        return ret;
 }
 
+BT_HIDDEN
+int bt_ctf_trace_element_modification(struct bt_ctf_ir_element *element,
+               void *trace_ptr)
+{
+       size_t i;
+       struct bt_ctf_trace *trace = trace_ptr;
+
+       assert(trace);
+       assert(element);
+
+       if (trace->listeners->len == 0) {
+               goto end;
+       }
+
+       for (i = 0; i < trace->listeners->len; i++) {
+               struct listener_wrapper *listener =
+                               g_ptr_array_index(trace->listeners, i);
+
+               listener->listener(element, listener->data);
+       }
+end:
+       return 0;
+}
+
 BT_HIDDEN
 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
 {
This page took 0.02652 seconds and 4 git commands to generate.