This brings about a couple of benefits:
- use of VIR_AUTOUNREF() simplifies several callers
- Fixes a todo about virDomainMomentObjList not being polymorphic enough
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/conf/moment_conf.h | 5 ++++-
src/conf/snapshot_conf.h | 1 -
cfg.mk | 2 --
src/conf/moment_conf.c | 28 ++++++++++++++++++++++++-
src/conf/snapshot_conf.c | 34 ++++++++++++++++++++++---------
src/conf/virdomainmomentobjlist.c | 3 +--
src/esx/esx_driver.c | 3 +--
src/libvirt_private.syms | 1 -
src/qemu/qemu_driver.c | 5 ++---
src/test/test_driver.c | 5 ++---
src/vbox/vbox_common.c | 6 ++----
src/vz/vz_driver.c | 3 +--
tests/domainsnapshotxml2xmltest.c | 3 +--
13 files changed, 65 insertions(+), 34 deletions(-)
diff --git a/src/conf/moment_conf.h b/src/conf/moment_conf.h
index 04e0c0648b..00ec1c1904 100644
--- a/src/conf/moment_conf.h
+++ b/src/conf/moment_conf.h
@@ -25,9 +25,12 @@
# include "internal.h"
# include "virconftypes.h"
+# include "virobject.h"
/* Base class for a domain moment */
struct _virDomainMomentDef {
+ virObject parent;
+
/* Common portion of public XML. */
char *name;
char *description;
@@ -37,7 +40,7 @@ struct _virDomainMomentDef {
virDomainDefPtr dom;
};
-void virDomainMomentDefClear(virDomainMomentDefPtr def);
+virClassPtr virClassForDomainMomentDef(void);
int virDomainMomentDefPostParse(virDomainMomentDefPtr def);
diff --git a/src/conf/snapshot_conf.h b/src/conf/snapshot_conf.h
index 0ce9dda355..55b7487cfb 100644
--- a/src/conf/snapshot_conf.h
+++ b/src/conf/snapshot_conf.h
@@ -115,7 +115,6 @@ virDomainSnapshotDefPtr virDomainSnapshotDefParseNode(xmlDocPtr xml,
bool *current,
unsigned int flags);
virDomainSnapshotDefPtr virDomainSnapshotDefNew(void);
-void virDomainSnapshotDefFree(virDomainSnapshotDefPtr def);
char *virDomainSnapshotDefFormat(const char *uuidstr,
virDomainSnapshotDefPtr def,
virCapsPtr caps,
diff --git a/cfg.mk b/cfg.mk
index b785089910..786aa6e80a 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -133,7 +133,6 @@ useless_free_options = \
--name=virDomainNetDefFree \
--name=virDomainObjFree \
--name=virDomainSmartcardDefFree \
- --name=virDomainSnapshotDefFree \
--name=virDomainSnapshotObjFree \
--name=virDomainSoundDefFree \
--name=virDomainVideoDefFree \
@@ -211,7 +210,6 @@ useless_free_options = \
# y virDomainInputDefFree
# y virDomainNetDefFree
# y virDomainObjFree
-# y virDomainSnapshotDefFree
# n virDomainSnapshotFree (returns int)
# n virDomainSnapshotFreeName (returns int)
# y virDomainSnapshotObjFree
diff --git a/src/conf/moment_conf.c b/src/conf/moment_conf.c
index 9829775b3c..fea13f0f97 100644
--- a/src/conf/moment_conf.c
+++ b/src/conf/moment_conf.c
@@ -34,8 +34,34 @@
VIR_LOG_INIT("conf.moment_conf");
-void virDomainMomentDefClear(virDomainMomentDefPtr def)
+static virClassPtr virDomainMomentDefClass;
+static void virDomainMomentDefDispose(void *obj);
+
+static int
+virDomainMomentOnceInit(void)
{
+ if (!VIR_CLASS_NEW(virDomainMomentDef, virClassForObject()))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virDomainMoment);
+
+virClassPtr
+virClassForDomainMomentDef(void)
+{
+ if (virDomainMomentInitialize() < 0)
+ return NULL;
+
+ return virDomainMomentDefClass;
+}
+
+static void
+virDomainMomentDefDispose(void *obj)
+{
+ virDomainMomentDefPtr def = obj;
+
VIR_FREE(def->name);
VIR_FREE(def->description);
VIR_FREE(def->parent_name);
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index e5771ae635..c7f29360e7 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -50,6 +50,20 @@
VIR_LOG_INIT("conf.snapshot_conf");
+static virClassPtr virDomainSnapshotDefClass;
+static void virDomainSnapshotDefDispose(void *obj);
+
+static int
+virDomainSnapshotOnceInit(void)
+{
+ if (!VIR_CLASS_NEW(virDomainSnapshotDef, virClassForDomainMomentDef()))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virDomainSnapshot);
+
VIR_ENUM_IMPL(virDomainSnapshotLocation,
VIR_DOMAIN_SNAPSHOT_LOCATION_LAST,
"default",
@@ -81,30 +95,30 @@ virDomainSnapshotDiskDefClear(virDomainSnapshotDiskDefPtr disk)
disk->src = NULL;
}
+/* Allocate a new virDomainSnapshotDef; free with virObjectUnref() */
virDomainSnapshotDefPtr
virDomainSnapshotDefNew(void)
{
virDomainSnapshotDefPtr def;
- ignore_value(VIR_ALLOC(def));
+ if (virDomainSnapshotInitialize() < 0)
+ return NULL;
+
+ def = virObjectNew(virDomainSnapshotDefClass);
return def;
}
-void
-virDomainSnapshotDefFree(virDomainSnapshotDefPtr def)
+static void
+virDomainSnapshotDefDispose(void *obj)
{
+ virDomainSnapshotDefPtr def = obj;
size_t i;
- if (!def)
- return;
-
- virDomainMomentDefClear(&def->parent);
VIR_FREE(def->file);
for (i = 0; i < def->ndisks; i++)
virDomainSnapshotDiskDefClear(&def->disks[i]);
VIR_FREE(def->disks);
virObjectUnref(def->cookie);
- VIR_FREE(def);
}
static int
@@ -374,7 +388,7 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
VIR_FREE(nodes);
VIR_FREE(memorySnapshot);
VIR_FREE(memoryFile);
- virDomainSnapshotDefFree(def);
+ virObjectUnref(def);
return ret;
}
@@ -996,7 +1010,7 @@ virDomainSnapshotRedefinePrep(virDomainPtr domain,
/* Drop and rebuild the parent relationship, but keep all
* child relations by reusing snap. */
virDomainMomentDropParent(other);
- virDomainSnapshotDefFree(otherdef);
+ virObjectUnref(otherdef);
other->def = &(*defptr)->parent;
*defptr = NULL;
*snap = other;
diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c
index e9df66c65b..f56b516343 100644
--- a/src/conf/virdomainmomentobjlist.c
+++ b/src/conf/virdomainmomentobjlist.c
@@ -208,8 +208,7 @@ virDomainMomentObjFree(virDomainMomentObjPtr moment)
VIR_DEBUG("obj=%p", moment);
- /* FIXME: Make this polymorphic by inheriting from virObject */
- virDomainSnapshotDefFree(virDomainSnapshotObjGetDef(moment));
+ virObjectUnref(moment->def);
VIR_FREE(moment);
}
diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index f45d96a4f5..deb800a6b7 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -4081,7 +4081,6 @@ esxDomainSnapshotCreateXML(virDomainPtr domain, const char
*xmlDesc,
unsigned int flags)
{
esxPrivate *priv = domain->conn->privateData;
- virDomainSnapshotDefPtr def = NULL;
esxVI_ObjectContent *virtualMachine = NULL;
esxVI_VirtualMachineSnapshotTree *rootSnapshotList = NULL;
esxVI_VirtualMachineSnapshotTree *snapshotTree = NULL;
@@ -4091,6 +4090,7 @@ esxDomainSnapshotCreateXML(virDomainPtr domain, const char
*xmlDesc,
virDomainSnapshotPtr snapshot = NULL;
bool diskOnly = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) != 0;
bool quiesce = (flags & VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE) != 0;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
/* ESX supports disk-only and quiesced snapshots; libvirt tracks no
* snapshot metadata so supporting that flag is trivial. */
@@ -4151,7 +4151,6 @@ esxDomainSnapshotCreateXML(virDomainPtr domain, const char
*xmlDesc,
snapshot = virGetDomainSnapshot(domain, def->parent.name);
cleanup:
- virDomainSnapshotDefFree(def);
esxVI_ObjectContent_Free(&virtualMachine);
esxVI_VirtualMachineSnapshotTree_Free(&rootSnapshotList);
esxVI_ManagedObjectReference_Free(&task);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0474a4e44c..909975750c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -892,7 +892,6 @@ virSecretEventValueChangedNew;
# conf/snapshot_conf.h
virDomainSnapshotAlignDisks;
virDomainSnapshotDefFormat;
-virDomainSnapshotDefFree;
virDomainSnapshotDefIsExternal;
virDomainSnapshotDefNew;
virDomainSnapshotDefParseString;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 833c1e88ee..2877642c4c 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -481,7 +481,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
snap = virDomainSnapshotAssignDef(vm->snapshots, def);
if (snap == NULL) {
- virDomainSnapshotDefFree(def);
+ virObjectUnref(def);
} else if (cur) {
if (current)
virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -15552,7 +15552,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
char *xml = NULL;
virDomainMomentObjPtr snap = NULL;
virDomainSnapshotPtr snapshot = NULL;
- virDomainSnapshotDefPtr def = NULL;
virDomainMomentObjPtr current = NULL;
bool update_current = true;
bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
@@ -15564,6 +15563,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
virCapsPtr caps = NULL;
qemuDomainObjPrivatePtr priv;
virDomainSnapshotState state;
+ VIR_AUTOFREE(virDomainSnapshotDefPtr) def = NULL;
virCheckFlags(VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE |
VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT |
@@ -15831,7 +15831,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
cleanup:
virDomainObjEndAPI(&vm);
- virDomainSnapshotDefFree(def);
VIR_FREE(xml);
virObjectUnref(caps);
virObjectUnref(cfg);
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index aaf67eaac9..a06d1fc402 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -848,7 +848,7 @@ testParseDomainSnapshots(testDriverPtr privconn,
goto error;
if (!(snap = virDomainSnapshotAssignDef(domobj->snapshots, def))) {
- virDomainSnapshotDefFree(def);
+ virObjectUnref(def);
goto error;
}
@@ -6348,13 +6348,13 @@ testDomainSnapshotCreateXML(virDomainPtr domain,
{
testDriverPtr privconn = domain->conn->privateData;
virDomainObjPtr vm = NULL;
- virDomainSnapshotDefPtr def = NULL;
virDomainMomentObjPtr snap = NULL;
virDomainSnapshotPtr snapshot = NULL;
virObjectEventPtr event = NULL;
bool update_current = true;
bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE;
unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
/*
* DISK_ONLY: Not implemented yet
@@ -6448,7 +6448,6 @@ testDomainSnapshotCreateXML(virDomainPtr domain,
virDomainObjEndAPI(&vm);
}
virObjectEventStateQueue(privconn->eventState, event);
- virDomainSnapshotDefFree(def);
return snapshot;
}
diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index 7e42f6a4fe..54e31bec9d 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -5476,7 +5476,6 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom,
unsigned int flags)
{
vboxDriverPtr data = dom->conn->privateData;
- virDomainSnapshotDefPtr def = NULL;
vboxIID domiid;
IMachine *machine = NULL;
IConsole *console = NULL;
@@ -5488,6 +5487,7 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom,
nsresult rc;
resultCodeUnion result;
virDomainSnapshotPtr ret = NULL;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
if (!data->vboxObj)
return ret;
@@ -5587,7 +5587,6 @@ vboxDomainSnapshotCreateXML(virDomainPtr dom,
gVBoxAPI.UISession.Close(data->vboxSession);
VBOX_RELEASE(machine);
vboxIIDUnalloc(&domiid);
- virDomainSnapshotDefFree(def);
return ret;
}
@@ -6200,7 +6199,6 @@ static char *vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr
snapshot,
ISnapshot *snap = NULL;
ISnapshot *parent = NULL;
nsresult rc;
- virDomainSnapshotDefPtr def = NULL;
PRUnichar *str16;
char *str8;
PRInt64 timestamp;
@@ -6208,6 +6206,7 @@ static char *vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr
snapshot,
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *ret = NULL;
virDomainDefPtr defdom;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
if (!data->vboxObj)
return ret;
@@ -6330,7 +6329,6 @@ static char *vboxDomainSnapshotGetXMLDesc(virDomainSnapshotPtr
snapshot,
ret = virDomainSnapshotDefFormat(uuidstr, def, data->caps, data->xmlopt, 0);
cleanup:
- virDomainSnapshotDefFree(def);
VBOX_RELEASE(parent);
VBOX_RELEASE(snap);
VBOX_RELEASE(machine);
diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
index 419e9d215f..dfd49e7cc7 100644
--- a/src/vz/vz_driver.c
+++ b/src/vz/vz_driver.c
@@ -2576,7 +2576,6 @@ vzDomainSnapshotCreateXML(virDomainPtr domain,
const char *xmlDesc,
unsigned int flags)
{
- virDomainSnapshotDefPtr def = NULL;
virDomainSnapshotPtr snapshot = NULL;
virDomainObjPtr dom;
vzConnPtr privconn = domain->conn->privateData;
@@ -2585,6 +2584,7 @@ vzDomainSnapshotCreateXML(virDomainPtr domain,
virDomainSnapshotObjListPtr snapshots = NULL;
virDomainMomentObjPtr current;
bool job = false;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
virCheckFlags(0, NULL);
@@ -2636,7 +2636,6 @@ vzDomainSnapshotCreateXML(virDomainPtr domain,
cleanup:
virDomainSnapshotObjListFree(snapshots);
- virDomainSnapshotDefFree(def);
if (job)
vzDomainObjEndJob(dom);
virDomainObjEndAPI(&dom);
diff --git a/tests/domainsnapshotxml2xmltest.c b/tests/domainsnapshotxml2xmltest.c
index 18ff2dc34c..c2c7bedb56 100644
--- a/tests/domainsnapshotxml2xmltest.c
+++ b/tests/domainsnapshotxml2xmltest.c
@@ -35,10 +35,10 @@ testCompareXMLToXMLFiles(const char *inxml,
char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
- virDomainSnapshotDefPtr def = NULL;
unsigned int parseflags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
unsigned int formatflags = VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE;
bool cur = false;
+ VIR_AUTOUNREF(virDomainSnapshotDefPtr) def = NULL;
if (flags & TEST_INTERNAL) {
parseflags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
@@ -85,7 +85,6 @@ testCompareXMLToXMLFiles(const char *inxml,
VIR_FREE(inXmlData);
VIR_FREE(outXmlData);
VIR_FREE(actual);
- virDomainSnapshotDefFree(def);
return ret;
}
--
2.20.1