]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: make virCPUDef into a ref counted struct
authorDaniel P. Berrangé <berrange@redhat.com>
Fri, 29 Nov 2019 11:00:26 +0000 (11:00 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Mon, 9 Dec 2019 10:17:27 +0000 (10:17 +0000)
Annoyingly there was no existing constructor, and identifying all the
places which do a VIR_ALLOC(cpu) is a bit error prone. Hopefully this
has found & converted them all.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
13 files changed:
src/conf/cpu_conf.c
src/conf/cpu_conf.h
src/cpu/cpu.c
src/cpu/cpu_arm.c
src/cpu/cpu_ppc64.c
src/cpu/cpu_x86.c
src/libvirt_private.syms
src/libxl/libxl_capabilities.c
src/libxl/xen_xl.c
src/qemu/qemu_capabilities.c
src/qemu/qemu_domain.c
src/vmx/vmx.c
tests/cputest.c

index 4542bcb7bda1833b7b9794b0de6ff060f9babc54..7490d6bf7305002ab6ff6e2fe8ff8542b3750330 100644 (file)
@@ -82,6 +82,13 @@ VIR_ENUM_IMPL(virCPUCacheMode,
 );
 
 
+virCPUDefPtr virCPUDefNew(void)
+{
+    virCPUDefPtr cpu = g_new0(virCPUDef, 1);
+    cpu->refs = 1;
+    return cpu;
+}
+
 void
 virCPUDefFreeFeatures(virCPUDefPtr def)
 {
@@ -104,16 +111,24 @@ virCPUDefFreeModel(virCPUDefPtr def)
     virCPUDefFreeFeatures(def);
 }
 
+void
+virCPUDefRef(virCPUDefPtr def)
+{
+    g_atomic_int_inc(&def->refs);
+}
+
 void
 virCPUDefFree(virCPUDefPtr def)
 {
     if (!def)
         return;
 
-    virCPUDefFreeModel(def);
-    VIR_FREE(def->cache);
-    VIR_FREE(def->tsc);
-    VIR_FREE(def);
+    if (g_atomic_int_dec_and_test(&def->refs)) {
+        virCPUDefFreeModel(def);
+        VIR_FREE(def->cache);
+        VIR_FREE(def->tsc);
+        VIR_FREE(def);
+    }
 }
 
 
@@ -214,9 +229,10 @@ virCPUDefCopyWithoutModel(const virCPUDef *cpu)
 {
     virCPUDefPtr copy;
 
-    if (!cpu || VIR_ALLOC(copy) < 0)
+    if (!cpu)
         return NULL;
 
+    copy = virCPUDefNew();
     copy->type = cpu->type;
     copy->mode = cpu->mode;
     copy->match = cpu->match;
@@ -340,8 +356,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
         goto cleanup;
     }
 
-    if (VIR_ALLOC(def) < 0)
-        goto cleanup;
+    def = virCPUDefNew();
 
     if (type == VIR_CPU_TYPE_AUTO) {
         if (virXPathBoolean("boolean(./arch)", ctxt)) {
index 96fda3e6b3998d97720393523c0bbecbebf502e2..ec3d2379cf699535c059ff327bd5965d2f776448 100644 (file)
@@ -122,6 +122,7 @@ struct _virCPUCacheDef {
 typedef struct _virCPUDef virCPUDef;
 typedef virCPUDef *virCPUDefPtr;
 struct _virCPUDef {
+    int refs;
     int type;           /* enum virCPUType */
     int mode;           /* enum virCPUMode */
     int match;          /* enum virCPUMatch */
@@ -142,6 +143,7 @@ struct _virCPUDef {
     virHostCPUTscInfoPtr tsc;
 };
 
+virCPUDefPtr virCPUDefNew(void);
 
 void ATTRIBUTE_NONNULL(1)
 virCPUDefFreeFeatures(virCPUDefPtr def);
@@ -149,6 +151,8 @@ virCPUDefFreeFeatures(virCPUDefPtr def);
 void ATTRIBUTE_NONNULL(1)
 virCPUDefFreeModel(virCPUDefPtr def);
 
+void
+virCPUDefRef(virCPUDefPtr def);
 void
 virCPUDefFree(virCPUDefPtr def);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCPUDef, virCPUDefFree);
index 40f725fd21b18041ab8ba01949c0cdf4b0e4a2a1..a2ae5b8c07873a771116c69e8a08fac32fec881e 100644 (file)
@@ -393,8 +393,7 @@ virCPUGetHost(virArch arch,
     if (!(driver = cpuGetSubDriver(arch)))
         return NULL;
 
-    if (VIR_ALLOC(cpu) < 0)
-        return NULL;
+    cpu = virCPUDefNew();
 
     switch (type) {
     case VIR_CPU_TYPE_HOST:
index 70dba6021ca4ef0a75e699a0e956e91f1a3baf3e..ee5802198f1c3bc330e75463cf878188dbfeb98c 100644 (file)
@@ -211,10 +211,7 @@ virCPUarmBaseline(virCPUDefPtr *cpus,
 {
     virCPUDefPtr cpu = NULL;
 
-    if (VIR_ALLOC(cpu) < 0) {
-        virCPUDefFree(cpu);
-        return NULL;
-    }
+    cpu = virCPUDefNew();
 
     cpu->model = g_strdup(cpus[0]->model);
 
index 3236a9616e8350d074e066a56060363d28c6730b..818f1ec6994d004f7a683373ef5940391ed3bc7e 100644 (file)
@@ -757,8 +757,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
         }
     }
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto error;
+    cpu = virCPUDefNew();
 
     cpu->model = g_strdup(model->name);
 
index c83cab0c5331831be5fd7c7ad17b9ddd0ffa78cc..1e913cc9fa1828c790c315b9ffdc9d0b80c24e1b 100644 (file)
@@ -775,8 +775,7 @@ x86DataToCPU(const virCPUx86Data *data,
     virCPUx86Data modelData = VIR_CPU_X86_DATA_INIT;
     virCPUx86VendorPtr vendor;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto error;
+    cpu = virCPUDefNew();
 
     cpu->model = g_strdup(model->name);
 
@@ -2807,8 +2806,7 @@ virCPUx86Baseline(virCPUDefPtr *cpus,
     if (!(base_model = x86ModelFromCPU(cpus[0], map, -1)))
         goto error;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto error;
+    cpu = virCPUDefNew();
 
     cpu->type = VIR_CPU_TYPE_GUEST;
     cpu->match = VIR_CPU_MATCH_EXACT;
index 3a695e6152677508eb8e6686d22a2d0f8cccf422..5da5307fa3b1ae743232838d2ad247dafc2bbd2a 100644 (file)
@@ -104,8 +104,10 @@ virCPUDefFreeModel;
 virCPUDefIsEqual;
 virCPUDefListFree;
 virCPUDefListParse;
+virCPUDefNew;
 virCPUDefParseXML;
 virCPUDefParseXMLString;
+virCPUDefRef;
 virCPUDefStealModel;
 virCPUDefUpdateFeature;
 virCPUModeTypeToString;
index e2327474fadb5f793bf7d67aa95aef1379872d0c..e9f958cd8a526bce12d2e8dd6746a51d2635a1df 100644 (file)
@@ -169,8 +169,7 @@ libxlCapsInitCPU(virCapsPtr caps, libxl_physinfo *phy_info,
     if (!phy_info->hw_cap[0])
         return 0;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto error;
+    cpu = virCPUDefNew();
 
     host_pae = phy_info->hw_cap[0] & LIBXL_X86_FEATURE_PAE_MASK;
     if (host_pae &&
index 18ea4a8d957cfd53c5109aea535453d335d88631..cdff71f11bd057e213c61b56017cc63b6096a8ba 100644 (file)
@@ -177,10 +177,7 @@ xenParseXLOS(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
             }
 
             if (!def->cpu) {
-                virCPUDefPtr cpu;
-                if (VIR_ALLOC(cpu) < 0)
-                    return -1;
-
+                virCPUDefPtr cpu = virCPUDefNew();
                 cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
                 cpu->type = VIR_CPU_TYPE_GUEST;
                 cpu->nfeatures = 0;
@@ -266,8 +263,7 @@ xenParseXLCPUID(virConfPtr conf, virDomainDefPtr def)
         return 0;
 
     if (!def->cpu) {
-        if (VIR_ALLOC(def->cpu) < 0)
-            goto cleanup;
+        def->cpu = virCPUDefNew();
         def->cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
         def->cpu->type = VIR_CPU_TYPE_GUEST;
         def->cpu->nfeatures = 0;
@@ -445,8 +441,7 @@ xenParseXLVnuma(virConfPtr conf,
     if (!virDomainNumaSetNodeCount(numa, nr_nodes))
         goto cleanup;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto cleanup;
+    cpu = virCPUDefNew();
 
     list = list->list;
     while (list) {
index 66bee49d6f2c5fc7531f8f89f0ff8eeb022ef8d9..16c4331b13b2d0a92ad3a156a144b36bd8ef44df 100644 (file)
@@ -2720,8 +2720,7 @@ virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
     if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_MODEL_EXPANSION))
         return 0;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto cleanup;
+    cpu = virCPUDefNew();
 
     cpu->model = g_strdup(model);
 
@@ -3355,10 +3354,7 @@ virQEMUCapsInitCPUModel(virQEMUCapsPtr qemuCaps,
 static virCPUDefPtr
 virQEMUCapsNewHostCPUModel(void)
 {
-    virCPUDefPtr cpu;
-
-    if (VIR_ALLOC(cpu) < 0)
-        return NULL;
+    virCPUDefPtr cpu = virCPUDefNew();
 
     cpu->type = VIR_CPU_TYPE_GUEST;
     cpu->mode = VIR_CPU_MODE_CUSTOM;
index c741c2fcbda23611cdfeef726707e7ea14ed82f1..a15364a71ed6deae16d3b9b0aeb038f630c558c1 100644 (file)
@@ -4481,7 +4481,7 @@ qemuDomainDefSetDefaultCPU(virDomainDefPtr def,
     }
 
     if (!def->cpu)
-        def->cpu = g_new0(virCPUDef, 1);
+        def->cpu = virCPUDefNew();
 
     def->cpu->type = VIR_CPU_TYPE_GUEST;
 
index df5eafbd448fbe0c03ccec9b3c8563f213ce2257..37211b7c62840b6fb7dd4f1e011b8a427fda5c7c 100644 (file)
@@ -1475,8 +1475,7 @@ virVMXParseConfig(virVMXContext *ctx,
         goto cleanup;
 
     if (coresPerSocket > 1) {
-        if (VIR_ALLOC(cpu) < 0)
-            goto cleanup;
+        cpu = virCPUDefNew();
 
         cpu->type = VIR_CPU_TYPE_GUEST;
         cpu->mode = VIR_CPU_MODE_CUSTOM;
index b883ba7b80d873f5f0dfd40f438d81776e2164b5..fd86344ea4f2953f7eadb4b8f33db3f92618b933 100644 (file)
@@ -482,8 +482,7 @@ cpuTestMakeQEMUCaps(const struct data *data)
     if (!(testMon = qemuMonitorTestNewFromFile(json, driver.xmlopt, true)))
         goto error;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto cleanup;
+    cpu = virCPUDefNew();
 
     cpu->model = g_strdup("host");
 
@@ -584,9 +583,7 @@ cpuTestCPUID(bool guest, const void *arg)
         !(hostData = virCPUDataParse(host)))
         goto cleanup;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto cleanup;
-
+    cpu = virCPUDefNew();
     cpu->arch = hostData->arch;
     if (guest) {
         cpu->type = VIR_CPU_TYPE_GUEST;
@@ -889,9 +886,7 @@ cpuTestJSONCPUID(const void *arg)
     if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
         goto cleanup;
 
-    if (VIR_ALLOC(cpu) < 0)
-        goto cleanup;
-
+    cpu = virCPUDefNew();
     cpu->arch = data->arch;
     cpu->type = VIR_CPU_TYPE_GUEST;
     cpu->match = VIR_CPU_MATCH_EXACT;