virobject.h 3.4 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 22 23 24 25
 * <http://www.gnu.org/licenses/>.
 *
 */

#ifndef __VIR_OBJECT_H__
# define __VIR_OBJECT_H__

# 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 79 80 81
virClassPtr
virClassNew(virClassPtr parent,
            const char *name,
            size_t objectSize,
            virObjectDisposeCallback dispose)
82
    VIR_PARENT_REQUIRED ATTRIBUTE_NONNULL(2);
83

84 85
const char *
virClassName(virClassPtr klass)
86 87
    ATTRIBUTE_NONNULL(1);

88 89 90
bool
virClassIsDerivedFrom(virClassPtr klass,
                      virClassPtr parent)
91 92
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

93 94
void *
virObjectNew(virClassPtr klass)
95 96
    ATTRIBUTE_NONNULL(1);

97 98 99 100 101 102 103 104 105
bool
virObjectUnref(void *obj);

void *
virObjectRef(void *obj);

bool
virObjectIsClass(void *obj,
                 virClassPtr klass)
106 107
    ATTRIBUTE_NONNULL(2);

108 109 110 111 112 113
void
virObjectFreeCallback(void *opaque);

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

115 116
void *
virObjectLockableNew(virClassPtr klass)
117 118
    ATTRIBUTE_NONNULL(1);

119 120 121 122
void *
virObjectRWLockableNew(virClassPtr klass)
    ATTRIBUTE_NONNULL(1);

123 124
void
virObjectLock(void *lockableobj)
125
    ATTRIBUTE_NONNULL(1);
126

127 128 129 130
void
virObjectLockRead(void *lockableobj)
    ATTRIBUTE_NONNULL(1);

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

135 136 137 138 139 140
void
virObjectListFree(void *list);

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

142
#endif /* __VIR_OBJECT_H */