Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::unique_ptr to manage memory.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 19 Apr 2019 08:08:05 +0000 (10:08 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 19 Apr 2019 09:54:27 +0000 (11:54 +0200)
include/simgrid/jedule/jedule_platform.hpp
src/instr/instr_paje_types.cpp
src/instr/instr_paje_types.hpp
src/instr/instr_platform.cpp
src/instr/jedule/jedule_platform.cpp
src/surf/cpu_ti.cpp
src/surf/cpu_ti.hpp
src/xbt/config.cpp

index a7f7439..cf5b0e0 100644 (file)
@@ -9,16 +9,18 @@
 #include <simgrid/forward.h>
 #include <xbt/dynar.h>
 
+#include <memory>
+#include <string>
 #include <unordered_map>
 #include <vector>
-#include <string>
 
 namespace simgrid {
 namespace jedule{
 class XBT_PUBLIC Container {
 public:
   explicit Container(const std::string& name);
-  ~Container();
+  Container(const Container&) = delete;
+  Container& operator=(const Container&) = delete;
 
 private:
   int last_id_;
@@ -28,7 +30,7 @@ public:
   std::string name;
   std::unordered_map<const char*, unsigned int> name2id;
   Container *parent = nullptr;
-  std::vector<Container*> children;
+  std::vector<std::unique_ptr<Container>> children;
   std::vector<sg_host_t> resource_list;
   void add_child(Container* child);
   void add_resources(std::vector<sg_host_t> hosts);
index 77f7ae9..335cb12 100644 (file)
@@ -22,7 +22,7 @@ Type::Type(const std::string& name, const std::string& alias, const std::string&
     THROWF(tracing_error, 0, "can't create a new type with no name or alias");
 
   if (father != nullptr){
-    father->children_.insert({alias, this});
+    father->children_.emplace(alias, this);
     XBT_DEBUG("new type %s, child of %s", get_cname(), father->get_cname());
   }
   if (trace_format == simgrid::instr::TraceFormat::Paje) {
@@ -30,12 +30,6 @@ Type::Type(const std::string& name, const std::string& alias, const std::string&
   }
 }
 
-Type::~Type()
-{
-  for (auto elm : children_)
-    delete elm.second;
-}
-
 ContainerType::ContainerType(const std::string& name, Type* father) : Type(name, name, "", father)
 {
   XBT_DEBUG("ContainerType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
@@ -165,12 +159,12 @@ void Type::log_definition(simgrid::instr::Type* source, simgrid::instr::Type* de
 Type* Type::by_name(const std::string& name)
 {
   Type* ret = nullptr;
-  for (auto elm : children_) {
+  for (auto const& elm : children_) {
     if (elm.second->name_ == name) {
       if (ret != nullptr) {
         THROWF (tracing_error, 0, "there are two children types with the same name?");
       } else {
-        ret = elm.second;
+        ret = elm.second.get();
       }
     }
   }
@@ -210,7 +204,8 @@ VariableType* Type::by_name_or_create(const std::string& name, const std::string
 {
   auto cont = children_.find(name);
   std::string mycolor = color.empty() ? "1 1 1" : color;
-  return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
+  return cont == children_.end() ? new VariableType(name, mycolor, this)
+                                 : static_cast<VariableType*>(cont->second.get());
 }
 
 LinkType* Type::by_name_or_create(const std::string& name, Type* source, Type* dest)
@@ -224,7 +219,7 @@ LinkType* Type::by_name_or_create(const std::string& name, Type* source, Type* d
     ret->log_definition(source, dest);
     return ret;
   } else
-    return static_cast<LinkType*>(it->second);
+    return static_cast<LinkType*>(it->second.get());
 }
 }
 }
index 9f0ad69..0c9c668 100644 (file)
@@ -7,6 +7,7 @@
 #define INSTR_PAJE_TYPES_HPP
 
 #include "src/instr/instr_private.hpp"
+#include <memory>
 #include <sstream>
 #include <string>
 #include <vector>
@@ -23,12 +24,12 @@ class Type {
   Type* father_;
 
 public:
-  std::map<std::string, Type*> children_;
+  std::map<std::string, std::unique_ptr<Type>> children_;
   Container* issuer_ = nullptr;
   std::stringstream stream_;
 
   Type(const std::string& name, const std::string& alias, const std::string& color, Type* father);
-  virtual ~Type();
+  virtual ~Type() = default;
 
   const std::string& get_name() const { return name_; }
   const char* get_cname() { return name_.c_str(); }
@@ -42,7 +43,7 @@ public:
   template <class T> T* by_name_or_create(const std::string& name)
   {
     auto cont = children_.find(name);
-    return cont == children_.end() ? new T(name, this) : static_cast<T*>(cont->second);
+    return cont == children_.end() ? new T(name, this) : static_cast<T*>(cont->second.get());
   }
 
   void set_calling_container(Container* container) { issuer_ = container; }
index a19c699..c3d88b9 100644 (file)
@@ -439,8 +439,8 @@ static void recursiveNewVariableType(const std::string& new_typename, const std:
   if (root->get_name() == "LINK")
     root->by_name_or_create(std::string("b") + new_typename, color);
 
-  for (auto elm : root->children_) {
-    recursiveNewVariableType(new_typename, color, elm.second);
+  for (auto const& elm : root->children_) {
+    recursiveNewVariableType(new_typename, color, elm.second.get());
   }
 }
 
@@ -455,8 +455,8 @@ static void recursiveNewUserVariableType(const std::string& father_type, const s
   if (root->get_name() == father_type) {
     root->by_name_or_create(new_typename, color);
   }
-  for (auto elm : root->children_)
-    recursiveNewUserVariableType(father_type, new_typename, color, elm.second);
+  for (auto const& elm : root->children_)
+    recursiveNewUserVariableType(father_type, new_typename, color, elm.second.get());
 }
 
 void instr_new_user_variable_type(const std::string& father_type, const std::string& new_typename,
@@ -471,8 +471,8 @@ static void recursiveNewUserStateType(const std::string& father_type, const std:
   if (root->get_name() == father_type)
     root->by_name_or_create<simgrid::instr::StateType>(new_typename);
 
-  for (auto elm : root->children_)
-    recursiveNewUserStateType(father_type, new_typename, elm.second);
+  for (auto const& elm : root->children_)
+    recursiveNewUserStateType(father_type, new_typename, elm.second.get());
 }
 
 void instr_new_user_state_type(const std::string& father_type, const std::string& new_typename)
@@ -486,8 +486,8 @@ static void recursiveNewValueForUserStateType(const std::string& type_name, cons
   if (root->get_name() == type_name)
     static_cast<simgrid::instr::StateType*>(root)->add_entity_value(val, color);
 
-  for (auto elm : root->children_)
-    recursiveNewValueForUserStateType(type_name, val, color, elm.second);
+  for (auto const& elm : root->children_)
+    recursiveNewValueForUserStateType(type_name, val, color, elm.second.get());
 }
 
 void instr_new_value_for_user_state_type(const std::string& type_name, const char* value, const std::string& color)
index 13d1ef6..891b31a 100644 (file)
@@ -29,16 +29,10 @@ Container::Container(const std::string& name) : name(name)
   container_name2container.insert({this->name, this});
 }
 
-Container::~Container()
-{
-  for (auto const& child : this->children)
-    delete child;
-}
-
 void Container::add_child(jed_container_t child)
 {
   xbt_assert(child != nullptr);
-  this->children.push_back(child);
+  this->children.emplace_back(child);
   child->parent = this;
 }
 
@@ -86,7 +80,7 @@ std::vector<int> Container::get_hierarchy()
       int child_nb = -1;
 
       for (auto const& child : this->parent->children) {
-        if( child == this) {
+        if (child.get() == this) {
           child_nb = i;
           break;
         }
index b38323e..997b716 100644 (file)
@@ -39,11 +39,6 @@ CpuTiProfile::CpuTiProfile(profile::Profile* profile)
   integral_.push_back(integral);
 }
 
-CpuTiTmgr::~CpuTiTmgr()
-{
-  delete profile_;
-}
-
 /**
  * @brief Integrate trace
  *
@@ -222,7 +217,7 @@ double CpuTiTmgr::get_power_scale(double a)
 CpuTiTmgr::CpuTiTmgr(kernel::profile::Profile* speed_profile, double value) : speed_profile_(speed_profile)
 {
   double total_time = 0.0;
-  profile_          = 0;
+  profile_.reset(nullptr);
 
   /* no availability file, fixed trace */
   if (not speed_profile) {
@@ -245,7 +240,7 @@ CpuTiTmgr::CpuTiTmgr(kernel::profile::Profile* speed_profile, double value) : sp
   for (auto const& val : speed_profile->event_list)
     total_time += val.date_;
 
-  profile_   = new CpuTiProfile(speed_profile);
+  profile_.reset(new CpuTiProfile(speed_profile));
   last_time_ = total_time;
   total_     = profile_->integrate_simple(0, total_time);
 
index 61c94fa..2bcdfab 100644 (file)
@@ -10,6 +10,7 @@
 #include "src/surf/cpu_interface.hpp"
 
 #include <boost/intrusive/list.hpp>
+#include <memory>
 
 namespace simgrid {
 namespace kernel {
@@ -48,7 +49,6 @@ public:
   CpuTiTmgr(profile::Profile* speed_profile, double value);
   CpuTiTmgr(const CpuTiTmgr&) = delete;
   CpuTiTmgr& operator=(const CpuTiTmgr&) = delete;
-  ~CpuTiTmgr();
 
   double integrate(double a, double b);
   double solve(double a, double amount);
@@ -62,8 +62,8 @@ private:
   double last_time_ = 0.0;             /*< Integral interval last point (discrete time) */
   double total_    = 0.0;             /*< Integral total between 0 and last_pointn */
 
-  CpuTiProfile* profile_                   = nullptr;
-  profile::Profile* speed_profile_         = nullptr;
+  std::unique_ptr<CpuTiProfile> profile_ = nullptr;
+  profile::Profile* speed_profile_       = nullptr;
 };
 
 /**********
index 6c2cc3a..90c3c63 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <functional>
 #include <map>
+#include <memory>
 #include <stdexcept>
 #include <string>
 #include <string>
@@ -252,14 +253,13 @@ template <class T> const char* TypedConfigurationElement<T>::get_type_name() //
 class Config {
 private:
   // name -> ConfigElement:
-  std::map<std::string, simgrid::config::ConfigurationElement*> options;
+  std::map<std::string, std::unique_ptr<simgrid::config::ConfigurationElement>> options;
   // alias -> ConfigElement from options:
   std::map<std::string, simgrid::config::ConfigurationElement*> aliases;
   bool warn_for_aliases = true;
 
 public:
   Config();
-  ~Config();
 
   // No copy:
   Config(Config const&) = delete;
@@ -276,7 +276,7 @@ public:
     TypedConfigurationElement<T>* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
     XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
               variable->get_type_name(), variable, this);
-    options.insert({name, variable});
+    options.emplace(name, variable);
     variable->update();
     return variable;
   }
@@ -294,18 +294,12 @@ Config::Config()
 {
   atexit(&sg_config_finalize);
 }
-Config::~Config()
-{
-  XBT_DEBUG("Frees cfg set %p", this);
-  for (auto const& elm : options)
-    delete elm.second;
-}
 
 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
 {
   auto opt = options.find(name);
   if (opt != options.end()) {
-    return opt->second;
+    return opt->second.get();
   } else {
     auto als = aliases.find(name);
     if (als != aliases.end()) {
@@ -362,7 +356,7 @@ void Config::show_aliases()
 void Config::help()
 {
   for (auto const& elm : options) {
-    simgrid::config::ConfigurationElement* variable = this->options.at(elm.first);
+    simgrid::config::ConfigurationElement* variable = elm.second.get();
     XBT_HELP("   %s: %s", elm.first.c_str(), variable->get_description().c_str());
     XBT_HELP("       Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
   }