Allow ctf-writer to use new time-keeping facilities
[babeltrace.git] / formats / ctf / ir / stream-class.c
index b5e7e79dca12b2f7330a68ee838101a1da6f4b89..f5b8923360c9d40f6e4a9e76a97fe11a5529b9d9 100644 (file)
 #include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-ir/clock-internal.h>
 #include <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-ir/event-class-internal.h>
 #include <babeltrace/ctf-ir/event-internal.h>
-#include <babeltrace/ctf-ir/event-types-internal.h>
-#include <babeltrace/ctf-ir/event-fields-internal.h>
+#include <babeltrace/ctf-ir/field-types-internal.h>
+#include <babeltrace/ctf-ir/fields-internal.h>
 #include <babeltrace/ctf-writer/stream.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/validation-internal.h>
+#include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ref.h>
@@ -70,6 +72,9 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
                goto error;
        }
 
+       stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash,
+                       g_int64_equal, g_free, NULL);
+
        ret = init_event_header(stream_class);
        if (ret) {
                goto error;
@@ -145,7 +150,8 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
        int ret = 0;
        struct bt_ctf_field_type *timestamp_field = NULL;
 
-       if (!stream_class || !clock || stream_class->frozen) {
+       if (!stream_class || stream_class->frozen ||
+                       !bt_ctf_clock_is_valid(clock)) {
                ret = -1;
                goto end;
        }
@@ -315,7 +321,7 @@ int bt_ctf_stream_class_add_event_class(
                struct bt_ctf_event_class *event_class)
 {
        int ret = 0;
-       int64_t event_id;
+       int64_t *event_id = NULL;
        struct bt_ctf_trace *trace = NULL;
        struct bt_ctf_stream_class *old_stream_class = NULL;
        struct bt_ctf_validation_output validation_output = { 0 };
@@ -333,6 +339,12 @@ int bt_ctf_stream_class_add_event_class(
                goto end;
        }
 
+       event_id = g_new(int64_t, 1);
+       if (!event_id) {
+               ret = -1;
+               goto end;
+       }
+
        /* Check for duplicate event classes */
        struct search_query query = { .value = event_class, .found = 0 };
        g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
@@ -408,13 +420,14 @@ int bt_ctf_stream_class_add_event_class(
        }
 
        /* Only set an event ID if none was explicitly set before */
-       event_id = bt_ctf_event_class_get_id(event_class);
-       if (event_id < 0) {
+       *event_id = bt_ctf_event_class_get_id(event_class);
+       if (*event_id < 0) {
                if (bt_ctf_event_class_set_id(event_class,
                        stream_class->next_event_id++)) {
                        ret = -1;
                        goto end;
                }
+               *event_id = stream_class->next_event_id;
        }
 
        ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
@@ -444,6 +457,9 @@ int bt_ctf_stream_class_add_event_class(
 
        /* Add to the event classes of the stream class */
        g_ptr_array_add(stream_class->event_classes, event_class);
+       g_hash_table_insert(stream_class->event_classes_ht, event_id,
+                       event_class);
+       event_id = NULL;
 
        /* Freeze the event class */
        bt_ctf_event_class_freeze(event_class);
@@ -460,6 +476,13 @@ int bt_ctf_stream_class_add_event_class(
                        stream_class->byte_order);
        }
 
+       /* Notifiy listeners of the trace's schema modification. */
+       if (trace) {
+               struct bt_ctf_ir_element element = { .element = event_class,
+                               .type = BT_CTF_IR_TYPE_EVENT_CLASS };
+
+               (void) bt_ctf_trace_element_modification(&element, trace);
+       }
 end:
        BT_PUT(trace);
        BT_PUT(old_stream_class);
@@ -470,6 +493,7 @@ end:
        assert(!stream_event_ctx_type);
        assert(!event_context_type);
        assert(!event_payload_type);
+       g_free(event_id);
 
        return ret;
 }
@@ -534,23 +558,16 @@ end:
 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
                struct bt_ctf_stream_class *stream_class, uint32_t id)
 {
-       size_t i;
+       int64_t id_key = id;
        struct bt_ctf_event_class *event_class = NULL;
 
        if (!stream_class) {
                goto end;
        }
 
-       for (i = 0; i < stream_class->event_classes->len; i++) {
-               struct bt_ctf_event_class *current_event_class =
-                       g_ptr_array_index(stream_class->event_classes, i);
-
-               if (bt_ctf_event_class_get_id(current_event_class) == id) {
-                       event_class = current_event_class;
-                       bt_get(event_class);
-                       goto end;
-               }
-       }
+       event_class = g_hash_table_lookup(stream_class->event_classes_ht,
+                       &id_key);
+       bt_get(event_class);
 end:
        return event_class;
 }
@@ -587,7 +604,7 @@ int bt_ctf_stream_class_set_packet_context_type(
                goto end;
        }
        if (bt_ctf_field_type_get_type_id(packet_context_type) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                /* A packet context must be a structure */
                ret = -1;
                goto end;
@@ -632,7 +649,7 @@ int bt_ctf_stream_class_set_event_header_type(
                goto end;
        }
        if (bt_ctf_field_type_get_type_id(event_header_type) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                /* An event header must be a structure */
                ret = -1;
                goto end;
@@ -673,7 +690,7 @@ int bt_ctf_stream_class_set_event_context_type(
        }
 
        if (bt_ctf_field_type_get_type_id(event_context_type) !=
-               CTF_TYPE_STRUCT) {
+               BT_CTF_TYPE_ID_STRUCT) {
                /* A packet context must be a structure */
                ret = -1;
                goto end;
@@ -696,6 +713,50 @@ void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
        bt_put(stream_class);
 }
 
+static
+int get_event_class_count(void *element)
+{
+       return bt_ctf_stream_class_get_event_class_count(
+                       (struct bt_ctf_stream_class *) element);
+}
+
+static
+void *get_event_class(void *element, int i)
+{
+       return bt_ctf_stream_class_get_event_class(
+                       (struct bt_ctf_stream_class *) element, i);
+}
+
+static
+int visit_event_class(void *element, bt_ctf_ir_visitor visitor,void *data)
+{
+       struct bt_ctf_ir_element ir_element =
+                       { .element = element,
+                       .type = BT_CTF_IR_TYPE_EVENT_CLASS };
+
+       return visitor(&ir_element, data);
+}
+
+int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
+               bt_ctf_ir_visitor visitor, void *data)
+{
+       int ret;
+       struct bt_ctf_ir_element element =
+                       { .element = stream_class,
+                       .type = BT_CTF_IR_TYPE_STREAM_CLASS };
+
+       if (!stream_class || !visitor) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = visitor_helper(&element, get_event_class_count,
+                       get_event_class,
+                       visit_event_class, visitor, data);
+end:
+       return ret;
+}
+
 BT_HIDDEN
 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
 {
@@ -800,6 +861,9 @@ void bt_ctf_stream_class_destroy(struct bt_object *obj)
        stream_class = container_of(obj, struct bt_ctf_stream_class, base);
        bt_put(stream_class->clock);
 
+       if (stream_class->event_classes_ht) {
+               g_hash_table_destroy(stream_class->event_classes_ht);
+       }
        if (stream_class->event_classes) {
                g_ptr_array_free(stream_class->event_classes, TRUE);
        }
This page took 0.026465 seconds and 4 git commands to generate.