datatypes.c 23.8 KB
Newer Older
1
/*
2
 * datatypes.c: management of structs for public data types
3
 *
4
 * Copyright (C) 2006-2015 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 35
VIR_LOG_INIT("datatypes");

36
virClassPtr virConnectClass;
37
virClassPtr virConnectCloseCallbackDataClass;
38 39 40 41 42 43 44 45 46 47 48 49
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);
50
static void virConnectCloseCallbackDataDispose(void *obj);
51 52 53 54 55 56 57 58 59 60 61
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);

62 63
virClassPtr virAdmConnectClass;
virClassPtr virAdmConnectCloseCallbackDataClass;
M
Martin Kletzander 已提交
64

65 66
static void virAdmConnectDispose(void *obj);
static void virAdmConnectCloseCallbackDataDispose(void *obj);
M
Martin Kletzander 已提交
67

68 69 70
virClassPtr virAdmServerClass;
static void virAdmServerDispose(void *obj);

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

85
    DECLARE_CLASS_LOCKABLE(virConnect);
86
    DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData);
87 88 89 90 91 92 93 94 95 96 97
    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);

98 99
    DECLARE_CLASS_LOCKABLE(virAdmConnect);
    DECLARE_CLASS_LOCKABLE(virAdmConnectCloseCallbackData);
100
    DECLARE_CLASS(virAdmServer);
M
Martin Kletzander 已提交
101

102 103
#undef DECLARE_CLASS_COMMON
#undef DECLARE_CLASS_LOCKABLE
104 105 106 107 108 109
#undef DECLARE_CLASS

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virDataTypes)
C
Chris Lalancette 已提交
110

111 112 113
/**
 * virGetConnect:
 *
M
Michael Chapman 已提交
114
 * Allocates a new hypervisor connection object.
115
 *
M
Michael Chapman 已提交
116
 * Returns a pointer to the connection object, or NULL on error.
117 118
 */
virConnectPtr
119 120
virGetConnect(void)
{
121 122
    virConnectPtr ret;

123 124 125
    if (virDataTypesInitialize() < 0)
        return NULL;

126
    if (!(ret = virObjectLockableNew(virConnectClass)))
127 128
        return NULL;

129
    if (!(ret->closeCallback = virObjectLockableNew(virConnectCloseCallbackDataClass)))
130 131
        goto error;

132
    return ret;
133

134
 error:
135 136
    virObjectUnref(ret);
    return NULL;
137 138 139
}

/**
140
 * virConnectDispose:
M
Michael Chapman 已提交
141
 * @obj: the hypervisor connection to release
142 143
 *
 * Unconditionally release all memory associated with a connection.
144
 * The connection object must not be used once this method returns.
145 146
 */
