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