Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 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 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 | |
| 38 | typedef enum { |
| 39 | QTYPE_NONE, |
Luiz Capitulino | 6b8d1ec | 2009-08-28 15:27:05 -0300 | [diff] [blame] | 40 | QTYPE_QINT, |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 41 | QTYPE_QSTRING, |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 42 | QTYPE_QDICT, |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 43 | QTYPE_QLIST, |
Anthony Liguori | ec072ce | 2009-11-11 10:36:08 -0600 | [diff] [blame] | 44 | QTYPE_QFLOAT, |
Anthony Liguori | f7e6b19 | 2009-11-11 10:37:39 -0600 | [diff] [blame] | 45 | QTYPE_QBOOL, |
Luiz Capitulino | 9f9daf9 | 2009-11-18 23:05:30 -0200 | [diff] [blame] | 46 | QTYPE_QERROR, |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 47 | } qtype_code; |
| 48 | |
| 49 | struct QObject; |
| 50 | |
| 51 | typedef struct QType { |
| 52 | qtype_code code; |
| 53 | void (*destroy)(struct QObject *); |
| 54 | } QType; |
| 55 | |
| 56 | typedef struct QObject { |
| 57 | const QType *type; |
| 58 | size_t refcnt; |
| 59 | } QObject; |
| 60 | |
| 61 | /* Objects definitions must include this */ |
| 62 | #define QObject_HEAD \ |
| 63 | QObject base |
| 64 | |
| 65 | /* Get the 'base' part of an object */ |
Anthony Liguori | c99ca93 | 2009-11-11 10:50:36 -0600 | [diff] [blame] | 66 | #define QOBJECT(obj) (&(obj)->base) |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 67 | |
| 68 | /* High-level interface for qobject_incref() */ |
| 69 | #define QINCREF(obj) \ |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 70 | qobject_incref(QOBJECT(obj)) |
| 71 | |
| 72 | /* High-level interface for qobject_decref() */ |
| 73 | #define QDECREF(obj) \ |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 74 | qobject_decref(QOBJECT(obj)) |
| 75 | |
| 76 | /* Initialize an object to default values */ |
| 77 | #define QOBJECT_INIT(obj, qtype_type) \ |
| 78 | obj->base.refcnt = 1; \ |
| 79 | obj->base.type = qtype_type |
| 80 | |
| 81 | /** |
| 82 | * qobject_incref(): Increment QObject's reference count |
| 83 | */ |
| 84 | static inline void qobject_incref(QObject *obj) |
| 85 | { |
Luiz Capitulino | d559ba1 | 2009-10-07 13:41:47 -0300 | [diff] [blame] | 86 | if (obj) |
| 87 | obj->refcnt++; |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * qobject_decref(): Decrement QObject's reference count, deallocate |
| 92 | * when it reaches zero |
| 93 | */ |
| 94 | static inline void qobject_decref(QObject *obj) |
| 95 | { |
Luiz Capitulino | d559ba1 | 2009-10-07 13:41:47 -0300 | [diff] [blame] | 96 | if (obj && --obj->refcnt == 0) { |
Luiz Capitulino | 5a1a235 | 2009-08-28 15:27:04 -0300 | [diff] [blame] | 97 | assert(obj->type != NULL); |
| 98 | assert(obj->type->destroy != NULL); |
| 99 | obj->type->destroy(obj); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * qobject_type(): Return the QObject's type |
| 105 | */ |
| 106 | static inline qtype_code qobject_type(const QObject *obj) |
| 107 | { |
| 108 | assert(obj->type != NULL); |
| 109 | return obj->type->code; |
| 110 | } |
| 111 | |
| 112 | #endif /* QOBJECT_H */ |