virobject.h 4.1 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 23
#ifndef LIBVIRT_VIROBJECT_H
# define LIBVIRT_VIROBJECT_H
24 25

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

typedef struct _virClass virClass;
typedef virClass *virClassPtr;

typedef struct _virObject virObject;
typedef virObject *virObjectPtr;

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

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

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

42 43 44
/* 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.  */
45
struct _virObject {
46 47 48 49 50 51 52 53 54 55 56
    /* 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;
57 58 59
    virClassPtr klass;
};

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

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

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

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

E
Eric Blake 已提交
78 79 80 81 82
/* 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.
 */
83
# define VIR_CLASS_NEW(name, prnt) \
84
    verify_expr(offsetof(name, parent) == 0, \
85 86 87
      (name##Class = virClassNew(prnt, #name, sizeof(name), \
                                 sizeof(((name *)NULL)->parent), \
                                 name##Dispose)))
88

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

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

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

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

110 111 112
bool
virObjectUnref(void *obj);

113 114 115
void
virObjectAutoUnref(void *objptr);

116 117 118 119 120 121
void *
virObjectRef(void *obj);

bool
virObjectIsClass(void *obj,
                 virClassPtr klass)
122 123
    ATTRIBUTE_NONNULL(2);

124 125 126 127 128 129
void
virObjectFreeCallback(void *opaque);

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

131 132
void *
virObjectLockableNew(virClassPtr klass)
133 134
    ATTRIBUTE_NONNULL(1);

135 136 137 138
void *
virObjectRWLockableNew(virClassPtr klass)
    ATTRIBUTE_NONNULL(1);

139 140
void
virObjectLock(void *lockableobj)
141
    ATTRIBUTE_NONNULL(1);
142

143
void
144
virObjectRWLockRead(void *lockableobj)
145 146
    ATTRIBUTE_NONNULL(1);

147 148 149 150
void
virObjectRWLockWrite(void *lockableobj)
    ATTRIBUTE_NONNULL(1);

151 152
void
virObjectUnlock(void *lockableobj)
153 154
    ATTRIBUTE_NONNULL(1);

155 156 157 158
void
virObjectRWUnlock(void *lockableobj)
    ATTRIBUTE_NONNULL(1);

159 160 161 162 163 164
void
virObjectListFree(void *list);

void
virObjectListFreeCount(void *list,
                       size_t count);
165

166
#endif /* LIBVIRT_VIROBJECT_H */