4 * Based on ideas by Avi Kivity <avi@redhat.com>
6 * Copyright (C) 2009, 2015 Red Hat Inc.
9 * Luiz Capitulino <lcapitulino@redhat.com>
11 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
12 * See the COPYING.LIB file in the top-level directory.
14 * QObject Reference Counts Terminology
15 * ------------------------------------
17 * - Returning references: A function that returns an object may
18 * return it as either a weak or a strong reference. If the reference
19 * is strong, you are responsible for calling QDECREF() on the reference
22 * If the reference is weak, the owner of the reference may free it at
23 * any time in the future. Before storing the reference anywhere, you
24 * should call QINCREF() to make the reference strong.
26 * - Transferring ownership: when you transfer ownership of a reference
27 * by calling a function, you are no longer responsible for calling
28 * QDECREF() when the reference is no longer needed. In other words,
29 * when the function returns you must behave as if the reference to the
30 * passed object was weak.
39 QTYPE_NONE, /* sentinel value, no QObject has this type code */
52 typedef struct QType {
54 void (*destroy)(struct QObject *);
57 typedef struct QObject {
62 /* Objects definitions must include this */
63 #define QObject_HEAD \
66 /* Get the 'base' part of an object */
67 #define QOBJECT(obj) (&(obj)->base)
69 /* High-level interface for qobject_incref() */
70 #define QINCREF(obj) \
71 qobject_incref(QOBJECT(obj))
73 /* High-level interface for qobject_decref() */
74 #define QDECREF(obj) \
75 qobject_decref(obj ? QOBJECT(obj) : NULL)
77 /* Initialize an object to default values */
78 #define QOBJECT_INIT(obj, qtype_type) \
79 obj->base.refcnt = 1; \
80 obj->base.type = qtype_type
83 * qobject_incref(): Increment QObject's reference count
85 static inline void qobject_incref(QObject *obj)
92 * qobject_decref(): Decrement QObject's reference count, deallocate
93 * when it reaches zero
95 static inline void qobject_decref(QObject *obj)
97 if (obj && --obj->refcnt == 0) {
98 assert(obj->type != NULL);
99 assert(obj->type->destroy != NULL);
100 obj->type->destroy(obj);
105 * qobject_type(): Return the QObject's type
107 static inline qtype_code qobject_type(const QObject *obj)
109 assert(obj->type != NULL);
110 return obj->type->code;
113 extern QObject qnull_;
115 static inline QObject *qnull(void)
117 qobject_incref(&qnull_);
121 #endif /* QOBJECT_H */