From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mbahw-00009T-Bc for qemu-devel@nongnu.org; Thu, 13 Aug 2009 09:51:08 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mbahr-0008T4-Bf for qemu-devel@nongnu.org; Thu, 13 Aug 2009 09:51:07 -0400 Received: from [199.232.76.173] (port=33483 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mbahq-0008Sr-TB for qemu-devel@nongnu.org; Thu, 13 Aug 2009 09:51:02 -0400 Received: from mx2.redhat.com ([66.187.237.31]:45059) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mbahq-0003C8-Aq for qemu-devel@nongnu.org; Thu, 13 Aug 2009 09:51:02 -0400 From: Luiz Capitulino Date: Thu, 13 Aug 2009 10:50:00 -0300 Message-Id: <1250171428-29308-2-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1250171428-29308-1-git-send-email-lcapitulino@redhat.com> References: <1250171428-29308-1-git-send-email-lcapitulino@redhat.com> Subject: [Qemu-devel] [PATCH 01/29] Introduce QObject List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, avi@redhat.com This commit introduces the qobject.h header file, it contains basic QObject definitions and helper macros. Signed-off-by: Luiz Capitulino --- qobject.h | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-) create mode 100644 qobject.h diff --git a/qobject.h b/qobject.h new file mode 100644 index 0000000..467f258 --- /dev/null +++ b/qobject.h @@ -0,0 +1,95 @@ +/* + * 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 qobject_decref() + * 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 qobject_decref() 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 + +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; + +// Get the QObject part of a type +#define QOBJECT(obj) (&obj->base) + +/** + * qobject_init(): Initialize a QObject to default values + */ +static inline void qobject_init(QObject *obj, const QType *type) +{ + obj->refcnt = 1; + obj->type = 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) + obj->type->destroy(obj); +} + +/** + * qobject_type(): Return the QObject's type + */ +static inline qtype_code qobject_type(const QObject *obj) +{ + return obj->type->code; +} + +#endif /* QOBJECT_H */ -- 1.6.4.67.gea5b1