lib: rename include dir to babeltrace2
[babeltrace.git] / lib / graph / port.c
index c0d5e061d2cd49e271d794de3bbe1e064a67fd5f..f119d60b6dfb5414ac805cc90b3b1550c4296f2b 100644 (file)
@@ -1,12 +1,7 @@
 /*
- * port.c
- *
- * Babeltrace Port
- *
+ * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
  * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * SOFTWARE.
  */
 
-#include <babeltrace/graph/component-internal.h>
-#include <babeltrace/graph/port-internal.h>
-#include <babeltrace/graph/connection-internal.h>
-#include <babeltrace/object-internal.h>
-#include <babeltrace/compiler-internal.h>
+#define BT_LOG_TAG "PORT"
+#include <babeltrace2/lib-logging-internal.h>
+
+#include <babeltrace2/assert-internal.h>
+#include <babeltrace2/assert-pre-internal.h>
+#include <babeltrace2/graph/port-const.h>
+#include <babeltrace2/graph/port-input-const.h>
+#include <babeltrace2/graph/port-output-const.h>
+#include <babeltrace2/graph/self-component-port.h>
+#include <babeltrace2/graph/self-component-port-input.h>
+#include <babeltrace2/graph/self-component-port-output.h>
+#include <babeltrace2/graph/component-internal.h>
+#include <babeltrace2/graph/port-internal.h>
+#include <babeltrace2/graph/connection-internal.h>
+#include <babeltrace2/object-internal.h>
+#include <babeltrace2/compiler-internal.h>
 
 static
-void bt_port_destroy(struct bt_object *obj)
+void destroy_port(struct bt_object *obj)
 {
-       struct bt_port *port = container_of(obj, struct bt_port, base);
+       struct bt_port *port = (void *) obj;
+
+       BT_LIB_LOGD("Destroying port: %!+p", port);
 
        if (port->name) {
                g_string_free(port->name, TRUE);
+               port->name = NULL;
        }
-       g_free(port);
-}
 
-struct bt_port *bt_port_from_private_port(
-               struct bt_private_port *private_port)
-{
-       return bt_get(bt_port_from_private(private_port));
+       g_free(port);
 }
 
 BT_HIDDEN
 struct bt_port *bt_port_create(struct bt_component *parent_component,
-               enum bt_port_type type, const char *name)
+               enum bt_port_type type, const char *name, void *user_data)
 {
        struct bt_port *port = NULL;
 
-       assert(name);
-       assert(parent_component);
-       assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
-
-       if (strlen(name) == 0) {
-               goto end;
-       }
-
+       BT_ASSERT(name);
+       BT_ASSERT(parent_component);
+       BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
+       BT_ASSERT(strlen(name) > 0);
        port = g_new0(struct bt_port, 1);
        if (!port) {
+               BT_LOGE_STR("Failed to allocate one port.");
                goto end;
        }
 
-       bt_object_init(port, bt_port_destroy);
+       BT_LIB_LOGD("Creating port for component: %![comp-]+c, port-type=%s, "
+               "port-name=\"%s\"", parent_component, bt_port_type_string(type),
+               name);
+       bt_object_init_shared_with_parent(&port->base, destroy_port);
        port->name = g_string_new(name);
        if (!port->name) {
-               BT_PUT(port);
+               BT_LOGE_STR("Failed to allocate one GString.");
+               BT_OBJECT_PUT_REF_AND_RESET(port);
                goto end;
        }
 
        port->type = type;
+       port->user_data = user_data;
+       bt_object_set_parent(&port->base, &parent_component->base);
+       BT_LIB_LOGD("Created port for component: "
+               "%![comp-]+c, %![port-]+p", parent_component, port);
 
-       bt_object_set_parent(port, &parent_component->base);
 end:
        return port;
 }
 
-const char *bt_port_get_name(struct bt_port *port)
-{
-       return port ? port->name->str : NULL;
-}
-
-enum bt_port_type bt_port_get_type(struct bt_port *port)
+const char *bt_port_get_name(const struct bt_port *port)
 {
-       return port ? port->type : BT_PORT_TYPE_UNKOWN;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->name->str;
 }
 
