Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / graph / port.c
index 4f974aa1fae91cd1eb1b6b95ef230d7c6e3dc165..25b427d2d0048a9ff7dc7b08ece32dfaf369c325 100644 (file)
@@ -35,6 +35,7 @@
 #include <babeltrace/graph/connection-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/compiler-internal.h>
+#include <babeltrace/assert-internal.h>
 
 static
 void bt_port_destroy(struct bt_object *obj)
@@ -51,10 +52,10 @@ void bt_port_destroy(struct bt_object *obj)
        g_free(port);
 }
 
-struct bt_port *bt_port_from_private_port(
+struct bt_port *bt_port_from_private(
                struct bt_private_port *private_port)
 {
-       return bt_get(bt_port_from_private(private_port));
+       return bt_get(bt_port_borrow_from_private(private_port));
 }
 
 BT_HIDDEN
@@ -63,9 +64,9 @@ struct bt_port *bt_port_create(struct bt_component *parent_component,
 {
        struct bt_port *port = NULL;
 
-       assert(name);
-       assert(parent_component);
-       assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
+       BT_ASSERT(name);
+       BT_ASSERT(parent_component);
+       BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
 
        if (strlen(name) == 0) {
                BT_LOGW_STR("Invalid parameter: name is an empty string.");
@@ -144,14 +145,14 @@ struct bt_private_connection *bt_private_port_get_private_connection(
                struct bt_private_port *private_port)
 {
        return bt_private_connection_from_connection(bt_port_get_connection(
-               bt_port_from_private(private_port)));
+               bt_port_borrow_from_private(private_port)));
 }
 
 struct bt_private_component *bt_private_port_get_private_component(
                struct bt_private_port *private_port)
 {
        return bt_private_component_from_component(bt_port_get_component(
-               bt_port_from_private(private_port)));
+               bt_port_borrow_from_private(private_port)));
 }
 
 BT_HIDDEN
@@ -169,48 +170,60 @@ void bt_port_set_connection(struct bt_port *port,
                port, bt_port_get_name(port), connection);
 }
 
-int bt_private_port_remove_from_component(
+enum bt_port_status bt_private_port_remove_from_component(
                struct bt_private_port *private_port)
 {
-       int ret = 0;
-       struct bt_port *port = bt_port_from_private(private_port);
+       enum bt_port_status status = BT_PORT_STATUS_OK;
+       struct bt_port *port = bt_port_borrow_from_private(private_port);
        struct bt_component *comp = NULL;
+       enum bt_component_status comp_status;
 
        if (!port) {
                BT_LOGW_STR("Invalid parameter: private port is NULL.");
-               ret = -1;
+               status = BT_PORT_STATUS_INVALID;
                goto end;
        }
 
        comp = (void *) bt_object_get_parent(port);
+       if (!comp) {
+               BT_LOGV("Port already removed from its component: "
+                       "port-addr=%p, port-name=\"%s\", ",
+                       port, bt_port_get_name(port));
+               goto end;
+       }
 
        /* bt_component_remove_port() logs details */
-       ret = bt_component_remove_port(comp, port);
+       comp_status = bt_component_remove_port(comp, port);
+       BT_ASSERT(comp_status != BT_COMPONENT_STATUS_INVALID);
+       if (comp_status < 0) {
+               status = BT_PORT_STATUS_ERROR;
+               goto end;
+       }
 
 end:
        bt_put(comp);
-       return ret;
+       return status;
 }
 
-int bt_port_disconnect(struct bt_port *port)
+enum bt_port_status bt_port_disconnect(struct bt_port *port)
 {
-       int ret = 0;
+       enum bt_port_status status = BT_PORT_STATUS_OK;
 
        if (!port) {
                BT_LOGW_STR("Invalid parameter: port is NULL.");
-               ret = -1;
+               status = BT_PORT_STATUS_INVALID;
                goto end;
        }
 
        if (port->connection) {
-               bt_connection_disconnect_ports(port->connection);
+               bt_connection_end(port->connection, true);
                BT_LOGV("Disconnected port: "
                        "port-addr=%p, port-name=\"%s\"",
                        port, bt_port_get_name(port));
        }
 
 end:
-       return ret;
+       return status;
 }
 
 bt_bool bt_port_is_connected(struct bt_port *port)
@@ -233,5 +246,5 @@ void *bt_private_port_get_user_data(
                struct bt_private_port *private_port)
 {
        return private_port ?
-               bt_port_from_private(private_port)->user_data : NULL;
+               bt_port_borrow_from_private(private_port)->user_data : NULL;
 }
This page took 0.025551 seconds and 4 git commands to generate.