datatypes.c 22.0 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 "virstring.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->networkClose(conn);
148
    if (conn->interfaceDriver)
149
        conn->interfaceDriver->interfaceClose(conn);
150
    if (conn->storageDriver)
151
        conn->storageDriver->storageClose(conn);
152
    if (conn->nodeDeviceDriver)
153
        conn->nodeDeviceDriver->nodeDeviceClose(conn);
154
    if (conn->secretDriver)
155
        conn->secretDriver->secretClose(conn);
156
    if (conn->nwfilterDriver)
157
        conn->nwfilterDriver->nwfilterClose(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
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
231 232

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

236
    return ret;
237

238
error:
239
    virObjectUnref(ret);
240
    return NULL;
241 242 243
}

/**
244
 * virDomainDispose:
245 246 247 248 249 250 251 252 253 254 255
 * @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
256 257 258
virDomainDispose(void *obj)
{
    virDomainPtr domain = obj;
259
    char uuidstr[VIR_UUID_STRING_BUFLEN];
260

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

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


/**
 * 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
278
 * virObjectUnref() is needed to not leak data.
279 280 281 282
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNetworkPtr
283 284
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
285 286
    virNetworkPtr ret = NULL;

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

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

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

300 301
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
302 303

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

306
    return ret;
307

308
error:
309
    virObjectUnref(ret);
310
    return NULL;
311 312 313
}

/**
314
 * virNetworkDispose:
315 316 317 318 319 320 321 322 323 324 325
 * @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
326 327 328
virNetworkDispose(void *obj)
{
    virNetworkPtr network = obj;
329
    char uuidstr[VIR_UUID_STRING_BUFLEN];
330

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

334
    VIR_FREE(network->name);
335
    virObjectUnref(network->conn);
336 337 338
}


D
Daniel Veillard 已提交
339 340 341 342 343 344 345
/**
 * 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,
346 347
 * if yes return a new pointer to it (possibly updating the MAC
 * address), if no allocate a new structure, and register it in the
348
 * table. In any case a corresponding call to virObjectUnref() is
349
 * needed to not leak data.
D
Daniel Veillard 已提交
350 351 352 353
 *
 * Returns a pointer to the interface, or NULL in case of failure
 */
virInterfacePtr
354 355
virGetInterface(virConnectPtr conn, const char *name, const char *mac)
{
D
Daniel Veillard 已提交
356 357
    virInterfacePtr ret = NULL;

358 359 360
    if (virDataTypesInitialize() < 0)
        return NULL;

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

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

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

374 375 376
    if (VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->mac, mac) < 0)
        goto error;
D
Daniel Veillard 已提交
377

378
    ret->conn = virObjectRef(conn);
D
Daniel Veillard 已提交
379

380
    return ret;
D
Daniel Veillard 已提交
381

382
error:
383
    virObjectUnref(ret);
384
    return NULL;
D
Daniel Veillard 已提交
385 386 387
}

/**
388
 * virInterfaceDispose:
D
Daniel Veillard 已提交
389 390
 * @interface: the interface to release
 *
D
Dan Kenigsberg 已提交
391
 * Unconditionally release all memory associated with an interface.
D
Daniel Veillard 已提交
392 393 394 395 396 397 398 399
 * 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
400 401 402
virInterfaceDispose(void *obj)
{
    virInterfacePtr iface = obj;
403
    VIR_DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
404

405 406
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
407
    virObjectUnref(iface->conn);
D
Daniel Veillard 已提交
408 409 410
}


411 412 413 414 415
/**
 * virGetStoragePool:
 * @conn: the hypervisor connection
 * @name: pointer to the storage pool name
 * @uuid: pointer to the uuid
416 417
 * @privateData: pointer to driver specific private data
 * @freeFunc: private data cleanup function pointer specfic to driver
418 419 420 421
 *
 * 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
422
 * virObjectUnref() is needed to not leak data.
423 424 425 426
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virStoragePoolPtr
427
virGetStoragePool(virConnectPtr conn, const char *name,
428 429
                  const unsigned char *uuid,
                  void *privateData, virFreeCallback freeFunc)
430
{
431 432
    virStoragePoolPtr ret = NULL;

433 434 435
    if (virDataTypesInitialize() < 0)
        return NULL;

436
    if (!VIR_IS_CONNECT(conn)) {
437
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
438
        return NULL;
439
    }
440 441 442
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

443 444
    if (!(ret = virObjectNew(virStoragePoolClass)))
        return NULL;
445

446 447
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
448 449

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

452 453 454 455
    /* set the driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

456
    return ret;
457

458
error:
459
    virObjectUnref(ret);
460
    return NULL;
461 462 463 464
}


/**
465
 * virStoragePoolDispose:
466 467 468 469 470 471 472 473 474 475 476
 * @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
477 478 479
virStoragePoolDispose(void *obj)
{
    virStoragePoolPtr pool = obj;
480
    char uuidstr[VIR_UUID_STRING_BUFLEN];
481

482
    virUUIDFormat(pool->uuid, uuidstr);
483
    VIR_DEBUG("release pool %p %s %s", pool, pool->name, uuidstr);
484

485 486 487 488
    if (pool->privateDataFreeFunc) {
        pool->privateDataFreeFunc(pool->privateData);
    }

489
    VIR_FREE(pool->name);
490
    virObjectUnref(pool->conn);
491 492 493 494 495 496 497 498
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
J
Jiri Denemark 已提交
499
 * @key: pointer to unique key of the volume
500 501
 * @privateData: pointer to driver specific private data
 * @freeFunc: private data cleanup function pointer specfic to driver
502 503 504 505
 *
 * 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
506
 * virObjectUnref() is needed to not leak data.
507 508 509 510
 *
 * Returns a pointer to the storage vol, or NULL in case of failure
 */
