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.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/conf/cpu_conf.c | 29 ++++++++++++++++++++++-------
src/conf/cpu_conf.h | 4 ++++
src/cpu/cpu.c | 3 +--
src/cpu/cpu_arm.c | 5 +----
src/cpu/cpu_ppc64.c | 3 +--
src/cpu/cpu_x86.c | 6 ++----
src/libvirt_private.syms | 2 ++
src/libxl/libxl_capabilities.c | 3 +--
src/libxl/xen_xl.c | 11 +++--------
src/qemu/qemu_capabilities.c | 8 ++------
src/qemu/qemu_domain.c | 2 +-
src/vmx/vmx.c | 3 +--
tests/cputest.c | 11 +++--------
13 files changed, 44 insertions(+), 46 deletions(-)
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index 4542bcb7bd..7490d6bf73 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -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)) {
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
index 96fda3e6b3..ec3d2379cf 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -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);
diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index 40f725fd21..a2ae5b8c07 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -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:
diff --git a/src/cpu/cpu_arm.c b/src/cpu/cpu_arm.c
index 70dba6021c..ee5802198f 100644
--- a/src/cpu/cpu_arm.c
+++ b/src/cpu/cpu_arm.c
@@ -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);
diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
index 3236a9616e..818f1ec699 100644
--- a/src/cpu/cpu_ppc64.c
+++ b/src/cpu/cpu_ppc64.c
@@ -757,8 +757,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
}
}
- if (VIR_ALLOC(cpu) < 0)
- goto error;
+ cpu = virCPUDefNew();
cpu->model = g_strdup(model->name);
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index c83cab0c53..1e913cc9fa 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -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;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0465ce8fc1..defa527306 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -104,8 +104,10 @@ virCPUDefFreeModel;
virCPUDefIsEqual;
virCPUDefListFree;
virCPUDefListParse;
+virCPUDefNew;
virCPUDefParseXML;
virCPUDefParseXMLString;
+virCPUDefRef;
virCPUDefStealModel;
virCPUDefUpdateFeature;
virCPUModeTypeToString;
diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index e2327474fa..e9f958cd8a 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -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 &&
diff --git a/src/libxl/xen_xl.c b/src/libxl/xen_xl.c
index 18ea4a8d95..cdff71f11b 100644
--- a/src/libxl/xen_xl.c
+++ b/src/libxl/xen_xl.c
@@ -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) {
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 66bee49d6f..16c4331b13 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -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;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index a18da01bf9..29a417e931 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -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;
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index df5eafbd44..37211b7c62 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -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;
diff --git a/tests/cputest.c b/tests/cputest.c
index b883ba7b80..fd86344ea4 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -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;
--
2.23.0