[PATCH 02/36] conf: capabilities: convert virCaps to GOBject

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx>
---
 src/bhyve/bhyve_capabilities.c  | 16 ++++--------
 src/bhyve/bhyve_driver.c        | 30 ++++++++--------------
 src/conf/capabilities.c         | 39 ++++++++++++++--------------
 src/conf/capabilities.h         |  6 ++---
 src/conf/storage_capabilities.c |  5 ++--
 src/conf/virconftypes.h         |  3 +--
 src/esx/esx_driver.c            | 25 +++++++-----------
 src/libxl/libxl_capabilities.c  | 19 +++++---------
 src/libxl/libxl_conf.c          |  3 ++-
 src/lxc/lxc_conf.c              | 16 +++++-------
 src/lxc/lxc_controller.c        |  3 ++-
 src/lxc/lxc_driver.c            | 45 ++++++++++++---------------------
 src/lxc/lxc_process.c           |  3 ++-
 src/openvz/openvz_conf.c        |  7 +++--
 src/qemu/qemu_capabilities.c    | 14 +++-------
 src/qemu/qemu_conf.c            |  8 +++---
 src/qemu/qemu_driver.c          |  3 ++-
 src/security/virt-aa-helper.c   |  8 +++---
 src/storage/storage_backend.c   |  3 +--
 src/test/test_driver.c          | 28 +++++++++-----------
 src/vbox/vbox_common.c          |  7 +++--
 src/vmware/vmware_conf.c        | 12 ++++-----
 src/vz/vz_driver.c              | 27 ++++++++------------
 tests/bhyveargv2xmltest.c       |  2 +-
 tests/bhyvexml2argvtest.c       |  2 +-
 tests/bhyvexml2xmltest.c        |  2 +-
 tests/domainconftest.c          |  3 ++-
 tests/genericxml2xmltest.c      |  3 ++-
 tests/openvzutilstest.c         |  2 +-
 tests/qemucaps2xmltest.c        | 13 +++-------
 tests/qemuhotplugtest.c         |  6 ++---
 tests/testutils.c               |  4 +--
 tests/testutilslxc.c            | 21 +++++++--------
 tests/testutilsqemu.c           | 11 ++++----
 tests/testutilsxen.c            |  3 +--
 tests/vircaps2xmltest.c         |  6 +----
 tests/vircapstest.c             | 14 +++-------
 tests/virresctrltest.c          |  3 +--
 tests/vmx2xmltest.c             |  8 ++----
 tests/xml2vmxtest.c             |  9 +++----
 40 files changed, 177 insertions(+), 265 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..4ca3666700 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;
 
     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,8 @@ bhyveStateCleanup(void)
         return -1;
 
     virObjectUnref(bhyve_driver->domains);
-    virObjectUnref(bhyve_driver->caps);
+    if (bhyve_driver->caps)
+        g_object_unref(bhyve_driver->caps);
     virObjectUnref(bhyve_driver->xmlopt);
     virSysinfoDefFree(bhyve_driver->hostsysinfo);
     virObjectUnref(bhyve_driver->closeCallbacks);
@@ -1440,19 +1434,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 +1462,6 @@ bhyveConnectCompareCPU(virConnectPtr conn,
                                xmlDesc, failIncompatible);
     }
 
- cleanup:
-    virObjectUnref(caps);
     return ret;
 }
 
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 2c91461a54..299892fadd 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,9 @@ virCapabilitiesClearSecModel(virCapsHostSecModelPtr secmodel)
 }
 
 static void
-virCapsDispose(void *object)
+virCapsFinalize(GObject *object)
 {
-    virCapsPtr caps = object;
+    virCapsPtr caps = VIR_CAPS(object);
     size_t i;
 
     for (i = 0; i < caps->npools; i++)
@@ -268,6 +252,21 @@ virCapsDispose(void *object)
     virCPUDefFree(caps->host.cpu);
     if (caps->host.resctrl)
         g_object_unref(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->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..bb612e0ec9 100644
--- a/src/conf/storage_capabilities.c
+++ b/src/conf/storage_capabilities.c
@@ -40,7 +40,8 @@ virStoragePoolCapsDispose(void *obj)
     virStoragePoolCapsPtr caps = obj;
     VIR_DEBUG("obj=%p", caps);
 
-    virObjectUnref(caps->driverCaps);
+    if (caps->driverCaps)
+        g_object_unref(caps->driverCaps);
 }
 
 
@@ -66,7 +67,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..f893d112d0 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -69,7 +69,8 @@ esxFreePrivate(esxPrivate **priv)
     esxVI_Context_Free(&(*priv)->host);
     esxVI_Context_Free(&(*priv)->vCenter);
     esxUtil_FreeParsedUri(&(*priv)->parsedUri);
-    virObjectUnref((*priv)->caps);
+    if ((*priv)->caps)
+        g_object_unref((*priv)->caps);
     virObjectUnref((*priv)->xmlopt);
     VIR_FREE(*priv);
 }
