From f2aaadd437ac30adad539d969164098b50cd2338 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 22 Mar 2021 11:08:56 +0100 Subject: [PATCH] Reduce scope for variables. --- src/kernel/lmm/maxmin.cpp | 10 +++------- src/plugins/link_energy_wifi.cpp | 3 +-- src/simix/smx_global.cpp | 6 ++---- src/surf/cpu_cas01.cpp | 9 +++------ src/surf/network_cm02.cpp | 6 ++---- src/surf/network_interface.cpp | 3 +-- src/surf/ptask_L07.cpp | 9 +++------ src/surf/surf_c_bindings.cpp | 6 ++---- 8 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/kernel/lmm/maxmin.cpp b/src/kernel/lmm/maxmin.cpp index bd13a0a405..793c0f7ef1 100644 --- a/src/kernel/lmm/maxmin.cpp +++ b/src/kernel/lmm/maxmin.cpp @@ -141,16 +141,13 @@ System::System(bool selective_update) : selective_update_active(selective_update System::~System() { - Variable* var; - Constraint* cnst; - - while ((var = extract_variable())) { + while (Variable* var = extract_variable()) { std::string demangled = boost::core::demangle(var->id_ ? typeid(*var->id_).name() : "(unidentified)"); XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled.c_str(), var->rank_); var_free(var); } - while ((cnst = extract_constraint())) + while (Constraint* cnst = extract_constraint()) cnst_free(cnst); xbt_mallocator_free(variable_mallocator_); @@ -208,8 +205,7 @@ void System::variable_free(Variable* var) void System::variable_free_all() { - Variable* var; - while ((var = extract_variable())) + while (Variable* var = extract_variable()) variable_free(var); } diff --git a/src/plugins/link_energy_wifi.cpp b/src/plugins/link_energy_wifi.cpp index c2ff738043..08e8a771b9 100644 --- a/src/plugins/link_energy_wifi.cpp +++ b/src/plugins/link_energy_wifi.cpp @@ -134,7 +134,6 @@ void LinkEnergyWifi::update(const simgrid::kernel::resource::NetworkAction&) auto const* wifi_link = static_cast(link_->get_impl()); - const kernel::lmm::Variable* var; const kernel::lmm::Element* elem = nullptr; /** @@ -147,7 +146,7 @@ void LinkEnergyWifi::update(const simgrid::kernel::resource::NetworkAction&) * used by the flow with the longest active time since the previous update */ double durUsage = 0; - while((var = wifi_link->get_constraint()->get_variable(&elem))) { + while (const auto* var = wifi_link->get_constraint()->get_variable(&elem)) { auto* action = static_cast(var->get_id()); XBT_DEBUG("cost: %f action value: %f link rate 1: %f link rate 2: %f", action->get_cost(), action->get_variable()->get_value(), wifi_link->get_host_rate(&action->get_src()),wifi_link->get_host_rate(&action->get_dst())); diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 97f826d7a5..ca6d7bf0a5 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -215,16 +215,14 @@ void Global::run_all_actors() void Global::wake_all_waiting_actors() const { for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models()) { - kernel::resource::Action* action; - XBT_DEBUG("Handling the failed actions (if any)"); - while ((action = model->extract_failed_action())) { + while (auto* action = model->extract_failed_action()) { XBT_DEBUG(" Handling Action %p", action); if (action->get_activity() != nullptr) kernel::activity::ActivityImplPtr(action->get_activity())->post(); } XBT_DEBUG("Handling the terminated actions (if any)"); - while ((action = model->extract_done_action())) { + while (auto* action = model->extract_done_action()) { XBT_DEBUG(" Handling Action %p", action); if (action->get_activity() == nullptr) XBT_DEBUG("probably vcpu's action %p, skip", action); diff --git a/src/surf/cpu_cas01.cpp b/src/surf/cpu_cas01.cpp index 8570cffd07..e11e31460f 100644 --- a/src/surf/cpu_cas01.cpp +++ b/src/surf/cpu_cas01.cpp @@ -86,13 +86,12 @@ bool CpuCas01::is_used() const /** @brief take into account changes of speed (either load or max) */ void CpuCas01::on_speed_change() { - const lmm::Variable* var; const lmm::Element* elem = nullptr; get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), get_core_count() * speed_.scale * speed_.peak); - while ((var = get_constraint()->get_variable(&elem))) { - const CpuCas01Action* action = static_cast(var->get_id()); + while (const auto* var = get_constraint()->get_variable(&elem)) { + const auto* action = static_cast(var->get_id()); get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), action->requested_core() * speed_.scale * speed_.peak); @@ -121,14 +120,12 @@ void CpuCas01::apply_event(profile::Event* event, double value) get_iface()->turn_on(); } } else { - const lmm::Constraint* cnst = get_constraint(); - const lmm::Variable* var; const lmm::Element* elem = nullptr; double date = surf_get_clock(); get_iface()->turn_off(); - while ((var = cnst->get_variable(&elem))) { + while (const auto* var = get_constraint()->get_variable(&elem)) { Action* action = var->get_id(); if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED || diff --git a/src/surf/network_cm02.cpp b/src/surf/network_cm02.cpp index f70895c7a1..c71a379b1c 100644 --- a/src/surf/network_cm02.cpp +++ b/src/surf/network_cm02.cpp @@ -367,11 +367,10 @@ void NetworkCm02Link::set_bandwidth(double value) if (sg_weight_S_parameter > 0) { double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale); - const kernel::lmm::Variable* var; const kernel::lmm::Element* elem = nullptr; const kernel::lmm::Element* nextelem = nullptr; int numelem = 0; - while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) { + while (const auto* var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem)) { auto* action = static_cast(var->get_id()); action->sharing_penalty_ += delta; if (not action->is_suspended()) @@ -385,7 +384,6 @@ LinkImpl* NetworkCm02Link::set_latency(double value) latency_check(value); double delta = value - latency_.peak; - const kernel::lmm::Variable* var; const kernel::lmm::Element* elem = nullptr; const kernel::lmm::Element* nextelem = nullptr; int numelem = 0; @@ -393,7 +391,7 @@ LinkImpl* NetworkCm02Link::set_latency(double value) latency_.scale = 1.0; latency_.peak = value; - while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) { + while (const auto* var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem)) { auto* action = static_cast(var->get_id()); action->lat_current_ += delta; action->sharing_penalty_ += delta; diff --git a/src/surf/network_interface.cpp b/src/surf/network_interface.cpp index 31147f7890..de68776856 100644 --- a/src/surf/network_interface.cpp +++ b/src/surf/network_interface.cpp @@ -138,10 +138,9 @@ void LinkImpl::turn_off() Resource::turn_off(); s4u::Link::on_state_change(this->piface_); - const kernel::lmm::Variable* var; const kernel::lmm::Element* elem = nullptr; double now = surf_get_clock(); - while ((var = get_constraint()->get_variable(&elem))) { + while (const auto* var = get_constraint()->get_variable(&elem)) { Action* action = var->get_id(); if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) { action->set_finish_time(now); diff --git a/src/surf/ptask_L07.cpp b/src/surf/ptask_L07.cpp index 14fb47d16e..027300fd66 100644 --- a/src/surf/ptask_L07.cpp +++ b/src/surf/ptask_L07.cpp @@ -282,11 +282,10 @@ bool CpuL07::is_used() const /** @brief take into account changes of speed (either load or max) */ void CpuL07::on_speed_change() { - const kernel::lmm::Variable* var; const kernel::lmm::Element* elem = nullptr; get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), speed_.peak * speed_.scale); - while ((var = get_constraint()->get_variable(&elem))) { + while (const auto* var = get_constraint()->get_variable(&elem)) { const kernel::resource::Action* action = var->get_id(); get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), speed_.scale * speed_.peak); @@ -368,13 +367,11 @@ void LinkL07::set_bandwidth(double value) kernel::resource::LinkImpl* LinkL07::set_latency(double value) { latency_check(value); - const kernel::lmm::Variable* var; - L07Action* action; const kernel::lmm::Element* elem = nullptr; latency_.peak = value; - while ((var = get_constraint()->get_variable(&elem))) { - action = static_cast(var->get_id()); + while (const auto* var = get_constraint()->get_variable(&elem)) { + auto* action = static_cast(var->get_id()); action->updateBound(); } return this; diff --git a/src/surf/surf_c_bindings.cpp b/src/surf/surf_c_bindings.cpp index c5b9139231..771f6a8f78 100644 --- a/src/surf/surf_c_bindings.cpp +++ b/src/surf/surf_c_bindings.cpp @@ -29,10 +29,9 @@ void surf_presolve() if (next_event_date > NOW) break; - simgrid::kernel::profile::Event* event; double value = -1.0; simgrid::kernel::resource::Resource* resource = nullptr; - while ((event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource))) { + while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) { if (value >= 0) resource->apply_event(event, value); } @@ -67,7 +66,6 @@ double surf_solve(double max_date) double time_delta = -1.0; /* duration */ double value = -1.0; simgrid::kernel::resource::Resource* resource = nullptr; - simgrid::kernel::profile::Event* event = nullptr; if (max_date != -1.0) { xbt_assert(max_date >= NOW, "You asked to simulate up to %f, but that's in the past already", max_date); @@ -130,7 +128,7 @@ double surf_solve(double max_date) XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", time_delta, NOW, next_event_date); - while ((event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource))) { + while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) { if (resource->is_used() || (watched_hosts().find(resource->get_cname()) != watched_hosts().end())) { time_delta = next_event_date - NOW; XBT_DEBUG("This event invalidates the next_occurring_event() computation of models. Next event set to %f", -- 2.20.1