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

Martin Kletzander mkletzan at redhat.com
Wed Jan 11 09:18:45 UTC 2017


On Tue, Jan 10, 2017 at 11:54:19PM +0000, Richard W.M. Jones wrote:
>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);
>}
>

Well, it is, since we already have that as virFree(), it's just usually
called using VIR_FREE() so that you don't have to add an extra
ampersand.

>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
>
>--
>libvir-list mailing list
>libvir-list at redhat.com
>https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20170111/e05e3322/attachment-0001.sig>


More information about the libvir-list mailing list