From fa40abdc1d0705869a181ee3f2e9be45325d4e25 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 20 Nov 2020 21:43:47 +0100 Subject: [PATCH] Use more std::string. --- src/mc/Session.cpp | 6 ++--- src/mc/remote/RemoteSimulation.cpp | 11 +++----- src/smpi/bindings/smpi_f77.cpp | 42 +++++++++-------------------- src/smpi/bindings/smpi_f77_comm.cpp | 9 +++---- src/smpi/bindings/smpi_f77_type.cpp | 12 ++++----- 5 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/mc/Session.cpp b/src/mc/Session.cpp index d29fbeb707..f8d8819460 100644 --- a/src/mc/Session.cpp +++ b/src/mc/Session.cpp @@ -16,6 +16,7 @@ #include "xbt/system_error.hpp" #include +#include #include #ifdef __linux__ @@ -50,10 +51,7 @@ template void run_child_process(int socket, Code code) // modifying its .got.plt during snapshot. setenv("LC_BIND_NOW", "1", 1); - char buffer[64]; - int res = std::snprintf(buffer, sizeof(buffer), "%i", socket); - xbt_assert((size_t)res < sizeof(buffer) && res != -1); - setenv(MC_ENV_SOCKET_FD, buffer, 1); + setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1); code(); } diff --git a/src/mc/remote/RemoteSimulation.cpp b/src/mc/remote/RemoteSimulation.cpp index ceb818df3c..ff36950e1d 100644 --- a/src/mc/remote/RemoteSimulation.cpp +++ b/src/mc/remote/RemoteSimulation.cpp @@ -17,6 +17,7 @@ #include // PROT_* #include +#include using simgrid::mc::remote; @@ -214,14 +215,8 @@ static void zero_buffer_init() int open_vm(pid_t pid, int flags) { - const size_t buffer_size = 30; - char buffer[buffer_size]; - int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long)pid); - if (res < 0 || (size_t)res >= buffer_size) { - errno = ENAMETOOLONG; - return -1; - } - return open(buffer, flags); + std::string buffer = "/proc/" + std::to_string(pid) + "/mem"; + return open(buffer.c_str(), flags); } // ***** RemoteSimulation diff --git a/src/smpi/bindings/smpi_f77.cpp b/src/smpi/bindings/smpi_f77.cpp index 8594d517e8..3e71af21b1 100644 --- a/src/smpi/bindings/smpi_f77.cpp +++ b/src/smpi/bindings/smpi_f77.cpp @@ -13,6 +13,8 @@ #include "smpi_win.hpp" #include "src/smpi/include/smpi_actor.hpp" +#include + static int running_processes = 0; void smpi_init_fortran_types() @@ -200,11 +202,8 @@ void mpi_win_set_name_(int* win, char* name, int* ierr, int size) size--; name++; } - char* tname = xbt_new(char,size+1); - strncpy(tname, name, size); - tname[size]='\0'; - *ierr = MPI_Win_set_name(simgrid::smpi::Win::f2c(*win), tname); - xbt_free(tname); + std::string tname(name, size); + *ierr = MPI_Win_set_name(simgrid::smpi::Win::f2c(*win), tname.c_str()); } void mpi_win_get_name_(int* win, char* name, int* len, int* ierr) @@ -371,9 +370,7 @@ void mpi_info_set_(int* info, char* key, char* value, int* ierr, unsigned int ke keylen--; key++; } - char* tkey = xbt_new(char,keylen+1); - strncpy(tkey, key, keylen); - tkey[keylen]='\0'; + std::string tkey(key, keylen); while(value[valuelen-1]==' ') valuelen--; @@ -381,13 +378,9 @@ void mpi_info_set_(int* info, char* key, char* value, int* ierr, unsigned int ke valuelen--; value++; } - char* tvalue = xbt_new(char,valuelen+1); - strncpy(tvalue, value, valuelen); - tvalue[valuelen]='\0'; + std::string tvalue(value, valuelen); - *ierr = MPI_Info_set( simgrid::smpi::Info::f2c(*info), tkey, tvalue); - xbt_free(tkey); - xbt_free(tvalue); + *ierr = MPI_Info_set(simgrid::smpi::Info::f2c(*info), tkey.c_str(), tvalue.c_str()); } void mpi_info_get_(int* info, char* key, int* valuelen, char* value, int* flag, int* ierr, unsigned int keylen) @@ -398,11 +391,8 @@ void mpi_info_get_(int* info, char* key, int* valuelen, char* value, int* flag, keylen--; key++; } - char* tkey = xbt_new(char,keylen+1); - strncpy(tkey, key, keylen); - tkey[keylen]='\0'; - *ierr = MPI_Info_get(simgrid::smpi::Info::f2c(*info),tkey,*valuelen, value, flag); - xbt_free(tkey); + std::string tkey(key, keylen); + *ierr = MPI_Info_get(simgrid::smpi::Info::f2c(*info), tkey.c_str(), *valuelen, value, flag); if(*flag!=0){ int replace=0; for (int i = 0; i < *valuelen; i++) { @@ -874,11 +864,8 @@ void mpi_info_get_valuelen_ ( int* info, char *key, int *valuelen, int *flag, in keylen--; key++; } - char* tkey = xbt_new(char, keylen+1); - strncpy(tkey, key, keylen); - tkey[keylen]='\0'; - *ierr = MPI_Info_get_valuelen( simgrid::smpi::Info::f2c(*info), tkey, valuelen, flag); - xbt_free(tkey); + std::string tkey(key, keylen); + *ierr = MPI_Info_get_valuelen(simgrid::smpi::Info::f2c(*info), tkey.c_str(), valuelen, flag); } void mpi_info_delete_ (int* info, char *key, int* ierr, unsigned int keylen){ @@ -888,11 +875,8 @@ void mpi_info_delete_ (int* info, char *key, int* ierr, unsigned int keylen){ keylen--; key++; } - char* tkey = xbt_new(char, keylen+1); - strncpy(tkey, key, keylen); - tkey[keylen]='\0'; - *ierr = MPI_Info_delete(simgrid::smpi::Info::f2c(*info), tkey); - xbt_free(tkey); + std::string tkey(key, keylen); + *ierr = MPI_Info_delete(simgrid::smpi::Info::f2c(*info), tkey.c_str()); } void mpi_info_get_nkeys_ ( int* info, int *nkeys, int* ierr){ diff --git a/src/smpi/bindings/smpi_f77_comm.cpp b/src/smpi/bindings/smpi_f77_comm.cpp index b087102470..b9fa1aa398 100644 --- a/src/smpi/bindings/smpi_f77_comm.cpp +++ b/src/smpi/bindings/smpi_f77_comm.cpp @@ -8,6 +8,8 @@ #include "smpi_errhandler.hpp" #include "smpi_info.hpp" +#include + extern "C" { // This should really use the C linkage to be usable from Fortran void mpi_comm_rank_(int* comm, int* rank, int* ierr) { @@ -146,11 +148,8 @@ void mpi_comm_set_name_ (int* comm, char* name, int* ierr){ int count; for(count=MPI_MAX_OBJECT_NAME-1; count>=0 && name[count]==' '; count--); count+=1; - char* tname = xbt_new(char, count+1); - strncpy(tname, name, count); - tname[count]='\0'; - *ierr = MPI_Comm_set_name (simgrid::smpi::Comm::f2c(*comm), tname); - xbt_free(tname); + std::string tname(name, count); + *ierr = MPI_Comm_set_name(simgrid::smpi::Comm::f2c(*comm), tname.c_str()); } void mpi_comm_dup_with_info_ (int* comm, int* info, int* newcomm, int* ierr){ diff --git a/src/smpi/bindings/smpi_f77_type.cpp b/src/smpi/bindings/smpi_f77_type.cpp index c24a6617a8..b492a10a3c 100644 --- a/src/smpi/bindings/smpi_f77_type.cpp +++ b/src/smpi/bindings/smpi_f77_type.cpp @@ -7,6 +7,8 @@ #include "smpi_comm.hpp" #include "smpi_datatype.hpp" +#include + extern "C" { // This should really use the C linkage to be usable from Fortran void mpi_type_extent_(int* datatype, MPI_Aint * extent, int* ierr){ @@ -42,12 +44,10 @@ void mpi_type_dup_ (int* datatype, int* newdatatype, int* ierr){ } } -void mpi_type_set_name_ (int* datatype, char * name, int* ierr, int size){ - char* tname = xbt_new(char, size+1); - strncpy(tname, name, size); - tname[size]='\0'; - *ierr = MPI_Type_set_name(simgrid::smpi::Datatype::f2c(*datatype), tname); - xbt_free(tname); +void mpi_type_set_name_(int* datatype, char* name, int* ierr, int size) +{ + std::string tname(name, size); + *ierr = MPI_Type_set_name(simgrid::smpi::Datatype::f2c(*datatype), tname.c_str()); } void mpi_type_get_name_ (int* datatype, char * name, int* len, int* ierr){ -- 2.30.2