conf: make virCPUDef into a ref counted struct

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>
This commit is contained in:
Daniel P. Berrangé 2019-11-29 11:00:26 +00:00
parent 1a1d848694
commit 8506afff7b
13 changed files with 44 additions and 46 deletions

View 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)) {

View 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);

View 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:

View 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);

View File

@ -757,8 +757,7 @@ virCPUppc64Baseline(virCPUDefPtr *cpus,
}
}
if (VIR_ALLOC(cpu) < 0)
goto error;
cpu = virCPUDefNew();
cpu->model = g_strdup(model->name);

View 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;

View File

@ -104,8 +104,10 @@ virCPUDefFreeModel;
virCPUDefIsEqual;
virCPUDefListFree;
virCPUDefListParse;
virCPUDefNew;
virCPUDefParseXML;
virCPUDefParseXMLString;
virCPUDefRef;
virCPUDefStealModel;
virCPUDefUpdateFeature;
virCPUModeTypeToString;

View 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 &&

View 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) {

View 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;

View 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;

View 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;

View 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;