qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 01/29] Introduce QObject


From: Luiz Capitulino
Subject: [Qemu-devel] [PATCH 01/29] Introduce QObject
Date: Wed, 19 Aug 2009 20:07:32 -0300

This commit introduces the qobject.h header file, it contains
basic QObject definitions and helper macros.

Signed-off-by: Luiz Capitulino <address@hidden>
---
 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 <address@hidden>
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <address@hidden>
+ *
+ * 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 <stddef.h>
+#include <assert.h>
+
+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 */
-- 
1.6.4.67.gea5b1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]