]>
git.proxmox.com Git - qemu.git/blob - qobject.h
c6bee8ec6125068ed83dc156e114da12e23da43c
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.
45 typedef struct QType
{
47 void (*destroy
)(struct QObject
*);
50 typedef struct QObject
{
55 /* Objects definitions must include this */
56 #define QObject_HEAD \
59 /* Get the 'base' part of an object */
60 #define QOBJECT(obj) (&obj->base)
62 /* High-level interface for qobject_incref() */
63 #define QINCREF(obj) \
64 assert(obj != NULL); \
65 qobject_incref(QOBJECT(obj))
67 /* High-level interface for qobject_decref() */
68 #define QDECREF(obj) \
69 assert(obj != NULL); \
70 qobject_decref(QOBJECT(obj))
72 /* Initialize an object to default values */
73 #define QOBJECT_INIT(obj, qtype_type) \
74 obj->base.refcnt = 1; \
75 obj->base.type = qtype_type
78 * qobject_incref(): Increment QObject's reference count
80 static inline void qobject_incref(QObject
*obj
)
86 * qobject_decref(): Decrement QObject's reference count, deallocate
87 * when it reaches zero
89 static inline void qobject_decref(QObject
*obj
)
91 if (--obj
->refcnt
== 0) {
92 assert(obj
->type
!= NULL
);
93 assert(obj
->type
->destroy
!= NULL
);
94 obj
->type
->destroy(obj
);
99 * qobject_type(): Return the QObject's type
101 static inline qtype_code
qobject_type(const QObject
*obj
)
103 assert(obj
->type
!= NULL
);
104 return obj
->type
->code
;
107 #endif /* QOBJECT_H */