Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/virinterfaceobj.c | 5 +- src/datatypes.c | 68 +++++++++++++++++-------- src/datatypes.h | 10 ++-- src/interface/interface_backend_netcf.c | 7 +-- src/interface/interface_backend_udev.c | 8 +-- src/libvirt-interface.c | 6 +-- src/libvirt_private.syms | 2 +- src/rpc/gendispatch.pl | 1 + 8 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index 43ba395d2b..e1f825723a 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -338,10 +338,7 @@ virInterfaceObjListExport(virConnectPtr conn, ret = data.nifaces; cleanup: virObjectRWUnlock(ifaceobjs); - while (data.ifaces && data.nifaces) - virObjectUnref(data.ifaces[--data.nifaces]); - - VIR_FREE(data.ifaces); + virGObjectListFreeCount(data.ifaces, data.nifaces); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index f06438c1c3..455bcd70ef 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,7 +36,6 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virInterfaceClass; virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; virClassPtr virStreamClass; @@ -46,7 +45,6 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virInterfaceDispose(void *obj); static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); @@ -89,6 +87,24 @@ vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) obj->finalize = virDomainSnapshotFinalize; } +G_DEFINE_TYPE(virInterface, vir_interface, G_TYPE_OBJECT); +static void virInterfaceDispose(GObject *obj); +static void virInterfaceFinalize(GObject *obj); + +static void +vir_interface_init(virInterface *iface G_GNUC_UNUSED) +{ +} + +static void +vir_interface_class_init(virInterfaceClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virInterfaceDispose; + obj->finalize = virInterfaceFinalize; +} + G_DEFINE_TYPE(virNetwork, vir_network, G_TYPE_OBJECT); static void virNetworkDispose(GObject *obj); static void virNetworkFinalize(GObject *obj); @@ -215,7 +231,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virInterface); DECLARE_CLASS(virNodeDevice); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); @@ -590,59 +605,70 @@ virNetworkPortFinalize(GObject *obj) * @mac: pointer to the mac * * Allocates a new interface 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 interface object, or NULL on error. */ virInterfacePtr virGetInterface(virConnectPtr conn, const char *name, const char *mac) { - virInterfacePtr ret = NULL; + g_autoptr(virInterface) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(name, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(name, NULL); /* a NULL mac from caller is okay. Treat it as blank */ if (mac == NULL) mac = ""; - if (!(ret = virObjectNew(virInterfaceClass))) - goto error; + ret = VIR_INTERFACE(g_object_new(VIR_TYPE_INTERFACE, NULL)); ret->name = g_strdup(name); ret->mac = g_strdup(mac); ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** * virInterfaceDispose: * @obj: the interface to release * - * Unconditionally release all memory associated with an interface. - * The interface 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 +virInterfaceDispose(GObject *obj) +{ + virInterfacePtr iface = VIR_INTERFACE(obj); + + virObjectUnref(iface->conn); + iface->conn = NULL; + + G_OBJECT_CLASS(vir_interface_parent_class)->dispose(obj); +} + +/** + * virInterfaceFinalize: + * @obj: the interface 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 an interface. + * The interface object must not be used once this method returns. */ static void -virInterfaceDispose(void *obj) +virInterfaceFinalize(GObject *obj) { - virInterfacePtr iface = obj; + virInterfacePtr iface = VIR_INTERFACE(obj); VIR_DEBUG("release interface %p %s", iface, iface->name); VIR_FREE(iface->name); VIR_FREE(iface->mac); - virObjectUnref(iface->conn); + + G_OBJECT_CLASS(vir_interface_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 41f366d21a..e9bc1574e7 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virInterfaceClass; extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; @@ -53,6 +52,9 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot, DOMAIN_SNAPSHOT, GObject); +#define VIR_TYPE_INTERFACE vir_interface_get_type() +G_DECLARE_FINAL_TYPE(virInterface, vir_interface, VIR, INTERFACE, GObject); + #define VIR_TYPE_NETWORK vir_network_get_type() G_DECLARE_FINAL_TYPE(virNetwork, vir_network, VIR, NETWORK, GObject); @@ -175,8 +177,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckInterfaceReturn(obj, retval) \ do { \ - virInterfacePtr _iface = (obj); \ - if (!virObjectIsClass(_iface, virInterfaceClass) || \ + virInterfacePtr _iface = VIR_INTERFACE(obj); \ + if (_iface == NULL || \ !virObjectIsClass(_iface->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_INTERFACE, \ VIR_ERR_INVALID_INTERFACE, \ @@ -669,7 +671,7 @@ struct _virNetworkPort { * Internal structure associated to a physical host interface */ struct _virInterface { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* the network external name */ char *mac; /* the interface MAC address */ diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c index dd0c1481d9..c09685dfad 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -738,12 +738,7 @@ netcfConnectListAllInterfaces(virConnectPtr conn, VIR_FREE(names[i]); VIR_FREE(names); - if (tmp_iface_objs) { - for (i = 0; i < niface_objs; i++) - virObjectUnref(tmp_iface_objs[i]); - VIR_FREE(tmp_iface_objs); - } - + virGObjectListFreeCount(tmp_iface_objs, niface_objs); virObjectUnlock(driver); return ret; } diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c index e388f98536..00ade59d02 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -310,7 +310,6 @@ udevConnectListAllInterfaces(virConnectPtr conn, struct udev_list_entry *dev_entry; virInterfacePtr *ifaces_list = NULL; virInterfacePtr iface_obj; - int tmp_count; int count = 0; int status = 0; int ret; @@ -419,12 +418,7 @@ udevConnectListAllInterfaces(virConnectPtr conn, udev_enumerate_unref(enumerate); udev_unref(udev); - if (ifaces) { - for (tmp_count = 0; tmp_count < count; tmp_count++) - virObjectUnref(ifaces_list[tmp_count]); - } - - VIR_FREE(ifaces_list); + virGObjectListFreeCount(ifaces_list, count); return ret; diff --git a/src/libvirt-interface.c b/src/libvirt-interface.c index 2d2df68131..0fd0ce7154 100644 --- a/src/libvirt-interface.c +++ b/src/libvirt-interface.c @@ -642,13 +642,13 @@ virInterfaceDestroy(virInterfacePtr iface, unsigned int flags) int virInterfaceRef(virInterfacePtr iface) { - VIR_DEBUG("iface=%p refs=%d", iface, iface ? iface->parent.u.s.refs : 0); + VIR_DEBUG("iface=%p", iface); virResetLastError(); virCheckInterfaceReturn(iface, -1); - virObjectRef(iface); + g_object_ref(iface); return 0; } @@ -671,7 +671,7 @@ virInterfaceFree(virInterfacePtr iface) virCheckInterfaceReturn(iface, -1); - virObjectUnref(iface); + g_object_unref(iface); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 69134087c1..1549a85133 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_interface_get_type; vir_network_get_type; vir_network_port_get_type; vir_nw_filter_binding_get_type; @@ -1373,7 +1374,6 @@ virGetSecret; virGetStoragePool; virGetStorageVol; virGetStream; -virInterfaceClass; virNewConnectCloseCallbackData; virNodeDeviceClass; virSecretClass; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 5c14038428..0f16ca89c2 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -198,6 +198,7 @@ my %gobject_impl = ( virAdmServer => 1, virDomainCheckpoint => 1, virDomainSnapshot => 1, + virInterface => 1, virNetwork => 1, virNetworkPort => 1, virNWFilter => 1, -- 2.25.3