Patchwork [01/29] Introduce QObject

login
register
mail settings
Submitter Luiz Capitulino
Date Aug. 13, 2009, 1:50 p.m.
Message ID <1250171428-29308-2-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/31300/
State Superseded
Headers show

Comments

Luiz Capitulino - Aug. 13, 2009, 1:50 p.m.
This commit introduces the qobject.h header file, it contains
basic QObject definitions and helper macros.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 qobject.h |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)
 create mode 100644 qobject.h

Patch

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 <avi@redhat.com>
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ *  Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * 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 <stddef.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;
+
+// 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 */