virobject.h 4.3 KB
Newer Older
1 2 3
/*
 * virobject.h: libvirt reference counted object
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2012-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
18 19 20 21
 * <http://www.gnu.org/licenses/>.
 *
 */

22
#pragma once
23

24 25
#include "internal.h"
#include "virthread.h"
26 27 28 29 30 31 32

typedef struct _virClass virClass;
typedef virClass *virClassPtr;

typedef struct _virObject virObject;
typedef virObject *virObjectPtr;

33 34 35
typedef struct _virObjectLockable virObjectLockable;
typedef virObjectLockable *virObjectLockablePtr;

36 37 38
typedef struct _virObjectRWLockable virObjectRWLockable;
typedef virObjectRWLockable *virObjectRWLockablePtr;

39 40
typedef void (*virObjectDisposeCallback)(void *obj);

41 42 43
/* Most code should not play with the contents of this struct; however,
 * the struct itself is public so that it can be embedded as the first
 * field of a subclassed object.  */
44
struct _virObject {
45 46 47 48 49 50 51 52 53 54 55
    /* Ensure correct alignment of this and all subclasses, even on
     * platforms where 'long long' or function pointers have stricter
     * requirements than 'void *'.  */
    union {
        long long dummy_align1;
        void (*dummy_align2) (void);
        struct {
            unsigned int magic;
            int refs;
        } s;
    } u;
56 57 58
    virClassPtr klass;
};

59 60 61 62 63
struct _virObjectLockable {
    virObject parent;
    virMutex lock;
};

64 65 66 67
struct _virObjectRWLockable {
    virObject parent;
    virRWLock lock;
};
68

69
virClassPtr virClassForObject(void);
70
virClassPtr virClassForObjectLockable(void);
71
virClassPtr virClassForObjectRWLockable(void);
72

73 74 75
#ifndef VIR_PARENT_REQUIRED
# define VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(1)
#endif
76

E
Eric Blake 已提交
77 78 79 80 81
/* Assign the class description nameClass to represent struct @name
 * (which must have an object-based 'parent' member at offset 0), and
 * with parent class @prnt. nameDispose must exist as either a
 * function or as a macro defined to NULL.
 */
82
#define VIR_CLASS_NEW(name, prnt) \
83
    verify_expr(offsetof(name, parent) == 0, \
84 85 86
      (name##Class = virClassNew(prnt, #name, sizeof(name), \
                                 sizeof(((name *)NULL)->parent), \
                                 name##Dispose)))
87

88 89 90 91
virClassPtr
virClassNew(virClassPtr parent,
            const char *name,
            size_t objectSize,
92
            size_t parentSize,
93
            virObjectDisposeCallback dispose)
94
    VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(2);
95

96 97
const char *
virClassName(virClassPtr klass)
98 99
    ATTRIBUTE_NONNULL(1);

100 101 102
bool
virClassIsDerivedFrom(virClassPtr klass,
                      virClassPtr parent)
103 104
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

105 106
void *
virObjectNew(virClassPtr klass)
107 108
    ATTRIBUTE_NONNULL(1);

109 110 111
bool
virObjectUnref(void *obj);

112 113 114
void
virObjectAutoUnref(void *objptr);

115 116 117 118 119 120 121
/**
 * VIR_AUTOUNREF:
 * @type: type of an virObject subclass to be unref'd automatically
 *
 * Declares a variable of @type which will be automatically unref'd when
 * control goes out of the scope.
 */
122
#define VIR_AUTOUNREF(type) \
123 124
    __attribute__((cleanup(virObjectAutoUnref))) type

125 126 127 128 129 130
void *
virObjectRef(void *obj);

bool
virObjectIsClass(void *obj,
                 virClassPtr klass)
131 132
    ATTRIBUTE_NONNULL(2);

133 134 135 136 137 138
void
virObjectFreeCallback(void *opaque);

void
virObjectFreeHashData(void *opaque,
                      const void *name);
139

140 141
void *
virObjectLockableNew(virClassPtr klass)
142 143
    ATTRIBUTE_NONNULL(1);

144 145 146 147
void *
virObjectRWLockableNew(virClassPtr klass)
    ATTRIBUTE_NONNULL(1);

148 149
void
virObjectLock(void *lockableobj)
150
    ATTRIBUTE_NONNULL(1);
151

152
void
153
virObjectRWLockRead(void *lockableobj)
154 155
    ATTRIBUTE_NONNULL(1);

156 157 158 159
void
virObjectRWLockWrite(void *lockableobj)
    ATTRIBUTE_NONNULL(1);

160 161
void
virObjectUnlock(void *lockableobj)
162 163
    ATTRIBUTE_NONNULL(1);

164 165 166 167
void
virObjectRWUnlock(void *lockableobj)
    ATTRIBUTE_NONNULL(1);

168 169 170 171 172 173
void
virObjectListFree(void *list);

void
virObjectListFreeCount(void *list,
                       size_t count);