[PATCH v2 00/40] convert virObjects to GObject

This patch series convert various simple instances of virObject to a GObject equivalent. virLockableObject and virObjects which are subclassed will be covered in future patchsets. New in v2: - use *Dispose for unreffing objects and *Finalize for freeing data, as suggested in the GLib documentation - use `g_clear_object` instead of `if + g_object_unref` - properly update gendispatch as types are converted - virDomain is now converted to GObject too - added a helper function for freeing array of GObjects - added previously missed conversion spots Rafael Fonseca (40): util: virresctrl: convert classes to GObject conf: capabilities: convert virCaps to GOBject qemu: convert virQEMUCaps to GObject util: add function to unref array of GObjects rpc: convert virNetClientProgram to GObject rpc: convert virNetServerProgram to GObject conf: convert virDomainXMLOption to GObject bhyve: convert bhyveMonitor to GObject bhyve: convert virBhyveDriverConfig to GObject rpc: convert virNetServerService to GObject conf: convert virDomainCapsCPUModels to GObject util: convert dnsmasqCaps to GObject conf: convert virDomainChrSourceDef to GObject rpc: gendispatch: prepare for GObject conversion admin: convert virAdmServer to GObject admin: convert virAdmClient to GObject datatypes: convert virDomainCheckpoint to GObject datatypes: convert virDomainSnapshot to GObject datatypes: convert virNWFilter to GObject datatypes: convert virNWFilterBinding to GObject datatypes: convert virNetwork to GObject datatypes: convert virNetworkPort to GObject datatypes: convert virInterface to GObject datatypes: convert virStoragePool to GObject datatypes: convert virStorageVol to GObject datatypes: convert virNodeDevice to GObject datatypes: convert virSecret to GObject datatypes: convert virStream to GObject datatypes: convert virDomain to GObject rpc: gendispatch: use g_autoptr where possible conf: convert virNetworkXMLOption to GObject lxc: convert virLXCDriverConfig to GObject libxl: convert libxlDriverConfig to GObject hypervisor: convert virHostdevManager to GObject libxl: convert libxlMigrationDstArgs to GObject qemu: convert qemuBlockJobData to GObject qemu: convert virQEMUDriverConfig to GObject conf: convert virDomain*Private to GObject conf: convert virSaveCookie to GObject util: convert virStorageSource to GObject src/admin/admin_remote.c | 9 +- src/admin/libvirt-admin.c | 4 +- src/admin/libvirt_admin_private.syms | 4 +- src/bhyve/bhyve_capabilities.c | 19 +- src/bhyve/bhyve_conf.c | 36 +- src/bhyve/bhyve_driver.c | 33 +- src/bhyve/bhyve_monitor.c | 48 +- src/bhyve/bhyve_monitor.h | 3 +- src/bhyve/bhyve_utils.h | 11 +- src/conf/capabilities.c | 51 +- src/conf/capabilities.h | 6 +- src/conf/domain_capabilities.c | 48 +- src/conf/domain_capabilities.h | 10 +- src/conf/domain_conf.c | 129 ++-- src/conf/domain_conf.h | 36 +- src/conf/domain_event.c | 58 +- src/conf/network_conf.c | 24 +- src/conf/network_conf.h | 12 +- src/conf/network_event.c | 7 +- src/conf/node_device_event.c | 11 +- src/conf/node_device_util.c | 4 +- src/conf/secret_event.c | 15 +- src/conf/snapshot_conf.c | 2 +- src/conf/snapshot_conf.h | 2 +- src/conf/storage_capabilities.c | 4 +- src/conf/storage_event.c | 15 +- src/conf/virchrdev.c | 4 +- src/conf/virconftypes.h | 3 +- src/conf/virdomaincheckpointobjlist.c | 7 +- src/conf/virdomainsnapshotobjlist.c | 7 +- src/conf/virinterfaceobj.c | 5 +- src/conf/virnetworkobj.c | 10 +- src/conf/virnodedeviceobj.c | 3 +- src/conf/virnwfilterbindingobjlist.c | 2 +- src/conf/virnwfilterobj.c | 7 +- src/conf/virsavecookie.c | 10 +- src/conf/virsavecookie.h | 14 +- src/conf/virsecretobj.c | 2 +- src/conf/virstorageobj.c | 4 +- src/datatypes.c | 977 ++++++++++++++++-------- src/datatypes.h | 219 +++--- src/esx/esx_driver.c | 33 +- src/hyperv/hyperv_driver.c | 8 +- src/hypervisor/virhostdev.c | 49 +- src/hypervisor/virhostdev.h | 13 +- src/interface/interface_backend_netcf.c | 7 +- src/interface/interface_backend_udev.c | 8 +- src/libvirt-domain-checkpoint.c | 7 +- src/libvirt-domain-snapshot.c | 7 +- src/libvirt-domain.c | 6 +- src/libvirt-interface.c | 6 +- src/libvirt-network.c | 14 +- src/libvirt-nodedev.c | 6 +- src/libvirt-nwfilter.c | 14 +- src/libvirt-secret.c | 7 +- src/libvirt-storage.c | 12 +- src/libvirt-stream.c | 7 +- src/libvirt_private.syms | 35 +- src/libxl/libxl_capabilities.c | 19 +- src/libxl/libxl_conf.c | 65 +- src/libxl/libxl_conf.h | 12 +- src/libxl/libxl_driver.c | 175 ++--- src/libxl/libxl_migration.c | 103 +-- src/libxl/xen_common.c | 3 +- src/locking/lock_daemon.c | 28 +- src/locking/lock_driver_lockd.c | 19 +- src/locking/sanlock_helper.c | 5 +- src/logging/log_daemon.c | 28 +- src/logging/log_manager.c | 15 +- src/lxc/lxc_conf.c | 46 +- src/lxc/lxc_conf.h | 10 +- src/lxc/lxc_controller.c | 20 +- src/lxc/lxc_driver.c | 98 +-- src/lxc/lxc_monitor.c | 2 +- src/lxc/lxc_process.c | 35 +- src/network/bridge_driver.c | 24 +- src/nwfilter/nwfilter_driver.c | 3 +- src/openvz/openvz_conf.c | 8 +- src/qemu/qemu_blockjob.c | 130 ++-- src/qemu/qemu_blockjob.h | 15 +- src/qemu/qemu_capabilities.c | 176 +++-- src/qemu/qemu_capabilities.h | 9 +- src/qemu/qemu_conf.c | 42 +- src/qemu/qemu_conf.h | 19 +- src/qemu/qemu_domain.c | 441 +++++------ src/qemu/qemu_domain.h | 157 ++-- src/qemu/qemu_driver.c | 34 +- src/qemu/qemu_hotplug.c | 3 +- src/qemu/qemu_migration.c | 34 +- src/qemu/qemu_process.c | 20 +- src/qemu/qemu_virtiofs.c | 12 +- src/remote/remote_daemon.c | 52 +- src/remote/remote_daemon_dispatch.c | 188 ++--- src/remote/remote_daemon_stream.c | 6 +- src/remote/remote_driver.c | 178 ++--- src/rpc/gendispatch.pl | 90 ++- src/rpc/virnetclient.c | 7 +- src/rpc/virnetclientprogram.c | 29 +- src/rpc/virnetclientprogram.h | 10 +- src/rpc/virnetclientstream.c | 4 +- src/rpc/virnetserver.c | 21 +- src/rpc/virnetserverprogram.c | 30 +- src/rpc/virnetserverprogram.h | 17 +- src/rpc/virnetserverservice.c | 103 ++- src/security/virt-aa-helper.c | 9 +- src/storage/storage_backend.c | 3 +- src/storage/storage_driver.c | 7 +- src/storage/storage_util.c | 4 +- src/test/test_driver.c | 33 +- src/util/virdnsmasq.c | 56 +- src/util/virdnsmasq.h | 6 +- src/util/virfdstream.c | 4 +- src/util/virfilecache.c | 43 +- src/util/virfilecache.h | 12 +- src/util/virobject.c | 24 + src/util/virobject.h | 4 + src/util/virresctrl.c | 164 ++-- src/util/virresctrl.h | 15 +- src/util/virsecret.c | 3 +- src/util/virstoragefile.c | 45 +- src/util/virstoragefile.h | 9 +- src/vbox/vbox_common.c | 19 +- src/vmware/vmware_conf.c | 13 +- src/vz/vz_driver.c | 33 +- src/vz/vz_sdk.c | 22 +- tests/bhyveargv2xmltest.c | 4 +- tests/bhyvexml2argvtest.c | 4 +- tests/bhyvexml2xmltest.c | 4 +- tests/cputest.c | 48 +- tests/domaincapstest.c | 5 +- tests/domainconftest.c | 4 +- tests/genericxml2xmltest.c | 4 +- tests/networkxml2conftest.c | 10 +- tests/networkxml2xmltest.c | 3 +- tests/openvzutilstest.c | 4 +- tests/qemublocktest.c | 3 +- tests/qemucapabilitiestest.c | 9 +- tests/qemucaps2xmltest.c | 29 +- tests/qemucapsprobe.c | 4 +- tests/qemuhotplugtest.c | 6 +- tests/qemumemlocktest.c | 3 +- tests/testutils.c | 4 +- tests/testutilslxc.c | 22 +- tests/testutilsqemu.c | 31 +- tests/testutilsxen.c | 7 +- tests/vircaps2xmltest.c | 6 +- tests/vircapstest.c | 14 +- tests/virfilecachetest.c | 69 +- tests/virnetdaemontest.c | 4 +- tests/virresctrltest.c | 6 +- tests/vmx2xmltest.c | 10 +- tests/xml2vmxtest.c | 12 +- 152 files changed, 2804 insertions(+), 2712 deletions(-) -- 2.25.3

This patch changes virResctrlInfo, virResctrlAlloc and virResctrlMonitor from virObject to GObject. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/capabilities.c | 2 +- src/conf/domain_conf.c | 19 ++--- src/libvirt_private.syms | 3 + src/util/virresctrl.c | 164 ++++++++++++++++++++------------------- src/util/virresctrl.h | 15 ++-- tests/virresctrltest.c | 3 +- 6 files changed, 107 insertions(+), 99 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 99b69aebb5..0741d76731 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -266,7 +266,7 @@ virCapsDispose(void *object) VIR_FREE(caps->host.netprefix); VIR_FREE(caps->host.pagesSize); virCPUDefFree(caps->host.cpu); - virObjectUnref(caps->host.resctrl); + g_clear_object(&caps->host.resctrl); } /** diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 8700d56761..674cfa408e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -3304,7 +3304,7 @@ virDomainResctrlMonDefFree(virDomainResctrlMonDefPtr domresmon) return; virBitmapFree(domresmon->vcpus); - virObjectUnref(domresmon->instance); + g_clear_object(&domresmon->instance); VIR_FREE(domresmon); } @@ -3320,7 +3320,7 @@ virDomainResctrlDefFree(virDomainResctrlDefPtr resctrl) for (i = 0; i < resctrl->nmonitors; i++) virDomainResctrlMonDefFree(resctrl->monitors[i]); - virObjectUnref(resctrl->alloc); + g_clear_object(&resctrl->alloc); virBitmapFree(resctrl->vcpus); VIR_FREE(resctrl->monitors); VIR_FREE(resctrl); @@ -19950,11 +19950,6 @@ virDomainResctrlMonDefParse(virDomainDefPtr def, domresmon->tag = tag; domresmon->instance = virResctrlMonitorNew(); - if (!domresmon->instance) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not create monitor")); - goto cleanup; - } if (tag == VIR_RESCTRL_MONITOR_TYPE_CACHE) { tmp = virXMLPropString(nodes[i], "level"); @@ -20064,7 +20059,7 @@ virDomainResctrlNew(xmlNodePtr node, goto cleanup; } - resctrl->alloc = virObjectRef(alloc); + resctrl->alloc = g_object_ref(alloc); ret = g_steal_pointer(&resctrl); cleanup: @@ -20111,8 +20106,7 @@ virDomainCachetuneDefParse(virDomainDefPtr def, return -1; } - if (!(alloc = virResctrlAllocNew())) - return -1; + alloc = virResctrlAllocNew(); for (i = 0; i < n; i++) { if (virDomainCachetuneDefParseCache(ctxt, nodes[i], alloc) < 0) @@ -20297,10 +20291,9 @@ virDomainMemorytuneDefParse(virDomainDefPtr def, return -1; if (resctrl) { - alloc = virObjectRef(resctrl->alloc); + alloc = g_object_ref(resctrl->alloc); } else { - if (!(alloc = virResctrlAllocNew())) - return -1; + alloc = virResctrlAllocNew(); } /* First, parse <memorytune/node> element if any <node> element exists */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 182a570382..7bba191b03 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2916,6 +2916,9 @@ virRandomInt; # util/virresctrl.h +vir_resctrl_alloc_get_type; +vir_resctrl_info_get_type; +vir_resctrl_monitor_get_type; virCacheKernelTypeFromString; virCacheKernelTypeToString; virCacheTypeFromString; diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index c537d606cc..ce11a24a54 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -112,12 +112,6 @@ typedef struct _virResctrlAllocMemBW virResctrlAllocMemBW; typedef virResctrlAllocMemBW *virResctrlAllocMemBWPtr; -/* Class definitions and initializations */ -static virClassPtr virResctrlInfoClass; -static virClassPtr virResctrlAllocClass; -static virClassPtr virResctrlMonitorClass; - - /* virResctrlInfo */ struct _virResctrlInfoPerType { /* Kernel-provided information */ @@ -182,7 +176,7 @@ struct _virResctrlInfoMongrp { }; struct _virResctrlInfo { - virObject parent; + GObject parent; virResctrlInfoPerLevelPtr *levels; size_t nlevels; @@ -192,14 +186,17 @@ struct _virResctrlInfo { virResctrlInfoMongrpPtr monitor_info; }; +/* Class definitions and initializations */ +G_DEFINE_TYPE(virResctrlInfo, vir_resctrl_info, G_TYPE_OBJECT); + static void -virResctrlInfoDispose(void *obj) +virResctrlInfoFinalize(GObject *obj) { size_t i = 0; size_t j = 0; - virResctrlInfoPtr resctrl = obj; + virResctrlInfoPtr resctrl = VIR_RESCTRL_INFO(obj); for (i = 0; i < resctrl->nlevels; i++) { virResctrlInfoPerLevelPtr level = resctrl->levels[i]; @@ -221,6 +218,8 @@ virResctrlInfoDispose(void *obj) VIR_FREE(resctrl->membw_info); VIR_FREE(resctrl->levels); VIR_FREE(resctrl->monitor_info); + + G_OBJECT_CLASS(vir_resctrl_info_parent_class)->finalize(obj); } @@ -334,7 +333,7 @@ struct _virResctrlAllocMemBW { }; struct _virResctrlAlloc { - virObject parent; + GObject parent; virResctrlAllocPerLevelPtr *levels; size_t nlevels; @@ -355,7 +354,7 @@ struct _virResctrlAlloc { * memory bandwidth. */ struct _virResctrlMonitor { - virObject parent; + GObject parent; /* Each virResctrlMonitor is associated with one specific allocation, * either the root directory allocation under /sys/fs/resctrl or a @@ -372,15 +371,20 @@ struct _virResctrlMonitor { char *path; }; +/* Class definitions and initializations */ +G_DEFINE_TYPE(virResctrlAlloc, vir_resctrl_alloc, G_TYPE_OBJECT); +G_DEFINE_TYPE(virResctrlMonitor, vir_resctrl_monitor, G_TYPE_OBJECT); + + static void -virResctrlAllocDispose(void *obj) +virResctrlAllocFinalize(GObject *obj) { size_t i = 0; size_t j = 0; size_t k = 0; - virResctrlAllocPtr alloc = obj; + virResctrlAllocPtr alloc = VIR_RESCTRL_ALLOC(obj); for (i = 0; i < alloc->nlevels; i++) { virResctrlAllocPerLevelPtr level = alloc->levels[i]; @@ -419,38 +423,71 @@ virResctrlAllocDispose(void *obj) VIR_FREE(alloc->id); VIR_FREE(alloc->path); VIR_FREE(alloc->levels); + + G_OBJECT_CLASS(vir_resctrl_alloc_parent_class)->finalize(obj); } static void -virResctrlMonitorDispose(void *obj) +virResctrlMonitorDispose(GObject *obj) { - virResctrlMonitorPtr monitor = obj; + virResctrlMonitorPtr monitor = VIR_RESCTRL_MONITOR(obj); + + g_clear_object(&monitor->alloc); + + G_OBJECT_CLASS(vir_resctrl_alloc_parent_class)->dispose(obj); +} + +static void +virResctrlMonitorFinalize(GObject *obj) +{ + virResctrlMonitorPtr monitor = VIR_RESCTRL_MONITOR(obj); - virObjectUnref(monitor->alloc); VIR_FREE(monitor->id); VIR_FREE(monitor->path); + + G_OBJECT_CLASS(vir_resctrl_monitor_parent_class)->finalize(obj); } +static void +vir_resctrl_info_init(virResctrlInfo *resctrlinfo G_GNUC_UNUSED) +{ +} -/* Global initialization for classes */ -static int -virResctrlOnceInit(void) +static void +vir_resctrl_info_class_init(virResctrlInfoClass *klass) { - if (!VIR_CLASS_NEW(virResctrlInfo, virClassForObject())) - return -1; + GObjectClass *obj = G_OBJECT_CLASS(klass); - if (!VIR_CLASS_NEW(virResctrlAlloc, virClassForObject())) - return -1; + obj->finalize = virResctrlInfoFinalize; +} - if (!VIR_CLASS_NEW(virResctrlMonitor, virClassForObject())) - return -1; +static void +vir_resctrl_alloc_init(virResctrlAlloc *resctrlalloc G_GNUC_UNUSED) +{ +} - return 0; +static void +vir_resctrl_alloc_class_init(virResctrlAllocClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virResctrlAllocFinalize; +} + +static void +vir_resctrl_monitor_init(virResctrlMonitor *resctrlmon G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(virResctrl); +static void +vir_resctrl_monitor_class_init(virResctrlMonitorClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + obj->dispose = virResctrlMonitorDispose; + obj->finalize = virResctrlMonitorFinalize; +} /* Common functions */ static int @@ -793,21 +830,13 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl) virResctrlInfoPtr virResctrlInfoNew(void) { - virResctrlInfoPtr ret = NULL; - - if (virResctrlInitialize() < 0) - return NULL; - - ret = virObjectNew(virResctrlInfoClass); - if (!ret) - return NULL; + g_autoptr(virResctrlInfo) ret = VIR_RESCTRL_INFO(g_object_new(VIR_TYPE_RESCTRL_INFO, NULL)); if (virResctrlGetInfo(ret) < 0) { - virObjectUnref(ret); return NULL; } - return ret; + return g_steal_pointer(&ret); } @@ -1034,10 +1063,7 @@ virResctrlInfoGetMonitorPrefix(virResctrlInfoPtr resctrl, virResctrlAllocPtr virResctrlAllocNew(void) { - if (virResctrlInitialize() < 0) - return NULL; - - return virObjectNew(virResctrlAllocClass); + return VIR_RESCTRL_ALLOC(g_object_new(VIR_TYPE_RESCTRL_ALLOC, NULL)); } @@ -1769,8 +1795,7 @@ virResctrlAllocGetGroup(virResctrlInfoPtr resctrl, error: VIR_FREE(schemata); - virObjectUnref(*alloc); - *alloc = NULL; + g_clear_object(alloc); return -1; } @@ -1779,9 +1804,7 @@ static virResctrlAllocPtr virResctrlAllocGetDefault(virResctrlInfoPtr resctrl) { virResctrlAllocPtr ret = NULL; - int rv = virResctrlAllocGetGroup(resctrl, ".", &ret); - - if (rv == -2) { + if (virResctrlAllocGetGroup(resctrl, ".", &ret) == -2) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not read schemata file for the default group")); } @@ -1836,9 +1859,6 @@ virResctrlAllocNewFromInfo(virResctrlInfoPtr info) virResctrlAllocPtr ret = virResctrlAllocNew(); virBitmapPtr mask = NULL; - if (!ret) - return NULL; - for (i = 0; i < info->nlevels; i++) { virResctrlInfoPerLevelPtr i_level = info->levels[i]; @@ -1884,8 +1904,7 @@ virResctrlAllocNewFromInfo(virResctrlInfoPtr info) virBitmapFree(mask); return ret; error: - virObjectUnref(ret); - ret = NULL; + g_clear_object(&ret); goto cleanup; } @@ -1907,7 +1926,7 @@ virResctrlAllocPtr virResctrlAllocGetUnused(virResctrlInfoPtr resctrl) { virResctrlAllocPtr ret = NULL; - virResctrlAllocPtr alloc = NULL; + g_autoptr(virResctrlAlloc) alloc = NULL; struct dirent *ent = NULL; DIR *dirp = NULL; int rv = -1; @@ -1927,7 +1946,7 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl) goto error; virResctrlAllocSubtract(ret, alloc); - virObjectUnref(alloc); + g_clear_object(&alloc); if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0) goto error; @@ -1948,20 +1967,17 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl) } virResctrlAllocSubtract(ret, alloc); - virObjectUnref(alloc); - alloc = NULL; + g_clear_object(&alloc); } if (rv < 0) goto error; cleanup: - virObjectUnref(alloc); VIR_DIR_CLOSE(dirp); return ret; error: - virObjectUnref(ret); - ret = NULL; + g_clear_object(&ret); goto cleanup; } @@ -2229,10 +2245,9 @@ static int virResctrlAllocAssign(virResctrlInfoPtr resctrl, virResctrlAllocPtr alloc) { - int ret = -1; unsigned int level = 0; - virResctrlAllocPtr alloc_free = NULL; - virResctrlAllocPtr alloc_default = NULL; + g_autoptr(virResctrlAlloc) alloc_free = NULL; + g_autoptr(virResctrlAlloc) alloc_default = NULL; alloc_free = virResctrlAllocGetUnused(resctrl); if (!alloc_free) @@ -2240,16 +2255,16 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl, alloc_default = virResctrlAllocGetDefault(resctrl); if (!alloc_default) - goto cleanup; + return -1; if (virResctrlAllocMemoryBandwidth(resctrl, alloc) < 0) - goto cleanup; + return -1; if (virResctrlAllocCopyMasks(alloc, alloc_default) < 0) - goto cleanup; + return -1; if (virResctrlAllocCopyMemBW(alloc, alloc_default) < 0) - goto cleanup; + return -1; for (level = 0; level < alloc->nlevels; level++) { virResctrlAllocPerLevelPtr a_level = alloc->levels[level]; @@ -2266,7 +2281,7 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Cache level %d does not support tuning"), level); - goto cleanup; + return -1; } for (type = 0; type < VIR_CACHE_TYPE_LAST; type++) { @@ -2282,7 +2297,7 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl, _("Cache level %d does not support tuning for " "scope type '%s'"), level, virCacheTypeToString(type)); - goto cleanup; + return -1; } for (cache = 0; cache < a_type->nsizes; cache++) { @@ -2290,16 +2305,12 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl, virResctrlInfoPerTypePtr i_type = i_level->types[type]; if (virResctrlAllocFindUnused(alloc, i_type, f_type, level, type, cache) < 0) - goto cleanup; + return -1; } } } - ret = 0; - cleanup: - virObjectUnref(alloc_free); - virObjectUnref(alloc_default); - return ret; + return 0; } @@ -2506,10 +2517,7 @@ virResctrlAllocRemove(virResctrlAllocPtr alloc) virResctrlMonitorPtr virResctrlMonitorNew(void) { - if (virResctrlInitialize() < 0) - return NULL; - - return virObjectNew(virResctrlMonitorClass); + return VIR_RESCTRL_MONITOR(g_object_new(VIR_TYPE_RESCTRL_MONITOR, NULL)); } @@ -2623,7 +2631,7 @@ void virResctrlMonitorSetAlloc(virResctrlMonitorPtr monitor, virResctrlAllocPtr alloc) { - monitor->alloc = virObjectRef(alloc); + monitor->alloc = g_object_ref(alloc); } diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h index dcd9ca2bb2..6724d904d1 100644 --- a/src/util/virresctrl.h +++ b/src/util/virresctrl.h @@ -19,8 +19,8 @@ #pragma once #include "internal.h" +#include <glib-object.h> -#include "virobject.h" #include "virbitmap.h" #include "virenum.h" @@ -93,7 +93,9 @@ struct _virResctrlInfoMon { unsigned int cache_level; }; -typedef struct _virResctrlInfo virResctrlInfo; +#define VIR_TYPE_RESCTRL_INFO vir_resctrl_info_get_type() +G_DECLARE_FINAL_TYPE(virResctrlInfo, vir_resctrl_info, VIR, RESCTRL_INFO, GObject); + typedef virResctrlInfo *virResctrlInfoPtr; virResctrlInfoPtr @@ -111,10 +113,11 @@ virResctrlInfoGetMemoryBandwidth(virResctrlInfoPtr resctrl, unsigned int level, virResctrlInfoMemBWPerNodePtr control); /* Alloc-related things */ -typedef struct _virResctrlAlloc virResctrlAlloc; +#define VIR_TYPE_RESCTRL_ALLOC vir_resctrl_alloc_get_type() +G_DECLARE_FINAL_TYPE(virResctrlAlloc, vir_resctrl_alloc, VIR, RESCTRL_ALLOC, GObject); + typedef virResctrlAlloc *virResctrlAllocPtr; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virResctrlAlloc, virObjectUnref); typedef int virResctrlAllocForeachCacheCallback(unsigned int level, @@ -190,7 +193,9 @@ virResctrlInfoGetMonitorPrefix(virResctrlInfoPtr resctrl, /* Monitor-related things */ -typedef struct _virResctrlMonitor virResctrlMonitor; +#define VIR_TYPE_RESCTRL_MONITOR vir_resctrl_monitor_get_type() +G_DECLARE_FINAL_TYPE(virResctrlMonitor, vir_resctrl_monitor, VIR, RESCTRL_MONITOR, GObject); + typedef virResctrlMonitor *virResctrlMonitorPtr; typedef struct _virResctrlMonitorStats virResctrlMonitorStats; diff --git a/tests/virresctrltest.c b/tests/virresctrltest.c index bb6d0fe48e..91baef506d 100644 --- a/tests/virresctrltest.c +++ b/tests/virresctrltest.c @@ -21,7 +21,7 @@ test_virResctrlGetUnused(const void *opaque) char *system_dir = NULL; char *resctrl_dir = NULL; int ret = -1; - virResctrlAllocPtr alloc = NULL; + g_autoptr(virResctrlAlloc) alloc = NULL; char *schemata_str = NULL; char *schemata_file; virCapsPtr caps = NULL; @@ -66,7 +66,6 @@ test_virResctrlGetUnused(const void *opaque) ret = 0; cleanup: virObjectUnref(caps); - virObjectUnref(alloc); VIR_FREE(system_dir); VIR_FREE(resctrl_dir); VIR_FREE(schemata_str); -- 2.25.3

On Tue, 2020-04-21 at 15:48 +0200, Rafael Fonseca wrote:
@@ -793,21 +830,13 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl) virResctrlInfoPtr virResctrlInfoNew(void) { - virResctrlInfoPtr ret = NULL; - - if (virResctrlInitialize() < 0) - return NULL; - - ret = virObjectNew(virResctrlInfoClass); - if (!ret) - return NULL; + g_autoptr(virResctrlInfo) ret = VIR_RESCTRL_INFO(g_object_new(VIR_TYPE_RESCTRL_INFO, NULL));
if (virResctrlGetInfo(ret) < 0) { - virObjectUnref(ret); return NULL; }
- return ret; + return g_steal_pointer(&ret); }
(FYI: Glib provides a base class GInitiable (and an async variant) for an object that requires initialization immediately after allocation and returns NULL if said initialization fails. Not sure that it would gain us anything here, but just thought I'd mention it.) Jonathon

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_capabilities.c | 16 ++++------- src/bhyve/bhyve_driver.c | 29 +++++++------------ src/conf/capabilities.c | 51 +++++++++++++++++++-------------- src/conf/capabilities.h | 6 ++-- src/conf/storage_capabilities.c | 4 +-- src/conf/virconftypes.h | 3 +- src/esx/esx_driver.c | 24 ++++++---------- src/libvirt_private.syms | 1 + src/libxl/libxl_capabilities.c | 19 +++++------- src/libxl/libxl_conf.c | 2 +- src/lxc/lxc_conf.c | 15 ++++------ src/lxc/lxc_controller.c | 2 +- src/lxc/lxc_driver.c | 44 ++++++++++------------------ src/lxc/lxc_process.c | 2 +- src/openvz/openvz_conf.c | 6 ++-- src/qemu/qemu_capabilities.c | 14 +++------ src/qemu/qemu_conf.c | 6 ++-- src/qemu/qemu_driver.c | 2 +- src/security/virt-aa-helper.c | 7 ++--- src/storage/storage_backend.c | 3 +- src/test/test_driver.c | 27 +++++++---------- src/vbox/vbox_common.c | 6 ++-- src/vmware/vmware_conf.c | 11 +++---- src/vz/vz_driver.c | 26 +++++++---------- tests/bhyveargv2xmltest.c | 2 +- tests/bhyvexml2argvtest.c | 2 +- tests/bhyvexml2xmltest.c | 2 +- tests/domainconftest.c | 2 +- tests/genericxml2xmltest.c | 2 +- tests/openvzutilstest.c | 2 +- tests/qemucaps2xmltest.c | 13 +++------ tests/qemuhotplugtest.c | 6 ++-- tests/testutils.c | 4 +-- tests/testutilslxc.c | 20 ++++++------- tests/testutilsqemu.c | 10 +++---- tests/testutilsxen.c | 3 +- tests/vircaps2xmltest.c | 6 +--- tests/vircapstest.c | 14 +++------ tests/virresctrltest.c | 3 +- tests/vmx2xmltest.c | 8 ++---- tests/xml2vmxtest.c | 8 ++---- 41 files changed, 167 insertions(+), 266 deletions(-) diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c index fb8829d571..66442e50ba 100644 --- a/src/bhyve/bhyve_capabilities.c +++ b/src/bhyve/bhyve_capabilities.c @@ -43,31 +43,25 @@ VIR_LOG_INIT("bhyve.bhyve_capabilities"); virCapsPtr virBhyveCapsBuild(void) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - return NULL; + caps = virCapabilitiesNew(virArchFromHost(), false, false); if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64, "bhyve", NULL, 0, NULL)) == NULL) - goto error; + return NULL; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_BHYVE, NULL, NULL, 0, NULL) == NULL) - goto error; + return NULL; if (!(caps->host.cpu = virCPUProbeHost(caps->host.arch))) VIR_WARN("Failed to get host CPU"); - return caps; - - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } int diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index b6204c7fb9..bbaf6ea1f5 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -126,22 +126,21 @@ bhyveAutostartDomains(bhyveConnPtr driver) * Get a reference to the virCapsPtr instance for the * driver. * - * The caller must release the reference with virObjetUnref + * The caller must release the reference with g_object_unref * * Returns: a reference to a virCapsPtr instance or NULL */ virCapsPtr ATTRIBUTE_NONNULL(1) bhyveDriverGetCapabilities(bhyveConnPtr driver) { - return virObjectRef(driver->caps); + return g_object_ref(driver->caps); } static char * bhyveConnectGetCapabilities(virConnectPtr conn) { bhyveConnPtr privconn = conn->privateData; - virCapsPtr caps; - char *xml = NULL; + g_autoptr(virCaps) caps = NULL; if (virConnectGetCapabilitiesEnsureACL(conn) < 0) return NULL; @@ -149,15 +148,10 @@ bhyveConnectGetCapabilities(virConnectPtr conn) if (!(caps = bhyveDriverGetCapabilities(privconn))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to get Capabilities")); - goto cleanup; + return NULL; } - if (!(xml = virCapabilitiesFormatXML(caps))) - goto cleanup; - - cleanup: - virObjectUnref(caps); - return xml; + return virCapabilitiesFormatXML(caps); } static virDomainObjPtr @@ -508,7 +502,7 @@ bhyveDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flag virDomainDefPtr oldDef = NULL; virDomainObjPtr vm = NULL; virObjectEventPtr event = NULL; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL); @@ -555,7 +549,6 @@ bhyveDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flag dom = virGetDomain(conn, vm->def->name, vm->def->uuid, vm->def->id); cleanup: - virObjectUnref(caps); virDomainDefFree(def); virDomainDefFree(oldDef); virDomainObjEndAPI(&vm); @@ -1188,7 +1181,7 @@ bhyveStateCleanup(void) return -1; virObjectUnref(bhyve_driver->domains); - virObjectUnref(bhyve_driver->caps); + g_clear_object(&bhyve_driver->caps); virObjectUnref(bhyve_driver->xmlopt); virSysinfoDefFree(bhyve_driver->hostsysinfo); virObjectUnref(bhyve_driver->closeCallbacks); @@ -1440,19 +1433,19 @@ bhyveConnectCompareCPU(virConnectPtr conn, { bhyveConnPtr driver = conn->privateData; int ret = VIR_CPU_COMPARE_ERROR; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; bool failIncompatible; virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE, VIR_CPU_COMPARE_ERROR); if (virConnectCompareCPUEnsureACL(conn) < 0) - goto cleanup; + return ret; failIncompatible = !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE); if (!(caps = bhyveDriverGetCapabilities(driver))) - goto cleanup; + return ret; if (!caps->host.cpu || !caps->host.cpu->model) { @@ -1468,8 +1461,6 @@ bhyveConnectCompareCPU(virConnectPtr conn, xmlDesc, failIncompatible); } - cleanup: - virObjectUnref(caps); return ret; } diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 0741d76731..7da67a6ce3 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -54,18 +54,8 @@ VIR_ENUM_IMPL(virCapsHostPMTarget, "suspend_mem", "suspend_disk", "suspend_hybrid", ); -static virClassPtr virCapsClass; -static void virCapsDispose(void *obj); +G_DEFINE_TYPE(virCaps, vir_caps, G_TYPE_OBJECT); -static int virCapabilitiesOnceInit(void) -{ - if (!VIR_CLASS_NEW(virCaps, virClassForObject())) - return -1; - - return 0; -} - -VIR_ONCE_GLOBAL_INIT(virCapabilities); /** * virCapabilitiesNew: @@ -80,13 +70,7 @@ virCapabilitiesNew(virArch hostarch, bool offlineMigrate, bool liveMigrate) { - virCapsPtr caps; - - if (virCapabilitiesInitialize() < 0) - return NULL; - - if (!(caps = virObjectNew(virCapsClass))) - return NULL; + virCapsPtr caps = VIR_CAPS(g_object_new(VIR_TYPE_CAPS, NULL)); caps->host.arch = hostarch; caps->host.offlineMigrate = offlineMigrate; @@ -225,9 +209,19 @@ virCapabilitiesClearSecModel(virCapsHostSecModelPtr secmodel) } static void -virCapsDispose(void *object) +virCapsDispose(GObject *object) +{ + virCapsPtr caps = VIR_CAPS(object); + + g_clear_object(&caps->host.resctrl); + + G_OBJECT_CLASS(vir_caps_parent_class)->dispose(object); +} + +static void +virCapsFinalize(GObject *object) { - virCapsPtr caps = object; + virCapsPtr caps = VIR_CAPS(object); size_t i; for (i = 0; i < caps->npools; i++) @@ -266,7 +260,22 @@ virCapsDispose(void *object) VIR_FREE(caps->host.netprefix); VIR_FREE(caps->host.pagesSize); virCPUDefFree(caps->host.cpu); - g_clear_object(&caps->host.resctrl); + + G_OBJECT_CLASS(vir_caps_parent_class)->finalize(object); +} + +static void +vir_caps_init(virCaps *caps G_GNUC_UNUSED) +{ +} + +static void +vir_caps_class_init(virCapsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virCapsDispose; + obj->finalize = virCapsFinalize; } /** diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h index e2581fac8b..b563f79148 100644 --- a/src/conf/capabilities.h +++ b/src/conf/capabilities.h @@ -200,7 +200,7 @@ struct _virCapsStoragePool { struct _virCaps { - virObject parent; + GObject parent; virCapsHost host; size_t nguests; @@ -212,8 +212,8 @@ struct _virCaps { virCapsStoragePoolPtr *pools; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCaps, virObjectUnref); - +#define VIR_TYPE_CAPS vir_caps_get_type() +G_DECLARE_FINAL_TYPE(virCaps, vir_caps, VIR, CAPS, GObject); struct _virCapsDomainData { int ostype; diff --git a/src/conf/storage_capabilities.c b/src/conf/storage_capabilities.c index 1a3417f90b..a2f8063ecb 100644 --- a/src/conf/storage_capabilities.c +++ b/src/conf/storage_capabilities.c @@ -40,7 +40,7 @@ virStoragePoolCapsDispose(void *obj) virStoragePoolCapsPtr caps = obj; VIR_DEBUG("obj=%p", caps); - virObjectUnref(caps->driverCaps); + g_clear_object(&caps->driverCaps); } @@ -66,7 +66,7 @@ virStoragePoolCapsNew(virCapsPtr driverCaps) if (!(caps = virObjectLockableNew(virStoragePoolCapsClass))) return NULL; - caps->driverCaps = virObjectRef(driverCaps); + caps->driverCaps = g_object_ref(driverCaps); return caps; } diff --git a/src/conf/virconftypes.h b/src/conf/virconftypes.h index 1c62cde251..96df0f9f6f 100644 --- a/src/conf/virconftypes.h +++ b/src/conf/virconftypes.h @@ -27,8 +27,7 @@ typedef struct _virBlkioDevice virBlkioDevice; typedef virBlkioDevice *virBlkioDevicePtr; -typedef struct _virCaps virCaps; -typedef virCaps *virCapsPtr; +typedef struct _virCaps *virCapsPtr; typedef struct _virCapsDomainData virCapsDomainData; typedef virCapsDomainData *virCapsDomainDataPtr; diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 0ede65279a..39412f746a 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -69,7 +69,7 @@ esxFreePrivate(esxPrivate **priv) esxVI_Context_Free(&(*priv)->host); esxVI_Context_Free(&(*priv)->vCenter); esxUtil_FreeParsedUri(&(*priv)->parsedUri); - virObjectUnref((*priv)->caps); + g_clear_object(&(*priv)->caps); virObjectUnref((*priv)->xmlopt); VIR_FREE(*priv); } @@ -540,7 +540,7 @@ static virCapsPtr esxCapsInit(esxPrivate *priv) { esxVI_Boolean supportsLongMode = esxSupportsLongMode(priv); - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest = NULL; if (supportsLongMode == esxVI_Boolean_Undefined) @@ -552,14 +552,11 @@ esxCapsInit(esxPrivate *priv) caps = virCapabilitiesNew(VIR_ARCH_I686, true, true); } - if (!caps) - return NULL; - virCapabilitiesAddHostMigrateTransport(caps, "vpxmigr"); if (esxLookupHostSystemBiosUuid(priv, caps->host.host_uuid) < 0) - goto failure; + return NULL; /* i686 */ guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, @@ -568,10 +565,10 @@ esxCapsInit(esxPrivate *priv) NULL); if (!guest) - goto failure; + return NULL; if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0, NULL)) - goto failure; + return NULL; /* x86_64 */ if (supportsLongMode == esxVI_Boolean_True) { @@ -581,18 +578,13 @@ esxCapsInit(esxPrivate *priv) 0, NULL); if (!guest) - goto failure; + return NULL; if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0, NULL)) - goto failure; + return NULL; } - return caps; - - failure: - virObjectUnref(caps); - - return NULL; + return g_steal_pointer(&caps); } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 7bba191b03..2cde3c9aa5 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -51,6 +51,7 @@ virDomainBackupDefParseString; # conf/capabilities.h +vir_caps_get_type; virCapabilitiesAddGuest; virCapabilitiesAddGuestDomain; virCapabilitiesAddGuestFeature; diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c index beac8c8cfc..4bbe761b3d 100644 --- a/src/libxl/libxl_capabilities.c +++ b/src/libxl/libxl_capabilities.c @@ -704,29 +704,24 @@ libxlMakeDomainDeviceHostdevCaps(virDomainCapsDeviceHostdevPtr dev) virCapsPtr libxlMakeCapabilities(libxl_ctx *ctx) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; #ifdef LIBXL_HAVE_NO_SUSPEND_RESUME - if ((caps = virCapabilitiesNew(virArchFromHost(), false, false)) == NULL) + caps = virCapabilitiesNew(virArchFromHost(), false, false); #else - if ((caps = virCapabilitiesNew(virArchFromHost(), true, true)) == NULL) + caps = virCapabilitiesNew(virArchFromHost(), true, true); #endif - return NULL; if (libxlCapsInitHost(ctx, caps) < 0) - goto error; + return NULL; if (libxlCapsInitNuma(ctx, caps) < 0) - goto error; + return NULL; if (libxlCapsInitGuests(ctx, caps) < 0) - goto error; - - return caps; + return NULL; - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } /* diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index b3f67f817a..b7c119366e 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -70,7 +70,7 @@ libxlDriverConfigDispose(void *obj) { libxlDriverConfigPtr cfg = obj; - virObjectUnref(cfg->caps); + g_clear_object(&cfg->caps); libxl_ctx_free(cfg->ctx); if (cfg->logger) libxlLoggerFree(cfg->logger); diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index 8469f30b9e..6942781731 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -59,14 +59,12 @@ VIR_ONCE_GLOBAL_INIT(virLXCConfig); /* Functions */ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; virArch altArch; char *lxc_path = NULL; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - goto error; + caps = virCapabilitiesNew(virArchFromHost(), false, false); /* Some machines have problematic NUMA topology causing * unexpected failures. We don't want to break the lxc @@ -164,11 +162,10 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) VIR_INFO("No driver, not initializing security driver"); } - return caps; + return g_steal_pointer(&caps); error: VIR_FREE(lxc_path); - virObjectUnref(caps); return NULL; } @@ -180,7 +177,7 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) * driver. If @refresh is true, the capabilities will be * rebuilt first * - * The caller must release the reference with virObjetUnref + * The caller must release the reference with g_object_unref * * Returns: a reference to a virCapsPtr instance or NULL */ @@ -194,7 +191,7 @@ virCapsPtr virLXCDriverGetCapabilities(virLXCDriverPtr driver, return NULL; lxcDriverLock(driver); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); driver->caps = caps; } else { lxcDriverLock(driver); @@ -207,7 +204,7 @@ virCapsPtr virLXCDriverGetCapabilities(virLXCDriverPtr driver, } } - ret = virObjectRef(driver->caps); + ret = g_object_ref(driver->caps); lxcDriverUnlock(driver); return ret; } diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 453b435dd6..43f75d639a 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -181,7 +181,7 @@ virLXCControllerDriverFree(virLXCDriverPtr driver) if (!driver) return; virObjectUnref(driver->xmlopt); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virMutexDestroy(&driver->lock); g_free(driver); } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 851894c459..0cce0d70b7 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -197,8 +197,7 @@ static int lxcConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED) static char *lxcConnectGetCapabilities(virConnectPtr conn) { virLXCDriverPtr driver = conn->privateData; - virCapsPtr caps; - char *xml; + g_autoptr(virCaps) caps = NULL; if (virConnectGetCapabilitiesEnsureACL(conn) < 0) return NULL; @@ -206,10 +205,7 @@ static char *lxcConnectGetCapabilities(virConnectPtr conn) { if (!(caps = virLXCDriverGetCapabilities(driver, true))) return NULL; - xml = virCapabilitiesFormatXML(caps); - - virObjectUnref(caps); - return xml; + return virCapabilitiesFormatXML(caps); } @@ -405,7 +401,7 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virObjectEventPtr event = NULL; virDomainDefPtr oldDef = NULL; virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; virCheckFlags(VIR_DOMAIN_DEFINE_VALIDATE, NULL); @@ -462,7 +458,6 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virDomainDefFree(oldDef); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(caps); virObjectUnref(cfg); return dom; } @@ -940,7 +935,7 @@ static char *lxcConnectDomainXMLFromNative(virConnectPtr conn, char *xml = NULL; virDomainDefPtr def = NULL; virLXCDriverPtr driver = conn->privateData; - virCapsPtr caps = virLXCDriverGetCapabilities(driver, false); + g_autoptr(virCaps) caps = virLXCDriverGetCapabilities(driver, false); virCheckFlags(0, NULL); @@ -959,7 +954,6 @@ static char *lxcConnectDomainXMLFromNative(virConnectPtr conn, xml = virDomainDefFormat(def, driver->xmlopt, 0); cleanup: - virObjectUnref(caps); virDomainDefFree(def); return xml; } @@ -1086,7 +1080,7 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainPtr dom = NULL; virObjectEventPtr event = NULL; virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; virCheckFlags(VIR_DOMAIN_START_AUTODESTROY | @@ -1156,7 +1150,6 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefFree(def); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(caps); virObjectUnref(cfg); virNWFilterUnlockFilterUpdates(); return dom; @@ -1235,29 +1228,27 @@ static int lxcNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel) { virLXCDriverPtr driver = conn->privateData; - virCapsPtr caps = NULL; - int ret = 0; + g_autoptr(virCaps) caps = NULL; memset(secmodel, 0, sizeof(*secmodel)); if (virNodeGetSecurityModelEnsureACL(conn) < 0) - goto cleanup; + return 0; if (!(caps = virLXCDriverGetCapabilities(driver, false))) - goto cleanup; + return 0; /* we treat no driver as success, but simply return no data in *secmodel */ if (caps->host.nsecModels == 0 || caps->host.secModels[0].model == NULL) - goto cleanup; + return 0; if (virStrcpy(secmodel->model, caps->host.secModels[0].model, VIR_SECURITY_MODEL_BUFLEN) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("security model string exceeds max %d bytes"), VIR_SECURITY_MODEL_BUFLEN - 1); - ret = -1; - goto cleanup; + return -1; } if (virStrcpy(secmodel->doi, caps->host.secModels[0].doi, @@ -1265,13 +1256,10 @@ static int lxcNodeGetSecurityModel(virConnectPtr conn, virReportError(VIR_ERR_INTERNAL_ERROR, _("security DOI string exceeds max %d bytes"), VIR_SECURITY_DOI_BUFLEN-1); - ret = -1; - goto cleanup; + return -1; } - cleanup: - virObjectUnref(caps); - return ret; + return 0; } @@ -1623,7 +1611,7 @@ static int lxcStateCleanup(void) virSysinfoDefFree(lxc_driver->hostsysinfo); virObjectUnref(lxc_driver->hostdevMgr); - virObjectUnref(lxc_driver->caps); + g_clear_object(&lxc_driver->caps); virObjectUnref(lxc_driver->securityManager); virObjectUnref(lxc_driver->xmlopt); @@ -1802,7 +1790,7 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, unsigned int flags) { virLXCDriverPtr driver = dom->conn->privateData; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; size_t i; virDomainObjPtr vm = NULL; virDomainDefPtr def = NULL; @@ -1924,7 +1912,6 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, cleanup: virDomainDefFree(persistentDefCopy); virDomainObjEndAPI(&vm); - virObjectUnref(caps); virObjectUnref(cfg); return ret; } @@ -4477,7 +4464,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, unsigned int flags) { virLXCDriverPtr driver = dom->conn->privateData; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; virDomainObjPtr vm = NULL; virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; @@ -4563,7 +4550,6 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(caps); virObjectUnref(cfg); return ret; } diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 5199f3806e..6a3c9b7da3 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -1565,7 +1565,7 @@ int virLXCProcessStart(virConnectPtr conn, VIR_FREE(pidfile); VIR_FREE(logfile); virObjectUnref(cfg); - virObjectUnref(caps); + g_clear_object(&caps); virErrorRestore(&err); diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index 78547b8b28..0d11d71dfd 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -151,9 +151,7 @@ virCapsPtr openvzCapsInit(void) g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - return NULL; + caps = virCapabilitiesNew(virArchFromHost(), false, false); if (!(caps->host.numa = virCapabilitiesHostNUMANewHost())) return NULL; @@ -483,7 +481,7 @@ openvzFreeDriver(struct openvz_driver *driver) virObjectUnref(driver->xmlopt); virObjectUnref(driver->domains); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); VIR_FREE(driver); } diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index fe311048d4..840373bae9 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -1067,13 +1067,11 @@ virQEMUCapsProbeHostCPU(virArch hostArch, virCapsPtr virQEMUCapsInit(virFileCachePtr cache) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; size_t i; virArch hostarch = virArchFromHost(); - if ((caps = virCapabilitiesNew(hostarch, - true, true)) == NULL) - goto error; + caps = virCapabilitiesNew(hostarch, true, true); if (virCapabilitiesInitCaches(caps) < 0) VIR_WARN("Failed to get host CPU cache info"); @@ -1101,13 +1099,9 @@ virQEMUCapsInit(virFileCachePtr cache) if (virQEMUCapsInitGuest(caps, cache, hostarch, i) < 0) - goto error; - - return caps; + return NULL; - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index c59824006c..c29b543b16 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1354,7 +1354,7 @@ virCapsPtr virQEMUDriverCreateCapabilities(virQEMUDriverPtr driver) * driver. If @refresh is true, the capabilities will be * rebuilt first * - * The caller must release the reference with virObjetUnref + * The caller must release the reference with g_object_unref * * Returns: a reference to a virCapsPtr instance or NULL */ @@ -1368,7 +1368,7 @@ virCapsPtr virQEMUDriverGetCapabilities(virQEMUDriverPtr driver, return NULL; qemuDriverLock(driver); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); driver->caps = caps; } else { qemuDriverLock(driver); @@ -1382,7 +1382,7 @@ virCapsPtr virQEMUDriverGetCapabilities(virQEMUDriverPtr driver, } } - ret = virObjectRef(driver->caps); + ret = g_object_ref(driver->caps); qemuDriverUnlock(driver); return ret; } diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index dfe0adaad8..bf0a817ff1 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1124,7 +1124,7 @@ qemuStateCleanup(void) virObjectUnref(qemu_driver->xmlopt); virCPUDefFree(qemu_driver->hostcpu); virCapabilitiesHostNUMAUnref(qemu_driver->hostnuma); - virObjectUnref(qemu_driver->caps); + g_clear_object(&qemu_driver->caps); ebtablesContextFree(qemu_driver->ebtables); VIR_FREE(qemu_driver->qemuImgBinary); virObjectUnref(qemu_driver->domains); diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 8526b7b985..02c0dc7fbc 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -79,7 +79,7 @@ vahDeinit(vahControl * ctl) return -1; VIR_FREE(ctl->def); - virObjectUnref(ctl->caps); + g_clear_object(&ctl->caps); virObjectUnref(ctl->xmlopt); VIR_FREE(ctl->files); VIR_FREE(ctl->virtType); @@ -632,10 +632,7 @@ get_definition(vahControl * ctl, const char *xmlStr) if (caps_mockup(ctl, xmlStr) != 0) return -1; - if ((ctl->caps = virCapabilitiesNew(ctl->arch, true, true)) == NULL) { - vah_error(ctl, 0, _("could not allocate memory")); - return -1; - } + ctl->caps = virCapabilitiesNew(ctl->arch, true, true); if (!(ctl->xmlopt = virDomainXMLOptionNew(&virAAHelperDomainDefParserConfig, NULL, NULL, NULL, NULL))) { diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index e9f6663e6d..cb573c4945 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -190,8 +190,7 @@ virStorageBackendGetCapabilities(void) virCapsPtr caps; size_t i; - if (!(caps = virCapabilitiesNew(VIR_ARCH_NONE, false, false))) - return NULL; + caps = virCapabilitiesNew(VIR_ARCH_NONE, false, false); for (i = 0; i < virStorageBackendsCount; i++) virCapabilitiesAddStoragePool(caps, virStorageBackends[i]->type); diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 7759847c2d..00ee8f7c59 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -158,7 +158,7 @@ testDriverDispose(void *obj) testDriverPtr driver = obj; size_t i; - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virObjectUnref(driver->xmlopt); virObjectUnref(driver->domains); virNodeDeviceObjListFree(driver->devs); @@ -284,24 +284,23 @@ static virCapsPtr testBuildCapabilities(virConnectPtr conn) { testDriverPtr privconn = conn->privateData; - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; int guest_types[] = { VIR_DOMAIN_OSTYPE_HVM, VIR_DOMAIN_OSTYPE_XEN }; size_t i, j; - if ((caps = virCapabilitiesNew(VIR_ARCH_I686, false, false)) == NULL) - goto error; + caps = virCapabilitiesNew(VIR_ARCH_I686, false, false); if (virCapabilitiesAddHostFeature(caps, "pae") < 0) - goto error; + return NULL; if (virCapabilitiesAddHostFeature(caps, "nonpae") < 0) - goto error; + return NULL; virCapabilitiesHostInitIOMMU(caps); if (VIR_ALLOC_N(caps->host.pagesSize, 4) < 0) - goto error; + return NULL; caps->host.pagesSize[caps->host.nPagesSize++] = 4; caps->host.pagesSize[caps->host.nPagesSize++] = 8; @@ -317,7 +316,7 @@ testBuildCapabilities(virConnectPtr conn) if (VIR_ALLOC_N(cpu_cells, privconn->cells[i].numCpus) < 0 || VIR_ALLOC_N(pages, nPages) < 0) { VIR_FREE(cpu_cells); - goto error; + return NULL; } memcpy(cpu_cells, privconn->cells[i].cpus, @@ -347,7 +346,7 @@ testBuildCapabilities(virConnectPtr conn) NULL, 0, NULL)) == NULL) - goto error; + return NULL; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_TEST, @@ -355,7 +354,7 @@ testBuildCapabilities(virConnectPtr conn) NULL, 0, NULL) == NULL) - goto error; + return NULL; virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_PAE); virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_NONPAE); @@ -363,16 +362,12 @@ testBuildCapabilities(virConnectPtr conn) caps->host.nsecModels = 1; if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0) - goto error; + return NULL; caps->host.secModels[0].model = g_strdup("testSecurity"); caps->host.secModels[0].doi = g_strdup(""); - return caps; - - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index e98ae04ec0..febf843cd5 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -89,9 +89,7 @@ vboxCapsInit(void) g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest = NULL; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - return NULL; + caps = virCapabilitiesNew(virArchFromHost(), false, false); if (!(caps->host.numa = virCapabilitiesHostNUMANewHost())) return NULL; @@ -124,7 +122,7 @@ vboxDriverDispose(void *obj) { vboxDriverPtr driver = obj; - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virObjectUnref(driver->xmlopt); } diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index fd62bb96f7..c8115ede3c 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -54,7 +54,7 @@ vmwareFreeDriver(struct vmware_driver *driver) virMutexDestroy(&driver->lock); virObjectUnref(driver->domains); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virObjectUnref(driver->xmlopt); VIR_FREE(driver->vmrun); VIR_FREE(driver); @@ -64,12 +64,10 @@ vmwareFreeDriver(struct vmware_driver *driver) virCapsPtr vmwareCapsInit(void) { - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest = NULL; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - goto error; + caps = virCapabilitiesNew(virArchFromHost(), false, false); if (!(caps->host.numa = virCapabilitiesHostNUMANewHost())) goto error; @@ -116,11 +114,10 @@ vmwareCapsInit(void) guest = NULL; } - return caps; + return g_steal_pointer(&caps); error: virCapabilitiesFreeGuest(guest); - virObjectUnref(caps); return NULL; } diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index d882b91def..85dac9a682 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -98,7 +98,7 @@ vzCapsAddGuestDomain(virCapsPtr caps, static virCapsPtr vzBuildCapabilities(void) { - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; virNodeInfo nodeinfo; virDomainOSType ostypes[] = { VIR_DOMAIN_OSTYPE_HVM, @@ -112,15 +112,13 @@ vzBuildCapabilities(void) }; size_t i, j, k; - if ((caps = virCapabilitiesNew(virArchFromHost(), - false, false)) == NULL) - return NULL; + caps = virCapabilitiesNew(virArchFromHost(), false, false); if (!(caps->host.numa = virCapabilitiesHostNUMANewHost())) - goto error; + return NULL; if (virCapabilitiesInitCaches(caps) < 0) - goto error; + return NULL; G_STATIC_ASSERT(G_N_ELEMENTS(archs) == G_N_ELEMENTS(emulators)); @@ -129,23 +127,19 @@ vzBuildCapabilities(void) for (k = 0; k < G_N_ELEMENTS(emulators); k++) if (vzCapsAddGuestDomain(caps, ostypes[i], archs[j], emulators[k], virt_types[k]) < 0) - goto error; + return NULL; if (virCapabilitiesGetNodeInfo(&nodeinfo)) - goto error; + return NULL; if (!(caps->host.cpu = virCPUGetHost(caps->host.arch, VIR_CPU_TYPE_HOST, &nodeinfo, NULL))) - goto error; + return NULL; if (virCapabilitiesAddHostMigrateTransport(caps, "vzmigr") < 0) - goto error; - - return caps; + return NULL; - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } static void vzDriverDispose(void * obj) @@ -154,7 +148,7 @@ static void vzDriverDispose(void * obj) prlsdkDisconnect(driver); virObjectUnref(driver->domains); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virObjectUnref(driver->xmlopt); virObjectUnref(driver->domainEventState); virSysinfoDefFree(driver->hostsysinfo); diff --git a/tests/bhyveargv2xmltest.c b/tests/bhyveargv2xmltest.c index 735cc4b338..3150c4583f 100644 --- a/tests/bhyveargv2xmltest.c +++ b/tests/bhyveargv2xmltest.c @@ -195,7 +195,7 @@ mymain(void) DO_TEST("bhyveload-bootorder"); DO_TEST_FAIL("extraargs"); - virObjectUnref(driver.caps); + g_clear_object(&driver.caps); virObjectUnref(driver.xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c index 9e7eb218b8..0429394246 100644 --- a/tests/bhyvexml2argvtest.c +++ b/tests/bhyvexml2argvtest.c @@ -249,7 +249,7 @@ mymain(void) driver.bhyvecaps &= ~BHYVE_CAP_CPUTOPOLOGY; DO_TEST_FAILURE("cputopology"); - virObjectUnref(driver.caps); + g_clear_object(&driver.caps); virObjectUnref(driver.xmlopt); virPortAllocatorRangeFree(driver.remotePorts); diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c index a0c20a14c1..27b9abb204 100644 --- a/tests/bhyvexml2xmltest.c +++ b/tests/bhyvexml2xmltest.c @@ -127,7 +127,7 @@ mymain(void) /* USB xhci tablet */ DO_TEST_DIFFERENT("input-xhci-tablet"); - virObjectUnref(driver.caps); + g_clear_object(&driver.caps); virObjectUnref(driver.xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/domainconftest.c b/tests/domainconftest.c index 754ef2eb3d..54fdd72c25 100644 --- a/tests/domainconftest.c +++ b/tests/domainconftest.c @@ -103,7 +103,7 @@ mymain(void) DO_TEST_GET_FS("/dev/pts", false); DO_TEST_GET_FS("/doesnotexist", false); - virObjectUnref(caps); + g_clear_object(&caps); virObjectUnref(xmlopt); cleanup: diff --git a/tests/genericxml2xmltest.c b/tests/genericxml2xmltest.c index 74e520522b..a92f5dc613 100644 --- a/tests/genericxml2xmltest.c +++ b/tests/genericxml2xmltest.c @@ -198,7 +198,7 @@ mymain(void) DO_TEST_BACKUP("backup-push-encrypted"); - virObjectUnref(caps); + g_clear_object(&caps); virObjectUnref(xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c index 2b1a9fb5ce..124f17c3ce 100644 --- a/tests/openvzutilstest.c +++ b/tests/openvzutilstest.c @@ -132,7 +132,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED) cleanup: virObjectUnref(driver.xmlopt); - virObjectUnref(driver.caps); + g_clear_object(&driver.caps); VIR_FREE(actual); virDomainDefFree(def); diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c index 7a5125fea6..349af0e431 100644 --- a/tests/qemucaps2xmltest.c +++ b/tests/qemucaps2xmltest.c @@ -99,7 +99,7 @@ static virCapsPtr testGetCaps(char *capsData, const testQemuData *data) { virQEMUCapsPtr qemuCaps = NULL; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; virArch arch = virArchFromString(data->archName); g_autofree char *binary = NULL; @@ -110,10 +110,7 @@ testGetCaps(char *capsData, const testQemuData *data) goto error; } - if ((caps = virCapabilitiesNew(arch, false, false)) == NULL) { - fprintf(stderr, "failed to create the fake capabilities"); - goto error; - } + caps = virCapabilitiesNew(arch, false, false); if (virQEMUCapsInitGuestFromBinary(caps, binary, @@ -124,11 +121,10 @@ testGetCaps(char *capsData, const testQemuData *data) } virObjectUnref(qemuCaps); - return caps; + return g_steal_pointer(&caps); error: virObjectUnref(qemuCaps); - virObjectUnref(caps); return NULL; } @@ -140,7 +136,7 @@ testQemuCapsXML(const void *opaque) char *capsFile = NULL, *xmlFile = NULL; char *capsData = NULL; char *capsXml = NULL; - virCapsPtr capsProvided = NULL; + g_autoptr(virCaps) capsProvided = NULL; xmlFile = g_strdup_printf("%s/caps.%s.xml", data->outputDir, data->archName); @@ -167,7 +163,6 @@ testQemuCapsXML(const void *opaque) VIR_FREE(capsFile); VIR_FREE(capsXml); VIR_FREE(capsData); - virObjectUnref(capsProvided); return ret; } diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index d9244dca44..b7ba016306 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -245,7 +245,7 @@ testQemuHotplug(const void *data) unsigned int device_parse_flags = 0; virDomainObjPtr vm = NULL; virDomainDeviceDefPtr dev = NULL; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; qemuMonitorTestPtr test_mon = NULL; qemuDomainObjPrivatePtr priv = NULL; @@ -356,7 +356,6 @@ testQemuHotplug(const void *data) test->vm = NULL; } virDomainDeviceDefFree(dev); - virObjectUnref(caps); qemuMonitorTestFree(test_mon); return ((ret < 0 && fail) || (!ret && !fail)) ? 0 : -1; } @@ -412,7 +411,7 @@ testQemuHotplugCpuPrepare(const char *test, virHashTablePtr qmpschema) { qemuDomainObjPrivatePtr priv = NULL; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; char *prefix = NULL; struct testQemuHotplugCpuData *data = NULL; @@ -464,7 +463,6 @@ testQemuHotplugCpuPrepare(const char *test, return data; error: - virObjectUnref(caps); testQemuHotplugCpuDataFree(data); VIR_FREE(prefix); return NULL; diff --git a/tests/testutils.c b/tests/testutils.c index 5fd81b70a2..5383df083d 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -909,9 +909,7 @@ virCapsPtr virTestGenericCapsInit(void) g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; - if ((caps = virCapabilitiesNew(VIR_ARCH_X86_64, - false, false)) == NULL) - return NULL; + caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false); if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_I686, "/usr/bin/acme-virt", NULL, diff --git a/tests/testutilslxc.c b/tests/testutilslxc.c index b5e2f542e7..bffc3ba440 100644 --- a/tests/testutilslxc.c +++ b/tests/testutilslxc.c @@ -11,7 +11,7 @@ virCapsPtr testLXCCapsInit(void) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; if ((caps = virCapabilitiesNew(VIR_ARCH_X86_64, @@ -22,20 +22,20 @@ testLXCCapsInit(void) VIR_ARCH_I686, "/usr/libexec/libvirt_lxc", NULL, 0, NULL)) == NULL) - goto error; + return NULL; if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_LXC, NULL, NULL, 0, NULL)) - goto error; + return NULL; if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_EXE, VIR_ARCH_X86_64, "/usr/libexec/libvirt_lxc", NULL, 0, NULL)) == NULL) - goto error; + return NULL; if (!virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_LXC, NULL, NULL, 0, NULL)) - goto error; + return NULL; if (virTestGetDebug()) { @@ -43,18 +43,14 @@ testLXCCapsInit(void) caps_str = virCapabilitiesFormatXML(caps); if (!caps_str) - goto error; + return NULL; VIR_TEST_DEBUG("LXC driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } - return caps; - - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } @@ -81,7 +77,7 @@ void testLXCDriverFree(virLXCDriverPtr driver) { virObjectUnref(driver->xmlopt); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virMutexDestroy(&driver->lock); g_free(driver); } diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index cb68ac0488..73a92a2830 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -196,11 +196,10 @@ testQemuAddGuest(virCapsPtr caps, virCapsPtr testQemuCapsInit(void) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; size_t i; - if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false))) - return NULL; + caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false); /* Add dummy 'none' security_driver. This is equal to setting * security_driver = "none" in qemu.conf. */ @@ -231,11 +230,10 @@ virCapsPtr testQemuCapsInit(void) VIR_FREE(caps_str); } - return caps; + return g_steal_pointer(&caps); cleanup: caps->host.cpu = NULL; - virObjectUnref(caps); return NULL; } @@ -314,7 +312,7 @@ void qemuTestDriverFree(virQEMUDriver *driver) } virObjectUnref(driver->qemuCapsCache); virObjectUnref(driver->xmlopt); - virObjectUnref(driver->caps); + g_clear_object(&driver->caps); virObjectUnref(driver->config); virObjectUnref(driver->securityManager); } diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index d50c3003da..7da90cdd1e 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -11,7 +11,7 @@ static virCapsPtr testXLInitCaps(void) { - virCapsPtr caps; + g_autoptr(virCaps) caps = NULL; virCapsGuestPtr guest; virCapsGuestMachinePtr *machines; int nmachines; @@ -78,7 +78,6 @@ testXLInitCaps(void) cleanup: virCapabilitiesFreeMachines(machines, nmachines); - virObjectUnref(caps); return NULL; } diff --git a/tests/vircaps2xmltest.c b/tests/vircaps2xmltest.c index 17cd600a7a..02907b5a91 100644 --- a/tests/vircaps2xmltest.c +++ b/tests/vircaps2xmltest.c @@ -38,7 +38,7 @@ test_virCapabilities(const void *opaque) { struct virCapabilitiesData *data = (struct virCapabilitiesData *) opaque; const char *archStr = virArchToString(data->arch); - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; char *capsXML = NULL; char *path = NULL; char *system = NULL; @@ -55,9 +55,6 @@ test_virCapabilities(const void *opaque) virFileWrapperAddPrefix("/sys/fs/resctrl", resctrl); caps = virCapabilitiesNew(data->arch, data->offlineMigrate, data->liveMigrate); - if (!caps) - goto cleanup; - if (!(caps->host.numa = virCapabilitiesHostNUMANewHost())) goto cleanup; @@ -82,7 +79,6 @@ test_virCapabilities(const void *opaque) VIR_FREE(resctrl); VIR_FREE(path); VIR_FREE(capsXML); - virObjectUnref(caps); return ret; } diff --git a/tests/vircapstest.c b/tests/vircapstest.c index 08eb017a76..9b37faa1d6 100644 --- a/tests/vircapstest.c +++ b/tests/vircapstest.c @@ -151,11 +151,10 @@ static int test_virCapsDomainDataLookupQEMU(const void *data G_GNUC_UNUSED) { int ret = 0; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; if (!(caps = testQemuCapsInit())) { - ret = -1; - goto out; + return -1; } /* Checking each parameter individually */ @@ -199,8 +198,6 @@ test_virCapsDomainDataLookupQEMU(const void *data G_GNUC_UNUSED) "/usr/bin/qemu-system-aarch64", "pc"); CAPS_EXPECT_ERR(-1, VIR_ARCH_NONE, VIR_DOMAIN_VIRT_VMWARE, NULL, "pc"); - out: - virObjectUnref(caps); return ret; } #endif /* WITH_QEMU */ @@ -210,11 +207,10 @@ static int test_virCapsDomainDataLookupLXC(const void *data G_GNUC_UNUSED) { int ret = 0; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; if (!(caps = testLXCCapsInit())) { - ret = -1; - goto out; + return -1; } CAPSCOMP(-1, VIR_ARCH_NONE, VIR_DOMAIN_VIRT_NONE, NULL, NULL, @@ -224,8 +220,6 @@ test_virCapsDomainDataLookupLXC(const void *data G_GNUC_UNUSED) VIR_DOMAIN_OSTYPE_EXE, VIR_ARCH_X86_64, VIR_DOMAIN_VIRT_LXC, "/usr/libexec/libvirt_lxc", NULL); - out: - virObjectUnref(caps); return ret; } #endif /* WITH_LXC */ diff --git a/tests/virresctrltest.c b/tests/virresctrltest.c index 91baef506d..76cc2ef3c6 100644 --- a/tests/virresctrltest.c +++ b/tests/virresctrltest.c @@ -24,7 +24,7 @@ test_virResctrlGetUnused(const void *opaque) g_autoptr(virResctrlAlloc) alloc = NULL; char *schemata_str = NULL; char *schemata_file; - virCapsPtr caps = NULL; + g_autoptr(virCaps) caps = NULL; system_dir = g_strdup_printf("%s/vircaps2xmldata/linux-%s/system", abs_srcdir, data->filename); @@ -65,7 +65,6 @@ test_virResctrlGetUnused(const void *opaque) ret = 0; cleanup: - virObjectUnref(caps); VIR_FREE(system_dir); VIR_FREE(resctrl_dir); VIR_FREE(schemata_str); diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index 1966aed6fe..613331cabc 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -25,9 +25,6 @@ testCapsInit(void) caps = virCapabilitiesNew(VIR_ARCH_I686, true, true); - if (caps == NULL) - return; - virCapabilitiesAddHostMigrateTransport(caps, "vpxmigr"); /* i686 guest */ @@ -61,8 +58,7 @@ testCapsInit(void) return; failure: - virObjectUnref(caps); - caps = NULL; + g_clear_object(&caps); } static int @@ -285,7 +281,7 @@ mymain(void) DO_TEST("datacenterpath", "datacenterpath"); - virObjectUnref(caps); + g_clear_object(&caps); virObjectUnref(xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c index 8f0a2a72a4..5d5fb8e69a 100644 --- a/tests/xml2vmxtest.c +++ b/tests/xml2vmxtest.c @@ -25,9 +25,6 @@ testCapsInit(void) caps = virCapabilitiesNew(VIR_ARCH_I686, true, true); - if (caps == NULL) - return; - virCapabilitiesAddHostMigrateTransport(caps, "esx"); @@ -62,9 +59,8 @@ testCapsInit(void) return; failure: - virObjectUnref(caps); + g_clear_object(&caps); virObjectUnref(xmlopt); - caps = NULL; } static int @@ -294,7 +290,7 @@ mymain(void) DO_TEST("datacenterpath", "datacenterpath", 4); - virObjectUnref(caps); + g_clear_object(&caps); virObjectUnref(xmlopt); return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE; -- 2.25.3

virFileCache had to also be changed to properly unref the QEMUCaps objects; in turn, virfilecachetest was also converted to GObject so it plays nicely with virFileCache. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/qemu/qemu_capabilities.c | 162 +++++++++++++++++------------------ src/qemu/qemu_capabilities.h | 9 +- src/qemu/qemu_domain.c | 15 ++-- src/qemu/qemu_process.c | 2 +- src/util/virfilecache.c | 43 ++++------ src/util/virfilecache.h | 12 +-- tests/cputest.c | 13 +-- tests/domaincapstest.c | 3 +- tests/qemublocktest.c | 3 +- tests/qemucapabilitiestest.c | 9 +- tests/qemucaps2xmltest.c | 16 ++-- tests/qemucapsprobe.c | 4 +- tests/qemumemlocktest.c | 3 +- tests/testutilsqemu.c | 17 ++-- tests/virfilecachetest.c | 69 +++++++-------- 15 files changed, 172 insertions(+), 208 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 840373bae9..f764c368a1 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -644,7 +644,7 @@ static void virQEMUDomainCapsCacheDispose(void *obj) * And don't forget to update virQEMUCapsNewCopy. */ struct _virQEMUCaps { - virObject parent; + GObject parent; bool kvmSupportsNesting; @@ -682,14 +682,25 @@ struct virQEMUCapsSearchData { }; -static virClassPtr virQEMUCapsClass; -static void virQEMUCapsDispose(void *obj); +G_DEFINE_TYPE(virQEMUCaps, vir_qemu_caps, G_TYPE_OBJECT); -static int virQEMUCapsOnceInit(void) +static void virQEMUCapsDispose(GObject *obj); +static void virQEMUCapsFinalize(GObject *obj); + +static void vir_qemu_caps_init(virQEMUCaps *caps G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virQEMUCaps, virClassForObject())) - return -1; +} + +static void vir_qemu_caps_class_init(virQEMUCapsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virQEMUCapsDispose; + obj->finalize = virQEMUCapsFinalize; +} +static int virQEMUCapsOnceInit(void) +{ if (!(VIR_CLASS_NEW(virQEMUDomainCapsCache, virClassForObjectLockable()))) return -1; @@ -869,7 +880,7 @@ virQEMUCapsInitGuest(virCapsPtr caps, virArch guestarch) { char *binary = NULL; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; int ret = -1; binary = virQEMUCapsGetDefaultEmulator(hostarch, guestarch); @@ -887,7 +898,6 @@ virQEMUCapsInitGuest(virCapsPtr caps, guestarch); VIR_FREE(binary); - virObjectUnref(qemuCaps); return ret; } @@ -1650,7 +1660,7 @@ int virQEMUCapsGetDefaultVersion(virCapsPtr caps, virFileCachePtr capsCache, unsigned int *version) { - virQEMUCapsPtr qemucaps; + g_autoptr(virQEMUCaps) qemucaps = NULL; virArch hostarch; virCapsDomainDataPtr capsdata; @@ -1673,7 +1683,6 @@ int virQEMUCapsGetDefaultVersion(virCapsPtr caps, return -1; *version = virQEMUCapsGetVersion(qemucaps); - virObjectUnref(qemucaps); return 0; } @@ -1699,26 +1708,17 @@ virQEMUDomainCapsCacheNew(void) virQEMUCapsPtr virQEMUCapsNew(void) { - virQEMUCapsPtr qemuCaps; - - if (virQEMUCapsInitialize() < 0) - return NULL; - - if (!(qemuCaps = virObjectNew(virQEMUCapsClass))) - return NULL; + g_autoptr(virQEMUCaps) qemuCaps = + VIR_QEMU_CAPS(g_object_new(VIR_TYPE_QEMU_CAPS, NULL)); qemuCaps->invalidation = true; if (!(qemuCaps->flags = virBitmapNew(QEMU_CAPS_LAST))) - goto error; + return NULL; if (!(qemuCaps->domCapsCache = virQEMUDomainCapsCacheNew())) - goto error; - - return qemuCaps; + return NULL; - error: - virObjectUnref(qemuCaps); - return NULL; + return g_steal_pointer(&qemuCaps); } @@ -1835,7 +1835,7 @@ virQEMUCapsAccelCopy(virQEMUCapsAccelPtr dst, virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps) { - virQEMUCapsPtr ret = virQEMUCapsNewBinary(qemuCaps->binary); + g_autoptr(virQEMUCaps) ret = virQEMUCapsNewBinary(qemuCaps->binary); size_t i; if (!ret) @@ -1859,10 +1859,10 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps) if (virQEMUCapsAccelCopy(&ret->kvm, &qemuCaps->kvm) < 0 || virQEMUCapsAccelCopy(&ret->tcg, &qemuCaps->tcg) < 0) - goto error; + return NULL; if (VIR_ALLOC_N(ret->gicCapabilities, qemuCaps->ngicCapabilities) < 0) - goto error; + return NULL; ret->ngicCapabilities = qemuCaps->ngicCapabilities; for (i = 0; i < qemuCaps->ngicCapabilities; i++) ret->gicCapabilities[i] = qemuCaps->gicCapabilities[i]; @@ -1870,13 +1870,9 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps) if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SEV_GUEST) && virQEMUCapsSEVInfoCopy(&ret->sevCapabilities, qemuCaps->sevCapabilities) < 0) - goto error; - - return ret; + return NULL; - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -1897,11 +1893,20 @@ virQEMUCapsAccelClear(virQEMUCapsAccelPtr caps) } -void virQEMUCapsDispose(void *obj) +static void virQEMUCapsDispose(GObject *obj) { - virQEMUCapsPtr qemuCaps = obj; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(obj); virObjectUnref(qemuCaps->domCapsCache); + qemuCaps->domCapsCache = NULL; + + G_OBJECT_CLASS(vir_qemu_caps_parent_class)->dispose(obj); +} + +static void virQEMUCapsFinalize(GObject *obj) +{ + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(obj); + virBitmapFree(qemuCaps->flags); VIR_FREE(qemuCaps->package); @@ -1914,6 +1919,8 @@ void virQEMUCapsDispose(void *obj) virQEMUCapsAccelClear(&qemuCaps->kvm); virQEMUCapsAccelClear(&qemuCaps->tcg); + + G_OBJECT_CLASS(vir_qemu_caps_parent_class)->finalize(obj); } void @@ -4522,14 +4529,17 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps) static int -virQEMUCapsSaveFile(void *data, +virQEMUCapsSaveFile(GObject *data, const char *filename, void *privData G_GNUC_UNUSED) { - virQEMUCapsPtr qemuCaps = data; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(data); char *xml = NULL; int ret = -1; + if (!qemuCaps) + return -1; + xml = virQEMUCapsFormatCache(qemuCaps); if (virFileWriteStr(filename, xml, 0600) < 0) { @@ -4635,15 +4645,18 @@ virQEMUCapsKVMUsable(virQEMUCapsCachePrivPtr priv) static bool -virQEMUCapsIsValid(void *data, +virQEMUCapsIsValid(GObject *data, void *privData) { - virQEMUCapsPtr qemuCaps = data; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(data); virQEMUCapsCachePrivPtr priv = privData; bool kvmUsable; struct stat sb; bool kvmSupportsNesting; + if (!qemuCaps) + return false; + if (!qemuCaps->invalidation) return true; @@ -5160,18 +5173,18 @@ virQEMUCapsNewForBinaryInternal(virArch hostArch, unsigned int microcodeVersion, const char *kernelVersion) { - virQEMUCapsPtr qemuCaps; + g_autoptr(virQEMUCaps) qemuCaps = NULL; struct stat sb; if (!(qemuCaps = virQEMUCapsNewBinary(binary))) - goto error; + return NULL; /* We would also want to check faccessat if we cared about ACLs, * but we don't. */ if (stat(binary, &sb) < 0) { virReportSystemError(errno, _("Cannot check QEMU binary %s"), binary); - goto error; + return NULL; } qemuCaps->ctime = sb.st_ctime; @@ -5182,11 +5195,11 @@ virQEMUCapsNewForBinaryInternal(virArch hostArch, if (!virFileIsExecutable(binary)) { virReportSystemError(errno, _("QEMU binary %s is not executable"), binary); - goto error; + return NULL; } if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid) < 0) - goto error; + return NULL; qemuCaps->libvirtCtime = virGetSelfLastChanged(); qemuCaps->libvirtVersion = LIBVIR_VERSION_NUMBER; @@ -5202,48 +5215,42 @@ virQEMUCapsNewForBinaryInternal(virArch hostArch, qemuCaps->kvmSupportsNesting = virQEMUCapsKVMSupportsNesting(); } - return qemuCaps; - - error: - virObjectUnref(qemuCaps); - return NULL; + return g_steal_pointer(&qemuCaps); } -static void * +static GObject * virQEMUCapsNewData(const char *binary, void *privData) { virQEMUCapsCachePrivPtr priv = privData; - return virQEMUCapsNewForBinaryInternal(priv->hostArch, - binary, - priv->libDir, - priv->runUid, - priv->runGid, - virHostCPUGetMicrocodeVersion(), - priv->kernelVersion); + return G_OBJECT( + virQEMUCapsNewForBinaryInternal(priv->hostArch, + binary, + priv->libDir, + priv->runUid, + priv->runGid, + virHostCPUGetMicrocodeVersion(), + priv->kernelVersion) + ); } -static void * +static GObject * virQEMUCapsLoadFile(const char *filename, const char *binary, void *privData) { - virQEMUCapsPtr qemuCaps = virQEMUCapsNewBinary(binary); + g_autoptr(virQEMUCaps) qemuCaps = virQEMUCapsNewBinary(binary); virQEMUCapsCachePrivPtr priv = privData; if (!qemuCaps) return NULL; if (virQEMUCapsLoadCache(priv->hostArch, qemuCaps, filename) < 0) - goto error; - - return qemuCaps; + return NULL; - error: - virObjectUnref(qemuCaps); - return NULL; + return g_steal_pointer(&qemuCaps); } @@ -5359,15 +5366,13 @@ virQEMUCapsCacheLookupCopy(virFileCachePtr cache, const char *binary, const char *machineType) { - virQEMUCapsPtr qemuCaps = virQEMUCapsCacheLookup(cache, binary); + g_autoptr(virQEMUCaps) qemuCaps = virQEMUCapsCacheLookup(cache, binary); virQEMUCapsPtr ret; if (!qemuCaps) return NULL; ret = virQEMUCapsNewCopy(qemuCaps); - virObjectUnref(qemuCaps); - if (!ret) return NULL; @@ -5471,8 +5476,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, virArch hostarch = virArchFromHost(); virArch arch = hostarch; virDomainVirtType capsType; - virQEMUCapsPtr qemuCaps = NULL; - virQEMUCapsPtr ret = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; virArch arch_from_caps; g_autofree char *probedbinary = NULL; @@ -5480,14 +5484,14 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, (virttype = virDomainVirtTypeFromString(virttypeStr)) < 0) { virReportError(VIR_ERR_INVALID_ARG, _("unknown virttype: %s"), virttypeStr); - goto cleanup; + return NULL; } if (archStr && (arch = virArchFromString(archStr)) == VIR_ARCH_NONE) { virReportError(VIR_ERR_INVALID_ARG, _("unknown architecture: %s"), archStr); - goto cleanup; + return NULL; } if (!binary) { @@ -5496,7 +5500,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, } if (!(qemuCaps = virQEMUCapsCacheLookup(cache, binary))) - goto cleanup; + return NULL; arch_from_caps = virQEMUCapsGetArch(qemuCaps); @@ -5510,7 +5514,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, "match given architecture '%s'"), virArchToString(arch_from_caps), virArchToString(arch)); - goto cleanup; + return NULL; } if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM)) @@ -5525,7 +5529,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, virReportError(VIR_ERR_INVALID_ARG, _("KVM is not supported by '%s' on this host"), binary); - goto cleanup; + return NULL; } if (machine) { @@ -5536,7 +5540,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, virReportError(VIR_ERR_INVALID_ARG, _("the machine '%s' is not supported by emulator '%s'"), machine, binary); - goto cleanup; + return NULL; } } else { machine = virQEMUCapsGetPreferredMachine(qemuCaps, virttype); @@ -5549,11 +5553,7 @@ virQEMUCapsCacheLookupDefault(virFileCachePtr cache, if (retMachine) *retMachine = machine; - ret = g_steal_pointer(&qemuCaps); - - cleanup: - virObjectUnref(qemuCaps); - return ret; + return g_steal_pointer(&qemuCaps); } bool diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 1681fc79a8..8ae727b0e1 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -21,7 +21,9 @@ #pragma once -#include "virobject.h" +#include "internal.h" +#include <glib-object.h> + #include "capabilities.h" #include "vircommand.h" #include "qemu_monitor.h" @@ -558,10 +560,11 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */ QEMU_CAPS_LAST /* this must always be the last item */ } virQEMUCapsFlags; -typedef struct _virQEMUCaps virQEMUCaps; +#define VIR_TYPE_QEMU_CAPS vir_qemu_caps_get_type() +G_DECLARE_FINAL_TYPE(virQEMUCaps, vir_qemu_caps, VIR, QEMU_CAPS, GObject); + typedef virQEMUCaps *virQEMUCapsPtr; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUCaps, virObjectUnref); virQEMUCapsPtr virQEMUCapsNew(void); virQEMUCapsPtr virQEMUCapsNewBinary(const char *binary); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 98ffd23a71..0ee0972902 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2250,8 +2250,7 @@ qemuDomainObjPrivateDataClear(qemuDomainObjPrivatePtr priv) VIR_FREE(priv->machineName); - virObjectUnref(priv->qemuCaps); - priv->qemuCaps = NULL; + g_clear_object(&priv->qemuCaps); VIR_FREE(priv->pidfile); @@ -4955,7 +4954,7 @@ qemuDomainDefPostParse(virDomainDefPtr def, { virQEMUDriverPtr driver = opaque; g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); - virQEMUCapsPtr qemuCaps = parseOpaque; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(parseOpaque); /* Note that qemuCaps may be NULL when this function is called. This * function shall not fail in that case. It will be re-run on VM startup @@ -5910,7 +5909,7 @@ qemuDomainDeviceDefPostParse(virDomainDeviceDefPtr dev, /* Note that qemuCaps may be NULL when this function is called. This * function shall not fail in that case. It will be re-run on VM startup * with the capabilities populated. */ - virQEMUCapsPtr qemuCaps = parseOpaque; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(parseOpaque); int ret = -1; switch ((virDomainDeviceType) dev->type) { @@ -5998,7 +5997,7 @@ qemuDomainDefAssignAddresses(virDomainDef *def, /* Note that qemuCaps may be NULL when this function is called. This * function shall not fail in that case. It will be re-run on VM startup * with the capabilities populated. */ - virQEMUCapsPtr qemuCaps = parseOpaque; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(parseOpaque); bool newDomain = parseFlags & VIR_DOMAIN_DEF_PARSE_ABI_UPDATE; /* Skip address assignment if @qemuCaps is not present. In such case devices @@ -6031,9 +6030,9 @@ qemuDomainPostParseDataAlloc(const virDomainDef *def, static void qemuDomainPostParseDataFree(void *parseOpaque) { - virQEMUCapsPtr qemuCaps = parseOpaque; + virQEMUCapsPtr qemuCaps = VIR_QEMU_CAPS(parseOpaque); - virObjectUnref(qemuCaps); + g_clear_object(&qemuCaps); } @@ -6876,7 +6875,7 @@ qemuDomainDefFormatBufInternal(virQEMUDriverPtr driver, g_autoptr(virQEMUCaps) qCaps = NULL; if (qemuCaps) { - qCaps = virObjectRef(qemuCaps); + qCaps = g_object_ref(qemuCaps); } else { if (!(qCaps = virQEMUCapsCacheLookupCopy(driver->qemuCapsCache, def->virtType, diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 8ea470f75f..b35377d42b 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -5490,7 +5490,7 @@ qemuProcessPrepareQEMUCaps(virDomainObjPtr vm, qemuDomainObjPrivatePtr priv = vm->privateData; size_t i; - virObjectUnref(priv->qemuCaps); + g_clear_object(&priv->qemuCaps); if (!(priv->qemuCaps = virQEMUCapsCacheLookupCopy(qemuCapsCache, vm->def->virtType, vm->def->emulator, diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c index aecabf173d..cafb700476 100644 --- a/src/util/virfilecache.c +++ b/src/util/virfilecache.c @@ -34,6 +34,7 @@ #include "virlog.h" #include "virobject.h" #include "virstring.h" +#include <glib-object.h> #include <sys/stat.h> #include <sys/types.h> @@ -125,59 +126,51 @@ virFileCacheGetFileName(virFileCachePtr cache, static int virFileCacheLoad(virFileCachePtr cache, const char *name, - void **data) + GObject **data) { g_autofree char *file = NULL; - int ret = -1; - void *loadData = NULL; + g_autoptr(GObject) loadData = NULL; *data = NULL; if (!(file = virFileCacheGetFileName(cache, name))) - return ret; + return -1; if (!virFileExists(file)) { if (errno == ENOENT) { VIR_DEBUG("No cached data '%s' for '%s'", file, name); - ret = 0; - goto cleanup; + return 0; } virReportSystemError(errno, _("Unable to access cache '%s' for '%s'"), file, name); - goto cleanup; + return -1; } if (!(loadData = cache->handlers.loadFile(file, name, cache->priv))) { VIR_WARN("Failed to load cached data from '%s' for '%s': %s", file, name, virGetLastErrorMessage()); virResetLastError(); - ret = 0; - goto cleanup; + return 0; } if (!cache->handlers.isValid(loadData, cache->priv)) { VIR_DEBUG("Outdated cached capabilities '%s' for '%s'", file, name); unlink(file); - ret = 0; - goto cleanup; + return 0; } VIR_DEBUG("Loaded cached data '%s' for '%s'", file, name); - ret = 1; - *data = g_steal_pointer(&loadData); - - cleanup: - virObjectUnref(loadData); - return ret; + g_set_object(data, loadData); + return 1; } static int virFileCacheSave(virFileCachePtr cache, const char *name, - void *data) + GObject *data) { g_autofree char *file = NULL; @@ -195,7 +188,7 @@ static void * virFileCacheNewData(virFileCachePtr cache, const char *name) { - void *data = NULL; + g_autoptr(GObject) data = NULL; int rv; if ((rv = virFileCacheLoad(cache, name, &data)) < 0) @@ -206,12 +199,11 @@ virFileCacheNewData(virFileCachePtr cache, return NULL; if (virFileCacheSave(cache, name, data) < 0) { - virObjectUnref(data); - data = NULL; + return NULL; } } - return data; + return g_steal_pointer(&data); } @@ -275,8 +267,7 @@ virFileCacheValidate(virFileCachePtr cache, if (*data) { VIR_DEBUG("Caching data '%p' for '%s'", *data, name); if (virHashAddEntry(cache->table, name, *data) < 0) { - virObjectUnref(*data); - *data = NULL; + g_clear_object(data); } } } @@ -306,7 +297,7 @@ virFileCacheLookup(virFileCachePtr cache, data = virHashLookup(cache->table, name); virFileCacheValidate(cache, name, &data); - virObjectRef(data); + g_object_ref(data); virObjectUnlock(cache); return data; @@ -337,7 +328,7 @@ virFileCacheLookupByFunc(virFileCachePtr cache, data = virHashSearch(cache->table, iter, iterData, (void **)&name); virFileCacheValidate(cache, name, &data); - virObjectRef(data); + g_object_ref(data); virObjectUnlock(cache); return data; diff --git a/src/util/virfilecache.h b/src/util/virfilecache.h index 006a9717cb..c8277872d3 100644 --- a/src/util/virfilecache.h +++ b/src/util/virfilecache.h @@ -23,8 +23,8 @@ #include "internal.h" -#include "virobject.h" #include "virhash.h" +#include <glib-object.h> typedef struct _virFileCache virFileCache; typedef virFileCache *virFileCachePtr; @@ -40,7 +40,7 @@ typedef virFileCache *virFileCachePtr; * Returns *true* if it's valid or *false* if not valid. */ typedef bool -(*virFileCacheIsValidPtr)(void *data, +(*virFileCacheIsValidPtr)(GObject *data, void *priv); /** @@ -49,11 +49,11 @@ typedef bool * @priv: private data created together with cache * * Creates a new data based on the @name. The returned data must be - * an instance of virObject. + * an instance of GObject. * * Returns data object or NULL on error. */ -typedef void * +typedef GObject * (*virFileCacheNewDataPtr)(const char *name, void *priv); @@ -67,7 +67,7 @@ typedef void * * * Returns cached data object or NULL on error. */ -typedef void * +typedef GObject * (*virFileCacheLoadFilePtr)(const char *filename, const char *name, void *priv); @@ -83,7 +83,7 @@ typedef void * * Returns 0 on success, -1 on error. */ typedef int -(*virFileCacheSaveFilePtr)(void *data, +(*virFileCacheSaveFilePtr)(GObject *data, const char *filename, void *priv); diff --git a/tests/cputest.c b/tests/cputest.c index 869d016ffc..0a91892f4e 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -519,8 +519,7 @@ cpuTestMakeQEMUCaps(const struct data *data) return qemuCaps; error: - virObjectUnref(qemuCaps); - qemuCaps = NULL; + g_clear_object(&qemuCaps); goto cleanup; } @@ -529,7 +528,7 @@ static int cpuTestGetCPUModels(const struct data *data, virDomainCapsCPUModelsPtr *models) { - virQEMUCapsPtr qemuCaps; + g_autoptr(virQEMUCaps) qemuCaps = NULL; *models = NULL; @@ -542,8 +541,6 @@ cpuTestGetCPUModels(const struct data *data, *models = virQEMUCapsGetCPUModels(qemuCaps, VIR_DOMAIN_VIRT_KVM, NULL, NULL); - virObjectUnref(qemuCaps); - return 0; } @@ -876,7 +873,7 @@ static int cpuTestJSONCPUID(const void *arg) { const struct data *data = arg; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; virCPUDefPtr cpu = NULL; char *result = NULL; int ret = -1; @@ -898,7 +895,6 @@ cpuTestJSONCPUID(const void *arg) ret = cpuTestCompareXML(data->arch, cpu, result); cleanup: - virObjectUnref(qemuCaps); virCPUDefFree(cpu); VIR_FREE(result); return ret; @@ -909,7 +905,7 @@ static int cpuTestJSONSignature(const void *arg) { const struct data *data = arg; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; virCPUDataPtr hostData = NULL; qemuMonitorCPUModelInfoPtr modelInfo; int ret = -1; @@ -924,7 +920,6 @@ cpuTestJSONSignature(const void *arg) ret = cpuTestCompareSignature(data, hostData); cleanup: - virObjectUnref(qemuCaps); virCPUDataFree(hostData); return ret; } diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index c3a9f4ef91..2393e312d1 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -79,7 +79,7 @@ fillQemuCaps(virDomainCapsPtr domCaps, { int ret = -1; char *path = NULL; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; virDomainCapsLoaderPtr loader = &domCaps->os.loader; virDomainVirtType virtType; @@ -131,7 +131,6 @@ fillQemuCaps(virDomainCapsPtr domCaps, ret = 0; cleanup: - virObjectUnref(qemuCaps); VIR_FREE(path); return ret; } diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index f9fbfc98f2..ec97e399c8 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -1450,7 +1450,8 @@ mymain(void) cleanup: qemuTestDriverFree(&driver); VIR_FREE(capslatest_x86_64); - virObjectUnref(caps_x86_64); + if (caps_x86_64) + g_object_unref(caps_x86_64); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index 5b32ac1451..b7a93748d9 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -74,7 +74,7 @@ testQemuCaps(const void *opaque) char *repliesFile = NULL; char *capsFile = NULL; qemuMonitorTestPtr mon = NULL; - virQEMUCapsPtr capsActual = NULL; + g_autoptr(virQEMUCaps) capsActual = NULL; char *binary = NULL; char *actual = NULL; unsigned int fakeMicrocodeVersion = 0; @@ -138,7 +138,6 @@ testQemuCaps(const void *opaque) VIR_FREE(actual); VIR_FREE(binary); qemuMonitorTestFree(mon); - virObjectUnref(capsActual); return ret; } @@ -149,8 +148,8 @@ testQemuCapsCopy(const void *opaque) int ret = -1; const testQemuData *data = opaque; char *capsFile = NULL; - virQEMUCapsPtr orig = NULL; - virQEMUCapsPtr copy = NULL; + g_autoptr(virQEMUCaps) orig = NULL; + g_autoptr(virQEMUCaps) copy = NULL; char *actual = NULL; capsFile = g_strdup_printf("%s/%s_%s.%s.xml", @@ -174,8 +173,6 @@ testQemuCapsCopy(const void *opaque) cleanup: VIR_FREE(capsFile); - virObjectUnref(orig); - virObjectUnref(copy); VIR_FREE(actual); return ret; } diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c index 349af0e431..6039895fd8 100644 --- a/tests/qemucaps2xmltest.c +++ b/tests/qemucaps2xmltest.c @@ -51,7 +51,7 @@ testQemuDataInit(testQemuDataPtr data) static virQEMUCapsPtr testQemuGetCaps(char *caps) { - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; xmlDocPtr xml; xmlXPathContextPtr ctxt = NULL; ssize_t i, n; @@ -85,11 +85,10 @@ testQemuGetCaps(char *caps) VIR_FREE(nodes); xmlFreeDoc(xml); xmlXPathFreeContext(ctxt); - return qemuCaps; + return g_steal_pointer(&qemuCaps); error: VIR_FREE(nodes); - virObjectUnref(qemuCaps); xmlFreeDoc(xml); xmlXPathFreeContext(ctxt); return NULL; @@ -98,7 +97,7 @@ testQemuGetCaps(char *caps) static virCapsPtr testGetCaps(char *capsData, const testQemuData *data) { - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; g_autoptr(virCaps) caps = NULL; virArch arch = virArchFromString(data->archName); g_autofree char *binary = NULL; @@ -107,7 +106,7 @@ testGetCaps(char *capsData, const testQemuData *data) if ((qemuCaps = testQemuGetCaps(capsData)) == NULL) { fprintf(stderr, "failed to parse qemu capabilities flags"); - goto error; + return NULL; } caps = virCapabilitiesNew(arch, false, false); @@ -117,15 +116,10 @@ testGetCaps(char *capsData, const testQemuData *data) qemuCaps, arch) < 0) { fprintf(stderr, "failed to create the capabilities from qemu"); - goto error; + return NULL; } - virObjectUnref(qemuCaps); return g_steal_pointer(&caps); - - error: - virObjectUnref(qemuCaps); - return NULL; } static int diff --git a/tests/qemucapsprobe.c b/tests/qemucapsprobe.c index c7e8f3309d..f3705a1ed5 100644 --- a/tests/qemucapsprobe.c +++ b/tests/qemucapsprobe.c @@ -45,7 +45,7 @@ int main(int argc, char **argv) { virThread thread; - virQEMUCapsPtr caps; + g_autoptr(virQEMUCaps) caps = NULL; const char *mock = VIR_TEST_MOCK("qemucapsprobe"); if (!virFileIsExecutable(mock)) { @@ -78,7 +78,5 @@ main(int argc, char **argv) -1, -1, 0, NULL))) return EXIT_FAILURE; - virObjectUnref(caps); - return EXIT_SUCCESS; } diff --git a/tests/qemumemlocktest.c b/tests/qemumemlocktest.c index 3f18fed1c3..c1945f1f2b 100644 --- a/tests/qemumemlocktest.c +++ b/tests/qemumemlocktest.c @@ -58,7 +58,7 @@ mymain(void) { int ret = 0; char *fakerootdir; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); @@ -148,7 +148,6 @@ mymain(void) DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED); cleanup: - virObjectUnref(qemuCaps); if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virFileDeleteTree(fakerootdir); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 73a92a2830..440c91868f 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -286,20 +286,16 @@ virQEMUCapsPtr qemuTestParseCapabilitiesArch(virArch arch, const char *capsFile) { - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; g_autofree char *binary = g_strdup_printf("/usr/bin/qemu-system-%s", virArchToString(arch)); if (!(qemuCaps = virQEMUCapsNewBinary(binary)) || virQEMUCapsLoadCache(arch, qemuCaps, capsFile) < 0) - goto error; + return NULL; virQEMUCapsSetInvalidation(qemuCaps, false); - return qemuCaps; - - error: - virObjectUnref(qemuCaps); - return NULL; + return g_steal_pointer(&qemuCaps); } @@ -358,7 +354,7 @@ int qemuTestCapsCacheInsert(virFileCachePtr cache, } if (virFileCacheInsertData(cache, qemu_emulators[i], tmpCaps) < 0) { - virObjectUnref(tmpCaps); + g_object_unref(tmpCaps); return -1; } } @@ -662,7 +658,7 @@ testQemuInfoSetArgs(struct testQemuInfo *info, { va_list argptr; testQemuInfoArgName argname; - virQEMUCapsPtr qemuCaps = NULL; + g_autoptr(virQEMUCaps) qemuCaps = NULL; int gic = GIC_NONE; char *capsarch = NULL; char *capsver = NULL; @@ -779,7 +775,6 @@ testQemuInfoSetArgs(struct testQemuInfo *info, ret = 0; cleanup: - virObjectUnref(qemuCaps); va_end(argptr); return ret; @@ -791,5 +786,5 @@ testQemuInfoClear(struct testQemuInfo *info) { VIR_FREE(info->infile); VIR_FREE(info->outfile); - virObjectUnref(info->qemuCaps); + g_clear_object(&info->qemuCaps); } diff --git a/tests/virfilecachetest.c b/tests/virfilecachetest.c index 6d280b3bec..66e83f49b6 100644 --- a/tests/virfilecachetest.c +++ b/tests/virfilecachetest.c @@ -24,52 +24,50 @@ #include "virfile.h" #include "virfilecache.h" +#include <glib-object.h> #define VIR_FROM_THIS VIR_FROM_NONE struct _testFileCacheObj { - virObject parent; + GObject parent; char *data; }; -typedef struct _testFileCacheObj testFileCacheObj; +#define TYPE_TEST_FILE_CACHE_OBJ test_file_cache_obj_get_type() +G_DECLARE_FINAL_TYPE(testFileCacheObj, test_file_cache_obj, TEST, FILE_CACHE_OBJ, GObject); typedef testFileCacheObj *testFileCacheObjPtr; -static virClassPtr testFileCacheObjClass; +G_DEFINE_TYPE(testFileCacheObj, test_file_cache_obj, G_TYPE_OBJECT); static void -testFileCacheObjDispose(void *opaque) +testFileCacheObjFinalize(GObject *opaque) { - testFileCacheObjPtr obj = opaque; + testFileCacheObjPtr obj = TEST_FILE_CACHE_OBJ(opaque); VIR_FREE(obj->data); -} + G_OBJECT_CLASS(test_file_cache_obj_parent_class)->finalize(opaque); +} -static int -testFileCacheObjOnceInit(void) +static void +test_file_cache_obj_init(testFileCacheObj *obj G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(testFileCacheObj, virClassForObject())) - return -1; - - return 0; } +static void +test_file_cache_obj_class_init(testFileCacheObjClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); -VIR_ONCE_GLOBAL_INIT(testFileCacheObj); + obj->finalize = testFileCacheObjFinalize; +} static testFileCacheObjPtr testFileCacheObjNew(const char *data) { - testFileCacheObjPtr obj; - - if (testFileCacheObjInitialize() < 0) - return NULL; - - if (!(obj = virObjectNew(testFileCacheObjClass))) - return NULL; + testFileCacheObjPtr obj = TEST_FILE_CACHE_OBJ(g_object_new(TYPE_TEST_FILE_CACHE_OBJ, NULL)); obj->data = g_strdup(data); @@ -87,27 +85,27 @@ typedef testFileCachePriv *testFileCachePrivPtr; static bool -testFileCacheIsValid(void *data, +testFileCacheIsValid(GObject *data, void *priv) { testFileCachePrivPtr testPriv = priv; - testFileCacheObjPtr obj = data; + testFileCacheObjPtr obj = TEST_FILE_CACHE_OBJ(data); - return STREQ(testPriv->expectData, obj->data); + return obj && STREQ(testPriv->expectData, obj->data); } -static void * +static GObject * testFileCacheNewData(const char *name G_GNUC_UNUSED, void *priv) { testFileCachePrivPtr testPriv = priv; - return testFileCacheObjNew(testPriv->newData); + return G_OBJECT(testFileCacheObjNew(testPriv->newData)); } -static void * +static GObject * testFileCacheLoadFile(const char *filename, const char *name G_GNUC_UNUSED, void *priv G_GNUC_UNUSED) @@ -121,12 +119,12 @@ testFileCacheLoadFile(const char *filename, obj = testFileCacheObjNew(data); VIR_FREE(data); - return obj; + return G_OBJECT(obj); } static int -testFileCacheSaveFile(void *data G_GNUC_UNUSED, +testFileCacheSaveFile(GObject *data G_GNUC_UNUSED, const char *filename G_GNUC_UNUSED, void *priv) { @@ -160,9 +158,8 @@ typedef testFileCacheData *testFileCacheDataPtr; static int testFileCache(const void *opaque) { - int ret = -1; const testFileCacheData *data = opaque; - testFileCacheObjPtr obj = NULL; + g_autoptr(testFileCacheObj) obj = NULL; testFileCachePrivPtr testPriv = virFileCacheGetPriv(data->cache); testPriv->dataSaved = false; @@ -171,27 +168,23 @@ testFileCache(const void *opaque) if (!(obj = virFileCacheLookup(data->cache, data->name))) { fprintf(stderr, "Getting cached data failed.\n"); - goto cleanup; + return -1; } if (!obj->data || STRNEQ(data->expectData, obj->data)) { fprintf(stderr, "Expect data '%s', loaded data '%s'.\n", data->expectData, NULLSTR(obj->data)); - goto cleanup; + return -1; } if (data->expectSave != testPriv->dataSaved) { fprintf(stderr, "Expect data to be saved '%s', data saved '%s'.\n", data->expectSave ? "yes" : "no", testPriv->dataSaved ? "yes" : "no"); - goto cleanup; + return -1; } - ret = 0; - - cleanup: - virObjectUnref(obj); - return ret; + return 0; } -- 2.25.3

As types are slowly converted to GObject, a helper function to unref all the GObjects in an array will come in handy, as it's already the case with virObject. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libvirt_private.syms | 1 + src/util/virobject.c | 24 ++++++++++++++++++++++++ src/util/virobject.h | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2cde3c9aa5..4286efdd36 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2734,6 +2734,7 @@ virClassForObjectRWLockable; virClassIsDerivedFrom; virClassName; virClassNew; +virGObjectListFreeCount; virObjectFreeCallback; virObjectFreeHashData; virObjectIsClass; diff --git a/src/util/virobject.c b/src/util/virobject.c index c71781550f..9030fb1625 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -29,6 +29,7 @@ #include "virlog.h" #include "virprobe.h" #include "virstring.h" +#include <glib-object.h> #define VIR_FROM_THIS VIR_FROM_NONE @@ -636,3 +637,26 @@ virObjectListFreeCount(void *list, VIR_FREE(list); } + + +/** + * virGObjectListFreeCount: + * @list: A pointer to a list of GObject pointers to free + * @count: Number of elements in the list. + * + * Unrefs all members of @list and frees the list itself. + */ +void +virGObjectListFreeCount(void *list, + size_t count) +{ + size_t i; + + if (!list) + return; + + for (i = 0; i < count; i++) + g_clear_object(&((GObject **)list)[i]); + + VIR_FREE(list); +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 62a8a3d132..1af048aefc 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -159,3 +159,7 @@ virObjectListFree(void *list); void virObjectListFreeCount(void *list, size_t count); + +void +virGObjectListFreeCount(void *list, + size_t count); -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_remote.c | 9 +++---- src/locking/lock_driver_lockd.c | 19 +++++++------- src/logging/log_manager.c | 15 +++++------ src/lxc/lxc_monitor.c | 2 +- src/remote/remote_driver.c | 46 +++++++++++++++------------------ src/rpc/virnetclient.c | 7 ++--- src/rpc/virnetclientprogram.c | 29 ++++++--------------- src/rpc/virnetclientprogram.h | 10 +++++-- src/rpc/virnetclientstream.c | 4 +-- 9 files changed, 62 insertions(+), 79 deletions(-) diff --git a/src/admin/admin_remote.c b/src/admin/admin_remote.c index ca5e0c9fe4..c732379695 100644 --- a/src/admin/admin_remote.c +++ b/src/admin/admin_remote.c @@ -41,7 +41,7 @@ remoteAdminPrivDispose(void *opaque) { remoteAdminPrivPtr priv = opaque; - virObjectUnref(priv->program); + g_clear_object(&priv->program); virObjectUnref(priv->client); } @@ -227,10 +227,9 @@ remoteAdminPrivNew(const char *sock_path) if (!(priv->client = virNetClientNewUNIX(sock_path, false, NULL))) goto error; - if (!(priv->program = virNetClientProgramNew(ADMIN_PROGRAM, - ADMIN_PROTOCOL_VERSION, - NULL, 0, NULL))) - goto error; + priv->program = virNetClientProgramNew(ADMIN_PROGRAM, + ADMIN_PROTOCOL_VERSION, + NULL, 0, NULL); if (virNetClientAddProgram(priv->client, priv->program) < 0) goto error; diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c index bec98dd05f..3101454a1d 100644 --- a/src/locking/lock_driver_lockd.c +++ b/src/locking/lock_driver_lockd.c @@ -213,12 +213,11 @@ static virNetClientPtr virLockManagerLockDaemonConnectionNew(bool privileged, daemonPath))) goto error; - if (!(*prog = virNetClientProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM, - VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION, - NULL, - 0, - NULL))) - goto error; + *prog = virNetClientProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM, + VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION, + NULL, + 0, + NULL); if (virNetClientAddProgram(client, *prog) < 0) goto error; @@ -233,7 +232,7 @@ static virNetClientPtr virLockManagerLockDaemonConnectionNew(bool privileged, VIR_FREE(lockdpath); virNetClientClose(client); virObjectUnref(client); - virObjectUnref(*prog); + g_clear_object(prog); return NULL; } @@ -296,7 +295,7 @@ static int virLockManagerLockDaemonSetupLockspace(const char *path) rv = 0; cleanup: - virObjectUnref(program); + g_clear_object(&program); virNetClientClose(client); virObjectUnref(client); return rv; @@ -660,7 +659,7 @@ static int virLockManagerLockDaemonAcquire(virLockManagerPtr lock, VIR_FORCE_CLOSE(*fd); virNetClientClose(client); virObjectUnref(client); - virObjectUnref(program); + g_clear_object(&program); return rv; } @@ -713,7 +712,7 @@ static int virLockManagerLockDaemonRelease(virLockManagerPtr lock, cleanup: virNetClientClose(client); virObjectUnref(client); - virObjectUnref(program); + g_clear_object(&program); return rv; } diff --git a/src/logging/log_manager.c b/src/logging/log_manager.c index fd7c2922b3..0428ea4728 100644 --- a/src/logging/log_manager.c +++ b/src/logging/log_manager.c @@ -83,12 +83,11 @@ virLogManagerConnect(bool privileged, daemonPath))) goto error; - if (!(*prog = virNetClientProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM, - VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION, - NULL, - 0, - NULL))) - goto error; + *prog = virNetClientProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM, + VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION, + NULL, + 0, + NULL); if (virNetClientAddProgram(client, *prog) < 0) goto error; @@ -103,7 +102,7 @@ virLogManagerConnect(bool privileged, VIR_FREE(logdpath); virNetClientClose(client); virObjectUnref(client); - virObjectUnref(*prog); + g_clear_object(prog); return NULL; } @@ -135,7 +134,7 @@ virLogManagerFree(virLogManagerPtr mgr) if (mgr->client) virNetClientClose(mgr->client); - virObjectUnref(mgr->program); + g_clear_object(&mgr->program); virObjectUnref(mgr->client); VIR_FREE(mgr); diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c index 479125374b..b3d6da6b98 100644 --- a/src/lxc/lxc_monitor.c +++ b/src/lxc/lxc_monitor.c @@ -198,7 +198,7 @@ static void virLXCMonitorDispose(void *opaque) VIR_DEBUG("mon=%p", mon); if (mon->cb.destroy) (mon->cb.destroy)(mon, mon->vm); - virObjectUnref(mon->program); + g_clear_object(&mon->program); virObjectUnref(mon->vm); } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 7bae0c2514..09e3e8c51b 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -1280,24 +1280,21 @@ doRemoteOpen(virConnectPtr conn, remoteClientCloseFunc, priv->closeCallback, virObjectFreeCallback); - if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM, - REMOTE_PROTOCOL_VERSION, - remoteEvents, - G_N_ELEMENTS(remoteEvents), - conn))) - goto failed; - if (!(priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM, - LXC_PROTOCOL_VERSION, - NULL, - 0, - NULL))) - goto failed; - if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM, - QEMU_PROTOCOL_VERSION, - qemuEvents, - G_N_ELEMENTS(qemuEvents), - conn))) - goto failed; + priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM, + REMOTE_PROTOCOL_VERSION, + remoteEvents, + G_N_ELEMENTS(remoteEvents), + conn); + priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM, + LXC_PROTOCOL_VERSION, + NULL, + 0, + NULL); + priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM, + QEMU_PROTOCOL_VERSION, + qemuEvents, + G_N_ELEMENTS(qemuEvents), + conn); if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 || virNetClientAddProgram(priv->client, priv->lxcProgram) < 0 || @@ -1369,9 +1366,9 @@ doRemoteOpen(virConnectPtr conn, return VIR_DRV_OPEN_SUCCESS; failed: - virObjectUnref(priv->remoteProgram); - virObjectUnref(priv->lxcProgram); - virObjectUnref(priv->qemuProgram); + g_clear_object(&priv->remoteProgram); + g_clear_object(&priv->lxcProgram); + g_clear_object(&priv->qemuProgram); virNetClientClose(priv->client); virObjectUnref(priv->client); priv->client = NULL; @@ -1532,10 +1529,9 @@ doRemoteClose(virConnectPtr conn, struct private_data *priv) priv->client = NULL; virObjectUnref(priv->closeCallback); priv->closeCallback = NULL; - virObjectUnref(priv->remoteProgram); - virObjectUnref(priv->lxcProgram); - virObjectUnref(priv->qemuProgram); - priv->remoteProgram = priv->qemuProgram = priv->lxcProgram = NULL; + g_clear_object(&priv->remoteProgram); + g_clear_object(&priv->lxcProgram); + g_clear_object(&priv->qemuProgram); /* Free hostname copy */ VIR_FREE(priv->hostname); diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index 1c5bef86a1..9bec5235b9 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -676,7 +676,6 @@ bool virNetClientHasPassFD(virNetClientPtr client) void virNetClientDispose(void *obj) { virNetClientPtr client = obj; - size_t i; PROBE(RPC_CLIENT_DISPOSE, "client=%p", client); @@ -684,9 +683,7 @@ void virNetClientDispose(void *obj) if (client->closeFf) client->closeFf(client->closeOpaque); - for (i = 0; i < client->nprograms; i++) - virObjectUnref(client->programs[i]); - VIR_FREE(client->programs); + virGObjectListFreeCount(client->programs, client->nprograms); g_main_loop_unref(client->eventLoop); g_main_context_unref(client->eventCtx); @@ -1010,7 +1007,7 @@ int virNetClientAddProgram(virNetClientPtr client, if (VIR_EXPAND_N(client->programs, client->nprograms, 1) < 0) goto error; - client->programs[client->nprograms-1] = virObjectRef(prog); + client->programs[client->nprograms-1] = g_object_ref(prog); virObjectUnlock(client); return 0; diff --git a/src/rpc/virnetclientprogram.c b/src/rpc/virnetclientprogram.c index 4d1a2bc28b..d76f3eaea7 100644 --- a/src/rpc/virnetclientprogram.c +++ b/src/rpc/virnetclientprogram.c @@ -38,7 +38,7 @@ VIR_LOG_INIT("rpc.netclientprogram"); struct _virNetClientProgram { - virObject parent; + GObject parent; unsigned program; unsigned version; @@ -47,19 +47,15 @@ struct _virNetClientProgram { void *eventOpaque; }; -static virClassPtr virNetClientProgramClass; -static void virNetClientProgramDispose(void *obj); +G_DEFINE_TYPE(virNetClientProgram, vir_net_client_program, G_TYPE_OBJECT); -static int virNetClientProgramOnceInit(void) +static void vir_net_client_program_init(virNetClientProgram *prg G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virNetClientProgram, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virNetClientProgram); - +static void vir_net_client_program_class_init(virNetClientProgramClass *klass G_GNUC_UNUSED) +{ +} virNetClientProgramPtr virNetClientProgramNew(unsigned program, unsigned version, @@ -67,13 +63,9 @@ virNetClientProgramPtr virNetClientProgramNew(unsigned program, size_t nevents, void *eventOpaque) { - virNetClientProgramPtr prog; + virNetClientProgramPtr prog = + VIR_NET_CLIENT_PROGRAM(g_object_new(VIR_TYPE_NET_CLIENT_PROGRAM, NULL)); - if (virNetClientProgramInitialize() < 0) - return NULL; - - if (!(prog = virObjectNew(virNetClientProgramClass))) - return NULL; prog->program = program; prog->version = version; @@ -85,11 +77,6 @@ virNetClientProgramPtr virNetClientProgramNew(unsigned program, } -void virNetClientProgramDispose(void *obj G_GNUC_UNUSED) -{ -} - - unsigned virNetClientProgramGetProgram(virNetClientProgramPtr prog) { return prog->program; diff --git a/src/rpc/virnetclientprogram.h b/src/rpc/virnetclientprogram.h index 1ac0f30023..1f7006d462 100644 --- a/src/rpc/virnetclientprogram.h +++ b/src/rpc/virnetclientprogram.h @@ -24,12 +24,18 @@ #include <rpc/xdr.h> #include "virnetmessage.h" -#include "virobject.h" +#include "internal.h" +#include <glib-object.h> typedef struct _virNetClient virNetClient; typedef virNetClient *virNetClientPtr; -typedef struct _virNetClientProgram virNetClientProgram; +#define VIR_TYPE_NET_CLIENT_PROGRAM vir_net_client_program_get_type() +G_DECLARE_FINAL_TYPE(virNetClientProgram, + vir_net_client_program, + VIR, + NET_CLIENT_PROGRAM, + GObject); typedef virNetClientProgram *virNetClientProgramPtr; typedef struct _virNetClientProgramEvent virNetClientProgramEvent; diff --git a/src/rpc/virnetclientstream.c b/src/rpc/virnetclientstream.c index f904eaba31..183eddd98b 100644 --- a/src/rpc/virnetclientstream.c +++ b/src/rpc/virnetclientstream.c @@ -145,7 +145,7 @@ virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog, if (!(st = virObjectLockableNew(virNetClientStreamClass))) return NULL; - st->prog = virObjectRef(prog); + st->prog = g_object_ref(prog); st->proc = proc; st->serial = serial; st->allowSkip = allowSkip; @@ -163,7 +163,7 @@ void virNetClientStreamDispose(void *obj) virNetMessageQueueServe(&st->rx); virNetMessageFree(msg); } - virObjectUnref(st->prog); + g_clear_object(&st->prog); } bool virNetClientStreamMatches(virNetClientStreamPtr st, -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/locking/lock_daemon.c | 28 ++++++---------- src/logging/log_daemon.c | 28 ++++++---------- src/lxc/lxc_controller.c | 11 +++--- src/remote/remote_daemon.c | 52 ++++++++++------------------- src/remote/remote_daemon_dispatch.c | 18 +++++----- src/remote/remote_daemon_stream.c | 4 +-- src/rpc/virnetserver.c | 14 ++++---- src/rpc/virnetserverprogram.c | 30 +++++------------ src/rpc/virnetserverprogram.h | 10 ++++-- 9 files changed, 77 insertions(+), 118 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index 4eff63014a..260a74a81a 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -860,8 +860,8 @@ virLockDaemonUsage(const char *argv0, bool privileged) int main(int argc, char **argv) { virNetServerPtr lockSrv = NULL; virNetServerPtr adminSrv = NULL; - virNetServerProgramPtr lockProgram = NULL; - virNetServerProgramPtr adminProgram = NULL; + g_autoptr(virNetServerProgram) lockProgram = NULL; + g_autoptr(virNetServerProgram) adminProgram = NULL; char *remote_config_file = NULL; int statuswrite = -1; int ret = 1; @@ -1134,13 +1134,10 @@ int main(int argc, char **argv) { goto cleanup; } - if (!(lockProgram = virNetServerProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM, - VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION, - virLockSpaceProtocolProcs, - virLockSpaceProtocolNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + lockProgram = virNetServerProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM, + VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION, + virLockSpaceProtocolProcs, + virLockSpaceProtocolNProcs); if (virNetServerAddProgram(lockSrv, lockProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; @@ -1148,13 +1145,10 @@ int main(int argc, char **argv) { } if (adminSrv != NULL) { - if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, - ADMIN_PROTOCOL_VERSION, - adminProcs, - adminNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, + ADMIN_PROTOCOL_VERSION, + adminProcs, + adminNProcs); if (virNetServerAddProgram(adminSrv, adminProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; @@ -1191,8 +1185,6 @@ int main(int argc, char **argv) { ret = 0; cleanup: - virObjectUnref(lockProgram); - virObjectUnref(adminProgram); virObjectUnref(lockSrv); virObjectUnref(adminSrv); virLockDaemonFree(lockDaemon); diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index f37054706e..98f8b9c68b 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -641,8 +641,8 @@ virLogDaemonUsage(const char *argv0, bool privileged) int main(int argc, char **argv) { virNetServerPtr logSrv = NULL; virNetServerPtr adminSrv = NULL; - virNetServerProgramPtr logProgram = NULL; - virNetServerProgramPtr adminProgram = NULL; + g_autoptr(virNetServerProgram) logProgram = NULL; + g_autoptr(virNetServerProgram) adminProgram = NULL; char *remote_config_file = NULL; int statuswrite = -1; int ret = 1; @@ -916,26 +916,20 @@ int main(int argc, char **argv) { goto cleanup; } - if (!(logProgram = virNetServerProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM, - VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION, - virLogManagerProtocolProcs, - virLogManagerProtocolNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + logProgram = virNetServerProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM, + VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION, + virLogManagerProtocolProcs, + virLogManagerProtocolNProcs); if (virNetServerAddProgram(logSrv, logProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; } if (adminSrv != NULL) { - if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, - ADMIN_PROTOCOL_VERSION, - adminProcs, - adminNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, + ADMIN_PROTOCOL_VERSION, + adminProcs, + adminNProcs); if (virNetServerAddProgram(adminSrv, adminProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; @@ -971,8 +965,6 @@ int main(int argc, char **argv) { ret = 0; cleanup: - virObjectUnref(logProgram); - virObjectUnref(adminProgram); virObjectUnref(logSrv); virObjectUnref(adminSrv); virLogDaemonFree(logDaemon); diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 43f75d639a..f078c1c425 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -316,6 +316,7 @@ static void virLXCControllerFree(virLXCControllerPtr ctrl) VIR_FREE(ctrl->nsFDs); virCgroupFree(&ctrl->cgroup); + g_clear_object(&ctrl->prog); /* This must always be the last thing to be closed */ VIR_FORCE_CLOSE(ctrl->handshakeFd); VIR_FREE(ctrl); @@ -991,11 +992,10 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) virObjectUnref(svc); svc = NULL; - if (!(ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM, - VIR_LXC_MONITOR_PROGRAM_VERSION, - virLXCMonitorProcs, - virLXCMonitorNProcs))) - goto error; + ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM, + VIR_LXC_MONITOR_PROGRAM_VERSION, + virLXCMonitorProcs, + virLXCMonitorNProcs); if (!(ctrl->daemon = virNetDaemonNew()) || virNetDaemonAddServer(ctrl->daemon, srv) < 0) @@ -1007,6 +1007,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) error: VIR_FREE(sockpath); + g_clear_object(&ctrl->prog); virObjectUnref(srv); virObjectUnref(ctrl->daemon); ctrl->daemon = NULL; diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 7eec599177..46b6b4fdc7 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -760,8 +760,8 @@ int main(int argc, char **argv) { virNetDaemonPtr dmn = NULL; virNetServerPtr srv = NULL; virNetServerPtr srvAdm = NULL; - virNetServerProgramPtr adminProgram = NULL; - virNetServerProgramPtr lxcProgram = NULL; + g_autoptr(virNetServerProgram) adminProgram = NULL; + g_autoptr(virNetServerProgram) lxcProgram = NULL; char *remote_config_file = NULL; int statuswrite = -1; int ret = 1; @@ -1038,37 +1038,28 @@ int main(int argc, char **argv) { remoteProcs[REMOTE_PROC_AUTH_SASL_STEP].needAuth = false; remoteProcs[REMOTE_PROC_AUTH_SASL_START].needAuth = false; remoteProcs[REMOTE_PROC_AUTH_POLKIT].needAuth = false; - if (!(remoteProgram = virNetServerProgramNew(REMOTE_PROGRAM, - REMOTE_PROTOCOL_VERSION, - remoteProcs, - remoteNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + remoteProgram = virNetServerProgramNew(REMOTE_PROGRAM, + REMOTE_PROTOCOL_VERSION, + remoteProcs, + remoteNProcs); if (virNetServerAddProgram(srv, remoteProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; } - if (!(lxcProgram = virNetServerProgramNew(LXC_PROGRAM, - LXC_PROTOCOL_VERSION, - lxcProcs, - lxcNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + lxcProgram = virNetServerProgramNew(LXC_PROGRAM, + LXC_PROTOCOL_VERSION, + lxcProcs, + lxcNProcs); if (virNetServerAddProgram(srv, lxcProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; } - if (!(qemuProgram = virNetServerProgramNew(QEMU_PROGRAM, - QEMU_PROTOCOL_VERSION, - qemuProcs, - qemuNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + qemuProgram = virNetServerProgramNew(QEMU_PROGRAM, + QEMU_PROTOCOL_VERSION, + qemuProcs, + qemuNProcs); if (virNetServerAddProgram(srv, qemuProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; @@ -1095,13 +1086,10 @@ int main(int argc, char **argv) { goto cleanup; } - if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, - ADMIN_PROTOCOL_VERSION, - adminProcs, - adminNProcs))) { - ret = VIR_DAEMON_ERR_INIT; - goto cleanup; - } + adminProgram = virNetServerProgramNew(ADMIN_PROGRAM, + ADMIN_PROTOCOL_VERSION, + adminProcs, + adminNProcs); if (virNetServerAddProgram(srvAdm, adminProgram) < 0) { ret = VIR_DAEMON_ERR_INIT; goto cleanup; @@ -1213,11 +1201,7 @@ int main(int argc, char **argv) { virStateCleanup(); } - virObjectUnref(adminProgram); virObjectUnref(srvAdm); - virObjectUnref(qemuProgram); - virObjectUnref(lxcProgram); - virObjectUnref(remoteProgram); virObjectUnref(srv); virObjectUnref(dmn); diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index c5506c2e11..5f9aa7e8a6 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -150,7 +150,7 @@ remoteEventCallbackFree(void *opaque) daemonClientEventCallbackPtr callback = opaque; if (!callback) return; - virObjectUnref(callback->program); + g_clear_object(&callback->program); virObjectUnref(callback->client); VIR_FREE(callback); } @@ -4255,7 +4255,7 @@ remoteDispatchConnectDomainEventRegister(virNetServerPtr server G_GNUC_UNUSED, if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE; callback->callbackID = -1; callback->legacy = true; @@ -4484,7 +4484,7 @@ remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; callback->legacy = true; @@ -4560,7 +4560,7 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; ref = callback; @@ -6043,7 +6043,7 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; ref = callback; @@ -6164,7 +6164,7 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; ref = callback; @@ -6284,7 +6284,7 @@ remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server G_GNUC_UN if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; ref = callback; @@ -6404,7 +6404,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(remoteProgram); + callback->program = g_object_ref(remoteProgram); callback->eventID = args->eventID; callback->callbackID = -1; ref = callback; @@ -6519,7 +6519,7 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS if (VIR_ALLOC(callback) < 0) goto cleanup; callback->client = virObjectRef(client); - callback->program = virObjectRef(qemuProgram); + callback->program = g_object_ref(qemuProgram); callback->eventID = -1; callback->callbackID = -1; ref = callback; diff --git a/src/remote/remote_daemon_stream.c b/src/remote/remote_daemon_stream.c index 73e4d7befb..62bc9e0f5b 100644 --- a/src/remote/remote_daemon_stream.c +++ b/src/remote/remote_daemon_stream.c @@ -367,7 +367,7 @@ daemonCreateClientStream(virNetServerClientPtr client, stream->refs = 1; stream->priv = priv; - stream->prog = virObjectRef(prog); + stream->prog = g_object_ref(prog); stream->procedure = header->proc; stream->serial = header->serial; stream->filterID = -1; @@ -399,7 +399,7 @@ int daemonFreeClientStream(virNetServerClientPtr client, VIR_DEBUG("client=%p, proc=%d, serial=%u", client, stream->procedure, stream->serial); - virObjectUnref(stream->prog); + g_clear_object(&stream->prog); msg = stream->rx; while (msg) { diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index 242052754f..284d68a88a 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -154,13 +154,13 @@ static void virNetServerHandleJob(void *jobOpaque, void *opaque) if (virNetServerProcessMsg(srv, job->client, job->prog, job->msg) < 0) goto error; - virObjectUnref(job->prog); + g_clear_object(&job->prog); virObjectUnref(job->client); VIR_FREE(job); return; error: - virObjectUnref(job->prog); + g_clear_object(&job->prog); virNetMessageFree(job->msg); virNetServerClientClose(job->client); virObjectUnref(job->client); @@ -218,14 +218,14 @@ virNetServerDispatchNewMessage(virNetServerClientPtr client, job->msg = msg; if (prog) { - job->prog = virObjectRef(prog); + job->prog = g_object_ref(prog); priority = virNetServerProgramGetPriority(prog, msg->header.proc); } if (virThreadPoolSendJob(srv->workers, priority, job) < 0) { virObjectUnref(client); VIR_FREE(job); - virObjectUnref(prog); + g_clear_object(&prog); goto error; } } else { @@ -825,7 +825,7 @@ int virNetServerAddProgram(virNetServerPtr srv, if (VIR_EXPAND_N(srv->programs, srv->nprograms, 1) < 0) goto error; - srv->programs[srv->nprograms-1] = virObjectRef(prog); + srv->programs[srv->nprograms-1] = g_object_ref(prog); virObjectUnlock(srv); return 0; @@ -917,9 +917,7 @@ void virNetServerDispose(void *obj) virObjectUnref(srv->services[i]); VIR_FREE(srv->services); - for (i = 0; i < srv->nprograms; i++) - virObjectUnref(srv->programs[i]); - VIR_FREE(srv->programs); + virGObjectListFreeCount(srv->programs, srv->nprograms); for (i = 0; i < srv->nclients; i++) virObjectUnref(srv->clients[i]); diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c index 8df92fb8df..119c9d01c1 100644 --- a/src/rpc/virnetserverprogram.c +++ b/src/rpc/virnetserverprogram.c @@ -35,7 +35,7 @@ VIR_LOG_INIT("rpc.netserverprogram"); struct _virNetServerProgram { - virObject parent; + GObject parent; unsigned program; unsigned version; @@ -44,32 +44,23 @@ struct _virNetServerProgram { }; -static virClassPtr virNetServerProgramClass; -static void virNetServerProgramDispose(void *obj); +G_DEFINE_TYPE(virNetServerProgram, vir_net_server_program, G_TYPE_OBJECT); -static int virNetServerProgramOnceInit(void) +static void vir_net_server_program_init(virNetServerProgram *prog G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virNetServerProgram, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virNetServerProgram); - +static void vir_net_server_program_class_init(virNetServerProgramClass *klass G_GNUC_UNUSED) +{ +} virNetServerProgramPtr virNetServerProgramNew(unsigned program, unsigned version, virNetServerProgramProcPtr procs, size_t nprocs) { - virNetServerProgramPtr prog; - - if (virNetServerProgramInitialize() < 0) - return NULL; - - if (!(prog = virObjectNew(virNetServerProgramClass))) - return NULL; + virNetServerProgramPtr prog = + VIR_NET_SERVER_PROGRAM(g_object_new(VIR_TYPE_NET_SERVER_PROGRAM, NULL)); prog->program = program; prog->version = version; @@ -567,8 +558,3 @@ int virNetServerProgramSendStreamHole(virNetServerProgramPtr prog, return virNetServerClientSendMessage(client, msg); } - - -void virNetServerProgramDispose(void *obj G_GNUC_UNUSED) -{ -} diff --git a/src/rpc/virnetserverprogram.h b/src/rpc/virnetserverprogram.h index 851bbf0183..bcc10809d4 100644 --- a/src/rpc/virnetserverprogram.h +++ b/src/rpc/virnetserverprogram.h @@ -23,7 +23,8 @@ #include "virnetmessage.h" #include "virnetserverclient.h" -#include "virobject.h" +#include "internal.h" +#include <glib-object.h> typedef struct _virNetDaemon virNetDaemon; typedef virNetDaemon *virNetDaemonPtr; @@ -31,7 +32,12 @@ typedef virNetDaemon *virNetDaemonPtr; typedef struct _virNetServerService virNetServerService; typedef virNetServerService *virNetServerServicePtr; -typedef struct _virNetServerProgram virNetServerProgram; +#define VIR_TYPE_NET_SERVER_PROGRAM vir_net_server_program_get_type() +G_DECLARE_FINAL_TYPE(virNetServerProgram, + vir_net_server_program, + VIR, + NET_SERVER_PROGRAM, + GObject); typedef virNetServerProgram *virNetServerProgramPtr; typedef struct _virNetServerProgramProc virNetServerProgramProc; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_driver.c | 2 +- src/conf/domain_conf.c | 29 +++++++++++++++++++---------- src/conf/domain_conf.h | 9 +++++++++ src/esx/esx_driver.c | 2 +- src/libvirt_private.syms | 1 + src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_controller.c | 2 +- src/lxc/lxc_driver.c | 2 +- src/openvz/openvz_conf.c | 2 +- src/qemu/qemu_driver.c | 2 +- src/qemu/qemu_process.c | 15 +++++---------- src/security/virt-aa-helper.c | 2 +- src/test/test_driver.c | 2 +- src/vbox/vbox_common.c | 2 +- src/vmware/vmware_conf.c | 2 +- src/vz/vz_driver.c | 2 +- tests/bhyveargv2xmltest.c | 2 +- tests/bhyvexml2argvtest.c | 2 +- tests/bhyvexml2xmltest.c | 2 +- tests/domainconftest.c | 2 +- tests/genericxml2xmltest.c | 2 +- tests/openvzutilstest.c | 2 +- tests/testutilslxc.c | 2 +- tests/testutilsqemu.c | 2 +- tests/testutilsxen.c | 2 +- tests/vmx2xmltest.c | 2 +- tests/xml2vmxtest.c | 4 ++-- 27 files changed, 58 insertions(+), 44 deletions(-) diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index bbaf6ea1f5..6a37c7973b 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -1182,7 +1182,7 @@ bhyveStateCleanup(void) virObjectUnref(bhyve_driver->domains); g_clear_object(&bhyve_driver->caps); - virObjectUnref(bhyve_driver->xmlopt); + g_clear_object(&bhyve_driver->xmlopt); virSysinfoDefFree(bhyve_driver->hostsysinfo); virObjectUnref(bhyve_driver->closeCallbacks); virObjectUnref(bhyve_driver->domainEventState); diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 674cfa408e..8126423a18 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -70,7 +70,7 @@ VIR_LOG_INIT("conf.domain_conf"); /* This structure holds various callbacks and data needed * while parsing and creating domain XMLs */ struct _virDomainXMLOption { - virObject parent; + GObject parent; /* XML parser callbacks and defaults */ virDomainDefParserConfig config; @@ -1280,18 +1280,26 @@ VIR_ENUM_IMPL(virDomainLaunchSecurity, ); static virClassPtr virDomainObjClass; -static virClassPtr virDomainXMLOptionClass; static void virDomainObjDispose(void *obj); -static void virDomainXMLOptionDispose(void *obj); +G_DEFINE_TYPE(virDomainXMLOption, vir_domain_xml_option, G_TYPE_OBJECT); +static void virDomainXMLOptionFinalize(GObject *obj); + +static void vir_domain_xml_option_init(virDomainXMLOption *option G_GNUC_UNUSED) +{ +} + +static void vir_domain_xml_option_class_init(virDomainXMLOptionClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virDomainXMLOptionFinalize; +} static int virDomainObjOnceInit(void) { if (!VIR_CLASS_NEW(virDomainObj, virClassForObjectLockable())) return -1; - if (!VIR_CLASS_NEW(virDomainXMLOption, virClassForObject())) - return -1; - return 0; } @@ -1299,12 +1307,14 @@ VIR_ONCE_GLOBAL_INIT(virDomainObj); static void -virDomainXMLOptionDispose(void *obj) +virDomainXMLOptionFinalize(GObject *obj) { - virDomainXMLOptionPtr xmlopt = obj; + virDomainXMLOptionPtr xmlopt = VIR_DOMAIN_XML_OPTION(obj); if (xmlopt->config.privFree) (xmlopt->config.privFree)(xmlopt->config.priv); + + G_OBJECT_CLASS(vir_domain_xml_option_parent_class)->finalize(obj); } /** @@ -1431,8 +1441,7 @@ virDomainXMLOptionNew(virDomainDefParserConfigPtr config, if (virDomainObjInitialize() < 0) return NULL; - if (!(xmlopt = virObjectNew(virDomainXMLOptionClass))) - return NULL; + xmlopt = VIR_DOMAIN_XML_OPTION(g_object_new(VIR_TYPE_DOMAIN_XML_OPTION, NULL)); if (priv) xmlopt->privateData = *priv; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 7d8f1aa31b..964f51f8d5 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -58,6 +58,8 @@ #include "virresctrl.h" #include "virenum.h" +#include <glib-object.h> + /* Flags for the 'type' field in virDomainDeviceDef */ typedef enum { VIR_DOMAIN_DEVICE_NONE = 0, @@ -2816,6 +2818,13 @@ struct _virDomainABIStability { virDomainABIStabilityDomain domain; }; +#define VIR_TYPE_DOMAIN_XML_OPTION vir_domain_xml_option_get_type() +G_DECLARE_FINAL_TYPE(virDomainXMLOption, + vir_domain_xml_option, + VIR, + DOMAIN_XML_OPTION, + GObject); + virDomainXMLOptionPtr virDomainXMLOptionNew(virDomainDefParserConfigPtr config, virDomainXMLPrivateDataCallbacksPtr priv, virXMLNamespacePtr xmlns, diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 39412f746a..42f6ab9392 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -70,7 +70,7 @@ esxFreePrivate(esxPrivate **priv) esxVI_Context_Free(&(*priv)->vCenter); esxUtil_FreeParsedUri(&(*priv)->parsedUri); g_clear_object(&(*priv)->caps); - virObjectUnref((*priv)->xmlopt); + g_clear_object(&(*priv)->xmlopt); VIR_FREE(*priv); } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4286efdd36..043a3afae8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -219,6 +219,7 @@ virSEVCapabilitiesFree; # conf/domain_conf.h +vir_domain_xml_option_get_type; virBlkioDeviceArrayClear; virDiskNameParse; virDiskNameToBusDeviceIndex; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 980984b199..0fce6225d8 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -507,7 +507,7 @@ libxlStateCleanup(void) return -1; virObjectUnref(libxl_driver->hostdevMgr); - virObjectUnref(libxl_driver->xmlopt); + g_clear_object(&libxl_driver->xmlopt); virObjectUnref(libxl_driver->domains); virPortAllocatorRangeFree(libxl_driver->reservedGraphicsPorts); virPortAllocatorRangeFree(libxl_driver->migrationPorts); diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index f078c1c425..0aa56f6d24 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -180,7 +180,7 @@ virLXCControllerDriverFree(virLXCDriverPtr driver) { if (!driver) return; - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); g_clear_object(&driver->caps); virMutexDestroy(&driver->lock); g_free(driver); diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 0cce0d70b7..09a2721ca3 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1613,7 +1613,7 @@ static int lxcStateCleanup(void) virObjectUnref(lxc_driver->hostdevMgr); g_clear_object(&lxc_driver->caps); virObjectUnref(lxc_driver->securityManager); - virObjectUnref(lxc_driver->xmlopt); + g_clear_object(&lxc_driver->xmlopt); if (lxc_driver->lockFD != -1) virPidFileRelease(lxc_driver->config->stateDir, "driver", lxc_driver->lockFD); diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index 0d11d71dfd..474f75840f 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -479,7 +479,7 @@ openvzFreeDriver(struct openvz_driver *driver) if (!driver) return; - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); virObjectUnref(driver->domains); g_clear_object(&driver->caps); VIR_FREE(driver); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index bf0a817ff1..9b353c6f07 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1121,7 +1121,7 @@ qemuStateCleanup(void) virObjectUnref(qemu_driver->securityManager); virObjectUnref(qemu_driver->domainEventState); virObjectUnref(qemu_driver->qemuCapsCache); - virObjectUnref(qemu_driver->xmlopt); + g_clear_object(&qemu_driver->xmlopt); virCPUDefFree(qemu_driver->hostcpu); virCapabilitiesHostNUMAUnref(qemu_driver->hostnuma); g_clear_object(&qemu_driver->caps); diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index b35377d42b..509e47926d 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8565,9 +8565,8 @@ qemuProcessQMPInitMonitor(qemuMonitorPtr mon) static int qemuProcessQMPConnectMonitor(qemuProcessQMPPtr proc) { - virDomainXMLOptionPtr xmlopt = NULL; + g_autoptr(virDomainXMLOption) xmlopt = NULL; virDomainChrSourceDef monConfig; - int ret = -1; VIR_DEBUG("proc=%p, emulator=%s, proc->pid=%lld", proc, proc->binary, (long long)proc->pid); @@ -8579,25 +8578,21 @@ qemuProcessQMPConnectMonitor(qemuProcessQMPPtr proc) if (!(xmlopt = virDomainXMLOptionNew(NULL, NULL, NULL, NULL, NULL)) || !(proc->vm = virDomainObjNew(xmlopt)) || !(proc->vm->def = virDomainDefNew())) - goto cleanup; + return -1; proc->vm->pid = proc->pid; if (!(proc->mon = qemuMonitorOpen(proc->vm, &monConfig, true, 0, virEventThreadGetContext(proc->eventThread), &callbacks, NULL))) - goto cleanup; + return -1; virObjectLock(proc->mon); if (qemuProcessQMPInitMonitor(proc->mon) < 0) - goto cleanup; - - ret = 0; + return -1; - cleanup: - virObjectUnref(xmlopt); - return ret; + return 0; } diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 02c0dc7fbc..dcfd3e95f8 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -80,7 +80,7 @@ vahDeinit(vahControl * ctl) VIR_FREE(ctl->def); g_clear_object(&ctl->caps); - virObjectUnref(ctl->xmlopt); + g_clear_object(&ctl->xmlopt); VIR_FREE(ctl->files); VIR_FREE(ctl->virtType); VIR_FREE(ctl->os); diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 00ee8f7c59..5b9a49d685 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -159,7 +159,7 @@ testDriverDispose(void *obj) size_t i; g_clear_object(&driver->caps); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); virObjectUnref(driver->domains); virNodeDeviceObjListFree(driver->devs); virObjectUnref(driver->networks); diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index febf843cd5..7ca314d0dc 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -123,7 +123,7 @@ vboxDriverDispose(void *obj) vboxDriverPtr driver = obj; g_clear_object(&driver->caps); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); } static int diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index c8115ede3c..f9471752d2 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -55,7 +55,7 @@ vmwareFreeDriver(struct vmware_driver *driver) virMutexDestroy(&driver->lock); virObjectUnref(driver->domains); g_clear_object(&driver->caps); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); VIR_FREE(driver->vmrun); VIR_FREE(driver); } diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index 85dac9a682..4fe072233e 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -149,7 +149,7 @@ static void vzDriverDispose(void * obj) prlsdkDisconnect(driver); virObjectUnref(driver->domains); g_clear_object(&driver->caps); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); virObjectUnref(driver->domainEventState); virSysinfoDefFree(driver->hostsysinfo); } diff --git a/tests/bhyveargv2xmltest.c b/tests/bhyveargv2xmltest.c index 3150c4583f..7abbf9a7fd 100644 --- a/tests/bhyveargv2xmltest.c +++ b/tests/bhyveargv2xmltest.c @@ -196,7 +196,7 @@ mymain(void) DO_TEST_FAIL("extraargs"); g_clear_object(&driver.caps); - virObjectUnref(driver.xmlopt); + g_clear_object(&driver.xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c index 0429394246..0f2caa011b 100644 --- a/tests/bhyvexml2argvtest.c +++ b/tests/bhyvexml2argvtest.c @@ -250,7 +250,7 @@ mymain(void) DO_TEST_FAILURE("cputopology"); g_clear_object(&driver.caps); - virObjectUnref(driver.xmlopt); + g_clear_object(&driver.xmlopt); virPortAllocatorRangeFree(driver.remotePorts); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c index 27b9abb204..782741aab7 100644 --- a/tests/bhyvexml2xmltest.c +++ b/tests/bhyvexml2xmltest.c @@ -128,7 +128,7 @@ mymain(void) DO_TEST_DIFFERENT("input-xhci-tablet"); g_clear_object(&driver.caps); - virObjectUnref(driver.xmlopt); + g_clear_object(&driver.xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/domainconftest.c b/tests/domainconftest.c index 54fdd72c25..361628dcf4 100644 --- a/tests/domainconftest.c +++ b/tests/domainconftest.c @@ -104,7 +104,7 @@ mymain(void) DO_TEST_GET_FS("/doesnotexist", false); g_clear_object(&caps); - virObjectUnref(xmlopt); + g_clear_object(&xmlopt); cleanup: return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/genericxml2xmltest.c b/tests/genericxml2xmltest.c index a92f5dc613..139e2326ff 100644 --- a/tests/genericxml2xmltest.c +++ b/tests/genericxml2xmltest.c @@ -199,7 +199,7 @@ mymain(void) g_clear_object(&caps); - virObjectUnref(xmlopt); + g_clear_object(&xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c index 124f17c3ce..00ed653835 100644 --- a/tests/openvzutilstest.c +++ b/tests/openvzutilstest.c @@ -131,7 +131,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED) result = 0; cleanup: - virObjectUnref(driver.xmlopt); + g_clear_object(&driver.xmlopt); g_clear_object(&driver.caps); VIR_FREE(actual); virDomainDefFree(def); diff --git a/tests/testutilslxc.c b/tests/testutilslxc.c index bffc3ba440..c871f3b920 100644 --- a/tests/testutilslxc.c +++ b/tests/testutilslxc.c @@ -76,7 +76,7 @@ testLXCDriverInit(void) void testLXCDriverFree(virLXCDriverPtr driver) { - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); g_clear_object(&driver->caps); virMutexDestroy(&driver->lock); g_free(driver); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 440c91868f..eece670aa0 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -307,7 +307,7 @@ void qemuTestDriverFree(virQEMUDriver *driver) virFileDeleteTree(driver->config->configDir); } virObjectUnref(driver->qemuCapsCache); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); g_clear_object(&driver->caps); virObjectUnref(driver->config); virObjectUnref(driver->securityManager); diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index 7da90cdd1e..d680fe36e3 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -112,7 +112,7 @@ libxlDriverPrivatePtr testXLInitDriver(void) void testXLFreeDriver(libxlDriverPrivatePtr driver) { virObjectUnref(driver->config); - virObjectUnref(driver->xmlopt); + g_clear_object(&driver->xmlopt); virMutexDestroy(&driver->lock); g_free(driver); } diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index 613331cabc..07cb718f12 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -282,7 +282,7 @@ mymain(void) DO_TEST("datacenterpath", "datacenterpath"); g_clear_object(&caps); - virObjectUnref(xmlopt); + g_clear_object(&xmlopt); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c index 5d5fb8e69a..58e8adafcb 100644 --- a/tests/xml2vmxtest.c +++ b/tests/xml2vmxtest.c @@ -60,7 +60,7 @@ testCapsInit(void) failure: g_clear_object(&caps); - virObjectUnref(xmlopt); + g_clear_object(&xmlopt); } static int @@ -291,7 +291,7 @@ mymain(void) DO_TEST("datacenterpath", "datacenterpath", 4); g_clear_object(&caps); - virObjectUnref(xmlopt); + g_clear_object(&xmlopt); return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_monitor.c | 48 +++++++++++++++++++++++++-------------- src/bhyve/bhyve_monitor.h | 3 ++- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/bhyve/bhyve_monitor.c b/src/bhyve/bhyve_monitor.c index e5cd39a086..3efa3323ff 100644 --- a/src/bhyve/bhyve_monitor.c +++ b/src/bhyve/bhyve_monitor.c @@ -39,7 +39,7 @@ VIR_LOG_INIT("bhyve.bhyve_monitor"); struct _bhyveMonitor { - virObject parent; + GObject parent; bhyveConnPtr driver; virDomainObjPtr vm; @@ -48,44 +48,59 @@ struct _bhyveMonitor { bool reboot; }; -static virClassPtr bhyveMonitorClass; +G_DEFINE_TYPE(bhyveMonitor, bhyve_monitor, G_TYPE_OBJECT); static void -bhyveMonitorDispose(void *obj) +bhyveMonitorDispose(GObject *obj) { - bhyveMonitorPtr mon = obj; + bhyveMonitorPtr mon = BHYVE_MONITOR(obj); - VIR_FORCE_CLOSE(mon->kq); virObjectUnref(mon->vm); + mon->vm = NULL; + + G_OBJECT_CLASS(bhyve_monitor_parent_class)->dispose(obj); } -static int -bhyveMonitorOnceInit(void) +static void +bhyveMonitorFinalize(GObject *obj) { - if (!VIR_CLASS_NEW(bhyveMonitor, virClassForObject())) - return -1; + bhyveMonitorPtr mon = BHYVE_MONITOR(obj); + + VIR_FORCE_CLOSE(mon->kq); + + G_OBJECT_CLASS(bhyve_monitor_parent_class)->finalize(obj); +} - return 0; +static void +bhyve_monitor_init(bhyveMonitor *mon G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(bhyveMonitor); +static void +bhyve_monitor_class_init(bhyveMonitorClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = bhyveMonitorDispose; + obj->finalize = bhyveMonitorFinalize; +} static void bhyveMonitorIO(int, int, int, void *); static bool bhyveMonitorRegister(bhyveMonitorPtr mon) { - virObjectRef(mon); + g_object_ref(mon); mon->watch = virEventAddHandle(mon->kq, VIR_EVENT_HANDLE_READABLE | VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP, bhyveMonitorIO, mon, - virObjectFreeCallback); + g_object_unref); if (mon->watch < 0) { VIR_DEBUG("failed to add event handle for mon %p", mon); - virObjectUnref(mon); + g_object_unref(mon); return false; } return true; @@ -181,8 +196,7 @@ bhyveMonitorOpenImpl(virDomainObjPtr vm, bhyveConnPtr driver) if (bhyveMonitorInitialize() < 0) return NULL; - if (!(mon = virObjectNew(bhyveMonitorClass))) - return NULL; + mon = BHYVE_MONITOR(g_object_new(BHYVE_TYPE_MONITOR, NULL)); mon->driver = driver; mon->reboot = false; @@ -238,5 +252,5 @@ bhyveMonitorClose(bhyveMonitorPtr mon) VIR_DEBUG("cleaning up bhyveMonitor %p", mon); bhyveMonitorUnregister(mon); - virObjectUnref(mon); + g_clear_object(&mon); } diff --git a/src/bhyve/bhyve_monitor.h b/src/bhyve/bhyve_monitor.h index 175cc87192..dcc6929696 100644 --- a/src/bhyve/bhyve_monitor.h +++ b/src/bhyve/bhyve_monitor.h @@ -24,7 +24,8 @@ #include "domain_conf.h" #include "bhyve_utils.h" -typedef struct _bhyveMonitor bhyveMonitor; +#define BHYVE_TYPE_MONITOR bhyve_monitor_get_type() +G_DECLARE_FINAL_TYPE(bhyveMonitor, bhyve_monitor, BHYVE, MONITOR, GObject); typedef bhyveMonitor *bhyveMonitorPtr; bhyveMonitorPtr bhyveMonitorOpen(virDomainObjPtr vm, bhyveConnPtr driver); -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_capabilities.c | 3 +-- src/bhyve/bhyve_conf.c | 36 +++++++++++++++++----------------- src/bhyve/bhyve_driver.c | 2 +- src/bhyve/bhyve_utils.h | 11 +++++++++-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/bhyve/bhyve_capabilities.c b/src/bhyve/bhyve_capabilities.c index 66442e50ba..00f7613594 100644 --- a/src/bhyve/bhyve_capabilities.c +++ b/src/bhyve/bhyve_capabilities.c @@ -131,7 +131,7 @@ virBhyveDomainCapsBuild(bhyveConnPtr conn, DIR *dir; struct dirent *entry; size_t firmwares_alloc = 0; - virBhyveDriverConfigPtr cfg = virBhyveDriverGetConfig(conn); + g_autoptr(virBhyveDriverConfig) cfg = virBhyveDriverGetConfig(conn); const char *firmware_dir = cfg->firmwareDir; virDomainCapsStringValuesPtr firmwares = NULL; @@ -167,7 +167,6 @@ virBhyveDomainCapsBuild(bhyveConnPtr conn, cleanup: VIR_FREE(firmwares); VIR_DIR_CLOSE(dir); - virObjectUnref(cfg); return caps; } diff --git a/src/bhyve/bhyve_conf.c b/src/bhyve/bhyve_conf.c index f3e2ebf7c7..85f170b1a7 100644 --- a/src/bhyve/bhyve_conf.c +++ b/src/bhyve/bhyve_conf.c @@ -32,29 +32,27 @@ VIR_LOG_INIT("bhyve.bhyve_conf"); -static virClassPtr virBhyveDriverConfigClass; -static void virBhyveDriverConfigDispose(void *obj); +G_DEFINE_TYPE(virBhyveDriverConfig, vir_bhyve_driver_config, G_TYPE_OBJECT); +static void virBhyveDriverConfigFinalize(GObject *obj); -static int virBhyveConfigOnceInit(void) +static void +vir_bhyve_driver_config_init(virBhyveDriverConfig *cfg G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virBhyveDriverConfig, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virBhyveConfig); +static void +vir_bhyve_driver_config_class_init(virBhyveDriverConfigClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virBhyveDriverConfigFinalize; +} virBhyveDriverConfigPtr virBhyveDriverConfigNew(void) { - virBhyveDriverConfigPtr cfg; - - if (virBhyveConfigInitialize() < 0) - return NULL; - - if (!(cfg = virObjectNew(virBhyveDriverConfigClass))) - return NULL; + virBhyveDriverConfigPtr cfg = + VIR_BHYVE_DRIVER_CONFIG(g_object_new(VIR_TYPE_BHYVE_DRIVER_CONFIG, NULL)); cfg->firmwareDir = g_strdup(DATADIR "/uefi-firmware"); @@ -87,17 +85,19 @@ virBhyveDriverGetConfig(bhyveConnPtr driver) { virBhyveDriverConfigPtr cfg; bhyveDriverLock(driver); - cfg = virObjectRef(driver->config); + cfg = g_object_ref(driver->config); bhyveDriverUnlock(driver); return cfg; } static void -virBhyveDriverConfigDispose(void *obj) +virBhyveDriverConfigFinalize(GObject *obj) { - virBhyveDriverConfigPtr cfg = obj; + virBhyveDriverConfigPtr cfg = VIR_BHYVE_DRIVER_CONFIG(obj); VIR_FREE(cfg->firmwareDir); + + G_OBJECT_CLASS(vir_bhyve_driver_config_parent_class)->finalize(obj); } void diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index 6a37c7973b..2bdffe0b63 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -1186,7 +1186,7 @@ bhyveStateCleanup(void) virSysinfoDefFree(bhyve_driver->hostsysinfo); virObjectUnref(bhyve_driver->closeCallbacks); virObjectUnref(bhyve_driver->domainEventState); - virObjectUnref(bhyve_driver->config); + g_clear_object(&bhyve_driver->config); virPortAllocatorRangeFree(bhyve_driver->remotePorts); if (bhyve_driver->lockFD != -1) diff --git a/src/bhyve/bhyve_utils.h b/src/bhyve/bhyve_utils.h index f3e80b6121..675b859098 100644 --- a/src/bhyve/bhyve_utils.h +++ b/src/bhyve/bhyve_utils.h @@ -29,16 +29,23 @@ #include "hypervisor/virclosecallbacks.h" #include "virportallocator.h" +#include <glib-object.h> + #define BHYVE_AUTOSTART_DIR SYSCONFDIR "/libvirt/bhyve/autostart" #define BHYVE_CONFIG_DIR SYSCONFDIR "/libvirt/bhyve" #define BHYVE_STATE_DIR RUNSTATEDIR "/libvirt/bhyve" #define BHYVE_LOG_DIR LOCALSTATEDIR "/log/libvirt/bhyve" -typedef struct _virBhyveDriverConfig virBhyveDriverConfig; +#define VIR_TYPE_BHYVE_DRIVER_CONFIG vir_bhyve_driver_config_get_type() +G_DECLARE_FINAL_TYPE(virBhyveDriverConfig, + vir_bhyve_driver_config, + VIR, + BHYVE_DRIVER_CONFIG, + GObject); typedef struct _virBhyveDriverConfig *virBhyveDriverConfigPtr; struct _virBhyveDriverConfig { - virObject parent; + GObject parent; char *firmwareDir; }; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/lxc/lxc_controller.c | 5 +- src/rpc/virnetserver.c | 7 +-- src/rpc/virnetserverprogram.h | 7 ++- src/rpc/virnetserverservice.c | 103 +++++++++++++++++----------------- tests/virnetdaemontest.c | 4 +- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 0aa56f6d24..a8a8b413f7 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -989,8 +989,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) if (virNetServerAddService(srv, svc) < 0) goto error; - virObjectUnref(svc); - svc = NULL; + g_clear_object(&svc); ctrl->prog = virNetServerProgramNew(VIR_LXC_MONITOR_PROGRAM, VIR_LXC_MONITOR_PROGRAM_VERSION, @@ -1011,7 +1010,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl) virObjectUnref(srv); virObjectUnref(ctrl->daemon); ctrl->daemon = NULL; - virObjectUnref(svc); + g_clear_object(&svc); return -1; } diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index 284d68a88a..bb645bd103 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -658,7 +658,7 @@ int virNetServerAddService(virNetServerPtr srv, if (VIR_EXPAND_N(srv->services, srv->nservices, 1) < 0) goto error; - srv->services[srv->nservices-1] = virObjectRef(svc); + srv->services[srv->nservices-1] = g_object_ref(svc); virNetServerServiceSetDispatcher(svc, virNetServerDispatchNewClient, @@ -913,10 +913,7 @@ void virNetServerDispose(void *obj) virThreadPoolFree(srv->workers); - for (i = 0; i < srv->nservices; i++) - virObjectUnref(srv->services[i]); - VIR_FREE(srv->services); - + virGObjectListFreeCount(srv->services, srv->nservices); virGObjectListFreeCount(srv->programs, srv->nprograms); for (i = 0; i < srv->nclients; i++) diff --git a/src/rpc/virnetserverprogram.h b/src/rpc/virnetserverprogram.h index bcc10809d4..72dff3b9cb 100644 --- a/src/rpc/virnetserverprogram.h +++ b/src/rpc/virnetserverprogram.h @@ -29,7 +29,12 @@ typedef struct _virNetDaemon virNetDaemon; typedef virNetDaemon *virNetDaemonPtr; -typedef struct _virNetServerService virNetServerService; +#define VIR_TYPE_NET_SERVER_SERVICE vir_net_server_service_get_type() +G_DECLARE_FINAL_TYPE(virNetServerService, + vir_net_server_service, + VIR, + NET_SERVER_SERVICE, + GObject); typedef virNetServerService *virNetServerServicePtr; #define VIR_TYPE_NET_SERVER_PROGRAM vir_net_server_program_get_type() diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c index 9d5df456a8..946db729c5 100644 --- a/src/rpc/virnetserverservice.c +++ b/src/rpc/virnetserverservice.c @@ -35,7 +35,7 @@ VIR_LOG_INIT("rpc.netserverservice"); struct _virNetServerService { - virObject parent; + GObject parent; size_t nsocks; virNetSocketPtr *socks; @@ -51,19 +51,21 @@ struct _virNetServerService { }; -static virClassPtr virNetServerServiceClass; -static void virNetServerServiceDispose(void *obj); +G_DEFINE_TYPE(virNetServerService, vir_net_server_service, G_TYPE_OBJECT); +static void virNetServerServiceDispose(GObject *obj); +static void virNetServerServiceFinalize(GObject *obj); -static int virNetServerServiceOnceInit(void) +static void vir_net_server_service_init(virNetServerService *svc G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virNetServerService, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virNetServerService); +static void vir_net_server_service_class_init(virNetServerServiceClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + obj->dispose = virNetServerServiceDispose; + obj->finalize = virNetServerServiceFinalize; +} static void virNetServerServiceAccept(virNetSocketPtr sock, int events G_GNUC_UNUSED, @@ -97,17 +99,13 @@ virNetServerServiceNewSocket(virNetSocketPtr *socks, size_t max_queued_clients, size_t nrequests_client_max) { - virNetServerServicePtr svc; + g_autoptr(virNetServerService) svc = NULL; size_t i; - if (virNetServerServiceInitialize() < 0) - return NULL; - - if (!(svc = virObjectNew(virNetServerServiceClass))) - return NULL; + svc = VIR_NET_SERVER_SERVICE(g_object_new(VIR_TYPE_NET_SERVER_SERVICE, NULL)); if (VIR_ALLOC_N(svc->socks, nsocks) < 0) - goto error; + return NULL; svc->nsocks = nsocks; for (i = 0; i < svc->nsocks; i++) { svc->socks[i] = socks[i]; @@ -120,27 +118,22 @@ virNetServerServiceNewSocket(virNetSocketPtr *socks, for (i = 0; i < svc->nsocks; i++) { if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0) - goto error; + return NULL; /* IO callback is initially disabled, until we're ready * to deal with incoming clients */ - virObjectRef(svc); + g_object_ref(svc); if (virNetSocketAddIOCallback(svc->socks[i], 0, virNetServerServiceAccept, svc, - virObjectFreeCallback) < 0) { - virObjectUnref(svc); - goto error; + g_object_unref) < 0) { + g_object_unref(svc); + return NULL; } } - - return svc; - - error: - virObjectUnref(svc); - return NULL; + return g_steal_pointer(&svc); } @@ -258,51 +251,47 @@ virNetServerServicePtr virNetServerServiceNewFDs(int *fds, virNetServerServicePtr virNetServerServiceNewPostExecRestart(virJSONValuePtr object) { - virNetServerServicePtr svc; + g_autoptr(virNetServerService) svc = NULL; virJSONValuePtr socks; size_t i; size_t n; unsigned int max; - if (virNetServerServiceInitialize() < 0) - return NULL; - - if (!(svc = virObjectNew(virNetServerServiceClass))) - return NULL; + svc = VIR_NET_SERVER_SERVICE(g_object_new(VIR_TYPE_NET_SERVER_SERVICE, NULL)); if (virJSONValueObjectGetNumberInt(object, "auth", &svc->auth) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing auth field in JSON state document")); - goto error; + return NULL; } if (virJSONValueObjectGetBoolean(object, "readonly", &svc->readonly) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing readonly field in JSON state document")); - goto error; + return NULL; } if (virJSONValueObjectGetNumberUint(object, "nrequests_client_max", &max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing nrequests_client_max field in JSON state document")); - goto error; + return NULL; } svc->nrequests_client_max = max; if (!(socks = virJSONValueObjectGet(object, "socks"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing socks field in JSON state document")); - goto error; + return NULL; } if (!virJSONValueIsArray(socks)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed socks array")); - goto error; + return NULL; } n = virJSONValueArraySize(socks); if (VIR_ALLOC_N(svc->socks, n) < 0) - goto error; + return NULL; svc->nsocks = n; for (i = 0; i < svc->nsocks; i++) { @@ -311,29 +300,25 @@ virNetServerServicePtr virNetServerServiceNewPostExecRestart(virJSONValuePtr obj if (!(sock = virNetSocketNewPostExecRestart(child))) { virObjectUnref(sock); - goto error; + return NULL; } svc->socks[i] = sock; /* IO callback is initially disabled, until we're ready * to deal with incoming clients */ - virObjectRef(svc); + g_object_ref(svc); if (virNetSocketAddIOCallback(sock, 0, virNetServerServiceAccept, svc, - virObjectFreeCallback) < 0) { - virObjectUnref(svc); - goto error; + g_object_unref) < 0) { + g_object_unref(svc); + return NULL; } } - return svc; - - error: - virObjectUnref(svc); - return NULL; + return g_steal_pointer(&svc); } @@ -415,16 +400,28 @@ void virNetServerServiceSetDispatcher(virNetServerServicePtr svc, } -void virNetServerServiceDispose(void *obj) +void virNetServerServiceDispose(GObject *obj) { - virNetServerServicePtr svc = obj; + virNetServerServicePtr svc = VIR_NET_SERVER_SERVICE(obj); size_t i; - for (i = 0; i < svc->nsocks; i++) + for (i = 0; i < svc->nsocks; i++) { virObjectUnref(svc->socks[i]); - VIR_FREE(svc->socks); + svc->socks[i] = NULL; + } virObjectUnref(svc->tls); + + G_OBJECT_CLASS(vir_net_server_service_parent_class)->dispose(obj); +} + +void virNetServerServiceFinalize(GObject *obj) +{ + virNetServerServicePtr svc = VIR_NET_SERVER_SERVICE(obj); + + VIR_FREE(svc->socks); + + G_OBJECT_CLASS(vir_net_server_service_parent_class)->finalize(obj); } void virNetServerServiceToggle(virNetServerServicePtr svc, diff --git a/tests/virnetdaemontest.c b/tests/virnetdaemontest.c index 09d268627c..3107fb6747 100644 --- a/tests/virnetdaemontest.c +++ b/tests/virnetdaemontest.c @@ -85,7 +85,7 @@ static virNetServerPtr testCreateServer(const char *server_name, const char *host, int family) { virNetServerPtr srv = NULL; - virNetServerServicePtr svc1 = NULL, svc2 = NULL; + g_autoptr(virNetServerService) svc1 = NULL, svc2 = NULL; virNetServerClientPtr cln1 = NULL, cln2 = NULL; virNetSocketPtr sk1 = NULL, sk2 = NULL; int fdclient[2]; @@ -170,8 +170,6 @@ testCreateServer(const char *server_name, const char *host, int family) virDispatchError(NULL); virObjectUnref(cln1); virObjectUnref(cln2); - virObjectUnref(svc1); - virObjectUnref(svc2); virObjectUnref(sk1); virObjectUnref(sk2); return srv; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_capabilities.c | 48 ++++++++++++++++++---------------- src/conf/domain_capabilities.h | 10 ++++--- src/libvirt_private.syms | 1 + tests/cputest.c | 35 ++++++++----------------- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/conf/domain_capabilities.c b/src/conf/domain_capabilities.c index 921d795630..bc0a505cb9 100644 --- a/src/conf/domain_capabilities.c +++ b/src/conf/domain_capabilities.c @@ -45,19 +45,27 @@ VIR_ENUM_IMPL(virDomainCapsFeature, ); static virClassPtr virDomainCapsClass; -static virClassPtr virDomainCapsCPUModelsClass; +G_DEFINE_TYPE(virDomainCapsCPUModels, vir_domain_caps_cpu_models, G_TYPE_OBJECT); static void virDomainCapsDispose(void *obj); -static void virDomainCapsCPUModelsDispose(void *obj); +static void virDomainCapsCPUModelsFinalize(GObject *obj); + +static void vir_domain_caps_cpu_models_init(virDomainCapsCPUModels *mod G_GNUC_UNUSED) +{ +} + +static void vir_domain_caps_cpu_models_class_init(virDomainCapsCPUModelsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virDomainCapsCPUModelsFinalize; +} static int virDomainCapsOnceInit(void) { if (!VIR_CLASS_NEW(virDomainCaps, virClassForObjectLockable())) return -1; - if (!VIR_CLASS_NEW(virDomainCapsCPUModels, virClassForObject())) - return -1; - return 0; } @@ -107,9 +115,9 @@ virDomainCapsDispose(void *obj) static void -virDomainCapsCPUModelsDispose(void *obj) +virDomainCapsCPUModelsFinalize(GObject *obj) { - virDomainCapsCPUModelsPtr cpuModels = obj; + virDomainCapsCPUModelsPtr cpuModels = VIR_DOMAIN_CAPS_CPU_MODELS(obj); size_t i; for (i = 0; i < cpuModels->nmodels; i++) { @@ -118,6 +126,8 @@ virDomainCapsCPUModelsDispose(void *obj) } VIR_FREE(cpuModels->models); + + G_OBJECT_CLASS(vir_domain_caps_cpu_models_parent_class)->finalize(obj); } @@ -147,30 +157,26 @@ virDomainCapsNew(const char *path, virDomainCapsCPUModelsPtr virDomainCapsCPUModelsNew(size_t nmodels) { - virDomainCapsCPUModelsPtr cpuModels = NULL; + g_autoptr(virDomainCapsCPUModels) cpuModels = NULL; if (virDomainCapsInitialize() < 0) return NULL; - if (!(cpuModels = virObjectNew(virDomainCapsCPUModelsClass))) - return NULL; + cpuModels = VIR_DOMAIN_CAPS_CPU_MODELS( + g_object_new(VIR_TYPE_DOMAIN_CAPS_CPU_MODELS, NULL)); if (VIR_ALLOC_N(cpuModels->models, nmodels) < 0) - goto error; + return NULL; cpuModels->nmodels_max = nmodels; - return cpuModels; - - error: - virObjectUnref(cpuModels); - return NULL; + return g_steal_pointer(&cpuModels); } virDomainCapsCPUModelsPtr virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old) { - virDomainCapsCPUModelsPtr cpuModels; + g_autoptr(virDomainCapsCPUModels) cpuModels = NULL; size_t i; if (!(cpuModels = virDomainCapsCPUModelsNew(old->nmodels))) @@ -181,14 +187,10 @@ virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old) old->models[i].name, old->models[i].usable, old->models[i].blockers) < 0) - goto error; + return NULL; } - return cpuModels; - - error: - virObjectUnref(cpuModels); - return NULL; + return g_steal_pointer(&cpuModels); } diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h index 9f4a23d015..88673ffe35 100644 --- a/src/conf/domain_capabilities.h +++ b/src/conf/domain_capabilities.h @@ -129,17 +129,21 @@ struct _virDomainCapsCPUModel { char **blockers; /* NULL-terminated list of usability blockers */ }; -typedef struct _virDomainCapsCPUModels virDomainCapsCPUModels; +#define VIR_TYPE_DOMAIN_CAPS_CPU_MODELS vir_domain_caps_cpu_models_get_type() +G_DECLARE_FINAL_TYPE(virDomainCapsCPUModels, + vir_domain_caps_cpu_models, + VIR, + DOMAIN_CAPS_CPU_MODELS, + GObject); typedef virDomainCapsCPUModels *virDomainCapsCPUModelsPtr; struct _virDomainCapsCPUModels { - virObject parent; + GObject parent; size_t nmodels_max; size_t nmodels; virDomainCapsCPUModelPtr models; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainCapsCPUModels, virObjectUnref); typedef struct _virDomainCapsCPU virDomainCapsCPU; typedef virDomainCapsCPU *virDomainCapsCPUPtr; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 043a3afae8..33ee7069de 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -204,6 +204,7 @@ virDomainAuditVcpu; # conf/domain_capabilities.h +vir_domain_caps_cpu_models_get_type; virDomainCapsCPUModelsAdd; virDomainCapsCPUModelsCopy; virDomainCapsCPUModelsGet; diff --git a/tests/cputest.c b/tests/cputest.c index 0a91892f4e..ad19721717 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -571,7 +571,7 @@ cpuTestCPUID(bool guest, const void *arg) char *host = NULL; virCPUDefPtr cpu = NULL; char *result = NULL; - virDomainCapsCPUModelsPtr models = NULL; + g_autoptr(virDomainCapsCPUModels) models = NULL; hostFile = g_strdup_printf("%s/cputestdata/%s-cpuid-%s.xml", abs_srcdir, virArchToString(data->arch), data->host); @@ -613,7 +613,6 @@ cpuTestCPUID(bool guest, const void *arg) virCPUDataFree(hostData); virCPUDefFree(cpu); VIR_FREE(result); - virObjectUnref(models); return ret; } @@ -785,8 +784,8 @@ cpuTestUpdateLive(const void *arg) virCPUDataPtr disabledData = NULL; char *expectedFile = NULL; virCPUDefPtr expected = NULL; - virDomainCapsCPUModelsPtr hvModels = NULL; - virDomainCapsCPUModelsPtr models = NULL; + g_autoptr(virDomainCapsCPUModels) hvModels = NULL; + g_autoptr(virDomainCapsCPUModels) models = NULL; int ret = -1; cpuFile = g_strdup_printf("cpuid-%s-guest", data->host); @@ -862,8 +861,6 @@ cpuTestUpdateLive(const void *arg) virCPUDataFree(disabledData); VIR_FREE(expectedFile); virCPUDefFree(expected); - virObjectUnref(hvModels); - virObjectUnref(models); return ret; } @@ -935,7 +932,7 @@ static const char *ppc_models_list[] = { "POWER6", "POWER7", "POWER8", NULL }; static virDomainCapsCPUModelsPtr cpuTestInitModels(const char **list) { - virDomainCapsCPUModelsPtr cpus; + g_autoptr(virDomainCapsCPUModels) cpus = NULL; const char **model; if (!(cpus = virDomainCapsCPUModelsNew(0))) @@ -944,25 +941,21 @@ cpuTestInitModels(const char **list) for (model = list; *model; model++) { if (virDomainCapsCPUModelsAdd(cpus, *model, VIR_DOMCAPS_CPU_USABLE_UNKNOWN, NULL) < 0) - goto error; + return NULL; } - return cpus; - - error: - virObjectUnref(cpus); - return NULL; + return g_steal_pointer(&cpus); } static int mymain(void) { - virDomainCapsCPUModelsPtr model486 = NULL; - virDomainCapsCPUModelsPtr nomodel = NULL; - virDomainCapsCPUModelsPtr models = NULL; - virDomainCapsCPUModelsPtr haswell = NULL; - virDomainCapsCPUModelsPtr ppc_models = NULL; + g_autoptr(virDomainCapsCPUModels) model486 = NULL; + g_autoptr(virDomainCapsCPUModels) nomodel = NULL; + g_autoptr(virDomainCapsCPUModels) models = NULL; + g_autoptr(virDomainCapsCPUModels) haswell = NULL; + g_autoptr(virDomainCapsCPUModels) ppc_models = NULL; int ret = 0; #if WITH_QEMU @@ -1273,12 +1266,6 @@ mymain(void) qemuTestDriverFree(&driver); #endif - virObjectUnref(model486); - virObjectUnref(nomodel); - virObjectUnref(models); - virObjectUnref(haswell); - virObjectUnref(ppc_models); - return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libvirt_private.syms | 1 + src/network/bridge_driver.c | 22 ++++++--------- src/util/virdnsmasq.c | 56 +++++++++++++++++-------------------- src/util/virdnsmasq.h | 6 ++-- tests/networkxml2conftest.c | 10 ++----- 5 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 33ee7069de..5b6f97bd97 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1939,6 +1939,7 @@ virDevMapperGetTargets; # util/virdnsmasq.h +dnsmasq_caps_get_type; dnsmasqAddDhcpHost; dnsmasqAddHost; dnsmasqCapsGet; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index f06099297a..728d432c4c 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -115,9 +115,10 @@ networkDriverUnlock(virNetworkDriverStatePtr driver) static dnsmasqCapsPtr networkGetDnsmasqCaps(virNetworkDriverStatePtr driver) { - dnsmasqCapsPtr ret; + dnsmasqCapsPtr ret = NULL; networkDriverLock(driver); - ret = virObjectRef(driver->dnsmasqCaps); + if (driver->dnsmasqCaps) + ret = g_object_ref(driver->dnsmasqCaps); networkDriverUnlock(driver); return ret; } @@ -132,7 +133,7 @@ networkDnsmasqCapsRefresh(virNetworkDriverStatePtr driver) return -1; networkDriverLock(driver); - virObjectUnref(driver->dnsmasqCaps); + g_clear_object(&driver->dnsmasqCaps); driver->dnsmasqCaps = caps; networkDriverUnlock(driver); return 0; @@ -543,7 +544,7 @@ networkUpdateState(virNetworkObjPtr obj, { virNetworkDefPtr def; virNetworkDriverStatePtr driver = opaque; - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); virMacMapPtr macmap; char *macMapFile = NULL; int ret = -1; @@ -629,7 +630,6 @@ networkUpdateState(virNetworkObjPtr obj, ret = 0; cleanup: virObjectUnlock(obj); - virObjectUnref(dnsmasq_caps); VIR_FREE(macMapFile); return ret; } @@ -905,7 +905,7 @@ networkStateCleanup(void) VIR_FREE(network_driver->dnsmasqStateDir); VIR_FREE(network_driver->radvdStateDir); - virObjectUnref(network_driver->dnsmasqCaps); + g_clear_object(&network_driver->dnsmasqCaps); virMutexDestroy(&network_driver->lock); @@ -1544,7 +1544,7 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver, dnsmasqContext *dctx) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); virCommandPtr cmd = NULL; int ret = -1; char *configfile = NULL; @@ -1587,7 +1587,6 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver, *cmdout = cmd; ret = 0; cleanup: - virObjectUnref(dnsmasq_caps); VIR_FREE(configfile); VIR_FREE(configstr); VIR_FREE(leaseshelper_path); @@ -1911,7 +1910,7 @@ networkStartRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); pid_t radvdPid; char *pidfile = NULL; char *radvdpidbase = NULL; @@ -1989,7 +1988,6 @@ networkStartRadvd(virNetworkDriverStatePtr driver, ret = 0; cleanup: - virObjectUnref(dnsmasq_caps); virCommandFree(cmd); VIR_FREE(configfile); VIR_FREE(radvdpidbase); @@ -2003,14 +2001,13 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def = virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver); g_autofree char *radvdpidbase = NULL; g_autofree char *pidfile = NULL; pid_t radvdPid; /* Is dnsmasq handling RA? */ if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) { - virObjectUnref(dnsmasq_caps); if ((radvdpidbase = networkRadvdPidfileBasename(def->name)) && (pidfile = virPidFileBuildPath(driver->pidDir, radvdpidbase))) { /* radvd should not be running but in case it is */ @@ -2019,7 +2016,6 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, } return 0; } - virObjectUnref(dnsmasq_caps); /* if there's no running radvd, just start it */ radvdPid = virNetworkObjGetRadvdPid(obj); diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index adc6f96bb6..a8fb22a368 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -598,7 +598,7 @@ dnsmasqReload(pid_t pid G_GNUC_UNUSED) * */ struct _dnsmasqCaps { - virObject parent; + GObject parent; char *binaryPath; bool noRefresh; time_t mtime; @@ -606,26 +606,31 @@ struct _dnsmasqCaps { unsigned long version; }; -static virClassPtr dnsmasqCapsClass; +G_DEFINE_TYPE(dnsmasqCaps, dnsmasq_caps, G_TYPE_OBJECT); static void -dnsmasqCapsDispose(void *obj) +dnsmasqCapsFinalize(GObject *obj) { - dnsmasqCapsPtr caps = obj; + dnsmasqCapsPtr caps = DNSMASQ_CAPS(obj); virBitmapFree(caps->flags); VIR_FREE(caps->binaryPath); + + G_OBJECT_CLASS(dnsmasq_caps_parent_class)->finalize(obj); } -static int dnsmasqCapsOnceInit(void) +static void +dnsmasq_caps_init(dnsmasqCaps *caps G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(dnsmasqCaps, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(dnsmasqCaps); +static void +dnsmasq_caps_class_init(dnsmasqCapsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = dnsmasqCapsFinalize; +} static void dnsmasqCapsSet(dnsmasqCapsPtr caps, @@ -764,65 +769,54 @@ dnsmasqCapsRefreshInternal(dnsmasqCapsPtr caps, bool force) static dnsmasqCapsPtr dnsmasqCapsNewEmpty(const char *binaryPath) { - dnsmasqCapsPtr caps; - - if (dnsmasqCapsInitialize() < 0) - return NULL; - if (!(caps = virObjectNew(dnsmasqCapsClass))) - return NULL; + g_autoptr(dnsmasqCaps) caps = + DNSMASQ_CAPS(g_object_new(DNSMASQ_TYPE_CAPS, NULL)); if (!(caps->flags = virBitmapNew(DNSMASQ_CAPS_LAST))) - goto error; + return NULL; caps->binaryPath = g_strdup(binaryPath ? binaryPath : DNSMASQ); - return caps; - - error: - virObjectUnref(caps); - return NULL; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromBuffer(const char *buf, const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsSetFromBuffer(caps, buf) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromFile(const char *dataPath, const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsSetFromFile(caps, dataPath) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } dnsmasqCapsPtr dnsmasqCapsNewFromBinary(const char *binaryPath) { - dnsmasqCapsPtr caps = dnsmasqCapsNewEmpty(binaryPath); + g_autoptr(dnsmasqCaps) caps = dnsmasqCapsNewEmpty(binaryPath); if (!caps) return NULL; if (dnsmasqCapsRefreshInternal(caps, true) < 0) { - virObjectUnref(caps); return NULL; } - return caps; + return g_steal_pointer(&caps); } /** dnsmasqCapsRefresh: diff --git a/src/util/virdnsmasq.h b/src/util/virdnsmasq.h index ff0e56d635..9cbbd5562d 100644 --- a/src/util/virdnsmasq.h +++ b/src/util/virdnsmasq.h @@ -23,7 +23,8 @@ #pragma once -#include "virobject.h" +#include "internal.h" +#include <glib-object.h> #include "virsocketaddr.h" typedef struct @@ -75,7 +76,8 @@ typedef enum { DNSMASQ_CAPS_LAST, /* this must always be the last item */ } dnsmasqCapsFlags; -typedef struct _dnsmasqCaps dnsmasqCaps; +#define DNSMASQ_TYPE_CAPS dnsmasq_caps_get_type() +G_DECLARE_FINAL_TYPE(dnsmasqCaps, dnsmasq_caps, DNSMASQ, CAPS, GObject); typedef dnsmasqCaps *dnsmasqCapsPtr; diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index a8355272b9..ed3a2cf6ae 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -101,11 +101,11 @@ static int mymain(void) { int ret = 0; - dnsmasqCapsPtr restricted + g_autoptr(dnsmasqCaps) restricted = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.48", DNSMASQ); - dnsmasqCapsPtr full + g_autoptr(dnsmasqCaps) full = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.63\n--bind-dynamic", DNSMASQ); - dnsmasqCapsPtr dhcpv6 + g_autoptr(dnsmasqCaps) dhcpv6 = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.64\n--bind-dynamic", DNSMASQ); #define DO_TEST(xname, xcaps) \ @@ -143,10 +143,6 @@ mymain(void) DO_TEST("ptr-domains-auto", dhcpv6); DO_TEST("dnsmasq-options", dhcpv6); - virObjectUnref(dhcpv6); - virObjectUnref(full); - virObjectUnref(restricted); - return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_conf.c | 52 +++++++++++++++++++++++----------------- src/conf/domain_conf.h | 9 ++++++- src/libvirt_private.syms | 1 + src/qemu/qemu_domain.c | 5 ++-- src/qemu/qemu_process.c | 3 +-- src/qemu/qemu_virtiofs.c | 12 ++++------ 6 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 8126423a18..85afadeb06 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -2439,8 +2439,7 @@ virDomainNetDefClear(virDomainNetDefPtr def) switch (def->type) { case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - virObjectUnref(def->data.vhostuser); - def->data.vhostuser = NULL; + g_clear_object(&def->data.vhostuser); break; case VIR_DOMAIN_NET_TYPE_SERVER: @@ -2515,6 +2514,7 @@ virDomainNetDefFree(virDomainNetDefPtr def) VIR_FREE(def); } +G_DEFINE_TYPE(virDomainChrSourceDef, vir_domain_chr_source_def, G_TYPE_OBJECT); const char * virDomainChrSourceDefGetPath(virDomainChrSourceDefPtr chr) @@ -2647,19 +2647,31 @@ virDomainChrSourceDefCopy(virDomainChrSourceDefPtr dest, } static void -virDomainChrSourceDefDispose(void *obj) +virDomainChrSourceDefDispose(GObject *obj) { - virDomainChrSourceDefPtr def = obj; + virDomainChrSourceDefPtr def = VIR_DOMAIN_CHR_SOURCE_DEF(obj); + + virObjectUnref(def->privateData); + def->privateData = NULL; + + G_OBJECT_CLASS(vir_domain_chr_source_def_parent_class)->dispose(obj); +} + +static void +virDomainChrSourceDefFinalize(GObject *obj) +{ + virDomainChrSourceDefPtr def = VIR_DOMAIN_CHR_SOURCE_DEF(obj); size_t i; virDomainChrSourceDefClear(def); - virObjectUnref(def->privateData); if (def->seclabels) { for (i = 0; i < def->nseclabels; i++) virSecurityDeviceLabelDefFree(def->seclabels[i]); VIR_FREE(def->seclabels); } + + G_OBJECT_CLASS(vir_domain_chr_source_def_parent_class)->finalize(obj); } @@ -13276,37 +13288,33 @@ virDomainChrSourceDefParseXML(virDomainChrSourceDefPtr def, } -static virClassPtr virDomainChrSourceDefClass; +static void +vir_domain_chr_source_def_init(virDomainChrSourceDef *def G_GNUC_UNUSED) +{ +} -static int -virDomainChrSourceDefOnceInit(void) +static void +vir_domain_chr_source_def_class_init(virDomainChrSourceDefClass *klass) { - if (!VIR_CLASS_NEW(virDomainChrSourceDef, virClassForObject())) - return -1; + GObjectClass *obj = G_OBJECT_CLASS(klass); - return 0; + obj->dispose = virDomainChrSourceDefDispose; + obj->finalize = virDomainChrSourceDefFinalize; } -VIR_ONCE_GLOBAL_INIT(virDomainChrSourceDef); - virDomainChrSourceDefPtr virDomainChrSourceDefNew(virDomainXMLOptionPtr xmlopt) { - virDomainChrSourceDefPtr def = NULL; - - if (virDomainChrSourceDefInitialize() < 0) - return NULL; + g_autoptr(virDomainChrSourceDef) def = NULL; - if (!(def = virObjectNew(virDomainChrSourceDefClass))) - return NULL; + def = VIR_DOMAIN_CHR_SOURCE_DEF(g_object_new(VIR_TYPE_DOMAIN_CHR_SOURCE_DEF, NULL)); if (xmlopt && xmlopt->privateData.chrSourceNew && !(def->privateData = xmlopt->privateData.chrSourceNew())) { - virObjectUnref(def); - def = NULL; + return NULL; } - return def; + return g_steal_pointer(&def); } diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 964f51f8d5..c9a44ecfd8 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1188,7 +1188,7 @@ struct _virDomainChrSourceReconnectDef { /* The host side information for a character device. */ struct _virDomainChrSourceDef { - virObject parent; + GObject parent; int type; /* virDomainChrType */ virObjectPtr privateData; union { @@ -1234,6 +1234,13 @@ struct _virDomainChrSourceDef { virSecurityDeviceLabelDefPtr *seclabels; }; +#define VIR_TYPE_DOMAIN_CHR_SOURCE_DEF vir_domain_chr_source_def_get_type() +G_DECLARE_FINAL_TYPE(virDomainChrSourceDef, + vir_domain_chr_source_def, + VIR, + DOMAIN_CHR_SOURCE_DEF, + GObject); + /* A complete character device, both host and domain views. */ struct _virDomainChrDef { int deviceType; /* enum virDomainChrDeviceType */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5b6f97bd97..1a46ec3e3e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -220,6 +220,7 @@ virSEVCapabilitiesFree; # conf/domain_conf.h +vir_domain_chr_source_def_get_type; vir_domain_xml_option_get_type; virBlkioDeviceArrayClear; virDiskNameParse; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 0ee0972902..c6980a53b6 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2315,7 +2315,7 @@ qemuDomainObjPrivateFree(void *data) qemuDomainObjPrivateDataClear(priv); - virObjectUnref(priv->monConfig); + g_clear_object(&priv->monConfig); qemuDomainObjFreeJob(priv); VIR_FREE(priv->lockState); VIR_FREE(priv->origname); @@ -3934,8 +3934,7 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt, error: virBitmapFree(priv->namespaces); priv->namespaces = NULL; - virObjectUnref(priv->monConfig); - priv->monConfig = NULL; + g_clear_object(&priv->monConfig); virStringListFree(priv->qemuDevices); priv->qemuDevices = NULL; return -1; diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 509e47926d..d7a7389f8f 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7388,8 +7388,7 @@ void qemuProcessStop(virQEMUDriverPtr driver, if (priv->monConfig) { if (priv->monConfig->type == VIR_DOMAIN_CHR_TYPE_UNIX) unlink(priv->monConfig->data.nix.path); - virObjectUnref(priv->monConfig); - priv->monConfig = NULL; + g_clear_object(&priv->monConfig); } qemuDomainObjStopWorker(vm); diff --git a/src/qemu/qemu_virtiofs.c b/src/qemu/qemu_virtiofs.c index 2e239cad66..483af5c798 100644 --- a/src/qemu/qemu_virtiofs.c +++ b/src/qemu/qemu_virtiofs.c @@ -80,7 +80,7 @@ qemuVirtioFSOpenChardev(virQEMUDriverPtr driver, virDomainObjPtr vm, const char *socket_path) { - virDomainChrSourceDefPtr chrdev = virDomainChrSourceDefNew(NULL); + g_autoptr(virDomainChrSourceDef) chrdev = virDomainChrSourceDefNew(NULL); virDomainChrDef chr = { .source = chrdev }; VIR_AUTOCLOSE fd = -1; int ret = -1; @@ -90,23 +90,21 @@ qemuVirtioFSOpenChardev(virQEMUDriverPtr driver, chrdev->data.nix.path = g_strdup(socket_path); if (qemuSecuritySetDaemonSocketLabel(driver->securityManager, vm->def) < 0) - goto cleanup; + return ret; fd = qemuOpenChrChardevUNIXSocket(chrdev); if (fd < 0) { ignore_value(qemuSecurityClearSocketLabel(driver->securityManager, vm->def)); - goto cleanup; + return ret; } if (qemuSecurityClearSocketLabel(driver->securityManager, vm->def) < 0) - goto cleanup; + return ret; if (qemuSecuritySetChardevLabel(driver, vm, &chr) < 0) - goto cleanup; + return ret; ret = fd; fd = -1; - cleanup: - virObjectUnref(chrdev); return ret; } -- 2.25.3

As we slowly convert virObject to GObject, prepare gendispatch.pl to choose the right unref implementation for each object type. Once a type has been converted, just add it to the `gobject_impl` hash. Note though that types explicitly declared should be changed directly where they are used. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/rpc/gendispatch.pl | 66 +++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 0b2ae59910..bc32246618 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -193,6 +193,22 @@ sub get_conn_method { return "remoteGetHypervisorConn"; } +my %gobject_impl = ( +); + +sub use_gobject { + (my $typename = shift) =~ s/Ptr$//; + return exists($gobject_impl{$typename}); +} + +sub get_unref_method { + my $type = shift; + if (use_gobject($type)) { + return "g_object_unref"; + } + return "virObjectUnref"; +} + # Read the input file (usually remote_protocol.x) and form an # opinion about the name, args and return type of each RPC. my ($name, $ProcName, $id, $flags, %calls, @calls, %opts); @@ -584,6 +600,7 @@ elsif ($mode eq "server") { next } elsif ($args_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|secret|nwfilter|nwfilter_binding) (\S+);/) { my $type_name = name_to_TypeName($1); + my $unref_impl = get_unref_method("vir${type_name}"); push(@vars_list, "vir${type_name}Ptr $2 = NULL"); push(@getters_list, @@ -591,9 +608,11 @@ elsif ($mode eq "server") { " goto cleanup;\n"); push(@args_list, "$2"); push(@free_list, - " virObjectUnref($2);"); + " if ($2)\n" . + " $unref_impl($2);"); } elsif ($args_member =~ m/^remote_nonnull_domain_(checkpoint|snapshot) (\S+);$/) { my $type_name = name_to_TypeName($1); + my $unref_impl = get_unref_method("virDomain${type_name}"); push(@vars_list, "virDomainPtr dom = NULL"); push(@vars_list, "virDomain${type_name}Ptr ${1} = NULL"); @@ -605,7 +624,8 @@ elsif ($mode eq "server") { " goto cleanup;\n"); push(@args_list, "$1"); push(@free_list, - " virObjectUnref($1);\n" . + " if ($1)\n" . + " $unref_impl($1);\n" . " virObjectUnref(dom);"); } elsif ($args_member =~ m/^(?:(?:admin|remote)_string|remote_uuid) (\S+)<\S+>;/) { push(@args_list, $conn_var) if !@args_list; @@ -686,6 +706,7 @@ elsif ($mode eq "server") { } } elsif ($args_member =~ m/^admin_nonnull_(server) (\S+);/) { my $type_name = name_to_TypeName($1); + my $unref_impl = get_unref_method("virNet${type_name}"); push(@vars_list, "virNet${type_name}Ptr $2 = NULL"); push(@getters_list, @@ -693,9 +714,11 @@ elsif ($mode eq "server") { " goto cleanup;\n"); push(@args_list, "$2"); push(@free_list, - " virObjectUnref($2);"); + " if ($2)\n" . + " $unref_impl($2);"); } elsif ($args_member =~ m/^admin_nonnull_(client) (\S+);/) { my $type_name = name_to_TypeName($1); + my $unref_impl = get_unref_method("virNetServer${type_name}"); push(@vars_list, "virNetServerPtr srv = NULL"); push(@vars_list, "virNetServer${type_name}Ptr $2 = NULL"); @@ -706,7 +729,9 @@ elsif ($mode eq "server") { " if (!($2 = get_nonnull_$1(srv, args->$2)))\n" . " goto cleanup;\n"); push(@args_list, "$2"); - push(@free_list, " virObjectUnref($2);"); + push(@free_list, + " if ($2)\n" . + " $unref_impl($2);"); push(@free_list, " virObjectUnref(srv);"); } elsif ($args_member =~ m/^(\/)?\*/) { # ignore comments @@ -815,10 +840,12 @@ elsif ($mode eq "server") { $single_ret_var = undef; $single_ret_by_ref = 1; } else { + my $unref_impl = get_unref_method("vir${type_name}"); push(@vars_list, "vir${type_name}Ptr $2 = NULL"); push(@ret_list, "make_nonnull_$1(&ret->$2, $2);\n"); push(@free_list, - " virObjectUnref($2);"); + " if ($2)\n" . + " $unref_impl($2);"); $single_ret_var = $2; $single_ret_by_ref = 0; $single_ret_check = " == NULL"; @@ -929,18 +956,22 @@ elsif ($mode eq "server") { die "opaque array without insert@<offset> annotation: $ret_member"; } elsif ($ret_member =~ m/^admin_nonnull_(server|client) (\S+);/) { my $type_name = name_to_TypeName($1); + my $unref_impl; if ($1 eq "client") { + $unref_impl = get_unref_method("virNetServer${type_name}"); push(@vars_list, "virNetServer${type_name}Ptr $2 = NULL"); push(@ret_list, "make_nonnull_$1(&ret->$2, $2);\n"); push(@ret_list, "make_nonnull_server(&ret->$2.srv, srv);\n"); } else { + $unref_impl = get_unref_method("virNet${type_name}"); push(@vars_list, "virNet${type_name}Ptr $2 = NULL"); push(@ret_list, "make_nonnull_$1(&ret->$2, $2);"); } push(@free_list, - " virObjectUnref($2);"); + " if ($2)\n" . + " $unref_impl($2);"); $single_ret_var = $2; $single_ret_by_ref = 0; $single_ret_check = " == NULL"; @@ -1245,11 +1276,12 @@ elsif ($mode eq "server") { } if ($modern_ret_as_list) { - print " if (result) {\n"; - print " for (i = 0; i < nresults; i++)\n"; - print " virObjectUnref(result[i]);\n"; - print " }\n"; - print " VIR_FREE(result);\n"; + if (use_gobject($modern_ret_struct_name)) { + print " virGObjectListFreeCount"; + } else { + print " virObjectListFreeCount"; + } + print "(result, nresults);\n"; } print " return rv;\n"; @@ -2003,12 +2035,12 @@ elsif ($mode eq "client") { print " }\n"; } if ($modern_ret_as_list) { - print " if (tmp_results) {\n"; - print " for (i = 0; i < ret.$single_ret_list_name.${single_ret_list_name}_len; i++)\n"; - print " virObjectUnref(tmp_results[i]);\n"; - print " VIR_FREE(tmp_results);\n"; - print " }\n"; - print "\n"; + if (use_gobject($modern_ret_var_type)) { + print " virGObjectListFreeCount"; + } else { + print " virObjectListFreeCount"; + } + print "(tmp_results, ret.$single_ret_list_name.${single_ret_list_name}_len);\n"; } print " xdr_free((xdrproc_t)xdr_$call->{ret}, (char *)&ret);\n"; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/libvirt-admin.c | 2 +- src/admin/libvirt_admin_private.syms | 2 +- src/datatypes.c | 56 ++++++++++++++++++++-------- src/datatypes.h | 20 ++++++---- src/rpc/gendispatch.pl | 1 + 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/admin/libvirt-admin.c b/src/admin/libvirt-admin.c index 835b5560d2..eccad1d0fc 100644 --- a/src/admin/libvirt-admin.c +++ b/src/admin/libvirt-admin.c @@ -586,7 +586,7 @@ int virAdmServerFree(virAdmServerPtr srv) virCheckAdmServerReturn(srv, -1); - virObjectUnref(srv); + g_clear_object(&srv); return 0; } diff --git a/src/admin/libvirt_admin_private.syms b/src/admin/libvirt_admin_private.syms index 157a45341e..cfdaba330d 100644 --- a/src/admin/libvirt_admin_private.syms +++ b/src/admin/libvirt_admin_private.syms @@ -34,13 +34,13 @@ xdr_admin_server_set_threadpool_parameters_args; xdr_admin_server_update_tls_files_args; # datatypes.h +vir_adm_server_get_type; virAdmClientClass; virAdmConnectClass; virAdmConnectCloseCallbackDataRegister; virAdmConnectCloseCallbackDataReset; virAdmConnectCloseCallbackDataUnregister; virAdmGetServer; -virAdmServerClass; # Let emacs know we want case-insensitive sorting # Local Variables: diff --git a/src/datatypes.c b/src/datatypes.c index 552115c7a3..cfdb9f9650 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -71,11 +71,27 @@ virClassPtr virAdmConnectCloseCallbackDataClass; static void virAdmConnectDispose(void *obj); static void virAdmConnectCloseCallbackDataDispose(void *obj); -virClassPtr virAdmServerClass; virClassPtr virAdmClientClass; -static void virAdmServerDispose(void *obj); static void virAdmClientDispose(void *obj); +G_DEFINE_TYPE(virAdmServer, vir_adm_server, G_TYPE_OBJECT); +static void virAdmServerDispose(GObject *obj); +static void virAdmServerFinalize(GObject *obj); + +static void +vir_adm_server_init(virAdmServer *srv G_GNUC_UNUSED) +{ +} + +static void +vir_adm_server_class_init(virAdmServerClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virAdmServerDispose; + obj->finalize = virAdmServerFinalize; +} + static int virDataTypesOnceInit(void) { @@ -105,7 +121,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); - DECLARE_CLASS(virAdmServer); DECLARE_CLASS(virAdmClient); #undef DECLARE_CLASS_COMMON @@ -1173,31 +1188,40 @@ virAdmConnectCloseCallbackDataRegister(virAdmConnectCloseCallbackDataPtr cbdata, virAdmServerPtr virAdmGetServer(virAdmConnectPtr conn, const char *name) { - virAdmServerPtr ret = NULL; + g_autoptr(virAdmServer) ret = NULL; if (virDataTypesInitialize() < 0) - goto error; + return NULL; + + ret = VIR_ADM_SERVER(g_object_new(VIR_TYPE_ADM_SERVER, NULL)); - if (!(ret = virObjectNew(virAdmServerClass))) - goto error; ret->name = g_strdup(name); ret->conn = virObjectRef(conn); - return ret; - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } static void -virAdmServerDispose(void *obj) +virAdmServerDispose(GObject *obj) { - virAdmServerPtr srv = obj; + virAdmServerPtr srv = VIR_ADM_SERVER(obj); + + virObjectUnref(srv->conn); + srv->conn = NULL; + + G_OBJECT_CLASS(vir_adm_server_parent_class)->dispose(obj); +} + +static void +virAdmServerFinalize(GObject *obj) +{ + virAdmServerPtr srv = VIR_ADM_SERVER(obj); VIR_DEBUG("release server srv=%p name=%s", srv, srv->name); VIR_FREE(srv->name); - virObjectUnref(srv->conn); + + G_OBJECT_CLASS(vir_adm_server_parent_class)->finalize(obj); } virAdmClientPtr @@ -1215,7 +1239,7 @@ virAdmGetClient(virAdmServerPtr srv, const unsigned long long id, ret->id = id; ret->timestamp = timestamp; ret->transport = transport; - ret->srv = virObjectRef(srv); + ret->srv = g_object_ref(srv); return ret; error: @@ -1229,5 +1253,5 @@ virAdmClientDispose(void *obj) virAdmClientPtr clt = obj; VIR_DEBUG("release client clt=%p, id=%llu", clt, clt->id); - virObjectUnref(clt->srv); + g_clear_object(&clt->srv); } diff --git a/src/datatypes.h b/src/datatypes.h index 2d0407f7ec..dee21068b3 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -28,6 +28,8 @@ #include "virobject.h" #include "viruuid.h" +#include <glib-object.h> + extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; extern virClassPtr virDomainCheckpointClass; @@ -44,9 +46,11 @@ extern virClassPtr virStorageVolClass; extern virClassPtr virStoragePoolClass; extern virClassPtr virAdmConnectClass; -extern virClassPtr virAdmServerClass; extern virClassPtr virAdmClientClass; +#define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() +G_DECLARE_FINAL_TYPE(virAdmServer, vir_adm_server, VIR, ADM_SERVER, GObject); + #define virCheckConnectReturn(obj, retval) \ do { \ if (!virObjectIsClass(obj, virConnectClass)) { \ @@ -419,8 +423,8 @@ extern virClassPtr virAdmClientClass; #define virCheckAdmServerReturn(obj, retval) \ do { \ - virAdmServerPtr _srv = (obj); \ - if (!virObjectIsClass(_srv, virAdmServerClass) || \ + virAdmServerPtr _srv = VIR_ADM_SERVER(obj); \ + if (_srv == NULL || \ !virObjectIsClass(_srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -431,8 +435,8 @@ extern virClassPtr virAdmClientClass; } while (0) #define virCheckAdmServerGoto(obj, label) \ do { \ - virAdmServerPtr _srv = (obj); \ - if (!virObjectIsClass(_srv, virAdmServerClass) || \ + virAdmServerPtr _srv = VIR_ADM_SERVER(obj); \ + if (_srv == NULL || \ !virObjectIsClass(_srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -445,7 +449,7 @@ extern virClassPtr virAdmClientClass; do { \ virAdmClientPtr _clt = (obj); \ if (!virObjectIsClass(_clt, virAdmClientClass) || \ - !virObjectIsClass(_clt->srv, virAdmServerClass) || \ + !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -458,7 +462,7 @@ extern virClassPtr virAdmClientClass; do { \ virAdmClientPtr _clt = (obj); \ if (!virObjectIsClass(_clt, virAdmClientClass) || \ - !virObjectIsClass(_clt->srv, virAdmServerClass) || \ + !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -573,7 +577,7 @@ struct _virAdmConnect { * Internal structure associated to a daemon server */ struct _virAdmServer { - virObject parent; + GObject parent; virAdmConnectPtr conn; /* pointer back to the admin connection */ char *name; /* the server external name */ }; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index bc32246618..a9152198d7 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -194,6 +194,7 @@ sub get_conn_method { } my %gobject_impl = ( + virAdmServer => 1, ); sub use_gobject { -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/libvirt-admin.c | 2 +- src/admin/libvirt_admin_private.syms | 2 +- src/datatypes.c | 36 ++++++++++++++++++---------- src/datatypes.h | 13 +++++----- src/rpc/gendispatch.pl | 1 + 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/admin/libvirt-admin.c b/src/admin/libvirt-admin.c index eccad1d0fc..56c63ea6e6 100644 --- a/src/admin/libvirt-admin.c +++ b/src/admin/libvirt-admin.c @@ -676,7 +676,7 @@ int virAdmClientFree(virAdmClientPtr client) virCheckAdmClientReturn(client, -1); - virObjectUnref(client); + g_clear_object(&client); return 0; } diff --git a/src/admin/libvirt_admin_private.syms b/src/admin/libvirt_admin_private.syms index cfdaba330d..e402635872 100644 --- a/src/admin/libvirt_admin_private.syms +++ b/src/admin/libvirt_admin_private.syms @@ -34,8 +34,8 @@ xdr_admin_server_set_threadpool_parameters_args; xdr_admin_server_update_tls_files_args; # datatypes.h +vir_adm_client_get_type; vir_adm_server_get_type; -virAdmClientClass; virAdmConnectClass; virAdmConnectCloseCallbackDataRegister; virAdmConnectCloseCallbackDataReset; diff --git a/src/datatypes.c b/src/datatypes.c index cfdb9f9650..83028708da 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -71,8 +71,21 @@ virClassPtr virAdmConnectCloseCallbackDataClass; static void virAdmConnectDispose(void *obj); static void virAdmConnectCloseCallbackDataDispose(void *obj); -virClassPtr virAdmClientClass; -static void virAdmClientDispose(void *obj); +G_DEFINE_TYPE(virAdmClient, vir_adm_client, G_TYPE_OBJECT); +static void virAdmClientDispose(GObject *obj); + +static void +vir_adm_client_init(virAdmClient *clt G_GNUC_UNUSED) +{ +} + +static void +vir_adm_client_class_init(virAdmClientClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virAdmClientDispose; +} G_DEFINE_TYPE(virAdmServer, vir_adm_server, G_TYPE_OBJECT); static void virAdmServerDispose(GObject *obj); @@ -121,7 +134,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); - DECLARE_CLASS(virAdmClient); #undef DECLARE_CLASS_COMMON #undef DECLARE_CLASS_LOCKABLE @@ -1228,30 +1240,28 @@ virAdmClientPtr virAdmGetClient(virAdmServerPtr srv, const unsigned long long id, unsigned long long timestamp, unsigned int transport) { - virAdmClientPtr ret = NULL; + g_autoptr(virAdmClient) ret = NULL; if (virDataTypesInitialize() < 0) - goto error; + return NULL; - if (!(ret = virObjectNew(virAdmClientClass))) - goto error; + ret = VIR_ADM_CLIENT(g_object_new(VIR_TYPE_ADM_CLIENT, NULL)); ret->id = id; ret->timestamp = timestamp; ret->transport = transport; ret->srv = g_object_ref(srv); - return ret; - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } static void -virAdmClientDispose(void *obj) +virAdmClientDispose(GObject *obj) { - virAdmClientPtr clt = obj; + virAdmClientPtr clt = VIR_ADM_CLIENT(obj); VIR_DEBUG("release client clt=%p, id=%llu", clt, clt->id); g_clear_object(&clt->srv); + + G_OBJECT_CLASS(vir_adm_client_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index dee21068b3..482ad0274c 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -46,10 +46,11 @@ extern virClassPtr virStorageVolClass; extern virClassPtr virStoragePoolClass; extern virClassPtr virAdmConnectClass; -extern virClassPtr virAdmClientClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() G_DECLARE_FINAL_TYPE(virAdmServer, vir_adm_server, VIR, ADM_SERVER, GObject); +#define VIR_TYPE_ADM_CLIENT vir_adm_client_get_type() +G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckConnectReturn(obj, retval) \ do { \ @@ -447,8 +448,8 @@ G_DECLARE_FINAL_TYPE(virAdmServer, vir_adm_server, VIR, ADM_SERVER, GObject); #define virCheckAdmClientReturn(obj, retval) \ do { \ - virAdmClientPtr _clt = (obj); \ - if (!virObjectIsClass(_clt, virAdmClientClass) || \ + virAdmClientPtr _clt = VIR_ADM_CLIENT(obj); \ + if (_clt == NULL || \ !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ @@ -460,8 +461,8 @@ G_DECLARE_FINAL_TYPE(virAdmServer, vir_adm_server, VIR, ADM_SERVER, GObject); } while (0) #define virCheckAdmClientGoto(obj, label) \ do { \ - virAdmClientPtr _clt = (obj); \ - if (!virObjectIsClass(_clt, virAdmClientClass) || \ + virAdmClientPtr _clt = VIR_ADM_CLIENT(obj); \ + if (_clt == NULL || \ !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ @@ -588,7 +589,7 @@ struct _virAdmServer { * Internal structure associated to a client connected to daemon */ struct _virAdmClient { - virObject parent; + GObject parent; virAdmServerPtr srv; /* pointer to the server client is * connected to, which also holds a * reference back to the admin connection diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index a9152198d7..fa5340d5e8 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -194,6 +194,7 @@ sub get_conn_method { } my %gobject_impl = ( + virAdmClient => 1, virAdmServer => 1, ); -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virdomaincheckpointobjlist.c | 7 +-- src/datatypes.c | 68 ++++++++++++++++++--------- src/datatypes.h | 14 ++++-- src/libvirt-domain-checkpoint.c | 7 ++- src/libvirt_private.syms | 2 +- src/rpc/gendispatch.pl | 1 + 6 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/conf/virdomaincheckpointobjlist.c b/src/conf/virdomaincheckpointobjlist.c index a4942ea706..2c4d484226 100644 --- a/src/conf/virdomaincheckpointobjlist.c +++ b/src/conf/virdomaincheckpointobjlist.c @@ -234,10 +234,7 @@ virDomainListCheckpoints(virDomainCheckpointObjListPtr checkpoints, for (i = 0; i < count; i++) VIR_FREE(names[i]); VIR_FREE(names); - if (ret < 0 && list) { - for (i = 0; i < count; i++) - virObjectUnref(list[i]); - VIR_FREE(list); - } + if (ret < 0) + virGObjectListFreeCount(list, count); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index 83028708da..8a044a2ab3 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,7 +36,6 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virDomainCheckpointClass; virClassPtr virDomainSnapshotClass; virClassPtr virInterfaceClass; virClassPtr virNetworkClass; @@ -52,7 +51,6 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virDomainCheckpointDispose(void *obj); static void virDomainSnapshotDispose(void *obj); static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); @@ -65,6 +63,24 @@ static void virStreamDispose(void *obj); static void virStorageVolDispose(void *obj); static void virStoragePoolDispose(void *obj); +G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); +static void virDomainCheckpointDispose(GObject *obj); +static void virDomainCheckpointFinalize(GObject *obj); + +static void +vir_domain_checkpoint_init(virDomainCheckpoint *dc G_GNUC_UNUSED) +{ +} + +static void +vir_domain_checkpoint_class_init(virDomainCheckpointClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virDomainCheckpointDispose; + obj->finalize = virDomainCheckpointFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -119,7 +135,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virDomainCheckpoint); DECLARE_CLASS(virDomainSnapshot); DECLARE_CLASS(virInterface); DECLARE_CLASS(virNetwork); @@ -986,7 +1001,7 @@ virNWFilterBindingDispose(void *obj) * @name: pointer to the domain checkpoint name * * Allocates a new domain checkpoint 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 domain checkpoint object, or NULL on error. */ @@ -994,25 +1009,20 @@ virDomainCheckpointPtr virGetDomainCheckpoint(virDomainPtr domain, const char *name) { - virDomainCheckpointPtr ret = NULL; + g_autoptr(virDomainCheckpoint) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckDomainGoto(domain, error); - virCheckNonNullArgGoto(name, error); + virCheckDomainReturn(domain, NULL); + virCheckNonNullArgReturn(name, NULL); - if (!(ret = virObjectNew(virDomainCheckpointClass))) - goto error; + ret = VIR_DOMAIN_CHECKPOINT(g_object_new(VIR_TYPE_DOMAIN_CHECKPOINT, NULL)); ret->name = g_strdup(name); ret->domain = virObjectRef(domain); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -1020,20 +1030,36 @@ virGetDomainCheckpoint(virDomainPtr domain, * virDomainCheckpointDispose: * @obj: the domain checkpoint to release * - * Unconditionally release all memory associated with a checkpoint. - * The checkpoint 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 +virDomainCheckpointDispose(GObject *obj) +{ + virDomainCheckpointPtr checkpoint = VIR_DOMAIN_CHECKPOINT(obj); + + virObjectUnref(checkpoint->domain); + checkpoint->domain = NULL; + + G_OBJECT_CLASS(vir_domain_checkpoint_parent_class)->dispose(obj); +} + +/** + * virDomainCheckpointFinalize: + * @obj: the domain checkpoint 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 checkpoint. + * The checkpoint object must not be used once this method returns. */ static void -virDomainCheckpointDispose(void *obj) +virDomainCheckpointFinalize(GObject *obj) { - virDomainCheckpointPtr checkpoint = obj; + virDomainCheckpointPtr checkpoint = VIR_DOMAIN_CHECKPOINT(obj); VIR_DEBUG("release checkpoint %p %s", checkpoint, checkpoint->name); VIR_FREE(checkpoint->name); - virObjectUnref(checkpoint->domain); + + G_OBJECT_CLASS(vir_domain_checkpoint_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 482ad0274c..e6817dc43d 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virDomainCheckpointClass; extern virClassPtr virDomainSnapshotClass; extern virClassPtr virInterfaceClass; extern virClassPtr virNetworkClass; @@ -45,6 +44,13 @@ extern virClassPtr virStreamClass; extern virClassPtr virStorageVolClass; extern virClassPtr virStoragePoolClass; +#define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() +G_DECLARE_FINAL_TYPE(virDomainCheckpoint, + vir_domain_checkpoint, + VIR, + DOMAIN_CHECKPOINT, + GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -327,8 +333,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckDomainCheckpointReturn(obj, retval) \ do { \ - virDomainCheckpointPtr _check = (obj); \ - if (!virObjectIsClass(_check, virDomainCheckpointClass) || \ + virDomainCheckpointPtr _check = VIR_DOMAIN_CHECKPOINT(obj); \ + if (_check == NULL || \ !virObjectIsClass(_check->domain, virDomainClass) || \ !virObjectIsClass(_check->domain->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOMAIN_CHECKPOINT, \ @@ -749,7 +755,7 @@ struct _virStream { * Internal structure associated with a domain checkpoint */ struct _virDomainCheckpoint { - virObject parent; + GObject parent; char *name; virDomainPtr domain; }; diff --git a/src/libvirt-domain-checkpoint.c b/src/libvirt-domain-checkpoint.c index 432c2d5a52..77ef434d30 100644 --- a/src/libvirt-domain-checkpoint.c +++ b/src/libvirt-domain-checkpoint.c @@ -535,14 +535,13 @@ virDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, int virDomainCheckpointRef(virDomainCheckpointPtr checkpoint) { - VIR_DEBUG("checkpoint=%p, refs=%d", checkpoint, - checkpoint ? checkpoint->parent.u.s.refs : 0); + VIR_DEBUG("checkpoint=%p", checkpoint); virResetLastError(); virCheckDomainCheckpointReturn(checkpoint, -1); - virObjectRef(checkpoint); + g_object_ref(checkpoint); return 0; } @@ -565,6 +564,6 @@ virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) virCheckDomainCheckpointReturn(checkpoint, -1); - virObjectUnref(checkpoint); + g_object_unref(checkpoint); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1a46ec3e3e..bfce1b0b8f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1346,13 +1346,13 @@ virCPUx86FeatureFilterDropMSR; virCPUx86FeatureFilterSelectMSR; # datatypes.h +vir_domain_checkpoint_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; -virDomainCheckpointClass; virDomainClass; virDomainSnapshotClass; virGetConnect; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index fa5340d5e8..c8fba3dc2b 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -196,6 +196,7 @@ sub get_conn_method { my %gobject_impl = ( virAdmClient => 1, virAdmServer => 1, + virDomainCheckpoint => 1, ); sub use_gobject { -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virdomainsnapshotobjlist.c | 7 +-- src/datatypes.c | 68 ++++++++++++++++++++--------- src/datatypes.h | 12 +++-- src/libvirt-domain-snapshot.c | 7 ++- src/libvirt_private.syms | 2 +- src/qemu/qemu_driver.c | 3 +- src/rpc/gendispatch.pl | 1 + 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/conf/virdomainsnapshotobjlist.c b/src/conf/virdomainsnapshotobjlist.c index 910b0714c7..54d1892dc4 100644 --- a/src/conf/virdomainsnapshotobjlist.c +++ b/src/conf/virdomainsnapshotobjlist.c @@ -285,10 +285,7 @@ virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, for (i = 0; i < count; i++) VIR_FREE(names[i]); VIR_FREE(names); - if (ret < 0 && list) { - for (i = 0; i < count; i++) - virObjectUnref(list[i]); - VIR_FREE(list); - } + if (ret < 0) + virGObjectListFreeCount(list, count); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index 8a044a2ab3..334d9d8638 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,7 +36,6 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virDomainSnapshotClass; virClassPtr virInterfaceClass; virClassPtr virNetworkClass; virClassPtr virNetworkPortClass; @@ -51,7 +50,6 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virDomainSnapshotDispose(void *obj); static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); static void virNetworkPortDispose(void *obj); @@ -81,6 +79,24 @@ vir_domain_checkpoint_class_init(virDomainCheckpointClass *klass) obj->finalize = virDomainCheckpointFinalize; } +G_DEFINE_TYPE(virDomainSnapshot, vir_domain_snapshot, G_TYPE_OBJECT); +static void virDomainSnapshotDispose(GObject *obj); +static void virDomainSnapshotFinalize(GObject *obj); + +static void +vir_domain_snapshot_init(virDomainSnapshot *ds G_GNUC_UNUSED) +{ +} + +static void +vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virDomainSnapshotDispose; + obj->finalize = virDomainSnapshotFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -135,7 +151,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virDomainSnapshot); DECLARE_CLASS(virInterface); DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); @@ -1069,32 +1084,27 @@ virDomainCheckpointFinalize(GObject *obj) * @name: pointer to the domain snapshot name * * Allocates a new domain snapshot 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 domain snapshot object, or NULL on error. */ virDomainSnapshotPtr virGetDomainSnapshot(virDomainPtr domain, const char *name) { - virDomainSnapshotPtr ret = NULL; + g_autoptr(virDomainSnapshot) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckDomainGoto(domain, error); - virCheckNonNullArgGoto(name, error); + virCheckDomainReturn(domain, NULL); + virCheckNonNullArgReturn(name, NULL); - if (!(ret = virObjectNew(virDomainSnapshotClass))) - goto error; + ret = VIR_DOMAIN_SNAPSHOT(g_object_new(VIR_TYPE_DOMAIN_SNAPSHOT, NULL)); ret->name = g_strdup(name); ret->domain = virObjectRef(domain); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -1102,20 +1112,36 @@ virGetDomainSnapshot(virDomainPtr domain, const char *name) * virDomainSnapshotDispose: * @obj: the domain snapshot to release * - * Unconditionally release all memory associated with a snapshot. - * The snapshot 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 +virDomainSnapshotDispose(GObject *obj) +{ + virDomainSnapshotPtr snapshot = VIR_DOMAIN_SNAPSHOT(obj); + + virObjectUnref(snapshot->domain); + snapshot->domain = NULL; + + G_OBJECT_CLASS(vir_domain_snapshot_parent_class)->dispose(obj); +} + +/** + * virDomainSnapshotFinalize: + * @obj: the domain snapshot 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 snapshot. + * The snapshot object must not be used once this method returns. */ static void -virDomainSnapshotDispose(void *obj) +virDomainSnapshotFinalize(GObject *obj) { - virDomainSnapshotPtr snapshot = obj; + virDomainSnapshotPtr snapshot = VIR_DOMAIN_SNAPSHOT(obj); VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name); VIR_FREE(snapshot->name); - virObjectUnref(snapshot->domain); + + G_OBJECT_CLASS(vir_domain_snapshot_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index e6817dc43d..9dfb18147e 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virDomainSnapshotClass; extern virClassPtr virInterfaceClass; extern virClassPtr virNetworkClass; extern virClassPtr virNetworkPortClass; @@ -51,6 +50,13 @@ G_DECLARE_FINAL_TYPE(virDomainCheckpoint, DOMAIN_CHECKPOINT, GObject); +#define VIR_TYPE_DOMAIN_SNAPSHOT vir_domain_snapshot_get_type() +G_DECLARE_FINAL_TYPE(virDomainSnapshot, + vir_domain_snapshot, + VIR, + DOMAIN_SNAPSHOT, + GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -349,7 +355,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckDomainSnapshotReturn(obj, retval) \ do { \ virDomainSnapshotPtr _snap = (obj); \ - if (!virObjectIsClass(_snap, virDomainSnapshotClass) || \ + if (_snap == NULL || \ !virObjectIsClass(_snap->domain, virDomainClass) || \ !virObjectIsClass(_snap->domain->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOMAIN_SNAPSHOT, \ @@ -767,7 +773,7 @@ struct _virDomainCheckpoint { * Internal structure associated with a domain snapshot */ struct _virDomainSnapshot { - virObject parent; + GObject parent; char *name; virDomainPtr domain; }; diff --git a/src/libvirt-domain-snapshot.c b/src/libvirt-domain-snapshot.c index 33593e11e9..ce870de712 100644 --- a/src/libvirt-domain-snapshot.c +++ b/src/libvirt-domain-snapshot.c @@ -1080,14 +1080,13 @@ virDomainSnapshotDelete(virDomainSnapshotPtr snapshot, int virDomainSnapshotRef(virDomainSnapshotPtr snapshot) { - VIR_DEBUG("snapshot=%p, refs=%d", snapshot, - snapshot ? snapshot->parent.u.s.refs : 0); + VIR_DEBUG("snapshot=%p", snapshot); virResetLastError(); virCheckDomainSnapshotReturn(snapshot, -1); - virObjectRef(snapshot); + g_object_ref(snapshot); return 0; } @@ -1110,6 +1109,6 @@ virDomainSnapshotFree(virDomainSnapshotPtr snapshot) virCheckDomainSnapshotReturn(snapshot, -1); - virObjectUnref(snapshot); + g_object_unref(snapshot); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index bfce1b0b8f..33d6d4950d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1347,6 +1347,7 @@ virCPUx86FeatureFilterSelectMSR; # datatypes.h vir_domain_checkpoint_get_type; +vir_domain_snapshot_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1354,7 +1355,6 @@ virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; virDomainClass; -virDomainSnapshotClass; virGetConnect; virGetDomain; virGetDomainCheckpoint; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 9b353c6f07..46851d063e 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15846,8 +15846,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, cfg->snapshotDir) < 0) { /* if writing of metadata fails, error out rather than trying * to silently carry on without completing the snapshot */ - virObjectUnref(snapshot); - snapshot = NULL; + g_clear_object(&snapshot); virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to save metadata for snapshot %s"), snap->def->name); diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index c8fba3dc2b..bca3b33e80 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -197,6 +197,7 @@ my %gobject_impl = ( virAdmClient => 1, virAdmServer => 1, virDomainCheckpoint => 1, + virDomainSnapshot => 1, ); sub use_gobject { -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virnwfilterobj.c | 7 +--- src/datatypes.c | 70 +++++++++++++++++++++++++++------------ src/datatypes.h | 10 +++--- src/libvirt-nwfilter.c | 7 ++-- src/libvirt_private.syms | 2 +- src/rpc/gendispatch.pl | 1 + 6 files changed, 60 insertions(+), 37 deletions(-) diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c index c9e224061d..c08dc7a5d6 100644 --- a/src/conf/virnwfilterobj.c +++ b/src/conf/virnwfilterobj.c @@ -472,12 +472,7 @@ virNWFilterObjListExport(virConnectPtr conn, ret = nfilters; cleanup: - if (tmp_filters) { - for (i = 0; i < nfilters; i ++) - virObjectUnref(tmp_filters[i]); - } - VIR_FREE(tmp_filters); - + virGObjectListFreeCount(tmp_filters, nfilters); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index 334d9d8638..e9f6cafd72 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -40,7 +40,6 @@ virClassPtr virInterfaceClass; virClassPtr virNetworkClass; virClassPtr virNetworkPortClass; virClassPtr virNodeDeviceClass; -virClassPtr virNWFilterClass; virClassPtr virNWFilterBindingClass; virClassPtr virSecretClass; virClassPtr virStreamClass; @@ -54,7 +53,6 @@ static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); static void virNetworkPortDispose(void *obj); static void virNodeDeviceDispose(void *obj); -static void virNWFilterDispose(void *obj); static void virNWFilterBindingDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); @@ -97,6 +95,24 @@ vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) obj->finalize = virDomainSnapshotFinalize; } +G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); +static void virNWFilterDispose(GObject *obj); +static void virNWFilterFinalize(GObject *obj); + +static void +vir_nw_filter_init(virNWFilter *filter G_GNUC_UNUSED) +{ +} + +static void +vir_nw_filter_class_init(virNWFilterClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virNWFilterDispose; + obj->finalize = virNWFilterFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -155,7 +171,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); DECLARE_CLASS(virNodeDevice); - DECLARE_CLASS(virNWFilter); DECLARE_CLASS(virNWFilterBinding); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); @@ -889,7 +904,7 @@ virStreamDispose(void *obj) * @uuid: pointer to the uuid * * Allocates a new network filter 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 filter object, or NULL on error. */ @@ -897,17 +912,16 @@ virNWFilterPtr virGetNWFilter(virConnectPtr conn, const char *name, const unsigned char *uuid) { - virNWFilterPtr ret = NULL; + g_autoptr(virNWFilter) 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(virNWFilterClass))) - goto error; + ret = VIR_NW_FILTER(g_object_new(VIR_TYPE_NW_FILTER, NULL)); ret->name = g_strdup(name); @@ -915,11 +929,7 @@ virGetNWFilter(virConnectPtr conn, const char *name, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -927,23 +937,39 @@ virGetNWFilter(virConnectPtr conn, const char *name, * virNWFilterDispose: * @obj: the network filter to release * - * Unconditionally release all memory associated with a nwfilter. - * The nwfilter 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 +virNWFilterDispose(GObject *obj) +{ + virNWFilterPtr nwfilter = VIR_NW_FILTER(obj); + + virObjectUnref(nwfilter->conn); + nwfilter->conn = NULL; + + G_OBJECT_CLASS(vir_nw_filter_parent_class)->dispose(obj); +} + +/** + * virNWFilterFinalize: + * @obj: the network filter 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 nwfilter. + * The nwfilter object must not be used once this method returns. */ static void -virNWFilterDispose(void *obj) +virNWFilterFinalize(GObject *obj) { - virNWFilterPtr nwfilter = obj; + virNWFilterPtr nwfilter = VIR_NW_FILTER(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(nwfilter->uuid, uuidstr); VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr); VIR_FREE(nwfilter->name); - virObjectUnref(nwfilter->conn); + + G_OBJECT_CLASS(vir_nw_filter_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 9dfb18147e..b51514d579 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 virNWFilterClass; extern virClassPtr virNWFilterBindingClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; @@ -57,6 +56,9 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot, DOMAIN_SNAPSHOT, GObject); +#define VIR_TYPE_NW_FILTER vir_nw_filter_get_type() +G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -311,8 +313,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckNWFilterReturn(obj, retval) \ do { \ - virNWFilterPtr _nw = (obj); \ - if (!virObjectIsClass(_nw, virNWFilterClass) || \ + virNWFilterPtr _nw = VIR_NW_FILTER(obj); \ + if (_nw == NULL || \ !virObjectIsClass(_nw->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NWFILTER, \ VIR_ERR_INVALID_NWFILTER, \ @@ -784,7 +786,7 @@ struct _virDomainSnapshot { * Internal structure associated to a network filter */ struct _virNWFilter { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* the network filter external name */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the network filter unique identifier */ diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c index d28220db8a..cd3f5759b7 100644 --- a/src/libvirt-nwfilter.c +++ b/src/libvirt-nwfilter.c @@ -281,7 +281,7 @@ virNWFilterFree(virNWFilterPtr nwfilter) virCheckNWFilterReturn(nwfilter, -1); - virObjectUnref(nwfilter); + g_object_unref(nwfilter); return 0; } @@ -503,14 +503,13 @@ virNWFilterGetXMLDesc(virNWFilterPtr nwfilter, unsigned int flags) int virNWFilterRef(virNWFilterPtr nwfilter) { - VIR_DEBUG("nwfilter=%p refs=%d", nwfilter, - nwfilter ? nwfilter->parent.u.s.refs : 0); + VIR_DEBUG("nwfilter=%p", nwfilter); virResetLastError(); virCheckNWFilterReturn(nwfilter, -1); - virObjectRef(nwfilter); + g_object_ref(nwfilter); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 33d6d4950d..44ffeb6abc 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_nw_filter_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1374,7 +1375,6 @@ virNetworkClass; virNetworkPortClass; virNewConnectCloseCallbackData; virNodeDeviceClass; -virNWFilterClass; virSecretClass; virStoragePoolClass; virStorageVolClass; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index bca3b33e80..b51e2715ee 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -198,6 +198,7 @@ my %gobject_impl = ( virAdmServer => 1, virDomainCheckpoint => 1, virDomainSnapshot => 1, + virNWFilter => 1, ); sub use_gobject { -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virnwfilterbindingobjlist.c | 2 +- src/datatypes.c | 68 +++++++++++++++++++--------- src/datatypes.h | 14 ++++-- src/libvirt-nwfilter.c | 7 ++- src/libvirt_private.syms | 1 + src/nwfilter/nwfilter_driver.c | 3 +- src/rpc/gendispatch.pl | 1 + 7 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/conf/virnwfilterbindingobjlist.c b/src/conf/virnwfilterbindingobjlist.c index 97e4d3c49b..4f4f295b92 100644 --- a/src/conf/virnwfilterbindingobjlist.c +++ b/src/conf/virnwfilterbindingobjlist.c @@ -489,7 +489,7 @@ virNWFilterBindingObjListExport(virNWFilterBindingObjListPtr bindings, cleanup: virObjectListFreeCount(bindingobjs, nbindings); if (ret < 0) { - virObjectListFreeCount(*bindinglist, nbindings); + virGObjectListFreeCount(*bindinglist, nbindings); *bindinglist = NULL; } return ret; diff --git a/src/datatypes.c b/src/datatypes.c index e9f6cafd72..a562b5da5f 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); @@ -113,6 +111,24 @@ vir_nw_filter_class_init(virNWFilterClass *klass) obj->finalize = virNWFilterFinalize; } +G_DEFINE_TYPE(virNWFilterBinding, vir_nw_filter_binding, G_TYPE_OBJECT); +static void virNWFilterBindingDispose(GObject *obj); +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->dispose = virNWFilterBindingDispose; + obj->finalize = virNWFilterBindingFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -171,7 +187,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); DECLARE_CLASS(virNodeDevice); - DECLARE_CLASS(virNWFilterBinding); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS(virStorageVol); @@ -980,7 +995,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. */ @@ -988,16 +1003,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); @@ -1005,11 +1019,7 @@ virGetNWFilterBinding(virConnectPtr conn, const char *portdev, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -1017,22 +1027,38 @@ virGetNWFilterBinding(virConnectPtr conn, const char *portdev, * virNWFilterBindingDispose: * @obj: the network filter binding to release * - * Unconditionally release all memory associated with a nwfilter binding. - * The nwfilter binding 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 +virNWFilterBindingDispose(GObject *obj) +{ + virNWFilterBindingPtr binding = VIR_NW_FILTER_BINDING(obj); + + virObjectUnref(binding->conn); + binding->conn = NULL; + + G_OBJECT_CLASS(vir_nw_filter_binding_parent_class)->dispose(obj); +} + +/** + * virNWFilterBindingFinalize: + * @obj: the network filter binding 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 nwfilter binding. + * The nwfilter binding object must not be used once this method returns. */ 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 b51514d579..5f0ab5c84b 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; @@ -59,6 +58,13 @@ G_DECLARE_FINAL_TYPE(virDomainSnapshot, #define VIR_TYPE_NW_FILTER vir_nw_filter_get_type() G_DECLARE_FINAL_TYPE(virNWFilter, vir_nw_filter, VIR, NW_FILTER, 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); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -327,8 +333,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 (_nw == NULL || \ !virObjectIsClass(_nw->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_NWFILTER, \ VIR_ERR_INVALID_NWFILTER_BINDING, \ @@ -799,7 +805,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/libvirt_private.syms b/src/libvirt_private.syms index 44ffeb6abc..ba4a5f0986 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_nw_filter_binding_get_type; vir_nw_filter_get_type; virConnectClass; virConnectCloseCallbackDataCall; 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); diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index b51e2715ee..553ede5024 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -199,6 +199,7 @@ my %gobject_impl = ( virDomainCheckpoint => 1, virDomainSnapshot => 1, virNWFilter => 1, + virNWFilterBinding => 1, ); sub use_gobject { -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_conf.c | 6 +-- src/conf/network_event.c | 7 +-- src/conf/virnetworkobj.c | 5 +- src/datatypes.c | 74 +++++++++++++++++++---------- 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, 79 insertions(+), 67 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 85afadeb06..57a2c09b91 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -31571,7 +31571,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; @@ -31592,7 +31592,6 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, cleanup: virObjectUnref(port); - virObjectUnref(net); return ret; } @@ -31645,7 +31644,7 @@ int virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, virNetDevBandwidthPtr newBandwidth) { - virNetworkPtr net = NULL; + g_autoptr(virNetwork) net = NULL; virNetworkPortPtr port = NULL; virTypedParameterPtr params = NULL; int nparams = 0; @@ -31672,7 +31671,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 a562b5da5f..cb4c5705b2 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); @@ -93,6 +91,24 @@ vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) obj->finalize = virDomainSnapshotFinalize; } +G_DEFINE_TYPE(virNetwork, vir_network, G_TYPE_OBJECT); +static void virNetworkDispose(GObject *obj); +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->dispose = virNetworkDispose; + obj->finalize = virNetworkFinalize; +} + G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); static void virNWFilterDispose(GObject *obj); static void virNWFilterFinalize(GObject *obj); @@ -184,7 +200,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); @@ -421,58 +436,69 @@ 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: * @obj: the network to release * - * Unconditionally release all memory associated with a network. - * The network 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 +virNetworkDispose(GObject *obj) +{ + virNetworkPtr network = VIR_NETWORK(obj); + + virObjectUnref(network->conn); + network->conn = NULL; + + G_OBJECT_CLASS(vir_network_parent_class)->dispose(obj); +} + +/** + * virNetworkFinalize: + * @obj: the network 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 network. + * The network object must not be used once this method returns. */ 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); VIR_DEBUG("release network %p %s %s", network, network->name, uuidstr); VIR_FREE(network->name); - virObjectUnref(network->conn); + + G_OBJECT_CLASS(vir_network_parent_class)->finalize(obj); } @@ -500,7 +526,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; @@ -529,7 +555,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 ba4a5f0986..b41eee4e59 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_network_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; virConnectClass; @@ -1372,7 +1373,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virInterfaceClass; -virNetworkClass; virNetworkPortClass; virNewConnectCloseCallbackData; virNodeDeviceClass; diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index b7c119366e..be37e0835d 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -1201,7 +1201,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; @@ -1400,7 +1400,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 9a385eba0d..3fc4456c75 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -1709,7 +1709,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", @@ -1717,7 +1717,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 5f9aa7e8a6..f0f2a075fb 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 09e3e8c51b..d479c3a9df 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.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_conf.c | 6 +-- src/conf/virnetworkobj.c | 5 +-- src/datatypes.c | 66 ++++++++++++++++++++--------- 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, 64 insertions(+), 49 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 57a2c09b91..fa87fb0590 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -31572,7 +31572,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 */ @@ -31591,7 +31591,6 @@ virDomainNetReleaseActualDevice(virConnectPtr conn, goto cleanup; cleanup: - virObjectUnref(port); return ret; } @@ -31645,7 +31644,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; @@ -31670,7 +31669,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 cb4c5705b2..f06438c1c3 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); @@ -109,6 +107,24 @@ vir_network_class_init(virNetworkClass *klass) obj->finalize = virNetworkFinalize; } +G_DEFINE_TYPE(virNetworkPort, vir_network_port, G_TYPE_OBJECT); +static void virNetworkPortDispose(GObject *obj); +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->dispose = virNetworkPortDispose; + obj->finalize = virNetworkPortFinalize; +} + G_DEFINE_TYPE(virNWFilter, vir_nw_filter, G_TYPE_OBJECT); static void virNWFilterDispose(GObject *obj); static void virNWFilterFinalize(GObject *obj); @@ -200,7 +216,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); @@ -508,54 +523,63 @@ 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: * @obj: the network port to release * - * Unconditionally release all memory associated with a network port. - * The network port object must not be used once this method returns. + * Unreferences the associated network object, which may also be + * released if its ref count hits zero. + */ +static void +virNetworkPortDispose(GObject *obj) +{ + virNetworkPortPtr port = VIR_NETWORK_PORT(obj); + + g_clear_object(&port->net); + + G_OBJECT_CLASS(vir_network_port_parent_class)->dispose(obj); +} + +/** + * virNetworkPortFinalize: + * @obj: the network port to release * - * It will also unreference the associated network object, - * which may also be released if its ref count hits zero. + * Unconditionally releases all memory associated with a network port. + * The network port object must not be used once this method returns. */ 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_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 b41eee4e59..69134087c1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1349,6 +1349,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; @@ -1373,7 +1374,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 f0f2a075fb..2e4e1a65db 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 d479c3a9df..9373ba78b7 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.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- 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

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/storage_event.c | 15 +++---- src/conf/virstorageobj.c | 2 +- src/datatypes.c | 70 ++++++++++++++++++++--------- src/datatypes.h | 15 ++++--- src/libvirt-storage.c | 6 +-- src/libvirt_private.syms | 2 +- src/qemu/qemu_migration.c | 3 +- src/remote/remote_daemon_dispatch.c | 3 +- src/remote/remote_driver.c | 6 +-- src/rpc/gendispatch.pl | 1 + src/vz/vz_sdk.c | 3 +- 11 files changed, 73 insertions(+), 53 deletions(-) diff --git a/src/conf/storage_event.c b/src/conf/storage_event.c index de36ec3af9..e18069f718 100644 --- a/src/conf/storage_event.c +++ b/src/conf/storage_event.c @@ -110,10 +110,10 @@ virStoragePoolEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virStoragePoolPtr pool = virGetStoragePool(conn, - event->meta.name, - event->meta.uuid, - NULL, NULL); + g_autoptr(virStoragePool) pool = virGetStoragePool(conn, + event->meta.name, + event->meta.uuid, + NULL, NULL); if (!pool) return; @@ -127,23 +127,20 @@ virStoragePoolEventDispatchDefaultFunc(virConnectPtr conn, storagePoolLifecycleEvent->type, storagePoolLifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_STORAGE_POOL_EVENT_ID_REFRESH: { ((virConnectStoragePoolEventGenericCallback)cb)(conn, pool, cbopaque); - goto cleanup; + return; } case VIR_STORAGE_POOL_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(pool); } diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c index 5cbd30f93c..4c25c9a9f8 100644 --- a/src/conf/virstorageobj.c +++ b/src/conf/virstorageobj.c @@ -2095,6 +2095,6 @@ virStoragePoolObjListExport(virConnectPtr conn, return data.nPools; error: - virObjectListFree(data.pools); + virGObjectListFreeCount(data.pools, data.nPools); return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index 455bcd70ef..a9a942b3a6 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -40,7 +40,6 @@ virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; virClassPtr virStreamClass; virClassPtr virStorageVolClass; -virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); @@ -49,7 +48,6 @@ static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); static void virStorageVolDispose(void *obj); -static void virStoragePoolDispose(void *obj); G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); static void virDomainCheckpointDispose(GObject *obj); @@ -177,6 +175,24 @@ vir_nw_filter_binding_class_init(virNWFilterBindingClass *klass) obj->finalize = virNWFilterBindingFinalize; } +G_DEFINE_TYPE(virStoragePool, vir_storage_pool, G_TYPE_OBJECT); +static void virStoragePoolDispose(GObject *obj); +static void virStoragePoolFinalize(GObject *obj); + +static void +vir_storage_pool_init(virStoragePool *pool G_GNUC_UNUSED) +{ +} + +static void +vir_storage_pool_class_init(virStoragePoolClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virStoragePoolDispose; + obj->finalize = virStoragePoolFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -235,7 +251,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS(virStorageVol); - DECLARE_CLASS(virStoragePool); DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); @@ -681,7 +696,7 @@ virInterfaceFinalize(GObject *obj) * @freeFunc: private data cleanup function pointer specific to driver * * Allocates a new storage pool 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 storage pool object, or NULL on error. */ @@ -690,17 +705,16 @@ virGetStoragePool(virConnectPtr conn, const char *name, const unsigned char *uuid, void *privateData, virFreeCallback freeFunc) { - virStoragePoolPtr ret = NULL; + g_autoptr(virStoragePool) 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(virStoragePoolClass))) - goto error; + ret = VIR_STORAGE_POOL(g_object_new(VIR_TYPE_STORAGE_POOL, NULL)); ret->name = g_strdup(name); @@ -711,11 +725,7 @@ virGetStoragePool(virConnectPtr conn, const char *name, ret->privateData = privateData; ret->privateDataFreeFunc = freeFunc; - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -723,16 +733,31 @@ virGetStoragePool(virConnectPtr conn, const char *name, * virStoragePoolDispose: * @obj: the storage pool to release * - * Unconditionally release all memory associated with a pool. - * The pool 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 +virStoragePoolDispose(GObject *obj) +{ + virStoragePoolPtr pool = VIR_STORAGE_POOL(obj); + + virObjectUnref(pool->conn); + pool->conn = NULL; + + G_OBJECT_CLASS(vir_storage_pool_parent_class)->dispose(obj); +} + +/** + * virStoragePoolFinalize: + * @obj: the storage pool 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 pool. + * The pool object must not be used once this method returns. */ static void -virStoragePoolDispose(void *obj) +virStoragePoolFinalize(GObject *obj) { - virStoragePoolPtr pool = obj; + virStoragePoolPtr pool = VIR_STORAGE_POOL(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(pool->uuid, uuidstr); @@ -742,7 +767,8 @@ virStoragePoolDispose(void *obj) pool->privateDataFreeFunc(pool->privateData); VIR_FREE(pool->name); - virObjectUnref(pool->conn); + + G_OBJECT_CLASS(vir_storage_pool_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index e9bc1574e7..2c8256388c 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -36,7 +36,6 @@ extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; extern virClassPtr virStorageVolClass; -extern virClassPtr virStoragePoolClass; #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() G_DECLARE_FINAL_TYPE(virDomainCheckpoint, @@ -71,6 +70,9 @@ G_DECLARE_FINAL_TYPE(virNWFilterBinding, NW_FILTER_BINDING, GObject); +#define VIR_TYPE_STORAGE_POOL vir_storage_pool_get_type() +G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -191,8 +193,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckStoragePoolReturn(obj, retval) \ do { \ - virStoragePoolPtr _pool = (obj); \ - if (!virObjectIsClass(_pool, virStoragePoolClass) || \ + virStoragePoolPtr _pool = VIR_STORAGE_POOL(obj); \ + if (_pool == NULL || \ !virObjectIsClass(_pool->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STORAGE, \ VIR_ERR_INVALID_STORAGE_POOL, \ @@ -205,8 +207,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckStoragePoolGoto(obj, label) \ do { \ - virStoragePoolPtr _pool= (obj); \ - if (!virObjectIsClass(_pool, virStoragePoolClass) || \ + virStoragePoolPtr _pool= VIR_STORAGE_POOL(obj); \ + if (_pool == NULL || \ !virObjectIsClass(_pool->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STORAGE, \ VIR_ERR_INVALID_STORAGE_POOL, \ @@ -683,7 +685,7 @@ struct _virInterface { * Internal structure associated to a storage pool */ struct _virStoragePool { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* the storage pool external name */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the storage pool unique identifier */ @@ -696,7 +698,6 @@ struct _virStoragePool { virFreeCallback privateDataFreeFunc; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePool, virObjectUnref); /** diff --git a/src/libvirt-storage.c b/src/libvirt-storage.c index 0406fe84d3..b88b2162de 100644 --- a/src/libvirt-storage.c +++ b/src/libvirt-storage.c @@ -846,7 +846,7 @@ virStoragePoolFree(virStoragePoolPtr pool) virCheckStoragePoolReturn(pool, -1); - virObjectUnref(pool); + g_object_unref(pool); return 0; } @@ -872,13 +872,13 @@ virStoragePoolFree(virStoragePoolPtr pool) int virStoragePoolRef(virStoragePoolPtr pool) { - VIR_DEBUG("pool=%p refs=%d", pool, pool ? pool->parent.u.s.refs : 0); + VIR_DEBUG("pool=%p", pool); virResetLastError(); virCheckStoragePoolReturn(pool, -1); - virObjectRef(pool); + g_object_ref(pool); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1549a85133..94009505bc 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1353,6 +1353,7 @@ vir_network_get_type; vir_network_port_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; +vir_storage_pool_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1377,7 +1378,6 @@ virGetStream; virNewConnectCloseCallbackData; virNodeDeviceClass; virSecretClass; -virStoragePoolClass; virStorageVolClass; virStreamClass; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index bc280e856a..de65c730f7 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -174,7 +174,7 @@ qemuMigrationDstPrecreateDisk(virConnectPtr conn, unsigned long long capacity) { int ret = -1; - virStoragePoolPtr pool = NULL; + g_autoptr(virStoragePool) pool = NULL; virStorageVolPtr vol = NULL; char *volName = NULL, *basePath = NULL; char *volStr = NULL; @@ -269,7 +269,6 @@ qemuMigrationDstPrecreateDisk(virConnectPtr conn, VIR_FREE(basePath); VIR_FREE(volStr); virObjectUnref(vol); - virObjectUnref(pool); return ret; } diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 2e4e1a65db..e7b2a5f363 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -6136,7 +6136,7 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U daemonClientEventCallbackPtr ref; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - virStoragePoolPtr pool = NULL; + g_autoptr(virStoragePool) pool = NULL; virConnectPtr conn = remoteGetStorageConn(client); virMutexLock(&priv->lock); @@ -6194,7 +6194,6 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(pool); return rv; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 9373ba78b7..745033befb 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5477,7 +5477,7 @@ remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_storage_pool_event_lifecycle_msg *msg = evdata; - virStoragePoolPtr pool; + g_autoptr(virStoragePool) pool = NULL; virObjectEventPtr event = NULL; pool = get_nonnull_storage_pool(conn, msg->pool); @@ -5486,7 +5486,6 @@ remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid, msg->event, msg->detail); - virObjectUnref(pool); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5499,7 +5498,7 @@ remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_storage_pool_event_refresh_msg *msg = evdata; - virStoragePoolPtr pool; + g_autoptr(virStoragePool) pool = NULL; virObjectEventPtr event = NULL; pool = get_nonnull_storage_pool(conn, msg->pool); @@ -5507,7 +5506,6 @@ remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog G_GNUC_UNUSED, return; event = virStoragePoolEventRefreshNew(pool->name, pool->uuid); - virObjectUnref(pool); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 0f16ca89c2..8d19651367 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -203,6 +203,7 @@ my %gobject_impl = ( virNetworkPort => 1, virNWFilter => 1, virNWFilterBinding => 1, + virStoragePool => 1, ); sub use_gobject { diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c index 9cee6f1fde..ed3d5ee2c0 100644 --- a/src/vz/vz_sdk.c +++ b/src/vz/vz_sdk.c @@ -4081,7 +4081,7 @@ prlsdkCreateVm(vzDriverPtr driver, virDomainDefPtr def) static int virStorageTranslatePoolLocal(virConnectPtr conn, virStorageSourcePtr src) { - virStoragePoolPtr pool = NULL; + g_autoptr(virStoragePool) pool = NULL; virStorageVolPtr vol = NULL; virStorageVolInfo info; int ret = -1; @@ -4115,7 +4115,6 @@ virStorageTranslatePoolLocal(virConnectPtr conn, virStorageSourcePtr src) ret = 0; cleanup: - virObjectUnref(pool); virObjectUnref(vol); return ret; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virstorageobj.c | 2 +- src/datatypes.c | 72 ++++++++++++++++++++++++------------ src/datatypes.h | 15 ++++---- src/libvirt-storage.c | 6 +-- src/libvirt_private.syms | 2 +- src/qemu/qemu_migration.c | 3 +- src/rpc/gendispatch.pl | 1 + src/storage/storage_driver.c | 7 ++-- src/vz/vz_sdk.c | 19 ++++------ 9 files changed, 74 insertions(+), 53 deletions(-) diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c index 4c25c9a9f8..d4ea1df81d 100644 --- a/src/conf/virstorageobj.c +++ b/src/conf/virstorageobj.c @@ -1052,7 +1052,7 @@ virStoragePoolObjVolumeListExport(virConnectPtr conn, return data.nvols; error: - virObjectListFree(data.vols); + virGObjectListFreeCount(data.vols, data.nvols); return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index a9a942b3a6..66d4f478ab 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -39,7 +39,6 @@ virClassPtr virDomainClass; virClassPtr virNodeDeviceClass; virClassPtr virSecretClass; virClassPtr virStreamClass; -virClassPtr virStorageVolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); @@ -47,7 +46,6 @@ static void virDomainDispose(void *obj); static void virNodeDeviceDispose(void *obj); static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); -static void virStorageVolDispose(void *obj); G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); static void virDomainCheckpointDispose(GObject *obj); @@ -193,6 +191,24 @@ vir_storage_pool_class_init(virStoragePoolClass *klass) obj->finalize = virStoragePoolFinalize; } +G_DEFINE_TYPE(virStorageVol, vir_storage_vol, G_TYPE_OBJECT); +static void virStorageVolDispose(GObject *obj); +static void virStorageVolFinalize(GObject *obj); + +static void +vir_storage_vol_init(virStorageVol *vol G_GNUC_UNUSED) +{ +} + +static void +vir_storage_vol_class_init(virStorageVolClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virStorageVolDispose; + obj->finalize = virStorageVolFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -250,7 +266,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS(virNodeDevice); DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); - DECLARE_CLASS(virStorageVol); DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); @@ -782,7 +797,7 @@ virStoragePoolFinalize(GObject *obj) * @freeFunc: private data cleanup function pointer specific to driver * * Allocates a new storage volume 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 storage volume object, or NULL on error. */ @@ -790,18 +805,17 @@ virStorageVolPtr virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key, void *privateData, virFreeCallback freeFunc) { - virStorageVolPtr ret = NULL; + g_autoptr(virStorageVol) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(pool, error); - virCheckNonNullArgGoto(name, error); - virCheckNonNullArgGoto(key, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(pool, NULL); + virCheckNonNullArgReturn(name, NULL); + virCheckNonNullArgReturn(key, NULL); - if (!(ret = virObjectNew(virStorageVolClass))) - goto error; + ret = VIR_STORAGE_VOL(g_object_new(VIR_TYPE_STORAGE_VOL, NULL)); ret->pool = g_strdup(pool); ret->name = g_strdup(name); @@ -813,11 +827,7 @@ virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, ret->privateData = privateData; ret->privateDataFreeFunc = freeFunc; - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } @@ -825,16 +835,31 @@ virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, * virStorageVolDispose: * @obj: the storage volume to release * - * Unconditionally release all memory associated with a volume. - * The volume 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 +virStorageVolDispose(GObject *obj) +{ + virStorageVolPtr vol = VIR_STORAGE_VOL(obj); + + virObjectUnref(vol->conn); + vol->conn = NULL; + + G_OBJECT_CLASS(vir_storage_vol_parent_class)->dispose(obj); +} + +/** + * virStorageVolFinalize: + * @obj: the storage volume 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 volume. + * The volume object must not be used once this method returns. */ static void -virStorageVolDispose(void *obj) +virStorageVolFinalize(GObject *obj) { - virStorageVolPtr vol = obj; + virStorageVolPtr vol = VIR_STORAGE_VOL(obj); VIR_DEBUG("release vol %p %s", vol, vol->name); if (vol->privateDataFreeFunc) @@ -843,7 +868,8 @@ virStorageVolDispose(void *obj) VIR_FREE(vol->key); VIR_FREE(vol->name); VIR_FREE(vol->pool); - virObjectUnref(vol->conn); + + G_OBJECT_CLASS(vir_storage_vol_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 2c8256388c..5492c2ac8a 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -35,7 +35,6 @@ extern virClassPtr virDomainClass; extern virClassPtr virNodeDeviceClass; extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; -extern virClassPtr virStorageVolClass; #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() G_DECLARE_FINAL_TYPE(virDomainCheckpoint, @@ -73,6 +72,9 @@ G_DECLARE_FINAL_TYPE(virNWFilterBinding, #define VIR_TYPE_STORAGE_POOL vir_storage_pool_get_type() G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObject); +#define VIR_TYPE_STORAGE_VOL vir_storage_vol_get_type() +G_DECLARE_FINAL_TYPE(virStorageVol, vir_storage_vol, VIR, STORAGE_VOL, GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -220,8 +222,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckStorageVolReturn(obj, retval) \ do { \ - virStorageVolPtr _vol = (obj); \ - if (!virObjectIsClass(_vol, virStorageVolClass) || \ + virStorageVolPtr _vol = VIR_STORAGE_VOL(obj); \ + if (_vol == NULL || \ !virObjectIsClass(_vol->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STORAGE, \ VIR_ERR_INVALID_STORAGE_VOL, \ @@ -233,8 +235,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); } while (0) #define virCheckStorageVolGoto(obj, label) \ do { \ - virStorageVolPtr _vol = (obj); \ - if (!virObjectIsClass(_vol, virStorageVolClass) || \ + virStorageVolPtr _vol = VIR_STORAGE_VOL(obj); \ + if (_vol == NULL || \ !virObjectIsClass(_vol->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STORAGE, \ VIR_ERR_INVALID_STORAGE_VOL, \ @@ -706,7 +708,7 @@ struct _virStoragePool { * Internal structure associated to a storage volume */ struct _virStorageVol { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *pool; /* Pool name of owner */ char *name; /* the storage vol external name */ @@ -720,7 +722,6 @@ struct _virStorageVol { virFreeCallback privateDataFreeFunc; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageVol, virObjectUnref); /** diff --git a/src/libvirt-storage.c b/src/libvirt-storage.c index b88b2162de..53ad9eeec6 100644 --- a/src/libvirt-storage.c +++ b/src/libvirt-storage.c @@ -1884,7 +1884,7 @@ virStorageVolFree(virStorageVolPtr vol) virCheckStorageVolReturn(vol, -1); - virObjectUnref(vol); + g_object_unref(vol); return 0; } @@ -1909,13 +1909,13 @@ virStorageVolFree(virStorageVolPtr vol) int virStorageVolRef(virStorageVolPtr vol) { - VIR_DEBUG("vol=%p refs=%d", vol, vol ? vol->parent.u.s.refs : 0); + VIR_DEBUG("vol=%p", vol); virResetLastError(); virCheckStorageVolReturn(vol, -1); - virObjectRef(vol); + g_object_ref(vol); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 94009505bc..9e559ad1f8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1354,6 +1354,7 @@ vir_network_port_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; vir_storage_pool_get_type; +vir_storage_vol_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1378,7 +1379,6 @@ virGetStream; virNewConnectCloseCallbackData; virNodeDeviceClass; virSecretClass; -virStorageVolClass; virStreamClass; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index de65c730f7..07e137e2c9 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -175,7 +175,7 @@ qemuMigrationDstPrecreateDisk(virConnectPtr conn, { int ret = -1; g_autoptr(virStoragePool) pool = NULL; - virStorageVolPtr vol = NULL; + g_autoptr(virStorageVol) vol = NULL; char *volName = NULL, *basePath = NULL; char *volStr = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; @@ -268,7 +268,6 @@ qemuMigrationDstPrecreateDisk(virConnectPtr conn, cleanup: VIR_FREE(basePath); VIR_FREE(volStr); - virObjectUnref(vol); return ret; } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 8d19651367..1cfafe3742 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -204,6 +204,7 @@ my %gobject_impl = ( virNWFilter => 1, virNWFilterBinding => 1, virStoragePool => 1, + virStorageVol => 1, ); sub use_gobject { diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index 2db763caa5..8c0f7eae44 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -1892,7 +1892,8 @@ storageVolCreateXML(virStoragePoolPtr pool, virStoragePoolObjPtr obj; virStoragePoolDefPtr def; virStorageBackendPtr backend; - virStorageVolPtr vol = NULL, newvol = NULL; + virStorageVolPtr vol = NULL; + g_autoptr(virStorageVol) newvol = NULL; g_autoptr(virStorageVolDef) voldef = NULL; virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL); @@ -2012,7 +2013,6 @@ storageVolCreateXML(virStoragePoolPtr pool, voldef = NULL; cleanup: - virObjectUnref(newvol); virStoragePoolObjEndAPI(&obj); return vol; } @@ -2029,7 +2029,7 @@ storageVolCreateXMLFrom(virStoragePoolPtr pool, virStorageBackendPtr backend; virStorageVolDefPtr voldefsrc = NULL; virStorageVolDefPtr shadowvol = NULL; - virStorageVolPtr newvol = NULL; + g_autoptr(virStorageVol) newvol = NULL; virStorageVolPtr vol = NULL; int buildret; g_autoptr(virStorageVolDef) voldef = NULL; @@ -2204,7 +2204,6 @@ storageVolCreateXMLFrom(virStoragePoolPtr pool, voldef = NULL; cleanup: - virObjectUnref(newvol); VIR_FREE(shadowvol); virStoragePoolObjEndAPI(&obj); virStoragePoolObjEndAPI(&objsrc); diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c index ed3d5ee2c0..17d8f9d94b 100644 --- a/src/vz/vz_sdk.c +++ b/src/vz/vz_sdk.c @@ -4082,9 +4082,8 @@ static int virStorageTranslatePoolLocal(virConnectPtr conn, virStorageSourcePtr src) { g_autoptr(virStoragePool) pool = NULL; - virStorageVolPtr vol = NULL; + g_autoptr(virStorageVol) vol = NULL; virStorageVolInfo info; - int ret = -1; if (!(pool = virStoragePoolLookupByName(conn, src->srcpool->pool))) return -1; @@ -4093,30 +4092,26 @@ virStorageTranslatePoolLocal(virConnectPtr conn, virStorageSourcePtr src) _("storage pool '%s' containing volume '%s' " "is not active"), src->srcpool->pool, src->srcpool->volume); - goto cleanup; + return -1; } if (!(vol = virStorageVolLookupByName(pool, src->srcpool->volume))) - goto cleanup; + return -1; if (virStorageVolGetInfo(vol, &info) < 0) - goto cleanup; + return -1; if (info.type != VIR_STORAGE_VOL_PLOOP) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported volume format '%s'"), virStorageVolTypeToString(info.type)); - goto cleanup; + return -1; } if (!(src->path = virStorageVolGetPath(vol))) - goto cleanup; - - ret = 0; + return -1; - cleanup: - virObjectUnref(vol); - return ret; + return 0; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- 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

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/secret_event.c | 15 +++---- src/conf/virsecretobj.c | 2 +- src/datatypes.c | 68 ++++++++++++++++++++--------- src/datatypes.h | 14 +++--- src/libvirt-secret.c | 7 ++- src/libvirt_private.syms | 2 +- src/remote/remote_daemon_dispatch.c | 6 +-- src/remote/remote_driver.c | 6 +-- src/rpc/gendispatch.pl | 1 + src/storage/storage_util.c | 4 +- src/util/virsecret.c | 3 +- 11 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/conf/secret_event.c b/src/conf/secret_event.c index 1c34dcfbc9..96fdcc3577 100644 --- a/src/conf/secret_event.c +++ b/src/conf/secret_event.c @@ -109,10 +109,10 @@ virSecretEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virSecretPtr secret = virGetSecret(conn, - event->meta.uuid, - event->meta.id, - event->meta.name); + g_autoptr(virSecret) secret = virGetSecret(conn, + event->meta.uuid, + event->meta.id, + event->meta.name); if (!secret) return; @@ -127,23 +127,20 @@ virSecretEventDispatchDefaultFunc(virConnectPtr conn, secretLifecycleEvent->type, secretLifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_SECRET_EVENT_ID_VALUE_CHANGED: { ((virConnectSecretEventGenericCallback)cb)(conn, secret, cbopaque); - goto cleanup; + return; } case VIR_SECRET_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(secret); } diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 81c9f2c346..3905fc0f9b 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -611,7 +611,7 @@ virSecretObjListExport(virConnectPtr conn, return data.nsecrets; error: - virObjectListFree(data.secrets); + virGObjectListFreeCount(data.secrets, data.nsecrets); return -1; } diff --git a/src/datatypes.c b/src/datatypes.c index f324a19ada..0bb4cfbd29 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,13 +36,11 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virSecretClass; virClassPtr virStreamClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virSecretDispose(void *obj); static void virStreamDispose(void *obj); G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); @@ -189,6 +187,24 @@ vir_nw_filter_binding_class_init(virNWFilterBindingClass *klass) obj->finalize = virNWFilterBindingFinalize; } +G_DEFINE_TYPE(virSecret, vir_secret, G_TYPE_OBJECT); +static void virSecretDispose(GObject *obj); +static void virSecretFinalize(GObject *obj); + +static void +vir_secret_init(virSecret *sec G_GNUC_UNUSED) +{ +} + +static void +vir_secret_class_init(virSecretClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virSecretDispose; + obj->finalize = virSecretFinalize; +} + G_DEFINE_TYPE(virStoragePool, vir_storage_pool, G_TYPE_OBJECT); static void virStoragePoolDispose(GObject *obj); static void virStoragePoolFinalize(GObject *obj); @@ -279,7 +295,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virSecret); DECLARE_CLASS(virStream); DECLARE_CLASS_LOCKABLE(virAdmConnect); @@ -962,7 +977,7 @@ virNodeDeviceFinalize(GObject *obj) * @uuid: secret UUID * * Allocates a new secret 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 secret object, or NULL on error. */ @@ -970,16 +985,15 @@ virSecretPtr virGetSecret(virConnectPtr conn, const unsigned char *uuid, int usageType, const char *usageID) { - virSecretPtr ret = NULL; + g_autoptr(virSecret) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckConnectGoto(conn, error); - virCheckNonNullArgGoto(uuid, error); + virCheckConnectReturn(conn, NULL); + virCheckNonNullArgReturn(uuid, NULL); - if (!(ret = virObjectNew(virSecretClass))) - return NULL; + ret = VIR_SECRET(g_object_new(VIR_TYPE_SECRET, NULL)); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->usageType = usageType; @@ -987,34 +1001,46 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, ret->conn = virObjectRef(conn); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** * virSecretDispose: * @obj: the secret to release * - * Unconditionally release all memory associated with a secret. - * The secret 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 +virSecretDispose(GObject *obj) +{ + virSecretPtr secret = VIR_SECRET(obj); + + virObjectUnref(secret->conn); + secret->conn = NULL; + + G_OBJECT_CLASS(vir_secret_parent_class)->dispose(obj); +} + +/** + * virSecretFinalize: + * @obj: the secret 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 secret. + * The secret object must not be used once this method returns. */ static void -virSecretDispose(void *obj) +virSecretFinalize(GObject *obj) { - virSecretPtr secret = obj; + virSecretPtr secret = VIR_SECRET(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(secret->uuid, uuidstr); VIR_DEBUG("release secret %p %s", secret, uuidstr); VIR_FREE(secret->usageID); - virObjectUnref(secret->conn); + + G_OBJECT_CLASS(vir_secret_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index cbf565097f..2f97268691 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virSecretClass; extern virClassPtr virStreamClass; #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() @@ -71,6 +70,9 @@ G_DECLARE_FINAL_TYPE(virNWFilterBinding, NW_FILTER_BINDING, GObject); +#define VIR_TYPE_SECRET vir_secret_get_type() +G_DECLARE_FINAL_TYPE(virSecret, vir_secret, VIR, SECRET, GObject); + #define VIR_TYPE_STORAGE_POOL vir_storage_pool_get_type() G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObject); @@ -277,8 +279,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckSecretReturn(obj, retval) \ do { \ - virSecretPtr _secret = (obj); \ - if (!virObjectIsClass(_secret, virSecretClass) || \ + virSecretPtr _secret = VIR_SECRET(obj); \ + if (_secret == NULL || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -291,8 +293,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckSecretGoto(obj, label) \ do { \ - virSecretPtr _secret = (obj); \ - if (!virObjectIsClass(_secret, virSecretClass) || \ + virSecretPtr _secret = VIR_SECRET(obj); \ + if (_secret == NULL || \ !virObjectIsClass(_secret->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_SECRET, \ VIR_ERR_INVALID_SECRET, \ @@ -744,7 +746,7 @@ struct _virNodeDevice { * Internal structure associated with a secret */ struct _virSecret { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ unsigned char uuid[VIR_UUID_BUFLEN]; /* the secret unique identifier */ int usageType; /* the type of usage */ diff --git a/src/libvirt-secret.c b/src/libvirt-secret.c index 33cbdd7b0b..4248536a5b 100644 --- a/src/libvirt-secret.c +++ b/src/libvirt-secret.c @@ -658,14 +658,13 @@ virSecretUndefine(virSecretPtr secret) int virSecretRef(virSecretPtr secret) { - VIR_DEBUG("secret=%p refs=%d", secret, - secret ? secret->parent.u.s.refs : 0); + VIR_DEBUG("secret=%p", secret); virResetLastError(); virCheckSecretReturn(secret, -1); - virObjectRef(secret); + g_object_ref(secret); return 0; } @@ -687,7 +686,7 @@ virSecretFree(virSecretPtr secret) virCheckSecretReturn(secret, -1); - virObjectUnref(secret); + g_object_unref(secret); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2112082277..915979bb42 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1354,6 +1354,7 @@ vir_network_port_get_type; vir_node_device_get_type; vir_nw_filter_binding_get_type; vir_nw_filter_get_type; +vir_secret_get_type; vir_storage_pool_get_type; vir_storage_vol_get_type; virConnectClass; @@ -1378,7 +1379,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virNewConnectCloseCallbackData; -virSecretClass; virStreamClass; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index e7b2a5f363..fb1f1bd469 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -4382,7 +4382,7 @@ remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED, remote_secret_get_value_args *args, remote_secret_get_value_ret *ret) { - virSecretPtr secret = NULL; + g_autoptr(virSecret) secret = NULL; size_t value_size; unsigned char *value; int rv = -1; @@ -4405,7 +4405,6 @@ remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); return rv; } @@ -6375,7 +6374,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED daemonClientEventCallbackPtr ref; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - virSecretPtr secret = NULL; + g_autoptr(virSecret) secret = NULL; virConnectPtr conn = remoteGetSecretConn(client); virMutexLock(&priv->lock); @@ -6433,7 +6432,6 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(secret); return rv; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 0d7f352ae0..7801e5e990 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5559,7 +5559,7 @@ remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_secret_event_lifecycle_msg *msg = evdata; - virSecretPtr secret; + g_autoptr(virSecret) secret = NULL; virObjectEventPtr event = NULL; secret = get_nonnull_secret(conn, msg->secret); @@ -5568,7 +5568,6 @@ remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virSecretEventLifecycleNew(secret->uuid, secret->usageType, secret->usageID, msg->event, msg->detail); - virObjectUnref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5581,7 +5580,7 @@ remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; remote_secret_event_value_changed_msg *msg = evdata; - virSecretPtr secret; + g_autoptr(virSecret) secret = NULL; virObjectEventPtr event = NULL; secret = get_nonnull_secret(conn, msg->secret); @@ -5589,7 +5588,6 @@ remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED, return; event = virSecretEventValueChangedNew(secret->uuid, secret->usageType, secret->usageID); - virObjectUnref(secret); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 6869311f06..902ff091a5 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -204,6 +204,7 @@ my %gobject_impl = ( virNodeDevice => 1, virNWFilter => 1, virNWFilterBinding => 1, + virSecret => 1, virStoragePool => 1, virStorageVol => 1, ); diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 1ec1d13625..7e3bfd1393 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -2172,7 +2172,7 @@ virStorageBackendVolDeleteLocal(virStoragePoolObjPtr pool G_GNUC_UNUSED, static int storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) { - virSecretPtr sec; + g_autoptr(virSecret) sec = NULL; virStorageEncryptionSecretPtr encsec = NULL; virConnectPtr conn = NULL; @@ -2199,7 +2199,6 @@ storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) if (VIR_ALLOC_N(vol->target.encryption->secrets, 1) < 0 || VIR_ALLOC(encsec) < 0) { VIR_FREE(vol->target.encryption->secrets); - virObjectUnref(sec); return -1; } @@ -2209,7 +2208,6 @@ storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol) encsec->type = VIR_STORAGE_ENCRYPTION_SECRET_TYPE_PASSPHRASE; encsec->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_UUID; virSecretGetUUID(sec, encsec->seclookupdef.u.uuid); - virObjectUnref(sec); return 0; } diff --git a/src/util/virsecret.c b/src/util/virsecret.c index 54d6bbcb7c..7279d22683 100644 --- a/src/util/virsecret.c +++ b/src/util/virsecret.c @@ -147,7 +147,7 @@ virSecretGetSecretString(virConnectPtr conn, uint8_t **secret, size_t *secret_size) { - virSecretPtr sec = NULL; + g_autoptr(virSecret) sec = NULL; int ret = -1; switch (seclookupdef->type) { @@ -190,6 +190,5 @@ virSecretGetSecretString(virConnectPtr conn, ret = 0; cleanup: - virObjectUnref(sec); return ret; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virchrdev.c | 4 +-- src/datatypes.c | 56 ++++++++++++++++++++++------- src/datatypes.h | 14 ++++---- src/libvirt-stream.c | 7 ++-- src/libvirt_private.syms | 2 +- src/libxl/libxl_migration.c | 3 +- src/qemu/qemu_migration.c | 6 ++-- src/remote/remote_daemon_dispatch.c | 4 +-- src/remote/remote_daemon_stream.c | 2 +- src/remote/remote_driver.c | 2 +- src/rpc/gendispatch.pl | 3 +- src/util/virfdstream.c | 4 +-- 12 files changed, 68 insertions(+), 39 deletions(-) diff --git a/src/conf/virchrdev.c b/src/conf/virchrdev.c index 800e82869e..8a60ac4dad 100644 --- a/src/conf/virchrdev.c +++ b/src/conf/virchrdev.c @@ -205,7 +205,7 @@ static void virChrdevHashEntryFree(void *data) return; /* free stream reference */ - virObjectUnref(ent->st); + g_clear_object(&ent->st); /* delete lock file */ virChrdevLockFileRemove(ent->dev); @@ -435,7 +435,7 @@ int virChrdevOpen(virChrdevsPtr devs, if (added) virHashRemoveEntry(devs->hash, path); else - virObjectUnref(st); + g_clear_object(&st); if (cbdata) VIR_FREE(cbdata->path); diff --git a/src/datatypes.c b/src/datatypes.c index 0bb4cfbd29..5d8752334d 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,12 +36,10 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virStreamClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virStreamDispose(void *obj); G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); static void virDomainCheckpointDispose(GObject *obj); @@ -241,6 +239,24 @@ vir_storage_vol_class_init(virStorageVolClass *klass) obj->finalize = virStorageVolFinalize; } +G_DEFINE_TYPE(virStream, vir_stream, G_TYPE_OBJECT); +static void virStreamDispose(GObject *obj); +static void virStreamFinalize(GObject *obj); + +static void +vir_stream_init(virStream *strm G_GNUC_UNUSED) +{ +} + +static void +vir_stream_class_init(virStreamClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virStreamDispose; + obj->finalize = virStreamFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -295,7 +311,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virStream); DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); @@ -1049,7 +1064,7 @@ virSecretFinalize(GObject *obj) * @conn: the hypervisor connection * * Allocates a new stream 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 stream object, or NULL on error. */ @@ -1061,8 +1076,7 @@ virGetStream(virConnectPtr conn) if (virDataTypesInitialize() < 0) return NULL; - if (!(ret = virObjectNew(virStreamClass))) - return NULL; + ret = VIR_STREAM(g_object_new(VIR_TYPE_STREAM, NULL)); ret->conn = virObjectRef(conn); @@ -1073,21 +1087,37 @@ virGetStream(virConnectPtr conn) * virStreamDispose: * @obj: the stream to release * - * Unconditionally release all memory associated with a stream. - * The stream 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 +virStreamDispose(GObject *obj) +{ + virStreamPtr st = VIR_STREAM(obj); + + virObjectUnref(st->conn); + st->conn = NULL; + + G_OBJECT_CLASS(vir_stream_parent_class)->dispose(obj); +} + +/** + * virStreamFinalize: + * @obj: the stream 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 stream. + * The stream object must not be used once this method returns. */ static void -virStreamDispose(void *obj) +virStreamFinalize(GObject *obj) { - virStreamPtr st = obj; + virStreamPtr st = VIR_STREAM(obj); VIR_DEBUG("release dev %p", st); if (st->ff) st->ff(st->privateData); - virObjectUnref(st->conn); + + G_OBJECT_CLASS(vir_stream_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index 2f97268691..e96654cac1 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virStreamClass; #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() G_DECLARE_FINAL_TYPE(virDomainCheckpoint, @@ -79,6 +78,9 @@ G_DECLARE_FINAL_TYPE(virStoragePool, vir_storage_pool, VIR, STORAGE_POOL, GObjec #define VIR_TYPE_STORAGE_VOL vir_storage_vol_get_type() G_DECLARE_FINAL_TYPE(virStorageVol, vir_storage_vol, VIR, STORAGE_VOL, GObject); +#define VIR_TYPE_STREAM vir_stream_get_type() +G_DECLARE_FINAL_TYPE(virStream, vir_stream, VIR, STREAM, GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -307,8 +309,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckStreamReturn(obj, retval) \ do { \ - virStreamPtr _st = (obj); \ - if (!virObjectIsClass(_st, virStreamClass) || \ + virStreamPtr _st = VIR_STREAM(obj); \ + if (_st == NULL || \ !virObjectIsClass(_st->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STREAMS, \ VIR_ERR_INVALID_STREAM, \ @@ -320,8 +322,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); } while (0) #define virCheckStreamGoto(obj, label) \ do { \ - virStreamPtr _st = (obj); \ - if (!virObjectIsClass(_st, virStreamClass) || \ + virStreamPtr _st = VIR_STREAM(obj); \ + if (_st == NULL || \ !virObjectIsClass(_st->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_STREAMS, \ VIR_ERR_INVALID_STREAM, \ @@ -763,7 +765,7 @@ typedef int (*virStreamFinishFunc)(virStreamPtr, void *opaque); * Internal structure associated with an input stream */ struct _virStream { - virObject parent; + GObject parent; virConnectPtr conn; unsigned int flags; diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c index 41b9cc1445..d1e2fde3b4 100644 --- a/src/libvirt-stream.c +++ b/src/libvirt-stream.c @@ -85,14 +85,13 @@ virStreamNew(virConnectPtr conn, int virStreamRef(virStreamPtr stream) { - VIR_DEBUG("stream=%p refs=%d", stream, - stream ? stream->parent.u.s.refs : 0); + VIR_DEBUG("stream=%p", stream); virResetLastError(); virCheckStreamReturn(stream, -1); - virObjectRef(stream); + g_object_ref(stream); return 0; } @@ -1265,6 +1264,6 @@ virStreamFree(virStreamPtr stream) /* XXX Enforce shutdown before free'ing resources ? */ - virObjectUnref(stream); + g_object_unref(stream); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 915979bb42..1514db8cb9 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1357,6 +1357,7 @@ vir_nw_filter_get_type; vir_secret_get_type; vir_storage_pool_get_type; vir_storage_vol_get_type; +vir_stream_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1379,7 +1380,6 @@ virGetStoragePool; virGetStorageVol; virGetStream; virNewConnectCloseCallbackData; -virStreamClass; # driver.h diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c index defdda5ed6..50804ca8b8 100644 --- a/src/libxl/libxl_migration.c +++ b/src/libxl/libxl_migration.c @@ -999,7 +999,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver, virErrorPtr orig_err = NULL; int ret = -1; /* For tunnel migration */ - virStreamPtr st = NULL; + g_autoptr(virStream) st = NULL; struct libxlTunnelControl *tc = NULL; if (dname && @@ -1110,7 +1110,6 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver, cleanup: if (flags & VIR_MIGRATE_TUNNELLED) { libxlMigrationSrcStopTunnel(tc); - virObjectUnref(st); } if (ddomain) { diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 07e137e2c9..13ff06f5c7 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -4000,7 +4000,7 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver, int cookielen = 0, ret; virErrorPtr orig_err = NULL; bool cancelled; - virStreamPtr st = NULL; + g_autoptr(virStream) st = NULL; unsigned long destflags; VIR_DEBUG("driver=%p, sconn=%p, dconn=%p, vm=%p, dconnuri=%s, " @@ -4109,7 +4109,6 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver, ret = -1; } - virObjectUnref(st); virErrorRestore(&orig_err); VIR_FREE(uri_out); @@ -4153,7 +4152,7 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver, int ret = -1; virErrorPtr orig_err = NULL; bool cancelled = true; - virStreamPtr st = NULL; + g_autoptr(virStream) st = NULL; unsigned long destflags; virTypedParameterPtr params = NULL; int nparams = 0; @@ -4446,7 +4445,6 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver, ret = -1; } - virObjectUnref(st); virErrorRestore(&orig_err); VIR_FREE(uri_out); diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index fb1f1bd469..c5eca3bc0c 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -5641,7 +5641,7 @@ remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server G_GNUC_UN char *cookieout = NULL; int cookieoutlen = 0; int rv = -1; - virStreamPtr st = NULL; + g_autoptr(virStream) st = NULL; daemonClientStreamPtr stream = NULL; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5688,7 +5688,7 @@ remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server G_GNUC_UN virStreamAbort(st); daemonFreeClientStream(client, stream); } else { - virObjectUnref(st); + g_clear_object(&st); } } return rv; diff --git a/src/remote/remote_daemon_stream.c b/src/remote/remote_daemon_stream.c index 62bc9e0f5b..6e1dba3bc0 100644 --- a/src/remote/remote_daemon_stream.c +++ b/src/remote/remote_daemon_stream.c @@ -419,7 +419,7 @@ int daemonFreeClientStream(virNetServerClientPtr client, msg = tmp; } - virObjectUnref(stream->st); + g_clear_object(&stream->st); VIR_FREE(stream); return ret; diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 7801e5e990..58ae7528a8 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5823,7 +5823,7 @@ static void remoteStreamCallbackFree(void *opaque) if (!cbdata->cb && cbdata->ff) (cbdata->ff)(cbdata->opaque); - virObjectUnref(cbdata->st); + g_clear_object(&cbdata->st); VIR_FREE(opaque); } diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 902ff091a5..29b3feb17c 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -207,6 +207,7 @@ my %gobject_impl = ( virSecret => 1, virStoragePool => 1, virStorageVol => 1, + virStream => 1, ); sub use_gobject { @@ -1068,7 +1069,7 @@ elsif ($mode eq "server") { push(@free_list_on_error, " virStreamAbort(st);"); push(@free_list_on_error, " daemonFreeClientStream(client, stream);"); push(@free_list_on_error, "} else {"); - push(@free_list_on_error, " virObjectUnref(st);"); + push(@free_list_on_error, " g_clear_object(&st);"); push(@free_list_on_error, "}"); } diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 111e451f8c..e6271b860c 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -407,7 +407,7 @@ virFDStreamThreadDataFree(virFDStreamThreadDataPtr data) if (!data) return; - virObjectUnref(data->st); + g_clear_object(&data->st); VIR_FREE(data->fdinname); VIR_FREE(data->fdoutname); VIR_FREE(data); @@ -1282,7 +1282,7 @@ virFDStreamOpenFileInternal(virStreamPtr st, if (VIR_ALLOC(threadData) < 0) goto error; - threadData->st = virObjectRef(st); + threadData->st = g_object_ref(st); threadData->length = length; threadData->sparse = sparse; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_event.c | 58 ++++++------ src/datatypes.c | 80 ++++++++++------ src/datatypes.h | 20 ++-- src/esx/esx_driver.c | 7 +- src/hyperv/hyperv_driver.c | 8 +- src/libvirt-domain.c | 6 +- src/libvirt_private.syms | 2 +- src/libxl/libxl_migration.c | 3 +- src/locking/sanlock_helper.c | 5 +- src/qemu/qemu_driver.c | 6 +- src/qemu/qemu_migration.c | 6 +- src/remote/remote_daemon_dispatch.c | 139 +++++++++------------------- src/remote/remote_driver.c | 100 ++++++-------------- src/rpc/gendispatch.pl | 6 +- src/vbox/vbox_common.c | 11 +-- src/vz/vz_driver.c | 5 +- 16 files changed, 183 insertions(+), 279 deletions(-) diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index 33fbf10406..d1a4953686 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -1625,8 +1625,8 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virDomainPtr dom = virGetDomain(conn, event->meta.name, - event->meta.uuid, event->meta.id); + g_autoptr(virDomain) dom = virGetDomain(conn, event->meta.name, + event->meta.uuid, event->meta.id); if (!dom) return; @@ -1641,13 +1641,13 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, lifecycleEvent->type, lifecycleEvent->detail, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_REBOOT: (cb)(conn, dom, cbopaque); - goto cleanup; + return; case VIR_DOMAIN_EVENT_ID_RTC_CHANGE: { @@ -1657,7 +1657,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventRTCChangeCallback)cb)(conn, dom, rtcChangeEvent->offset, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_WATCHDOG: @@ -1668,7 +1668,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventWatchdogCallback)cb)(conn, dom, watchdogEvent->action, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_IO_ERROR: @@ -1681,7 +1681,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ioErrorEvent->devAlias, ioErrorEvent->action, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON: @@ -1695,7 +1695,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ioErrorEvent->action, ioErrorEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_GRAPHICS: @@ -1710,13 +1710,13 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, graphicsEvent->authScheme, graphicsEvent->subject, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR: (cb)(conn, dom, cbopaque); - goto cleanup; + return; case VIR_DOMAIN_EVENT_ID_BLOCK_JOB: case VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2: @@ -1729,7 +1729,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, blockJobEvent->type, blockJobEvent->status, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_DISK_CHANGE: @@ -1743,7 +1743,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, diskChangeEvent->devAlias, diskChangeEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE: @@ -1755,7 +1755,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, trayChangeEvent->devAlias, trayChangeEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_PMWAKEUP: @@ -1765,7 +1765,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventPMWakeupCallback)cb)(conn, dom, pmEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_PMSUSPEND: @@ -1775,7 +1775,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventPMSuspendCallback)cb)(conn, dom, pmEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE: @@ -1786,7 +1786,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventBalloonChangeCallback)cb)(conn, dom, balloonChangeEvent->actual, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK: @@ -1796,7 +1796,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventPMSuspendDiskCallback)cb)(conn, dom, pmEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED: @@ -1807,7 +1807,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventDeviceRemovedCallback)cb)(conn, dom, deviceRemovedEvent->devAlias, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_TUNABLE: @@ -1818,7 +1818,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, tunableEvent->params, tunableEvent->nparams, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE: @@ -1829,7 +1829,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, agentLifecycleEvent->state, agentLifecycleEvent->reason, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_DEVICE_ADDED: @@ -1840,7 +1840,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventDeviceAddedCallback)cb)(conn, dom, deviceAddedEvent->devAlias, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_MIGRATION_ITERATION: @@ -1851,7 +1851,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventMigrationIterationCallback)cb)(conn, dom, ev->iteration, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_JOB_COMPLETED: @@ -1863,7 +1863,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ev->params, ev->nparams, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_DEVICE_REMOVAL_FAILED: @@ -1874,7 +1874,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, ((virConnectDomainEventDeviceRemovalFailedCallback)cb)(conn, dom, deviceRemovalFailedEvent->devAlias, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_METADATA_CHANGE: @@ -1886,7 +1886,7 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, metadataChangeEvent->type, metadataChangeEvent->nsuri, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_BLOCK_THRESHOLD: @@ -1900,16 +1900,13 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, blockThresholdEvent->threshold, blockThresholdEvent->excess, cbopaque); - goto cleanup; + return; } case VIR_DOMAIN_EVENT_ID_LAST: break; } VIR_WARN("Unexpected event ID %d", event->eventID); - - cleanup: - virObjectUnref(dom); } @@ -1963,7 +1960,7 @@ virDomainQemuMonitorEventDispatchFunc(virConnectPtr conn, virConnectObjectEventGenericCallback cb, void *cbopaque) { - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virDomainQemuMonitorEventPtr qemuMonitorEvent; virDomainQemuMonitorEventData *data = cbopaque; @@ -1978,7 +1975,6 @@ virDomainQemuMonitorEventDispatchFunc(virConnectPtr conn, qemuMonitorEvent->micros, qemuMonitorEvent->details, data->opaque); - virObjectUnref(dom); } diff --git a/src/datatypes.c b/src/datatypes.c index 5d8752334d..e72e839562 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -35,11 +35,27 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; -virClassPtr virDomainClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); -static void virDomainDispose(void *obj); + +G_DEFINE_TYPE(virDomain, vir_domain, G_TYPE_OBJECT); +static void virDomainDispose(GObject *obj); +static void virDomainFinalize(GObject *obj); + +static void +vir_domain_init(virDomain *dom G_GNUC_UNUSED) +{ +} + +static void +vir_domain_class_init(virDomainClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virDomainDispose; + obj->finalize = virDomainFinalize; +} G_DEFINE_TYPE(virDomainCheckpoint, vir_domain_checkpoint, G_TYPE_OBJECT); static void virDomainCheckpointDispose(GObject *obj); @@ -310,7 +326,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); - DECLARE_CLASS(virDomain); DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); @@ -475,7 +490,7 @@ virConnectCloseCallbackDataGetCallback(virConnectCloseCallbackDataPtr closeData) * @id: domain ID * * Allocates a new domain 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 domain object, or NULL on error. */ @@ -485,17 +500,16 @@ virGetDomain(virConnectPtr conn, const unsigned char *uuid, int id) { - virDomainPtr ret = NULL; + g_autoptr(virDomain) 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(virDomainClass))) - goto error; + ret = VIR_DOMAIN(g_object_new(VIR_TYPE_DOMAIN, NULL)); ret->name = g_strdup(name); @@ -503,34 +517,46 @@ virGetDomain(virConnectPtr conn, ret->id = id; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** * virDomainDispose: * @obj: the domain to release * - * Unconditionally release all memory associated with a domain. - * The domain 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 +virDomainDispose(GObject *obj) +{ + virDomainPtr domain = VIR_DOMAIN(obj); + + virObjectUnref(domain->conn); + domain->conn = NULL; + + G_OBJECT_CLASS(vir_domain_parent_class)->dispose(obj); +} + +/** + * virDomainFinalize: + * @obj: the domain 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 domain. + * The domain object must not be used once this method returns. */ static void -virDomainDispose(void *obj) +virDomainFinalize(GObject *obj) { - virDomainPtr domain = obj; + virDomainPtr domain = VIR_DOMAIN(obj); char uuidstr[VIR_UUID_STRING_BUFLEN]; virUUIDFormat(domain->uuid, uuidstr); VIR_DEBUG("release domain %p %s %s", domain, domain->name, uuidstr); VIR_FREE(domain->name); - virObjectUnref(domain->conn); + + G_OBJECT_CLASS(vir_domain_parent_class)->finalize(obj); } @@ -1296,7 +1322,7 @@ virGetDomainCheckpoint(virDomainPtr domain, ret = VIR_DOMAIN_CHECKPOINT(g_object_new(VIR_TYPE_DOMAIN_CHECKPOINT, NULL)); ret->name = g_strdup(name); - ret->domain = virObjectRef(domain); + ret->domain = g_object_ref(domain); return g_steal_pointer(&ret); } @@ -1314,8 +1340,7 @@ virDomainCheckpointDispose(GObject *obj) { virDomainCheckpointPtr checkpoint = VIR_DOMAIN_CHECKPOINT(obj); - virObjectUnref(checkpoint->domain); - checkpoint->domain = NULL; + g_clear_object(&checkpoint->domain); G_OBJECT_CLASS(vir_domain_checkpoint_parent_class)->dispose(obj); } @@ -1363,7 +1388,7 @@ virGetDomainSnapshot(virDomainPtr domain, const char *name) ret = VIR_DOMAIN_SNAPSHOT(g_object_new(VIR_TYPE_DOMAIN_SNAPSHOT, NULL)); ret->name = g_strdup(name); - ret->domain = virObjectRef(domain); + ret->domain = g_object_ref(domain); return g_steal_pointer(&ret); } @@ -1381,8 +1406,7 @@ virDomainSnapshotDispose(GObject *obj) { virDomainSnapshotPtr snapshot = VIR_DOMAIN_SNAPSHOT(obj); - virObjectUnref(snapshot->domain); - snapshot->domain = NULL; + g_clear_object(&snapshot->domain); G_OBJECT_CLASS(vir_domain_snapshot_parent_class)->dispose(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index e96654cac1..f647096938 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -31,7 +31,9 @@ #include <glib-object.h> extern virClassPtr virConnectClass; -extern virClassPtr virDomainClass; + +#define VIR_TYPE_DOMAIN vir_domain_get_type() +G_DECLARE_FINAL_TYPE(virDomain, vir_domain, VIR, DOMAIN, GObject); #define VIR_TYPE_DOMAIN_CHECKPOINT vir_domain_checkpoint_get_type() G_DECLARE_FINAL_TYPE(virDomainCheckpoint, @@ -110,8 +112,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckDomainReturn(obj, retval) \ do { \ - virDomainPtr _dom = (obj); \ - if (!virObjectIsClass(_dom, virDomainClass) || \ + virDomainPtr _dom = VIR_DOMAIN(obj); \ + if (_dom == NULL || \ !virObjectIsClass(_dom->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOM, VIR_ERR_INVALID_DOMAIN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -122,8 +124,8 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); } while (0) #define virCheckDomainGoto(obj, label) \ do { \ - virDomainPtr _dom = (obj); \ - if (!virObjectIsClass(_dom, virDomainClass) || \ + virDomainPtr _dom = VIR_DOMAIN(obj); \ + if (_dom == NULL || \ !virObjectIsClass(_dom->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOM, VIR_ERR_INVALID_DOMAIN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -365,7 +367,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); do { \ virDomainCheckpointPtr _check = VIR_DOMAIN_CHECKPOINT(obj); \ if (_check == NULL || \ - !virObjectIsClass(_check->domain, virDomainClass) || \ + !VIR_IS_DOMAIN(_check->domain) || \ !virObjectIsClass(_check->domain->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOMAIN_CHECKPOINT, \ VIR_ERR_INVALID_DOMAIN_CHECKPOINT, \ @@ -380,7 +382,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); do { \ virDomainSnapshotPtr _snap = (obj); \ if (_snap == NULL || \ - !virObjectIsClass(_snap->domain, virDomainClass) || \ + !VIR_IS_DOMAIN(_snap->domain) || \ !virObjectIsClass(_snap->domain->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOMAIN_SNAPSHOT, \ VIR_ERR_INVALID_DOMAIN_SNAPSHOT, \ @@ -427,7 +429,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); char _uuidstr[VIR_UUID_STRING_BUFLEN]; \ const char *_domname = NULL; \ \ - if (!virObjectIsClass(dom, virDomainClass)) { \ + if (!VIR_IS_DOMAIN(dom)) { \ memset(_uuidstr, 0, sizeof(_uuidstr)); \ } else { \ virUUIDFormat((dom)->uuid, _uuidstr); \ @@ -641,7 +643,7 @@ struct _virAdmClient { * Internal structure associated to a domain */ struct _virDomain { - virObject parent; + GObject parent; virConnectPtr conn; /* pointer back to the connection */ char *name; /* the domain external name */ int id; /* the domain ID */ diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 42f6ab9392..fc3a58c3de 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -5006,12 +5006,7 @@ esxConnectListAllDomains(virConnectPtr conn, ret = count; cleanup: - if (doms) { - for (id = 0; id < count; id++) - virObjectUnref(doms[id]); - - VIR_FREE(doms); - } + virGObjectListFreeCount(doms, count); VIR_FREE(name); esxVI_AutoStartDefaults_Free(&autoStartDefaults); diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c index 4677a25ff8..a0eb14a947 100644 --- a/src/hyperv/hyperv_driver.c +++ b/src/hyperv/hyperv_driver.c @@ -1206,7 +1206,6 @@ hypervConnectListAllDomains(virConnectPtr conn, virDomainPtr *doms = NULL; int count = 0; int ret = -1; - size_t i; virCheckFlags(VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL, -1); @@ -1308,12 +1307,7 @@ hypervConnectListAllDomains(virConnectPtr conn, ret = count; cleanup: - if (doms) { - for (i = 0; i < count; ++i) - virObjectUnref(doms[i]); - - VIR_FREE(doms); - } + virGObjectListFreeCount(doms, count); hypervFreeObject(priv, (hypervObject *)computerSystemList); diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index a12809c2d5..6f889f0c3b 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -565,7 +565,7 @@ virDomainFree(virDomainPtr domain) virCheckDomainReturn(domain, -1); - virObjectUnref(domain); + g_clear_object(&domain); return 0; } @@ -590,13 +590,13 @@ virDomainFree(virDomainPtr domain) int virDomainRef(virDomainPtr domain) { - VIR_DOMAIN_DEBUG(domain, "refs=%d", domain ? domain->parent.u.s.refs : 0); + VIR_DOMAIN_DEBUG(domain); virResetLastError(); virCheckDomainReturn(domain, -1); - virObjectRef(domain); + g_object_ref(domain); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1514db8cb9..d96331e633 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1347,6 +1347,7 @@ virCPUx86FeatureFilterSelectMSR; # datatypes.h vir_domain_checkpoint_get_type; +vir_domain_get_type; vir_domain_snapshot_get_type; vir_interface_get_type; vir_network_get_type; @@ -1364,7 +1365,6 @@ virConnectCloseCallbackDataClass; virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; -virDomainClass; virGetConnect; virGetDomain; virGetDomainCheckpoint; diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c index 50804ca8b8..11d3070935 100644 --- a/src/libxl/libxl_migration.c +++ b/src/libxl/libxl_migration.c @@ -985,7 +985,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver, const char *uri, unsigned int flags) { - virDomainPtr ddomain = NULL; + g_autoptr(virDomain) ddomain = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int maxparams = 0; @@ -1113,7 +1113,6 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver, } if (ddomain) { - virObjectUnref(ddomain); ret = 0; } else { ret = -1; diff --git a/src/locking/sanlock_helper.c b/src/locking/sanlock_helper.c index 50deccfd67..7a8480932f 100644 --- a/src/locking/sanlock_helper.c +++ b/src/locking/sanlock_helper.c @@ -5,7 +5,7 @@ #include "viralloc.h" #include "domain_conf.h" #include "virgettext.h" - +#include "datatypes.h" static int getArgs(int argc, @@ -52,7 +52,7 @@ main(int argc, char **argv) virDomainLockFailureAction action; char *xml = NULL; virConnectPtr conn = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int ret = EXIT_FAILURE; int authTypes[] = { @@ -99,7 +99,6 @@ main(int argc, char **argv) } cleanup: - virObjectUnref(dom); if (conn) virConnectClose(conn); VIR_FREE(xml); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b35cca4dcc..c05803f08a 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1089,11 +1089,7 @@ qemuStateStop(void) ret = -1; cleanup: - if (domains) { - for (i = 0; i < numDomains; i++) - virObjectUnref(domains[i]); - VIR_FREE(domains); - } + virGObjectListFreeCount(domains, numDomains); return ret; } diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 13ff06f5c7..ab4ae8e150 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -3993,7 +3993,7 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver, unsigned long resource, qemuMigrationParamsPtr migParams) { - virDomainPtr ddomain = NULL; + g_autoptr(virDomain) ddomain = NULL; char *uri_out = NULL; char *cookie = NULL; char *dom_xml = NULL; @@ -4103,7 +4103,6 @@ qemuMigrationSrcPerformPeer2Peer2(virQEMUDriverPtr driver, cleanup: if (ddomain) { - virObjectUnref(ddomain); ret = 0; } else { ret = -1; @@ -4142,7 +4141,7 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver, bool useParams, unsigned long flags) { - virDomainPtr ddomain = NULL; + g_autoptr(virDomain) ddomain = NULL; char *uri_out = NULL; char *cookiein = NULL; char *cookieout = NULL; @@ -4439,7 +4438,6 @@ qemuMigrationSrcPerformPeer2Peer3(virQEMUDriverPtr driver, cleanup: if (ddomain) { - virObjectUnref(ddomain); ret = 0; } else { ret = -1; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index c5eca3bc0c..29944bdfa3 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -2264,7 +2264,7 @@ remoteDispatchDomainGetSchedulerType(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_scheduler_type_args *args, remote_domain_get_scheduler_type_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *type; int nparams; int rv = -1; @@ -2286,7 +2286,6 @@ remoteDispatchDomainGetSchedulerType(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -2298,7 +2297,7 @@ remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_scheduler_parameters_args *args, remote_domain_get_scheduler_parameters_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int rv = -1; @@ -2334,7 +2333,6 @@ remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -2346,7 +2344,7 @@ remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server G_GNUC_UN remote_domain_get_scheduler_parameters_flags_args *args, remote_domain_get_scheduler_parameters_flags_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int rv = -1; @@ -2383,7 +2381,6 @@ remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server G_GNUC_UN if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -2395,7 +2392,7 @@ remoteDispatchDomainMemoryStats(virNetServerPtr server G_GNUC_UNUSED, remote_domain_memory_stats_args *args, remote_domain_memory_stats_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virDomainMemoryStatPtr stats = NULL; int nr_stats; size_t i; @@ -2437,7 +2434,6 @@ remoteDispatchDomainMemoryStats(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); VIR_FREE(stats); return rv; } @@ -2450,7 +2446,7 @@ remoteDispatchDomainBlockPeek(virNetServerPtr server G_GNUC_UNUSED, remote_domain_block_peek_args *args, remote_domain_block_peek_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *path; unsigned long long offset; size_t size; @@ -2489,7 +2485,6 @@ remoteDispatchDomainBlockPeek(virNetServerPtr server G_GNUC_UNUSED, virNetMessageSaveError(rerr); VIR_FREE(ret->buffer.buffer_val); } - virObjectUnref(dom); return rv; } @@ -2502,7 +2497,7 @@ remoteDispatchDomainBlockStatsFlags(virNetServerPtr server G_GNUC_UNUSED, remote_domain_block_stats_flags_ret *ret) { virTypedParameterPtr params = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; const char *path = args->path; int nparams = 0; unsigned int flags; @@ -2550,7 +2545,6 @@ remoteDispatchDomainBlockStatsFlags(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -2562,7 +2556,7 @@ remoteDispatchDomainMemoryPeek(virNetServerPtr server G_GNUC_UNUSED, remote_domain_memory_peek_args *args, remote_domain_memory_peek_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; unsigned long long offset; size_t size; unsigned int flags; @@ -2599,7 +2593,6 @@ remoteDispatchDomainMemoryPeek(virNetServerPtr server G_GNUC_UNUSED, virNetMessageSaveError(rerr); VIR_FREE(ret->buffer.buffer_val); } - virObjectUnref(dom); return rv; } @@ -2611,7 +2604,7 @@ remoteDispatchDomainGetSecurityLabel(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_security_label_args *args, remote_domain_get_security_label_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virSecurityLabelPtr seclabel = NULL; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -2639,7 +2632,6 @@ remoteDispatchDomainGetSecurityLabel(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); VIR_FREE(seclabel); return rv; } @@ -2652,7 +2644,7 @@ remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_security_label_list_args *args, remote_domain_get_security_label_list_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virSecurityLabelPtr seclabels = NULL; int len, rv = -1; size_t i; @@ -2689,7 +2681,6 @@ remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); VIR_FREE(seclabels); return rv; } @@ -2738,7 +2729,7 @@ remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_vcpu_pin_info_args *args, remote_domain_get_vcpu_pin_info_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; unsigned char *cpumaps = NULL; int num; int rv = -1; @@ -2788,7 +2779,6 @@ remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); VIR_FREE(cpumaps); - virObjectUnref(dom); return rv; } @@ -2800,7 +2790,7 @@ remoteDispatchDomainPinEmulator(virNetServerPtr server G_GNUC_UNUSED, remote_domain_pin_emulator_args *args) { int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virConnectPtr conn = remoteGetHypervisorConn(client); if (!conn) @@ -2820,7 +2810,6 @@ remoteDispatchDomainPinEmulator(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -2833,7 +2822,7 @@ remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_emulator_pin_info_args *args, remote_domain_get_emulator_pin_info_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; unsigned char *cpumaps = NULL; int r; int rv = -1; @@ -2867,7 +2856,6 @@ remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); VIR_FREE(cpumaps); - virObjectUnref(dom); return rv; } @@ -2879,7 +2867,7 @@ remoteDispatchDomainGetVcpus(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_vcpus_args *args, remote_domain_get_vcpus_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virVcpuInfoPtr info = NULL; unsigned char *cpumaps = NULL; int info_len; @@ -2945,7 +2933,6 @@ remoteDispatchDomainGetVcpus(virNetServerPtr server G_GNUC_UNUSED, } VIR_FREE(cpumaps); VIR_FREE(info); - virObjectUnref(dom); return rv; } @@ -2960,7 +2947,7 @@ remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server G_GNUC_UNUSED, int rv = -1; size_t i; virDomainIOThreadInfoPtr *info = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; remote_domain_iothread_info *dst; int ninfo = 0; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -3010,7 +2997,6 @@ remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); if (ninfo >= 0) for (i = 0; i < ninfo; i++) virDomainIOThreadInfoFree(info[i]); @@ -3128,7 +3114,7 @@ remoteDispatchDomainGetMemoryParameters(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_memory_parameters_args *args, remote_domain_get_memory_parameters_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; unsigned int flags; @@ -3176,7 +3162,6 @@ remoteDispatchDomainGetMemoryParameters(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -3188,7 +3173,7 @@ remoteDispatchDomainGetNumaParameters(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_numa_parameters_args *args, remote_domain_get_numa_parameters_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; unsigned int flags; @@ -3236,7 +3221,6 @@ remoteDispatchDomainGetNumaParameters(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -3248,7 +3232,7 @@ remoteDispatchDomainGetBlkioParameters(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_blkio_parameters_args *args, remote_domain_get_blkio_parameters_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; unsigned int flags; @@ -3296,7 +3280,6 @@ remoteDispatchDomainGetBlkioParameters(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -3444,7 +3427,7 @@ remoteDispatchDomainGetLaunchSecurityInfo(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_launch_security_info_args *args, remote_domain_get_launch_security_info_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int rv = -1; @@ -3472,7 +3455,6 @@ remoteDispatchDomainGetLaunchSecurityInfo(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -3484,7 +3466,7 @@ remoteDispatchDomainGetPerfEvents(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_perf_events_args *args, remote_domain_get_perf_events_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int rv = -1; @@ -3512,7 +3494,6 @@ remoteDispatchDomainGetPerfEvents(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -3524,7 +3505,7 @@ remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_block_job_info_args *args, remote_domain_get_block_job_info_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virDomainBlockJobInfo tmp; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -3549,7 +3530,6 @@ remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -3561,7 +3541,7 @@ remoteDispatchDomainGetBlockIoTune(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_block_io_tune_args *args, remote_domain_get_block_io_tune_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; virTypedParameterPtr params = NULL; int nparams = 0; @@ -3609,7 +3589,6 @@ remoteDispatchDomainGetBlockIoTune(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -4416,7 +4395,7 @@ remoteDispatchDomainGetState(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_state_args *args, remote_domain_get_state_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -4434,7 +4413,6 @@ remoteDispatchDomainGetState(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -4532,7 +4510,7 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU daemonClientEventCallbackPtr ref; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virConnectPtr conn = remoteGetHypervisorConn(client); virMutexLock(&priv->lock); @@ -4590,7 +4568,6 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -4705,7 +4682,7 @@ qemuDispatchDomainMonitorCommand(virNetServerPtr server G_GNUC_UNUSED, qemu_domain_monitor_command_args *args, qemu_domain_monitor_command_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -4724,7 +4701,6 @@ qemuDispatchDomainMonitorCommand(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -4738,7 +4714,7 @@ remoteDispatchDomainMigrateBegin3(virNetServerPtr server G_GNUC_UNUSED, remote_domain_migrate_begin3_ret *ret) { char *xml = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *dname; char *xmlin; char *cookieout = NULL; @@ -4772,7 +4748,6 @@ remoteDispatchDomainMigrateBegin3(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -4838,7 +4813,7 @@ remoteDispatchDomainMigratePerform3(virNetServerPtr server G_GNUC_UNUSED, remote_domain_migrate_perform3_args *args, remote_domain_migrate_perform3_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *xmlin; char *dname; char *uri; @@ -4877,7 +4852,6 @@ remoteDispatchDomainMigratePerform3(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -4890,7 +4864,7 @@ remoteDispatchDomainMigrateFinish3(virNetServerPtr server G_GNUC_UNUSED, remote_domain_migrate_finish3_args *args, remote_domain_migrate_finish3_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *cookieout = NULL; int cookieoutlen = 0; char *uri; @@ -4927,7 +4901,6 @@ remoteDispatchDomainMigrateFinish3(virNetServerPtr server G_GNUC_UNUSED, virNetMessageSaveError(rerr); VIR_FREE(cookieout); } - virObjectUnref(dom); return rv; } @@ -4939,7 +4912,7 @@ remoteDispatchDomainMigrateConfirm3(virNetServerPtr server G_GNUC_UNUSED, virNetMessageErrorPtr rerr, remote_domain_migrate_confirm3_args *args) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -4960,7 +4933,6 @@ remoteDispatchDomainMigrateConfirm3(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5035,7 +5007,7 @@ remoteDispatchDomainOpenGraphics(virNetServerPtr server G_GNUC_UNUSED, virNetMessageErrorPtr rerr, remote_domain_open_graphics_args *args) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; int fd = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5061,7 +5033,6 @@ remoteDispatchDomainOpenGraphics(virNetServerPtr server G_GNUC_UNUSED, VIR_FORCE_CLOSE(fd); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5073,7 +5044,7 @@ remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server G_GNUC_UNUSED, virNetMessageErrorPtr rerr, remote_domain_open_graphics_fd_args *args) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; int fd = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5101,7 +5072,6 @@ remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5114,7 +5084,7 @@ remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_interface_parameters_args *args, remote_domain_get_interface_parameters_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; const char *device = args->device; int nparams = 0; @@ -5163,7 +5133,6 @@ remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -5175,7 +5144,7 @@ remoteDispatchDomainGetCPUStats(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_cpu_stats_args *args, remote_domain_get_cpu_stats_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int rv = -1; int percpu_len = 0; @@ -5232,7 +5201,6 @@ remoteDispatchDomainGetCPUStats(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, args->ncpus * args->nparams); - virObjectUnref(dom); return rv; } @@ -5245,7 +5213,7 @@ remoteDispatchDomainGetDiskErrors(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_disk_errors_ret *ret) { int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virDomainDiskErrorPtr errors = NULL; int len = 0; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5283,7 +5251,6 @@ remoteDispatchDomainGetDiskErrors(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); if (errors && len > 0) { size_t i; for (i = 0; i < len; i++) @@ -5439,7 +5406,7 @@ lxcDispatchDomainOpenNamespace(virNetServerPtr server G_GNUC_UNUSED, int rv = -1; int *fdlist = NULL; int ret; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; size_t i; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5472,7 +5439,6 @@ lxcDispatchDomainOpenNamespace(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5484,7 +5450,7 @@ remoteDispatchDomainGetJobStats(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_job_stats_args *args, remote_domain_get_job_stats_ret *ret) { - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; int rv = -1; @@ -5513,7 +5479,6 @@ remoteDispatchDomainGetJobStats(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } @@ -5526,7 +5491,7 @@ remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server G_GNUC_UNUSED, remote_domain_migrate_begin3_params_ret *ret) { char *xml = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; char *cookieout = NULL; @@ -5567,7 +5532,6 @@ remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server G_GNUC_UNUSED, virTypedParamsFree(params, nparams); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5705,7 +5669,7 @@ remoteDispatchDomainMigratePerform3Params(virNetServerPtr server G_GNUC_UNUSED, { virTypedParameterPtr params = NULL; int nparams = 0; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *cookieout = NULL; int cookieoutlen = 0; char *dconnuri; @@ -5748,7 +5712,6 @@ remoteDispatchDomainMigratePerform3Params(virNetServerPtr server G_GNUC_UNUSED, virTypedParamsFree(params, nparams); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5763,7 +5726,7 @@ remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server G_GNUC_UNUSED, { virTypedParameterPtr params = NULL; int nparams = 0; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; char *cookieout = NULL; int cookieoutlen = 0; int rv = -1; @@ -5805,7 +5768,6 @@ remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server G_GNUC_UNUSED, virNetMessageSaveError(rerr); VIR_FREE(cookieout); } - virObjectUnref(dom); return rv; } @@ -5819,7 +5781,7 @@ remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server G_GNUC_UNUSED, { virTypedParameterPtr params = NULL; int nparams = 0; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -5853,7 +5815,6 @@ remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server G_GNUC_UNUSED, virTypedParamsFree(params, nparams); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5916,7 +5877,7 @@ remoteDispatchDomainCreateXMLWithFiles(virNetServerPtr server G_GNUC_UNUSED, remote_domain_create_xml_with_files_ret *ret) { int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int *files = NULL; unsigned int nfiles = 0; size_t i; @@ -5948,7 +5909,6 @@ remoteDispatchDomainCreateXMLWithFiles(virNetServerPtr server G_GNUC_UNUSED, VIR_FREE(files); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -5961,7 +5921,7 @@ static int remoteDispatchDomainCreateWithFiles(virNetServerPtr server G_GNUC_UNU remote_domain_create_with_files_ret *ret) { int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; int *files = NULL; unsigned int nfiles = 0; size_t i; @@ -5996,7 +5956,6 @@ static int remoteDispatchDomainCreateWithFiles(virNetServerPtr server G_GNUC_UNU VIR_FREE(files); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -6493,7 +6452,7 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS daemonClientEventCallbackPtr ref; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; const char *event = args->event ? *args->event : NULL; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -6547,7 +6506,6 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -6606,7 +6564,7 @@ remoteDispatchDomainGetTime(virNetServerPtr server G_GNUC_UNUSED, remote_domain_get_time_ret *ret) { int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; long long seconds; unsigned int nseconds; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -6627,7 +6585,6 @@ remoteDispatchDomainGetTime(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); return rv; } @@ -6939,7 +6896,7 @@ remoteDispatchDomainGetFSInfo(virNetServerPtr server G_GNUC_UNUSED, int rv = -1; size_t i, j; virDomainFSInfoPtr *info = NULL; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; remote_domain_fsinfo *dst; int ninfo = 0; size_t ndisk; @@ -7023,7 +6980,6 @@ remoteDispatchDomainGetFSInfo(virNetServerPtr server G_GNUC_UNUSED, VIR_FREE(ret->info.info_val); } } - virObjectUnref(dom); if (ninfo >= 0) for (i = 0; i < ninfo; i++) virDomainFSInfoFree(info[i]); @@ -7123,7 +7079,7 @@ remoteDispatchDomainInterfaceAddresses(virNetServerPtr server G_GNUC_UNUSED, { size_t i; int rv = -1; - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virDomainInterfacePtr *ifaces = NULL; int ifaces_count = 0; virConnectPtr conn = remoteGetHypervisorConn(client); @@ -7146,8 +7102,6 @@ remoteDispatchDomainInterfaceAddresses(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); - virObjectUnref(dom); - if (ifaces && ifaces_count > 0) { for (i = 0; i < ifaces_count; i++) virDomainInterfaceFree(ifaces[i]); @@ -7419,7 +7373,7 @@ remoteDispatchDomainGetGuestInfo(virNetServerPtr server G_GNUC_UNUSED, { int rv = -1; virConnectPtr conn = remoteGetHypervisorConn(client); - virDomainPtr dom = NULL; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; @@ -7445,7 +7399,6 @@ remoteDispatchDomainGetGuestInfo(virNetServerPtr server G_GNUC_UNUSED, if (rv < 0) virNetMessageSaveError(rerr); virTypedParamsFree(params, nparams); - virObjectUnref(dom); return rv; } diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 58ae7528a8..f00083e0c3 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -4572,7 +4572,7 @@ remoteDomainBuildEventLifecycleHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4580,7 +4580,6 @@ remoteDomainBuildEventLifecycleHelper(virConnectPtr conn, return; event = virDomainEventLifecycleNewFromDom(dom, msg->event, msg->detail); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -4610,7 +4609,7 @@ remoteDomainBuildEventRebootHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4618,7 +4617,6 @@ remoteDomainBuildEventRebootHelper(virConnectPtr conn, return; event = virDomainEventRebootNewFromDom(dom); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -4647,7 +4645,7 @@ remoteDomainBuildEventRTCChangeHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4655,7 +4653,6 @@ remoteDomainBuildEventRTCChangeHelper(virConnectPtr conn, return; event = virDomainEventRTCChangeNewFromDom(dom, msg->offset); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -4684,7 +4681,7 @@ remoteDomainBuildEventWatchdogHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4692,7 +4689,6 @@ remoteDomainBuildEventWatchdogHelper(virConnectPtr conn, return; event = virDomainEventWatchdogNewFromDom(dom, msg->action); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -4721,7 +4717,7 @@ remoteDomainBuildEventIOErrorHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4732,7 +4728,6 @@ remoteDomainBuildEventIOErrorHelper(virConnectPtr conn, msg->srcPath, msg->devAlias, msg->action); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -4761,7 +4756,7 @@ remoteDomainBuildEventIOErrorReasonHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4774,8 +4769,6 @@ remoteDomainBuildEventIOErrorReasonHelper(virConnectPtr conn, msg->action, msg->reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -4803,7 +4796,7 @@ remoteDomainBuildEventBlockJobHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4813,8 +4806,6 @@ remoteDomainBuildEventBlockJobHelper(virConnectPtr conn, event = virDomainEventBlockJobNewFromDom(dom, msg->path, msg->type, msg->status); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -4843,7 +4834,7 @@ remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; remote_domain_event_block_job_2_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4853,8 +4844,6 @@ remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virDomainEventBlockJob2NewFromDom(dom, msg->dst, msg->type, msg->status); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -4864,7 +4853,7 @@ remoteDomainBuildEventGraphicsHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; virDomainEventGraphicsAddressPtr localAddr = NULL; virDomainEventGraphicsAddressPtr remoteAddr = NULL; @@ -4904,8 +4893,6 @@ remoteDomainBuildEventGraphicsHelper(virConnectPtr conn, msg->authScheme, subject); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); return; @@ -4928,7 +4915,6 @@ remoteDomainBuildEventGraphicsHelper(virConnectPtr conn, VIR_FREE(subject->identities); VIR_FREE(subject); } - virObjectUnref(dom); return; } static void @@ -4956,7 +4942,7 @@ remoteDomainBuildEventControlErrorHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -4965,8 +4951,6 @@ remoteDomainBuildEventControlErrorHelper(virConnectPtr conn, event = virDomainEventControlErrorNewFromDom(dom); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -4995,7 +4979,7 @@ remoteDomainBuildEventDiskChangeHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5008,8 +4992,6 @@ remoteDomainBuildEventDiskChangeHelper(virConnectPtr conn, msg->devAlias, msg->reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5038,7 +5020,7 @@ remoteDomainBuildEventTrayChangeHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5049,8 +5031,6 @@ remoteDomainBuildEventTrayChangeHelper(virConnectPtr conn, msg->devAlias, msg->reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5079,7 +5059,7 @@ remoteDomainBuildEventPMWakeupHelper(virConnectPtr conn, int reason) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5088,8 +5068,6 @@ remoteDomainBuildEventPMWakeupHelper(virConnectPtr conn, event = virDomainEventPMWakeupNewFromDom(dom, reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5119,7 +5097,7 @@ remoteDomainBuildEventPMSuspendHelper(virConnectPtr conn, int reason) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5128,8 +5106,6 @@ remoteDomainBuildEventPMSuspendHelper(virConnectPtr conn, event = virDomainEventPMSuspendNewFromDom(dom, reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5159,7 +5135,7 @@ remoteDomainBuildEventBalloonChangeHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5167,7 +5143,6 @@ remoteDomainBuildEventBalloonChangeHelper(virConnectPtr conn, return; event = virDomainEventBalloonChangeNewFromDom(dom, msg->actual); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } @@ -5198,7 +5173,7 @@ remoteDomainBuildEventPMSuspendDiskHelper(virConnectPtr conn, int reason) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5207,8 +5182,6 @@ remoteDomainBuildEventPMSuspendDiskHelper(virConnectPtr conn, event = virDomainEventPMSuspendDiskNewFromDom(dom, reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5238,7 +5211,7 @@ remoteDomainBuildEventDeviceRemovedHelper(virConnectPtr conn, int callbackID) { struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5247,8 +5220,6 @@ remoteDomainBuildEventDeviceRemovedHelper(virConnectPtr conn, event = virDomainEventDeviceRemovedNewFromDom(dom, msg->devAlias); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, callbackID); } static void @@ -5278,7 +5249,7 @@ remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog G_GNUC_UNU virConnectPtr conn = opaque; remote_domain_event_callback_device_added_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5287,8 +5258,6 @@ remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog G_GNUC_UNU event = virDomainEventDeviceAddedNewFromDom(dom, msg->devAlias); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5301,7 +5270,7 @@ remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog G_ virConnectPtr conn = opaque; remote_domain_event_callback_device_removal_failed_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; if (!(dom = get_nonnull_domain(conn, msg->dom))) @@ -5309,8 +5278,6 @@ remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog G_ event = virDomainEventDeviceRemovalFailedNewFromDom(dom, msg->devAlias); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5322,7 +5289,7 @@ remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; remote_domain_event_callback_tunable_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virTypedParameterPtr params = NULL; int nparams = 0; virObjectEventPtr event = NULL; @@ -5341,8 +5308,6 @@ remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog G_GNUC_UNUSED, event = virDomainEventTunableNewFromDom(dom, params, nparams); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5355,7 +5320,7 @@ remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog G_GNUC_ virConnectPtr conn = opaque; remote_domain_event_callback_agent_lifecycle_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; if (!(dom = get_nonnull_domain(conn, msg->dom))) @@ -5364,8 +5329,6 @@ remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog G_GNUC_ event = virDomainEventAgentLifecycleNewFromDom(dom, msg->state, msg->reason); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5379,7 +5342,7 @@ remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog G_G virConnectPtr conn = opaque; remote_domain_event_callback_migration_iteration_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; if (!(dom = get_nonnull_domain(conn, msg->dom))) @@ -5387,8 +5350,6 @@ remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog G_G event = virDomainEventMigrationIterationNewFromDom(dom, msg->iteration); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5402,7 +5363,7 @@ remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog G_GNUC_UN virConnectPtr conn = opaque; remote_domain_event_callback_job_completed_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; virTypedParameterPtr params = NULL; int nparams = 0; @@ -5420,8 +5381,6 @@ remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog G_GNUC_UN event = virDomainEventJobCompletedNewFromDom(dom, params, nparams); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5434,7 +5393,7 @@ remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog G_GNUC_ virConnectPtr conn = opaque; remote_domain_event_callback_metadata_change_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; if (!(dom = get_nonnull_domain(conn, msg->dom))) @@ -5442,8 +5401,6 @@ remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog G_GNUC_ event = virDomainEventMetadataChangeNewFromDom(dom, msg->type, msg->nsuri ? *msg->nsuri : NULL); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5600,7 +5557,7 @@ remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; struct private_data *priv = conn->privateData; qemu_domain_monitor_event_msg *msg = evdata; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; dom = get_nonnull_domain(conn, msg->dom); @@ -5611,7 +5568,6 @@ remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog G_GNUC_UNUSED, msg->event, msg->seconds, msg->micros, msg->details ? *msg->details : NULL); - virObjectUnref(dom); virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -5660,7 +5616,7 @@ remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog G_GNUC_UNUSED, virConnectPtr conn = opaque; remote_domain_event_block_threshold_msg *msg = evdata; struct private_data *priv = conn->privateData; - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virObjectEventPtr event = NULL; if (!(dom = get_nonnull_domain(conn, msg->dom))) @@ -5670,8 +5626,6 @@ remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog G_GNUC_UNUSED, msg->path ? *msg->path : NULL, msg->threshold, msg->excess); - virObjectUnref(dom); - virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID); } @@ -7779,7 +7733,7 @@ remoteConnectGetAllDomainStats(virConnectPtr conn, cleanup: if (elem) { - virObjectUnref(elem->dom); + g_clear_object(&elem->dom); VIR_FREE(elem); } virDomainStatsRecordListFree(tmpret); diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 29b3feb17c..8c45fa3bc7 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -196,6 +196,7 @@ sub get_conn_method { my %gobject_impl = ( virAdmClient => 1, virAdmServer => 1, + virDomain => 1, virDomainCheckpoint => 1, virDomainSnapshot => 1, virInterface => 1, @@ -626,7 +627,7 @@ elsif ($mode eq "server") { my $type_name = name_to_TypeName($1); my $unref_impl = get_unref_method("virDomain${type_name}"); - push(@vars_list, "virDomainPtr dom = NULL"); + push(@vars_list, "g_autoptr(virDomain) dom = NULL"); push(@vars_list, "virDomain${type_name}Ptr ${1} = NULL"); push(@getters_list, " if (!(dom = get_nonnull_domain($conn_var, args->${2}.dom)))\n" . @@ -637,8 +638,7 @@ elsif ($mode eq "server") { push(@args_list, "$1"); push(@free_list, " if ($1)\n" . - " $unref_impl($1);\n" . - " virObjectUnref(dom);"); + " $unref_impl($1);\n"); } elsif ($args_member =~ m/^(?:(?:admin|remote)_string|remote_uuid) (\S+)<\S+>;/) { push(@args_list, $conn_var) if !@args_list; push(@args_list, "args->$1.$1_val"); diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index 7ca314d0dc..ef0bf8b5bd 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -2287,7 +2287,7 @@ static virDomainPtr vboxDomainCreateXML(virConnectPtr conn, const char *xml, * change this behaviour to the expected one. */ - virDomainPtr dom; + g_autoptr(virDomain) dom = NULL; virCheckFlags(0, NULL); @@ -2297,11 +2297,10 @@ static virDomainPtr vboxDomainCreateXML(virConnectPtr conn, const char *xml, if (vboxDomainCreate(dom) < 0) { vboxDomainUndefineFlags(dom, 0); - virObjectUnref(dom); return NULL; } - return dom; + return g_steal_pointer(&dom); } static int vboxDomainIsActive(virDomainPtr dom) @@ -7618,11 +7617,7 @@ vboxConnectListAllDomains(virConnectPtr conn, ret = count; cleanup: - if (doms) { - for (i = 0; i < count; i++) - virObjectUnref(doms[i]); - } - VIR_FREE(doms); + virGObjectListFreeCount(doms, count); gVBoxAPI.UArray.vboxArrayRelease(&machines); return ret; diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index 4fe072233e..64383889a2 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -3146,7 +3146,7 @@ vzDomainMigratePerformP2P(virDomainObjPtr dom, int nparams, unsigned int flags) { - virDomainPtr ddomain = NULL; + g_autoptr(virDomain) ddomain = NULL; char *uri = NULL; char *cookiein = NULL; char *cookieout = NULL; @@ -3222,7 +3222,6 @@ vzDomainMigratePerformP2P(virDomainObjPtr dom, virObjectLock(dom); if (ddomain) ret = 0; - virObjectUnref(ddomain); /* confirm step is NOOP thus no need to call it */ @@ -3735,7 +3734,7 @@ vzDomainGetAllStats(virConnectPtr conn, error: virTypedParamsFree(stat->params, stat->nparams); - virObjectUnref(stat->dom); + g_clear_object(&stat->dom); VIR_FREE(stat); return NULL; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/rpc/gendispatch.pl | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index 8c45fa3bc7..e605d50060 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -613,22 +613,17 @@ elsif ($mode eq "server") { next } elsif ($args_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|secret|nwfilter|nwfilter_binding) (\S+);/) { my $type_name = name_to_TypeName($1); - my $unref_impl = get_unref_method("vir${type_name}"); - push(@vars_list, "vir${type_name}Ptr $2 = NULL"); + push(@vars_list, "g_autoptr(vir${type_name}) $2 = NULL"); push(@getters_list, " if (!($2 = get_nonnull_$1($conn_var, args->$2)))\n" . " goto cleanup;\n"); push(@args_list, "$2"); - push(@free_list, - " if ($2)\n" . - " $unref_impl($2);"); } elsif ($args_member =~ m/^remote_nonnull_domain_(checkpoint|snapshot) (\S+);$/) { my $type_name = name_to_TypeName($1); - my $unref_impl = get_unref_method("virDomain${type_name}"); push(@vars_list, "g_autoptr(virDomain) dom = NULL"); - push(@vars_list, "virDomain${type_name}Ptr ${1} = NULL"); + push(@vars_list, "g_autoptr(virDomain${type_name}) ${1} = NULL"); push(@getters_list, " if (!(dom = get_nonnull_domain($conn_var, args->${2}.dom)))\n" . " goto cleanup;\n" . @@ -636,9 +631,6 @@ elsif ($mode eq "server") { " if (!($1 = get_nonnull_domain_${1}(dom, args->$2)))\n" . " goto cleanup;\n"); push(@args_list, "$1"); - push(@free_list, - " if ($1)\n" . - " $unref_impl($1);\n"); } elsif ($args_member =~ m/^(?:(?:admin|remote)_string|remote_uuid) (\S+)<\S+>;/) { push(@args_list, $conn_var) if !@args_list; push(@args_list, "args->$1.$1_val"); @@ -852,12 +844,8 @@ elsif ($mode eq "server") { $single_ret_var = undef; $single_ret_by_ref = 1; } else { - my $unref_impl = get_unref_method("vir${type_name}"); - push(@vars_list, "vir${type_name}Ptr $2 = NULL"); + push(@vars_list, "g_autoptr(vir${type_name}) $2 = NULL"); push(@ret_list, "make_nonnull_$1(&ret->$2, $2);\n"); - push(@free_list, - " if ($2)\n" . - " $unref_impl($2);"); $single_ret_var = $2; $single_ret_by_ref = 0; $single_ret_check = " == NULL"; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/network_conf.c | 24 ++++++------------------ src/conf/network_conf.h | 12 +++++++++--- src/network/bridge_driver.c | 2 +- tests/networkxml2xmltest.c | 3 +-- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 819b645df7..39330124a3 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -70,35 +70,23 @@ VIR_ENUM_IMPL(virNetworkTaint, "hook-script", ); -static virClassPtr virNetworkXMLOptionClass; +G_DEFINE_TYPE(virNetworkXMLOption, vir_network_xml_option, G_TYPE_OBJECT); static void -virNetworkXMLOptionDispose(void *obj G_GNUC_UNUSED) +vir_network_xml_option_init(virNetworkXMLOption *xmlopt G_GNUC_UNUSED) { - return; } -static int -virNetworkXMLOnceInit(void) +static void +vir_network_xml_option_class_init(virNetworkXMLOptionClass *klass G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virNetworkXMLOption, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virNetworkXML); - virNetworkXMLOptionPtr virNetworkXMLOptionNew(virXMLNamespacePtr xmlns) { - virNetworkXMLOptionPtr xmlopt; - - if (virNetworkXMLInitialize() < 0) - return NULL; - - if (!(xmlopt = virObjectNew(virNetworkXMLOptionClass))) - return NULL; + virNetworkXMLOptionPtr xmlopt = + VIR_NETWORK_XML_OPTION(g_object_new(VIR_TYPE_NETWORK_XML_OPTION, NULL)); if (xmlns) xmlopt->ns = *xmlns; diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h index db7243eef5..e492ca9c6f 100644 --- a/src/conf/network_conf.h +++ b/src/conf/network_conf.h @@ -33,17 +33,23 @@ #include "device_conf.h" #include "virbitmap.h" #include "networkcommon_conf.h" -#include "virobject.h" #include "virmacmap.h" #include "virenum.h" #include "virxml.h" +#include <glib-object.h> struct _virNetworkXMLOption { - virObject parent; + GObject parent; virXMLNamespace ns; }; -typedef struct _virNetworkXMLOption virNetworkXMLOption; + +#define VIR_TYPE_NETWORK_XML_OPTION vir_network_xml_option_get_type() +G_DECLARE_FINAL_TYPE(virNetworkXMLOption, + vir_network_xml_option, + VIR, + NETWORK_XML_OPTION, + GObject); typedef virNetworkXMLOption *virNetworkXMLOptionPtr; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 728d432c4c..646d567974 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -889,7 +889,7 @@ networkStateCleanup(void) return -1; virObjectUnref(network_driver->networkEventState); - virObjectUnref(network_driver->xmlopt); + g_clear_object(&network_driver->xmlopt); /* free inactive networks */ virObjectUnref(network_driver->networks); diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c index ec679e72ee..b881e03c3b 100644 --- a/tests/networkxml2xmltest.c +++ b/tests/networkxml2xmltest.c @@ -30,7 +30,7 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, int ret; testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS; virNetworkDefPtr dev = NULL; - virNetworkXMLOptionPtr xmlopt = NULL; + g_autoptr(virNetworkXMLOption) xmlopt = NULL; if (!(xmlopt = networkDnsmasqCreateXMLConf())) goto cleanup; @@ -72,7 +72,6 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml, VIR_FREE(actual); virNetworkDefFree(dev); - virObjectUnref(xmlopt); return ret; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/lxc/lxc_conf.c | 31 +++++++++++++-------------- src/lxc/lxc_conf.h | 10 +++++++-- src/lxc/lxc_driver.c | 50 +++++++++++++++---------------------------- src/lxc/lxc_process.c | 33 +++++++++------------------- 4 files changed, 50 insertions(+), 74 deletions(-) diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index 6942781731..399e774ffe 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -42,19 +42,20 @@ VIR_LOG_INIT("lxc.lxc_conf"); -static virClassPtr virLXCDriverConfigClass; -static void virLXCDriverConfigDispose(void *obj); +G_DEFINE_TYPE(virLXCDriverConfig, vir_lxc_driver_config, G_TYPE_OBJECT); -static int virLXCConfigOnceInit(void) -{ - if (!VIR_CLASS_NEW(virLXCDriverConfig, virClassForObject())) - return -1; +static void virLXCDriverConfigFinalize(GObject *obj); - return 0; +static void vir_lxc_driver_config_init(virLXCDriverConfig *cfg G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(virLXCConfig); +static void vir_lxc_driver_config_class_init(virLXCDriverConfigClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + obj->finalize = virLXCDriverConfigFinalize; +} /* Functions */ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver) @@ -226,11 +227,7 @@ virLXCDriverConfigNew(void) { virLXCDriverConfigPtr cfg; - if (virLXCConfigInitialize() < 0) - return NULL; - - if (!(cfg = virObjectNew(virLXCDriverConfigClass))) - return NULL; + cfg = VIR_LXC_DRIVER_CONFIG(g_object_new(VIR_TYPE_LXC_DRIVER_CONFIG, NULL)); cfg->securityDefaultConfined = false; cfg->securityRequireConfined = false; @@ -277,19 +274,21 @@ virLXCDriverConfigPtr virLXCDriverGetConfig(virLXCDriverPtr driver) { virLXCDriverConfigPtr cfg; lxcDriverLock(driver); - cfg = virObjectRef(driver->config); + cfg = g_object_ref(driver->config); lxcDriverUnlock(driver); return cfg; } static void -virLXCDriverConfigDispose(void *obj) +virLXCDriverConfigFinalize(GObject *obj) { - virLXCDriverConfigPtr cfg = obj; + virLXCDriverConfigPtr cfg = VIR_LXC_DRIVER_CONFIG(obj); VIR_FREE(cfg->configDir); VIR_FREE(cfg->autostartDir); VIR_FREE(cfg->stateDir); VIR_FREE(cfg->logDir); VIR_FREE(cfg->securityDriverName); + + G_OBJECT_CLASS(vir_lxc_driver_config_parent_class)->finalize(obj); } diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index f2f0e0a570..5ed84f79c7 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -34,6 +34,7 @@ #include "virusb.h" #include "virclosecallbacks.h" #include "virhostdev.h" +#include <glib-object.h> #define LXC_DRIVER_NAME "LXC" @@ -45,11 +46,16 @@ typedef struct _virLXCDriver virLXCDriver; typedef virLXCDriver *virLXCDriverPtr; -typedef struct _virLXCDriverConfig virLXCDriverConfig; +#define VIR_TYPE_LXC_DRIVER_CONFIG vir_lxc_driver_config_get_type() +G_DECLARE_FINAL_TYPE(virLXCDriverConfig, + vir_lxc_driver_config, + VIR, + LXC_DRIVER_CONFIG, + GObject); typedef virLXCDriverConfig *virLXCDriverConfigPtr; struct _virLXCDriverConfig { - virObject parent; + GObject parent; char *configDir; char *autostartDir; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 09a2721ca3..288d394989 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -400,7 +400,7 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virDomainPtr dom = NULL; virObjectEventPtr event = NULL; virDomainDefPtr oldDef = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; @@ -458,7 +458,6 @@ lxcDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) virDomainDefFree(oldDef); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return dom; } @@ -475,7 +474,7 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(0, -1); @@ -510,7 +509,6 @@ static int lxcDomainUndefineFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -638,7 +636,7 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, int ret = -1; virLXCDomainObjPrivatePtr priv; virLXCDriverPtr driver = dom->conn->privateData; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG | @@ -719,7 +717,6 @@ static int lxcDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -743,7 +740,7 @@ lxcDomainSetMemoryParameters(virDomainPtr dom, virDomainDefPtr persistentDef = NULL; virDomainObjPtr vm = NULL; virLXCDomainObjPrivatePtr priv = NULL; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virLXCDriverPtr driver = dom->conn->privateData; int ret = -1; @@ -804,7 +801,6 @@ lxcDomainSetMemoryParameters(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -976,7 +972,7 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_START_AUTODESTROY, -1); @@ -1023,7 +1019,6 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); virNWFilterUnlockFilterUpdates(); return ret; } @@ -1079,7 +1074,7 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefPtr def = NULL; virDomainPtr dom = NULL; virObjectEventPtr event = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virCaps) caps = NULL; unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; @@ -1150,7 +1145,6 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virDomainDefFree(def); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); virNWFilterUnlockFilterUpdates(); return dom; } @@ -1582,7 +1576,7 @@ static void lxcNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque) static int lxcStateReload(void) { - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; if (!lxc_driver) return 0; @@ -1594,7 +1588,6 @@ lxcStateReload(void) cfg->autostartDir, false, lxc_driver->xmlopt, lxcNotifyLoadDomain, lxc_driver); - virObjectUnref(cfg); return 0; } @@ -1618,7 +1611,7 @@ static int lxcStateCleanup(void) if (lxc_driver->lockFD != -1) virPidFileRelease(lxc_driver->config->stateDir, "driver", lxc_driver->lockFD); - virObjectUnref(lxc_driver->config); + g_clear_object(&lxc_driver->config); virMutexDestroy(&lxc_driver->lock); VIR_FREE(lxc_driver); @@ -1799,7 +1792,7 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, int ret = -1; int rc; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -1912,7 +1905,6 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom, cleanup: virDomainDefFree(persistentDefCopy); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2237,7 +2229,7 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, virDomainDefPtr def = NULL; virDomainDefPtr persistentDef = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; virLXCDomainObjPrivatePtr priv; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | @@ -2302,7 +2294,6 @@ lxcDomainSetBlkioParameters(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2461,7 +2452,7 @@ static int lxcDomainSetAutostart(virDomainPtr dom, virDomainObjPtr vm; char *configFile = NULL, *autostartLink = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2527,7 +2518,6 @@ static int lxcDomainSetAutostart(virDomainPtr dom, VIR_FREE(configFile); VIR_FREE(autostartLink); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2623,7 +2613,7 @@ static int lxcDomainSuspend(virDomainPtr dom) virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2660,7 +2650,6 @@ static int lxcDomainSuspend(virDomainPtr dom) cleanup: virObjectEventStateQueue(driver->domainEventState, event); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2672,7 +2661,7 @@ static int lxcDomainResume(virDomainPtr dom) int ret = -1; int state; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (!(vm = lxcDomObjFromDomain(dom))) goto cleanup; @@ -2717,7 +2706,6 @@ static int lxcDomainResume(virDomainPtr dom) cleanup: virObjectEventStateQueue(driver->domainEventState, event); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4295,7 +4283,7 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4382,7 +4370,6 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4404,7 +4391,7 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4454,7 +4441,6 @@ static int lxcDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDefFree(vmdef); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4469,7 +4455,7 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL, dev_copy = NULL; int ret = -1; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG, -1); @@ -4550,7 +4536,6 @@ static int lxcDomainDetachDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4823,7 +4808,7 @@ lxcDomainSetMetadata(virDomainPtr dom, { virLXCDriverPtr driver = dom->conn->privateData; virDomainObjPtr vm; - virLXCDriverConfigPtr cfg = NULL; + g_autoptr(virLXCDriverConfig) cfg = NULL; int ret = -1; virCheckFlags(VIR_DOMAIN_AFFECT_LIVE | @@ -4854,7 +4839,6 @@ lxcDomainSetMetadata(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 6a3c9b7da3..c83a6c5a8f 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -162,7 +162,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, size_t i; virLXCDomainObjPrivatePtr priv = vm->privateData; const virNetDevVPortProfile *vport = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virConnectPtr conn = NULL; VIR_DEBUG("Cleanup VM name=%s pid=%d reason=%d", @@ -257,7 +257,6 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, } virDomainObjRemoveTransientDef(vm); - virObjectUnref(cfg); virObjectUnref(conn); } @@ -340,11 +339,10 @@ virLXCProcessSetupInterfaceDirect(virLXCDriverPtr driver, virDomainDefPtr def, virDomainNetDefPtr net) { - char *ret = NULL; char *res_ifname = NULL; const virNetDevBandwidth *bw; const virNetDevVPortProfile *prof; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); const char *linkdev = virDomainNetGetActualDirectDev(net); unsigned int macvlan_create_flags = VIR_NETDEV_MACVLAN_CREATE_IFUP; @@ -386,13 +384,9 @@ virLXCProcessSetupInterfaceDirect(virLXCDriverPtr driver, cfg->stateDir, NULL, 0, macvlan_create_flags) < 0) - goto cleanup; - - ret = res_ifname; + return NULL; - cleanup: - virObjectUnref(cfg); - return ret; + return res_ifname; } static const char *nsInfoLocal[VIR_LXC_DOMAIN_NAMESPACE_LAST] = { @@ -786,7 +780,7 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon G_GNUC_UNUSED, { virLXCDriverPtr driver = lxc_driver; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); ino_t inode = 0; virObjectLock(vm); @@ -806,7 +800,6 @@ static void virLXCProcessMonitorInitNotify(virLXCMonitorPtr mon G_GNUC_UNUSED, VIR_WARN("Cannot update XML with PID for LXC %s", vm->def->name); virObjectUnlock(vm); - virObjectUnref(cfg); } static virLXCMonitorCallbacks monitorCallbacks = { @@ -820,10 +813,10 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, virDomainObjPtr vm) { virLXCMonitorPtr monitor = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); if (virSecurityManagerSetSocketLabel(driver->securityManager, vm->def) < 0) - goto cleanup; + return NULL; /* Hold an extra reference because we can't allow 'vm' to be * deleted while the monitor is active. This will be unreffed @@ -840,11 +833,8 @@ static virLXCMonitorPtr virLXCProcessConnectMonitor(virLXCDriverPtr driver, virObjectUnref(monitor); monitor = NULL; } - goto cleanup; } - cleanup: - virObjectUnref(cfg); return monitor; } @@ -932,7 +922,7 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, char *filterstr; char *outputstr; virCommandPtr cmd; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); cmd = virCommandNew(vm->def->emulator); @@ -1013,7 +1003,6 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver, virCommandRequireHandshake(cmd); cleanup: - virObjectUnref(cfg); return cmd; error: virCommandFree(cmd); @@ -1202,7 +1191,7 @@ int virLXCProcessStart(virConnectPtr conn, virLXCDomainObjPrivatePtr priv = vm->privateData; virCapsPtr caps = NULL; virErrorPtr err = NULL; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); virCgroupPtr selfcgroup; int status; char *pidfile = NULL; @@ -1564,7 +1553,6 @@ int virLXCProcessStart(virConnectPtr conn, VIR_FORCE_CLOSE(handshakefds[1]); VIR_FREE(pidfile); VIR_FREE(logfile); - virObjectUnref(cfg); g_clear_object(&caps); virErrorRestore(&err); @@ -1662,7 +1650,7 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm, { virLXCDriverPtr driver = opaque; virLXCDomainObjPrivatePtr priv; - virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); + g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); int ret = -1; virObjectLock(vm); @@ -1728,7 +1716,6 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm, ret = 0; cleanup: - virObjectUnref(cfg); virObjectUnlock(vm); return ret; -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libxl/libxl_conf.c | 62 ++++++++------ src/libxl/libxl_conf.h | 12 ++- src/libxl/libxl_driver.c | 162 ++++++++++++------------------------ src/libxl/libxl_migration.c | 21 ++--- tests/testutilsxen.c | 2 +- 5 files changed, 107 insertions(+), 152 deletions(-) diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index be37e0835d..9ea4e1c941 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -52,25 +52,40 @@ VIR_LOG_INIT("libxl.libxl_conf"); -static virClassPtr libxlDriverConfigClass; -static void libxlDriverConfigDispose(void *obj); +G_DEFINE_TYPE(libxlDriverConfig, libxl_driver_config, G_TYPE_OBJECT); -static int libxlConfigOnceInit(void) -{ - if (!VIR_CLASS_NEW(libxlDriverConfig, virClassForObject())) - return -1; +static void libxlDriverConfigDispose(GObject *obj); +static void libxlDriverConfigFinalize(GObject *obj); - return 0; +static void +libxl_driver_config_init(libxlDriverConfig *cfg G_GNUC_UNUSED) +{ } -VIR_ONCE_GLOBAL_INIT(libxlConfig); +static void +libxl_driver_config_class_init(libxlDriverConfigClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = libxlDriverConfigDispose; + obj->finalize = libxlDriverConfigFinalize; +} static void -libxlDriverConfigDispose(void *obj) +libxlDriverConfigDispose(GObject *obj) { - libxlDriverConfigPtr cfg = obj; + libxlDriverConfigPtr cfg = LIBXL_DRIVER_CONFIG(obj); g_clear_object(&cfg->caps); + + G_OBJECT_CLASS(libxl_driver_config_parent_class)->dispose(obj); +} + +static void +libxlDriverConfigFinalize(GObject *obj) +{ + libxlDriverConfigPtr cfg = LIBXL_DRIVER_CONFIG(obj); + libxl_ctx_free(cfg->ctx); if (cfg->logger) libxlLoggerFree(cfg->logger); @@ -86,6 +101,8 @@ libxlDriverConfigDispose(void *obj) VIR_FREE(cfg->lockManagerName); VIR_FREE(cfg->channelDir); virFirmwareFreeList(cfg->firmwares, cfg->nfirmwares); + + G_OBJECT_CLASS(libxl_driver_config_parent_class)->finalize(obj); } @@ -1688,13 +1705,8 @@ libxlGetAutoballoonConf(libxlDriverConfigPtr cfg, libxlDriverConfigPtr libxlDriverConfigNew(void) { - libxlDriverConfigPtr cfg; - - if (libxlConfigInitialize() < 0) - return NULL; - - if (!(cfg = virObjectNew(libxlDriverConfigClass))) - return NULL; + g_autoptr(libxlDriverConfig) cfg = + LIBXL_DRIVER_CONFIG(g_object_new(LIBXL_TYPE_DRIVER_CONFIG, NULL)); cfg->configBaseDir = g_strdup(LIBXL_CONFIG_BASE_DIR); cfg->configDir = g_strdup(LIBXL_CONFIG_DIR); @@ -1710,34 +1722,30 @@ libxlDriverConfigNew(void) if (virFirmwareParseList(DEFAULT_LOADER_NVRAM, &cfg->firmwares, &cfg->nfirmwares) < 0) - goto error; + return NULL; #else if (VIR_ALLOC_N(cfg->firmwares, 1) < 0) - goto error; + return NULL; cfg->nfirmwares = 1; if (VIR_ALLOC(cfg->firmwares[0]) < 0) - goto error; + return NULL; cfg->firmwares[0]->name = g_strdup(LIBXL_FIRMWARE_DIR "/ovmf.bin"); #endif /* Always add hvmloader to firmwares */ if (VIR_REALLOC_N(cfg->firmwares, cfg->nfirmwares + 1) < 0) - goto error; + return NULL; cfg->nfirmwares++; if (VIR_ALLOC(cfg->firmwares[cfg->nfirmwares - 1]) < 0) - goto error; + return NULL; cfg->firmwares[cfg->nfirmwares - 1]->name = g_strdup(LIBXL_FIRMWARE_DIR "/hvmloader"); /* defaults for keepalive messages */ cfg->keepAliveInterval = 5; cfg->keepAliveCount = 5; - return cfg; - - error: - virObjectUnref(cfg); - return NULL; + return g_steal_pointer(&cfg); } int diff --git a/src/libxl/libxl_conf.h b/src/libxl/libxl_conf.h index 07b3373170..94386cc8cc 100644 --- a/src/libxl/libxl_conf.h +++ b/src/libxl/libxl_conf.h @@ -37,6 +37,7 @@ #include "virfirmware.h" #include "libxl_capabilities.h" #include "libxl_logger.h" +#include <glib-object.h> #define LIBXL_DRIVER_NAME "xenlight" #define LIBXL_VNC_PORT_MIN 5900 @@ -60,11 +61,17 @@ typedef struct _libxlDriverPrivate libxlDriverPrivate; typedef libxlDriverPrivate *libxlDriverPrivatePtr; -typedef struct _libxlDriverConfig libxlDriverConfig; +#define LIBXL_TYPE_DRIVER_CONFIG libxl_driver_config_get_type() +G_DECLARE_FINAL_TYPE(libxlDriverConfig, + libxl_driver_config, + LIBXL, + DRIVER_CONFIG, + GObject); + typedef libxlDriverConfig *libxlDriverConfigPtr; struct _libxlDriverConfig { - virObject parent; + GObject parent; const libxl_version_info *verInfo; unsigned int version; @@ -102,7 +109,6 @@ struct _libxlDriverConfig { size_t nfirmwares; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(libxlDriverConfig, virObjectUnref); struct _libxlDriverPrivate { diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 0e4d6c56fc..affae0e86e 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -390,7 +390,7 @@ libxlReconnectDomain(virDomainObjPtr vm, { libxlDriverPrivatePtr driver = opaque; libxlDomainObjPrivatePtr priv = vm->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); int rc; libxl_dominfo d_info; int len; @@ -484,7 +484,6 @@ libxlReconnectDomain(virDomainObjPtr vm, libxl_dominfo_dispose(&d_info); virObjectUnlock(vm); virObjectUnref(vm); - virObjectUnref(cfg); return ret; error: @@ -519,7 +518,7 @@ libxlStateCleanup(void) if (libxl_driver->lockFD != -1) virPidFileRelease(libxl_driver->config->stateDir, "driver", libxl_driver->lockFD); - virObjectUnref(libxl_driver->config); + g_clear_object(&libxl_driver->config); virMutexDestroy(&libxl_driver->lock); VIR_FREE(libxl_driver); @@ -589,7 +588,7 @@ const struct libxl_event_hooks ev_hooks = { static int libxlAddDom0(libxlDriverPrivatePtr driver) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def = NULL; virDomainObjPtr vm = NULL; libxl_dominfo d_info; @@ -642,7 +641,6 @@ libxlAddDom0(libxlDriverPrivatePtr driver) libxl_dominfo_dispose(&d_info); virDomainDefFree(def); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -831,7 +829,7 @@ libxlStateInitialize(bool privileged, static int libxlStateReload(void) { - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; if (!libxl_driver) return 0; @@ -849,7 +847,6 @@ libxlStateReload(void) libxlAutostartDomain, libxl_driver); - virObjectUnref(cfg); return 0; } @@ -918,14 +915,13 @@ static int libxlConnectGetVersion(virConnectPtr conn, unsigned long *version) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; if (virConnectGetVersionEnsureACL(conn) < 0) return 0; cfg = libxlDriverConfigGet(driver); *version = cfg->version; - virObjectUnref(cfg); return 0; } @@ -965,7 +961,7 @@ libxlConnectGetMaxVcpus(virConnectPtr conn, const char *type G_GNUC_UNUSED) { int ret; libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; if (virConnectGetMaxVcpusEnsureACL(conn) < 0) return -1; @@ -979,7 +975,6 @@ libxlConnectGetMaxVcpus(virConnectPtr conn, const char *type G_GNUC_UNUSED) if (ret <= 0) ret = -1; - virObjectUnref(cfg); return ret; } @@ -997,7 +992,7 @@ libxlConnectGetCapabilities(virConnectPtr conn) { libxlDriverPrivatePtr driver = conn->privateData; char *xml; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; if (virConnectGetCapabilitiesEnsureACL(conn) < 0) return NULL; @@ -1005,7 +1000,6 @@ libxlConnectGetCapabilities(virConnectPtr conn) cfg = libxlDriverConfigGet(driver); xml = virCapabilitiesFormatXML(cfg->caps); - virObjectUnref(cfg); return xml; } @@ -1041,7 +1035,7 @@ libxlDomainCreateXML(virConnectPtr conn, const char *xml, virDomainDefPtr def; virDomainObjPtr vm = NULL; virDomainPtr dom = NULL; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; virCheckFlags(VIR_DOMAIN_START_PAUSED | @@ -1086,7 +1080,6 @@ libxlDomainCreateXML(virConnectPtr conn, const char *xml, cleanup: virDomainDefFree(def); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return dom; } @@ -1163,7 +1156,7 @@ static int libxlDomainSuspend(virDomainPtr dom) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; @@ -1207,7 +1200,6 @@ libxlDomainSuspend(virDomainPtr dom) cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -1216,7 +1208,7 @@ static int libxlDomainResume(virDomainPtr dom) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virObjectEventPtr event = NULL; int ret = -1; @@ -1261,7 +1253,6 @@ libxlDomainResume(virDomainPtr dom) cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -1269,7 +1260,7 @@ static int libxlDomainShutdownFlags(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; int ret = -1; @@ -1319,7 +1310,6 @@ libxlDomainShutdownFlags(virDomainPtr dom, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -1334,7 +1324,7 @@ static int libxlDomainReboot(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; int ret = -1; @@ -1366,7 +1356,6 @@ libxlDomainReboot(virDomainPtr dom, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -1375,7 +1364,7 @@ libxlDomainDestroyFlags(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; int ret = -1; virObjectEventPtr event = NULL; @@ -1420,7 +1409,6 @@ libxlDomainDestroyFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -1440,7 +1428,7 @@ libxlDomainPMSuspendForDuration(virDomainPtr dom, virDomainObjPtr vm; int ret = -1; libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virObjectEventPtr event = NULL; virCheckFlags(0, -1); @@ -1506,7 +1494,7 @@ libxlDomainPMWakeup(virDomainPtr dom, unsigned int flags) int ret = -1; virObjectEventPtr event = NULL; libxlDomainObjPrivatePtr priv; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virCheckFlags(0, -1); @@ -1635,7 +1623,7 @@ libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virDomainDefPtr persistentDef = NULL; int ret = -1; @@ -1720,7 +1708,6 @@ libxlDomainSetMemoryFlags(virDomainPtr dom, unsigned long newmem, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -1740,7 +1727,7 @@ static int libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; libxl_dominfo d_info; int ret = -1; @@ -1776,7 +1763,6 @@ libxlDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info) cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -1814,7 +1800,7 @@ libxlDoDomainSave(libxlDriverPrivatePtr driver, const char *to, bool managed) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxlSavefileHeader hdr; virObjectEventPtr event = NULL; char *xml = NULL; @@ -1891,7 +1877,6 @@ libxlDoDomainSave(libxlDriverPrivatePtr driver, if (VIR_CLOSE(fd) < 0) virReportSystemError(errno, "%s", _("cannot close file")); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -1956,7 +1941,7 @@ libxlDomainRestoreFlags(virConnectPtr conn, const char *from, const char *dxml, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm = NULL; virDomainDefPtr def = NULL; libxlSavefileHeader hdr; @@ -2009,7 +1994,6 @@ libxlDomainRestoreFlags(virConnectPtr conn, const char *from, virReportSystemError(errno, "%s", _("cannot close file")); virDomainDefFree(def); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2023,7 +2007,7 @@ static int libxlDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virObjectEventPtr event = NULL; bool paused = false; @@ -2106,7 +2090,6 @@ libxlDomainCoreDump(virDomainPtr dom, const char *to, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -2239,7 +2222,7 @@ libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def; virDomainObjPtr vm; libxl_bitmap map; @@ -2377,7 +2360,6 @@ libxlDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus, cleanup: VIR_FREE(bitmask); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2458,7 +2440,7 @@ libxlDomainPinVcpuFlags(virDomainPtr dom, unsigned int vcpu, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr targetDef = NULL; virBitmapPtr pcpumap = NULL; virDomainVcpuDefPtr vcpuinfo; @@ -2526,7 +2508,6 @@ libxlDomainPinVcpuFlags(virDomainPtr dom, unsigned int vcpu, cleanup: virDomainObjEndAPI(&vm); virBitmapFree(pcpumap); - virObjectUnref(cfg); return ret; } @@ -2544,7 +2525,7 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm = NULL; virDomainDefPtr targetDef = NULL; int ret = -1; @@ -2573,7 +2554,6 @@ libxlDomainGetVcpuPinInfo(virDomainPtr dom, int ncpumaps, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2582,7 +2562,7 @@ libxlDomainGetVcpus(virDomainPtr dom, virVcpuInfoPtr info, int maxinfo, unsigned char *cpumaps, int maplen) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; int ret = -1; libxl_vcpuinfo *vcpuinfo; @@ -2634,7 +2614,6 @@ libxlDomainGetVcpus(virDomainPtr dom, virVcpuInfoPtr info, int maxinfo, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2642,7 +2621,7 @@ static char * libxlDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virDomainDefPtr def; char *ret = NULL; @@ -2665,7 +2644,6 @@ libxlDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -2676,7 +2654,7 @@ libxlConnectDomainXMLFromNative(virConnectPtr conn, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def = NULL; g_autoptr(virConf) conf = NULL; char *xml = NULL; @@ -2715,7 +2693,6 @@ libxlConnectDomainXMLFromNative(virConnectPtr conn, cleanup: virDomainDefFree(def); - virObjectUnref(cfg); return xml; } @@ -2726,7 +2703,7 @@ libxlConnectDomainXMLToNative(virConnectPtr conn, const char * nativeFormat, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def = NULL; g_autoptr(virConf) conf = NULL; int len = MAX_CONFIG_SIZE; @@ -2765,7 +2742,6 @@ libxlConnectDomainXMLToNative(virConnectPtr conn, const char * nativeFormat, cleanup: virDomainDefFree(def); - virObjectUnref(cfg); return ret; } @@ -2845,7 +2821,7 @@ static virDomainPtr libxlDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def = NULL; virDomainObjPtr vm = NULL; virDomainPtr dom = NULL; @@ -2895,7 +2871,6 @@ libxlDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flag virDomainDefFree(oldDef); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return dom; } @@ -2910,7 +2885,7 @@ libxlDomainUndefineFlags(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; virObjectEventPtr event = NULL; char *name = NULL; @@ -2966,7 +2941,6 @@ libxlDomainUndefineFlags(virDomainPtr dom, VIR_FREE(name); virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } @@ -2979,7 +2953,7 @@ libxlDomainUndefine(virDomainPtr dom) static int libxlDomainChangeEjectableMedia(virDomainObjPtr vm, virDomainDiskDefPtr disk) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(libxl_driver); virDomainDiskDefPtr origdisk = NULL; libxl_device_disk x_disk; size_t i; @@ -3026,14 +3000,13 @@ libxlDomainChangeEjectableMedia(virDomainObjPtr vm, virDomainDiskDefPtr disk) ret = 0; cleanup: - virObjectUnref(cfg); return ret; } static int libxlDomainAttachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(libxl_driver); virDomainDiskDefPtr l_disk = dev->data.disk; libxl_device_disk x_disk; int ret = -1; @@ -3097,7 +3070,6 @@ libxlDomainAttachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev) } cleanup: - virObjectUnref(cfg); return ret; } @@ -3106,7 +3078,7 @@ libxlDomainAttachHostPCIDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainHostdevDefPtr hostdev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxl_device_pci pcidev; virDomainHostdevDefPtr found; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; @@ -3153,7 +3125,6 @@ libxlDomainAttachHostPCIDevice(libxlDriverPrivatePtr driver, vm->def->name, &hostdev, 1, NULL); cleanup: - virObjectUnref(cfg); libxl_device_pci_dispose(&pcidev); return ret; } @@ -3164,7 +3135,7 @@ libxlDomainAttachControllerDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainControllerDefPtr controller) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); const char *type = virDomainControllerTypeToString(controller->type); libxl_device_usbctrl usbctrl; int ret = -1; @@ -3208,7 +3179,6 @@ libxlDomainAttachControllerDevice(libxlDriverPrivatePtr driver, ret = 0; cleanup: - virObjectUnref(cfg); libxl_device_usbctrl_dispose(&usbctrl); return ret; } @@ -3218,7 +3188,7 @@ libxlDomainAttachHostUSBDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainHostdevDefPtr hostdev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxl_device_usbdev usbdev; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; int ret = -1; @@ -3288,7 +3258,6 @@ libxlDomainAttachHostUSBDevice(libxlDriverPrivatePtr driver, vm->def->name, &hostdev, 1); cleanup: - virObjectUnref(cfg); libxl_device_usbdev_dispose(&usbdev); return ret; } @@ -3332,7 +3301,7 @@ libxlDomainAttachHostDevice(libxlDriverPrivatePtr driver, static int libxlDomainDetachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(libxl_driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(libxl_driver); virDomainDiskDefPtr l_disk = NULL; libxl_device_disk x_disk; int idx; @@ -3385,7 +3354,6 @@ libxlDomainDetachDeviceDiskLive(virDomainObjPtr vm, virDomainDeviceDefPtr dev) } cleanup: - virObjectUnref(cfg); return ret; } @@ -3394,7 +3362,7 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainNetDefPtr net) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainNetType actualType; libxl_device_nic nic; int ret = -1; @@ -3475,7 +3443,6 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver, virDomainNetReleaseActualDevice(conn, vm->def, net); } virObjectUnref(conn); - virObjectUnref(cfg); virErrorRestore(&save_err); return ret; } @@ -3643,7 +3610,7 @@ libxlDomainDetachHostPCIDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainHostdevDefPtr hostdev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys; virDomainHostdevSubsysPCIPtr pcisrc = &subsys->u.pci; libxl_device_pci pcidev; @@ -3698,7 +3665,6 @@ libxlDomainDetachHostPCIDevice(libxlDriverPrivatePtr driver, virDomainHostdevDefFree(detach); cleanup: - virObjectUnref(cfg); libxl_device_pci_dispose(&pcidev); return ret; } @@ -3714,7 +3680,7 @@ libxlDomainDetachControllerDevice(libxlDriverPrivatePtr driver, virDomainControllerDefPtr controller = dev->data.controller; const char *type = virDomainControllerTypeToString(controller->type); libxl_device_usbctrl usbctrl; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxl_device_usbctrl_init(&usbctrl); @@ -3750,7 +3716,6 @@ libxlDomainDetachControllerDevice(libxlDriverPrivatePtr driver, cleanup: virDomainControllerDefFree(detach); - virObjectUnref(cfg); libxl_device_usbctrl_dispose(&usbctrl); return ret; } @@ -3760,7 +3725,7 @@ libxlDomainDetachHostUSBDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainHostdevDefPtr hostdev) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys; virDomainHostdevSubsysUSBPtr usbsrc = &subsys->u.usb; virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr; @@ -3818,7 +3783,6 @@ libxlDomainDetachHostUSBDevice(libxlDriverPrivatePtr driver, cleanup: virDomainHostdevDefFree(detach); - virObjectUnref(cfg); libxl_device_usbdev_dispose(&usbdev); return ret; } @@ -3861,7 +3825,7 @@ libxlDomainDetachNetDevice(libxlDriverPrivatePtr driver, virDomainObjPtr vm, virDomainNetDefPtr net) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); int detachidx; virDomainNetDefPtr detach = NULL; libxl_device_nic nic; @@ -3912,7 +3876,6 @@ libxlDomainDetachNetDevice(libxlDriverPrivatePtr driver, } virDomainNetRemove(vm->def, detachidx); } - virObjectUnref(cfg); virErrorRestore(&save_err); return ret; } @@ -4101,7 +4064,7 @@ libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm = NULL; virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL; @@ -4173,7 +4136,6 @@ libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml, virDomainDefFree(vmdef); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4189,7 +4151,7 @@ libxlDomainDetachDeviceFlags(virDomainPtr dom, const char *xml, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm = NULL; virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL; @@ -4263,7 +4225,6 @@ libxlDomainDetachDeviceFlags(virDomainPtr dom, const char *xml, virDomainDefFree(vmdef); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4279,7 +4240,7 @@ libxlDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm = NULL; virDomainDefPtr vmdef = NULL; virDomainDeviceDefPtr dev = NULL; @@ -4345,7 +4306,6 @@ libxlDomainUpdateDeviceFlags(virDomainPtr dom, const char *xml, virDomainDefFree(vmdef); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4354,7 +4314,7 @@ libxlNodeGetFreeMemory(virConnectPtr conn) { libxl_physinfo phy_info; libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); unsigned long long ret = 0; libxl_physinfo_init(&phy_info); @@ -4371,7 +4331,6 @@ libxlNodeGetFreeMemory(virConnectPtr conn) cleanup: libxl_physinfo_dispose(&phy_info); - virObjectUnref(cfg); return ret; } @@ -4385,7 +4344,7 @@ libxlNodeGetCellsFreeMemory(virConnectPtr conn, int ret = -1, nr_nodes = 0; libxl_numainfo *numa_info = NULL; libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); if (virNodeGetCellsFreeMemoryEnsureACL(conn) < 0) goto cleanup; @@ -4419,7 +4378,6 @@ libxlNodeGetCellsFreeMemory(virConnectPtr conn, cleanup: libxl_numainfo_list_free(numa_info, nr_nodes); - virObjectUnref(cfg); return ret; } @@ -4484,7 +4442,7 @@ static int libxlDomainSetAutostart(virDomainPtr dom, int autostart) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; char *configFile = NULL, *autostartLink = NULL; int ret = -1; @@ -4548,7 +4506,6 @@ libxlDomainSetAutostart(virDomainPtr dom, int autostart) VIR_FREE(configFile); VIR_FREE(autostartLink); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4556,7 +4513,7 @@ static char * libxlDomainGetSchedulerType(virDomainPtr dom, int *nparams) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; char * ret = NULL; const char *name = NULL; @@ -4603,7 +4560,6 @@ libxlDomainGetSchedulerType(virDomainPtr dom, int *nparams) cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4614,7 +4570,7 @@ libxlDomainGetSchedulerParametersFlags(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; libxl_domain_sched_params sc_info; libxl_scheduler sched_id; @@ -4665,7 +4621,6 @@ libxlDomainGetSchedulerParametersFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4683,7 +4638,7 @@ libxlDomainSetSchedulerParametersFlags(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; libxl_domain_sched_params sc_info; int sched_id; @@ -4749,7 +4704,6 @@ libxlDomainSetSchedulerParametersFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -4846,7 +4800,7 @@ libxlDomainGetNumaParameters(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainObjPtr vm; libxl_bitmap nodemap; virBitmapPtr nodes = NULL; @@ -4954,7 +4908,6 @@ libxlDomainGetNumaParameters(virDomainPtr dom, virBitmapFree(nodes); libxl_bitmap_dispose(&nodemap); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } #endif @@ -5061,7 +5014,7 @@ libxlDomainGetTotalCPUStats(libxlDriverPrivatePtr driver, virTypedParameterPtr params, unsigned int nparams) { - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; libxl_dominfo d_info; int ret = -1; @@ -5086,7 +5039,6 @@ libxlDomainGetTotalCPUStats(libxlDriverPrivatePtr driver, cleanup: libxl_dominfo_dispose(&d_info); - virObjectUnref(cfg); return ret; } @@ -5101,7 +5053,7 @@ libxlDomainGetPerCPUStats(libxlDriverPrivatePtr driver, libxl_vcpuinfo *vcpuinfo; int maxcpu, hostcpus; size_t i; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; int ret = -1; if (nparams == 0 && ncpus != 0) @@ -5130,7 +5082,6 @@ libxlDomainGetPerCPUStats(libxlDriverPrivatePtr driver, cleanup: if (vcpuinfo) libxl_vcpuinfo_list_free(vcpuinfo, maxcpu); - virObjectUnref(cfg); return ret; } @@ -5182,7 +5133,7 @@ libxlDomainMemoryStats(virDomainPtr dom, unsigned int flags) { libxlDriverPrivatePtr driver = dom->conn->privateData; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; virDomainObjPtr vm; libxl_dominfo d_info; unsigned mem, maxmem; @@ -5226,7 +5177,6 @@ libxlDomainMemoryStats(virDomainPtr dom, cleanup: libxl_dominfo_dispose(&d_info); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -6411,7 +6361,7 @@ libxlConnectGetDomainCapabilities(virConnectPtr conn, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; char *ret = NULL; int virttype = VIR_DOMAIN_VIRT_XEN; virDomainCapsPtr domCaps = NULL; @@ -6472,7 +6422,6 @@ libxlConnectGetDomainCapabilities(virConnectPtr conn, cleanup: virObjectUnref(domCaps); - virObjectUnref(cfg); return ret; } @@ -6483,7 +6432,7 @@ libxlConnectCompareCPU(virConnectPtr conn, unsigned int flags) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg; + g_autoptr(libxlDriverConfig) cfg = NULL; int ret = VIR_CPU_COMPARE_ERROR; bool failIncompatible; @@ -6500,7 +6449,6 @@ libxlConnectCompareCPU(virConnectPtr conn, ret = virCPUCompareXML(cfg->caps->host.arch, cfg->caps->host.cpu, xmlDesc, failIncompatible); - virObjectUnref(cfg); return ret; } diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c index 11d3070935..50225855ae 100644 --- a/src/libxl/libxl_migration.c +++ b/src/libxl/libxl_migration.c @@ -348,7 +348,7 @@ libxlDoMigrateSrcSend(libxlDriverPrivatePtr driver, unsigned long flags, int sockfd) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); int xl_flags = 0; int ret; @@ -363,7 +363,6 @@ libxlDoMigrateSrcSend(libxlDriverPrivatePtr driver, ret = -1; } - virObjectUnref(cfg); return ret; } @@ -388,7 +387,7 @@ libxlDomainMigrationSrcBegin(virConnectPtr conn, int *cookieoutlen) { libxlDriverPrivatePtr driver = conn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxlMigrationCookiePtr mig = NULL; virDomainDefPtr tmpdef = NULL; virDomainDefPtr def; @@ -438,7 +437,6 @@ libxlDomainMigrationSrcBegin(virConnectPtr conn, cleanup: libxlMigrationCookieFree(mig); virDomainDefFree(tmpdef); - virObjectUnref(cfg); return xml; } @@ -447,7 +445,7 @@ libxlDomainMigrationDstPrepareDef(libxlDriverPrivatePtr driver, const char *dom_xml, const char *dname) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virDomainDefPtr def; char *name = NULL; @@ -469,7 +467,6 @@ libxlDomainMigrationDstPrepareDef(libxlDriverPrivatePtr driver, } cleanup: - virObjectUnref(cfg); VIR_FREE(name); return def; } @@ -657,7 +654,7 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, unsigned int flags) { libxlDriverPrivatePtr driver = dconn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxlMigrationCookiePtr mig = NULL; virDomainObjPtr vm = NULL; char *hostname = NULL; @@ -828,7 +825,6 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, virURIFree(uri); virObjectUnref(args); virDomainObjEndAPI(&vm); - virObjectUnref(cfg); return ret; } @@ -1156,7 +1152,7 @@ libxlDomainMigrationSrcPerformP2P(libxlDriverPrivatePtr driver, bool useParams; virConnectPtr dconn = NULL; virErrorPtr orig_err = NULL; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); virObjectUnlock(vm); dconn = virConnectOpenAuth(dconnuri, &virConnectAuthConfig, 0); @@ -1199,7 +1195,6 @@ libxlDomainMigrationSrcPerformP2P(libxlDriverPrivatePtr driver, virErrorPreserveLast(&orig_err); virObjectUnlock(vm); virObjectUnref(dconn); - virObjectUnref(cfg); virObjectLock(vm); virErrorRestore(&orig_err); return ret; @@ -1276,7 +1271,7 @@ libxlDomainMigrationDstFinish(virConnectPtr dconn, int cancelled) { libxlDriverPrivatePtr driver = dconn->privateData; - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxlDomainObjPrivatePtr priv = vm->privateData; virObjectEventPtr event = NULL; virDomainPtr dom = NULL; @@ -1366,7 +1361,6 @@ libxlDomainMigrationDstFinish(virConnectPtr dconn, /* EndJob for corresponding BeginJob in prepare phase */ libxlDomainObjEndJob(driver, vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return dom; } @@ -1376,7 +1370,7 @@ libxlDomainMigrationSrcConfirm(libxlDriverPrivatePtr driver, unsigned int flags, int cancelled) { - libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver); + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxlDomainObjPrivatePtr priv = vm->privateData; virObjectEventPtr event = NULL; int ret = -1; @@ -1422,6 +1416,5 @@ libxlDomainMigrationSrcConfirm(libxlDriverPrivatePtr driver, /* EndJob for corresponding BeginJob in begin phase */ libxlDomainObjEndJob(driver, vm); virObjectEventStateQueue(driver->domainEventState, event); - virObjectUnref(cfg); return ret; } diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index d680fe36e3..62f01909f7 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -111,7 +111,7 @@ libxlDriverPrivatePtr testXLInitDriver(void) void testXLFreeDriver(libxlDriverPrivatePtr driver) { - virObjectUnref(driver->config); + g_clear_object(&driver->config); g_clear_object(&driver->xmlopt); virMutexDestroy(&driver->lock); g_free(driver); -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/hypervisor/virhostdev.c | 49 +++++++++++++++++++++++++++++-------- src/hypervisor/virhostdev.h | 13 +++++++--- src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_driver.c | 2 +- src/qemu/qemu_driver.c | 2 +- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/hypervisor/virhostdev.c b/src/hypervisor/virhostdev.c index 9017cc3be8..b484603f5d 100644 --- a/src/hypervisor/virhostdev.c +++ b/src/hypervisor/virhostdev.c @@ -44,8 +44,23 @@ VIR_LOG_INIT("util.hostdev"); static virHostdevManagerPtr manager; /* global hostdev manager, never freed */ -static virClassPtr virHostdevManagerClass; -static void virHostdevManagerDispose(void *obj); +G_DEFINE_TYPE(virHostdevManager, vir_hostdev_manager, G_TYPE_OBJECT); + +static void virHostdevManagerDispose(GObject *obj); +static void virHostdevManagerFinalize(GObject *obj); + +static void vir_hostdev_manager_init(virHostdevManager *mgr G_GNUC_UNUSED) +{ +} + +static void vir_hostdev_manager_class_init(virHostdevManagerClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virHostdevManagerDispose; + obj->finalize = virHostdevManagerFinalize; +} + static virHostdevManagerPtr virHostdevManagerNew(void); struct virHostdevIsPCINodeDeviceUsedData { @@ -112,9 +127,6 @@ static int virHostdevIsPCINodeDeviceUsed(virPCIDeviceAddressPtr devAddr, void *o static int virHostdevManagerOnceInit(void) { - if (!VIR_CLASS_NEW(virHostdevManager, virClassForObject())) - return -1; - if (!(manager = virHostdevManagerNew())) return -1; @@ -124,18 +136,36 @@ static int virHostdevManagerOnceInit(void) VIR_ONCE_GLOBAL_INIT(virHostdevManager); static void -virHostdevManagerDispose(void *obj) +virHostdevManagerDispose(GObject *obj) { - virHostdevManagerPtr hostdevMgr = obj; + virHostdevManagerPtr hostdevMgr = VIR_HOSTDEV_MANAGER(obj); virObjectUnref(hostdevMgr->activePCIHostdevs); + hostdevMgr->activePCIHostdevs = NULL; virObjectUnref(hostdevMgr->inactivePCIHostdevs); + hostdevMgr->inactivePCIHostdevs = NULL; virObjectUnref(hostdevMgr->activeUSBHostdevs); + hostdevMgr->activeUSBHostdevs = NULL; virObjectUnref(hostdevMgr->activeSCSIHostdevs); + hostdevMgr->activeSCSIHostdevs = NULL; virObjectUnref(hostdevMgr->activeSCSIVHostHostdevs); + hostdevMgr->activeSCSIVHostHostdevs = NULL; virObjectUnref(hostdevMgr->activeMediatedHostdevs); + hostdevMgr->activeMediatedHostdevs = NULL; virObjectUnref(hostdevMgr->activeNVMeHostdevs); + hostdevMgr->activeNVMeHostdevs = NULL; + + G_OBJECT_CLASS(vir_hostdev_manager_parent_class)->dispose(obj); +} + +static void +virHostdevManagerFinalize(GObject *obj) +{ + virHostdevManagerPtr hostdevMgr = VIR_HOSTDEV_MANAGER(obj); + VIR_FREE(hostdevMgr->stateDir); + + G_OBJECT_CLASS(vir_hostdev_manager_parent_class)->finalize(obj); } static virHostdevManagerPtr @@ -144,8 +174,7 @@ virHostdevManagerNew(void) g_autoptr(virHostdevManager) hostdevMgr = NULL; bool privileged = geteuid() == 0; - if (!(hostdevMgr = virObjectNew(virHostdevManagerClass))) - return NULL; + hostdevMgr = VIR_HOSTDEV_MANAGER(g_object_new(VIR_TYPE_HOSTDEV_MANAGER, NULL)); if (!(hostdevMgr->activePCIHostdevs = virPCIDeviceListNew())) return NULL; @@ -206,7 +235,7 @@ virHostdevManagerGetDefault(void) if (virHostdevManagerInitialize() < 0) return NULL; - return virObjectRef(manager); + return g_object_ref(manager); } /** diff --git a/src/hypervisor/virhostdev.h b/src/hypervisor/virhostdev.h index 811bda40ed..be5345992c 100644 --- a/src/hypervisor/virhostdev.h +++ b/src/hypervisor/virhostdev.h @@ -30,6 +30,7 @@ #include "conf/domain_conf.h" #include "virmdev.h" #include "virnvme.h" +#include <glib-object.h> typedef enum { VIR_HOSTDEV_STRICT_ACS_CHECK = (1 << 0), /* strict acs check */ @@ -41,10 +42,8 @@ typedef enum { } virHostdevFlag; -typedef struct _virHostdevManager virHostdevManager; -typedef virHostdevManager *virHostdevManagerPtr; struct _virHostdevManager { - virObject parent; + GObject parent; char *stateDir; @@ -59,7 +58,13 @@ struct _virHostdevManager { virNVMeDeviceListPtr activeNVMeHostdevs; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virHostdevManager, virObjectUnref); +#define VIR_TYPE_HOSTDEV_MANAGER vir_hostdev_manager_get_type() +G_DECLARE_FINAL_TYPE(virHostdevManager, + vir_hostdev_manager, + VIR, + HOSTDEV_MANAGER, + GObject); +typedef virHostdevManager *virHostdevManagerPtr; virHostdevManagerPtr virHostdevManagerGetDefault(void); diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index affae0e86e..dbcbbdc96d 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -505,7 +505,7 @@ libxlStateCleanup(void) if (!libxl_driver) return -1; - virObjectUnref(libxl_driver->hostdevMgr); + g_clear_object(&libxl_driver->hostdevMgr); g_clear_object(&libxl_driver->xmlopt); virObjectUnref(libxl_driver->domains); virPortAllocatorRangeFree(libxl_driver->reservedGraphicsPorts); diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 288d394989..cbd7fbfcb0 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1603,7 +1603,7 @@ static int lxcStateCleanup(void) virSysinfoDefFree(lxc_driver->hostsysinfo); - virObjectUnref(lxc_driver->hostdevMgr); + g_clear_object(&lxc_driver->hostdevMgr); g_clear_object(&lxc_driver->caps); virObjectUnref(lxc_driver->securityManager); g_clear_object(&lxc_driver->xmlopt); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index c05803f08a..346edb47d6 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1113,7 +1113,7 @@ qemuStateCleanup(void) virPortAllocatorRangeFree(qemu_driver->webSocketPorts); virPortAllocatorRangeFree(qemu_driver->remotePorts); virHashFree(qemu_driver->sharedDevices); - virObjectUnref(qemu_driver->hostdevMgr); + g_clear_object(&qemu_driver->hostdevMgr); virObjectUnref(qemu_driver->securityManager); virObjectUnref(qemu_driver->domainEventState); virObjectUnref(qemu_driver->qemuCapsCache); -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libxl/libxl_migration.c | 76 ++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c index 50225855ae..8530b172ec 100644 --- a/src/libxl/libxl_migration.c +++ b/src/libxl/libxl_migration.c @@ -60,7 +60,7 @@ struct _libxlMigrationCookie { }; typedef struct _libxlMigrationDstArgs { - virObject parent; + GObject parent; int recvfd; virConnectPtr conn; @@ -73,7 +73,30 @@ typedef struct _libxlMigrationDstArgs { size_t nsocks; } libxlMigrationDstArgs; -static virClassPtr libxlMigrationDstArgsClass; +G_DEFINE_TYPE(libxlMigrationDstArgs, libxl_migration_dst_args, G_TYPE_OBJECT); +#define LIBXL_TYPE_MIGRATION_DST_ARGS libxl_migration_dst_args_get_type() +G_DECLARE_FINAL_TYPE(libxlMigrationDstArgs, + libxl_migration_dst_args, + LIBXL, + MIGRATION_DST_ARGS, + GObject); + +static void libxlMigrationDstArgsDispose(GObject *obj); +static void libxlMigrationDstArgsFinalize(GObject *obj); + +static void +libxl_migration_dst_args_init(lixlMigrationDstArgs *args G_GNUC_UNUSED) +{ +} + +static void +libxl_migration_dst_args_class_init(lixlMigrationDstArgsClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = libxlMigrationDstArgsDispose; + obj->finalize = libxlMigrationDstArgsFinalize; +} static void @@ -226,31 +249,34 @@ libxlMigrationEatCookie(const char *cookiein, } static void -libxlMigrationDstArgsDispose(void *obj) +libxlMigrationDstArgsDispose(GObject *obj) { - libxlMigrationDstArgs *args = obj; + libxlMigrationDstArgs *args = LIBXL_MIGRATION_DST_ARGS(obj); - libxlMigrationCookieFree(args->migcookie); - VIR_FREE(args->socks); virObjectUnref(args->conn); + args->conn = NULL; virObjectUnref(args->vm); + args->vm = NULL; + + G_OBJECT_CLASS(libxl_migration_dst_args_parent_class)->dispose(obj); } -static int -libxlMigrationDstArgsOnceInit(void) +static void +libxlMigrationDstArgsFinalize(GObject *obj) { - if (!VIR_CLASS_NEW(libxlMigrationDstArgs, virClassForObject())) - return -1; + libxlMigrationDstArgs *args = LIBXL_MIGRATION_DST_ARGS(obj); - return 0; + libxlMigrationCookieFree(args->migcookie); + VIR_FREE(args->socks); + + G_OBJECT_CLASS(libxl_migration_dst_args_parent_class)->finalize(obj); } -VIR_ONCE_GLOBAL_INIT(libxlMigrationDstArgs); static void libxlDoMigrateDstReceive(void *opaque) { - libxlMigrationDstArgs *args = opaque; + g_autoptr(libxlMigrationDstArgs) args = LIBXL_MIGRATION_DST_ARGS(opaque); virDomainObjPtr vm = args->vm; virNetSocketPtr *socks = args->socks; size_t nsocks = args->nsocks; @@ -277,7 +303,6 @@ libxlDoMigrateDstReceive(void *opaque) } args->nsocks = 0; VIR_FORCE_CLOSE(recvfd); - virObjectUnref(args); virDomainObjEndAPI(&vm); } @@ -287,7 +312,7 @@ libxlMigrateDstReceive(virNetSocketPtr sock, int events G_GNUC_UNUSED, void *opaque) { - libxlMigrationDstArgs *args = opaque; + g_autoptr(libxlMigrationDstArgs) args = LIBXL_MIGRATION_DST_ARGS(opaque); virNetSocketPtr *socks = args->socks; size_t nsocks = args->nsocks; libxlDomainObjPrivatePtr priv = args->vm->privateData; @@ -321,7 +346,7 @@ libxlMigrateDstReceive(virNetSocketPtr sock, libxlDoMigrateDstReceive, name, false, - args) < 0) { + g_object_ref(args)) < 0) { virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("Failed to create thread for receiving migration data")); goto fail; @@ -339,7 +364,6 @@ libxlMigrateDstReceive(virNetSocketPtr sock, } args->nsocks = 0; VIR_FORCE_CLOSE(recvfd); - virObjectUnref(args); } static int @@ -551,7 +575,7 @@ libxlDomainMigrationDstPrepareTunnel3(virConnectPtr dconn, libxlMigrationCookiePtr mig = NULL; libxlDriverPrivatePtr driver = dconn->privateData; virDomainObjPtr vm = NULL; - libxlMigrationDstArgs *args = NULL; + g_autoptr(libxlMigrationDstArgs) args = NULL; bool taint_hook = false; libxlDomainObjPrivatePtr priv = NULL; char *xmlout = NULL; @@ -600,8 +624,8 @@ libxlDomainMigrationDstPrepareTunnel3(virConnectPtr dconn, if (libxlMigrationDstArgsInitialize() < 0) goto endjob; - if (!(args = virObjectNew(libxlMigrationDstArgsClass))) - goto endjob; + args = LIBXL_MIGRATION_DST_ARGS( + g_object_new(LIBXL_TYPE_MIGRATION_DST_ARGS, NULL)); args->conn = virObjectRef(dconn); args->vm = virObjectRef(vm); @@ -618,7 +642,7 @@ libxlDomainMigrationDstPrepareTunnel3(virConnectPtr dconn, name = g_strdup_printf("mig-%s", args->vm->def->name); if (virThreadCreateFull(priv->migrationDstReceiveThr, true, libxlDoMigrateDstReceive, - name, false, args) < 0) { + name, false, g_object_ref(args)) < 0) { virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("Failed to create thread for receiving migration data")); goto endjob; @@ -634,7 +658,6 @@ libxlDomainMigrationDstPrepareTunnel3(virConnectPtr dconn, libxlMigrationCookieFree(mig); VIR_FORCE_CLOSE(dataFD[1]); VIR_FORCE_CLOSE(dataFD[0]); - virObjectUnref(args); /* Remove virDomainObj from domain list */ if (vm) virDomainObjListRemove(driver->domains, vm); @@ -665,7 +688,7 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, virNetSocketPtr *socks = NULL; size_t nsocks = 0; int nsocks_listen = 0; - libxlMigrationDstArgs *args = NULL; + g_autoptr(libxlMigrationDstArgs) args = NULL; bool taint_hook = false; libxlDomainObjPrivatePtr priv = NULL; size_t i; @@ -765,8 +788,8 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, if (libxlMigrationDstArgsInitialize() < 0) goto endjob; - if (!(args = virObjectNew(libxlMigrationDstArgsClass))) - goto endjob; + args = LIBXL_MIGRATION_DST_ARGS( + g_object_new(LIBXL_TYPE_MIGRATION_DST_ARGS, NULL)); args->conn = virObjectRef(dconn); args->vm = virObjectRef(vm); @@ -786,7 +809,7 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, if (virNetSocketAddIOCallback(socks[i], VIR_EVENT_HANDLE_READABLE, libxlMigrateDstReceive, - virObjectRef(args), + g_object_ref(args), NULL) < 0) continue; @@ -823,7 +846,6 @@ libxlDomainMigrationDstPrepare(virConnectPtr dconn, VIR_FREE(hostname); else virURIFree(uri); - virObjectUnref(args); virDomainObjEndAPI(&vm); return ret; } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/qemu/qemu_blockjob.c | 67 ++++++++++++++++++++++----------------- src/qemu/qemu_blockjob.h | 15 ++++++--- src/qemu/qemu_domain.c | 4 +-- src/qemu/qemu_hotplug.c | 3 +- src/qemu/qemu_migration.c | 16 +++------- 5 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c index 2032c0c1c5..38fbba2b90 100644 --- a/src/qemu/qemu_blockjob.c +++ b/src/qemu/qemu_blockjob.c @@ -71,8 +71,23 @@ VIR_ENUM_IMPL(qemuBlockjob, "create", "broken"); -static virClassPtr qemuBlockJobDataClass; +G_DEFINE_TYPE(qemuBlockJobData, qemu_block_job_data, G_TYPE_OBJECT); +static void qemuBlockJobDataDispose(GObject *obj); +static void qemuBlockJobDataFinalize(GObject *obj); +static void +qemu_block_job_data_init(qemuBlockJobData *job G_GNUC_UNUSED) +{ +} + +static void +qemu_block_job_data_class_init(qemuBlockJobDataClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = qemuBlockJobDataDispose; + obj->finalize = qemuBlockJobDataFinalize; +} static void qemuBlockJobDataDisposeJobdata(qemuBlockJobDataPtr job) @@ -88,43 +103,38 @@ qemuBlockJobDataDisposeJobdata(qemuBlockJobDataPtr job) static void -qemuBlockJobDataDispose(void *obj) +qemuBlockJobDataDispose(GObject *obj) { - qemuBlockJobDataPtr job = obj; + qemuBlockJobDataPtr job = QEMU_BLOCK_JOB_DATA(obj); virObjectUnref(job->chain); + job->chain = NULL; virObjectUnref(job->mirrorChain); + job->mirrorChain = NULL; + + G_OBJECT_CLASS(qemu_block_job_data_parent_class)->dispose(obj); +} + +static void +qemuBlockJobDataFinalize(GObject *obj) +{ + qemuBlockJobDataPtr job = QEMU_BLOCK_JOB_DATA(obj); qemuBlockJobDataDisposeJobdata(job); g_free(job->name); g_free(job->errmsg); -} - -static int -qemuBlockJobDataOnceInit(void) -{ - if (!VIR_CLASS_NEW(qemuBlockJobData, virClassForObject())) - return -1; - - return 0; + G_OBJECT_CLASS(qemu_block_job_data_parent_class)->finalize(obj); } -VIR_ONCE_GLOBAL_INIT(qemuBlockJobData); - qemuBlockJobDataPtr qemuBlockJobDataNew(qemuBlockJobType type, const char *name) { - g_autoptr(qemuBlockJobData) job = NULL; - - if (qemuBlockJobDataInitialize() < 0) - return NULL; - - if (!(job = virObjectNew(qemuBlockJobDataClass))) - return NULL; + g_autoptr(qemuBlockJobData) job = + QEMU_BLOCK_JOB_DATA(g_object_new(QEMU_TYPE_BLOCK_JOB_DATA, NULL)); job->name = g_strdup(name); @@ -180,15 +190,15 @@ qemuBlockJobRegister(qemuBlockJobDataPtr job, return -1; } - if (virHashAddEntry(priv->blockjobs, job->name, virObjectRef(job)) < 0) { - virObjectUnref(job); + if (virHashAddEntry(priv->blockjobs, job->name, g_object_ref(job)) < 0) { + g_object_unref(job); return -1; } if (disk) { job->disk = disk; job->chain = virObjectRef(disk->src); - QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob = virObjectRef(job); + QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob = g_object_ref(job); } if (savestatus) @@ -209,8 +219,7 @@ qemuBlockJobUnregister(qemuBlockJobDataPtr job, diskPriv = QEMU_DOMAIN_DISK_PRIVATE(job->disk); if (job == diskPriv->blockjob) { - virObjectUnref(diskPriv->blockjob); - diskPriv->blockjob = NULL; + g_clear_object(&diskPriv->blockjob); } job->disk = NULL; @@ -427,7 +436,7 @@ qemuBlockJobDiskGetJob(virDomainDiskDefPtr disk) if (!job) return NULL; - return virObjectRef(job); + return g_object_ref(job); } @@ -466,7 +475,7 @@ qemuBlockJobStartupFinalize(virDomainObjPtr vm, if (job->state == QEMU_BLOCKJOB_STATE_NEW) qemuBlockJobUnregister(job, vm); - virObjectUnref(job); + g_object_unref(job); } @@ -1738,7 +1747,7 @@ qemuBlockJobGetByDisk(virDomainDiskDefPtr disk) if (!job) return NULL; - return virObjectRef(job); + return g_object_ref(job); } diff --git a/src/qemu/qemu_blockjob.h b/src/qemu/qemu_blockjob.h index 19498b5bd8..fc36131c67 100644 --- a/src/qemu/qemu_blockjob.h +++ b/src/qemu/qemu_blockjob.h @@ -23,6 +23,7 @@ #include "internal.h" #include "qemu_conf.h" +#include <glib-object.h> /** * This enum has to map all known block job states from enum virDomainBlockJobType @@ -119,11 +120,8 @@ struct _qemuBlockJobBackupData { }; -typedef struct _qemuBlockJobData qemuBlockJobData; -typedef qemuBlockJobData *qemuBlockJobDataPtr; - struct _qemuBlockJobData { - virObject parent; + GObject parent; char *name; @@ -154,7 +152,14 @@ struct _qemuBlockJobData { bool invalidData; /* the job data (except name) is not valid */ bool reconnected; /* internal field for tracking whether job is live after reconnect to qemu */ }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuBlockJobData, virObjectUnref); + +#define QEMU_TYPE_BLOCK_JOB_DATA qemu_block_job_data_get_type() +G_DECLARE_FINAL_TYPE(qemuBlockJobData, + qemu_block_job_data, + QEMU, + BLOCK_JOB_DATA, + GObject); +typedef qemuBlockJobData *qemuBlockJobDataPtr; int qemuBlockJobRegister(qemuBlockJobDataPtr job, diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index c6980a53b6..99478890b6 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1166,7 +1166,7 @@ qemuDomainDiskPrivateDispose(void *obj) virObjectUnref(priv->migrSource); VIR_FREE(priv->qomName); VIR_FREE(priv->nodeCopyOnRead); - virObjectUnref(priv->blockjob); + g_clear_object(&priv->blockjob); } static virClassPtr qemuDomainStorageSourcePrivateClass; @@ -2215,7 +2215,7 @@ qemuDomainObjPrivateAlloc(void *opaque) if (!(priv->devs = virChrdevAlloc())) goto error; - if (!(priv->blockjobs = virHashCreate(5, virObjectFreeHashData))) + if (!(priv->blockjobs = virHashCreate(5, g_object_unref))) goto error; /* agent commands block by default, user can choose different behavior */ diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 14654a17d7..98b02710d5 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -4276,8 +4276,7 @@ qemuDomainRemoveDiskDevice(virQEMUDriverPtr driver, if (diskPriv->blockjob) { /* the block job keeps reference to the disk chain */ diskPriv->blockjob->disk = NULL; - virObjectUnref(diskPriv->blockjob); - diskPriv->blockjob = NULL; + g_clear_object(&diskPriv->blockjob); } else { if (!(diskBackend = qemuBlockStorageSourceChainDetachPrepareBlockdev(disk->src))) goto cleanup; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index ab4ae8e150..6b668029b1 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -512,7 +512,7 @@ qemuMigrationSrcNBDStorageCopyReady(virDomainObjPtr vm, for (i = 0; i < vm->def->ndisks; i++) { virDomainDiskDefPtr disk = vm->def->disks[i]; qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk); - qemuBlockJobDataPtr job; + g_autoptr(qemuBlockJobData) job = NULL; if (!diskPriv->migrating) continue; @@ -526,12 +526,9 @@ qemuMigrationSrcNBDStorageCopyReady(virDomainObjPtr vm, qemuBlockJobUpdate(vm, job, asyncJob); if (job->state == VIR_DOMAIN_BLOCK_JOB_FAILED) { qemuMigrationNBDReportMirrorError(job, disk->dst); - virObjectUnref(job); return -1; } - virObjectUnref(job); - if (disk->mirrorState != VIR_DOMAIN_DISK_MIRROR_STATE_READY) notReady++; } @@ -570,7 +567,7 @@ qemuMigrationSrcNBDCopyCancelled(virDomainObjPtr vm, for (i = 0; i < vm->def->ndisks; i++) { virDomainDiskDefPtr disk = vm->def->disks[i]; qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk); - qemuBlockJobDataPtr job; + g_autoptr(qemuBlockJobData) job = NULL; if (!diskPriv->migrating) continue; @@ -597,8 +594,6 @@ qemuMigrationSrcNBDCopyCancelled(virDomainObjPtr vm, if (job->state == VIR_DOMAIN_BLOCK_JOB_COMPLETED) completed++; - - virObjectUnref(job); } /* Updating completed block job drops the lock thus we have to recheck @@ -704,14 +699,13 @@ qemuMigrationSrcNBDCopyCancel(virQEMUDriverPtr driver, for (i = 0; i < vm->def->ndisks; i++) { virDomainDiskDefPtr disk = vm->def->disks[i]; qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk); - qemuBlockJobDataPtr job; + g_autoptr(qemuBlockJobData) job = NULL; if (!(job = qemuBlockJobDiskGetJob(disk)) || !qemuBlockJobIsRunning(job)) diskPriv->migrating = false; if (!diskPriv->migrating) { - virObjectUnref(job); continue; } @@ -726,8 +720,6 @@ qemuMigrationSrcNBDCopyCancel(virQEMUDriverPtr driver, qemuBlockJobSyncEnd(vm, job, asyncJob); diskPriv->migrating = false; } - - virObjectUnref(job); } while ((rv = qemuMigrationSrcNBDCopyCancelled(vm, asyncJob, check)) != 1) { @@ -5437,7 +5429,7 @@ qemuMigrationSrcCancel(virQEMUDriverPtr driver, storage = true; } - virObjectUnref(job); + g_clear_object(&job); } if (storage && -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/qemu/qemu_conf.c | 36 ++++++++++++++++++------------------ src/qemu/qemu_conf.h | 19 ++++++++++++------- src/qemu/qemu_driver.c | 2 +- tests/domaincapstest.c | 2 +- tests/testutilsqemu.c | 2 +- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index c29b543b16..1e7d1a8e9d 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -69,18 +69,21 @@ VIR_LOG_INIT("qemu.qemu_conf"); #define QEMU_MIGRATION_PORT_MIN 49152 #define QEMU_MIGRATION_PORT_MAX 49215 -static virClassPtr virQEMUDriverConfigClass; -static void virQEMUDriverConfigDispose(void *obj); +G_DEFINE_TYPE(virQEMUDriverConfig, vir_qemu_driver_config, G_TYPE_OBJECT); +static void virQEMUDriverConfigFinalize(GObject *obj); -static int virQEMUConfigOnceInit(void) +static void +vir_qemu_driver_config_init(virQEMUDriverConfig *cfg G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(virQEMUDriverConfig, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(virQEMUConfig); +static void +vir_qemu_driver_config_class_init(virQEMUDriverConfigClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virQEMUDriverConfigFinalize; +} static void @@ -106,13 +109,8 @@ qemuDriverUnlock(virQEMUDriverPtr driver) virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged, const char *root) { - g_autoptr(virQEMUDriverConfig) cfg = NULL; - - if (virQEMUConfigInitialize() < 0) - return NULL; - - if (!(cfg = virObjectNew(virQEMUDriverConfigClass))) - return NULL; + g_autoptr(virQEMUDriverConfig) cfg = + VIR_QEMU_DRIVER_CONFIG(g_object_new(VIR_TYPE_QEMU_DRIVER_CONFIG, NULL)); if (root) { cfg->uri = g_strdup_printf("qemu:///embed?root=%s", root); @@ -294,9 +292,9 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged, } -static void virQEMUDriverConfigDispose(void *obj) +static void virQEMUDriverConfigFinalize(GObject *obj) { - virQEMUDriverConfigPtr cfg = obj; + virQEMUDriverConfigPtr cfg = VIR_QEMU_DRIVER_CONFIG(obj); virBitmapFree(cfg->namespaces); @@ -369,6 +367,8 @@ static void virQEMUDriverConfigDispose(void *obj) VIR_FREE(cfg->swtpmStorageDir); virStringListFree(cfg->capabilityfilters); + + G_OBJECT_CLASS(vir_qemu_driver_config_parent_class)->finalize(obj); } @@ -1229,7 +1229,7 @@ virQEMUDriverConfigPtr virQEMUDriverGetConfig(virQEMUDriverPtr driver) { virQEMUDriverConfigPtr conf; qemuDriverLock(driver); - conf = virObjectRef(driver->config); + conf = g_object_ref(driver->config); qemuDriverUnlock(driver); return conf; } diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index b9ef4551a3..0a07188962 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -48,15 +48,13 @@ #include "virfile.h" #include "virfilecache.h" #include "virfirmware.h" +#include <glib-object.h> #define QEMU_DRIVER_NAME "QEMU" typedef struct _virQEMUDriver virQEMUDriver; typedef virQEMUDriver *virQEMUDriverPtr; -typedef struct _virQEMUDriverConfig virQEMUDriverConfig; -typedef virQEMUDriverConfig *virQEMUDriverConfigPtr; - /* Main driver config. The data in these object * instances is immutable, so can be accessed * without locking. Threads must, however, hold @@ -65,15 +63,15 @@ typedef virQEMUDriverConfig *virQEMUDriverConfigPtr; * * eg * qemuDriverLock(driver); - * virQEMUDriverConfigPtr cfg = virObjectRef(driver->config); + * virQEMUDriverConfigPtr cfg = g_object_ref(driver->config); * qemuDriverUnlock(driver); * * ...do stuff with 'cfg'.. * - * virObjectUnref(cfg); + * g_object_unref(cfg); */ struct _virQEMUDriverConfig { - virObject parent; + GObject parent; char *uri; @@ -220,7 +218,14 @@ struct _virQEMUDriverConfig { char **capabilityfilters; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUDriverConfig, virObjectUnref); +#define VIR_TYPE_QEMU_DRIVER_CONFIG vir_qemu_driver_config_get_type() +G_DECLARE_FINAL_TYPE(virQEMUDriverConfig, + vir_qemu_driver_config, + VIR, + QEMU_DRIVER_CONFIG, + GObject); +typedef virQEMUDriverConfig *virQEMUDriverConfigPtr; + /* Main driver state */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 346edb47d6..4d0f76fb21 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1129,7 +1129,7 @@ qemuStateCleanup(void) if (qemu_driver->lockFD != -1) virPidFileRelease(qemu_driver->config->stateDir, "driver", qemu_driver->lockFD); - virObjectUnref(qemu_driver->config); + g_clear_object(&qemu_driver->config); virMutexDestroy(&qemu_driver->lock); VIR_FREE(qemu_driver); diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index 2393e312d1..1bbd6a3e61 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -450,7 +450,7 @@ mymain(void) * file has been added, run "VIR_TEST_REGENERATE_OUTPUT=1 make check". */ - virObjectUnref(cfg); + g_clear_object(&cfg); virFileWrapperClearPrefixes(); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index eece670aa0..e149e36ffe 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -309,7 +309,7 @@ void qemuTestDriverFree(virQEMUDriver *driver) virObjectUnref(driver->qemuCapsCache); g_clear_object(&driver->xmlopt); g_clear_object(&driver->caps); - virObjectUnref(driver->config); + g_clear_object(&driver->config); virObjectUnref(driver->securityManager); } -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_conf.c | 21 ++- src/conf/domain_conf.h | 18 +- src/qemu/qemu_domain.c | 359 +++++++++++++++++--------------------- src/qemu/qemu_domain.h | 145 ++++++++++----- src/util/virstoragefile.c | 2 +- src/util/virstoragefile.h | 3 +- 6 files changed, 286 insertions(+), 262 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fa87fb0590..15a0c2d881 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1785,7 +1785,7 @@ void virDomainGraphicsDefFree(virDomainGraphicsDefPtr def) virDomainGraphicsListenDefClear(&def->listens[i]); VIR_FREE(def->listens); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); VIR_FREE(def); } @@ -1833,7 +1833,7 @@ static virDomainVcpuDefPtr virDomainVcpuDefNew(virDomainXMLOptionPtr xmlopt) { virDomainVcpuDefPtr ret = NULL; - g_autoptr(virObject) priv = NULL; + g_autoptr(GObject) priv = NULL; if (xmlopt && xmlopt->privateData.vcpuNew && !(priv = xmlopt->privateData.vcpuNew())) @@ -1856,7 +1856,7 @@ virDomainVcpuDefFree(virDomainVcpuDefPtr info) virBitmapFree(info->cpumask); info->cpumask = NULL; - virObjectUnref(info->privateData); + g_clear_object(&info->privateData); VIR_FREE(info); } @@ -2158,7 +2158,7 @@ virDomainDiskDefFree(virDomainDiskDefPtr def) VIR_FREE(def->blkdeviotune.group_name); VIR_FREE(def->virtio); virDomainDeviceInfoClear(&def->info); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); VIR_FREE(def); } @@ -2361,7 +2361,7 @@ void virDomainFSDefFree(virDomainFSDefPtr def) VIR_FREE(def->dst); virDomainDeviceInfoClear(&def->info); VIR_FREE(def->virtio); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); VIR_FREE(def->binary); VIR_FREE(def); @@ -2422,7 +2422,7 @@ virDomainVsockDefFree(virDomainVsockDefPtr vsock) if (!vsock) return; - virObjectUnref(vsock->privateData); + g_clear_object(&vsock->privateData); virDomainDeviceInfoClear(&vsock->info); VIR_FREE(vsock); } @@ -2510,7 +2510,7 @@ virDomainNetDefFree(virDomainNetDefPtr def) if (!def) return; virDomainNetDefClear(def); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); VIR_FREE(def); } @@ -2651,8 +2651,7 @@ virDomainChrSourceDefDispose(GObject *obj) { virDomainChrSourceDefPtr def = VIR_DOMAIN_CHR_SOURCE_DEF(obj); - virObjectUnref(def->privateData); - def->privateData = NULL; + g_clear_object(&def->privateData); G_OBJECT_CLASS(vir_domain_chr_source_def_parent_class)->dispose(obj); } @@ -2909,7 +2908,7 @@ virDomainVideoDefClear(virDomainVideoDefPtr def) if (def->driver) VIR_FREE(def->driver->vhost_user_binary); VIR_FREE(def->driver); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); memset(def, 0, sizeof(*def)); } @@ -27098,7 +27097,7 @@ virDomainRNGDefFree(virDomainRNGDefPtr def) VIR_FREE(def->source.file); break; case VIR_DOMAIN_RNG_BACKEND_EGD: - virObjectUnref(def->source.chardev); + g_clear_object(&def->source.chardev); break; case VIR_DOMAIN_RNG_BACKEND_BUILTIN: case VIR_DOMAIN_RNG_BACKEND_LAST: diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index c9a44ecfd8..ae9a064106 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -528,7 +528,7 @@ typedef enum { struct _virDomainDiskDef { virStorageSourcePtr src; /* non-NULL. XXX Allow NULL for empty cdrom? */ - virObjectPtr privateData; + GObject *privateData; int device; /* enum virDomainDiskDevice */ int bus; /* enum virDomainDiskBus */ @@ -852,7 +852,7 @@ struct _virDomainFSDef { virTristateSwitch posix_lock; virTristateSwitch flock; virDomainVirtioOptionsPtr virtio; - virObjectPtr privateData; + GObject *privateData; }; @@ -1071,7 +1071,7 @@ struct _virDomainNetDef { unsigned int mtu; virNetDevCoalescePtr coalesce; virDomainVirtioOptionsPtr virtio; - virObjectPtr privateData; + GObject *privateData; }; typedef enum { @@ -1190,7 +1190,7 @@ struct _virDomainChrSourceReconnectDef { struct _virDomainChrSourceDef { GObject parent; int type; /* virDomainChrType */ - virObjectPtr privateData; + GObject *privateData; union { /* no <source> for null, vc, stdio */ struct { @@ -1493,7 +1493,7 @@ struct _virDomainVideoDriverDef { }; struct _virDomainVideoDef { - virObjectPtr privateData; + GObject *privateData; int type; /* enum virDomainVideoType */ unsigned int ram; /* kibibytes (multiples of 1024) */ @@ -1640,7 +1640,7 @@ struct _virDomainGraphicsListenDef { }; struct _virDomainGraphicsDef { - virObjectPtr privateData; + GObject *privateData; /* Port value discipline: * Value -1 is legacy syntax indicating that it should be auto-allocated. @@ -2318,7 +2318,7 @@ struct _virDomainVcpuDef { virDomainThreadSchedParam sched; - virObjectPtr privateData; + GObject *privateData; }; struct _virDomainBlkiotune { @@ -2416,7 +2416,7 @@ typedef enum { } virDomainVsockModel; struct _virDomainVsockDef { - virObjectPtr privateData; + GObject *privateData; virDomainVsockModel model; unsigned int guest_cid; @@ -2774,7 +2774,7 @@ struct _virDomainDefParserConfig { typedef void *(*virDomainXMLPrivateDataAllocFunc)(void *); typedef void (*virDomainXMLPrivateDataFreeFunc)(void *); -typedef virObjectPtr (*virDomainXMLPrivateDataNewFunc)(void); +typedef GObject* (*virDomainXMLPrivateDataNewFunc)(void); typedef int (*virDomainXMLPrivateDataFormatFunc)(virBufferPtr, virDomainObjPtr); typedef int (*virDomainXMLPrivateDataParseFunc)(xmlXPathContextPtr, diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 99478890b6..7f6ac10659 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1130,362 +1130,329 @@ qemuDomainSecretInfoDestroy(qemuDomainSecretInfoPtr secinfo) } -static virClassPtr qemuDomainDiskPrivateClass; -static void qemuDomainDiskPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainDiskPrivate, qemu_domain_disk_private, G_TYPE_OBJECT); +static void qemuDomainDiskPrivateDispose(GObject *obj); +static void qemuDomainDiskPrivateFinalize(GObject *obj); -static int -qemuDomainDiskPrivateOnceInit(void) +static void +qemu_domain_disk_private_init(qemuDomainDiskPrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainDiskPrivate, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(qemuDomainDiskPrivate); +static void +qemu_domain_disk_private_class_init(qemuDomainDiskPrivateClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = qemuDomainDiskPrivateDispose; + obj->finalize = qemuDomainDiskPrivateFinalize; +} -static virObjectPtr +static GObject * qemuDomainDiskPrivateNew(void) { - qemuDomainDiskPrivatePtr priv; + return g_object_new(QEMU_TYPE_DOMAIN_DISK_PRIVATE, NULL); +} - if (qemuDomainDiskPrivateInitialize() < 0) - return NULL; +static void +qemuDomainDiskPrivateDispose(GObject *obj) +{ + qemuDomainDiskPrivatePtr priv = QEMU_DOMAIN_DISK(obj); - if (!(priv = virObjectNew(qemuDomainDiskPrivateClass))) - return NULL; + virObjectUnref(priv->migrSource); + priv->migrSource = NULL; + g_clear_object(&priv->blockjob); - return (virObjectPtr) priv; + G_OBJECT_CLASS(qemu_domain_disk_private_parent_class)->dispose(obj); } static void -qemuDomainDiskPrivateDispose(void *obj) +qemuDomainDiskPrivateFinalize(GObject *obj) { - qemuDomainDiskPrivatePtr priv = obj; + qemuDomainDiskPrivatePtr priv = QEMU_DOMAIN_DISK(obj); - virObjectUnref(priv->migrSource); VIR_FREE(priv->qomName); VIR_FREE(priv->nodeCopyOnRead); - g_clear_object(&priv->blockjob); + + G_OBJECT_CLASS(qemu_domain_disk_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainStorageSourcePrivateClass; -static void qemuDomainStorageSourcePrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainStorageSourcePrivate, qemu_domain_storage_source_private, G_TYPE_OBJECT); +static void qemuDomainStorageSourcePrivateFinalize(GObject *obj); -static int -qemuDomainStorageSourcePrivateOnceInit(void) +static void +qemu_domain_storage_source_private_init(qemuDomainStorageSourcePrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainStorageSourcePrivate, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(qemuDomainStorageSourcePrivate); - -virObjectPtr -qemuDomainStorageSourcePrivateNew(void) +static void +qemu_domain_storage_source_private_class_init(qemuDomainStorageSourcePrivateClass *klass) { - qemuDomainStorageSourcePrivatePtr priv; - - if (qemuDomainStorageSourcePrivateInitialize() < 0) - return NULL; + GObjectClass *obj = G_OBJECT_CLASS(klass); - if (!(priv = virObjectNew(qemuDomainStorageSourcePrivateClass))) - return NULL; + obj->finalize = qemuDomainStorageSourcePrivateFinalize; +} - return (virObjectPtr) priv; +GObject * +qemuDomainStorageSourcePrivateNew(void) +{ + return g_object_new(QEMU_TYPE_DOMAIN_STORAGE_SOURCE_PRIVATE, NULL); } static void -qemuDomainStorageSourcePrivateDispose(void *obj) +qemuDomainStorageSourcePrivateFinalize(GObject *obj) { - qemuDomainStorageSourcePrivatePtr priv = obj; + qemuDomainStorageSourcePrivatePtr priv = QEMU_DOMAIN_STORAGE_SOURCE(obj); g_clear_pointer(&priv->secinfo, qemuDomainSecretInfoFree); g_clear_pointer(&priv->encinfo, qemuDomainSecretInfoFree); + + G_OBJECT_CLASS(qemu_domain_storage_source_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainVcpuPrivateClass; -static void qemuDomainVcpuPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainVcpuPrivate, qemu_domain_vcpu_private, G_TYPE_OBJECT); +static void qemuDomainVcpuPrivateFinalize(GObject *obj); -static int -qemuDomainVcpuPrivateOnceInit(void) +static void +qemu_domain_vcpu_private_init(qemuDomainVcpuPrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainVcpuPrivate, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(qemuDomainVcpuPrivate); - -static virObjectPtr -qemuDomainVcpuPrivateNew(void) +static void +qemu_domain_vcpu_private_class_init(qemuDomainVcpuPrivateClass *klass) { - qemuDomainVcpuPrivatePtr priv; - - if (qemuDomainVcpuPrivateInitialize() < 0) - return NULL; + GObjectClass *obj = G_OBJECT_CLASS(klass); - if (!(priv = virObjectNew(qemuDomainVcpuPrivateClass))) - return NULL; + obj->finalize = qemuDomainVcpuPrivateFinalize; +} - return (virObjectPtr) priv; +static GObject * +qemuDomainVcpuPrivateNew(void) +{ + return g_object_new(QEMU_TYPE_DOMAIN_VCPU_PRIVATE, NULL); } static void -qemuDomainVcpuPrivateDispose(void *obj) +qemuDomainVcpuPrivateFinalize(GObject *obj) { - qemuDomainVcpuPrivatePtr priv = obj; + qemuDomainVcpuPrivatePtr priv = QEMU_DOMAIN_VCPU(obj); VIR_FREE(priv->type); VIR_FREE(priv->alias); virJSONValueFree(priv->props); - return; + + G_OBJECT_CLASS(qemu_domain_vcpu_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainChrSourcePrivateClass; -static void qemuDomainChrSourcePrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainChrSourcePrivate, qemu_domain_chr_source_private, G_TYPE_OBJECT); +static void qemuDomainChrSourcePrivateFinalize(GObject *obj); -static int -qemuDomainChrSourcePrivateOnceInit(void) +static void +qemu_domain_chr_source_private_init(qemuDomainChrSourcePrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainChrSourcePrivate, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(qemuDomainChrSourcePrivate); - -static virObjectPtr -qemuDomainChrSourcePrivateNew(void) +static void +qemu_domain_chr_source_private_class_init(qemuDomainChrSourcePrivateClass *klass) { - qemuDomainChrSourcePrivatePtr priv; - - if (qemuDomainChrSourcePrivateInitialize() < 0) - return NULL; + GObjectClass *obj = G_OBJECT_CLASS(klass); - if (!(priv = virObjectNew(qemuDomainChrSourcePrivateClass))) - return NULL; + obj->finalize = qemuDomainChrSourcePrivateFinalize; +} - return (virObjectPtr) priv; +static GObject * +qemuDomainChrSourcePrivateNew(void) +{ + return g_object_new(QEMU_TYPE_DOMAIN_CHR_SOURCE_PRIVATE, NULL); } static void -qemuDomainChrSourcePrivateDispose(void *obj) +qemuDomainChrSourcePrivateFinalize(GObject *obj) { - qemuDomainChrSourcePrivatePtr priv = obj; + qemuDomainChrSourcePrivatePtr priv = QEMU_DOMAIN_CHR_SOURCE(obj); g_clear_pointer(&priv->secinfo, qemuDomainSecretInfoFree); + + G_OBJECT_CLASS(qemu_domain_chr_source_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainVsockPrivateClass; -static void qemuDomainVsockPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainVsockPrivate, qemu_domain_vsock_private, G_TYPE_OBJECT); +static void qemuDomainVsockPrivateFinalize(GObject *obj); -static int -qemuDomainVsockPrivateOnceInit(void) +static void +qemu_domain_vsock_private_init(qemuDomainVsockPrivate *priv) { - if (!VIR_CLASS_NEW(qemuDomainVsockPrivate, virClassForObject())) - return -1; - - return 0; + priv->vhostfd = -1; } -VIR_ONCE_GLOBAL_INIT(qemuDomainVsockPrivate); - -static virObjectPtr -qemuDomainVsockPrivateNew(void) +static void +qemu_domain_vsock_private_class_init(qemuDomainVsockPrivateClass *klass) { - qemuDomainVsockPrivatePtr priv; - - if (qemuDomainVsockPrivateInitialize() < 0) - return NULL; - - if (!(priv = virObjectNew(qemuDomainVsockPrivateClass))) - return NULL; + GObjectClass *obj = G_OBJECT_CLASS(klass); - priv->vhostfd = -1; + obj->finalize = qemuDomainVsockPrivateFinalize; +} - return (virObjectPtr) priv; +static GObject * +qemuDomainVsockPrivateNew(void) +{ + return g_object_new(QEMU_TYPE_DOMAIN_VSOCK_PRIVATE, NULL); } static void -qemuDomainVsockPrivateDispose(void *obj G_GNUC_UNUSED) +qemuDomainVsockPrivateFinalize(GObject *obj) { - qemuDomainVsockPrivatePtr priv = obj; + qemuDomainVsockPrivatePtr priv = QEMU_DOMAIN_VSOCK(obj); VIR_FORCE_CLOSE(priv->vhostfd); + + G_OBJECT_CLASS(qemu_domain_vsock_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainGraphicsPrivateClass; -static void qemuDomainGraphicsPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainGraphicsPrivate, qemu_domain_graphics_private, G_TYPE_OBJECT); +static void qemuDomainGraphicsPrivateFinalize(GObject *obj); -static int -qemuDomainGraphicsPrivateOnceInit(void) +static void +qemu_domain_graphics_private_init(qemuDomainGraphicsPrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainGraphicsPrivate, virClassForObject())) - return -1; - - return 0; } -VIR_ONCE_GLOBAL_INIT(qemuDomainGraphicsPrivate); - -static virObjectPtr -qemuDomainGraphicsPrivateNew(void) +static void +qemu_domain_graphics_private_class_init(qemuDomainGraphicsPrivateClass *klass) { - qemuDomainGraphicsPrivatePtr priv; - - if (qemuDomainGraphicsPrivateInitialize() < 0) - return NULL; + GObjectClass *obj = G_OBJECT_CLASS(klass); - if (!(priv = virObjectNew(qemuDomainGraphicsPrivateClass))) - return NULL; + obj->finalize = qemuDomainGraphicsPrivateFinalize; +} - return (virObjectPtr) priv; +static GObject * +qemuDomainGraphicsPrivateNew(void) +{ + return g_object_new(QEMU_TYPE_DOMAIN_GRAPHICS_PRIVATE, NULL); } static void -qemuDomainGraphicsPrivateDispose(void *obj) +qemuDomainGraphicsPrivateFinalize(GObject *obj) { - qemuDomainGraphicsPrivatePtr priv = obj; + qemuDomainGraphicsPrivatePtr priv = QEMU_DOMAIN_GRAPHICS(obj); VIR_FREE(priv->tlsAlias); g_clear_pointer(&priv->secinfo, qemuDomainSecretInfoFree); -} + G_OBJECT_CLASS(qemu_domain_graphics_private_parent_class)->finalize(obj); +} -static virClassPtr qemuDomainNetworkPrivateClass; -static void qemuDomainNetworkPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainNetworkPrivate, qemu_domain_network_private, G_TYPE_OBJECT); +static void qemuDomainNetworkPrivateFinalize(GObject *obj); -static int -qemuDomainNetworkPrivateOnceInit(void) +static void +qemu_domain_network_private_init(qemuDomainNetworkPrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainNetworkPrivate, virClassForObject())) - return -1; - - return 0; } +static void +qemu_domain_network_private_class_init(qemuDomainNetworkPrivateClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); -VIR_ONCE_GLOBAL_INIT(qemuDomainNetworkPrivate); + obj->finalize = qemuDomainNetworkPrivateFinalize; +} -static virObjectPtr +static GObject * qemuDomainNetworkPrivateNew(void) { - qemuDomainNetworkPrivatePtr priv; - - if (qemuDomainNetworkPrivateInitialize() < 0) - return NULL; - - if (!(priv = virObjectNew(qemuDomainNetworkPrivateClass))) - return NULL; - - return (virObjectPtr) priv; + return g_object_new(QEMU_TYPE_DOMAIN_NETWORK_PRIVATE, NULL); } static void -qemuDomainNetworkPrivateDispose(void *obj G_GNUC_UNUSED) +qemuDomainNetworkPrivateFinalize(GObject *obj) { - qemuDomainNetworkPrivatePtr priv = obj; + qemuDomainNetworkPrivatePtr priv = QEMU_DOMAIN_NETWORK(obj); qemuSlirpFree(priv->slirp); -} + G_OBJECT_CLASS(qemu_domain_network_private_parent_class)->finalize(obj); +} -static virClassPtr qemuDomainFSPrivateClass; -static void qemuDomainFSPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainFSPrivate, qemu_domain_fs_private, G_TYPE_OBJECT); +static void qemuDomainFSPrivateFinalize(GObject *obj); -static int -qemuDomainFSPrivateOnceInit(void) +static void +qemu_domain_fs_private_init(qemuDomainFSPrivate *priv G_GNUC_UNUSED) { - if (!VIR_CLASS_NEW(qemuDomainFSPrivate, virClassForObject())) - return -1; - - return 0; } +static void +qemu_domain_fs_private_class_init(qemuDomainFSPrivateClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); -VIR_ONCE_GLOBAL_INIT(qemuDomainFSPrivate); - + obj->finalize = qemuDomainFSPrivateFinalize; +} -static virObjectPtr +static GObject * qemuDomainFSPrivateNew(void) { - qemuDomainFSPrivatePtr priv; - - if (qemuDomainFSPrivateInitialize() < 0) - return NULL; - - if (!(priv = virObjectNew(qemuDomainFSPrivateClass))) - return NULL; - - return (virObjectPtr) priv; + return g_object_new(QEMU_TYPE_DOMAIN_FS_PRIVATE, NULL); } static void -qemuDomainFSPrivateDispose(void *obj) +qemuDomainFSPrivateFinalize(GObject *obj) { - qemuDomainFSPrivatePtr priv = obj; + qemuDomainFSPrivatePtr priv = QEMU_DOMAIN_FS(obj); g_free(priv->vhostuser_fs_sock); + + G_OBJECT_CLASS(qemu_domain_fs_private_parent_class)->finalize(obj); } -static virClassPtr qemuDomainVideoPrivateClass; -static void qemuDomainVideoPrivateDispose(void *obj); +G_DEFINE_TYPE(qemuDomainVideoPrivate, qemu_domain_video_private, G_TYPE_OBJECT); +static void qemuDomainVideoPrivateFinalize(GObject *obj); +static void +qemu_domain_video_private_init(qemuDomainVideoPrivate *priv) +{ + priv->vhost_user_fd = -1; +} -static int -qemuDomainVideoPrivateOnceInit(void) +static void +qemu_domain_video_private_class_init(qemuDomainVideoPrivateClass *klass) { - if (!VIR_CLASS_NEW(qemuDomainVideoPrivate, virClassForObject())) - return -1; + GObjectClass *obj = G_OBJECT_CLASS(klass); - return 0; + obj->finalize = qemuDomainVideoPrivateFinalize; } -VIR_ONCE_GLOBAL_INIT(qemuDomainVideoPrivate); - -static virObjectPtr +static GObject * qemuDomainVideoPrivateNew(void) { - qemuDomainVideoPrivatePtr priv; - - if (qemuDomainVideoPrivateInitialize() < 0) - return NULL; - - if (!(priv = virObjectNew(qemuDomainVideoPrivateClass))) - return NULL; - - priv->vhost_user_fd = -1; - - return (virObjectPtr) priv; + return g_object_new(QEMU_TYPE_DOMAIN_VIDEO_PRIVATE, NULL); } static void -qemuDomainVideoPrivateDispose(void *obj) +qemuDomainVideoPrivateFinalize(GObject *obj) { - qemuDomainVideoPrivatePtr priv = obj; + qemuDomainVideoPrivatePtr priv = QEMU_DOMAIN_VIDEO(obj); VIR_FORCE_CLOSE(priv->vhost_user_fd); + + G_OBJECT_CLASS(qemu_domain_video_private_parent_class)->finalize(obj); } diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index cf19f4d101..7dbd3ffc69 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -433,13 +433,8 @@ struct _qemuDomainObjPrivate { #define QEMU_DOMAIN_PRIVATE(vm) \ ((qemuDomainObjPrivatePtr) (vm)->privateData) -#define QEMU_DOMAIN_DISK_PRIVATE(disk) \ - ((qemuDomainDiskPrivatePtr) (disk)->privateData) - -typedef struct _qemuDomainDiskPrivate qemuDomainDiskPrivate; -typedef qemuDomainDiskPrivate *qemuDomainDiskPrivatePtr; struct _qemuDomainDiskPrivate { - virObject parent; + GObject parent; /* ideally we want a smarter way to interlock block jobs on single qemu disk * in the future, but for now we just disallow any concurrent job on a @@ -457,13 +452,20 @@ struct _qemuDomainDiskPrivate { char *nodeCopyOnRead; /* nodename of the disk-wide copy-on-read blockdev layer */ }; -#define QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src) \ - ((qemuDomainStorageSourcePrivatePtr) (src)->privateData) +#define QEMU_TYPE_DOMAIN_DISK_PRIVATE qemu_domain_disk_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainDiskPrivate, + qemu_domain_disk_private, + QEMU, + DOMAIN_DISK, + GObject); +typedef qemuDomainDiskPrivate *qemuDomainDiskPrivatePtr; + +#define QEMU_DOMAIN_DISK_PRIVATE(disk) \ + ((qemuDomainDiskPrivatePtr) (disk)->privateData) + -typedef struct _qemuDomainStorageSourcePrivate qemuDomainStorageSourcePrivate; -typedef qemuDomainStorageSourcePrivate *qemuDomainStorageSourcePrivatePtr; struct _qemuDomainStorageSourcePrivate { - virObject parent; + GObject parent; /* data required for authentication to the storage source */ qemuDomainSecretInfoPtr secinfo; @@ -475,12 +477,23 @@ struct _qemuDomainStorageSourcePrivate { qemuDomainSecretInfoPtr httpcookie; }; -virObjectPtr qemuDomainStorageSourcePrivateNew(void); +#define QEMU_TYPE_DOMAIN_STORAGE_SOURCE_PRIVATE qemu_domain_storage_source_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainStorageSourcePrivate, + qemu_domain_storage_source_private, + QEMU, + DOMAIN_STORAGE_SOURCE, + GObject); +typedef qemuDomainStorageSourcePrivate *qemuDomainStorageSourcePrivatePtr; + +#define QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src) \ + ((qemuDomainStorageSourcePrivatePtr) (src)->privateData) + + +GObject *qemuDomainStorageSourcePrivateNew(void); + -typedef struct _qemuDomainVcpuPrivate qemuDomainVcpuPrivate; -typedef qemuDomainVcpuPrivate *qemuDomainVcpuPrivatePtr; struct _qemuDomainVcpuPrivate { - virObject parent; + GObject parent; pid_t tid; /* vcpu thread id */ int enable_id; /* order in which the vcpus were enabled in qemu */ @@ -500,6 +513,14 @@ struct _qemuDomainVcpuPrivate { int vcpus; }; +#define QEMU_TYPE_DOMAIN_VCPU_PRIVATE qemu_domain_vcpu_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainVcpuPrivate, + qemu_domain_vcpu_private, + QEMU, + DOMAIN_VCPU, + GObject); +typedef qemuDomainVcpuPrivate *qemuDomainVcpuPrivatePtr; + #define QEMU_DOMAIN_VCPU_PRIVATE(vcpu) \ ((qemuDomainVcpuPrivatePtr) (vcpu)->privateData) @@ -513,77 +534,113 @@ struct qemuDomainDiskInfo { char *nodename; }; -#define QEMU_DOMAIN_CHR_SOURCE_PRIVATE(dev) \ - ((qemuDomainChrSourcePrivatePtr) (dev)->privateData) - -typedef struct _qemuDomainChrSourcePrivate qemuDomainChrSourcePrivate; -typedef qemuDomainChrSourcePrivate *qemuDomainChrSourcePrivatePtr; struct _qemuDomainChrSourcePrivate { - virObject parent; + GObject parent; /* for char devices using secret * NB: *not* to be written to qemu domain object XML */ qemuDomainSecretInfoPtr secinfo; }; +#define QEMU_TYPE_DOMAIN_CHR_SOURCE_PRIVATE qemu_domain_chr_source_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainChrSourcePrivate, + qemu_domain_chr_source_private, + QEMU, + DOMAIN_CHR_SOURCE, + GObject); +typedef qemuDomainChrSourcePrivate *qemuDomainChrSourcePrivatePtr; + +#define QEMU_DOMAIN_CHR_SOURCE_PRIVATE(dev) \ + ((qemuDomainChrSourcePrivatePtr) (dev)->privateData) + -typedef struct _qemuDomainVsockPrivate qemuDomainVsockPrivate; -typedef qemuDomainVsockPrivate *qemuDomainVsockPrivatePtr; struct _qemuDomainVsockPrivate { - virObject parent; + GObject parent; int vhostfd; }; +#define QEMU_TYPE_DOMAIN_VSOCK_PRIVATE qemu_domain_vsock_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainVsockPrivate, + qemu_domain_vsock_private, + QEMU, + DOMAIN_VSOCK, + GObject); +typedef qemuDomainVsockPrivate *qemuDomainVsockPrivatePtr; -#define QEMU_DOMAIN_VIDEO_PRIVATE(dev) \ - ((qemuDomainVideoPrivatePtr) (dev)->privateData) -typedef struct _qemuDomainVideoPrivate qemuDomainVideoPrivate; -typedef qemuDomainVideoPrivate *qemuDomainVideoPrivatePtr; struct _qemuDomainVideoPrivate { - virObject parent; + GObject parent; int vhost_user_fd; }; +#define QEMU_TYPE_DOMAIN_VIDEO_PRIVATE qemu_domain_video_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainVideoPrivate, + qemu_domain_video_private, + QEMU, + DOMAIN_VIDEO, + GObject); +typedef qemuDomainVideoPrivate *qemuDomainVideoPrivatePtr; + +#define QEMU_DOMAIN_VIDEO_PRIVATE(dev) \ + ((qemuDomainVideoPrivatePtr) (dev)->privateData) -#define QEMU_DOMAIN_GRAPHICS_PRIVATE(dev) \ - ((qemuDomainGraphicsPrivatePtr) (dev)->privateData) -typedef struct _qemuDomainGraphicsPrivate qemuDomainGraphicsPrivate; -typedef qemuDomainGraphicsPrivate *qemuDomainGraphicsPrivatePtr; struct _qemuDomainGraphicsPrivate { - virObject parent; + GObject parent; char *tlsAlias; qemuDomainSecretInfoPtr secinfo; }; +#define QEMU_TYPE_DOMAIN_GRAPHICS_PRIVATE qemu_domain_graphics_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainGraphicsPrivate, + qemu_domain_graphics_private, + QEMU, + DOMAIN_GRAPHICS, + GObject); +typedef qemuDomainGraphicsPrivate *qemuDomainGraphicsPrivatePtr; + +#define QEMU_DOMAIN_GRAPHICS_PRIVATE(dev) \ + ((qemuDomainGraphicsPrivatePtr) (dev)->privateData) -#define QEMU_DOMAIN_NETWORK_PRIVATE(dev) \ - ((qemuDomainNetworkPrivatePtr) (dev)->privateData) -typedef struct _qemuDomainNetworkPrivate qemuDomainNetworkPrivate; -typedef qemuDomainNetworkPrivate *qemuDomainNetworkPrivatePtr; struct _qemuDomainNetworkPrivate { - virObject parent; + GObject parent; qemuSlirpPtr slirp; }; +#define QEMU_TYPE_DOMAIN_NETWORK_PRIVATE qemu_domain_network_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainNetworkPrivate, + qemu_domain_network_private, + QEMU, + DOMAIN_NETWORK, + GObject); +typedef qemuDomainNetworkPrivate *qemuDomainNetworkPrivatePtr; + +#define QEMU_DOMAIN_NETWORK_PRIVATE(dev) \ + ((qemuDomainNetworkPrivatePtr) (dev)->privateData) -#define QEMU_DOMAIN_FS_PRIVATE(dev) \ - ((qemuDomainFSPrivatePtr) (dev)->privateData) -typedef struct _qemuDomainFSPrivate qemuDomainFSPrivate; -typedef qemuDomainFSPrivate *qemuDomainFSPrivatePtr; struct _qemuDomainFSPrivate { - virObject parent; + GObject parent; char *vhostuser_fs_sock; }; +#define QEMU_TYPE_DOMAIN_FS_PRIVATE qemu_domain_fs_private_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainFSPrivate, + qemu_domain_fs_private, + QEMU, + DOMAIN_FS, + GObject); +typedef qemuDomainFSPrivate *qemuDomainFSPrivatePtr; + +#define QEMU_DOMAIN_FS_PRIVATE(dev) \ + ((qemuDomainFSPrivatePtr) (dev)->privateData) + typedef enum { QEMU_PROCESS_EVENT_WATCHDOG = 0, diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index ffc8bdb344..0f26a81391 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -2717,7 +2717,7 @@ virStorageSourceClear(virStorageSourcePtr def) virStorageNetHostDefFree(def->nhosts, def->hosts); virStorageAuthDefFree(def->auth); - virObjectUnref(def->privateData); + g_clear_object(&def->privateData); VIR_FREE(def->nodestorage); VIR_FREE(def->nodeformat); diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index 7939c09cd5..a744bcbf5c 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -30,6 +30,7 @@ #include "virsecret.h" #include "virenum.h" #include "virpci.h" +#include <glib-object.h> /* Minimum header size required to probe all known formats with * virStorageFileProbeFormat, or obtain metadata from a known format. @@ -304,7 +305,7 @@ struct _virStorageSource { virStorageSourceInitiatorDef initiator; - virObjectPtr privateData; + GObject *privateData; int format; /* virStorageFileFormat in domain backing chains, but * pool-specific enum for storage volumes */ -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/snapshot_conf.c | 2 +- src/conf/snapshot_conf.h | 2 +- src/conf/virsavecookie.c | 10 ++++---- src/conf/virsavecookie.h | 14 +++++------ src/qemu/qemu_domain.c | 53 +++++++++++++++++++--------------------- src/qemu/qemu_domain.h | 12 ++++++--- src/qemu/qemu_driver.c | 8 +++--- 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index 37b5c2fdf7..138c7eb5e0 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -129,7 +129,7 @@ virDomainSnapshotDefDispose(void *obj) for (i = 0; i < def->ndisks; i++) virDomainSnapshotDiskDefClear(&def->disks[i]); VIR_FREE(def->disks); - virObjectUnref(def->cookie); + g_clear_object(&def->cookie); } int diff --git a/src/conf/snapshot_conf.h b/src/conf/snapshot_conf.h index b5b1ef2718..97a7b3b4e5 100644 --- a/src/conf/snapshot_conf.h +++ b/src/conf/snapshot_conf.h @@ -84,7 +84,7 @@ struct _virDomainSnapshotDef { size_t ndisks; /* should not exceed dom->ndisks */ virDomainSnapshotDiskDef *disks; - virObjectPtr cookie; + GObject *cookie; }; G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainSnapshotDef, virObjectUnref); diff --git a/src/conf/virsavecookie.c b/src/conf/virsavecookie.c index bdc22bb324..6cdfa4fc5a 100644 --- a/src/conf/virsavecookie.c +++ b/src/conf/virsavecookie.c @@ -34,7 +34,7 @@ VIR_LOG_INIT("conf.savecookie"); static int virSaveCookieParseNode(xmlXPathContextPtr ctxt, - virObjectPtr *obj, + GObject **obj, virSaveCookieCallbacksPtr saveCookie) { *obj = NULL; @@ -54,7 +54,7 @@ virSaveCookieParseNode(xmlXPathContextPtr ctxt, int virSaveCookieParse(xmlXPathContextPtr ctxt, - virObjectPtr *obj, + GObject **obj, virSaveCookieCallbacksPtr saveCookie) { xmlNodePtr node = ctxt->node; @@ -77,7 +77,7 @@ virSaveCookieParse(xmlXPathContextPtr ctxt, int virSaveCookieParseString(const char *xml, - virObjectPtr *obj, + GObject **obj, virSaveCookieCallbacksPtr saveCookie) { xmlDocPtr doc = NULL; @@ -105,7 +105,7 @@ virSaveCookieParseString(const char *xml, int virSaveCookieFormatBuf(virBufferPtr buf, - virObjectPtr obj, + GObject *obj, virSaveCookieCallbacksPtr saveCookie) { if (!obj || !saveCookie || !saveCookie->format) @@ -125,7 +125,7 @@ virSaveCookieFormatBuf(virBufferPtr buf, char * -virSaveCookieFormat(virObjectPtr obj, +virSaveCookieFormat(GObject *obj, virSaveCookieCallbacksPtr saveCookie) { virBuffer buf = VIR_BUFFER_INITIALIZER; diff --git a/src/conf/virsavecookie.h b/src/conf/virsavecookie.h index eafffbed66..b17701c727 100644 --- a/src/conf/virsavecookie.h +++ b/src/conf/virsavecookie.h @@ -23,14 +23,14 @@ #include <libxml/xpath.h> #include "internal.h" -#include "virobject.h" #include "virbuffer.h" +#include <glib-object.h> typedef int (*virSaveCookieParseFunc)(xmlXPathContextPtr ctxt, - virObjectPtr *obj); + GObject **obj); typedef int (*virSaveCookieFormatFunc)(virBufferPtr buf, - virObjectPtr obj); + GObject *obj); typedef struct _virSaveCookieCallbacks virSaveCookieCallbacks; typedef virSaveCookieCallbacks *virSaveCookieCallbacksPtr; @@ -42,19 +42,19 @@ struct _virSaveCookieCallbacks { int virSaveCookieParse(xmlXPathContextPtr ctxt, - virObjectPtr *obj, + GObject **obj, virSaveCookieCallbacksPtr saveCookie); int virSaveCookieParseString(const char *xml, - virObjectPtr *obj, + GObject **obj, virSaveCookieCallbacksPtr saveCookie); int virSaveCookieFormatBuf(virBufferPtr buf, - virObjectPtr obj, + GObject *obj, virSaveCookieCallbacksPtr saveCookie); char * -virSaveCookieFormat(virObjectPtr obj, +virSaveCookieFormat(GObject *obj, virSaveCookieCallbacksPtr saveCookie); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 7f6ac10659..a61c16eb2a 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -163,21 +163,12 @@ struct _qemuDomainLogContext { }; G_DEFINE_TYPE(qemuDomainLogContext, qemu_domain_log_context, G_TYPE_OBJECT); -static virClassPtr qemuDomainSaveCookieClass; +G_DEFINE_TYPE(qemuDomainSaveCookie, qemu_domain_save_cookie, G_TYPE_OBJECT); static void qemuDomainLogContextFinalize(GObject *obj); -static void qemuDomainSaveCookieDispose(void *obj); +static void qemuDomainSaveCookieFinalize(GObject *obj); -static int -qemuDomainOnceInit(void) -{ - if (!VIR_CLASS_NEW(qemuDomainSaveCookie, virClassForObject())) - return -1; - - return 0; -} - static void qemu_domain_log_context_init(qemuDomainLogContext *logctxt G_GNUC_UNUSED) { } @@ -189,8 +180,6 @@ static void qemu_domain_log_context_class_init(qemuDomainLogContextClass *klass) obj->finalize = qemuDomainLogContextFinalize; } -VIR_ONCE_GLOBAL_INIT(qemuDomain); - static void qemuDomainLogContextFinalize(GObject *object) { @@ -204,6 +193,20 @@ qemuDomainLogContextFinalize(GObject *object) G_OBJECT_CLASS(qemu_domain_log_context_parent_class)->finalize(object); } +static void +qemu_domain_save_cookie_init(qemuDomainSaveCookie *cookie G_GNUC_UNUSED) +{ +} + +static void +qemu_domain_save_cookie_class_init(qemuDomainSaveCookieClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = qemuDomainSaveCookieFinalize; +} + + const char * qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job, int phase G_GNUC_UNUSED) @@ -12753,13 +12756,15 @@ qemuDomainGetStorageSourceByDevstr(const char *devstr, static void -qemuDomainSaveCookieDispose(void *obj) +qemuDomainSaveCookieFinalize(GObject *obj) { - qemuDomainSaveCookiePtr cookie = obj; + qemuDomainSaveCookiePtr cookie = QEMU_DOMAIN_SAVE_COOKIE(obj); VIR_DEBUG("cookie=%p", cookie); virCPUDefFree(cookie->cpu); + + G_OBJECT_CLASS(qemu_domain_save_cookie_parent_class)->finalize(obj); } @@ -12769,11 +12774,7 @@ qemuDomainSaveCookieNew(virDomainObjPtr vm) qemuDomainObjPrivatePtr priv = vm->privateData; g_autoptr(qemuDomainSaveCookie) cookie = NULL; - if (qemuDomainInitialize() < 0) - return NULL; - - if (!(cookie = virObjectNew(qemuDomainSaveCookieClass))) - return NULL; + cookie = QEMU_DOMAIN_SAVE_COOKIE(g_object_new(QEMU_TYPE_DOMAIN_SAVE_COOKIE, NULL)); if (priv->origCPU && !(cookie->cpu = virCPUDefCopy(vm->def->cpu))) return NULL; @@ -12789,15 +12790,11 @@ qemuDomainSaveCookieNew(virDomainObjPtr vm) static int qemuDomainSaveCookieParse(xmlXPathContextPtr ctxt G_GNUC_UNUSED, - virObjectPtr *obj) + GObject **obj) { g_autoptr(qemuDomainSaveCookie) cookie = NULL; - if (qemuDomainInitialize() < 0) - return -1; - - if (!(cookie = virObjectNew(qemuDomainSaveCookieClass))) - return -1; + cookie = QEMU_DOMAIN_SAVE_COOKIE(g_object_new(QEMU_TYPE_DOMAIN_SAVE_COOKIE, NULL)); if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &cookie->cpu) < 0) @@ -12805,14 +12802,14 @@ qemuDomainSaveCookieParse(xmlXPathContextPtr ctxt G_GNUC_UNUSED, cookie->slirpHelper = virXPathBoolean("boolean(./slirpHelper)", ctxt) > 0; - *obj = (virObjectPtr) g_steal_pointer(&cookie); + *obj = G_OBJECT(g_steal_pointer(&cookie)); return 0; } static int qemuDomainSaveCookieFormat(virBufferPtr buf, - virObjectPtr obj) + GObject *obj) { qemuDomainSaveCookiePtr cookie = (qemuDomainSaveCookiePtr) obj; diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index 7dbd3ffc69..65a1f03a2b 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -672,16 +672,20 @@ void qemuProcessEventFree(struct qemuProcessEvent *event); G_DECLARE_FINAL_TYPE(qemuDomainLogContext, qemu_domain_log_context, QEMU, DOMAIN_LOG_CONTEXT, GObject); typedef qemuDomainLogContext *qemuDomainLogContextPtr; -typedef struct _qemuDomainSaveCookie qemuDomainSaveCookie; -typedef qemuDomainSaveCookie *qemuDomainSaveCookiePtr; struct _qemuDomainSaveCookie { - virObject parent; + GObject parent; virCPUDefPtr cpu; bool slirpHelper; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuDomainSaveCookie, virObjectUnref); +#define QEMU_TYPE_DOMAIN_SAVE_COOKIE qemu_domain_save_cookie_get_type() +G_DECLARE_FINAL_TYPE(qemuDomainSaveCookie, + qemu_domain_save_cookie, + QEMU, + DOMAIN_SAVE_COOKIE, + GObject); +typedef qemuDomainSaveCookie *qemuDomainSaveCookiePtr; typedef struct _qemuDomainXmlNsDef qemuDomainXmlNsDef; typedef qemuDomainXmlNsDef *qemuDomainXmlNsDefPtr; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 4d0f76fb21..543d8fb1c2 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -2872,7 +2872,7 @@ virQEMUSaveDataNew(char *domXML, data->xml = g_steal_pointer(&domXML); if (cookieObj && - !(data->cookie = virSaveCookieFormat((virObjectPtr) cookieObj, + !(data->cookie = virSaveCookieFormat(G_OBJECT(cookieObj), virDomainXMLOptionGetSaveCookie(xmlopt)))) goto error; @@ -6837,7 +6837,7 @@ qemuDomainSaveImageStartVM(virConnectPtr conn, g_autoptr(qemuDomainSaveCookie) cookie = NULL; int rc = 0; - if (virSaveCookieParseString(data->cookie, (virObjectPtr *)&cookie, + if (virSaveCookieParseString(data->cookie, (GObject **)&cookie, virDomainXMLOptionGetSaveCookie(driver->xmlopt)) < 0) goto cleanup; @@ -14478,7 +14478,7 @@ qemuDomainSnapshotCreateActiveInternal(virQEMUDriverPtr driver, if (ret < 0) goto cleanup; - if (!(snapdef->cookie = (virObjectPtr) qemuDomainSaveCookieNew(vm))) + if (!(snapdef->cookie = G_OBJECT(qemuDomainSaveCookieNew(vm)))) goto cleanup; if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_HALT) { @@ -15486,7 +15486,7 @@ qemuDomainSnapshotCreateActiveExternal(virQEMUDriverPtr driver, if (!(xml = qemuDomainDefFormatLive(driver, priv->qemuCaps, vm->def, priv->origCPU, true, true)) || - !(snapdef->cookie = (virObjectPtr) qemuDomainSaveCookieNew(vm))) + !(snapdef->cookie = G_OBJECT(qemuDomainSaveCookieNew(vm)))) goto cleanup; if (!(data = virQEMUSaveDataNew(xml, -- 2.25.3

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libvirt_private.syms | 1 + src/qemu/qemu_blockjob.c | 67 ++++++++++++++++----------------------- src/qemu/qemu_domain.c | 11 +++---- src/util/virstoragefile.c | 43 ++++++++++++------------- src/util/virstoragefile.h | 6 ++-- 5 files changed, 57 insertions(+), 71 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d96331e633..a5dc53b4f6 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3082,6 +3082,7 @@ virStorageGenerateQcowPassphrase; # util/virstoragefile.h +vir_storage_source_get_type; virStorageAuthDefCopy; virStorageAuthDefFormat; virStorageAuthDefFree; diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c index 38fbba2b90..5aac483724 100644 --- a/src/qemu/qemu_blockjob.c +++ b/src/qemu/qemu_blockjob.c @@ -93,10 +93,10 @@ static void qemuBlockJobDataDisposeJobdata(qemuBlockJobDataPtr job) { if (job->type == QEMU_BLOCKJOB_TYPE_CREATE) - virObjectUnref(job->data.create.src); + g_object_unref(job->data.create.src); if (job->type == QEMU_BLOCKJOB_TYPE_BACKUP) { - virObjectUnref(job->data.backup.store); + g_object_unref(job->data.backup.store); g_free(job->data.backup.bitmap); } } @@ -107,10 +107,8 @@ qemuBlockJobDataDispose(GObject *obj) { qemuBlockJobDataPtr job = QEMU_BLOCK_JOB_DATA(obj); - virObjectUnref(job->chain); - job->chain = NULL; - virObjectUnref(job->mirrorChain); - job->mirrorChain = NULL; + g_clear_object(&job->chain); + g_clear_object(&job->mirrorChain); G_OBJECT_CLASS(qemu_block_job_data_parent_class)->dispose(obj); } @@ -197,7 +195,7 @@ qemuBlockJobRegister(qemuBlockJobDataPtr job, if (disk) { job->disk = disk; - job->chain = virObjectRef(disk->src); + job->chain = g_object_ref(disk->src); QEMU_DOMAIN_DISK_PRIVATE(disk)->blockjob = g_object_ref(job); } @@ -349,9 +347,9 @@ qemuBlockJobNewCreate(virDomainObjPtr vm, return NULL; if (virStorageSourceIsBacking(chain)) - job->chain = virObjectRef(chain); + job->chain = g_object_ref(chain); - job->data.create.src = virObjectRef(src); + job->data.create.src = g_object_ref(src); if (qemuBlockJobRegister(job, vm, NULL, true) < 0) return NULL; @@ -382,7 +380,7 @@ qemuBlockJobDiskNewCopy(virDomainObjPtr vm, if (!(job = qemuBlockJobDataNew(QEMU_BLOCKJOB_TYPE_COPY, jobname))) return NULL; - job->mirrorChain = virObjectRef(mirror); + job->mirrorChain = g_object_ref(mirror); if (shallow && !reuse) job->data.copy.shallownew = true; @@ -411,7 +409,7 @@ qemuBlockJobDiskNewBackup(virDomainObjPtr vm, return NULL; job->data.backup.bitmap = g_strdup(bitmap); - job->data.backup.store = virObjectRef(store); + job->data.backup.store = g_object_ref(store); /* backup jobs are usually started in bulk by transaction so the caller * shall save the status XML */ @@ -700,14 +698,12 @@ qemuBlockJobRewriteConfigDiskSource(virDomainObjPtr vm, /* discard any detected backing store */ if (virStorageSourceIsBacking(n->backingStore) && n->backingStore->detected) { - virObjectUnref(n->backingStore); - n->backingStore = NULL; + g_clear_object(&n->backingStore); break; } } - virObjectUnref(persistDisk->src); - persistDisk->src = g_steal_pointer(©); + g_set_object(&persistDisk->src, copy); } @@ -741,8 +737,7 @@ qemuBlockJobEventProcessLegacyCompleted(virQEMUDriverPtr driver, disk->dst); } - virObjectUnref(disk->src); - disk->src = disk->mirror; + g_set_object(&disk->src, disk->mirror); } else { if (disk->mirror) { virDomainLockImageDetach(driver->lockManager, vm, disk->mirror); @@ -752,7 +747,7 @@ qemuBlockJobEventProcessLegacyCompleted(virQEMUDriverPtr driver, * Remove security driver metadata so that they are not leaked. */ qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->mirror); - virObjectUnref(disk->mirror); + g_object_unref(disk->mirror); } qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->src); @@ -829,8 +824,7 @@ qemuBlockJobEventProcessLegacy(virQEMUDriverPtr driver, * Remove security driver metadata so that they are not leaked. */ qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->mirror); - virObjectUnref(disk->mirror); - disk->mirror = NULL; + g_clear_object(&disk->mirror); } disk->mirrorState = VIR_DOMAIN_DISK_MIRROR_STATE_NONE; disk->mirrorJob = VIR_DOMAIN_BLOCK_JOB_TYPE_UNKNOWN; @@ -942,8 +936,8 @@ qemuBlockJobClearConfigChain(virDomainObjPtr vm, if (!virStorageSourceIsSameLocation(disk->src, cfgdisk->src)) return; - virObjectUnref(cfgdisk->src->backingStore); - cfgdisk->src->backingStore = NULL; + if (cfgdisk->src->backingStore) + g_clear_object(&cfgdisk->src->backingStore); } @@ -1009,14 +1003,14 @@ qemuBlockJobProcessEventCompletedPull(virQEMUDriverPtr driver, if (baseparent) baseparent->backingStore = NULL; qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, tmp); - virObjectUnref(tmp); + g_clear_object(&tmp); if (cfgdisk) { tmp = cfgdisk->src->backingStore; cfgdisk->src->backingStore = cfgbase; if (cfgbaseparent) cfgbaseparent->backingStore = NULL; - virObjectUnref(tmp); + g_clear_object(&tmp); } } @@ -1183,8 +1177,7 @@ qemuBlockJobProcessEventCompletedCommit(virQEMUDriverPtr driver, if (job->data.commit.deleteCommittedImages) qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top); - virObjectUnref(job->data.commit.top); - job->data.commit.top = NULL; + g_clear_object(&job->data.commit.top); if (cfgbaseparent) { cfgbase = cfgbaseparent->backingStore; @@ -1195,7 +1188,7 @@ qemuBlockJobProcessEventCompletedCommit(virQEMUDriverPtr driver, else cfgdisk->src = cfgbase; - virObjectUnref(cfgtop); + g_clear_object(&cfgtop); } } @@ -1261,7 +1254,7 @@ qemuBlockJobProcessEventCompletedActiveCommit(virQEMUDriverPtr driver, cfgbaseparent->backingStore = NULL; cfgdisk->src = cfgbase; cfgdisk->src->readonly = cfgtop->readonly; - virObjectUnref(cfgtop); + g_clear_object(&cfgtop); } /* Move security driver metadata */ @@ -1277,11 +1270,9 @@ qemuBlockJobProcessEventCompletedActiveCommit(virQEMUDriverPtr driver, if (job->data.commit.deleteCommittedImages) qemuBlockJobDeleteImages(driver, vm, job->disk, job->data.commit.top); - virObjectUnref(job->data.commit.top); - job->data.commit.top = NULL; + g_clear_object(&job->data.commit.top); /* the mirror element does not serve functional purpose for the commit job */ - virObjectUnref(job->disk->mirror); - job->disk->mirror = NULL; + g_clear_object(&job->disk->mirror); } @@ -1309,8 +1300,7 @@ qemuBlockJobProcessEventConcludedCopyPivot(virQEMUDriverPtr driver, qemuBlockJobRewriteConfigDiskSource(vm, job->disk, job->disk->mirror); qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->disk->src); - virObjectUnref(job->disk->src); - job->disk->src = g_steal_pointer(&job->disk->mirror); + g_set_object(&job->disk->src, job->disk->mirror); } @@ -1328,8 +1318,7 @@ qemuBlockJobProcessEventConcludedCopyAbort(virQEMUDriverPtr driver, return; qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->disk->mirror); - virObjectUnref(job->disk->mirror); - job->disk->mirror = NULL; + g_clear_object(&job->disk->mirror); } @@ -1350,8 +1339,7 @@ qemuBlockJobProcessEventFailedActiveCommit(virQEMUDriverPtr driver, * not leaking security driver metadata is more important. */ qemuBlockRemoveImageMetadata(driver, vm, disk->dst, disk->mirror); - virObjectUnref(disk->mirror); - disk->mirror = NULL; + g_clear_object(&disk->mirror); } @@ -1401,8 +1389,7 @@ qemuBlockJobProcessEventConcludedCreate(virQEMUDriverPtr driver, * it will handle further hotplug of the created volume and also that * the 'chain' which was registered is under their control */ if (job->synchronous) { - virObjectUnref(job->chain); - job->chain = NULL; + g_clear_object(&job->chain); return; } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index a61c16eb2a..be7c6837b5 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1162,8 +1162,7 @@ qemuDomainDiskPrivateDispose(GObject *obj) { qemuDomainDiskPrivatePtr priv = QEMU_DOMAIN_DISK(obj); - virObjectUnref(priv->migrSource); - priv->migrSource = NULL; + g_clear_object(&priv->migrSource); g_clear_object(&priv->blockjob); G_OBJECT_CLASS(qemu_domain_disk_private_parent_class)->dispose(obj); @@ -2258,10 +2257,8 @@ qemuDomainObjPrivateDataClear(qemuDomainObjPrivatePtr priv) virHashRemoveAll(priv->blockjobs); - virObjectUnref(priv->pflash0); - priv->pflash0 = NULL; - virObjectUnref(priv->pflash1); - priv->pflash1 = NULL; + g_clear_object(&priv->pflash0); + g_clear_object(&priv->pflash1); virDomainBackupDefFree(priv->backup); priv->backup = NULL; @@ -3396,7 +3393,7 @@ qemuDomainObjPrivateXMLParseBlockjobData(virDomainObjPtr vm, if (mirror) { if (disk) - job->mirrorChain = virObjectRef(disk->mirror); + job->mirrorChain = g_object_ref(disk->mirror); else invalidData = true; } diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 0f26a81391..baec1602b4 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -45,7 +45,21 @@ VIR_LOG_INIT("util.storagefile"); -static virClassPtr virStorageSourceClass; +G_DEFINE_TYPE(virStorageSource, vir_storage_source, G_TYPE_OBJECT); +static void virStorageSourceFinalize(GObject *obj); + +static void +vir_storage_source_init(virStorageSource *src G_GNUC_UNUSED) +{ +} + +static void +vir_storage_source_class_init(virStorageSourceClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virStorageSourceFinalize; +} VIR_ENUM_IMPL(virStorage, VIR_STORAGE_TYPE_LAST, @@ -2682,8 +2696,7 @@ virStorageSourceBackingStoreClear(virStorageSourcePtr def) VIR_FREE(def->backingStoreRaw); /* recursively free backing chain */ - virObjectUnref(def->backingStore); - def->backingStore = NULL; + g_clear_object(&def->backingStore); } @@ -2712,8 +2725,7 @@ virStorageSourceClear(virStorageSourcePtr def) virStorageSourceSliceFree(def->sliceStorage); - virObjectUnref(def->externalDataStore); - def->externalDataStore = NULL; + g_clear_object(&def->externalDataStore); virStorageNetHostDefFree(def->nhosts, def->hosts); virStorageAuthDefFree(def->auth); @@ -2739,34 +2751,21 @@ virStorageSourceClear(virStorageSourcePtr def) static void -virStorageSourceDispose(void *obj) +virStorageSourceFinalize(GObject *obj) { - virStorageSourcePtr src = obj; + virStorageSourcePtr src = VIR_STORAGE_SOURCE(obj); virStorageSourceClear(src); -} - -static int -virStorageSourceOnceInit(void) -{ - if (!VIR_CLASS_NEW(virStorageSource, virClassForObject())) - return -1; - - return 0; + G_OBJECT_CLASS(vir_storage_source_parent_class)->finalize(obj); } -VIR_ONCE_GLOBAL_INIT(virStorageSource); - virStorageSourcePtr virStorageSourceNew(void) { - if (virStorageSourceInitialize() < 0) - return NULL; - - return virObjectNew(virStorageSourceClass); + return VIR_STORAGE_SOURCE(g_object_new(VIR_TYPE_STORAGE_SOURCE, NULL)); } diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index a744bcbf5c..ce56e8e18e 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -275,7 +275,7 @@ typedef virStorageSource *virStorageSourcePtr; * IMPORTANT: When adding fields to this struct it's also necessary to add * appropriate code to the virStorageSourceCopy deep copy function */ struct _virStorageSource { - virObject parent; + GObject parent; unsigned int id; /* backing chain identifier, 0 is unset */ int type; /* virStorageType */ @@ -393,7 +393,9 @@ struct _virStorageSource { bool ssh_host_key_check_disabled; }; -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageSource, virObjectUnref); +#define VIR_TYPE_STORAGE_SOURCE vir_storage_source_get_type() +G_DECLARE_FINAL_TYPE(virStorageSource, vir_storage_source, VIR, STORAGE_SOURCE, GObject); + #ifndef DEV_BSIZE -- 2.25.3

On Tue, Apr 21, 2020 at 03:48:41PM +0200, Rafael Fonseca wrote:
This patch series convert various simple instances of virObject to a GObject equivalent.
virLockableObject and virObjects which are subclassed will be covered in future patchsets.
New in v2: - use *Dispose for unreffing objects and *Finalize for freeing data, as suggested in the GLib documentation
Can you point to the docs with the rationale for that. Looking at the patches the distinction looks pretty arbitary, creating extra methods without an obvious benefit. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Tue, Apr 21, 2020 at 4:03 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
On Tue, Apr 21, 2020 at 03:48:41PM +0200, Rafael Fonseca wrote:
This patch series convert various simple instances of virObject to a GObject equivalent.
virLockableObject and virObjects which are subclassed will be covered in future patchsets.
New in v2: - use *Dispose for unreffing objects and *Finalize for freeing data, as suggested in the GLib documentation
Can you point to the docs with the rationale for that. Looking at the patches the distinction looks pretty arbitary, creating extra methods without an obvious benefit.
https://developer.gnome.org/gobject/stable/gobject-memory.html#gobject-memor... I did the changes as requested here: https://www.redhat.com/archives/libvir-list/2020-April/msg00383.html Att -- Rafael Fonseca

On Tue, Apr 21, 2020 at 04:12:09PM +0200, Rafael Fonseca wrote:
On Tue, Apr 21, 2020 at 4:03 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
On Tue, Apr 21, 2020 at 03:48:41PM +0200, Rafael Fonseca wrote:
This patch series convert various simple instances of virObject to a GObject equivalent.
virLockableObject and virObjects which are subclassed will be covered in future patchsets.
New in v2: - use *Dispose for unreffing objects and *Finalize for freeing data, as suggested in the GLib documentation
Can you point to the docs with the rationale for that. Looking at the patches the distinction looks pretty arbitary, creating extra methods without an obvious benefit.
https://developer.gnome.org/gobject/stable/gobject-memory.html#gobject-memor...
I did the changes as requested here: https://www.redhat.com/archives/libvir-list/2020-April/msg00383.html
Sorry I didn't see that suggestion before, as I don't really agree with it. Reading the GObject docs, I see this: "the destruction process is split in two phases: the first phase, executed in the dispose handler is supposed to release all references to other member objects. The second phase, executed by the finalize handler is supposed to complete the object's destruction process. Object methods should be able to run without program error in-between the two phases." And this: "When dispose ends, the object should not hold any reference to any other member object. The object is also expected to be able to answer client method invocations (with possibly an error code but no memory violation) until finalize is executed." The existing libvirt code is written from the POV that everything is released in one time, in a finalize method. Thus by definition our current code has no cycle problems that would require the split dispose/finalize approach. More importantly though, I very much doubt we are able to to satisfy the requirement for "dispose" wrt arbitrary object methods not having memory violations. I'm sure our code expects the objects to be non-NULL and would thus crash on a NULL pointer if run. Overall I'm not convinced there's any benefit to using the separate dispose method in libvirt. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Tue, 2020-04-21 at 15:26 +0100, Daniel P. Berrangé wrote:
On Tue, Apr 21, 2020 at 04:12:09PM +0200, Rafael Fonseca wrote:
On Tue, Apr 21, 2020 at 4:03 PM Daniel P. Berrangé < berrange@redhat.com> wrote:
On Tue, Apr 21, 2020 at 03:48:41PM +0200, Rafael Fonseca wrote:
This patch series convert various simple instances of virObject to a GObject equivalent.
virLockableObject and virObjects which are subclassed will be covered in future patchsets.
New in v2: - use *Dispose for unreffing objects and *Finalize for freeing data, as suggested in the GLib documentation
Can you point to the docs with the rationale for that. Looking at the patches the distinction looks pretty arbitary, creating extra methods without an obvious benefit.
https://developer.gnome.org/gobject/stable/gobject-memory.html#gobject-memor...
I did the changes as requested here: https://www.redhat.com/archives/libvir-list/2020-April/msg00383.html
Sorry I didn't see that suggestion before, as I don't really agree with it.
Reading the GObject docs, I see this:
"the destruction process is split in two phases: the first phase, executed in the dispose handler is supposed to release all references to other member objects. The second phase, executed by the finalize handler is supposed to complete the object's destruction process. Object methods should be able to run without program error in-between the two phases."
And this:
"When dispose ends, the object should not hold any reference to any other member object. The object is also expected to be able to answer client method invocations (with possibly an error code but no memory violation) until finalize is executed."
The existing libvirt code is written from the POV that everything is released in one time, in a finalize method. Thus by definition our current code has no cycle problems that would require the split dispose/finalize approach.
More importantly though, I very much doubt we are able to to satisfy the requirement for "dispose" wrt arbitrary object methods not having memory violations. I'm sure our code expects the objects to be non- NULL and would thus crash on a NULL pointer if run.
Overall I'm not convinced there's any benefit to using the separate dispose method in libvirt.
Fair enough. I believe that this cycle issue tends to happen more often in language bindings. For example, I'm pretty sure that I've seen cases in the past where reference cycles are introduced between the base GObject and language binding wrapper objects. So even if the base library code doesn't have any reference cycles, bindings can introduce them. But maybe this is not the case for us. I was just being (perhaps overly) cautious. Jonathon

On Tue, 2020-04-21 at 15:26 +0100, Daniel P. Berrangé wrote:
Overall I'm not convinced there's any benefit to using the separate dispose method in libvirt.
I have an updated version with dispose merged back into finalize. I'm just waiting for any other comments before sending the update. Att -- Rafael Fonseca

On Mon, May 11, 2020 at 01:10:14PM +0200, Rafael Fonseca wrote:
On Tue, 2020-04-21 at 15:26 +0100, Daniel P. Berrangé wrote:
Overall I'm not convinced there's any benefit to using the separate dispose method in libvirt.
I have an updated version with dispose merged back into finalize. I'm just waiting for any other comments before sending the update.
Go ahead and send it. The rest of the code looks pretty sane to me, but there's a huge amount of it to review, so I'd rather just review a v3 straight away, and then start to incrementally push patches as they are reviewed. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (3)
-
Daniel P. Berrangé
-
Jonathon Jongsma
-
Rafael Fonseca