qobject.h 2.6 KB
Newer Older
L
Luiz Capitulino 已提交
1 2 3 4 5
/*
 * QEMU Object Model.
 *
 * Based on ideas by Avi Kivity <avi@redhat.com>
 *
6
 * Copyright (C) 2009, 2015 Red Hat Inc.
L
Luiz Capitulino 已提交
7 8 9 10
 *
 * Authors:
 *  Luiz Capitulino <lcapitulino@redhat.com>
 *
L
Luiz Capitulino 已提交
11 12
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
L
Luiz Capitulino 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * QObject Reference Counts Terminology
 * ------------------------------------
 *
 *  - Returning references: A function that returns an object may
 *  return it as either a weak or a strong reference.  If the reference
 *  is strong, you are responsible for calling QDECREF() on the reference
 *  when you are done.
 *
 *  If the reference is weak, the owner of the reference may free it at
 *  any time in the future.  Before storing the reference anywhere, you
 *  should call QINCREF() to make the reference strong.
 *
 *  - Transferring ownership: when you transfer ownership of a reference
 *  by calling a function, you are no longer responsible for calling
 *  QDECREF() when the reference is no longer needed.  In other words,
 *  when the function returns you must behave as if the reference to the
 *  passed object was weak.
 */
#ifndef QOBJECT_H
#define QOBJECT_H

35
#include "qapi-types.h"
L
Luiz Capitulino 已提交
36

37
struct QObject {
E
Eric Blake 已提交
38
    QType type;
L
Luiz Capitulino 已提交
39
    size_t refcnt;
40
};
L
Luiz Capitulino 已提交
41 42

/* Get the 'base' part of an object */
43
#define QOBJECT(obj) (&(obj)->base)
L
Luiz Capitulino 已提交
44 45 46 47 48 49 50

/* High-level interface for qobject_incref() */
#define QINCREF(obj)      \
    qobject_incref(QOBJECT(obj))

/* High-level interface for qobject_decref() */
#define QDECREF(obj)              \
51
    qobject_decref(obj ? QOBJECT(obj) : NULL)
L
Luiz Capitulino 已提交
52 53

/* Initialize an object to default values */
E
Eric Blake 已提交
54
static inline void qobject_init(QObject *obj, QType type)
E
Eric Blake 已提交
55
{
56
    assert(QTYPE_NONE < type && type < QTYPE__MAX);
E
Eric Blake 已提交
57 58 59
    obj->refcnt = 1;
    obj->type = type;
}
L
Luiz Capitulino 已提交
60 61 62 63 64 65

/**
 * qobject_incref(): Increment QObject's reference count
 */
static inline void qobject_incref(QObject *obj)
{
L
Luiz Capitulino 已提交
66 67
    if (obj)
        obj->refcnt++;
L
Luiz Capitulino 已提交
68 69
}

E
Eric Blake 已提交
70 71 72 73 74
/**
 * qobject_destroy(): Free resources used by the object
 */
void qobject_destroy(QObject *obj);

L
Luiz Capitulino 已提交
75 76 77 78 79 80
/**
 * qobject_decref(): Decrement QObject's reference count, deallocate
 * when it reaches zero
 */
static inline void qobject_decref(QObject *obj)
{
81
    assert(!obj || obj->refcnt);
L
Luiz Capitulino 已提交
82
    if (obj && --obj->refcnt == 0) {
E
Eric Blake 已提交
83
        qobject_destroy(obj);
L
Luiz Capitulino 已提交
84 85 86 87 88 89
    }
}

/**
 * qobject_type(): Return the QObject's type
 */
E
Eric Blake 已提交
90
static inline QType qobject_type(const QObject *obj)
L
Luiz Capitulino 已提交
91
{
92
    assert(QTYPE_NONE < obj->type && obj->type < QTYPE__MAX);
E
Eric Blake 已提交
93
    return obj->type;
L
Luiz Capitulino 已提交
94 95
}

96 97 98 99 100 101 102 103
extern QObject qnull_;

static inline QObject *qnull(void)
{
    qobject_incref(&qnull_);
    return &qnull_;
}

L
Luiz Capitulino 已提交
104
#endif /* QOBJECT_H */