提交 71a390e0 编写于 作者: M Michal Privoznik

util: Rework virStringListAdd

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: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NErik Skultety <eskultet@redhat.com>
上级 f2a519a5
...@@ -90,7 +90,6 @@ virMacMapAddLocked(virMacMapPtr mgr, ...@@ -90,7 +90,6 @@ virMacMapAddLocked(virMacMapPtr mgr,
{ {
int ret = -1; int ret = -1;
char **macsList = NULL; char **macsList = NULL;
char **newMacsList = NULL;
if ((macsList = virHashLookup(mgr->macs, domain)) && if ((macsList = virHashLookup(mgr->macs, domain)) &&
virStringListHasString((const char**) macsList, mac)) { virStringListHasString((const char**) macsList, mac)) {
...@@ -98,15 +97,12 @@ virMacMapAddLocked(virMacMapPtr mgr, ...@@ -98,15 +97,12 @@ virMacMapAddLocked(virMacMapPtr mgr,
goto cleanup; goto cleanup;
} }
if (!(newMacsList = virStringListAdd((const char **) macsList, mac)) || if (virStringListAdd(&macsList, mac) < 0 ||
virHashUpdateEntry(mgr->macs, domain, newMacsList) < 0) virHashUpdateEntry(mgr->macs, domain, macsList) < 0)
goto cleanup; goto cleanup;
newMacsList = NULL;
virStringListFree(macsList);
ret = 0; ret = 0;
cleanup: cleanup:
virStringListFree(newMacsList);
return ret; return ret;
} }
......
...@@ -175,32 +175,23 @@ char *virStringListJoin(const char **strings, ...@@ -175,32 +175,23 @@ char *virStringListJoin(const char **strings,
* @strings: a NULL-terminated array of strings * @strings: a NULL-terminated array of strings
* @item: string to add * @item: string to add
* *
* Creates new strings list with all strings duplicated and @item * Appends @item into string list @strings. If *@strings is not
* at the end of the list. Callers is responsible for freeing * allocated yet new string list is created.
* both @strings and returned list. *
* Returns: 0 on success,
* -1 otherwise
*/ */
char ** int
virStringListAdd(const char **strings, virStringListAdd(char ***strings,
const char *item) const char *item)
{ {
char **ret = NULL; size_t i = virStringListLength((const char **) *strings);
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;
}
if (VIR_STRDUP(ret[i], item) < 0) if (VIR_EXPAND_N(*strings, i, 2) < 0 ||
goto error; VIR_STRDUP((*strings)[i - 2], item) < 0)
return -1;
return ret; return 0;
error:
virStringListFree(ret);
return NULL;
} }
......
...@@ -44,8 +44,8 @@ char *virStringListJoin(const char **strings, ...@@ -44,8 +44,8 @@ char *virStringListJoin(const char **strings,
const char *delim) const char *delim)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
char **virStringListAdd(const char **strings, int virStringListAdd(char ***strings,
const char *item); const char *item);
void virStringListRemove(char ***strings, void virStringListRemove(char ***strings,
const char *item); const char *item);
......
...@@ -179,12 +179,8 @@ static int testAdd(const void *args) ...@@ -179,12 +179,8 @@ static int testAdd(const void *args)
size_t i; size_t i;
for (i = 0; data->tokens[i]; i++) { for (i = 0; data->tokens[i]; i++) {
char **tmp = virStringListAdd((const char **)list, data->tokens[i]); if (virStringListAdd(&list, data->tokens[i]) < 0)
if (!tmp)
goto cleanup; goto cleanup;
virStringListFree(list);
list = tmp;
tmp = NULL;
} }
if (!list && if (!list &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册