datatypes.c 20.9 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 virAdmDaemonClass;
virClassPtr virAdmDaemonCloseCallbackDataClass;
M
Martin Kletzander 已提交
64

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

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

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

95 96
    DECLARE_CLASS_LOCKABLE(virAdmDaemon);
    DECLARE_CLASS_LOCKABLE(virAdmDaemonCloseCallbackData);
M
Martin Kletzander 已提交
97

98 99
#undef DECLARE_CLASS_COMMON
#undef DECLARE_CLASS_LOCKABLE
100 101 102 103 104 105
#undef DECLARE_CLASS

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virDataTypes)
C
Chris Lalancette 已提交
106

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

119 120 121
    if (virDataTypesInitialize() < 0)
        return NULL;

122
    if (!(ret = virObjectLockableNew(virConnectClass)))
123 124
        return NULL;

125
    if (!(ret->closeCallback = virObjectLockableNew(virConnectCloseCallbackDataClass)))
126 127
        goto error;

128
    return ret;
129

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

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

    if (conn->driver)
148
        conn->driver->connectClose(conn);
149

150 151
    virResetError(&conn->err);

152
    virURIFree(conn->uri);
153

154 155 156 157
    if (conn->closeCallback) {
        virObjectLock(conn->closeCallback);
        conn->closeCallback->callback = NULL;
        virObjectUnlock(conn->closeCallback);
158

159 160
        virObjectUnref(conn->closeCallback);
    }
161 162 163
}


164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * 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);
}


184 185 186 187 188 189
/**
 * virGetDomain:
 * @conn: the hypervisor connection
 * @name: pointer to the domain name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
190 191
 * Allocates a new domain object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
192
 *
M
Michael Chapman 已提交
193
 * Returns a pointer to the domain object, or NULL on error.
194 195
 */
virDomainPtr
196 197
virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
198 199
    virDomainPtr ret = NULL;

200 201 202
    if (virDataTypesInitialize() < 0)
        return NULL;

203 204 205
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
206

207
    if (!(ret = virObjectNew(virDomainClass)))
208
        goto error;
209

210 211
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
212 213

    ret->conn = virObjectRef(conn);
214 215 216
    ret->id = -1;
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

217
    return ret;
218

219
 error:
220
    virObjectUnref(ret);
221
    return NULL;
222 223 224
}

/**
225
 * virDomainDispose:
M
Michael Chapman 已提交
226
 * @obj: the domain to release
227 228
 *
 * Unconditionally release all memory associated with a domain.
229
 * The domain object must not be used once this method returns.
230 231 232 233 234
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
235 236 237
virDomainDispose(void *obj)
{
    virDomainPtr domain = obj;
238
    char uuidstr[VIR_UUID_STRING_BUFLEN];
239

240
    virUUIDFormat(domain->uuid, uuidstr);
241
    VIR_DEBUG("release domain %p %s %s", domain, domain->name, uuidstr);
242

243
    VIR_FREE(domain->name);
244
    virObjectUnref(domain->conn);
245 246 247 248 249 250 251 252 253
}


/**
 * virGetNetwork:
 * @conn: the hypervisor connection
 * @name: pointer to the network name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
254 255
 * Allocates a new network object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
256
 *
M
Michael Chapman 已提交
257
 * Returns a pointer to the network object, or NULL on error.
258 259
 */
virNetworkPtr
260 261
virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid)
{
262 263
    virNetworkPtr ret = NULL;

264 265 266
    if (virDataTypesInitialize() < 0)
        return NULL;

267 268 269
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
270

271
    if (!(ret = virObjectNew(virNetworkClass)))
272
        goto error;
273

274 275
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
276 277

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

280
    return ret;
281

282
 error:
283
    virObjectUnref(ret);
284
    return NULL;
285 286 287
}

