Fix: variable declaration shadows previously declared variable
[babeltrace.git] / lib / ctf-ir / stream-class.c
index a9f1c12a01cb88d184a771766b62e99b24093d4d..6fdb8ed2f5747b4cf5bc0b70d50bebf4b7412447 100644 (file)
@@ -97,12 +97,6 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
 
        BT_LOGD("Creating empty stream class object: name=\"%s\"", name);
 
-       if (name && bt_ctf_validate_identifier(name)) {
-               BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: "
-                       "name=\"%s\"", name);
-               goto error;
-       }
-
        stream_class = g_new0(struct bt_ctf_stream_class, 1);
        if (!stream_class) {
                BT_LOGE_STR("Failed to allocate one stream class.");
@@ -124,24 +118,6 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
                goto error;
        }
 
-       stream_class->packet_context_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->packet_context_type) {
-               BT_LOGE_STR("Cannot create stream class's initial packet context field type.");
-               goto error;
-       }
-
-       stream_class->event_header_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->event_header_type) {
-               BT_LOGE_STR("Cannot create stream class's initial event header field type.");
-               goto error;
-       }
-
-       stream_class->event_context_type = bt_ctf_field_type_structure_create();
-       if (!stream_class->event_context_type) {
-               BT_LOGE_STR("Cannot create stream class's initial event context field type.");
-               goto error;
-       }
-
        bt_object_init(stream_class, bt_ctf_stream_class_destroy);
        BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
                stream_class, name);
@@ -170,7 +146,7 @@ const char *bt_ctf_stream_class_get_name(
                goto end;
        }
 
-       name = stream_class->name->str;
+       name = stream_class->name->len > 0 ? stream_class->name->str : NULL;
 end:
        return name;
 }
@@ -195,7 +171,18 @@ int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       g_string_assign(stream_class->name, name);
+       if (!name) {
+               g_string_assign(stream_class->name, "");
+       } else {
+               if (strlen(name) == 0) {
+                       BT_LOGW("Invalid parameter: name is empty.");
+                       ret = -1;
+                       goto end;
+               }
+
+               g_string_assign(stream_class->name, name);
+       }
+
        BT_LOGV("Set stream class's name: "
                "addr=%p, name=\"%s\", id=%" PRId64,
                stream_class, bt_ctf_stream_class_get_name(stream_class),
@@ -307,41 +294,12 @@ struct event_class_set_stream_class_id_data {
        int ret;
 };
 
-static
-void event_class_set_stream_id(gpointer event_class, gpointer data)
-{
-       struct event_class_set_stream_class_id_data *typed_data = data;
-
-       typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
-               typed_data->stream_class_id);
-}
-
 BT_HIDDEN
 int bt_ctf_stream_class_set_id_no_check(
                struct bt_ctf_stream_class *stream_class, int64_t id)
 {
-       int ret = 0;
-       struct event_class_set_stream_class_id_data data =
-               { .stream_class_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) {
-               BT_LOGE("Cannot set the IDs of all stream class's event classes: "
-                       "addr=%p, name=\"%s\", id=%" PRId64,
-                       stream_class, bt_ctf_stream_class_get_name(stream_class),
-                       bt_ctf_stream_class_get_id(stream_class));
-               goto end;
-       }
-
        _bt_ctf_stream_class_set_id(stream_class, id);
-end:
-       return ret;
+       return 0;
 }
 
 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
@@ -575,13 +533,6 @@ int bt_ctf_stream_class_add_event_class(
                *event_id = stream_class->next_event_id;
        }
 
-       ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
-       if (ret) {
-               BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d",
-                       ret);
-               goto end;
-       }
-
        bt_object_set_parent(event_class, stream_class);
 
        if (trace) {
@@ -1009,6 +960,8 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
 {
        int ret = 0;
        size_t i;
+       struct bt_ctf_trace *trace;
+       struct bt_ctf_field_type *packet_header_type = NULL;
 
        BT_LOGD("Serializing stream class's metadata: "
                "stream-class-addr=%p, stream-class-name=\"%s\", "
@@ -1023,8 +976,38 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       g_string_append_printf(context->string,
-               "stream {\n\tid = %" PRId64 ";\n", stream_class->id);
+       g_string_append(context->string, "stream {\n");
+
+       /*
+        * The reference to the trace is only borrowed since the
+        * serialization of the stream class might have been triggered
+        * by the trace's destruction. In such a case, the trace's
+        * reference count would, unexepectedly, go through the sequence
+        * 1 -> 0 -> 1 -> 0 -> ..., provoking an endless loop of destruction
+        * and serialization.
+        */
+       trace = bt_ctf_stream_class_borrow_trace(stream_class);
+       assert(trace);
+       packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
+       trace = NULL;
+       if (packet_header_type) {
+               struct bt_ctf_field_type *stream_id_type;
+
+               stream_id_type =
+                       bt_ctf_field_type_structure_get_field_type_by_name(
+                               packet_header_type, "stream_id");
+               if (stream_id_type) {
+                       /*
+                        * Only set the stream's id if the trace's packet header
+                        * contains a stream_id field. This field is only
+                        * needed if the trace contains only one stream
+                        * class.
+                        */
+                       g_string_append_printf(context->string,
+                               "\tid = %" PRId64 ";\n", stream_class->id);
+               }
+               bt_put(stream_id_type);
+       }
        if (stream_class->event_header_type) {
                BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
                g_string_append(context->string, "\tevent.header := ");
@@ -1083,6 +1066,7 @@ int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
                }
        }
 end:
+       bt_put(packet_header_type);
        context->current_indentation_level = 0;
        return ret;
 }
This page took 0.027389 seconds and 4 git commands to generate.