From: "Daniel P. Berrange" <berrange(a)redhat.com>
Remove custom reference counting from virLXCMonitor, using
virObject instead
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/lxc/lxc_monitor.c | 63 +++++++++++++++++++++------------------------------
src/lxc/lxc_monitor.h | 4 +---
src/lxc/lxc_process.c | 11 +++------
3 files changed, 30 insertions(+), 48 deletions(-)
diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c
index dacaab4..772c613 100644
--- a/src/lxc/lxc_monitor.c
+++ b/src/lxc/lxc_monitor.c
@@ -35,9 +35,9 @@
#define VIR_FROM_THIS VIR_FROM_LXC
struct _virLXCMonitor {
- int refs;
+ virObject parent;
- virMutex lock; /* also used to protect refs */
+ virMutex lock;
virDomainObjPtr vm;
virLXCMonitorCallbacksPtr cb;
@@ -46,7 +46,21 @@ struct _virLXCMonitor {
virNetClientProgramPtr program;
};
-static void virLXCMonitorFree(virLXCMonitorPtr mon);
+static virClassPtr virLXCMonitorClass;
+static void virLXCMonitorDispose(void *obj);
+
+static int virLXCMonitorOnceInit(void)
+{
+ if (!(virLXCMonitorClass = virClassNew("virLXCMonitor",
+ sizeof(virLXCMonitor),
+ virLXCMonitorDispose)))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virLXCMonitor)
+
static void
virLXCMonitorHandleEventExit(virNetClientProgramPtr prog,
virNetClientPtr client,
@@ -95,9 +109,7 @@ static void virLXCMonitorEOFNotify(virNetClientPtr client
ATTRIBUTE_UNUSED,
static void virLXCMonitorCloseFreeCallback(void *opaque)
{
virLXCMonitorPtr mon = opaque;
- virLXCMonitorLock(mon);
- if (virLXCMonitorUnref(mon) > 0)
- virLXCMonitorUnlock(mon);
+ virObjectUnref(mon);;
}
@@ -108,12 +120,11 @@ virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
virLXCMonitorPtr mon;
char *sockpath = NULL;
- if (VIR_ALLOC(mon) < 0) {
- virReportOOMError();
+ if (virLXCMonitorInitialize() < 0)
return NULL;
- }
- mon->refs = 1;
+ if (!(mon = virObjectNew(virLXCMonitorClass)))
+ return NULL;
if (virMutexInit(&mon->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -146,7 +157,7 @@ virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
mon->vm = vm;
mon->cb = cb;
- virLXCMonitorRef(mon);
+ virObjectRef(mon);
virNetClientSetCloseCallback(mon->client, virLXCMonitorEOFNotify, mon,
virLXCMonitorCloseFreeCallback);
@@ -157,43 +168,21 @@ cleanup:
no_memory:
virReportOOMError();
error:
- virLXCMonitorFree(mon);
+ virObjectUnref(mon);
mon = NULL;
goto cleanup;
}
-static void virLXCMonitorFree(virLXCMonitorPtr mon)
+static void virLXCMonitorDispose(void *opaque)
{
- VIR_DEBUG("mon=%p", mon);
- if (mon->client)
- virLXCMonitorClose(mon);
+ virLXCMonitorPtr mon = opaque;
+ VIR_DEBUG("mon=%p", mon);
if (mon->cb && mon->cb->destroy)
(mon->cb->destroy)(mon, mon->vm);
virMutexDestroy(&mon->lock);
virObjectUnref(mon->program);
- VIR_FREE(mon);
-}
-
-
-int virLXCMonitorRef(virLXCMonitorPtr mon)
-{
- mon->refs++;
- return mon->refs;
-}
-
-int virLXCMonitorUnref(virLXCMonitorPtr mon)
-{
- mon->refs--;
-
- if (mon->refs == 0) {
- virLXCMonitorUnlock(mon);
- virLXCMonitorFree(mon);
- return 0;
- }
-
- return mon->refs;
}
diff --git a/src/lxc/lxc_monitor.h b/src/lxc/lxc_monitor.h
index 806a6c6..bb8349a 100644
--- a/src/lxc/lxc_monitor.h
+++ b/src/lxc/lxc_monitor.h
@@ -21,6 +21,7 @@
#ifndef __LXC_MONITOR_H__
# define __LXC_MONITOR_H__
+# include "virobject.h"
# include "domain_conf.h"
# include "lxc_protocol.h"
@@ -54,7 +55,4 @@ void virLXCMonitorClose(virLXCMonitorPtr mon);
void virLXCMonitorLock(virLXCMonitorPtr mon);
void virLXCMonitorUnlock(virLXCMonitorPtr mon);
-int virLXCMonitorRef(virLXCMonitorPtr mon);
-int virLXCMonitorUnref(virLXCMonitorPtr mon);
-
#endif /* __LXC_MONITOR_H__ */
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 84128d1..15572cd 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -238,9 +238,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
if (priv->monitor) {
virLXCMonitorClose(priv->monitor);
- virLXCMonitorLock(priv->monitor);
- if (virLXCMonitorUnref(priv->monitor) > 0)
- virLXCMonitorUnlock(priv->monitor);
+ virObjectUnref(priv->monitor);
priv->monitor = NULL;
}
@@ -675,8 +673,7 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr
driver,
if (virSecurityManagerClearSocketLabel(driver->securityManager, vm->def) <
0) {
if (monitor) {
- virLXCMonitorLock(monitor);
- virLXCMonitorUnref(monitor);
+ virObjectUnref(monitor);
monitor = NULL;
}
goto cleanup;
@@ -1198,9 +1195,7 @@ cleanup:
}
if (rc != 0) {
if (priv->monitor) {
- virLXCMonitorLock(priv->monitor);
- if (virLXCMonitorUnref(priv->monitor) > 0)
- virLXCMonitorUnlock(priv->monitor);
+ virObjectUnref(priv->monitor);
priv->monitor = NULL;
}
virDomainConfVMNWFilterTeardown(vm);
--
1.7.11.2