Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/conf/virdomainsnapshotobjlist.c | 7 ++--- src/datatypes.c | 44 ++++++++++++++++++----------- src/datatypes.h | 12 ++++++-- src/libvirt-domain-snapshot.c | 7 ++--- src/libvirt_private.syms | 2 +- src/qemu/qemu_driver.c | 3 +- src/rpc/gendispatch.pl | 1 + 7 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/conf/virdomainsnapshotobjlist.c b/src/conf/virdomainsnapshotobjlist.c index 910b0714c7..54d1892dc4 100644 --- a/src/conf/virdomainsnapshotobjlist.c +++ b/src/conf/virdomainsnapshotobjlist.c @@ -285,10 +285,7 @@ virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots, for (i = 0; i < count; i++) VIR_FREE(names[i]); VIR_FREE(names); - if (ret < 0 && list) { - for (i = 0; i < count; i++) - virObjectUnref(list[i]); - VIR_FREE(list); - } + if (ret < 0) + virGObjectListFreeCount(list, count); return ret; } diff --git a/src/datatypes.c b/src/datatypes.c index 0f3365a10a..504e045bd6 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -36,7 +36,6 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; -virClassPtr virDomainSnapshotClass; virClassPtr virInterfaceClass; virClassPtr virNetworkClass; virClassPtr virNetworkPortClass; @@ -51,7 +50,6 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); -static void virDomainSnapshotDispose(void *obj); static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); static void virNetworkPortDispose(void *obj); @@ -79,6 +77,22 @@ vir_domain_checkpoint_class_init(virDomainCheckpointClass *klass) obj->finalize = virDomainCheckpointFinalize; } +G_DEFINE_TYPE(virDomainSnapshot, vir_domain_snapshot, G_TYPE_OBJECT); +static void virDomainSnapshotFinalize(GObject *obj); + +static void +vir_domain_snapshot_init(virDomainSnapshot *ds G_GNUC_UNUSED) +{ +} + +static void +vir_domain_snapshot_class_init(virDomainSnapshotClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->finalize = virDomainSnapshotFinalize; +} + virClassPtr virAdmConnectClass; virClassPtr virAdmConnectCloseCallbackDataClass; @@ -131,7 +145,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); - DECLARE_CLASS(virDomainSnapshot); DECLARE_CLASS(virInterface); DECLARE_CLASS(virNetwork); DECLARE_CLASS(virNetworkPort); @@ -1051,37 +1064,32 @@ virDomainCheckpointFinalize(GObject *obj) * @name: pointer to the domain snapshot name * * Allocates a new domain snapshot object. When the object is no longer needed, - * virObjectUnref() must be called in order to not leak data. + * g_object_unref() must be called in order to not leak data. * * Returns a pointer to the domain snapshot object, or NULL on error. */ virDomainSnapshotPtr virGetDomainSnapshot(virDomainPtr domain, const char *name) { - virDomainSnapshotPtr ret = NULL; + g_autoptr(virDomainSnapshot) ret = NULL; if (virDataTypesInitialize() < 0) return NULL; - virCheckDomainGoto(domain, error); - virCheckNonNullArgGoto(name, error); + virCheckDomainReturn(domain, NULL); + virCheckNonNullArgReturn(name, NULL); - if (!(ret = virObjectNew(virDomainSnapshotClass))) - goto error; + ret = VIR_DOMAIN_SNAPSHOT(g_object_new(VIR_TYPE_DOMAIN_SNAPSHOT, NULL)); ret->name = g_strdup(name); ret->domain = virObjectRef(domain); - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } /** - * virDomainSnapshotDispose: + * virDomainSnapshotFinalize: * @obj: the domain snapshot to release * * Unconditionally release all memory associated with a snapshot. @@ -1091,13 +1099,15 @@ virGetDomainSnapshot(virDomainPtr domain, const char *name) * which may also be released if its ref count hits zero. */ static void -virDomainSnapshotDispose(void *obj) +virDomainSnapshotFinalize(GObject *obj) { - virDomainSnapshotPtr snapshot = obj; + virDomainSnapshotPtr snapshot = VIR_DOMAIN_SNAPSHOT(obj); VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name); VIR_FREE(snapshot->name); virObjectUnref(snapshot->domain); + + G_OBJECT_CLASS(vir_domain_snapshot_parent_class)->finalize(obj); } diff --git a/src/datatypes.h b/src/datatypes.h index e6817dc43d..9dfb18147e 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -32,7 +32,6 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; -extern virClassPtr virDomainSnapshotClass; extern virClassPtr virInterfaceClass; extern virClassPtr virNetworkClass; extern virClassPtr virNetworkPortClass; @@ -51,6 +50,13 @@ G_DECLARE_FINAL_TYPE(virDomainCheckpoint, DOMAIN_CHECKPOINT, GObject); +#define VIR_TYPE_DOMAIN_SNAPSHOT vir_domain_snapshot_get_type() +G_DECLARE_FINAL_TYPE(virDomainSnapshot, + vir_domain_snapshot, + VIR, + DOMAIN_SNAPSHOT, + GObject); + extern virClassPtr virAdmConnectClass; #define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() @@ -349,7 +355,7 @@ G_DECLARE_FINAL_TYPE(virAdmClient, vir_adm_client, VIR, ADM_CLIENT, GObject); #define virCheckDomainSnapshotReturn(obj, retval) \ do { \ virDomainSnapshotPtr _snap = (obj); \ - if (!virObjectIsClass(_snap, virDomainSnapshotClass) || \ + if (_snap == NULL || \ !virObjectIsClass(_snap->domain, virDomainClass) || \ !virObjectIsClass(_snap->domain->conn, virConnectClass)) { \ virReportErrorHelper(VIR_FROM_DOMAIN_SNAPSHOT, \ @@ -767,7 +773,7 @@ struct _virDomainCheckpoint { * Internal structure associated with a domain snapshot */ struct _virDomainSnapshot { - virObject parent; + GObject parent; char *name; virDomainPtr domain; }; diff --git a/src/libvirt-domain-snapshot.c b/src/libvirt-domain-snapshot.c index 33593e11e9..ce870de712 100644 --- a/src/libvirt-domain-snapshot.c +++ b/src/libvirt-domain-snapshot.c @@ -1080,14 +1080,13 @@ virDomainSnapshotDelete(virDomainSnapshotPtr snapshot, int virDomainSnapshotRef(virDomainSnapshotPtr snapshot) { - VIR_DEBUG("snapshot=%p, refs=%d", snapshot, - snapshot ? snapshot->parent.u.s.refs : 0); + VIR_DEBUG("snapshot=%p", snapshot); virResetLastError(); virCheckDomainSnapshotReturn(snapshot, -1); - virObjectRef(snapshot); + g_object_ref(snapshot); return 0; } @@ -1110,6 +1109,6 @@ virDomainSnapshotFree(virDomainSnapshotPtr snapshot) virCheckDomainSnapshotReturn(snapshot, -1); - virObjectUnref(snapshot); + g_object_unref(snapshot); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d47714f067..be779a2d33 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1357,6 +1357,7 @@ virCPUx86FeatureFilterSelectMSR; # datatypes.h vir_domain_checkpoint_get_type; +vir_domain_snapshot_get_type; virConnectClass; virConnectCloseCallbackDataCall; virConnectCloseCallbackDataClass; @@ -1364,7 +1365,6 @@ virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; virDomainClass; -virDomainSnapshotClass; virGetConnect; virGetDomain; virGetDomainCheckpoint; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 13d3948bd6..26b7450e84 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15848,8 +15848,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, cfg->snapshotDir) < 0) { /* if writing of metadata fails, error out rather than trying * to silently carry on without completing the snapshot */ - virObjectUnref(snapshot); - snapshot = NULL; + g_clear_object(&snapshot); virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to save metadata for snapshot %s"), snap->def->name); diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index c8fba3dc2b..bca3b33e80 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -197,6 +197,7 @@ my %gobject_impl = ( virAdmClient => 1, virAdmServer => 1, virDomainCheckpoint => 1, + virDomainSnapshot => 1, ); sub use_gobject { -- 2.26.2