Remove bt_component_set_name()
[babeltrace.git] / lib / graph / component.c
index e3464f45b646929b43952a78699fb73824d25f92..5e6a3303a1aa955770f0ddb4ab8117fb81d3490c 100644 (file)
@@ -41,6 +41,8 @@
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/ref.h>
+#include <babeltrace/types.h>
+#include <stdint.h>
 
 static
 struct bt_component * (* const component_create_funcs[])(
@@ -76,6 +78,16 @@ void bt_component_destroy(struct bt_object *obj)
                return;
        }
 
+       /*
+        * The component's reference count is 0 if we're here. Increment
+        * it to avoid a double-destroy (possibly infinitely recursive).
+        * This could happen for example if the component's finalization
+        * function does bt_get() (or anything that causes bt_get() to
+        * be called) on itself (ref. count goes from 0 to 1), and then
+        * bt_put(): the reference count would go from 1 to 0 again and
+        * this function would be called again.
+        */
+       obj->ref_count.count++;
        component = container_of(obj, struct bt_component, base);
 
        /* Call destroy listeners in reverse registration order */
@@ -134,7 +146,7 @@ enum bt_component_class_type bt_component_get_class_type(
 static
 struct bt_port *bt_component_add_port(
                struct bt_component *component, GPtrArray *ports,
-               enum bt_port_type port_type, const char *name)
+               enum bt_port_type port_type, const char *name, void *user_data)
 {
        size_t i;
        struct bt_port *new_port = NULL;
@@ -161,7 +173,7 @@ struct bt_port *bt_component_add_port(
                }
        }
 
-       new_port = bt_port_create(component, port_type, name);
+       new_port = bt_port_create(component, port_type, name, user_data);
        if (!new_port) {
                goto end;
        }