/**
288
 * virNetworkDispose:
M
Michael Chapman 已提交
289
 * @obj: the network to release
290 291
 *
 * Unconditionally release all memory associated with a network.
292
 * The network object must not be used once this method returns.
293 294 295 296 297
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
298 299 300
virNetworkDispose(void *obj)
{
    virNetworkPtr network = obj;
301
    char uuidstr[VIR_UUID_STRING_BUFLEN];
302

303
    virUUIDFormat(network->uuid, uuidstr);
304
    VIR_DEBUG("release network %p %s %s", network, network->name, uuidstr);
305

306
    VIR_FREE(network->name);
307
    virObjectUnref(network->conn);
308 309 310
}


D
Daniel Veillard 已提交
311 312 313 314 315 316
/**
 * virGetInterface:
 * @conn: the hypervisor connection
 * @name: pointer to the interface name
 * @mac: pointer to the mac
 *
M
Michael Chapman 已提交
317 318
 * 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 已提交
319
 *
M
Michael Chapman 已提交
320
 * Returns a pointer to the interface object, or NULL on error.
D
Daniel Veillard 已提交
321 322
 */
virInterfacePtr
323 324
virGetInterface(virConnectPtr conn, const char *name, const char *mac)
{
D
Daniel Veillard 已提交
325 326
    virInterfacePtr ret = NULL;

327 328 329
    if (virDataTypesInitialize() < 0)
        return NULL;

330 331
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
332 333 334 335 336

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

337
    if (!(ret = virObjectNew(virInterfaceClass)))
338
        goto error;
D
Daniel Veillard 已提交
339

340 341 342
    if (VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->mac, mac) < 0)
        goto error;
D
Daniel Veillard 已提交
343

344
    ret->conn = virObjectRef(conn);
D
Daniel Veillard 已提交
345

346
    return ret;
D
Daniel Veillard 已提交
347

348
 error:
349
    virObjectUnref(ret);
350
    return NULL;
D
Daniel Veillard 已提交
351 352 353
}

/**
354
 * virInterfaceDispose:
M
Michael Chapman 已提交
355
 * @obj: the interface to release
D
Daniel Veillard 已提交
356
 *
D
Dan Kenigsberg 已提交
357
 * Unconditionally release all memory associated with an interface.
358
 * The interface object must not be used once this method returns.
D
Daniel Veillard 已提交
359 360 361 362 363
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
364 365 366
virInterfaceDispose(void *obj)
{
    virInterfacePtr iface = obj;
367
    VIR_DEBUG("release interface %p %s", iface, iface->name);
D
Daniel Veillard 已提交
368

369 370
    VIR_FREE(iface->name);
    VIR_FREE(iface->mac);
371
    virObjectUnref(iface->conn);
D
Daniel Veillard 已提交
372 373 374
}


375 376 377 378 379
/**
 * virGetStoragePool:
 * @conn: the hypervisor connection
 * @name: pointer to the storage pool name
 * @uuid: pointer to the uuid
380
 * @privateData: pointer to driver specific private data
M
Martin Kletzander 已提交
381
 * @freeFunc: private data cleanup function pointer specific to driver
382
 *
M
Michael Chapman 已提交
383 384
 * Allocates a new storage pool object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
385
 *
M
Michael Chapman 已提交
386
 * Returns a pointer to the storage pool object, or NULL on error.
387 388
 */
virStoragePoolPtr
389
virGetStoragePool(virConnectPtr conn, const char *name,
390 391
                  const unsigned char *uuid,
                  void *privateData, virFreeCallback freeFunc)
