lib: create common base for bt_component_class_{source,filter}
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 7 Jan 2020 22:23:31 +0000 (17:23 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 20 Jan 2020 20:15:24 +0000 (15:15 -0500)
There are multiple spots which deal with message iterators, that have
duplicated code for source and filter components.  The code is the same,
except that one side deals with a bt_component_class_source and the
other with a bt_component_class_filter.

This patch introduce a common base,
bt_component_class_with_iterator_class, that holds the message iterator
class property.  The aforementioned code paths can then be deduplicated.

Change-Id: Ib2b42da4e77a0ab7faf94533684a7c1d665eb2e9
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2744
Tested-by: jenkins <jenkins@lttng.org>
src/lib/graph/component-class.c
src/lib/graph/component-class.h
src/lib/graph/iterator.c

index ea90b5ce32ab8987be8064983aeefd6b747b5046..1c1497db4c6e24981aa28ad40e690b139bc98825 100644 (file)
@@ -94,22 +94,13 @@ void destroy_component_class(struct bt_object *obj)
                class->destroy_listeners = NULL;
        }
 
-       if (class->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
-               struct bt_component_class_source *class_src
-                       = container_of(class, struct bt_component_class_source,
-                               parent);
-
-               BT_ASSERT(class_src->msg_iter_cls);
-               bt_message_iterator_class_put_ref(class_src->msg_iter_cls);
-               class_src->msg_iter_cls = NULL;
-       } else if (class->type == BT_COMPONENT_CLASS_TYPE_FILTER) {
-               struct bt_component_class_filter *class_flt
-                       = container_of(class, struct bt_component_class_filter,
-                               parent);
-
-               BT_ASSERT(class_flt->msg_iter_cls);
-               bt_message_iterator_class_put_ref(class_flt->msg_iter_cls);
-               class_flt->msg_iter_cls = NULL;
+       if (bt_component_class_has_message_iterator_class(class)) {
+               struct bt_component_class_with_iterator_class *class_with_iter_class =
+                       container_of(class, struct bt_component_class_with_iterator_class, parent);
+
+               BT_ASSERT(class_with_iter_class->msg_iter_cls);
+               bt_message_iterator_class_put_ref(class_with_iter_class->msg_iter_cls);
+               class_with_iter_class->msg_iter_cls = NULL;
        }
 
        g_free(class);
@@ -164,6 +155,27 @@ end:
        return ret;
 }
 
+static
+int bt_component_class_with_iterator_class_init(
+               struct bt_component_class_with_iterator_class *class,
+               enum bt_component_class_type type, const char *name,
+               struct bt_message_iterator_class *message_iterator_class)
+{
+       int ret;
+
+       ret = bt_component_class_init(&class->parent, type, name);
+       if (ret != 0) {
+               goto end;
+       }
+
+       class->msg_iter_cls = message_iterator_class;
+       bt_message_iterator_class_get_ref(class->msg_iter_cls);
+       bt_message_iterator_class_freeze(class->msg_iter_cls);
+
+end:
+       return ret;
+}
+
 struct bt_component_class_source *bt_component_class_source_create(
                const char *name,
                struct bt_message_iterator_class *message_iterator_class)
@@ -185,8 +197,8 @@ struct bt_component_class_source *bt_component_class_source_create(
        }
 
        /* bt_component_class_init() logs errors */
