From c076aa56c88595b4f625df015a02aa870ab3947d Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 28 Feb 2019 23:22:03 +0100 Subject: [PATCH] Make destructors useless. --- examples/s4u/dht-kademlia/answer.cpp | 2 +- examples/s4u/dht-kademlia/answer.hpp | 2 +- examples/s4u/dht-kademlia/message.hpp | 11 ++++------ examples/s4u/dht-kademlia/node.cpp | 14 ++++++------- examples/s4u/dht-kademlia/node.hpp | 5 ++--- examples/s4u/dht-kademlia/routing_table.cpp | 23 +++++++-------------- examples/s4u/dht-kademlia/routing_table.hpp | 9 ++++---- 7 files changed, 26 insertions(+), 40 deletions(-) diff --git a/examples/s4u/dht-kademlia/answer.cpp b/examples/s4u/dht-kademlia/answer.cpp index 4c53034944..0afc935e84 100644 --- a/examples/s4u/dht-kademlia/answer.cpp +++ b/examples/s4u/dht-kademlia/answer.cpp @@ -69,7 +69,7 @@ bool Answer::destinationFound() /** @brief Adds the content of a bucket unsigned into a answer object. * @param bucket the bucket we have to had unsigned into */ -void Answer::addBucket(Bucket* bucket) +void Answer::addBucket(const Bucket* bucket) { xbt_assert((bucket != nullptr), "Provided a NULL bucket"); diff --git a/examples/s4u/dht-kademlia/answer.hpp b/examples/s4u/dht-kademlia/answer.hpp index 1ff56eac82..ab93e99a7e 100644 --- a/examples/s4u/dht-kademlia/answer.hpp +++ b/examples/s4u/dht-kademlia/answer.hpp @@ -29,7 +29,7 @@ public: unsigned int merge(Answer* a); void trim(); bool destinationFound(); - void addBucket(kademlia::Bucket* bucket); + void addBucket(const kademlia::Bucket* bucket); }; } diff --git a/examples/s4u/dht-kademlia/message.hpp b/examples/s4u/dht-kademlia/message.hpp index 04306c0aae..9e32c59dac 100644 --- a/examples/s4u/dht-kademlia/message.hpp +++ b/examples/s4u/dht-kademlia/message.hpp @@ -9,6 +9,8 @@ #include "s4u-dht-kademlia.hpp" #include "simgrid/s4u.hpp" +#include + namespace kademlia { class Message { @@ -17,7 +19,7 @@ public: unsigned int destination_id_ = 0; // Id we are trying to find, if needed. Answer* answer_ = nullptr; // Answer to the request made, if needed. simgrid::s4u::MailboxPtr answer_to_ = nullptr; // mailbox to send the answer to (if not an answer). - char* issuer_host_name_ = nullptr; // used for logging + std::string issuer_host_name_; // used for logging explicit Message(unsigned int sender_id, unsigned int destination_id, Answer* answer, simgrid::s4u::MailboxPtr mailbox, const char* hostname) @@ -25,7 +27,7 @@ public: , destination_id_(destination_id) , answer_(answer) , answer_to_(mailbox) - , issuer_host_name_(xbt_strdup(hostname)) + , issuer_host_name_(hostname) { } explicit Message(unsigned int sender_id, unsigned int destination_id, simgrid::s4u::MailboxPtr mailbox, @@ -35,11 +37,6 @@ public: } Message(const Message&) = delete; Message& operator=(const Message&) = delete; - ~Message() - { - if (issuer_host_name_) - xbt_free(issuer_host_name_); - } }; } #endif diff --git a/examples/s4u/dht-kademlia/node.cpp b/examples/s4u/dht-kademlia/node.cpp index ead02fe4e3..7956dedcbc 100644 --- a/examples/s4u/dht-kademlia/node.cpp +++ b/examples/s4u/dht-kademlia/node.cpp @@ -57,7 +57,7 @@ bool Node::join(unsigned int known_id) } while (not got_answer); /* Second step: Send a FIND_NODE to a a random node in buckets */ - unsigned int bucket_id = table->findBucket(known_id)->getId(); + unsigned int bucket_id = table.findBucket(known_id)->getId(); xbt_assert(bucket_id <= IDENTIFIER_SIZE); for (i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) { if (bucket_id > i) { @@ -118,7 +118,7 @@ unsigned int Node::sendFindNodeToBest(Answer* node_list) void Node::routingTableUpdate(unsigned int id) { // retrieval of the bucket in which the should be - Bucket* bucket = table->findBucket(id); + Bucket* bucket = table.findBucket(id); // check if the id is already in the bucket. auto id_pos = std::find(bucket->nodes.begin(), bucket->nodes.end(), id); @@ -146,7 +146,7 @@ Answer* Node::findClosest(unsigned int destination_id) { Answer* answer = new Answer(destination_id); /* We find the corresponding bucket for the id */ - Bucket* bucket = table->findBucket(destination_id); + const Bucket* bucket = table.findBucket(destination_id); int bucket_id = bucket->getId(); xbt_assert((bucket_id <= IDENTIFIER_SIZE), "Bucket found has a wrong identifier"); /* So, we copy the contents of the bucket unsigned into our answer */ @@ -158,12 +158,12 @@ Answer* Node::findClosest(unsigned int destination_id) for (int i = 1; answer->getSize() < BUCKET_SIZE && ((bucket_id - i > 0) || (bucket_id + i < IDENTIFIER_SIZE)); i++) { /* We check the previous buckets */ if (bucket_id - i >= 0) { - Bucket* bucket_p = table->buckets[bucket_id - i]; + const Bucket* bucket_p = &table.buckets[bucket_id - i]; answer->addBucket(bucket_p); } /* We check the next buckets */ if (bucket_id + i <= IDENTIFIER_SIZE) { - Bucket* bucket_n = table->buckets[bucket_id + i]; + const Bucket* bucket_n = &table.buckets[bucket_id + i]; answer->addBucket(bucket_n); } } @@ -217,7 +217,7 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) nodes_added = node_list->merge(msg->answer_); XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", msg->answer_to_->get_cname(), - msg->issuer_host_name_, msg->answer_->nodes.size()); + msg->issuer_host_name_.c_str(), msg->answer_->nodes.size()); } else { if (msg->answer_) { routingTableUpdate(msg->sender_id_); @@ -272,7 +272,7 @@ void Node::handleFindNode(Message* msg) { routingTableUpdate(msg->sender_id_); XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x", msg->answer_to_->get_cname(), - msg->issuer_host_name_, msg->destination_id_); + msg->issuer_host_name_.c_str(), msg->destination_id_); // Building the answer to the request Message* answer = new Message(id_, msg->destination_id_, findClosest(msg->destination_id_), diff --git a/examples/s4u/dht-kademlia/node.hpp b/examples/s4u/dht-kademlia/node.hpp index 27fa2c76fe..0aa8d80310 100644 --- a/examples/s4u/dht-kademlia/node.hpp +++ b/examples/s4u/dht-kademlia/node.hpp @@ -15,16 +15,15 @@ namespace kademlia { class Node { unsigned int id_; // node id - 160 bits - RoutingTable* table = nullptr; // node routing table + RoutingTable table; // node routing table public: simgrid::s4u::CommPtr receive_comm; void* received_msg = nullptr; unsigned int find_node_success = 0; // Number of find_node which have succeeded. unsigned int find_node_failed = 0; // Number of find_node which have failed. - explicit Node(unsigned int node_id) : id_(node_id), table(new RoutingTable(node_id)), receive_comm(nullptr) {} + explicit Node(unsigned int node_id) : id_(node_id), table(node_id), receive_comm(nullptr) {} Node(const Node&) = delete; Node& operator=(const Node&) = delete; - ~Node() { delete table; } unsigned int getId() { return id_; } bool join(unsigned int known_id); diff --git a/examples/s4u/dht-kademlia/routing_table.cpp b/examples/s4u/dht-kademlia/routing_table.cpp index 341153ba26..ffc664c8d2 100644 --- a/examples/s4u/dht-kademlia/routing_table.cpp +++ b/examples/s4u/dht-kademlia/routing_table.cpp @@ -14,29 +14,20 @@ namespace kademlia { /** @brief Initialization of a node routing table. */ RoutingTable::RoutingTable(unsigned int node_id) : id_(node_id) { - buckets = new Bucket*[IDENTIFIER_SIZE + 1]; + buckets.reserve(IDENTIFIER_SIZE + 1); for (unsigned int i = 0; i < IDENTIFIER_SIZE + 1; i++) - buckets[i] = new Bucket(i); + buckets.emplace_back(i); } -RoutingTable::~RoutingTable() -{ - // Free the buckets. - for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) { - delete buckets[i]; - } - delete[] buckets; -} - -void RoutingTable::print() +void RoutingTable::print() const { XBT_INFO("Routing table of %08x:", id_); for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) { - if (not buckets[i]->nodes.empty()) { + if (not buckets[i].nodes.empty()) { XBT_INFO("Bucket number %u: ", i); int j = 0; - for (auto value : buckets[i]->nodes) { + for (auto value : buckets[i].nodes) { XBT_INFO("Element %d: %08x", j, value); j++; } @@ -53,13 +44,13 @@ Bucket* RoutingTable::findBucket(unsigned int id) unsigned int xor_number = id_ ^ id; unsigned int prefix = get_node_prefix(xor_number, IDENTIFIER_SIZE); xbt_assert(prefix <= IDENTIFIER_SIZE, "Tried to return a bucket that doesn't exist."); - return buckets[prefix]; + return &buckets[prefix]; } /** Returns if the routing table contains the id. */ bool RoutingTable::contains(unsigned int node_id) { - Bucket* bucket = findBucket(node_id); + const Bucket* bucket = findBucket(node_id); return std::find(bucket->nodes.begin(), bucket->nodes.end(), node_id) != bucket->nodes.end(); } } diff --git a/examples/s4u/dht-kademlia/routing_table.hpp b/examples/s4u/dht-kademlia/routing_table.hpp index 08656fdab3..4d8258aae8 100644 --- a/examples/s4u/dht-kademlia/routing_table.hpp +++ b/examples/s4u/dht-kademlia/routing_table.hpp @@ -8,6 +8,7 @@ #define _KADEMLIA_ROUTING_TABLE_HPP #include "s4u-dht-kademlia.hpp" #include +#include namespace kademlia { @@ -16,21 +17,19 @@ class Bucket { unsigned int id_; // bucket id public: std::deque nodes; // Nodes in the bucket. - unsigned int getId() { return id_; } + unsigned int getId() const { return id_; } explicit Bucket(unsigned int id) : id_(id) {} - ~Bucket() = default; }; /* Node routing table */ class RoutingTable { unsigned int id_; // node id of the client's routing table public: - Bucket** buckets; // Node bucket list - 160 sized. + std::vector buckets; // Node bucket list - 160 sized. explicit RoutingTable(unsigned int node_id); RoutingTable(const RoutingTable&) = delete; RoutingTable& operator=(const RoutingTable&) = delete; - ~RoutingTable(); - void print(); + void print() const; Bucket* findBucket(unsigned int id); bool contains(unsigned int node_id); }; -- 2.20.1