392
{
393 394
    virStoragePoolPtr ret = NULL;

395 396 397
    if (virDataTypesInitialize() < 0)
        return NULL;

398 399 400
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
401

402
    if (!(ret = virObjectNew(virStoragePoolClass)))
403
        goto error;
404

405 406
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
407 408

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

411 412 413 414
    /* set the driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

415
    return ret;
416

417
 error:
418
    virObjectUnref(ret);
419
    return NULL;
420 421 422 423
}


/**
424
 * virStoragePoolDispose:
M
Michael Chapman 已提交
425
 * @obj: the storage pool to release
426 427
 *
 * Unconditionally release all memory associated with a pool.
428
 * The pool object must not be used once this method returns.
429 430 431 432 433
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
434 435 436
virStoragePoolDispose(void *obj)
{
    virStoragePoolPtr pool = obj;
437
    char uuidstr[VIR_UUID_STRING_BUFLEN];
438

439
    virUUIDFormat(pool->uuid, uuidstr);
440
    VIR_DEBUG("release pool %p %s %s", pool, pool->name, uuidstr);
441

442
    if (pool->privateDataFreeFunc)
443 444
        pool->privateDataFreeFunc(pool->privateData);

445
    VIR_FREE(pool->name);
446
    virObjectUnref(pool->conn);
447 448 449 450 451 452 453 454
}


/**
 * virGetStorageVol:
 * @conn: the hypervisor connection
 * @pool: pool owning the volume
 * @name: pointer to the storage vol name
J
Jiri Denemark 已提交
455
 * @key: pointer to unique key of the volume
456
 * @privateData: pointer to driver specific private data
M
Martin Kletzander 已提交
457
 * @freeFunc: private data cleanup function pointer specific to driver
458
 *
M
Michael Chapman 已提交
459 460
 * Allocates a new storage volume object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
461
 *
M
Michael Chapman 已提交
462
 * Returns a pointer to the storage volume object, or NULL on error.
463 464
 */
virStorageVolPtr
465
virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
466
                 const char *key, void *privateData, virFreeCallback freeFunc)
