datatypes.c 22.3 KB
Newer Older
1 2 3
/*
 * datatypes.h: management of structs for public data types
 *
4
 * Copyright (C) 2006-2012 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
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22
 *
 */

#include <config.h>
D
Daniel P. Berrange 已提交
23
#include <unistd.h>
24 25

#include "datatypes.h"
26
#include "virerror.h"
27
#include "virlog.h"
28
#include "viralloc.h"
29
#include "viruuid.h"
30
#include "virutil.h"
31

32 33
#define VIR_FROM_THIS VIR_FROM_NONE

34
#define virLibConnError(code, ...)                                \
35
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,           \
36
                         __FUNCTION__, __LINE__, __VA_ARGS__)
37

38

39
virClassPtr virConnectClass;
40
virClassPtr virConnectCloseCallbackDataClass;
41 42 43 44 45 46 47 48 49 50 51 52
virClassPtr virDomainClass;
virClassPtr virDomainSnapshotClass;
virClassPtr virInterfaceClass;
virClassPtr virNetworkClass;
virClassPtr virNodeDeviceClass;
virClassPtr virNWFilterClass;
virClassPtr virSecretClass;
virClassPtr virStreamClass;
virClassPtr virStorageVolClass;
virClassPtr virStoragePoolClass;

static void virConnectDispose(void *obj);
53
static void virConnectCloseCallbackDataDispose(void *obj);
54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void virDomainDispose(void *obj);
static void virDomainSnapshotDispose(void *obj);
static void virInterfaceDispose(void *obj);
static void virNetworkDispose(void *obj);
static void virNodeDeviceDispose(void *obj);
static void virNWFilterDispose(void *obj);
static void virSecretDispose(void *obj);
static void virStreamDispose(void *obj);
static void virStorageVolDispose(void *obj);
static void virStoragePoolDispose(void *obj);

