[libvirt] [GSoC] Design ideas for implementing cleanup attribute
Andrea Bolognani
abologna at redhat.com
Fri May 25 07:13:51 UTC 2018
On Wed, 2018-05-23 at 18:05 +0200, Pavel Hrdina wrote:
> I liked the way how GLib is solving the issue so we can simply use the
> same approach since it looks reasonable.
>
> There would be three different macros that would be used to annotate
> variable with attribute cleanup:
>
> VIR_AUTOFREE char *str = NULL;
>
> - this would call virFree on that variable
>
> VIR_AUTOPTR(virDomain) domain = NULL;
>
> - this would call registered free function on that variable
> - to register the free function you would use:
>
> VIR_DEFINE_AUTOPTR_FUNC(virDomain, virDomainFree);
>
> VIR_AUTOCLEAR(virDomain) domain = { 0 };
>
> - this would call registered clear function to free the content of
> that structure
> - to register that clear function you would use:
>
> VIR_DEFINE_AUTOCLEAR_FUNC(virDomain, virDomainClear);
I assume you would get a compilation error when trying to eg. use
VIR_AUTOCLEAR() with a type that doesn't have a clear function
registered?
As for VIR_AUTOFREE() and VIR_AUTOPTR(), I'd very much prefer if we
could have a single macro, since from the high-level point of view
they're both doing the same thing, that is, freeing memory that was
allocated on the heap.
However, I realize it might not be possible to register free
functions for a native type without having to introduce something
like
typedef char * virString;
thus causing massive churn. How does GLib deal with that?
--
Andrea Bolognani / Red Hat / Virtualization
More information about the libvir-list
mailing list