static void
147 148 149
virConnectDispose(void *obj)
{
    virConnectPtr conn = obj;
150 151

    if (conn->driver)
152
        conn->driver->connectClose(conn);
153

154 155
    virResetError(&conn->err);

156
    virURIFree(conn->uri);
157

158 159 160 161
    if (conn->closeCallback) {
        virObjectLock(conn->closeCallback);
        conn->closeCallback->callback = NULL;
        virObjectUnlock(conn->closeCallback);
162

163 164
        virObjectUnref(conn->closeCallback);
    }
165 166 167
}


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/**
 * 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);
183
    virObjectUnref(cb->conn);
184 185 186 187

    virObjectUnlock(cb);
}

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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
void virConnectCloseCallbackDataRegister(virConnectCloseCallbackDataPtr close,
                                         virConnectPtr conn,
                                         virConnectCloseFunc cb,
                                         void *opaque,
                                         virFreeCallback freecb)
{
    virObjectLock(close);

    if (close->callback != NULL) {
        VIR_WARN("Attempt to register callback on armed"
                 " close callback object %p", close);
        goto cleanup;
        return;
    }

    close->conn = conn;
    virObjectRef(close->conn);
    close->callback = cb;
    close->opaque = opaque;
    close->freeCallback = freecb;

 cleanup:

    virObjectUnlock(close);
}

void virConnectCloseCallbackDataUnregister(virConnectCloseCallbackDataPtr close,
                                           virConnectCloseFunc cb)
{
    virObjectLock(close);

    if (close->callback != cb) {
        VIR_WARN("Attempt to unregister different callback on "
                 " close callback object %p", close);
        goto cleanup;
    }

    close->callback = NULL;
    if (close->freeCallback)
        close->freeCallback(close->opaque);
    close->freeCallback = NULL;
    virObjectUnref(close->conn);
230
    close->conn = NULL;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

 cleanup:

    virObjectUnlock(close);
}

void virConnectCloseCallbackDataCall(virConnectCloseCallbackDataPtr close,
                                     int reason)
{
    virObjectLock(close);

    if (!close->callback)
        goto exit;

    VIR_DEBUG("Triggering connection close callback %p reason=%d, opaque=%p",
              close->callback, reason, close->opaque);
    close->callback(close->conn, reason, close->opaque);

    if (close->freeCallback)
        close->freeCallback(close->opaque);
    close->callback = NULL;
    close->freeCallback = NULL;
253 254
    virObjectUnref(close->conn);
    close->conn = NULL;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

 exit:
    virObjectUnlock(close);
}

virConnectCloseFunc
virConnectCloseCallbackDataGetCallback(virConnectCloseCallbackDataPtr close)
{
    virConnectCloseFunc cb;

    virObjectLock(close);
    cb = close->callback;
    virObjectUnlock(close);

    return cb;
}
271

272 273 274 275 276 277
/**
 * virGetDomain:
 * @conn: the hypervisor connection
 * @name: pointer to the domain name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
278 279
 * Allocates a new domain object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
280
 *
M
Michael Chapman 已提交
281
 * Returns a pointer to the domain object, or NULL on error.
282 283
 */
virDomainPtr
284 285
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
286 287
    virDomainPtr ret = NULL;

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

291 292 293
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
294

295
    if (!(ret = virObjectNew(virDomainClass)))
296
        goto error;
297

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

    ret->conn = virObjectRef(conn);
302 303 304
    ret->id = -1;
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

305
    return ret;
306

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

/**
313
 * virDomainDispose:
M
Michael Chapman 已提交
314
 * @obj: the domain to release
315 316
 *
 * Unconditionally release all memory associated with a domain.
317
 * The domain object must not be used once this method returns.
318 319 320 321 322
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
323 324 325
virDomainDispose(void *obj)
{
    virDomainPtr domain = obj;
326
    char uuidstr[VIR_UUID_STRING_BUFLEN];
327

328
    virUUIDFormat(domain->uuid, uuidstr);
329
    VIR_DEBUG("release domain %p %s %s", domain, domain->name, uuidstr);
330

331
    VIR_FREE(domain->name);
332
    virObjectUnref(domain->conn);
333 334 335 336 337 338 339 340 341
}


/**
 * virGetNetwork:
 * @conn: the hypervisor connection
 * @name: pointer to the network name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
342 343
 * Allocates a new network object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
344
 *
M
Michael Chapman 已提交
345
 * Returns a pointer to the network object, or NULL on error.
346 347
 */
virNetworkPtr
348 349
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
350 351
    virNetworkPtr ret = NULL;

352 353 354
    if (virDataTypesInitialize() < 0)
        return NULL;

355 356 357
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
358

359
    if (!(ret = virObjectNew(virNetworkClass)))
360
        goto error;
361

362 363
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
364 365

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

368
    return ret;
369

370
 error:
371
    virObjectUnref(ret);
372
    return NULL;
373 374 375
}

/**
376
 * virNetworkDispose:
M
Michael Chapman 已提交
377
 * @obj: the network to release
378 379
 *
 * Unconditionally release all memory associated with a network.
380
 * The network object must not be used once this method returns.
381 382 383 384 385
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
386 387 388
virNetworkDispose(void *obj)
{
    virNetworkPtr network = obj;
389
    char uuidstr[VIR_UUID_STRING_BUFLEN];
390

391
    virUUIDFormat(network->uuid, uuidstr);
392
    VIR_DEBUG("release network %p %s %s", network, network->name, uuidstr);
393

394
    VIR_FREE(network->name);
395
    virObjectUnref(network->conn);
396 397 398
}


D
Daniel Veillard 已提交
399 400 401 402 403 404
/**
 * virGetInterface:
 * @conn: the hypervisor connection
 * @name: pointer to the interface name
 * @mac: pointer to the mac
 *
M
Michael Chapman 已提交
405 406
 * Allocates a new interface object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
D
Daniel Veillard 已提交
407
 *
M
Michael Chapman 已提交
408
 * Returns a pointer to the interface object, or NULL on error.
D
Daniel Veillard 已提交
409 410
 */
