Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/domain_conf.c | 6 ++-- src/conf/virnetworkobj.c | 5 +--- src/datatypes.c | 46 ++++++++++++++++++----------- src/datatypes.h | 15 +++++----- src/libvirt-network.c | 7 ++--- src/libvirt_private.syms | 2 +- src/remote/remote_daemon_dispatch.c | 7 ++--- src/remote/remote_driver.c | 4 +-- src/rpc/gendispatch.pl | 1 + 9 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 5f677d5bb3..f7392fce05 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -31883,7 +31883,7 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, virDomainNetDefPtr iface) { g_autoptr(virNetwork) net = NULL; - virNetworkPortPtr port = NULL; + g_autoptr(virNetworkPort) port = NULL; int ret = -1; /* Port might not exist if a failure occurred during VM startup */ @@ -31902,7 +31902,6 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, goto cleanup; cleanup: - virObjectUnref(port); return ret; } @@ -31956,7 +31955,7 @@ virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, virNetDevBandwidthPtr newBandwidth) { g_autoptr(virNetwork) net = NULL; - virNetworkPortPtr port = NULL; + g_autoptr(virNetworkPort) port = NULL; virTypedParameterPtr params = NULL; int nparams = 0; virConnectPtr conn = NULL; @@ -31981,7 +31980,6 @@ virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, cleanup: virObjectUnref(conn); virTypedParamsFree(params, nparams); - virObjectUnref(port); return ret; } diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index 2d375deb47..946008cb5e 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -1821,10 +1821,7 @@ virNetworkObjPortListExport(virNetworkPtr net, ret = data.nports; cleanup: - while (data.ports && data.nports) - virObjectUnref(data.ports[--data.nports]); - - VIR_FREE(data.ports); + virGObjectListFreeCount(data.ports, data.nports); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index 3889ece17e..3cad8f2bc6 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -37,7 +37,6 @@ virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; virClassPtr virInterfaceClass; -virClassPtr virNetworkPortClass; virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; virClassPtr virStreamClass; @@ -48,7 +47,6 @@ static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); static void virInterfaceDispose(void *obj); -static void virNetworkPortDispose(void *obj); static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); @@ -103,6 +101,22 @@ vir_network_class_init(virNetworkClass *klass) obj->finalize = virNetworkFinalize; } +G_DEFINE_TYPE(virNetworkPort, vir_network_port, G_TYPE_OBJECT); +static void virNetworkPortFinalize(GObject *obj); + +static void +vir_network_port_init(virNetworkPort *np G_GNUC_UNUSED) +{ +} + +static void +vir_network_port_class_init(virNetworkPortClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virNetworkPortFinalize; +} + G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); static void virNWFilterFinalize(GObject *obj); @@ -188,7 +202,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); DECLARE_CLASS(virInterface); - DECLARE_CLASS(virNetworkPort); DECLARE_CLASS(virNodeDevice); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); @@ -482,36 +495,31 @@ virNetworkFinalize(GObject *obj) * @uuid: pointer to the uuid * * Allocates a new network port 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 port object, or NULL on error. */ virNetworkPortPtr virGetNetworkPort(virNetworkPtr net, const unsigned char *uuid) { - virNetworkPortPtr ret = NULL; + g_autoptr(virNetworkPort) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckNetworkGoto(net, error); - virCheckNonNullArgGoto(uuid, error); + virCheckNetworkReturn(net, NULL); + virCheckNonNullArgReturn(uuid, NULL); - if (!(ret = virObjectNew(virNetworkPortClass))) - goto error; + ret = VIR_NETWORK_PORT(g_object_new(VIR_TYPE_NETWORK_PORT, NULL)); ret->net = g_object_ref(net); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** - * virNetworkPortDispose: + * virNetworkPortFinalize: * @obj: the network port to release * * Unconditionally release all memory associated with a network port. @@ -521,15 +529,17 @@ virGetNetworkPort(virNetworkPtr net, const unsigned char *uuid) * which may also be released if its ref count hits zero. */ static void -virNetworkPortDispose(void *obj) +virNetworkPortFinalize(GObject *obj) { - virNetworkPortPtr port = obj; + virNetworkPortPtr port = VIR_NETWORK_PORT(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(port->uuid, uuidstr); VIR_DEBUG("release network port %p %s", port, uuidstr); - g_object_unref(port->net); + g_clear_object(&port->net); + + G_OBJECT_CLASS(vir_network_port_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 706cec340f..41f366d21a 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -33,7 +33,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; extern virClassPtr virInterfaceClass; -extern virClassPtr virNetworkPortClass; extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; @@ -57,6 +56,9 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot, #define VIR_TYPE_NETWORK vir_network_get_type() G_DECLARE_FINAL_TYPE(virNetwork, vir_network, VIR, NETWORK, GObject); +#define VIR_TYPE_NETWORK_PORT vir_network_port_get_type() +G_DECLARE_FINAL_TYPE(virNetworkPort, vir_network_port, VIR, NETWORK_PORT, GObject); + #define VIR_TYPE_NW_FILTER vir_nw_filter_get_type() G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, GObject); @@ -146,8 +148,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNetworkPortReturn(obj, retval) \ do { \ - virNetworkPortPtr _port = (obj); \ - if (!virObjectIsClass(_port, virNetworkPortClass) || \ + virNetworkPortPtr _port = VIR_NETWORK_PORT(obj); \ + if (_port == NULL || \ !VIR_IS_NETWORK(_port->net)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK_PORT, \ @@ -160,8 +162,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNetworkPortGoto(obj, label) \ do { \ - virNetworkPortPtr _port = (obj); \ - if (!virObjectIsClass(_port, virNetworkPortClass) || \ + virNetworkPortPtr _port = VIR_NETWORK_PORT(obj); \ + if (_port == NULL || \ !VIR_IS_NETWORK(_port->net)) { \ virReportErrorHelper(VIR_FROM_NETWORK, \ VIR_ERR_INVALID_NETWORK_PORT, \ @@ -654,12 +656,11 @@ struct _virNetwork { * Internal structure associated to a network port */ struct _virNetworkPort { - virObject parent; + GObject parent; virNetworkPtr net; /* pointer back to the connection */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the network unique identifier */ }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkPort, virObjectUnref); /** diff --git a/src/libvirt-network.c b/src/libvirt-network.c index 3847617d4d..1ab499fb18 100644 --- a/src/libvirt-network.c +++ b/src/libvirt-network.c @@ -1688,7 +1688,7 @@ virNetworkPortFree(virNetworkPortPtr port) virCheckNetworkPortReturn(port, -1); - virObjectUnref(port); + g_object_unref(port); return 0; } @@ -1713,13 +1713,12 @@ virNetworkPortFree(virNetworkPortPtr port) int virNetworkPortRef(virNetworkPortPtr port) { - VIR_DEBUG("port=%p refs=%d", port, - port ? port->parent.u.s.refs : 0); + VIR_DEBUG("port=%p", port); virResetLastError(); virCheckNetworkPortReturn(port, -1); - virObjectRef(port); + g_object_ref(port); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a7255d6f18..782d9c1eac 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1359,6 +1359,7 @@ virCPUx86FeatureFilterSelectMSR; vir_domain_checkpoint_get_type; vir_domain_snapshot_get_type; vir_network_get_type; +vir_network_port_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; virConnectClass; @@ -1383,7 +1384,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virInterfaceClass; -virNetworkPortClass; virNewConnectCloseCallbackData; virNodeDeviceClass; virSecretClass; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 4add62bb86..d5c5ab9aab 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -7170,7 +7170,7 @@ remoteDispatchNetworkPortGetParameters(virNetServerPtr server G_GNUC_UNUSED, remote_network_port_get_parameters_ret *ret) { int rv = -1; - virNetworkPortPtr port = NULL; + g_autoptr(virNetworkPort) port = NULL; virTypedParameterPtr params = NULL; int nparams = 0; virConnectPtr conn = remoteGetNetworkConn(client); @@ -7196,7 +7196,6 @@ remoteDispatchNetworkPortGetParameters(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(port); virTypedParamsFree(params, nparams); return rv; } @@ -7225,13 +7224,11 @@ get_nonnull_network(virConnectPtr conn, remote_nonnull_network network) static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port) { - virNetworkPortPtr ret; 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); - return ret; + return virGetNetworkPort(net, BAD_CAST port.uuid); } static virInterfacePtr diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 283a0e9c12..48d83d5fc4 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -8268,13 +8268,11 @@ get_nonnull_network(virConnectPtr conn, remote_nonnull_network network) static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port) { - virNetworkPortPtr ret; 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); - return ret; + return virGetNetworkPort(net, BAD_CAST port.uuid); } static virInterfacePtr diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 2c101fab6d..5c14038428 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -199,6 +199,7 @@ my %gobject_impl = ( virDomainCheckpoint => 1, virDomainSnapshot => 1, virNetwork => 1, + virNetworkPort => 1, virNWFilter => 1, virNWFilterBinding => 1, ); -- 2.26.2