root / qobject.h @ abb6ae2c
History | View | Annotate | Download (2.7 kB)
1 | 5a1a2356 | Luiz Capitulino | /*
|
---|---|---|---|
2 | 5a1a2356 | Luiz Capitulino | * QEMU Object Model.
|
3 | 5a1a2356 | Luiz Capitulino | *
|
4 | 5a1a2356 | Luiz Capitulino | * Based on ideas by Avi Kivity <avi@redhat.com>
|
5 | 5a1a2356 | Luiz Capitulino | *
|
6 | 5a1a2356 | Luiz Capitulino | * Copyright (C) 2009 Red Hat Inc.
|
7 | 5a1a2356 | Luiz Capitulino | *
|
8 | 5a1a2356 | Luiz Capitulino | * Authors:
|
9 | 5a1a2356 | Luiz Capitulino | * Luiz Capitulino <lcapitulino@redhat.com>
|
10 | 5a1a2356 | Luiz Capitulino | *
|
11 | 5a1a2356 | Luiz Capitulino | * This work is licensed under the terms of the GNU GPL, version 2. See
|
12 | 5a1a2356 | Luiz Capitulino | * the COPYING file in the top-level directory.
|
13 | 5a1a2356 | Luiz Capitulino | *
|
14 | 5a1a2356 | Luiz Capitulino | * QObject Reference Counts Terminology
|
15 | 5a1a2356 | Luiz Capitulino | * ------------------------------------
|
16 | 5a1a2356 | Luiz Capitulino | *
|
17 | 5a1a2356 | Luiz Capitulino | * - Returning references: A function that returns an object may
|
18 | 5a1a2356 | Luiz Capitulino | * return it as either a weak or a strong reference. If the reference
|
19 | 5a1a2356 | Luiz Capitulino | * is strong, you are responsible for calling QDECREF() on the reference
|
20 | 5a1a2356 | Luiz Capitulino | * when you are done.
|
21 | 5a1a2356 | Luiz Capitulino | *
|
22 | 5a1a2356 | Luiz Capitulino | * If the reference is weak, the owner of the reference may free it at
|
23 | 5a1a2356 | Luiz Capitulino | * any time in the future. Before storing the reference anywhere, you
|
24 | 5a1a2356 | Luiz Capitulino | * should call QINCREF() to make the reference strong.
|
25 | 5a1a2356 | Luiz Capitulino | *
|
26 | 5a1a2356 | Luiz Capitulino | * - Transferring ownership: when you transfer ownership of a reference
|
27 | 5a1a2356 | Luiz Capitulino | * by calling a function, you are no longer responsible for calling
|
28 | 5a1a2356 | Luiz Capitulino | * QDECREF() when the reference is no longer needed. In other words,
|
29 | 5a1a2356 | Luiz Capitulino | * when the function returns you must behave as if the reference to the
|
30 | 5a1a2356 | Luiz Capitulino | * passed object was weak.
|
31 | 5a1a2356 | Luiz Capitulino | */
|
32 | 5a1a2356 | Luiz Capitulino | #ifndef QOBJECT_H
|
33 | 5a1a2356 | Luiz Capitulino | #define QOBJECT_H
|
34 | 5a1a2356 | Luiz Capitulino | |
35 | 5a1a2356 | Luiz Capitulino | #include <stddef.h> |
36 | 5a1a2356 | Luiz Capitulino | #include <assert.h> |
37 | 5a1a2356 | Luiz Capitulino | |
38 | 5a1a2356 | Luiz Capitulino | typedef enum { |
39 | 5a1a2356 | Luiz Capitulino | QTYPE_NONE, |
40 | 6b8d1ece | Luiz Capitulino | QTYPE_QINT, |
41 | 66f70487 | Luiz Capitulino | QTYPE_QSTRING, |
42 | fb08dde0 | Luiz Capitulino | QTYPE_QDICT, |
43 | 5a1a2356 | Luiz Capitulino | } qtype_code; |
44 | 5a1a2356 | Luiz Capitulino | |
45 | 5a1a2356 | Luiz Capitulino | struct QObject;
|
46 | 5a1a2356 | Luiz Capitulino | |
47 | 5a1a2356 | Luiz Capitulino | typedef struct QType { |
48 | 5a1a2356 | Luiz Capitulino | qtype_code code; |
49 | 5a1a2356 | Luiz Capitulino | void (*destroy)(struct QObject *); |
50 | 5a1a2356 | Luiz Capitulino | } QType; |
51 | 5a1a2356 | Luiz Capitulino | |
52 | 5a1a2356 | Luiz Capitulino | typedef struct QObject { |
53 | 5a1a2356 | Luiz Capitulino | const QType *type;
|
54 | 5a1a2356 | Luiz Capitulino | size_t refcnt; |
55 | 5a1a2356 | Luiz Capitulino | } QObject; |
56 | 5a1a2356 | Luiz Capitulino | |
57 | 5a1a2356 | Luiz Capitulino | /* Objects definitions must include this */
|
58 | 5a1a2356 | Luiz Capitulino | #define QObject_HEAD \
|
59 | 5a1a2356 | Luiz Capitulino | QObject base |
60 | 5a1a2356 | Luiz Capitulino | |
61 | 5a1a2356 | Luiz Capitulino | /* Get the 'base' part of an object */
|
62 | 5a1a2356 | Luiz Capitulino | #define QOBJECT(obj) (&obj->base)
|
63 | 5a1a2356 | Luiz Capitulino | |
64 | 5a1a2356 | Luiz Capitulino | /* High-level interface for qobject_incref() */
|
65 | 5a1a2356 | Luiz Capitulino | #define QINCREF(obj) \
|
66 | 5a1a2356 | Luiz Capitulino | assert(obj != NULL); \
|
67 | 5a1a2356 | Luiz Capitulino | qobject_incref(QOBJECT(obj)) |
68 | 5a1a2356 | Luiz Capitulino | |
69 | 5a1a2356 | Luiz Capitulino | /* High-level interface for qobject_decref() */
|
70 | 5a1a2356 | Luiz Capitulino | #define QDECREF(obj) \
|
71 | 5a1a2356 | Luiz Capitulino | assert(obj != NULL); \
|
72 | 5a1a2356 | Luiz Capitulino | qobject_decref(QOBJECT(obj)) |
73 | 5a1a2356 | Luiz Capitulino | |
74 | 5a1a2356 | Luiz Capitulino | /* Initialize an object to default values */
|
75 | 5a1a2356 | Luiz Capitulino | #define QOBJECT_INIT(obj, qtype_type) \
|
76 | 5a1a2356 | Luiz Capitulino | obj->base.refcnt = 1; \
|
77 | 5a1a2356 | Luiz Capitulino | obj->base.type = qtype_type |
78 | 5a1a2356 | Luiz Capitulino | |
79 | 5a1a2356 | Luiz Capitulino | /**
|
80 | 5a1a2356 | Luiz Capitulino | * qobject_incref(): Increment QObject's reference count
|
81 | 5a1a2356 | Luiz Capitulino | */
|
82 | 5a1a2356 | Luiz Capitulino | static inline void qobject_incref(QObject *obj) |
83 | 5a1a2356 | Luiz Capitulino | { |
84 | 5a1a2356 | Luiz Capitulino | obj->refcnt++; |
85 | 5a1a2356 | Luiz Capitulino | } |
86 | 5a1a2356 | Luiz Capitulino | |
87 | 5a1a2356 | Luiz Capitulino | /**
|
88 | 5a1a2356 | Luiz Capitulino | * qobject_decref(): Decrement QObject's reference count, deallocate
|
89 | 5a1a2356 | Luiz Capitulino | * when it reaches zero
|
90 | 5a1a2356 | Luiz Capitulino | */
|
91 | 5a1a2356 | Luiz Capitulino | static inline void qobject_decref(QObject *obj) |
92 | 5a1a2356 | Luiz Capitulino | { |
93 | 5a1a2356 | Luiz Capitulino | if (--obj->refcnt == 0) { |
94 | 5a1a2356 | Luiz Capitulino | assert(obj->type != NULL);
|
95 | 5a1a2356 | Luiz Capitulino | assert(obj->type->destroy != NULL);
|
96 | 5a1a2356 | Luiz Capitulino | obj->type->destroy(obj); |
97 | 5a1a2356 | Luiz Capitulino | } |
98 | 5a1a2356 | Luiz Capitulino | } |
99 | 5a1a2356 | Luiz Capitulino | |
100 | 5a1a2356 | Luiz Capitulino | /**
|
101 | 5a1a2356 | Luiz Capitulino | * qobject_type(): Return the QObject's type
|
102 | 5a1a2356 | Luiz Capitulino | */
|
103 | 5a1a2356 | Luiz Capitulino | static inline qtype_code qobject_type(const QObject *obj) |
104 | 5a1a2356 | Luiz Capitulino | { |
105 | 5a1a2356 | Luiz Capitulino | assert(obj->type != NULL);
|
106 | 5a1a2356 | Luiz Capitulino | return obj->type->code;
|
107 | 5a1a2356 | Luiz Capitulino | } |
108 | 5a1a2356 | Luiz Capitulino | |
109 | 5a1a2356 | Luiz Capitulino | #endif /* QOBJECT_H */ |