Signed-off-by: Rafael Fonseca <r4f4rfs@xxxxxxxxx> --- src/admin/libvirt-admin.c | 2 +- src/admin/libvirt_admin_private.syms | 2 +- src/datatypes.c | 56 ++++++++++++++++++++-------- src/datatypes.h | 20 ++++++---- src/rpc/gendispatch.pl | 1 + 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/admin/libvirt-admin.c b/src/admin/libvirt-admin.c index 835b5560d2..eccad1d0fc 100644 --- a/src/admin/libvirt-admin.c +++ b/src/admin/libvirt-admin.c @@ -586,7 +586,7 @@ int virAdmServerFree(virAdmServerPtr srv) virCheckAdmServerReturn(srv, -1); - virObjectUnref(srv); + g_clear_object(&srv); return 0; } diff --git a/src/admin/libvirt_admin_private.syms b/src/admin/libvirt_admin_private.syms index 157a45341e..cfdaba330d 100644 --- a/src/admin/libvirt_admin_private.syms +++ b/src/admin/libvirt_admin_private.syms @@ -34,13 +34,13 @@ xdr_admin_server_set_threadpool_parameters_args; xdr_admin_server_update_tls_files_args; # datatypes.h +vir_adm_server_get_type; virAdmClientClass; virAdmConnectClass; virAdmConnectCloseCallbackDataRegister; virAdmConnectCloseCallbackDataReset; virAdmConnectCloseCallbackDataUnregister; virAdmGetServer; -virAdmServerClass; # Let emacs know we want case-insensitive sorting # Local Variables: diff --git a/src/datatypes.c b/src/datatypes.c index 552115c7a3..cfdb9f9650 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -71,11 +71,27 @@ virClassPtr virAdmConnectCloseCallbackDataClass; static void virAdmConnectDispose(void *obj); static void virAdmConnectCloseCallbackDataDispose(void *obj); -virClassPtr virAdmServerClass; virClassPtr virAdmClientClass; -static void virAdmServerDispose(void *obj); static void virAdmClientDispose(void *obj); +G_DEFINE_TYPE(virAdmServer, vir_adm_server, G_TYPE_OBJECT); +static void virAdmServerDispose(GObject *obj); +static void virAdmServerFinalize(GObject *obj); + +static void +vir_adm_server_init(virAdmServer *srv G_GNUC_UNUSED) +{ +} + +static void +vir_adm_server_class_init(virAdmServerClass *klass) +{ + GObjectClass *obj = G_OBJECT_CLASS(klass); + + obj->dispose = virAdmServerDispose; + obj->finalize = virAdmServerFinalize; +} + static int virDataTypesOnceInit(void) { @@ -105,7 +121,6 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virAdmConnect); DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData); - DECLARE_CLASS(virAdmServer); DECLARE_CLASS(virAdmClient); #undef DECLARE_CLASS_COMMON @@ -1173,31 +1188,40 @@ virAdmConnectCloseCallbackDataRegister(virAdmConnectCloseCallbackDataPtr cbdata, virAdmServerPtr virAdmGetServer(virAdmConnectPtr conn, const char *name) { - virAdmServerPtr ret = NULL; + g_autoptr(virAdmServer) ret = NULL; if (virDataTypesInitialize() < 0) - goto error; + return NULL; + + ret = VIR_ADM_SERVER(g_object_new(VIR_TYPE_ADM_SERVER, NULL)); - if (!(ret = virObjectNew(virAdmServerClass))) - goto error; ret->name = g_strdup(name); ret->conn = virObjectRef(conn); - return ret; - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } static void -virAdmServerDispose(void *obj) +virAdmServerDispose(GObject *obj) { - virAdmServerPtr srv = obj; + virAdmServerPtr srv = VIR_ADM_SERVER(obj); + + virObjectUnref(srv->conn); + srv->conn = NULL; + + G_OBJECT_CLASS(vir_adm_server_parent_class)->dispose(obj); +} + +static void +virAdmServerFinalize(GObject *obj) +{ + virAdmServerPtr srv = VIR_ADM_SERVER(obj); VIR_DEBUG("release server srv=%p name=%s", srv, srv->name); VIR_FREE(srv->name); - virObjectUnref(srv->conn); + + G_OBJECT_CLASS(vir_adm_server_parent_class)->finalize(obj); } virAdmClientPtr @@ -1215,7 +1239,7 @@ virAdmGetClient(virAdmServerPtr srv, const unsigned long long id, ret->id = id; ret->timestamp = timestamp; ret->transport = transport; - ret->srv = virObjectRef(srv); + ret->srv = g_object_ref(srv); return ret; error: @@ -1229,5 +1253,5 @@ virAdmClientDispose(void *obj) virAdmClientPtr clt = obj; VIR_DEBUG("release client clt=%p, id=%llu", clt, clt->id); - virObjectUnref(clt->srv); + g_clear_object(&clt->srv); } diff --git a/src/datatypes.h b/src/datatypes.h index 2d0407f7ec..dee21068b3 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -28,6 +28,8 @@ #include "virobject.h" #include "viruuid.h" +#include <glib-object.h> + extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; extern virClassPtr virDomainCheckpointClass; @@ -44,9 +46,11 @@ extern virClassPtr virStorageVolClass; extern virClassPtr virStoragePoolClass; extern virClassPtr virAdmConnectClass; -extern virClassPtr virAdmServerClass; extern virClassPtr virAdmClientClass; +#define VIR_TYPE_ADM_SERVER vir_adm_server_get_type() +G_DECLARE_FINAL_TYPE(virAdmServer, vir_adm_server, VIR, ADM_SERVER, GObject); + #define virCheckConnectReturn(obj, retval) \ do { \ if (!virObjectIsClass(obj, virConnectClass)) { \ @@ -419,8 +423,8 @@ extern virClassPtr virAdmClientClass; #define virCheckAdmServerReturn(obj, retval) \ do { \ - virAdmServerPtr _srv = (obj); \ - if (!virObjectIsClass(_srv, virAdmServerClass) || \ + virAdmServerPtr _srv = VIR_ADM_SERVER(obj); \ + if (_srv == NULL || \ !virObjectIsClass(_srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -431,8 +435,8 @@ extern virClassPtr virAdmClientClass; } while (0) #define virCheckAdmServerGoto(obj, label) \ do { \ - virAdmServerPtr _srv = (obj); \ - if (!virObjectIsClass(_srv, virAdmServerClass) || \ + virAdmServerPtr _srv = VIR_ADM_SERVER(obj); \ + if (_srv == NULL || \ !virObjectIsClass(_srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -445,7 +449,7 @@ extern virClassPtr virAdmClientClass; do { \ virAdmClientPtr _clt = (obj); \ if (!virObjectIsClass(_clt, virAdmClientClass) || \ - !virObjectIsClass(_clt->srv, virAdmServerClass) || \ + !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -458,7 +462,7 @@ extern virClassPtr virAdmClientClass; do { \ virAdmClientPtr _clt = (obj); \ if (!virObjectIsClass(_clt, virAdmClientClass) || \ - !virObjectIsClass(_clt->srv, virAdmServerClass) || \ + !VIR_IS_ADM_SERVER(_clt->srv) || \ !virObjectIsClass(_clt->srv->conn, virAdmConnectClass)) { \ virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \ __FILE__, __FUNCTION__, __LINE__, \ @@ -573,7 +577,7 @@ struct _virAdmConnect { * Internal structure associated to a daemon server */ struct _virAdmServer { - virObject parent; + GObject parent; virAdmConnectPtr conn; /* pointer back to the admin connection */ char *name; /* the server external name */ }; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index bc32246618..a9152198d7 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -194,6 +194,7 @@ sub get_conn_method { } my %gobject_impl = ( + virAdmServer => 1, ); sub use_gobject { -- 2.25.3