[PATCHv4 06/17] conf: use VIR_(APPEND|DELETE)_ELEMENT for vcpupin list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



nvcpupin was declared as int, as were the nvcpupin args in several
functions. These were all changed to size_t.

virDomainVcpuPinAdd() was leaking a cpumask if VIR_REALLOC_N
failed. This is completely inconsequential (since the only reason for
failure was OOM, so we would already be dead at this time anyway), but
I fixed the omission just to be tidy.
---
 src/conf/domain_conf.c | 47 ++++++++++++++---------------------------------
 src/conf/domain_conf.h | 12 ++++++------
 src/qemu/qemu_cgroup.c |  2 +-
 src/qemu/qemu_cgroup.h |  2 +-
 src/qemu/qemu_driver.c |  4 ++--
 5 files changed, 24 insertions(+), 43 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 321d6dd..d92d0be 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1553,7 +1553,7 @@ virDomainClockDefClear(virDomainClockDefPtr def)
 }
 
 virDomainVcpuPinDefPtr *
-virDomainVcpuPinDefCopy(virDomainVcpuPinDefPtr *src, int nvcpupin)
+virDomainVcpuPinDefCopy(virDomainVcpuPinDefPtr *src, size_t nvcpupin)
 {
     int i = 0;
     virDomainVcpuPinDefPtr *ret = NULL;
@@ -1598,9 +1598,9 @@ virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr def)
 
 void
 virDomainVcpuPinDefArrayFree(virDomainVcpuPinDefPtr *def,
-                             int nvcpupin)
+                             size_t nvcpupin)
 {
-    int i;
+    size_t i;
 
     if (!def || !nvcpupin)
         return;
@@ -11532,7 +11532,7 @@ int virDomainDefAddImplicitControllers(virDomainDefPtr def)
  * Return 1 if exists, 0 if not. */
 int
 virDomainVcpuPinIsDuplicate(virDomainVcpuPinDefPtr *def,
-                            int nvcpupin,
+                            size_t nvcpupin,
                             int vcpu)
 {
     int i;
@@ -11550,7 +11550,7 @@ virDomainVcpuPinIsDuplicate(virDomainVcpuPinDefPtr *def,
 
 virDomainVcpuPinDefPtr
 virDomainVcpuPinFindByVcpu(virDomainVcpuPinDefPtr *def,
-                           int nvcpupin,
+                           size_t nvcpupin,
                            int vcpu)
 {
     int i;
@@ -11567,7 +11567,7 @@ virDomainVcpuPinFindByVcpu(virDomainVcpuPinDefPtr *def,
 }
 
 int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list,
-                        int *nvcpupin,
+                        size_t *nvcpupin,
                         unsigned char *cpumap,
                         int maplen,
                         int vcpu)
@@ -11605,14 +11605,13 @@ int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list,
         return -1;
     }
 
-    if (VIR_REALLOC_N(*vcpupin_list, *nvcpupin + 1) < 0) {
+    if (VIR_APPEND_ELEMENT(*vcpupin_list, *nvcpupin, vcpupin) < 0) {
         virReportOOMError();
+        VIR_FREE(vcpupin->cpumask);
         VIR_FREE(vcpupin);
         return -1;
     }
 
-    (*vcpupin_list)[(*nvcpupin)++] = vcpupin;
-
     return 0;
 }
 
@@ -11620,38 +11619,20 @@ int
 virDomainVcpuPinDel(virDomainDefPtr def, int vcpu)
 {
     int n;
-    bool deleted = false;
-    virDomainVcpuPinDefPtr *vcpupin_list = def->cputune.vcpupin;
 
     /* No vcpupin exists yet */
-    if (!def->cputune.nvcpupin) {
+    if (!def->cputune.nvcpupin)
         return 0;
-    }
 
     for (n = 0; n < def->cputune.nvcpupin; n++) {
-        if (vcpupin_list[n]->vcpuid == vcpu) {
-            VIR_FREE(vcpupin_list[n]->cpumask);
-            VIR_FREE(vcpupin_list[n]);
-            memmove(&vcpupin_list[n],
-                    &vcpupin_list[n+1],
-                    (def->cputune.nvcpupin - n - 1) * sizeof(virDomainVcpuPinDef *));
-            deleted = true;
+        if (def->cputune.vcpupin[n]->vcpuid == vcpu) {
+            VIR_FREE(def->cputune.vcpupin[n]->cpumask);
+            VIR_FREE(def->cputune.vcpupin[n]);
+            VIR_DELETE_ELEMENT(def->cputune.vcpupin, n,
+                               def->cputune.nvcpupin);
             break;
         }
     }
-
-    if (!deleted)
-        return 0;
-
-    if (--def->cputune.nvcpupin == 0) {
-        VIR_FREE(def->cputune.vcpupin);
-    } else {
-        if (VIR_REALLOC_N(def->cputune.vcpupin, def->cputune.nvcpupin) < 0) {
-            virReportOOMError();
-            return -1;
-        }
-    }
-
     return 0;
 }
 
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 7ad5377..d37b454 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1627,17 +1627,17 @@ struct _virDomainVcpuPinDef {
 };
 
 void virDomainVcpuPinDefFree(virDomainVcpuPinDefPtr def);
-void virDomainVcpuPinDefArrayFree(virDomainVcpuPinDefPtr *def, int nvcpupin);
+void virDomainVcpuPinDefArrayFree(virDomainVcpuPinDefPtr *def, size_t nvcpupin);
 
 virDomainVcpuPinDefPtr *virDomainVcpuPinDefCopy(virDomainVcpuPinDefPtr *src,
-                                                int nvcpupin);
+                                                size_t nvcpupin);
 
 int virDomainVcpuPinIsDuplicate(virDomainVcpuPinDefPtr *def,
-                                int nvcpupin,
+                                size_t nvcpupin,
                                 int vcpu);
 
 virDomainVcpuPinDefPtr virDomainVcpuPinFindByVcpu(virDomainVcpuPinDefPtr *def,
-                                                  int nvcpupin,
+                                                  size_t nvcpupin,
                                                   int vcpu);
 
 typedef struct _virDomainNumatuneDef virDomainNumatuneDef;
@@ -1707,7 +1707,7 @@ struct _virDomainDef {
         long long quota;
         unsigned long long emulator_period;
         long long emulator_quota;
-        int nvcpupin;
+        size_t nvcpupin;
         virDomainVcpuPinDefPtr *vcpupin;
         virDomainVcpuPinDefPtr emulatorpin;
     } cputune;
@@ -1997,7 +1997,7 @@ int virDomainDefCompatibleDevice(virDomainDefPtr def,
                                  virDomainDeviceDefPtr dev);
 
 int virDomainVcpuPinAdd(virDomainVcpuPinDefPtr **vcpupin_list,
-                        int *nvcpupin,
+                        size_t *nvcpupin,
                         unsigned char *cpumap,
                         int maplen,
                         int vcpu);
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 30cd1d6..0494f93 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -491,7 +491,7 @@ cleanup:
 
 int qemuSetupCgroupVcpuPin(virCgroupPtr cgroup,
                            virDomainVcpuPinDefPtr *vcpupin,
-                           int nvcpupin,
+                           size_t nvcpupin,
                            int vcpuid)
 {
     int i;
diff --git a/src/qemu/qemu_cgroup.h b/src/qemu/qemu_cgroup.h
index e212581..34d7fde 100644
--- a/src/qemu/qemu_cgroup.h
+++ b/src/qemu/qemu_cgroup.h
@@ -53,7 +53,7 @@ int qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                           long long quota);
 int qemuSetupCgroupVcpuPin(virCgroupPtr cgroup,
                            virDomainVcpuPinDefPtr *vcpupin,
-                           int nvcpupin,
+                           size_t nvcpupin,
                            int vcpuid);
 int qemuSetupCgroupEmulatorPin(virCgroupPtr cgroup, virBitmapPtr cpumask);
 int qemuSetupCgroupForVcpu(virQEMUDriverPtr driver, virDomainObjPtr vm);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e099c5c..11bebe4 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4066,7 +4066,7 @@ qemuDomainPinVcpuFlags(virDomainPtr dom,
     int ret = -1;
     qemuDomainObjPrivatePtr priv;
     bool doReset = false;
-    int newVcpuPinNum = 0;
+    size_t newVcpuPinNum = 0;
     virDomainVcpuPinDefPtr *newVcpuPin = NULL;
     virBitmapPtr pcpumap = NULL;
 
@@ -4339,7 +4339,7 @@ qemuDomainPinEmulator(virDomainPtr dom,
     int ret = -1;
     qemuDomainObjPrivatePtr priv;
     bool doReset = false;
-    int newVcpuPinNum = 0;
+    size_t newVcpuPinNum = 0;
     virDomainVcpuPinDefPtr *newVcpuPin = NULL;
     virBitmapPtr pcpumap = NULL;
 
-- 
1.7.11.7

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]