[PATCH v2 19/40] datatypes: convert virNWFilter to GObject

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

 



Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx>
---
 src/conf/virnwfilterobj.c |  7 +---
 src/datatypes.c           | 70 +++++++++++++++++++++++++++------------
 src/datatypes.h           | 10 +++---
 src/libvirt-nwfilter.c    |  7 ++--
 src/libvirt_private.syms  |  2 +-
 src/rpc/gendispatch.pl    |  1 +
 6 files changed, 60 insertions(+), 37 deletions(-)

diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c
index c9e224061d..c08dc7a5d6 100644
--- a/src/conf/virnwfilterobj.c
+++ b/src/conf/virnwfilterobj.c
@@ -472,12 +472,7 @@ virNWFilterObjListExport(virConnectPtr conn,
     ret = nfilters;
 
  cleanup:
-    if (tmp_filters) {
-        for (i = 0; i < nfilters; i ++)
-            virObjectUnref(tmp_filters[i]);
-    }
-    VIR_FREE(tmp_filters);
-
+    virGObjectListFreeCount(tmp_filters, nfilters);
     return ret;
 }
 
diff --git a/src/datatypes.c b/src/datatypes.c
index 334d9d8638..e9f6cafd72 100644
--- a/src/datatypes.c
+++ b/src/datatypes.c
@@ -40,7 +40,6 @@ virClassPtr virInterfaceClass;
 virClassPtr virNetworkClass;
 virClassPtr virNetworkPortClass;
 virClassPtr virNodeDeviceClass;
-virClassPtr virNWFilterClass;
 virClassPtr virNWFilterBindingClass;
 virClassPtr virSecretClass;
 virClassPtr virStreamClass;
@@ -54,7 +53,6 @@ static void virInterfaceDispose(void *obj);
 static void virNetworkDispose(void *obj);
 static void virNetworkPortDispose(void *obj);
 static void virNodeDeviceDispose(void *obj);
-static void virNWFilterDispose(void *obj);
 static void virNWFilterBindingDispose(void *obj);
 static void virSecretDispose(void *obj);
 static void virStreamDispose(void *obj);
@@ -97,6 +95,24 @@ vir_domain_snapshot_class_init(virDomainSnapshotClass *klass)
     obj->finalize = virDomainSnapshotFinalize;
 }
 
+G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT);
+static void virNWFilterDispose(GObject *obj);
+static void virNWFilterFinalize(GObject *obj);
+
+static void
+vir_nw_filter_init(virNWFilter *filter G_GNUC_UNUSED)
+{
+}
+
+static void
+vir_nw_filter_class_init(virNWFilterClass *klass)
+{
+    GObjectClass *obj = G_OBJECT_CLASS(klass);
+
+    obj->dispose = virNWFilterDispose;
+    obj->finalize = virNWFilterFinalize;
+}
+
 virClassPtr virAdmConnectClass;
 virClassPtr virAdmConnectCloseCallbackDataClass;
 
@@ -155,7 +171,6 @@ virDataTypesOnceInit(void)
     DECLARE_CLASS(virNetwork);
     DECLARE_CLASS(virNetworkPort);
     DECLARE_CLASS(virNodeDevice);
-    DECLARE_CLASS(virNWFilter);
     DECLARE_CLASS(virNWFilterBinding);
     DECLARE_CLASS(virSecret);
     DECLARE_CLASS(virStream);
@@ -889,7 +904,7 @@ virStreamDispose(void *obj)
  * @uuid: pointer to the uuid
  *
  * Allocates a new network filter 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 network filter object, or NULL on error.
  */
