aboutsummaryrefslogtreecommitdiff
blob: 7bb3d9a0b07814afd22db0a34b3776c75e7736ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * virobject.c: libvirt reference counted object
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * 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
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "virobject.h"
#include "threads.h"
#include "memory.h"
#include "viratomic.h"
#include "virterror_internal.h"
#include "logging.h"

#define VIR_FROM_THIS VIR_FROM_NONE

static unsigned int magicCounter = 0xCAFE0000;

struct _virClass {
    unsigned int magic;
    const char *name;
    size_t objectSize;

    virObjectDisposeCallback dispose;
};


/**
 * virClassNew:
 * @name: the class name
 * @objectSize: total size of the object struct
 * @dispose: callback to run to free object fields
 *
 * Register a new object class with @name. The @objectSize
 * should give the total size of the object struct, which
 * is expected to have a 'virObject object;' field as its
 * first member. When the last reference on the object is
 * released, the @dispose callback will be invoked to free
 * memory of the object fields
 *
 * Returns a new class instance
 */
virClassPtr virClassNew(const char *name,
                        size_t objectSize,
                        virObjectDisposeCallback dispose)
{
    virClassPtr klass;

    if (VIR_ALLOC(klass) < 0)
        goto no_memory;

    if (!(klass->name = strdup(name)))
        goto no_memory;
    klass->magic = virAtomicIntInc(&magicCounter);
    klass->objectSize = objectSize;
    klass->dispose = dispose;

    return klass;

no_memory:
    VIR_FREE(klass);
    virReportOOMError();
    return NULL;
}


/**
 * virObjectNew:
 * @klass: the klass of object to create
 *
 * Allocates a new object of type @klass. The returned
 * object will be an instance of "virObjectPtr", which
 * can be cast to the struct associated with @klass.
 *
 * The initial reference count of the object will be 1.
 *
 * Returns the new object
 */
void *virObjectNew(virClassPtr klass)
{
    virObjectPtr obj = NULL;
    char *somebytes;

    if (VIR_ALLOC_N(somebytes, klass->objectSize) < 0) {
        virReportOOMError();
        return NULL;
    }
    obj = (virObjectPtr)somebytes;

    obj->magic = klass->magic;
    obj->klass = klass;
    virAtomicIntSet(&obj->refs, 1);

    PROBE(OBJECT_NEW, "obj=%p classname=%s", obj, obj->klass->name);

    return obj;
}


/**
 * virObjectUnref:
 * @anyobj: any instance of virObjectPtr
 *
 * Decrement the reference count on @anyobj and if
 * it hits zero, runs the "dispose" callback associated
 * with the object class and frees @anyobj.
 *
 * Returns true if the remaining reference count is
 * non-zero, false if the object was disposed of
 */
bool virObjectUnref(void *anyobj)
{
    virObjectPtr obj = anyobj;

    if (!obj)
        return false;

    bool lastRef = virAtomicIntDecAndTest(&obj->refs);
    PROBE(OBJECT_UNREF, "obj=%p", obj);
    if (lastRef) {
        PROBE(OBJECT_DISPOSE, "obj=%p", obj);
        if (obj->klass->dispose)
            obj->klass->dispose(obj);

        /* Clear & poison object */
        memset(obj, 0, obj->klass->objectSize);
        obj->magic = 0xDEADBEEF;
        obj->klass = (void*)0xDEADBEEF;
        VIR_FREE(obj);
    }

    return !lastRef;
}


/**
 * virObjectRef:
 * @anyobj: any instance of virObjectPtr
 *
 * Increment the reference count on @anyobj and return
 * the same pointer
 *
 * Returns @anyobj
 */
void *virObjectRef(void *anyobj)
{
    virObjectPtr obj = anyobj;

    if (!obj)
        return NULL;
    virAtomicIntInc(&obj->refs);
    PROBE(OBJECT_REF, "obj=%p", obj);
    return anyobj;
}


/**
 * virObjectIsClass:
 * @anyobj: any instance of virObjectPtr
 * @klass: the class to check
 *
 * Checks whether @anyobj is an instance of
 * @klass
 *
 * Returns true if @anyobj is an instance of @klass
 */
bool virObjectIsClass(void *anyobj,
                      virClassPtr klass)
{
    virObjectPtr obj = anyobj;
    return obj != NULL && (obj->magic == klass->magic) && (obj->klass == klass);
}


/**
 * virClassName:
 * @klass: the object class
 *
 * Returns the name of @klass
 */
const char *virClassName(virClassPtr klass)
{
    return klass->name;
}


/**
 * virObjectFreeCallback:
 * @opaque: a pointer to a virObject instance
 *
 * Provides identical functionality to virObjectUnref,
 * but with the signature matching the virFreeCallback
 * typedef.
 */
void virObjectFreeCallback(void *opaque)
{
    virObjectUnref(opaque);
}