string list functions
Subsystems (2)
v4 cover string list functions
Add some handy string list functions for general use, and use them in live migration functions. These will also be needed for cpr exec mode.
Changes in V4:
* added exec migration patch
Steve Sistare (5):
util: strList_from_string
qapi: QAPI_LIST_LENGTH
util: strv_from_strList
util: strList unit tests
migration: simplify exec migration functions
include/monitor/hmp.h | 1 - include/qapi/util.h | 13 ++++++++ include/qemu/strList.h | 30 ++++++++++++++++++ migration/exec.c | 58 +++++----------------------------- monitor/hmp-cmds.c | 19 ----------- net/net-hmp-cmds.c | 3 +- stats/stats-hmp-cmds.c | 3 +- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + util/strList.c | 38 ++++++++++++++++++++++ 11 files changed, 175 insertions(+), 72 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 tests/unit/test-strList.c create mode 100644 util/strList.c
Signature · 1 line
1.8.3.1
PATCH v4 1/5 util: strList_from_string
Generalize hmp_split_at_comma() to take any delimiter string, rename as strList_from_string(), and move it to util/strList.c.
No functional change.
--- include/monitor/hmp.h | 1 - include/qemu/strList.h | 24 ++++++++++++++++++++++++ monitor/hmp-cmds.c | 19 ------------------- net/net-hmp-cmds.c | 3 ++- stats/stats-hmp-cmds.c | 3 ++- util/meson.build | 1 + util/strList.c | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 util/strList.c
include/monitor/hmp.h +0 -1
@@ -19,7 +19,6 @@bool hmp_handle_error (Monitor *mon,Error *err);void hmp_help_cmd (Monitor *mon,const char *name); -strList *hmp_split_at_comma (const char *str);void hmp_info_name (Monitor *mon,const QDict *qdict);void hmp_info_version (Monitor *mon,const QDict *qdict);
include/qemu/strList.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_STR_LIST_H +#define QEMU_STR_LIST_H + +#include "qapi/qapi-builtin-types.h" + +/* +* Break @in into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @in is NULL or an empty string. +* A leading, trailing, or consecutive delimiter produces an +* empty string at that position in the output. +* All strings are g_strdup'd, and the result can be freed +* using qapi_free_strList. +*/ +strList *strList_from_string (const char *in,const char *delim); + +#endif
monitor/hmp-cmds.c +0 -19
@@ -38,25 +38,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)return false; } -/* -* Split @str at comma. -* A null @str defaults to "". -*/ -strList *hmp_split_at_comma (const char *str) -{ -char **split =g_strsplit (str ?:"" ,"," ,-1 ); -strList *res =NULL ; -strList **tail = &res; -int i; - -for (i =0 ; split[i]; i++) { -QAPI_LIST_APPEND (tail, split[i]); - } - -g_free (split); -return res; -} -void hmp_info_name (Monitor *mon,const QDict *qdict) {NameInfo *info;
net/net-hmp-cmds.c +2 -1
@@ -26,6 +26,7 @@#include "qemu/config-file.h" #include "qemu/help_option.h" #include "qemu/option.h" +#include "qemu/strList.h" void hmp_info_network (Monitor *mon,const QDict *qdict) {
@@ -72,7 +73,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict) migrate_announce_params());qapi_free_strList (params->interfaces); - params->interfaces =hmp_split_at_comma (interfaces_str); + params->interfaces =strList_from_string (interfaces_str,"," ); params->has_interfaces = params->interfaces !=NULL ; params->id =g_strdup (id);qmp_announce_self (params,NULL );
stats/stats-hmp-cmds.c +2 -1
@@ -10,6 +10,7 @@#include "monitor/hmp.h" #include "monitor/monitor.h" #include "qemu/cutils.h" +#include "qemu/strList.h" #include "hw/core/cpu.h" #include "qapi/qmp/qdict.h" #include "qapi/error.h"
@@ -176,7 +177,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names, request->provider = provider_idx;if (names && !g_str_equal (names,"*" )) { request->has_names = true; - request->names =hmp_split_at_comma (names); + request->names =strList_from_string (names,"," ); }QAPI_LIST_PREPEND (request_list, request); }
util/meson.build +1 -0
@@ -1,4 +1,5 @@ util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c')) +util_ss.add(files('strList.c')) util_ss.add(files('thread-context.c'), numa) if not config_host_data.get('CONFIG_ATOMIC64') util_ss.add(files('atomic64.c'))
util/strList.c new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2023 Red Hat, Inc. +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/strList.h" + +strList *strList_from_string (const char *str,const char *delim) +{ + g_autofree char **split = g_strsplit(str ?: "", delim, -1); +strList *res =NULL ; +strList **tail = &res; +int i; + +for (i =0 ; split[i]; i++) { +QAPI_LIST_APPEND (tail, split[i]); + } + +return res; +}
Signature · 1 line
1.8.3.1
Re: [PATCH V4 1/5] util: strList_from_string
I apologize for the lateness of my review.
Quoted 6 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstrList_from_string(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>
I can't see an actual use of generalized delimiters outside tests in this series. Do you have uses?
Quoted 42 lines
--- include/monitor/hmp.h | 1 - include/qemu/strList.h | 24 ++++++++++++++++++++++++ monitor/hmp-cmds.c | 19 ------------------- net/net-hmp-cmds.c | 3 ++- stats/stats-hmp-cmds.c | 3 ++- util/meson.build | 1 + util/strList.c | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 util/strList.cinclude/monitor/hmp.h +0
-1@@ -19,7 +19,6 @@bool hmp_handle_error (Monitor *mon,Error *err);void hmp_help_cmd (Monitor *mon,const char *name); -strList *hmp_split_at_comma (const char *str);void hmp_info_name (Monitor *mon,const QDict *qdict);void hmp_info_version (Monitor *mon,const QDict *qdict);include/qemu/strList.h new file +12
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_STR_LIST_H +#define QEMU_STR_LIST_H + +#include "qapi/qapi-builtin-types.h" +
include/qemu/strList.h line 22
Quoted 10 lines
include/qemu/strList.h new file +10
-0+/* +* Break @in into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @in is NULL or an empty string. +* A leading, trailing, or consecutive delimiter produces an +* empty string at that position in the output. +* All strings are g_strdup'd, and the result can be freed +* using qapi_free_strList. +*/ +strList *strList_from_string (const char *in,const char *delim);
The function name no longer tells us explicitly what the function does:
splitting the string.
`> +
Quoted 113 lines
include/qemu/strList.h new file +1
-0+#endif monitor/hmp-cmds.c +0
-19@@ -38,25 +38,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)return false; } -/* -* Split @str at comma. -* A null @str defaults to "". -*/ -strList *hmp_split_at_comma (const char *str) -{ -char **split =g_strsplit (str ?:"" ,"," ,-1 ); -strList *res =NULL ; -strList **tail = &res; -int i; - -for (i =0 ; split[i]; i++) { -QAPI_LIST_APPEND (tail, split[i]); - } - -g_free (split); -return res; -} -void hmp_info_name (Monitor *mon,const QDict *qdict) {NameInfo *info;net/net-hmp-cmds.c +2
-1@@ -26,6 +26,7 @@#include "qemu/config-file.h" #include "qemu/help_option.h" #include "qemu/option.h" +#include "qemu/strList.h" void hmp_info_network (Monitor *mon,const QDict *qdict) {@@ -72,7 +73,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict) migrate_announce_params());qapi_free_strList (params->interfaces); - params->interfaces =hmp_split_at_comma (interfaces_str); + params->interfaces =strList_from_string (interfaces_str,"," ); params->has_interfaces = params->interfaces !=NULL ; params->id =g_strdup (id);qmp_announce_self (params,NULL );stats/stats-hmp-cmds.c +2
-1@@ -10,6 +10,7 @@#include "monitor/hmp.h" #include "monitor/monitor.h" #include "qemu/cutils.h" +#include "qemu/strList.h" #include "hw/core/cpu.h" #include "qapi/qmp/qdict.h" #include "qapi/error.h" @@ -176,7 +177,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names, request->provider = provider_idx;if (names && !g_str_equal (names,"*" )) { request->has_names = true; - request->names =hmp_split_at_comma (names); + request->names =strList_from_string (names,"," ); }QAPI_LIST_PREPEND (request_list, request); }util/meson.build +1
-0@@ -1,4 +1,5 @@ util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c')) +util_ss.add(files('strList.c')) util_ss.add(files('thread-context.c'), numa) if not config_host_data.get('CONFIG_ATOMIC64') util_ss.add(files('atomic64.c'))util/strList.c new file +24
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2023 Red Hat, Inc. +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/strList.h" + +strList *strList_from_string (const char *str,const char *delim) +{ + g_autofree char **split = g_strsplit(str ?: "", delim, -1); +strList *res =NULL ; +strList **tail = &res; +int i; + +for (i =0 ; split[i]; i++) { +QAPI_LIST_APPEND (tail, split[i]); + } + +return res; +}
Re: [PATCH V4 1/5] util: strList_from_string
Quoted 1 line · Markus Armbruster
I apologize for the lateness of my review.
No problem. Thanks for the review.
Quoted 11 lines
Quoted 6 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstrList_from_string(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>I can't see an actual use of generalized delimiters outside tests in this series. Do you have uses?
In this series, it is called from hmp_announce_self and stats_filter; those were formerly calls to hmp_split_at_comma.
In my live update cpr-exec series, there is an additional call site, with a space delimiter instead of comma. Live update V9 is posted but is old and obsolete. I will post V10 soon, but I am hoping you can pull this series first, so I can whittle down my pending patches and omit these from V10.
Quoted 47 lines
Quoted 47 lines
--- include/monitor/hmp.h | 1 - include/qemu/strList.h | 24 ++++++++++++++++++++++++ monitor/hmp-cmds.c | 19 ------------------- net/net-hmp-cmds.c | 3 ++- stats/stats-hmp-cmds.c | 3 ++- util/meson.build | 1 + util/strList.c | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 util/strList.cinclude/monitor/hmp.h +0
-1@@ -19,7 +19,6 @@bool hmp_handle_error (Monitor *mon,Error *err);void hmp_help_cmd (Monitor *mon,const char *name); -strList *hmp_split_at_comma (const char *str);void hmp_info_name (Monitor *mon,const QDict *qdict);void hmp_info_version (Monitor *mon,const QDict *qdict);include/qemu/strList.h new file +17
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_STR_LIST_H +#define QEMU_STR_LIST_H + +#include "qapi/qapi-builtin-types.h" + +/* +* Break @in into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @in is NULL or an empty string. +* A leading, trailing, or consecutive delimiter produces an
Quoted 8 lines
include/qemu/strList.h line 22
Quoted 5 lines
include/qemu/strList.h new file +5
-0+* empty string at that position in the output. +* All strings are g_strdup'd, and the result can be freed +* using qapi_free_strList. +*/ +strList *strList_from_string (const char *in,const char *delim);The function name no longer tells us explicitly what the function does:
splitting the string.
The first sentence does not say it?
"Break @in into a strList using the delimiter string @delim"
Would you prefer this?
"Split string @in into a strList using the delimiter string @delim"
- Steve
Quoted 114 lines
Quoted 113 lines
include/qemu/strList.h new file +1
-0+#endif monitor/hmp-cmds.c +0
-19@@ -38,25 +38,6 @@ bool hmp_handle_error(Monitor *mon, Error *err)return false; } -/* -* Split @str at comma. -* A null @str defaults to "". -*/ -strList *hmp_split_at_comma (const char *str) -{ -char **split =g_strsplit (str ?:"" ,"," ,-1 ); -strList *res =NULL ; -strList **tail = &res; -int i; - -for (i =0 ; split[i]; i++) { -QAPI_LIST_APPEND (tail, split[i]); - } - -g_free (split); -return res; -} -void hmp_info_name (Monitor *mon,const QDict *qdict) {NameInfo *info;net/net-hmp-cmds.c +2
-1@@ -26,6 +26,7 @@#include "qemu/config-file.h" #include "qemu/help_option.h" #include "qemu/option.h" +#include "qemu/strList.h" void hmp_info_network (Monitor *mon,const QDict *qdict) {@@ -72,7 +73,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict) migrate_announce_params());qapi_free_strList (params->interfaces); - params->interfaces =hmp_split_at_comma (interfaces_str); + params->interfaces =strList_from_string (interfaces_str,"," ); params->has_interfaces = params->interfaces !=NULL ; params->id =g_strdup (id);qmp_announce_self (params,NULL );stats/stats-hmp-cmds.c +2
-1@@ -10,6 +10,7 @@#include "monitor/hmp.h" #include "monitor/monitor.h" #include "qemu/cutils.h" +#include "qemu/strList.h" #include "hw/core/cpu.h" #include "qapi/qmp/qdict.h" #include "qapi/error.h" @@ -176,7 +177,7 @@ static StatsFilter *stats_filter(StatsTarget target, const char *names, request->provider = provider_idx;if (names && !g_str_equal (names,"*" )) { request->has_names = true; - request->names =hmp_split_at_comma (names); + request->names =strList_from_string (names,"," ); }QAPI_LIST_PREPEND (request_list, request); }util/meson.build +1
-0@@ -1,4 +1,5 @@ util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c')) +util_ss.add(files('strList.c')) util_ss.add(files('thread-context.c'), numa) if not config_host_data.get('CONFIG_ATOMIC64') util_ss.add(files('atomic64.c'))util/strList.c new file +24
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2023 Red Hat, Inc. +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/strList.h" + +strList *strList_from_string (const char *str,const char *delim) +{ + g_autofree char **split = g_strsplit(str ?: "", delim, -1); +strList *res =NULL ; +strList **tail = &res; +int i; + +for (i =0 ; split[i]; i++) { +QAPI_LIST_APPEND (tail, split[i]); + } + +return res; +}
Re: [PATCH V4 1/5] util: strList_from_string
Quoted 81 lines · Steven Sistare
Quoted 1 line · Markus Armbruster
I apologize for the lateness of my review.
No problem. Thanks for the review.
Quoted 11 lines
Quoted 6 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstrList_from_string(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>I can't see an actual use of generalized delimiters outside tests in this series. Do you have uses?
In this series, it is called from
hmp_announce_selfandstats_filter; those were formerly calls tohmp_split_at_comma.In my live update cpr-exec series, there is an additional call site, with a space delimiter instead of comma. Live update V9 is posted but is old and obsolete. I will post V10 soon, but I am hoping you can pull this series first, so I can whittle down my pending patches and omit these from V10.
Quoted 47 lines
Quoted 47 lines
--- include/monitor/hmp.h | 1 - include/qemu/strList.h | 24 ++++++++++++++++++++++++ monitor/hmp-cmds.c | 19 ------------------- net/net-hmp-cmds.c | 3 ++- stats/stats-hmp-cmds.c | 3 ++- util/meson.build | 1 + util/strList.c | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 include/qemu/strList.h create mode 100644 util/strList.cinclude/monitor/hmp.h +0
-1@@ -19,7 +19,6 @@bool hmp_handle_error (Monitor *mon,Error *err);void hmp_help_cmd (Monitor *mon,const char *name); -strList *hmp_split_at_comma (const char *str);void hmp_info_name (Monitor *mon,const QDict *qdict);void hmp_info_version (Monitor *mon,const QDict *qdict);include/qemu/strList.h new file +17
-0@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_STR_LIST_H +#define QEMU_STR_LIST_H + +#include "qapi/qapi-builtin-types.h" + +/* +* Break @in into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @in is NULL or an empty string. +* A leading, trailing, or consecutive delimiter produces an Quoted 8 lines
include/qemu/strList.h line 22
Quoted 5 lines
include/qemu/strList.h new file +5
-0+* empty string at that position in the output. +* All strings are g_strdup'd, and the result can be freed +* using qapi_free_strList. +*/ +strList *strList_from_string (const char *in,const char *delim);The function name no longer tells us explicitly what the function does:
splitting the string.
Quoted 5 lines
The first sentence does not say it?
"Break @in into a strList using the delimiter string @delim"Would you prefer this?
"Split string @in into a strList using the delimiter string @delim"
Sorry, I read your comment too quickly. You want a different function name. I propose:
strList *str_split(const char *str, const char *delim);
- Steve
PATCH v4 2/5 qapi: QAPI_LIST_LENGTH
In reply to Steve Sistare--- include/qapi/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/qapi/util.h +13 -0
@@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete); (tail) = &(*(tail))->next; \ } while (0) +/* +* For any GenericList @list, return its length. + */ +#define QAPI_LIST_LENGTH (list) \ + ({ \ + int len = 0; \ + typeof(list) elem; \ + for (elem = list; elem != NULL; elem = elem->next) { \ + len++; \ + } \ + len; \ + }) +#endif
Signature · 1 line
1.8.3.1
Re: [PATCH V4 2/5] qapi: QAPI_LIST_LENGTH
Quoted 11 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- include/qapi/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)include/qapi/util.h +0
-0
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 64
Quoted 9 lines
include/qapi/util.h +6
-0@@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete); (tail) = &(*(tail))->next; \ } while (0) +/* + * For any GenericList @list, return its length. + */ +#define QAPI_LIST_LENGTH(list) \ + ({ \ + int len = 0; \
size_t
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 65
Quoted 1 line
include/qapi/util.h +1
-0+typeof (list) elem; \
Name this @tail, please.
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 72
Quoted 7 lines
include/qapi/util.h +6
-0+for (elem = list; elem !=NULL ; elem = elem->next) { \ + len++; \ + } \ + len; \ + }) +#endif
This is a macro instead of a function so users don't have to cast their FooList * to GenericList *.
The only user outside tests is strv_from_strList(). I'd be tempted to open-code it there and call it a day. Or do you have more users in mind?
If we keep the macro, please align the backslashes like this:
#define QAPI_LIST_LENGTH(list) \
({ \
int len = 0; \
typeof(list) elem; \
for (elem = list; elem != NULL; elem = elem->next) { \
len++; \
} \
len; \
})Re: [PATCH V4 2/5] qapi: QAPI_LIST_LENGTH
Quoted 16 lines · Markus Armbruster
Quoted 14 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- include/qapi/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)include/qapi/util.h +0
-0@@ -56,4 +56,17 @@ int parse_qapi_name(const char *name, bool complete); (tail) = &(*(tail))->next; \ }while (0 )
Quoted 8 lines
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 64
Quoted 6 lines
include/qapi/util.h +6
-0+/* +* For any GenericList @list, return its length. +*/ +#define QAPI_LIST_LENGTH (list) \ + ({ \ + int len = 0; \
size_t
ok.
Quoted 3 lines
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 65
Quoted 1 line
include/qapi/util.h +1
-0+typeof (list) elem; \Name this @tail, please.
ok.
Quoted 8 lines
include/qapi/util.h int parse_qapi_name(const char *name, bool complete); · line 72
Quoted 7 lines
include/qapi/util.h +6
-0+for (elem = list; elem !=NULL ; elem = elem->next) { \ + len++; \ + } \ + len; \ + }) +#endif
Quoted 6 lines
This is a macro instead of a function so users don't have to cast their FooList * to GenericList *.
The only user outside tests is
strv_from_strList(). I'd be tempted to open-code it there and call it a day. Or do you have more users in mind?
That's the only use. If I make it private, I would still define it as a static subroutine in util/strList.c, because it simplifies strv_from_strList. IMO providing a public list length function or macro is pretty basic, but I am not wedded to it. Your call.
- Steve
Quoted 12 lines
If we keep the macro, please align the backslashes like this:
#define QAPI_LIST_LENGTH(list) \ ({ \ int len = 0; \ typeof(list) elem; \ for (elem = list; elem != NULL; elem = elem->next) { \ len++; \ } \ len; \ })
PATCH v4 3/5 util: strv_from_strList
In reply to Steve Sistare--- include/qemu/strList.h | 6 ++++++ util/strList.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)
include/qemu/strList.h +6 -0
@@ -21,4 +21,10 @@*/ strList *strList_from_string (const char *in,const char *delim); +/* +* Produce and return a NULL-terminated array of strings from @args. +* The result is g_malloc'd and all strings are g_strdup'd. +*/ +GStrv strv_from_strList (const strList *args); +#endif
util/strList.c +14 -0
@@ -22,3 +22,17 @@ strList *strList_from_string(const char *str, const char *delim)return res; } + +GStrv strv_from_strList (const strList *args) +{ +const strList *arg; +int i =0 ; + GStrv argv = g_new(char *, QAPI_LIST_LENGTH(args) + 1); + +for (arg = args; arg !=NULL ; arg = arg->next) { + argv[i++] =g_strdup (arg->value); + } + argv[i] =NULL ; + +return argv; +}
Signature · 1 line
1.8.3.1
Re: [PATCH V4 3/5] util: strv_from_strList
Quoted 27 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- include/qemu/strList.h | 6 ++++++ util/strList.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)include/qemu/strList.h +6
-0@@ -21,4 +21,10 @@*/ strList *strList_from_string (const char *in,const char *delim); +/* +* Produce and return a NULL-terminated array of strings from @args. +* The result is g_malloc'd and all strings are g_strdup'd. +*/ +GStrv strv_from_strList (const strList *args); +#endif util/strList.c +0
-0
util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 26
Quoted 5 lines
util/strList.c +2
-0@@ -22,3 +22,17 @@ strList *strList_from_string(const char *str, const char *delim)return res; } + +GStrv strv_from_strList (const strList *args)
Suggest to name the argument @list.
util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 28
Quoted 2 lines
util/strList.c +2
-0+{ +const strList *arg;
Suggest to name this @tail.
util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 38
Quoted 10 lines
util/strList.c +10
-0+int i =0 ; + GStrv argv = g_new(char *, QAPI_LIST_LENGTH(args) + 1); + +for (arg = args; arg !=NULL ; arg = arg->next) { + argv[i++] =g_strdup (arg->value); + } + argv[i] =NULL ; + +return argv; +}
Can we use char ** instread of GStrv? I'd find that clearer. For what it's worth, GLib documentation of functions like g_strsplit() doesn't use the GStrv typedef, either.
Re: [PATCH V4 3/5] util: strv_from_strList
Quoted 37 lines · Markus Armbruster
Quoted 27 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- include/qemu/strList.h | 6 ++++++ util/strList.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+)include/qemu/strList.h +6
-0@@ -21,4 +21,10 @@*/ strList *strList_from_string (const char *in,const char *delim); +/* +* Produce and return a NULL-terminated array of strings from @args. +* The result is g_malloc'd and all strings are g_strdup'd. +*/ +GStrv strv_from_strList (const strList *args); +#endif util/strList.c +0
-0util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 26
Quoted 5 lines
util/strList.c +2
-0@@ -22,3 +22,17 @@ strList *strList_from_string(const char *str, const char *delim)return res; } + +GStrv strv_from_strList (const strList *args)Suggest to name the argument @list.
Quoted 4 lines
util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 28
Quoted 2 lines
util/strList.c +2
-0+{ +const strList *arg;Suggest to name this @tail.
ok.
Quoted 6 lines
Quoted 6 lines
util/strList.c +6
-0+int i =0 ; + GStrv argv = g_new(char *, QAPI_LIST_LENGTH(args) + 1); + +for (arg = args; arg !=NULL ; arg = arg->next) { + argv[i++] =g_strdup (arg->value); + }
Quoted 8 lines
util/strList.c strList *strList_from_string(const char *str, const char *delim) · line 38
Quoted 4 lines
util/strList.c +4
-0+ argv[i] =NULL ; + +return argv; +}Can we use char ** instread of GStrv? I'd find that clearer. For what it's worth, GLib documentation of functions like
g_strsplit()doesn't use the GStrv typedef, either.
ok.
- Steve
PATCH v4 4/5 util: strList unit tests
In reply to Steve Sistare--- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/unit/test-strList.c
tests/unit/meson.build +1 -0
@@ -35,6 +35,7 @@ tests = { 'test-rcu-simpleq': [], 'test-rcu-tailq': [], 'test-rcu-slist': [], + 'test-strList': [], 'test-qdist': [], 'test-qht': [], 'test-qtree': [],
tests/unit/test-strList.c new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH V4 4/5] util: strList unit tests
Quoted 67 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/unit/test-strList.ctests/unit/meson.build +1
-0@@ -35,6 +35,7 @@ tests = { 'test-rcu-simpleq': [], 'test-rcu-tailq': [], 'test-rcu-slist': [], + 'test-strList': [], 'test-qdist': [], 'test-qht': [], 'test-qtree': [],tests/unit/test-strList.c new file +41
-0@@ -0,0 +1,80 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/strList.h" + +static strList *make_list (int length) +{ +strList *head =0 , *list, **prev = &head; + +while (length--) { + list = *prev =g_new0 (strList,1 ); + list->value =g_strdup ("aaa" ); + prev = &list->next; + } +return head; +} + +static void test_length (void ) +{ +strList *list; +int i; + +for (i =0 ; i <5 ; i++) { + list =make_list (i); + g_assert_cmpint(i, ==, QAPI_LIST_LENGTH(list)); +qapi_free_strList (list); + } +} + +struct { +const char *string; +const char *delim; +const char *args[5 ]; +} list_data[] = { + {0 ,"," , {0 } }, + {"" ,"," , {0 } },
tests/unit/test-strList.c line 51
Quoted 10 lines
tests/unit/test-strList.c new file +10
-0+ { "a", ",", { "a", 0 } }, + { "a,b", ",", { "a", "b", 0 } }, + { "a,b,c", ",", { "a", "b", "c", 0 } }, + { "first last", " ", { "first", "last", 0 } }, + { "a:", ":", { "a", "", 0 } }, + { "a::b", ":", { "a", "", "b", 0 } }, + { ":", ":", { "", "", 0 } }, + { ":a", ":", { "", "a", 0 } }, + { "::a", ":", { "", "", "a", 0 } }, +};
NULL instead of 0, please.
tests/unit/test-strList.c line 58
Quoted 7 lines
tests/unit/test-strList.c new file +7
-0+ +static void test_strv (void ) +{ +int i, j; +const char **expect; +strList *list; +GStrv args;
I'd prefer char **argv;
tests/unit/test-strList.c line 65
Quoted 7 lines
tests/unit/test-strList.c new file +7
-0+ +for (i =0 ; i <ARRAY_SIZE (list_data); i++) { + expect = list_data[i].args; + list =strList_from_string (list_data[i].string, list_data[i].delim); + args =strv_from_strList (list); +qapi_free_strList (list); +for (j =0 ; expect[j] && args[j]; j++) {
Loop stops when either array element is null. That's wrong, we need to exhaust both arrays: || instead of &&.
Quoted 15 lines
tests/unit/test-strList.c new file +15
-0+ g_assert_cmpstr(expect[j], ==, args[j]); + } +g_assert_null (expect[j]); +g_assert_null (args[j]); +g_strfreev (args); + } +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); +g_test_add_func ("/test-string/length" , test_length); +g_test_add_func ("/test-string/strv" , test_strv); +return g_test_run (); +}
Re: [PATCH V4 4/5] util: strList unit tests
Quoted 73 lines · Markus Armbruster
Quoted 71 lines · Steve Sistare
Signed-off-by Steve Sistare <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>--- tests/unit/meson.build | 1 + tests/unit/test-strList.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/unit/test-strList.ctests/unit/meson.build +1
-0@@ -35,6 +35,7 @@ tests = { 'test-rcu-simpleq': [], 'test-rcu-tailq': [], 'test-rcu-slist': [], + 'test-strList': [], 'test-qdist': [], 'test-qht': [], 'test-qtree': [],tests/unit/test-strList.c new file +45
-0@@ -0,0 +1,80 @@ +/* +* Copyright (c) 2022 - 2024 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/strList.h" + +static strList *make_list (int length) +{ +strList *head =0 , *list, **prev = &head; + +while (length--) { + list = *prev =g_new0 (strList,1 ); + list->value =g_strdup ("aaa" ); + prev = &list->next; + } +return head; +} + +static void test_length (void ) +{ +strList *list; +int i; + +for (i =0 ; i <5 ; i++) { + list =make_list (i); + g_assert_cmpint(i, ==, QAPI_LIST_LENGTH(list)); +qapi_free_strList (list); + } +} + +struct { +const char *string; +const char *delim; +const char *args[5 ]; +} list_data[] = { + {0 ,"," , {0 } }, + {"" ,"," , {0 } }, + {"a" ,"," , {"a" ,0 } }, + {"a,b" ,"," , {"a" ,"b" ,0 } }, + {"a,b,c" ,"," , {"a" ,"b" ,"c" ,0 } }, + {"first last" ," " , {"first" ,"last" ,0 } },
Quoted 8 lines
tests/unit/test-strList.c line 51
Quoted 6 lines
tests/unit/test-strList.c new file +6
-0+ { "a:", ":", { "a", "", 0 } }, + { "a::b", ":", { "a", "", "b", 0 } }, + { ":", ":", { "", "", 0 } }, + { ":a", ":", { "", "a", 0 } }, + { "::a", ":", { "", "", "a", 0 } }, +};NULL instead of 0, please.
ok.
Quoted 9 lines
tests/unit/test-strList.c line 58
Quoted 7 lines
tests/unit/test-strList.c new file +7
-0+ +static void test_strv (void ) +{ +int i, j; +const char **expect; +strList *list; +GStrv args;I'd prefer char **argv;
ok.
Quoted 10 lines
tests/unit/test-strList.c line 65
Quoted 7 lines
tests/unit/test-strList.c new file +7
-0+ +for (i =0 ; i <ARRAY_SIZE (list_data); i++) { + expect = list_data[i].args; + list =strList_from_string (list_data[i].string, list_data[i].delim); + args =strv_from_strList (list); +qapi_free_strList (list); +for (j =0 ; expect[j] && args[j]; j++) {Loop stops when either array element is null. That's wrong, we need to exhaust both arrays: || instead of &&.
|| is not safe. After one array is exhausted, [j] will be out of bounds for the other. The g_assert_null calls guarantee the arrays are the same length
and all elements have been compared.
- Steve
Quoted 16 lines
Quoted 15 lines
tests/unit/test-strList.c new file +15
-0+ g_assert_cmpstr(expect[j], ==, args[j]); + } +g_assert_null (expect[j]); +g_assert_null (args[j]); +g_strfreev (args); + } +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); +g_test_add_func ("/test-string/length" , test_length); +g_test_add_func ("/test-string/strv" , test_strv); +return g_test_run (); +}
PATCH v4 5/5 migration: simplify exec migration functions
In reply to Steve SistareSimplify the exec migration code by using list utility functions.
As a side effect, this also fixes a minor memory leak. On function return, "g_auto(GStrv) argv" frees argv and each element, which is wrong, because the function does not own the individual elements. To compensate, the code uses g_steal_pointer which NULLs argv and prevents the destructor from running, but argv is leaked.
--- migration/exec.c | 58 ++++++++------------------------------------------------ 1 file changed, 8 insertions(+), 50 deletions(-)
migration/exec.c +8 -50
@@ -19,12 +19,12 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/strList.h" #include "channel.h" #include "exec.h" #include "migration.h" #include "io/channel-command.h" #include "trace.h" -#include "qemu/cutils.h" #ifdef WIN32 const char *exec_get_cmd_path (void )
@@ -39,51 +39,16 @@ const char *exec_get_cmd_path(void) }#endif -/* provides the length of strList */ -static int -str_list_length (strList *list) -{ -int len =0 ; -strList *elem; - -for (elem = list; elem !=NULL ; elem = elem->next) { - len++; - } - -return len; -} - -static void -init_exec_array (strList *command,char **argv,Error **errp) -{ -int i =0 ; -strList *lst; - -for (lst = command; lst; lst = lst->next) { - argv[i++] = lst->value; - } - - argv[i] =NULL ; -return ; -} -void exec_start_outgoing_migration (MigrationState *s,strList *command,Error **errp) { -QIOChannel *ioc; - -int length =str_list_length (command); - g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); - -init_exec_array (command, argv, errp); +QIOChannel *ioc =NULL ; +g_auto (GStrv) argv =strv_from_strList (command); +const char *const *args = (const char *const *) argv; g_autofree char *new_command = g_strjoinv(" ", (char **)argv);trace_migration_exec_outgoing (new_command); - ioc =QIO_CHANNEL ( -qio_channel_command_new_spawn ( - (const char *const *)g_steal_pointer (&argv), -O_RDWR , - errp)); + ioc =QIO_CHANNEL (qio_channel_command_new_spawn (args,O_RDWR , errp));if (!ioc) {return ; }
@@ -105,19 +70,12 @@ static gboolean exec_accept_incoming_migration(QIOChannel *ioc,void exec_start_incoming_migration (strList *command,Error **errp) {QIOChannel *ioc; - -int length =str_list_length (command); - g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); - -init_exec_array (command, argv, errp); +g_auto (GStrv) argv =strv_from_strList (command); +const char *const *args = (const char *const *) argv; g_autofree char *new_command = g_strjoinv(" ", (char **)argv);trace_migration_exec_incoming (new_command); - ioc =QIO_CHANNEL ( -qio_channel_command_new_spawn ( - (const char *const *)g_steal_pointer (&argv), -O_RDWR , - errp)); + ioc =QIO_CHANNEL (qio_channel_command_new_spawn (args,O_RDWR , errp));if (!ioc) {return ; }
Signature · 1 line
1.8.3.1
Re: [PATCH V4 5/5] migration: simplify exec migration functions
Het Gala, Peter, or Fabiano, please review.
Quoted 111 lines · Steve Sistare
Simplify the exec migration code by using list utility functions.
As a side effect, this also fixes a minor memory leak. On function return, "
g_auto(GStrv)argv" frees argv and each element, which is wrong, because the function does not own the individual elements. To compensate, the code usesg_steal_pointerwhich NULLs argv and prevents the destructor from running, but argv is leaked.Fixes cbab4face57b ("migration: convert exec backend ...")Signed-off-by Steve Sistare <[email protected]>--- migration/exec.c | 58 ++++++++------------------------------------------------ 1 file changed, 8 insertions(+), 50 deletions(-)migration/exec.c +8
-50@@ -19,12 +19,12 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/strList.h" #include "channel.h" #include "exec.h" #include "migration.h" #include "io/channel-command.h" #include "trace.h" -#include "qemu/cutils.h" #ifdef WIN32 const char *exec_get_cmd_path (void )@@ -39,51 +39,16 @@ const char *exec_get_cmd_path(void) }#endif -/* provides the length of strList */ -static int -str_list_length (strList *list) -{ -int len =0 ; -strList *elem; - -for (elem = list; elem !=NULL ; elem = elem->next) { - len++; - } - -return len; -} - -static void -init_exec_array (strList *command,char **argv,Error **errp) -{ -int i =0 ; -strList *lst; - -for (lst = command; lst; lst = lst->next) { - argv[i++] = lst->value; - } - - argv[i] =NULL ; -return ; -} -void exec_start_outgoing_migration (MigrationState *s,strList *command,Error **errp) { -QIOChannel *ioc; - -int length =str_list_length (command); - g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); - -init_exec_array (command, argv, errp); +QIOChannel *ioc =NULL ; +g_auto (GStrv) argv =strv_from_strList (command); +const char *const *args = (const char *const *) argv; g_autofree char *new_command = g_strjoinv(" ", (char **)argv);trace_migration_exec_outgoing (new_command); - ioc =QIO_CHANNEL ( -qio_channel_command_new_spawn ( - (const char *const *)g_steal_pointer (&argv), -O_RDWR , - errp)); + ioc =QIO_CHANNEL (qio_channel_command_new_spawn (args,O_RDWR , errp));if (!ioc) {return ; }@@ -105,19 +70,12 @@ static gboolean exec_accept_incoming_migration(QIOChannel *ioc,void exec_start_incoming_migration (strList *command,Error **errp) {QIOChannel *ioc; - -int length =str_list_length (command); - g_auto(GStrv) argv = (char **) g_new0(const char *, length + 1); - -init_exec_array (command, argv, errp); +g_auto (GStrv) argv =strv_from_strList (command); +const char *const *args = (const char *const *) argv; g_autofree char *new_command = g_strjoinv(" ", (char **)argv);trace_migration_exec_incoming (new_command); - ioc =QIO_CHANNEL ( -qio_channel_command_new_spawn ( - (const char *const *)g_steal_pointer (&argv), -O_RDWR , - errp)); + ioc =QIO_CHANNEL (qio_channel_command_new_spawn (args,O_RDWR , errp));if (!ioc) {return ; }
Re: [PATCH V4 5/5] migration: simplify exec migration functions
In reply to Steve SistareQuoted 10 lines · Steve Sistare
Simplify the exec migration code by using list utility functions.
As a side effect, this also fixes a minor memory leak. On function return, "
g_auto(GStrv)argv" frees argv and each element, which is wrong, because the function does not own the individual elements. To compensate, the code usesg_steal_pointerwhich NULLs argv and prevents the destructor from running, but argv is leaked.Fixes cbab4face57b ("migration: convert exec backend ...")Signed-off-by Steve Sistare <[email protected]>
Re: [PATCH V4 5/5] migration: simplify exec migration functions
Quoted 10 lines · Fabiano Rosas
Quoted 8 lines · Steve Sistare
Simplify the exec migration code by using list utility functions.
As a side effect, this also fixes a minor memory leak. On function return, "
g_auto(GStrv)argv" frees argv and each element, which is wrong, because the function does not own the individual elements. To compensate, the code usesg_steal_pointerwhich NULLs argv and prevents the destructor from running, but argv is leaked.
Quoted 4 lines
Quoted 2 lines
Fixes cbab4face57b ("migration: convert exec backend ...")Signed-off-by Steve Sistare <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>
You'll have to reintroduce the qemu/cutils.h include:
../migration/exec.c: In function 'exec_get_cmd_path':../migration/exec.c:37:5: error: implicit declaration of function 'pstrcat'; did you mean 'strcat'? [-Werror=implicit-function-declaration]
37 | pstrcat(detected_path, MAX_PATH, "\\cmd.exe");
| ^~~~~~~
| strcat../migration/exec.c:37:5: error: nested extern declaration of 'pstrcat' [-Werror=nested-externs]
Re: [PATCH V4 5/5] migration: simplify exec migration functions
Quoted 17 lines · Fabiano Rosas
Quoted 10 lines · Fabiano Rosas
Quoted 8 lines · Steve Sistare
Simplify the exec migration code by using list utility functions.
As a side effect, this also fixes a minor memory leak. On function return, "
g_auto(GStrv)argv" frees argv and each element, which is wrong, because the function does not own the individual elements. To compensate, the code usesg_steal_pointerwhich NULLs argv and prevents the destructor from running, but argv is leaked.Quoted 4 lines
Quoted 2 lines
Fixes cbab4face57b ("migration: convert exec backend ...")Signed-off-by Steve Sistare <[email protected]>Reviewed-by Fabiano Rosas <[email protected]>
Quoted 8 lines
You'll have to reintroduce the
qemu/cutils.hinclude:
../migration/exec.c: In function 'exec_get_cmd_path':../migration/exec.c:37:5: error: implicit declaration of function 'pstrcat'; did you mean 'strcat'? [-Werror=implicit-function-declaration]37 | pstrcat(detected_path, MAX_PATH, "\\cmd.exe"); | ^~~~~~~ | strcat
../migration/exec.c:37:5: error: nested extern declaration of 'pstrcat' [-Werror=nested-externs]
Thanks, I will rebase to the tip and verify all is well before I post V5.
- Steve