Add and use qemuProcessEventFree for freeing qemuProcessEvents. This
is less error-prone as the compiler can help us make sure that for
every new enumeration value of qemuProcessEventType the
qemuProcessEventFree function has to be adapted.
All process*Event functions are *only* called by
qemuProcessHandleEvent and this function does the freeing by itself
with qemuProcessEventFree. This means that an explicit freeing of
processEvent->data is no longer required in each process*Event
handler.
The effectiveness of this change is also demonstrated by the fact that
it fixes a memory leak of the panic info data in
qemuProcessHandleGuestPanic.
Reported-by: Wang Dong <dongdwdw(a)linux.vnet.ibm.com>
Signed-off-by: Bjoern Walk <bwalk(a)linux.vnet.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay(a)linux.vnet.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy(a)linux.vnet.ibm.com>
---
src/qemu/qemu_domain.c | 23 +++++++++++++++++++++++
src/qemu/qemu_domain.h | 2 ++
src/qemu/qemu_driver.c | 12 ++----------
src/qemu/qemu_process.c | 22 +++++++---------------
4 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c8123ce59bc4..4472b00d6540 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -10910,3 +10910,26 @@ qemuDomainPrepareDiskSource(virConnectPtr conn,
return 0;
}
+
+
+void
+qemuProcessEventFree(struct qemuProcessEvent *event)
+{
+ if (!event)
+ return;
+
+ switch (event->eventType) {
+ case QEMU_PROCESS_EVENT_GUESTPANIC:
+ qemuMonitorEventPanicInfoFree(event->data);
+ break;
+ case QEMU_PROCESS_EVENT_WATCHDOG:
+ case QEMU_PROCESS_EVENT_DEVICE_DELETED:
+ case QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED:
+ case QEMU_PROCESS_EVENT_SERIAL_CHANGED:
+ case QEMU_PROCESS_EVENT_BLOCK_JOB:
+ case QEMU_PROCESS_EVENT_MONITOR_EOF:
+ case QEMU_PROCESS_EVENT_LAST:
+ VIR_FREE(event->data);
+ }
+ VIR_FREE(event);
+}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index ddfc46dcd0c1..7c9364f0bb69 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -445,6 +445,8 @@ struct qemuProcessEvent {
void *data;
};
+void qemuProcessEventFree(struct qemuProcessEvent *event);
+
typedef struct _qemuDomainLogContext qemuDomainLogContext;
typedef qemuDomainLogContext *qemuDomainLogContextPtr;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d64686df4c5f..d760b77c81e7 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4183,7 +4183,6 @@ processWatchdogEvent(virQEMUDriverPtr driver,
qemuDomainObjEndAsyncJob(driver, vm);
cleanup:
- VIR_FREE(dumpfile);
virObjectUnref(cfg);
}
@@ -4309,7 +4308,6 @@ processGuestPanicEvent(virQEMUDriverPtr driver,
qemuDomainRemoveInactiveJob(driver, vm);
cleanup:
- qemuMonitorEventPanicInfoFree(info);
virObjectUnref(cfg);
}
@@ -4351,7 +4349,6 @@ processDeviceDeletedEvent(virQEMUDriverPtr driver,
qemuDomainObjEndJob(driver, vm);
cleanup:
- VIR_FREE(devAlias);
virObjectUnref(cfg);
}
@@ -4648,7 +4645,6 @@ processNicRxFilterChangedEvent(virQEMUDriverPtr driver,
cleanup:
virNetDevRxFilterFree(hostFilter);
virNetDevRxFilterFree(guestFilter);
- VIR_FREE(devAlias);
virObjectUnref(cfg);
}
@@ -4735,9 +4731,7 @@ processSerialChangedEvent(virQEMUDriverPtr driver,
qemuDomainObjEndJob(driver, vm);
cleanup:
- VIR_FREE(devAlias);
virObjectUnref(cfg);
-
}
@@ -4751,7 +4745,7 @@ processBlockJobEvent(virQEMUDriverPtr driver,
virDomainDiskDefPtr disk;
if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
- goto cleanup;
+ return;
if (!virDomainObjIsActive(vm)) {
VIR_DEBUG("Domain is not running");
@@ -4763,8 +4757,6 @@ processBlockJobEvent(virQEMUDriverPtr driver,
endjob:
qemuDomainObjEndJob(driver, vm);
- cleanup:
- VIR_FREE(diskAlias);
}
@@ -4856,7 +4848,7 @@ static void qemuProcessEventHandler(void *data, void *opaque)
}
virDomainObjEndAPI(&vm);
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
}
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index c17a6e9abfc6..f9b608434f47 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -303,7 +303,7 @@ qemuProcessHandleMonitorEOF(qemuMonitorPtr mon,
if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
ignore_value(virObjectUnref(vm));
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
goto cleanup;
}
@@ -917,7 +917,7 @@ qemuProcessHandleWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
if (!virObjectUnref(vm))
vm = NULL;
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
}
}
}
@@ -1048,9 +1048,7 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virObjectUnlock(vm);
return 0;
error:
- if (processEvent)
- VIR_FREE(processEvent->data);
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
goto cleanup;
}
@@ -1356,7 +1354,7 @@ qemuProcessHandleGuestPanic(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
if (!virObjectUnref(vm))
vm = NULL;
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
}
cleanup:
@@ -1404,9 +1402,7 @@ qemuProcessHandleDeviceDeleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virObjectUnlock(vm);
return 0;
error:
- if (processEvent)
- VIR_FREE(processEvent->data);
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
goto cleanup;
}
@@ -1552,9 +1548,7 @@ qemuProcessHandleNicRxFilterChanged(qemuMonitorPtr mon
ATTRIBUTE_UNUSED,
virObjectUnlock(vm);
return 0;
error:
- if (processEvent)
- VIR_FREE(processEvent->data);
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
goto cleanup;
}
@@ -1594,9 +1588,7 @@ qemuProcessHandleSerialChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virObjectUnlock(vm);
return 0;
error:
- if (processEvent)
- VIR_FREE(processEvent->data);
- VIR_FREE(processEvent);
+ qemuProcessEventFree(processEvent);
goto cleanup;
}
--
2.13.4