[PATCHv2 22/33] util: string: Add helper to free non-NULL terminated string arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sometimes the length of the string list is known but the array isn't
NULL terminated. Add helper to free the array in such cases.
---
 src/libvirt_private.syms |  1 +
 src/util/virstring.c     | 20 ++++++++++++++++++++
 src/util/virstring.h     |  1 +
 3 files changed, 22 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ee745ea..cb1fd72 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1901,6 +1901,7 @@ virStrcpy;
 virStrdup;
 virStringArrayHasString;
 virStringFreeList;
+virStringFreeListCount;
 virStringJoin;
 virStringListLength;
 virStringReplace;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 6dcc7a8..35b99a5 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -187,6 +187,26 @@ void virStringFreeList(char **strings)
 }


+/**
+ * virStringFreeListCount:
+ * @strings: array of strings to free
+ * @count: number of elements in the array
+ *
+ * Frees a string array of @count length.
+ */
+void
+virStringFreeListCount(char **strings,
+                       size_t count)
+{
+    size_t i;
+
+    for (i = 0; i < count; i++)
+        VIR_FREE(strings[i]);
+
+    VIR_FREE(strings);
+}
+
+
 bool
 virStringArrayHasString(char **strings, const char *needle)
 {
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 0ab9d96..42d68b5 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -42,6 +42,7 @@ char *virStringJoin(const char **strings,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

 void virStringFreeList(char **strings);
+void virStringFreeListCount(char **strings, size_t count);

 bool virStringArrayHasString(char **strings, const char *needle);

-- 
1.9.3

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




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]