blob: 4cc928770db2ee529edb11afb6037792c2e8479c [file] [log] [blame]
Luiz Capitulino5a1a2352009-08-28 15:27:04 -03001/*
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 GPL, version 2. See
12 * the COPYING 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
38typedef enum {
39 QTYPE_NONE,
Luiz Capitulino6b8d1ec2009-08-28 15:27:05 -030040 QTYPE_QINT,
Luiz Capitulino66f70482009-08-28 15:27:06 -030041 QTYPE_QSTRING,
Luiz Capitulinofb08dde2009-08-28 15:27:07 -030042 QTYPE_QDICT,
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030043 QTYPE_QLIST,
Luiz Capitulino5a1a2352009-08-28 15:27:04 -030044} qtype_code;
45
46struct QObject;
47
48typedef struct QType {
49 qtype_code code;
50 void (*destroy)(struct QObject *);
51} QType;
52
53typedef struct QObject {
54 const QType *type;
55 size_t refcnt;
56} QObject;
57
58/* Objects definitions must include this */
59#define QObject_HEAD \
60 QObject base
61
62/* Get the 'base' part of an object */
63#define QOBJECT(obj) (&obj->base)
64
65/* High-level interface for qobject_incref() */
66#define QINCREF(obj) \
Luiz Capitulino5a1a2352009-08-28 15:27:04 -030067 qobject_incref(QOBJECT(obj))
68
69/* High-level interface for qobject_decref() */
70#define QDECREF(obj) \
Luiz Capitulino5a1a2352009-08-28 15:27:04 -030071 qobject_decref(QOBJECT(obj))
72
73/* Initialize an object to default values */
74#define QOBJECT_INIT(obj, qtype_type) \
75 obj->base.refcnt = 1; \
76 obj->base.type = qtype_type
77
78/**
79 * qobject_incref(): Increment QObject's reference count
80 */
81static inline void qobject_incref(QObject *obj)
82{
Luiz Capitulinod559ba12009-10-07 13:41:47 -030083 if (obj)
84 obj->refcnt++;
Luiz Capitulino5a1a2352009-08-28 15:27:04 -030085}
86
87/**
88 * qobject_decref(): Decrement QObject's reference count, deallocate
89 * when it reaches zero
90 */
91static inline void qobject_decref(QObject *obj)
92{
Luiz Capitulinod559ba12009-10-07 13:41:47 -030093 if (obj && --obj->refcnt == 0) {
Luiz Capitulino5a1a2352009-08-28 15:27:04 -030094 assert(obj->type != NULL);
95 assert(obj->type->destroy != NULL);
96 obj->type->destroy(obj);
97 }
98}
99
100/**
101 * qobject_type(): Return the QObject's type
102 */
103static inline qtype_code qobject_type(const QObject *obj)
104{
105 assert(obj->type != NULL);
106 return obj->type->code;
107}
108
109#endif /* QOBJECT_H */