cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / graph / port.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/PORT"
9 #include "lib/logging.h"
10
11 #include "common/assert.h"
12 #include "lib/assert-cond.h"
13 #include <babeltrace2/graph/port.h>
14 #include <babeltrace2/graph/self-component-port.h>
15 #include "lib/object.h"
16 #include "compat/compiler.h"
17
18 #include "component.h"
19 #include "connection.h"
20 #include "port.h"
21
22 static
23 void destroy_port(struct bt_object *obj)
24 {
25 struct bt_port *port = (void *) obj;
26
27 BT_LIB_LOGI("Destroying port: %!+p", port);
28
29 if (port->name) {
30 g_string_free(port->name, TRUE);
31 port->name = NULL;
32 }
33
34 g_free(port);
35 }
36
37 struct bt_port *bt_port_create(struct bt_component *parent_component,
38 enum bt_port_type type, const char *name, void *user_data)
39 {
40 struct bt_port *port = NULL;
41
42 BT_ASSERT(name);
43 BT_ASSERT(parent_component);
44 BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
45 BT_ASSERT(strlen(name) > 0);
46 port = g_new0(struct bt_port, 1);
47 if (!port) {
48 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one port.");
49 goto end;
50 }
51
52 BT_LIB_LOGI("Creating port for component: %![comp-]+c, port-type=%s, "
53 "port-name=\"%s\"", parent_component, bt_port_type_string(type),
54 name);
55 bt_object_init_shared_with_parent(&port->base, destroy_port);
56 port->name = g_string_new(name);
57 if (!port->name) {
58 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
59 BT_OBJECT_PUT_REF_AND_RESET(port);
60 goto end;
61 }
62
63 port->type = type;
64 port->user_data = user_data;
65 bt_object_set_parent(&port->base, &parent_component->base);
66 BT_LIB_LOGI("Created port for component: "
67 "%![comp-]+c, %![port-]+p", parent_component, port);
68
69 end:
70 return port;
71 }
72
73 BT_EXPORT
74 const char *bt_port_get_name(const struct bt_port *port)
75 {
76 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
77 return port->name->str;
78 }
79
80 BT_EXPORT
81 enum bt_port_type bt_port_get_type(const struct bt_port *port)
82 {
83 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
84 return port->type;
85 }
86
87 BT_EXPORT
88 const struct bt_connection *bt_port_borrow_connection_const(
89 const struct bt_port *port)
90 {
91 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
92 return port->connection;
93 }
94
95 BT_EXPORT
96 const struct bt_component *bt_port_borrow_component_const(
97 const struct bt_port *port)
98 {
99 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
100 return bt_port_borrow_component_inline(port);
101 }
102
103 BT_EXPORT
104 struct bt_self_component *bt_self_component_port_borrow_component(
105 struct bt_self_component_port *port)
106 {
107 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
108 return (void *) bt_object_borrow_parent((void *) port);
109 }
110
111 void bt_port_set_connection(struct bt_port *port,
112 struct bt_connection *connection)
113 {
114 /*
115 * Don't take a reference on connection as its existence is
116 * guaranteed by the existence of the graph in which the
117 * connection exists.
118 */
119 port->connection = connection;
120 BT_LIB_LOGI("Set port's connection: %![port-]+p, %![conn-]+x", port,
121 connection);
122 }
123
124 BT_EXPORT
125 bt_bool bt_port_is_connected(const struct bt_port *port)
126 {
127 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
128 return port->connection ? BT_TRUE : BT_FALSE;
129 }
130
131 BT_EXPORT
132 void *bt_self_component_port_get_data(const struct bt_self_component_port *port)
133 {
134 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
135 return ((struct bt_port *) port)->user_data;
136 }
137
138 BT_EXPORT
139 void bt_port_get_ref(const struct bt_port *port)
140 {
141 bt_object_get_ref(port);
142 }
143
144 BT_EXPORT
145 void bt_port_put_ref(const struct bt_port *port)
146 {
147 bt_object_put_ref(port);
148 }
149
150 BT_EXPORT
151 void bt_port_input_get_ref(const struct bt_port_input *port_input)
152 {
153 bt_object_get_ref(port_input);
154 }
155
156 BT_EXPORT
157 void bt_port_input_put_ref(const struct bt_port_input *port_input)
158 {
159 bt_object_put_ref(port_input);
160 }
161
162 BT_EXPORT
163 void bt_port_output_get_ref(const struct bt_port_output *port_output)
164 {
165 bt_object_get_ref(port_output);
166 }
167
168 BT_EXPORT
169 void bt_port_output_put_ref(const struct bt_port_output *port_output)
170 {
171 bt_object_put_ref(port_output);
172 }
This page took 0.037117 seconds and 4 git commands to generate.