]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: string: Introduce macro for automatic string lists
authorPeter Krempa <pkrempa@redhat.com>
Tue, 26 Feb 2019 15:10:17 +0000 (16:10 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 4 Mar 2019 12:04:20 +0000 (13:04 +0100)
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@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h

index 038a7449811a8433cfe7104c1df9ca9e492d9272..e68e3f3a3b9caa9bf466fc48daf2f98c6c67c4c2 100644 (file)
@@ -2958,6 +2958,7 @@ virStringHasControlChars;
 virStringIsEmpty;
 virStringIsPrintable;
 virStringListAdd;
+virStringListAutoFree;
 virStringListFree;
 virStringListFreeCount;
 virStringListGetFirstWithPrefix;
index e890dde5460a3b069e1668ed7cf1df520aa27991..8a791f96d4e993f6924a74e4c9508932feec8169 100644 (file)
@@ -318,6 +318,16 @@ void virStringListFree(char **strings)
 }
 
 
+void virStringListAutoFree(char ***strings)
+{
+    if (!*strings)
+        return;
+
+    virStringListFree(*strings);
+    *strings = NULL;
+}
+
+
 /**
  * virStringListFreeCount:
  * @strings: array of strings to free
index aef82471c29fbd6f6ba2b2ef1127657cc697b63f..d14b7f4f498a6f9a1b5f057dc51d0aa21068486d 100644 (file)
@@ -53,6 +53,7 @@ int virStringListCopy(char ***dst,
                       const char **src);
 
 void virStringListFree(char **strings);
+void virStringListAutoFree(char ***strings);
 void virStringListFreeCount(char **strings,
                             size_t count);
 
@@ -307,6 +308,15 @@ int virStringParsePort(const char *str,
                        unsigned int *port)
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
+/**
+ * VIR_AUTOSTRINGLIST:
+ *
+ * Declares a NULL-terminated list of strings which will be automatically freed
+ * when the pointer goes out of scope.
+ */
+# define VIR_AUTOSTRINGLIST \
+        __attribute__((cleanup(virStringListAutoFree))) char **
+
 VIR_DEFINE_AUTOPTR_FUNC(virString, virStringListFree);
 
 #endif /* LIBVIRT_VIRSTRING_H */