From d8755066e8b60fcf5fce4502a366dba2d017995e Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 19 Jul 2018 11:27:42 +0200 Subject: [PATCH] give some magic numbers names --- include/libtorrent/sha1_hash.hpp | 17 +++++++++-------- src/create_torrent.cpp | 7 +++++-- src/read_resume_data.cpp | 22 +++++++++++++--------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/libtorrent/sha1_hash.hpp b/include/libtorrent/sha1_hash.hpp index 2f1dacc23..6d55ca1ac 100644 --- a/include/libtorrent/sha1_hash.hpp +++ b/include/libtorrent/sha1_hash.hpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include // for CHAR_BIT #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" @@ -72,7 +73,7 @@ namespace aux { public: // the size of the hash in bytes - static constexpr std::size_t size() noexcept { return N / 8; } + static constexpr std::size_t size() noexcept { return N / CHAR_BIT; } // constructs an all-zero digest digest32() noexcept { clear(); } @@ -81,7 +82,7 @@ namespace aux { digest32& operator=(digest32 const&) noexcept = default; // returns an all-F digest. i.e. the maximum value - // representable by an N bit number (N/8 bytes). This is + // representable by an N bit number (N/CHAR_BIT bytes). This is // a static member function. static digest32 (max)() noexcept { @@ -91,7 +92,7 @@ namespace aux { } // returns an all-zero digest. i.e. the minimum value - // representable by an N bit number (N/8 bytes). This is + // representable by an N bit number (N/CHAR_BIT bytes). This is // a static member function. static digest32 (min)() noexcept { @@ -100,8 +101,8 @@ namespace aux { return ret; } - // copies N/8 bytes from the pointer provided, into the digest. - // The passed in string MUST be at least N/8 bytes. 0-terminators + // copies N/CHAR_BIT bytes from the pointer provided, into the digest. + // The passed in string MUST be at least N/CHAR_BIT bytes. 0-terminators // are ignored, ``s`` is treated like a raw memory buffer. explicit digest32(char const* s) noexcept { @@ -121,7 +122,7 @@ namespace aux { } void assign(span s) noexcept { - TORRENT_ASSERT(s.size() >= N / 8); + TORRENT_ASSERT(s.size() >= N / CHAR_BIT); std::size_t const sl = s.size() < size() ? s.size() : size(); std::memcpy(m_number.data(), s.data(), sl); } @@ -256,8 +257,8 @@ namespace aux { iterator end() { return reinterpret_cast(m_number.data()) + size(); } - // return a copy of the N/8 bytes representing the digest as a std::string. - // It's still a binary string with N/8 binary characters. + // return a copy of the N/CHAR_BIT bytes representing the digest as a std::string. + // It's still a binary string with N/CHAR_BIT binary characters. std::string to_string() const { return std::string(reinterpret_cast(m_number.data()), size()); diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index f3b8f08c7..49653619c 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -296,11 +296,14 @@ namespace { settings_pack sett; sett.set_int(settings_pack::cache_size, 0); - sett.set_int(settings_pack::aio_threads, 3); + int const num_threads = disk_io_thread::hasher_thread_divisor - 1; + int const jobs_per_thread = 4; + sett.set_int(settings_pack::aio_threads, num_threads); disk_thread.set_settings(&sett); - int const piece_read_ahead = std::max(12, 16 * 1024 * 1024 / t.piece_length()); + int const piece_read_ahead = std::max(num_threads * jobs_per_thread + , default_block_size / t.piece_length()); hash_state st = { t, std::move(storage), disk_thread, piece_index_t(0), piece_index_t(0), f, ec }; for (piece_index_t i(0); i < piece_index_t(piece_read_ahead); ++i) diff --git a/src/read_resume_data.cpp b/src/read_resume_data.cpp index 813189bf6..d2ae424a4 100644 --- a/src/read_resume_data.cpp +++ b/src/read_resume_data.cpp @@ -86,7 +86,7 @@ namespace { } auto info_hash = rd.dict_find_string_value("info-hash"); - if (info_hash.size() != 20) + if (info_hash.size() != sha1_hash::size()) { ec = errors::missing_info_hash; return ret; @@ -253,11 +253,11 @@ namespace { } bdecode_node const mt = rd.dict_find_string("merkle tree"); - if (mt && mt.string_length() >= 20) + if (mt && mt.string_length() >= int(sha1_hash::size())) { - ret.merkle_tree.resize(aux::numeric_cast(mt.string_length() / 20)); + ret.merkle_tree.resize(aux::numeric_cast(mt.string_length()) / sha1_hash::size()); std::memcpy(ret.merkle_tree.data(), mt.string_ptr() - , ret.merkle_tree.size() * 20); + , ret.merkle_tree.size() * sha1_hash::size()); } // some sanity checking. Maybe we shouldn't be in seed mode anymore @@ -292,11 +292,15 @@ namespace { } } +#if TORRENT_USE_IPV6 + int const v6_size = 18; +#endif + int const v4_size = 6; using namespace libtorrent::detail; // for read_*_endpoint() if (bdecode_node const peers_entry = rd.dict_find_string("peers")) { char const* ptr = peers_entry.string_ptr(); - for (int i = 5; i < peers_entry.string_length(); i += 6) + for (int i = v4_size - 1; i < peers_entry.string_length(); i += v4_size) ret.peers.push_back(read_v4_endpoint(ptr)); } @@ -304,7 +308,7 @@ namespace { if (bdecode_node const peers_entry = rd.dict_find_string("peers6")) { char const* ptr = peers_entry.string_ptr(); - for (int i = 17; i < peers_entry.string_length(); i += 18) + for (int i = v6_size - 1; i < peers_entry.string_length(); i += v6_size) ret.peers.push_back(read_v6_endpoint(ptr)); } #endif @@ -312,7 +316,7 @@ namespace { if (bdecode_node const peers_entry = rd.dict_find_string("banned_peers")) { char const* ptr = peers_entry.string_ptr(); - for (int i = 5; i < peers_entry.string_length(); i += 6) + for (int i = v4_size; i < peers_entry.string_length(); i += v4_size) ret.banned_peers.push_back(read_v4_endpoint(ptr)); } @@ -320,7 +324,7 @@ namespace { if (bdecode_node const peers_entry = rd.dict_find_string("banned_peers6")) { char const* ptr = peers_entry.string_ptr(); - for (int i = 17; i < peers_entry.string_length(); i += 18) + for (int i = v6_size - 1; i < peers_entry.string_length(); i += v6_size) ret.banned_peers.push_back(read_v6_endpoint(ptr)); } #endif @@ -338,7 +342,7 @@ namespace { bdecode_node const bitmask = e.dict_find_string("bitmask"); if (!bitmask || bitmask.string_length() == 0) continue; ret.unfinished_pieces[piece].assign( - bitmask.string_ptr(), bitmask.string_length() * 8); + bitmask.string_ptr(), bitmask.string_length() * CHAR_BIT); } }