From 96b18f4b65694f66c8aea2d702b6f087fdcf9cb7 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 16 Oct 2016 02:01:22 +0200 Subject: [PATCH 1/1] kill a stupid function. Let's do regular C++ --- include/simgrid/s4u/host.hpp | 7 ++----- src/s4u/s4u_host.cpp | 9 +-------- src/simix/smx_vm.cpp | 14 +++++++++----- src/surf/network_ns3.cpp | 2 +- src/surf/sg_platf.cpp | 4 +--- src/surf/virtual_machine.cpp | 7 +++---- 6 files changed, 17 insertions(+), 26 deletions(-) diff --git a/include/simgrid/s4u/host.hpp b/include/simgrid/s4u/host.hpp index 698e64fc14..4e59267257 100644 --- a/include/simgrid/s4u/host.hpp +++ b/include/simgrid/s4u/host.hpp @@ -42,12 +42,9 @@ namespace s4u { XBT_PUBLIC_CLASS Host : public simgrid::xbt::Extendable { -private: +public: explicit Host(const char *name); -public: // TODO, make me private - ~Host(); - /** Do not use this function, it should be private */ - static Host* by_name_or_create(const char* name); + ~Host(); // TODO, make me private /** Retrieves an host from its name, or return nullptr */ static Host* by_name_or_null(const char* name); diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp index ce44cd7cdf..3181507957 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -40,6 +40,7 @@ simgrid::xbt::signal Host::onStateChange; Host::Host(const char* name) : name_(name) { + xbt_assert(sg_host_by_name(name) == nullptr, "Refusing to create a second host named '%s'.", name); xbt_dict_set(host_list, name, this, nullptr); } @@ -66,14 +67,6 @@ Host* Host::by_name_or_null(const char* name) }); return (Host*) xbt_dict_get_or_null(host_list, name); } -Host* Host::by_name_or_create(const char* name) -{ - Host* host = by_name_or_null(name); - if (host == nullptr) - host = new Host(name); - - return host; -} Host *Host::current(){ smx_actor_t smx_proc = SIMIX_process_self(); diff --git a/src/simix/smx_vm.cpp b/src/simix/smx_vm.cpp index faecc817c4..6c99ae8ded 100644 --- a/src/simix/smx_vm.cpp +++ b/src/simix/smx_vm.cpp @@ -298,11 +298,12 @@ void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm) SIMIX_vm_shutdown(vm, simcall->issuer); } -/** - * @brief Function to destroy a SIMIX VM host. - * - * @param vm the vm host to destroy (a sg_host_t) - */ +extern xbt_dict_t host_list; // FIXME:killme don't dupplicate the content of s4u::Host this way + /** + * @brief Function to destroy a SIMIX VM host. + * + * @param vm the vm host to destroy (a sg_host_t) + */ void SIMIX_vm_destroy(sg_host_t vm) { /* this code basically performs a similar thing like SIMIX_host_destroy() */ @@ -319,4 +320,7 @@ void SIMIX_vm_destroy(sg_host_t vm) /* Don't free these things twice: they are the ones of my physical host */ vm->pimpl_cpu = nullptr; vm->pimpl_netcard = nullptr; + + if (xbt_dict_get_or_null(host_list, vm->name().c_str())) + xbt_dict_remove(host_list, vm->name().c_str()); } diff --git a/src/surf/network_ns3.cpp b/src/surf/network_ns3.cpp index fed3633107..515ce2cd9b 100644 --- a/src/surf/network_ns3.cpp +++ b/src/surf/network_ns3.cpp @@ -73,7 +73,7 @@ static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster) for (int i : *cluster->radicals) { char* router_id = bprintf("router_%s%d%s", cluster->prefix, i, cluster->suffix); - simgrid::s4u::Host* router = simgrid::s4u::Host::by_name_or_create(router_id); + simgrid::s4u::Host* router = new simgrid::s4u::Host(router_id); ns3_add_host(*router); // Create private link diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 632ae08bd6..59997d93a6 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -75,8 +75,6 @@ void sg_platf_exit() { /** @brief Add an "host" to the current AS */ void sg_platf_new_host(sg_platf_host_cbarg_t host) { - xbt_assert(sg_host_by_name(host->id) == nullptr, "Refusing to create a second host named '%s'.", host->id); - simgrid::kernel::routing::AsImpl* current_routing = routing_get_current(); if (current_routing->hierarchy_ == simgrid::kernel::routing::AsImpl::RoutingMode::unset) current_routing->hierarchy_ = simgrid::kernel::routing::AsImpl::RoutingMode::base; @@ -84,7 +82,7 @@ void sg_platf_new_host(sg_platf_host_cbarg_t host) simgrid::kernel::routing::NetCard *netcard = new simgrid::kernel::routing::NetCardImpl(host->id, simgrid::kernel::routing::NetCard::Type::Host, current_routing); - sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id); + simgrid::s4u::Host* h = new simgrid::s4u::Host(host->id); h->pimpl_netcard = netcard; if(mount_list) { diff --git a/src/surf/virtual_machine.cpp b/src/surf/virtual_machine.cpp index 31de254608..79a112be83 100644 --- a/src/surf/virtual_machine.cpp +++ b/src/surf/virtual_machine.cpp @@ -109,7 +109,7 @@ VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u: { /* Register this VM to the list of all VMs */ allVms_.push_back(this); - piface_ = simgrid::s4u::Host::by_name_or_create(name); + piface_ = new simgrid::s4u::Host(name); piface_->extension_set(this); /* Currently, we assume a VM has no storage. */ @@ -121,7 +121,7 @@ VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u: * from the VM name, we have to make sure that the system does not call the * free callback for the network resource object. The network resource object * is still used by the physical machine. */ - sg_host_t host_VM = simgrid::s4u::Host::by_name_or_create(name); + sg_host_t host_VM = piface_; host_VM->pimpl_netcard = host_PM->pimpl_netcard; vmState_ = SURF_VM_STATE_CREATED; @@ -130,8 +130,7 @@ VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u: // Roughly, create a vcpu resource by using the values of the sub_cpu one. CpuCas01 *sub_cpu = dynamic_cast(host_PM->pimpl_cpu); - cpu_ = surf_cpu_model_vm->createCpu(host_VM, // the machine hosting the VM - sub_cpu->getSpeedPeakList(), 1 /*cores*/); + cpu_ = surf_cpu_model_vm->createCpu(host_VM, sub_cpu->getSpeedPeakList(), 1 /*cores*/); if (sub_cpu->getPState() != 0) cpu_->setPState(sub_cpu->getPState()); -- 2.20.1