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

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



For those who don't already know, GCC and CLang both implement a C language
extension that enables automatic free'ing of resources when variables go
out of scope. This is done by annotating the variable with the "cleanup"
attribute, pointing to a function the compiler will wire up a call to when
unwinding the stack. Since the annotation points to an arbitrary user
defined function, you're not limited to simple free() like semantics. The
cleanup function could unlock a mutex, or decrement a reference count, etc

This annotation is used extensively by systemd, and libguestfs, amongst
other projects. This obviously doesn't bring full garbage collection to
C, but it does enable the code to be simplified. By removing the need to
put in many free() (or equiv) calls to cleanup state, the "interesting"
logic in the code stands out more, not being obscured by cleanup calls
and goto jumps.

I'm wondering what people think of making use of this in libvirt ?

To my mind the only real reason to *not* use it, would be to maintain
code portability to non-GCC/non-CLang compilers. OS-X, *BSD and *Linux
all use GCC or CLang or both, so its a non-issue there. So the only place
this could cause pain is people building libvirt on Win32, who are using
the Microsoft compilers instead og GCC.

IMHO, it is perfectly valid for us to declare that MSVC is unsupported
with Libvirt and users must use GCC to build on Windows, either natively
via cygwin, or cross-build from Linux hosts.


As an example of what it would involve...

This commit enables the basic helper macros in libguestfs:

  https://github.com/libguestfs/libguestfs/commit/98b64650c852ccc9a8eef8b9691052faeb4873c8

These commits make use of them

  https://github.com/libguestfs/libguestfs/commit/61162bdce1a00a921a47eb3e788e8fb9e4bd07ac
  https://github.com/libguestfs/libguestfs/commit/5a3da366268825b26b470cde35658b67c1d11cd4
  https://github.com/libguestfs/libguestfs/commit/791ad3e9e600ef528e3e5a8d50be79d56b18129d

Finally, I'm absolutely *not* volunteering to actually implement this
idea myself, as I don't have the free time. I just want to raise it
as a discussion item, and if we agree its something we'd do, then we
can make it a GSoC idea, or let any other interested person hack
on it at will. There's no need for a "big bang" convert everything
approach, we can do it incrementally.

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


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