static int
virDataTypesOnceInit(void)
{
68 69
#define DECLARE_CLASS_COMMON(basename, parent)                   \
    if (!(basename ## Class = virClassNew(parent,                \
70
                                          #basename,             \
71 72 73
                                          sizeof(basename),      \
                                          basename ## Dispose))) \
        return -1;
74 75 76 77
#define DECLARE_CLASS(basename)                                  \
    DECLARE_CLASS_COMMON(basename, virClassForObject())
#define DECLARE_CLASS_LOCKABLE(basename)                         \
    DECLARE_CLASS_COMMON(basename, virClassForObjectLockable())
78 79

    DECLARE_CLASS(virConnect);
80
    DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData);
81 82 83 84 85 86 87 88 89 90 91
    DECLARE_CLASS(virDomain);
    DECLARE_CLASS(virDomainSnapshot);
    DECLARE_CLASS(virInterface);
    DECLARE_CLASS(virNetwork);
    DECLARE_CLASS(virNodeDevice);
    DECLARE_CLASS(virNWFilter);
    DECLARE_CLASS(virSecret);
    DECLARE_CLASS(virStream);
    DECLARE_CLASS(virStorageVol);
    DECLARE_CLASS(virStoragePool);

92 93
#undef DECLARE_CLASS_COMMON
#undef DECLARE_CLASS_LOCKABLE
94 95 96 97 98 99
#undef DECLARE_CLASS

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virDataTypes)
C
Chris Lalancette 已提交
100

101 102 103 104 105 106 107 108
/**
 * virGetConnect:
 *
 * Allocates a new hypervisor connection structure
 *
 * Returns a new pointer or NULL in case of error.
 */
virConnectPtr
109 110
virGetConnect(void)
{
111 112
    virConnectPtr ret;

113 114 115 116 117 118
    if (virDataTypesInitialize() < 0)
        return NULL;

    if (!(ret = virObjectNew(virConnectClass)))
        return NULL;

119 120 121 122 123
    if (!(ret->closeCallback = virObjectNew(virConnectCloseCallbackDataClass)))
        goto error;

    if (virMutexInit(&ret->lock) < 0)
        goto error;
124

125
    return ret;
126 127 128 129

error:
    virObjectUnref(ret);
    return NULL;
130 131 132
}

/**
133
 * virConnectDispose:
134 135 136 137 138 139 140 141
 * @conn: the hypervisor connection to release
 *
 * Unconditionally release all memory associated with a connection.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The connection obj must not
 * be used once this method returns.
 */
static void
142 143 144
virConnectDispose(void *obj)
{
    virConnectPtr conn = obj;
145 146

    if (conn->networkDriver)
147
        conn->networkDriver->connectClose(conn);
148
    if (conn->interfaceDriver)
149
        conn->interfaceDriver->connectClose(conn);
150
    if (conn->storageDriver)
151
        conn->storageDriver->connectClose(conn);
152 153
    if (conn->nodeDeviceDriver)
        conn->nodeDeviceDriver->connectClose(conn);
154
    if (conn->secretDriver)
155
        conn->secretDriver->connectClose(conn);
156
    if (conn->nwfilterDriver)
157
        conn->nwfilterDriver->connectClose(conn);
158
    if (conn->driver)
159
        conn->driver->connectClose(conn);
160 161 162

    virMutexLock(&conn->lock);

163 164
    virResetError(&conn->err);

165
    virURIFree(conn->uri);
166

167 168 169 170 171 172
    virObjectLock(conn->closeCallback);
    conn->closeCallback->callback = NULL;
    virObjectUnlock(conn->closeCallback);

    virObjectUnref(conn->closeCallback);

173 174
    virMutexUnlock(&conn->lock);
    virMutexDestroy(&conn->lock);
175 176 177
}


178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/**
 * virConnectCloseCallbackDataDispose:
 * @obj: the close callback data to release
 *
 * Release resources bound to the connection close callback.
 */
static void
virConnectCloseCallbackDataDispose(void *obj)
{
    virConnectCloseCallbackDataPtr cb = obj;

    virObjectLock(cb);

    if (cb->freeCallback)
        cb->freeCallback(cb->opaque);

    virObjectUnlock(cb);
}


198 199 200 201 202 203 204 205 206
/**
 * virGetDomain:
 * @conn: the hypervisor connection
 * @name: pointer to the domain name
 * @uuid: pointer to the uuid
 *
 * Lookup if the domain is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
207
 * virObjectUnref() is needed to not leak data.
208 209 210 211
 *
 * Returns a pointer to the domain, or NULL in case of failure
 */
virDomainPtr
212 213
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
214 215
    virDomainPtr ret = NULL;

216 217 218
    if (virDataTypesInitialize() < 0)
        return NULL;

219
    if (!VIR_IS_CONNECT(conn)) {
220
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
221
        return NULL;
222
    }
223 224 225
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

226 227
    if (!(ret = virObjectNew(virDomainClass)))
        return NULL;
228

229 230 231 232
    if (!(ret->name = strdup(name)))
        goto no_memory;

    ret->conn = virObjectRef(conn);
233 234 235
    ret->id = -1;
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

236
    return ret;
237

238 239 240
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
241
    return NULL;
242 243 244
}

