[libvirt] RFC: Use __attribute__ ((cleanup) in libvirt ?

Richard W.M. Jones rjones at redhat.com
Tue Jan 10 23:54:19 UTC 2017


On Tue, Jan 10, 2017 at 10:00:31AM +0000, Daniel P. Berrange wrote:
> If we mandate use of gcc / clang, then we wouldn't need to hide it
> behind a macro - we'd be able to use it inline. That said, using a
> macro makes it smaller and gives a bit of standardization. eg with
> libguestfs style:
> 
>   #define CLEANUP_FREE __attribute__((cleanup(free)))
>   #define CLEANUP_OBJECT_UNREF __attribute__((cleanup(virObjectUnref)))
> 
>   CLEANUP_FREE char *str;
>   CLEANUP_OBJECT_UNREF virDomainPtr dom;
> 
> vs full inline style:
> 
>   __attribute__((cleanup(free))) char *str;
>   __attribute__((cleanup(virObjectUnref))) virDomainPtr dom;
> 
> That said I see systemd took a halfway house
> 
>   #define _cleanup_(x) __attribute__((cleanup(x)))
> 
>   _cleanup(free) char *str;
>   _cleanup(virObjectUnref) virDomainPtr dom;

I think it's not quite as simple as that because GCC passes
the pointer to the pointer.  libguestfs uses:

#define CLEANUP_FREE __attribute__((cleanup(guestfs_int_cleanup_free)))

...

void
guestfs_int_cleanup_free (void *ptr)
{
  free (* (void **) ptr);
}

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html




More information about the libvir-list mailing list