virobject.c 13.9 KB
Newer Older
1 2 3
/*
 * virobject.c: 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
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

24
#define VIR_PARENT_REQUIRED /* empty, to allow virObject to have no parent */
25
#include "virobject.h"
26
#include "virthread.h"
27
#include "viralloc.h"
28
#include "virerror.h"
29
#include "virlog.h"
30
#include "virprobe.h"
31
#include "virstring.h"
32 33 34

#define VIR_FROM_THIS VIR_FROM_NONE

35 36
VIR_LOG_INIT("util.object");

37 38 39
static unsigned int magicCounter = 0xCAFE0000;

struct _virClass {
40 41
    virClassPtr parent;

42
    unsigned int magic;
43
    char *name;
44 45 46 47 48
    size_t objectSize;

    virObjectDisposeCallback dispose;
};

49 50
#define VIR_OBJECT_NOTVALID(obj) (!obj || ((obj->u.s.magic & 0xFFFF0000) != 0xCAFE0000))

51 52 53 54 55 56 57 58 59 60 61
#define VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, objclass) \
    do { \
        virObjectPtr obj = anyobj; \
        if (VIR_OBJECT_NOTVALID(obj)) { \
            if (!obj) \
                VIR_WARN("Object cannot be NULL"); \
            else \
                VIR_WARN("Object %p has a bad magic number %X", \
                         obj, obj->u.s.magic); \
        } else { \
            VIR_WARN("Object %p (%s) is not a %s instance", \
62
                     anyobj, obj->klass->name, #objclass); \
63
        } \
64 65 66
    } while (0)


67
static virClassPtr virObjectClass;
68
static virClassPtr virObjectLockableClass;
69
static virClassPtr virObjectRWLockableClass;
70 71

static void virObjectLockableDispose(void *anyobj);
72
static void virObjectRWLockableDispose(void *anyobj);
73

74 75
static int
virObjectOnceInit(void)
76 77 78 79
{
    if (!(virObjectClass = virClassNew(NULL,
                                       "virObject",
                                       sizeof(virObject),
80
                                       0,
81 82 83
                                       NULL)))
        return -1;

84
    if (!VIR_CLASS_NEW(virObjectLockable, virObjectClass))
85 86
        return -1;

87
    if (!VIR_CLASS_NEW(virObjectRWLockable, virObjectClass))
88 89
        return -1;

90 91 92 93 94 95 96 97 98 99 100
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virObject);


/**
 * virClassForObject:
 *
 * Returns the class instance for the base virObject type
 */
101 102
virClassPtr
virClassForObject(void)
103
{
104
    if (virObjectInitialize() < 0)
105 106 107 108 109
        return NULL;

    return virObjectClass;
}

110

111 112 113 114 115
/**
 * virClassForObjectLockable:
 *
 * Returns the class instance for the virObjectLockable type
 */
116 117
virClassPtr
virClassForObjectLockable(void)
118
{
119
    if (virObjectInitialize() < 0)
120 121 122 123 124 125
        return NULL;

    return virObjectLockableClass;
}


126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * virClassForObjectRWLockable:
 *
 * Returns the class instance for the virObjectRWLockable type
 */
virClassPtr
virClassForObjectRWLockable(void)
{
    if (virObjectInitialize() < 0)
        return NULL;

    return virObjectRWLockableClass;
}


141 142
/**
 * virClassNew:
143
 * @parent: the parent class
144 145 146 147 148 149
 * @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
E
Eric Blake 已提交
150 151 152 153 154
 * is expected to have a 'virObject parent;' field as (or
 * contained in) its first member. When the last reference
 * on the object is released, the @dispose callback will be
 * invoked to free memory of the local object fields, as
 * well as the dispose callbacks of the parent classes.
155 156 157
 *
 * Returns a new class instance
 */
158 159 160 161
virClassPtr
virClassNew(virClassPtr parent,
            const char *name,
            size_t objectSize,
162
            size_t parentSize,
163
            virObjectDisposeCallback dispose)
164 165 166
{
    virClassPtr klass;

167 168 169 170
    if (parent == NULL &&
        STRNEQ(name, "virObject")) {
        virReportInvalidNonNullArg(parent);
        return NULL;
171 172 173
    } else if (objectSize <= parentSize ||
               parentSize != (parent ? parent->objectSize : 0)) {
        sa_assert(parent);
174
        virReportInvalidArg(objectSize,
175
                            _("object size %zu of %s is not larger than parent class %zu"),
176 177 178 179
                            objectSize, name, parent->objectSize);
        return NULL;
    }

180
    if (VIR_ALLOC(klass) < 0)
181
        goto error;
182

183
    klass->parent = parent;
184
    klass->magic = g_atomic_int_add(&magicCounter, 1);
185 186 187 188 189
    if (klass->magic > 0xCAFEFFFF) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("too many object classes defined"));
        goto error;
    }
190
    klass->name = g_strdup(name);
191 192 193 194 195
    klass->objectSize = objectSize;
    klass->dispose = dispose;

    return klass;

196
 error:
197 198 199 200 201
    VIR_FREE(klass);
    return NULL;
}


