[libvirt] [PATCH 04/23] util: add VIR_AUTOSTRUCT for directly calling a struct free function

Daniel P. Berrangé berrange at redhat.com
Thu Jul 11 13:45:51 UTC 2019


On Wed, Jul 10, 2019 at 05:32:16PM +0200, Ján Tomko wrote:
> On Thu, Jun 27, 2019 at 10:54:33AM +0100, Daniel P. Berrangé wrote:
> > The current VIR_AUTOPTR macro assumes that the struct needs to have a
> > auto-free function auto-generated to call the real free function.
> > 
> > The new VIR_AUTOSTRUCT macro allows for structs which already have a
> > free function which takes a pointer to a pointer to the struct and can
> > thus be used directly for auto-cleanup.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
> > ---
> > src/util/viralloc.h | 13 +++++++++++++
> > 1 file changed, 13 insertions(+)
> > 
> > diff --git a/src/util/viralloc.h b/src/util/viralloc.h
> > index 2b82096fde..5de18b9ea0 100644
> > --- a/src/util/viralloc.h
> > +++ b/src/util/viralloc.h
> > @@ -614,3 +614,16 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
> >  * when the variable goes out of scope.
> >  */
> > #define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type
> > +
> > +/**
> > + * VIR_AUTOSTRUCT:
> > + * @type: type of the struct variable to be freed automatically
> > + *
> > + * Macro to automatically free the memory allocated to
> > + * the struct variable declared with it by calling vir$STRUCTFree
> > + * when the variable goes out of scope.
> > + *
> > + * The vir$STRUCTFree function must take a pointer to a pointer
> > + * to the struct.
> > + */
> > +#define VIR_AUTOSTRUCT(type) __attribute__((cleanup(type ## Free))) type
> 
> commit a4bfc2521f8aeff8f4e4431a8e2332cc74806b8a
>    util: Move the VIR_AUTO(CLEAN|PTR) helper macros into a separate header
> 
> moved the macros not depending on other stuff from viralloc.h to virautoclean.h
> 
> This macro would better fit there.

I'm going to drop this patch in fact. I realized there's a trivial way to
use the existing VIR_AUTOPTR in the way I want:

diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
index 2858732d00..5d56c78835 100644
--- a/src/util/virsystemd.h
+++ b/src/util/virsystemd.h
@@ -79,3 +79,5 @@ void virSystemdActivationClaimFDs(virSystemdActivationPtr act,
                                   size_t *nfds);
 
 void virSystemdActivationFree(virSystemdActivationPtr *act);
+
+#define virSystemdActivationAutoPtrFree virSystemdActivationFree
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index edca45d9f0..586c512fca 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -554,7 +554,7 @@ testActivation(bool useNames)
     virSystemdActivationMap map[2];
     int *fds = NULL;
     size_t nfds = 0;
-    VIR_AUTOSTRUCT(virSystemdActivation) *act = NULL;
+    VIR_AUTOPTR(virSystemdActivation) act = NULL;
 
     if (testActivationCreateFDs(&sockUNIX, &sockIP, &nsockIP) < 0)
         return -1;


This is nicer because it lets us incrementally get rid of all the
VIR_DEFINE_AUTOPTR_FUNC() by converting the regular xxxFree() funcs
to take a pointer to a pointer, without having to replace all the
VIR_AUTOPTR uss with VIR_AUTOSTRUCT

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the libvir-list mailing list