From nobody Thu May 15 23:32:33 2025 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