ir: add weak reference to parent trace to bt_ctf_stream_class
[babeltrace.git] / formats / ctf / ir / stream-class.c
index cd87dffa8624c4493042be22bef19f544c0b3202..22ea8a5db6628d819153026dd8201b98487d345d 100644 (file)
@@ -51,7 +51,7 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
        int ret;
        struct bt_ctf_stream_class *stream_class = NULL;
 
-       if (!name || !strlen(name) || bt_ctf_validate_identifier(name)) {
+       if (name && bt_ctf_validate_identifier(name)) {
                goto error;
        }
 
@@ -101,6 +101,21 @@ end:
        return name;
 }
 
+int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
+               const char *name)
+{
+       int ret = 0;
+
+       if (!stream_class || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       g_string_assign(stream_class->name, name);
+end:
+       return ret;
+}
+
 struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
                struct bt_ctf_stream_class *stream_class)
 {
@@ -120,12 +135,37 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_clock *clock)
 {
        int ret = 0;
+       struct bt_ctf_field_type *timestamp_field = NULL;
 
        if (!stream_class || !clock || stream_class->frozen) {
                ret = -1;
                goto end;
        }
 
+       /*
+        * Look for a "timestamp" field in the stream class' event header type
+        * and map the stream's clock to that field if no current mapping is
+        * currently set.
+        */
+       timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
+               stream_class->event_header_type, "timestamp");
+       if (timestamp_field) {
+               struct bt_ctf_clock *mapped_clock;
+
+               mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
+                       timestamp_field);
+               if (mapped_clock) {
+                       bt_ctf_clock_put(mapped_clock);
+                       goto end;
+               }
+
+               ret = bt_ctf_field_type_integer_set_mapped_clock(
+                       timestamp_field, clock);
+               if (ret) {
+                       goto end;
+               }
+       }
+
        if (stream_class->clock) {
                bt_ctf_clock_put(stream_class->clock);
        }
@@ -133,6 +173,9 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
        stream_class->clock = clock;
        bt_ctf_clock_get(clock);
 end:
+       if (timestamp_field) {
+               bt_ctf_field_type_put(timestamp_field);
+       }
        return ret;
 }
 
@@ -150,6 +193,15 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+int _bt_ctf_stream_class_set_id(
+               struct bt_ctf_stream_class *stream_class, uint32_t id)
+{
+       stream_class->id = id;
+       stream_class->id_set = 1;
+       return 0;
+}
+
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                uint32_t id)
 {
@@ -160,12 +212,58 @@ int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       stream_class->id = id;
-       stream_class->id_set = 1;
+       ret = _bt_ctf_stream_class_set_id(stream_class, id);
+       if (ret) {
+               goto end;
+       }
 end:
        return ret;
 }
 
+static
+void event_class_exists(gpointer element, gpointer query)
+{
+       struct bt_ctf_event_class *event_class_a = element;
+       struct search_query *search_query = query;
+       struct bt_ctf_event_class *event_class_b = search_query->value;
+       int64_t id_a, id_b;
+
+       if (search_query->value == element) {
+               search_query->found = 1;
+               goto end;
+       }
+
+       /*
+        * Two event classes cannot share the same name in a given
+        * stream class.
+        */
+       if (!strcmp(bt_ctf_event_class_get_name(event_class_a),
+                       bt_ctf_event_class_get_name(event_class_b))) {
+               search_query->found = 1;
+               goto end;
+       }
+
+       /*
+        * Two event classes cannot share the same ID in a given
+        * stream class.
+        */
+       id_a = bt_ctf_event_class_get_id(event_class_a);
+       id_b = bt_ctf_event_class_get_id(event_class_b);
+
+       if (id_a < 0 || id_b < 0) {
+               /* at least one ID is not set: will be automatically set later */
+               goto end;
+       }
+
+       if (id_a == id_b) {
+               search_query->found = 1;
+               goto end;
+       }
+
+end:
+       return;
+}
+
 int bt_ctf_stream_class_add_event_class(
                struct bt_ctf_stream_class *stream_class,
                struct bt_ctf_event_class *event_class)
