[PATCH v2 28/40] datatypes: convert virStream to GObject

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

 



Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx>
---
 src/conf/virchrdev.c                |  4 +--
 src/datatypes.c                     | 56 ++++++++++++++++++++++-------
 src/datatypes.h                     | 14 ++++----
 src/libvirt-stream.c                |  7 ++--
 src/libvirt_private.syms            |  2 +-
 src/libxl/libxl_migration.c         |  3 +-
 src/qemu/qemu_migration.c           |  6 ++--
 src/remote/remote_daemon_dispatch.c |  4 +--
 src/remote/remote_daemon_stream.c   |  2 +-
 src/remote/remote_driver.c          |  2 +-
 src/rpc/gendispatch.pl              |  3 +-
 src/util/virfdstream.c              |  4 +--
 12 files changed, 68 insertions(+), 39 deletions(-)

diff --git a/src/conf/virchrdev.c b/src/conf/virchrdev.c
index 800e82869e..8a60ac4dad 100644
--- a/src/conf/virchrdev.c
+++ b/src/conf/virchrdev.c
@@ -205,7 +205,7 @@ static void virChrdevHashEntryFree(void *data)
         return;
 
     /* free stream reference */
-    virObjectUnref(ent->st);
+    g_clear_object(&ent->st);
 
     /* delete lock file */
     virChrdevLockFileRemove(ent->dev);
@@ -435,7 +435,7 @@ int virChrdevOpen(virChrdevsPtr devs,
     if (added)
         virHashRemoveEntry(devs->hash, path);
     else
-        virObjectUnref(st);
+        g_clear_object(&st);
 
     if (cbdata)
         VIR_FREE(cbdata->path);
diff --git a/src/datatypes.c b/src/datatypes.c
index 0bb4cfbd29..5d8752334d 100644
--- a/src/datatypes.c
+++ b/src/datatypes.c
@@ -36,12 +36,10 @@ VIR_LOG_INIT("datatypes");
 virClassPtr virConnectClass;
 virClassPtr virConnectCloseCallbackDataClass;
 virClassPtr virDomainClass;
-virClassPtr virStreamClass;
 
 static void virConnectDispose(void *obj);
 static void virConnectCloseCallbackDataDispose(void *obj);
 static void virDomainDispose(void *obj);
-static void virStreamDispose(void *obj);
 
 G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT);
 static void virDomainCheckpointDispose(GObject *obj);
@@ -241,6 +239,24 @@ vir_storage_vol_class_init(virStorageVolClass *klass)
     obj->finalize = virStorageVolFinalize;
 }
 
+G_DEFINE_TYPE(virStream, vir_stream, G_TYPE_OBJECT);
+static void virStreamDispose(GObject *obj);
+static void virStreamFinalize(GObject *obj);
+
+static void
+vir_stream_init(virStream *strm G_GNUC_UNUSED)
+{
+}
+
+static void
+vir_stream_class_init(virStreamClass *klass)
+{
+    GObjectClass *obj = G_OBJECT_CLASS(klass);
+
+    obj->dispose = virStreamDispose;
+    obj->finalize = virStreamFinalize;
+}
+
 virClassPtr virAdmConnectClass;
 virClassPtr virAdmConnectCloseCallbackDataClass;
 
@@ -295,7 +311,6 @@ virDataTypesOnceInit(void)
     DECLARE_CLASS_LOCKABLE(virConnect);
     DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData);
     DECLARE_CLASS(virDomain);
-    DECLARE_CLASS(virStream);
 
     DECLARE_CLASS_LOCKABLE(virAdmConnect);
     DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData);
@@ -1049,7 +1064,7 @@ virSecretFinalize(GObject *obj)
  * @conn: the hypervisor connection
  *
  * Allocates a new stream object. When the object is no longer needed,
- * virObjectUnref() must be called in order to not leak data.
+ * g_object_unref() must be called in order to not leak data.
  *
  * Returns a pointer to the stream object, or NULL on error.
  */
@@ -1061,8 +1076,7 @@ virGetStream(virConnectPtr conn)
     if (virDataTypesInitialize() < 0)
         return NULL;
 
-    if (!(ret = virObjectNew(virStreamClass)))
-        return NULL;
+    ret = VIR_STREAM(g_object_new(VIR_TYPE_STREAM, NULL));
 
     ret->conn = virObjectRef(conn);
 
@@ -1073,21 +1087,37 @@ virGetStream(virConnectPtr conn)
  * virStreamDispose:
  * @obj: the stream to release
  *
- * Unconditionally release all memory associated with a stream.
- * The stream object must not be used once this method returns.
+ * Unreferences the associated connection object, which may also be
+ * released if its ref count hits zero.
+ */
+static void
+virStreamDispose(GObject *obj)
+{
+    virStreamPtr st = VIR_STREAM(obj);
+
+    virObjectUnref(st->conn);
+    st->conn = NULL;
+
+    G_OBJECT_CLASS(vir_stream_parent_class)->dispose(obj);
+}
+
+/**
+ * virStreamFinalize:
+ * @obj: the stream to release
  *
- * It will also unreference the associated connection object,
- * which may also be released if its ref count hits zero.
+ * Unconditionally releases all memory associated with a stream.
+ * The stream object must not be used once this method returns.
  */
 static void
