From nobody Tue Feb 10 02:28:04 2026 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.zoho.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; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496856445745398.41149075765884; Wed, 7 Jun 2017 10:27:25 -0700 (PDT) Received: from localhost ([::1]:45321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIeYj-0007od-AG for importer@patchew.org; Wed, 07 Jun 2017 13:15:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dIe2F-00034P-8b for qemu-devel@nongnu.org; Wed, 07 Jun 2017 12:42:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dIe2C-0001Fd-3S for qemu-devel@nongnu.org; Wed, 07 Jun 2017 12:42:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46396) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dIe2B-0001FV-RI for qemu-devel@nongnu.org; Wed, 07 Jun 2017 12:42:16 -0400 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 D282D1293 for ; Wed, 7 Jun 2017 16:42:14 +0000 (UTC) Received: from localhost (ovpn-112-40.ams2.redhat.com [10.36.112.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4FA37E660; Wed, 7 Jun 2017 16:42:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D282D1293 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=pass smtp.mailfrom=marcandre.lureau@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D282D1293 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Wed, 7 Jun 2017 20:36:34 +0400 Message-Id: <20170607163635.17635-43-marcandre.lureau@redhat.com> In-Reply-To: <20170607163635.17635-1-marcandre.lureau@redhat.com> References: <20170607163635.17635-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 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.29]); Wed, 07 Jun 2017 16:42:15 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 42/43] RFC: qdict: add uint 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: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , armbru@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Similar to int support, add uint support. Note this is RFC because this is currently unused in qemu, I haven't found a good user for it yet (kaslr qemu-ga code did use it though). Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Markus Armbruster --- include/qapi/qmp/qdict.h | 5 +++++ qobject/qdict.c | 33 +++++++++++++++++++++++++++++++++ tests/check-qdict.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 363e431106..8c7c2b762b 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -56,6 +56,8 @@ void qdict_destroy_obj(QObject *obj); /* Helpers for int, bool, and string */ #define qdict_put_int(qdict, key, value) \ qdict_put(qdict, key, qnum_from_int(value)) +#define qdict_put_uint(qdict, key, value) \ + qdict_put(qdict, key, qnum_from_uint(value)) #define qdict_put_bool(qdict, key, value) \ qdict_put(qdict, key, qbool_from_bool(value)) #define qdict_put_str(qdict, key, value) \ @@ -64,12 +66,15 @@ void qdict_destroy_obj(QObject *obj); /* High level helpers */ double qdict_get_double(const QDict *qdict, const char *key); int64_t qdict_get_int(const QDict *qdict, const char *key); +uint64_t qdict_get_uint(const QDict *qdict, const char *key); bool qdict_get_bool(const QDict *qdict, const char *key); QList *qdict_get_qlist(const QDict *qdict, const char *key); QDict *qdict_get_qdict(const QDict *qdict, const char *key); const char *qdict_get_str(const QDict *qdict, const char *key); int64_t qdict_get_try_int(const QDict *qdict, const char *key, int64_t def_value); +uint64_t qdict_get_try_uint(const QDict *qdict, const char *key, + uint64_t def_value); bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_valu= e); const char *qdict_get_try_str(const QDict *qdict, const char *key); =20 diff --git a/qobject/qdict.c b/qobject/qdict.c index 576018e531..65069baa1b 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -202,6 +202,19 @@ int64_t qdict_get_int(const QDict *qdict, const char *= key) } =20 /** + * qdict_get_uint(): Get an unsigned integer mapped by 'key' + * + * This function assumes that 'key' exists and it stores a + * QNum int object. + * + * Return unsigned integer mapped by 'key'. + */ +uint64_t qdict_get_uint(const QDict *qdict, const char *key) +{ + return qnum_get_uint(qobject_to_qnum(qdict_get(qdict, key))); +} + +/** * qdict_get_bool(): Get a bool mapped by 'key' * * This function assumes that 'key' exists and it stores a @@ -265,6 +278,26 @@ int64_t qdict_get_try_int(const QDict *qdict, const ch= ar *key, } =20 /** + * qdict_get_try_uint(): Try to get usigned integer mapped by 'key' + * + * Return unsigned integer mapped by 'key', if it is not present in + * the dictionary or if the stored object is not of QNum type + * 'def_value' will be returned. + */ +uint64_t qdict_get_try_uint(const QDict *qdict, const char *key, + uint64_t def_value) +{ + QNum *qnum =3D qobject_to_qnum(qdict_get(qdict, key)); + uint64_t val; + + if (!qnum || !qnum_get_try_uint(qnum, &val)) { + val =3D def_value; + } + + return val; +} + +/** * qdict_get_try_bool(): Try to get a bool mapped by 'key' * * Return bool mapped by 'key', if it is not present in the diff --git a/tests/check-qdict.c b/tests/check-qdict.c index 35405778cc..34392869ac 100644 --- a/tests/check-qdict.c +++ b/tests/check-qdict.c @@ -100,6 +100,21 @@ static void qdict_get_int_test(void) QDECREF(tests_dict); } =20 +static void qdict_get_uint_test(void) +{ + uint64_t ret; + const uint64_t value =3D UINT64_MAX; + const char *key =3D "uint"; + QDict *tests_dict =3D qdict_new(); + + qdict_put_uint(tests_dict, key, value); + + ret =3D qdict_get_uint(tests_dict, key); + g_assert(ret =3D=3D value); + + QDECREF(tests_dict); +} + static void qdict_get_try_int_test(void) { int ret; @@ -122,6 +137,28 @@ static void qdict_get_try_int_test(void) QDECREF(tests_dict); } =20 +static void qdict_get_try_uint_test(void) +{ + uint64_t ret; + const uint64_t value =3D UINT64_MAX; + const char *key =3D "uint"; + QDict *tests_dict =3D qdict_new(); + + qdict_put_uint(tests_dict, key, value); + qdict_put_str(tests_dict, "string", "test"); + + ret =3D qdict_get_try_uint(tests_dict, key, 0); + g_assert_cmpuint(ret, =3D=3D, value); + + ret =3D qdict_get_try_uint(tests_dict, "missing", 42); + g_assert_cmpuint(ret, =3D=3D, 42); + + ret =3D qdict_get_try_uint(tests_dict, "string", 42); + g_assert_cmpuint(ret, =3D=3D, 42); + + QDECREF(tests_dict); +} + static void qdict_get_str_test(void) { const char *p; @@ -854,7 +891,9 @@ int main(int argc, char **argv) /* Continue, but now with fixtures */ g_test_add_func("/public/get", qdict_get_test); g_test_add_func("/public/get_int", qdict_get_int_test); + g_test_add_func("/public/get_uint", qdict_get_uint_test); g_test_add_func("/public/get_try_int", qdict_get_try_int_test); + g_test_add_func("/public/get_try_uint", qdict_get_try_uint_test); g_test_add_func("/public/get_str", qdict_get_str_test); g_test_add_func("/public/get_try_str", qdict_get_try_str_test); g_test_add_func("/public/defaults", qdict_defaults_test); --=20 2.13.0.91.g00982b8dd