From 0020fc32c148966748b6ae6b9f144791c62c326a Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 23 Nov 2020 10:53:21 +0100 Subject: [PATCH] Use std::unique_ptr for Message::answer_. --- examples/s4u/dht-kademlia/message.hpp | 9 +++++---- examples/s4u/dht-kademlia/node.cpp | 17 ++++++----------- examples/s4u/dht-kademlia/node.hpp | 4 +++- examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp | 1 - 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/examples/s4u/dht-kademlia/message.hpp b/examples/s4u/dht-kademlia/message.hpp index 15f5e5fb87..a51b902783 100644 --- a/examples/s4u/dht-kademlia/message.hpp +++ b/examples/s4u/dht-kademlia/message.hpp @@ -9,6 +9,7 @@ #include "s4u-dht-kademlia.hpp" #include "simgrid/s4u.hpp" +#include #include namespace kademlia { @@ -17,15 +18,15 @@ class Message { public: unsigned int sender_id_ = 0; // Id of the guy who sent the task unsigned int destination_id_ = 0; // Id we are trying to find, if needed. - Answer* answer_ = nullptr; // Answer to the request made, if needed. + std::unique_ptr answer_ = nullptr; // Answer to the request made, if needed. simgrid::s4u::Mailbox* answer_to_ = nullptr; // mailbox to send the answer to (if not an answer). std::string issuer_host_name_; // used for logging - explicit Message(unsigned int sender_id, unsigned int destination_id, Answer* answer, simgrid::s4u::Mailbox* mailbox, - const char* hostname) + explicit Message(unsigned int sender_id, unsigned int destination_id, std::unique_ptr answer, + simgrid::s4u::Mailbox* mailbox, const char* hostname) : sender_id_(sender_id) , destination_id_(destination_id) - , answer_(answer) + , answer_(std::move(answer)) , answer_to_(mailbox) , issuer_host_name_(hostname) { diff --git a/examples/s4u/dht-kademlia/node.cpp b/examples/s4u/dht-kademlia/node.cpp index 86130d5713..d72a5c3028 100644 --- a/examples/s4u/dht-kademlia/node.cpp +++ b/examples/s4u/dht-kademlia/node.cpp @@ -12,7 +12,6 @@ namespace kademlia { static void destroy(void* message) { const auto* msg = static_cast(message); - delete msg->answer_; delete msg; } @@ -22,7 +21,6 @@ static void destroy(void* message) */ bool Node::join(unsigned int known_id) { - const Answer* node_list; bool got_answer = false; /* Add the guy we know to our routing table and ourselves. */ @@ -41,14 +39,13 @@ bool Node::join(unsigned int known_id) got_answer = true; // retrieve the node list and ping them. const auto* msg = static_cast(received_msg); - node_list = msg->answer_; + const Answer* node_list = msg->answer_.get(); if (node_list) { for (auto const& contact : node_list->getNodes()) routingTableUpdate(contact.first); } else { handleFindNode(msg); } - delete msg->answer_; delete msg; receive_comm = nullptr; } else @@ -142,9 +139,9 @@ void Node::routingTableUpdate(unsigned int id) * @param node : our node * @param destination_id : the id of the guy we are trying to find */ -Answer* Node::findClosest(unsigned int destination_id) +std::unique_ptr Node::findClosest(unsigned int destination_id) { - auto* answer = new Answer(destination_id); + auto answer = std::make_unique(destination_id); /* We find the corresponding bucket for the id */ const Bucket* bucket = table.findBucket(destination_id); int bucket_id = bucket->getId(); @@ -186,14 +183,14 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) unsigned int steps = 0; /* First we build a list of who we already know */ - Answer* node_list = findClosest(id_to_find); + std::unique_ptr node_list = findClosest(id_to_find); xbt_assert((node_list != nullptr), "node_list incorrect"); XBT_DEBUG("Doing a FIND_NODE on %08x", id_to_find); /* Ask the nodes on our list if they have information about the node we are trying to find */ do { answers = 0; - queries = sendFindNodeToBest(node_list); + queries = sendFindNodeToBest(node_list.get()); nodes_added = 0; double timeout = simgrid::s4u::Engine::get_clock() + FIND_NODE_TIMEOUT; steps++; @@ -214,7 +211,7 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) routingTableUpdate(contact.first); answers++; - nodes_added = node_list->merge(msg->answer_); + nodes_added = node_list->merge(msg->answer_.get()); XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", msg->answer_to_->get_cname(), msg->issuer_host_name_.c_str(), msg->answer_->getSize()); } else { @@ -228,7 +225,6 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) timeout += simgrid::s4u::Engine::get_clock() - time_beginreceive; time_beginreceive = simgrid::s4u::Engine::get_clock(); } - delete msg->answer_; delete msg; receive_comm = nullptr; } else { @@ -251,7 +247,6 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) XBT_VERB("%08x not found in %u steps", id_to_find, steps); } } - delete node_list; return destination_found; } diff --git a/examples/s4u/dht-kademlia/node.hpp b/examples/s4u/dht-kademlia/node.hpp index f65106c77a..c508c06820 100644 --- a/examples/s4u/dht-kademlia/node.hpp +++ b/examples/s4u/dht-kademlia/node.hpp @@ -11,6 +11,8 @@ #include "routing_table.hpp" #include "s4u-dht-kademlia.hpp" +#include + namespace kademlia { class Node { @@ -30,7 +32,7 @@ public: void sendFindNode(unsigned int id, unsigned int destination) const; unsigned int sendFindNodeToBest(const Answer* node_list) const; void routingTableUpdate(unsigned int id); - Answer* findClosest(unsigned int destination_id); + std::unique_ptr findClosest(unsigned int destination_id); bool findNode(unsigned int id_to_find, bool count_in_stats); void randomLookup(); void handleFindNode(const Message* msg); diff --git a/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp b/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp index dd3acdddf0..e8fcbcda6f 100644 --- a/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp +++ b/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp @@ -55,7 +55,6 @@ static void node(std::vector args) const auto* msg = static_cast(node.received_msg); if (msg) { node.handleFindNode(msg); - delete msg->answer_; delete msg; node.receive_comm = nullptr; } else -- 2.20.1