From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093108759187.24279211529142; Fri, 18 Aug 2017 14:51:48 -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 CF755356F2; Fri, 18 Aug 2017 21:51:46 +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 B0FCB5D961; Fri, 18 Aug 2017 21:51:46 +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 7E50B3FAD2; Fri, 18 Aug 2017 21:51:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILoifo004975 for ; Fri, 18 Aug 2017 17:50:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id EACDD5D962; Fri, 18 Aug 2017 21:50:44 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB2E25D961 for ; Fri, 18 Aug 2017 21:50:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CF755356F2 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:25 -0400 Message-Id: <20170818215041.8118-2-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 01/17] util: Use VIR_ERROR instead of VIR_WARN 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.30]); Fri, 18 Aug 2017 21:51:47 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than use VIR_WARN, let's be a bit more forceful and direct using VIR_ERROR so that the "average consumer" may actually be more concerned that something is wrong even though we still continue to operate. Signed-off-by: John Ferlan --- src/util/virobject.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/virobject.c b/src/util/virobject.c index cfa821c..38db692 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -49,17 +49,17 @@ struct _virClass { =20 #define VIR_OBJECT_NOTVALID(obj) (!obj || ((obj->u.s.magic & 0xFFFF0000) != =3D 0xCAFE0000)) =20 -#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) = \ +#define VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, objclass) = \ do { = \ virObjectPtr obj =3D anyobj; = \ if (VIR_OBJECT_NOTVALID(obj)) { = \ if (!obj) = \ - VIR_WARN("Object cannot be NULL"); = \ + VIR_ERROR(_("Object cannot be NULL")); = \ else = \ - VIR_WARN("Object %p has a bad magic number %X", = \ + VIR_ERROR(_("Object %p has a bad magic number %X"), = \ obj, obj->u.s.magic); = \ } else { = \ - VIR_WARN("Object %p (%s) is not a %s instance", = \ + VIR_ERROR(_("Object %p (%s) is not a %s instance"), = \ anyobj, obj->klass->name, #objclass); = \ } = \ } while (0) @@ -396,7 +396,7 @@ virObjectGetLockableObj(void *anyobj) if (virObjectIsClass(anyobj, virObjectLockableClass)) return anyobj; =20 - VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockable); + VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectLockable); return NULL; } =20 @@ -407,7 +407,7 @@ virObjectGetRWLockableObj(void *anyobj) if (virObjectIsClass(anyobj, virObjectRWLockableClass)) return anyobj; =20 - VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectRWLockable); + VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectRWLockable); return NULL; } =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093062518687.8436174502541; Fri, 18 Aug 2017 14:51:02 -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 74BEF81DE3; Fri, 18 Aug 2017 21:50:59 +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 CEFC260BF2; Fri, 18 Aug 2017 21:50:57 +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 3781E3FC72; Fri, 18 Aug 2017 21:50:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILojv1004981 for ; Fri, 18 Aug 2017 17:50:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4667D5D962; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1525B5D961 for ; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 74BEF81DE3 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:26 -0400 Message-Id: <20170818215041.8118-3-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 02/17] util: Introduce virObjectLookupKeys 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.25]); Fri, 18 Aug 2017 21:51:00 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new virObjectLockable child virObjectLookupKeys that can be used by various driver/vir*obj consumers as the means to define the lookup keys for an object. The API requires that at least the @key1 argument is provided as non-NULL thus ensuring that there's at least one key. The keys will be used as input to a soon to be introduced hash table object that will support one or two hash tables and manage the various add, search, and remove functionality using the keys. The initial consumer will be the driver/vir*obj.c APIs which will make use of the keys via the @UUID or @Name that is used to uniquely describe the object. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 88 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virobject.h | 18 ++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2149b11..082a0ba 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2318,6 +2318,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectLookupKeys; virClassForObjectRWLockable; virClassIsDerivedFrom; virClassName; @@ -2329,6 +2330,7 @@ virObjectListFree; virObjectListFreeCount; virObjectLock; virObjectLockableNew; +virObjectLookupKeysNew; virObjectNew; virObjectRef; virObjectRWLockableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 38db692..aec10eb 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -68,9 +68,11 @@ struct _virClass { static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; static virClassPtr virObjectRWLockableClass; +static virClassPtr virObjectLookupKeysClass; =20 static void virObjectLockableDispose(void *anyobj); static void virObjectRWLockableDispose(void *anyobj); +static void virObjectLookupKeysDispose(void *anyobj); =20 static int virObjectOnceInit(void) @@ -93,6 +95,12 @@ virObjectOnceInit(void) virObjectRWLockableDispos= e))) return -1; =20 + if (!(virObjectLookupKeysClass =3D virClassNew(virObjectLockableClass, + "virObjectLookupKeys", + sizeof(virObjectLookupKey= s), + virObjectLookupKeysDispos= e))) + return -1; + return 0; } =20 @@ -145,6 +153,21 @@ virClassForObjectRWLockable(void) =20 =20 /** + * virClassForObjectLookupKeys: + * + * Returns the class instance for the virObjectLookupKeys type + */ +virClassPtr +virClassForObjectLookupKeys(void) +{ + if (virObjectInitialize() < 0) + return NULL; + + return virObjectLookupKeysClass; +} + + +/** * virClassNew: * @parent: the parent class * @name: the class name @@ -328,6 +351,68 @@ virObjectRWLockableDispose(void *anyobj) =20 =20 /** + * virObjectLookupKeysNew: + * @klass: the klass to check + * @key1: key to be used for unique identifier (required) + * @key2: second key to be used as secondary unique identifier + * + * Create an object with at least @key1 as a means to provide input + * of an input object to add the object into a hash table. If @key2 is + * provided, then the object will exist into two hash tables for faster + * lookups by key for the table; otherwise, hash table searches would + * need to be used to find data from an object that matches some specific + * search the caller performs. + * + * Returns: New object on success, NULL on failure w/ error message set + */ +void * +virObjectLookupKeysNew(virClassPtr klass, + const char *key1, + const char *key2) +{ + virObjectLookupKeysPtr obj; + + if (!virClassIsDerivedFrom(klass, virClassForObjectLookupKeys())) { + virReportInvalidArg(klass, + _("Class %s must derive from virObjectLookupKe= ys"), + virClassName(klass)); + return NULL; + } + + if (!key1) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("key1 must be provided")); + return NULL; + } + + if (!(obj =3D virObjectLockableNew(klass))) + return NULL; + + if (VIR_STRDUP(obj->key1, key1) < 0) + goto error; + + if (VIR_STRDUP(obj->key2, key2) < 0) + goto error; + + return obj; + + error: + virObjectUnref(obj); + return NULL; +} + + +static void +virObjectLookupKeysDispose(void *anyobj) +{ + virObjectLookupKeysPtr obj =3D anyobj; + + VIR_FREE(obj->key1); + VIR_FREE(obj->key2); +} + + +/** * virObjectUnref: * @anyobj: any instance of virObjectPtr * @@ -393,7 +478,8 @@ virObjectRef(void *anyobj) static virObjectLockablePtr virObjectGetLockableObj(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectLockableClass)) + if (virObjectIsClass(anyobj, virObjectLockableClass) || + virObjectIsClass(anyobj, virObjectLookupKeysClass)) return anyobj; =20 VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectLockable); diff --git a/src/util/virobject.h b/src/util/virobject.h index ac6cf22..0f7d5ca 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -37,6 +37,9 @@ typedef virObjectLockable *virObjectLockablePtr; typedef struct _virObjectRWLockable virObjectRWLockable; typedef virObjectRWLockable *virObjectRWLockablePtr; =20 +typedef struct _virObjectLookupKeys virObjectLookupKeys; +typedef virObjectLookupKeys *virObjectLookupKeysPtr; + typedef void (*virObjectDisposeCallback)(void *obj); =20 /* Most code should not play with the contents of this struct; however, @@ -67,9 +70,18 @@ struct _virObjectRWLockable { virRWLock lock; }; =20 +struct _virObjectLookupKeys { + virObjectLockable parent; + + char *key1; + char *key2; +}; + + virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); virClassPtr virClassForObjectRWLockable(void); +virClassPtr virClassForObjectLookupKeys(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -120,6 +132,12 @@ void * virObjectRWLockableNew(virClassPtr klass) ATTRIBUTE_NONNULL(1); =20 +void * +virObjectLookupKeysNew(virClassPtr klass, + const char *key1, + const char *key2) + ATTRIBUTE_NONNULL(1); + 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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15030931403061016.6578642173916; Fri, 18 Aug 2017 14:52:20 -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 6D6975F7B8; Fri, 18 Aug 2017 21:52:18 +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 465DF18985; Fri, 18 Aug 2017 21:52:18 +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 0DA9C3FAD1; Fri, 18 Aug 2017 21:52:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILojOC004987 for ; Fri, 18 Aug 2017 17:50:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 94DB05D962; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63DB55D961 for ; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6D6975F7B8 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:27 -0400 Message-Id: <20170818215041.8118-4-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 03/17] util: Introduce virObjectLookupHash 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, 18 Aug 2017 21:52:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Using the virObjectRWLockable class as the base with the concepts from virObjectLookupKeys of either 1 or 2 lookup keys possible, create the virObjectLookupHash to manage a common mechanism to store objects for driver/vir*obj.c via self locking hash tables using the RW locking APIs. Each hash table will have: objsKey1 -> Hash table for the LookupKeys->key1 and may also have: objsKey2 -> Hash table for the LookupKeys->key2 A secondary benefit of self locking hash tables is each driver then does not have to keep a higher level driver lock for interactions with the object storage since the object itself can manage locking as needed. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 83 ++++++++++++++++++++++++++++++++++++++++++++= +++- src/util/virobject.h | 23 ++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 082a0ba..66cf865 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2318,6 +2318,7 @@ virNumaSetupMemoryPolicy; # util/virobject.h virClassForObject; virClassForObjectLockable; +virClassForObjectLookupHash; virClassForObjectLookupKeys; virClassForObjectRWLockable; virClassIsDerivedFrom; @@ -2330,6 +2331,7 @@ virObjectListFree; virObjectListFreeCount; virObjectLock; virObjectLockableNew; +virObjectLookupHashNew; virObjectLookupKeysNew; virObjectNew; virObjectRef; diff --git a/src/util/virobject.c b/src/util/virobject.c index aec10eb..f677b5f 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -69,10 +69,12 @@ static virClassPtr virObjectClass; static virClassPtr virObjectLockableClass; static virClassPtr virObjectRWLockableClass; static virClassPtr virObjectLookupKeysClass; +static virClassPtr virObjectLookupHashClass; =20 static void virObjectLockableDispose(void *anyobj); static void virObjectRWLockableDispose(void *anyobj); static void virObjectLookupKeysDispose(void *anyobj); +static void virObjectLookupHashDispose(void *anyobj); =20 static int virObjectOnceInit(void) @@ -101,6 +103,12 @@ virObjectOnceInit(void) virObjectLookupKeysDispos= e))) return -1; =20 + if (!(virObjectLookupHashClass =3D virClassNew(virObjectRWLockableClas= s, + "virObjectLookupHash", + sizeof(virObjectLookupHas= h), + virObjectLookupHashDispos= e))) + return -1; + return 0; } =20 @@ -168,6 +176,21 @@ virClassForObjectLookupKeys(void) =20 =20 /** + * virClassForObjectLookupHash: + * + * Returns the class instance for the virObjectLookupHash type + */ +virClassPtr +virClassForObjectLookupHash(void) +{ + if (virObjectInitialize() < 0) + return NULL; + + return virObjectLookupHashClass; +} + + +/** * virClassNew: * @parent: the parent class * @name: the class name @@ -413,6 +436,63 @@ virObjectLookupKeysDispose(void *anyobj) =20 =20 /** + * virObjectLookupHashNew: + * @klass: the klass to check + * @tableElemsStart: initial size of each hash table + * @createBoth: boolean to determine how many hash tables to create + * + * Create a new poolable hash table object for storing either 1 or 2 + * hash tables capable of storing virObjectLookupKeys objects. This + * object will use the RWLockable objects in order to allow for concurrent + * table reads by multiple threads looking to return lists of data. + * + * Returns: New object on success, NULL on failure w/ error message set + */ +void * +virObjectLookupHashNew(virClassPtr klass, + int tableElemsStart, + bool createBoth) +{ + virObjectLookupHashPtr obj; + + if (!virClassIsDerivedFrom(klass, virClassForObjectLookupHash())) { + virReportInvalidArg(klass, + _("Class %s must derive from virObjectLookupHa= sh"), + virClassName(klass)); + return NULL; + } + + if (!(obj =3D virObjectRWLockableNew(klass))) + return NULL; + + if (!(obj->objsKey1 =3D virHashCreate(tableElemsStart, + virObjectFreeHashData))) + goto error; + + if (createBoth && + !(obj->objsKey2 =3D virHashCreate(tableElemsStart, + virObjectFreeHashData))) + goto error; + + return obj; + + error: + virObjectUnref(obj); + return NULL; +} + + +static void +virObjectLookupHashDispose(void *anyobj) +{ + virObjectLookupHashPtr obj =3D anyobj; + + virHashFree(obj->objsKey1); + virHashFree(obj->objsKey2); +} + + +/** * virObjectUnref: * @anyobj: any instance of virObjectPtr * @@ -490,7 +570,8 @@ virObjectGetLockableObj(void *anyobj) static virObjectRWLockablePtr virObjectGetRWLockableObj(void *anyobj) { - if (virObjectIsClass(anyobj, virObjectRWLockableClass)) + if (virObjectIsClass(anyobj, virObjectRWLockableClass) || + virObjectIsClass(anyobj, virObjectLookupHashClass)) return anyobj; =20 VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectRWLockable); diff --git a/src/util/virobject.h b/src/util/virobject.h index 0f7d5ca..a5c03be 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; @@ -40,6 +41,9 @@ typedef virObjectRWLockable *virObjectRWLockablePtr; typedef struct _virObjectLookupKeys virObjectLookupKeys; typedef virObjectLookupKeys *virObjectLookupKeysPtr; =20 +typedef struct _virObjectLookupHash virObjectLookupHash; +typedef virObjectLookupHash *virObjectLookupHashPtr; + typedef void (*virObjectDisposeCallback)(void *obj); =20 /* Most code should not play with the contents of this struct; however, @@ -77,11 +81,24 @@ struct _virObjectLookupKeys { char *key2; }; =20 +struct _virObjectLookupHash { + virObjectRWLockable parent; + + /* key1 string -> object mapping for O(1), + * lockless lookup-by-key1 */ + virHashTable *objsKey1; + + /* key2 string -> object mapping for O(1), + * lockless lookup-by-key2 */ + virHashTable *objsKey2; +}; + =20 virClassPtr virClassForObject(void); virClassPtr virClassForObjectLockable(void); virClassPtr virClassForObjectRWLockable(void); virClassPtr virClassForObjectLookupKeys(void); +virClassPtr virClassForObjectLookupHash(void); =20 # ifndef VIR_PARENT_REQUIRED # define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1) @@ -138,6 +155,12 @@ virObjectLookupKeysNew(virClassPtr klass, const char *key2) ATTRIBUTE_NONNULL(1); =20 +void * +virObjectLookupHashNew(virClassPtr klass, + int tableElemsStart, + bool createBoth) + ATTRIBUTE_NONNULL(1); + 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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093143065588.6864829892829; Fri, 18 Aug 2017 14:52:23 -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 148804ACCA; Fri, 18 Aug 2017 21:52:21 +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 D5174707D4; Fri, 18 Aug 2017 21:52:20 +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 97F501800C8A; Fri, 18 Aug 2017 21:52:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILojKu004993 for ; Fri, 18 Aug 2017 17:50:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id E46775D962; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B3A735D961 for ; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 148804ACCA Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:28 -0400 Message-Id: <20170818215041.8118-5-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 04/17] util: Introduce virObjectLookupKeys*Active 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 18 Aug 2017 21:52:21 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a 'bool active' field to the virObjectLookupKeys object and then virObjectLookupKeysIsActive and virObjectLookupKeysSetActive API's to manage it. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 51 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 11 +++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 66cf865..9eb0589 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2332,7 +2332,9 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLookupHashNew; +virObjectLookupKeysIsActive; virObjectLookupKeysNew; +virObjectLookupKeysSetActive; virObjectNew; virObjectRef; virObjectRWLockableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index f677b5f..657597f 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -807,3 +807,54 @@ virObjectListFreeCount(void *list, =20 VIR_FREE(list); } + + +static virObjectLookupKeysPtr +virObjectGetLookupKeysObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectLookupKeysClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectLookupKeysClass); + + return NULL; +} + + +/** + * virObjectLookupKeysIsActive + * @anyobj: Pointer to a locked LookupKeys object + * + * Returns: True if object is active, false if not + */ +bool +virObjectLookupKeysIsActive(void *anyobj) +{ + virObjectLookupKeysPtr obj =3D virObjectGetLookupKeysObj(anyobj); + + if (!obj) + return false; + + return obj->active; +} + + +/** + * virObjectLookupKeysSetActive + * @anyobj: Pointer to a locked LookupKeys object + * @active: New active setting + * + * Set the lookup keys active bool value; value not changed if object + * is not a lookup keys object + */ +void +virObjectLookupKeysSetActive(void *anyobj, + bool active) +{ + virObjectLookupKeysPtr obj =3D virObjectGetLookupKeysObj(anyobj); + + if (!obj) + return; + + obj->active =3D active; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index a5c03be..0d3b90b 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -79,6 +79,8 @@ struct _virObjectLookupKeys { =20 char *key1; char *key2; + + bool active; /* true if object is active */ }; =20 struct _virObjectLookupHash { @@ -188,4 +190,13 @@ void virObjectListFreeCount(void *list, size_t count); =20 +bool +virObjectLookupKeysIsActive(void *anyobj) + ATTRIBUTE_NONNULL(1); + +void +virObjectLookupKeysSetActive(void *anyobj, + bool active) + ATTRIBUTE_NONNULL(1); + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093145673162.69909648096336; Fri, 18 Aug 2017 14:52:25 -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 7006C7F7B9; Fri, 18 Aug 2017 21:52:23 +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 4D857173DB; Fri, 18 Aug 2017 21:52:23 +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 19D803FAD2; Fri, 18 Aug 2017 21:52:23 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILokLH005002 for ; Fri, 18 Aug 2017 17:50:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 40E865D962; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 103E05D961 for ; Fri, 18 Aug 2017 21:50:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7006C7F7B9 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:29 -0400 Message-Id: <20170818215041.8118-6-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 05/17] util: Introduce virObjectLookupHash{Add|Remove} 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]); Fri, 18 Aug 2017 21:52:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a pair of API's to add/remove the LookupKeys object to/from the LookupHash object. The caller must check return status and handle failure properly for the Add. The Remove API callers are all void functions, so only report the problem and ignore the attempt to remove if for some reason the passed @tableobj is not as expected. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 92 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 10 ++++++ 3 files changed, 104 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9eb0589..c6d22d7 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2331,7 +2331,9 @@ virObjectListFree; virObjectListFreeCount; virObjectLock; virObjectLockableNew; +virObjectLookupHashAdd; virObjectLookupHashNew; +virObjectLookupHashRemove; virObjectLookupKeysIsActive; virObjectLookupKeysNew; virObjectLookupKeysSetActive; diff --git a/src/util/virobject.c b/src/util/virobject.c index 657597f..59c8ac6 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -858,3 +858,95 @@ virObjectLookupKeysSetActive(void *anyobj, =20 obj->active =3D active; } + + +static virObjectLookupHashPtr +virObjectGetLookupHashObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectLookupHashClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectLookupHashClass); + + return NULL; +} + + +/** + * virObjectLookupHashAdd: + * @anyobj: LookupHash object + * @obj: The LookupKeys object to insert in the hash table(s) + * + * Insert @obj into the hash tables found in @anyobj. Assumes that the + * caller has determined that the key1 and possibly key2 do not already + * exist in their respective hash table to be inserted. + * + * Returns 0 on success, -1 on failure. + */ +int +virObjectLookupHashAdd(void *anyobj, + virObjectLookupKeysPtr obj) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + + if (!hashObj) + return -1; + + if (obj->key2 && !hashObj->objsKey2) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("hashObj=3D%p has one table, but two keys from ob= j=3D%p"), + hashObj, obj); + return -1; + } + + if (virHashAddEntry(hashObj->objsKey1, obj->key1, obj) < 0) + return -1; + virObjectRef(obj); + + if (obj->key2) { + if (virHashAddEntry(hashObj->objsKey2, obj->key2, obj) < 0) { + virHashRemoveEntry(hashObj->objsKey1, obj->key1); + return -1; + } + virObjectRef(obj); + } + + return 0; +} + + +/** + * virObjectLookupHashRemove: + * @anyobj: LookupHash object + * @obj: The LookupKeys object to remove from the hash table(s) + * + * Remove @obj from the hash tables found in @anyobj. The common + * function to remove an object from a hash table will also cause + * the virObjectUnref to be called via virObjectFreeHashData since + * the virHashCreate used that as the Free object element argument. + * + * Even though this is a void, report the error for a bad @anyobj. + */ +void +virObjectLookupHashRemove(void *anyobj, + virObjectLookupKeysPtr obj) +{ + virObjectLookupHashPtr hashObj; + + if (!obj) + return; + + if (!(hashObj =3D virObjectGetLookupHashObj(anyobj))) + return; + + virObjectRef(obj); + virObjectUnlock(obj); + virObjectRWLockWrite(hashObj); + virObjectLock(obj); + virHashRemoveEntry(hashObj->objsKey1, obj->key1); + if (obj->key2) + virHashRemoveEntry(hashObj->objsKey2, obj->key2); + virObjectUnlock(obj); + virObjectUnref(obj); + virObjectRWUnlock(hashObj); +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 0d3b90b..bb02781 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -199,4 +199,14 @@ virObjectLookupKeysSetActive(void *anyobj, bool active) ATTRIBUTE_NONNULL(1); =20 +int +virObjectLookupHashAdd(void *anyobj, + virObjectLookupKeysPtr obj) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +void +virObjectLookupHashRemove(void *anyobj, + virObjectLookupKeysPtr obj) + ATTRIBUTE_NONNULL(1); + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093083387505.21607483656237; Fri, 18 Aug 2017 14:51: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 498DF883D1; Fri, 18 Aug 2017 21:51:21 +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 F3BCE6292C; Fri, 18 Aug 2017 21:51:20 +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 8B4A11806106; Fri, 18 Aug 2017 21:51:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILok3O005013 for ; Fri, 18 Aug 2017 17:50:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8ECE45D962; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5EC8F5D961 for ; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 498DF883D1 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:30 -0400 Message-Id: <20170818215041.8118-7-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 06/17] util: Introduce virObjectLookupHashFind[Locked] 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, 18 Aug 2017 21:51:21 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" These API's will use the virHashLookup in order to find the object in the hash table object by the specified @key. If two hash tables exist in the hash table object, then both will be searched for the key. Both API's will call an virObjectLookupHashFindInternal in order in handle the fetch. The virObjectLookupHashFindLocked is the primary workhorse and should only be called externally if the caller has taken the proper RW LookupHash lock. This is necessary in some paths, such as during Add/Assign processing where getting the RW Write lock early is required to ensure no other thread attempts to create/add the same object Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 69 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 10 +++++++ 3 files changed, 81 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c6d22d7..b6ab173 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2332,6 +2332,8 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLookupHashAdd; +virObjectLookupHashFind; +virObjectLookupHashFindLocked; virObjectLookupHashNew; virObjectLookupHashRemove; virObjectLookupKeysIsActive; diff --git a/src/util/virobject.c b/src/util/virobject.c index 59c8ac6..7dbfd1b 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -950,3 +950,72 @@ virObjectLookupHashRemove(void *anyobj, virObjectUnref(obj); virObjectRWUnlock(hashObj); } + + +static virObjectLookupKeysPtr +virObjectLookupHashFindInternal(virObjectLookupHashPtr hashObj, + const char *key) +{ + virObjectLookupKeysPtr obj; + + if ((obj =3D virHashLookup(hashObj->objsKey1, key))) + return virObjectRef(obj); + + if (hashObj->objsKey2) + obj =3D virHashLookup(hashObj->objsKey2, key); + + return virObjectRef(obj); +} + + +/** + * virObjectLookupHashFindLocked: + * @anyobj: LookupHash object + * @key: Key to use for lookup + * + * Search through the hash tables looking for the key. The key may be + * either key1 or key2 - both tables if they exist will be searched. + * + * NB: Assumes that the LookupHash has already been locked + * + * Returns a pointer to the entry with refcnt incremented or NULL on failu= re + */ +virObjectLookupKeysPtr +virObjectLookupHashFindLocked(void *anyobj, + const char *key) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + + if (!hashObj) + return NULL; + + return virObjectLookupHashFindInternal(anyobj, key); + +} + + +/** + * virObjectLookupHashFind: + * @anyobj: LookupHash object + * @key: Key to use for lookup + * + * Call virObjectLookupHashFindLocked after locking the LookupHash + * + * Returns a pointer to the entry with refcnt incremented or NULL on failu= re + */ +virObjectLookupKeysPtr +virObjectLookupHashFind(void *anyobj, + const char *key) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + virObjectLookupKeysPtr obj; + + if (!hashObj) + return NULL; + + virObjectRWLockRead(hashObj); + obj =3D virObjectLookupHashFindInternal(hashObj, key); + virObjectRWUnlock(hashObj); + + return obj; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index bb02781..dc668ca 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -209,4 +209,14 @@ virObjectLookupHashRemove(void *anyobj, virObjectLookupKeysPtr obj) ATTRIBUTE_NONNULL(1); =20 +virObjectLookupKeysPtr +virObjectLookupHashFindLocked(void *anyobj, + const char *key) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +virObjectLookupKeysPtr +virObjectLookupHashFind(void *anyobj, + const char *key) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093089976186.02898684440765; Fri, 18 Aug 2017 14:51:29 -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 16FB885541; Fri, 18 Aug 2017 21:51:28 +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 E2EC9183D1; Fri, 18 Aug 2017 21:51:27 +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 AF0F63FACD; Fri, 18 Aug 2017 21:51:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILokP8005020 for ; Fri, 18 Aug 2017 17:50:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id DED025D962; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id ACFEF5D961 for ; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 16FB885541 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:31 -0400 Message-Id: <20170818215041.8118-8-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 07/17] util: Introduce virObjectLookupHashForEach 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, 18 Aug 2017 21:51:28 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Introduce an API to use the virHashForEach API to go through each element of the LookupHash calling a callback with a LookupKey object entry from the LookupHash in order for it to be processed. Create a common structure _virObjectLookupHashForEachData to define the various pieces of data needed by the callback consumers. Fields described in the API function description section. Upon successful completion, the data.nElems is returned to the caller and possibly various fields within the structure filled in depending on the callers need as follows: vir{Object}NumOf{Elem} vir{Object}Get{Key} =3D> Callback function can be combined since generally all that the NumOf cares about is counts, but the Get function also adds some {Key} to a returned list of @names. vir{Object}Export =3D> When @maxElems =3D=3D -1, will cause allocation into data->elems of an array sized by the hash table size which will be used by the callback function to store addresses of objects specific to each {Object} that are typically associated with the virConnectListAll* API's from the virGet{Object}() calls. The @flags argument is typically used for additional filtering via vir{Object}Match or vir{Object}Filter APIs found in various vir*obj modules. Upon return from the call, the calling function must then move the data->elems into the returned structure. It is up to the callback function to set @error in the event of an error during processing resulting in calling the VIR_FREE() for each of the @nElems already in the array. When an error occurs, any collection done by the callback functions into the @elems array is discarded. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virobject.c | 74 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 21 ++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b6ab173..3978106 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2334,6 +2334,7 @@ virObjectLockableNew; virObjectLookupHashAdd; virObjectLookupHashFind; virObjectLookupHashFindLocked; +virObjectLookupHashForEach; virObjectLookupHashNew; virObjectLookupHashRemove; virObjectLookupKeysIsActive; diff --git a/src/util/virobject.c b/src/util/virobject.c index 7dbfd1b..a76f1cb 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -1019,3 +1019,77 @@ virObjectLookupHashFind(void *anyobj, =20 return obj; } + + +/** + * virObjectLookupHashForEach + * @anyobj: LookupHash object + * @callback: callback function to handle the object specific checks + * @opaque: callback data + * + * For each element of the objsKey1 hash table make a call into the + * callback routine to handle its task. Even if there were two hash + * tables all the objects exist in both, so it's only necessary to + * run through one of them. + * + * NB: + * struct _virObjectLookupHashForEachData { + * virConnectPtr conn; -> Connect ptr for @filter APIs + * void *opaque; -> Opaque data as determined by caller + * void *filter; -> A pointer to function for ACL calls + * bool wantActive; -> Filter active objs + * bool error; -> Set by callback functions for error + * const char *matchStr; -> Filter for specific string in many objs + * unsigned int flags; -> @flags argument to for Export calls + * int nElems; -> # of elements found and passing filters + * void **elems; -> array of elements + * int maxElems; -> maximum # of elements to collect + * Use -1 to allocate array of N table sized + * elements to use for Export functions + * Use -2 for NumOf functions to avoid the + * allocation, but allow sharing with the + * GetNames type functions + * }; + * + * Returns number of elements found on success, -1 on failure + */ +int +virObjectLookupHashForEach(void *anyobj, + virHashIterator callback, + virObjectLookupHashForEachDataPtr data) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + + if (!hashObj) + return -1; + + if (data->maxElems =3D=3D -1) { + if (VIR_ALLOC_N(data->elems, virHashSize(hashObj->objsKey1) + 1) <= 0) + return -1; + } + + virObjectRWLockRead(hashObj); + virHashForEach(hashObj->objsKey1, callback, data); + virObjectRWUnlock(hashObj); + + if (data->error) + goto error; + + if (data->maxElems =3D=3D -1) { + /* trim the array to the final size */ + ignore_value(VIR_REALLOC_N(data->elems, data->nElems + 1)); + } + + return data->nElems; + + error: + if (data->elems) { + if (data->maxElems =3D=3D -1) { + virObjectListFree(data->elems); + } else { + while (--data->nElems) + VIR_FREE(data->elems[data->nElems]); + } + } + return -1; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index dc668ca..ddebf6c 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -219,4 +219,25 @@ virObjectLookupHashFind(void *anyobj, const char *key) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 +typedef struct _virObjectLookupHashForEachData virObjectLookupHashForEachD= ata; +typedef virObjectLookupHashForEachData *virObjectLookupHashForEachDataPtr; +struct _virObjectLookupHashForEachData { + virConnectPtr conn; + void *opaque; + void *filter; + bool wantActive; + bool error; + const char *matchStr; + unsigned int flags; + int nElems; + void **elems; + int maxElems; +}; + +int +virObjectLookupHashForEach(void *anyobj, + virHashIterator callback, + virObjectLookupHashForEachDataPtr data) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093147719861.6949226195783; Fri, 18 Aug 2017 14:52:27 -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 E0B0A155E2; Fri, 18 Aug 2017 21:52:25 +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 BB0DE173DB; Fri, 18 Aug 2017 21:52:25 +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 800B31803B21; Fri, 18 Aug 2017 21:52:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILolAJ005026 for ; Fri, 18 Aug 2017 17:50:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3B6B45D962; Fri, 18 Aug 2017 21:50:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09C615D961 for ; Fri, 18 Aug 2017 21:50:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E0B0A155E2 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:32 -0400 Message-Id: <20170818215041.8118-9-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 08/17] util: Introduce virObjectLookupHashSearch[Locked] 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.29]); Fri, 18 Aug 2017 21:52:26 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" A common object API wrapper to use the virHashSearch API to search the LookupHash for specific data defined in the @opaque parameter. Once data is found, the search would end and the LookupKeys object that is represented is returned locked with it's reference count incremented. The virObjectLookupHashSearchLocked is the workhorse, but similar to the virObjectLookupFind* APIs may be required an Add or AssignDef caller has the Write lock already to ensure no other thread will grab the lock and add the same or competing object. It is up to the caller to unlock and lower the refcnt once done with the object and of course handle a NULL return indicating no object found. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 2 ++ src/util/virobject.c | 74 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 12 ++++++++ 3 files changed, 88 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3978106..df3f246 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2337,6 +2337,8 @@ virObjectLookupHashFindLocked; virObjectLookupHashForEach; virObjectLookupHashNew; virObjectLookupHashRemove; +virObjectLookupHashSearch; +virObjectLookupHashSearchLocked; virObjectLookupKeysIsActive; virObjectLookupKeysNew; virObjectLookupKeysSetActive; diff --git a/src/util/virobject.c b/src/util/virobject.c index a76f1cb..17ea4e6 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -1093,3 +1093,77 @@ virObjectLookupHashForEach(void *anyobj, } return -1; } + + +static virObjectLookupKeysPtr +virObjectLookupHashSearchInternal(virObjectLookupHashPtr hashObj, + virHashSearcher callback, + void *opaque) +{ + virObjectLookupKeysPtr obj; + + obj =3D virHashSearch(hashObj->objsKey1, callback, opaque, NULL); + virObjectRef(obj); + + if (obj) + virObjectLock(obj); + + return obj; +} + + +/** + * virObjectLookupHashSearchLocked + * @anyobj: LookupHash object + * @callback: callback function to handle the object specific checks + * @opaque: callback data + * + * Search the hash table objsKey1 table calling the specified @callback + * routine with an object and @opaque data in order to determine whether + * the object is represented by the @opaque data. + * + * NB: Caller assumes the responsibility for locking LookupHash + * + * Returns locked/refcnt incremented object on success, NULL on failure + */ +virObjectLookupKeysPtr +virObjectLookupHashSearchLocked(void *anyobj, + virHashSearcher callback, + void *opaque) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + + if (!hashObj) + return NULL; + + return virObjectLookupHashSearchInternal(hashObj, callback, opaque); +} + + +/** + * virObjectLookupHashSearch + * @anyobj: LookupHash object + * @callback: callback function to handle the object specific checks + * @opaque: callback data + * + * Call virObjectLookupHashSearchLocked with a locked hash table + * + * Returns @obj from virObjectLookupHashSearchLocked + */ +virObjectLookupKeysPtr +virObjectLookupHashSearch(void *anyobj, + virHashSearcher callback, + void *opaque) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + virObjectLookupKeysPtr obj; + + if (!hashObj) + return NULL; + + virObjectRWLockRead(hashObj); + obj =3D virObjectLookupHashSearchInternal(hashObj, callback, opaque); + virObjectRWUnlock(hashObj); + + return obj; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index ddebf6c..d4bc3c3 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -240,4 +240,16 @@ virObjectLookupHashForEach(void *anyobj, virObjectLookupHashForEachDataPtr data) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); =20 +virObjectLookupKeysPtr +virObjectLookupHashSearchLocked(void *anyobj, + virHashSearcher callback, + void *opaque) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +virObjectLookupKeysPtr +virObjectLookupHashSearch(void *anyobj, + virHashSearcher callback, + void *opaque) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093110940288.10604262524373; Fri, 18 Aug 2017 14:51: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 549EC4E341; Fri, 18 Aug 2017 21:51:49 +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 2CE90183C5; Fri, 18 Aug 2017 21:51:49 +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 EAC731806108; Fri, 18 Aug 2017 21:51:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILolws005032 for ; Fri, 18 Aug 2017 17:50:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9C7B55D962; Fri, 18 Aug 2017 21:50:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5A6725D961 for ; Fri, 18 Aug 2017 21:50:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 549EC4E341 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:33 -0400 Message-Id: <20170818215041.8118-10-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 09/17] nodedev: Use virObjectLookup{Keys|Hash} 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.38]); Fri, 18 Aug 2017 21:51:49 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Use the virObjectLookupKeys in _virNodeDeviceObj and use the virObjectLookupHash in _virNodeDeviceObjList. Convert the code to use the LookupHash object and APIs rather than code within this module that uses virHash* calls. Since the _virNodeDeviceObjList only now contains the @parent object, the virClassNew must be removed from OnceInit because instantiation would fail since the object size would be the same as the parent object size. Usage of HashLookup{Find|Search} APIs returns a locked/reffed object so remove the virObjectLock after FindBy*Locked calls. The only function requiring taking a lock is the Add function since it needs to be synchronized in such a way to avoid multiple threads attempting to add the same named node device at the same time. Also change the virNodeDeviceObjListFilter to follow convention of other drivers of virNodeDeviceObjListACLFilter. The NumOfDevicesCallback and GetNamesCallback can use the same callback function with the only difference being the filling in of the @names array from the passed @data structure if it exists. Signed-off-by: John Ferlan --- src/conf/virnodedeviceobj.c | 286 +++++++++++++---------------------------= ---- 1 file changed, 80 insertions(+), 206 deletions(-) diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index b0dcee1..7f025b6 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -34,41 +34,28 @@ VIR_LOG_INIT("conf.virnodedeviceobj"); =20 struct _virNodeDeviceObj { - virObjectLockable parent; + virObjectLookupKeys parent; =20 virNodeDeviceDefPtr def; /* device definition */ }; =20 struct _virNodeDeviceObjList { - virObjectLockable parent; - - /* name string -> virNodeDeviceObj mapping - * for O(1), lockless lookup-by-name */ - virHashTable *objs; - + virObjectLookupHash parent; }; =20 =20 static virClassPtr virNodeDeviceObjClass; -static virClassPtr virNodeDeviceObjListClass; static void virNodeDeviceObjDispose(void *opaque); -static void virNodeDeviceObjListDispose(void *opaque); =20 static int virNodeDeviceObjOnceInit(void) { - if (!(virNodeDeviceObjClass =3D virClassNew(virClassForObjectLockable(= ), + if (!(virNodeDeviceObjClass =3D virClassNew(virClassForObjectLookupKey= s(), "virNodeDeviceObj", sizeof(virNodeDeviceObj), virNodeDeviceObjDispose))) return -1; =20 - if (!(virNodeDeviceObjListClass =3D virClassNew(virClassForObjectLocka= ble(), - "virNodeDeviceObjList", - sizeof(virNodeDeviceObjL= ist), - virNodeDeviceObjListDisp= ose))) - return -1; - return 0; } =20 @@ -85,14 +72,14 @@ virNodeDeviceObjDispose(void *opaque) =20 =20 static virNodeDeviceObjPtr -virNodeDeviceObjNew(void) +virNodeDeviceObjNew(const char *name) { virNodeDeviceObjPtr obj; =20 if (virNodeDeviceObjInitialize() < 0) return NULL; =20 - if (!(obj =3D virObjectLockableNew(virNodeDeviceObjClass))) + if (!(obj =3D virObjectLookupKeysNew(virNodeDeviceObjClass, name, NULL= ))) return NULL; =20 virObjectLock(obj); @@ -229,17 +216,9 @@ virNodeDeviceObjListSearch(virNodeDeviceObjListPtr dev= s, virHashSearcher callback, const void *data) { - virNodeDeviceObjPtr obj; - - virObjectLock(devs); - obj =3D virHashSearch(devs->objs, callback, data, NULL); - virObjectRef(obj); - virObjectUnlock(devs); - - if (obj) - virObjectLock(obj); - - return obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashSearch(devs, callbac= k, + (void *)data); + return (virNodeDeviceObjPtr) obj; } =20 =20 @@ -274,7 +253,8 @@ static virNodeDeviceObjPtr virNodeDeviceObjListFindByNameLocked(virNodeDeviceObjListPtr devs, const char *name) { - return virObjectRef(virHashLookup(devs->objs, name)); + virObjectLookupKeysPtr obj =3D virObjectLookupHashFindLocked(devs, nam= e); + return (virNodeDeviceObjPtr) obj; } =20 =20 @@ -282,15 +262,8 @@ virNodeDeviceObjPtr virNodeDeviceObjListFindByName(virNodeDeviceObjListPtr devs, const char *name) { - virNodeDeviceObjPtr obj; - - virObjectLock(devs); - obj =3D virNodeDeviceObjListFindByNameLocked(devs, name); - virObjectUnlock(devs); - if (obj) - virObjectLock(obj); - - return obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFind(devs, name); + return (virNodeDeviceObjPtr) obj; } =20 =20 @@ -445,32 +418,10 @@ virNodeDeviceObjListFindSCSIHostByWWNs(virNodeDeviceO= bjListPtr devs, } =20 =20 -static void -virNodeDeviceObjListDispose(void *obj) -{ - virNodeDeviceObjListPtr devs =3D obj; - - virHashFree(devs->objs); -} - - virNodeDeviceObjListPtr virNodeDeviceObjListNew(void) { - virNodeDeviceObjListPtr devs; - - if (virNodeDeviceObjInitialize() < 0) - return NULL; - - if (!(devs =3D virObjectLockableNew(virNodeDeviceObjListClass))) - return NULL; - - if (!(devs->objs =3D virHashCreate(50, virObjectFreeHashData))) { - virObjectUnref(devs); - return NULL; - } - - return devs; + return virObjectLookupHashNew(virClassForObjectLookupHash(), 50, false= ); } =20 =20 @@ -486,29 +437,31 @@ virNodeDeviceObjListAssignDef(virNodeDeviceObjListPtr= devs, virNodeDeviceDefPtr def) { virNodeDeviceObjPtr obj; + virNodeDeviceObjPtr ret =3D NULL; =20 - virObjectLock(devs); + virObjectRWLockWrite(devs); =20 if ((obj =3D virNodeDeviceObjListFindByNameLocked(devs, def->name))) { - virObjectLock(obj); virNodeDeviceDefFree(obj->def); obj->def =3D def; } else { - if (!(obj =3D virNodeDeviceObjNew())) + if (!(obj =3D virNodeDeviceObjNew(def->name))) goto cleanup; =20 - if (virHashAddEntry(devs->objs, def->name, obj) < 0) { - virNodeDeviceObjEndAPI(&obj); + if (virObjectLookupHashAdd(devs, (virObjectLookupKeysPtr)obj) < 0) goto cleanup; - } =20 obj->def =3D def; virObjectRef(obj); } =20 + ret =3D obj; + obj =3D NULL; + cleanup: - virObjectUnlock(devs); - return obj; + virNodeDeviceObjEndAPI(&obj); + virObjectRWUnlock(devs); + return ret; } =20 =20 @@ -516,20 +469,7 @@ void virNodeDeviceObjListRemove(virNodeDeviceObjListPtr devs, virNodeDeviceObjPtr obj) { - virNodeDeviceDefPtr def; - - if (!obj) - return; - def =3D obj->def; - - virObjectRef(obj); - virObjectUnlock(obj); - virObjectLock(devs); - virObjectLock(obj); - virHashRemoveEntry(devs->objs, def->name); - virObjectUnlock(obj); - virObjectUnref(obj); - virObjectUnlock(devs); + virObjectLookupHashRemove(devs, (virObjectLookupKeysPtr)obj); } =20 =20 @@ -730,29 +670,33 @@ virNodeDeviceCapMatch(virNodeDeviceObjPtr obj, } =20 =20 -struct virNodeDeviceCountData { - virConnectPtr conn; - virNodeDeviceObjListFilter filter; - const char *matchstr; - int count; -}; - static int -virNodeDeviceObjListNumOfDevicesCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) +virNodeDeviceObjListGetHelper(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) { virNodeDeviceObjPtr obj =3D payload; virNodeDeviceDefPtr def; - struct virNodeDeviceCountData *data =3D opaque; + virObjectLookupHashForEachDataPtr data =3D opaque; virNodeDeviceObjListFilter filter =3D data->filter; + char **names =3D (char **)data->elems; + + if (data->error) + return 0; =20 virObjectLock(obj); def =3D obj->def; + if ((!filter || filter(data->conn, def)) && - (!data->matchstr || virNodeDeviceObjHasCap(obj, data->matchstr))) - data->count++; + (!data->matchStr || virNodeDeviceObjHasCap(obj, data->matchStr))) { + if (names && VIR_STRDUP(names[data->nElems], def->name) < 0) { + data->error =3D true; + goto cleanup; + } + data->nElems++; + } =20 + cleanup: virObjectUnlock(obj); return 0; } @@ -764,55 +708,11 @@ virNodeDeviceObjListNumOfDevices(virNodeDeviceObjList= Ptr devs, const char *cap, virNodeDeviceObjListFilter filter) { - struct virNodeDeviceCountData data =3D { - .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .count =3D = 0 }; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .matchStr = =3D cap, + .nElems =3D 0, .elems =3D NULL, .maxElems =3D -2 }; =20 - virObjectLock(devs); - virHashForEach(devs->objs, virNodeDeviceObjListNumOfDevicesCallback, &= data); - virObjectUnlock(devs); - - return data.count; -} - - -struct virNodeDeviceGetNamesData { - virConnectPtr conn; - virNodeDeviceObjListFilter filter; - const char *matchstr; - int nnames; - char **names; - int maxnames; - bool error; -}; - -static int -virNodeDeviceObjListGetNamesCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - virNodeDeviceObjPtr obj =3D payload; - virNodeDeviceDefPtr def; - struct virNodeDeviceGetNamesData *data =3D opaque; - virNodeDeviceObjListFilter filter =3D data->filter; - - if (data->error) - return 0; - - virObjectLock(obj); - def =3D obj->def; - - if ((!filter || filter(data->conn, def)) && - (!data->matchstr || virNodeDeviceObjHasCap(obj, data->matchstr))) { - if (VIR_STRDUP(data->names[data->nnames], def->name) < 0) { - data->error =3D true; - goto cleanup; - } - data->nnames++; - } - - cleanup: - virObjectUnlock(obj); - return 0; + return virObjectLookupHashForEach(devs, virNodeDeviceObjListGetHelper,= &data); } =20 =20 @@ -824,23 +724,11 @@ virNodeDeviceObjListGetNames(virNodeDeviceObjListPtr = devs, char **const names, int maxnames) { - struct virNodeDeviceGetNamesData data =3D { - .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .names =3D = names, - .nnames =3D 0, .maxnames =3D maxnames, .error =3D false }; - - virObjectLock(devs); - virHashForEach(devs->objs, virNodeDeviceObjListGetNamesCallback, &data= ); - virObjectUnlock(devs); - - if (data.error) - goto error; - - return data.nnames; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .matchStr = =3D cap, + .nElems =3D 0, .elems =3D (void **)names, .maxElems =3D maxnames }; =20 - error: - while (--data.nnames) - VIR_FREE(data.names[data.nnames]); - return -1; + return virObjectLookupHashForEach(devs, virNodeDeviceObjListGetHelper,= &data); } =20 =20 @@ -876,15 +764,6 @@ virNodeDeviceMatch(virNodeDeviceObjPtr obj, #undef MATCH =20 =20 -struct virNodeDeviceObjListExportData { - virConnectPtr conn; - virNodeDeviceObjListFilter filter; - unsigned int flags; - virNodeDevicePtr *devices; - int ndevices; - bool error; -}; - static int virNodeDeviceObjListExportCallback(void *payload, const void *name ATTRIBUTE_UNUSED, @@ -892,8 +771,10 @@ virNodeDeviceObjListExportCallback(void *payload, { virNodeDeviceObjPtr obj =3D payload; virNodeDeviceDefPtr def; - struct virNodeDeviceObjListExportData *data =3D opaque; + virObjectLookupHashForEachDataPtr data =3D opaque; + virNodeDeviceObjListFilter filter =3D data->filter; virNodeDevicePtr device =3D NULL; + virNodeDevicePtr *devices =3D (virNodeDevicePtr *)data->elems; =20 if (data->error) return 0; @@ -901,19 +782,24 @@ virNodeDeviceObjListExportCallback(void *payload, virObjectLock(obj); def =3D obj->def; =20 - if ((!data->filter || data->filter(data->conn, def)) && - virNodeDeviceMatch(obj, data->flags)) { - if (data->devices) { - if (!(device =3D virGetNodeDevice(data->conn, def->name)) || - VIR_STRDUP(device->parent, def->parent) < 0) { - virObjectUnref(device); - data->error =3D true; - goto cleanup; - } - data->devices[data->ndevices] =3D device; - } - data->ndevices++; + if (filter && !filter(data->conn, def)) + goto cleanup; + + if (!virNodeDeviceMatch(obj, data->flags)) + goto cleanup; + + if (!devices) { + data->nElems++; + goto cleanup; + } + + if (!(device =3D virGetNodeDevice(data->conn, def->name)) || + VIR_STRDUP(device->parent, def->parent) < 0) { + virObjectUnref(device); + data->error =3D true; + goto cleanup; } + devices[data->nElems++] =3D device; =20 cleanup: virObjectUnlock(obj); @@ -928,31 +814,19 @@ virNodeDeviceObjListExport(virConnectPtr conn, virNodeDeviceObjListFilter filter, unsigned int flags) { - struct virNodeDeviceObjListExportData data =3D { - .conn =3D conn, .filter =3D filter, .flags =3D flags, - .devices =3D NULL, .ndevices =3D 0, .error =3D false }; - - virObjectLock(devs); - if (devices && - VIR_ALLOC_N(data.devices, virHashSize(devs->objs) + 1) < 0) { - virObjectUnlock(devs); - return -1; - } - - virHashForEach(devs->objs, virNodeDeviceObjListExportCallback, &data); - virObjectUnlock(devs); + int ret; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .nElems =3D = 0, + .elems =3D NULL, .maxElems =3D 0, .flags =3D flags }; =20 - if (data.error) - goto cleanup; + if (devices) + data.maxElems =3D -1; =20 - if (data.devices) { - ignore_value(VIR_REALLOC_N(data.devices, data.ndevices + 1)); - *devices =3D data.devices; - } + ret =3D virObjectLookupHashForEach(devs, virNodeDeviceObjListExportCal= lback, + &data); =20 - return data.ndevices; + if (devices) + *devices =3D (virNodeDevicePtr *)data.elems; =20 - cleanup: - virObjectListFree(data.devices); - return -1; + return ret; } --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093094431660.2566455078326; Fri, 18 Aug 2017 14:51:34 -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 94E8EC0587C4; Fri, 18 Aug 2017 21:51:32 +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 7396B5D965; Fri, 18 Aug 2017 21:51:32 +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 3D8791806109; Fri, 18 Aug 2017 21:51:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILolEM005039 for ; Fri, 18 Aug 2017 17:50:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id ECB5A5D962; Fri, 18 Aug 2017 21:50:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B9A7E5D961 for ; Fri, 18 Aug 2017 21:50:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 94E8EC0587C4 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:34 -0400 Message-Id: <20170818215041.8118-11-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 10/17] secret: Use virObjectLookup{Keys|Hash} 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.32]); Fri, 18 Aug 2017 21:51:33 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Use the virObjectLookupKeys in _virSecretObj and use the virObjectLookupHash in _virSecretObjList. Convert the code to use the LookupHash object and APIs rather than local code and usage of the virHash* calls. Since the _virSecretObjList only now contains the @parent object, the virClassNew must be removed from OnceInit because instantiation would fail since the object size would be the same as the parent object size. Usage of HashLookup{Find|Search} API's returns a locked/reffed object so need to remove virObjectLock after FindBy*Locked calls. The only function requiring a taking a lock is the Add function since it needs to be synchronized in such a way to avoid multiple threads attempting to add the same secret via UUID or UsageID at the same time. NB: We cannot make usageID a LookupHash key because it's possible that the usageType is NONE and thus usageID is NULL, thus leaving the only "unique" element the UUID. The NumOfSecretsCallback and GetUUIDsCallback can use the same callback function with the only difference being the filling in of the @uuids array from the passed @data structure if it exists. Signed-off-by: John Ferlan --- src/conf/virsecretobj.c | 263 +++++++++++++-------------------------------= ---- 1 file changed, 68 insertions(+), 195 deletions(-) diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 4dca152..0a0e40f 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -38,7 +38,7 @@ VIR_LOG_INIT("conf.virsecretobj"); =20 struct _virSecretObj { - virObjectLockable parent; + virObjectLookupKeys parent; char *configFile; char *base64File; virSecretDefPtr def; @@ -47,16 +47,10 @@ struct _virSecretObj { }; =20 static virClassPtr virSecretObjClass; -static virClassPtr virSecretObjListClass; static void virSecretObjDispose(void *obj); -static void virSecretObjListDispose(void *obj); =20 struct _virSecretObjList { - virObjectLockable parent; - - /* uuid string -> virSecretObj mapping - * for O(1), lockless lookup-by-uuid */ - virHashTable *objs; + virObjectLookupHash parent; }; =20 struct virSecretSearchData { @@ -68,18 +62,12 @@ struct virSecretSearchData { static int virSecretObjOnceInit(void) { - if (!(virSecretObjClass =3D virClassNew(virClassForObjectLockable(), + if (!(virSecretObjClass =3D virClassNew(virClassForObjectLookupKeys(), "virSecretObj", sizeof(virSecretObj), virSecretObjDispose))) return -1; =20 - if (!(virSecretObjListClass =3D virClassNew(virClassForObjectLockable(= ), - "virSecretObjList", - sizeof(virSecretObjList), - virSecretObjListDispose))) - return -1; - return 0; } =20 @@ -87,14 +75,14 @@ virSecretObjOnceInit(void) VIR_ONCE_GLOBAL_INIT(virSecretObj) =20 static virSecretObjPtr -virSecretObjNew(void) +virSecretObjNew(const char *uuidstr) { virSecretObjPtr obj; =20 if (virSecretObjInitialize() < 0) return NULL; =20 - if (!(obj =3D virObjectLockableNew(virSecretObjClass))) + if (!(obj =3D virObjectLookupKeysNew(virSecretObjClass, uuidstr, NULL)= )) return NULL; =20 virObjectLock(obj); @@ -118,20 +106,7 @@ virSecretObjEndAPI(virSecretObjPtr *obj) virSecretObjListPtr virSecretObjListNew(void) { - virSecretObjListPtr secrets; - - if (virSecretObjInitialize() < 0) - return NULL; - - if (!(secrets =3D virObjectLockableNew(virSecretObjListClass))) - return NULL; - - if (!(secrets->objs =3D virHashCreate(50, virObjectFreeHashData))) { - virObjectUnref(secrets); - return NULL; - } - - return secrets; + return virObjectLookupHashNew(virClassForObjectLookupHash(), 50, false= ); } =20 =20 @@ -151,15 +126,6 @@ virSecretObjDispose(void *opaque) } =20 =20 -static void -virSecretObjListDispose(void *obj) -{ - virSecretObjListPtr secrets =3D obj; - - virHashFree(secrets->objs); -} - - /** * virSecretObjFindByUUIDLocked: * @secrets: list of secret objects @@ -173,7 +139,8 @@ static virSecretObjPtr virSecretObjListFindByUUIDLocked(virSecretObjListPtr secrets, const char *uuidstr) { - return virObjectRef(virHashLookup(secrets->objs, uuidstr)); + virObjectLookupKeysPtr obj =3D virObjectLookupHashFindLocked(secrets, = uuidstr); + return (virSecretObjPtr)obj; } =20 =20 @@ -191,14 +158,9 @@ virSecretObjPtr virSecretObjListFindByUUID(virSecretObjListPtr secrets, const char *uuidstr) { - virSecretObjPtr obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFind(secrets, uuidst= r); =20 - virObjectLock(secrets); - obj =3D virSecretObjListFindByUUIDLocked(secrets, uuidstr); - virObjectUnlock(secrets); - if (obj) - virObjectLock(obj); - return obj; + return (virSecretObjPtr) obj; } =20 =20 @@ -243,14 +205,12 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr= secrets, int usageType, const char *usageID) { - virSecretObjPtr obj =3D NULL; + virObjectLookupKeysPtr obj =3D NULL; struct virSecretSearchData data =3D { .usageType =3D usageType, .usageID =3D usageID }; =20 - obj =3D virHashSearch(secrets->objs, virSecretObjSearchName, &data, NU= LL); - if (obj) - virObjectRef(obj); - return obj; + obj =3D virObjectLookupHashSearchLocked(secrets, virSecretObjSearchNam= e, &data); + return (virSecretObjPtr)obj; } =20 =20 @@ -263,6 +223,12 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr = secrets, * This function locks @secrets and finds the secret object which * corresponds to @usageID of @usageType. * + * NB: The usageID cannot be used as a hash table key because + * virSecretDefParseUsage will not fill in the def->usage_id + * if the def->usage_type is VIR_SECRET_USAGE_TYPE_NONE, thus + * we cannot use def->usage_id as a key since both keys must + * be present in every object in order to be valid. + * * Returns: locked and ref'd secret object. */ virSecretObjPtr @@ -270,14 +236,12 @@ virSecretObjListFindByUsage(virSecretObjListPtr secre= ts, int usageType, const char *usageID) { - virSecretObjPtr obj; + virObjectLookupKeysPtr obj =3D NULL; + struct virSecretSearchData data =3D { .usageType =3D usageType, + .usageID =3D usageID }; =20 - virObjectLock(secrets); - obj =3D virSecretObjListFindByUsageLocked(secrets, usageType, usageID); - virObjectUnlock(secrets); - if (obj) - virObjectLock(obj); - return obj; + obj =3D virObjectLookupHashSearch(secrets, virSecretObjSearchName, &da= ta); + return (virSecretObjPtr)obj; } =20 =20 @@ -294,23 +258,7 @@ void virSecretObjListRemove(virSecretObjListPtr secrets, virSecretObjPtr obj) { - char uuidstr[VIR_UUID_STRING_BUFLEN]; - virSecretDefPtr def; - - if (!obj) - return; - def =3D obj->def; - - virUUIDFormat(def->uuid, uuidstr); - virObjectRef(obj); - virObjectUnlock(obj); - - virObjectLock(secrets); - virObjectLock(obj); - virHashRemoveEntry(secrets->objs, uuidstr); - virObjectUnlock(obj); - virObjectUnref(obj); - virObjectUnlock(secrets); + virObjectLookupHashRemove(secrets, (virObjectLookupKeysPtr)obj); } =20 =20 @@ -336,7 +284,7 @@ virSecretObjListAdd(virSecretObjListPtr secrets, virSecretObjPtr ret =3D NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; =20 - virObjectLock(secrets); + virObjectRWLockWrite(secrets); =20 if (oldDef) *oldDef =3D NULL; @@ -345,7 +293,6 @@ virSecretObjListAdd(virSecretObjListPtr secrets, =20 /* Is there a secret already matching this UUID */ if ((obj =3D virSecretObjListFindByUUIDLocked(secrets, uuidstr))) { - virObjectLock(obj); objdef =3D obj->def; =20 if (STRNEQ_NULLABLE(objdef->usage_id, newdef->usage_id)) { @@ -373,7 +320,6 @@ virSecretObjListAdd(virSecretObjListPtr secrets, if ((obj =3D virSecretObjListFindByUsageLocked(secrets, newdef->usage_type, newdef->usage_id))) { - virObjectLock(obj); objdef =3D obj->def; virUUIDFormat(objdef->uuid, uuidstr); virReportError(VIR_ERR_INTERNAL_ERROR, @@ -383,7 +329,7 @@ virSecretObjListAdd(virSecretObjListPtr secrets, goto cleanup; } =20 - if (!(obj =3D virSecretObjNew())) + if (!(obj =3D virSecretObjNew(uuidstr))) goto cleanup; =20 /* Generate the possible configFile and base64File strings @@ -393,11 +339,10 @@ virSecretObjListAdd(virSecretObjListPtr secrets, !(obj->base64File =3D virFileBuildPath(configDir, uuidstr, ".b= ase64"))) goto cleanup; =20 - if (virHashAddEntry(secrets->objs, uuidstr, obj) < 0) + if (virObjectLookupHashAdd(secrets, (virObjectLookupKeysPtr)obj) <= 0) goto cleanup; =20 obj->def =3D newdef; - virObjectRef(obj); } =20 ret =3D obj; @@ -405,72 +350,35 @@ virSecretObjListAdd(virSecretObjListPtr secrets, =20 cleanup: virSecretObjEndAPI(&obj); - virObjectUnlock(secrets); + virObjectRWUnlock(secrets); return ret; } =20 =20 -struct virSecretCountData { - virConnectPtr conn; - virSecretObjListACLFilter filter; - int count; -}; - static int -virSecretObjListNumOfSecretsCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) +virSecretObjListGetHelper(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) { - struct virSecretCountData *data =3D opaque; - virSecretObjPtr obj =3D payload; - virSecretDefPtr def; - - virObjectLock(obj); - def =3D obj->def; - - if (data->filter && !data->filter(data->conn, def)) - goto cleanup; - - data->count++; - - cleanup: - virObjectUnlock(obj); - return 0; -} - - -struct virSecretListData { - virConnectPtr conn; - virSecretObjListACLFilter filter; - int nuuids; - char **uuids; - int maxuuids; - bool error; -}; - - -static int -virSecretObjListGetUUIDsCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - struct virSecretListData *data =3D opaque; virSecretObjPtr obj =3D payload; + virObjectLookupHashForEachDataPtr data =3D opaque; + virSecretObjListACLFilter filter =3D data->filter; + char **uuids =3D (char **)data->elems; virSecretDefPtr def; =20 if (data->error) return 0; =20 - if (data->maxuuids >=3D 0 && data->nuuids =3D=3D data->maxuuids) + if (data->maxElems >=3D 0 && data->nElems =3D=3D data->maxElems) return 0; =20 virObjectLock(obj); def =3D obj->def; =20 - if (data->filter && !data->filter(data->conn, def)) + if (filter && !filter(data->conn, def)) goto cleanup; =20 - if (data->uuids) { + if (uuids) { char *uuidstr; =20 if (VIR_ALLOC_N(uuidstr, VIR_UUID_STRING_BUFLEN) < 0) { @@ -479,7 +387,9 @@ virSecretObjListGetUUIDsCallback(void *payload, } =20 virUUIDFormat(def->uuid, uuidstr); - data->uuids[data->nuuids++] =3D uuidstr; + uuids[data->nElems++] =3D uuidstr; + } else { + data->nElems++; } =20 cleanup: @@ -493,14 +403,11 @@ virSecretObjListNumOfSecrets(virSecretObjListPtr secr= ets, virSecretObjListACLFilter filter, virConnectPtr conn) { - struct virSecretCountData data =3D { - .conn =3D conn, .filter =3D filter, .count =3D 0 }; - - virObjectLock(secrets); - virHashForEach(secrets->objs, virSecretObjListNumOfSecretsCallback, &d= ata); - virObjectUnlock(secrets); + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .nElems =3D = 0, + .elems =3D NULL, .maxElems =3D -2 }; =20 - return data.count; + return virObjectLookupHashForEach(secrets, virSecretObjListGetHelper, = &data); } =20 =20 @@ -532,22 +439,15 @@ virSecretObjMatchFlags(virSecretObjPtr obj, #undef MATCH =20 =20 -struct virSecretObjListData { - virConnectPtr conn; - virSecretPtr *secrets; - virSecretObjListACLFilter filter; - unsigned int flags; - int nsecrets; - bool error; -}; - static int virSecretObjListExportCallback(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque) { - struct virSecretObjListData *data =3D opaque; virSecretObjPtr obj =3D payload; + virObjectLookupHashForEachDataPtr data =3D opaque; + virSecretPtr *secrets =3D (virSecretPtr *)data->elems; + virSecretObjListACLFilter filter =3D data->filter; virSecretDefPtr def; virSecretPtr secret =3D NULL; =20 @@ -557,25 +457,24 @@ virSecretObjListExportCallback(void *payload, virObjectLock(obj); def =3D obj->def; =20 - if (data->filter && !data->filter(data->conn, def)) + if (filter && !filter(data->conn, def)) goto cleanup; =20 if (!virSecretObjMatchFlags(obj, data->flags)) goto cleanup; =20 - if (!data->secrets) { - data->nsecrets++; + if (!secrets) { + data->nElems++; goto cleanup; } =20 if (!(secret =3D virGetSecret(data->conn, def->uuid, - def->usage_type, - def->usage_id))) { + def->usage_type, def->usage_id))) { data->error =3D true; goto cleanup; } =20 - data->secrets[data->nsecrets++] =3D secret; + secrets[data->nElems++] =3D secret; =20 cleanup: virObjectUnlock(obj); @@ -590,35 +489,21 @@ virSecretObjListExport(virConnectPtr conn, virSecretObjListACLFilter filter, unsigned int flags) { - struct virSecretObjListData data =3D { - .conn =3D conn, .secrets =3D NULL, - .filter =3D filter, .flags =3D flags, - .nsecrets =3D 0, .error =3D false }; - - virObjectLock(secretobjs); - if (secrets && - VIR_ALLOC_N(data.secrets, virHashSize(secretobjs->objs) + 1) < 0) { - virObjectUnlock(secretobjs); - return -1; - } + int ret; =20 - virHashForEach(secretobjs->objs, virSecretObjListExportCallback, &data= ); - virObjectUnlock(secretobjs); + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .nElems =3D = 0, + .elems =3D NULL, .maxElems =3D 0, .flags =3D flags }; =20 - if (data.error) - goto error; - - if (data.secrets) { - /* trim the array to the final size */ - ignore_value(VIR_REALLOC_N(data.secrets, data.nsecrets + 1)); - *secrets =3D data.secrets; - } + if (secrets) + data.maxElems =3D -1; =20 - return data.nsecrets; + ret =3D virObjectLookupHashForEach(secretobjs, virSecretObjListExportC= allback, + &data); + if (secrets) + *secrets =3D (virSecretPtr *)data.elems; =20 - error: - virObjectListFree(data.secrets); - return -1; + return ret; } =20 =20 @@ -629,23 +514,11 @@ virSecretObjListGetUUIDs(virSecretObjListPtr secrets, virSecretObjListACLFilter filter, virConnectPtr conn) { - struct virSecretListData data =3D { - .conn =3D conn, .filter =3D filter, .uuids =3D uuids, .nuuids =3D = 0, - .maxuuids =3D maxuuids, .error =3D false }; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .nElems =3D = 0, + .elems =3D (void **)uuids, .maxElems =3D maxuuids }; =20 - virObjectLock(secrets); - virHashForEach(secrets->objs, virSecretObjListGetUUIDsCallback, &data); - virObjectUnlock(secrets); - - if (data.error) - goto error; - - return data.nuuids; - - error: - while (--data.nuuids) - VIR_FREE(data.uuids[data.nuuids]); - return -1; + return virObjectLookupHashForEach(secrets, virSecretObjListGetHelper, = &data); } =20 =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093097093550.1462540828495; Fri, 18 Aug 2017 14:51:37 -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 5EA165AFEB; Fri, 18 Aug 2017 21:51:35 +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 3AC221756B; Fri, 18 Aug 2017 21:51:35 +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 039753FAD1; Fri, 18 Aug 2017 21:51:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILomDO005046 for ; Fri, 18 Aug 2017 17:50:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 46F2A5D962; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 16CB65D961 for ; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5EA165AFEB 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:35 -0400 Message-Id: <20170818215041.8118-12-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 11/17] util: Introduce virObjectLookupHashClone 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.39]); Fri, 18 Aug 2017 21:51:35 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" A convenience API that will utilize the virHashForEach API for the LookupHash in order to create a clone/copy. Primary consumer is the interface driver which has a desire to save off a copy of its only hash table in order to possible restore it if something goes wrong during processing. The callback function's primary purpose is to copy anything within the local LookupKeys into the target. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virobject.c | 79 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 9 ++++++ 3 files changed, 89 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index df3f246..c7c9762 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2332,6 +2332,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLookupHashAdd; +virObjectLookupHashClone; virObjectLookupHashFind; virObjectLookupHashFindLocked; virObjectLookupHashForEach; diff --git a/src/util/virobject.c b/src/util/virobject.c index 17ea4e6..0a4195d 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -1167,3 +1167,82 @@ virObjectLookupHashSearch(void *anyobj, =20 return obj; } + + +struct cloneData { + virObjectLookupHashCloneCallback callback; + virObjectLookupHashPtr dst; + bool error; +}; + +/* + * Take the provided virHashForEach element and call the @cb function + * with the input @dst hash table and the source element from the + * @src hash table in order to perform the copy - tracking success/ + * failure using the error boolean. + * + * Once there's a failure, no future copy/clone will occur. + * + * The @cb function can expect the @src hash table object to be + * locked upon entry. + * + * Returns 0 to the virHashForEach on success, -1 on failure. + */ +static int +cloneCallback(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virObjectLookupKeysPtr obj =3D payload; + struct cloneData *data =3D opaque; + + if (data->error) + return 0; + + virObjectLock(obj); + + if (data->callback(data->dst, obj) < 0) + data->error =3D true; + + virObjectUnlock(obj); + + if (data->error) + return -1; + + return 0; +} + +/** + * virObjectLookupHashClone + * @srcAnyobj: source LookupHash object to clone from + * @dstAnyobj: destination LookupHash object to clone to + * @cb: callback function to handle the clone + * + * The clone function is designed to traverse each source hash element + * and call the driver specific @cb function with the element from the + * source hash table in order to clone into the destination hash table. + * + * Return 0 on success, -1 on failure + */ +int +virObjectLookupHashClone(void *srcAnyobj, + void *dstAnyobj, + virObjectLookupHashCloneCallback cb) +{ + virObjectLookupHashPtr srcHashObj =3D virObjectGetLookupHashObj(srcAny= obj); + virObjectLookupHashPtr dstHashObj =3D virObjectGetLookupHashObj(dstAny= obj); + struct cloneData data =3D { .callback =3D cb, .dst =3D dstHashObj, + .error =3D false }; + + if (!srcHashObj || !dstHashObj) + return -1; + + virObjectRWLockRead(srcHashObj); + virHashForEach(srcHashObj->objsKey1, cloneCallback, &data); + virObjectRWUnlock(srcHashObj); + + if (data.error) + return -1; + + return 0; +} diff --git a/src/util/virobject.h b/src/util/virobject.h index d4bc3c3..b9e6311 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -252,4 +252,13 @@ virObjectLookupHashSearch(void *anyobj, void *opaque) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =20 +typedef int (*virObjectLookupHashCloneCallback)(void *destHashTable, + void *sourceObject); +int +virObjectLookupHashClone(void *srcAnyobj, + void *dstAnyobj, + virObjectLookupHashCloneCallback cb) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); + + #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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093113807854.8830731557962; Fri, 18 Aug 2017 14:51:53 -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 F00A4C0587F5; Fri, 18 Aug 2017 21:51:51 +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 C779F619D9; Fri, 18 Aug 2017 21:51:51 +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 8FF9D18045C2; Fri, 18 Aug 2017 21:51:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILomKH005057 for ; Fri, 18 Aug 2017 17:50:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 963C75D962; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 669725D961 for ; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F00A4C0587F5 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:36 -0400 Message-Id: <20170818215041.8118-13-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 12/17] Revert "interface: Consume @def in virInterfaceObjNew" 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.32]); Fri, 18 Aug 2017 21:51:52 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This reverts commit 92840eb3a7e47cdf761e52afccc41d2a35327fbd. More recent reviews/changes don't have the vir*ObjNew APIs consuming the @def, so remove from Interface as well. Changes needed to also deal with conflicts from commit id '46f5eca4'. Signed-off-by: John Ferlan --- src/conf/virinterfaceobj.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index 106f232..e993b92 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -74,7 +74,7 @@ virInterfaceObjDispose(void *opaque) =20 =20 static virInterfaceObjPtr -virInterfaceObjNew(virInterfaceDefPtr def) +virInterfaceObjNew(void) { virInterfaceObjPtr obj; =20 @@ -85,7 +85,6 @@ virInterfaceObjNew(virInterfaceDefPtr def) return NULL; =20 virObjectLock(obj); - obj->def =3D def; =20 return obj; } @@ -261,15 +260,15 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr i= nterfaces, return obj; } =20 - if (!(obj =3D virInterfaceObjNew(def))) + if (!(obj =3D virInterfaceObjNew())) return NULL; =20 if (VIR_APPEND_ELEMENT_COPY(interfaces->objs, interfaces->count, obj) < 0) { - obj->def =3D NULL; virInterfaceObjEndAPI(&obj); return NULL; } + obj->def =3D def; return virObjectRef(obj); } =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 150309309963531.957280822255143; Fri, 18 Aug 2017 14:51:39 -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 E7FBBC058EA8; Fri, 18 Aug 2017 21:51:37 +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 9BA46707DE; Fri, 18 Aug 2017 21:51: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 5F688180610A; Fri, 18 Aug 2017 21:51:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILom23005068 for ; Fri, 18 Aug 2017 17:50:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id E7F485D962; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B5C325D961 for ; Fri, 18 Aug 2017 21:50:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E7FBBC058EA8 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:37 -0400 Message-Id: <20170818215041.8118-14-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 13/17] interface: Use virObjectLookup{Keys|Hash} 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.32]); Fri, 18 Aug 2017 21:51:38 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Use the virObjectLookupKeys in _virInterfaceObj and use the virObjectLookupHash in _virInterfaceObjList. Convert the code to use the LookupHash object and APIs rather than the local forward linked list processing. Usage of HashLookup{Find|Search} API's is via a callback mechanism and returns a locked/reffed object. The Clone API will make use of the ForEach functionality in copying whatever is in one HashLookup list into the destination. The only function requiring taking a lock is the AssignDef function since it needs to be synchronized in such a way to avoid multiple threads attempting to add the same named object at the same time. The NumOfInterfaces and GetNames can use the same callback function with the only difference being the filling in of the @names array from the passed @data structure if it exists. Signed-off-by: John Ferlan --- src/conf/virinterfaceobj.c | 300 ++++++++++++++++++++++++-----------------= ---- 1 file changed, 157 insertions(+), 143 deletions(-) diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index e993b92..f2475b8 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -33,15 +33,13 @@ VIR_LOG_INIT("conf.virinterfaceobj"); =20 struct _virInterfaceObj { - virObjectLockable parent; + virObjectLookupKeys parent; =20 - bool active; /* true if interface is active (up) */ virInterfaceDefPtr def; /* The interface definition */ }; =20 struct _virInterfaceObjList { - size_t count; - virInterfaceObjPtr *objs; + virObjectLookupHash parent; }; =20 /* virInterfaceObj manipulation */ @@ -52,7 +50,7 @@ static void virInterfaceObjDispose(void *obj); static int virInterfaceObjOnceInit(void) { - if (!(virInterfaceObjClass =3D virClassNew(virClassForObjectLockable(), + if (!(virInterfaceObjClass =3D virClassNew(virClassForObjectLookupKeys= (), "virInterfaceObj", sizeof(virInterfaceObj), virInterfaceObjDispose))) @@ -74,14 +72,14 @@ virInterfaceObjDispose(void *opaque) =20 =20 static virInterfaceObjPtr -virInterfaceObjNew(void) +virInterfaceObjNew(const char *name) { virInterfaceObjPtr obj; =20 if (virInterfaceObjInitialize() < 0) return NULL; =20 - if (!(obj =3D virObjectLockableNew(virInterfaceObjClass))) + if (!(obj =3D virObjectLookupKeysNew(virInterfaceObjClass, name, NULL)= )) return NULL; =20 virObjectLock(obj); @@ -112,7 +110,7 @@ virInterfaceObjGetDef(virInterfaceObjPtr obj) bool virInterfaceObjIsActive(virInterfaceObjPtr obj) { - return obj->active; + return virObjectLookupKeysIsActive(obj); } =20 =20 @@ -120,7 +118,7 @@ void virInterfaceObjSetActive(virInterfaceObjPtr obj, bool active) { - obj->active =3D active; + virObjectLookupKeysSetActive(obj, active); } =20 =20 @@ -128,11 +126,40 @@ virInterfaceObjSetActive(virInterfaceObjPtr obj, virInterfaceObjListPtr virInterfaceObjListNew(void) { - virInterfaceObjListPtr interfaces; + return virObjectLookupHashNew(virClassForObjectLookupHash(), 10, false= ); +} =20 - if (VIR_ALLOC(interfaces) < 0) - return NULL; - return interfaces; + +static int +virInterfaceObjListFindByMACStringCb(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virInterfaceObjPtr obj =3D payload; + virObjectLookupHashForEachDataPtr data =3D opaque; + char **const matches =3D (char **const)data->elems; + virInterfaceDefPtr def; + int ret =3D -1; + + if (data->error) + return 0; + + virObjectLock(obj); + def =3D obj->def; + if (STRCASEEQ(def->mac, data->matchStr)) { + if (data->nElems < data->maxElems) { + if (VIR_STRDUP(matches[data->nElems], def->name) < 0) { + data->error =3D true; + goto cleanup; + } + data->nElems++; + } + } + ret =3D 0; + + cleanup: + virObjectUnlock(obj); + return ret; } =20 =20 @@ -142,33 +169,22 @@ virInterfaceObjListFindByMACString(virInterfaceObjLis= tPtr interfaces, char **const matches, int maxmatches) { - size_t i; - int matchct =3D 0; - - for (i =3D 0; i < interfaces->count; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; - - virObjectLock(obj); - def =3D obj->def; - if (STRCASEEQ(def->mac, mac)) { - if (matchct < maxmatches) { - if (VIR_STRDUP(matches[matchct], def->name) < 0) { - virObjectUnlock(obj); - goto error; - } - matchct++; - } - } - virObjectUnlock(obj); - } - return matchct; + virObjectLookupHashForEachData data =3D { + .error =3D false, .matchStr =3D mac, .nElems =3D 0, + .elems =3D (void **)matches, .maxElems =3D maxmatches }; + + return virObjectLookupHashForEach(interfaces, + virInterfaceObjListFindByMACStringCb, + &data); +} =20 - error: - while (--matchct >=3D 0) - VIR_FREE(matches[matchct]); =20 - return -1; +static virInterfaceObjPtr +virInterfaceObjListFindByNameLocked(virInterfaceObjListPtr interfaces, + const char *name) +{ + virObjectLookupKeysPtr obj =3D virObjectLookupHashFindLocked(interface= s, name); + return (virInterfaceObjPtr)obj; } =20 =20 @@ -176,73 +192,73 @@ virInterfaceObjPtr virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces, const char *name) { - size_t i; - - for (i =3D 0; i < interfaces->count; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; - - virObjectLock(obj); - def =3D obj->def; - if (STREQ(def->name, name)) - return virObjectRef(obj); - virObjectUnlock(obj); - } - - return NULL; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFind(interfaces, nam= e); + return (virInterfaceObjPtr) obj; } =20 =20 void virInterfaceObjListFree(virInterfaceObjListPtr interfaces) { - size_t i; + virObjectUnref(interfaces); +} + + +struct interfaceCloneData { + const char *primaryKey; + virInterfaceObjListPtr dest; + bool error; +}; + +static int +virInterfaceObjListCloneCb(void *destHashTable, + void *sourceObject) +{ + virInterfaceObjListPtr dest =3D destHashTable; + virInterfaceObjPtr srcObj =3D sourceObject; + int ret =3D -1; + char *xml =3D NULL; + virInterfaceObjPtr obj; + virInterfaceDefPtr backup =3D NULL; + + if (!(xml =3D virInterfaceDefFormat(srcObj->def))) + goto cleanup; =20 - for (i =3D 0; i < interfaces->count; i++) - virObjectUnref(interfaces->objs[i]); - VIR_FREE(interfaces->objs); - VIR_FREE(interfaces); + if (!(backup =3D virInterfaceDefParseString(xml))) + goto cleanup; + + if (!(obj =3D virInterfaceObjListAssignDef(dest, backup))) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FREE(xml); + virInterfaceDefFree(backup); + + return ret; } =20 =20 virInterfaceObjListPtr virInterfaceObjListClone(virInterfaceObjListPtr interfaces) { - size_t i; - unsigned int cnt; - virInterfaceObjListPtr dest; + virInterfaceObjListPtr destInterfaces =3D NULL; =20 if (!interfaces) return NULL; =20 - if (!(dest =3D virInterfaceObjListNew())) + if (!(destInterfaces =3D virInterfaceObjListNew())) return NULL; =20 - cnt =3D interfaces->count; - for (i =3D 0; i < cnt; i++) { - virInterfaceObjPtr srcobj =3D interfaces->objs[i]; - virInterfaceDefPtr backup; - virInterfaceObjPtr obj; - char *xml =3D virInterfaceDefFormat(srcobj->def); + if (virObjectLookupHashClone(interfaces, destInterfaces, + virInterfaceObjListCloneCb) < 0) + goto cleanup; =20 - if (!xml) - goto error; - - if (!(backup =3D virInterfaceDefParseString(xml))) { - VIR_FREE(xml); - goto error; - } - - VIR_FREE(xml); - if (!(obj =3D virInterfaceObjListAssignDef(dest, backup))) - goto error; - virInterfaceObjEndAPI(&obj); - } + return destInterfaces; =20 - return dest; - - error: - virInterfaceObjListFree(dest); + cleanup: + virObjectUnref(destInterfaces); return NULL; } =20 @@ -252,24 +268,29 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr i= nterfaces, virInterfaceDefPtr def) { virInterfaceObjPtr obj; + virInterfaceObjPtr ret =3D NULL; + + virObjectRWLockWrite(interfaces); =20 - if ((obj =3D virInterfaceObjListFindByName(interfaces, def->name))) { + if ((obj =3D virInterfaceObjListFindByNameLocked(interfaces, def->name= ))) { virInterfaceDefFree(obj->def); obj->def =3D def; + } else { + if (!(obj =3D virInterfaceObjNew(def->name))) + goto cleanup; =20 - return obj; + if (virObjectLookupHashAdd(interfaces, (virObjectLookupKeysPtr)obj= ) < 0) + goto cleanup; + obj->def =3D def; } =20 - if (!(obj =3D virInterfaceObjNew())) - return NULL; + ret =3D obj; + obj =3D NULL; =20 - if (VIR_APPEND_ELEMENT_COPY(interfaces->objs, - interfaces->count, obj) < 0) { - virInterfaceObjEndAPI(&obj); - return NULL; - } - obj->def =3D def; - return virObjectRef(obj); + cleanup: + virInterfaceObjEndAPI(&obj); + virObjectRWUnlock(interfaces); + return ret; } =20 =20 @@ -277,20 +298,39 @@ void virInterfaceObjListRemove(virInterfaceObjListPtr interfaces, virInterfaceObjPtr obj) { - size_t i; + virObjectLookupHashRemove(interfaces, (virObjectLookupKeysPtr)obj); +} =20 - virObjectUnlock(obj); - for (i =3D 0; i < interfaces->count; i++) { - virObjectLock(interfaces->objs[i]); - if (interfaces->objs[i] =3D=3D obj) { - virObjectUnlock(interfaces->objs[i]); - virObjectUnref(interfaces->objs[i]); - - VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count); - break; + +static int +virInterfaceObjListGetHelper(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virInterfaceObjPtr obj =3D payload; + virObjectLookupHashForEachDataPtr data =3D opaque; + char **const names =3D (char **const)data->elems; + virInterfaceDefPtr def; + + if (data->error) + return 0; + + if (data->maxElems >=3D 0 && data->nElems =3D=3D data->maxElems) + return 0; + + virObjectLock(obj); + def =3D obj->def; + if (data->wantActive =3D=3D virInterfaceObjIsActive(obj)) { + if (names && VIR_STRDUP(names[data->nElems], def->name) < 0) { + data->error =3D true; + goto cleanup; } - virObjectUnlock(interfaces->objs[i]); - } + data->nElems++; + } + + cleanup: + virObjectUnlock(obj); + return 0; } =20 =20 @@ -298,18 +338,12 @@ int virInterfaceObjListNumOfInterfaces(virInterfaceObjListPtr interfaces, bool wantActive) { - size_t i; - int ninterfaces =3D 0; - - for (i =3D 0; (i < interfaces->count); i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virObjectLock(obj); - if (wantActive =3D=3D virInterfaceObjIsActive(obj)) - ninterfaces++; - virObjectUnlock(obj); - } + virObjectLookupHashForEachData data =3D { + .wantActive =3D wantActive, .error =3D false, .nElems =3D 0, + .elems =3D NULL, .maxElems =3D -2 }; =20 - return ninterfaces; + return virObjectLookupHashForEach(interfaces, virInterfaceObjListGetHe= lper, + &data); } =20 =20 @@ -319,30 +353,10 @@ virInterfaceObjListGetNames(virInterfaceObjListPtr in= terfaces, char **const names, int maxnames) { - int nnames =3D 0; - size_t i; - - for (i =3D 0; i < interfaces->count && nnames < maxnames; i++) { - virInterfaceObjPtr obj =3D interfaces->objs[i]; - virInterfaceDefPtr def; - - virObjectLock(obj); - def =3D obj->def; - if (wantActive =3D=3D virInterfaceObjIsActive(obj)) { - if (VIR_STRDUP(names[nnames], def->name) < 0) { - virObjectUnlock(obj); - goto failure; - } - nnames++; - } - virObjectUnlock(obj); - } - - return nnames; - - failure: - while (--nnames >=3D 0) - VIR_FREE(names[nnames]); + virObjectLookupHashForEachData data =3D { + .wantActive =3D wantActive, .error =3D false, .nElems =3D 0, + .elems =3D (void **)names, .maxElems =3D maxnames }; =20 - return -1; + return virObjectLookupHashForEach(interfaces, virInterfaceObjListGetHe= lper, + &data); } --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093116329393.5228759733658; Fri, 18 Aug 2017 14:51:56 -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 729CF86647; Fri, 18 Aug 2017 21:51:54 +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 4D1551897B; Fri, 18 Aug 2017 21:51:54 +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 17E1F3FAD3; Fri, 18 Aug 2017 21:51:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILonWN005078 for ; Fri, 18 Aug 2017 17:50:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 441C55D962; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 114FB5D961 for ; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 729CF86647 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:38 -0400 Message-Id: <20170818215041.8118-15-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 14/17] test: Clean up test driver Interface interactions 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]); Fri, 18 Aug 2017 21:51:55 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Now that we have self locking hash table for Interface object lookups, there's no need to take the test driver lock in order to "lock" between the various API's, so remove the Lock/Unlock logic accordingly. Add a virInterfaceObjListFree during testDriverFree for the backupIfaces "just in case". Also use the VIR_STEAL_PTR rather than inline the code. Finally alter a couple of "if ((obj =3D function()) =3D=3D NULL" to use the more standard "if (!(obj =3D function()))" syntax. Signed-off-by: John Ferlan --- src/test/test_driver.c | 55 ++++++++++++----------------------------------= ---- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index aa38f54..70977b8 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -155,6 +155,7 @@ testDriverFree(testDriverPtr driver) virNodeDeviceObjListFree(driver->devs); virObjectUnref(driver->networks); virInterfaceObjListFree(driver->ifaces); + virInterfaceObjListFree(driver->backupIfaces); virStoragePoolObjListFree(&driver->pools); virObjectUnref(driver->eventState); virMutexUnlock(&driver->lock); @@ -3689,11 +3690,7 @@ testInterfaceObjFindByName(testDriverPtr privconn, { virInterfaceObjPtr obj; =20 - testDriverLock(privconn); - obj =3D virInterfaceObjListFindByName(privconn->ifaces, name); - testDriverUnlock(privconn); - - if (!obj) + if (!(obj =3D virInterfaceObjListFindByName(privconn->ifaces, name))) virReportError(VIR_ERR_NO_INTERFACE, _("no interface with matching name '%s'"), name); @@ -3706,12 +3703,8 @@ static int testConnectNumOfInterfaces(virConnectPtr conn) { testDriverPtr privconn =3D conn->privateData; - int ninterfaces; =20 - testDriverLock(privconn); - ninterfaces =3D virInterfaceObjListNumOfInterfaces(privconn->ifaces, t= rue); - testDriverUnlock(privconn); - return ninterfaces; + return virInterfaceObjListNumOfInterfaces(privconn->ifaces, true); } =20 =20 @@ -3721,14 +3714,8 @@ testConnectListInterfaces(virConnectPtr conn, int maxnames) { testDriverPtr privconn =3D conn->privateData; - int nnames; =20 - testDriverLock(privconn); - nnames =3D virInterfaceObjListGetNames(privconn->ifaces, true, - names, maxnames); - testDriverUnlock(privconn); - - return nnames; + return virInterfaceObjListGetNames(privconn->ifaces, true, names, maxn= ames); } =20 =20 @@ -3736,12 +3723,8 @@ static int testConnectNumOfDefinedInterfaces(virConnectPtr conn) { testDriverPtr privconn =3D conn->privateData; - int ninterfaces; =20 - testDriverLock(privconn); - ninterfaces =3D virInterfaceObjListNumOfInterfaces(privconn->ifaces, f= alse); - testDriverUnlock(privconn); - return ninterfaces; + return virInterfaceObjListNumOfInterfaces(privconn->ifaces, false); } =20 =20 @@ -3751,14 +3734,8 @@ testConnectListDefinedInterfaces(virConnectPtr conn, int maxnames) { testDriverPtr privconn =3D conn->privateData; - int nnames; =20 - testDriverLock(privconn); - nnames =3D virInterfaceObjListGetNames(privconn->ifaces, false, - names, maxnames); - testDriverUnlock(privconn); - - return nnames; + return virInterfaceObjListGetNames(privconn->ifaces, false, names, max= names); } =20 =20 @@ -3791,12 +3768,8 @@ testInterfaceLookupByMACString(virConnectPtr conn, char *ifacenames[] =3D { NULL, NULL }; virInterfacePtr ret =3D NULL; =20 - testDriverLock(privconn); - ifacect =3D virInterfaceObjListFindByMACString(privconn->ifaces, mac, - ifacenames, 2); - testDriverUnlock(privconn); - - if (ifacect =3D=3D 0) { + if ((ifacect =3D virInterfaceObjListFindByMACString(privconn->ifaces, = mac, + ifacenames, 2)) =3D= =3D 0) { virReportError(VIR_ERR_NO_INTERFACE, _("no interface with matching mac '%s'"), mac); goto cleanup; @@ -3880,6 +3853,7 @@ testInterfaceChangeCommit(virConnectPtr conn, } =20 virInterfaceObjListFree(privconn->backupIfaces); + privconn->backupIfaces =3D NULL; privconn->transaction_running =3D false; =20 ret =3D 0; @@ -3910,8 +3884,7 @@ testInterfaceChangeRollback(virConnectPtr conn, } =20 virInterfaceObjListFree(privconn->ifaces); - privconn->ifaces =3D privconn->backupIfaces; - privconn->backupIfaces =3D NULL; + VIR_STEAL_PTR(privconn->ifaces, privconn->backupIfaces); =20 privconn->transaction_running =3D false; =20 @@ -3958,11 +3931,10 @@ testInterfaceDefineXML(virConnectPtr conn, =20 virCheckFlags(0, NULL); =20 - testDriverLock(privconn); - if ((def =3D virInterfaceDefParseString(xmlStr)) =3D=3D NULL) - goto cleanup; + if (!(def =3D virInterfaceDefParseString(xmlStr))) + return NULL; =20 - if ((obj =3D virInterfaceObjListAssignDef(privconn->ifaces, def)) =3D= =3D NULL) + if (!(obj =3D virInterfaceObjListAssignDef(privconn->ifaces, def))) goto cleanup; def =3D NULL; objdef =3D virInterfaceObjGetDef(obj); @@ -3972,7 +3944,6 @@ testInterfaceDefineXML(virConnectPtr conn, cleanup: virInterfaceDefFree(def); virInterfaceObjEndAPI(&obj); - testDriverUnlock(privconn); return ret; } =20 --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093150138330.8536325344818; Fri, 18 Aug 2017 14:52:30 -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 75B8F85540; Fri, 18 Aug 2017 21:52:28 +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 546947B123; Fri, 18 Aug 2017 21:52:28 +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 18784180610B; Fri, 18 Aug 2017 21:52:28 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILon1u005083 for ; Fri, 18 Aug 2017 17:50:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 924DD5D962; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 61E905D961 for ; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 75B8F85540 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:39 -0400 Message-Id: <20170818215041.8118-16-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 15/17] util: Introduce virObjectLookupHashPrune 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.28]); Fri, 18 Aug 2017 21:52:28 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" A convenience API that will utilize the virHashForEach API for the LookupHash in order to remove elements from the hash table(s) that match some requirement from the callback. NB: Once elements are removed from objsKey1 - if something goes wrong in objsKey2, the best that can be done is to issue a warning and hope for the best. Signed-off-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virobject.c | 36 ++++++++++++++++++++++++++++++++++++ src/util/virobject.h | 5 +++++ 3 files changed, 42 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c7c9762..47393c6 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2337,6 +2337,7 @@ virObjectLookupHashFind; virObjectLookupHashFindLocked; virObjectLookupHashForEach; virObjectLookupHashNew; +virObjectLookupHashPrune; virObjectLookupHashRemove; virObjectLookupHashSearch; virObjectLookupHashSearchLocked; diff --git a/src/util/virobject.c b/src/util/virobject.c index 0a4195d..47787f7 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -1246,3 +1246,39 @@ virObjectLookupHashClone(void *srcAnyobj, =20 return 0; } + + +/** + * virObjectLookupHashPrune + * @anyobj: LookupHash object + * @callback: callback function to handle the object specific checks + * @opaque: callback data + * + * Call the callback function from virHashRemoveSet in order to determine + * if the incoming opaque data should be removed from the hash table(s) + * for the LookupHash. If a second hash table exists and we fail to remove + * data from it + */ +void +virObjectLookupHashPrune(void *anyobj, + virHashSearcher callback, + void *opaque) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + ssize_t cntKey1; + ssize_t cntKey2; + + if (!hashObj) + return; + + virObjectRWLockWrite(hashObj); + cntKey1 =3D virHashRemoveSet(hashObj->objsKey1, callback, opaque); + if (cntKey1 > 0 && hashObj->objsKey2) { + cntKey2 =3D virHashRemoveSet(hashObj->objsKey2, callback, opaque); + + if (cntKey2 !=3D cntKey1) + VIR_ERROR(_("removed %zd elems from objsKey1 and %zd elems " + "from objsKey2"), cntKey1, cntKey2); + } + virObjectRWUnlock(hashObj); +} diff --git a/src/util/virobject.h b/src/util/virobject.h index b9e6311..54eff81 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -260,5 +260,10 @@ virObjectLookupHashClone(void *srcAnyobj, virObjectLookupHashCloneCallback cb) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); =20 +void +virObjectLookupHashPrune(void *anyobj, + virHashSearcher callback, + void *opaque) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); =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 Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093152903287.7486614270242; Fri, 18 Aug 2017 14:52:32 -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 1B8E07EA88; Fri, 18 Aug 2017 21:52:31 +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 EA99919EE7; Fri, 18 Aug 2017 21:52:30 +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 B6A553FAD4; Fri, 18 Aug 2017 21:52:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILoook005094 for ; Fri, 18 Aug 2017 17:50:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id F1E595D962; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1A5D5D961 for ; Fri, 18 Aug 2017 21:50:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1B8E07EA88 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:40 -0400 Message-Id: <20170818215041.8118-17-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 16/17] network: Fix virNetworkObjBridgeInUse return type 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.28]); Fri, 18 Aug 2017 21:52:31 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than an int, it returns a bool - so let's define it that way Signed-off-by: John Ferlan --- src/conf/virnetworkobj.c | 2 +- src/conf/virnetworkobj.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index 2bd69dc..20f846d 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -1172,7 +1172,7 @@ virNetworkObjBridgeInUseHelper(const void *payload, } =20 =20 -int +bool virNetworkObjBridgeInUse(virNetworkObjListPtr nets, const char *bridge, const char *skipname) diff --git a/src/conf/virnetworkobj.h b/src/conf/virnetworkobj.h index 463be26..627277b 100644 --- a/src/conf/virnetworkobj.h +++ b/src/conf/virnetworkobj.h @@ -174,7 +174,7 @@ virNetworkObjDeleteConfig(const char *configDir, const char *autostartDir, virNetworkObjPtr net); =20 -int +bool virNetworkObjBridgeInUse(virNetworkObjListPtr nets, const char *bridge, const char *skipname); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 10:16:21 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.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503093155203176.0420783800256; Fri, 18 Aug 2017 14:52:35 -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 7C14C81DF3; Fri, 18 Aug 2017 21:52:33 +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 5C3145D970; Fri, 18 Aug 2017 21:52:33 +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 2735D3FAD8; Fri, 18 Aug 2017 21:52:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ILoo37005104 for ; Fri, 18 Aug 2017 17:50:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5E53F5D962; Fri, 18 Aug 2017 21:50:50 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-120-47.rdu2.redhat.com [10.10.120.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C6FC5D961 for ; Fri, 18 Aug 2017 21:50:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7C14C81DF3 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=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 18 Aug 2017 17:50:41 -0400 Message-Id: <20170818215041.8118-18-jferlan@redhat.com> In-Reply-To: <20170818215041.8118-1-jferlan@redhat.com> References: <20170818215041.8118-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v4 17/17] network: Use virObjectLookup{Keys|Hash} 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.25]); Fri, 18 Aug 2017 21:52:33 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Use the virObjectLookupKeys in _virNetworkObj and use the virObjectLookupHash in _virNetworkObjList. Convert the code to use the LookupHash object and APIs rather than the local code and usage of virHash* calls. Since the _virNetworkObjList only contains the @parent object the virClassNew must be removed from OnceInit because instantiation would fail since the object size would be the same as the parent object size. Usage of HashLookup{Find|Search} API's returns a locked/reffed object so need to remove virObjectLock after FindBy*Locked calls. Use the def->name as a second key to the LookupHash to make for faster lookup's by name (instead of using Search on uuid key and matching the name. Signed-off-by: John Ferlan --- src/conf/virnetworkobj.c | 291 ++++++++++++----------------------------= ---- src/conf/virnetworkobj.h | 3 +- tests/networkxml2conftest.c | 4 +- 3 files changed, 86 insertions(+), 212 deletions(-) diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index 20f846d..d175191 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -40,11 +40,10 @@ VIR_LOG_INIT("conf.virnetworkobj"); #define INIT_CLASS_ID_BITMAP_SIZE (1<<4) =20 struct _virNetworkObj { - virObjectLockable parent; + virObjectLookupKeys parent; =20 pid_t dnsmasqPid; pid_t radvdPid; - bool active; bool autostart; bool persistent; =20 @@ -61,30 +60,21 @@ struct _virNetworkObj { }; =20 struct _virNetworkObjList { - virObjectLockable parent; - - virHashTablePtr objs; + virObjectLookupHash parent; }; =20 static virClassPtr virNetworkObjClass; -static virClassPtr virNetworkObjListClass; static void virNetworkObjDispose(void *obj); -static void virNetworkObjListDispose(void *obj); =20 static int virNetworkObjOnceInit(void) { - if (!(virNetworkObjClass =3D virClassNew(virClassForObjectLockable(), + if (!(virNetworkObjClass =3D virClassNew(virClassForObjectLookupKeys(), "virNetworkObj", sizeof(virNetworkObj), virNetworkObjDispose))) return -1; =20 - if (!(virNetworkObjListClass =3D virClassNew(virClassForObjectLockable= (), - "virNetworkObjList", - sizeof(virNetworkObjList), - virNetworkObjListDispose))) - return -1; return 0; } =20 @@ -92,14 +82,15 @@ virNetworkObjOnceInit(void) VIR_ONCE_GLOBAL_INIT(virNetworkObj) =20 virNetworkObjPtr -virNetworkObjNew(void) +virNetworkObjNew(const char *uuidstr, + const char *name) { virNetworkObjPtr obj; =20 if (virNetworkObjInitialize() < 0) return NULL; =20 - if (!(obj =3D virObjectLockableNew(virNetworkObjClass))) + if (!(obj =3D virObjectLookupKeysNew(virNetworkObjClass, uuidstr, name= ))) return NULL; =20 if (!(obj->classIdMap =3D virBitmapNew(INIT_CLASS_ID_BITMAP_SIZE))) @@ -158,7 +149,7 @@ virNetworkObjGetNewDef(virNetworkObjPtr obj) bool virNetworkObjIsActive(virNetworkObjPtr obj) { - return obj->active; + return virObjectLookupKeysIsActive(obj); } =20 =20 @@ -166,7 +157,7 @@ void virNetworkObjSetActive(virNetworkObjPtr obj, bool active) { - obj->active =3D active; + virObjectLookupKeysSetActive(obj, active); } =20 =20 @@ -332,36 +323,16 @@ virNetworkObjMacMgrDel(virNetworkObjPtr obj, virNetworkObjListPtr virNetworkObjListNew(void) { - virNetworkObjListPtr nets; - - if (virNetworkObjInitialize() < 0) - return NULL; - - if (!(nets =3D virObjectLockableNew(virNetworkObjListClass))) - return NULL; - - if (!(nets->objs =3D virHashCreate(50, virObjectFreeHashData))) { - virObjectUnref(nets); - return NULL; - } - - return nets; + return virObjectLookupHashNew(virClassForObjectLookupHash(), 50, true); } =20 =20 static virNetworkObjPtr virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets, - const unsigned char *uuid) + const char *uuidstr) { - virNetworkObjPtr obj =3D NULL; - char uuidstr[VIR_UUID_STRING_BUFLEN]; - - virUUIDFormat(uuid, uuidstr); - - obj =3D virHashLookup(nets->objs, uuidstr); - if (obj) - virObjectRef(obj); - return obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFindLocked(nets, uui= dstr); + return (virNetworkObjPtr)obj; } =20 =20 @@ -379,30 +350,11 @@ virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets, const unsigned char *uuid) { - virNetworkObjPtr obj; - - virObjectLock(nets); - obj =3D virNetworkObjFindByUUIDLocked(nets, uuid); - virObjectUnlock(nets); - if (obj) - virObjectLock(obj); - return obj; -} - - -static int -virNetworkObjSearchName(const void *payload, - const void *name ATTRIBUTE_UNUSED, - const void *data) -{ - virNetworkObjPtr obj =3D (virNetworkObjPtr) payload; - int want =3D 0; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + virUUIDFormat(uuid, uuidstr); =20 - virObjectLock(obj); - if (STREQ(obj->def->name, (const char *)data)) - want =3D 1; - virObjectUnlock(obj); - return want; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFind(nets, uuidstr); + return (virNetworkObjPtr)obj; } =20 =20 @@ -410,12 +362,8 @@ static virNetworkObjPtr virNetworkObjFindByNameLocked(virNetworkObjListPtr nets, const char *name) { - virNetworkObjPtr obj =3D NULL; - - obj =3D virHashSearch(nets->objs, virNetworkObjSearchName, name, NULL); - if (obj) - virObjectRef(obj); - return obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFindLocked(nets, nam= e); + return (virNetworkObjPtr)obj; } =20 =20 @@ -433,14 +381,8 @@ virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets, const char *name) { - virNetworkObjPtr obj; - - virObjectLock(nets); - obj =3D virNetworkObjFindByNameLocked(nets, name); - virObjectUnlock(nets); - if (obj) - virObjectLock(obj); - return obj; + virObjectLookupKeysPtr obj =3D virObjectLookupHashFind(nets, name); + return (virNetworkObjPtr)obj; } =20 =20 @@ -470,15 +412,6 @@ virNetworkObjDispose(void *opaque) } =20 =20 -static void -virNetworkObjListDispose(void *opaque) -{ - virNetworkObjListPtr nets =3D opaque; - - virHashFree(nets->objs); -} - - /* * virNetworkObjUpdateAssignDef: * @network: the network object to update @@ -560,12 +493,12 @@ virNetworkObjAssignDefLocked(virNetworkObjListPtr net= s, virNetworkObjPtr ret =3D NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; =20 + virUUIDFormat(def->uuid, uuidstr); /* See if a network with matching UUID already exists */ - if ((obj =3D virNetworkObjFindByUUIDLocked(nets, def->uuid))) { + if ((obj =3D virNetworkObjFindByUUIDLocked(nets, uuidstr))) { virObjectLock(obj); /* UUID matches, but if names don't match, refuse it */ if (STRNEQ(obj->def->name, def->name)) { - virUUIDFormat(obj->def->uuid, uuidstr); virReportError(VIR_ERR_OPERATION_FAILED, _("network '%s' is already defined with uuid %s= "), obj->def->name, uuidstr); @@ -588,20 +521,17 @@ virNetworkObjAssignDefLocked(virNetworkObjListPtr net= s, /* UUID does not match, but if a name matches, refuse it */ if ((obj =3D virNetworkObjFindByNameLocked(nets, def->name))) { virObjectLock(obj); - virUUIDFormat(obj->def->uuid, uuidstr); virReportError(VIR_ERR_OPERATION_FAILED, _("network '%s' already exists with uuid %s"), def->name, uuidstr); goto cleanup; } =20 - if (!(obj =3D virNetworkObjNew())) - goto cleanup; + if (!(obj =3D virNetworkObjNew(uuidstr, def->name))) + goto cleanup; =20 - virUUIDFormat(def->uuid, uuidstr); - if (virHashAddEntry(nets->objs, uuidstr, obj) < 0) + if (virObjectLookupHashAdd(nets, (virObjectLookupKeysPtr)obj) < 0) goto cleanup; - virObjectRef(obj); =20 obj->def =3D def; obj->persistent =3D !(flags & VIR_NETWORK_OBJ_LIST_ADD_LIVE); @@ -638,9 +568,9 @@ virNetworkObjAssignDef(virNetworkObjListPtr nets, { virNetworkObjPtr obj; =20 - virObjectLock(nets); + virObjectRWLockWrite(nets); obj =3D virNetworkObjAssignDefLocked(nets, def, flags); - virObjectUnlock(nets); + virObjectRWUnlock(nets); return obj; } =20 @@ -784,16 +714,7 @@ void virNetworkObjRemoveInactive(virNetworkObjListPtr nets, virNetworkObjPtr obj) { - char uuidstr[VIR_UUID_STRING_BUFLEN]; - - virUUIDFormat(obj->def->uuid, uuidstr); - virObjectRef(obj); - virObjectUnlock(obj); - virObjectLock(nets); - virObjectLock(obj); - virHashRemoveEntry(nets->objs, uuidstr); - virObjectUnlock(nets); - virObjectUnref(obj); + virObjectLookupHashRemove(nets, (virObjectLookupKeysPtr)obj); } =20 =20 @@ -968,7 +889,8 @@ virNetworkLoadState(virNetworkObjListPtr nets, obj->floor_sum =3D floor_sum_val; =20 obj->taint =3D taint; - obj->active =3D true; /* network with a state file is by definition ac= tive */ + /* network with a state file is by definition active */ + virNetworkObjSetActive(obj, true); =20 cleanup: VIR_FREE(configFile); @@ -1177,14 +1099,15 @@ virNetworkObjBridgeInUse(virNetworkObjListPtr nets, const char *bridge, const char *skipname) { + bool ret; virNetworkObjPtr obj; struct virNetworkObjBridgeInUseHelperData data =3D {bridge, skipname}; =20 - virObjectLock(nets); - obj =3D virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &dat= a, NULL); - virObjectUnlock(nets); + obj =3D (virNetworkObjPtr)virObjectLookupHashSearch(nets, virNetworkOb= jBridgeInUseHelper, &data); =20 - return obj !=3D NULL; + ret =3D obj !=3D NULL; + virNetworkObjEndAPI(&obj); + return ret; } =20 =20 @@ -1309,21 +1232,14 @@ virNetworkMatch(virNetworkObjPtr obj, #undef MATCH =20 =20 -struct virNetworkObjListData { - virConnectPtr conn; - virNetworkPtr *nets; - virNetworkObjListFilter filter; - unsigned int flags; - int nnets; - bool error; -}; - static int -virNetworkObjListPopulate(void *payload, +virNetworkObjListExportCb(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque) { - struct virNetworkObjListData *data =3D opaque; + virObjectLookupHashForEachDataPtr data =3D opaque; + virNetworkPtr *nets =3D (virNetworkPtr *)data->elems; + virNetworkObjListFilter filter =3D data->filter; virNetworkObjPtr obj =3D payload; virNetworkPtr net =3D NULL; =20 @@ -1332,15 +1248,14 @@ virNetworkObjListPopulate(void *payload, =20 virObjectLock(obj); =20 - if (data->filter && - !data->filter(data->conn, obj->def)) + if (filter && !filter(data->conn, obj->def)) goto cleanup; =20 if (!virNetworkMatch(obj, data->flags)) goto cleanup; =20 - if (!data->nets) { - data->nnets++; + if (!nets) { + data->nElems++; goto cleanup; } =20 @@ -1349,7 +1264,7 @@ virNetworkObjListPopulate(void *payload, goto cleanup; } =20 - data->nets[data->nnets++] =3D net; + nets[data->nElems++] =3D net; =20 cleanup: virObjectUnlock(obj); @@ -1364,34 +1279,19 @@ virNetworkObjListExport(virConnectPtr conn, virNetworkObjListFilter filter, unsigned int flags) { - int ret =3D -1; - struct virNetworkObjListData data =3D { - .conn =3D conn, .nets =3D NULL, .filter =3D filter, .flags =3D fla= gs, - .nnets =3D 0, .error =3D false }; - - virObjectLock(netobjs); - if (nets && VIR_ALLOC_N(data.nets, virHashSize(netobjs->objs) + 1) < 0) - goto cleanup; - - virHashForEach(netobjs->objs, virNetworkObjListPopulate, &data); + int ret; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .error =3D false, .nElems =3D = 0, + .elems =3D NULL, .maxElems =3D 0, .flags =3D flags }; =20 - if (data.error) - goto cleanup; + if (nets) + data.maxElems =3D -1; =20 - if (data.nets) { - /* trim the array to the final size */ - ignore_value(VIR_REALLOC_N(data.nets, data.nnets + 1)); - *nets =3D data.nets; - data.nets =3D NULL; - } + ret =3D virObjectLookupHashForEach(netobjs, virNetworkObjListExportCb,= &data); =20 - ret =3D data.nnets; - cleanup: - virObjectUnlock(netobjs); - while (data.nets && data.nnets) - virObjectUnref(data.nets[--data.nnets]); + if (nets) + *nets =3D (virNetworkPtr *)data.elems; =20 - VIR_FREE(data.nets); return ret; } =20 @@ -1407,10 +1307,11 @@ virNetworkObjListForEachHelper(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque) { - struct virNetworkObjListForEachHelperData *data =3D opaque; + virObjectLookupHashForEachDataPtr data =3D opaque; + struct virNetworkObjListForEachHelperData *helperData =3D data->opaque; =20 - if (data->callback(payload, data->opaque) < 0) - data->ret =3D -1; + if (helperData->callback(payload, helperData->opaque) < 0) + helperData->ret =3D -1; return 0; } =20 @@ -1433,54 +1334,45 @@ virNetworkObjListForEach(virNetworkObjListPtr nets, virNetworkObjListIterator callback, void *opaque) { - struct virNetworkObjListForEachHelperData data =3D { + struct virNetworkObjListForEachHelperData helperData =3D { .callback =3D callback, .opaque =3D opaque, .ret =3D 0}; - virObjectLock(nets); - virHashForEach(nets->objs, virNetworkObjListForEachHelper, &data); - virObjectUnlock(nets); - return data.ret; -} + virObjectLookupHashForEachData data =3D { + .opaque =3D &helperData, .error =3D false, .maxElems =3D 0 }; =20 + return virObjectLookupHashForEach(nets, virNetworkObjListForEachHelper, + &data); +} =20 -struct virNetworkObjListGetHelperData { - virConnectPtr conn; - virNetworkObjListFilter filter; - char **names; - int nnames; - int maxnames; - bool active; - bool error; -}; =20 static int virNetworkObjListGetHelper(void *payload, const void *name ATTRIBUTE_UNUSED, void *opaque) { - struct virNetworkObjListGetHelperData *data =3D opaque; virNetworkObjPtr obj =3D payload; + virObjectLookupHashForEachDataPtr data =3D opaque; + virNetworkObjListFilter filter =3D data->filter; + char **names =3D (char **)data->elems; =20 if (data->error) return 0; =20 - if (data->maxnames >=3D 0 && - data->nnames =3D=3D data->maxnames) + if (data->maxElems >=3D 0 && + data->nElems =3D=3D data->maxElems) return 0; =20 virObjectLock(obj); =20 - if (data->filter && - !data->filter(data->conn, obj->def)) + if (filter && !filter(data->conn, obj->def)) goto cleanup; =20 - if ((data->active && virNetworkObjIsActive(obj)) || - (!data->active && !virNetworkObjIsActive(obj))) { - if (data->names && - VIR_STRDUP(data->names[data->nnames], obj->def->name) < 0) { + if ((data->wantActive && virNetworkObjIsActive(obj)) || + (!data->wantActive && !virNetworkObjIsActive(obj))) { + if (names && VIR_STRDUP(names[data->nElems], obj->def->name) < 0) { data->error =3D true; goto cleanup; } - data->nnames++; + data->nElems++; } =20 cleanup: @@ -1497,26 +1389,11 @@ virNetworkObjListGetNames(virNetworkObjListPtr nets, virNetworkObjListFilter filter, virConnectPtr conn) { - int ret =3D -1; - - struct virNetworkObjListGetHelperData data =3D { - .conn =3D conn, .filter =3D filter, .names =3D names, .nnames =3D = 0, - .maxnames =3D maxnames, .active =3D active, .error =3D false}; - - virObjectLock(nets); - virHashForEach(nets->objs, virNetworkObjListGetHelper, &data); - virObjectUnlock(nets); - - if (data.error) - goto cleanup; + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .wantActive =3D active, .error= =3D false, + .nElems =3D 0, .elems =3D (void **)names, .maxElems =3D maxnames }; =20 - ret =3D data.nnames; - cleanup: - if (ret < 0) { - while (data.nnames) - VIR_FREE(data.names[--data.nnames]); - } - return ret; + return virObjectLookupHashForEach(nets, virNetworkObjListGetHelper, &d= ata); } =20 =20 @@ -1526,15 +1403,11 @@ virNetworkObjListNumOfNetworks(virNetworkObjListPtr= nets, virNetworkObjListFilter filter, virConnectPtr conn) { - struct virNetworkObjListGetHelperData data =3D { - .conn =3D conn, .filter =3D filter, .names =3D NULL, .nnames =3D 0, - .maxnames =3D -1, .active =3D active, .error =3D false}; - - virObjectLock(nets); - virHashForEach(nets->objs, virNetworkObjListGetHelper, &data); - virObjectUnlock(nets); + virObjectLookupHashForEachData data =3D { + .conn =3D conn, .filter =3D filter, .wantActive =3D active, .error= =3D false, + .nElems =3D 0, .elems =3D NULL, .maxElems =3D -2 }; =20 - return data.nnames; + return virObjectLookupHashForEach(nets, virNetworkObjListGetHelper, &d= ata); } =20 =20 @@ -1572,7 +1445,5 @@ virNetworkObjListPrune(virNetworkObjListPtr nets, { struct virNetworkObjListPruneHelperData data =3D {flags}; =20 - virObjectLock(nets); - virHashRemoveSet(nets->objs, virNetworkObjListPruneHelper, &data); - virObjectUnlock(nets); + virObjectLookupHashPrune(nets, virNetworkObjListPruneHelper, &data); } diff --git a/src/conf/virnetworkobj.h b/src/conf/virnetworkobj.h index 627277b..050a184 100644 --- a/src/conf/virnetworkobj.h +++ b/src/conf/virnetworkobj.h @@ -28,7 +28,8 @@ typedef struct _virNetworkObj virNetworkObj; typedef virNetworkObj *virNetworkObjPtr; =20 virNetworkObjPtr -virNetworkObjNew(void); +virNetworkObjNew(const char *uuidstr, + const char *name); =20 virNetworkDefPtr virNetworkObjGetDef(virNetworkObjPtr obj); diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 4251a22..1a27aab 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -28,11 +28,13 @@ testCompareXMLToConfFiles(const char *inxml, const char= *outconf, dnsmasqCapsPtr virCommandPtr cmd =3D NULL; char *pidfile =3D NULL; dnsmasqContext *dctx =3D NULL; + char uuidstr[VIR_UUID_STRING_BUFLEN]; =20 if (!(def =3D virNetworkDefParseFile(inxml))) goto fail; =20 - if (!(obj =3D virNetworkObjNew())) + virUUIDFormat(def->uuid, uuidstr); + if (!(obj =3D virNetworkObjNew(uuidstr, def->name))) goto fail; =20 virNetworkObjSetDef(obj, def); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list