virStorageVolPtr
511
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
512
                 const char *key, void *privateData, virFreeCallback freeFunc)
513
{
514 515
    virStorageVolPtr ret = NULL;

516 517 518
    if (virDataTypesInitialize() < 0)
        return NULL;

519
    if (!VIR_IS_CONNECT(conn)) {
520
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
521
        return NULL;
522
    }
523
    virCheckNonNullArgReturn(pool, NULL);
524 525 526
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(key, NULL);

527 528
    if (!(ret = virObjectNew(virStorageVolClass)))
        return NULL;
529

530 531 532 533
    if (VIR_STRDUP(ret->pool, pool) < 0 ||
        VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->key, key) < 0)
        goto error;
534 535

    ret->conn = virObjectRef(conn);
536

537 538 539 540
    /* set driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

541
    return ret;
542

543
error:
544
    virObjectUnref(ret);
545
    return NULL;
546 547 548 549
}


/**
550
 * virStorageVolDispose:
551 552 553 554 555 556 557 558 559 560 561
 * @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
562 563 564
virStorageVolDispose(void *obj)
{
    virStorageVolPtr vol = obj;
565
    VIR_DEBUG("release vol %p %s", vol, vol->name);
566

567 568 569 570
    if (vol->privateDataFreeFunc) {
        vol->privateDataFreeFunc(vol->privateData);
    }

571
    VIR_FREE(vol->key);
572 573
    VIR_FREE(vol->name);
    VIR_FREE(vol->pool);
574
    virObjectUnref(vol->conn);
575
}
576 577 578 579 580 581 582 583 584 585


/**
 * 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
586
 * virObjectUnref() is needed to not leak data.
587 588 589 590 591 592 593 594
 *
 * Returns a pointer to the node device, or NULL in case of failure
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

595 596 597
    if (virDataTypesInitialize() < 0)
        return NULL;

598
    if (!VIR_IS_CONNECT(conn)) {
599
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
600
        return NULL;
601
    }
602 603
    virCheckNonNullArgReturn(name, NULL);

604 605
    if (!(ret = virObjectNew(virNodeDeviceClass)))
        return NULL;
606

607 608
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
609

610
    ret->conn = virObjectRef(conn);
611
    return ret;
612 613

error:
614
    virObjectUnref(ret);
615
    return NULL;
616 617 618 619
}


/**
620
 * virNodeDeviceDispose:
621 622 623 624 625 626 627 628 629 630 631
 * @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
632 633 634
virNodeDeviceDispose(void *obj)
{
    virNodeDevicePtr dev = obj;
635
    VIR_DEBUG("release dev %p %s", dev, dev->name);
636 637

    VIR_FREE(dev->name);
638
    VIR_FREE(dev->parent);
639

640
    virObjectUnref(dev->conn);
641
}
642

643

644 645 646 647 648 649 650
/**
 * 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
651
 * table. In any case a corresponding call to virObjectUnref() is needed to not
652 653 654 655 656
 * leak data.
 *
 * Returns a pointer to the secret, or NULL in case of failure
 */
virSecretPtr
657 658
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
659 660 661
{
    virSecretPtr ret = NULL;

662 663 664
    if (virDataTypesInitialize() < 0)
        return NULL;

665
    if (!VIR_IS_CONNECT(conn)) {
666
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
667 668
        return NULL;
    }
669 670 671
    virCheckNonNullArgReturn(uuid, NULL);
    virCheckNonNullArgReturn(usageID, NULL);

672 673
    if (!(ret = virObjectNew(virSecretClass)))
        return NULL;
674

675 676
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
    ret->usageType = usageType;
677 678
    if (VIR_STRDUP(ret->usageID, usageID) < 0)
        goto error;
679

680 681 682
    ret->conn = virObjectRef(conn);

    return ret;
683 684

error:
685
    virObjectUnref(ret);
686 687 688 689
    return NULL;
}

