Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/node_device_event.c | 11 +++--- src/conf/node_device_util.c | 4 +-- src/conf/virnodedeviceobj.c | 3 +- src/datatypes.c | 67 +++++++++++++++++++++++++----------- src/datatypes.h | 14 ++++---- src/libvirt-nodedev.c | 6 ++-- src/libvirt_private.syms | 2 +- src/libxl/libxl_driver.c | 9 ++--- src/qemu/qemu_driver.c | 9 ++--- src/remote/remote_driver.c | 6 ++-- src/rpc/gendispatch.pl | 5 ++- src/test/test_driver.c | 4 +-- 12 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/conf/node_device_event.c b/src/conf/node_device_event.c index c6915be593..e4037f5ae0 100644 --- a/src/conf/node_device_event.c +++ b/src/conf/node_device_event.c @@ -110,8 +110,8 @@ virNodeDeviceEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virNodeDevicePtr dev = virGetNodeDevice(conn, - event->meta.name); + g_autoptr(virNodeDevice) dev = virGetNodeDevice(conn, + event->meta.name); if (!dev) return; @@ -126,23 +126,20 @@ virNodeDeviceEventDispatchDefaultFunc(virConnectPtr conn, nodeDeviceLifecycleEvent->type, nodeDeviceLifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_NODE_DEVICE_EVENT_ID_UPDATE: { ((virConnectNodeDeviceEventGenericCallback)cb)(conn, dev, cbopaque); - goto cleanup; + return; } case VIR_NODE_DEVICE_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(dev); } diff --git a/src/conf/node_device_util.c b/src/conf/node_device_util.c index 5e80422d7f..0b9292ffd3 100644 --- a/src/conf/node_device_util.c +++ b/src/conf/node_device_util.c @@ -20,6 +20,7 @@ #include "internal.h" +#include "datatypes.h" #include "node_device_util.h" #include "virlog.h" #include "virscsihost.h" @@ -45,7 +46,7 @@ char * virNodeDeviceGetParentName(virConnectPtr conn, const char *nodedev_name) { - virNodeDevicePtr device = NULL; + g_autoptr(virNodeDevice) device = NULL; char *parent; if (!(device = virNodeDeviceLookupByName(conn, nodedev_name))) { @@ -56,7 +57,6 @@ virNodeDeviceGetParentName(virConnectPtr conn, } parent = g_strdup(virNodeDeviceGetParent(device)); - virObjectUnref(device); return parent; } diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index 3a34a324ca..9b1133f96b 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -866,7 +866,6 @@ virNodeDeviceObjListExportCallback(void *payload, virNodeDeviceObjMatch(obj, data->flags)) { if (data->devices) { if (!(device = virGetNodeDevice(data->conn, def->name))) { - virObjectUnref(device); data->error = true; goto cleanup; } @@ -914,7 +913,7 @@ virNodeDeviceObjListExport(virConnectPtr conn, return data.ndevices; cleanup: - virObjectListFree(data.devices); + virGObjectListFreeCount(data.devices, data.ndevices); return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index 66d4f478ab..f324a19ada 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,14 +36,12 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; virClassPtr virStreamClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); @@ -137,6 +135,24 @@ vir_network_port_class_init(virNetworkPortClass *klass) obj->finalize = virNetworkPortFinalize; } +G_DEFINE_TYPE(virNodeDevice, vir_node_device, G_TYPE_OBJECT); +static void virNodeDeviceDispose(GObject *obj); +static void virNodeDeviceFinalize(GObject *obj); + +static void +vir_node_device_init(virNodeDevice *dev G_GNUC_UNUSED) +{ +} + +static void +vir_node_device_class_init(virNodeDeviceClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virNodeDeviceDispose; + obj->finalize = virNodeDeviceFinalize; +} + G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); static void virNWFilterDispose(GObject *obj); static void virNWFilterFinalize(GObject *obj); @@ -263,7 +279,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virNodeDevice); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); @@ -879,32 +894,27 @@ virStorageVolFinalize(GObject *obj) * @name: device name (unique on node) * * Allocates a new node device 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 node device object, or NULL on error. */ virNodeDevicePtr virGetNodeDevice(virConnectPtr conn, const char *name) { - virNodeDevicePtr ret = NULL; + g_autoptr(virNodeDevice) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(name, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(name, NULL); - if (!(ret = virObjectNew(virNodeDeviceClass))) - goto error; + ret = VIR_NODE_DEVICE(g_object_new(VIR_TYPE_NODE_DEVICE, NULL)); ret->name = g_strdup(name); ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -912,22 +922,37 @@ virGetNodeDevice(virConnectPtr conn, const char *name) * virNodeDeviceDispose: * @obj: the node device to release * - * Unconditionally release all memory associated with a device. - * The device 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 +virNodeDeviceDispose(GObject *obj) +{ + virNodeDevicePtr dev = VIR_NODE_DEVICE(obj); + + virObjectUnref(dev->conn); + dev->conn = NULL; + + G_OBJECT_CLASS(vir_node_device_parent_class)->dispose(obj); +} + +/** + * virNodeDeviceFinalize: + * @obj: the node device 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 device. + * The device object must not be used once this method returns. */ static void -virNodeDeviceDispose(void *obj) +virNodeDeviceFinalize(GObject *obj) { - virNodeDevicePtr dev = obj; + virNodeDevicePtr dev = VIR_NODE_DEVICE(obj); VIR_DEBUG("release dev %p %s", dev, dev->name); VIR_FREE(dev->name); VIR_FREE(dev->parentName); - virObjectUnref(dev->conn); + G_OBJECT_CLASS(vir_node_device_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 5492c2ac8a..cbf565097f 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; @@ -59,6 +58,9 @@ 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_NODE_DEVICE vir_node_device_get_type() +G_DECLARE_FINAL_TYPE(virNodeDevice, vir_node_device, VIR, NODE_DEVICE, GObject); + #define VIR_TYPE_NW_FILTER vir_nw_filter_get_type() G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, GObject); @@ -248,8 +250,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNodeDeviceReturn(obj, retval) \ do { \ - virNodeDevicePtr _node = (obj); \ - if (!virObjectIsClass(_node, virNodeDeviceClass) || \ + virNodeDevicePtr _node = VIR_NODE_DEVICE(obj); \ + if (_node == NULL || \ !virObjectIsClass(_node->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NODEDEV, \ VIR_ERR_INVALID_NODE_DEVICE, \ @@ -262,8 +264,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNodeDeviceGoto(obj, label) \ do { \ - virNodeDevicePtr _dev= (obj); \ - if (!virObjectIsClass(_dev, virNodeDeviceClass) || \ + virNodeDevicePtr _dev= VIR_NODE_DEVICE(obj); \ + if (_dev == NULL || \ !virObjectIsClass(_dev->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NODEDEV, \ VIR_ERR_INVALID_NODE_DEVICE, \ @@ -730,7 +732,7 @@ struct _virStorageVol { * Internal structure associated with a node device */ struct _virNodeDevice { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* device name (unique on node) */ char *parentName; /* parent device name */ diff --git a/src/libvirt-nodedev.c b/src/libvirt-nodedev.c index dce46b7181..98db5e9813 100644 --- a/src/libvirt-nodedev.c +++ b/src/libvirt-nodedev.c @@ -452,7 +452,7 @@ virNodeDeviceFree(virNodeDevicePtr dev) virCheckNodeDeviceReturn(dev, -1); - virObjectUnref(dev); + g_object_unref(dev); return 0; } @@ -477,13 +477,13 @@ virNodeDeviceFree(virNodeDevicePtr dev) int virNodeDeviceRef(virNodeDevicePtr dev) { - VIR_DEBUG("dev=%p refs=%d", dev, dev ? dev->parent.u.s.refs : 0); + VIR_DEBUG("dev=%p", dev); virResetLastError(); virCheckNodeDeviceReturn(dev, -1); - virObjectRef(dev); + g_object_ref(dev); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9e559ad1f8..2112082277 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1351,6 +1351,7 @@ vir_domain_snapshot_get_type; vir_interface_get_type; vir_network_get_type; vir_network_port_get_type; +vir_node_device_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; vir_storage_pool_get_type; @@ -1377,7 +1378,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virNewConnectCloseCallbackData; -virNodeDeviceClass; virSecretClass; virStreamClass; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 0fce6225d8..0e4d6c56fc 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -5787,7 +5787,7 @@ libxlNodeDeviceDetachFlags(virNodeDevicePtr dev, libxlDriverPrivatePtr driver = dev->conn->privateData; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; virCheckFlags(0, -1); @@ -5837,7 +5837,6 @@ libxlNodeDeviceDetachFlags(virNodeDevicePtr dev, cleanup: virPCIDeviceFree(pci); virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); VIR_FREE(xml); return ret; @@ -5860,7 +5859,7 @@ libxlNodeDeviceReAttach(virNodeDevicePtr dev) libxlDriverPrivatePtr driver = dev->conn->privateData; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; if (!(nodeconn = virGetConnectNodeDev())) goto cleanup; @@ -5901,7 +5900,6 @@ libxlNodeDeviceReAttach(virNodeDevicePtr dev) cleanup: virPCIDeviceFree(pci); virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); VIR_FREE(xml); return ret; @@ -5918,7 +5916,7 @@ libxlNodeDeviceReset(virNodeDevicePtr dev) libxlDriverPrivatePtr driver = dev->conn->privateData; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; if (!(nodeconn = virGetConnectNodeDev())) goto cleanup; @@ -5959,7 +5957,6 @@ libxlNodeDeviceReset(virNodeDevicePtr dev) cleanup: virPCIDeviceFree(pci); virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); VIR_FREE(xml); return ret; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 46851d063e..b35cca4dcc 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -12957,7 +12957,7 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev, bool vfio = qemuHostdevHostSupportsPassthroughVFIO(); virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; virCheckFlags(0, -1); @@ -13018,7 +13018,6 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev, cleanup: virPCIDeviceFree(pci); virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); return ret; } @@ -13040,7 +13039,7 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev) g_autofree char *xml = NULL; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; if (!(nodeconn = virGetConnectNodeDev())) goto cleanup; @@ -13078,7 +13077,6 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev) virPCIDeviceFree(pci); cleanup: virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); return ret; } @@ -13094,7 +13092,7 @@ qemuNodeDeviceReset(virNodeDevicePtr dev) g_autofree char *xml = NULL; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; virConnectPtr nodeconn = NULL; - virNodeDevicePtr nodedev = NULL; + g_autoptr(virNodeDevice) nodedev = NULL; if (!(nodeconn = virGetConnectNodeDev())) goto cleanup; @@ -13132,7 +13130,6 @@ qemuNodeDeviceReset(virNodeDevicePtr dev) virPCIDeviceFree(pci); cleanup: virNodeDeviceDefFree(def); - virObjectUnref(nodedev); virObjectUnref(nodeconn); return ret; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 745033befb..0d7f352ae0 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5518,7 +5518,7 @@ remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_node_device_event_lifecycle_msg *msg = evdata; - virNodeDevicePtr dev; + g_autoptr(virNodeDevice) dev = NULL; virObjectEventPtr event = NULL; dev = get_nonnull_node_device(conn, msg->dev); @@ -5527,7 +5527,6 @@ remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virNodeDeviceEventLifecycleNew(dev->name, msg->event, msg->detail); - virObjectUnref(dev); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5540,7 +5539,7 @@ remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_node_device_event_update_msg *msg = evdata; - virNodeDevicePtr dev; + g_autoptr(virNodeDevice) dev = NULL; virObjectEventPtr event = NULL; dev = get_nonnull_node_device(conn, msg->dev); @@ -5548,7 +5547,6 @@ remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog G_GNUC_UNUSED, return; event = virNodeDeviceEventUpdateNew(dev->name); - virObjectUnref(dev); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 1cfafe3742..6869311f06 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -201,6 +201,7 @@ my %gobject_impl = ( virInterface => 1, virNetwork => 1, virNetworkPort => 1, + virNodeDevice => 1, virNWFilter => 1, virNWFilterBinding => 1, virStoragePool => 1, @@ -596,13 +597,11 @@ elsif ($mode eq "server") { !($argtype =~ m/^remote_node_device_create_xml_/) and !($argtype =~ m/^remote_node_device_lookup_scsi_host_by_wwn_/)) { $has_node_device = 1; - push(@vars_list, "virNodeDevicePtr dev = NULL"); + push(@vars_list, "g_autoptr(virNodeDevice) dev = NULL"); push(@getters_list, " if (!(dev = get_nonnull_node_device_name($conn_var, args->name)))\n" . " goto cleanup;\n"); push(@args_list, "dev"); - push(@free_list, - " virObjectUnref(dev);"); } foreach my $args_member (@{$call->{args_members}}) { diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 5b9a49d685..c4065eb00a 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -7451,7 +7451,8 @@ testNodeDeviceCreateXML(virConnectPtr conn, { testDriverPtr driver = conn->privateData; virNodeDeviceDefPtr def = NULL; - virNodeDevicePtr dev = NULL, ret = NULL; + g_autoptr(virNodeDevice) dev = NULL; + virNodeDevicePtr ret = NULL; virNodeDeviceObjPtr obj = NULL; virNodeDeviceDefPtr objdef; g_autofree char *wwnn = NULL; @@ -7494,7 +7495,6 @@ testNodeDeviceCreateXML(virConnectPtr conn, cleanup: virNodeDeviceObjEndAPI(&obj); virNodeDeviceDefFree(def); - virObjectUnref(dev); return ret; } -- 2.25.3