Fix possible leaks in graph's current design
[babeltrace.git] / lib / graph / component.c
CommitLineData
de713ce0
JG
1/*
2 * component.c
3 *
4 * Babeltrace Plugin Component
5 *
6 * Copyright 2015 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/private-component.h>
30#include <babeltrace/graph/component.h>
31#include <babeltrace/graph/component-internal.h>
32#include <babeltrace/graph/component-class-internal.h>
33#include <babeltrace/graph/component-source-internal.h>
34#include <babeltrace/graph/component-filter-internal.h>
35#include <babeltrace/graph/component-sink-internal.h>
36#include <babeltrace/graph/private-connection.h>
37#include <babeltrace/graph/connection-internal.h>
38#include <babeltrace/graph/graph-internal.h>
39#include <babeltrace/graph/notification-iterator-internal.h>
40#include <babeltrace/graph/private-notification-iterator.h>
de713ce0 41#include <babeltrace/babeltrace-internal.h>
3d9990ac 42#include <babeltrace/compiler-internal.h>
b8a06801 43#include <babeltrace/ref.h>
c55a9f58 44#include <babeltrace/types.h>
9ac68eb1 45#include <stdint.h>
de713ce0 46
7c7c0433
JG
47static
48struct bt_component * (* const component_create_funcs[])(
49 struct bt_component_class *, struct bt_value *) = {
d3e4dcd8
PP
50 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
51 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
52 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
7c7c0433
JG
53};
54
72b913fb
PP
55static
56void (*component_destroy_funcs[])(struct bt_component *) = {
57 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
58 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
59 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
60};
61
7c7c0433
JG
62static
63enum bt_component_status (* const component_validation_funcs[])(
64 struct bt_component *) = {
d3e4dcd8
PP
65 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_validate,
66 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_validate,
67 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_validate,
7c7c0433
JG
68};
69
b8a06801
JG
70static
71void bt_component_destroy(struct bt_object *obj)
72{
73 struct bt_component *component = NULL;
74 struct bt_component_class *component_class = NULL;
3230ee6b 75 int i;
b8a06801
JG
76
77 if (!obj) {
78 return;
79 }
80
bd14d768
PP
81 /*
82 * The component's reference count is 0 if we're here. Increment
83 * it to avoid a double-destroy (possibly infinitely recursive).
84 * This could happen for example if the component's finalization
85 * function does bt_get() (or anything that causes bt_get() to
86 * be called) on itself (ref. count goes from 0 to 1), and then
87 * bt_put(): the reference count would go from 1 to 0 again and
88 * this function would be called again.
89 */
90 obj->ref_count.count++;
b8a06801 91 component = container_of(obj, struct bt_component, base);
3230ee6b
PP
92
93 /* Call destroy listeners in reverse registration order */
94 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
95 struct bt_component_destroy_listener *listener =
96 &g_array_index(component->destroy_listeners,
97 struct bt_component_destroy_listener, i);
98
99 listener->func(component, listener->data);
100 }
101
7c7c0433
JG
102 component_class = component->class;
103
104 /*
b8a06801
JG
105 * User data is destroyed first, followed by the concrete component
106 * instance.
107 */
64cadc66
PP
108 if (component->class->methods.finalize) {
109 component->class->methods.finalize(
890882ef 110 bt_private_component_from_component(component));
7c7c0433 111 }
b8a06801 112
ab09f844
JG
113 if (component->destroy) {
114 component->destroy(component);
115 }
116
72b913fb
PP
117 if (component->input_ports) {
118 g_ptr_array_free(component->input_ports, TRUE);
119 }
b8a06801 120
72b913fb
PP
121 if (component->output_ports) {
122 g_ptr_array_free(component->output_ports, TRUE);
b8a06801
JG
123 }
124
3230ee6b
PP
125 if (component->destroy_listeners) {
126 g_array_free(component->destroy_listeners, TRUE);
127 }
128
72b913fb
PP
129 g_string_free(component->name, TRUE);
130 bt_put(component_class);
131 g_free(component);
b8a06801 132}
de713ce0 133
890882ef
PP
134struct bt_component *bt_component_from_private_component(
135 struct bt_private_component *private_component)
38b48196 136{
890882ef 137 return bt_get(bt_component_from_private(private_component));
38b48196
JG
138}
139
890882ef
PP
140enum bt_component_class_type bt_component_get_class_type(
141 struct bt_component *component)
5645cd95 142{
890882ef 143 return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN;
5645cd95
JG
144}
145
72b913fb
PP
146static
147struct bt_port *bt_component_add_port(
148 struct bt_component *component, GPtrArray *ports,
3e9b0023 149 enum bt_port_type port_type, const char *name, void *user_data)
72b913fb
PP
150{
151 size_t i;
152 struct bt_port *new_port = NULL;
1bf957a0 153 struct bt_graph *graph = NULL;
72b913fb
PP
154
155 if (!name || strlen(name) == 0) {
156 goto end;
157 }
158
159 /* Look for a port having the same name. */
160 for (i = 0; i < ports->len; i++) {
161 const char *port_name;
162 struct bt_port *port = g_ptr_array_index(
163 ports, i);
164
165 port_name = bt_port_get_name(port);
166 if (!port_name) {
167 continue;
168 }
169
170 if (!strcmp(name, port_name)) {
171 /* Port name clash, abort. */
172 goto end;
173 }
174 }
175
3e9b0023 176 new_port = bt_port_create(component, port_type, name, user_data);
72b913fb
PP
177 if (!new_port) {
178 goto end;
179 }
180
181 /*
182 * No name clash, add the port.
183 * The component is now the port's parent; it should _not_
184 * hold a reference to the port since the port's lifetime
185 * is now protected by the component's own lifetime.
186 */
187 g_ptr_array_add(ports, new_port);
1bf957a0
PP
188
189 /*
190 * Notify the graph's creator that a new port was added.
191 */
192 graph = bt_component_get_graph(component);
193 if (graph) {
194 bt_graph_notify_port_added(graph, new_port);
195 BT_PUT(graph);
196 }
197
72b913fb
PP
198end:
199 return new_port;
200}
201
202BT_HIDDEN
544d0515 203int64_t bt_component_get_input_port_count(struct bt_component *comp)
72b913fb
PP
204{
205 assert(comp);
9ac68eb1 206 return (int64_t) comp->input_ports->len;
72b913fb
PP
207}
208
209BT_HIDDEN
544d0515 210int64_t bt_component_get_output_port_count(struct bt_component *comp)
72b913fb
PP
211{
212 assert(comp);
9ac68eb1 213 return (int64_t) comp->output_ports->len;
72b913fb
PP
214}
215
6358c163 216struct bt_component *bt_component_create_with_init_method_data(
7c7c0433 217 struct bt_component_class *component_class, const char *name,
6358c163 218 struct bt_value *params, void *init_method_data)
38b48196 219{
7c7c0433 220 int ret;
38b48196 221 struct bt_component *component = NULL;
d3e4dcd8 222 enum bt_component_class_type type;
38b48196 223
0df078e5
PP
224 bt_get(params);
225
38b48196
JG
226 if (!component_class) {
227 goto end;
228 }
229
7c7c0433 230 type = bt_component_class_get_type(component_class);
d3e4dcd8
PP
231 if (type <= BT_COMPONENT_CLASS_TYPE_UNKNOWN ||
232 type > BT_COMPONENT_CLASS_TYPE_FILTER) {
7c7c0433
JG
233 goto end;
234 }
235
0df078e5
PP
236 /*
237 * Parameters must be a map value, but we create a convenient
238 * empty one if it's NULL.
239 */
240 if (params) {
241 if (!bt_value_is_map(params)) {
242 goto end;
243 }
244 } else {
245 params = bt_value_map_create();
246 if (!params) {
247 goto end;
248 }
249 }
250
7c7c0433
JG
251 component = component_create_funcs[type](component_class, params);
252 if (!component) {
253 goto end;
254 }
255
256 bt_object_init(component, bt_component_destroy);
72b913fb
PP
257 component->class = bt_get(component_class);
258 component->destroy = component_destroy_funcs[type];
7c7c0433 259 component->name = g_string_new(name);
4b70dd83 260 if (!component->name) {
7c7c0433
JG
261 BT_PUT(component);
262 goto end;
263 }
264
72b913fb
PP
265 component->input_ports = g_ptr_array_new_with_free_func(
266 bt_object_release);
267 if (!component->input_ports) {
268 BT_PUT(component);
269 goto end;
270 }
271
272 component->output_ports = g_ptr_array_new_with_free_func(
273 bt_object_release);
274 if (!component->output_ports) {
275 BT_PUT(component);
276 goto end;
277 }
278
3230ee6b
PP
279 component->destroy_listeners = g_array_new(FALSE, TRUE,
280 sizeof(struct bt_component_destroy_listener));
281 if (!component->destroy_listeners) {
282 BT_PUT(component);
283 goto end;
284 }
285
c55a9f58 286 component->initializing = BT_TRUE;
72b913fb 287
d3e4dcd8 288 if (component_class->methods.init) {
890882ef
PP
289 ret = component_class->methods.init(
290 bt_private_component_from_component(component), params,
6358c163 291 init_method_data);
c55a9f58 292 component->initializing = BT_FALSE;
d3e4dcd8
PP
293 if (ret != BT_COMPONENT_STATUS_OK) {
294 BT_PUT(component);
295 goto end;
296 }
528debdf 297 }
d3e4dcd8 298
c55a9f58 299 component->initializing = BT_FALSE;
7c7c0433 300 ret = component_validation_funcs[type](component);
692b38d2 301 if (ret != BT_COMPONENT_STATUS_OK) {
7c7c0433 302 BT_PUT(component);
38b48196
JG
303 goto end;
304 }
1e4d8103
PP
305
306 bt_component_class_freeze(component->class);
38b48196 307end:
0df078e5 308 bt_put(params);
38b48196
JG
309 return component;
310}
311
6358c163
PP
312struct bt_component *bt_component_create(
313 struct bt_component_class *component_class, const char *name,
314 struct bt_value *params)
315{
316 return bt_component_create_with_init_method_data(component_class, name,
317 params, NULL);
318}
319
de713ce0
JG
320const char *bt_component_get_name(struct bt_component *component)
321{
322 const char *ret = NULL;
323
324 if (!component) {
325 goto end;
326 }
327
f1222e7d 328 ret = component->name->len == 0 ? NULL : component->name->str;
de713ce0
JG
329end:
330 return ret;
331}
332
333enum bt_component_status bt_component_set_name(struct bt_component *component,
334 const char *name)
335{
336 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
337
338 if (!component || !name || name[0] == '\0') {
30d619df 339 ret = BT_COMPONENT_STATUS_INVALID;
de713ce0
JG
340 goto end;
341 }
342
343 g_string_assign(component->name, name);
344end:
345 return ret;
346}
347
38b48196
JG
348struct bt_component_class *bt_component_get_class(
349 struct bt_component *component)
de713ce0 350{
38b48196 351 return component ? bt_get(component->class) : NULL;
de713ce0
JG
352}
353
890882ef
PP
354void *bt_private_component_get_user_data(
355 struct bt_private_component *private_component)
de713ce0 356{
890882ef
PP
357 struct bt_component *component =
358 bt_component_from_private(private_component);
359
38b48196 360 return component ? component->user_data : NULL;
de713ce0
JG
361}
362
890882ef
PP
363enum bt_component_status bt_private_component_set_user_data(
364 struct bt_private_component *private_component,
de713ce0
JG
365 void *data)
366{
890882ef
PP
367 struct bt_component *component =
368 bt_component_from_private(private_component);
de713ce0
JG
369 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
370
fec2a9f2 371 if (!component || !component->initializing) {
30d619df 372 ret = BT_COMPONENT_STATUS_INVALID;
de713ce0
JG
373 goto end;
374 }
375
376 component->user_data = data;
377end:
378 return ret;
379}
366e034f
JG
380
381BT_HIDDEN
f60c8b34 382void bt_component_set_graph(struct bt_component *component,
366e034f
JG
383 struct bt_graph *graph)
384{
f60c8b34
JG
385 struct bt_object *parent = bt_object_get_parent(&component->base);
386
387 assert(!parent || parent == &graph->base);
388 if (!parent) {
389 bt_object_set_parent(component, &graph->base);
390 }
391 bt_put(parent);
366e034f
JG
392}
393
394struct bt_graph *bt_component_get_graph(
395 struct bt_component *component)
396{
397 return (struct bt_graph *) bt_object_get_parent(&component->base);
398}
399
72b913fb 400static
9ac68eb1
PP
401struct bt_port *bt_component_get_port_by_name(GPtrArray *ports,
402 const char *name)
366e034f
JG
403{
404 size_t i;
405 struct bt_port *ret_port = NULL;
406
72b913fb
PP
407 assert(name);
408
366e034f
JG
409 for (i = 0; i < ports->len; i++) {
410 struct bt_port *port = g_ptr_array_index(ports, i);
411 const char *port_name = bt_port_get_name(port);
412
413 if (!port_name) {
414 continue;
415 }
416
417 if (!strcmp(name, port_name)) {
418 ret_port = bt_get(port);
419 break;
420 }
421 }
422
423 return ret_port;
424}
425
426BT_HIDDEN
9ac68eb1 427struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp,
72b913fb
PP
428 const char *name)
429{
430 assert(comp);
431
9ac68eb1 432 return bt_component_get_port_by_name(comp->input_ports, name);
72b913fb
PP
433}
434
435BT_HIDDEN
9ac68eb1 436struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp,
72b913fb
PP
437 const char *name)
438{
439 assert(comp);
440
9ac68eb1 441 return bt_component_get_port_by_name(comp->output_ports, name);
72b913fb
PP
442}
443
444static
9ac68eb1 445struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index)
366e034f
JG
446{
447 struct bt_port *port = NULL;
448
9ac68eb1 449 if (index >= ports->len) {
366e034f
JG
450 goto end;
451 }
452
453 port = bt_get(g_ptr_array_index(ports, index));
454end:
455 return port;
456}
457
458BT_HIDDEN
9ac68eb1
PP
459struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp,
460 uint64_t index)
366e034f 461{
72b913fb 462 assert(comp);
366e034f 463
9ac68eb1 464 return bt_component_get_port_by_index(comp->input_ports, index);
72b913fb 465}
366e034f 466
72b913fb 467BT_HIDDEN
9ac68eb1
PP
468struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp,
469 uint64_t index)
72b913fb
PP
470{
471 assert(comp);
366e034f 472
9ac68eb1 473 return bt_component_get_port_by_index(comp->output_ports, index);
72b913fb 474}
366e034f 475
72b913fb
PP
476BT_HIDDEN
477struct bt_port *bt_component_add_input_port(
3e9b0023
PP
478 struct bt_component *component, const char *name,
479 void *user_data)
72b913fb
PP
480{
481 return bt_component_add_port(component, component->input_ports,
3e9b0023 482 BT_PORT_TYPE_INPUT, name, user_data);
72b913fb 483}
366e034f 484
72b913fb
PP
485BT_HIDDEN
486struct bt_port *bt_component_add_output_port(
3e9b0023
PP
487 struct bt_component *component, const char *name,
488 void *user_data)
72b913fb
PP
489{
490 return bt_component_add_port(component, component->output_ports,
3e9b0023 491 BT_PORT_TYPE_OUTPUT, name, user_data);
72b913fb
PP
492}
493
494static
9ac68eb1 495void bt_component_remove_port_by_index(struct bt_component *component,
72b913fb
PP
496 GPtrArray *ports, size_t index)
497{
498 struct bt_port *port;
1bf957a0 499 struct bt_graph *graph;
72b913fb
PP
500
501 assert(ports);
502 assert(index < ports->len);
503 port = g_ptr_array_index(ports, index);
504
505 /* Disconnect both ports of this port's connection, if any */
506 if (port->connection) {
2038affb 507 bt_connection_disconnect_ports(port->connection);
f60c8b34
JG
508 }
509
72b913fb
PP
510 /* Remove from parent's array of ports (weak refs) */
511 g_ptr_array_remove_index(ports, index);
512
513 /* Detach port from its component parent */
514 BT_PUT(port->base.parent);
515
1bf957a0
PP
516 /*
517 * Notify the graph's creator that a port is removed.
518 */
519 graph = bt_component_get_graph(component);
520 if (graph) {
521 bt_graph_notify_port_removed(graph, component, port);
522 BT_PUT(graph);
523 }
366e034f
JG
524}
525
526BT_HIDDEN
527enum bt_component_status bt_component_remove_port(
72b913fb 528 struct bt_component *component, struct bt_port *port)
366e034f
JG
529{
530 size_t i;
531 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
72b913fb 532 GPtrArray *ports = NULL;
366e034f 533
72b913fb 534 if (!component || !port) {
366e034f
JG
535 status = BT_COMPONENT_STATUS_INVALID;
536 goto end;
537 }
538
72b913fb
PP
539 if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
540 ports = component->input_ports;
541 } else if (bt_port_get_type(port) == BT_PORT_TYPE_OUTPUT) {
542 ports = component->output_ports;
543 }
366e034f 544
72b913fb 545 assert(ports);
366e034f 546
72b913fb
PP
547 for (i = 0; i < ports->len; i++) {
548 struct bt_port *cur_port = g_ptr_array_index(ports, i);
549
550 if (cur_port == port) {
9ac68eb1 551 bt_component_remove_port_by_index(component,
72b913fb 552 ports, i);
366e034f
JG
553 goto end;
554 }
555 }
72b913fb 556
366e034f
JG
557 status = BT_COMPONENT_STATUS_NOT_FOUND;
558end:
559 return status;
560}
f60c8b34
JG
561
562BT_HIDDEN
72b913fb 563enum bt_component_status bt_component_accept_port_connection(
8f4799f7
PP
564 struct bt_component *comp, struct bt_port *self_port,
565 struct bt_port *other_port)
f60c8b34
JG
566{
567 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
568
72b913fb 569 assert(comp);
8f4799f7
PP
570 assert(self_port);
571 assert(other_port);
72b913fb
PP
572
573 if (comp->class->methods.accept_port_connection) {
574 status = comp->class->methods.accept_port_connection(
890882ef 575 bt_private_component_from_component(comp),
8f4799f7
PP
576 bt_private_port_from_port(self_port),
577 other_port);
f60c8b34
JG
578 }
579
580 return status;
581}
72b913fb 582
0d8b4d8e
PP
583BT_HIDDEN
584void bt_component_port_connected(struct bt_component *comp,
585 struct bt_port *self_port, struct bt_port *other_port)
586{
587 assert(comp);
588 assert(self_port);
589 assert(other_port);
590
591 if (comp->class->methods.port_connected) {
592 comp->class->methods.port_connected(
593 bt_private_component_from_component(comp),
594 bt_private_port_from_port(self_port), other_port);
595 }
596}
597
72b913fb
PP
598BT_HIDDEN
599void bt_component_port_disconnected(struct bt_component *comp,
600 struct bt_port *port)
601{
602 assert(comp);
603 assert(port);
604
605 if (comp->class->methods.port_disconnected) {
890882ef
PP
606 comp->class->methods.port_disconnected(
607 bt_private_component_from_component(comp),
608 bt_private_port_from_port(port));
72b913fb
PP
609 }
610}
3230ee6b
PP
611
612BT_HIDDEN
613void bt_component_add_destroy_listener(struct bt_component *component,
614 bt_component_destroy_listener_func func, void *data)
615{
616 struct bt_component_destroy_listener listener;
617
618 assert(component);
619 assert(func);
620 listener.func = func;
621 listener.data = data;
622 g_array_append_val(component->destroy_listeners, listener);
623}
624
625BT_HIDDEN
626void bt_component_remove_destroy_listener(struct bt_component *component,
627 bt_component_destroy_listener_func func, void *data)
628{
629 size_t i;
630
631 assert(component);
632 assert(func);
633
634 for (i = 0; i < component->destroy_listeners->len; i++) {
635 struct bt_component_destroy_listener *listener =
636 &g_array_index(component->destroy_listeners,
637 struct bt_component_destroy_listener, i);
638
639 if (listener->func == func && listener->data == data) {
640 g_array_remove_index(component->destroy_listeners, i);
641 i--;
642 }
643 }
644}
This page took 0.063881 seconds and 4 git commands to generate.