[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 13/13] Turn virNetClient* into virObject instances



From: "Daniel P. Berrange" <berrange redhat com>

Make all the virNetClient* objects use virObject APIs for
reference counting

Signed-off-by: Daniel P. Berrange <berrange redhat com>
---
 src/libvirt_probes.d          |    4 +-
 src/remote/remote_driver.c    |   20 ++++-----
 src/rpc/gendispatch.pl        |    4 +-
 src/rpc/virnetclient.c        |   96 +++++++++++++++++------------------------
 src/rpc/virnetclient.h        |    4 +-
 src/rpc/virnetclientprogram.c |   43 +++++++++---------
 src/rpc/virnetclientprogram.h |    5 +--
 src/rpc/virnetclientstream.c  |   65 +++++++++++++---------------
 src/rpc/virnetclientstream.h  |    5 +--
 9 files changed, 108 insertions(+), 138 deletions(-)

diff --git a/src/libvirt_probes.d b/src/libvirt_probes.d
index 27f4e9a..9343fa4 100644
--- a/src/libvirt_probes.d
+++ b/src/libvirt_probes.d
@@ -40,9 +40,7 @@ provider libvirt {
 
 	# file: src/rpc/virnetclient.c
 	# prefix: rpc
-	probe rpc_client_new(void *client, int refs, void *sock);
-	probe rpc_client_ref(void *client, int refs);
-	probe rpc_client_free(void *client, int refs);
+	probe rpc_client_new(void *client, void *sock);
 
 	probe rpc_client_msg_tx_queue(void *client, int len, int prog, int vers, int proc, int type, int status, int serial);
 	probe rpc_client_msg_rx(void *client, int len, int prog, int vers, int proc, int type, int status, int serial);
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index f2600a8..3420ff8 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -757,10 +757,10 @@ doRemoteOpen (virConnectPtr conn,
     virReportOOMError();
 
  failed:
-    virNetClientProgramFree(priv->remoteProgram);
-    virNetClientProgramFree(priv->qemuProgram);
+    virObjectUnref(priv->remoteProgram);
+    virObjectUnref(priv->qemuProgram);
     virNetClientClose(priv->client);
-    virNetClientFree(priv->client);
+    virObjectUnref(priv->client);
     priv->client = NULL;
 
     VIR_FREE(priv->hostname);
@@ -911,10 +911,10 @@ doRemoteClose (virConnectPtr conn, struct private_data *priv)
     virObjectUnref(priv->tls);
     priv->tls = NULL;
     virNetClientClose(priv->client);
-    virNetClientFree(priv->client);
+    virObjectUnref(priv->client);
     priv->client = NULL;
-    virNetClientProgramFree(priv->remoteProgram);
-    virNetClientProgramFree(priv->qemuProgram);
+    virObjectUnref(priv->remoteProgram);
+    virObjectUnref(priv->qemuProgram);
     priv->remoteProgram = priv->qemuProgram = NULL;
 
     /* Free hostname copy */
@@ -4115,7 +4115,7 @@ remoteStreamFinish(virStreamPtr st)
 
 cleanup:
     virNetClientRemoveStream(priv->client, privst);
-    virNetClientStreamFree(privst);
+    virObjectUnref(privst);
     st->privateData = NULL;
     st->driver = NULL;
 
@@ -4144,7 +4144,7 @@ remoteStreamAbort(virStreamPtr st)
 
 cleanup:
     virNetClientRemoveStream(priv->client, privst);
-    virNetClientStreamFree(privst);
+    virObjectUnref(privst);
     st->privateData = NULL;
     st->driver = NULL;
 
@@ -4445,7 +4445,7 @@ remoteDomainMigratePrepareTunnel3(virConnectPtr dconn,
         goto done;
 
     if (virNetClientAddStream(priv->client, netst) < 0) {
-        virNetClientStreamFree(netst);
+        virObjectUnref(netst);
         goto done;
     }
 
@@ -4463,7 +4463,7 @@ remoteDomainMigratePrepareTunnel3(virConnectPtr dconn,
              (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_ret, (char *) &ret) == -1) {
         virNetClientRemoveStream(priv->client, netst);
-        virNetClientStreamFree(netst);
+        virObjectUnref(netst);
         goto done;
     }
 
diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl
index f161ee0..ea31ae9 100755
--- a/src/rpc/gendispatch.pl
+++ b/src/rpc/gendispatch.pl
@@ -1404,7 +1404,7 @@ elsif ($opt_k) {
             print "        goto done;\n";
             print "\n";
             print "    if (virNetClientAddStream(priv->client, netst) < 0) {\n";
-            print "        virNetClientStreamFree(netst);\n";
+            print "        virObjectUnref(netst);\n";
             print "        goto done;\n";
             print "    }";
             print "\n";
@@ -1480,7 +1480,7 @@ elsif ($opt_k) {
 
         if ($call->{streamflag} ne "none") {
             print "        virNetClientRemoveStream(priv->client, netst);\n";
-            print "        virNetClientStreamFree(netst);\n";
+            print "        virObjectUnref(netst);\n";
             print "        st->driver = NULL;\n";
             print "        st->privateData = NULL;\n";
         }
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 4da5082..9be0592 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -66,7 +66,7 @@ struct _virNetClientCall {
 
 
 struct _virNetClient {
-    int refs;
+    virObject object;
 
     virMutex lock;
 
@@ -107,6 +107,21 @@ struct _virNetClient {
 };
 
 
+static virClassPtr virNetClientClass;
+static void virNetClientDispose(void *obj);
+
+static int virNetClientOnceInit(void)
+{
+    if (!(virNetClientClass = virClassNew("virNetClient",
+                                          sizeof(virNetClient),
+                                          virNetClientDispose)))
+        return -1;
+
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNetClient)
+
 static void virNetClientIOEventLoopPassTheBuck(virNetClientPtr client,
                                                virNetClientCallPtr thiscall);
 static int virNetClientQueueNonBlocking(virNetClientPtr client,
@@ -220,13 +235,6 @@ static bool virNetClientCallMatchPredicate(virNetClientCallPtr head,
 }
 
 
-static void virNetClientEventFree(void *opaque)
-{
-    virNetClientPtr client = opaque;
-
-    virNetClientFree(client);
-}
-
 bool
 virNetClientKeepAliveIsSupported(virNetClientPtr client)
 {
@@ -286,19 +294,22 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
     int wakeupFD[2] = { -1, -1 };
     virKeepAlivePtr ka = NULL;
 
+    if (virNetClientInitialize() < 0)
+        return NULL;
+
     if (pipe2(wakeupFD, O_CLOEXEC) < 0) {
         virReportSystemError(errno, "%s",
                              _("unable to make pipe"));
         goto error;
     }
 
-    if (VIR_ALLOC(client) < 0)
-        goto no_memory;
-
-    client->refs = 1;
+    if (!(client = virObjectNew(virNetClientClass)))
+        goto error;
 
-    if (virMutexInit(&client->lock) < 0)
+    if (virMutexInit(&client->lock) < 0) {
+        VIR_FREE(client);
         goto error;
+    }
 
     client->sock = sock;
     client->wakeupReadFD = wakeupFD[0];
@@ -310,13 +321,13 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
         goto no_memory;
 
     /* Set up a callback to listen on the socket data */
-    client->refs++;
+    virObjectRef(client);
     if (virNetSocketAddIOCallback(client->sock,
                                   VIR_EVENT_HANDLE_READABLE,
                                   virNetClientIncomingEvent,
                                   client,
-                                  virNetClientEventFree) < 0) {
-        client->refs--;
+                                  (virFreeCallback)virObjectUnref) < 0) {
+        virObjectUnref(client);
         VIR_DEBUG("Failed to add event watch, disabling events and support for"
                   " keepalive messages");
     } else {
@@ -325,16 +336,16 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
         if (!(ka = virKeepAliveNew(-1, 0, client,
                                    virNetClientKeepAliveSendCB,
                                    virNetClientKeepAliveDeadCB,
-                                   virNetClientEventFree)))
+                                   (virFreeCallback)virObjectUnref)))
             goto error;
         /* keepalive object has a reference to client */
-        client->refs++;
+        virObjectRef(client);
     }
 
     client->keepalive = ka;
     PROBE(RPC_CLIENT_NEW,
-          "client=%p refs=%d sock=%p",
-          client, client->refs, client->sock);
+          "client=%p sock=%p",
+          client, client->sock);
     return client;
 
 no_memory:
@@ -346,7 +357,7 @@ error:
         virKeepAliveStop(ka);
         virObjectUnref(ka);
     }
-    virNetClientFree(client);
+    virObjectUnref(client);
     return NULL;
 }
 
@@ -405,17 +416,6 @@ virNetClientPtr virNetClientNewExternal(const char **cmdargv)
 }
 
 
-void virNetClientRef(virNetClientPtr client)
-{
-    virNetClientLock(client);
-    client->refs++;
-    PROBE(RPC_CLIENT_REF,
-          "client=%p refs=%d",
-          client, client->refs);
-    virNetClientUnlock(client);
-}
-
-
 int virNetClientGetFD(virNetClientPtr client)
 {
     int fd;
@@ -446,25 +446,13 @@ bool virNetClientHasPassFD(virNetClientPtr client)
 }
 
 
-void virNetClientFree(virNetClientPtr client)
+void virNetClientDispose(void *obj)
 {
+    virNetClientPtr client = obj;
     int i;
 
-    if (!client)
-        return;
-
-    virNetClientLock(client);
-    PROBE(RPC_CLIENT_FREE,
-          "client=%p refs=%d",
-          client, client->refs);
-    client->refs--;
-    if (client->refs > 0) {
-        virNetClientUnlock(client);
-        return;
-    }
-
     for (i = 0 ; i < client->nprograms ; i++)
-        virNetClientProgramFree(client->programs[i]);
+        virObjectUnref(client->programs[i]);
     VIR_FREE(client->programs);
 
     VIR_FORCE_CLOSE(client->wakeupSendFD);
@@ -481,8 +469,6 @@ void virNetClientFree(virNetClientPtr client)
 #endif
     virNetClientUnlock(client);
     virMutexDestroy(&client->lock);
-
-    VIR_FREE(client);
 }
 
 
@@ -510,14 +496,14 @@ virNetClientCloseLocked(virNetClientPtr client)
     client->wantClose = false;
 
     if (ka) {
-        client->refs++;
+        virObjectRef(client);
         virNetClientUnlock(client);
 
         virKeepAliveStop(ka);
         virObjectUnref(ka);
 
         virNetClientLock(client);
-        client->refs--;
+        virObjectUnref(client);
     }
 }
 
