This way qemuDomainLogContextRef() and qemuDomainLogContextFree() is
no longer needed. The naming qemuDomainLogContextFree() was also
somewhat misleading. Additionally, it's easier to turn
qemuDomainLogContext in a self-locking object.
Signed-off-by: Marc Hartmayer <mhartmay(a)linux.vnet.ibm.com>
Reviewed-by: Bjoern Walk <bwalk(a)linux.vnet.ibm.com>
---
src/qemu/qemu_domain.c | 72 ++++++++++++++++++++++++++-----------------------
src/qemu/qemu_domain.h | 2 --
src/qemu/qemu_process.c | 10 +++----
3 files changed, 44 insertions(+), 40 deletions(-)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index b733505..6be7a4e 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -111,7 +111,8 @@ VIR_ENUM_IMPL(qemuDomainNamespace, QEMU_DOMAIN_NS_LAST,
struct _qemuDomainLogContext {
- int refs;
+ virObject parent;
+
int writefd;
int readfd; /* Only used if manager == NULL */
off_t pos;
@@ -120,6 +121,36 @@ struct _qemuDomainLogContext {
virLogManagerPtr manager;
};
+static virClassPtr qemuDomainLogContextClass;
+
+static void qemuDomainLogContextDispose(void *obj);
+
+static int
+qemuDomainLogContextOnceInit(void)
+{
+ if (!(qemuDomainLogContextClass = virClassNew(virClassForObject(),
+ "qemuDomainLogContext",
+ sizeof(qemuDomainLogContext),
+ qemuDomainLogContextDispose)))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(qemuDomainLogContext)
+
+static void
+qemuDomainLogContextDispose(void *obj)
+{
+ qemuDomainLogContextPtr ctxt = obj;
+ VIR_DEBUG("ctxt=%p", ctxt);
+
+ virLogManagerFree(ctxt->manager);
+ VIR_FREE(ctxt->path);
+ VIR_FORCE_CLOSE(ctxt->writefd);
+ VIR_FORCE_CLOSE(ctxt->readfd);
+}
+
const char *
qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job,
int phase ATTRIBUTE_UNUSED)
@@ -4175,7 +4206,7 @@ void qemuDomainObjTaint(virQEMUDriverPtr driver,
cleanup:
VIR_FREE(timestamp);
if (closeLog)
- qemuDomainLogContextFree(logCtxt);
+ virObjectUnref(logCtxt);
if (orig_err) {
virSetError(orig_err);
virFreeError(orig_err);
@@ -4287,13 +4318,15 @@ qemuDomainLogContextPtr qemuDomainLogContextNew(virQEMUDriverPtr
driver,
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
qemuDomainLogContextPtr ctxt = NULL;
- if (VIR_ALLOC(ctxt) < 0)
- goto error;
+ if (qemuDomainLogContextInitialize() < 0)
+ goto cleanup;
+
+ if (!(ctxt = virObjectNew(qemuDomainLogContextClass)))
+ goto cleanup;
VIR_DEBUG("Context new %p stdioLogD=%d", ctxt, cfg->stdioLogD);
ctxt->writefd = -1;
ctxt->readfd = -1;
- virAtomicIntSet(&ctxt->refs, 1);
if (virAsprintf(&ctxt->path, "%s/%s.log", cfg->logDir,
vm->def->name) < 0)
goto error;
@@ -4361,7 +4394,7 @@ qemuDomainLogContextPtr qemuDomainLogContextNew(virQEMUDriverPtr
driver,
return ctxt;
error:
- qemuDomainLogContextFree(ctxt);
+ virObjectUnref(ctxt);
ctxt = NULL;
goto cleanup;
}
@@ -4530,39 +4563,12 @@ void qemuDomainLogContextMarkPosition(qemuDomainLogContextPtr
ctxt)
}
-void qemuDomainLogContextRef(qemuDomainLogContextPtr ctxt)
-{
- VIR_DEBUG("Context ref %p", ctxt);
- virAtomicIntInc(&ctxt->refs);
-}
-
-
virLogManagerPtr qemuDomainLogContextGetManager(qemuDomainLogContextPtr ctxt)
{
return ctxt->manager;
}
-void qemuDomainLogContextFree(qemuDomainLogContextPtr ctxt)
-{
- bool lastRef;
-
- if (!ctxt)
- return;
-
- lastRef = virAtomicIntDecAndTest(&ctxt->refs);
- VIR_DEBUG("Context free %p lastref=%d", ctxt, lastRef);
- if (!lastRef)
- return;
-
- virLogManagerFree(ctxt->manager);
- VIR_FREE(ctxt->path);
- VIR_FORCE_CLOSE(ctxt->writefd);
- VIR_FORCE_CLOSE(ctxt->readfd);
- VIR_FREE(ctxt);
-}
-
-
/* Locate an appropriate 'qemu-img' binary. */
const char *
qemuFindQemuImgBinary(virQEMUDriverPtr driver)
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 91573ff..caac5d5 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -540,8 +540,6 @@ ssize_t qemuDomainLogContextRead(qemuDomainLogContextPtr ctxt,
char **msg);
int qemuDomainLogContextGetWriteFD(qemuDomainLogContextPtr ctxt);
void qemuDomainLogContextMarkPosition(qemuDomainLogContextPtr ctxt);
-void qemuDomainLogContextRef(qemuDomainLogContextPtr ctxt);
-void qemuDomainLogContextFree(qemuDomainLogContextPtr ctxt);
virLogManagerPtr qemuDomainLogContextGetManager(qemuDomainLogContextPtr ctxt);
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index e450d06..028f0c5 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -1692,7 +1692,7 @@ static void
qemuProcessMonitorLogFree(void *opaque)
{
qemuDomainLogContextPtr logCtxt = opaque;
- qemuDomainLogContextFree(logCtxt);
+ virObjectUnref(logCtxt);
}
static int
@@ -1731,7 +1731,7 @@ qemuConnectMonitor(virQEMUDriverPtr driver, virDomainObjPtr vm, int
asyncJob,
driver);
if (mon && logCtxt) {
- qemuDomainLogContextRef(logCtxt);
+ virObjectRef(logCtxt);
qemuMonitorSetDomainLog(mon,
qemuProcessMonitorReportLogError,
logCtxt,
@@ -5871,7 +5871,7 @@ qemuProcessLaunch(virConnectPtr conn,
cleanup:
qemuDomainSecretDestroy(vm);
virCommandFree(cmd);
- qemuDomainLogContextFree(logCtxt);
+ virObjectUnref(logCtxt);
virObjectUnref(cfg);
virObjectUnref(caps);
VIR_FREE(nicindexes);
@@ -6667,7 +6667,7 @@ int qemuProcessAttach(virConnectPtr conn ATTRIBUTE_UNUSED,
goto error;
}
- qemuDomainLogContextFree(logCtxt);
+ virObjectUnref(logCtxt);
VIR_FREE(seclabel);
VIR_FREE(sec_managers);
virObjectUnref(cfg);
@@ -6687,7 +6687,7 @@ int qemuProcessAttach(virConnectPtr conn ATTRIBUTE_UNUSED,
qemuMonitorClose(priv->mon);
priv->mon = NULL;
- qemuDomainLogContextFree(logCtxt);
+ virObjectUnref(logCtxt);
VIR_FREE(seclabel);
VIR_FREE(sec_managers);
if (seclabelgen)
--
2.5.5