From 4c53d7b2da309a7c5ede5f8cb2eef8fee1b645fc Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Tue, 6 Sep 2016 23:37:36 -0400 Subject: [PATCH] refactor to use std unordered containers and minor cleanup (#1070) --- examples/client_test.cpp | 1 + examples/torrent_view.cpp | 9 ++++----- examples/torrent_view.hpp | 5 +++-- include/libtorrent/allocator.hpp | 1 - include/libtorrent/block_cache.hpp | 20 ++++++++------------ include/libtorrent/create_torrent.hpp | 4 +--- include/libtorrent/disk_buffer_pool.hpp | 1 - include/libtorrent/disk_job_pool.hpp | 4 +--- include/libtorrent/io_service_fwd.hpp | 19 +++---------------- include/libtorrent/sha1_hash.hpp | 13 +++---------- include/libtorrent/torrent_handle.hpp | 21 +++++++++++++++------ include/libtorrent/torrent_status.hpp | 12 ++++++++++++ include/libtorrent/tracker_manager.hpp | 15 ++++++--------- src/block_cache.cpp | 4 ++-- src/lsd.cpp | 1 - src/session_call.cpp | 7 +++---- src/torrent.cpp | 11 +++++------ src/torrent_handle.cpp | 6 ------ test/test_storage.cpp | 2 -- 19 files changed, 67 insertions(+), 89 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 2f26d96aa..f463e2c5f 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include // for atoi #include #include +#include #include "libtorrent/config.hpp" diff --git a/examples/torrent_view.cpp b/examples/torrent_view.cpp index aaf78bed4..c233fe055 100644 --- a/examples/torrent_view.cpp +++ b/examples/torrent_view.cpp @@ -103,7 +103,7 @@ void torrent_view::update_torrents(std::vector const& st) for (std::vector::const_iterator i = st.begin(); i != st.end(); ++i) { - boost::unordered_set::iterator j = m_all_handles.find(*i); + auto j = m_all_handles.find(*i); // add new entries here if (j == m_all_handles.end()) { @@ -370,11 +370,10 @@ void torrent_view::update_filtered_torrents() { m_scroll_position = 0; m_filtered_handles.clear(); - for (boost::unordered_set::iterator i = m_all_handles.begin() - , end(m_all_handles.end()); i != end; ++i) + for (auto const& h : m_all_handles) { - if (!show_torrent(*i)) continue; - m_filtered_handles.push_back(&*i); + if (!show_torrent(h)) continue; + m_filtered_handles.push_back(&h); } if (m_active_torrent >= int(m_filtered_handles.size())) m_active_torrent = int(m_filtered_handles.size()) - 1; if (m_active_torrent < 0) m_active_torrent = 0; diff --git a/examples/torrent_view.hpp b/examples/torrent_view.hpp index 0060b8c47..e67fa94e1 100644 --- a/examples/torrent_view.hpp +++ b/examples/torrent_view.hpp @@ -3,9 +3,10 @@ #include #include -#include +#include #include "libtorrent/torrent_handle.hpp" +#include "libtorrent/torrent_status.hpp" namespace lt = libtorrent; @@ -62,7 +63,7 @@ private: void update_filtered_torrents(); // all torrents - boost::unordered_set m_all_handles; + std::unordered_set m_all_handles; // pointers into m_all_handles of the remaining torrents after filtering std::vector m_filtered_handles; diff --git a/include/libtorrent/allocator.hpp b/include/libtorrent/allocator.hpp index 3c04aaea6..625f28b4e 100644 --- a/include/libtorrent/allocator.hpp +++ b/include/libtorrent/allocator.hpp @@ -56,4 +56,3 @@ namespace libtorrent } #endif - diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index 992e33642..474df8e9f 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -34,15 +34,13 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_BLOCK_CACHE #include "libtorrent/aux_/disable_warnings_push.hpp" - -#include #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" #include #include #include +#include #include "libtorrent/time.hpp" #include "libtorrent/error_code.hpp" @@ -329,12 +327,6 @@ namespace libtorrent #endif }; - // internal - inline std::size_t hash_value(cached_piece_entry const& p) - { - return std::size_t(p.storage.get()) + std::size_t(p.piece); - } - struct TORRENT_EXTRA_EXPORT block_cache : disk_buffer_pool { block_cache(int block_size, io_service& ios @@ -342,11 +334,15 @@ namespace libtorrent private: - typedef boost::unordered_set cache_t; + struct hash_value + { + std::size_t operator()(cached_piece_entry const& p) const + { return std::size_t(p.storage.get()) + std::size_t(p.piece); } + }; + typedef std::unordered_set cache_t; public: - typedef cache_t::iterator iterator; typedef cache_t::const_iterator const_iterator; // returns the number of blocks this job would cause to be read in @@ -357,7 +353,7 @@ namespace libtorrent // returns a range of all pieces. This might be a very // long list, use carefully - std::pair all_pieces() const; + std::pair all_pieces() const; int num_pieces() const { return int(m_pieces.size()); } list_iterator write_lru_pieces() const diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 0f4b2488e..f3dad5fc8 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -40,14 +40,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hasher.hpp" #include "libtorrent/file.hpp" // for combine_path etc. -#include "libtorrent/aux_/disable_warnings_push.hpp" - #include #include #include +#include "libtorrent/aux_/disable_warnings_push.hpp" #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" // OVERVIEW diff --git a/include/libtorrent/disk_buffer_pool.hpp b/include/libtorrent/disk_buffer_pool.hpp index 93d1e76d6..15232da8f 100644 --- a/include/libtorrent/disk_buffer_pool.hpp +++ b/include/libtorrent/disk_buffer_pool.hpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" -#include #ifndef TORRENT_DISABLE_POOL_ALLOCATOR #include "libtorrent/allocator.hpp" // for page_aligned_allocator diff --git a/include/libtorrent/disk_job_pool.hpp b/include/libtorrent/disk_job_pool.hpp index 95b48c616..dc7eab021 100644 --- a/include/libtorrent/disk_job_pool.hpp +++ b/include/libtorrent/disk_job_pool.hpp @@ -37,9 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - +#include #include "libtorrent/aux_/disable_warnings_pop.hpp" namespace libtorrent diff --git a/include/libtorrent/io_service_fwd.hpp b/include/libtorrent/io_service_fwd.hpp index c14997d82..6b4922742 100644 --- a/include/libtorrent/io_service_fwd.hpp +++ b/include/libtorrent/io_service_fwd.hpp @@ -33,29 +33,17 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_IO_SERVICE_FWD_HPP_INCLUDED #define TORRENT_IO_SERVICE_FWD_HPP_INCLUDED -#ifdef __OBJC__ -#define Protocol Protocol_ -#endif - -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" - -#ifdef __OBJC__ -#undef Protocol -#endif +#include "libtorrent/config.hpp" #if defined TORRENT_BUILD_SIMULATOR namespace sim { namespace asio { struct io_service; }} -#endif - +#else namespace boost { namespace asio { class io_service; }} +#endif namespace libtorrent { @@ -67,4 +55,3 @@ namespace libtorrent } #endif - diff --git a/include/libtorrent/sha1_hash.hpp b/include/libtorrent/sha1_hash.hpp index 502d66174..f5f5cff3c 100644 --- a/include/libtorrent/sha1_hash.hpp +++ b/include/libtorrent/sha1_hash.hpp @@ -277,14 +277,7 @@ namespace libtorrent // peer IDs, node IDs etc. using sha1_hash = digest32<160>; - // this is here to support usage of sha1_hash in boost unordered containers typedef sha1_hash peer_id; - inline std::size_t hash_value(sha1_hash const& b) - { - std::size_t ret; - std::memcpy(&ret, &b[0], sizeof(ret)); - return ret; - } #if TORRENT_USE_IOSTREAM @@ -297,8 +290,8 @@ namespace libtorrent #endif // TORRENT_USE_IOSTREAM } -namespace std { - +namespace std +{ template <> struct hash { @@ -306,7 +299,7 @@ namespace std { { std::size_t ret; // this is OK because sha1_hash is already a hash - std::memcpy(&ret, k.data(), sizeof(ret)); + std::memcpy(&ret, &k[0], sizeof(ret)); return ret; } }; diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 8cdca4565..ba94a6826 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -71,9 +71,6 @@ namespace libtorrent struct storage_interface; class torrent; - // allows torrent_handle to be used in unordered_map and unordered_set. - TORRENT_EXPORT std::size_t hash_value(torrent_status const& ts); - #ifndef BOOST_NO_EXCEPTIONS void throw_invalid_handle() TORRENT_NO_RETURN; #endif @@ -199,8 +196,8 @@ namespace libtorrent state_t piece_state; }; - // for boost::hash (and to support using this type in unordered_map etc.) - std::size_t hash_value(torrent_handle const& h); + // for std::hash (and to support using this type in unordered_map etc.) + TORRENT_EXPORT std::size_t hash_value(torrent_handle const& h); // You will usually have to store your torrent handles somewhere, since it's // the object through which you retrieve information about the torrent and @@ -241,7 +238,7 @@ namespace libtorrent friend struct aux::session_impl; friend struct session_handle; friend class torrent; - friend std::size_t hash_value(torrent_handle const& th); + friend TORRENT_EXPORT std::size_t hash_value(torrent_handle const& th); // constructs a torrent handle that does not refer to a torrent. // i.e. is_valid() will return false. @@ -1305,4 +1302,16 @@ namespace libtorrent } +namespace std +{ + template <> + struct hash + { + std::size_t operator()(libtorrent::torrent_handle const& th) const + { + return libtorrent::hash_value(th); + } + }; +} + #endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED diff --git a/include/libtorrent/torrent_status.hpp b/include/libtorrent/torrent_status.hpp index b6a30927a..7c4368320 100644 --- a/include/libtorrent/torrent_status.hpp +++ b/include/libtorrent/torrent_status.hpp @@ -521,4 +521,16 @@ namespace libtorrent } +namespace std +{ + template <> + struct hash + { + std::size_t operator()(libtorrent::torrent_status const& ts) const + { + return libtorrent::hash_value(ts.handle); + } + }; +} + #endif // TORRENT_TORRENT_STATUS_HPP_INCLUDED diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 680e070e8..620481035 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -43,16 +43,13 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include - -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include +#include #ifdef TORRENT_USE_OPENSSL +#include "libtorrent/aux_/disable_warnings_push.hpp" #include -#endif - #include "libtorrent/aux_/disable_warnings_pop.hpp" +#endif #include "libtorrent/socket.hpp" #include "libtorrent/address.hpp" @@ -395,11 +392,11 @@ namespace libtorrent // maps transactionid to the udp_tracker_connection // These must use shared_ptr to avoid a dangling reference // if a connection is erased while a timeout event is in the queue - typedef boost::unordered_map> udp_conns_t; + using udp_conns_t = std::unordered_map> ; udp_conns_t m_udp_conns; - typedef std::vector> http_conns_t; + using http_conns_t = std::vector>; http_conns_t m_http_conns; send_fun_t m_send_fun; diff --git a/src/block_cache.cpp b/src/block_cache.cpp index d099557c1..ad47c624e 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -853,7 +853,7 @@ void block_cache::blocks_flushed(cached_piece_entry* pe, int const* flushed, int update_cache_state(pe); } -std::pair block_cache::all_pieces() const +std::pair block_cache::all_pieces() const { return std::make_pair(m_pieces.begin(), m_pieces.end()); } @@ -1662,7 +1662,7 @@ void block_cache::check_invariant() const } } - boost::unordered_set buffers; + std::unordered_set buffers; for (auto const& p :m_pieces) { TORRENT_PIECE_ASSERT(p.blocks, &p); diff --git a/src/lsd.cpp b/src/lsd.cpp index b88a83154..8aef939f4 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -47,7 +47,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include #include "libtorrent/aux_/disable_warnings_pop.hpp" diff --git a/src/session_call.cpp b/src/session_call.cpp index 7c068fd15..783e99724 100644 --- a/src/session_call.cpp +++ b/src/session_call.cpp @@ -36,7 +36,7 @@ namespace libtorrent { namespace aux { #ifdef TORRENT_PROFILE_CALLS static std::mutex g_calls_mutex; -static boost::unordered_map g_blocking_calls; +static std::unordered_map g_blocking_calls; #endif void blocking_call() @@ -57,10 +57,9 @@ void dump_call_profile() std::map profile; std::unique_lock l(g_calls_mutex); - for (boost::unordered_map::const_iterator i = g_blocking_calls.begin() - , end(g_blocking_calls.end()); i != end; ++i) + for (auto const& c : g_blocking_calls) { - profile[i->second] = i->first; + profile[c.second] = c.first; } for (std::map::const_reverse_iterator i = profile.rbegin() , end(profile.rend()); i != end; ++i) diff --git a/src/torrent.cpp b/src/torrent.cpp index 36fabb648..35993455e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3781,8 +3781,7 @@ namespace libtorrent TORRENT_ASSERT(i->finished <= m_picker->blocks_in_piece(index)); #if TORRENT_USE_ASSERTS - for (std::vector::const_iterator j = boost::next(i); - j != dl_queue.end(); ++j) + for (auto j = std::next(i); j != dl_queue.end(); ++j) { TORRENT_ASSERT(j->index != index); } @@ -4800,14 +4799,14 @@ namespace libtorrent i->flags = flags; // resort i since deadline might have changed - while (boost::next(i) != m_time_critical_pieces.end() && i->deadline > boost::next(i)->deadline) + while (std::next(i) != m_time_critical_pieces.end() && i->deadline > std::next(i)->deadline) { - std::iter_swap(i, boost::next(i)); + std::iter_swap(i, std::next(i)); ++i; } - while (i != m_time_critical_pieces.begin() && i->deadline < boost::prior(i)->deadline) + while (i != m_time_critical_pieces.begin() && i->deadline < std::prev(i)->deadline) { - std::iter_swap(i, boost::prior(i)); + std::iter_swap(i, std::prev(i)); --i; } // just in case this piece had priority 0 diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 32f58e42f..a0b6ed358 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -727,16 +727,10 @@ namespace libtorrent return m_torrent.lock(); } - std::size_t hash_value(torrent_status const& ts) - { - return hash_value(ts.handle); - } - std::size_t hash_value(torrent_handle const& th) { // using the locked shared_ptr value as hash doesn't work // for expired weak_ptrs. So, we're left with a hack return std::size_t(*reinterpret_cast(&th.m_torrent)); } - } diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 982ca6b20..ecf07a55f 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -45,8 +45,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/torrent_info.hpp" #include "libtorrent/read_resume_data.hpp" -#include - #include #include