2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/COMPONENT-CLASS"
9 #include "lib/logging.h"
11 #include "common/assert.h"
12 #include "lib/assert-cond.h"
13 #include "compat/compiler.h"
14 #include <babeltrace2/graph/component-class.h>
15 #include <babeltrace2/types.h>
18 #include "component-class.h"
19 #include "lib/func-status.h"
20 #include "lib/graph/message-iterator-class.h"
22 #define BT_ASSERT_PRE_DEV_COMP_CLS_HOT(_cc) \
23 BT_ASSERT_PRE_DEV_HOT("component-class", \
24 ((const struct bt_component_class *) (_cc)), \
25 "Component class", ": %!+C", (_cc))
28 void destroy_component_class(struct bt_object
*obj
)
30 struct bt_component_class
*class;
34 class = container_of(obj
, struct bt_component_class
, base
);
36 BT_LIB_LOGI("Destroying component class: %!+C", class);
38 /* Call destroy listeners in reverse registration order */
39 if (class->destroy_listeners
) {
40 for (i
= class->destroy_listeners
->len
- 1; i
>= 0; i
--) {
41 struct bt_component_class_destroy_listener
*listener
=
42 &g_array_index(class->destroy_listeners
,
43 struct bt_component_class_destroy_listener
,
46 BT_LOGD("Calling destroy listener: func-addr=%p, data-addr=%p",
47 listener
->func
, listener
->data
);
48 listener
->func(class, listener
->data
);
53 g_string_free(class->name
, TRUE
);
57 if (class->description
) {
58 g_string_free(class->description
, TRUE
);
59 class->description
= NULL
;
63 g_string_free(class->help
, TRUE
);
67 if (class->plugin_name
) {
68 g_string_free(class->plugin_name
, TRUE
);
69 class->plugin_name
= NULL
;
72 if (class->destroy_listeners
) {
73 g_array_free(class->destroy_listeners
, TRUE
);
74 class->destroy_listeners
= NULL
;
77 if (bt_component_class_has_message_iterator_class(class)) {
78 struct bt_component_class_with_iterator_class
*class_with_iter_class
=
79 container_of(class, struct bt_component_class_with_iterator_class
, parent
);
81 BT_ASSERT(class_with_iter_class
->msg_iter_cls
);
82 bt_message_iterator_class_put_ref(class_with_iter_class
->msg_iter_cls
);
83 class_with_iter_class
->msg_iter_cls
= NULL
;
90 int bt_component_class_init(struct bt_component_class
*class,
91 enum bt_component_class_type type
, const char *name
)
95 bt_object_init_shared(&class->base
, destroy_component_class
);
97 class->name
= g_string_new(name
);
99 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
103 class->description
= g_string_new(NULL
);
104 if (!class->description
) {
105 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
109 class->help
= g_string_new(NULL
);
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
115 class->plugin_name
= g_string_new(NULL
);
116 if (!class->plugin_name
) {
117 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
121 class->destroy_listeners
= g_array_new(FALSE
, TRUE
,
122 sizeof(struct bt_component_class_destroy_listener
));
123 if (!class->destroy_listeners
) {
124 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
131 BT_OBJECT_PUT_REF_AND_RESET(class);
139 int bt_component_class_with_iterator_class_init(
140 struct bt_component_class_with_iterator_class
*class,
141 enum bt_component_class_type type
, const char *name
,
142 struct bt_message_iterator_class
*message_iterator_class
)
146 ret
= bt_component_class_init(&class->parent
, type
, name
);
151 class->msg_iter_cls
= message_iterator_class
;
152 bt_message_iterator_class_get_ref(class->msg_iter_cls
);
153 bt_message_iterator_class_freeze(class->msg_iter_cls
);
159 struct bt_component_class_source
*bt_component_class_source_create(
161 struct bt_message_iterator_class
*message_iterator_class
)
163 struct bt_component_class_source
*source_class
= NULL
;
166 BT_ASSERT_PRE_NO_ERROR();
167 BT_ASSERT_PRE_NAME_NON_NULL(name
);
168 BT_ASSERT_PRE_MSG_ITER_CLS_NON_NULL(message_iterator_class
);
169 BT_LIB_LOGI("Creating source component class: "
170 "name=\"%s\", %![msg-iter-cls-]+I",
171 name
, message_iterator_class
);
172 source_class
= g_new0(struct bt_component_class_source
, 1);
174 BT_LIB_LOGE_APPEND_CAUSE(
175 "Failed to allocate one source component class.");
179 /* bt_component_class_init() logs errors */
180 ret
= bt_component_class_with_iterator_class_init(&source_class
->parent
,
181 BT_COMPONENT_CLASS_TYPE_SOURCE
, name
, message_iterator_class
);
184 * If bt_component_class_init() fails, the component
185 * class is put, therefore its memory is already
192 BT_LIB_LOGI("Created source component class: %!+C", source_class
);
195 return (void *) source_class
;
198 struct bt_component_class_filter
*bt_component_class_filter_create(
200 struct bt_message_iterator_class
*message_iterator_class
)
202 struct bt_component_class_filter
*filter_class
= NULL
;
205 BT_ASSERT_PRE_NO_ERROR();
206 BT_ASSERT_PRE_NAME_NON_NULL(name
);
207 BT_ASSERT_PRE_MSG_ITER_CLS_NON_NULL(message_iterator_class
);
208 BT_LIB_LOGI("Creating filter component class: "
209 "name=\"%s\", %![msg-iter-cls-]+I",
210 name
, message_iterator_class
);
211 filter_class
= g_new0(struct bt_component_class_filter
, 1);
213 BT_LIB_LOGE_APPEND_CAUSE(
214 "Failed to allocate one filter component class.");
218 /* bt_component_class_init() logs errors */
219 ret
= bt_component_class_with_iterator_class_init(&filter_class
->parent
,
220 BT_COMPONENT_CLASS_TYPE_FILTER
, name
, message_iterator_class
);
223 * If bt_component_class_init() fails, the component
224 * class is put, therefore its memory is already
231 BT_LIB_LOGI("Created filter component class: %!+C", filter_class
);
234 return (void *) filter_class
;
237 struct bt_component_class_sink
*bt_component_class_sink_create(
238 const char *name
, bt_component_class_sink_consume_method method
)
240 struct bt_component_class_sink
*sink_class
= NULL
;
243 BT_ASSERT_PRE_NO_ERROR();
244 BT_ASSERT_PRE_NAME_NON_NULL(name
);
245 BT_ASSERT_PRE_NON_NULL("consume-method", method
, "Consume next method");
246 BT_LOGI("Creating sink component class: "
247 "name=\"%s\", consume-method-addr=%p",
249 sink_class
= g_new0(struct bt_component_class_sink
, 1);
251 BT_LIB_LOGE_APPEND_CAUSE(
252 "Failed to allocate one sink component class.");
256 /* bt_component_class_init() logs errors */
257 ret
= bt_component_class_init(&sink_class
->parent
,
258 BT_COMPONENT_CLASS_TYPE_SINK
, name
);
261 * If bt_component_class_init() fails, the component
262 * class is put, therefore its memory is already
269 sink_class
->methods
.consume
= method
;
270 BT_LIB_LOGI("Created sink component class: %!+C", sink_class
);
273 return (void *) sink_class
;
276 enum bt_component_class_set_method_status
277 bt_component_class_source_set_get_supported_mip_versions_method(
278 struct bt_component_class_source
*comp_cls
,
279 bt_component_class_source_get_supported_mip_versions_method method
)
281 BT_ASSERT_PRE_NO_ERROR();
282 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
283 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
284 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
285 comp_cls
->methods
.get_supported_mip_versions
= method
;
286 BT_LIB_LOGD("Set source component class's \"get supported MIP versions\" method: "
288 return BT_FUNC_STATUS_OK
;
291 enum bt_component_class_set_method_status
292 bt_component_class_filter_set_get_supported_mip_versions_method(
293 struct bt_component_class_filter
*comp_cls
,
294 bt_component_class_filter_get_supported_mip_versions_method method
)
296 BT_ASSERT_PRE_NO_ERROR();
297 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
298 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
299 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
300 comp_cls
->methods
.get_supported_mip_versions
= method
;
301 BT_LIB_LOGD("Set filter component class's \"get supported MIP versions\" method: "
303 return BT_FUNC_STATUS_OK
;
306 enum bt_component_class_set_method_status
307 bt_component_class_sink_set_get_supported_mip_versions_method(
308 struct bt_component_class_sink
*comp_cls
,
309 bt_component_class_sink_get_supported_mip_versions_method method
)
311 BT_ASSERT_PRE_NO_ERROR();
312 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
313 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
314 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
315 comp_cls
->methods
.get_supported_mip_versions
= method
;
316 BT_LIB_LOGD("Set sink component class's \"get supported MIP versions\" method: "
318 return BT_FUNC_STATUS_OK
;
321 enum bt_component_class_set_method_status
322 bt_component_class_source_set_initialize_method(
323 struct bt_component_class_source
*comp_cls
,
324 bt_component_class_source_initialize_method method
)
326 BT_ASSERT_PRE_NO_ERROR();
327 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
328 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
329 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
330 comp_cls
->methods
.init
= method
;
331 BT_LIB_LOGD("Set source component class's initialization method: "
333 return BT_FUNC_STATUS_OK
;
336 enum bt_component_class_set_method_status
337 bt_component_class_filter_set_initialize_method(
338 struct bt_component_class_filter
*comp_cls
,
339 bt_component_class_filter_initialize_method method
)
341 BT_ASSERT_PRE_NO_ERROR();
342 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
343 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
344 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
345 comp_cls
->methods
.init
= method
;
346 BT_LIB_LOGD("Set filter component class's initialization method: "
348 return BT_FUNC_STATUS_OK
;
351 enum bt_component_class_set_method_status
352 bt_component_class_sink_set_initialize_method(
353 struct bt_component_class_sink
*comp_cls
,
354 bt_component_class_sink_initialize_method method
)
356 BT_ASSERT_PRE_NO_ERROR();
357 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
358 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
359 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
360 comp_cls
->methods
.init
= method
;
361 BT_LIB_LOGD("Set sink component class's initialization method: "
363 return BT_FUNC_STATUS_OK
;
366 enum bt_component_class_set_method_status
367 bt_component_class_source_set_finalize_method(
368 struct bt_component_class_source
*comp_cls
,
369 bt_component_class_source_finalize_method method
)
371 BT_ASSERT_PRE_NO_ERROR();
372 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
373 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
374 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
375 comp_cls
->methods
.finalize
= method
;
376 BT_LIB_LOGD("Set source component class's finalization method: "
378 return BT_FUNC_STATUS_OK
;
381 enum bt_component_class_set_method_status
382 bt_component_class_filter_set_finalize_method(
383 struct bt_component_class_filter
*comp_cls
,
384 bt_component_class_filter_finalize_method method
)
386 BT_ASSERT_PRE_NO_ERROR();
387 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
388 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
389 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
390 comp_cls
->methods
.finalize
= method
;
391 BT_LIB_LOGD("Set filter component class's finalization method: "
393 return BT_FUNC_STATUS_OK
;
396 enum bt_component_class_set_method_status
397 bt_component_class_sink_set_finalize_method(
398 struct bt_component_class_sink
*comp_cls
,
399 bt_component_class_sink_finalize_method method
)
401 BT_ASSERT_PRE_NO_ERROR();
402 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
403 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
404 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
405 comp_cls
->methods
.finalize
= method
;
406 BT_LIB_LOGD("Set sink component class's finalization method: "
408 return BT_FUNC_STATUS_OK
;
411 enum bt_component_class_set_method_status
412 bt_component_class_source_set_query_method(
413 struct bt_component_class_source
*comp_cls
,
414 bt_component_class_source_query_method method
)
416 BT_ASSERT_PRE_NO_ERROR();
417 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
418 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
419 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
420 comp_cls
->methods
.query
= method
;
421 BT_LIB_LOGD("Set source component class's query method: "
423 return BT_FUNC_STATUS_OK
;
426 enum bt_component_class_set_method_status
427 bt_component_class_filter_set_query_method(
428 struct bt_component_class_filter
*comp_cls
,
429 bt_component_class_filter_query_method method
)
431 BT_ASSERT_PRE_NO_ERROR();
432 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
433 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
434 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
435 comp_cls
->methods
.query
= method
;
436 BT_LIB_LOGD("Set filter component class's query method: "
438 return BT_FUNC_STATUS_OK
;
441 enum bt_component_class_set_method_status
442 bt_component_class_sink_set_query_method(
443 struct bt_component_class_sink
*comp_cls
,
444 bt_component_class_sink_query_method method
)
446 BT_ASSERT_PRE_NO_ERROR();
447 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
448 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
449 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
450 comp_cls
->methods
.query
= method
;
451 BT_LIB_LOGD("Set sink component class's query method: "
453 return BT_FUNC_STATUS_OK
;
456 enum bt_component_class_set_method_status
457 bt_component_class_filter_set_input_port_connected_method(
458 struct bt_component_class_filter
*comp_cls
,
459 bt_component_class_filter_input_port_connected_method method
)
461 BT_ASSERT_PRE_NO_ERROR();
462 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
463 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
464 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
465 comp_cls
->methods
.input_port_connected
= method
;
466 BT_LIB_LOGD("Set filter component class's \"input port connected\" method"
468 return BT_FUNC_STATUS_OK
;
471 enum bt_component_class_set_method_status
472 bt_component_class_sink_set_input_port_connected_method(
473 struct bt_component_class_sink
*comp_cls
,
474 bt_component_class_sink_input_port_connected_method method
)
476 BT_ASSERT_PRE_NO_ERROR();
477 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
478 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
479 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
480 comp_cls
->methods
.input_port_connected
= method
;
481 BT_LIB_LOGD("Set sink component class's \"input port connected\" method"
483 return BT_FUNC_STATUS_OK
;
486 enum bt_component_class_set_method_status
487 bt_component_class_source_set_output_port_connected_method(
488 struct bt_component_class_source
*comp_cls
,
489 bt_component_class_source_output_port_connected_method method
)
491 BT_ASSERT_PRE_NO_ERROR();
492 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
493 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
494 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
495 comp_cls
->methods
.output_port_connected
= method
;
496 BT_LIB_LOGD("Set source component class's \"output port connected\" method"
498 return BT_FUNC_STATUS_OK
;
501 enum bt_component_class_set_method_status
502 bt_component_class_filter_set_output_port_connected_method(
503 struct bt_component_class_filter
*comp_cls
,
504 bt_component_class_filter_output_port_connected_method method
)
506 BT_ASSERT_PRE_NO_ERROR();
507 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
508 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
509 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
510 comp_cls
->methods
.output_port_connected
= method
;
511 BT_LIB_LOGD("Set filter component class's \"output port connected\" method"
513 return BT_FUNC_STATUS_OK
;
516 enum bt_component_class_set_method_status
517 bt_component_class_sink_set_graph_is_configured_method(
518 struct bt_component_class_sink
*comp_cls
,
519 bt_component_class_sink_graph_is_configured_method method
)
521 BT_ASSERT_PRE_NO_ERROR();
522 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
523 BT_ASSERT_PRE_METHOD_NON_NULL(method
);
524 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
525 comp_cls
->methods
.graph_is_configured
= method
;
526 BT_LIB_LOGD("Set sink component class's \"graph is configured\" method"
528 return BT_FUNC_STATUS_OK
;
531 enum bt_component_class_set_description_status
532 bt_component_class_set_description(
533 struct bt_component_class
*comp_cls
,
534 const char *description
)
536 BT_ASSERT_PRE_NO_ERROR();
537 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
538 BT_ASSERT_PRE_DESCR_NON_NULL(description
);
539 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
540 g_string_assign(comp_cls
->description
, description
);
541 BT_LIB_LOGD("Set component class's description: "
542 "addr=%p, name=\"%s\", type=%s",
544 bt_component_class_get_name(comp_cls
),
545 bt_common_component_class_type_string(comp_cls
->type
));
546 return BT_FUNC_STATUS_OK
;
549 enum bt_component_class_set_help_status
bt_component_class_set_help(
550 struct bt_component_class
*comp_cls
,
553 BT_ASSERT_PRE_NO_ERROR();
554 BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls
);
555 BT_ASSERT_PRE_NON_NULL("help-text", help
, "Help text");
556 BT_ASSERT_PRE_DEV_COMP_CLS_HOT(comp_cls
);
557 g_string_assign(comp_cls
->help
, help
);
558 BT_LIB_LOGD("Set component class's help text: %!+C", comp_cls
);
559 return BT_FUNC_STATUS_OK
;
562 const char *bt_component_class_get_name(const struct bt_component_class
*comp_cls
)
564 BT_ASSERT_PRE_DEV_COMP_CLS_NON_NULL(comp_cls
);
565 return comp_cls
->name
->str
;
568 enum bt_component_class_type
bt_component_class_get_type(
569 const struct bt_component_class
*comp_cls
)
571 BT_ASSERT_PRE_DEV_COMP_CLS_NON_NULL(comp_cls
);
572 return comp_cls
->type
;
575 const char *bt_component_class_get_description(
576 const struct bt_component_class
*comp_cls
)
578 BT_ASSERT_PRE_DEV_COMP_CLS_NON_NULL(comp_cls
);
579 return comp_cls
->description
&&
580 comp_cls
->description
->str
[0] != '\0' ?
581 comp_cls
->description
->str
: NULL
;
584 const char *bt_component_class_get_help(
585 const struct bt_component_class
*comp_cls
)
587 BT_ASSERT_PRE_DEV_COMP_CLS_NON_NULL(comp_cls
);
588 return comp_cls
->help
&&
589 comp_cls
->help
->str
[0] != '\0' ? comp_cls
->help
->str
: NULL
;
593 void bt_component_class_add_destroy_listener(
594 struct bt_component_class
*comp_cls
,
595 bt_component_class_destroy_listener_func func
, void *data
)
597 struct bt_component_class_destroy_listener listener
;
601 listener
.func
= func
;
602 listener
.data
= data
;
603 g_array_append_val(comp_cls
->destroy_listeners
, listener
);
604 BT_LIB_LOGD("Added destroy listener to component class: "
605 "%![cc-]+C, listener-func-addr=%p", comp_cls
, func
);
609 void _bt_component_class_freeze(const struct bt_component_class
*comp_cls
)
612 BT_LIB_LOGD("Freezing component class: %!+C", comp_cls
);
613 ((struct bt_component_class
*) comp_cls
)->frozen
= true;
616 void bt_component_class_get_ref(
617 const struct bt_component_class
*component_class
)
619 bt_object_get_ref(component_class
);
622 void bt_component_class_put_ref(
623 const struct bt_component_class
*component_class
)
625 bt_object_put_ref(component_class
);
628 void bt_component_class_source_get_ref(
629 const struct bt_component_class_source
*component_class_source
)
631 bt_object_get_ref(component_class_source
);
634 void bt_component_class_source_put_ref(
635 const struct bt_component_class_source
*component_class_source
)
637 bt_object_put_ref(component_class_source
);
640 void bt_component_class_filter_get_ref(
641 const struct bt_component_class_filter
*component_class_filter
)
643 bt_object_get_ref(component_class_filter
);
646 void bt_component_class_filter_put_ref(
647 const struct bt_component_class_filter
*component_class_filter
)
649 bt_object_put_ref(component_class_filter
);
652 void bt_component_class_sink_get_ref(
653 const struct bt_component_class_sink
*component_class_sink
)
655 bt_object_get_ref(component_class_sink
);
658 void bt_component_class_sink_put_ref(
659 const struct bt_component_class_sink
*component_class_sink
)
661 bt_object_put_ref(component_class_sink
);