virobject.h 3.8 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 78

# define VIR_CLASS_NEW(name, prnt) \
79 80
    verify_expr(offsetof(name, parent) == 0, \
      (name##Class = virClassNew(prnt, #name, sizeof(name), name##Dispose)))
81

82 83 84 85 86
virClassPtr
virClassNew(virClassPtr parent,
            const char *name,
            size_t objectSize,
            virObjectDisposeCallback dispose)
87
    VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(2);
88

89 90
const char *
virClassName(virClassPtr klass)
91 92
    ATTRIBUTE_NONNULL(1);

93 94 95
bool
virClassIsDerivedFrom(virClassPtr klass,
                      virClassPtr parent)
96 97
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

98 99
void *
virObjectNew(virClassPtr klass)
100 101
    ATTRIBUTE_NONNULL(1);

102 103 104
bool
virObjectUnref(void *obj);

105 106 107
void
virObjectAutoUnref(void *objptr);

108 109 110 111 112 113
void *
virObjectRef(void *obj);

bool
virObjectIsClass(void *obj,
                 virClassPtr klass)
114 115
    ATTRIBUTE_NONNULL(2);

116 117 118 119 120 121
void
virObjectFreeCallback(void *opaque);

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

123 124
void *
virObjectLockableNew(virClassPtr klass)
125 126
    ATTRIBUTE_NONNULL(1);

127 128 129 130
void *
virObjectRWLockableNew(virClassPtr klass)
    ATTRIBUTE_NONNULL(1);

131 132
void
virObjectLock(void *lockableobj)
133
    ATTRIBUTE_NONNULL(1);
134

135
void
136
virObjectRWLockRead(void *lockableobj)
137 138
    ATTRIBUTE_NONNULL(1);

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

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

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

151 152 153 154 155 156
void
virObjectListFree(void *list);

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

158
#endif /* LIBVIRT_VIROBJECT_H */