lib: graph: add "self" and some "private" APIs
[babeltrace.git] / lib / graph / port.c
1 /*
2 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_LOG_TAG "PORT"
26 #include <babeltrace/lib-logging-internal.h>
27
28 #include <babeltrace/graph/port.h>
29 #include <babeltrace/graph/self-component-port.h>
30 #include <babeltrace/graph/self-component-port-input.h>
31 #include <babeltrace/graph/self-component-port-output.h>
32 #include <babeltrace/graph/component-internal.h>
33 #include <babeltrace/graph/port-internal.h>
34 #include <babeltrace/graph/connection-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/object.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/assert-internal.h>
39 #include <babeltrace/assert-pre-internal.h>
40
41 static
42 void destroy_port(struct bt_object *obj)
43 {
44 struct bt_port *port = (void *) obj;
45
46 BT_LIB_LOGD("Destroying port: %!+p", port);
47
48 if (port->name) {
49 g_string_free(port->name, TRUE);
50 port->name = NULL;
51 }
52
53 g_free(port);
54 }
55
56 BT_HIDDEN
57 struct bt_port *bt_port_create(struct bt_component *parent_component,
58 enum bt_port_type type, const char *name, void *user_data)
59 {
60 struct bt_port *port = NULL;
61
62 BT_ASSERT(name);
63 BT_ASSERT(parent_component);
64 BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
65 BT_ASSERT(strlen(name) > 0);
66 port = g_new0(struct bt_port, 1);
67 if (!port) {
68 BT_LOGE_STR("Failed to allocate one port.");
69 goto end;
70 }
71
72 BT_LIB_LOGD("Creating port for component: %![comp-]+c, port-type=%s, "
73 "port-name=\"%s\"", parent_component, bt_port_type_string(type),
74 name);
75 bt_object_init_shared_with_parent(&port->base, destroy_port);
76 port->name = g_string_new(name);
77 if (!port->name) {
78 BT_LOGE_STR("Failed to allocate one GString.");
79 BT_OBJECT_PUT_REF_AND_RESET(port);
80 goto end;
81 }
82
83 port->type = type;
84 port->user_data = user_data;
85 bt_object_set_parent(&port->base, &parent_component->base);
86 BT_LIB_LOGD("Created port for component: "
87 "%![comp-]+c, %![port-]+p", parent_component, port);
88
89 end:
90 return port;
91 }
92
93 const char *bt_port_get_name(struct bt_port *port)
94 {
95 BT_ASSERT_PRE_NON_NULL(port, "Port");
96 return port->name->str;
97 }
98
99 enum bt_port_type bt_port_get_type(struct bt_port *port)
100 {
101 BT_ASSERT_PRE_NON_NULL(port, "Port");
102 return port->type;
103 }
104
105 struct bt_connection *bt_port_borrow_connection(struct bt_port *port)
106 {
107 BT_ASSERT_PRE_NON_NULL(port, "Port");
108 return port->connection;
109 }
110
111 struct bt_component *bt_port_borrow_component(struct bt_port *port)
112 {
113 BT_ASSERT_PRE_NON_NULL(port, "Port");
114 return (struct bt_component *) bt_object_borrow_parent(&port->base);
115 }
116
117 struct bt_self_component *bt_self_component_port_borrow_component(
118 struct bt_self_component_port *port)
119 {
120 BT_ASSERT_PRE_NON_NULL(port, "Port");
121 return (void *) bt_object_borrow_parent((void *) port);
122 }
123
124 BT_HIDDEN
125 void bt_port_set_connection(struct bt_port *port,
126 struct bt_connection *connection)
127 {
128 /*
129 * Don't take a reference on connection as its existence is
130 * guaranteed by the existence of the graph in which the
131 * connection exists.
132 */
133 port->connection = connection;
134 BT_LIB_LOGV("Set port's connection: %![port-]+p, %![conn-]+x", port,
135 connection);
136 }
137
138 enum bt_self_component_port_status bt_self_component_port_remove_from_component(
139 struct bt_self_component_port *self_port)
140 {
141 struct bt_port *port = (void *) self_port;
142 struct bt_component *comp = NULL;
143
144 BT_ASSERT_PRE_NON_NULL(port, "Port");
145
146 comp = (void *) bt_object_borrow_parent(&port->base);
147 if (!comp) {
148 BT_LIB_LOGV("Port already removed from its component: %!+p",
149 port);
150 goto end;
151 }
152
153 /* bt_component_remove_port() logs details */
154 bt_component_remove_port(comp, port);
155
156 end:
157 return BT_SELF_PORT_STATUS_OK;
158 }
159
160 bt_bool bt_port_is_connected(struct bt_port *port)
161 {
162 BT_ASSERT_PRE_NON_NULL(port, "Port");
163 return port->connection ? BT_TRUE : BT_FALSE;
164 }
165
166 void *bt_self_component_port_get_data(struct bt_self_component_port *port)
167 {
168 BT_ASSERT_PRE_NON_NULL(port, "Port");
169 return ((struct bt_port *) port)->user_data;
170 }
This page took 0.032259 seconds and 4 git commands to generate.