VIR_AUTOFREE is a beautiful macro. Let's use it across the board
inside qemu_driver.c to make the code a bit tidier and smaller,
sparing VIR_FREE() calls and sometimes a whole 'cleanup'
label.
This is the last part of this change.
Signed-off-by: Daniel Henrique Barboza <danielhb413(a)gmail.com>
---
src/qemu/qemu_driver.c | 119 ++++++++++++++---------------------------
1 file changed, 41 insertions(+), 78 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 489698108d..bd05aa9e11 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -12055,7 +12055,7 @@ qemuDomainMemoryPeek(virDomainPtr dom,
{
virQEMUDriverPtr driver = dom->conn->privateData;
virDomainObjPtr vm;
- char *tmp = NULL;
+ VIR_AUTOFREE(char *) tmp = NULL;
int fd = -1, ret = -1;
qemuDomainObjPrivatePtr priv;
VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
@@ -12127,7 +12127,6 @@ qemuDomainMemoryPeek(virDomainPtr dom,
VIR_FORCE_CLOSE(fd);
if (tmp)
unlink(tmp);
- VIR_FREE(tmp);
virDomainObjEndAPI(&vm);
return ret;
}
@@ -12293,7 +12292,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
int ret = -1;
int fd = -1;
struct stat sb;
- char *buf = NULL;
+ VIR_AUTOFREE(char *) buf = NULL;
ssize_t len;
if ((rc = qemuDomainStorageOpenStat(driver, cfg, vm, src, &fd, &sb,
@@ -12329,7 +12328,6 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
ret = 1;
cleanup:
- VIR_FREE(buf);
qemuDomainStorageCloseStat(src, &fd);
return ret;
}
@@ -12550,7 +12548,7 @@ qemuDomainMigratePrepareTunnel(virConnectPtr dconn,
{
virQEMUDriverPtr driver = dconn->privateData;
virDomainDefPtr def = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -12585,7 +12583,6 @@ qemuDomainMigratePrepareTunnel(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -12607,7 +12604,7 @@ qemuDomainMigratePrepare2(virConnectPtr dconn,
{
virQEMUDriverPtr driver = dconn->privateData;
virDomainDefPtr def = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -12652,7 +12649,6 @@ qemuDomainMigratePrepare2(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -12853,7 +12849,7 @@ qemuDomainMigratePrepare3(virConnectPtr dconn,
{
virQEMUDriverPtr driver = dconn->privateData;
virDomainDefPtr def = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -12888,7 +12884,6 @@ qemuDomainMigratePrepare3(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -12914,7 +12909,7 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
int nbdPort = 0;
int nmigrate_disks;
const char **migrate_disks = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -12977,7 +12972,6 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
VIR_FREE(migrate_disks);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -12997,7 +12991,7 @@ qemuDomainMigratePrepareTunnel3(virConnectPtr dconn,
{
virQEMUDriverPtr driver = dconn->privateData;
virDomainDefPtr def = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -13026,7 +13020,6 @@ qemuDomainMigratePrepareTunnel3(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -13046,7 +13039,7 @@ qemuDomainMigratePrepareTunnel3Params(virConnectPtr dconn,
virDomainDefPtr def = NULL;
const char *dom_xml = NULL;
const char *dname = NULL;
- char *origname = NULL;
+ VIR_AUTOFREE(char *) origname = NULL;
qemuMigrationParamsPtr migParams = NULL;
int ret = -1;
@@ -13085,7 +13078,6 @@ qemuDomainMigratePrepareTunnel3Params(virConnectPtr dconn,
cleanup:
qemuMigrationParamsFree(migParams);
- VIR_FREE(origname);
virDomainDefFree(def);
return ret;
}
@@ -13407,7 +13399,7 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
unsigned domain = 0, bus = 0, slot = 0, function = 0;
int ret = -1;
virNodeDeviceDefPtr def = NULL;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
bool vfio = qemuHostdevHostSupportsPassthroughVFIO();
virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
@@ -13457,7 +13449,6 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
cleanup:
virPCIDeviceFree(pci);
virNodeDeviceDefFree(def);
- VIR_FREE(xml);
return ret;
}
@@ -13475,7 +13466,7 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev)
unsigned domain = 0, bus = 0, slot = 0, function = 0;
int ret = -1;
virNodeDeviceDefPtr def = NULL;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
xml = virNodeDeviceGetXMLDesc(dev, 0);
@@ -13501,7 +13492,6 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev)
virPCIDeviceFree(pci);
cleanup:
virNodeDeviceDefFree(def);
- VIR_FREE(xml);
return ret;
}
@@ -13513,7 +13503,7 @@ qemuNodeDeviceReset(virNodeDevicePtr dev)
unsigned domain = 0, bus = 0, slot = 0, function = 0;
int ret = -1;
virNodeDeviceDefPtr def = NULL;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
xml = virNodeDeviceGetXMLDesc(dev, 0);
@@ -13539,7 +13529,6 @@ qemuNodeDeviceReset(virNodeDevicePtr dev)
virPCIDeviceFree(pci);
cleanup:
virNodeDeviceDefFree(def);
- VIR_FREE(xml);
return ret;
}
@@ -15229,7 +15218,6 @@ qemuDomainSnapshotDiskPrepareOne(virQEMUDriverPtr driver,
qemuDomainAsyncJob asyncJob)
{
qemuDomainObjPrivatePtr priv = vm->privateData;
- char *backingStoreStr;
virDomainDiskDefPtr persistdisk;
VIR_AUTOUNREF(virStorageSourcePtr) terminator = NULL;
bool supportsCreate;
@@ -15275,13 +15263,13 @@ qemuDomainSnapshotDiskPrepareOne(virQEMUDriverPtr driver,
* block commit still works */
if (reuse) {
if (supportsBacking) {
+ VIR_AUTOFREE(char *) backingStoreStr = NULL;
+
if (virStorageFileGetBackingStoreStr(dd->src, &backingStoreStr)
< 0)
return -1;
if (backingStoreStr != NULL) {
if (virStorageIsRelative(backingStoreStr))
VIR_STEAL_PTR(dd->relPath, backingStoreStr);
- else
- VIR_FREE(backingStoreStr);
}
}
} else {
@@ -15728,7 +15716,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
{
virQEMUDriverPtr driver = domain->conn->privateData;
virDomainObjPtr vm = NULL;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
virDomainMomentObjPtr snap = NULL;
virDomainSnapshotPtr snapshot = NULL;
virDomainMomentObjPtr current = NULL;
@@ -16025,7 +16013,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
cleanup:
virDomainObjEndAPI(&vm);
- VIR_FREE(xml);
virObjectUnref(caps);
return snapshot;
}
@@ -17029,9 +17016,8 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr
caps,
virDomainObjPtr vm,
virDomainCheckpointDefPtr def)
{
- int ret = -1;
size_t i;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
qemuDomainObjPrivatePtr priv = vm->privateData;
/* Easiest way to clone inactive portion of vm->def is via
@@ -17043,10 +17029,10 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr
caps,
priv->qemuCaps,
VIR_DOMAIN_DEF_PARSE_INACTIVE |
VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE)))
- goto cleanup;
+ return -1;
if (virDomainCheckpointAlignDisks(def) < 0)
- goto cleanup;
+ return -1;
for (i = 0; i < def->ndisks; i++) {
virDomainCheckpointDiskDefPtr disk = &def->disks[i];
@@ -17061,15 +17047,11 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr
caps,
disk->name,
virStorageFileFormatTypeToString(
vm->def->disks[i]->src->format));
- goto cleanup;
+ return -1;
}
}
- ret = 0;
-
- cleanup:
- VIR_FREE(xml);
- return ret;
+ return 0;
}
static int
@@ -17139,7 +17121,7 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain,
{
virQEMUDriverPtr driver = domain->conn->privateData;
virDomainObjPtr vm = NULL;
- char *xml = NULL;
+ VIR_AUTOFREE(char *) xml = NULL;
virDomainMomentObjPtr chk = NULL;
virDomainCheckpointPtr checkpoint = NULL;
bool update_current = true;
@@ -17286,7 +17268,6 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain,
cleanup:
virJSONValueFree(actions);
virDomainObjEndAPI(&vm);
- VIR_FREE(xml);
virObjectUnref(caps);
return checkpoint;
}
@@ -18174,7 +18155,7 @@ qemuDomainBlockJobSetSpeed(virDomainPtr dom,
virDomainDiskDefPtr disk;
int ret = -1;
virDomainObjPtr vm;
- char *device = NULL;
+ VIR_AUTOFREE(char *) device = NULL;
unsigned long long speed = bandwidth;
virCheckFlags(VIR_DOMAIN_BLOCK_JOB_SPEED_BANDWIDTH_BYTES, -1);
@@ -18219,7 +18200,6 @@ qemuDomainBlockJobSetSpeed(virDomainPtr dom,
qemuDomainObjEndJob(driver, vm);
cleanup:
- VIR_FREE(device);
virDomainObjEndAPI(&vm);
return ret;
@@ -19310,7 +19290,7 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
virDomainDefPtr def = NULL;
virDomainDefPtr persistentDef = NULL;
virDomainBlockIoTuneInfo info;
- char *drivealias = NULL;
+ VIR_AUTOFREE(char *) drivealias = NULL;
const char *qdevid = NULL;
int ret = -1;
size_t i;
@@ -19637,7 +19617,6 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
cleanup:
VIR_FREE(info.group_name);
- VIR_FREE(drivealias);
virDomainObjEndAPI(&vm);
if (eventNparams)
virTypedParamsFree(eventParams, eventNparams);
@@ -19658,7 +19637,7 @@ qemuDomainGetBlockIoTune(virDomainPtr dom,
virDomainDefPtr def = NULL;
virDomainDefPtr persistentDef = NULL;
virDomainBlockIoTuneInfo reply = {0};
- char *drivealias = NULL;
+ VIR_AUTOFREE(char *) drivealias = NULL;
const char *qdevid = NULL;
int ret = -1;
int maxparams;
@@ -19795,7 +19774,6 @@ qemuDomainGetBlockIoTune(virDomainPtr dom,
cleanup:
VIR_FREE(reply.group_name);
- VIR_FREE(drivealias);
virDomainObjEndAPI(&vm);
return ret;
}
@@ -21593,12 +21571,10 @@ qemuDomainGetStatsBlockExportDisk(virDomainDiskDefPtr disk,
bool blockdev)
{
- char *alias = NULL;
virStorageSourcePtr n;
const char *frontendalias;
const char *backendalias;
const char *backendstoragealias;
- int ret = -1;
/*
* This helps to keep logs clean from error messages on getting stats
@@ -21615,6 +21591,8 @@ qemuDomainGetStatsBlockExportDisk(virDomainDiskDefPtr disk,
}
for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
+ VIR_AUTOFREE(char *) alias = NULL;
+
if (blockdev) {
frontendalias = QEMU_DOMAIN_DISK_PRIVATE(disk)->qomName;
backendalias = n->nodeformat;
@@ -21623,7 +21601,7 @@ qemuDomainGetStatsBlockExportDisk(virDomainDiskDefPtr disk,
/* alias may be NULL if the VM is not running */
if (disk->info.alias &&
!(alias = qemuDomainStorageAlias(disk->info.alias, n->id)))
- goto cleanup;
+ return -1;
qemuDomainGetStatsOneBlockRefreshNamed(n, alias, stats, nodestats);
@@ -21634,37 +21612,32 @@ qemuDomainGetStatsBlockExportDisk(virDomainDiskDefPtr disk,
if (qemuDomainGetStatsBlockExportHeader(disk, n, *recordnr,
records, nrecords) < 0)
- goto cleanup;
+ return -1;
/* The following stats make sense only for the frontend device */
if (n == disk->src) {
if (qemuDomainGetStatsBlockExportFrontend(frontendalias, stats, *recordnr,
records, nrecords) < 0)
- goto cleanup;
+ return -1;
}
if (qemuDomainGetStatsOneBlock(driver, cfg, dom, records, nrecords,
backendalias, n, *recordnr,
stats) < 0)
- goto cleanup;
+ return -1;
if (qemuDomainGetStatsBlockExportBackendStorage(backendstoragealias,
stats, *recordnr,
records, nrecords) < 0)
- goto cleanup;
+ return -1;
- VIR_FREE(alias);
(*recordnr)++;
if (!visitBacking)
break;
}
- ret = 0;
-
- cleanup:
- VIR_FREE(alias);
- return ret;
+ return 0;
}
@@ -22441,12 +22414,12 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
virObjectEventPtr event_new = NULL;
virObjectEventPtr event_old = NULL;
int ret = -1;
- char *new_dom_name = NULL;
- char *old_dom_name = NULL;
- char *new_dom_cfg_file = NULL;
- char *old_dom_cfg_file = NULL;
- char *new_dom_autostart_link = NULL;
- char *old_dom_autostart_link = NULL;
+ VIR_AUTOFREE(char *) new_dom_name = NULL;
+ VIR_AUTOFREE(char *) old_dom_name = NULL;
+ VIR_AUTOFREE(char *) new_dom_cfg_file = NULL;
+ VIR_AUTOFREE(char *) old_dom_cfg_file = NULL;
+ VIR_AUTOFREE(char *) new_dom_autostart_link = NULL;
+ VIR_AUTOFREE(char *) old_dom_autostart_link = NULL;
virCheckFlags(0, ret);
@@ -22506,7 +22479,7 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
if (virFileIsLink(old_dom_autostart_link) &&
unlink(old_dom_autostart_link) < 0) {
virReportSystemError(errno,
- _("Failed to delete symlink '%s'"),
+ _("Failed to delete symlink '%s'"),
old_dom_autostart_link);
goto rollback;
}
@@ -22518,12 +22491,6 @@ qemuDomainRenameCallback(virDomainObjPtr vm,
ret = 0;
cleanup:
- VIR_FREE(old_dom_autostart_link);
- VIR_FREE(new_dom_autostart_link);
- VIR_FREE(old_dom_cfg_file);
- VIR_FREE(new_dom_cfg_file);
- VIR_FREE(old_dom_name);
- VIR_FREE(new_dom_name);
virObjectEventStateQueue(driver->domainEventState, event_old);
virObjectEventStateQueue(driver->domainEventState, event_new);
return ret;
@@ -22628,7 +22595,7 @@ qemuDomainGetGuestVcpusParams(virTypedParameterPtr *params,
virBitmapPtr vcpus = NULL;
virBitmapPtr online = NULL;
virBitmapPtr offlinable = NULL;
- char *tmp = NULL;
+ VIR_AUTOFREE(char *) tmp = NULL;
size_t i;
int ret = -1;
@@ -22657,7 +22624,6 @@ qemuDomainGetGuestVcpusParams(virTypedParameterPtr *params,
goto cleanup; \
if (virTypedParamsAddString(&par, &npar, &maxpar, #name, tmp) < 0) \
goto cleanup; \
- VIR_FREE(tmp)
ADD_BITMAP(vcpus);
ADD_BITMAP(online);
@@ -22671,7 +22637,6 @@ qemuDomainGetGuestVcpusParams(virTypedParameterPtr *params,
ret = 0;
cleanup:
- VIR_FREE(tmp);
virBitmapFree(vcpus);
virBitmapFree(online);
virBitmapFree(offlinable);
@@ -22899,7 +22864,7 @@ qemuDomainSetBlockThreshold(virDomainPtr dom,
qemuDomainObjPrivatePtr priv;
virDomainObjPtr vm = NULL;
virStorageSourcePtr src;
- char *nodename = NULL;
+ VIR_AUTOFREE(char *) nodename = NULL;
int rc;
int ret = -1;
@@ -22953,7 +22918,6 @@ qemuDomainSetBlockThreshold(virDomainPtr dom,
qemuDomainObjEndJob(driver, vm);
cleanup:
- VIR_FREE(nodename);
virDomainObjEndAPI(&vm);
return ret;
}
@@ -23133,7 +23097,7 @@ qemuDomainGetSEVMeasurement(virQEMUDriverPtr driver,
unsigned int flags)
{
int ret = -1;
- char *tmp = NULL;
+ VIR_AUTOFREE(char *) tmp = NULL;
int maxpar = 0;
virCheckFlags(VIR_TYPED_PARAM_STRING_OKAY, -1);
@@ -23158,7 +23122,6 @@ qemuDomainGetSEVMeasurement(virQEMUDriverPtr driver,
ret = 0;
endjob:
- VIR_FREE(tmp);
qemuDomainObjEndJob(driver, vm);
return ret;
}
--
2.21.0