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