virInterfacePtr
411 412
virGetInterface(virConnectPtr conn, const char *name, const char *mac)
{
D
Daniel Veillard 已提交
413 414
    virInterfacePtr ret = NULL;

415 416 417
    if (virDataTypesInitialize() < 0)
        return NULL;

418 419
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
420 421 422 423 424

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

425
    if (!(ret = virObjectNew(virInterfaceClass)))
426
        goto error;
D
Daniel Veillard 已提交
427

428 429 430
    if (VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->mac, mac) < 0)
        goto error;
D
Daniel Veillard 已提交
431

432
    ret->conn = virObjectRef(conn);
D
Daniel Veillard 已提交
433

434
    return ret;
D
Daniel Veillard 已提交
435

436
 error:
437
    virObjectUnref(ret);
438
    return NULL;
D
Daniel Veillard 已提交
439 440 441
}

/**
442
 * virInterfaceDispose:
M
Michael Chapman 已提交
443
 * @obj: the interface to release
D
Daniel Veillard 已提交
444
 *
D
Dan Kenigsberg 已提交
445
 * Unconditionally release all memory associated with an interface.
446
 * The interface object must not be used once this method returns.
D
Daniel Veillard 已提交
447 448 449 450 451
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
452 453 454
virInterfaceDispose(void *obj)
{
    virInterfacePtr iface = obj;
455
    VIR_DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
456

457 458
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
459
    virObjectUnref(iface->conn);
D
Daniel Veillard 已提交
460 461 462
}


463 464 465 466 467
/**
 * virGetStoragePool:
 * @conn: the hypervisor connection
 * @name: pointer to the storage pool name
 * @uuid: pointer to the uuid
468
 * @privateData: pointer to driver specific private data
M
Martin Kletzander 已提交
469
 * @freeFunc: private data cleanup function pointer specific to driver
470
 *
M
Michael Chapman 已提交
471 472
 * Allocates a new storage pool object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
473
 *
M
Michael Chapman 已提交
474
 * Returns a pointer to the storage pool object, or NULL on error.
475 476
 */
virStoragePoolPtr
477
virGetStoragePool(virConnectPtr conn, const char *name,
478 479
                  const unsigned char *uuid,
                  void *privateData, virFreeCallback freeFunc)
480
{
481 482
    virStoragePoolPtr ret = NULL;

483 484 485
    if (virDataTypesInitialize() < 0)
        return NULL;

486 487 488
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
489

490
    if (!(ret = virObjectNew(virStoragePoolClass)))
491
        goto error;
492

493 494
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
495 496

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

499 500 501 502
    /* set the driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

503
    return ret;
504

505
 error:
506
    virObjectUnref(ret);
507
    return NULL;
508 509 510 511
}


/**
512
 * virStoragePoolDispose:
M
Michael Chapman 已提交
513
 * @obj: the storage pool to release
514 515
 *
 * Unconditionally release all memory associated with a pool.
516
 * The pool object must not be used once this method returns.
517 518 519 520 521
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
522 523 524
virStoragePoolDispose(void *obj)
{
    virStoragePoolPtr pool = obj;
525
    char uuidstr[VIR_UUID_STRING_BUFLEN];
526

527
    virUUIDFormat(pool->uuid, uuidstr);
528
    VIR_DEBUG("release pool %p %s %s", pool, pool->name, uuidstr);
529

530
    if (pool->privateDataFreeFunc)
531 532
        pool->privateDataFreeFunc(pool->privateData);

533
    VIR_FREE(pool->name);
534
    virObjectUnref(pool->conn);
535 536 537 538 539 540 541 542
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
J
Jiri Denemark 已提交
543
 * @key: pointer to unique key of the volume
544
 * @privateData: pointer to driver specific private data
M
Martin Kletzander 已提交
545
 * @freeFunc: private data cleanup function pointer specific to driver
546
 *
M
Michael Chapman 已提交
547 548
 * Allocates a new storage volume object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
549
 *
M
Michael Chapman 已提交
550
 * Returns a pointer to the storage volume object, or NULL on error.
551 552
 */
virStorageVolPtr
553
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
554
                 const char *key, void *privateData, virFreeCallback freeFunc)