202 203 204 205 206 207 208 209 210
/**
 * virClassIsDerivedFrom:
 * @klass: the klass to check
 * @parent: the possible parent class
 *
 * Determine if @klass is derived from @parent
 *
 * Return true if @klass is derived from @parent, false otherwise
 */
211 212 213
bool
virClassIsDerivedFrom(virClassPtr klass,
                      virClassPtr parent)
214 215 216 217 218 219 220 221 222 223
{
    while (klass) {
        if (klass->magic == parent->magic)
            return true;
        klass = klass->parent;
    }
    return false;
}


224 225 226 227 228 229 230 231 232 233 234 235
/**
 * 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
 */
236 237
void *
virObjectNew(virClassPtr klass)
238 239 240
{
    virObjectPtr obj = NULL;

241 242
    if (VIR_ALLOC_VAR(obj,
                      char,
243
                      klass->objectSize - sizeof(virObject)) < 0)
244 245
        return NULL;

246
    obj->u.s.magic = klass->magic;
247
    obj->klass = klass;
248
    g_atomic_int_set(&obj->u.s.refs, 1);
249 250 251 252 253 254 255

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

    return obj;
}


256 257
void *
virObjectLockableNew(virClassPtr klass)
258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
    virObjectLockablePtr obj;

    if (!virClassIsDerivedFrom(klass, virClassForObjectLockable())) {
        virReportInvalidArg(klass,
                            _("Class %s must derive from virObjectLockable"),
                            virClassName(klass));
        return NULL;
    }

    if (!(obj = virObjectNew(klass)))
        return NULL;

    if (virMutexInit(&obj->lock) < 0) {
J
Jiri Denemark 已提交
272 273
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize mutex"));
274 275 276 277 278 279 280 281
        virObjectUnref(obj);
        return NULL;
    }

    return obj;
}


282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
void *
virObjectRWLockableNew(virClassPtr klass)
{
    virObjectRWLockablePtr obj;

    if (!virClassIsDerivedFrom(klass, virClassForObjectRWLockable())) {
        virReportInvalidArg(klass,
                            _("Class %s must derive from virObjectRWLockable"),
                            virClassName(klass));
        return NULL;
    }

    if (!(obj = virObjectNew(klass)))
        return NULL;

297
    if (virRWLockInit(&obj->lock) < 0) {
298 299 300 301 302 303 304 305 306 307
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize RW lock"));
        virObjectUnref(obj);
        return NULL;
    }

    return obj;
}


308 309
static void
virObjectLockableDispose(void *anyobj)
310 311 312 313 314 315
{
    virObjectLockablePtr obj = anyobj;

    virMutexDestroy(&obj->lock);
}

316

317 318 319 320 321 322 323 324 325
static void
virObjectRWLockableDispose(void *anyobj)
{
    virObjectRWLockablePtr obj = anyobj;

    virRWLockDestroy(&obj->lock);
}


326 327 328 329 330
/**
 * virObjectUnref:
 * @anyobj: any instance of virObjectPtr
 *
 * Decrement the reference count on @anyobj and if
E
Eric Blake 已提交
331 332 333
 * it hits zero, runs the "dispose" callbacks associated
 * with the object class and its parents before freeing
 * @anyobj.
334
 */
335
void
336
virObjectUnref(void *anyobj)
337 338 339
{
    virObjectPtr obj = anyobj;

340
    if (VIR_OBJECT_NOTVALID(obj))
341
        return;
342

343
    bool lastRef = !!g_atomic_int_dec_and_test(&obj->u.s.refs);
344 345 346
    PROBE(OBJECT_UNREF, "obj=%p", obj);
    if (lastRef) {
        PROBE(OBJECT_DISPOSE, "obj=%p", obj);
347 348 349 350 351 352 353
        virClassPtr klass = obj->klass;
        while (klass) {
            if (klass->dispose)
                klass->dispose(obj);
            klass = klass->parent;
        }

354 355
        /* Clear & poison object */
        memset(obj, 0, obj->klass->objectSize);
356
        obj->u.s.magic = 0xDEADBEEF;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        obj->klass = (void*)0xDEADBEEF;
        VIR_FREE(obj);
    }
}


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

377
    if (VIR_OBJECT_NOTVALID(obj))
378
        return NULL;
379
    g_atomic_int_add(&obj->u.s.refs, 1);
380 381 382 383 384
    PROBE(OBJECT_REF, "obj=%p", obj);
    return anyobj;
}


385 386 387 388 389 390
static virObjectLockablePtr
virObjectGetLockableObj(void *anyobj)
{
    if (virObjectIsClass(anyobj, virObjectLockableClass))
        return anyobj;

391
    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockable);
392 393 394 395
    return NULL;
}


396 397 398 399 400 401
static virObjectRWLockablePtr
virObjectGetRWLockableObj(void *anyobj)
{
    if (virObjectIsClass(anyobj, virObjectRWLockableClass))
        return anyobj;

402
    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectRWLockable);
403 404 405 406
    return NULL;
}


