[Top][All Lists]
[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: |
Thu, 13 Aug 2009 10:50:00 -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 | 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 <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 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 */
--
1.6.4.67.gea5b1
- [Qemu-devel] [PATCH v0 00/29] QMonitor, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 03/29] Introduce QString, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 04/29] Introduce QDict, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 05/29] Add wrappers to functions used by the Monitor, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 06/29] monitor: New format for handlers argument types, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 07/29] monitor: Setup a QDict with arguments to handlers, Luiz Capitulino, 2009/08/13
- [Qemu-devel] [PATCH 09/29] monitor: Port handler_0 to use QDict, Luiz Capitulino, 2009/08/13