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@xxxxxxxxx> --- 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