From patchwork Wed Aug 19 23:07:32 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 31674 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 bilbo.ozlabs.org (Postfix) with ESMTPS id C5318B7087 for ; Thu, 20 Aug 2009 09:10:52 +1000 (EST) Received: from localhost ([127.0.0.1]:56115 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MduIq-0002gU-0v for incoming@patchwork.ozlabs.org; Wed, 19 Aug 2009 19:10:48 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MduGg-0001Wz-TI for qemu-devel@nongnu.org; Wed, 19 Aug 2009 19:08:34 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MduGb-0001UR-KG for qemu-devel@nongnu.org; Wed, 19 Aug 2009 19:08:33 -0400 Received: from [199.232.76.173] (port=49878 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MduGb-0001UG-HF for qemu-devel@nongnu.org; Wed, 19 Aug 2009 19:08:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:64738) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MduGb-0005RU-0d for qemu-devel@nongnu.org; Wed, 19 Aug 2009 19:08:29 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n7JN8RI3030373; Wed, 19 Aug 2009 19:08:27 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n7JN8Q5G030956; Wed, 19 Aug 2009 19:08:27 -0400 Received: from localhost (vpn-10-5.bos.redhat.com [10.16.10.5]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n7JN8P8L031934; Wed, 19 Aug 2009 19:08:25 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Wed, 19 Aug 2009 20:07:32 -0300 Message-Id: <1250723280-3509-2-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1250723280-3509-1-git-send-email-lcapitulino@redhat.com> References: <1250723280-3509-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: aliguori@us.ibm.com, avi@redhat.com Subject: [Qemu-devel] [PATCH 01/29] Introduce QObject 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 This commit introduces the qobject.h header file, it contains basic QObject definitions and helper macros. Signed-off-by: Luiz Capitulino --- qobject.h | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 110 insertions(+), 0 deletions(-) create mode 100644 qobject.h diff --git a/qobject.h b/qobject.h new file mode 100644 index 0000000..c23b5f3 --- /dev/null +++ b/qobject.h @@ -0,0 +1,110 @@ +/* + * QEMU Object Model. + * + * Based on ideas by Avi Kivity + * + * Copyright (C) 2009 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + * QObject Reference Counts + * ------------------------ + * + * The concept of reference counting used here is the same of Python, ie, + * 'ownership of references'. + * + * Basic terminology: + * + * - Owning a reference: means being responsible for calling QDECREF() + * when the reference is no longer needed + * + * - New reference: means that the caller is now the owner of a reference, + * for example, if you call a function that returns a 'new reference' you + * must call QDECREF() when you are done + * + * - Borrowing a reference: nothing needs to be done, you are not the + * owner of the reference + * + * - Stealing a reference: when you pass a reference to a function that + * 'steals a reference' this function assumes that it now owns that + * reference + */ +#ifndef QOBJECT_H +#define QOBJECT_H + +#include +#include + +typedef enum { + QTYPE_NONE, +} qtype_code; + +struct QObject; + +typedef struct QType { + qtype_code code; + void (*destroy)(struct QObject *); +} QType; + +typedef struct QObject { + const QType *type; + size_t refcnt; +} QObject; + +/* Type definitions must include this */ +#define QType_HEAD \ + QObject base; + +/* Get the QObject part of a type */ +#define QOBJECT(obj) (&obj->base) + +/* High-level interface for qobject_incref() */ +#define QINCREF(qtype) \ + assert(qtype != NULL); \ + qobject_incref(QOBJECT(qtype)) + +/* High-level interface for qobject_decref() */ +#define QDECREF(qtype) \ + assert(qtype != NULL); \ + qobject_decref(QOBJECT(qtype)) + +/* Initialize a type to default values */ +#define QOBJECT_INIT(qtype, qtype_type) \ + qtype->base.refcnt = 1; \ + qtype->base.type = qtype_type + +/** + * qobject_incref(): Increment QObject's reference count + */ +static inline void qobject_incref(QObject *obj) +{ + obj->refcnt++; +} + +/** + * qobject_decref(): Decrement QObject's reference count, deallocate + * when it reaches zero + */ +static inline void qobject_decref(QObject *obj) +{ + if (--obj->refcnt == 0) { + assert(obj->type != NULL); + assert(obj->type->destroy != NULL); + obj->type->destroy(obj); + } +} + +/** + * qobject_type(): Return the QObject's type + */ +static inline qtype_code qobject_type(const QObject *obj) +{ + assert(obj->type != NULL); + return obj->type->code; +} + +#endif /* QOBJECT_H */