From 4e1034f2699881431dbd65b0d4d1af6131a60302 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 21 Nov 2017 16:02:00 +0100 Subject: [PATCH 1/1] Use a std::vector for process arguments. --- src/simix/smx_deployment.cpp | 8 ++------ src/surf/sg_platf.cpp | 8 ++++---- src/surf/xml/platf_private.hpp | 3 +-- src/surf/xml/surfxml_sax_cb.cpp | 19 ++++--------------- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index f380ffe0c9..18bf778ded 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -123,17 +123,13 @@ void SIMIX_process_set_function(const char* process_host, const char* process_fu if (not host) THROWF(arg_error, 0, "Host '%s' unknown", process_host); process.host = process_host; - - process.argc = 1 + xbt_dynar_length(arguments); - process.argv = static_cast(xbt_new(const char*, process.argc + 1)); - process.argv[0] = xbt_strdup(process_function); + process.args.push_back(process_function); /* add arguments */ unsigned int i; char *arg; xbt_dynar_foreach(arguments, i, arg) { - process.argv[i + 1] = xbt_strdup(arg); + process.args.push_back(arg); } - process.argv[process.argc] = nullptr; // Check we know how to handle this function name: simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function); diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index b22b8b89a7..81a15d0189 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -440,14 +440,14 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) double kill_time = process->kill_time; int auto_restart = process->on_failure == SURF_ACTOR_ON_FAILURE_DIE ? 0 : 1; - std::vector args(process->argv, process->argv + process->argc); - std::function code = factory(std::move(args)); + std::string process_name = process->args[0]; + std::function code = factory(std::move(process->args)); std::shared_ptr> properties(process->properties); smx_process_arg_t arg = nullptr; arg = new simgrid::simix::ProcessArg(); - arg->name = std::string(process->argv[0]); + arg->name = process_name; arg->code = code; arg->data = nullptr; arg->host = host; @@ -459,7 +459,7 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) if (start_time > SIMIX_get_clock()) { arg = new simgrid::simix::ProcessArg(); - arg->name = std::string(process->argv[0]); + arg->name = process_name; arg->code = std::move(code); arg->data = nullptr; arg->host = host; diff --git a/src/surf/xml/platf_private.hpp b/src/surf/xml/platf_private.hpp index 82def2f21a..13b3012cff 100644 --- a/src/surf/xml/platf_private.hpp +++ b/src/surf/xml/platf_private.hpp @@ -174,8 +174,7 @@ public: }; struct s_sg_platf_process_cbarg_t { - const char** argv = nullptr; - int argc = 0; + std::vector args; std::map* properties = nullptr; const char* host = nullptr; const char* function = nullptr; diff --git a/src/surf/xml/surfxml_sax_cb.cpp b/src/surf/xml/surfxml_sax_cb.cpp index 67f14a4432..3d6ead11d1 100644 --- a/src/surf/xml/surfxml_sax_cb.cpp +++ b/src/surf/xml/surfxml_sax_cb.cpp @@ -877,8 +877,7 @@ void ETag_surfxml_config() current_property_set = nullptr; } -static int argc; -static char **argv; +static std::vector arguments; void STag_surfxml_process() { @@ -889,9 +888,7 @@ void STag_surfxml_process() void STag_surfxml_actor() { ZONE_TAG = 0; - argc = 1; - argv = xbt_new(char *, 1); - argv[0] = xbt_strdup(A_surfxml_actor_function); + arguments.assign(1, A_surfxml_actor_function); xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)"); } @@ -912,8 +909,7 @@ void ETag_surfxml_actor() actor.properties = current_property_set; current_property_set = nullptr; - actor.argc = argc; - actor.argv = (const char **)argv; + actor.args.swap(arguments); actor.host = A_surfxml_actor_host; actor.function = A_surfxml_actor_function; actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time); @@ -933,17 +929,10 @@ void ETag_surfxml_actor() } sg_platf_new_process(&actor); - - for (int i = 0; i != argc; ++i) - xbt_free(argv[i]); - xbt_free(argv); - argv = nullptr; } void STag_surfxml_argument(){ - argc++; - argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **)); - argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value); + arguments.push_back(A_surfxml_argument_value); } void STag_surfxml_model___prop(){ -- 2.20.1