Fix possible leaks in graph's current design
[babeltrace.git] / lib / graph / connection.c
CommitLineData
784cdc68 1/*
7d55361f 2 * connection.c
784cdc68
JG
3 *
4 * Babeltrace Connection
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
b2e0c907
PP
29#include <babeltrace/graph/notification-iterator-internal.h>
30#include <babeltrace/graph/component-internal.h>
31#include <babeltrace/graph/component-source-internal.h>
32#include <babeltrace/graph/component-filter-internal.h>
33#include <babeltrace/graph/connection-internal.h>
34#include <babeltrace/graph/private-connection.h>
35#include <babeltrace/graph/graph-internal.h>
36#include <babeltrace/graph/port-internal.h>
784cdc68 37#include <babeltrace/object-internal.h>
3d9990ac 38#include <babeltrace/compiler-internal.h>
784cdc68
JG
39#include <glib.h>
40
41static
42void bt_connection_destroy(struct bt_object *obj)
43{
44 struct bt_connection *connection = container_of(obj,
45 struct bt_connection, base);
bd14d768
PP
46 size_t i;
47
48 /*
49 * Make sure that each notification iterator which was created
50 * for this connection is finalized before we destroy it. Once a
51 * notification iterator is finalized, all its method return
52 * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
53 *
54 * Because connections are destroyed before components within a
55 * graph, this ensures that notification iterators are always
56 * finalized before their upstream component.
57 */
58 if (connection->iterators) {
59 for (i = 0; i < connection->iterators->len; i++) {
60 struct bt_notification_iterator *iterator =
61 g_ptr_array_index(connection->iterators, i);
62
63 bt_notification_iterator_finalize(iterator);
64
65 /*
66 * Make sure this iterator does not try to
67 * remove itself from this connection's
68 * iterators on destruction because this
69 * connection won't exist anymore.
70 */
71 bt_notification_iterator_set_connection(iterator,
72 NULL);
73 }
74
75 g_ptr_array_free(connection->iterators, TRUE);
76 }
784cdc68
JG
77
78 /*
79 * No bt_put on ports as a connection only holds _weak_ references
80 * to them.
81 */
82 g_free(connection);
83}
84
890882ef
PP
85struct bt_connection *bt_connection_from_private_connection(
86 struct bt_private_connection *private_connection)
87{
88 return bt_get(bt_connection_from_private(private_connection));
89}
90
784cdc68
JG
91BT_HIDDEN
92struct bt_connection *bt_connection_create(
93 struct bt_graph *graph,
72b913fb
PP
94 struct bt_port *upstream_port,
95 struct bt_port *downstream_port)
784cdc68
JG
96{
97 struct bt_connection *connection = NULL;
98
72b913fb 99 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
784cdc68
JG
100 goto end;
101 }
72b913fb 102 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
784cdc68
JG
103 goto end;
104 }
105
106 connection = g_new0(struct bt_connection, 1);
107 if (!connection) {
108 goto end;
109 }
110
111 bt_object_init(connection, bt_connection_destroy);
bd14d768
PP
112 connection->iterators = g_ptr_array_new();
113 if (!connection->iterators) {
114 BT_PUT(connection);
115 goto end;
116 }
117
784cdc68 118 /* Weak references are taken, see comment in header. */
72b913fb
PP
119 connection->upstream_port = upstream_port;
120 connection->downstream_port = downstream_port;
121 bt_port_set_connection(upstream_port, connection);
122 bt_port_set_connection(downstream_port, connection);
784cdc68
JG
123 bt_object_set_parent(connection, &graph->base);
124end:
125 return connection;
126}
127
72b913fb 128BT_HIDDEN
2038affb 129void bt_connection_disconnect_ports(struct bt_connection *conn)
72b913fb
PP
130{
131 struct bt_component *downstream_comp = NULL;
132 struct bt_component *upstream_comp = NULL;
133 struct bt_port *downstream_port = conn->downstream_port;
134 struct bt_port *upstream_port = conn->upstream_port;
f345f8bb 135 struct bt_graph *graph = (void *) bt_object_get_parent(conn);
72b913fb
PP
136
137 if (downstream_port) {
138 downstream_comp = bt_port_get_component(downstream_port);
139 bt_port_set_connection(downstream_port, NULL);
140 conn->downstream_port = NULL;
141 }
142
143 if (upstream_port) {
144 upstream_comp = bt_port_get_component(upstream_port);
145 bt_port_set_connection(upstream_port, NULL);
146 conn->upstream_port = NULL;
147 }
148
2038affb 149 if (downstream_comp) {
72b913fb
PP
150 bt_component_port_disconnected(downstream_comp,
151 downstream_port);
152 }
153
2038affb 154 if (upstream_comp) {
72b913fb
PP
155 bt_component_port_disconnected(upstream_comp, upstream_port);
156 }
157
f345f8bb
PP
158 assert(graph);
159 bt_graph_notify_ports_disconnected(graph, upstream_comp,
160 downstream_comp, upstream_port, downstream_port);
72b913fb
PP
161 bt_put(downstream_comp);
162 bt_put(upstream_comp);
f345f8bb 163 bt_put(graph);
72b913fb
PP
164}
165
166struct bt_port *bt_connection_get_upstream_port(
784cdc68
JG
167 struct bt_connection *connection)
168{
72b913fb 169 return connection ? bt_get(connection->upstream_port) : NULL;
784cdc68
JG
170}
171
72b913fb 172struct bt_port *bt_connection_get_downstream_port(
784cdc68
JG
173 struct bt_connection *connection)
174{
72b913fb 175 return connection ? bt_get(connection->downstream_port) : NULL;
784cdc68
JG
176}
177
178struct bt_notification_iterator *
890882ef 179bt_private_connection_create_notification_iterator(
fa054faf
PP
180 struct bt_private_connection *private_connection,
181 const enum bt_notification_type *notification_types)
784cdc68 182{
890882ef
PP
183 enum bt_notification_iterator_status ret_iterator;
184 enum bt_component_class_type upstream_comp_class_type;
185 struct bt_notification_iterator *iterator = NULL;
186 struct bt_port *upstream_port = NULL;
784cdc68 187 struct bt_component *upstream_component = NULL;
890882ef
PP
188 struct bt_component_class *upstream_comp_class = NULL;
189 struct bt_connection *connection = NULL;
190 bt_component_class_notification_iterator_init_method init_method = NULL;
fa054faf
PP
191 static const enum bt_notification_type all_notif_types[] = {
192 BT_NOTIFICATION_TYPE_ALL,
193 BT_NOTIFICATION_TYPE_SENTINEL,
194 };
784cdc68 195
890882ef
PP
196 if (!private_connection) {
197 goto error;
784cdc68
JG
198 }
199
fa054faf
PP
200 if (!notification_types) {
201 notification_types = all_notif_types;
202 }
203
890882ef 204 connection = bt_connection_from_private(private_connection);
72b913fb 205 if (!connection->upstream_port || !connection->downstream_port) {
890882ef 206 goto error;
72b913fb
PP
207 }
208
890882ef
PP
209 upstream_port = connection->upstream_port;
210 assert(upstream_port);
211 upstream_component = bt_port_get_component(upstream_port);
784cdc68 212 assert(upstream_component);
890882ef
PP
213 upstream_comp_class = upstream_component->class;
214
215 if (!upstream_component) {
216 goto error;
217 }
218
219 upstream_comp_class_type =
220 bt_component_get_class_type(upstream_component);
221 if (upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
222 upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_FILTER) {
223 /* Unsupported operation. */
224 goto error;
225 }
784cdc68 226
3230ee6b 227 iterator = bt_notification_iterator_create(upstream_component,
bd14d768 228 upstream_port, notification_types, connection);
890882ef
PP
229 if (!iterator) {
230 goto error;
231 }
232
233 switch (upstream_comp_class_type) {
784cdc68 234 case BT_COMPONENT_CLASS_TYPE_SOURCE:
890882ef
PP
235 {
236 struct bt_component_class_source *source_class =
237 container_of(upstream_comp_class,
238 struct bt_component_class_source, parent);
239 init_method = source_class->methods.iterator.init;
784cdc68 240 break;
890882ef 241 }
784cdc68 242 case BT_COMPONENT_CLASS_TYPE_FILTER:
890882ef
PP
243 {
244 struct bt_component_class_filter *filter_class =
245 container_of(upstream_comp_class,
246 struct bt_component_class_filter, parent);
247 init_method = filter_class->methods.iterator.init;
784cdc68 248 break;
890882ef 249 }
784cdc68 250 default:
890882ef
PP
251 /* Unreachable. */
252 assert(0);
253 }
254
255 if (init_method) {
256 enum bt_notification_iterator_status status = init_method(
91457551
PP
257 bt_private_notification_iterator_from_notification_iterator(iterator),
258 bt_private_port_from_port(upstream_port));
890882ef
PP
259 if (status < 0) {
260 goto error;
261 }
262 }
263
264 ret_iterator = bt_notification_iterator_validate(iterator);
265 if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
266 goto error;
784cdc68 267 }
890882ef 268
bd14d768 269 g_ptr_array_add(connection->iterators, iterator);
890882ef
PP
270 goto end;
271
272error:
273 BT_PUT(iterator);
274
784cdc68
JG
275end:
276 bt_put(upstream_component);
890882ef 277 return iterator;
784cdc68 278}
bd14d768
PP
279
280BT_HIDDEN
281void bt_connection_remove_iterator(struct bt_connection *conn,
282 struct bt_notification_iterator *iterator)
283{
284 g_ptr_array_remove(conn->iterators, iterator);
285}
This page took 0.038122 seconds and 4 git commands to generate.