virobject.c 14.1 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 "viratomic.h"
29
#include "virerror.h"
30
#include "virlog.h"
31
#include "virprobe.h"
32
#include "virstring.h"
33 34 35

#define VIR_FROM_THIS VIR_FROM_NONE

36 37
VIR_LOG_INIT("util.object");

38 39 40
static unsigned int magicCounter = 0xCAFE0000;

struct _virClass {
41 42
    virClassPtr parent;

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

    virObjectDisposeCallback dispose;
};

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

52 53 54 55 56 57 58 59 60 61 62
#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", \
63
                     anyobj, obj->klass->name, #objclass); \
64
        } \
65 66 67
    } while (0)


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

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

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

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

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

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

VIR_ONCE_GLOBAL_INIT(virObject);


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

    return virObjectClass;
}

111

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

    return virObjectLockableClass;
}


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

    return virObjectRWLockableClass;
}


142 143
/**
 * virClassNew:
144
 * @parent: the parent class
145 146 147 148 149 150
 * @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 已提交
151 152 153 154 155
 * 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.
156 157 158
 *
 * Returns a new class instance
 */
159 160 161 162
virClassPtr
virClassNew(virClassPtr parent,
            const char *name,
            size_t objectSize,
163
            size_t parentSize,
164
            virObjectDisposeCallback dispose)
165 166 167
{
    virClassPtr klass;

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

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

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

    return klass;

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


203 204 205 206 207 208 209 210 211
/**
 * 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
 */
212 213 214
bool
virClassIsDerivedFrom(virClassPtr klass,
                      virClassPtr parent)
215 216 217 218 219 220 221 222 223 224
{
    while (klass) {
        if (klass->magic == parent->magic)
            return true;
        klass = klass->parent;
    }
    return false;
}


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

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

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

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

    return obj;
}


257 258
void *
virObjectLockableNew(virClassPtr klass)
259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
    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 已提交
273 274
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize mutex"));
275 276 277 278 279 280 281 282
        virObjectUnref(obj);
        return NULL;
    }

    return obj;
}


283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
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;

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

    return obj;
}


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

    virMutexDestroy(&obj->lock);
}

317

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

    virRWLockDestroy(&obj->lock);
}


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

344
    if (VIR_OBJECT_NOTVALID(obj))
345 346
        return false;

347
    bool lastRef = virAtomicIntDecAndTest(&obj->u.s.refs);
348 349 350
    PROBE(OBJECT_UNREF, "obj=%p", obj);
    if (lastRef) {
        PROBE(OBJECT_DISPOSE, "obj=%p", obj);
351 352 353 354 355 356 357
        virClassPtr klass = obj->klass;
        while (klass) {
            if (klass->dispose)
                klass->dispose(obj);
            klass = klass->parent;
        }

358 359
        /* Clear & poison object */
        memset(obj, 0, obj->klass->objectSize);
360
        obj->u.s.magic = 0xDEADBEEF;
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        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
 */
378 379
void *
virObjectRef(void *anyobj)
380 381 382
{
    virObjectPtr obj = anyobj;

383
    if (VIR_OBJECT_NOTVALID(obj))
384
        return NULL;
385
    virAtomicIntInc(&obj->u.s.refs);
386 387 388 389 390
    PROBE(OBJECT_REF, "obj=%p", obj);
    return anyobj;
}


391 392 393 394 395 396
static virObjectLockablePtr
virObjectGetLockableObj(void *anyobj)
{
    if (virObjectIsClass(anyobj, virObjectLockableClass))
        return anyobj;

397
    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectLockable);
398 399 400 401
    return NULL;
}


402 403 404 405 406 407
static virObjectRWLockablePtr
virObjectGetRWLockableObj(void *anyobj)
{
    if (virObjectIsClass(anyobj, virObjectRWLockableClass))
        return anyobj;

408
    VIR_OBJECT_USAGE_PRINT_WARNING(anyobj, virObjectRWLockable);
409 410 411 412
    return NULL;
}


413 414
/**
 * virObjectLock:
415
 * @anyobj: any instance of virObjectLockable
416 417
 *
 * Acquire a lock on @anyobj. The lock must be released by
418
 * virObjectUnlock.
419 420 421 422 423 424 425 426
 *
 * 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)
427
{
428 429 430 431 432 433
    virObjectLockablePtr obj = virObjectGetLockableObj(anyobj);

    if (!obj)
        return;

    virMutexLock(&obj->lock);
434 435 436
}


437
/**
438
 * virObjectRWLockRead:
439
 * @anyobj: any instance of virObjectRWLockable
440
 *
441
 * Acquire a read lock on @anyobj. The lock must be
442
 * released by virObjectRWUnlock.
443 444 445 446 447
 *
 * 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.
448 449 450 451 452
 *
 * 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.
453
 */
454
void
455
virObjectRWLockRead(void *anyobj)
456
{
457 458 459 460 461 462
    virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);

    if (!obj)
        return;

    virRWLockRead(&obj->lock);
463 464 465
}


466 467 468 469 470
/**
 * virObjectRWLockWrite:
 * @anyobj: any instance of virObjectRWLockable
 *
 * Acquire a write lock on @anyobj. The lock must be
471
 * released by virObjectRWUnlock.
472 473 474 475 476 477 478 479 480 481 482 483 484 485
 *
 * 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)
{
486 487 488 489 490 491
    virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj);

    if (!obj)
        return;

    virRWLockWrite(&obj->lock);
492 493 494
}


495 496
/**
 * virObjectUnlock:
497
 * @anyobj: any instance of virObjectLockable
498
 *
499
 * Release a lock on @anyobj. The lock must have been acquired by
500
 * virObjectLock.
501
 */
502 503
void
virObjectUnlock(void *anyobj)
504
{
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    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);
530 531 532
}


533 534 535 536 537 538 539 540 541 542
/**
 * 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
 */
543 544 545
bool
virObjectIsClass(void *anyobj,
                 virClassPtr klass)
546 547
{
    virObjectPtr obj = anyobj;
548
    if (VIR_OBJECT_NOTVALID(obj))
549 550 551
        return false;

    return virClassIsDerivedFrom(obj->klass, klass);
552 553 554 555 556 557 558 559 560
}


/**
 * virClassName:
 * @klass: the object class
 *
 * Returns the name of @klass
 */
561 562
const char *
virClassName(virClassPtr klass)
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
{
    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 已提交
580 581 582 583 584 585 586 587 588 589 590


/**
 * 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.
 */
591
void
592
virObjectFreeHashData(void *opaque)
E
Eric Blake 已提交
593 594 595
{
    virObjectUnref(opaque);
}
596 597 598 599 600 601 602 603


/**
 * virObjectListFree:
 * @list: A pointer to a NULL-terminated list of object pointers to free
 *
 * Unrefs all members of @list and frees the list itself.
 */
604 605
void
virObjectListFree(void *list)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
{
    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.
 */
626 627 628
void
virObjectListFreeCount(void *list,
                       size_t count)
629 630 631 632 633 634 635 636 637 638 639
{
    size_t i;

    if (!list)
        return;

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

    VIR_FREE(list);
}