From 164982c63a33706699d201839b06aeae0f39e9f6 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 14 Nov 2018 12:30:27 +0100 Subject: [PATCH] pass the dht item object by const reference instead of by value --- include/libtorrent/kademlia/item.hpp | 6 +++--- include/libtorrent/kademlia/put_data.hpp | 3 ++- src/kademlia/node.cpp | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/libtorrent/kademlia/item.hpp b/include/libtorrent/kademlia/item.hpp index 06b47e440..718ad272c 100644 --- a/include/libtorrent/kademlia/item.hpp +++ b/include/libtorrent/kademlia/item.hpp @@ -74,7 +74,7 @@ TORRENT_EXPORT signature sign_mutable_item( class TORRENT_EXTRA_EXPORT item { public: - item() : m_seq(0), m_mutable(false) {} + item() {} item(public_key const& pk, span salt); explicit item(entry v); item(entry v @@ -117,8 +117,8 @@ private: std::string m_salt; public_key m_pk; signature m_sig; - sequence_number m_seq; - bool m_mutable; + sequence_number m_seq{0}; + bool m_mutable = false; }; } } // namespace libtorrent::dht diff --git a/include/libtorrent/kademlia/put_data.hpp b/include/libtorrent/kademlia/put_data.hpp index a21502a7f..61e198004 100644 --- a/include/libtorrent/kademlia/put_data.hpp +++ b/include/libtorrent/kademlia/put_data.hpp @@ -54,7 +54,8 @@ struct put_data: traversal_algorithm char const* name() const override; void start() override; - void set_data(item const& data) { m_data = data; } + void set_data(item&& data) { m_data = std::move(data); } + void set_data(item const& data) = delete; void set_targets(std::vector> const& targets); diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 2f82e96b7..5577d7906 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -516,15 +516,16 @@ void put(std::vector> const& nodes ta->start(); } -void put_data_cb(item i, bool auth +void put_data_cb(item const& i, bool auth , std::shared_ptr const& ta , std::function const& f) { // call data_callback only when we got authoritative data. if (auth) { - f(i); - ta->set_data(i); + item copy(i); + f(copy); + ta->set_data(std::move(copy)); } } @@ -543,7 +544,7 @@ void node::put_item(sha1_hash const& target, entry const& data, std::function(*this, std::bind(f, _2)); - put_ta->set_data(i); + put_ta->set_data(std::move(i)); auto ta = std::make_shared(*this, target , get_item::data_callback(), std::bind(&put, _1, put_ta));