give some magic numbers names

This commit is contained in:
arvidn 2018-07-19 11:27:42 +02:00 committed by Arvid Norberg
parent a7b7623f23
commit d8755066e8
3 changed files with 27 additions and 19 deletions

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <string>
#include <cstring>
#include <array>
#include <climits> // 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<char const> 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<std::uint8_t*>(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<char const*>(m_number.data()), size());

View File

@ -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)

View File

@ -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<std::size_t>(mt.string_length() / 20));
ret.merkle_tree.resize(aux::numeric_cast<std::size_t>(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<tcp::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<tcp::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<tcp::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<tcp::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);
}
}