Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/virnwfilterbindingobjlist.c | 6 +++- src/datatypes.c | 44 +++++++++++++++++----------- src/datatypes.h | 14 ++++++--- src/libvirt-nwfilter.c | 7 ++--- src/nwfilter/nwfilter_driver.c | 3 +- 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/conf/virnwfilterbindingobjlist.c b/src/conf/virnwfilterbindingobjlist.c index 97e4d3c49b..110403bfcc 100644 --- a/src/conf/virnwfilterbindingobjlist.c +++ b/src/conf/virnwfilterbindingobjlist.c @@ -489,7 +489,11 @@ virNWFilterBindingObjListExport(virNWFilterBindingObjListPtr bindings, cleanup: virObjectListFreeCount(bindingobjs, nbindings); if (ret < 0) { - virObjectListFreeCount(*bindinglist, nbindings); + for (i = 0; i < nbindings; i++) { + if ((*bindinglist)[i]) + g_object_unref((*bindinglist)[i]); + } + VIR_FREE(*bindinglist); *bindinglist = NULL; } return ret; diff --git a/src/datatypes.c b/src/datatypes.c index d4f60db2be..b9879cfb9e 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -40,7 +40,6 @@ virClassPtr virInterfaceClass; virClassPtr virNetworkClass; virClassPtr virNetworkPortClass; virClassPtr virNodeDeviceClass; -virClassPtr virNWFilterBindingClass; virClassPtr virSecretClass; virClassPtr virStreamClass; virClassPtr virStorageVolClass; @@ -53,7 +52,6 @@ static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); static void virNetworkPortDispose(void *obj); static void virNodeDeviceDispose(void *obj); -static void virNWFilterBindingDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); static void virStorageVolDispose(void *obj); @@ -107,6 +105,22 @@ vir_nw_filter_class_init(virNWFilterClass *klass) obj->finalize = virNWFilterFinalize; } +G_DEFINE_TYPE(virNWFilterBinding, vir_nw_filter_binding, G_TYPE_OBJECT); +static void virNWFilterBindingFinalize(GObject *obj); + +static void +vir_nw_filter_binding_init(virNWFilterBinding *bdg G_GNUC_UNUSED) +{ +} + +static void +vir_nw_filter_binding_class_init(virNWFilterBindingClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virNWFilterBindingFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -163,7 +177,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); DECLARE_CLASS(virNodeDevice); - DECLARE_CLASS(virNWFilterBinding); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS(virStorageVol); @@ -958,7 +971,7 @@ virNWFilterFinalize(GObject *obj) * @filtername: name of the network filter * * Allocates a new network filter binding object. When the object is no longer - * needed, virObjectUnref() must be called in order to not leak data. + * needed, g_object_unref() must be called in order to not leak data. * * Returns a pointer to the network filter binding object, or NULL on error. */ @@ -966,16 +979,15 @@ virNWFilterBindingPtr virGetNWFilterBinding(virConnectPtr conn, const char *portdev, const char *filtername) { - virNWFilterBindingPtr ret = NULL; + g_autoptr(virNWFilterBinding) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(portdev, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(portdev, NULL); - if (!(ret = virObjectNew(virNWFilterBindingClass))) - goto error; + ret = VIR_NW_FILTER_BINDING(g_object_new(VIR_TYPE_NW_FILTER_BINDING, NULL)); ret->portdev = g_strdup(portdev); @@ -983,16 +995,12 @@ virGetNWFilterBinding(virConnectPtr conn, const char *portdev, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** - * virNWFilterBindingDispose: + * virNWFilterBindingFinalize: * @obj: the network filter binding to release * * Unconditionally release all memory associated with a nwfilter binding. @@ -1002,15 +1010,17 @@ virGetNWFilterBinding(virConnectPtr conn, const char *portdev, * which may also be released if its ref count hits zero. */ static void -virNWFilterBindingDispose(void *obj) +virNWFilterBindingFinalize(GObject *obj) { - virNWFilterBindingPtr binding = obj; + virNWFilterBindingPtr binding = VIR_NW_FILTER_BINDING(obj); VIR_DEBUG("release binding %p %s", binding, binding->portdev); VIR_FREE(binding->portdev); VIR_FREE(binding->filtername); virObjectUnref(binding->conn); + + G_OBJECT_CLASS(vir_nw_filter_binding_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index e8bd9bf1e5..87c6a1366e 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 virNWFilterBindingClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; extern virClassPtr virStorageVolClass; @@ -50,6 +49,13 @@ G_DECLARE_FINAL_TYPE(virDomainCheckpoint, DOMAIN_CHECKPOINT, GObject); +#define VIR_TYPE_NW_FILTER_BINDING vir_nw_filter_binding_get_type() +G_DECLARE_FINAL_TYPE(virNWFilterBinding, + vir_nw_filter_binding, + VIR, + NW_FILTER_BINDING, + GObject); + #define VIR_TYPE_DOMAIN_SNAPSHOT vir_domain_snapshot_get_type() G_DECLARE_FINAL_TYPE(virDomainSnapshot, vir_domain_snapshot, @@ -328,8 +334,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNWFilterBindingReturn(obj, retval) \ do { \ - virNWFilterBindingPtr _nw = (obj); \ - if (!virObjectIsClass(_nw, virNWFilterBindingClass) || \ + virNWFilterBindingPtr _nw = VIR_NW_FILTER_BINDING(obj); \ + if (!G_IS_OBJECT(_nw) || !(G_OBJECT_TYPE(_nw) == VIR_TYPE_NW_FILTER_BINDING) || \ !virObjectIsClass(_nw->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NWFILTER, \ VIR_ERR_INVALID_NWFILTER_BINDING, \ @@ -800,7 +806,7 @@ struct _virNWFilter { * Internal structure associated to a network filter port binding */ struct _virNWFilterBinding { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *portdev; /* the network filter port device name */ char *filtername; /* the network filter name */ diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c index cd3f5759b7..ac34d1fea0 100644 --- a/src/libvirt-nwfilter.c +++ b/src/libvirt-nwfilter.c @@ -621,7 +621,7 @@ virNWFilterBindingFree(virNWFilterBindingPtr binding) virCheckNWFilterBindingReturn(binding, -1); - virObjectUnref(binding); + g_object_unref(binding); return 0; } @@ -819,13 +819,12 @@ virNWFilterBindingGetXMLDesc(virNWFilterBindingPtr binding, unsigned int flags) int virNWFilterBindingRef(virNWFilterBindingPtr binding) { - VIR_DEBUG("binding=%p refs=%d", binding, - binding ? binding->parent.u.s.refs : 0); + VIR_DEBUG("binding=%p", binding); virResetLastError(); virCheckNWFilterBindingReturn(binding, -1); - virObjectRef(binding); + g_object_ref(binding); return 0; } diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index 1c407727db..cbf396342e 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -785,8 +785,7 @@ nwfilterBindingCreateXML(virConnectPtr conn, if (virNWFilterInstantiateFilter(driver, def) < 0) { virNWFilterBindingObjListRemove(driver->bindings, obj); - virObjectUnref(ret); - ret = NULL; + g_clear_object(&ret); goto cleanup; } virNWFilterBindingObjSave(obj, driver->bindingDir); -- 2.25.1