Commit | Line | Data |
---|---|---|
8b285017 JG |
1 | /* |
2 | * component-port.c | |
3 | * | |
4 | * Babeltrace Port | |
5 | * | |
6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/component/component-internal.h> | |
30 | #include <babeltrace/component/component-port-internal.h> | |
31 | #include <babeltrace/object-internal.h> | |
32 | #include <babeltrace/compiler.h> | |
33 | ||
34 | static | |
35 | void bt_port_destroy(struct bt_object *obj) | |
36 | { | |
37 | struct bt_port *port = container_of(obj, struct bt_port, base); | |
38 | ||
39 | if (port->name) { | |
40 | g_string_free(port->name, TRUE); | |
41 | } | |
42 | if (port->connections) { | |
43 | g_ptr_array_free(port->connections, TRUE); | |
44 | } | |
45 | g_free(port); | |
46 | } | |
47 | ||
48 | BT_HIDDEN | |
49 | struct bt_port *bt_port_create(struct bt_component *parent_component, | |
50 | enum bt_port_type type, const char *name) | |
51 | { | |
52 | struct bt_port *port; | |
53 | ||
54 | assert(name); | |
55 | assert(parent_component); | |
56 | assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); | |
57 | ||
58 | if (*name == '\0') { | |
59 | port = NULL; | |
60 | goto end; | |
61 | } | |
62 | ||
63 | port = g_new0(struct bt_port, 1); | |
64 | if (!port) { | |
65 | goto end; | |
66 | } | |
67 | ||
68 | bt_object_init(port, bt_port_destroy); | |
69 | port->name = g_string_new(name); | |
70 | if (!port->name) { | |
71 | BT_PUT(port); | |
72 | goto end; | |
73 | } | |
74 | ||
75 | port->type = type; | |
76 | port->connections = g_ptr_array_new(); | |
77 | if (!port->connections) { | |
78 | BT_PUT(port); | |
79 | goto end; | |
80 | } | |
81 | ||
82 | port->max_connection_count = 1; | |
83 | ||
84 | bt_object_set_parent(port, &parent_component->base); | |
85 | end: | |
86 | return port; | |
87 | } | |
88 | ||
89 | const char *bt_port_get_name(struct bt_port *port) | |
90 | { | |
91 | return port ? port->name->str : NULL; | |
92 | } | |
93 | ||
94 | enum bt_port_type bt_port_get_type(struct bt_port *port) | |
95 | { | |
96 | return port ? port->type : BT_PORT_TYPE_UNKOWN; | |
97 | } | |
98 | ||
99 | enum bt_port_status bt_port_get_connection_count(struct bt_port *port, | |
100 | uint64_t *count) | |
101 | { | |
102 | enum bt_port_status status = BT_PORT_STATUS_OK; | |
103 | ||
104 | if (!port || !count) { | |
105 | status = BT_PORT_STATUS_INVALID; | |
106 | goto end; | |
107 | } | |
108 | ||
109 | *count = (uint64_t) port->connections->len; | |
110 | end: | |
111 | return status; | |
112 | } | |
113 | ||
114 | struct bt_connection *bt_port_get_connection(struct bt_port *port, int index) | |
115 | { | |
116 | struct bt_connection *connection; | |
117 | ||
118 | if (!port || index < 0 || index >= port->connections->len) { | |
119 | connection = NULL; | |
120 | goto end; | |
121 | } | |
122 | ||
123 | connection = bt_get(g_ptr_array_index(port->connections, index)); | |
124 | end: | |
125 | return connection; | |
126 | } | |
127 | ||
128 | struct bt_component *bt_port_get_component(struct bt_port *port) | |
129 | { | |
130 | return (struct bt_component *) bt_object_get_parent(port); | |
131 | } | |
132 | ||
133 | BT_HIDDEN | |
134 | int bt_port_add_connection(struct bt_port *port, | |
135 | struct bt_connection *connection) | |
136 | { | |
137 | int ret = 0; | |
138 | ||
139 | if (port->connections->len == port->max_connection_count) { | |
140 | ret = -1; | |
141 | goto end; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Don't take a reference on connection as its existence is guaranteed | |
146 | * by the existence of the graph in which the connection exists. | |
147 | */ | |
148 | g_ptr_array_add(port->connections, connection); | |
149 | end: | |
150 | return ret; | |
151 | } | |
152 | ||
153 | enum bt_port_status bt_port_get_maximum_connection_count( | |
154 | struct bt_port *port, uint64_t *count) | |
155 | { | |
156 | enum bt_port_status status = BT_PORT_STATUS_OK; | |
157 | ||
158 | if (!port || !count) { | |
159 | status = BT_PORT_STATUS_INVALID; | |
160 | goto end; | |
161 | } | |
162 | ||
163 | *count = port->max_connection_count; | |
164 | end: | |
165 | return status; | |
166 | } | |
167 | ||
168 | enum bt_port_status bt_port_set_maximum_connection_count( | |
169 | struct bt_port *port, uint64_t count) | |
170 | { | |
171 | enum bt_port_status status = BT_PORT_STATUS_OK; | |
172 | ||
173 | if (!port || count < port->connections->len || count == 0) { | |
174 | status = BT_PORT_STATUS_INVALID; | |
175 | goto end; | |
176 | } | |
177 | ||
178 | port->max_connection_count = count; | |
179 | end: | |
180 | return status; | |
181 | } |