[libvirt] [PATCH v2 09/35] util: netdev: define cleanup function using VIR_DEFINE_AUTOPTR_FUNC

Sukrit Bhatnagar skrtbhtngr at gmail.com
Sun Aug 5 20:43:36 UTC 2018


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 virNetDevRxFilterPtr and virNetDevMcastEntryPtr
are declared using VIR_AUTOPTR, the functions virNetDevRxFilterFree
and virNetDevMcastEntryFree, respectively, will be run
automatically on them when they go out of scope.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr at gmail.com>
---
 src/util/virnetdev.c | 9 ++++++++-
 src/util/virnetdev.h | 4 ++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 0777eca..9eca786 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -29,7 +29,6 @@
 #include "virfile.h"
 #include "virerror.h"
 #include "vircommand.h"
-#include "viralloc.h"
 #include "virpci.h"
 #include "virlog.h"
 #include "virstring.h"
@@ -120,6 +119,14 @@ struct _virNetDevMcastEntry  {
         virMacAddr macaddr;
 };
 
+static void
+virNetDevMcastEntryFree(virNetDevMcastEntryPtr entry)
+{
+    VIR_FREE(entry);
+}
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevMcastEntry, virNetDevMcastEntryFree)
+
 typedef struct _virNetDevMcastList virNetDevMcastList;
 typedef virNetDevMcastList *virNetDevMcastListPtr;
 struct _virNetDevMcastList {
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 71eaf45..8860ea1 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -30,6 +30,7 @@
 # include "virmacaddr.h"
 # include "virpci.h"
 # include "virnetdevvlan.h"
+# include "viralloc.h"
 
 # ifdef HAVE_STRUCT_IFREQ
 typedef struct ifreq virIfreq;
@@ -313,4 +314,7 @@ int virNetDevSysfsFile(char **pf_sysfs_device_link,
 
 int virNetDevRunEthernetScript(const char *ifname, const char *script)
     ATTRIBUTE_NOINLINE;
+
+VIR_DEFINE_AUTOPTR_FUNC(virNetDevRxFilter, virNetDevRxFilterFree)
+
 #endif /* __VIR_NETDEV_H__ */
-- 
1.8.3.1




More information about the libvir-list mailing list