From nobody Mon Feb 9 03:46:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1604509625; cv=none; d=zohomail.com; s=zohoarc; b=cPvV0RtHu2slj2MsY8HnOKVnXZqwdzlRpkY5FMv8HIkE0TAyCzZEtCwljTVdmgdsRw9ybERGntzCjFjN5pTKqkgyF7FznTiDXfzXVMwD1q5Uv0LU68jAH3gsZY8qCsDhKY5ZTDz/NmtQXiAoUxeW6yCiN6fA14Oyk0qWrSyrfsU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604509625; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bgCJut4KlWzbe+AWmhKMvRF5f37kWg+mnZlfflTfCwg=; b=KW759S7SFJhmJJrBdImdgUlM9XqmzxJ9pRQfW0H/AE10nMf3MBM6xyDZHL/1NVJ5Z7faZYU3EQT6Wchspxj0Sj9EnvEZUWi3WwkkWmhUa/t8KXuSv3AtXzlk/rIwuoZNdPYX8p5KIrV2qGkdqFk20k+7k1SpsGFoPhdOOnNmH4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 160450962544246.22461362678928; Wed, 4 Nov 2020 09:07:05 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-271-GWEiQ9r3OWaJXY1O3rxXDw-1; Wed, 04 Nov 2020 12:06:15 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4669E1087D84; Wed, 4 Nov 2020 17:06:08 +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 1F8B35C3E1; Wed, 4 Nov 2020 17:06:08 +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 DF886181A872; Wed, 4 Nov 2020 17:06:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0A4H61Q9002182 for ; Wed, 4 Nov 2020 12:06:01 -0500 Received: by smtp.corp.redhat.com (Postfix) id F26C010013C1; Wed, 4 Nov 2020 17:06:00 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 575F11007518 for ; Wed, 4 Nov 2020 17:06:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1604509624; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=bgCJut4KlWzbe+AWmhKMvRF5f37kWg+mnZlfflTfCwg=; b=RbF9NoA0v8xFx6eurpW8mvhTtRboxql2+rRW6atrorJzZVgwe/e6+pOBGLB8PcKxGgE2Eh FRUJejHb2fJnoYJp62Weyss8d5mfG0jCqdS9lshfU1YCvxV3pI9pvyU3BwInW/+dYqwvGn KN2Huj/+N4/vrAyE008tw4uqbbBrJxk= X-MC-Unique: GWEiQ9r3OWaJXY1O3rxXDw-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH v2 06/12] util: hash: Add delete-safe hash iterator Date: Wed, 4 Nov 2020 18:05:42 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 'virHashForEach' historically allowed deletion of the current element as 'virHashRemoveSet' didn't exits. To prevent us from having to deepy analyse all iterators add virHashForEachSafe which first gets a list of elements and iterates them outside of the hash table. This will allow replace the internals of the hash table with other implementation which don't allow such operation. Signed-off-by: Peter Krempa Reviewed-by: Daniel Henrique Barboza --- src/libvirt_private.syms | 1 + src/util/virhash.c | 26 +++++++++++++++++++++++--- src/util/virhash.h | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1793b81ad9..f7b0d11ca2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2203,6 +2203,7 @@ virHashAtomicSteal; virHashAtomicUpdate; virHashEqual; virHashForEach; +virHashForEachSafe; virHashForEachSorted; virHashFree; virHashGetItems; diff --git a/src/util/virhash.c b/src/util/virhash.c index f205291de9..e54052985f 100644 --- a/src/util/virhash.c +++ b/src/util/virhash.c @@ -481,7 +481,7 @@ virHashRemoveEntry(virHashTablePtr table, const char *n= ame) /** - * virHashForEach, virHashForEachSorted + * virHashForEach, virHashForEachSorted, virHashForEachSafe * @table: the hash table to process * @iter: callback to process each element * @opaque: opaque data to pass to the iterator @@ -490,14 +490,14 @@ virHashRemoveEntry(virHashTablePtr table, const char = *name) * * The elements are iterated in arbitrary order. * - * virHashForEach allows the callback to remove the current + * virHashForEach, virHashForEachSafe allow the callback to remove the cur= rent * element using virHashRemoveEntry but calling other virHash* functions is * prohibited. Note that removing the entry invalidates @key and @payload = in * the callback. * * virHashForEachSorted iterates the elements in order by sorted key. * - * virHashForEachSorted is more computationally + * virHashForEachSorted and virHashForEachSafe are more computationally * expensive than virHashForEach. * * If @iter fails and returns a negative value, the evaluation is stopped = and -1 @@ -531,6 +531,26 @@ virHashForEach(virHashTablePtr table, virHashIterator = iter, void *opaque) } +int +virHashForEachSafe(virHashTablePtr table, + virHashIterator iter, + void *opaque) +{ + g_autofree virHashKeyValuePairPtr items =3D virHashGetItems(table, NUL= L, false); + size_t i; + + if (!items) + return -1; + + for (i =3D 0; items[i].key; i++) { + if (iter((void *)items[i].value, items[i].key, opaque) < 0) + return -1; + } + + return 0; +} + + int virHashForEachSorted(virHashTablePtr table, virHashIterator iter, diff --git a/src/util/virhash.h b/src/util/virhash.h index 1a59e9799d..b00ab0447e 100644 --- a/src/util/virhash.h +++ b/src/util/virhash.h @@ -136,6 +136,7 @@ bool virHashEqual(const virHashTable *table1, * Iterators */ int virHashForEach(virHashTablePtr table, virHashIterator iter, void *opaq= ue); +int virHashForEachSafe(virHashTablePtr table, virHashIterator iter, void *= opaque); int virHashForEachSorted(virHashTablePtr table, virHashIterator iter, void= *opaque); ssize_t virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, cons= t void *opaque); void *virHashSearch(const virHashTable *table, virHashSearcher iter, --=20 2.26.2