Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/domain_conf.c | 6 ++-- src/conf/network_event.c | 7 ++-- src/conf/virnetworkobj.c | 5 +-- src/datatypes.c | 50 +++++++++++++++++------------ src/datatypes.h | 19 +++++------ src/libvirt-network.c | 7 ++-- src/libvirt_private.syms | 2 +- src/libxl/libxl_conf.c | 3 +- src/libxl/xen_common.c | 3 +- src/remote/remote_daemon_dispatch.c | 11 +++---- src/remote/remote_driver.c | 8 ++--- src/rpc/gendispatch.pl | 1 + 12 files changed, 59 insertions(+), 63 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 15d371df6e..5f677d5bb3 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -31882,7 +31882,7 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, virDomainDefPtr dom G_GNUC_UNUSED, virDomainNetDefPtr iface) { - virNetworkPtr net = NULL; + g_autoptr(virNetwork) net = NULL; virNetworkPortPtr port = NULL; int ret = -1; @@ -31903,7 +31903,6 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, cleanup: virObjectUnref(port); - virObjectUnref(net); return ret; } @@ -31956,7 +31955,7 @@ int virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, virNetDevBandwidthPtr newBandwidth) { - virNetworkPtr net = NULL; + g_autoptr(virNetwork) net = NULL; virNetworkPortPtr port = NULL; virTypedParameterPtr params = NULL; int nparams = 0; @@ -31983,7 +31982,6 @@ virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, virObjectUnref(conn); virTypedParamsFree(params, nparams); virObjectUnref(port); - virObjectUnref(net); return ret; } diff --git a/src/conf/network_event.c b/src/conf/network_event.c index 41cabe340a..f96f19c5ef 100644 --- a/src/conf/network_event.c +++ b/src/conf/network_event.c @@ -88,7 +88,7 @@ virNetworkEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virNetworkPtr net = virGetNetwork(conn, event->meta.name, event->meta.uuid); + g_autoptr(virNetwork) net = virGetNetwork(conn, event->meta.name, event->meta.uuid); if (!net) return; @@ -102,16 +102,13 @@ virNetworkEventDispatchDefaultFunc(virConnectPtr conn, networkLifecycleEvent->type, networkLifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_NETWORK_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(net); } diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index b2affaacd3..2d375deb47 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -1429,10 +1429,7 @@ virNetworkObjListExport(virConnectPtr conn, ret = data.nnets; cleanup: virObjectRWUnlock(netobjs); - while (data.nets && data.nnets) - virObjectUnref(data.nets[--data.nnets]); - - VIR_FREE(data.nets); + virGObjectListFreeCount(data.nets, data.nnets); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index d95e7e5fcc..3889ece17e 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -37,7 +37,6 @@ virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; virClassPtr virInterfaceClass; -virClassPtr virNetworkClass; virClassPtr virNetworkPortClass; virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; @@ -49,7 +48,6 @@ static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); static void virInterfaceDispose(void *obj); -static void virNetworkDispose(void *obj); static void virNetworkPortDispose(void *obj); static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); @@ -89,6 +87,22 @@ vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) obj->finalize = virDomainSnapshotFinalize; } +G_DEFINE_TYPE(virNetwork, vir_network, G_TYPE_OBJECT); +static void virNetworkFinalize(GObject *obj); + +static void +vir_network_init(virNetwork *net G_GNUC_UNUSED) +{ +} + +static void +vir_network_class_init(virNetworkClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virNetworkFinalize; +} + G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); static void virNWFilterFinalize(GObject *obj); @@ -174,7 +188,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); DECLARE_CLASS(virInterface); - DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); DECLARE_CLASS(virNodeDevice); DECLARE_CLASS(virSecret); @@ -411,39 +424,34 @@ virDomainDispose(void *obj) * @uuid: pointer to the uuid * * Allocates a new network 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 object, or NULL on error. */ virNetworkPtr virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) { - virNetworkPtr ret = NULL; + g_autoptr(virNetwork) 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(virNetworkClass))) - goto error; + ret = VIR_NETWORK(g_object_new(VIR_TYPE_NETWORK, NULL)); ret->name = g_strdup(name); ret->conn = virObjectRef(conn); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** - * virNetworkDispose: + * virNetworkFinalize: * @obj: the network to release * * Unconditionally release all memory associated with a network. @@ -453,9 +461,9 @@ virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid) * which may also be released if its ref count hits zero. */ static void -virNetworkDispose(void *obj) +virNetworkFinalize(GObject *obj) { - virNetworkPtr network = obj; + virNetworkPtr network = VIR_NETWORK(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(network->uuid, uuidstr); @@ -463,6 +471,8 @@ virNetworkDispose(void *obj) VIR_FREE(network->name); virObjectUnref(network->conn); + + G_OBJECT_CLASS(vir_network_parent_class)->finalize(obj); } @@ -490,7 +500,7 @@ virGetNetworkPort(virNetworkPtr net, const unsigned char *uuid) if (!(ret = virObjectNew(virNetworkPortClass))) goto error; - ret->net = virObjectRef(net); + ret->net = g_object_ref(net); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); return ret; @@ -519,7 +529,7 @@ virNetworkPortDispose(void *obj) virUUIDFormat(port->uuid, uuidstr); VIR_DEBUG("release network port %p %s", port, uuidstr); - virObjectUnref(port->net); + g_object_unref(port->net); } diff --git a/src/datatypes.h b/src/datatypes.h index 5f0ab5c84b..706cec340f 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -33,7 +33,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; extern virClassPtr virInterfaceClass; -extern virClassPtr virNetworkClass; extern virClassPtr virNetworkPortClass; extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; @@ -55,6 +54,9 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot, DOMAIN_SNAPSHOT, GObject); +#define VIR_TYPE_NETWORK vir_network_get_type() +G_DECLARE_FINAL_TYPE(virNetwork, vir_network, VIR, NETWORK, GObject); + #define VIR_TYPE_NW_FILTER vir_nw_filter_get_type() G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, GObject); @@ -118,8 +120,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNetworkReturn(obj, retval) \ do { \ - virNetworkPtr _net = (obj); \ - if (!virObjectIsClass(_net, virNetworkClass) || \ + virNetworkPtr _net = VIR_NETWORK(obj); \ + if (_net == NULL || \ !virObjectIsClass(_net->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK, \ @@ -131,8 +133,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); } while (0) #define virCheckNetworkGoto(obj, label) \ do { \ - virNetworkPtr _net = (obj); \ - if (!virObjectIsClass(_net, virNetworkClass) || \ + virNetworkPtr _net = VIR_NETWORK(obj); \ + if (_net == NULL || \ !virObjectIsClass(_net->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK, \ @@ -146,7 +148,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); do { \ virNetworkPortPtr _port = (obj); \ if (!virObjectIsClass(_port, virNetworkPortClass) || \ - !virObjectIsClass(_port->net, virNetworkClass)) { \ + !VIR_IS_NETWORK(_port->net)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK_PORT, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -160,7 +162,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); do { \ virNetworkPortPtr _port = (obj); \ if (!virObjectIsClass(_port, virNetworkPortClass) || \ - !virObjectIsClass(_port->net, virNetworkClass)) { \ + !VIR_IS_NETWORK(_port->net)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK_PORT, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -638,13 +640,12 @@ struct _virDomain { * Internal structure associated to a network */ struct _virNetwork { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* the network external name */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */ }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetwork, virObjectUnref); /** diff --git a/src/libvirt-network.c b/src/libvirt-network.c index 09e24fb0a8..3847617d4d 100644 --- a/src/libvirt-network.c +++ b/src/libvirt-network.c @@ -654,7 +654,7 @@ virNetworkFree(virNetworkPtr network) virCheckNetworkReturn(network, -1); - virObjectUnref(network); + g_object_unref(network); return 0; } @@ -679,14 +679,13 @@ virNetworkFree(virNetworkPtr network) int virNetworkRef(virNetworkPtr network) { - VIR_DEBUG("network=%p refs=%d", network, - network ? network->parent.u.s.refs : 0); + VIR_DEBUG("network=%p", network); virResetLastError(); virCheckNetworkReturn(network, -1); - virObjectRef(network); + g_object_ref(network); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e93d005bba..a7255d6f18 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1358,6 +1358,7 @@ virCPUx86FeatureFilterSelectMSR; # datatypes.h vir_domain_checkpoint_get_type; vir_domain_snapshot_get_type; +vir_network_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; virConnectClass; @@ -1382,7 +1383,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virInterfaceClass; -virNetworkClass; virNetworkPortClass; virNewConnectCloseCallbackData; virNodeDeviceClass; diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index a41f22386f..2a719486fa 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -1236,7 +1236,7 @@ libxlMakeNic(virDomainDefPtr def, bool attach) { virDomainNetType actual_type = virDomainNetGetActualType(l_nic); - virNetworkPtr network = NULL; + g_autoptr(virNetwork) network = NULL; virConnectPtr conn = NULL; const virNetDevBandwidth *actual_bw; const virNetDevVPortProfile *port_profile; @@ -1435,7 +1435,6 @@ libxlMakeNic(virDomainDefPtr def, cleanup: virBufferFreeAndReset(&buf); - virObjectUnref(network); virObjectUnref(conn); return ret; diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index 5c37e431eb..631c9fe8d5 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -1750,7 +1750,7 @@ xenFormatNet(virConnectPtr conn, case VIR_DOMAIN_NET_TYPE_NETWORK: { - virNetworkPtr network = virNetworkLookupByName(conn, net->data.network.name); + g_autoptr(virNetwork) network = virNetworkLookupByName(conn, net->data.network.name); char *bridge; if (!network) { virReportError(VIR_ERR_NO_NETWORK, "%s", @@ -1758,7 +1758,6 @@ xenFormatNet(virConnectPtr conn, return -1; } bridge = virNetworkGetBridgeName(network); - virObjectUnref(network); if (!bridge) { virReportError(VIR_ERR_INTERNAL_ERROR, _("network %s is not active"), diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 3b016636e0..4add62bb86 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -6014,7 +6014,7 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE int rv = -1; daemonClientEventCallbackPtr callback = NULL; daemonClientEventCallbackPtr ref; - virNetworkPtr net = NULL; + g_autoptr(virNetwork) net = NULL; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetNetworkConn(client); @@ -6074,7 +6074,6 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(net); return rv; } @@ -6756,7 +6755,7 @@ remoteDispatchNetworkGetDHCPLeases(virNetServerPtr server G_GNUC_UNUSED, int rv = -1; size_t i; virNetworkDHCPLeasePtr *leases = NULL; - virNetworkPtr net = NULL; + g_autoptr(virNetwork) net = NULL; int nleases = 0; virConnectPtr conn = remoteGetNetworkConn(client); @@ -6806,7 +6805,6 @@ remoteDispatchNetworkGetDHCPLeases(virNetServerPtr server G_GNUC_UNUSED, for (i = 0; i < nleases; i++) virNetworkDHCPLeaseFree(leases[i]); VIR_FREE(leases); - virObjectUnref(net); return rv; } @@ -7228,12 +7226,11 @@ static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port) { virNetworkPortPtr ret; - virNetworkPtr net; - net = virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid); + g_autoptr(virNetwork) net = + virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid); if (!net) return NULL; ret = virGetNetworkPort(net, BAD_CAST port.uuid); - virObjectUnref(net); return ret; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 961f8f78d9..283a0e9c12 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5456,7 +5456,7 @@ remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_network_event_lifecycle_msg *msg = evdata; - virNetworkPtr net; + g_autoptr(virNetwork) net = NULL; virObjectEventPtr event = NULL; net = get_nonnull_network(conn, msg->net); @@ -5465,7 +5465,6 @@ remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virNetworkEventLifecycleNew(net->name, net->uuid, msg->event, msg->detail); - virObjectUnref(net); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -8270,12 +8269,11 @@ static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port) { virNetworkPortPtr ret; - virNetworkPtr net; - net = virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid); + g_autoptr(virNetwork) net = + virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid); if (!net) return NULL; ret = virGetNetworkPort(net, BAD_CAST port.uuid); - virObjectUnref(net); return ret; } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 553ede5024..2c101fab6d 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -198,6 +198,7 @@ my %gobject_impl = ( virAdmServer => 1, virDomainCheckpoint => 1, virDomainSnapshot => 1, + virNetwork => 1, virNWFilter => 1, virNWFilterBinding => 1, ); -- 2.26.2