Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use RAII std::string for s_mc_comm_pattern::rdv
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 5 Apr 2016 11:34:40 +0000 (13:34 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 5 Apr 2016 14:32:44 +0000 (16:32 +0200)
src/mc/CommunicationDeterminismChecker.cpp
src/mc/Process.cpp
src/mc/Process.hpp
src/mc/mc_comm_pattern.cpp
src/mc/mc_comm_pattern.h
src/mc/mc_smx.cpp

index 368d4e8..f2fb17e 100644 (file)
@@ -38,7 +38,7 @@ xbt_dynar_t incomplete_communications_pattern;
 static e_mc_comm_pattern_difference_t compare_comm_pattern(mc_comm_pattern_t comm1, mc_comm_pattern_t comm2) {
   if(comm1->type != comm2->type)
     return TYPE_DIFF;
-  if (strcmp(comm1->rdv, comm2->rdv) != 0)
+  if (comm1->rdv != comm2->rdv)
     return RDV_DIFF;
   if (comm1->src_proc != comm2->src_proc)
     return SRC_PROC_DIFF;
index dd04f15..de44cf8 100644 (file)
@@ -439,17 +439,16 @@ void Process::read_variable(const char* name, void* target, size_t size) const
   this->read_bytes(target, size, remote(var->address));
 }
 
-char* Process::read_string(RemotePtr<void> address) const
+std::string Process::read_string(RemotePtr<void> address) const
 {
   if (!address)
-    return nullptr;
+    return {};
 
-  off_t len = 128;
-  char* res = (char*) malloc(len);
+  std::vector<char> res(128);
   off_t off = 0;
 
   while (1) {
-    ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off);
+    ssize_t c = pread(this->memory_file, res.data() + off, res.size() - off, (off_t) address.address() + off);
     if (c == -1) {
       if (errno == EINTR)
         continue;
@@ -459,15 +458,13 @@ char* Process::read_string(RemotePtr<void> address) const
     if (c==0)
       xbt_die("Could not read string from remote process");
 
-    void* p = memchr(res + off, '\0', c);
+    void* p = memchr(res.data() + off, '\0', c);
     if (p)
-      return res;
+      return std::string(res.data());
 
     off += c;
-    if (off == len) {
-      len *= 2;
-      res = (char*) realloc(res, len);
-    }
+    if (off == (off_t) res.size())
+      res.resize(res.size() * 2);
   }
 }
 
index 150f7d1..b709c33 100644 (file)
@@ -13,6 +13,7 @@
 #include <type_traits>
 #include <vector>
 #include <memory>
+#include <string>
 
 #include <sys/types.h>
 
@@ -118,7 +119,7 @@ public:
     read_variable(name, &res, sizeof(T));
     return res;
   }
-  char* read_string(RemotePtr<void> address) const;
+  std::string read_string(RemotePtr<void> address) const;
 
   // Write memory:
   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
index e474dde..e8536f0 100644 (file)
@@ -26,7 +26,7 @@ mc_comm_pattern_t MC_comm_pattern_dup(mc_comm_pattern_t comm)
   res->index = comm->index;
   res->type = comm->type;
   res->comm_addr = comm->comm_addr;
-  res->rdv = xbt_strdup(comm->rdv);
+  res->rdv = comm->rdv;
   res->data_size = -1;
   res->data = nullptr;
   if (comm->type == SIMIX_COMM_SEND) {
index d944f3d..1fd9774 100644 (file)
@@ -10,6 +10,8 @@
 #include <cstddef>
 #include <cstring>
 
+#include <string>
+
 #include <simgrid_config.h>
 #include <xbt/dynar.h>
 
@@ -29,7 +31,7 @@ typedef struct s_mc_comm_pattern{
   unsigned long dst_proc = 0;
   const char *src_host = nullptr;
   const char *dst_host = nullptr;
-  char *rdv = nullptr;
+  std::string rdv;
   ssize_t data_size = 0;
   void *data = nullptr;
   int tag = 0;
@@ -41,7 +43,6 @@ typedef struct s_mc_comm_pattern{
   }
   ~s_mc_comm_pattern()
   {
-    xbt_free(this->rdv);
     xbt_free(this->data);
   }
 
index 3f6102f..6913890 100644 (file)
@@ -203,11 +203,8 @@ const char* MC_smx_process_get_name(smx_process_t p)
     return nullptr;
 
   simgrid::mc::SimixProcessInformation* info = MC_smx_process_get_info(p);
-  if (info->name.empty()) {
-    char* name = process->read_string(p->name);
-    info->name = name;
-    free(name);
-  }
+  if (info->name.empty())
+    info->name = process->read_string(p->name);
   return info->name.c_str();
 }