From 302c1c4ddcf29390c1845adc161264e96d8a9a44 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 23 Nov 2020 16:37:21 +0100 Subject: [PATCH] Make Datatype::name_ and Win::name_ a std::string. --- src/smpi/include/smpi_datatype.hpp | 4 ++-- src/smpi/include/smpi_win.hpp | 2 +- src/smpi/mpi/smpi_datatype.cpp | 17 +++++++---------- src/smpi/mpi/smpi_win.cpp | 16 +++++----------- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/smpi/include/smpi_datatype.hpp b/src/smpi/include/smpi_datatype.hpp index f55d2a8d13..dace552ca0 100644 --- a/src/smpi/include/smpi_datatype.hpp +++ b/src/smpi/include/smpi_datatype.hpp @@ -89,7 +89,7 @@ class Datatype_contents { }; class Datatype : public F2C, public Keyval{ - char* name_ = nullptr; + std::string name_ = ""; /* The id here is the (unique) datatype id used for this datastructure. * It's default value is set to -1 since some code expects this return value * when no other id has been assigned @@ -115,7 +115,7 @@ public: Datatype& operator=(const Datatype&) = delete; ~Datatype() override; - char* name() const { return name_; } + const char* name() const { return name_.c_str(); } size_t size() const { return size_; } MPI_Aint lb() const { return lb_; } MPI_Aint ub() const { return ub_; } diff --git a/src/smpi/include/smpi_win.hpp b/src/smpi/include/smpi_win.hpp index cb84e72828..7e40d29d0e 100644 --- a/src/smpi/include/smpi_win.hpp +++ b/src/smpi/include/smpi_win.hpp @@ -29,7 +29,7 @@ class Win : public F2C, public Keyval { s4u::MutexPtr mut_; s4u::Barrier* bar_; MPI_Win* connected_wins_; - char* name_; + std::string name_; int opened_; MPI_Group group_; int count_; //for ordering the accs diff --git a/src/smpi/mpi/smpi_datatype.cpp b/src/smpi/mpi/smpi_datatype.cpp index 3f770e9c9d..85d16a6c3e 100644 --- a/src/smpi/mpi/smpi_datatype.cpp +++ b/src/smpi/mpi/smpi_datatype.cpp @@ -151,7 +151,6 @@ Datatype::~Datatype() cleanup_attr(); delete contents_; - xbt_free(name_); } int Datatype::copy_attrs(Datatype* datatype){ @@ -261,18 +260,16 @@ int Datatype::extent(MPI_Aint* lb, MPI_Aint* extent) const void Datatype::get_name(char* name, int* length) const { - if(name_!=nullptr){ - *length = strlen(name_); - strncpy(name, name_, *length+1); - }else{ - *length = 0; + *length = name_.length(); + if (not name_.empty()) { + name_.copy(name, *length); + name[*length] = '\0'; } } -void Datatype::set_name(const char* name){ - if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0) - xbt_free(name_); - name_ = xbt_strdup(name); +void Datatype::set_name(const char* name) +{ + name_ = name; } int Datatype::pack(const void* inbuf, int incount, void* outbuf, int outcount, int* position, const Comm*) diff --git a/src/smpi/mpi/smpi_win.cpp b/src/smpi/mpi/smpi_win.cpp index 6f1245e60c..73bd32d6cc 100644 --- a/src/smpi/mpi/smpi_win.cpp +++ b/src/smpi/mpi/smpi_win.cpp @@ -36,7 +36,6 @@ Win::Win(void* base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, if(info!=MPI_INFO_NULL) info->ref(); int comm_size = comm->size(); - name_ = nullptr; opened_ = 0; group_ = MPI_GROUP_NULL; requests_ = new std::vector(); @@ -72,9 +71,6 @@ Win::~Win(){ delete requests_; delete[] connected_wins_; - if (name_ != nullptr){ - xbt_free(name_); - } if (info_ != MPI_INFO_NULL) simgrid::smpi::Info::unref(info_); if (errhandler_ != MPI_ERRHANDLER_NULL) @@ -112,13 +108,11 @@ int Win::detach(const void* /*base*/) void Win::get_name(char* name, int* length) const { - if(name_==nullptr){ - *length=0; - name=nullptr; - return; + *length = name_.length(); + if (not name_.empty()) { + name_.copy(name, *length); + name[*length] = '\0'; } - *length = strlen(name_); - strncpy(name, name_, *length+1); } void Win::get_group(MPI_Group* group){ @@ -172,7 +166,7 @@ void Win::set_info(MPI_Info info) } void Win::set_name(const char* name){ - name_ = xbt_strdup(name); + name_ = name; } int Win::fence(int assert) -- 2.20.1