-       ret = bt_component_class_init(&source_class->parent,
-               BT_COMPONENT_CLASS_TYPE_SOURCE, name);
+       ret = bt_component_class_with_iterator_class_init(&source_class->parent,
+               BT_COMPONENT_CLASS_TYPE_SOURCE, name, message_iterator_class);
        if (ret) {
                /*
                 * If bt_component_class_init() fails, the component
@@ -197,10 +209,6 @@ struct bt_component_class_source *bt_component_class_source_create(
                goto end;
        }
 
-       source_class->msg_iter_cls = message_iterator_class;
-       bt_message_iterator_class_get_ref(source_class->msg_iter_cls);
-       bt_message_iterator_class_freeze(source_class->msg_iter_cls);
-
        BT_LIB_LOGI("Created source component class: %!+C", source_class);
 
 end:
@@ -228,8 +236,8 @@ struct bt_component_class_filter *bt_component_class_filter_create(
        }
 
        /* bt_component_class_init() logs errors */
-       ret = bt_component_class_init(&filter_class->parent,
-               BT_COMPONENT_CLASS_TYPE_FILTER, name);
+       ret = bt_component_class_with_iterator_class_init(&filter_class->parent,
+               BT_COMPONENT_CLASS_TYPE_FILTER, name, message_iterator_class);
        if (ret) {
                /*
                 * If bt_component_class_init() fails, the component
@@ -240,10 +248,6 @@ struct bt_component_class_filter *bt_component_class_filter_create(
                goto end;
        }
 
-       filter_class->msg_iter_cls = message_iterator_class;
-       bt_message_iterator_class_get_ref(filter_class->msg_iter_cls);
-       bt_message_iterator_class_freeze(filter_class->msg_iter_cls);
-
        BT_LIB_LOGI("Created filter component class: %!+C", filter_class);
 
 end:
index 2b4b9454f4cd9eb1ca64a03f8db86ee8596d935e..5974d832a9a9ecc0cfee298f4f4a4fd2f8c1378d 100644 (file)
@@ -64,8 +64,13 @@ struct bt_component_class {
        struct bt_plugin_so_shared_lib_handle *so_handle;
 };
 
-struct bt_component_class_source {
+struct bt_component_class_with_iterator_class {
        struct bt_component_class parent;
+       bt_message_iterator_class *msg_iter_cls;
+};
+
+struct bt_component_class_source {
+       struct bt_component_class_with_iterator_class parent;
        struct {
                bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions;
                bt_component_class_source_initialize_method init;
@@ -73,7 +78,18 @@ struct bt_component_class_source {
                bt_component_class_source_query_method query;
                bt_component_class_source_output_port_connected_method output_port_connected;
        } methods;
-       bt_message_iterator_class *msg_iter_cls;
+};
+
+struct bt_component_class_filter {
+       struct bt_component_class_with_iterator_class parent;
+       struct {
+               bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions;
+               bt_component_class_filter_initialize_method init;
+               bt_component_class_filter_finalize_method finalize;
+               bt_component_class_filter_query_method query;
+               bt_component_class_filter_input_port_connected_method input_port_connected;
+               bt_component_class_filter_output_port_connected_method output_port_connected;
+       } methods;
 };
 
 struct bt_component_class_sink {
@@ -89,19 +105,6 @@ struct bt_component_class_sink {
        } methods;
 };
 
-struct bt_component_class_filter {
-       struct bt_component_class parent;
-       struct {
-               bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions;
-               bt_component_class_filter_initialize_method init;
-               bt_component_class_filter_finalize_method finalize;
-               bt_component_class_filter_query_method query;
-               bt_component_class_filter_input_port_connected_method input_port_connected;
-               bt_component_class_filter_output_port_connected_method output_port_connected;
-       } methods;
-       bt_message_iterator_class *msg_iter_cls;
-};
-
 BT_HIDDEN
 void bt_component_class_add_destroy_listener(struct bt_component_class *class,
                bt_component_class_destroy_listener_func func, void *data);
@@ -131,4 +134,12 @@ const char *bt_component_class_type_string(enum bt_component_class_type type)
        }
 }
 
+static inline
+bool bt_component_class_has_message_iterator_class(
+       struct bt_component_class *component_class)
+{
+       return component_class->type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
+               component_class->type == BT_COMPONENT_CLASS_TYPE_FILTER;
+}
+
 #endif /* BABELTRACE_GRAPH_COMPONENT_CLASS_INTERNAL_H */
index 4dd69c6ae69477c26b54ac35e3c22b2033c79ef0..c7ab253c3aaae3edc836a8429d6039e0d50931f3 100644 (file)
@@ -210,31 +210,15 @@ void bt_self_component_port_input_message_iterator_try_finalize(
        /* Call user-defined destroy method */
        if (call_user_finalize) {
                typedef void (*method_t)(void *);
-               method_t method = NULL;
+               method_t method;
                struct bt_component_class *comp_class =
                        iterator->upstream_component->class;
+               struct bt_component_class_with_iterator_class *class_with_iter_class;
 
-               switch (comp_class->type) {
-               case BT_COMPONENT_CLASS_TYPE_SOURCE:
-               {
-                       struct bt_component_class_source *src_comp_cls =
-                               (void *) comp_class;
-
-                       method = (method_t) src_comp_cls->msg_iter_cls->methods.finalize;
-                       break;
-               }
-               case BT_COMPONENT_CLASS_TYPE_FILTER:
-               {
-                       struct bt_component_class_filter *flt_comp_cls =
-                               (void *) comp_class;
-
-                       method = (method_t) flt_comp_cls->msg_iter_cls->methods.finalize;
-                       break;
-               }
-               default:
-                       /* Unreachable */
-                       bt_common_abort();
-               }
+               BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class));
+               class_with_iter_class = container_of(comp_class,
+                       struct bt_component_class_with_iterator_class, parent);
+               method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize;
 
                if (method) {
                        const bt_error *saved_error;
@@ -329,6 +313,7 @@ int create_self_component_input_port_message_iterator(
        struct bt_component *comp;
        struct bt_component *upstream_comp;
        struct bt_component_class *upstream_comp_cls;
+       struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls;
        int status;
 
        BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator");
@@ -399,54 +384,26 @@ int create_self_component_input_port_message_iterator(
        set_self_comp_port_input_msg_iterator_state(iterator,
                BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
 
-       switch (iterator->upstream_component->class->type) {
-       case BT_COMPONENT_CLASS_TYPE_SOURCE:
-       {
-               struct bt_component_class_source *src_comp_cls =
-                       (void *) iterator->upstream_component->class;
-
-               iterator->methods.next =
-                       (bt_self_component_port_input_message_iterator_next_method)
-                               src_comp_cls->msg_iter_cls->methods.next;
-               iterator->methods.seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               src_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
-               iterator->methods.seek_beginning =
-                       (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               src_comp_cls->msg_iter_cls->methods.seek_beginning;
-               iterator->methods.can_seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
-                               src_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
-               iterator->methods.can_seek_beginning =
-                       (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               src_comp_cls->msg_iter_cls->methods.can_seek_beginning;
-               break;
-       }
-       case BT_COMPONENT_CLASS_TYPE_FILTER:
-       {
-               struct bt_component_class_filter *flt_comp_cls =
-                       (void *) iterator->upstream_component->class;
-
-               iterator->methods.next =
-                       (bt_self_component_port_input_message_iterator_next_method)
-                               flt_comp_cls->msg_iter_cls->methods.next;
-               iterator->methods.seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               flt_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
-               iterator->methods.seek_beginning =
-                       (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               flt_comp_cls->msg_iter_cls->methods.seek_beginning;
-               iterator->methods.can_seek_ns_from_origin =
-                       (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
-                               flt_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
-               iterator->methods.can_seek_beginning =
-                       (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               flt_comp_cls->msg_iter_cls->methods.can_seek_beginning;
-               break;
-       }
-       default:
-               bt_common_abort();
-       }
+       /* Copy methods from the message iterator class to the message iterator. */
+       BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls));
+       upstream_comp_cls_with_iter_cls = container_of(upstream_comp_cls,
+               struct bt_component_class_with_iterator_class, parent);
+
+       iterator->methods.next =
+               (bt_self_component_port_input_message_iterator_next_method)
+                       upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next;
+       iterator->methods.seek_ns_from_origin =
+               (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
+                       upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin;
+       iterator->methods.seek_beginning =
+               (bt_self_component_port_input_message_iterator_seek_beginning_method)
+                       upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning;
+       iterator->methods.can_seek_ns_from_origin =
+               (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
+                       upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
+       iterator->methods.can_seek_beginning =
+               (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
+                       upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning;
 
        if (iterator->methods.seek_ns_from_origin &&
                        !iterator->methods.can_seek_ns_from_origin) {
@@ -462,27 +419,8 @@ int create_self_component_input_port_message_iterator(
                                can_seek_beginning_true;
        }
 
-       switch (upstream_comp_cls->type) {
-       case BT_COMPONENT_CLASS_TYPE_SOURCE:
-       {
-               struct bt_component_class_source *src_comp_cls =
-                       (void *) upstream_comp_cls;
-
-               init_method = src_comp_cls->msg_iter_cls->methods.initialize;
-               break;
-       }
-       case BT_COMPONENT_CLASS_TYPE_FILTER:
-       {
-               struct bt_component_class_filter *flt_comp_cls =
-                       (void *) upstream_comp_cls;
-
-               init_method = flt_comp_cls->msg_iter_cls->methods.initialize;
-               break;
-       }
-       default:
-               /* Unreachable */
-               bt_common_abort();
-       }
+       /* Call iterator's init method. */
+       init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize;
 
        if (init_method) {
                enum bt_message_iterator_class_initialize_method_status iter_status;
This page took 0.028816 seconds and 4 git commands to generate.