From patchwork Sun Oct 18 21:50:07 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 36344 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C897FB7BAE for ; Mon, 19 Oct 2009 08:50:54 +1100 (EST) Received: from localhost ([127.0.0.1]:49392 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MzdeN-0001Uf-01 for incoming@patchwork.ozlabs.org; Sun, 18 Oct 2009 17:50:51 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mzddt-0001U5-4d for qemu-devel@nongnu.org; Sun, 18 Oct 2009 17:50:21 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mzddo-0001TR-Fd for qemu-devel@nongnu.org; Sun, 18 Oct 2009 17:50:20 -0400 Received: from [199.232.76.173] (port=53794 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mzddo-0001TO-Cl for qemu-devel@nongnu.org; Sun, 18 Oct 2009 17:50:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:17823) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mzddn-000337-Py for qemu-devel@nongnu.org; Sun, 18 Oct 2009 17:50:16 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9ILoEMY004243; Sun, 18 Oct 2009 17:50:14 -0400 Received: from doriath (vpn-12-32.rdu.redhat.com [10.11.12.32]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n9ILoA2m011580 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Sun, 18 Oct 2009 17:50:13 -0400 Date: Sun, 18 Oct 2009 19:50:07 -0200 From: Luiz Capitulino To: Anthony Liguori Subject: Re: [Qemu-devel] [PATCH 06/11] qobject: add QBool type Message-ID: <20091018195007.4dbaa599@doriath> In-Reply-To: <1255786571-3528-7-git-send-email-aliguori@us.ibm.com> References: <1255786571-3528-1-git-send-email-aliguori@us.ibm.com> <1255786571-3528-7-git-send-email-aliguori@us.ibm.com> Organization: Red Hat Mime-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Anthony Liguori , qemu-devel@nongnu.org X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org On Sat, 17 Oct 2009 08:36:06 -0500 Anthony Liguori wrote: > We currently model as json bool as an int. This works fine on the server side > but it means we cannot send back proper bools to the client. Introducing a > proper QBool type fixes that. As we talked earlier today, I think it would be simpler to have QTrue, QFalse and QNull as static objects. We could define that static objects get reference counting disabled, I don't see issues in doing that and we get a very simple model. The big patch bellow does it, only compiled tested though. If you don't agree with this model, we'll have to allocate QNull objects. Actually I would suggest a destroy() method which always resets the refcount to 1, but then it will have the concurrency problems on threaded applications you've mentioned. diff --git a/Makefile b/Makefile index e78a3d0..1f985cf 100644 --- a/Makefile +++ b/Makefile @@ -125,7 +125,7 @@ obj-y += net.o net-queue.o obj-y += qemu-char.o aio.o net-checksum.o savevm.o obj-y += msmouse.o ps2.o obj-y += qdev.o qdev-properties.o -obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o +obj-y += qint.o qstring.o qdict.o qlist.o qmisc.o qemu-config.o obj-$(CONFIG_BRLAPI) += baum.o obj-$(CONFIG_WIN32) += tap-win32.o diff --git a/qmisc.c b/qmisc.c new file mode 100644 index 0000000..38a87dd --- /dev/null +++ b/qmisc.c @@ -0,0 +1,31 @@ +#include "qmisc.h" + +/* + * QBool + */ + +static const QType qbool_type = { + .code = QTYPE_QBOOL, +}; + +const QBool QTrue = { + QOBJECT_INIT_STATIC(&qbool_type), + .value = 1 +}; + +const QBool QFalse = { + QOBJECT_INIT_STATIC(&qbool_type), + .value = 0 +}; + +/* + * QNull + */ + +static const QType qnull_type = { + .code = QTYPE_QNULL, +}; + +const qnull QNull = { + QOBJECT_INIT_STATIC(&qnull_type), +}; diff --git a/qmisc.h b/qmisc.h new file mode 100644 index 0000000..dd9d5a9 --- /dev/null +++ b/qmisc.h @@ -0,0 +1,38 @@ +#ifndef QMISC_H +#define QMISC_H + +#include "qobject.h" + +/* + * QBool + */ + +typedef struct QBool { + QObject_HEAD; + int value; +} QBool; + +extern const QBool QTrue; +extern const QBool QFalse; + +static inline int qbool_is_true(const QBool *qbool) +{ + return qbool->value; +} + +static inline int qbool_is_false(const QBool *qbool) +{ + return !qbool->value; +} + +/* + * QNull + */ + +typedef struct qnull { + QObject_HEAD; +} qnull; + +extern const qnull QNull; + +#endif /* QMISC_H */ diff --git a/qobject.h b/qobject.h index 4cc9287..81e4d94 100644 --- a/qobject.h +++ b/qobject.h @@ -41,6 +41,8 @@ typedef enum { QTYPE_QSTRING, QTYPE_QDICT, QTYPE_QLIST, + QTYPE_QBOOL, + QTYPE_QNULL, } qtype_code; struct QObject; @@ -75,12 +77,15 @@ typedef struct QObject { obj->base.refcnt = 1; \ obj->base.type = qtype_type +#define QOBJECT_INIT_STATIC(qtype_type) \ + .base.type = qtype_type + /** * qobject_incref(): Increment QObject's reference count */ static inline void qobject_incref(QObject *obj) { - if (obj) + if (obj && obj->type->destroy) obj->refcnt++; } @@ -90,9 +95,7 @@ static inline void qobject_incref(QObject *obj) */ static inline void qobject_decref(QObject *obj) { - if (obj && --obj->refcnt == 0) { - assert(obj->type != NULL); - assert(obj->type->destroy != NULL); + if (obj && obj->type->destroy && --obj->refcnt == 0) { obj->type->destroy(obj); } }