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. Signed-off-by: Chunyan Liu <cyliu@xxxxxxxx> --- src/util/virhostdev.c | 54 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index cc8ae78..bb2888f 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -41,11 +41,31 @@ #define VIR_FROM_THIS VIR_FROM_NONE #define HOSTDEV_STATE_DIR LOCALSTATEDIR "/run/libvirt/hostdevmgr" -static virHostdevManagerPtr hostdevMgr; +static virHostdevManagerPtr g_hostdev_mgr; /* global hostdev manager*/ + +static virClassPtr virHostdevManagerClass; +static void virHostdevManagerDispose(void *obj); + +static int virHostdevManagerOnceInit(void) +{ + virHostdevManagerClass = virClassNew(virClassForObject(), + "virHostdevManager", + sizeof(virHostdevManager), + virHostdevManagerDispose); + + if (!virHostdevManagerClass) + return -1; + else + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virHostdevManager) static void -virHostdevManagerCleanup(void) +virHostdevManagerDispose(void *obj) { + virHostdevManagerPtr hostdevMgr = obj; + if (!hostdevMgr) return; @@ -58,11 +78,16 @@ virHostdevManagerCleanup(void) VIR_FREE(hostdevMgr); } -static int -virHostdevOnceInit(void) +static virHostdevManagerPtr +virHostdevManagerNew(void) { - if (VIR_ALLOC(hostdevMgr) < 0) - goto error; + virHostdevManagerPtr hostdevMgr; + + if (virHostdevManagerInitialize() < 0) + return NULL; + + if (!(hostdevMgr = virObjectNew(virHostdevManagerClass))) + return NULL; if ((hostdevMgr->activePciHostdevs = virPCIDeviceListNew()) == NULL) goto error; @@ -86,19 +111,20 @@ virHostdevOnceInit(void) goto error; } - return 0; + return hostdevMgr; error: - virHostdevManagerCleanup(); - return -1; + virObjectUnref(hostdevMgr); + return NULL; } -VIR_ONCE_GLOBAL_INIT(virHostdev) - virHostdevManagerPtr virHostdevManagerGetDefault(void) { - if (virHostdevInitialize() < 0) - return NULL; - return hostdevMgr; + if (g_hostdev_mgr == NULL) { + g_hostdev_mgr = virHostdevManagerNew(); + return g_hostdev_mgr; + } else { + return virObjectRef(g_hostdev_mgr); + } } -- 1.9.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list