From nobody Fri May 3 17:03:01 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 1491480695682752.3315131419515; Thu, 6 Apr 2017 05:11:35 -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 70BFC3D958; Thu, 6 Apr 2017 12:11:34 +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 43FD0A862E; Thu, 6 Apr 2017 12:11:34 +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 F0D6718521CA; Thu, 6 Apr 2017 12:11:33 +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 v36CAuYS002267 for ; Thu, 6 Apr 2017 08:10:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5BB1C171D0; Thu, 6 Apr 2017 12:10:56 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 06EF61719F for ; Thu, 6 Apr 2017 12:10:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 70BFC3D958 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 70BFC3D958 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:45 -0400 Message-Id: <20170406121052.5815-2-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 06 Apr 2017 12:11:35 +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.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 149148069949194.51986010312066; Thu, 6 Apr 2017 05:11:39 -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 020B4AA455; Thu, 6 Apr 2017 12:11:38 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C9BD7A482D; Thu, 6 Apr 2017 12:11:37 +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 851ED5ED63; Thu, 6 Apr 2017 12:11:37 +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 v36CAunA002273 for ; Thu, 6 Apr 2017 08:10:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id CC6B31719F; Thu, 6 Apr 2017 12:10:56 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 83EF1171D7 for ; Thu, 6 Apr 2017 12:10:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 020B4AA455 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 020B4AA455 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:46 -0400 Message-Id: <20170406121052.5815-3-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 06 Apr 2017 12:11:38 +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 | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 792685b..5d5b6a3 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -309,6 +309,23 @@ 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 +341,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 +360,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.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480673849169.3820551274748; Thu, 6 Apr 2017 05:11:13 -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 798F581231; Thu, 6 Apr 2017 12:11:12 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4E2691719F; Thu, 6 Apr 2017 12:11:12 +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 0603B5ED63; Thu, 6 Apr 2017 12:11:12 +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 v36CAvLP002279 for ; Thu, 6 Apr 2017 08:10:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id 40FD9171D0; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id F3ACF1719F for ; Thu, 6 Apr 2017 12:10:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 798F581231 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 798F581231 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:47 -0400 Message-Id: <20170406121052.5815-4-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 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]); Thu, 06 Apr 2017 12:11:13 +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 5d5b6a3..9fe4e71 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 --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480703220146.14043609163093; Thu, 6 Apr 2017 05:11:43 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9E9E3624D7; Thu, 6 Apr 2017 12:11:41 +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 7253A9A57A; Thu, 6 Apr 2017 12:11:41 +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 0193818523C8; Thu, 6 Apr 2017 12:11:40 +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 v36CAvCF002289 for ; Thu, 6 Apr 2017 08:10:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id C89C8171D0; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88DB31719F for ; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9E9E3624D7 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 9E9E3624D7 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:48 -0400 Message-Id: <20170406121052.5815-5-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 4/8] util: Add magic_marker for all virObjects 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 06 Apr 2017 12:11:42 +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, let's add a "magic_marker" to the base virObject class that contains a value "0xFEEDBEEF", then any place in the code which would accept or process the base opaque virObjectPtr, compare the magic_marker against 0xFEEDBEEF to make sure this is an object allocated by this code. 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 ++++++++---- src/util/virobject.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index 9fe4e71..aea8a6d 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -47,10 +47,12 @@ struct _virClass { virObjectDisposeCallback dispose; }; =20 +#define VIR_OBJECT_NOTVALID(obj) (!obj || obj->magic_marker !=3D 0xFEEDBEE= F) + #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", = \ @@ -212,6 +214,7 @@ virObjectNew(virClassPtr klass) return NULL; =20 obj->u.s.magic =3D klass->magic; + obj->magic_marker =3D 0xFEEDBEEF; obj->klass =3D klass; virAtomicIntSet(&obj->u.s.refs, 1); =20 @@ -272,7 +275,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); @@ -289,6 +292,7 @@ virObjectUnref(void *anyobj) /* Clear & poison object */ memset(obj, 0, obj->klass->objectSize); obj->u.s.magic =3D 0xDEADBEEF; + obj->magic_marker =3D 0xDEADBEEF; obj->klass =3D (void*)0xDEADBEEF; VIR_FREE(obj); } @@ -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); @@ -390,7 +394,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); diff --git a/src/util/virobject.h b/src/util/virobject.h index f4c292b..89f8050 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -51,6 +51,7 @@ struct _virObject { int refs; } s; } u; + unsigned int magic_marker; virClassPtr klass; }; =20 --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480706690427.298640960282; Thu, 6 Apr 2017 05:11:46 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3D461C04B941; Thu, 6 Apr 2017 12:11:45 +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 135129A592; Thu, 6 Apr 2017 12:11: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 B6AB718523CC; Thu, 6 Apr 2017 12:11: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 v36CAwKs002299 for ; Thu, 6 Apr 2017 08:10:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3C838171D0; Thu, 6 Apr 2017 12:10:58 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0ADB1719F for ; Thu, 6 Apr 2017 12:10:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3D461C04B941 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 3D461C04B941 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:49 -0400 Message-Id: <20170406121052.5815-6-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 5/8] util: Introduce virObjectPoolableHashTable 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 06 Apr 2017 12:11:46 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add the initial infrastructure to define a new object which will be a child of the virObjectLockable class that can be used to create an object that has two hash tables for object consumers to be able to utilize to store elements in one or two hash tables depending on the need to have more than one lookup mechanism for the same object TODO: Remove VIR_DEBUG's or add separately Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 72 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virobject.h | 29 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 92083e5..6861155 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2223,6 +2223,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectPoolableHashTable; virClassIsDerivedFrom; virClassName; virClassNew; @@ -2234,6 +2235,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashTableNew; virObjectRef; virObjectUnlock; virObjectUnref; diff --git a/src/util/virobject.c b/src/util/virobject.c index aea8a6d..d284dde 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -61,8 +61,11 @@ struct _virClass { =20 static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; +static virClassPtr virObjectPoolableHashTableClass; =20 static void virObjectLockableDispose(void *anyobj); +static void virObjectPoolableHashTableDispose(void *anyobj); + =20 static int virObjectOnceInit(void) @@ -79,6 +82,13 @@ virObjectOnceInit(void) virObjectLockableDispose))) return -1; =20 + if (!(virObjectPoolableHashTableClass =3D + virClassNew(virObjectLockableClass, + "virObjectPoolableHashTable", + sizeof(virObjectPoolableHashTable), + virObjectPoolableHashTableDispose))) + return -1; + return 0; } =20 @@ -96,6 +106,7 @@ virClassForObject(void) if (virObjectInitialize() < 0) return NULL; =20 + VIR_DEBUG("virObjectClass=3D%p", virObjectClass); return virObjectClass; } =20 @@ -111,11 +122,29 @@ virClassForObjectLockable(void) if (virObjectInitialize() < 0) return NULL; =20 + VIR_DEBUG("virObjectLockableClass=3D%p", virObjectLockableClass); return virObjectLockableClass; } =20 =20 /** + * virClassForObjectPoolableHashTable: + * + * Returns the class instance for the virObjectPoolableHashTable type + */ +virClassPtr +virClassForObjectPoolableHashTable(void) +{ + if (virObjectInitialize() < 0) + return NULL; + + VIR_DEBUG("virObjectPoolableHashTableClass=3D%p", + virObjectPoolableHashTableClass); + return virObjectPoolableHashTableClass; +} + + +/** * virClassNew: * @parent: the parent class * @name: the class name @@ -139,6 +168,9 @@ virClassNew(virClassPtr parent, { virClassPtr klass; =20 + VIR_DEBUG("parent=3D%p, name=3D%s, objectSize=3D%zu, dispose=3D%p", + parent, name, objectSize, dispose); + if (parent =3D=3D NULL && STRNEQ(name, "virObject")) { virReportInvalidNonNullArg(parent); @@ -246,6 +278,7 @@ virObjectLockableNew(virClassPtr klass) return NULL; } =20 + VIR_DEBUG("lockable new obj=3D%p", obj); return obj; } =20 @@ -255,10 +288,46 @@ virObjectLockableDispose(void *anyobj) { virObjectLockablePtr obj =3D anyobj; =20 + VIR_DEBUG("lockable dispose obj=3D%p", obj); virMutexDestroy(&obj->lock); } =20 =20 +void * +virObjectPoolableHashTableNew(virClassPtr klass, + virObjectPoolableHashTableObjType objtype) +{ + virObjectPoolableHashTablePtr obj; + + if (!virClassIsDerivedFrom(klass, virClassForObjectPoolableHashTable()= )) { + virReportInvalidArg(klass, + _("Class %s must derive from " + "virObjectPoolableHashTable"), + virClassName(klass)); + return NULL; + } + + if (!(obj =3D virObjectLockableNew(klass))) + return NULL; + + obj->objtype =3D objtype; + + VIR_DEBUG("poolable new obj=3D%p, type=3D%d", + obj, objtype); + + return obj; + +} + +static void +virObjectPoolableHashTableDispose(void *anyobj) +{ + virObjectPoolableHashTablePtr obj =3D anyobj; + + VIR_DEBUG("poolable dispose obj=3D%p", obj); +} + + /** * virObjectUnref: * @anyobj: any instance of virObjectPtr @@ -326,7 +395,8 @@ virObjectRef(void *anyobj) static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectLockableClass)) + if (virObjectIsClass(anyobj, virObjectLockableClass) || + virObjectIsClass(anyobj, virObjectPoolableHashTableClass)) return anyobj; =20 VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass); diff --git a/src/util/virobject.h b/src/util/virobject.h index 89f8050..30ce6a1 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -23,6 +23,7 @@ # define __VIR_OBJECT_H__ =20 # include "internal.h" +# include "virhash.h" # include "virthread.h" =20 typedef struct _virClass virClass; @@ -34,6 +35,9 @@ typedef virObject *virObjectPtr; typedef struct _virObjectLockable virObjectLockable; typedef virObjectLockable *virObjectLockablePtr; =20 +typedef struct _virObjectPoolableHashTable virObjectPoolableHashTable; +typedef virObjectPoolableHashTable *virObjectPoolableHashTablePtr; + typedef void (*virObjectDisposeCallback)(void *obj); =20 /* Most code should not play with the contents of this struct; however, @@ -60,9 +64,29 @@ struct _virObjectLockable { virMutex lock; }; =20 +typedef enum { + VIR_OBJECTPOOLABLE_NODEDEVICE, + VIR_OBJECTPOOLABLE_INTERFACE, + VIR_OBJECTPOOLABLE_NWFILTER, + VIR_OBJECTPOOLABLE_VOLUME, + VIR_OBJECTPOOLABLE_BLOCK_STORAGE, + VIR_OBJECTPOOLABLE_SECRET, + VIR_OBJECTPOOLABLE_NETWORK, + VIR_OBJECTPOOLABLE_SNAPSHOT, + VIR_OBJECTPOOLABLE_DOMAIN, + + VIR_OBJECTPOOLABLE_LAST +} virObjectPoolableHashTableObjType; + +struct _virObjectPoolableHashTable { + virObjectLockable parent; + virObjectPoolableHashTableObjType objtype; +}; + =20 virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); +virClassPtr virClassForObjectPoolableHashTable(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -109,6 +133,11 @@ void * virObjectLockableNew(virClassPtr klass) ATTRIBUTE_NONNULL(1); =20 +void * +virObjectPoolableHashTableNew(virClassPtr klass, + virObjectPoolableHashTableObjType objtype) + ATTRIBUTE_NONNULL(1); + void virObjectLock(void *lockableobj) ATTRIBUTE_NONNULL(1); --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480710007305.0595034654061; Thu, 6 Apr 2017 05:11:50 -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 A3E6D7E9E6; Thu, 6 Apr 2017 12:11:48 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7A6D5A863F; Thu, 6 Apr 2017 12:11: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 361AE5ED65; Thu, 6 Apr 2017 12:11:48 +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 v36CAwju002309 for ; Thu, 6 Apr 2017 08:10:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id A4BB41719F; Thu, 6 Apr 2017 12:10:58 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64403171D7 for ; Thu, 6 Apr 2017 12:10:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A3E6D7E9E6 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 A3E6D7E9E6 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:50 -0400 Message-Id: <20170406121052.5815-7-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 6/8] util: Add the hash tables to virObjectPoolableHashTable 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.26]); Thu, 06 Apr 2017 12:11:49 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a pair of hash tables to the object - a primary and a secondary one. If the consumer requested only a primary, then only it will be created. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 78 ++++++++++++++++++++++++++++++++++++++++++++= +--- src/util/virobject.h | 22 +++++++++++++- 3 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6861155..9b3345a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2235,6 +2235,8 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashTableGetPrimary; +virObjectPoolableHashTableGetSecondary; virObjectPoolableHashTableNew; virObjectRef; virObjectUnlock; diff --git a/src/util/virobject.c b/src/util/virobject.c index d284dde..625bf90 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -295,7 +295,9 @@ virObjectLockableDispose(void *anyobj) =20 void * virObjectPoolableHashTableNew(virClassPtr klass, - virObjectPoolableHashTableObjType objtype) + virObjectPoolableHashTableObjType objtype, + int tableElemsStart, + bool primaryOnly) { virObjectPoolableHashTablePtr obj; =20 @@ -311,20 +313,39 @@ virObjectPoolableHashTableNew(virClassPtr klass, return NULL; =20 obj->objtype =3D objtype; + obj->tableElemsStart =3D tableElemsStart; =20 - VIR_DEBUG("poolable new obj=3D%p, type=3D%d", - obj, objtype); + if (!(obj->objsPrimary =3D virHashCreate(tableElemsStart, + virObjectFreeHashData))) + goto error; + + if (!primaryOnly && + !(obj->objsSecondary =3D virHashCreate(tableElemsStart, + virObjectFreeHashData))) + goto error; + + + VIR_DEBUG("obj=3D%p, type=3D%d, elems=3D%d objsPrimary=3D%p objsSecond= ary=3D%p", + obj, objtype, tableElemsStart, + obj->objsPrimary, obj->objsSecondary); =20 return obj; =20 + error: + virObjectUnref(obj); + return NULL; } =20 + static void virObjectPoolableHashTableDispose(void *anyobj) { virObjectPoolableHashTablePtr obj =3D anyobj; =20 VIR_DEBUG("poolable dispose obj=3D%p", obj); + + virHashFree(obj->objsPrimary); + virHashFree(obj->objsSecondary); } =20 =20 @@ -402,7 +423,6 @@ virObjectGetLockableObj(void *anyobj) VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockableClass); =20 return NULL; - } =20 =20 @@ -557,3 +577,53 @@ virObjectListFreeCount(void *list, =20 VIR_FREE(list); } + + +static virObjectPoolableHashTablePtr +virObjectGetPoolableHashTableObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectPoolableHashTableClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableHashTableClass= ); + + return NULL; +} + + +/** + * virObjectPoolableHashTableGetPrimary + * @anyobj: Pointer to a PoolableHashTable object + * + * Returns: Pointer to the Primary Hash Table or NULL on failure + */ +virHashTablePtr +virObjectPoolableHashTableGetPrimary(void *anyobj) +{ + virObjectPoolableHashTablePtr obj =3D + virObjectGetPoolableHashTableObj(anyobj); + + if (!obj) + return NULL; + + return obj->objsPrimary; +} + + +/** + * virObjectPoolableHashTableGetSecondary + * @anyobj: Pointer to a PoolableHashTable object + * + * Returns: Pointer to the Secondary Hash Table or NULL on failure + */ +virHashTablePtr +virObjectPoolableHashTableGetSecondary(void *anyobj) +{ + virObjectPoolableHashTablePtr obj =3D + virObjectGetPoolableHashTableObj(anyobj); + + if (!obj) + return NULL; + + return obj->objsSecondary; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 30ce6a1..ac21190 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -81,6 +81,18 @@ typedef enum { struct _virObjectPoolableHashTable { virObjectLockable parent; virObjectPoolableHashTableObjType objtype; + + int tableElemsStart; + + /* primary key -> object mapping for O(1), + * lockless lookup-by-primary */ + virHashTable *objsPrimary; + + /* uuid string -> virPoolObj mapping + * for O(1), lockless lookup-by-uuid */ + /* secondary key -> object mapping for O(1), + * lockless lookup-by-secondary */ + virHashTable *objsSecondary; }; =20 =20 @@ -135,7 +147,9 @@ virObjectLockableNew(virClassPtr klass) =20 void * virObjectPoolableHashTableNew(virClassPtr klass, - virObjectPoolableHashTableObjType objtype) + virObjectPoolableHashTableObjType objtype, + int tableElemsStart, + bool primaryOnly) ATTRIBUTE_NONNULL(1); =20 void @@ -153,4 +167,10 @@ void virObjectListFreeCount(void *list, size_t count); =20 +virHashTablePtr +virObjectPoolableHashTableGetPrimary(void *anyobj); + +virHashTablePtr +virObjectPoolableHashTableGetSecondary(void *anyobj); + #endif /* __VIR_OBJECT_H */ --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480677708536.1717015536518; Thu, 6 Apr 2017 05:11:17 -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 3AB39C04B951; Thu, 6 Apr 2017 12:11:16 +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 0DC6CA482E; Thu, 6 Apr 2017 12:11:16 +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 ACBDD18523C7; Thu, 6 Apr 2017 12:11:15 +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 v36CAxBo002320 for ; Thu, 6 Apr 2017 08:10:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 19AC7171D0; Thu, 6 Apr 2017 12:10:59 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD1BD1719F for ; Thu, 6 Apr 2017 12:10:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3AB39C04B951 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 3AB39C04B951 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:51 -0400 Message-Id: <20170406121052.5815-8-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 7/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.31]); Thu, 06 Apr 2017 12:11:17 +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 describe elements that would be inserted into the virObjectPoolableHashTable. Each virObjectPoolableHashElement will have a primaryKey and a secondaryKey which can be used to insert the same object into both of the hash tables that are part of the virObjectPoolableHashTable. This allows for lookup of the object by more than one means. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 77 ++++++++++++++++++++++++++++++++++++++++++++= ++-- src/util/virobject.h | 17 +++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9b3345a..74d4bf8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2223,6 +2223,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectPoolableHashElement; virClassForObjectPoolableHashTable; virClassIsDerivedFrom; virClassName; @@ -2235,6 +2236,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashElementNew; virObjectPoolableHashTableGetPrimary; virObjectPoolableHashTableGetSecondary; virObjectPoolableHashTableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 625bf90..c9e631c 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -62,10 +62,11 @@ struct _virClass { static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; static virClassPtr virObjectPoolableHashTableClass; +static virClassPtr virObjectPoolableHashElementClass; =20 static void virObjectLockableDispose(void *anyobj); static void virObjectPoolableHashTableDispose(void *anyobj); - +static void virObjectPoolableHashElementDispose(void *anyobj); =20 static int virObjectOnceInit(void) @@ -89,6 +90,13 @@ virObjectOnceInit(void) virObjectPoolableHashTableDispose))) return -1; =20 + if (!(virObjectPoolableHashElementClass =3D + virClassNew(virObjectLockableClass, + "virObjectPoolableHashElement", + sizeof(virObjectPoolableHashElement), + virObjectPoolableHashElementDispose))) + return -1; + return 0; } =20 @@ -145,6 +153,23 @@ virClassForObjectPoolableHashTable(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 @@ -349,6 +374,53 @@ virObjectPoolableHashTableDispose(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 @@ -417,7 +489,8 @@ static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { if (virObjectIsClass(anyobj, virObjectLockableClass) || - virObjectIsClass(anyobj, virObjectPoolableHashTableClass)) + virObjectIsClass(anyobj, virObjectPoolableHashTableClass) || + 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 ac21190..36fd842 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -38,6 +38,9 @@ typedef virObjectLockable *virObjectLockablePtr; typedef struct _virObjectPoolableHashTable virObjectPoolableHashTable; typedef virObjectPoolableHashTable *virObjectPoolableHashTablePtr; =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, @@ -96,9 +99,17 @@ struct _virObjectPoolableHashTable { }; =20 =20 +struct _virObjectPoolableHashElement { + virObjectLockable parent; + + char *primaryKey; + char *secondaryKey; +}; + virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); virClassPtr virClassForObjectPoolableHashTable(void); +virClassPtr virClassForObjectPoolableHashElement(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -152,6 +163,12 @@ virObjectPoolableHashTableNew(virClassPtr klass, bool primaryOnly) 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.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 17:03:01 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 1491480683572626.352290762867; Thu, 6 Apr 2017 05:11:23 -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 C3141F823; Thu, 6 Apr 2017 12:11:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8FF17A482C; Thu, 6 Apr 2017 12:11:19 +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 413FA5ED64; Thu, 6 Apr 2017 12:11:19 +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 v36CAxn9002325 for ; Thu, 6 Apr 2017 08:10:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id A0BDF171D0; Thu, 6 Apr 2017 12:10:59 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-19.phx2.redhat.com [10.3.116.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F5381719F for ; Thu, 6 Apr 2017 12:10:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C3141F823 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 C3141F823 From: John Ferlan To: libvir-list@redhat.com Date: Thu, 6 Apr 2017 08:10:52 -0400 Message-Id: <20170406121052.5815-9-jferlan@redhat.com> In-Reply-To: <20170406121052.5815-1-jferlan@redhat.com> References: <20170406121052.5815-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 8/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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 06 Apr 2017 12:11:22 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 51 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 6 ++++++ 3 files changed, 59 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 74d4bf8..b18614b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2236,6 +2236,8 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectNew; +virObjectPoolableHashElementGetPrimaryKey; +virObjectPoolableHashElementGetSecondaryKey; virObjectPoolableHashElementNew; virObjectPoolableHashTableGetPrimary; virObjectPoolableHashTableGetSecondary; diff --git a/src/util/virobject.c b/src/util/virobject.c index c9e631c..87bf56a 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -700,3 +700,54 @@ virObjectPoolableHashTableGetSecondary(void *anyobj) =20 return obj->objsSecondary; } + + +static virObjectPoolableHashElementPtr +virObjectGetPoolableHashElementObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectPoolableHashElementClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectPoolableHashElementCla= ss); + + return NULL; + +} + + +/** + * 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 36fd842..c1e40da 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -190,4 +190,10 @@ virObjectPoolableHashTableGetPrimary(void *anyobj); virHashTablePtr virObjectPoolableHashTableGetSecondary(void *anyobj); =20 +const char * +virObjectPoolableHashElementGetPrimaryKey(void *anyobj); + +const char * +virObjectPoolableHashElementGetSecondaryKey(void *anyobj); + #endif /* __VIR_OBJECT_H */ --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list