xref: /openbmc/qemu/include/qapi/qmp/qobject.h (revision 1d300b5f)
1 /*
2  * QEMU Object Model.
3  *
4  * Based on ideas by Avi Kivity <avi@redhat.com>
5  *
6  * Copyright (C) 2009 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,
40     QTYPE_QINT,
41     QTYPE_QSTRING,
42     QTYPE_QDICT,
43     QTYPE_QLIST,
44     QTYPE_QFLOAT,
45     QTYPE_QBOOL,
46     QTYPE_QERROR,
47     QTYPE_MAX,
48 } qtype_code;
49 
50 struct QObject;
51 
52 typedef struct QType {
53     qtype_code code;
54     void (*destroy)(struct QObject *);
55 } QType;
56 
57 typedef struct QObject {
58     const QType *type;
59     size_t refcnt;
60 } QObject;
61 
62 /* Objects definitions must include this */
63 #define QObject_HEAD  \
64     QObject base
65 
66 /* Get the 'base' part of an object */
67 #define QOBJECT(obj) (&(obj)->base)
68 
69 /* High-level interface for qobject_incref() */
70 #define QINCREF(obj)      \
71     qobject_incref(QOBJECT(obj))
72 
73 /* High-level interface for qobject_decref() */
74 #define QDECREF(obj)              \
75     qobject_decref(obj ? QOBJECT(obj) : NULL)
76 
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
81 
82 /**
83  * qobject_incref(): Increment QObject's reference count
84  */
85 static inline void qobject_incref(QObject *obj)
86 {
87     if (obj)
88         obj->refcnt++;
89 }
90 
91 /**
92  * qobject_decref(): Decrement QObject's reference count, deallocate
93  * when it reaches zero
94  */
95 static inline void qobject_decref(QObject *obj)
96 {
97     if (obj && --obj->refcnt == 0) {
98         assert(obj->type != NULL);
99         assert(obj->type->destroy != NULL);
100         obj->type->destroy(obj);
101     }
102 }
103 
104 /**
105  * qobject_type(): Return the QObject's type
106  */
107 static inline qtype_code qobject_type(const QObject *obj)
108 {
109     assert(obj->type != NULL);
110     return obj->type->code;
111 }
112 
113 #endif /* QOBJECT_H */
114