From nobody Wed Nov 5 13:14:12 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534526615803777.0612500218546; Fri, 17 Aug 2018 10:23:35 -0700 (PDT) Received: from localhost ([::1]:35581 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiTB-0000ng-Mh for importer@patchew.org; Fri, 17 Aug 2018 13:23:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34546) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqiPd-0004vc-5k for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:19:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqiPc-0005Hg-DO for qemu-devel@nongnu.org; Fri, 17 Aug 2018 13:19:49 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47378 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqiPa-0005GS-Fd; Fri, 17 Aug 2018 13:19:46 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2BE694022414; Fri, 17 Aug 2018 17:19:46 +0000 (UTC) Received: from localhost (ovpn-112-51.ams2.redhat.com [10.36.112.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB1AB100F37D; Fri, 17 Aug 2018 17:19:43 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Fri, 17 Aug 2018 19:19:31 +0200 Message-Id: <20180817171932.31976-2-marcandre.lureau@redhat.com> In-Reply-To: <20180817171932.31976-1-marcandre.lureau@redhat.com> References: <20180817171932.31976-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:19:46 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 17 Aug 2018 17:19:46 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'marcandre.lureau@redhat.com' RCPT:'' Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 1/2] qdict: add qdict_steal() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "open list:Block layer core" , armbru@redhat.com, Max Reitz , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Dr. David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a new function qdict_steal(), that deletes a key from a dict, and returns the associated value, if any. Simplify related code. Signed-off-by: Marc-Andr=C3=A9 Lureau --- include/qapi/qmp/qdict.h | 1 + monitor.c | 3 +-- qobject/block-qdict.c | 7 ++----- qobject/qdict.c | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 7f3ec10a10..84644bfba6 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -37,6 +37,7 @@ QObject *qdict_entry_value(const QDictEntry *entry); size_t qdict_size(const QDict *qdict); void qdict_put_obj(QDict *qdict, const char *key, QObject *value); void qdict_del(QDict *qdict, const char *key); +QObject *qdict_steal(QDict *qdict, const char *key); int qdict_haskey(const QDict *qdict, const char *key); QObject *qdict_get(const QDict *qdict, const char *key); bool qdict_is_equal(const QObject *x, const QObject *y); diff --git a/monitor.c b/monitor.c index a1999e396c..eab2dc7b7b 100644 --- a/monitor.c +++ b/monitor.c @@ -4262,8 +4262,7 @@ static void handle_qmp_command(JSONMessageParser *par= ser, GQueue *tokens) =20 qdict =3D qobject_to(QDict, req); if (qdict) { - id =3D qobject_ref(qdict_get(qdict, "id")); - qdict_del(qdict, "id"); + id =3D qdict_steal(qdict, "id"); } /* else will fail qmp_dispatch() */ =20 if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c index 42054cc274..a07664ee6a 100644 --- a/qobject/block-qdict.c +++ b/qobject/block-qdict.c @@ -692,8 +692,6 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite) */ bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **= errp) { - QObject *qobj; - while (renames->from) { if (qdict_haskey(qdict, renames->from)) { if (qdict_haskey(qdict, renames->to)) { @@ -702,9 +700,8 @@ bool qdict_rename_keys(QDict *qdict, const QDictRenames= *renames, Error **errp) return false; } =20 - qobj =3D qdict_get(qdict, renames->from); - qdict_put_obj(qdict, renames->to, qobject_ref(qobj)); - qdict_del(qdict, renames->from); + qdict_put_obj(qdict, renames->to, + qdict_steal(qdict, renames->from)); } =20 renames++; diff --git a/qobject/qdict.c b/qobject/qdict.c index 3d8c2f7bbc..322f23f094 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -406,6 +406,28 @@ void qdict_del(QDict *qdict, const char *key) } } =20 +/** + * qdict_steal(): Steal a 'key:value' pair from the dictionary + * + * Return the associated entry value and remove it, or NULL. + */ +QObject *qdict_steal(QDict *qdict, const char *key) +{ + QDictEntry *entry; + QObject *val =3D NULL; + + entry =3D qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX); + if (entry) { + QLIST_REMOVE(entry, next); + val =3D qobject_ref(entry->value); + qentry_destroy(entry); + qdict->size--; + } + + return val; +} + + /** * qdict_is_equal(): Test whether the two QDicts are equal * --=20 2.18.0.547.g1d89318c48