Unify reference counting using a common bt_object base
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 18ae57eaccadc7ba733d8d36288af8b0f3f578a4..573fb421ebfe6c26e37df2f8b0bf36b8a4fc384d 100644 (file)
 #include <babeltrace/ctf-ir/event-fields-internal.h>
 #include <babeltrace/ctf-writer/stream.h>
 #include <babeltrace/ctf-ir/stream-class-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>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_stream_class_destroy(struct bt_object *obj);
 static
 int init_event_header(struct bt_ctf_stream_class *stream_class);
 static
@@ -51,7 +53,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;
        }
 
@@ -62,31 +64,46 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
 
        stream_class->name = g_string_new(name);
        stream_class->event_classes = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_event_class_put);
+               (GDestroyNotify) bt_put);
        if (!stream_class->event_classes) {
-               goto error_destroy;
+               goto error;
        }
 
        ret = init_event_header(stream_class);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
        ret = init_packet_context(stream_class);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
-       bt_ctf_ref_init(&stream_class->ref_count);
+       bt_object_init(stream_class, bt_ctf_stream_class_destroy);
        return stream_class;
 
-error_destroy:
-       bt_ctf_stream_class_destroy(&stream_class->ref_count);
-       stream_class = NULL;
 error:
+        BT_PUT(stream_class);
        return stream_class;
 }
 
+struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_trace *trace = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       trace = stream_class->trace;
+       if (trace) {
+               bt_get(trace);
+       }
+end:
+       return trace;
+}
+
 const char *bt_ctf_stream_class_get_name(
                struct bt_ctf_stream_class *stream_class)
 {
@@ -101,6 +118,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)
 {
@@ -111,7 +143,7 @@ struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
        }
 
        clock = stream_class->clock;
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
 end:
        return clock;
 }
@@ -140,7 +172,7 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
                        timestamp_field);
                if (mapped_clock) {
-                       bt_ctf_clock_put(mapped_clock);
+                       bt_put(mapped_clock);
                        goto end;
                }
 
@@ -152,14 +184,14 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
        }
 
        if (stream_class->clock) {
-               bt_ctf_clock_put(stream_class->clock);
+               bt_put(stream_class->clock);
        }
 
        stream_class->clock = clock;
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
 end:
        if (timestamp_field) {
-               bt_ctf_field_type_put(timestamp_field);
+               bt_put(timestamp_field);
        }
        return ret;
 }
@@ -178,6 +210,56 @@ 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;
+}
+
+struct event_class_set_stream_id_data {
+       uint32_t stream_id;
+       int ret;
+};
+
+static
+void event_class_set_stream_id(gpointer event_class, gpointer data)
+{
+       struct event_class_set_stream_id_data *typed_data = data;
+
+       typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
+               typed_data->stream_id);
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_id_no_check(
+               struct bt_ctf_stream_class *stream_class, uint32_t id)
+{
+       int ret = 0;
+       struct event_class_set_stream_id_data data =
+               { .stream_id = id, .ret = 0 };
+
+       /*
+        * Make sure all event classes have their "stream_id" attribute
+        * set to this value.
+        */
+       g_ptr_array_foreach(stream_class->event_classes,
+               event_class_set_stream_id, &data);
+       ret = data.ret;
+       if (ret) {
+               goto end;
+       }
+
+       ret = _bt_ctf_stream_class_set_id(stream_class, id);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
                uint32_t id)
 {
@@ -188,12 +270,55 @@ 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_no_check(stream_class, id);
 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)
@@ -208,12 +333,27 @@ 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;
        }
 
