From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398652046654.196297968226; Fri, 2 Jun 2017 03:17:32 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 637FC80C0E; Fri, 2 Jun 2017 10:17:29 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2672677EAA; Fri, 2 Jun 2017 10:17:29 +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 98E4841F51; Fri, 2 Jun 2017 10:17:27 +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 v52AHQEa027889 for ; Fri, 2 Jun 2017 06:17:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 58A6518216; Fri, 2 Jun 2017 10:17:26 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 159E94DA1D for ; Fri, 2 Jun 2017 10:17:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 637FC80C0E Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 637FC80C0E From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:15 -0400 Message-Id: <20170602101722.12157-2-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 1/8] util: Formatting cleanups to virobject API 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: , MIME-Version: 1.0 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 02 Jun 2017 10:17:30 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Alter to use more recent formatting guidelines Signed-off-by: John Ferlan --- src/util/virobject.c | 64 ++++++++++++++++++++++++++++++++++--------------= ---- src/util/virobject.h | 59 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 82 insertions(+), 41 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 51876b9..792685b 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -52,7 +52,8 @@ static virClassPtr virObjectLockableClass; =20 static void virObjectLockableDispose(void *anyobj); =20 -static int virObjectOnceInit(void) +static int +virObjectOnceInit(void) { if (!(virObjectClass =3D virClassNew(NULL, "virObject", @@ -77,7 +78,8 @@ VIR_ONCE_GLOBAL_INIT(virObject); * * Returns the class instance for the base virObject type */ -virClassPtr virClassForObject(void) +virClassPtr +virClassForObject(void) { if (virObjectInitialize() < 0) return NULL; @@ -91,7 +93,8 @@ virClassPtr virClassForObject(void) * * Returns the class instance for the virObjectLockable type */ -virClassPtr virClassForObjectLockable(void) +virClassPtr +virClassForObjectLockable(void) { if (virObjectInitialize() < 0) return NULL; @@ -116,10 +119,11 @@ virClassPtr virClassForObjectLockable(void) * * Returns a new class instance */ -virClassPtr virClassNew(virClassPtr parent, - const char *name, - size_t objectSize, - virObjectDisposeCallback dispose) +virClassPtr +virClassNew(virClassPtr parent, + const char *name, + size_t objectSize, + virObjectDisposeCallback dispose) { virClassPtr klass; =20 @@ -162,8 +166,9 @@ virClassPtr virClassNew(virClassPtr parent, * * Return true if @klass is derived from @parent, false otherwise */ -bool virClassIsDerivedFrom(virClassPtr klass, - virClassPtr parent) +bool +virClassIsDerivedFrom(virClassPtr klass, + virClassPtr parent) { while (klass) { if (klass->magic =3D=3D parent->magic) @@ -186,7 +191,8 @@ bool virClassIsDerivedFrom(virClassPtr klass, * * Returns the new object */ -void *virObjectNew(virClassPtr klass) +void * +virObjectNew(virClassPtr klass) { virObjectPtr obj =3D NULL; =20 @@ -205,7 +211,8 @@ void *virObjectNew(virClassPtr klass) } =20 =20 -void *virObjectLockableNew(virClassPtr klass) +void * +virObjectLockableNew(virClassPtr klass) { virObjectLockablePtr obj; =20 @@ -230,13 +237,15 @@ void *virObjectLockableNew(virClassPtr klass) } =20 =20 -static void virObjectLockableDispose(void *anyobj) +static void +virObjectLockableDispose(void *anyobj) { virObjectLockablePtr obj =3D anyobj; =20 virMutexDestroy(&obj->lock); } =20 + /** * virObjectUnref: * @anyobj: any instance of virObjectPtr @@ -248,7 +257,8 @@ static void virObjectLockableDispose(void *anyobj) * Returns true if the remaining reference count is * non-zero, false if the object was disposed of */ -bool virObjectUnref(void *anyobj) +bool +virObjectUnref(void *anyobj) { virObjectPtr obj =3D anyobj; =20 @@ -286,7 +296,8 @@ bool virObjectUnref(void *anyobj) * * Returns @anyobj */ -void *virObjectRef(void *anyobj) +void * +virObjectRef(void *anyobj) { virObjectPtr obj =3D anyobj; =20 @@ -310,7 +321,8 @@ void *virObjectRef(void *anyobj) * The object must be unlocked before releasing this * reference. */ -void virObjectLock(void *anyobj) +void +virObjectLock(void *anyobj) { virObjectLockablePtr obj =3D anyobj; =20 @@ -331,7 +343,8 @@ void virObjectLock(void *anyobj) * Release a lock on @anyobj. The lock must have been * acquired by virObjectLock. */ -void virObjectUnlock(void *anyobj) +void +virObjectUnlock(void *anyobj) { virObjectLockablePtr obj =3D anyobj; =20 @@ -355,8 +368,9 @@ void virObjectUnlock(void *anyobj) * * Returns true if @anyobj is an instance of @klass */ -bool virObjectIsClass(void *anyobj, - virClassPtr klass) +bool +virObjectIsClass(void *anyobj, + virClassPtr klass) { virObjectPtr obj =3D anyobj; if (!obj) @@ -372,7 +386,8 @@ bool virObjectIsClass(void *anyobj, * * Returns the name of @klass */ -const char *virClassName(virClassPtr klass) +const char * +virClassName(virClassPtr klass) { return klass->name; } @@ -401,7 +416,9 @@ void virObjectFreeCallback(void *opaque) * but with the signature matching the virHashDataFree * typedef. */ -void virObjectFreeHashData(void *opaque, const void *name ATTRIBUTE_UNUSED) +void +virObjectFreeHashData(void *opaque, + const void *name ATTRIBUTE_UNUSED) { virObjectUnref(opaque); } @@ -413,7 +430,8 @@ void virObjectFreeHashData(void *opaque, const void *na= me ATTRIBUTE_UNUSED) * * Unrefs all members of @list and frees the list itself. */ -void virObjectListFree(void *list) +void +virObjectListFree(void *list) { void **next; =20 @@ -434,7 +452,9 @@ void virObjectListFree(void *list) * * Unrefs all members of @list and frees the list itself. */ -void virObjectListFreeCount(void *list, size_t count) +void +virObjectListFreeCount(void *list, + size_t count) { size_t i; =20 diff --git a/src/util/virobject.h b/src/util/virobject.h index c3ecc1e..f4c292b 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -66,40 +66,61 @@ virClassPtr virClassForObjectLockable(void); # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) # endif -virClassPtr virClassNew(virClassPtr parent, - const char *name, - size_t objectSize, - virObjectDisposeCallback dispose) +virClassPtr +virClassNew(virClassPtr parent, + const char *name, + size_t objectSize, + virObjectDisposeCallback dispose) VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(2); =20 -const char *virClassName(virClassPtr klass) +const char * +virClassName(virClassPtr klass) ATTRIBUTE_NONNULL(1); =20 -bool virClassIsDerivedFrom(virClassPtr klass, - virClassPtr parent) +bool +virClassIsDerivedFrom(virClassPtr klass, + virClassPtr parent) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 -void *virObjectNew(virClassPtr klass) +void * +virObjectNew(virClassPtr klass) ATTRIBUTE_NONNULL(1); -bool virObjectUnref(void *obj); -void *virObjectRef(void *obj); =20 -bool virObjectIsClass(void *obj, - virClassPtr klass) +bool +virObjectUnref(void *obj); + +void * +virObjectRef(void *obj); + +bool +virObjectIsClass(void *obj, + virClassPtr klass) ATTRIBUTE_NONNULL(2); =20 -void virObjectFreeCallback(void *opaque); -void virObjectFreeHashData(void *opaque, const void *name); +void +virObjectFreeCallback(void *opaque); + +void +virObjectFreeHashData(void *opaque, + const void *name); =20 -void *virObjectLockableNew(virClassPtr klass) +void * +virObjectLockableNew(virClassPtr klass) ATTRIBUTE_NONNULL(1); =20 -void virObjectLock(void *lockableobj) +void +virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); -void virObjectUnlock(void *lockableobj) + +void +virObjectUnlock(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 -void virObjectListFree(void *list); -void virObjectListFreeCount(void *list, size_t count); +void +virObjectListFree(void *list); + +void +virObjectListFreeCount(void *list, + size_t count); =20 #endif /* __VIR_OBJECT_H */ --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398652560506.8509742671115; Fri, 2 Jun 2017 03:17:32 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 24BBF61E4A; Fri, 2 Jun 2017 10:17:30 +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 DAAC087E33; Fri, 2 Jun 2017 10:17:29 +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 C714B180BAF4; Fri, 2 Jun 2017 10:17:28 +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 v52AHQ9e027894 for ; Fri, 2 Jun 2017 06:17:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id C40F618216; Fri, 2 Jun 2017 10:17:26 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 80AED5C549 for ; Fri, 2 Jun 2017 10:17:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 24BBF61E4A Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 24BBF61E4A From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:16 -0400 Message-Id: <20170602101722.12157-3-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 2/8] util: Introduce virObjectGetLockableObj 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: , MIME-Version: 1.0 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 10:17:30 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Split out the object fetch in virObject{Lock|Unlock} into a helper Signed-off-by: John Ferlan --- src/util/virobject.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 792685b..34805d3 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -309,6 +309,22 @@ virObjectRef(void *anyobj) } =20 =20 +static virObjectLockablePtr +virObjectGetLockableObj(void *anyobj) +{ + virObjectPtr obj; + + if (virObjectIsClass(anyobj, virObjectLockableClass)) + return anyobj; + + obj =3D anyobj; + VIR_WARN("Object %p (%s) is not a virObjectLockable instance", + anyobj, obj ? obj->klass->name : "(unknown)"); + + return NULL; +} + + /** * virObjectLock: * @anyobj: any instance of virObjectLockablePtr @@ -324,13 +340,10 @@ virObjectRef(void *anyobj) void virObjectLock(void *anyobj) { - virObjectLockablePtr obj =3D anyobj; + virObjectLockablePtr obj =3D virObjectGetLockableObj(anyobj); =20 - if (!virObjectIsClass(obj, virObjectLockableClass)) { - VIR_WARN("Object %p (%s) is not a virObjectLockable instance", - obj, obj ? obj->parent.klass->name : "(unknown)"); + if (!obj) return; - } =20 virMutexLock(&obj->lock); } @@ -346,13 +359,10 @@ virObjectLock(void *anyobj) void virObjectUnlock(void *anyobj) { - virObjectLockablePtr obj =3D anyobj; + virObjectLockablePtr obj =3D virObjectGetLockableObj(anyobj); =20 - if (!virObjectIsClass(obj, virObjectLockableClass)) { - VIR_WARN("Object %p (%s) is not a virObjectLockable instance", - obj, obj ? obj->parent.klass->name : "(unknown)"); + if (!obj) return; - } =20 virMutexUnlock(&obj->lock); } --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398661807176.35040078701775; Fri, 2 Jun 2017 03:17:41 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 86FFD80E7E; Fri, 2 Jun 2017 10:17:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 444B64DA0B; Fri, 2 Jun 2017 10:17:39 +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 C845E41F63; Fri, 2 Jun 2017 10:17:38 +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 v52AHR8j027899 for ; Fri, 2 Jun 2017 06:17:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3B04518216; Fri, 2 Jun 2017 10:17:27 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED1245C549 for ; Fri, 2 Jun 2017 10:17:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 86FFD80E7E Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 86FFD80E7E From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:17 -0400 Message-Id: <20170602101722.12157-4-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 3/8] util: Generate a common internal only error print 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: , MIME-Version: 1.0 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 02 Jun 2017 10:17:40 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" If virObjectIsClass fails "internally" to virobject.c, create a macro to generate the VIR_WARN describing what the problem is. Also improve the checks and message a bit to indicate which was the failure - whether the obj was NULL or just not the right class Signed-off-by: John Ferlan --- src/util/virobject.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 34805d3..9f5f187 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -47,6 +47,16 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ + do { = \ + virObjectPtr obj =3D anyobj; = \ + if (!obj) = \ + VIR_WARN("Object %p is not a virObject class instance", anyobj= );\ + else = \ + VIR_WARN("Object %p (%s) is not a %s instance", = \ + anyobj, obj->klass->name, #objclass); = \ + } while (0) + static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; =20 @@ -312,14 +322,10 @@ virObjectRef(void *anyobj) static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { - virObjectPtr obj; - if (virObjectIsClass(anyobj, virObjectLockableClass)) return anyobj; =20 - obj =3D anyobj; - VIR_WARN("Object %p (%s) is not a virObjectLockable instance", - anyobj, obj ? obj->klass->name : "(unknown)"); + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass); =20 return NULL; } --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 149639866252248.65650584771515; Fri, 2 Jun 2017 03:17:42 -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 6BE58142A65; Fri, 2 Jun 2017 10:17:39 +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 368CC83EB7; Fri, 2 Jun 2017 10:17:39 +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 C2EE4180BAFC; Fri, 2 Jun 2017 10:17:38 +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 v52AHRgT027907 for ; Fri, 2 Jun 2017 06:17:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id ABE5D18216; Fri, 2 Jun 2017 10:17:27 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 680665C54A for ; Fri, 2 Jun 2017 10:17:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6BE58142A65 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6BE58142A65 From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:18 -0400 Message-Id: <20170602101722.12157-5-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 4/8] util: Add safety net of checks to ensure valid object 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: , MIME-Version: 1.0 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.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 02 Jun 2017 10:17:40 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The virObject logic "assumes" that whatever is passed to its API's would be some sort of virObjectPtr; however, if it is not then some really bad things can happen. So far there's been only virObject{Ref|Unref}, virObject{Lock|Unlock}, and virObjectIsClass and the virObject and virObjectLockable class consumers have been well behaved and code well tested. Soon there will be more consumers and one such consumer tripped over this during testing by passing a virHashTablePtr to virObjectIsClass which ends up calling virClassIsDerivedFrom using "obj->klass", which wasn't really a klass object causing one of those bad things to happen. To avoid the future possibility that a non virObject class memory was passed to some virObject* API, this patch adds two new checks - one to validate that the object has the 0xCAFExxxx value in obj->->u.s.magic and the other to ensure obj->u.s.magic doesn't "wrap" some day to 0xCAFF0000 if we ever get that many objects. It is still left up to the caller to handle the failed API calls just as it would be if it passed a NULL opaque pointer anyobj. Signed-off-by: John Ferlan --- src/util/virobject.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 9f5f187..e0465b1 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -22,6 +22,7 @@ #include =20 #define VIR_PARENT_REQUIRED /* empty, to allow virObject to have no parent= */ +#include #include "virobject.h" #include "virthread.h" #include "viralloc.h" @@ -47,10 +48,12 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_NOTVALID(obj) (!obj || ((obj->u.s.magic & 0xCAFE0000) != =3D 0xCAFE0000)) + #define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ do { = \ virObjectPtr obj =3D anyobj; = \ - if (!obj) = \ + if (VIR_OBJECT_NOTVALID(obj)) = \ VIR_WARN("Object %p is not a virObject class instance", anyobj= );\ else = \ VIR_WARN("Object %p (%s) is not a %s instance", = \ @@ -156,6 +159,7 @@ virClassNew(virClassPtr parent, if (VIR_STRDUP(klass->name, name) < 0) goto error; klass->magic =3D virAtomicIntInc(&magicCounter); + assert(klass->magic <=3D 0xCAFEFFFF); klass->objectSize =3D objectSize; klass->dispose =3D dispose; =20 @@ -272,7 +276,7 @@ virObjectUnref(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 bool lastRef =3D virAtomicIntDecAndTest(&obj->u.s.refs); @@ -311,7 +315,7 @@ virObjectRef(void *anyobj) { virObjectPtr obj =3D anyobj; =20 - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return NULL; virAtomicIntInc(&obj->u.s.refs); PROBE(OBJECT_REF, "obj=3D%p", obj); @@ -389,7 +393,7 @@ virObjectIsClass(void *anyobj, virClassPtr klass) { virObjectPtr obj =3D anyobj; - if (!obj) + if (VIR_OBJECT_NOTVALID(obj)) return false; =20 return virClassIsDerivedFrom(obj->klass, klass); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 149639866726568.6515258733665; Fri, 2 Jun 2017 03:17:47 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 007D2B1E20; Fri, 2 Jun 2017 10:17:44 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CBB327D68A; Fri, 2 Jun 2017 10:17: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 6D36F41F65; Fri, 2 Jun 2017 10:17: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 v52AHSG7027919 for ; Fri, 2 Jun 2017 06:17:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2239B4DA01; Fri, 2 Jun 2017 10:17:28 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id D3E2818216 for ; Fri, 2 Jun 2017 10:17:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 007D2B1E20 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 007D2B1E20 From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:19 -0400 Message-Id: <20170602101722.12157-6-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 5/8] util: Introduce virObjectPoolableHashElement 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: , MIME-Version: 1.0 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 02 Jun 2017 10:17:44 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new virObjectLockable child which will be used to more generically describe driver objects. Eventually these objects will be placed into a more generic hash table object which will take care of object mgmt function= s. Each virObjectPoolableHashElement will have a primaryKey (required) and a secondaryKey (optional) which can be used to insert the same object into two hash tables for faster lookups. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 76 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virobject.h | 17 +++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 429b095..a400d8f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2258,6 +2258,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectPoolableHashElement; virClassIsDerivedFrom; virClassName; virClassNew; @@ -2269,6 +2270,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashElementNew; virObjectRef; virObjectUnlock; virObjectUnref; diff --git a/src/util/virobject.c b/src/util/virobject.c index e0465b1..302b7ac 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -62,8 +62,10 @@ struct _virClass { =20 static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; +static virClassPtr virObjectPoolableHashElementClass; =20 static void virObjectLockableDispose(void *anyobj); +static void virObjectPoolableHashElementDispose(void *anyobj); =20 static int virObjectOnceInit(void) @@ -80,6 +82,13 @@ virObjectOnceInit(void) virObjectLockableDispose))) return -1; =20 + if (!(virObjectPoolableHashElementClass =3D + virClassNew(virObjectLockableClass, + "virObjectPoolableHashElement", + sizeof(virObjectPoolableHashElement), + virObjectPoolableHashElementDispose))) + return -1; + return 0; } =20 @@ -117,6 +126,23 @@ virClassForObjectLockable(void) =20 =20 /** + * virClassForObjectPoolableHashElement: + * + * Returns the class instance for the virObjectPoolableHashElement type + */ +virClassPtr +virClassForObjectPoolableHashElement(void) +{ + if (virObjectInitialize() < 0) + return NULL; + + VIR_DEBUG("virObjectPoolableHashElementClass=3D%p", + virObjectPoolableHashElementClass); + return virObjectPoolableHashElementClass; +} + + +/** * virClassNew: * @parent: the parent class * @name: the class name @@ -260,6 +286,53 @@ virObjectLockableDispose(void *anyobj) } =20 =20 +void * +virObjectPoolableHashElementNew(virClassPtr klass, + const char *primaryKey, + const char *secondaryKey) +{ + virObjectPoolableHashElementPtr obj; + + if (!virClassIsDerivedFrom(klass, virClassForObjectPoolableHashElement= ())) { + virReportInvalidArg(klass, + _("Class %s must derive from " + "virObjectPoolableHashElement"), + virClassName(klass)); + return NULL; + } + + if (!(obj =3D virObjectLockableNew(klass))) + return NULL; + + if (VIR_STRDUP(obj->primaryKey, primaryKey) < 0) + goto error; + + if (secondaryKey && VIR_STRDUP(obj->secondaryKey, secondaryKey) < 0) + goto error; + + VIR_DEBUG("obj=3D%p, primary=3D%s secondary=3D%s", + obj, obj->primaryKey, NULLSTR(obj->secondaryKey)); + + return obj; + + error: + virObjectUnref(obj); + return NULL; +} + + +static void +virObjectPoolableHashElementDispose(void *anyobj) +{ + virObjectPoolableHashElementPtr obj =3D anyobj; + + VIR_DEBUG("dispose obj=3D%p", obj); + + VIR_FREE(obj->primaryKey); + VIR_FREE(obj->secondaryKey); +} + + /** * virObjectUnref: * @anyobj: any instance of virObjectPtr @@ -326,7 +399,8 @@ virObjectRef(void *anyobj) static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectLockableClass)) + if (virObjectIsClass(anyobj, virObjectLockableClass) || + virObjectIsClass(anyobj, virObjectPoolableHashElementClass)) return anyobj; =20 VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass); diff --git a/src/util/virobject.h b/src/util/virobject.h index f4c292b..e29dae7 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -34,6 +34,9 @@ typedef virObject *virObjectPtr; typedef struct _virObjectLockable virObjectLockable; typedef virObjectLockable *virObjectLockablePtr; =20 +typedef struct _virObjectPoolableHashElement virObjectPoolableHashElement; +typedef virObjectPoolableHashElement *virObjectPoolableHashElementPtr; + typedef void (*virObjectDisposeCallback)(void *obj); =20 /* Most code should not play with the contents of this struct; however, @@ -59,9 +62,17 @@ struct _virObjectLockable { virMutex lock; }; =20 +struct _virObjectPoolableHashElement { + virObjectLockable parent; + + char *primaryKey; + char *secondaryKey; +}; + =20 virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); +virClassPtr virClassForObjectPoolableHashElement(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -108,6 +119,12 @@ void * virObjectLockableNew(virClassPtr klass) ATTRIBUTE_NONNULL(1); =20 +void * +virObjectPoolableHashElementNew(virClassPtr klass, + const char *primaryKey, + const char *secondaryKey) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + void virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398667323161.4843084340315; Fri, 2 Jun 2017 03:17:47 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 629C757027; Fri, 2 Jun 2017 10:17:45 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2359587E33; Fri, 2 Jun 2017 10:17:45 +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 BF5DA41F7E; Fri, 2 Jun 2017 10:17:44 +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 v52AHSbZ027924 for ; Fri, 2 Jun 2017 06:17:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id BE4A718216; Fri, 2 Jun 2017 10:17:28 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B35C4DA21 for ; Fri, 2 Jun 2017 10:17:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 629C757027 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 629C757027 From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:20 -0400 Message-Id: <20170602101722.12157-7-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 6/8] util: Add virObjectPoolableHashElementGet*Key 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: , MIME-Version: 1.0 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 02 Jun 2017 10:17:46 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a pair of accessor API's: virObjectPoolableHashElementGetPrimaryKey virObjectPoolableHashElementGetSecondaryKey which will return the requested key from the object to the caller. It is up to the caller to check the returned key and error if the return value is NULL. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 50 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 6 ++++++ 3 files changed, 58 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a400d8f..dd3ea68 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2270,6 +2270,8 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashElementGetPrimaryKey; +virObjectPoolableHashElementGetSecondaryKey; virObjectPoolableHashElementNew; virObjectRef; virObjectUnlock; diff --git a/src/util/virobject.c b/src/util/virobject.c index 302b7ac..18cef0f 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -409,6 +409,18 @@ virObjectGetLockableObj(void *anyobj) } =20 =20 +static virObjectPoolableHashElementPtr +virObjectGetPoolableHashElementObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectPoolableHashElementClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableHashElementCla= ss); + + return NULL; +} + + /** * virObjectLock: * @anyobj: any instance of virObjectLockablePtr @@ -560,3 +572,41 @@ virObjectListFreeCount(void *list, =20 VIR_FREE(list); } + + +/** + * virObjectPoolableHashElementGetPrimaryKey + * @anyobj: Pointer to a PoolableHashElement object + * + * Returns: Pointer to the primaryKey or NULL on failure + */ +const char * +virObjectPoolableHashElementGetPrimaryKey(void *anyobj) +{ + virObjectPoolableHashElementPtr obj =3D + virObjectGetPoolableHashElementObj(anyobj); + + if (!obj) + return NULL; + + return obj->primaryKey; +} + + +/** + * virObjectPoolableHashElementGetSecondaryKey + * @anyobj: Pointer to a PoolableHashElement object + * + * Returns: Pointer to the secondaryKey which may be NULL + */ +const char * +virObjectPoolableHashElementGetSecondaryKey(void *anyobj) +{ + virObjectPoolableHashElementPtr obj =3D + virObjectGetPoolableHashElementObj(anyobj); + + if (!obj) + return NULL; + + return obj->secondaryKey; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index e29dae7..1ed856e 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -140,4 +140,10 @@ void virObjectListFreeCount(void *list, size_t count); =20 +const char * +virObjectPoolableHashElementGetPrimaryKey(void *anyobj); + +const char * +virObjectPoolableHashElementGetSecondaryKey(void *anyobj); + #endif /* __VIR_OBJECT_H */ --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398662011915.6679753286631; Fri, 2 Jun 2017 03:17:42 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E0F89334582; Fri, 2 Jun 2017 10:17:39 +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 6C82B779CE; Fri, 2 Jun 2017 10:17:39 +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 E1E34180BAF9; Fri, 2 Jun 2017 10:17:38 +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 v52AHTqk027932 for ; Fri, 2 Jun 2017 06:17:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 34B3618216; Fri, 2 Jun 2017 10:17:29 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4D914DA1D for ; Fri, 2 Jun 2017 10:17:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E0F89334582 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E0F89334582 From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:21 -0400 Message-Id: <20170602101722.12157-8-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 7/8] util: Introduce virObjectPoolableDef 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: , MIME-Version: 1.0 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 02 Jun 2017 10:17:40 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new virObjectPoolableHashElement child which will be used to provide the basis for driver def/newDef elements. Each virObjectPoolableDef has: 1. A required @primaryKey to be used to uniquely identity the object by some string value. 2. An optional @secondaryKey to be used as a secondary means of search for the object by some string value. 3. A required @def and @defFreeFunc. The @def will be consumed by the object and when disposed the free function will be called. The _virObjectPoolableDef has an additional @newDef element to store the "next" boot configuration for consumers that support the functionality. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 83 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virobject.h | 24 ++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index dd3ea68..1896f24 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2258,6 +2258,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectPoolableDef; virClassForObjectPoolableHashElement; virClassIsDerivedFrom; virClassName; @@ -2270,6 +2271,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableDefNew; virObjectPoolableHashElementGetPrimaryKey; virObjectPoolableHashElementGetSecondaryKey; virObjectPoolableHashElementNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 18cef0f..0aed790 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -63,9 +63,11 @@ struct _virClass { static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; static virClassPtr virObjectPoolableHashElementClass; +static virClassPtr virObjectPoolableDefClass; =20 static void virObjectLockableDispose(void *anyobj); static void virObjectPoolableHashElementDispose(void *anyobj); +static void virObjectPoolableDefDispose(void *anyobj); =20 static int virObjectOnceInit(void) @@ -89,6 +91,13 @@ virObjectOnceInit(void) virObjectPoolableHashElementDispose))) return -1; =20 + if (!(virObjectPoolableDefClass =3D + virClassNew(virObjectPoolableHashElementClass, + "virObjectPoolableDef", + sizeof(virObjectPoolableDef), + virObjectPoolableDefDispose))) + return -1; + return 0; } =20 @@ -143,6 +152,23 @@ virClassForObjectPoolableHashElement(void) =20 =20 /** + * virClassForObjectPoolableDef: + * + * Returns the class instance for the virObjectPoolableDef type + */ +virClassPtr +virClassForObjectPoolableDef(void) +{ + if (virObjectInitialize() < 0) + return NULL; + + VIR_DEBUG("virObjectPoolableDefClass=3D%p", + virObjectPoolableDefClass); + return virObjectPoolableDefClass; +} + + +/** * virClassNew: * @parent: the parent class * @name: the class name @@ -334,6 +360,60 @@ virObjectPoolableHashElementDispose(void *anyobj) =20 =20 /** + * virObjectPoolableDefNew: + * @klass: the klass to check + * @primaryKey: primary key (required) + * @secondaryKey: secondary key + * @def: XML definition (required) + * @defFreeFunc: Free function for @def and @newDef (required) + * + * Create a new poolable def object for storing "common" domain defs. + * + * Returns: New object on success, NULL on failure w/ error message set + */ +void * +virObjectPoolableDefNew(virClassPtr klass, + const char *primaryKey, + const char *secondaryKey, + void *def, + virFreeCallback defFreeFunc) +{ + virObjectPoolableDefPtr obj; + + if (!virClassIsDerivedFrom(klass, virClassForObjectPoolableDef())) { + virReportInvalidArg(klass, + _("Class %s must derive from " + "virObjectPoolableDef"), + virClassName(klass)); + return NULL; + } + + if (!(obj =3D virObjectPoolableHashElementNew(klass, primaryKey, + secondaryKey))) + return NULL; + + obj->def =3D def; + obj->defFreeFunc =3D defFreeFunc; + + VIR_DEBUG("obj=3D%p, def=3D%p ff=3D%p", obj, obj->def, obj->defFreeFun= c); + + return obj; +} + + +static void +virObjectPoolableDefDispose(void *anyobj) +{ + virObjectPoolableDefPtr obj =3D anyobj; + + VIR_DEBUG("dispose obj=3D%p", obj); + + (obj->defFreeFunc)(obj->def); + (obj->defFreeFunc)(obj->newDef); +} + + +/** * virObjectUnref: * @anyobj: any instance of virObjectPtr * @@ -400,7 +480,8 @@ static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { if (virObjectIsClass(anyobj, virObjectLockableClass) || - virObjectIsClass(anyobj, virObjectPoolableHashElementClass)) + virObjectIsClass(anyobj, virObjectPoolableHashElementClass) || + virObjectIsClass(anyobj, virObjectPoolableDefClass)) return anyobj; =20 VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass); diff --git a/src/util/virobject.h b/src/util/virobject.h index 1ed856e..085ed43 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -37,6 +37,9 @@ typedef virObjectLockable *virObjectLockablePtr; typedef struct _virObjectPoolableHashElement virObjectPoolableHashElement; typedef virObjectPoolableHashElement *virObjectPoolableHashElementPtr; =20 +typedef struct _virObjectPoolableDef virObjectPoolableDef; +typedef virObjectPoolableDef *virObjectPoolableDefPtr; + typedef void (*virObjectDisposeCallback)(void *obj); =20 /* Most code should not play with the contents of this struct; however, @@ -69,10 +72,22 @@ struct _virObjectPoolableHashElement { char *secondaryKey; }; =20 +struct _virObjectPoolableDef { + virObjectPoolableHashElement parent; + + /* 'def' is the current config definition. + * 'newDef' is the next boot configuration. + */ + void *def; + void *newDef; + virFreeCallback defFreeFunc; +}; + =20 virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); virClassPtr virClassForObjectPoolableHashElement(void); +virClassPtr virClassForObjectPoolableDef(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -125,6 +140,15 @@ virObjectPoolableHashElementNew(virClassPtr klass, const char *secondaryKey) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 +void * +virObjectPoolableDefNew(virClassPtr klass, + const char *primaryKey, + const char *secondaryKey, + void *def, + virFreeCallback defFreeFunc) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4) + ATTRIBUTE_NONNULL(5); + void virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:28:59 2024 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.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; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496398670129689.1694096069645; Fri, 2 Jun 2017 03:17:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 487DBC0467D7; Fri, 2 Jun 2017 10:17:48 +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 21FA07D66A; Fri, 2 Jun 2017 10:17:48 +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 B1345180BAFC; Fri, 2 Jun 2017 10:17:47 +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 v52AHTah027945 for ; Fri, 2 Jun 2017 06:17:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id AA1D718216; Fri, 2 Jun 2017 10:17:29 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-108.phx2.redhat.com [10.3.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 661FA4DA1D for ; Fri, 2 Jun 2017 10:17:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 487DBC0467D7 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 487DBC0467D7 From: John Ferlan To: libvir-list@redhat.com Date: Fri, 2 Jun 2017 06:17:22 -0400 Message-Id: <20170602101722.12157-9-jferlan@redhat.com> In-Reply-To: <20170602101722.12157-1-jferlan@redhat.com> References: <20170602101722.12157-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 8/8] util: Add virObjectPoolableDef* accessor API's 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: , MIME-Version: 1.0 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 02 Jun 2017 10:17:48 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add new API's for object management: virObjectPoolableDefGetDef - Just return the obj->def from the object. virObjectPoolableDefSetDef - If the new @def is non-NULL, replace the previous obj->def with the new @def argument calling the obj->defFreeFunc on the previous obj->def. If the @def is NULL, then just clear the obj->def leaving the caller to handle removal of the previous obj->def. virObjectPoolableDefGetNewDef - Just return the obj->newDef from the object. virObjectPoolableDefSetNewDef - If the new @newDef is non-NULL, replace the previous obj->newDef with the new @newDef argument calling the obj->defFreeFunc on the previous obj->newDef. If the @newDef is NULL, then just clear the obj->newDef leaving the caller to handle removal of the previous obj->newDef. virObjectPoolableDefSwapNewDef - Manage swapping the obj->newDef into obj->def by first calling the obj->defFreeFunc on the current obj->def and then stealing the obj->newDef into obj->def. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 5 ++ src/util/virobject.c | 143 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 17 ++++++ 3 files changed, 165 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1896f24..afc04c5 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2271,7 +2271,12 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableDefGetDef; +virObjectPoolableDefGetNewDef; virObjectPoolableDefNew; +virObjectPoolableDefSetDef; +virObjectPoolableDefSetNewDef; +virObjectPoolableDefStealNewDef; virObjectPoolableHashElementGetPrimaryKey; virObjectPoolableHashElementGetSecondaryKey; virObjectPoolableHashElementNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 0aed790..831cde2 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -502,6 +502,18 @@ virObjectGetPoolableHashElementObj(void *anyobj) } =20 =20 +static virObjectPoolableDefPtr +virObjectGetPoolableDefObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectPoolableDefClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableDefClass); + + return NULL; +} + + /** * virObjectLock: * @anyobj: any instance of virObjectLockablePtr @@ -691,3 +703,134 @@ virObjectPoolableHashElementGetSecondaryKey(void *any= obj) =20 return obj->secondaryKey; } + + +/** + * virObjectPoolableDefGetDef + * @anyobj: Pointer to a locked PoolableDef object + * + * Returns: Pointer to the object @def or NULL on failure + */ +void * +virObjectPoolableDefGetDef(void *anyobj) +{ + virObjectPoolableDefPtr obj =3D virObjectGetPoolableDefObj(anyobj); + + if (!obj) + return NULL; + + return obj->def; +} + + +/** + * virObjectPoolableDefSetDef + * @anyobj: Pointer to a locked PoolableDef object + * @def: Opaque object definition to replace in the object + * + * Set the @def of the object - this may be NULL if clearing the @def + * from the object in which case it is up to the caller to handle managing + * the previous obj->def. If @def is not NULL, this includes a call to the + * defFreeFunc prior to the set. + * + * Returns 0 on success, -1 on failure + */ +int +virObjectPoolableDefSetDef(void *anyobj, + void *def) +{ + virObjectPoolableDefPtr obj =3D virObjectGetPoolableDefObj(anyobj); + + if (!obj) + return -1; + + if (def) { + obj->defFreeFunc(obj->def); + obj->def =3D def; + } else { + obj->def =3D NULL; + } + + return 0; +} + + +/** + * virObjectPoolableDefGetNewDef + * @anyobj: Pointer to a locked PoolableDef object + * + * Returns: Pointer to the object 'newDef' or NULL on failure + */ +void * +virObjectPoolableDefGetNewDef(void *anyobj) +{ + virObjectPoolableDefPtr obj =3D virObjectGetPoolableDefObj(anyobj); + + if (!obj) + return NULL; + + return obj->newDef; +} + + +/** + * virObjectPoolableDefSetNewDef + * @anyobj: Pointer to a locked PoolableDef object + * @newDef: Opaque 'newDef' to replace in the object, may be NULL + * + * Set the 'newDef' of the object - this may be NULL if clearing the newDef + * from the object in which case it is up to the caller to handle managing + * the previous newDef. If @newDef is not NULL, this includes a call to the + * defFreeFunc prior to the set. + * + * Returns 0 on success, -1 on failure + */ +int +virObjectPoolableDefSetNewDef(void *anyobj, + void *newDef) +{ + virObjectPoolableDefPtr obj =3D virObjectGetPoolableDefObj(anyobj); + + if (!obj) + return -1; + + if (newDef) { + obj->defFreeFunc(obj->newDef); + obj->newDef =3D newDef; + } else { + obj->newDef =3D NULL; + } + + return 0; +} + + +/** + * virObjectPoolableDefSwapNewDef + * @anyobj: Pointer to a locked PoolableDef object + * + * Manage swapping the obj->newDef into obj->def in one pass. + * When called, the previous obj->def will be free'd and the + * existing defDef will take it's place. + * + * Returns 0 on success, -1 on failure + */ +int +virObjectPoolableDefStealNewDef(void *anyobj) +{ + virObjectPoolableDefPtr obj =3D virObjectGetPoolableDefObj(anyobj); + + if (!obj) + return -1; + + if (!obj->newDef) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to steal NULL newDef")); + return -1; + } + + obj->defFreeFunc(obj->def); + VIR_STEAL_PTR(obj->def, obj->newDef); + + return 0; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 085ed43..dd6412e 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -170,4 +170,21 @@ virObjectPoolableHashElementGetPrimaryKey(void *anyobj= ); const char * virObjectPoolableHashElementGetSecondaryKey(void *anyobj); =20 +void * +virObjectPoolableDefGetDef(void *anyobj); + +int +virObjectPoolableDefSetDef(void *anyobj, + void *def); + +void * +virObjectPoolableDefGetNewDef(void *anyobj); + +int +virObjectPoolableDefSetNewDef(void *anyobj, + void *newDef); + +int +virObjectPoolableDefStealNewDef(void *anyobj); + #endif /* __VIR_OBJECT_H */ --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list