/**
245
 * virDomainDispose:
246 247 248 249 250 251 252 253 254 255 256
 * @domain: the domain to release
 *
 * Unconditionally release all memory associated with a domain.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The domain obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
257 258 259
virDomainDispose(void *obj)
{
    virDomainPtr domain = obj;
260
    char uuidstr[VIR_UUID_STRING_BUFLEN];
261

262
    virUUIDFormat(domain->uuid, uuidstr);
263
    VIR_DEBUG("release domain %p %s %s", domain, domain->name, uuidstr);
264

265
    VIR_FREE(domain->name);
266
    virObjectUnref(domain->conn);
267 268 269 270 271 272 273 274 275 276 277 278
}


/**
 * virGetNetwork:
 * @conn: the hypervisor connection
 * @name: pointer to the network name
 * @uuid: pointer to the uuid
 *
 * Lookup if the network is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
279
 * virObjectUnref() is needed to not leak data.
280 281 282 283
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNetworkPtr
284 285
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
286 287
    virNetworkPtr ret = NULL;

288 289 290
    if (virDataTypesInitialize() < 0)
        return NULL;

291
    if (!VIR_IS_CONNECT(conn)) {
292
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
293
        return NULL;
294
    }
295 296 297
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

298 299
    if (!(ret = virObjectNew(virNetworkClass)))
        return NULL;
300

301 302 303 304
    if (!(ret->name = strdup(name)))
        goto no_memory;

    ret->conn = virObjectRef(conn);
305 306
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

307
    return ret;
308

309 310 311
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
312
    return NULL;
313 314 315
}

/**
316
 * virNetworkDispose:
317 318 319 320 321 322 323 324 325 326 327
 * @network: the network to release
 *
 * Unconditionally release all memory associated with a network.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The network obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
328 329 330
virNetworkDispose(void *obj)
{
    virNetworkPtr network = obj;
331
    char uuidstr[VIR_UUID_STRING_BUFLEN];
332

333
    virUUIDFormat(network->uuid, uuidstr);
334
    VIR_DEBUG("release network %p %s %s", network, network->name, uuidstr);
335

336
    VIR_FREE(network->name);
337
    virObjectUnref(network->conn);
338 339 340
}


D
Daniel Veillard 已提交
341 342 343 344 345 346 347
/**
 * virGetInterface:
 * @conn: the hypervisor connection
 * @name: pointer to the interface name
 * @mac: pointer to the mac
 *
 * Lookup if the interface is already registered for that connection,
348 349
 * if yes return a new pointer to it (possibly updating the MAC
 * address), if no allocate a new structure, and register it in the
350
 * table. In any case a corresponding call to virObjectUnref() is
351
 * needed to not leak data.
D
Daniel Veillard 已提交
352 353 354 355
 *
 * Returns a pointer to the interface, or NULL in case of failure
 */
virInterfacePtr
356 357
virGetInterface(virConnectPtr conn, const char *name, const char *mac)
{
D
Daniel Veillard 已提交
358 359
    virInterfacePtr ret = NULL;

360 361 362
    if (virDataTypesInitialize() < 0)
        return NULL;

363
    if (!VIR_IS_CONNECT(conn)) {
364
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
365
        return NULL;
D
Daniel Veillard 已提交
366
    }
367
    virCheckNonNullArgReturn(name, NULL);
368 369 370 371 372

    /* a NULL mac from caller is okay. Treat it as blank */
    if (mac == NULL)
       mac = "";

373 374
    if (!(ret = virObjectNew(virInterfaceClass)))
        return NULL;
D
Daniel Veillard 已提交
375

376 377 378 379
    if (!(ret->name = strdup(name)))
        goto no_memory;
    if (!(ret->mac = strdup(mac)))
        goto no_memory;
D
Daniel Veillard 已提交
380

381
    ret->conn = virObjectRef(conn);
D
Daniel Veillard 已提交
382

383
    return ret;
D
Daniel Veillard 已提交
384

385 386 387
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
388
    return NULL;
D
Daniel Veillard 已提交
389 390 391
}

