Re: [PATCH v14 02/49] virhostdev: use virObject to virHostdevManager to keep reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Mar 07, 2014 at 06:52:29PM +0800, Chunyan Liu wrote:
> 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);
> +    }
>  }

Technically this is not threadsafe, but we only use it during libvirtd
daemon initialization so we're ok IMHO. Also if the last reference is
removed,the we never clear  g_hostdev_mgr - could do that in the dispose
method easily enough so I'll fix that.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]