From patchwork Fri May 8 13:34:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 470049 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DB67F140218 for ; Fri, 8 May 2015 23:37:31 +1000 (AEST) Received: from localhost ([::1]:55864 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqiT4-0006I5-62 for incoming@patchwork.ozlabs.org; Fri, 08 May 2015 09:37:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41849) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqiQ1-0000nt-Fl for qemu-devel@nongnu.org; Fri, 08 May 2015 09:34:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YqiQ0-0002WL-CR for qemu-devel@nongnu.org; Fri, 08 May 2015 09:34:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54032) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqiQ0-0002W1-63 for qemu-devel@nongnu.org; Fri, 08 May 2015 09:34:20 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t48DYHSO001509 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 8 May 2015 09:34:18 -0400 Received: from localhost (ovpn-113-30.phx2.redhat.com [10.3.113.30]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t48DYHdt023314; Fri, 8 May 2015 09:34:17 -0400 From: Luiz Capitulino To: peter.maydell@linaro.org Date: Fri, 8 May 2015 09:34:05 -0400 Message-Id: <1431092051-31046-5-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1431092051-31046-1-git-send-email-lcapitulino@redhat.com> References: <1431092051-31046-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 04/10] qobject: Add a special null QObject X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Markus Armbruster I'm going to fix the JSON parser to recognize null. The obvious representation of JSON null as (QObject *)NULL doesn't work, because the parser already uses it as an error value. Perhaps we should change it to free NULL for null, but that's more than I can do right now. Create a special null QObject instead. The existing QDict, QList, and QString all represent something that is a pointer in C and could therefore be associated with NULL. But right now, all three of these sub-types are always non-null once created, so the new null sentinel object is intentionally unrelated to them. Signed-off-by: Markus Armbruster Signed-off-by: Eric Blake Signed-off-by: Luiz Capitulino --- include/qapi/qmp/qobject.h | 11 ++++++++++- qobject/Makefile.objs | 2 +- qobject/qjson.c | 3 +++ qobject/qnull.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 qobject/qnull.c diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index 0991296..84b2d9f 100644 --- a/include/qapi/qmp/qobject.h +++ b/include/qapi/qmp/qobject.h @@ -3,7 +3,7 @@ * * Based on ideas by Avi Kivity * - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009, 2015 Red Hat Inc. * * Authors: * Luiz Capitulino @@ -37,6 +37,7 @@ typedef enum { QTYPE_NONE, /* sentinel value, no QObject has this type code */ + QTYPE_QNULL, QTYPE_QINT, QTYPE_QSTRING, QTYPE_QDICT, @@ -110,4 +111,12 @@ static inline qtype_code qobject_type(const QObject *obj) return obj->type->code; } +extern QObject qnull_; + +static inline QObject *qnull(void) +{ + qobject_incref(&qnull_); + return &qnull_; +} + #endif /* QOBJECT_H */ diff --git a/qobject/Makefile.objs b/qobject/Makefile.objs index c9ff59c..f7595f5 100644 --- a/qobject/Makefile.objs +++ b/qobject/Makefile.objs @@ -1,3 +1,3 @@ -util-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o +util-obj-y = qnull.o qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o util-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o util-obj-y += qerror.o diff --git a/qobject/qjson.c b/qobject/qjson.c index f2857c1..846733d 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -127,6 +127,9 @@ static void to_json_list_iter(QObject *obj, void *opaque) static void to_json(const QObject *obj, QString *str, int pretty, int indent) { switch (qobject_type(obj)) { + case QTYPE_QNULL: + qstring_append(str, "null"); + break; case QTYPE_QINT: { QInt *val = qobject_to_qint(obj); char buffer[1024]; diff --git a/qobject/qnull.c b/qobject/qnull.c new file mode 100644 index 0000000..9873e26 --- /dev/null +++ b/qobject/qnull.c @@ -0,0 +1,29 @@ +/* + * QNull + * + * Copyright (C) 2015 Red Hat, Inc. + * + * Authors: + * Markus Armbruster + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 + * or later. See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu-common.h" +#include "qapi/qmp/qobject.h" + +static void qnull_destroy_obj(QObject *obj) +{ + assert(0); +} + +static const QType qnull_type = { + .code = QTYPE_QNULL, + .destroy = qnull_destroy_obj, +}; + +QObject qnull_ = { + .type = &qnull_type, + .refcnt = 1, +};