@@ -180,7 +278,8 @@ int bt_ctf_stream_class_add_event_class(
 
        /* Check for duplicate event classes */
        struct search_query query = { .value = event_class, .found = 0 };
-       g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
+       g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
+               &query);
        if (query.found) {
                ret = -1;
                goto end;
@@ -203,6 +302,19 @@ int bt_ctf_stream_class_add_event_class(
 
        bt_ctf_event_class_get(event_class);
        g_ptr_array_add(stream_class->event_classes, event_class);
+       bt_ctf_event_class_freeze(event_class);
+
+       if (stream_class->byte_order) {
+               /*
+                * Only set native byte order if it has been initialized
+                * when the stream class was added to a trace.
+                *
+                * If not set here, this will be set when the stream
+                * classe will be added to a trace.
+                */
+               bt_ctf_event_class_set_native_byte_order(event_class,
+                       stream_class->byte_order);
+       }
 end:
        return ret;
 }
@@ -242,15 +354,35 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
                struct bt_ctf_stream_class *stream_class, const char *name)
 {
        size_t i;
-       GQuark name_quark;
        struct bt_ctf_event_class *event_class = NULL;
 
        if (!stream_class || !name) {
                goto end;
        }
 
-       name_quark = g_quark_try_string(name);
-       if (!name_quark) {
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *cur_event_class =
+                       g_ptr_array_index(stream_class->event_classes, i);
+               const char *cur_event_class_name =
+                       bt_ctf_event_class_get_name(cur_event_class);
+
+               if (!strcmp(name, cur_event_class_name)) {
+                       event_class = cur_event_class;
+                       bt_ctf_event_class_get(event_class);
+                       goto end;
+               }
+       }
+end:
+       return event_class;
+}
+
+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;
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (!stream_class) {
                goto end;
        }
 
@@ -258,7 +390,7 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
                struct bt_ctf_event_class *current_event_class =
                        g_ptr_array_index(stream_class->event_classes, i);
 
-               if (name_quark == current_event_class->name) {
+               if (bt_ctf_event_class_get_id(current_event_class) == id) {
                        event_class = current_event_class;
                        bt_ctf_event_class_get(event_class);
                        goto end;
@@ -420,8 +552,6 @@ void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
 BT_HIDDEN
 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
 {
-       size_t i;
-
        if (!stream_class) {
                return;
        }
@@ -431,33 +561,18 @@ void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
        bt_ctf_field_type_freeze(stream_class->packet_context_type);
        bt_ctf_field_type_freeze(stream_class->event_context_type);
        bt_ctf_clock_freeze(stream_class->clock);
-
-       bt_ctf_field_type_set_native_byte_order(
-               stream_class->event_header_type, stream_class->byte_order);
-       bt_ctf_field_type_set_native_byte_order(
-               stream_class->packet_context_type, stream_class->byte_order);
-       bt_ctf_field_type_set_native_byte_order(
-               stream_class->event_context_type, stream_class->byte_order);
-       for (i = 0; i < stream_class->event_classes->len; i++) {
-               bt_ctf_event_class_set_native_byte_order(
-                       g_ptr_array_index(stream_class->event_classes, i),
-                       stream_class->byte_order);
-               bt_ctf_event_class_freeze(
-                       g_ptr_array_index(stream_class->event_classes, i));
-       }
 }
 
 BT_HIDDEN
 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
        enum bt_ctf_byte_order byte_order)
 {
-       int ret = 0;
+       int i, ret = 0;
        int internal_byte_order;
 
        /* Note that "NATIVE" means the trace's endianness, not the host's. */
        if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
-               byte_order > BT_CTF_BYTE_ORDER_NETWORK ||
-               stream_class->frozen) {
+               byte_order > BT_CTF_BYTE_ORDER_NETWORK) {
                ret = -1;
                goto end;
        }
@@ -476,6 +591,23 @@ int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
        }
 
        stream_class->byte_order = internal_byte_order;
+
+       /* Set native byte order to little or big endian */
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_header_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->packet_context_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_context_type, stream_class->byte_order);
+
+       /* Set all events' native byte order */
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               bt_ctf_event_class_set_native_byte_order(
+                       g_ptr_array_index(stream_class->event_classes, i),
+                       stream_class->byte_order);
+               bt_ctf_event_class_freeze(
+                       g_ptr_array_index(stream_class->event_classes, i));
+       }
 end:
        return ret;
 }
@@ -534,6 +666,28 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_trace *trace)
+{
+       int ret = 0;
+
+       if (!stream_class) {
+               ret = -1;
+               goto end;
+       }
+
+       if (stream_class->trace && trace) {
+               /* Already attached to a trace */
+               ret = -1;
+               goto end;
+       }
+
+       stream_class->trace = trace;
+end:
+       return ret;
+}
+
 static
 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
 {
This page took 0.026618 seconds and 4 git commands to generate.