/**
392
 * virInterfaceDispose:
D
Daniel Veillard 已提交
393 394
 * @interface: the interface to release
 *
D
Dan Kenigsberg 已提交
395
 * Unconditionally release all memory associated with an interface.
D
Daniel Veillard 已提交
396 397 398 399 400 401 402 403
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The interface obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
404 405 406
virInterfaceDispose(void *obj)
{
    virInterfacePtr iface = obj;
407
    VIR_DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
408

409 410
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
411
    virObjectUnref(iface->conn);
D
Daniel Veillard 已提交
412 413 414
}


415 416 417 418 419
/**
 * virGetStoragePool:
 * @conn: the hypervisor connection
 * @name: pointer to the storage pool name
 * @uuid: pointer to the uuid
420 421
 * @privateData: pointer to driver specific private data
 * @freeFunc: private data cleanup function pointer specfic to driver
422 423 424 425
 *
 * Lookup if the storage pool is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
426
 * virObjectUnref() is needed to not leak data.
427 428 429 430
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virStoragePoolPtr
431
virGetStoragePool(virConnectPtr conn, const char *name,
432 433
                  const unsigned char *uuid,
                  void *privateData, virFreeCallback freeFunc)
434
{
435 436
    virStoragePoolPtr ret = NULL;

437 438 439
    if (virDataTypesInitialize() < 0)
        return NULL;

440
    if (!VIR_IS_CONNECT(conn)) {
441
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
442
        return NULL;
443
    }
444 445 446
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

447 448
    if (!(ret = virObjectNew(virStoragePoolClass)))
        return NULL;
449

450 451 452 453
    if (!(ret->name = strdup(name)))
        goto no_memory;

    ret->conn = virObjectRef(conn);
454 455
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

456 457 458 459
    /* set the driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

460
    return ret;
461

462 463 464
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
465
    return NULL;
466 467 468 469
}


/**
470
 * virStoragePoolDispose:
471 472 473 474 475 476 477 478 479 480 481
 * @pool: the pool to release
 *
 * Unconditionally release all memory associated with a pool.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The pool obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
482 483 484
virStoragePoolDispose(void *obj)
{
    virStoragePoolPtr pool = obj;
485
    char uuidstr[VIR_UUID_STRING_BUFLEN];
486

487
    virUUIDFormat(pool->uuid, uuidstr);
488
    VIR_DEBUG("release pool %p %s %s", pool, pool->name, uuidstr);
489

490 491 492 493
    if (pool->privateDataFreeFunc) {
        pool->privateDataFreeFunc(pool->privateData);
    }

494
    VIR_FREE(pool->name);
495
    virObjectUnref(pool->conn);
496 497 498 499 500 501 502 503
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
J
Jiri Denemark 已提交
504
 * @key: pointer to unique key of the volume
505 506
 * @privateData: pointer to driver specific private data
 * @freeFunc: private data cleanup function pointer specfic to driver
507 508 509 510
 *
 * Lookup if the storage vol is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
511
 * virObjectUnref() is needed to not leak data.
512 513 514 515
 *
 * Returns a pointer to the storage vol, or NULL in case of failure
 */
virStorageVolPtr
516
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
517
                 const char *key, void *privateData, virFreeCallback freeFunc)
