string list functions
Subsystems (2)
v5 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 V5:
* renamed some variables and one function, replaced GStrv with char **
* aligned backslashes in QAPI_LIST_LENGTH
* restored cutils.h to exec.c
Changes in V4:
* added exec migration patch
Steve Sistare (5):
util: str_split
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 | 57 +++++---------------------------- 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(+), 71 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 v5 1/5 util: str_split
Generalize hmp_split_at_comma() to take any delimiter string, rename as str_split(), 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" + +/* +* Split @str into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @str 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 *str_split (const char *str,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 =str_split (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 =str_split (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 *str_split (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 V5 1/5] util: str_split
Quoted 15 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>--- 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(-)
Quoted 2 lines
create mode 100644
include/qemu/strList.h
create mode 100644util/strList.c
Re: [PATCH V5 1/5] util: str_split
In reply to Steve SistareQuoted 19 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>--- 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 100644util/strList.c
include/qemu/strList.h line 20
Quoted 10 lines
include/qemu/strList.h new file +10
-0+#include "qapi/qapi-builtin-types.h" + +/* +* Split @str into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @str 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.
Note "qapi/qapi-builtin-types.h" defines:
G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)
Maybe mention we can also use:
g_autoptr(strList)
?
Quoted 4 lines
+ */
include/qemu/strList.h new file +3
-0+strList *str_split (const char *str,const char *delim); + +#endif
Re: [PATCH V5 1/5] util: str_split
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>---
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 100644util/strList.cinclude/qemu/strList.h line 20
Quoted 10 lines
include/qemu/strList.h new file +10
-0+#include "qapi/qapi-builtin-types.h" + +/* +* Split @str into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @str 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. Note "
qapi/qapi-builtin-types.h" defines:
Quoted 5 lines
G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)Maybe mention we can also use:
g_autoptr(strList)
Thanks Philippe. If we get to V6 for this series, I will mention this, and also mention g_autoptr(GStrv) in the header comment for strv_from_strlist.
- Steve
Quoted 5 lines
Quoted 4 lines
+ */
include/qemu/strList.h new file +3
-0+strList *str_split (const char *str,const char *delim); + +#endif
Re: [PATCH V5 1/5] util: str_split
Quoted 34 lines · Steven Sistare
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>--- 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 100644util/strList.cinclude/qemu/strList.h line 20
Quoted 10 lines
include/qemu/strList.h new file +10
-0+#include "qapi/qapi-builtin-types.h" + +/* +* Split @str into a strList using the delimiter string @delim. +* The delimiter is not included in the result. +* Return NULL if @str 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. Note "
qapi/qapi-builtin-types.h" defines:
Quoted 8 lines
Quoted 5 lines
G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)Maybe mention we can also use:
g_autoptr(strList)Thanks Philippe. If we get to V6 for this series, I will mention this, and also mention
g_autoptr(GStrv)in the header comment forstrv_from_strlist.
If there is no need for v6, do you mind sharing here what would be the resulting comment? Maybe Markus can update it directly... (assuming he takes your series).
Re: [PATCH V5 1/5] util: str_split
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Steven Sistare
Quoted 37 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>---
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 100644util/strList.cReview
Quoted 10 lines
(quoted diff) +10
-0+#include "qapi/qapi-builtin-types.h" + +/* + * Split @str into a strList using the delimiter string @delim. + * The delimiter is not included in the result. + * Return NULL if @str 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.Note "
qapi/qapi-builtin-types.h" defines:
G_DEFINE_AUTOPTR_CLEANUP_FUNC(strList, qapi_free_strList)Maybe mention we can also use:
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
g_autoptr(strList)Thanks Philippe. If we get to V6 for this series, I will mention this, and also mention
g_autoptr(GStrv)in the header comment forstrv_from_strlist.If there is no need for v6, do you mind sharing here what would be the resulting comment? Maybe Markus can update it directly... (assuming he takes your series).
Sure - steve
--------------------
include/qemu/strList.h +5 -2
@@ -17,13 +17,16 @@* 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. +* using qapi_free_strList, or by declaring a local variable +* with g_autoptr(strList). */ strList *str_split (const char *str,const char *delim);/* * Produce and return a NULL-terminated array of strings from @list. -* The result is g_malloc'd and all strings are g_strdup'd. +* The result is g_malloc'd and all strings are g_strdup'd. The result +* can be freed using g_strfreev, or by declaring a local variable with +* g_auto(GStrv). */ char **strv_from_strList (const strList *list);
--------------------
Re: [PATCH V5 1/5] util: str_split
In reply to Steve SistareQuoted 16 lines · Steve Sistare
Generalize
hmp_split_at_comma()to take any delimiter string, rename asstr_split(), and move it toutil/strList.c.No functional change.
Signed-off-by Steve Sistare <[email protected]>--- 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(-)
Quoted 2 lines
create mode 100644
include/qemu/strList.h
create mode 100644util/strList.c
Markus, is that OK to include these two in your QAPI section in MAINTAINERS? Squashing:
-- >8 --
MAINTAINERS +2 -0
@@ -3196,6 +3196,8 @@ X: qapi/*.json F: include/qapi/ X: include/qapi/qmp/ F: include/qapi/qmp/dispatch.h +F: include/qemu/strList.h +F: util/strList.c F: tests/qapi-schema/ F: tests/unit/test-*-visitor.c F: tests/unit/test-qapi-*.c
---
PATCH v5 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) \ + ({ \ + size_t _len = 0; \ + typeof(list) _tail; \ + for (_tail = list; _tail != NULL; _tail = _tail->next) { \ + _len++; \ + } \ + _len; \ + }) +#endif
Signature · 1 line
1.8.3.1
PATCH v5 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 *str_split (const char *str,const char *delim); +/* +* Produce and return a NULL-terminated array of strings from @list. +* The result is g_malloc'd and all strings are g_strdup'd. +*/ +char **strv_from_strList (const strList *list); +#endif
util/strList.c +14 -0
@@ -22,3 +22,17 @@ strList *str_split(const char *str, const char *delim)return res; } + +char **strv_from_strList (const strList *list) +{ +const strList *tail; +int i =0 ; + char **argv = g_new(char *, QAPI_LIST_LENGTH(list) + 1); + +for (tail = list; tail !=NULL ; tail = tail->next) { + argv[i++] =g_strdup (tail->value); + } + argv[i] =NULL ; + +return argv; +}
Signature · 1 line
1.8.3.1
PATCH v5 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
PATCH v5 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 | 57 ++++++++------------------------------------------------ 1 file changed, 8 insertions(+), 49 deletions(-)
migration/exec.c +8 -49
@@ -19,6 +19,7 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/strList.h" #include "channel.h" #include "exec.h" #include "migration.h"
@@ -39,51 +40,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 +71,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