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