-virStreamDispose(void *obj)
+virStreamFinalize(GObject *obj)
 {
-    virStreamPtr st = obj;
+    virStreamPtr st = VIR_STREAM(obj);
     VIR_DEBUG("release dev %p", st);
 
     if (st->ff)
         st->ff(st->privateData);
-    virObjectUnref(st->conn);
+
+    G_OBJECT_CLASS(vir_stream_parent_class)->finalize(obj);
 }
 
 
diff --git a/src/datatypes.h b/src/datatypes.h
index 2f97268691..e96654cac1 100644
--- a/src/datatypes.h
+++ b/src/datatypes.h
@@ -32,7 +32,6 @@
 
 extern virClassPtr virConnectClass;
 extern virClassPtr virDomainClass;
-extern virClassPtr virStreamClass;
 
 #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type()
 G_DECLARE_FINAL_TYPE(virDomainCheckpoint,
@@ -79,6 +78,9 @@ G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObjec
 #define VIR_TYPE_STORAGE_VOL vir_storage_vol_get_type()
 G_DECLARE_FINAL_TYPE(virStorageVol, vir_storage_vol, VIR, STORAGE_VOL, GObject);
 
+#define VIR_TYPE_STREAM vir_stream_get_type()
+G_DECLARE_FINAL_TYPE(virStream, vir_stream, VIR, STREAM, GObject);
+
 extern virClassPtr virAdmConnectClass;
 
 #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type()
@@ -307,8 +309,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject);
 
 #define virCheckStreamReturn(obj, retval) \
     do { \
-        virStreamPtr _st = (obj); \
-        if (!virObjectIsClass(_st, virStreamClass) || \
+        virStreamPtr _st = VIR_STREAM(obj); \
+        if (_st == NULL || \
             !virObjectIsClass(_st->conn, virConnectClass)) { \
             virReportErrorHelper(VIR_FROM_STREAMS, \
                                  VIR_ERR_INVALID_STREAM, \
@@ -320,8 +322,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject);
     } while (0)
 #define virCheckStreamGoto(obj, label) \
     do { \
-        virStreamPtr _st = (obj); \
-        if (!virObjectIsClass(_st, virStreamClass) || \
+        virStreamPtr _st = VIR_STREAM(obj); \
+        if (_st == NULL || \
             !virObjectIsClass(_st->conn, virConnectClass)) { \
             virReportErrorHelper(VIR_FROM_STREAMS, \
                                  VIR_ERR_INVALID_STREAM, \
@@ -763,7 +765,7 @@ typedef int (*virStreamFinishFunc)(virStreamPtr, void *opaque);
  * Internal structure associated with an input stream
  */
 struct _virStream {
-    virObject parent;
+    GObject parent;
     virConnectPtr conn;
     unsigned int flags;
 
diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c
index 41b9cc1445..d1e2fde3b4 100644
--- a/src/libvirt-stream.c
+++ b/src/libvirt-stream.c
@@ -85,14 +85,13 @@ virStreamNew(virConnectPtr conn,
 int
 virStreamRef(virStreamPtr stream)
 {
-    VIR_DEBUG("stream=%p refs=%d", stream,
-              stream ? stream->parent.u.s.refs : 0);
+    VIR_DEBUG("stream=%p", stream);
 
     virResetLastError();
 
     virCheckStreamReturn(stream, -1);
 
-    virObjectRef(stream);
+    g_object_ref(stream);
     return 0;
 }
 
@@ -1265,6 +1264,6 @@ virStreamFree(virStreamPtr stream)
 
     /* XXX Enforce shutdown before free'ing resources ? */
 
-    virObjectUnref(stream);
+    g_object_unref(stream);
     return 0;
 }
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 915979bb42..1514db8cb9 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1357,6 +1357,7 @@ vir_nw_filter_get_type;
 vir_secret_get_type;
 vir_storage_pool_get_type;
 vir_storage_vol_get_type;
+vir_stream_get_type;
 virConnectClass;
 virConnectCloseCallbackDataCall;
 virConnectCloseCallbackDataClass;
@@ -1379,7 +1380,6 @@ virGetStoragePool;
 virGetStorageVol;
 virGetStream;
 virNewConnectCloseCallbackData;
-virStreamClass;
 
 
 # driver.h
diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c
index defdda5ed6..50804ca8b8 100644
--- a/src/libxl/libxl_migration.c
+++ b/src/libxl/libxl_migration.c
@@ -999,7 +999,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
     virErrorPtr orig_err = NULL;
     int ret = -1;
     /* For tunnel migration */
-    virStreamPtr st = NULL;
+    g_autoptr(virStream) st = NULL;
     struct libxlTunnelControl *tc = NULL;
 
     if (dname &&
@@ -1110,7 +1110,6 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
  cleanup:
     if (flags & VIR_MIGRATE_TUNNELLED) {
         libxlMigrationSrcStopTunnel(tc);
-        virObjectUnref(st);
     }
 
     if (ddomain) {
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 07e137e2c9..13ff06f5c7 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -4000,7 +4000,7 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver,
     int cookielen = 0, ret;
     virErrorPtr orig_err = NULL;
     bool cancelled;
-    virStreamPtr st = NULL;
+    g_autoptr(virStream) st = NULL;
     unsigned long destflags;
 
     VIR_DEBUG("driver=%p, sconn=%p, dconn=%p, vm=%p, dconnuri=%s, "
@@ -4109,7 +4109,6 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver,
         ret = -1;
     }
 
-    virObjectUnref(st);
 
     virErrorRestore(&orig_err);
     VIR_FREE(uri_out);
@@ -4153,7 +4152,7 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver,
     int ret = -1;
     virErrorPtr orig_err = NULL;
     bool cancelled = true;
-    virStreamPtr st = NULL;
+    g_autoptr(virStream) st = NULL;
     unsigned long destflags;
     virTypedParameterPtr params = NULL;
     int nparams = 0;
@@ -4446,7 +4445,6 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver,
         ret = -1;
     }
 
-    virObjectUnref(st);
 
     virErrorRestore(&orig_err);
     VIR_FREE(uri_out);
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index fb1f1bd469..c5eca3bc0c 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -5641,7 +5641,7 @@ remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server G_GNUC_UN
     char *cookieout = NULL;
     int cookieoutlen = 0;
     int rv = -1;
-    virStreamPtr st = NULL;
+    g_autoptr(virStream) st = NULL;
     daemonClientStreamPtr stream = NULL;
     virConnectPtr conn = remoteGetHypervisorConn(client);
 
@@ -5688,7 +5688,7 @@ remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server G_GNUC_UN
             virStreamAbort(st);
             daemonFreeClientStream(client, stream);
         } else {
-            virObjectUnref(st);
+            g_clear_object(&st);
         }
     }
     return rv;
diff --git a/src/remote/remote_daemon_stream.c b/src/remote/remote_daemon_stream.c
index 62bc9e0f5b..6e1dba3bc0 100644
--- a/src/remote/remote_daemon_stream.c
+++ b/src/remote/remote_daemon_stream.c
@@ -419,7 +419,7 @@ int daemonFreeClientStream(virNetServerClientPtr client,
         msg = tmp;
     }
 
-    virObjectUnref(stream->st);
+    g_clear_object(&stream->st);
     VIR_FREE(stream);
 
     return ret;
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 7801e5e990..58ae7528a8 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -5823,7 +5823,7 @@ static void remoteStreamCallbackFree(void *opaque)
     if (!cbdata->cb && cbdata->ff)
         (cbdata->ff)(cbdata->opaque);
 
-    virObjectUnref(cbdata->st);
+    g_clear_object(&cbdata->st);
     VIR_FREE(opaque);
 }
 
diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl
index 902ff091a5..29b3feb17c 100755
--- a/src/rpc/gendispatch.pl
+++ b/src/rpc/gendispatch.pl
@@ -207,6 +207,7 @@ my %gobject_impl = (
     virSecret => 1,
     virStoragePool => 1,
     virStorageVol => 1,
+    virStream => 1,
 );
 
 sub use_gobject {
@@ -1068,7 +1069,7 @@ elsif ($mode eq "server") {
             push(@free_list_on_error, "    virStreamAbort(st);");
             push(@free_list_on_error, "    daemonFreeClientStream(client, stream);");
             push(@free_list_on_error, "} else {");
-            push(@free_list_on_error, "    virObjectUnref(st);");
+            push(@free_list_on_error, "    g_clear_object(&st);");
             push(@free_list_on_error, "}");
         }
 
diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c
index 111e451f8c..e6271b860c 100644
--- a/src/util/virfdstream.c
+++ b/src/util/virfdstream.c
@@ -407,7 +407,7 @@ virFDStreamThreadDataFree(virFDStreamThreadDataPtr data)
     if (!data)
         return;
 
-    virObjectUnref(data->st);
+    g_clear_object(&data->st);
     VIR_FREE(data->fdinname);
     VIR_FREE(data->fdoutname);
     VIR_FREE(data);
@@ -1282,7 +1282,7 @@ virFDStreamOpenFileInternal(virStreamPtr st,
         if (VIR_ALLOC(threadData) < 0)
             goto error;
 
-        threadData->st = virObjectRef(st);
+        threadData->st = g_object_ref(st);
         threadData->length = length;
         threadData->sparse = sparse;
 
-- 
2.25.3






[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux