9pfs: use g_malloc0 to allocate space for xattr
[qemu/ar7.git] / include / qapi / qmp / qobject.h
blobeab29edd12c4d901c89e30b2911ddcd62f7d4cfa
1 /*
2 * QEMU Object Model.
4 * Based on ideas by Avi Kivity <avi@redhat.com>
6 * Copyright (C) 2009, 2015 Red Hat Inc.
8 * Authors:
9 * Luiz Capitulino <lcapitulino@redhat.com>
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.
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
20 * when you are done.
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.
32 #ifndef QOBJECT_H
33 #define QOBJECT_H
35 #include "qapi-types.h"
37 struct QObject {
38 QType type;
39 size_t refcnt;
42 /* Get the 'base' part of an object */
43 #define QOBJECT(obj) (&(obj)->base)
45 /* High-level interface for qobject_incref() */
46 #define QINCREF(obj) \
47 qobject_incref(QOBJECT(obj))
49 /* High-level interface for qobject_decref() */
50 #define QDECREF(obj) \
51 qobject_decref(obj ? QOBJECT(obj) : NULL)
53 /* Initialize an object to default values */
54 static inline void qobject_init(QObject *obj, QType type)
56 assert(QTYPE_NONE < type && type < QTYPE__MAX);
57 obj->refcnt = 1;
58 obj->type = type;
61 /**
62 * qobject_incref(): Increment QObject's reference count
64 static inline void qobject_incref(QObject *obj)
66 if (obj)
67 obj->refcnt++;
70 /**
71 * qobject_destroy(): Free resources used by the object
73 void qobject_destroy(QObject *obj);
75 /**
76 * qobject_decref(): Decrement QObject's reference count, deallocate
77 * when it reaches zero
79 static inline void qobject_decref(QObject *obj)
81 assert(!obj || obj->refcnt);
82 if (obj && --obj->refcnt == 0) {
83 qobject_destroy(obj);
87 /**
88 * qobject_type(): Return the QObject's type
90 static inline QType qobject_type(const QObject *obj)
92 assert(QTYPE_NONE < obj->type && obj->type < QTYPE__MAX);
93 return obj->type;
96 struct QNull {
97 QObject base;
100 extern QNull qnull_;
102 static inline QNull *qnull(void)
104 QINCREF(&qnull_);
105 return &qnull_;
108 #endif /* QOBJECT_H */