984721
From 4727de9e9048ffa25784eb0cd536a13ac9644529 Mon Sep 17 00:00:00 2001
984721
Message-Id: <4727de9e9048ffa25784eb0cd536a13ac9644529@dist-git>
984721
From: Michal Privoznik <mprivozn@redhat.com>
984721
Date: Fri, 19 Jun 2020 11:43:12 +0200
984721
Subject: [PATCH] util: Rework virStringListAdd
984721
MIME-Version: 1.0
984721
Content-Type: text/plain; charset=UTF-8
984721
Content-Transfer-Encoding: 8bit
984721
984721
So every caller does the same: they use virStringListAdd() to add
984721
new item into the list and then free the old copy to replace it
984721
with new list. It's not very memory effective, nor environmental
984721
friendly.
984721
984721
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
984721
Reviewed-by: Erik Skultety <eskultet@redhat.com>
984721
(cherry picked from commit 71a390e0fdb4e6cbeaef4c9045a501a6c8de5412)
984721
984721
https://bugzilla.redhat.com/show_bug.cgi?id=1839992
984721
984721
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
984721
Message-Id: <75aadf9f9464504bd1feec27d082b28c9fec0cbe.1592559697.git.mprivozn@redhat.com>
984721
Reviewed-by: Ján Tomko <jtomko@redhat.com>
984721
---
984721
 src/util/virmacmap.c  |  8 ++------
984721
 src/util/virstring.c  | 33 ++++++++++++---------------------
984721
 src/util/virstring.h  |  4 ++--
984721
 tests/virstringtest.c |  6 +-----
984721
 4 files changed, 17 insertions(+), 34 deletions(-)
984721
984721
diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c
984721
index 88ca9b3f36..c7b700fa05 100644
984721
--- a/src/util/virmacmap.c
984721
+++ b/src/util/virmacmap.c
984721
@@ -90,7 +90,6 @@ virMacMapAddLocked(virMacMapPtr mgr,
984721
 {
984721
     int ret = -1;
984721
     char **macsList = NULL;
984721
-    char **newMacsList = NULL;
984721
 
984721
     if ((macsList = virHashLookup(mgr->macs, domain)) &&
984721
         virStringListHasString((const char**) macsList, mac)) {
984721
@@ -98,15 +97,12 @@ virMacMapAddLocked(virMacMapPtr mgr,
984721
         goto cleanup;
984721
     }
984721
 
984721
-    if (!(newMacsList = virStringListAdd((const char **) macsList, mac)) ||
984721
-        virHashUpdateEntry(mgr->macs, domain, newMacsList) < 0)
984721
+    if (virStringListAdd(&macsList, mac) < 0 ||
984721
+        virHashUpdateEntry(mgr->macs, domain, macsList) < 0)
984721
         goto cleanup;
984721
-    newMacsList = NULL;
984721
-    virStringListFree(macsList);
984721
 
984721
     ret = 0;
984721
  cleanup:
984721
-    virStringListFree(newMacsList);
984721
     return ret;
984721
 }
984721
 
984721
diff --git a/src/util/virstring.c b/src/util/virstring.c
984721
index 0f13b6c664..74fa2f6a94 100644
984721
--- a/src/util/virstring.c
984721
+++ b/src/util/virstring.c
984721
@@ -175,32 +175,23 @@ char *virStringListJoin(const char **strings,
984721
  * @strings: a NULL-terminated array of strings
984721
  * @item: string to add
984721
  *
984721
- * Creates new strings list with all strings duplicated and @item
984721
- * at the end of the list. Callers is responsible for freeing
984721
- * both @strings and returned list.
984721
+ * Appends @item into string list @strings. If *@strings is not
984721
+ * allocated yet new string list is created.
984721
+ *
984721
+ * Returns: 0 on success,
984721
+ *         -1 otherwise
984721
  */
984721
-char **
984721
-virStringListAdd(const char **strings,
984721
+int
984721
+virStringListAdd(char ***strings,
984721
                  const char *item)
984721
 {
984721
-    char **ret = NULL;
984721
-    size_t i = virStringListLength(strings);
984721
-
984721
-    if (VIR_ALLOC_N(ret, i + 2) < 0)
984721
-        goto error;
984721
-
984721
-    for (i = 0; strings && strings[i]; i++) {
984721
-        if (VIR_STRDUP(ret[i], strings[i]) < 0)
984721
-            goto error;
984721
-    }
984721
+    size_t i = virStringListLength((const char **) *strings);
984721
 
984721
-    if (VIR_STRDUP(ret[i], item) < 0)
984721
-        goto error;
984721
+    if (VIR_EXPAND_N(*strings, i, 2) < 0 ||
984721
+        VIR_STRDUP((*strings)[i - 2], item) < 0)
984721
+        return -1;
984721
 
984721
-    return ret;
984721
- error:
984721
-    virStringListFree(ret);
984721
-    return NULL;
984721
+    return 0;
984721
 }
984721
 
984721
 
984721
diff --git a/src/util/virstring.h b/src/util/virstring.h
984721
index e68b9eec79..a3db08ce58 100644
984721
--- a/src/util/virstring.h
984721
+++ b/src/util/virstring.h
984721
@@ -41,8 +41,8 @@ char *virStringListJoin(const char **strings,
984721
                         const char *delim)
984721
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
984721
 
984721
-char **virStringListAdd(const char **strings,
984721
-                        const char *item);
984721
+int virStringListAdd(char ***strings,
984721
+                     const char *item);
984721
 void virStringListRemove(char ***strings,
984721
                          const char *item);
984721
 
984721
diff --git a/tests/virstringtest.c b/tests/virstringtest.c
984721
index 1230aba5b7..1a1e6364d1 100644
984721
--- a/tests/virstringtest.c
984721
+++ b/tests/virstringtest.c
984721
@@ -179,12 +179,8 @@ static int testAdd(const void *args)
984721
     size_t i;
984721
 
984721
     for (i = 0; data->tokens[i]; i++) {
984721
-        char **tmp = virStringListAdd((const char **)list, data->tokens[i]);
984721
-        if (!tmp)
984721
+        if (virStringListAdd(&list, data->tokens[i]) < 0)
984721
             goto cleanup;
984721
-        virStringListFree(list);
984721
-        list = tmp;
984721
-        tmp = NULL;
984721
     }
984721
 
984721
     if (!list &&
984721
-- 
984721
2.27.0
984721