From nobody Wed Nov 5 14:58:28 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.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 1497454415072317.55376475312835; Wed, 14 Jun 2017 08:33:35 -0700 (PDT) Received: from localhost ([::1]:49627 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLAIU-0000KA-JV for importer@patchew.org; Wed, 14 Jun 2017 11:33:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLAGO-0007FR-N9 for qemu-devel@nongnu.org; Wed, 14 Jun 2017 11:31:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dLAGN-0003sC-1G for qemu-devel@nongnu.org; Wed, 14 Jun 2017 11:31:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60628) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dLAGJ-0003pA-11; Wed, 14 Jun 2017 11:31:15 -0400 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 F0C664DD5F; Wed, 14 Jun 2017 15:31:13 +0000 (UTC) Received: from localhost (unknown [10.40.205.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 368B48E516; Wed, 14 Jun 2017 15:31:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F0C664DD5F 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=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com F0C664DD5F From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 14 Jun 2017 17:31:00 +0200 Message-Id: <20170614153102.9077-3-mreitz@redhat.com> In-Reply-To: <20170614153102.9077-1-mreitz@redhat.com> References: <20170614153102.9077-1-mreitz@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.38]); Wed, 14 Jun 2017 15:31:14 +0000 (UTC) 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 2/4] qapi: Add qobject_is_equal() 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 , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This generic function (along with its implementations for different types) determines whether two QObjects are equal. Signed-off-by: Max Reitz --- include/qapi/qmp/qbool.h | 1 + include/qapi/qmp/qdict.h | 1 + include/qapi/qmp/qfloat.h | 1 + include/qapi/qmp/qint.h | 1 + include/qapi/qmp/qlist.h | 1 + include/qapi/qmp/qnull.h | 2 ++ include/qapi/qmp/qobject.h | 9 +++++++++ include/qapi/qmp/qstring.h | 1 + qobject/qbool.c | 8 ++++++++ qobject/qdict.c | 24 ++++++++++++++++++++++++ qobject/qfloat.c | 8 ++++++++ qobject/qint.c | 8 ++++++++ qobject/qlist.c | 25 +++++++++++++++++++++++++ qobject/qnull.c | 5 +++++ qobject/qobject.c | 30 ++++++++++++++++++++++++++++++ qobject/qstring.c | 9 +++++++++ 16 files changed, 134 insertions(+) diff --git a/include/qapi/qmp/qbool.h b/include/qapi/qmp/qbool.h index a41111c..f77ea86 100644 --- a/include/qapi/qmp/qbool.h +++ b/include/qapi/qmp/qbool.h @@ -24,6 +24,7 @@ typedef struct QBool { QBool *qbool_from_bool(bool value); bool qbool_get_bool(const QBool *qb); QBool *qobject_to_qbool(const QObject *obj); +bool qbool_is_equal(const QObject *x, const QObject *y); void qbool_destroy_obj(QObject *obj); =20 #endif /* QBOOL_H */ diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 188440a..134a526 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -41,6 +41,7 @@ void qdict_del(QDict *qdict, const char *key); int qdict_haskey(const QDict *qdict, const char *key); QObject *qdict_get(const QDict *qdict, const char *key); QDict *qobject_to_qdict(const QObject *obj); +bool qdict_is_equal(const QObject *x, const QObject *y); void qdict_iter(const QDict *qdict, void (*iter)(const char *key, QObject *obj, void *opaque), void *opaque); diff --git a/include/qapi/qmp/qfloat.h b/include/qapi/qmp/qfloat.h index b5d1583..318e91e 100644 --- a/include/qapi/qmp/qfloat.h +++ b/include/qapi/qmp/qfloat.h @@ -24,6 +24,7 @@ typedef struct QFloat { QFloat *qfloat_from_double(double value); double qfloat_get_double(const QFloat *qi); QFloat *qobject_to_qfloat(const QObject *obj); +bool qfloat_is_equal(const QObject *x, const QObject *y); void qfloat_destroy_obj(QObject *obj); =20 #endif /* QFLOAT_H */ diff --git a/include/qapi/qmp/qint.h b/include/qapi/qmp/qint.h index 3aaff76..20975da 100644 --- a/include/qapi/qmp/qint.h +++ b/include/qapi/qmp/qint.h @@ -23,6 +23,7 @@ typedef struct QInt { QInt *qint_from_int(int64_t value); int64_t qint_get_int(const QInt *qi); QInt *qobject_to_qint(const QObject *obj); +bool qint_is_equal(const QObject *x, const QObject *y); void qint_destroy_obj(QObject *obj); =20 #endif /* QINT_H */ diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h index 5dc4ed9..4380a5b 100644 --- a/include/qapi/qmp/qlist.h +++ b/include/qapi/qmp/qlist.h @@ -57,6 +57,7 @@ QObject *qlist_peek(QList *qlist); int qlist_empty(const QList *qlist); size_t qlist_size(const QList *qlist); QList *qobject_to_qlist(const QObject *obj); +bool qlist_is_equal(const QObject *x, const QObject *y); void qlist_destroy_obj(QObject *obj); =20 static inline const QListEntry *qlist_first(const QList *qlist) diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h index 69555ac..9299683 100644 --- a/include/qapi/qmp/qnull.h +++ b/include/qapi/qmp/qnull.h @@ -23,4 +23,6 @@ static inline QObject *qnull(void) return &qnull_; } =20 +bool qnull_is_equal(const QObject *x, const QObject *y); + #endif /* QNULL_H */ diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index ef1d1a9..cac72e3 100644 --- a/include/qapi/qmp/qobject.h +++ b/include/qapi/qmp/qobject.h @@ -68,6 +68,15 @@ static inline void qobject_incref(QObject *obj) } =20 /** + * qobject_is_equal(): Returns whether the two objects are equal. + * + * Any of the pointers may be NULL; will return true if both are. Will alw= ays + * return false if only one is (therefore a QNull object is not considered= equal + * to a NULL pointer). + */ +bool qobject_is_equal(const QObject *x, const QObject *y); + +/** * qobject_destroy(): Free resources used by the object */ void qobject_destroy(QObject *obj); diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h index 10076b7..65c05a9 100644 --- a/include/qapi/qmp/qstring.h +++ b/include/qapi/qmp/qstring.h @@ -31,6 +31,7 @@ void qstring_append_int(QString *qstring, int64_t value); void qstring_append(QString *qstring, const char *str); void qstring_append_chr(QString *qstring, int c); QString *qobject_to_qstring(const QObject *obj); +bool qstring_is_equal(const QObject *x, const QObject *y); void qstring_destroy_obj(QObject *obj); =20 #endif /* QSTRING_H */ diff --git a/qobject/qbool.c b/qobject/qbool.c index 0606bbd..f1d1719 100644 --- a/qobject/qbool.c +++ b/qobject/qbool.c @@ -52,6 +52,14 @@ QBool *qobject_to_qbool(const QObject *obj) } =20 /** + * qbool_is_equal(): Tests whether the two QBools are equal + */ +bool qbool_is_equal(const QObject *x, const QObject *y) +{ + return qobject_to_qbool(x)->value =3D=3D qobject_to_qbool(y)->value; +} + +/** * qbool_destroy_obj(): Free all memory allocated by a * QBool object */ diff --git a/qobject/qdict.c b/qobject/qdict.c index 88e2ecd..2ed57ca 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -410,6 +410,30 @@ void qdict_del(QDict *qdict, const char *key) } =20 /** + * qdict_is_equal(): Tests whether the two QDicts are equal + */ +bool qdict_is_equal(const QObject *x, const QObject *y) +{ + const QDict *dict_x =3D qobject_to_qdict(x), *dict_y =3D qobject_to_qd= ict(y); + const QDictEntry *e; + + if (qdict_size(dict_x) !=3D qdict_size(dict_y)) { + return false; + } + + for (e =3D qdict_first(dict_x); e; e =3D qdict_next(dict_x, e)) { + const QObject *obj_x =3D qdict_entry_value(e); + const QObject *obj_y =3D qdict_get(dict_y, qdict_entry_key(e)); + + if (!qobject_is_equal(obj_x, obj_y)) { + return false; + } + } + + return true; +} + +/** * qdict_destroy_obj(): Free all the memory allocated by a QDict */ void qdict_destroy_obj(QObject *obj) diff --git a/qobject/qfloat.c b/qobject/qfloat.c index d5da847..291ecc4 100644 --- a/qobject/qfloat.c +++ b/qobject/qfloat.c @@ -52,6 +52,14 @@ QFloat *qobject_to_qfloat(const QObject *obj) } =20 /** + * qfloat_is_equal(): Tests whether the two QFloats are equal + */ +bool qfloat_is_equal(const QObject *x, const QObject *y) +{ + return qobject_to_qfloat(x)->value =3D=3D qobject_to_qfloat(y)->value; +} + +/** * qfloat_destroy_obj(): Free all memory allocated by a * QFloat object */ diff --git a/qobject/qint.c b/qobject/qint.c index d7d1b30..f638cb7 100644 --- a/qobject/qint.c +++ b/qobject/qint.c @@ -51,6 +51,14 @@ QInt *qobject_to_qint(const QObject *obj) } =20 /** + * qint_is_equal(): Tests whether the two QInts are equal + */ +bool qint_is_equal(const QObject *x, const QObject *y) +{ + return qobject_to_qint(x)->value =3D=3D qobject_to_qint(y)->value; +} + +/** * qint_destroy_obj(): Free all memory allocated by a * QInt object */ diff --git a/qobject/qlist.c b/qobject/qlist.c index 86b60cb..b3033c6 100644 --- a/qobject/qlist.c +++ b/qobject/qlist.c @@ -140,6 +140,31 @@ QList *qobject_to_qlist(const QObject *obj) } =20 /** + * qlist_is_equal(): Tests whether the two QLists are equal + */ +bool qlist_is_equal(const QObject *x, const QObject *y) +{ + const QList *list_x =3D qobject_to_qlist(x), *list_y =3D qobject_to_ql= ist(y); + const QListEntry *entry_x, *entry_y; + + entry_x =3D qlist_first(list_x); + entry_y =3D qlist_first(list_y); + + while (entry_x && entry_y) { + if (!qobject_is_equal(qlist_entry_obj(entry_x), + qlist_entry_obj(entry_y))) + { + return false; + } + + entry_x =3D qlist_next(entry_x); + entry_y =3D qlist_next(entry_y); + } + + return !entry_x && !entry_y; +} + +/** * qlist_destroy_obj(): Free all the memory allocated by a QList */ void qlist_destroy_obj(QObject *obj) diff --git a/qobject/qnull.c b/qobject/qnull.c index b3cc85e..af5453d 100644 --- a/qobject/qnull.c +++ b/qobject/qnull.c @@ -19,3 +19,8 @@ QObject qnull_ =3D { .type =3D QTYPE_QNULL, .refcnt =3D 1, }; + +bool qnull_is_equal(const QObject *x, const QObject *y) +{ + return true; +} diff --git a/qobject/qobject.c b/qobject/qobject.c index fe4fa10..af2869a 100644 --- a/qobject/qobject.c +++ b/qobject/qobject.c @@ -28,3 +28,33 @@ void qobject_destroy(QObject *obj) assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX); qdestroy[obj->type](obj); } + + +static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) =3D= { + [QTYPE_NONE] =3D NULL, /* No such object exists */ + [QTYPE_QNULL] =3D qnull_is_equal, + [QTYPE_QINT] =3D qint_is_equal, + [QTYPE_QSTRING] =3D qstring_is_equal, + [QTYPE_QDICT] =3D qdict_is_equal, + [QTYPE_QLIST] =3D qlist_is_equal, + [QTYPE_QFLOAT] =3D qfloat_is_equal, + [QTYPE_QBOOL] =3D qbool_is_equal, +}; + +bool qobject_is_equal(const QObject *x, const QObject *y) +{ + /* We cannot test x =3D=3D y because an object does not need to be equ= al to + * itself (e.g. NaN floats are not). */ + + if (!x && !y) { + return true; + } + + if (!x || !y || x->type !=3D y->type) { + return false; + } + + assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX); + + return qis_equal[x->type](x, y); +} diff --git a/qobject/qstring.c b/qobject/qstring.c index 5da7b5f..a2b51fa 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -129,6 +129,15 @@ const char *qstring_get_str(const QString *qstring) } =20 /** + * qstring_is_equal(): Tests whether the two QStrings are equal + */ +bool qstring_is_equal(const QObject *x, const QObject *y) +{ + return !strcmp(qobject_to_qstring(x)->string, + qobject_to_qstring(y)->string); +} + +/** * qstring_destroy_obj(): Free all memory allocated by a QString * object */ --=20 2.9.4