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 void
virCPUDefFreeFeatures(virCPUDefPtr def) virCPUDefFreeFeatures(virCPUDefPtr def)
{ {
@ -104,16 +111,24 @@ virCPUDefFreeModel(virCPUDefPtr def)
virCPUDefFreeFeatures(def); virCPUDefFreeFeatures(def);
} }
void
virCPUDefRef(virCPUDefPtr def)
{
g_atomic_int_inc(&def->refs);
}
void void
virCPUDefFree(virCPUDefPtr def) virCPUDefFree(virCPUDefPtr def)
{ {
if (!def) if (!def)
return; return;
virCPUDefFreeModel(def); if (g_atomic_int_dec_and_test(&def->refs)) {
VIR_FREE(def->cache); virCPUDefFreeModel(def);
VIR_FREE(def->tsc); VIR_FREE(def->cache);
VIR_FREE(def); VIR_FREE(def->tsc);
VIR_FREE(def);
}
} }
@ -214,9 +229,10 @@ virCPUDefCopyWithoutModel(const virCPUDef *cpu)
{ {
virCPUDefPtr copy; virCPUDefPtr copy;
if (!cpu || VIR_ALLOC(copy) < 0) if (!cpu)
return NULL; return NULL;
copy = virCPUDefNew();
copy->type = cpu->type; copy->type = cpu->type;
copy->mode = cpu->mode; copy->mode = cpu->mode;
copy->match = cpu->match; copy->match = cpu->match;
@ -340,8 +356,7 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC(def) < 0) def = virCPUDefNew();
goto cleanup;
if (type == VIR_CPU_TYPE_AUTO) { if (type == VIR_CPU_TYPE_AUTO) {
if (virXPathBoolean("boolean(./arch)", ctxt)) { if (virXPathBoolean("boolean(./arch)", ctxt)) {

View File

@ -122,6 +122,7 @@ struct _virCPUCacheDef {
typedef struct _virCPUDef virCPUDef; typedef struct _virCPUDef virCPUDef;
typedef virCPUDef *virCPUDefPtr; typedef virCPUDef *virCPUDefPtr;
struct _virCPUDef { struct _virCPUDef {
int refs;
int type; /* enum virCPUType */ int type; /* enum virCPUType */
int mode; /* enum virCPUMode */ int mode; /* enum virCPUMode */
int match; /* enum virCPUMatch */ int match; /* enum virCPUMatch */
@ -142,6 +143,7 @@ struct _virCPUDef {
virHostCPUTscInfoPtr tsc; virHostCPUTscInfoPtr tsc;
}; };
virCPUDefPtr virCPUDefNew(void);
void ATTRIBUTE_NONNULL(1) void ATTRIBUTE_NONNULL(1)
virCPUDefFreeFeatures(virCPUDefPtr def); virCPUDefFreeFeatures(virCPUDefPtr def);
@ -149,6 +151,8 @@ virCPUDefFreeFeatures(virCPUDefPtr def);
void ATTRIBUTE_NONNULL(1) void ATTRIBUTE_NONNULL(1)
virCPUDefFreeModel(virCPUDefPtr def); virCPUDefFreeModel(virCPUDefPtr def);
void
virCPUDefRef(virCPUDefPtr def);
void void
virCPUDefFree(virCPUDefPtr def); virCPUDefFree(virCPUDefPtr def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCPUDef, virCPUDefFree); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCPUDef, virCPUDefFree);

View File

@ -393,8 +393,7 @@ virCPUGetHost(virArch arch,
if (!(driver = cpuGetSubDriver(arch))) if (!(driver = cpuGetSubDriver(arch)))
return NULL; return NULL;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
return NULL;
switch (type) { switch (type) {
case VIR_CPU_TYPE_HOST: case VIR_CPU_TYPE_HOST:

View File

@ -211,10 +211,7 @@ virCPUarmBaseline(virCPUDefPtr *cpus,
{ {
virCPUDefPtr cpu = NULL; virCPUDefPtr cpu = NULL;
if (VIR_ALLOC(cpu) < 0) { cpu = virCPUDefNew();
virCPUDefFree(cpu);
return NULL;
}
cpu->model = g_strdup(cpus[0]->model); cpu->model = g_strdup(cpus[0]->model);

View File

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

View File

@ -775,8 +775,7 @@ x86DataToCPU(const virCPUx86Data *data,
virCPUx86Data modelData = VIR_CPU_X86_DATA_INIT; virCPUx86Data modelData = VIR_CPU_X86_DATA_INIT;
virCPUx86VendorPtr vendor; virCPUx86VendorPtr vendor;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto error;
cpu->model = g_strdup(model->name); cpu->model = g_strdup(model->name);
@ -2807,8 +2806,7 @@ virCPUx86Baseline(virCPUDefPtr *cpus,
if (!(base_model = x86ModelFromCPU(cpus[0], map, -1))) if (!(base_model = x86ModelFromCPU(cpus[0], map, -1)))
goto error; goto error;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto error;
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
cpu->match = VIR_CPU_MATCH_EXACT; cpu->match = VIR_CPU_MATCH_EXACT;

View File

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

View File

@ -169,8 +169,7 @@ libxlCapsInitCPU(virCapsPtr caps, libxl_physinfo *phy_info,
if (!phy_info->hw_cap[0]) if (!phy_info->hw_cap[0])
return 0; return 0;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto error;
host_pae = phy_info->hw_cap[0] & LIBXL_X86_FEATURE_PAE_MASK; host_pae = phy_info->hw_cap[0] & LIBXL_X86_FEATURE_PAE_MASK;
if (host_pae && if (host_pae &&

View File

@ -177,10 +177,7 @@ xenParseXLOS(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
} }
if (!def->cpu) { if (!def->cpu) {
virCPUDefPtr cpu; virCPUDefPtr cpu = virCPUDefNew();
if (VIR_ALLOC(cpu) < 0)
return -1;
cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH; cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
cpu->nfeatures = 0; cpu->nfeatures = 0;
@ -266,8 +263,7 @@ xenParseXLCPUID(virConfPtr conf, virDomainDefPtr def)
return 0; return 0;
if (!def->cpu) { if (!def->cpu) {
if (VIR_ALLOC(def->cpu) < 0) def->cpu = virCPUDefNew();
goto cleanup;
def->cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH; def->cpu->mode = VIR_CPU_MODE_HOST_PASSTHROUGH;
def->cpu->type = VIR_CPU_TYPE_GUEST; def->cpu->type = VIR_CPU_TYPE_GUEST;
def->cpu->nfeatures = 0; def->cpu->nfeatures = 0;
@ -445,8 +441,7 @@ xenParseXLVnuma(virConfPtr conf,
if (!virDomainNumaSetNodeCount(numa, nr_nodes)) if (!virDomainNumaSetNodeCount(numa, nr_nodes))
goto cleanup; goto cleanup;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
list = list->list; list = list->list;
while (list) { while (list) {

View File

@ -2720,8 +2720,7 @@ virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_MODEL_EXPANSION)) if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_MODEL_EXPANSION))
return 0; return 0;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
cpu->model = g_strdup(model); cpu->model = g_strdup(model);
@ -3355,10 +3354,7 @@ virQEMUCapsInitCPUModel(virQEMUCapsPtr qemuCaps,
static virCPUDefPtr static virCPUDefPtr
virQEMUCapsNewHostCPUModel(void) virQEMUCapsNewHostCPUModel(void)
{ {
virCPUDefPtr cpu; virCPUDefPtr cpu = virCPUDefNew();
if (VIR_ALLOC(cpu) < 0)
return NULL;
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
cpu->mode = VIR_CPU_MODE_CUSTOM; cpu->mode = VIR_CPU_MODE_CUSTOM;

View File

@ -4481,7 +4481,7 @@ qemuDomainDefSetDefaultCPU(virDomainDefPtr def,
} }
if (!def->cpu) if (!def->cpu)
def->cpu = g_new0(virCPUDef, 1); def->cpu = virCPUDefNew();
def->cpu->type = VIR_CPU_TYPE_GUEST; def->cpu->type = VIR_CPU_TYPE_GUEST;

View File

@ -1475,8 +1475,7 @@ virVMXParseConfig(virVMXContext *ctx,
goto cleanup; goto cleanup;
if (coresPerSocket > 1) { if (coresPerSocket > 1) {
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
cpu->mode = VIR_CPU_MODE_CUSTOM; cpu->mode = VIR_CPU_MODE_CUSTOM;

View File

@ -482,8 +482,7 @@ cpuTestMakeQEMUCaps(const struct data *data)
if (!(testMon = qemuMonitorTestNewFromFile(json, driver.xmlopt, true))) if (!(testMon = qemuMonitorTestNewFromFile(json, driver.xmlopt, true)))
goto error; goto error;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
cpu->model = g_strdup("host"); cpu->model = g_strdup("host");
@ -584,9 +583,7 @@ cpuTestCPUID(bool guest, const void *arg)
!(hostData = virCPUDataParse(host))) !(hostData = virCPUDataParse(host)))
goto cleanup; goto cleanup;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
cpu->arch = hostData->arch; cpu->arch = hostData->arch;
if (guest) { if (guest) {
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
@ -889,9 +886,7 @@ cpuTestJSONCPUID(const void *arg)
if (!(qemuCaps = cpuTestMakeQEMUCaps(data))) if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
goto cleanup; goto cleanup;
if (VIR_ALLOC(cpu) < 0) cpu = virCPUDefNew();
goto cleanup;
cpu->arch = data->arch; cpu->arch = data->arch;
cpu->type = VIR_CPU_TYPE_GUEST; cpu->type = VIR_CPU_TYPE_GUEST;
cpu->match = VIR_CPU_MATCH_EXACT; cpu->match = VIR_CPU_MATCH_EXACT;