467
{
468 469
    virStorageVolPtr ret = NULL;

470 471 472
    if (virDataTypesInitialize() < 0)
        return NULL;

473 474 475 476
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(pool, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(key, error);
477

478
    if (!(ret = virObjectNew(virStorageVolClass)))
479
        goto error;
480

481 482 483 484
    if (VIR_STRDUP(ret->pool, pool) < 0 ||
        VIR_STRDUP(ret->name, name) < 0 ||
        VIR_STRDUP(ret->key, key) < 0)
        goto error;
485 486

    ret->conn = virObjectRef(conn);
487

488 489 490 491
    /* set driver specific data */
    ret->privateData = privateData;
    ret->privateDataFreeFunc = freeFunc;

492
    return ret;
493

494
 error:
495
    virObjectUnref(ret);
496
    return NULL;
497 498 499 500
}


/**
501
 * virStorageVolDispose:
M
Michael Chapman 已提交
502
 * @obj: the storage volume to release
503
 *
504 505
 * Unconditionally release all memory associated with a volume.
 * The volume object must not be used once this method returns.
506 507 508 509 510
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
511 512 513
virStorageVolDispose(void *obj)
{
    virStorageVolPtr vol = obj;
514
    VIR_DEBUG("release vol %p %s", vol, vol->name);
515

516
    if (vol->privateDataFreeFunc)
517 518
        vol->privateDataFreeFunc(vol->privateData);

519
    VIR_FREE(vol->key);
520 521
    VIR_FREE(vol->name);
    VIR_FREE(vol->pool);
522
    virObjectUnref(vol->conn);
523
}
524 525 526 527 528 529 530


/**
 * virGetNodeDevice:
 * @conn: the hypervisor connection
 * @name: device name (unique on node)
 *
M
Michael Chapman 已提交
531 532
 * Allocates a new node device object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
533
 *
M
Michael Chapman 已提交
534
 * Returns a pointer to the node device object, or NULL on error.
535 536 537 538 539 540
 */
virNodeDevicePtr
virGetNodeDevice(virConnectPtr conn, const char *name)
{
    virNodeDevicePtr ret = NULL;

541 542 543
    if (virDataTypesInitialize() < 0)
        return NULL;

544 545
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
546

547
    if (!(ret = virObjectNew(virNodeDeviceClass)))
548
        goto error;
549

550 551
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
552

553
    ret->conn = virObjectRef(conn);
554
    return ret;
555

556
 error:
557
    virObjectUnref(ret);
558
    return NULL;
559 560 561 562
}


/**
563
 * virNodeDeviceDispose:
M
Michael Chapman 已提交
564
 * @obj: the node device to release
565
 *
566 567
 * Unconditionally release all memory associated with a device.
 * The device object must not be used once this method returns.
568 569 570 571 572
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
573 574 575
virNodeDeviceDispose(void *obj)
{
    virNodeDevicePtr dev = obj;
576
    VIR_DEBUG("release dev %p %s", dev, dev->name);
577 578

    VIR_FREE(dev->name);
579
    VIR_FREE(dev->parent);
580

581
    virObjectUnref(dev->conn);
582
}
583

584

585 586 587 588 589
/**
 * virGetSecret:
 * @conn: the hypervisor connection
 * @uuid: secret UUID
 *
M
Michael Chapman 已提交
590 591
 * Allocates a new secret object. When the object is no longer needed,
 * virObjectUnref() must be called in order to not leak data.
592
 *
M
Michael Chapman 已提交
593
 * Returns a pointer to the secret object, or NULL on error.
594 595
 */
virSecretPtr
596 597
virGetSecret(virConnectPtr conn, const unsigned char *uuid,
             int usageType, const char *usageID)
598 599 600
{
    virSecretPtr ret = NULL;

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

604 605 606
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(uuid, error);
    virCheckNonNullArgGoto(usageID, error);
607

608 609
    if (!(ret = virObjectNew(virSecretClass)))
        return NULL;
610

611 612
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
    ret->usageType = usageType;
613 614
    if (VIR_STRDUP(ret->usageID, usageID) < 0)
        goto error;
615

616 617 618
    ret->conn = virObjectRef(conn);

    return ret;
619

620
 error:
621
    virObjectUnref(ret);
622 623 624 625
    return NULL;
}

/**
626
 * virSecretDispose:
M
Michael Chapman 已提交
627
 * @obj: the secret to release
628
 *
629 630
 * Unconditionally release all memory associated with a secret.
 * The secret object must not be used once this method returns.
631
 *
632 633
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
634 635
 */
static void
636 637 638
virSecretDispose(void *obj)
{
    virSecretPtr secret = obj;
639
    char uuidstr[VIR_UUID_STRING_BUFLEN];
640

641
    virUUIDFormat(secret->uuid, uuidstr);
642
    VIR_DEBUG("release secret %p %s", secret, uuidstr);
J
Jiri Denemark 已提交
643

644
    VIR_FREE(secret->usageID);
645
    virObjectUnref(secret->conn);
646 647 648
}


M
Michael Chapman 已提交
649 650 651 652 653 654 655 656 657
/**
 * 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.
 */
658 659 660 661
virStreamPtr
virGetStream(virConnectPtr conn)
{
    virStreamPtr ret = NULL;
662

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

666 667
    if (!(ret = virObjectNew(virStreamClass)))
        return NULL;
668

669
    ret->conn = virObjectRef(conn);
670

671
    return ret;
672 673
}

M
Michael Chapman 已提交
674 675 676 677 678 679 680 681 682 683
/**
 * 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.
 */
684
static void
685 686 687
virStreamDispose(void *obj)
{
    virStreamPtr st = obj;
688
    VIR_DEBUG("release dev %p", st);
689

690
    virObjectUnref(st->conn);
691
}
S
Stefan Berger 已提交
692 693 694 695 696 697 698 699


/**
 * virGetNWFilter:
 * @conn: the hypervisor connection
 * @name: pointer to the network filter pool name
 * @uuid: pointer to the uuid
 *
M
Michael Chapman 已提交
700 701
 * 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 已提交
702
 *
M
Michael Chapman 已提交
703
 * Returns a pointer to the network filter object, or NULL on error.
S
Stefan Berger 已提交
704 705
 */
virNWFilterPtr
706 707 708
virGetNWFilter(virConnectPtr conn, const char *name,
               const unsigned char *uuid)
{
S
Stefan Berger 已提交
709 710
    virNWFilterPtr ret = NULL;

711 712 713
    if (virDataTypesInitialize() < 0)
        return NULL;

714 715 716
    virCheckConnectGoto(conn, error);
    virCheckNonNullArgGoto(name, error);
    virCheckNonNullArgGoto(uuid, error);
717

718
    if (!(ret = virObjectNew(virNWFilterClass)))
719
        goto error;
720

721 722
    if (VIR_STRDUP(ret->name, name) < 0)
        goto error;
S
Stefan Berger 已提交
723

724 725
    memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);

726
    ret->conn = virObjectRef(conn);
S
Stefan Berger 已提交
727

728
    return ret;
729

730
 error:
731
    virObjectUnref(ret);
732
    return NULL;
S
Stefan Berger 已提交
733 734 735 736
}