555
{
556 557
    virStorageVolPtr ret = NULL;

558 559 560
    if (virDataTypesInitialize() < 0)
        return NULL;

561 562 563 564
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(pool, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(key, error);
565

566
    if (!(ret = virObjectNew(virStorageVolClass)))
567
        goto error;
568

569 570 571 572
    if (VIR_STRDUP(ret->pool, pool) < 0 ||
        VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->key, key) < 0)
        goto error;
573 574

    ret->conn = virObjectRef(conn);
575

576 577 578 579
    /* set driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

580
    return ret;
581

582
 error:
583
    virObjectUnref(ret);
584
    return NULL;
585 586 587 588
}


/**
589
 * virStorageVolDispose:
M
Michael Chapman 已提交
590
 * @obj: the storage volume to release
591
 *
592 593
 * Unconditionally release all memory associated with a volume.
 * The volume object must not be used once this method returns.
594 595 596 597 598
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
599 600 601
virStorageVolDispose(void *obj)
{
    virStorageVolPtr vol = obj;
602
    VIR_DEBUG("release vol %p %s", vol, vol->name);
603

604
    if (vol->privateDataFreeFunc)
605 606
        vol->privateDataFreeFunc(vol->privateData);

607
    VIR_FREE(vol->key);
608 609
    VIR_FREE(vol->name);
    VIR_FREE(vol->pool);
610
    virObjectUnref(vol->conn);
611
}
612 613 614 615 616 617 618


/**
 * virGetNodeDevice:
 * @conn: the hypervisor connection
 * @name: device name (unique on node)
 *
M
Michael Chapman 已提交
619 620
 * Allocates a new node device object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
621
 *
M
Michael Chapman 已提交
622
 * Returns a pointer to the node device object, or NULL on error.
623 624 625 626 627 628
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

629 630 631
    if (virDataTypesInitialize() < 0)
        return NULL;

632 633
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
634

635
    if (!(ret = virObjectNew(virNodeDeviceClass)))
636
        goto error;
637

638 639
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
640

641
    ret->conn = virObjectRef(conn);
642
    return ret;
643

644
 error:
645
    virObjectUnref(ret);
646
    return NULL;
647 648 649 650
}


/**
651
 * virNodeDeviceDispose:
M
Michael Chapman 已提交
652
 * @obj: the node device to release
653
 *
654 655
 * Unconditionally release all memory associated with a device.
 * The device object must not be used once this method returns.
656 657 658 659 660
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
661 662 663
virNodeDeviceDispose(void *obj)
{
    virNodeDevicePtr dev = obj;
664
    VIR_DEBUG("release dev %p %s", dev, dev->name);
665 666

    VIR_FREE(dev->name);
667
    VIR_FREE(dev->parent);
668

669
    virObjectUnref(dev->conn);
670
}
671

672

673 674 675 676 677
/**
 * virGetSecret:
 * @conn: the hypervisor connection
 * @uuid: secret UUID
 *
M
Michael Chapman 已提交
678 679
 * Allocates a new secret object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
680
 *
M
Michael Chapman 已提交
681
 * Returns a pointer to the secret object, or NULL on error.
682 683
 */
virSecretPtr
684 685
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
686 687 688
{
    virSecretPtr ret = NULL;

689 690 691
    if (virDataTypesInitialize() < 0)
        return NULL;

692 693 694
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(uuid, error);
    virCheckNonNullArgGoto(usageID, error);
695

696 697
    if (!(ret = virObjectNew(virSecretClass)))
        return NULL;
698

699 700
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
    ret->usageType = usageType;
701 702
    if (VIR_STRDUP(ret->usageID, usageID) < 0)
        goto error;
703

704 705 706
    ret->conn = virObjectRef(conn);

    return ret;
707

708
 error:
709
    virObjectUnref(ret);
710 711 712 713
    return NULL;
}

/**
714
 * virSecretDispose:
M
Michael Chapman 已提交
715
 * @obj: the secret to release
716
 *
717 718
 * Unconditionally release all memory associated with a secret.
 * The secret object must not be used once this method returns.
719
 *
720 721
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
722 723
 */
static void
724 725 726
virSecretDispose(void *obj)
{
    virSecretPtr secret = obj;
727
    char uuidstr[VIR_UUID_STRING_BUFLEN];
728

729
    virUUIDFormat(secret->uuid, uuidstr);
730
    VIR_DEBUG("release secret %p %s", secret, uuidstr);
J
Jiri Denemark 已提交
731

732
    VIR_FREE(secret->usageID);
733
    virObjectUnref(secret->conn);
734 735 736
}


M
Michael Chapman 已提交
737 738 739 740 741 742 743 744 745
/**
 * virGetStream:
 * @conn: the hypervisor connection
 *
 * Allocates a new stream object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
 *
 * Returns a pointer to the stream object, or NULL on error.
 */
746 747 748 749
virStreamPtr
virGetStream(virConnectPtr conn)
{
    virStreamPtr ret = NULL;
750

751 752
    if (virDataTypesInitialize() < 0)
        return NULL;
753

754 755
    if (!(ret = virObjectNew(virStreamClass)))
        return NULL;
756

757
    ret->conn = virObjectRef(conn);
758

759
    return ret;
760 761
}

M
Michael Chapman 已提交
762 763 764 765 766 767 768 769 770 771
/**
 * virStreamDispose:
 * @obj: the stream to release
 *
 * Unconditionally release all memory associated with a stream.
 * The stream object 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.
 */
772
static void
773 774 775
virStreamDispose(void *obj)
{
    virStreamPtr st = obj;
776
    VIR_DEBUG("release dev %p", st);
777

778
    virObjectUnref(st->conn);
779
}
S
Stefan Berger 已提交
780 781 782 783 784 785 786 787


/**
 * virGetNWFilter:
 * @conn: the hypervisor connection
 * @name: pointer to the network filter pool name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
788 789
 * Allocates a new network filter object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
S
Stefan Berger 已提交
790
 *
M
Michael Chapman 已提交
791
 * Returns a pointer to the network filter object, or NULL on error.
S
Stefan Berger 已提交
792 793
 */
virNWFilterPtr
794 795 796
virGetNWFilter(virConnectPtr conn, const char *name,
               const unsigned char *uuid)
{
S
Stefan Berger 已提交
797 798
    virNWFilterPtr ret = NULL;

799 800 801
    if (virDataTypesInitialize() < 0)
        return NULL;

802 803 804
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
805

806
    if (!(ret = virObjectNew(virNWFilterClass)))
807
        goto error;
808

809 810
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
S
Stefan Berger 已提交
811

812 813
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

814
    ret->conn = virObjectRef(conn);
S
Stefan Berger 已提交
815

816
    return ret;
817

818
 error:
819
    virObjectUnref(ret);
820
    return NULL;
S
Stefan Berger 已提交
821 822 823 824
}


/**
825
 * virNWFilterDispose:
M
Michael Chapman 已提交
826
 * @obj: the network filter to release
S
Stefan Berger 已提交
827
 *
828
 * Unconditionally release all memory associated with a nwfilter.
829
 * The nwfilter object must not be used once this method returns.
S
Stefan Berger 已提交
830 831 832 833 834
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
835
virNWFilterDispose(void *obj)
836
{
837
    virNWFilterPtr nwfilter = obj;
838
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
839

840
    virUUIDFormat(nwfilter->uuid, uuidstr);
841
    VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
842

843
    VIR_FREE(nwfilter->name);
844
    virObjectUnref(nwfilter->conn);
S
Stefan Berger 已提交
845
}
C
Chris Lalancette 已提交
846 847


M
Michael Chapman 已提交
848 849 850 851 852 853 854 855 856 857
/**
 * virGetDomainSnapshot:
 * @domain: the domain to snapshot
 * @name: pointer to the domain snapshot name
 *
 * Allocates a new domain snapshot object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
 *
 * Returns a pointer to the domain snapshot object, or NULL on error.
 */
C
Chris Lalancette 已提交
858 859 860 861 862
virDomainSnapshotPtr
virGetDomainSnapshot(virDomainPtr domain, const char *name)
{
    virDomainSnapshotPtr ret = NULL;

863 864 865
    if (virDataTypesInitialize() < 0)
        return NULL;

866 867
    virCheckDomainGoto(domain, error);
    virCheckNonNullArgGoto(name, error);
868

869
    if (!(ret = virObjectNew(virDomainSnapshotClass)))
870
        goto error;
871
    if (VIR_STRDUP(ret->name, name) < 0)
872
        goto error;
873

874
    ret->domain = virObjectRef(domain);
875

876
    return ret;
877

878
 error:
879
    virObjectUnref(ret);
880
    return NULL;
C
Chris Lalancette 已提交
881 882 883
}


M
Michael Chapman 已提交
884 885 886 887 888 889 890 891 892 893
/**
 * virDomainSnapshotDispose:
 * @obj: the domain snapshot to release
 *
 * Unconditionally release all memory associated with a snapshot.
 * The snapshot object 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.
 */
C
Chris Lalancette 已提交
894
static void
895
virDomainSnapshotDispose(void *obj)
C
Chris Lalancette 已提交
896
{
897
    virDomainSnapshotPtr snapshot = obj;
898
    VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name);
C
Chris Lalancette 已提交
899 900

    VIR_FREE(snapshot->name);
901
    virObjectUnref(snapshot->domain);
C
Chris Lalancette 已提交
902
}
M
Martin Kletzander 已提交
903 904


