mirror of https://gitee.com/openkylin/libvirt.git
util: convert virIdentity class to use GObject
Converting from virObject to GObject is reasonably straightforward, as illustrated by this patch for virIdentity In the header file - Remove typedef struct _virIdentity virIdentity - Add #define VIR_TYPE_IDENTITY virIdentity_get_type () G_DECLARE_FINAL_TYPE (virIdentity, vir_identity, VIR, IDENTITY, GObject); Which provides the typedef we just removed, and class declaration boilerplate and various other constants/macros. In the source file - Change 'virObject parent' to 'GObject parent' in the struct - Remove the virClass variable and its initializing call - Add G_DEFINE_TYPE(virIdentity, vir_identity, G_TYPE_OBJECT) which declares the instance & class constructor functions - Add an impl of the instance & class constructors wiring up the finalize method to point to our dispose impl In all files - Replace VIR_AUTOUNREF(virIdentityPtr) with g_autoptr(virIdentity) - Replace virObjectRef/Unref with g_object_ref/unref. Note the latter functions do *NOT* accept a NULL object where as libvirt's do. If you replace g_object_unref with g_clear_object it is NULL safe, but also clears the pointer. Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
b74a95d6a2
commit
16121a88a7
|
@ -24,10 +24,10 @@ AC_DEFUN([LIBVIRT_ARG_GLIB], [
|
|||
AC_DEFUN([LIBVIRT_CHECK_GLIB],[
|
||||
GLIB_REQUIRED=2.48.0
|
||||
|
||||
LIBVIRT_CHECK_PKG([GLIB], [glib-2.0], [$GLIB_REQUIRED])
|
||||
LIBVIRT_CHECK_PKG([GLIB], [glib-2.0 gobject-2.0], [$GLIB_REQUIRED])
|
||||
|
||||
if test "$with_glib" = "no" ; then
|
||||
AC_MSG_ERROR([glib-2.0 >= $GLIB_REQUIRED is required for libvirt])
|
||||
AC_MSG_ERROR([glib-2.0, gobject-2.0 >= $GLIB_REQUIRED are required for libvirt])
|
||||
fi
|
||||
])
|
||||
|
||||
|
|
|
@ -8040,7 +8040,7 @@ qemuProcessReconnect(void *opaque)
|
|||
bool tryMonReconn = false;
|
||||
|
||||
virIdentitySetCurrent(data->identity);
|
||||
virObjectUnref(data->identity);
|
||||
g_clear_object(&data->identity);
|
||||
VIR_FREE(data);
|
||||
|
||||
qemuDomainObjRestoreJob(obj, &oldjob);
|
||||
|
@ -8353,7 +8353,7 @@ qemuProcessReconnectHelper(virDomainObjPtr obj,
|
|||
|
||||
virDomainObjEndAPI(&obj);
|
||||
virNWFilterUnlockFilterUpdates();
|
||||
virObjectUnref(data->identity);
|
||||
g_clear_object(&data->identity);
|
||||
VIR_FREE(data);
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -829,7 +829,7 @@ virIdentityPtr virNetServerClientGetIdentity(virNetServerClientPtr client)
|
|||
if (!client->identity)
|
||||
client->identity = virNetServerClientCreateIdentity(client);
|
||||
if (client->identity)
|
||||
ret = virObjectRef(client->identity);
|
||||
ret = g_object_ref(client->identity);
|
||||
virObjectUnlock(client);
|
||||
return ret;
|
||||
}
|
||||
|
@ -839,10 +839,10 @@ void virNetServerClientSetIdentity(virNetServerClientPtr client,
|
|||
virIdentityPtr identity)
|
||||
{
|
||||
virObjectLock(client);
|
||||
virObjectUnref(client->identity);
|
||||
g_clear_object(&client->identity);
|
||||
client->identity = identity;
|
||||
if (client->identity)
|
||||
virObjectRef(client->identity);
|
||||
g_object_ref(client->identity);
|
||||
virObjectUnlock(client);
|
||||
}
|
||||
|
||||
|
@ -979,7 +979,7 @@ void virNetServerClientDispose(void *obj)
|
|||
if (client->privateData)
|
||||
client->privateDataFreeFunc(client->privateData);
|
||||
|
||||
virObjectUnref(client->identity);
|
||||
g_clear_object(&client->identity);
|
||||
|
||||
#if WITH_SASL
|
||||
virObjectUnref(client->sasl);
|
||||
|
@ -1674,7 +1674,7 @@ virNetServerClientGetInfo(virNetServerClientPtr client,
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
*identity = virObjectRef(client->identity);
|
||||
*identity = g_object_ref(client->identity);
|
||||
|
||||
ret = 0;
|
||||
cleanup:
|
||||
|
|
|
@ -43,25 +43,29 @@
|
|||
VIR_LOG_INIT("util.identity");
|
||||
|
||||
struct _virIdentity {
|
||||
virObject parent;
|
||||
GObject parent;
|
||||
|
||||
int nparams;
|
||||
int maxparams;
|
||||
virTypedParameterPtr params;
|
||||
};
|
||||
|
||||
static virClassPtr virIdentityClass;
|
||||
G_DEFINE_TYPE(virIdentity, vir_identity, G_TYPE_OBJECT)
|
||||
|
||||
static virThreadLocal virIdentityCurrent;
|
||||
|
||||
static void virIdentityDispose(void *obj);
|
||||
static void virIdentityFinalize(GObject *obj);
|
||||
|
||||
static void virIdentityCurrentCleanup(void *ident)
|
||||
{
|
||||
if (ident)
|
||||
g_object_unref(ident);
|
||||
}
|
||||
|
||||
static int virIdentityOnceInit(void)
|
||||
{
|
||||
if (!VIR_CLASS_NEW(virIdentity, virClassForObject()))
|
||||
return -1;
|
||||
|
||||
if (virThreadLocalInit(&virIdentityCurrent,
|
||||
(virThreadLocalCleanup)virObjectUnref) < 0) {
|
||||
virIdentityCurrentCleanup) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("Cannot initialize thread local for current identity"));
|
||||
return -1;
|
||||
|
@ -72,13 +76,24 @@ static int virIdentityOnceInit(void)
|
|||
|
||||
VIR_ONCE_GLOBAL_INIT(virIdentity);
|
||||
|
||||
static void vir_identity_init(virIdentity *ident G_GNUC_UNUSED)
|
||||
{
|
||||
}
|
||||
|
||||
static void vir_identity_class_init(virIdentityClass *klass)
|
||||
{
|
||||
GObjectClass *obj = G_OBJECT_CLASS(klass);
|
||||
|
||||
obj->finalize = virIdentityFinalize;
|
||||
}
|
||||
|
||||
/**
|
||||
* virIdentityGetCurrent:
|
||||
*
|
||||
* Get the current identity associated with this thread. The
|
||||
* caller will own a reference to the returned identity, but
|
||||
* must not modify the object in any way, other than to
|
||||
* release the reference when done with virObjectUnref
|
||||
* release the reference when done with g_object_unref
|
||||
*
|
||||
* Returns: a reference to the current identity, or NULL
|
||||
*/
|
||||
|
@ -90,7 +105,9 @@ virIdentityPtr virIdentityGetCurrent(void)
|
|||
return NULL;
|
||||
|
||||
ident = virThreadLocalGet(&virIdentityCurrent);
|
||||
return virObjectRef(ident);
|
||||
if (ident)
|
||||
g_object_ref(ident);
|
||||
return ident;
|
||||
}
|
||||
|
||||
|
||||
|
@ -113,10 +130,11 @@ int virIdentitySetCurrent(virIdentityPtr ident)
|
|||
old = virThreadLocalGet(&virIdentityCurrent);
|
||||
|
||||
if (virThreadLocalSet(&virIdentityCurrent,
|
||||
virObjectRef(ident)) < 0) {
|
||||
ident ? g_object_ref(ident) : NULL) < 0) {
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||
_("Unable to set thread local identity"));
|
||||
virObjectUnref(ident);
|
||||
if (ident)
|
||||
g_object_unref(ident);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -197,23 +215,17 @@ virIdentityPtr virIdentityGetSystem(void)
|
|||
*/
|
||||
virIdentityPtr virIdentityNew(void)
|
||||
{
|
||||
virIdentityPtr ident;
|
||||
|
||||
if (virIdentityInitialize() < 0)
|
||||
return NULL;
|
||||
|
||||
if (!(ident = virObjectNew(virIdentityClass)))
|
||||
return NULL;
|
||||
|
||||
return ident;
|
||||
return VIR_IDENTITY(g_object_new(VIR_TYPE_IDENTITY, NULL));
|
||||
}
|
||||
|
||||
|
||||
static void virIdentityDispose(void *object)
|
||||
static void virIdentityFinalize(GObject *object)
|
||||
{
|
||||
virIdentityPtr ident = object;
|
||||
virIdentityPtr ident = VIR_IDENTITY(object);
|
||||
|
||||
virTypedParamsFree(ident->params, ident->nparams);
|
||||
|
||||
G_OBJECT_CLASS(vir_identity_parent_class)->finalize(object);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -21,13 +21,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "virobject.h"
|
||||
#include "internal.h"
|
||||
#include <glib-object.h>
|
||||
|
||||
#define VIR_TYPE_IDENTITY vir_identity_get_type()
|
||||
G_DECLARE_FINAL_TYPE(virIdentity, vir_identity, VIR, IDENTITY, GObject);
|
||||
|
||||
typedef struct _virIdentity virIdentity;
|
||||
typedef virIdentity *virIdentityPtr;
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virIdentity, virObjectUnref);
|
||||
|
||||
virIdentityPtr virIdentityGetCurrent(void);
|
||||
int virIdentitySetCurrent(virIdentityPtr ident);
|
||||
|
||||
|
|
|
@ -38,13 +38,10 @@ VIR_LOG_INIT("tests.identitytest");
|
|||
|
||||
static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED)
|
||||
{
|
||||
g_autoptr(virIdentity) ident = NULL;
|
||||
g_autoptr(virIdentity) ident = virIdentityNew();
|
||||
const char *val;
|
||||
int rc;
|
||||
|
||||
if (!(ident = virIdentityNew()))
|
||||
return -1;
|
||||
|
||||
if (virIdentitySetUserName(ident, "fred") < 0)
|
||||
return -1;
|
||||
|
||||
|
|
Loading…
Reference in New Issue