407 408
/**
 * virObjectLock:
409
 * @anyobj: any instance of virObjectLockable
410 411
 *
 * Acquire a lock on @anyobj. The lock must be released by
412
 * virObjectUnlock.
413 414 415 416 417 418 419 420
 *
 * The caller is expected to have acquired a reference
 * on the object before locking it (eg virObjectRef).
 * The object must be unlocked before releasing this
 * reference.
 */
void
virObjectLock(void *anyobj)
421
{
422 423 424 425 426 427
    virObjectLockablePtr obj = virObjectGetLockableObj(anyobj);

    if (!obj)
        return;

    virMutexLock(&obj->lock);
428 429 430
}


431
/**
432
 * virObjectRWLockRead:
433
 * @anyobj: any instance of virObjectRWLockable
434
 *
435
 * Acquire a read lock on @anyobj. The lock must be
436
 * released by virObjectRWUnlock.
437 438 439 440 441
 *
 * The caller is expected to have acquired a reference
 * on the object before locking it (eg virObjectRef).
 * The object must be unlocked before releasing this
 * reference.
442 443 444 445 446
 *
 * NB: It's possible to return without the lock if
 *     @anyobj was invalid - this has been considered
 *     a programming error rather than something that
 *     should be checked.
447
 */
448
void
449
virObjectRWLockRead(void *anyobj)
450
{
451 452 453 454 455 456
    virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);

    if (!obj)
        return;

    virRWLockRead(&obj->lock);
457 458 459
}


460 461 462 463 464
/**
 * virObjectRWLockWrite:
 * @anyobj: any instance of virObjectRWLockable
 *
 * Acquire a write lock on @anyobj. The lock must be
465
 * released by virObjectRWUnlock.
466 467 468 469 470 471 472 473 474 475 476 477 478 479
 *
 * The caller is expected to have acquired a reference
 * on the object before locking it (eg virObjectRef).
 * The object must be unlocked before releasing this
 * reference.
 *
 * NB: It's possible to return without the lock if
 *     @anyobj was invalid - this has been considered
 *     a programming error rather than something that
 *     should be checked.
 */
void
virObjectRWLockWrite(void *anyobj)
{
480 481 482 483 484 485
    virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);

    if (!obj)
        return;

    virRWLockWrite(&obj->lock);
486 487 488
}


489 490
/**
 * virObjectUnlock:
491
 * @anyobj: any instance of virObjectLockable
492
 *
493
 * Release a lock on @anyobj. The lock must have been acquired by
494
 * virObjectLock.
495
 */
496 497
void
virObjectUnlock(void *anyobj)
498
{
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    virObjectLockablePtr obj = virObjectGetLockableObj(anyobj);

    if (!obj)
        return;

    virMutexUnlock(&obj->lock);
}


/**
 * virObjectRWUnlock:
 * @anyobj: any instance of virObjectRWLockable
 *
 * Release a lock on @anyobj. The lock must have been acquired by
 * virObjectRWLockRead or virObjectRWLockWrite.
 */
void
virObjectRWUnlock(void *anyobj)
{
    virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);

    if (!obj)
        return;

    virRWLockUnlock(&obj->lock);
524 525 526
}


527 528 529 530 531 532 533 534 535 536
/**
 * 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
 */
537 538 539
bool
virObjectIsClass(void *anyobj,
                 virClassPtr klass)
540 541
{
    virObjectPtr obj = anyobj;
542
    if (VIR_OBJECT_NOTVALID(obj))
543 544 545
        return false;

    return virClassIsDerivedFrom(obj->klass, klass);
546 547 548 549 550 551 552 553 554
}


/**
 * virClassName:
 * @klass: the object class
 *
 * Returns the name of @klass
 */
555 556
const char *
virClassName(virClassPtr klass)
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
{
    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);
}
E
Eric Blake 已提交
574 575 576 577 578 579 580 581 582 583 584


/**
 * virObjectFreeHashData:
 * @opaque: a pointer to a virObject instance
 * @name: ignored, name of the hash key being deleted
 *
 * Provides identical functionality to virObjectUnref,
 * but with the signature matching the virHashDataFree
 * typedef.
 */
585
void
586
virObjectFreeHashData(void *opaque)
E
Eric Blake 已提交
587 588 589
{
    virObjectUnref(opaque);
}
590 591 592 593 594 595 596 597


/**
 * virObjectListFree:
 * @list: A pointer to a NULL-terminated list of object pointers to free
 *
 * Unrefs all members of @list and frees the list itself.
 */
598 599
void
virObjectListFree(void *list)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
{
    void **next;

    if (!list)
        return;

    for (next = (void **) list; *next; next++)
        virObjectUnref(*next);

    VIR_FREE(list);
}


/**
 * virObjectListFreeCount:
 * @list: A pointer to a list of object pointers to freea
 * @count: Number of elements in the list.
 *
 * Unrefs all members of @list and frees the list itself.
 */
620 621 622
void
virObjectListFreeCount(void *list,
                       size_t count)
623 624 625 626 627 628 629 630 631 632 633
{
    size_t i;

    if (!list)
        return;

    for (i = 0; i < count; i++)
        virObjectUnref(((void **)list)[i]);

    VIR_FREE(list);
}