From 2738598c9f876339ee6f8b3fc217984b7b837539 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 19 Apr 2019 10:08:05 +0200 Subject: [PATCH] Use std::unique_ptr to manage memory. --- include/simgrid/jedule/jedule_platform.hpp | 8 +++++--- src/instr/instr_paje_types.cpp | 17 ++++++----------- src/instr/instr_paje_types.hpp | 7 ++++--- src/instr/instr_platform.cpp | 16 ++++++++-------- src/instr/jedule/jedule_platform.cpp | 10 ++-------- src/surf/cpu_ti.cpp | 9 ++------- src/surf/cpu_ti.hpp | 6 +++--- src/xbt/config.cpp | 16 +++++----------- 8 files changed, 35 insertions(+), 54 deletions(-) diff --git a/include/simgrid/jedule/jedule_platform.hpp b/include/simgrid/jedule/jedule_platform.hpp index a7f74391f4..cf5b0e0a33 100644 --- a/include/simgrid/jedule/jedule_platform.hpp +++ b/include/simgrid/jedule/jedule_platform.hpp @@ -9,16 +9,18 @@ #include #include +#include +#include #include #include -#include 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 name2id; Container *parent = nullptr; - std::vector children; + std::vector> children; std::vector resource_list; void add_child(Container* child); void add_resources(std::vector hosts); diff --git a/src/instr/instr_paje_types.cpp b/src/instr/instr_paje_types.cpp index 77f7ae99dc..335cb12b5a 100644 --- a/src/instr/instr_paje_types.cpp +++ b/src/instr/instr_paje_types.cpp @@ -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(cont->second); + return cont == children_.end() ? new VariableType(name, mycolor, this) + : static_cast(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(it->second); + return static_cast(it->second.get()); } } } diff --git a/src/instr/instr_paje_types.hpp b/src/instr/instr_paje_types.hpp index 9f0ad69326..0c9c66834c 100644 --- a/src/instr/instr_paje_types.hpp +++ b/src/instr/instr_paje_types.hpp @@ -7,6 +7,7 @@ #define INSTR_PAJE_TYPES_HPP #include "src/instr/instr_private.hpp" +#include #include #include #include @@ -23,12 +24,12 @@ class Type { Type* father_; public: - std::map children_; + std::map> 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 T* by_name_or_create(const std::string& name) { auto cont = children_.find(name); - return cont == children_.end() ? new T(name, this) : static_cast(cont->second); + return cont == children_.end() ? new T(name, this) : static_cast(cont->second.get()); } void set_calling_container(Container* container) { issuer_ = container; } diff --git a/src/instr/instr_platform.cpp b/src/instr/instr_platform.cpp index a19c6998a0..c3d88b94ec 100644 --- a/src/instr/instr_platform.cpp +++ b/src/instr/instr_platform.cpp @@ -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(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(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) diff --git a/src/instr/jedule/jedule_platform.cpp b/src/instr/jedule/jedule_platform.cpp index 13d1ef6ca6..891b31aef4 100644 --- a/src/instr/jedule/jedule_platform.cpp +++ b/src/instr/jedule/jedule_platform.cpp @@ -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 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; } diff --git a/src/surf/cpu_ti.cpp b/src/surf/cpu_ti.cpp index b38323e276..997b716ae3 100644 --- a/src/surf/cpu_ti.cpp +++ b/src/surf/cpu_ti.cpp @@ -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); diff --git a/src/surf/cpu_ti.hpp b/src/surf/cpu_ti.hpp index 61c94fa5f5..2bcdfabf2c 100644 --- a/src/surf/cpu_ti.hpp +++ b/src/surf/cpu_ti.hpp @@ -10,6 +10,7 @@ #include "src/surf/cpu_interface.hpp" #include +#include 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 profile_ = nullptr; + profile::Profile* speed_profile_ = nullptr; }; /********** diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 6c2cc3a457..90c3c63ea3 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -252,14 +253,13 @@ template const char* TypedConfigurationElement::get_type_name() // class Config { private: // name -> ConfigElement: - std::map options; + std::map> options; // alias -> ConfigElement from options: std::map aliases; bool warn_for_aliases = true; public: Config(); - ~Config(); // No copy: Config(Config const&) = delete; @@ -276,7 +276,7 @@ public: TypedConfigurationElement* variable = new TypedConfigurationElement(name, std::forward(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()); } -- 2.20.1