81c573b16dd23d2f8449141c8f1ed415bb4c543c
[babeltrace.git] / lib / graph / component.c
1 /*
2 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_LOG_TAG "COMP"
26 #include <babeltrace/lib-logging-internal.h>
27
28 #include <babeltrace/graph/self-component.h>
29 #include <babeltrace/graph/component-const.h>
30 #include <babeltrace/graph/component-source-const.h>
31 #include <babeltrace/graph/component-filter-const.h>
32 #include <babeltrace/graph/component-sink-const.h>
33 #include <babeltrace/graph/component-internal.h>
34 #include <babeltrace/graph/component-class-internal.h>
35 #include <babeltrace/graph/component-source-internal.h>
36 #include <babeltrace/graph/component-filter-internal.h>
37 #include <babeltrace/graph/component-sink-internal.h>
38 #include <babeltrace/graph/connection-internal.h>
39 #include <babeltrace/graph/graph-internal.h>
40 #include <babeltrace/graph/notification-iterator-internal.h>
41 #include <babeltrace/graph/port-internal.h>
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/object.h>
45 #include <babeltrace/types.h>
46 #include <babeltrace/value.h>
47 #include <babeltrace/value-internal.h>
48 #include <babeltrace/assert-internal.h>
49 #include <babeltrace/assert-pre-internal.h>
50 #include <stdint.h>
51 #include <inttypes.h>
52
53 static
54 struct bt_component * (* const component_create_funcs[])(
55 const struct bt_component_class *) = {
56 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
57 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
58 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
59 };
60
61 static
62 void (*component_destroy_funcs[])(struct bt_component *) = {
63 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
64 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
65 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
66 };
67
68 static
69 void finalize_component(struct bt_component *comp)
70 {
71 typedef void (*method_t)(void *);
72
73 method_t method = NULL;
74
75 BT_ASSERT(comp);
76
77 switch (comp->class->type) {
78 case BT_COMPONENT_CLASS_TYPE_SOURCE:
79 {
80 struct bt_component_class_source *src_cc = (void *) comp->class;
81
82 method = (method_t) src_cc->methods.finalize;
83 break;
84 }
85 case BT_COMPONENT_CLASS_TYPE_FILTER:
86 {
87 struct bt_component_class_filter *flt_cc = (void *) comp->class;
88
89 method = (method_t) flt_cc->methods.finalize;
90 break;
91 }
92 case BT_COMPONENT_CLASS_TYPE_SINK:
93 {
94 struct bt_component_class_sink *sink_cc = (void *) comp->class;
95
96 method = (method_t) sink_cc->methods.finalize;
97 break;
98 }
99 default:
100 abort();
101 }
102
103 if (method) {
104 BT_LIB_LOGD("Calling user's finalization method: "
105 "%![comp-]+c", comp);
106 method(comp);
107 }
108 }
109
110 static
111 void destroy_component(struct bt_object *obj)
112 {
113 struct bt_component *component = NULL;
114 int i;
115
116 if (!obj) {
117 return;
118 }
119
120 /*
121 * The component's reference count is 0 if we're here. Increment
122 * it to avoid a double-destroy (possibly infinitely recursive).
123 * This could happen for example if the component's finalization
124 * function does bt_object_get_ref() (or anything that causes
125 * bt_object_get_ref() to be called) on itself (ref. count goes
126 * from 0 to 1), and then bt_object_put_ref(): the reference
127 * count would go from 1 to 0 again and this function would be
128 * called again.
129 */
130 obj->ref_count++;
131 component = container_of(obj, struct bt_component, base);
132 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
133 component, bt_component_borrow_graph(component));
134
135 /* Call destroy listeners in reverse registration order */
136 BT_LOGD_STR("Calling destroy listeners.");
137
138 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
139 struct bt_component_destroy_listener *listener =
140 &g_array_index(component->destroy_listeners,
141 struct bt_component_destroy_listener, i);
142
143 listener->func(component, listener->data);
144 }
145
146 /*
147 * User data is destroyed first, followed by the concrete
148 * component instance. Do not finalize if the component's user
149 * initialization method failed in the first place.
150 */
151 if (component->initialized) {
152 finalize_component(component);
153 }
154
155 if (component->destroy) {
156 BT_LOGD_STR("Destroying type-specific data.");
157 component->destroy(component);
158 }
159
160 if (component->input_ports) {
161 BT_LOGD_STR("Destroying input ports.");
162 g_ptr_array_free(component->input_ports, TRUE);
163 component->input_ports = NULL;
164 }
165
166 if (component->output_ports) {
167 BT_LOGD_STR("Destroying output ports.");
168 g_ptr_array_free(component->output_ports, TRUE);
169 component->output_ports = NULL;
170 }
171
172 if (component->destroy_listeners) {
173 g_array_free(component->destroy_listeners, TRUE);
174 component->destroy_listeners = NULL;
175 }
176
177 if (component->name) {
178 g_string_free(component->name, TRUE);
179 component->name = NULL;
180 }
181
182 BT_LOGD_STR("Putting component class.");
183 BT_OBJECT_PUT_REF_AND_RESET(component->class);
184 g_free(component);
185 }
186
187 enum bt_component_class_type bt_component_get_class_type(
188 const struct bt_component *component)
189 {
190 BT_ASSERT_PRE_NON_NULL(component, "Component");
191 return component->class->type;
192 }
193
194 static
195 struct bt_port *add_port(
196 struct bt_component *component, GPtrArray *ports,
197 enum bt_port_type port_type, const char *name, void *user_data)
198 {
199 struct bt_port *new_port = NULL;
200 struct bt_graph *graph = NULL;
201
202 BT_ASSERT_PRE_NON_NULL(component, "Component");
203 BT_ASSERT_PRE_NON_NULL(name, "Name");
204 BT_ASSERT_PRE(strlen(name) > 0, "Name is empty");
205 graph = bt_component_borrow_graph(component);
206 BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph),
207 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
208 component, graph);
209
210 // TODO: Validate that the name is not already used.
211
212 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
213 "port-type=%s, port-name=\"%s\"", component,
214 bt_port_type_string(port_type), name);
215
216 new_port = bt_port_create(component, port_type, name, user_data);
217 if (!new_port) {
218 BT_LOGE_STR("Cannot create port object.");
219 goto end;
220 }
221
222 /*
223 * No name clash, add the port.
224 * The component is now the port's parent; it should _not_
225 * hold a reference to the port since the port's lifetime
226 * is now protected by the component's own lifetime.
227 */
228 g_ptr_array_add(ports, new_port);
229
230 /*
231 * Notify the graph's creator that a new port was added.
232 */
233 bt_object_get_ref(bt_component_borrow_graph(component));
234 graph = bt_component_borrow_graph(component);
235 if (graph) {
236 bt_graph_notify_port_added(graph, new_port);
237 BT_OBJECT_PUT_REF_AND_RESET(graph);
238 }
239
240 BT_LIB_LOGD("Created and added port to component: "
241 "%![comp-]+c, %![port-]+p", component, new_port);
242
243 end:
244 return new_port;
245 }
246
247 BT_HIDDEN
248 uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
249 {
250 BT_ASSERT_PRE_NON_NULL(comp, "Component");
251 return (uint64_t) comp->input_ports->len;
252 }
253
254 BT_HIDDEN
255 uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
256 {
257 BT_ASSERT_PRE_NON_NULL(comp, "Component");
258 return (uint64_t) comp->output_ports->len;
259 }
260
261 BT_HIDDEN
262 int bt_component_create(struct bt_component_class *component_class,
263 const char *name, struct bt_component **user_component)
264 {
265 int ret = 0;
266 struct bt_component *component = NULL;
267 enum bt_component_class_type type;
268
269 BT_ASSERT(user_component);
270 BT_ASSERT(component_class);
271 BT_ASSERT(name);
272
273 type = bt_component_class_get_type(component_class);
274 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
275 "comp-name=\"%s\"", component_class, name);
276 component = component_create_funcs[type](component_class);
277 if (!component) {
278 BT_LOGE_STR("Cannot create specific component object.");
279 ret = -1;
280 goto end;
281 }
282
283 bt_object_init_shared_with_parent(&component->base,
284 destroy_component);
285 component->class = component_class;
286 bt_object_get_no_null_check(component->class);
287 component->destroy = component_destroy_funcs[type];
288 component->name = g_string_new(name);
289 if (!component->name) {
290 BT_LOGE_STR("Failed to allocate one GString.");
291 ret = -1;
292 goto end;
293 }
294
295 component->input_ports = g_ptr_array_new_with_free_func(
296 (GDestroyNotify) bt_object_try_spec_release);
297 if (!component->input_ports) {
298 BT_LOGE_STR("Failed to allocate one GPtrArray.");
299 ret = -1;
300 goto end;
301 }
302
303 component->output_ports = g_ptr_array_new_with_free_func(
304 (GDestroyNotify) bt_object_try_spec_release);
305 if (!component->output_ports) {
306 BT_LOGE_STR("Failed to allocate one GPtrArray.");
307 ret = -1;
308 goto end;
309 }
310
311 component->destroy_listeners = g_array_new(FALSE, TRUE,
312 sizeof(struct bt_component_destroy_listener));
313 if (!component->destroy_listeners) {
314 BT_LOGE_STR("Failed to allocate one GArray.");
315 ret = -1;
316 goto end;
317 }
318
319 BT_LIB_LOGD("Created empty component from component class: "
320 "%![cc-]+C, %![comp-]+c", component_class, component);
321 BT_OBJECT_MOVE_REF(*user_component, component);
322
323 end:
324 bt_object_put_ref(component);
325 return ret;
326 }
327
328 const char *bt_component_get_name(const struct bt_component *component)
329 {
330 BT_ASSERT_PRE_NON_NULL(component, "Component");
331 return component->name->str;
332 }
333
334 struct bt_component_class *bt_component_borrow_class(
335 const struct bt_component *component)
336 {
337 BT_ASSERT_PRE_NON_NULL(component, "Component");
338 return component->class;
339 }
340
341 void *bt_self_component_get_data(const struct bt_self_component *self_comp)
342 {
343 struct bt_component *component = (void *) self_comp;
344
345 BT_ASSERT_PRE_NON_NULL(component, "Component");
346 return component->user_data;
347 }
348
349 void bt_self_component_set_data(struct bt_self_component *self_comp,
350 void *data)
351 {
352 struct bt_component *component = (void *) self_comp;
353
354 BT_ASSERT_PRE_NON_NULL(component, "Component");
355 component->user_data = data;
356 BT_LIB_LOGV("Set component's user data: %!+c", component);
357 }
358
359 BT_HIDDEN
360 void bt_component_set_graph(struct bt_component *component,
361 struct bt_graph *graph)
362 {
363 bt_object_set_parent(&component->base,
364 graph ? &graph->base : NULL);
365 }
366
367 bt_bool bt_component_graph_is_canceled(const struct bt_component *component)
368 {
369 return bt_graph_is_canceled(
370 (void *) bt_object_borrow_parent(&component->base));
371 }
372
373 static
374 struct bt_port *borrow_port_by_name(GPtrArray *ports,
375 const char *name)
376 {
377 uint64_t i;
378 struct bt_port *ret_port = NULL;
379
380 BT_ASSERT(name);
381
382 for (i = 0; i < ports->len; i++) {
383 struct bt_port *port = g_ptr_array_index(ports, i);
384
385 if (!strcmp(name, port->name->str)) {
386 ret_port = port;
387 break;
388 }
389 }
390
391 return ret_port;
392 }
393
394 BT_HIDDEN
395 struct bt_port_input *bt_component_borrow_input_port_by_name(
396 struct bt_component *comp, const char *name)
397 {
398 BT_ASSERT(comp);
399 return (void *) borrow_port_by_name(comp->input_ports, name);
400 }
401
402 BT_HIDDEN
403 struct bt_port_output *bt_component_borrow_output_port_by_name(
404 struct bt_component *comp, const char *name)
405 {
406 BT_ASSERT_PRE_NON_NULL(comp, "Component");
407 return (void *)
408 borrow_port_by_name(comp->output_ports, name);
409 }
410
411 static
412 struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index)
413 {
414 BT_ASSERT(index < ports->len);
415 return g_ptr_array_index(ports, index);
416 }
417
418 BT_HIDDEN
419 struct bt_port_input *bt_component_borrow_input_port_by_index(
420 struct bt_component *comp, uint64_t index)
421 {
422 BT_ASSERT_PRE_NON_NULL(comp, "Component");
423 BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len);
424 return (void *)
425 borrow_port_by_index(comp->input_ports, index);
426 }
427
428 BT_HIDDEN
429 struct bt_port_output *bt_component_borrow_output_port_by_index(
430 struct bt_component *comp, uint64_t index)
431 {
432 BT_ASSERT_PRE_NON_NULL(comp, "Component");
433 BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len);
434 return (void *)
435 borrow_port_by_index(comp->output_ports, index);
436 }
437
438 BT_HIDDEN
439 struct bt_port_input *bt_component_add_input_port(
440 struct bt_component *component, const char *name,
441 void *user_data)
442 {
443 /* add_port() logs details */
444 return (void *)
445 add_port(component, component->input_ports,
446 BT_PORT_TYPE_INPUT, name, user_data);
447 }
448
449 BT_HIDDEN
450 struct bt_port_output *bt_component_add_output_port(
451 struct bt_component *component, const char *name,
452 void *user_data)
453 {
454 /* add_port() logs details */
455 return (void *)
456 add_port(component, component->output_ports,
457 BT_PORT_TYPE_OUTPUT, name, user_data);
458 }
459
460 static
461 void remove_port_by_index(struct bt_component *component,
462 GPtrArray *ports, uint64_t index)
463 {
464 struct bt_port *port;
465 struct bt_graph *graph;
466
467 BT_ASSERT(ports);
468 BT_ASSERT(index < ports->len);
469 port = g_ptr_array_index(ports, index);
470 BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p",
471 component, port);
472
473 /* Disconnect both ports of this port's connection, if any */
474 if (port->connection) {
475 bt_connection_end(port->connection, true);
476 }
477
478 /*
479 * The port's current reference count can be 0 at this point,
480 * which means its parent (component) keeps it alive. We are
481 * about to remove the port from its parent's container (with
482 * the g_ptr_array_remove_index() call below), which in this
483 * case would destroy it. This is not good because we still
484 * need the port for the bt_graph_notify_port_removed() call
485 * below (in which its component is `NULL` as expected because
486 * of the bt_object_set_parent() call below).
487 *
488 * To avoid a destroyed port during the notification callback,
489 * get a reference now, and put it (destroying the port if its
490 * reference count is 0 at this point) after notifying the
491 * graph's user.
492 */
493 bt_object_get_no_null_check(&port->base);
494
495 /*
496 * Remove from parent's array of ports (weak refs). This never
497 * destroys the port object because its reference count is at
498 * least 1 thanks to the bt_object_get_no_null_check() call
499 * above.
500 */
501 g_ptr_array_remove_index(ports, index);
502
503 /* Detach port from its component parent */
504 bt_object_set_parent(&port->base, NULL);
505
506 /*
507 * Notify the graph's creator that a port is removed.
508 */
509 graph = bt_component_borrow_graph(component);
510 if (graph) {
511 bt_graph_notify_port_removed(graph, component, port);
512 }
513
514 BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p",
515 component, port);
516
517 /*
518 * Put the local reference. If this port's reference count was 0
519 * when entering this function, it is 1 now, so it is destroyed
520 * immediately.
521 */
522 bt_object_put_no_null_check(&port->base);
523 }
524
525 BT_HIDDEN
526 void bt_component_remove_port(struct bt_component *component,
527 struct bt_port *port)
528 {
529 uint64_t i;
530 GPtrArray *ports = NULL;
531
532 BT_ASSERT(component);
533 BT_ASSERT(port);
534
535 switch (port->type) {
536 case BT_PORT_TYPE_INPUT:
537 ports = component->input_ports;
538 break;
539 case BT_PORT_TYPE_OUTPUT:
540 ports = component->output_ports;
541 break;
542 default:
543 abort();
544 }
545
546 BT_ASSERT(ports);
547
548 for (i = 0; i < ports->len; i++) {
549 struct bt_port *cur_port = g_ptr_array_index(ports, i);
550
551 if (cur_port == port) {
552 remove_port_by_index(component,
553 ports, i);
554 goto end;
555 }
556 }
557
558 BT_LIB_LOGW("Port to remove from component was not found: "
559 "%![comp-]+c, %![port-]+p", component, port);
560
561 end:
562 return;
563 }
564
565 BT_HIDDEN
566 enum bt_self_component_status bt_component_accept_port_connection(
567 struct bt_component *comp, struct bt_port *self_port,
568 struct bt_port *other_port)
569 {
570 typedef enum bt_self_component_status (*method_t)(
571 void *, void *, const void *);
572
573 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
574 method_t method = NULL;
575
576 BT_ASSERT(comp);
577 BT_ASSERT(self_port);
578 BT_ASSERT(other_port);
579
580 switch (comp->class->type) {
581 case BT_COMPONENT_CLASS_TYPE_SOURCE:
582 {
583 struct bt_component_class_source *src_cc = (void *) comp->class;
584
585 switch (self_port->type) {
586 case BT_PORT_TYPE_OUTPUT:
587 method = (method_t) src_cc->methods.accept_output_port_connection;
588 break;
589 default:
590 abort();
591 }
592
593 break;
594 }
595 case BT_COMPONENT_CLASS_TYPE_FILTER:
596 {
597 struct bt_component_class_filter *flt_cc = (void *) comp->class;
598
599 switch (self_port->type) {
600 case BT_PORT_TYPE_INPUT:
601 method = (method_t) flt_cc->methods.accept_input_port_connection;
602 break;
603 case BT_PORT_TYPE_OUTPUT:
604 method = (method_t) flt_cc->methods.accept_output_port_connection;
605 break;
606 default:
607 abort();
608 }
609
610 break;
611 }
612 case BT_COMPONENT_CLASS_TYPE_SINK:
613 {
614 struct bt_component_class_sink *sink_cc = (void *) comp->class;
615
616 switch (self_port->type) {
617 case BT_PORT_TYPE_INPUT:
618 method = (method_t) sink_cc->methods.accept_input_port_connection;
619 break;
620 default:
621 abort();
622 }
623
624 break;
625 }
626 default:
627 abort();
628 }
629
630 if (method) {
631 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
632 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
633 comp, self_port, other_port);
634 status = method(comp, self_port, (void *) other_port);
635 BT_LOGD("User method returned: status=%s",
636 bt_self_component_status_string(status));
637 }
638
639 return status;
640 }
641
642 BT_HIDDEN
643 enum bt_self_component_status bt_component_port_connected(
644 struct bt_component *comp, struct bt_port *self_port,
645 struct bt_port *other_port)
646 {
647 typedef enum bt_self_component_status (*method_t)(
648 void *, void *, const void *);
649
650 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
651 method_t method = NULL;
652
653 BT_ASSERT(comp);
654 BT_ASSERT(self_port);
655 BT_ASSERT(other_port);
656
657 switch (comp->class->type) {
658 case BT_COMPONENT_CLASS_TYPE_SOURCE:
659 {
660 struct bt_component_class_source *src_cc = (void *) comp->class;
661
662 switch (self_port->type) {
663 case BT_PORT_TYPE_OUTPUT:
664 method = (method_t) src_cc->methods.output_port_connected;
665 break;
666 default:
667 abort();
668 }
669
670 break;
671 }
672 case BT_COMPONENT_CLASS_TYPE_FILTER:
673 {
674 struct bt_component_class_filter *flt_cc = (void *) comp->class;
675
676 switch (self_port->type) {
677 case BT_PORT_TYPE_INPUT:
678 method = (method_t) flt_cc->methods.input_port_connected;
679 break;
680 case BT_PORT_TYPE_OUTPUT:
681 method = (method_t) flt_cc->methods.output_port_connected;
682 break;
683 default:
684 abort();
685 }
686
687 break;
688 }
689 case BT_COMPONENT_CLASS_TYPE_SINK:
690 {
691 struct bt_component_class_sink *sink_cc = (void *) comp->class;
692
693 switch (self_port->type) {
694 case BT_PORT_TYPE_INPUT:
695 method = (method_t) sink_cc->methods.input_port_connected;
696 break;
697 default:
698 abort();
699 }
700
701 break;
702 }
703 default:
704 abort();
705 }
706
707 if (method) {
708 BT_LIB_LOGD("Calling user's \"port connected\" method: "
709 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
710 comp, self_port, other_port);
711 status = method(comp, self_port, (void *) other_port);
712 BT_LOGD("User method returned: status=%s",
713 bt_self_component_status_string(status));
714 }
715
716 return status;
717 }
718
719 BT_HIDDEN
720 void bt_component_port_disconnected(struct bt_component *comp,
721 struct bt_port *port)
722 {
723 typedef void (*method_t)(void *, void *);
724
725 method_t method = NULL;
726
727 BT_ASSERT(comp);
728 BT_ASSERT(port);
729
730 switch (comp->class->type) {
731 case BT_COMPONENT_CLASS_TYPE_SOURCE:
732 {
733 struct bt_component_class_source *src_cc = (void *) comp->class;
734
735 switch (port->type) {
736 case BT_PORT_TYPE_OUTPUT:
737 method = (method_t) src_cc->methods.output_port_disconnected;
738 break;
739 default:
740 abort();
741 }
742
743 break;
744 }
745 case BT_COMPONENT_CLASS_TYPE_FILTER:
746 {
747 struct bt_component_class_filter *flt_cc = (void *) comp->class;
748
749 switch (port->type) {
750 case BT_PORT_TYPE_INPUT:
751 method = (method_t) flt_cc->methods.input_port_disconnected;
752 break;
753 case BT_PORT_TYPE_OUTPUT:
754 method = (method_t) flt_cc->methods.output_port_disconnected;
755 break;
756 default:
757 abort();
758 }
759
760 break;
761 }
762 case BT_COMPONENT_CLASS_TYPE_SINK:
763 {
764 struct bt_component_class_sink *sink_cc = (void *) comp->class;
765
766 switch (port->type) {
767 case BT_PORT_TYPE_INPUT:
768 method = (method_t) sink_cc->methods.input_port_disconnected;
769 break;
770 default:
771 abort();
772 }
773
774 break;
775 }
776 default:
777 abort();
778 }
779
780 if (method) {
781 BT_LIB_LOGD("Calling user's \"port disconnected\" method: "
782 "%![comp-]+c, %![port-]+p", comp, port);
783 method(comp, port);
784 }
785 }
786
787 BT_HIDDEN
788 void bt_component_add_destroy_listener(struct bt_component *component,
789 bt_component_destroy_listener_func func, void *data)
790 {
791 struct bt_component_destroy_listener listener;
792
793 BT_ASSERT(component);
794 BT_ASSERT(func);
795 listener.func = func;
796 listener.data = data;
797 g_array_append_val(component->destroy_listeners, listener);
798 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
799 "func-addr=%p, data-addr=%p",
800 component, func, data);
801 }
802
803 BT_HIDDEN
804 void bt_component_remove_destroy_listener(struct bt_component *component,
805 bt_component_destroy_listener_func func, void *data)
806 {
807 uint64_t i;
808
809 BT_ASSERT(component);
810 BT_ASSERT(func);
811
812 for (i = 0; i < component->destroy_listeners->len; i++) {
813 struct bt_component_destroy_listener *listener =
814 &g_array_index(component->destroy_listeners,
815 struct bt_component_destroy_listener, i);
816
817 if (listener->func == func && listener->data == data) {
818 g_array_remove_index(component->destroy_listeners, i);
819 i--;
820 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
821 "func-addr=%p, data-addr=%p",
822 component, func, data);
823 }
824 }
825 }
This page took 0.066992 seconds and 3 git commands to generate.