xref: /qemu/include/qapi/qmp/qobject.h (revision 55e1819c)
1 /*
2  * QEMU Object Model.
3  *
4  * Based on ideas by Avi Kivity <avi@redhat.com>
5  *
6  * Copyright (C) 2009, 2015 Red Hat Inc.
7  *
8  * Authors:
9  *  Luiz Capitulino <lcapitulino@redhat.com>
10  *
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.
13  *
14  * QObject Reference Counts Terminology
15  * ------------------------------------
16  *
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
20  *  when you are done.
21  *
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.
25  *
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.
31  */
32 #ifndef QOBJECT_H
33 #define QOBJECT_H
34 
35 #include <stddef.h>
36 #include <assert.h>
37 
38 typedef enum {
39     QTYPE_NONE,    /* sentinel value, no QObject has this type code */
40     QTYPE_QNULL,
41     QTYPE_QINT,
42     QTYPE_QSTRING,
43     QTYPE_QDICT,
44     QTYPE_QLIST,
45     QTYPE_QFLOAT,
46     QTYPE_QBOOL,
47     QTYPE_MAX,
48 } qtype_code;
49 
50 typedef struct QObject {
51     qtype_code type;
52     size_t refcnt;
53 } QObject;
54 
55 /* Get the 'base' part of an object */
56 #define QOBJECT(obj) (&(obj)->base)
57 
58 /* High-level interface for qobject_incref() */
59 #define QINCREF(obj)      \
60     qobject_incref(QOBJECT(obj))
61 
62 /* High-level interface for qobject_decref() */
63 #define QDECREF(obj)              \
64     qobject_decref(obj ? QOBJECT(obj) : NULL)
65 
66 /* Initialize an object to default values */
67 static inline void qobject_init(QObject *obj, qtype_code type)
68 {
69     assert(QTYPE_NONE < type && type < QTYPE_MAX);
70     obj->refcnt = 1;
71     obj->type = type;
72 }
73 
74 /**
75  * qobject_incref(): Increment QObject's reference count
76  */
77 static inline void qobject_incref(QObject *obj)
78 {
79     if (obj)
80         obj->refcnt++;
81 }
82 
83 /**
84  * qobject_destroy(): Free resources used by the object
85  */
86 void qobject_destroy(QObject *obj);
87 
88 /**
89  * qobject_decref(): Decrement QObject's reference count, deallocate
90  * when it reaches zero
91  */
92 static inline void qobject_decref(QObject *obj)
93 {
94     assert(!obj || obj->refcnt);
95     if (obj && --obj->refcnt == 0) {
96         qobject_destroy(obj);
97     }
98 }
99 
100 /**
101  * qobject_type(): Return the QObject's type
102  */
103 static inline qtype_code qobject_type(const QObject *obj)
104 {
105     assert(QTYPE_NONE < obj->type && obj->type < QTYPE_MAX);
106     return obj->type;
107 }
108 
109 extern QObject qnull_;
110 
111 static inline QObject *qnull(void)
112 {
113     qobject_incref(&qnull_);
114     return &qnull_;
115 }
116 
117 #endif /* QOBJECT_H */
118