Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Automatic memory management with std::unique_ptr.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 11:11:25 +0000 (12:11 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 15:14:07 +0000 (16:14 +0100)
examples/s4u/replay-io/s4u-replay-io.cpp
examples/smpi/replay_multiple_manual_deploy/replay_multiple_manual.cpp

index 70327db..ddf1457 100644 (file)
@@ -20,7 +20,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(replay_io, "Messages specific for this example");
     ((void)0)
 
 class Replayer {
-  static std::unordered_map<std::string, simgrid::s4u::File*> opened_files;
+  static std::unordered_map<std::string, std::unique_ptr<simgrid::s4u::File>> opened_files;
 
   static void log_action(const simgrid::xbt::ReplayAction& action, double date)
   {
@@ -33,7 +33,7 @@ class Replayer {
   static simgrid::s4u::File* get_file_descriptor(const std::string& file_name)
   {
     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
-    return opened_files.at(full_name);
+    return opened_files.at(full_name).get();
   }
 
 public:
@@ -56,9 +56,9 @@ public:
     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
 
     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
-    auto* file = new simgrid::s4u::File(file_name, nullptr);
+    auto file = std::make_unique<simgrid::s4u::File>(file_name, nullptr);
 
-    opened_files.insert({full_name, file});
+    opened_files.insert({full_name, std::move(file)});
 
     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
   }
@@ -80,18 +80,18 @@ public:
   static void close(simgrid::xbt::ReplayAction& action)
   {
     std::string file_name = action[2];
+    std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
     double clock          = simgrid::s4u::Engine::get_clock();
 
-    const simgrid::s4u::File* file = get_file_descriptor(file_name);
-
     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
-    delete file;
+    XBT_ATTRIB_UNUSED auto count = opened_files.erase(full_name);
+    xbt_assert(count == 1, "File not found in opened files: %s", full_name.c_str());
 
     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
   }
 };
 
-std::unordered_map<std::string, simgrid::s4u::File*> Replayer::opened_files;
+std::unordered_map<std::string, std::unique_ptr<simgrid::s4u::File>> Replayer::opened_files;
 
 int main(int argc, char* argv[])
 {
index 2e769f6..ae7a7df 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <algorithm>
 #include <fstream>
+#include <memory>
 #include <sstream>
 #include <stdexcept>
 #include <vector>
@@ -47,7 +48,7 @@ struct Job {
 static std::vector<simgrid::s4u::Host*> hosts;
 static int noise_between_jobs;
 
-static bool job_comparator(const Job* j1, const Job* j2)
+static bool job_comparator(const std::unique_ptr<Job>& j1, const std::unique_ptr<Job>& j2)
 {
   if (j1->starting_time == j2->starting_time)
     return j1->smpi_app_name < j2->smpi_app_name;
@@ -105,9 +106,9 @@ static int job_executor_process(Job* job)
 }
 
 // Executes a workload of SMPI processes
-static int workload_executor_process(const std::vector<Job*>* workload)
+static int workload_executor_process(const std::vector<std::unique_ptr<Job>>* workload)
 {
-  for (Job* job : *workload) {
+  for (auto const& job : *workload) {
     // Let's wait until the job's waiting time if needed
     double curr_time = simgrid::s4u::Engine::get_clock();
     if (job->starting_time > curr_time) {
@@ -127,7 +128,7 @@ static int workload_executor_process(const std::vector<Job*>* workload)
     // Let's finally run the job executor
     char* str_pname = bprintf("job_%04d", job->unique_job_number);
     XBT_INFO("Launching the job executor of job %d (app '%s')", job->unique_job_number, job->smpi_app_name.c_str());
-    simgrid::s4u::Actor::create(str_pname, hosts[job->allocation[0]], job_executor_process, job);
+    simgrid::s4u::Actor::create(str_pname, hosts[job->allocation[0]], job_executor_process, job.get());
     xbt_free(str_pname);
   }
 
@@ -135,11 +136,11 @@ static int workload_executor_process(const std::vector<Job*>* workload)
 }
 
 // Reads jobs from a workload file and returns them
-static std::vector<Job*> all_jobs(const std::string& workload_file)
+static std::vector<std::unique_ptr<Job>> all_jobs(const std::string& workload_file)
 {
   std::ifstream f(workload_file);
   xbt_assert(f.is_open(), "Cannot open file '%s'.", workload_file.c_str());
-  std::vector<Job*> jobs;
+  std::vector<std::unique_ptr<Job>> jobs;
 
   simgrid::xbt::Path path(workload_file);
   std::string dir = path.get_dir_name();
@@ -155,42 +156,42 @@ static std::vector<Job*> all_jobs(const std::string& workload_file)
     std::istringstream is(line);
     if (is >> app_name >> filename_unprefixed >> app_size >> starting_time >> alloc) {
       try {
-        Job job;
-        job.smpi_app_name = app_name;
-        job.filename      = dir + "/" + filename_unprefixed;
-        job.app_size      = app_size;
-        job.starting_time = starting_time;
+        auto job           = std::make_unique<Job>();
+        job->smpi_app_name = app_name;
+        job->filename      = dir + "/" + filename_unprefixed;
+        job->app_size      = app_size;
+        job->starting_time = starting_time;
 
         std::vector<std::string> subparts;
         boost::split(subparts, alloc, boost::is_any_of(","), boost::token_compress_on);
 
-        if ((int)subparts.size() != job.app_size)
+        if ((int)subparts.size() != job->app_size)
           throw std::invalid_argument("size/alloc inconsistency");
 
-        job.allocation.resize(subparts.size());
+        job->allocation.resize(subparts.size());
         for (unsigned int i = 0; i < subparts.size(); ++i)
-          job.allocation[i] = stoi(subparts[i]);
+          job->allocation[i] = stoi(subparts[i]);
 
         // Let's read the filename
-        std::ifstream traces_file(job.filename);
+        std::ifstream traces_file(job->filename);
         if (!traces_file.is_open())
-          throw std::invalid_argument("Cannot open file " + job.filename);
+          throw std::invalid_argument("Cannot open file " + job->filename);
 
         std::string traces_line;
         while (std::getline(traces_file, traces_line)) {
           boost::trim_right(traces_line);
-          job.traces_filenames.push_back(dir + "/" + traces_line);
+          job->traces_filenames.push_back(dir + "/" + traces_line);
         }
 
-        if (static_cast<int>(job.traces_filenames.size()) < job.app_size)
+        if (static_cast<int>(job->traces_filenames.size()) < job->app_size)
           throw std::invalid_argument("size/tracefiles inconsistency");
-        job.traces_filenames.resize(job.app_size);
+        job->traces_filenames.resize(job->app_size);
 
         XBT_INFO("Job read: app='%s', file='%s', size=%d, start=%d, "
                  "alloc='%s'",
-                 job.smpi_app_name.c_str(), filename_unprefixed.c_str(), job.app_size, job.starting_time,
+                 job->smpi_app_name.c_str(), filename_unprefixed.c_str(), job->app_size, job->starting_time,
                  alloc.c_str());
-        jobs.push_back(new Job(std::move(job)));
+        jobs.emplace_back(std::move(job));
       } catch (const std::invalid_argument& e) {
         xbt_die("Bad line '%s' of file '%s': %s.\n", line.c_str(), workload_file.c_str(), e.what());
       }
@@ -221,10 +222,10 @@ int main(int argc, char* argv[])
   xbt_assert(hosts.size() >= 4, "The given platform should contain at least 4 hosts (found %zu).", hosts.size());
 
   // Let's retrieve all SMPI jobs
-  std::vector<Job*> jobs = all_jobs(argv[2]);
+  std::vector<std::unique_ptr<Job>> jobs = all_jobs(argv[2]);
 
   // Let's register them
-  for (const Job* job : jobs)
+  for (auto const& job : jobs)
     SMPI_app_instance_register(job->smpi_app_name.c_str(), nullptr, job->app_size);
 
   SMPI_init();
@@ -249,8 +250,5 @@ int main(int argc, char* argv[])
 
   SMPI_finalize();
 
-  for (const Job* job : jobs)
-    delete job;
-
   return 0;
 }