From nobody Sun Feb 8 05:27:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1570704885; cv=none; d=zoho.com; s=zohoarc; b=MVxlYNavFiyBJTDERafLPTXPqoQUfTw5vnXGfn9pROadSQSlMpLKBYOicRQgerJMZgUb10WthBVTs/cp7FL99RnA8voKxYUcfEPxjQfbuNRbxYHiGvFPaAjC0fugKOqxztG85BCw5wGEHzM2fq2A1ARLBRjzYvCb501ga/+x8fw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570704885; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=xwzsIxPhOqW8Ukor/nh7QMGQHI608UuqrDZoN8wcWt8=; b=JjspckhIYwA9PC7Ue11kPNE9j5oy/L4/pkA+E07cNjvNc2z3/lcuG8FKNCHNBUyLshAoHFS8PCvRB5HvGUGLnd9VfXk3yWQC8JRmXR13Naozvdtkr7L3hEOsBE4NPnL2MFaImXIp7zK/71RqRn3yepqP4ll0LuDgwyytd8Ver4Q= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1570704885355381.90390211973977; Thu, 10 Oct 2019 03:54:45 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 928E910CC1FF; Thu, 10 Oct 2019 10:54:43 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6BF98600C4; Thu, 10 Oct 2019 10:54:43 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2BE79180B536; Thu, 10 Oct 2019 10:54:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9AAsdGT010498 for ; Thu, 10 Oct 2019 06:54:39 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6B1925C22F; Thu, 10 Oct 2019 10:54:39 +0000 (UTC) Received: from dhcp-16-231.lcy.redhat.com (unknown [10.42.16.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB7E35C1B5; Thu, 10 Oct 2019 10:54:38 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 10 Oct 2019 11:54:09 +0100 Message-Id: <20191010105413.4091-16-berrange@redhat.com> In-Reply-To: <20191010105413.4091-1-berrange@redhat.com> References: <20191010105413.4091-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?J=C3=A1n=20Tomko?= Subject: [libvirt] [PATCH v3 15/19] util: convert virIdentity class to use GObject X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.65]); Thu, 10 Oct 2019 10:54:44 +0000 (UTC) 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, GObjec= t); 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=C3=A1n Tomko Signed-off-by: Daniel P. Berrang=C3=A9 --- m4/virt-glib.m4 | 4 +-- src/qemu/qemu_process.c | 4 +-- src/rpc/virnetserverclient.c | 10 +++---- src/util/viridentity.c | 56 ++++++++++++++++++++++-------------- src/util/viridentity.h | 9 +++--- tests/viridentitytest.c | 5 +--- 6 files changed, 49 insertions(+), 39 deletions(-) diff --git a/m4/virt-glib.m4 b/m4/virt-glib.m4 index 5a5bc19660..eb2c77b25b 100644 --- a/m4/virt-glib.m4 +++ b/m4/virt-glib.m4 @@ -24,10 +24,10 @@ AC_DEFUN([LIBVIRT_ARG_GLIB], [ AC_DEFUN([LIBVIRT_CHECK_GLIB],[ GLIB_REQUIRED=3D2.48.0 =20 - LIBVIRT_CHECK_PKG([GLIB], [glib-2.0], [$GLIB_REQUIRED]) + LIBVIRT_CHECK_PKG([GLIB], [glib-2.0 gobject-2.0], [$GLIB_REQUIRED]) =20 if test "$with_glib" =3D "no" ; then - AC_MSG_ERROR([glib-2.0 >=3D $GLIB_REQUIRED is required for libvirt]) + AC_MSG_ERROR([glib-2.0, gobject-2.0 >=3D $GLIB_REQUIRED are required f= or libvirt]) fi ]) =20 diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index c14c09da11..3b45b2f641 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8040,7 +8040,7 @@ qemuProcessReconnect(void *opaque) bool tryMonReconn =3D false; =20 virIdentitySetCurrent(data->identity); - virObjectUnref(data->identity); + g_clear_object(&data->identity); VIR_FREE(data); =20 qemuDomainObjRestoreJob(obj, &oldjob); @@ -8353,7 +8353,7 @@ qemuProcessReconnectHelper(virDomainObjPtr obj, =20 virDomainObjEndAPI(&obj); virNWFilterUnlockFilterUpdates(); - virObjectUnref(data->identity); + g_clear_object(&data->identity); VIR_FREE(data); return -1; } diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c index 79287572b6..8482c5c29c 100644 --- a/src/rpc/virnetserverclient.c +++ b/src/rpc/virnetserverclient.c @@ -829,7 +829,7 @@ virIdentityPtr virNetServerClientGetIdentity(virNetServ= erClientPtr client) if (!client->identity) client->identity =3D virNetServerClientCreateIdentity(client); if (client->identity) - ret =3D virObjectRef(client->identity); + ret =3D g_object_ref(client->identity); virObjectUnlock(client); return ret; } @@ -839,10 +839,10 @@ void virNetServerClientSetIdentity(virNetServerClient= Ptr client, virIdentityPtr identity) { virObjectLock(client); - virObjectUnref(client->identity); + g_clear_object(&client->identity); client->identity =3D identity; if (client->identity) - virObjectRef(client->identity); + g_object_ref(client->identity); virObjectUnlock(client); } =20 @@ -979,7 +979,7 @@ void virNetServerClientDispose(void *obj) if (client->privateData) client->privateDataFreeFunc(client->privateData); =20 - virObjectUnref(client->identity); + g_clear_object(&client->identity); =20 #if WITH_SASL virObjectUnref(client->sasl); @@ -1674,7 +1674,7 @@ virNetServerClientGetInfo(virNetServerClientPtr clien= t, goto cleanup; } =20 - *identity =3D virObjectRef(client->identity); + *identity =3D g_object_ref(client->identity); =20 ret =3D 0; cleanup: diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 6636077161..8cc2db2568 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -43,25 +43,29 @@ VIR_LOG_INIT("util.identity"); =20 struct _virIdentity { - virObject parent; + GObject parent; =20 int nparams; int maxparams; virTypedParameterPtr params; }; =20 -static virClassPtr virIdentityClass; +G_DEFINE_TYPE(virIdentity, vir_identity, G_TYPE_OBJECT) + static virThreadLocal virIdentityCurrent; =20 -static void virIdentityDispose(void *obj); +static void virIdentityFinalize(GObject *obj); =20 -static int virIdentityOnceInit(void) +static void virIdentityCurrentCleanup(void *ident) { - if (!VIR_CLASS_NEW(virIdentity, virClassForObject())) - return -1; + if (ident) + g_object_unref(ident); +} =20 +static int virIdentityOnceInit(void) +{ if (virThreadLocalInit(&virIdentityCurrent, - (virThreadLocalCleanup)virObjectUnref) < 0) { + virIdentityCurrentCleanup) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot initialize thread local for current ident= ity")); return -1; @@ -72,13 +76,24 @@ static int virIdentityOnceInit(void) =20 VIR_ONCE_GLOBAL_INIT(virIdentity); =20 +static void vir_identity_init(virIdentity *ident G_GNUC_UNUSED) +{ +} + +static void vir_identity_class_init(virIdentityClass *klass) +{ + GObjectClass *obj =3D G_OBJECT_CLASS(klass); + + obj->finalize =3D 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; =20 ident =3D virThreadLocalGet(&virIdentityCurrent); - return virObjectRef(ident); + if (ident) + g_object_ref(ident); + return ident; } =20 =20 @@ -113,10 +130,11 @@ int virIdentitySetCurrent(virIdentityPtr ident) old =3D virThreadLocalGet(&virIdentityCurrent); =20 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; } =20 @@ -197,23 +215,17 @@ virIdentityPtr virIdentityGetSystem(void) */ virIdentityPtr virIdentityNew(void) { - virIdentityPtr ident; - - if (virIdentityInitialize() < 0) - return NULL; - - if (!(ident =3D virObjectNew(virIdentityClass))) - return NULL; - - return ident; + return VIR_IDENTITY(g_object_new(VIR_TYPE_IDENTITY, NULL)); } =20 =20 -static void virIdentityDispose(void *object) +static void virIdentityFinalize(GObject *object) { - virIdentityPtr ident =3D object; + virIdentityPtr ident =3D VIR_IDENTITY(object); =20 virTypedParamsFree(ident->params, ident->nparams); + + G_OBJECT_CLASS(vir_identity_parent_class)->finalize(object); } =20 =20 diff --git a/src/util/viridentity.h b/src/util/viridentity.h index 7513dd4e35..2940e02054 100644 --- a/src/util/viridentity.h +++ b/src/util/viridentity.h @@ -21,12 +21,13 @@ =20 #pragma once =20 -#include "virobject.h" +#include "internal.h" +#include =20 -typedef struct _virIdentity virIdentity; -typedef virIdentity *virIdentityPtr; +#define VIR_TYPE_IDENTITY vir_identity_get_type() +G_DECLARE_FINAL_TYPE(virIdentity, vir_identity, VIR, IDENTITY, GObject); =20 -G_DEFINE_AUTOPTR_CLEANUP_FUNC(virIdentity, virObjectUnref); +typedef virIdentity *virIdentityPtr; =20 virIdentityPtr virIdentityGetCurrent(void); int virIdentitySetCurrent(virIdentityPtr ident); diff --git a/tests/viridentitytest.c b/tests/viridentitytest.c index db041a98a8..90e7247817 100644 --- a/tests/viridentitytest.c +++ b/tests/viridentitytest.c @@ -38,13 +38,10 @@ VIR_LOG_INIT("tests.identitytest"); =20 static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED) { - g_autoptr(virIdentity) ident =3D NULL; + g_autoptr(virIdentity) ident =3D virIdentityNew(); const char *val; int rc; =20 - if (!(ident =3D virIdentityNew())) - return -1; - if (virIdentitySetUserName(ident, "fred") < 0) return -1; =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list