From b9490d128b1a65d4d1ca273ddd54c696fa62ad73 Mon Sep 17 00:00:00 2001
Message-Id: <b9490d128b1a65d4d1ca273ddd54c696fa62ad73@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Fri, 21 Jun 2019 09:26:01 +0200
Subject: [PATCH] util: string: Introduce macro for automatic string lists
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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>
(cherry picked from commit daefda165b28b399478137c75b5466f865c65be5)
https://bugzilla.redhat.com/show_bug.cgi?id=1697627
Conflicts:
src/util/virstring.h
- different header file guard
- no AUTOPTR for string lists
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Message-Id: <7f1e7edf9c9a096fde1a2adf37090f8190e99be2.1561068591.git.jdenemar@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virstring.c | 10 ++++++++++
src/util/virstring.h | 10 ++++++++++
3 files changed, 21 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a20e0593f0..06374deaae 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2923,6 +2923,7 @@ virStringHasControlChars;
virStringIsEmpty;
virStringIsPrintable;
virStringListAdd;
+virStringListAutoFree;
virStringListFree;
virStringListFreeCount;
virStringListGetFirstWithPrefix;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 15f367af7c..0f13b6c664 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -332,6 +332,16 @@ void virStringListFree(char **strings)
}
+void virStringListAutoFree(char ***strings)
+{
+ if (!*strings)
+ return;
+
+ virStringListFree(*strings);
+ *strings = NULL;
+}
+
+
/**
* virStringListFreeCount:
* @strings: array of strings to free
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 607ae66e99..e68b9eec79 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -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);
@@ -309,4 +310,13 @@ 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 **
+
#endif /* __VIR_STRING_H__ */
--
2.22.0