Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make destructors useless.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 28 Feb 2019 22:22:03 +0000 (23:22 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 28 Feb 2019 22:22:03 +0000 (23:22 +0100)
examples/s4u/dht-kademlia/answer.cpp
examples/s4u/dht-kademlia/answer.hpp
examples/s4u/dht-kademlia/message.hpp
examples/s4u/dht-kademlia/node.cpp
examples/s4u/dht-kademlia/node.hpp
examples/s4u/dht-kademlia/routing_table.cpp
examples/s4u/dht-kademlia/routing_table.hpp

index 4c53034..0afc935 100644 (file)
@@ -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");
 
index 1ff56ea..ab93e99 100644 (file)
@@ -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);
 };
 }
 
index 04306c0..9e32c59 100644 (file)
@@ -9,6 +9,8 @@
 #include "s4u-dht-kademlia.hpp"
 #include "simgrid/s4u.hpp"
 
+#include <string>
+
 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
index ead02fe..7956ded 100644 (file)
@@ -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_),
index 27fa2c7..0aa8d80 100644 (file)
@@ -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);
index 341153b..ffc664c 100644 (file)
@@ -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();
 }
 }
index 08656fd..4d8258a 100644 (file)
@@ -8,6 +8,7 @@
 #define _KADEMLIA_ROUTING_TABLE_HPP
 #include "s4u-dht-kademlia.hpp"
 #include <deque>
+#include <vector>
 
 namespace kademlia {
 
@@ -16,21 +17,19 @@ class Bucket {
   unsigned int id_; // bucket id
 public:
   std::deque<unsigned int> 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<Bucket> 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);
 };