@@ -540,7 +541,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 +553,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 +566,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 +579,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/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 be5fc505fe..21089c33d7 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -70,7 +70,8 @@ libxlDriverConfigDispose(void *obj)
 {
     libxlDriverConfigPtr cfg = obj;
 
-    virObjectUnref(cfg->caps);
+    if (cfg->caps)
+        g_object_unref(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..cfb80eaf22 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,8 @@ virCapsPtr virLXCDriverGetCapabilities(virLXCDriverPtr driver,
             return NULL;
 
         lxcDriverLock(driver);
-        virObjectUnref(driver->caps);
+        if (driver->caps)
+            g_object_unref(driver->caps);
         driver->caps = caps;
     } else {
         lxcDriverLock(driver);
@@ -207,7 +205,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..3d3258eabe 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -181,7 +181,8 @@ virLXCControllerDriverFree(virLXCDriverPtr driver)
     if (!driver)
         return;
     virObjectUnref(driver->xmlopt);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virMutexDestroy(&driver->lock);
     g_free(driver);
 }
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 851894c459..1c59ddef6d 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,8 @@ static int lxcStateCleanup(void)
     virSysinfoDefFree(lxc_driver->hostsysinfo);
 
     virObjectUnref(lxc_driver->hostdevMgr);
-    virObjectUnref(lxc_driver->caps);
+    if (lxc_driver->caps)
+        g_object_unref(lxc_driver->caps);
     virObjectUnref(lxc_driver->securityManager);
     virObjectUnref(lxc_driver->xmlopt);
 
@@ -1802,7 +1791,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 +1913,6 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom,
  cleanup:
     virDomainDefFree(persistentDefCopy);
     virDomainObjEndAPI(&vm);
-    virObjectUnref(caps);
     virObjectUnref(cfg);
     return ret;
 }
@@ -4477,7 +4465,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 +4551,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..699accc633 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -1565,7 +1565,8 @@ int virLXCProcessStart(virConnectPtr conn,
     VIR_FREE(pidfile);
     VIR_FREE(logfile);
     virObjectUnref(cfg);
-    virObjectUnref(caps);
+    if (caps)
+        g_object_unref(caps);
 
     virErrorRestore(&err);
 
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index 78547b8b28..1f70bfc3d9 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,8 @@ openvzFreeDriver(struct openvz_driver *driver)
 
     virObjectUnref(driver->xmlopt);
     virObjectUnref(driver->domains);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     VIR_FREE(driver);
 }
 
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 73a8856f34..4bbd14f7ad 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1062,13 +1062,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");
@@ -1096,13 +1094,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 15837cece4..68df8a419a 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1350,7 +1350,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
  */
@@ -1364,7 +1364,8 @@ virCapsPtr virQEMUDriverGetCapabilities(virQEMUDriverPtr driver,
             return NULL;
 
         qemuDriverLock(driver);
-        virObjectUnref(driver->caps);
+        if (driver->caps)
+            g_object_unref(driver->caps);
         driver->caps = caps;
     } else {
         qemuDriverLock(driver);
@@ -1378,7 +1379,8 @@ virCapsPtr virQEMUDriverGetCapabilities(virQEMUDriverPtr driver,
         }
     }
 
-    ret = virObjectRef(driver->caps);
+    if (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 daa3cb397d..99a5058033 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1134,7 +1134,8 @@ qemuStateCleanup(void)
     virObjectUnref(qemu_driver->xmlopt);
     virCPUDefFree(qemu_driver->hostcpu);
     virCapabilitiesHostNUMAUnref(qemu_driver->hostnuma);
-    virObjectUnref(qemu_driver->caps);
+    if (qemu_driver->caps)
+        g_object_unref(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..7b0ae6acad 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -79,7 +79,8 @@ vahDeinit(vahControl * ctl)
         return -1;
 
     VIR_FREE(ctl->def);
-    virObjectUnref(ctl->caps);
+    if (ctl->caps)
+        g_object_unref(ctl->caps);
     virObjectUnref(ctl->xmlopt);
     VIR_FREE(ctl->files);
     VIR_FREE(ctl->virtType);
@@ -632,10 +633,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..00c3d34a81 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -158,7 +158,8 @@ testDriverDispose(void *obj)
     testDriverPtr driver = obj;
     size_t i;
 
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virObjectUnref(driver->xmlopt);
     virObjectUnref(driver->domains);
     virNodeDeviceObjListFree(driver->devs);
@@ -284,24 +285,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 +317,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 +347,7 @@ testBuildCapabilities(virConnectPtr conn)
                                              NULL,
                                              0,
                                              NULL)) == NULL)
