[libvirt] [PATCH v2 1/4] util: string: Introduce macro for automatic string lists

Peter Krempa pkrempa at redhat.com
Thu Feb 28 13:43:03 UTC 2019


On Wed, Feb 27, 2019 at 13:32:43 +0100, Michal Privoznik wrote:
> On 2/27/19 10:52 AM, Erik Skultety wrote:
> > On Tue, Feb 26, 2019 at 04:48:23PM +0100, Peter Krempa wrote:
> > > Similar to VIR_AUTOPTR, VIR_AUTOSTRINGLIST defines a list of strings
> > > which will be freed if the pointer is leaving scope.
> > > 
> > > Signed-off-by: Peter Krempa <pkrempa at redhat.com>
> > > ---
> > >   src/libvirt_private.syms |  1 +
> > >   src/util/virstring.c     | 10 ++++++++++
> > >   src/util/virstring.h     | 10 ++++++++++
> > >   3 files changed, 21 insertions(+)

[...]

> > > + */
> > > +# define VIR_AUTOSTRINGLIST \
> > > +        __attribute__((cleanup(virStringListAutoFree))) char **
> > > +
> > 
> > IIRC at the beginning we said that all the VIR_AUTO macros should be consistent
> > in terms of how we use the macro, IOW:
> > 
> > VIR_AUTOFREE(type)
> > VIR_AUTOPTR(type)
> > VIR_AUTOCLEAN(type)
> 
> Well, we already have VIR_AUTOCLOSE. I don't mind having a macro that
> implicitly defines type of a variable.

It makes sense especially if the freeing function uses a specified type
and thus you have to use it anyways. It actually prevents bugs.

> On the other hand, this could be renamed to VIR_AUTOLISTFREE as it is
> capable of freeing any NULL terminated list of pointers where plain free()
> ove each member is enough. But it will have to lose the implicit type def in
> that case.

I proposed it in first version with a slightly more complex backed
freeing function. It's quite useless with plain free() and I think it
would invite to more bugs.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20190228/4587a301/attachment-0001.sig>


More information about the libvir-list mailing list