/**
690
 * virSecretDispose:
691 692 693 694 695 696 697 698 699 700
 * @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
701 702 703
virSecretDispose(void *obj)
{
    virSecretPtr secret = obj;
704
    char uuidstr[VIR_UUID_STRING_BUFLEN];
705

706
    virUUIDFormat(secret->uuid, uuidstr);
707
    VIR_DEBUG("release secret %p %s", secret, uuidstr);
J
Jiri Denemark 已提交
708

709
    VIR_FREE(secret->usageID);
710
    virObjectUnref(secret->conn);
711 712 713
}


714 715 716 717
virStreamPtr
virGetStream(virConnectPtr conn)
{
    virStreamPtr ret = NULL;
718

719 720
    if (virDataTypesInitialize() < 0)
        return NULL;
721

722 723
    if (!(ret = virObjectNew(virStreamClass)))
        return NULL;
724

725
    ret->conn = virObjectRef(conn);
726

727
    return ret;
728 729 730
}

static void
731 732 733
virStreamDispose(void *obj)
{
    virStreamPtr st = obj;
734
    VIR_DEBUG("release dev %p", st);
735

736
    virObjectUnref(st->conn);
737
}
S
Stefan Berger 已提交
738 739 740 741 742 743 744 745 746 747 748


/**
 * 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
749
 * virObjectUnref() is needed to not leak data.
S
Stefan Berger 已提交
750 751 752 753
 *
 * Returns a pointer to the network, or NULL in case of failure
 */
virNWFilterPtr
754 755 756
virGetNWFilter(virConnectPtr conn, const char *name,
               const unsigned char *uuid)
{
S
Stefan Berger 已提交
757 758
    virNWFilterPtr ret = NULL;

759 760 761
    if (virDataTypesInitialize() < 0)
        return NULL;

762
    if (!VIR_IS_CONNECT(conn)) {
763
        virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
764
        return NULL;
S
Stefan Berger 已提交
765
    }
766 767 768
    virCheckNonNullArgReturn(name, NULL);
    virCheckNonNullArgReturn(uuid, NULL);

769 770 771
    if (!(ret = virObjectNew(virNWFilterClass)))
        return NULL;

772 773
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
S
Stefan Berger 已提交
774

775 776
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

777
    ret->conn = virObjectRef(conn);
S
Stefan Berger 已提交
778

779
    return ret;
780 781

error:
782
    virObjectUnref(ret);
783
    return NULL;
S
Stefan Berger 已提交
784 785 786 787
}


/**
788
 * virNWFilterDispose:
789
 * @nwfilter: the nwfilter to release
S
Stefan Berger 已提交
790
 *
791
 * Unconditionally release all memory associated with a nwfilter.
S
Stefan Berger 已提交
792
 * The conn.lock mutex must be held prior to calling this, and will
793
 * be released prior to this returning. The nwfilter obj must not
S
Stefan Berger 已提交
794 795 796 797 798 799
 * 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
800
virNWFilterDispose(void *obj)
801
{
802
    virNWFilterPtr nwfilter = obj;
803
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
804

805
    virUUIDFormat(nwfilter->uuid, uuidstr);
806
    VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
807

808
    VIR_FREE(nwfilter->name);
809
    virObjectUnref(nwfilter->conn);
S
Stefan Berger 已提交
810
}
C
Chris Lalancette 已提交
811 812 813 814 815 816 817


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

818 819 820
    if (virDataTypesInitialize() < 0)
        return NULL;

821
    if (!VIR_IS_DOMAIN(domain)) {
822
        virLibConnError(VIR_ERR_INVALID_DOMAIN, "%s", _("bad domain"));
823
        return NULL;
C
Chris Lalancette 已提交
824
    }
825 826
    virCheckNonNullArgReturn(name, NULL);

827 828
    if (!(ret = virObjectNew(virDomainSnapshotClass)))
        return NULL;
829 830 831
    if (VIR_STRDUP(ret->name, name) < 0)
        goto cleanup;

832
    ret->domain = virObjectRef(domain);
833

834
    return ret;
835 836

cleanup:
837
    virObjectUnref(ret);
838
    return NULL;
C
Chris Lalancette 已提交
839 840 841 842
}


static void
843
virDomainSnapshotDispose(void *obj)
C
Chris Lalancette 已提交
844
{
845
    virDomainSnapshotPtr snapshot = obj;
846
    VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name);
C
Chris Lalancette 已提交
847 848

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