[systemd-devel] [PATCH 16/26] install, cgtop: adjust hashmap_move_one callers for -ENOMEM possibility

Michal Schmidt mschmidt at redhat.com
Thu Oct 16 00:50:54 PDT 2014


That hashmap_move_one() currently cannot fail with -ENOMEM is an
implementation detail, which is not possible to guarantee in general.
Hashmap implementations based on anything else than chaining of
individual entries may have to allocate.

hashmap_move_one will not fail with -ENOMEM if a proper reservation has
been made beforehand. Use reservations in install.c.

In cgtop.c simply propagate the error instead of asserting.
---
 src/cgtop/cgtop.c    |  4 +++-
 src/shared/install.c | 40 +++++++++++++++++++++++++++++-----------
 2 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index ab8c4cf..932a7ba 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -126,7 +126,9 @@ static int process(const char *controller, const char *path, Hashmap *a, Hashmap
                                 return r;
                         }
                 } else {
-                        assert_se(hashmap_move_one(a, b, path) == 0);
+                        r = hashmap_move_one(a, b, path);
+                        if (r < 0)
+                                return r;
                         g->cpu_valid = g->memory_valid = g->io_valid = g->n_tasks_valid = false;
                 }
         }
diff --git a/src/shared/install.c b/src/shared/install.c
index 4b9fb7a..fba46ed 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1395,18 +1395,24 @@ static int install_context_apply(
                 unsigned *n_changes) {
 
         InstallInfo *i;
-        int r = 0, q;
+        int r, q;
 
         assert(c);
         assert(paths);
         assert(config_path);
 
-        while ((i = linked_hashmap_first(c->will_install))) {
+        if (!linked_hashmap_isempty(c->will_install)) {
+                r = linked_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
+                if (r < 0)
+                        return r;
 
-                q = linked_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
-                if (q < 0)
-                        return q;
+                r = linked_hashmap_reserve(c->have_installed, linked_hashmap_size(c->will_install));
+                if (r < 0)
+                        return r;
+        }
 
+        r = 0;
+        while ((i = linked_hashmap_first(c->will_install))) {
                 assert_se(linked_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
 
                 q = unit_file_search(c, i, paths, root_dir, false, true);
@@ -1434,7 +1440,7 @@ static int install_context_mark_for_removal(
                 const char *root_dir) {
 
         InstallInfo *i;
-        int r = 0, q;
+        int r, q;
 
         assert(c);
         assert(paths);
@@ -1442,12 +1448,18 @@ static int install_context_mark_for_removal(
 
         /* Marks all items for removal */
 
-        while ((i = linked_hashmap_first(c->will_install))) {
+        if (!linked_hashmap_isempty(c->will_install)) {
+                r = linked_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
+                if (r < 0)
+                        return r;
 
-                q = linked_hashmap_ensure_allocated(&c->have_installed, &string_hash_ops);
-                if (q < 0)
-                        return q;
+                r = linked_hashmap_reserve(c->have_installed, linked_hashmap_size(c->will_install));
+                if (r < 0)
+                        return r;
+        }
 
+        r = 0;
+        while ((i = linked_hashmap_first(c->will_install))) {
                 assert_se(linked_hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
 
                 q = unit_file_search(c, i, paths, root_dir, false, true);
@@ -1544,11 +1556,17 @@ int unit_file_add_dependency(
                         return r;
         }
 
-        while ((info = linked_hashmap_first(c.will_install))) {
+        if (!linked_hashmap_isempty(c.will_install)) {
                 r = linked_hashmap_ensure_allocated(&c.have_installed, &string_hash_ops);
                 if (r < 0)
                         return r;
 
+                r = linked_hashmap_reserve(c.have_installed, linked_hashmap_size(c.will_install));
+                if (r < 0)
+                        return r;
+        }
+
+        while ((info = linked_hashmap_first(c.will_install))) {
                 assert_se(linked_hashmap_move_one(c.have_installed, c.will_install, info->name) == 0);
 
                 r = unit_file_search(&c, info, &paths, root_dir, false, false);
-- 
2.1.0



More information about the systemd-devel mailing list