lib: de-duplicate assertions in bt_component_add_{input,output}_port
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 19 May 2023 21:24:39 +0000 (17:24 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 21 May 2023 12:02:12 +0000 (08:02 -0400)
Move assertions from bt_component_add_input_port and
bt_component_add_output_port to add_port.  Add a parameter to indicate
to add_port if it's adding an input or output port.  This is only used
to select the correct strings for the "no duplicate name" pre-condition
check.  I think this de-duplication helps ensure both the input and
output paths are equivalent.

Change-Id: I217794719231251bf73d0347d6208af9fcaf6a0d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10037
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/lib/graph/component.c

index 569a743e7c7cc555c8048f1c4d81d2c8db5000fa..95df2dff97b18ea9e70e39d3183acda08512ef5a 100644 (file)
@@ -188,18 +188,46 @@ enum bt_component_class_type bt_component_get_class_type(
        return component->class->type;
 }
 
+static
+bool port_name_is_unique(GPtrArray *ports, const char *name)
+{
+       guint i;
+       bool unique;
+
+       for (i = 0; i < ports->len; i++) {
+               struct bt_port *port = g_ptr_array_index(ports, i);
+
+               if (strcmp(port->name->str, name) == 0) {
+                       unique = false;
+                       goto end;
+               }
+       }
+
+       unique = true;
+
+end:
+       return unique;
+}
+
 static
 enum bt_self_component_add_port_status add_port(
                struct bt_component *component, GPtrArray *ports,
                enum bt_port_type port_type, const char *name, void *user_data,
-               struct bt_port **port, const char *api_func)
+               struct bt_port **port, const char *api_func, bool input)
 {
        struct bt_port *new_port = NULL;
        struct bt_graph *graph = NULL;
        enum bt_self_component_add_port_status status;
 
-       BT_ASSERT(component);
-       BT_ASSERT(name);
+       BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func);
+       BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func, component);
+       BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func, name);
+       BT_ASSERT_PRE_FROM_FUNC(api_func,
+               input ? "input" : "output" "-port-name-is-unique",
+               port_name_is_unique(component->output_ports, name),
+               input ? "Input" : "Output"
+                       " port name is not unique: name=\"%s\", %![comp-]c",
+               name, component);
        BT_ASSERT_PRE_FROM_FUNC(api_func, "name-is-not-empty",
                strlen(name) > 0, "Name is empty");
        graph = bt_component_borrow_graph(component);
@@ -450,42 +478,13 @@ struct bt_port_output *bt_component_borrow_output_port_by_index(
                borrow_port_by_index(comp->output_ports, index, api_func);
 }
 
-static
-bool port_name_is_unique(GPtrArray *ports, const char *name)
-{
-       guint i;
-       bool unique;
-
-       for (i = 0; i < ports->len; i++) {
-               struct bt_port *port = g_ptr_array_index(ports, i);
-
-               if (strcmp(port->name->str, name) == 0) {
-                       unique = false;
-                       goto end;
-               }
-       }
-
-       unique = true;
-
-end:
-       return unique;
-}
-
 enum bt_self_component_add_port_status bt_component_add_input_port(
                struct bt_component *component, const char *name,
                void *user_data, struct bt_port **port, const char *api_func)
 {
-       BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func);
-       BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func, component);
-       BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func, name);
-       BT_ASSERT_PRE_FROM_FUNC(api_func, "input-port-name-is-unique",
-               port_name_is_unique(component->input_ports, name),
-               "Input port name is not unique: name=\"%s\", %![comp-]c",
-               name, component);
-
        /* add_port() logs details and checks preconditions */
        return add_port(component, component->input_ports,
-               BT_PORT_TYPE_INPUT, name, user_data, port, api_func);
+               BT_PORT_TYPE_INPUT, name, user_data, port, api_func, true);
 }
 
 enum bt_self_component_add_port_status bt_component_add_output_port(
@@ -493,17 +492,9 @@ enum bt_self_component_add_port_status bt_component_add_output_port(
                void *user_data, struct bt_port **port,
                const char *api_func)
 {
-       BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func);
-       BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func, component);
-       BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func, name);
-       BT_ASSERT_PRE_FROM_FUNC(api_func, "output-port-name-is-unique",
-               port_name_is_unique(component->output_ports, name),
-               "Output port name is not unique: name=\"%s\", %![comp-]c",
-               name, component);
-
        /* add_port() logs details and checks preconditions */
        return add_port(component, component->output_ports,
-               BT_PORT_TYPE_OUTPUT, name, user_data, port, api_func);
+               BT_PORT_TYPE_OUTPUT, name, user_data, port, api_func, false);
 }
 
 enum bt_component_class_port_connected_method_status
This page took 0.02582 seconds and 4 git commands to generate.