518
{
519 520
    virStorageVolPtr ret = NULL;

521 522 523
    if (virDataTypesInitialize() < 0)
        return NULL;

524
    if (!VIR_IS_CONNECT(conn)) {
525
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
526
        return NULL;
527
    }
528 529 530
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(key, NULL);

531 532
    if (!(ret = virObjectNew(virStorageVolClass)))
        return NULL;
533

534 535 536 537 538 539 540 541
    if (!(ret->pool = strdup(pool)))
        goto no_memory;
    if (!(ret->name = strdup(name)))
        goto no_memory;
    if (!(ret->key = strdup(key)))
        goto no_memory;

    ret->conn = virObjectRef(conn);
542

543 544 545 546
    /* set driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

547
    return ret;
548

549 550 551
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
552
    return NULL;
553 554 555 556
}


/**
557
 * virStorageVolDispose:
558 559 560 561 562 563 564 565 566 567 568
 * @vol: the vol to release
 *
 * Unconditionally release all memory associated with a vol.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The vol obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
569 570 571
virStorageVolDispose(void *obj)
{
    virStorageVolPtr vol = obj;
572
    VIR_DEBUG("release vol %p %s", vol, vol->name);
573

574 575 576 577
    if (vol->privateDataFreeFunc) {
        vol->privateDataFreeFunc(vol->privateData);
    }

578
    VIR_FREE(vol->key);
579 580
    VIR_FREE(vol->name);
    VIR_FREE(vol->pool);
581
    virObjectUnref(vol->conn);
582
}
583 584 585 586 587 588 589 590 591 592


/**
 * virGetNodeDevice:
 * @conn: the hypervisor connection
 * @name: device name (unique on node)
 *
 * Lookup if the device is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
593
 * virObjectUnref() is needed to not leak data.
594 595 596 597 598 599 600 601
 *
 * Returns a pointer to the node device, or NULL in case of failure
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

602 603 604
    if (virDataTypesInitialize() < 0)
        return NULL;

605
    if (!VIR_IS_CONNECT(conn)) {
606
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
607
        return NULL;
608
    }
609 610
    virCheckNonNullArgReturn(name, NULL);

611 612
    if (!(ret = virObjectNew(virNodeDeviceClass)))
        return NULL;
613

614 615
    if (!(ret->name = strdup(name)))
        goto no_memory;
616

617
    ret->conn = virObjectRef(conn);
618
    return ret;
619 620 621
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
622
    return NULL;
623 624 625 626
}


/**
627
 * virNodeDeviceDispose:
628 629 630 631 632 633 634 635 636 637 638
 * @dev: the dev to release
 *
 * Unconditionally release all memory associated with a dev.
 * The conn.lock mutex must be held prior to calling this, and will
 * be released prior to this returning. The dev obj must not
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
639 640 641
virNodeDeviceDispose(void *obj)
{
    virNodeDevicePtr dev = obj;
642
    VIR_DEBUG("release dev %p %s", dev, dev->name);
643 644

    VIR_FREE(dev->name);
645
    VIR_FREE(dev->parent);
646

647
    virObjectUnref(dev->conn);
648
}
649

650

651 652 653 654 655 656 657
/**
 * virGetSecret:
 * @conn: the hypervisor connection
 * @uuid: secret UUID
 *
 * Lookup if the secret is already registered for that connection, if so return
 * a pointer to it, otherwise allocate a new structure, and register it in the
658
 * table. In any case a corresponding call to virObjectUnref() is needed to not
659 660 661 662 663
 * leak data.
 *
 * Returns a pointer to the secret, or NULL in case of failure
 */
virSecretPtr
664 665
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
666 667 668
{
    virSecretPtr ret = NULL;

669 670 671
    if (virDataTypesInitialize() < 0)
        return NULL;

672
    if (!VIR_IS_CONNECT(conn)) {
673
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
674 675
        return NULL;
    }
676 677 678
    virCheckNonNullArgReturn(uuid, NULL);
    virCheckNonNullArgReturn(usageID, NULL);

679 680
    if (!(ret = virObjectNew(virSecretClass)))
        return NULL;
681

682 683
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
    ret->usageType = usageType;
684 685
    if (!(ret->usageID = strdup(usageID)))
        goto no_memory;
686

687 688 689 690 691 692
    ret->conn = virObjectRef(conn);

    return ret;
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
693 694 695 696
    return NULL;
}

/**
697
 * virSecretDispose:
698 699 700 701 702 703 704 705 706 707
 * @secret: the secret to release
 *
 * Unconditionally release all memory associated with a secret.  The conn.lock
 * mutex must be held prior to calling this, and will be released prior to this
 * returning. The secret obj must not be used once this method returns.
 *
 * It will also unreference the associated connection object, which may also be
 * released if its ref count hits zero.
 */
static void
708 709 710
virSecretDispose(void *obj)
{
    virSecretPtr secret = obj;
711
    char uuidstr[VIR_UUID_STRING_BUFLEN];
712

713
    virUUIDFormat(secret->uuid, uuidstr);
714
    VIR_DEBUG("release secret %p %s", secret, uuidstr);
J
Jiri Denemark 已提交
715

716
    VIR_FREE(secret->usageID);
717
    virObjectUnref(secret->conn);
718 719 720
}


721 722 723 724
virStreamPtr
virGetStream(virConnectPtr conn)
{
    virStreamPtr ret = NULL;
725

726 727
    if (virDataTypesInitialize() < 0)
        return NULL;
728

729 730
    if (!(ret = virObjectNew(virStreamClass)))
        return NULL;
731

732
    ret->conn = virObjectRef(conn);
733

734
    return ret;
735 736 737
}

static void
738 739 740
virStreamDispose(void *obj)
{
    virStreamPtr st = obj;
741
    VIR_DEBUG("release dev %p", st);
742

743
    virObjectUnref(st->conn);
744
}
S
Stefan Berger 已提交
745 746 747 748 749 750 751 752 753 754 755


/**
 * virGetNWFilter:
 * @conn: the hypervisor connection
 * @name: pointer to the network filter pool name
 * @uuid: pointer to the uuid
 *
 * Lookup if the network filter is already registered for that connection,
 * if yes return a new pointer to it, if no allocate a new structure,
 * and register it in the table. In any case a corresponding call to
756
 * virObjectUnref() is needed to not leak data.
S
Stefan Berger 已提交
757 758 759 760
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNWFilterPtr
761 762 763
virGetNWFilter(virConnectPtr conn, const char *name,
               const unsigned char *uuid)
{
S
Stefan Berger 已提交
764 765
    virNWFilterPtr ret = NULL;

766 767 768
    if (virDataTypesInitialize() < 0)
        return NULL;

769
    if (!VIR_IS_CONNECT(conn)) {
770
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
771
        return NULL;
S
Stefan Berger 已提交
772
    }
773 774 775
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

776 777 778 779 780
    if (!(ret = virObjectNew(virNWFilterClass)))
        return NULL;

    if (!(ret->name = strdup(name)))
        goto no_memory;
S
Stefan Berger 已提交
781

782 783
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

784
    ret->conn = virObjectRef(conn);
S
Stefan Berger 已提交
785

786 787 788 789
    return ret;
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
790
    return NULL;
S
Stefan Berger 已提交
791 792 793 794
}


/**
795
 * virNWFilterDispose:
796
 * @nwfilter: the nwfilter to release
S
Stefan Berger 已提交
797
 *
798
 * Unconditionally release all memory associated with a nwfilter.
S
Stefan Berger 已提交
799
 * The conn.lock mutex must be held prior to calling this, and will
800
 * be released prior to this returning. The nwfilter obj must not
S
Stefan Berger 已提交
801 802 803 804 805 806
 * be used once this method returns.
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
807
virNWFilterDispose(void *obj)
808
{
809
    virNWFilterPtr nwfilter = obj;
810
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
811

812
    virUUIDFormat(nwfilter->uuid, uuidstr);
813
    VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
814

815
    VIR_FREE(nwfilter->name);
816
    virObjectUnref(nwfilter->conn);
S
Stefan Berger 已提交
817
}
C
Chris Lalancette 已提交
818 819 820 821 822 823 824


virDomainSnapshotPtr
virGetDomainSnapshot(virDomainPtr domain, const char *name)
{
    virDomainSnapshotPtr ret = NULL;

825 826 827
    if (virDataTypesInitialize() < 0)
        return NULL;

828
    if (!VIR_IS_DOMAIN(domain)) {
829
        virLibConnError(VIR_ERR_INVALID_DOMAIN, "%s", _("bad domain"));
830
        return NULL;
C
Chris Lalancette 已提交
831
    }
832 833
    virCheckNonNullArgReturn(name, NULL);

834 835 836 837 838
    if (!(ret = virObjectNew(virDomainSnapshotClass)))
        return NULL;
    if (!(ret->name = strdup(name)))
        goto no_memory;
    ret->domain = virObjectRef(domain);
839

840
    return ret;
841 842 843
no_memory:
    virReportOOMError();
    virObjectUnref(ret);
844
    return NULL;
C
Chris Lalancette 已提交
845 846 847 848
}


static void
849
virDomainSnapshotDispose(void *obj)
C
Chris Lalancette 已提交
850
{
851
    virDomainSnapshotPtr snapshot = obj;
852
    VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name);
C
Chris Lalancette 已提交
853 854

    VIR_FREE(snapshot->name);
855
    virObjectUnref(snapshot->domain);
C
Chris Lalancette 已提交
856
}