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