From b313de39256dc30a08bdae34aa1e8de24bc8b34b Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Mon, 5 Sep 2016 22:25:20 -0400 Subject: [PATCH] some refactor and cleanup (#1065) --- .../libtorrent/aux_/allocating_handler.hpp | 4 -- include/libtorrent/bandwidth_manager.hpp | 7 +--- include/libtorrent/bandwidth_queue_entry.hpp | 3 +- include/libtorrent/bandwidth_socket.hpp | 4 +- include/libtorrent/block_cache.hpp | 4 +- include/libtorrent/kademlia/node_id.hpp | 2 - include/libtorrent/resolver.hpp | 1 - include/libtorrent/torrent_handle.hpp | 1 - src/bandwidth_manager.cpp | 42 +++++++++---------- src/block_cache.cpp | 11 ++--- src/disk_io_thread.cpp | 18 ++++---- src/kademlia/node_id.cpp | 15 ++++--- src/resolve_links.cpp | 5 +-- src/session_impl.cpp | 2 +- src/torrent_handle.cpp | 1 - src/web_peer_connection.cpp | 10 +---- 16 files changed, 48 insertions(+), 82 deletions(-) diff --git a/include/libtorrent/aux_/allocating_handler.hpp b/include/libtorrent/aux_/allocating_handler.hpp index 1503270ed..fce0e6a55 100644 --- a/include/libtorrent/aux_/allocating_handler.hpp +++ b/include/libtorrent/aux_/allocating_handler.hpp @@ -33,13 +33,10 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_ALLOCATING_HANDLER_HPP_INCLUDED #define TORRENT_ALLOCATING_HANDLER_HPP_INCLUDED -#include #include "libtorrent/config.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" - #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" namespace libtorrent { namespace aux @@ -125,4 +122,3 @@ namespace libtorrent { namespace aux } #endif - diff --git a/include/libtorrent/bandwidth_manager.hpp b/include/libtorrent/bandwidth_manager.hpp index 414db80bf..e9b70c72a 100644 --- a/include/libtorrent/bandwidth_manager.hpp +++ b/include/libtorrent/bandwidth_manager.hpp @@ -34,9 +34,8 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_BANDWIDTH_MANAGER_HPP_INCLUDED #include +#include -#include "libtorrent/socket.hpp" -#include "libtorrent/error_code.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/bandwidth_limit.hpp" @@ -76,8 +75,7 @@ struct TORRENT_EXTRA_EXPORT bandwidth_manager private: // these are the consumers that want bandwidth - typedef std::vector queue_t; - queue_t m_queue; + std::vector m_queue; // the number of bytes all the requests in queue are for std::int64_t m_queued_bytes; @@ -91,4 +89,3 @@ private: } #endif - diff --git a/include/libtorrent/bandwidth_queue_entry.hpp b/include/libtorrent/bandwidth_queue_entry.hpp index d7c5df994..430e73b3d 100644 --- a/include/libtorrent/bandwidth_queue_entry.hpp +++ b/include/libtorrent/bandwidth_queue_entry.hpp @@ -63,7 +63,7 @@ struct TORRENT_EXTRA_EXPORT bw_request // from the most limiting one int assign_bandwidth(); - enum { max_bandwidth_channels = 10 }; + constexpr static int max_bandwidth_channels = 10; // we don't actually support more than 10 channels per peer bandwidth_channel* channel[max_bandwidth_channels]; }; @@ -71,4 +71,3 @@ struct TORRENT_EXTRA_EXPORT bw_request } #endif - diff --git a/include/libtorrent/bandwidth_socket.hpp b/include/libtorrent/bandwidth_socket.hpp index 9bf7f3230..c83e30906 100644 --- a/include/libtorrent/bandwidth_socket.hpp +++ b/include/libtorrent/bandwidth_socket.hpp @@ -30,10 +30,11 @@ POSSIBILITY OF SUCH DAMAGE. */ - #ifndef TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED #define TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED +#include "libtorrent/export.hpp" + namespace libtorrent { struct TORRENT_EXTRA_EXPORT bandwidth_socket @@ -45,4 +46,3 @@ namespace libtorrent } #endif // TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED - diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index 7bbf6641a..992e33642 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -355,7 +355,7 @@ namespace libtorrent void reclaim_block(block_cache_reference const& ref); - // returns a range of all pieces. This migh be a very + // returns a range of all pieces. This might be a very // long list, use carefully std::pair all_pieces() const; int num_pieces() const { return int(m_pieces.size()); } @@ -536,7 +536,7 @@ namespace libtorrent int m_pinned_blocks; #if TORRENT_USE_ASSERTS - std::vector > m_deleted_storages; + std::vector> m_deleted_storages; #endif }; diff --git a/include/libtorrent/kademlia/node_id.hpp b/include/libtorrent/kademlia/node_id.hpp index b0903429a..8c11666f2 100644 --- a/include/libtorrent/kademlia/node_id.hpp +++ b/include/libtorrent/kademlia/node_id.hpp @@ -38,7 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/peer_id.hpp" -#include "libtorrent/assert.hpp" #include "libtorrent/address.hpp" namespace libtorrent { namespace dht @@ -77,4 +76,3 @@ node_id TORRENT_EXTRA_EXPORT generate_prefix_mask(int bits); } } // namespace libtorrent::dht #endif // NODE_ID_HPP - diff --git a/include/libtorrent/resolver.hpp b/include/libtorrent/resolver.hpp index 4ade1de03..1718f9e84 100644 --- a/include/libtorrent/resolver.hpp +++ b/include/libtorrent/resolver.hpp @@ -93,4 +93,3 @@ private: } #endif - diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 2d0f68865..8cdca4565 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -1306,4 +1306,3 @@ namespace libtorrent } #endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED - diff --git a/src/bandwidth_manager.cpp b/src/bandwidth_manager.cpp index 6622d8c73..b8c19234a 100644 --- a/src/bandwidth_manager.cpp +++ b/src/bandwidth_manager.cpp @@ -31,7 +31,10 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/bandwidth_manager.hpp" -#include "libtorrent/time.hpp" + +#if TORRENT_USE_ASSERTS +#include +#endif namespace libtorrent { @@ -47,7 +50,7 @@ namespace libtorrent { m_abort = true; - queue_t tm; + std::vector tm; tm.swap(m_queue); m_queued_bytes = 0; @@ -62,10 +65,9 @@ namespace libtorrent #if TORRENT_USE_ASSERTS bool bandwidth_manager::is_queued(bandwidth_socket const* peer) const { - for (queue_t::const_iterator i = m_queue.begin() - , end(m_queue.end()); i != end; ++i) + for (auto const& r : m_queue) { - if (i->peer.get() == peer) return true; + if (r.peer.get() == peer) return true; } return false; } @@ -125,10 +127,9 @@ namespace libtorrent void bandwidth_manager::check_invariant() const { std::int64_t queued = 0; - for (queue_t::const_iterator i = m_queue.begin() - , end(m_queue.end()); i != end; ++i) + for (auto const& r : m_queue) { - queued += i->request_size - i->assigned; + queued += r.request_size - r.assigned; } TORRENT_ASSERT(queued == m_queued_bytes); } @@ -148,10 +149,9 @@ namespace libtorrent std::vector channels; - queue_t tm; + std::vector tm; - for (queue_t::iterator i = m_queue.begin(); - i != m_queue.end();) + for (auto i = m_queue.begin(); i != m_queue.end();) { if (i->peer->is_disconnecting()) { @@ -178,26 +178,23 @@ namespace libtorrent ++i; } - for (queue_t::iterator i = m_queue.begin() - , end(m_queue.end()); i != end; ++i) + for (auto const& r : m_queue) { - for (int j = 0; j < bw_request::max_bandwidth_channels && i->channel[j]; ++j) + for (int j = 0; j < bw_request::max_bandwidth_channels && r.channel[j]; ++j) { - bandwidth_channel* bwc = i->channel[j]; + bandwidth_channel* bwc = r.channel[j]; if (bwc->tmp == 0) channels.push_back(bwc); - TORRENT_ASSERT(INT_MAX - bwc->tmp > i->priority); - bwc->tmp += i->priority; + TORRENT_ASSERT(INT_MAX - bwc->tmp > r.priority); + bwc->tmp += r.priority; } } - for (std::vector::iterator i = channels.begin() - , end(channels.end()); i != end; ++i) + for (auto const& ch : channels) { - (*i)->update_quota(int(dt_milliseconds)); + ch->update_quota(int(dt_milliseconds)); } - for (queue_t::iterator i = m_queue.begin(); - i != m_queue.end();) + for (auto i = m_queue.begin(); i != m_queue.end();) { int a = i->assign_bandwidth(); if (i->assigned == i->request_size @@ -223,4 +220,3 @@ namespace libtorrent } } } - diff --git a/src/block_cache.cpp b/src/block_cache.cpp index 3b7a40806..d099557c1 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -1222,10 +1222,9 @@ void block_cache::clear(tailqueue& jobs) // at the end std::vector bufs; - for (iterator p = m_pieces.begin() - , end(m_pieces.end()); p != end; ++p) + for (auto const& p : m_pieces) { - cached_piece_entry& pe = const_cast(*p); + cached_piece_entry& pe = const_cast(p); #if TORRENT_USE_ASSERTS for (tailqueue_iterator i = pe.jobs.iterate(); i.get(); i.next()) TORRENT_PIECE_ASSERT((static_cast(i.get()))->piece == pe.piece, &pe); @@ -1664,9 +1663,8 @@ void block_cache::check_invariant() const } boost::unordered_set buffers; - for (iterator i = m_pieces.begin(), end(m_pieces.end()); i != end; ++i) + for (auto const& p :m_pieces) { - cached_piece_entry const& p = *i; TORRENT_PIECE_ASSERT(p.blocks, &p); TORRENT_PIECE_ASSERT(p.storage, &p); @@ -1887,7 +1885,7 @@ cached_piece_entry* block_cache::find_piece(piece_manager* st, int piece) cached_piece_entry model; model.storage = st->shared_from_this(); model.piece = piece; - iterator i = m_pieces.find(model); + auto i = m_pieces.find(model); TORRENT_ASSERT(i == m_pieces.end() || (i->storage.get() == st && i->piece == piece)); if (i == m_pieces.end()) return nullptr; TORRENT_PIECE_ASSERT(i->in_use, &*i); @@ -1904,4 +1902,3 @@ cached_piece_entry* block_cache::find_piece(piece_manager* st, int piece) } } - diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 87afca4ca..369f95ffe 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -180,8 +180,7 @@ namespace libtorrent #if TORRENT_USE_ASSERTS // by now, all pieces should have been evicted - std::pair pieces - = m_disk_cache.all_pieces(); + auto pieces = m_disk_cache.all_pieces(); TORRENT_ASSERT(pieces.first == pieces.second); #endif @@ -837,7 +836,7 @@ namespace libtorrent } else { - std::pair range = m_disk_cache.all_pieces(); + auto range = m_disk_cache.all_pieces(); while (range.first != range.second) { // TODO: it would be nice to optimize this by having the cache @@ -1635,8 +1634,8 @@ namespace libtorrent #if TORRENT_USE_ASSERTS && defined TORRENT_EXPENSIVE_INVARIANT_CHECKS std::unique_lock l2_(m_cache_mutex); - std::pair range = m_disk_cache.all_pieces(); - for (block_cache::iterator i = range.first; i != range.second; ++i) + auto range = m_disk_cache.all_pieces(); + for (auto i = range.first; i != range.second; ++i) { cached_piece_entry const& p = *i; int bs = m_disk_cache.block_size(); @@ -2784,10 +2783,8 @@ namespace libtorrent { ret->pieces.reserve(m_disk_cache.num_pieces()); - std::pair range - = m_disk_cache.all_pieces(); - - for (block_cache::iterator i = range.first; i != range.second; ++i) + auto range = m_disk_cache.all_pieces(); + for (auto i = range.first; i != range.second; ++i) { if (i->cache_state == cached_piece_entry::read_lru2_ghost || i->cache_state == cached_piece_entry::read_lru1_ghost) @@ -3308,8 +3305,7 @@ namespace libtorrent #if TORRENT_USE_ASSERTS // by now, all pieces should have been evicted - std::pair pieces - = m_disk_cache.all_pieces(); + auto pieces = m_disk_cache.all_pieces(); TORRENT_ASSERT(pieces.first == pieces.second); #endif diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index dd75b5d85..7c057d226 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/kademlia/node_entry.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/broadcast_socket.hpp" // for is_local et.al -#include "libtorrent/socket_io.hpp" // for hash_address #include "libtorrent/random.hpp" // for random #include "libtorrent/hasher.hpp" // for hasher #include "libtorrent/crc32c.hpp" // for crc32c @@ -153,13 +152,14 @@ void make_id_secret(node_id& in) hasher h(reinterpret_cast(&secret), 4); h.update(reinterpret_cast(&rand), 4); sha1_hash const secret_hash = h.final(); - memcpy(&in[20-4], &secret_hash[0], 4); - memcpy(&in[20-8], &rand, 4); + std::memcpy(&in[20 - 4], &secret_hash[0], 4); + std::memcpy(&in[20 - 8], &rand, 4); } node_id generate_random_id() { char r[20]; + // TODO: use here aux::random_bytes? for (int i = 0; i < 20; ++i) r[i] = random(0xff); return hasher(r, 20).final(); } @@ -176,9 +176,9 @@ bool verify_secret_id(node_id const& nid) if (secret == 0) return false; hasher h(reinterpret_cast(&secret), 4); - h.update(reinterpret_cast(&nid[20-8]), 4); + h.update(reinterpret_cast(&nid[20 - 8]), 4); sha1_hash secret_hash = h.final(); - return memcmp(&nid[20-4], &secret_hash[0], 4) == 0; + return std::memcmp(&nid[20 - 4], &secret_hash[0], 4) == 0; } // verifies whether a node-id matches the IP it's used from @@ -211,10 +211,9 @@ node_id generate_prefix_mask(int bits) TORRENT_ASSERT(bits <= 160); node_id mask(nullptr); int b = 0; - for (; b < bits - 7; b += 8) mask[b/8] |= 0xff; - if (bits < 160) mask[b/8] |= (0xff << (8 - (bits&7))) & 0xff; + for (; b < bits - 7; b += 8) mask[b / 8] |= 0xff; + if (bits < 160) mask[b / 8] |= (0xff << (8 - (bits & 7))) & 0xff; return mask; } } } // namespace libtorrent::dht - diff --git a/src/resolve_links.cpp b/src/resolve_links.cpp index 3d975a920..63f2a50c4 100644 --- a/src/resolve_links.cpp +++ b/src/resolve_links.cpp @@ -49,7 +49,7 @@ resolve_links::resolve_links(std::shared_ptr ti) for (int i = 0; i < fs.num_files(); ++i) { // don't match pad-files, and don't match files that aren't aligned to - // ieces. Files are matched by comparing piece hashes, so pieces must + // pieces. Files are matched by comparing piece hashes, so pieces must // be aligned and the same size if (fs.pad_file_at(i)) continue; if ((fs.file_offset(i) % piece_size) != 0) continue; @@ -84,8 +84,7 @@ void resolve_links::match(std::shared_ptr const& ti std::int64_t file_size = fs.file_size(i); - typedef boost::unordered_multimap::iterator iterator; - iterator iter = m_file_sizes.find(file_size); + auto iter = m_file_sizes.find(file_size); // we don't have a file whose size matches, look at the next one if (iter == m_file_sizes.end()) continue; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index ae5db5772..32f7ebb4c 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4590,7 +4590,7 @@ namespace aux { add_torrent_params params = p; std::shared_ptr torrent_ptr; bool added; - boost::tie(torrent_ptr, added) = add_torrent_impl(params, ec); + std::tie(torrent_ptr, added) = add_torrent_impl(params, ec); torrent_handle const handle(torrent_ptr); m_alerts.emplace_alert(handle, params, ec); diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index a6f4cce43..32f58e42f 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -740,4 +740,3 @@ namespace libtorrent } } - diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 16a3f2266..63765363d 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -32,12 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" -#include "libtorrent/aux_/disable_warnings_push.hpp" - #include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" - #include #include #include // for snprintf @@ -61,10 +56,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { -enum -{ - request_size_overhead = 5000 -}; +constexpr int request_size_overhead = 5000; struct disk_interface;