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