From nobody Thu May 15 19:17: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 150352337391658.79069159763378; Wed, 23 Aug 2017 14:22:53 -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 E1AF24A6F6; Wed, 23 Aug 2017 21:22:51 +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 C359667C9C; Wed, 23 Aug 2017 21:22: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 8D39B3FA58; Wed, 23 Aug 2017 21:22:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7NLMlDP023968 for ; Wed, 23 Aug 2017 17:22:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 60C5480D99; Wed, 23 Aug 2017 21:22:47 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-123-165.rdu2.redhat.com [10.10.123.165]) by smtp.corp.redhat.com (Postfix) with ESMTP id F316466D36 for ; Wed, 23 Aug 2017 21:22:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E1AF24A6F6 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: Wed, 23 Aug 2017 17:21:59 -0400 Message-Id: <20170823212211.4693-4-jferlan@redhat.com> In-Reply-To: <20170823212211.4693-1-jferlan@redhat.com> References: <20170823212211.4693-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v5 03/15] 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 23 Aug 2017 21:22:52 +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 an object (virObjectLockable) 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 | 126 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virobject.h | 14 ++++++ 3 files changed, 142 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 33dbd92..e98646c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2330,7 +2330,9 @@ virObjectListFree; virObjectListFreeCount; virObjectLock; virObjectLockableNew; +virObjectLookupHashAdd; virObjectLookupHashNew; +virObjectLookupHashRemove; virObjectNew; virObjectRef; virObjectRWLockableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index edd8097..76bf1bf 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -731,3 +731,129 @@ virObjectListFreeCount(void *list, =20 VIR_FREE(list); } + + +static virObjectLookupHashPtr +virObjectGetLookupHashObj(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectLookupHashClass)) + return anyobj; + + VIR_OBJECT_USAGE_PRINT_ERROR(anyobj, virObjectLookupHashClass); + + return NULL; +} + + +static bool +virObjectLookupHashValidAddRemoveArgs(virObjectLookupHashPtr hashObj, + virObjectLockablePtr obj, + const char *uuidstr, + const char *name) +{ + if (!hashObj || !obj) + return false; + + if (uuidstr && !hashObj->objsUUID) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("no objsUUID for hashObj=3D%p, but uuidstr=3D%s p= rovided"), + hashObj, uuidstr); + return false; + } + + if (name && !hashObj->objsName) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("no objsName for hashObj=3D%p, but name=3D%s prov= ided"), + hashObj, name); + return false; + } + + return true; + +} + + +/** + * virObjectLookupHashAdd: + * @anyobj: LookupHash object + * @addObj: The (virObjectLockable) object to add to the hash table(s) + * @uuidstr: uuid formatted into a char string to add to UUID table + * @name: name to add to Name table + * + * Insert @obj into the hash tables found in @anyobj. Assumes that the + * caller has determined that @uuidstr and @name do not already exist + * in their respective hash table to be inserted. + * + * Returns 0 on success, -1 on failure. + */ +int +virObjectLookupHashAdd(void *anyobj, + void *addObj, + const char *uuidstr, + const char *name) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + virObjectLockablePtr obj =3D virObjectGetLockableObj(addObj); + + if (!virObjectLookupHashValidAddRemoveArgs(hashObj, obj, uuidstr, name= )) + return -1; + + if (hashObj->objsUUID) { + if (virHashAddEntry(hashObj->objsUUID, uuidstr, obj) < 0) + return -1; + virObjectRef(obj); + } + + if (hashObj->objsName) { + if (virHashAddEntry(hashObj->objsName, name, obj) < 0) { + if (hashObj->objsUUID) + virHashRemoveEntry(hashObj->objsUUID, uuidstr); + return -1; + } + virObjectRef(obj); + } + + return 0; +} + + +/** + * virObjectLookupHashRemove: + * @anyobj: LookupHash object + * @delObj: The (virObjectLockable) object to remove from the hash table(s) + * @uuidstr: uuid formatted into a char string to add to UUID table + * @name: name to add to Name table + * + * 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. + * + * NB: Caller must first check if @obj is NULL before calling. + * + * Even though this is a void, report the error for a bad @anyobj. + */ +void +virObjectLookupHashRemove(void *anyobj, + void *delObj, + const char *uuidstr, + const char *name) +{ + virObjectLookupHashPtr hashObj =3D virObjectGetLookupHashObj(anyobj); + virObjectLockablePtr obj =3D virObjectGetLockableObj(delObj); + + if (!virObjectLookupHashValidAddRemoveArgs(hashObj, obj, uuidstr, name= )) + return; + + virObjectRef(obj); + virObjectUnlock(obj); + virObjectRWLockWrite(hashObj); + virObjectLock(obj); + if (uuidstr) + virHashRemoveEntry(hashObj->objsUUID, uuidstr); + if (name) + virHashRemoveEntry(hashObj->objsName, name); + virObjectUnlock(obj); + virObjectUnref(obj); + virObjectRWUnlock(hashObj); +} diff --git a/src/util/virobject.h b/src/util/virobject.h index 75efa90..d149f30 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -181,4 +181,18 @@ void virObjectListFreeCount(void *list, size_t count); =20 +int +virObjectLookupHashAdd(void *anyobj, + void *addObj, + const char *uuidstr, + const char *name) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + +void +virObjectLookupHashRemove(void *anyobj, + void *delObj, + const char *uuidstr, + const char *name) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + #endif /* __VIR_OBJECT_H */ --=20 2.9.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list