@@ -191,14 +203,14 @@ BT_HIDDEN
 int64_t bt_component_get_input_port_count(struct bt_component *comp)
 {
        assert(comp);
-       return comp->input_ports->len;
+       return (int64_t) comp->input_ports->len;
 }
 
 BT_HIDDEN
 int64_t bt_component_get_output_port_count(struct bt_component *comp)
 {
        assert(comp);
-       return comp->output_ports->len;
+       return (int64_t) comp->output_ports->len;
 }
 
 struct bt_component *bt_component_create_with_init_method_data(
@@ -208,7 +220,6 @@ struct bt_component *bt_component_create_with_init_method_data(
        int ret;
        struct bt_component *component = NULL;
        enum bt_component_class_type type;
-       struct bt_port *default_port = NULL;
 
        bt_get(params);
 
@@ -272,46 +283,20 @@ struct bt_component *bt_component_create_with_init_method_data(
                goto end;
        }
 
-       if (type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
-                       type == BT_COMPONENT_CLASS_TYPE_FILTER) {
-               default_port = bt_component_add_port(component,
-                       component->output_ports, BT_PORT_TYPE_OUTPUT,
-                       DEFAULT_OUTPUT_PORT_NAME);
-               if (!default_port) {
-                       BT_PUT(component);
-                       goto end;
-               }
-
-               BT_PUT(default_port);
-       }
-
-       if (type == BT_COMPONENT_CLASS_TYPE_FILTER ||
-                       type == BT_COMPONENT_CLASS_TYPE_SINK) {
-               default_port = bt_component_add_port(component,
-                       component->input_ports, BT_PORT_TYPE_INPUT,
-                       DEFAULT_INPUT_PORT_NAME);
-               if (!default_port) {
-                       BT_PUT(component);
-                       goto end;
-               }
-
-               BT_PUT(default_port);
-       }
-
-       component->initializing = true;
+       component->initializing = BT_TRUE;
 
        if (component_class->methods.init) {
                ret = component_class->methods.init(
                        bt_private_component_from_component(component), params,
                        init_method_data);
-               component->initializing = false;
+               component->initializing = BT_FALSE;
                if (ret != BT_COMPONENT_STATUS_OK) {
                        BT_PUT(component);
                        goto end;
                }
        }
 
-       component->initializing = false;
+       component->initializing = BT_FALSE;
        ret = component_validation_funcs[type](component);
        if (ret != BT_COMPONENT_STATUS_OK) {
                BT_PUT(component);
@@ -321,7 +306,6 @@ struct bt_component *bt_component_create_with_init_method_data(
        bt_component_class_freeze(component->class);
 end:
        bt_put(params);
-       bt_put(default_port);
        return component;
 }
 
@@ -346,21 +330,6 @@ end:
        return ret;
 }
 
-enum bt_component_status bt_component_set_name(struct bt_component *component,
-               const char *name)
-{
-       enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
-
-       if (!component || !name || name[0] == '\0') {
-               ret = BT_COMPONENT_STATUS_INVALID;
-               goto end;
-       }
-
-       g_string_assign(component->name, name);
-end:
-       return ret;
-}
-
 struct bt_component_class *bt_component_get_class(
                struct bt_component *component)
 {
@@ -414,7 +383,8 @@ struct bt_graph *bt_component_get_graph(
 }
 
 static
-struct bt_port *bt_component_get_port(GPtrArray *ports, const char *name)
+struct bt_port *bt_component_get_port_by_name(GPtrArray *ports,
+               const char *name)
 {
        size_t i;
        struct bt_port *ret_port = NULL;
@@ -439,29 +409,29 @@ struct bt_port *bt_component_get_port(GPtrArray *ports, const char *name)
 }
 
 BT_HIDDEN
-struct bt_port *bt_component_get_input_port(struct bt_component *comp,
+struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp,
                const char *name)
 {
        assert(comp);
 
-       return bt_component_get_port(comp->input_ports, name);
+       return bt_component_get_port_by_name(comp->input_ports, name);
 }
 
 BT_HIDDEN
-struct bt_port *bt_component_get_output_port(struct bt_component *comp,
+struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp,
                const char *name)
 {
        assert(comp);
 
-       return bt_component_get_port(comp->output_ports, name);
+       return bt_component_get_port_by_name(comp->output_ports, name);
 }
 
 static
-struct bt_port *bt_component_get_port_at_index(GPtrArray *ports, int index)
+struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index)
 {
        struct bt_port *port = NULL;
 
-       if (index < 0 || index >= ports->len) {
+       if (index >= ports->len) {
                goto end;
        }
 
@@ -471,41 +441,43 @@ end:
 }
 
 BT_HIDDEN
-struct bt_port *bt_component_get_input_port_at_index(struct bt_component *comp,
-               int index)
+struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp,
+               uint64_t index)
 {
        assert(comp);
 
-       return bt_component_get_port_at_index(comp->input_ports, index);
+       return bt_component_get_port_by_index(comp->input_ports, index);
 }
 
 BT_HIDDEN
-struct bt_port *bt_component_get_output_port_at_index(struct bt_component *comp,
-               int index)
+struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp,
+               uint64_t index)
 {
        assert(comp);
 
-       return bt_component_get_port_at_index(comp->output_ports, index);
+       return bt_component_get_port_by_index(comp->output_ports, index);
 }
 
 BT_HIDDEN
 struct bt_port *bt_component_add_input_port(
-               struct bt_component *component, const char *name)
+               struct bt_component *component, const char *name,
+               void *user_data)
 {
        return bt_component_add_port(component, component->input_ports,
-               BT_PORT_TYPE_INPUT, name);
+               BT_PORT_TYPE_INPUT, name, user_data);
 }
 
 BT_HIDDEN
 struct bt_port *bt_component_add_output_port(
-               struct bt_component *component, const char *name)
+               struct bt_component *component, const char *name,
+               void *user_data)
 {
        return bt_component_add_port(component, component->output_ports,
-               BT_PORT_TYPE_OUTPUT, name);
+               BT_PORT_TYPE_OUTPUT, name, user_data);
 }
 
 static
-void bt_component_remove_port_at_index(struct bt_component *component,
+void bt_component_remove_port_by_index(struct bt_component *component,
                GPtrArray *ports, size_t index)
 {
        struct bt_port *port;
@@ -561,7 +533,7 @@ enum bt_component_status bt_component_remove_port(
                struct bt_port *cur_port = g_ptr_array_index(ports, i);
 
                if (cur_port == port) {
-                       bt_component_remove_port_at_index(component,
+                       bt_component_remove_port_by_index(component,
                                ports, i);
                        goto end;
                }
This page took 0.040802 seconds and 4 git commands to generate.