905 906
virAdmConnectPtr
virAdmConnectNew(void)
M
Martin Kletzander 已提交
907
{
908
    virAdmConnectPtr ret;
M
Martin Kletzander 已提交
909 910 911 912

    if (virDataTypesInitialize() < 0)
        return NULL;

913
    if (!(ret = virObjectLockableNew(virAdmConnectClass)))
M
Martin Kletzander 已提交
914 915
        return NULL;

916
    if (!(ret->closeCallback = virObjectLockableNew(virAdmConnectCloseCallbackDataClass)))
917 918
        goto error;

M
Martin Kletzander 已提交
919
    return ret;
920 921 922 923

 error:
    virObjectUnref(ret);
    return NULL;
M
Martin Kletzander 已提交
924 925 926
}

static void
927
virAdmConnectDispose(void *obj)
M
Martin Kletzander 已提交
928
{
929
    virAdmConnectPtr conn = obj;
M
Martin Kletzander 已提交
930

931 932
    if (conn->privateDataFreeFunc)
        conn->privateDataFreeFunc(conn);
933

934 935
    virURIFree(conn->uri);
    virObjectUnref(conn->closeCallback);
936 937 938
}

static void
939
virAdmConnectCloseCallbackDataDispose(void *obj)
940
{
941
    virAdmConnectCloseCallbackDataPtr cb_data = obj;
942 943 944 945 946 947 948

    virObjectLock(cb_data);

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

    virObjectUnlock(cb_data);
M
Martin Kletzander 已提交
949
}
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

virAdmServerPtr
virAdmGetServer(virAdmConnectPtr conn, const char *name)
{
    virAdmServerPtr ret = NULL;

    if (virDataTypesInitialize() < 0)
        goto error;

    if (!(ret = virObjectNew(virAdmServerClass)))
        goto error;
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;

    ret->conn = virObjectRef(conn);

    return ret;
 error:
    virObjectUnref(ret);
    return NULL;
}

static void
virAdmServerDispose(void *obj)
{
    virAdmServerPtr srv = obj;
    VIR_DEBUG("release server srv=%p name=%s", srv, srv->name);

    VIR_FREE(srv->name);
    virObjectUnref(srv->conn);
}