-            goto error;
+            return NULL;
 
         if (virCapabilitiesAddGuestDomain(guest,
                                           VIR_DOMAIN_VIRT_TEST,
@@ -355,7 +355,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 +363,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..0e009a5739 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,8 @@ vboxDriverDispose(void *obj)
 {
     vboxDriverPtr driver = obj;
 
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virObjectUnref(driver->xmlopt);
 }
 
diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c
index fd62bb96f7..ee21e96971 100644
--- a/src/vmware/vmware_conf.c
+++ b/src/vmware/vmware_conf.c
@@ -54,7 +54,8 @@ vmwareFreeDriver(struct vmware_driver *driver)
 
     virMutexDestroy(&driver->lock);
     virObjectUnref(driver->domains);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virObjectUnref(driver->xmlopt);
     VIR_FREE(driver->vmrun);
     VIR_FREE(driver);
@@ -64,12 +65,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 +115,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..517259131d 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,8 @@ static void vzDriverDispose(void * obj)
 
     prlsdkDisconnect(driver);
     virObjectUnref(driver->domains);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virObjectUnref(driver->xmlopt);
     virObjectUnref(driver->domainEventState);
     virSysinfoDefFree(driver->hostsysinfo);
diff --git a/tests/bhyveargv2xmltest.c b/tests/bhyveargv2xmltest.c
index 735cc4b338..5de5b48a8e 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_object_unref(driver.caps);
     virObjectUnref(driver.xmlopt);
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c
index 9e7eb218b8..9ea0d76f06 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_object_unref(driver.caps);
     virObjectUnref(driver.xmlopt);
     virPortAllocatorRangeFree(driver.remotePorts);
 
diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c
index a0c20a14c1..cb0f6022c3 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_object_unref(driver.caps);
     virObjectUnref(driver.xmlopt);
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/tests/domainconftest.c b/tests/domainconftest.c
index 754ef2eb3d..ada397b925 100644
--- a/tests/domainconftest.c
+++ b/tests/domainconftest.c
@@ -103,7 +103,8 @@ mymain(void)
     DO_TEST_GET_FS("/dev/pts", false);
     DO_TEST_GET_FS("/doesnotexist", false);
 
-    virObjectUnref(caps);
+    if (caps)
+        g_object_unref(caps);
     virObjectUnref(xmlopt);
 
  cleanup:
diff --git a/tests/genericxml2xmltest.c b/tests/genericxml2xmltest.c
index 501bcdb0a1..0659ca1b28 100644
--- a/tests/genericxml2xmltest.c
+++ b/tests/genericxml2xmltest.c
@@ -195,7 +195,8 @@ mymain(void)
     DO_TEST_BACKUP("backup-push");
     DO_TEST_BACKUP("backup-push-seclabel");
 
-    virObjectUnref(caps);
+    if (caps)
+        g_object_unref(caps);
     virObjectUnref(xmlopt);
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c
index 2b1a9fb5ce..48de2d1c7d 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_object_unref(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..2f28975332 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,8 @@ void
 testLXCDriverFree(virLXCDriverPtr driver)
 {
     virObjectUnref(driver->xmlopt);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(driver->caps);
     virMutexDestroy(&driver->lock);
     g_free(driver);
 }
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index f3b4e2b3b2..082467f58e 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,8 @@ void qemuTestDriverFree(virQEMUDriver *driver)
     }
     virObjectUnref(driver->qemuCapsCache);
     virObjectUnref(driver->xmlopt);
-    virObjectUnref(driver->caps);
+    if (driver->caps)
+        g_object_unref(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..b5c557fe33 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_object_unref(caps);
     virObjectUnref(xmlopt);
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c
index 8f0a2a72a4..d6c7eb4f53 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,9 @@ testCapsInit(void)
     return;
 
  failure:
-    virObjectUnref(caps);
+    if (caps)
+        g_clear_object(&caps);
     virObjectUnref(xmlopt);
-    caps = NULL;
 }
 
 static int
@@ -294,7 +291,7 @@ mymain(void)
 
     DO_TEST("datacenterpath", "datacenterpath", 4);
 
-    virObjectUnref(caps);
+    g_object_unref(caps);
     virObjectUnref(xmlopt);
 
     return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
-- 
2.25.1







[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux