From: "Daniel P. Berrange" <berrange(a)redhat.com>
Make virKeepAlive use the virObject APIs for reference counting
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/libvirt_probes.d | 4 +--
src/rpc/virkeepalive.c | 73 +++++++++++++++++-------------------------
src/rpc/virkeepalive.h | 4 +--
src/rpc/virnetclient.c | 4 +--
src/rpc/virnetserverclient.c | 4 +--
5 files changed, 35 insertions(+), 54 deletions(-)
diff --git a/src/libvirt_probes.d b/src/libvirt_probes.d
index 3b138a9..807239f 100644
--- a/src/libvirt_probes.d
+++ b/src/libvirt_probes.d
@@ -77,9 +77,7 @@ provider libvirt {
# file: src/rpc/virkeepalive.c
# prefix: rpc
- probe rpc_keepalive_new(void *ka, void *client, int refs);
- probe rpc_keepalive_ref(void *ka, void *client, int refs);
- probe rpc_keepalive_free(void *ka, void *client, int refs);
+ probe rpc_keepalive_new(void *ka, void *client);
probe rpc_keepalive_start(void *ka, void *client, int interval, int count);
probe rpc_keepalive_stop(void *ka, void *client);
probe rpc_keepalive_send(void *ka, void *client, int prog, int vers, int proc);
diff --git a/src/rpc/virkeepalive.c b/src/rpc/virkeepalive.c
index 70cf31e..f6d7242 100644
--- a/src/rpc/virkeepalive.c
+++ b/src/rpc/virkeepalive.c
@@ -38,7 +38,8 @@
__FUNCTION__, __LINE__, __VA_ARGS__)
struct _virKeepAlive {
- int refs;
+ virObject object;
+
virMutex lock;
int interval;
@@ -55,6 +56,21 @@ struct _virKeepAlive {
};
+static virClassPtr virKeepAliveClass;
+static void virKeepAliveDispose(void *obj);
+
+static int virKeepAliveOnceInit(void)
+{
+ if (!(virKeepAliveClass = virClassNew("virKeepAlive",
+ sizeof(virKeepAlive),
+ virKeepAliveDispose)))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virKeepAlive)
+
static void
virKeepAliveLock(virKeepAlivePtr ka)
{
@@ -168,7 +184,7 @@ virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
if (!dead && !msg)
goto cleanup;
- ka->refs++;
+ virObjectRef(ka);
virKeepAliveUnlock(ka);
if (dead) {
@@ -179,20 +195,13 @@ virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
}
virKeepAliveLock(ka);
- ka->refs--;
+ virObjectUnref(ka);
cleanup:
virKeepAliveUnlock(ka);
}
-static void
-virKeepAliveTimerFree(void *opaque)
-{
- virKeepAliveFree(opaque);
-}
-
-
virKeepAlivePtr
virKeepAliveNew(int interval,
unsigned int count,
@@ -205,17 +214,17 @@ virKeepAliveNew(int interval,
VIR_DEBUG("client=%p, interval=%d, count=%u", client, interval, count);
- if (VIR_ALLOC(ka) < 0) {
- virReportOOMError();
+ if (virKeepAliveInitialize() < 0)
+ return NULL;
+
+ if (!(ka = virObjectNew(virKeepAliveClass)))
return NULL;
- }
if (virMutexInit(&ka->lock) < 0) {
VIR_FREE(ka);
return NULL;
}
- ka->refs = 1;
ka->interval = interval;
ka->count = count;
ka->countToDeath = count;
@@ -226,44 +235,20 @@ virKeepAliveNew(int interval,
ka->freeCB = freeCB;
PROBE(RPC_KEEPALIVE_NEW,
- "ka=%p client=%p refs=%d",
- ka, ka->client, ka->refs);
+ "ka=%p client=%p",
+ ka, ka->client);
return ka;
}
void
-virKeepAliveRef(virKeepAlivePtr ka)
+virKeepAliveDispose(void *obj)
{
- virKeepAliveLock(ka);
- ka->refs++;
- PROBE(RPC_KEEPALIVE_REF,
- "ka=%p client=%p refs=%d",
- ka, ka->client, ka->refs);
- virKeepAliveUnlock(ka);
-}
-
-
-void
-virKeepAliveFree(virKeepAlivePtr ka)
-{
- if (!ka)
- return;
-
- virKeepAliveLock(ka);
- PROBE(RPC_KEEPALIVE_FREE,
- "ka=%p client=%p refs=%d",
- ka, ka->client, ka->refs);
-
- if (--ka->refs > 0) {
- virKeepAliveUnlock(ka);
- return;
- }
+ virKeepAlivePtr ka = obj;
virMutexDestroy(&ka->lock);
ka->freeCB(ka->client);
- VIR_FREE(ka);
}
@@ -314,12 +299,12 @@ virKeepAliveStart(virKeepAlivePtr ka,
timeout = ka->interval - delay;
ka->intervalStart = now - (ka->interval - timeout);
ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer,
- ka, virKeepAliveTimerFree);
+ ka, (virFreeCallback)virObjectUnref);
if (ka->timer < 0)
goto cleanup;
/* the timer now has another reference to this object */
- ka->refs++;
+ virObjectRef(ka);
ret = 0;
cleanup:
diff --git a/src/rpc/virkeepalive.h b/src/rpc/virkeepalive.h
index 1e25214..c397a7e 100644
--- a/src/rpc/virkeepalive.h
+++ b/src/rpc/virkeepalive.h
@@ -24,6 +24,7 @@
# define __VIR_KEEPALIVE_H__
# include "virnetmessage.h"
+# include "virobject.h"
typedef int (*virKeepAliveSendFunc)(void *client, virNetMessagePtr msg);
typedef void (*virKeepAliveDeadFunc)(void *client);
@@ -42,9 +43,6 @@ virKeepAlivePtr virKeepAliveNew(int interval,
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6);
-void virKeepAliveRef(virKeepAlivePtr ka);
-void virKeepAliveFree(virKeepAlivePtr ka);
-
int virKeepAliveStart(virKeepAlivePtr ka,
int interval,
unsigned int count);
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 8dd09ef..45f3309 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -344,7 +344,7 @@ error:
VIR_FORCE_CLOSE(wakeupFD[1]);
if (ka) {
virKeepAliveStop(ka);
- virKeepAliveFree(ka);
+ virObjectUnref(ka);
}
virNetClientFree(client);
return NULL;
@@ -514,7 +514,7 @@ virNetClientCloseLocked(virNetClientPtr client)
virNetClientUnlock(client);
virKeepAliveStop(ka);
- virKeepAliveFree(ka);
+ virObjectUnref(ka);
virNetClientLock(client);
client->refs--;
diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c
index 97941c9..b2ab12f 100644
--- a/src/rpc/virnetserverclient.c
+++ b/src/rpc/virnetserverclient.c
@@ -631,7 +631,7 @@ void virNetServerClientClose(virNetServerClientPtr client)
client->keepalive = NULL;
client->refs++;
virNetServerClientUnlock(client);
- virKeepAliveFree(ka);
+ virObjectUnref(ka);
virNetServerClientLock(client);
client->refs--;
}
@@ -1201,7 +1201,7 @@ cleanup:
virNetServerClientUnlock(client);
if (ka)
virKeepAliveStop(ka);
- virKeepAliveFree(ka);
+ virObjectUnref(ka);
return ret;
}
--
1.7.10.2