4 * Based on ideas by Avi Kivity <avi@redhat.com>
6 * Copyright (C) 2009 Red Hat Inc.
9 * Luiz Capitulino <lcapitulino@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING 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.
50 typedef struct QType
{
52 void (*destroy
)(struct QObject
*);
55 typedef struct QObject
{
60 /* Objects definitions must include this */
61 #define QObject_HEAD \
64 /* Get the 'base' part of an object */
65 #define QOBJECT(obj) (&(obj)->base)
67 /* High-level interface for qobject_incref() */
68 #define QINCREF(obj) \
69 qobject_incref(QOBJECT(obj))
71 /* High-level interface for qobject_decref() */
72 #define QDECREF(obj) \
73 qobject_decref(QOBJECT(obj))
75 /* Initialize an object to default values */
76 #define QOBJECT_INIT(obj, qtype_type) \
77 obj->base.refcnt = 1; \
78 obj->base.type = qtype_type
81 * qobject_incref(): Increment QObject's reference count
83 static inline void qobject_incref(QObject
*obj
)
90 * qobject_decref(): Decrement QObject's reference count, deallocate
91 * when it reaches zero
93 static inline void qobject_decref(QObject
*obj
)
95 if (obj
&& --obj
->refcnt
== 0) {
96 assert(obj
->type
!= NULL
);
97 assert(obj
->type
->destroy
!= NULL
);
98 obj
->type
->destroy(obj
);
103 * qobject_type(): Return the QObject's type
105 static inline qtype_code
qobject_type(const QObject
*obj
)
107 assert(obj
->type
!= NULL
);
108 return obj
->type
->code
;
111 #endif /* QOBJECT_H */