]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Rework virStringListAdd
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 26 Jul 2018 14:10:10 +0000 (16:10 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 27 Jul 2018 13:47:45 +0000 (15:47 +0200)
So every caller does the same: they use virStringListAdd() to add
new item into the list and then free the old copy to replace it
with new list. It's not very memory effective, nor environmental
friendly.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/util/virmacmap.c
src/util/virstring.c
src/util/virstring.h
tests/virstringtest.c

index 88ca9b3f36ae0f5687a2a5e84eb3dd9366df3f16..c7b700fa05c824875edfba2a57bccde6a85fcc4c 100644 (file)
@@ -90,7 +90,6 @@ virMacMapAddLocked(virMacMapPtr mgr,
 {
     int ret = -1;
     char **macsList = NULL;
-    char **newMacsList = NULL;
 
     if ((macsList = virHashLookup(mgr->macs, domain)) &&
         virStringListHasString((const char**) macsList, mac)) {
@@ -98,15 +97,12 @@ virMacMapAddLocked(virMacMapPtr mgr,
         goto cleanup;
     }
 
-    if (!(newMacsList = virStringListAdd((const char **) macsList, mac)) ||
-        virHashUpdateEntry(mgr->macs, domain, newMacsList) < 0)
+    if (virStringListAdd(&macsList, mac) < 0 ||
+        virHashUpdateEntry(mgr->macs, domain, macsList) < 0)
         goto cleanup;
-    newMacsList = NULL;
-    virStringListFree(macsList);
 
     ret = 0;
  cleanup:
-    virStringListFree(newMacsList);
     return ret;
 }
 
index 93fda69d7f286133d08bac2e5862e23b70ee5166..afad5736e1f7f1245993681d490650986de28316 100644 (file)
@@ -175,32 +175,23 @@ char *virStringListJoin(const char **strings,
  * @strings: a NULL-terminated array of strings
  * @item: string to add
  *
- * Creates new strings list with all strings duplicated and @item
- * at the end of the list. Callers is responsible for freeing
- * both @strings and returned list.
+ * Appends @item into string list @strings. If *@strings is not
+ * allocated yet new string list is created.
+ *
+ * Returns: 0 on success,
+ *         -1 otherwise
  */
-char **
-virStringListAdd(const char **strings,
+int
+virStringListAdd(char ***strings,
                  const char *item)
 {
-    char **ret = NULL;
-    size_t i = virStringListLength(strings);
-
-    if (VIR_ALLOC_N(ret, i + 2) < 0)
-        goto error;
-
-    for (i = 0; strings && strings[i]; i++) {
-        if (VIR_STRDUP(ret[i], strings[i]) < 0)
-            goto error;
-    }
+    size_t i = virStringListLength((const char **) *strings);
 
-    if (VIR_STRDUP(ret[i], item) < 0)
-        goto error;
+    if (VIR_EXPAND_N(*strings, i, 2) < 0 ||
+        VIR_STRDUP((*strings)[i - 2], item) < 0)
+        return -1;
 
-    return ret;
- error:
-    virStringListFree(ret);
-    return NULL;
+    return 0;
 }
 
 
index 125fd4eeded03151ff6e704da014e3420a6c6f31..a2133ab7ce30b9ba632f457a3f04ed5231806279 100644 (file)
@@ -44,8 +44,8 @@ char *virStringListJoin(const char **strings,
                         const char *delim)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
-char **virStringListAdd(const char **strings,
-                        const char *item);
+int virStringListAdd(char ***strings,
+                     const char *item);
 void virStringListRemove(char ***strings,
                          const char *item);
 
index 1230aba5b7a320a8adb84d3f6f5d74c380a7e184..1a1e6364d17b851fce349e5560a6c7ab16430198 100644 (file)
@@ -179,12 +179,8 @@ static int testAdd(const void *args)
     size_t i;
 
     for (i = 0; data->tokens[i]; i++) {
-        char **tmp = virStringListAdd((const char **)list, data->tokens[i]);
-        if (!tmp)
+        if (virStringListAdd(&list, data->tokens[i]) < 0)
             goto cleanup;
-        virStringListFree(list);
-        list = tmp;
-        tmp = NULL;
     }
 
     if (!list &&