/**
737
 * virNWFilterDispose:
M
Michael Chapman 已提交
738
 * @obj: the network filter to release
S
Stefan Berger 已提交
739
 *
740
 * Unconditionally release all memory associated with a nwfilter.
741
 * The nwfilter object must not be used once this method returns.
S
Stefan Berger 已提交
742 743 744 745 746
 *
 * It will also unreference the associated connection object,
 * which may also be released if its ref count hits zero.
 */
static void
747
virNWFilterDispose(void *obj)
748
{
749
    virNWFilterPtr nwfilter = obj;
750
    char uuidstr[VIR_UUID_STRING_BUFLEN];
S
Stefan Berger 已提交
751

752
    virUUIDFormat(nwfilter->uuid, uuidstr);
753
    VIR_DEBUG("release nwfilter %p %s %s", nwfilter, nwfilter->name, uuidstr);
754

755
    VIR_FREE(nwfilter->name);
756
    virObjectUnref(nwfilter->conn);
S
Stefan Berger 已提交
757
}
C
Chris Lalancette 已提交
758 759


M
Michael Chapman 已提交
760 761 762 763 764 765 766 767 768 769
/**
 * 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 已提交
770 771 772 773 774
virDomainSnapshotPtr
virGetDomainSnapshot(virDomainPtr domain, const char *name)
{
    virDomainSnapshotPtr ret = NULL;

775 776 777
    if (virDataTypesInitialize() < 0)
        return NULL;

778 779
    virCheckDomainGoto(domain, error);
    virCheckNonNullArgGoto(name, error);
780

781
    if (!(ret = virObjectNew(virDomainSnapshotClass)))
782
        goto error;
783
    if (VIR_STRDUP(ret->name, name) < 0)
784
        goto error;
785

786
    ret->domain = virObjectRef(domain);
787

788
    return ret;
789

790
 error:
791
    virObjectUnref(ret);
792
    return NULL;
C
Chris Lalancette 已提交
793 794 795
}


M
Michael Chapman 已提交
796 797 798 799 800 801 802 803 804 805
/**
 * 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 已提交
806
static void
807
virDomainSnapshotDispose(void *obj)
C
Chris Lalancette 已提交
808
{
809
    virDomainSnapshotPtr snapshot = obj;
810
    VIR_DEBUG("release snapshot %p %s", snapshot, snapshot->name);
C
Chris Lalancette 已提交
811 812

    VIR_FREE(snapshot->name);
813
    virObjectUnref(snapshot->domain);
C
Chris Lalancette 已提交
814
}
M
Martin Kletzander 已提交
815 816


817 818
virAdmDaemonPtr
virAdmDaemonNew(void)
M
Martin Kletzander 已提交
819
{
820
    virAdmDaemonPtr ret;
M
Martin Kletzander 已提交
821 822 823 824

    if (virDataTypesInitialize() < 0)
        return NULL;

825
    if (!(ret = virObjectLockableNew(virAdmDaemonClass)))
M
Martin Kletzander 已提交
826 827
        return NULL;

828
    if (!(ret->closeCallback = virObjectLockableNew(virAdmDaemonCloseCallbackDataClass)))
829 830
        goto error;

M
Martin Kletzander 已提交
831
    return ret;
832 833 834 835

 error:
    virObjectUnref(ret);
    return NULL;
M
Martin Kletzander 已提交
836 837 838
}

static void
839
virAdmDaemonDispose(void *obj)
M
Martin Kletzander 已提交
840
{
841
    virAdmDaemonPtr dmn = obj;
M
Martin Kletzander 已提交
842

843 844
    if (dmn->privateDataFreeFunc)
        dmn->privateDataFreeFunc(dmn);
845

846 847
    virURIFree(dmn->uri);
    virObjectUnref(dmn->closeCallback);
848 849 850
}

static void
851
virAdmDaemonCloseCallbackDataDispose(void *obj)
852
{
853
    virAdmDaemonCloseCallbackDataPtr cb_data = obj;
854 855 856 857 858 859 860

    virObjectLock(cb_data);

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

    virObjectUnlock(cb_data);
M
Martin Kletzander 已提交
861
}