@@ -703,8 +689,7 @@ int virNetClientAddProgram(virNetClientPtr client,
     if (VIR_EXPAND_N(client->programs, client->nprograms, 1) < 0)
         goto no_memory;
 
-    client->programs[client->nprograms-1] = prog;
-    virNetClientProgramRef(prog);
+    client->programs[client->nprograms-1] = virObjectRef(prog);
 
     virNetClientUnlock(client);
     return 0;
@@ -724,8 +709,7 @@ int virNetClientAddStream(virNetClientPtr client,
     if (VIR_EXPAND_N(client->streams, client->nstreams, 1) < 0)
         goto no_memory;
 
-    client->streams[client->nstreams-1] = st;
-    virNetClientStreamRef(st);
+    client->streams[client->nstreams-1] = virObjectRef(st);
 
     virNetClientUnlock(client);
     return 0;
@@ -759,7 +743,7 @@ void virNetClientRemoveStream(virNetClientPtr client,
         VIR_FREE(client->streams);
         client->nstreams = 0;
     }
-    virNetClientStreamFree(st);
+    virObjectUnref(st);
 
 cleanup:
     virNetClientUnlock(client);
diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h
index 13b4f96..6e41e75 100644
--- a/src/rpc/virnetclient.h
+++ b/src/rpc/virnetclient.h
@@ -30,6 +30,7 @@
 # endif
 # include "virnetclientprogram.h"
 # include "virnetclientstream.h"
+# include "virobject.h"
 
 
 virNetClientPtr virNetClientNewUNIX(const char *path,
@@ -51,8 +52,6 @@ virNetClientPtr virNetClientNewSSH(const char *nodename,
 
 virNetClientPtr virNetClientNewExternal(const char **cmdargv);
 
-void virNetClientRef(virNetClientPtr client);
-
 int virNetClientGetFD(virNetClientPtr client);
 int virNetClientDupFD(virNetClientPtr client, bool cloexec);
 
@@ -96,7 +95,6 @@ const char *virNetClientRemoteAddrString(virNetClientPtr client);
 
 int virNetClientGetTLSKeySize(virNetClientPtr client);
 
-void virNetClientFree(virNetClientPtr client);
 void virNetClientClose(virNetClientPtr client);
 
 bool virNetClientKeepAliveIsSupported(virNetClientPtr client);
diff --git a/src/rpc/virnetclientprogram.c b/src/rpc/virnetclientprogram.c
index e1e8846..f6d2173 100644
--- a/src/rpc/virnetclientprogram.c
+++ b/src/rpc/virnetclientprogram.c
@@ -33,6 +33,7 @@
 #include "logging.h"
 #include "util.h"
 #include "virfile.h"
+#include "threads.h"
 
 #define VIR_FROM_THIS VIR_FROM_RPC
 #define virNetError(code, ...)                                    \
@@ -40,7 +41,7 @@
                          __FUNCTION__, __LINE__, __VA_ARGS__)
 
 struct _virNetClientProgram {
-    int refs;
+    virObject object;
 
     unsigned program;
     unsigned version;
@@ -49,6 +50,22 @@ struct _virNetClientProgram {
     void *eventOpaque;
 };
 
+static virClassPtr virNetClientProgramClass;
+static void virNetClientProgramDispose(void *obj);
+
+static int virNetClientProgramOnceInit(void)
+{
+    if (!(virNetClientProgramClass = virClassNew("virNetClientProgram",
+                                                 sizeof(virNetClientProgram),
+                                                 virNetClientProgramDispose)))
+        return -1;
+
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNetClientProgram)
+
+
 virNetClientProgramPtr virNetClientProgramNew(unsigned program,
                                               unsigned version,
                                               virNetClientProgramEventPtr events,
@@ -57,12 +74,12 @@ virNetClientProgramPtr virNetClientProgramNew(unsigned program,
 {
     virNetClientProgramPtr prog;
 
-    if (VIR_ALLOC(prog) < 0) {
-        virReportOOMError();
+    if (virNetClientProgramInitialize() < 0)
+        return NULL;
+
+    if (!(prog = virObjectNew(virNetClientProgramClass)))
         return NULL;
-    }
 
-    prog->refs = 1;
     prog->program = program;
     prog->version = version;
     prog->events = events;
@@ -73,22 +90,8 @@ virNetClientProgramPtr virNetClientProgramNew(unsigned program,
 }
 
 
-void virNetClientProgramRef(virNetClientProgramPtr prog)
+void virNetClientProgramDispose(void *obj ATTRIBUTE_UNUSED)
 {
-    prog->refs++;
-}
-
-
-void virNetClientProgramFree(virNetClientProgramPtr prog)
-{
-    if (!prog)
-        return;
-
-    prog->refs--;
-    if (prog->refs > 0)
-        return;
-
-    VIR_FREE(prog);
 }
 
 
diff --git a/src/rpc/virnetclientprogram.h b/src/rpc/virnetclientprogram.h
index 14a4c96..3fde3bf 100644
--- a/src/rpc/virnetclientprogram.h
+++ b/src/rpc/virnetclientprogram.h
@@ -27,6 +27,7 @@
 # include <rpc/xdr.h>
 
 # include "virnetmessage.h"
+# include "virobject.h"
 
 typedef struct _virNetClient virNetClient;
 typedef virNetClient *virNetClientPtr;
@@ -62,10 +63,6 @@ virNetClientProgramPtr virNetClientProgramNew(unsigned program,
 unsigned virNetClientProgramGetProgram(virNetClientProgramPtr prog);
 unsigned virNetClientProgramGetVersion(virNetClientProgramPtr prog);
 
-void virNetClientProgramRef(virNetClientProgramPtr prog);
-
-void virNetClientProgramFree(virNetClientProgramPtr prog);
-
 int virNetClientProgramMatches(virNetClientProgramPtr prog,
                                virNetMessagePtr msg);
 
diff --git a/src/rpc/virnetclientstream.c b/src/rpc/virnetclientstream.c
index de61a62..acdb67c 100644
--- a/src/rpc/virnetclientstream.c
+++ b/src/rpc/virnetclientstream.c
@@ -36,12 +36,13 @@
                          __FUNCTION__, __LINE__, __VA_ARGS__)
 
 struct _virNetClientStream {
+    virObject object;
+
     virMutex lock;
 
     virNetClientProgramPtr prog;
     int proc;
     unsigned serial;
-    int refs;
 
     virError err;
 
@@ -66,6 +67,22 @@ struct _virNetClientStream {
 };
 
 
+static virClassPtr virNetClientStreamClass;
+static void virNetClientStreamDispose(void *obj);
+
+static int virNetClientStreamOnceInit(void)
+{
+    if (!(virNetClientStreamClass = virClassNew("virNetClientStream",
+                                                sizeof(virNetClientStream),
+                                                virNetClientStreamDispose)))
+        return -1;
+
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNetClientStream)
+
+
 static void
 virNetClientStreamEventTimerUpdate(virNetClientStreamPtr st)
 {
@@ -122,26 +139,18 @@ virNetClientStreamEventTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
 }
 
 
-static void
-virNetClientStreamEventTimerFree(void *opaque)
-{
-    virNetClientStreamPtr st = opaque;
-    virNetClientStreamFree(st);
-}
-
-
 virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog,
                                             int proc,
                                             unsigned serial)
 {
     virNetClientStreamPtr st;
 
-    if (VIR_ALLOC(st) < 0) {
-        virReportOOMError();
+    if (virNetClientStreamInitialize() < 0)
+        return NULL;
+
+    if (!(st = virObjectNew(virNetClientStreamClass)))
         return NULL;
-    }
 
-    st->refs = 1;
     st->prog = prog;
     st->proc = proc;
     st->serial = serial;
@@ -153,35 +162,19 @@ virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog,
         return NULL;
     }
 
-    virNetClientProgramRef(prog);
+    virObjectRef(prog);
 
     return st;
 }
 
-
-void virNetClientStreamRef(virNetClientStreamPtr st)
+void virNetClientStreamDispose(void *obj)
 {
-    virMutexLock(&st->lock);
-    st->refs++;
-    virMutexUnlock(&st->lock);
-}
-
-void virNetClientStreamFree(virNetClientStreamPtr st)
-{
-    virMutexLock(&st->lock);
-    st->refs--;
-    if (st->refs > 0) {
-        virMutexUnlock(&st->lock);
-        return;
-    }
-
-    virMutexUnlock(&st->lock);
+    virNetClientStreamPtr st = obj;
 
     virResetError(&st->err);
     VIR_FREE(st->incoming);
     virMutexDestroy(&st->lock);
-    virNetClientProgramFree(st->prog);
-    VIR_FREE(st);
+    virObjectUnref(st->prog);
 }
 
 bool virNetClientStreamMatches(virNetClientStreamPtr st,
@@ -457,13 +450,13 @@ int virNetClientStreamEventAddCallback(virNetClientStreamPtr st,
         goto cleanup;
     }
 
-    st->refs++;
+    virObjectRef(st);
     if ((st->cbTimer =
          virEventAddTimeout(-1,
                             virNetClientStreamEventTimer,
                             st,
-                            virNetClientStreamEventTimerFree)) < 0) {
-        st->refs--;
+                            (virFreeCallback)virObjectUnref)) < 0) {
+        virObjectUnref(st);
         goto cleanup;
     }
 
diff --git a/src/rpc/virnetclientstream.h b/src/rpc/virnetclientstream.h
index fd7a2ee..9e22adb 100644
--- a/src/rpc/virnetclientstream.h
+++ b/src/rpc/virnetclientstream.h
@@ -24,6 +24,7 @@
 # define __VIR_NET_CLIENT_STREAM_H__
 
 # include "virnetclientprogram.h"
+# include "virobject.h"
 
 typedef struct _virNetClientStream virNetClientStream;
 typedef virNetClientStream *virNetClientStreamPtr;
@@ -35,10 +36,6 @@ virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog,
                                             int proc,
                                             unsigned serial);
 
-void virNetClientStreamRef(virNetClientStreamPtr st);
-
-void virNetClientStreamFree(virNetClientStreamPtr st);
-
 bool virNetClientStreamRaiseError(virNetClientStreamPtr st);
 
 int virNetClientStreamSetError(virNetClientStreamPtr st,
-- 
1.7.10.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]