virhostdev: use virObject to virHostdevManager to keep reference

Use virObject to virHostdevManager, so that each driver using virHostdevManager
can keep a reference to it, and through counting refs to make virHostdevManager
get freed.
This commit is contained in:
Chunyan Liu 2014-03-06 12:14:21 +08:00 committed by Daniel P. Berrange
parent e562e82f76
commit 6b306d66fa

View File

@ -41,11 +41,33 @@
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
#define HOSTDEV_STATE_DIR LOCALSTATEDIR "/run/libvirt/hostdevmgr" #define HOSTDEV_STATE_DIR LOCALSTATEDIR "/run/libvirt/hostdevmgr"
static virHostdevManagerPtr hostdevMgr; static virHostdevManagerPtr manager; /* global hostdev manager, never freed */
static virClassPtr virHostdevManagerClass;
static void virHostdevManagerDispose(void *obj);
static virHostdevManagerPtr virHostdevManagerNew(void);
static int virHostdevManagerOnceInit(void)
{
if (!(virHostdevManagerClass = virClassNew(virClassForObject(),
"virHostdevManager",
sizeof(virHostdevManager),
virHostdevManagerDispose)))
return -1;
if (!(manager = virHostdevManagerNew()))
return -1;
return 0;
}
VIR_ONCE_GLOBAL_INIT(virHostdevManager)
static void static void
virHostdevManagerCleanup(void) virHostdevManagerDispose(void *obj)
{ {
virHostdevManagerPtr hostdevMgr = obj;
if (!hostdevMgr) if (!hostdevMgr)
return; return;
@ -58,11 +80,13 @@ virHostdevManagerCleanup(void)
VIR_FREE(hostdevMgr); VIR_FREE(hostdevMgr);
} }
static int static virHostdevManagerPtr
virHostdevOnceInit(void) virHostdevManagerNew(void)
{ {
if (VIR_ALLOC(hostdevMgr) < 0) virHostdevManagerPtr hostdevMgr;
goto error;
if (!(hostdevMgr = virObjectNew(virHostdevManagerClass)))
return NULL;
if ((hostdevMgr->activePciHostdevs = virPCIDeviceListNew()) == NULL) if ((hostdevMgr->activePciHostdevs = virPCIDeviceListNew()) == NULL)
goto error; goto error;
@ -86,19 +110,18 @@ virHostdevOnceInit(void)
goto error; goto error;
} }
return 0; return hostdevMgr;
error: error:
virHostdevManagerCleanup(); virObjectUnref(hostdevMgr);
return -1; return NULL;
} }
VIR_ONCE_GLOBAL_INIT(virHostdev)
virHostdevManagerPtr virHostdevManagerPtr
virHostdevManagerGetDefault(void) virHostdevManagerGetDefault(void)
{ {
if (virHostdevInitialize() < 0) if (virHostdevManagerInitialize() < 0)
return NULL; return NULL;
return hostdevMgr;
return virObjectRef(manager);
} }