-struct bt_connection *bt_port_get_connection(struct bt_port *port)
+enum bt_port_type bt_port_get_type(const struct bt_port *port)
 {
-       struct bt_connection *connection = NULL;
-
-       if (!port || !port->connection) {
-               goto end;
-       }
-
-       connection = bt_get(port->connection);
-end:
-       return connection;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->type;
 }
 
-struct bt_component *bt_port_get_component(struct bt_port *port)
+const struct bt_connection *bt_port_borrow_connection_const(
+               const struct bt_port *port)
 {
-       return (struct bt_component *) bt_object_get_parent(port);
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->connection;
 }
 
-struct bt_private_connection *bt_private_port_get_private_connection(
-               struct bt_private_port *private_port)
+const struct bt_component *bt_port_borrow_component_const(
+               const struct bt_port *port)
 {
-       return bt_private_connection_from_connection(bt_port_get_connection(
-               bt_port_from_private(private_port)));
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return bt_port_borrow_component_inline(port);
 }
 
-struct bt_private_component *bt_private_port_get_private_component(
-               struct bt_private_port *private_port)
+struct bt_self_component *bt_self_component_port_borrow_component(
+               struct bt_self_component_port *port)
 {
-       return bt_private_component_from_component(bt_port_get_component(
-               bt_port_from_private(private_port)));
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return (void *) bt_object_borrow_parent((void *) port);
 }
 
 BT_HIDDEN
@@ -134,79 +133,48 @@ void bt_port_set_connection(struct bt_port *port,
         * connection exists.
         */
        port->connection = connection;
+       BT_LIB_LOGV("Set port's connection: %![port-]+p, %![conn-]+x", port,
+               connection);
 }
 
-int bt_private_port_remove_from_component(
-               struct bt_private_port *private_port)
+bt_bool bt_port_is_connected(const struct bt_port *port)
 {
-       int ret = 0;
-       struct bt_port *port = bt_port_from_private(private_port);
-       struct bt_component *comp = NULL;
-
-       if (!port) {
-               ret = -1;
-               goto end;
-       }
-
-       comp = (void *) bt_object_get_parent(port);
-       ret = bt_component_remove_port(comp, port);
-
-end:
-       bt_put(comp);
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return port->connection ? BT_TRUE : BT_FALSE;
 }
 
-int bt_port_disconnect(struct bt_port *port)
+void *bt_self_component_port_get_data(const struct bt_self_component_port *port)
 {
-       int ret = 0;
-
-       if (!port) {
-               ret = -1;
-               goto end;
-       }
-
-       if (port->connection) {
-               bt_connection_disconnect_ports(port->connection);
-       }
-
-end:
-       return ret;
+       BT_ASSERT_PRE_NON_NULL(port, "Port");
+       return ((struct bt_port *) port)->user_data;
 }
 
-int bt_port_is_connected(struct bt_port *port)
+void bt_port_get_ref(const struct bt_port *port)
 {
-       int ret;
-
-       if (!port) {
-               ret = -1;
-               goto end;
-       }
-
-       ret = port->connection ? 1 : 0;
-
-end:
-       return ret;
+       bt_object_get_ref(port);
 }
 
-int bt_private_port_set_user_data(
-               struct bt_private_port *private_port, void *user_data)
+void bt_port_put_ref(const struct bt_port *port)
 {
-       int ret = 0;
+       bt_object_put_ref(port);
+}
 
-       if (!private_port) {
-               ret = -1;
-               goto end;
-       }
+void bt_port_input_get_ref(const struct bt_port_input *port_input)
+{
+       bt_object_get_ref(port_input);
+}
 
-       bt_port_from_private(private_port)->user_data = user_data;
+void bt_port_input_put_ref(const struct bt_port_input *port_input)
+{
+       bt_object_put_ref(port_input);
+}
 
-end:
-       return ret;
+void bt_port_output_get_ref(const struct bt_port_output *port_output)
+{
+       bt_object_get_ref(port_output);
 }
 
-void *bt_private_port_get_user_data(
-               struct bt_private_port *private_port)
+void bt_port_output_put_ref(const struct bt_port_output *port_output)
 {
-       return private_port ?
-               bt_port_from_private(private_port)->user_data : NULL;
+       bt_object_put_ref(port_output);
 }
This page took 0.026995 seconds and 4 git commands to generate.