@@ -897,17 +912,16 @@ virNWFilterPtr
 virGetNWFilter(virConnectPtr conn, const char *name,
                const unsigned char *uuid)
 {
-    virNWFilterPtr ret = NULL;
+    g_autoptr(virNWFilter) ret = NULL;
 
     if (virDataTypesInitialize() < 0)
         return NULL;
 
-    virCheckConnectGoto(conn, error);
-    virCheckNonNullArgGoto(name, error);
-    virCheckNonNullArgGoto(uuid, error);
+    virCheckConnectReturn(conn, NULL);
+    virCheckNonNullArgReturn(name, NULL);
+    virCheckNonNullArgReturn(uuid, NULL);
 
-    if (!(ret = virObjectNew(virNWFilterClass)))
-        goto error;
+    ret = VIR_NW_FILTER(g_object_new(VIR_TYPE_NW_FILTER, NULL));
 
     ret->name = g_strdup(name);
 
@@ -915,11 +929,7 @@ virGetNWFilter(virConnectPtr conn, const char *name,
 
     ret->conn = virObjectRef(conn);
 
-    return ret;
-
- error:
-    virObjectUnref(ret);
-    return NULL;
+    return g_steal_pointer(&ret);
 }
 
 
@@ -927,23 +937,39 @@ virGetNWFilter(virConnectPtr conn, const char *name,
  * virNWFilterDispose:
  * @obj: the network filter to release
  *
- * Unconditionally release all memory associated with a nwfilter.
- * The nwfilter 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
+virNWFilterDispose(GObject *obj)
+{
+    virNWFilterPtr nwfilter = VIR_NW_FILTER(obj);
+
+    virObjectUnref(nwfilter->conn);
+    nwfilter->conn = NULL;
+
+    G_OBJECT_CLASS(vir_nw_filter_parent_class)->dispose(obj);
+}
+
+/**
+ * virNWFilterFinalize:
+ * @obj: the network filter 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 nwfilter.
+ * The nwfilter object must not be used once this method returns.
  */
 static void
-virNWFilterDispose(void *obj)
+virNWFilterFinalize(GObject *obj)
 {
-    virNWFilterPtr nwfilter = obj;
+    virNWFilterPtr nwfilter = VIR_NW_FILTER(obj);
     char uuidstr[VIR_UUID_STRING_BUFLEN];
 
     virUUIDFormat(nwfilter->uuid, uuidstr);
     VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
 
     VIR_FREE(nwfilter->name);
-    virObjectUnref(nwfilter->conn);
+
+    G_OBJECT_CLASS(vir_nw_filter_parent_class)->finalize(obj);
 }
 
 
diff --git a/src/datatypes.h b/src/datatypes.h
index 9dfb18147e..b51514d579 100644
--- a/src/datatypes.h
+++ b/src/datatypes.h
@@ -36,7 +36,6 @@ extern virClassPtr virInterfaceClass;
 extern virClassPtr virNetworkClass;
 extern virClassPtr virNetworkPortClass;
 extern virClassPtr virNodeDeviceClass;
-extern virClassPtr virNWFilterClass;
 extern virClassPtr virNWFilterBindingClass;
 extern virClassPtr virSecretClass;
 extern virClassPtr virStreamClass;
@@ -57,6 +56,9 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot,
                      DOMAIN_SNAPSHOT,
                      GObject);
 
+#define VIR_TYPE_NW_FILTER vir_nw_filter_get_type()
+G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, GObject);
+
 extern virClassPtr virAdmConnectClass;
 
 #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type()
@@ -311,8 +313,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject);
 
 #define virCheckNWFilterReturn(obj, retval) \
     do { \
-        virNWFilterPtr _nw = (obj); \
-        if (!virObjectIsClass(_nw, virNWFilterClass) || \
+        virNWFilterPtr _nw = VIR_NW_FILTER(obj); \
+        if (_nw == NULL || \
             !virObjectIsClass(_nw->conn, virConnectClass)) { \
             virReportErrorHelper(VIR_FROM_NWFILTER, \
                                  VIR_ERR_INVALID_NWFILTER, \
@@ -784,7 +786,7 @@ struct _virDomainSnapshot {
 * Internal structure associated to a network filter
 */
 struct _virNWFilter {
-    virObject parent;
+    GObject parent;
     virConnectPtr conn;                  /* pointer back to the connection */
     char *name;                          /* the network filter external name */
     unsigned char uuid[VIR_UUID_BUFLEN]; /* the network filter unique identifier */
diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c
index d28220db8a..cd3f5759b7 100644
--- a/src/libvirt-nwfilter.c
+++ b/src/libvirt-nwfilter.c
@@ -281,7 +281,7 @@ virNWFilterFree(virNWFilterPtr nwfilter)
 
     virCheckNWFilterReturn(nwfilter, -1);
 
-    virObjectUnref(nwfilter);
+    g_object_unref(nwfilter);
     return 0;
 }
 
@@ -503,14 +503,13 @@ virNWFilterGetXMLDesc(virNWFilterPtr nwfilter, unsigned int flags)
 int
 virNWFilterRef(virNWFilterPtr nwfilter)
 {
-    VIR_DEBUG("nwfilter=%p refs=%d", nwfilter,
-              nwfilter ? nwfilter->parent.u.s.refs : 0);
+    VIR_DEBUG("nwfilter=%p", nwfilter);
 
     virResetLastError();
 
     virCheckNWFilterReturn(nwfilter, -1);
 
-    virObjectRef(nwfilter);
+    g_object_ref(nwfilter);
     return 0;
 }
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 33d6d4950d..44ffeb6abc 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1348,6 +1348,7 @@ virCPUx86FeatureFilterSelectMSR;
 # datatypes.h
 vir_domain_checkpoint_get_type;
 vir_domain_snapshot_get_type;
+vir_nw_filter_get_type;
 virConnectClass;
 virConnectCloseCallbackDataCall;
 virConnectCloseCallbackDataClass;
@@ -1374,7 +1375,6 @@ virNetworkClass;
 virNetworkPortClass;
 virNewConnectCloseCallbackData;
 virNodeDeviceClass;
-virNWFilterClass;
 virSecretClass;
 virStoragePoolClass;
 virStorageVolClass;
diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl
index bca3b33e80..b51e2715ee 100755
--- a/src/rpc/gendispatch.pl
+++ b/src/rpc/gendispatch.pl
@@ -198,6 +198,7 @@ my %gobject_impl = (
     virAdmServer => 1,
     virDomainCheckpoint => 1,
     virDomainSnapshot => 1,
+    virNWFilter => 1,
 );
 
 sub use_gobject {
-- 
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