[libvirt] [PATCH v1 04/40] util: buffer: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

Sukrit Bhatnagar posted 40 patches 7 years, 6 months ago
There is a newer version of this series
[libvirt] [PATCH v1 04/40] util: buffer: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
Posted by Sukrit Bhatnagar 7 years, 6 months ago
Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
src/util/viralloc.h, define a new wrapper around an existing
cleanup function which will be called when a variable declared
with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
viralloc.h include, since that has moved from the source module into
the header.

When variables of type virBufferPtr and virBufferEscapePairPtr
are declared using VIR_AUTOPTR, the functions virBufferFreeAndReset
and virBufferEscapePairFree, respectively, will be run automatically
on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
---
 src/util/virbuffer.c | 2 +-
 src/util/virbuffer.h | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
index 8076cd3..65f4a08 100644
--- a/src/util/virbuffer.c
+++ b/src/util/virbuffer.c
@@ -31,7 +31,6 @@
 #define __VIR_BUFFER_C__
 
 #include "virbuffer.h"
-#include "viralloc.h"
 #include "virerror.h"
 #include "virstring.h"
 
@@ -664,6 +663,7 @@ virBufferEscapePairFree(virBufferEscapePairPtr pair)
 
     VIR_FREE(pair);
 }
+VIR_DEFINE_AUTOPTR_FUNC(virBufferEscapePair, virBufferEscapePairFree)
 
 
 /**
diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
index e95ee87..3b31060 100644
--- a/src/util/virbuffer.h
+++ b/src/util/virbuffer.h
@@ -23,10 +23,13 @@
 #ifndef __VIR_BUFFER_H__
 # define __VIR_BUFFER_H__
 
-# include "internal.h"
-
 # include <stdarg.h>
 
+# include "internal.h"
+
+# include "viralloc.h"
+
+
 /**
  * virBuffer:
  *
@@ -119,4 +122,6 @@ int virBufferGetIndent(const virBuffer *buf, bool dynamic);
 void virBufferTrim(virBufferPtr buf, const char *trim, int len);
 void virBufferAddStr(virBufferPtr buf, const char *str);
 
+VIR_DEFINE_AUTOPTR_FUNC(virBuffer, virBufferFreeAndReset)
+
 #endif /* __VIR_BUFFER_H__ */
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v1 04/40] util: buffer: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC
Posted by Erik Skultety 7 years, 6 months ago
On Sat, Jul 21, 2018 at 05:36:36PM +0530, Sukrit Bhatnagar wrote:
> Using the new VIR_DEFINE_AUTOPTR_FUNC macro defined in
> src/util/viralloc.h, define a new wrapper around an existing
> cleanup function which will be called when a variable declared
> with VIR_AUTOPTR macro goes out of scope. Also, drop the redundant
> viralloc.h include, since that has moved from the source module into
> the header.
>
> When variables of type virBufferPtr and virBufferEscapePairPtr
> are declared using VIR_AUTOPTR, the functions virBufferFreeAndReset
> and virBufferEscapePairFree, respectively, will be run automatically
> on them when they go out of scope.
>
> Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
> ---
>  src/util/virbuffer.c | 2 +-
>  src/util/virbuffer.h | 9 +++++++--
>  2 files changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
> index 8076cd3..65f4a08 100644
> --- a/src/util/virbuffer.c
> +++ b/src/util/virbuffer.c
> @@ -31,7 +31,6 @@
>  #define __VIR_BUFFER_C__
>
>  #include "virbuffer.h"
> -#include "viralloc.h"
>  #include "virerror.h"
>  #include "virstring.h"
>
> @@ -664,6 +663,7 @@ virBufferEscapePairFree(virBufferEscapePairPtr pair)
>
>      VIR_FREE(pair);
>  }
> +VIR_DEFINE_AUTOPTR_FUNC(virBufferEscapePair, virBufferEscapePairFree)

I'm wondering whether we shouldn't export virBufferEscapePair struct to other
potential callers just to make things cleaner by having the
VIR_DEFINE_AUTOPTR_FUNC macros coupled together, but that can be done ad-hoc
once someone needs it, I'm fine with this change too.

>
>
>  /**
> diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
> index e95ee87..3b31060 100644
> --- a/src/util/virbuffer.h
> +++ b/src/util/virbuffer.h
> @@ -23,10 +23,13 @@
>  #ifndef __VIR_BUFFER_H__
>  # define __VIR_BUFFER_H__
>
> -# include "internal.h"
> -
>  # include <stdarg.h>
>
> +# include "internal.h"
> +

^This newline is redundant.

> +# include "viralloc.h"
> +
> +
>  /**
>   * virBuffer:
>   *
> @@ -119,4 +122,6 @@ int virBufferGetIndent(const virBuffer *buf, bool dynamic);
>  void virBufferTrim(virBufferPtr buf, const char *trim, int len);
>  void virBufferAddStr(virBufferPtr buf, const char *str);
>
> +VIR_DEFINE_AUTOPTR_FUNC(virBuffer, virBufferFreeAndReset)
> +
>  #endif /* __VIR_BUFFER_H__ */
> --
> 1.8.3.1
>

If you squash the static definition of virBufferEscapePairFree into this patch:

Reviewed-by: Erik Skultety <eskultet@redhat.com>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list