richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ac3a84
From 471e4ee0b4fa9c7e9a5ea875fbf22de77fdd25d0 Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Thu, 17 Nov 2022 12:46:45 +0900
ac3a84
Subject: [PATCH] core/unit: merge two loops into one
ac3a84
ac3a84
No functional change, just refactoring.
ac3a84
ac3a84
(cherry picked from commit 4b7918a65cc2571a2b3fc166229e1b8db463e217)
ac3a84
ac3a84
Related: #2160477
ac3a84
---
ac3a84
 src/core/unit.c | 47 +++++++++++++++--------------------------------
ac3a84
 1 file changed, 15 insertions(+), 32 deletions(-)
ac3a84
ac3a84
diff --git a/src/core/unit.c b/src/core/unit.c
ac3a84
index dbbf818622..6b49edc2de 100644
ac3a84
--- a/src/core/unit.c
ac3a84
+++ b/src/core/unit.c
ac3a84
@@ -1047,7 +1047,6 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
ac3a84
         Hashmap *deps;
ac3a84
         void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
ac3a84
                    * since the hashmaps all want it as void pointer. */
ac3a84
-        int r;
ac3a84
 
ac3a84
         assert(u);
ac3a84
         assert(other);
ac3a84
@@ -1074,6 +1073,8 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
ac3a84
                 if (!other_deps)
ac3a84
                         break; /* done! */
ac3a84
 
ac3a84
+                deps = hashmap_get(u->dependencies, dt);
ac3a84
+
ac3a84
                 /* Now iterate through all dependencies of this dependency type, of 'other'. We refer to the
ac3a84
                  * referenced units as 'back'. */
ac3a84
                 HASHMAP_FOREACH_KEY(di_back.data, back, other_deps) {
ac3a84
@@ -1084,6 +1085,7 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
ac3a84
                                 /* This is a dependency pointing back to the unit we want to merge with?
ac3a84
                                  * Suppress it (but warn) */
ac3a84
                                 unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
ac3a84
+                                hashmap_remove(other_deps, back);
ac3a84
                                 continue;
ac3a84
                         }
ac3a84
 
ac3a84
@@ -1102,40 +1104,21 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
ac3a84
                                                           di_move.origin_mask,
ac3a84
                                                           di_move.destination_mask) >= 0);
ac3a84
                         }
ac3a84
-                }
ac3a84
 
ac3a84
-                /* Now all references towards 'other' of the current type 'dt' are corrected to point to
ac3a84
-                 * 'u'. Lets's now move the deps of type 'dt' from 'other' to 'u'. First, let's try to move
ac3a84
-                 * them per type wholesale. */
ac3a84
-                r = hashmap_put(u->dependencies, dt, other_deps);
ac3a84
-                if (r == -EEXIST) {
ac3a84
                         /* The target unit already has dependencies of this type, let's then merge this individually. */
ac3a84
-
ac3a84
-                        assert_se(deps = hashmap_get(u->dependencies, dt));
ac3a84
-
ac3a84
-                        for (;;) {
ac3a84
-                                UnitDependencyInfo di_move;
ac3a84
-
ac3a84
-                                /* Get first dep */
ac3a84
-                                di_move.data = hashmap_steal_first_key_and_value(other_deps, (void**) &back);
ac3a84
-                                if (!di_move.data)
ac3a84
-                                        break; /* done */
ac3a84
-                                if (back == u) {
ac3a84
-                                        /* Would point back to us, ignore */
ac3a84
-                                        unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
ac3a84
-                                        continue;
ac3a84
-                                }
ac3a84
-
ac3a84
-                                assert_se(unit_per_dependency_type_hashmap_update(deps, back, di_move.origin_mask, di_move.destination_mask) >= 0);
ac3a84
-                        }
ac3a84
-                } else {
ac3a84
-                        assert_se(r >= 0);
ac3a84
-
ac3a84
-                        if (hashmap_remove(other_deps, u))
ac3a84
-                                unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
ac3a84
-
ac3a84
-                        TAKE_PTR(other_deps);
ac3a84
+                        if (deps)
ac3a84
+                                assert_se(unit_per_dependency_type_hashmap_update(
ac3a84
+                                                          deps,
ac3a84
+                                                          back,
ac3a84
+                                                          di_back.origin_mask,
ac3a84
+                                                          di_back.destination_mask) >= 0);
ac3a84
                 }
ac3a84
+
ac3a84
+                /* Now all references towards 'other' of the current type 'dt' are corrected to point to 'u'.
ac3a84
+                 * Lets's now move the deps of type 'dt' from 'other' to 'u'. If the unit does not have
ac3a84
+                 * dependencies of this type, let's move them per type wholesale. */
ac3a84
+                if (!deps)
ac3a84
+                        assert_se(hashmap_put(u->dependencies, dt, TAKE_PTR(other_deps)) >= 0);
ac3a84
         }
ac3a84
 
ac3a84
         other->dependencies = hashmap_free(other->dependencies);