]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: cgroup: Use GHashTable instead of virHashTable
authorPeter Krempa <pkrempa@redhat.com>
Tue, 20 Oct 2020 15:01:51 +0000 (17:01 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 22 Oct 2020 13:02:46 +0000 (15:02 +0200)
Rewrite using GHashTable which already has interfaces for using a number
as hash key. Glib's implementation doesn't copy the key by default, so
we need to allocate it, but overal the interface is more suited for this
case.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/util/vircgroup.c
src/util/vircgroupbackend.h
src/util/vircgrouppriv.h
src/util/vircgroupv1.c
src/util/vircgroupv2.c

index 5f4cb01bc0bacc9dc113bef11ad9ada2ccef8c29..b74ec1a8fae3d1df8a34e5e61355b4ca8460e112 100644 (file)
@@ -42,7 +42,6 @@
 #include "virlog.h"
 #include "virfile.h"
 #include "virhash.h"
-#include "virhashcode.h"
 #include "virstring.h"
 #include "virsystemd.h"
 #include "virtypedparam.h"
@@ -2382,7 +2381,7 @@ virCgroupRemove(virCgroupPtr group)
 static int
 virCgroupKillInternal(virCgroupPtr group,
                       int signum,
-                      virHashTablePtr pids,
+                      GHashTable *pids,
                       int controller,
                       const char *taskFile)
 {
@@ -2415,8 +2414,9 @@ virCgroupKillInternal(virCgroupPtr group,
             goto cleanup;
         } else {
             while (!feof(fp)) {
-                long pid_value;
-                if (fscanf(fp, "%ld", &pid_value) != 1) {
+                g_autofree long long *pid_value = g_new0(long long, 1);
+
+                if (fscanf(fp, "%lld", pid_value) != 1) {
                     if (feof(fp))
                         break;
                     virReportSystemError(errno,
@@ -2424,16 +2424,17 @@ virCgroupKillInternal(virCgroupPtr group,
                                          keypath);
                     goto cleanup;
                 }
-                if (virHashLookup(pids, (void*)pid_value))
+
+                if (g_hash_table_lookup(pids, pid_value))
                     continue;
 
-                VIR_DEBUG("pid=%ld", pid_value);
+                VIR_DEBUG("pid=%lld", *pid_value);
                 /* Cgroups is a Linux concept, so this cast is safe.  */
-                if (kill((pid_t)pid_value, signum) < 0) {
+                if (kill((pid_t)*pid_value, signum) < 0) {
                     if (errno != ESRCH) {
                         virReportSystemError(errno,
-                                             _("Failed to kill process %ld"),
-                                             pid_value);
+                                             _("Failed to kill process %lld"),
+                                             *pid_value);
                         goto cleanup;
                     }
                     /* Leave RC == 0 since we didn't kill one */
@@ -2442,7 +2443,7 @@ virCgroupKillInternal(virCgroupPtr group,
                     done = false;
                 }
 
-                ignore_value(virHashAddEntry(pids, (void*)pid_value, (void*)1));
+                g_hash_table_add(pids, g_steal_pointer(&pid_value));
             }
             VIR_FORCE_FCLOSE(fp);
         }
@@ -2458,39 +2459,10 @@ virCgroupKillInternal(virCgroupPtr group,
 }
 
 
-static uint32_t
-virCgroupPidCode(const void *name, uint32_t seed)
-{
-    long pid_value = (long)(intptr_t)name;
-    return virHashCodeGen(&pid_value, sizeof(pid_value), seed);
-}
-
-
-static bool
-virCgroupPidEqual(const void *namea, const void *nameb)
-{
-    return namea == nameb;
-}
-
-
-static void *
-virCgroupPidCopy(const void *name)
-{
-    return (void*)name;
-}
-
-
-static char *
-virCgroupPidPrintHuman(const void *name)
-{
-    return g_strdup_printf("%ld", (const long)name);
-}
-
-
 int
 virCgroupKillRecursiveInternal(virCgroupPtr group,
                                int signum,
-                               virHashTablePtr pids,
+                               GHashTable *pids,
                                int controller,
                                const char *taskFile,
                                bool dormdir)
@@ -2565,13 +2537,7 @@ virCgroupKillRecursive(virCgroupPtr group, int signum)
     size_t i;
     bool backendAvailable = false;
     virCgroupBackendPtr *backends = virCgroupBackendGetAll();
-    virHashTablePtr pids = virHashCreateFull(100,
-                                             NULL,
-                                             virCgroupPidCode,
-                                             virCgroupPidEqual,
-                                             virCgroupPidCopy,
-                                             virCgroupPidPrintHuman,
-                                             NULL);
+    g_autoptr(GHashTable) pids = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, NULL);
 
     VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);
 
@@ -2596,7 +2562,6 @@ virCgroupKillRecursive(virCgroupPtr group, int signum)
     }
 
  cleanup:
-    virHashFree(pids);
     return ret;
 }
 
index bcbe435d78fefde9f37e7e336ea1a81fc06bcffb..f677157a91b60073df554f524603172dbb82315a 100644 (file)
@@ -23,7 +23,6 @@
 #include "internal.h"
 
 #include "vircgroup.h"
-#include "virhash.h"
 
 #define CGROUP_MAX_VAL 512
 
@@ -136,7 +135,7 @@ typedef int
 typedef int
 (*virCgroupKillRecursiveCB)(virCgroupPtr group,
                             int signum,
-                            virHashTablePtr pids);
+                            GHashTable *pids);
 
 typedef int
 (*virCgroupBindMountCB)(virCgroupPtr group,
index f2a80aeb82d2de7e53355df9e620e103c95b7eb5..f85a36efdb940a04c100c62faef12dd5c4e5968f 100644 (file)
@@ -136,7 +136,7 @@ int virCgroupRemoveRecursively(char *grppath);
 
 int virCgroupKillRecursiveInternal(virCgroupPtr group,
                                    int signum,
-                                   virHashTablePtr pids,
+                                   GHashTable *pids,
                                    int controller,
                                    const char *taskFile,
                                    bool dormdir);
index a42b7750f97e8e525df3710d0f26f3dca759381b..91c1c4d4b1deaa4bbbe29a59cb0dfdf4752bb3c8 100644 (file)
@@ -752,7 +752,7 @@ virCgroupV1HasEmptyTasks(virCgroupPtr cgroup,
 static int
 virCgroupV1KillRecursive(virCgroupPtr group,
                          int signum,
-                         virHashTablePtr pids)
+                         GHashTable *pids)
 {
     int controller = virCgroupV1GetAnyController(group);
 
index ddbe3d666391a37243c7baa5e3355a1823288684..285b7675d9ab41cdad096dde177f6c41a7693cfd 100644 (file)
@@ -533,7 +533,7 @@ virCgroupV2HasEmptyTasks(virCgroupPtr cgroup,
 static int
 virCgroupV2KillRecursive(virCgroupPtr group,
                          int signum,
-                         virHashTablePtr pids)
+                         GHashTable *pids)
 {
     int controller = virCgroupV2GetAnyController(group);