+       /*
+        * Resolve the event's sequence length and variant tags if the
+        * stream is already associated with a trace. Otherwise, this
+        * validation will be performed once the stream is registered
+        * to a trace.
+        */
+       if (stream_class->trace) {
+               ret = bt_ctf_event_class_resolve_types(event_class,
+                       stream_class->trace, stream_class);
+               if (ret) {
+                       goto end;
+               }
+       }
+
        /* 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) {
@@ -229,8 +369,26 @@ int bt_ctf_stream_class_add_event_class(
                goto end;
        }
 
-       bt_ctf_event_class_get(event_class);
+       ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
+       if (ret) {
+               goto end;
+       }
+
+       bt_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;
 }
@@ -261,7 +419,7 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
        }
 
        event_class = g_ptr_array_index(stream_class->event_classes, index);
-       bt_ctf_event_class_get(event_class);
+       bt_get(event_class);
 end:
        return event_class;
 }
@@ -270,15 +428,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_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;
        }
 
@@ -286,9 +464,9 @@ 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);
+                       bt_get(event_class);
                        goto end;
                }
        }
@@ -306,7 +484,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
        }
 
        assert(stream_class->packet_context_type);
-       bt_ctf_field_type_get(stream_class->packet_context_type);
+       bt_get(stream_class->packet_context_type);
        ret = stream_class->packet_context_type;
 end:
        return ret;
@@ -334,8 +512,8 @@ int bt_ctf_stream_class_set_packet_context_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_type_get(packet_context_type);
+       bt_put(stream_class->packet_context_type);
+       bt_get(packet_context_type);
        stream_class->packet_context_type = packet_context_type;
 end:
        return ret;
@@ -351,7 +529,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
        }
 
        assert(stream_class->event_header_type);
-       bt_ctf_field_type_get(stream_class->event_header_type);
+       bt_get(stream_class->event_header_type);
        ret = stream_class->event_header_type;
 end:
        return ret;
@@ -379,8 +557,8 @@ int bt_ctf_stream_class_set_event_header_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_type_get(event_header_type);
+       bt_put(stream_class->event_header_type);
+       bt_get(event_header_type);
        stream_class->event_header_type = event_header_type;
 end:
        return ret;
@@ -396,7 +574,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
        }
 
        assert(stream_class->event_context_type);
-       bt_ctf_field_type_get(stream_class->event_context_type);
+       bt_get(stream_class->event_context_type);
        ret = stream_class->event_context_type;
 end:
        return ret;
@@ -420,8 +598,8 @@ int bt_ctf_stream_class_set_event_context_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_type_get(event_context_type);
+       bt_put(stream_class->event_context_type);
+       bt_get(event_context_type);
        stream_class->event_context_type = event_context_type;
 end:
        return ret;
@@ -429,27 +607,17 @@ end:
 
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
 {
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&stream_class->ref_count);
+       bt_get(stream_class);
 }
 
 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
 {
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
+       bt_put(stream_class);
 }
 
 BT_HIDDEN
 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
 {
-       size_t i;
-
        if (!stream_class) {
                return;
        }
@@ -459,33 +627,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;
        }
@@ -504,6 +657,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;
 }
@@ -562,17 +732,35 @@ end:
        return ret;
 }
 
-static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
+BT_HIDDEN
+int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_trace *trace)
 {
-       struct bt_ctf_stream_class *stream_class;
+       int ret = 0;
 
-       if (!ref) {
-               return;
+       if (!stream_class) {
+               ret = -1;
+               goto end;
        }
 
-       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
-       bt_ctf_clock_put(stream_class->clock);
+       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_object *obj)
+{
+       struct bt_ctf_stream_class *stream_class;
+
+       stream_class = container_of(obj, struct bt_ctf_stream_class, base);
+       bt_put(stream_class->clock);
 
        if (stream_class->event_classes) {
                size_t i;
@@ -593,11 +781,9 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
                g_string_free(stream_class->name, TRUE);
        }
 
-       bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_type_put(stream_class->packet_context_type);
-       if (stream_class->event_context_type) {
-               bt_ctf_field_type_put(stream_class->event_context_type);
-       }
+       bt_put(stream_class->event_header_type);
+       bt_put(stream_class->packet_context_type);
+       bt_put(stream_class->event_context_type);
        g_free(stream_class);
 }
 
@@ -630,16 +816,16 @@ int init_event_header(struct bt_ctf_stream_class *stream_class)
        }
 
        if (stream_class->event_header_type) {
-               bt_ctf_field_type_put(stream_class->event_header_type);
+               bt_put(stream_class->event_header_type);
        }
        stream_class->event_header_type = event_header_type;
 end:
        if (ret) {
-               bt_ctf_field_type_put(event_header_type);
+               bt_put(event_header_type);
        }
 
-       bt_ctf_field_type_put(_uint32_t);
-       bt_ctf_field_type_put(_uint64_t);
+       bt_put(_uint32_t);
+       bt_put(_uint64_t);
        return ret;
 }
 
@@ -691,16 +877,14 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class)
                goto end;
        }
 
-       if (stream_class->packet_context_type) {
-               bt_ctf_field_type_put(stream_class->packet_context_type);
-       }
+       bt_put(stream_class->packet_context_type);
        stream_class->packet_context_type = packet_context_type;
 end:
        if (ret) {
-               bt_ctf_field_type_put(packet_context_type);
+               bt_put(packet_context_type);
                goto end;
        }
 
-       bt_ctf_field_type_put(_uint64_t);
+       bt_put(_uint64_t);
        return ret;
 }
This page took 0.030984 seconds and 4 git commands to generate.