fixing sign-conversion warnings, part 12, minor refactor, eol cleanup (#1670)

fixing sign-conversion warnings, part 12, minor refactor, eol cleanup. created aux::clamp and default_piece_priority refactor
This commit is contained in:
Alden Torres 2017-02-07 00:22:30 -05:00 committed by Arvid Norberg
parent b84766e346
commit c8569b5e34
20 changed files with 87 additions and 80 deletions

View File

@ -53,6 +53,15 @@ namespace libtorrent { namespace aux {
return r;
}
// in C++ 17 you can use std::clamp
template <class T, typename Cond = typename std::enable_if<
std::is_integral<T>::value>::type>
T clamp(T v, T lo, T hi)
{
TORRENT_ASSERT(lo <= hi);
return (v < lo) ? lo : (hi < v) ? hi : v;
}
}}
#endif

View File

@ -61,4 +61,3 @@ namespace libtorrent { namespace aux {
}}
#endif

View File

@ -51,4 +51,3 @@ namespace libtorrent { namespace aux
}}
#endif

View File

@ -98,4 +98,3 @@ private:
}
#endif

View File

@ -286,7 +286,7 @@ struct TORRENT_EXPORT ip_filter
// can currently be 0 or ``ip_filter::blocked``. The complexity of this operation
// is O(``log`` n), where n is the minimum number of non-overlapping ranges to describe
// the current filter.
int access(address const& addr) const;
std::uint32_t access(address const& addr) const;
#if TORRENT_USE_IPV6
using filter_tuple_t = std::tuple<std::vector<ip_range<address_v4>>
@ -338,7 +338,7 @@ public:
// test the specified port (``port``) for whether it is blocked
// or not. The returned value is the flags set for this port.
// see access_flags.
int access(std::uint16_t port) const;
std::uint32_t access(std::uint16_t port) const;
private:

View File

@ -95,6 +95,8 @@ namespace libtorrent
class bt_peer_connection;
struct listen_socket_t;
constexpr int default_piece_priority = 4;
enum class waste_reason
{
piece_timed_out, piece_cancelled, piece_unknown, piece_seed

View File

@ -127,4 +127,3 @@ namespace libtorrent
}
#endif

View File

@ -83,4 +83,3 @@ namespace libtorrent
#endif // !BOOST_NO_STD_WSTRING
#endif

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include "libtorrent/file.hpp"
#include "libtorrent/utf8.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/crc.hpp>
@ -157,7 +158,7 @@ namespace libtorrent
, [&] (std::string const& str)
{
if (int(str.size()) != branch_len) return false;
return std::memcmp(str.c_str(), branch_path, branch_len) == 0;
return std::memcmp(str.c_str(), branch_path, aux::numeric_cast<std::size_t>(branch_len)) == 0;
});
if (p == m_paths.rend())
@ -168,7 +169,7 @@ namespace libtorrent
// poor man's emplace back
m_paths.resize(m_paths.size() + 1);
m_paths.back().assign(branch_path, branch_len);
m_paths.back().assign(branch_path, aux::numeric_cast<std::size_t>(branch_len));
}
else
{
@ -303,7 +304,7 @@ namespace libtorrent
else if (borrow_string)
{
name = n;
name_len = string_len;
name_len = aux::numeric_cast<std::uint64_t>(string_len);
}
else
{
@ -384,7 +385,7 @@ namespace libtorrent
{
// find the file iterator and file offset
internal_file_entry target;
target.offset = offset;
target.offset = aux::numeric_cast<std::uint64_t>(offset);
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
auto file_iter = std::upper_bound(
@ -405,7 +406,7 @@ namespace libtorrent
{
// find the file iterator and file offset
internal_file_entry target;
target.offset = offset;
target.offset = aux::numeric_cast<std::uint64_t>(offset);
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
auto file_iter = std::upper_bound(
@ -438,13 +439,13 @@ namespace libtorrent
// find the file iterator and file offset
internal_file_entry target;
target.offset = static_cast<int>(piece) * std::int64_t(m_piece_length) + offset;
TORRENT_ASSERT_PRECOND(std::int64_t(target.offset + size) <= m_total_size);
target.offset = aux::numeric_cast<std::uint64_t>(static_cast<int>(piece) * std::int64_t(m_piece_length) + offset);
TORRENT_ASSERT_PRECOND(std::int64_t(target.offset) + size <= m_total_size);
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
// in case the size is past the end, fix it up
if (std::int64_t(target.offset + size) > m_total_size)
size = int(m_total_size - target.offset);
if (std::int64_t(target.offset) + size > m_total_size)
size = aux::numeric_cast<int>(m_total_size - std::int64_t(target.offset));
auto file_iter = std::upper_bound(
m_files.begin(), m_files.end(), target, compare_file_offset);
@ -465,7 +466,7 @@ namespace libtorrent
+ file_base_deprecated(f.file_index)
#endif
;
f.size = std::min(std::uint64_t(file_iter->size) - file_offset, std::uint64_t(size));
f.size = std::min(std::int64_t(file_iter->size) - file_offset, std::int64_t(size));
TORRENT_ASSERT(f.size <= size);
size -= int(f.size);
file_offset += f.size;
@ -581,8 +582,8 @@ namespace libtorrent
if (filename)
e.set_name(filename, true, filename_len);
e.size = file_size;
e.offset = m_total_size;
e.size = aux::numeric_cast<std::uint64_t>(file_size);
e.offset = aux::numeric_cast<std::uint64_t>(m_total_size);
e.pad_file = (file_flags & file_storage::flag_pad_file) != 0;
e.hidden_attribute = (file_flags & file_storage::flag_hidden) != 0;
e.executable_attribute = (file_flags & file_storage::flag_executable) != 0;
@ -638,7 +639,7 @@ namespace libtorrent
void process_string_lowercase(CRC& crc, string_view str)
{
for (char const c : str)
crc.process_byte(to_lower(c));
crc.process_byte(to_lower(c) & 0xff);
}
template <class CRC>
@ -652,7 +653,7 @@ namespace libtorrent
{
if (*str == TORRENT_SEPARATOR)
table.insert(crc.checksum());
crc.process_byte(to_lower(*str));
crc.process_byte(to_lower(*str) & 0xff);
}
table.insert(crc.checksum());
}
@ -975,7 +976,7 @@ namespace libtorrent
{
// a file whose size fits the alignment always takes priority,
// since it will let us keep placing aligned files
if ((k->size % alignment) == 0)
if ((k->size % aux::numeric_cast<std::uint64_t>(alignment)) == 0)
{
best_match = k;
break;
@ -1029,7 +1030,7 @@ namespace libtorrent
int cur_index = int(i - m_files.begin());
reorder_file(index, cur_index);
i = m_files.begin() + cur_index;
i->offset = off;
i->offset = aux::numeric_cast<std::uint64_t>(off);
off += i->size;
continue;
}
@ -1049,7 +1050,7 @@ namespace libtorrent
TORRENT_ASSERT((off % alignment) == 0);
continue;
}
i->offset = off;
i->offset = aux::numeric_cast<std::uint64_t>(off);
off += i->size;
if (tail_padding
@ -1084,8 +1085,8 @@ namespace libtorrent
internal_file_entry& e = m_files.back();
// i may have been invalidated, refresh it
i = m_files.begin() + cur_index;
e.size = size;
e.offset = offset;
e.size = aux::numeric_cast<std::uint64_t>(size);
e.offset = aux::numeric_cast<std::uint64_t>(offset);
char name[30];
std::snprintf(name, sizeof(name), ".pad" TORRENT_SEPARATOR_STR "%d"
, pad_file_counter);

View File

@ -52,7 +52,7 @@ namespace libtorrent
TORRENT_ASSERT_FAIL();
}
int ip_filter::access(address const& addr) const
std::uint32_t ip_filter::access(address const& addr) const
{
if (addr.is_v4())
return m_filter4.access(addr.to_v4().to_bytes());
@ -79,7 +79,7 @@ namespace libtorrent
m_filter.add_rule(first, last, flags);
}
int port_filter::access(std::uint16_t port) const
std::uint32_t port_filter::access(std::uint16_t port) const
{
return m_filter.access(port);
}
@ -93,4 +93,3 @@ namespace libtorrent
}
*/
}

View File

@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/session_settings.hpp>
#include <libtorrent/random.hpp>
#include <libtorrent/aux_/vector.hpp>
#include <libtorrent/aux_/numeric_cast.hpp>
namespace libtorrent {
namespace dht {
@ -183,12 +184,6 @@ namespace
int count() const { return int(samples.size()); }
};
int clamp(int v, int lo, int hi)
{
TORRENT_ASSERT(lo <= hi);
return (v < lo) ? lo : (hi < v) ? hi : v;
}
class dht_default_storage final : public dht_storage_interface, boost::noncopyable
{
public:
@ -473,7 +468,7 @@ namespace
int get_infohashes_sample(entry& item) override
{
item["interval"] = clamp(m_settings.sample_infohashes_interval
item["interval"] = aux::clamp(m_settings.sample_infohashes_interval
, 0, sample_infohashes_interval_max);
item["num"] = int(m_map.size());
@ -571,10 +566,10 @@ namespace
void refresh_infohashes_sample()
{
time_point const now = aux::time_now();
int const interval = clamp(m_settings.sample_infohashes_interval
int const interval = aux::clamp(m_settings.sample_infohashes_interval
, 0, sample_infohashes_interval_max);
int const max_count = clamp(m_settings.max_infohashes_sample_count
int const max_count = aux::clamp(m_settings.max_infohashes_sample_count
, 0, infohashes_sample_count_max);
int const count = std::min(max_count, int(m_map.size()));

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/kademlia/item.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/kademlia/ed25519.hpp>
#include <libtorrent/aux_/numeric_cast.hpp>
#include <cstdio> // for snprintf
#include <cinttypes> // for PRId64 et.al.
@ -61,17 +62,17 @@ namespace
#endif
char* ptr = out.data();
size_t left = out.size() - (ptr - out.data());
std::size_t left = out.size() - aux::numeric_cast<std::size_t>(ptr - out.data());
if (salt.size() > 0)
{
ptr += std::snprintf(ptr, left, "4:salt%d:", int(salt.size()));
left = out.size() - (ptr - out.data());
left = out.size() - aux::numeric_cast<std::size_t>(ptr - out.data());
std::memcpy(ptr, salt.data(), std::min(salt.size(), left));
ptr += std::min(salt.size(), left);
left = out.size() - (ptr - out.data());
left = out.size() - aux::numeric_cast<std::size_t>(ptr - out.data());
}
ptr += std::snprintf(ptr, left, "3:seqi%" PRId64 "e1:v", seq.value);
left = out.size() - (ptr - out.data());
left = out.size() - aux::numeric_cast<std::size_t>(ptr - out.data());
std::memcpy(ptr, v.data(), std::min(v.size(), left));
ptr += std::min(v.size(), left);
TORRENT_ASSERT((ptr - out.data()) <= int(out.size()));
@ -165,7 +166,7 @@ void item::assign(entry v, span<char const> salt
char buffer[1000];
int bsize = bencode(buffer, v);
TORRENT_ASSERT(bsize <= 1000);
m_sig = sign_mutable_item(span<char const>(buffer, bsize)
m_sig = sign_mutable_item({buffer, aux::numeric_cast<std::size_t>(bsize)}
, salt, seq, pk, sk);
m_salt.assign(salt.data(), salt.size());
m_pk = pk;

View File

@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/time.hpp"
#include "libtorrent/debug.hpp"
#include "libtorrent/aux_/escape_string.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
namespace libtorrent {
@ -207,7 +208,7 @@ void natpmp::disable(error_code const& ec)
close_impl();
}
void natpmp::delete_mapping(int index)
void natpmp::delete_mapping(int const index)
{
TORRENT_ASSERT(is_single_thread());
@ -471,7 +472,7 @@ void natpmp::on_reply(error_code const& e
int version = read_uint8(in);
int cmd = read_uint8(in);
int result = read_uint16(in);
int time = read_uint32(in);
int time = aux::numeric_cast<int>(read_uint32(in));
TORRENT_UNUSED(version);
TORRENT_UNUSED(time);
@ -500,7 +501,7 @@ void natpmp::on_reply(error_code const& e
int const private_port = read_uint16(in);
int const public_port = read_uint16(in);
int const lifetime = read_uint32(in);
int const lifetime = aux::numeric_cast<int>(read_uint32(in));
portmap_protocol const protocol = (cmd - 128 == 1)
? portmap_protocol::udp
@ -515,7 +516,7 @@ void natpmp::on_reply(error_code const& e
if (version != 0)
{
std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "unexpected version: %u"
std::snprintf(msg + num_chars, sizeof(msg) - aux::numeric_cast<std::size_t>(num_chars), "unexpected version: %u"
, version);
log("%s", msg);
}
@ -523,8 +524,7 @@ void natpmp::on_reply(error_code const& e
mapping_t* m = nullptr;
int index = -1;
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
, end(m_mappings.end()); i != end; ++i)
for (auto i = m_mappings.begin(), end(m_mappings.end()); i != end; ++i)
{
if (private_port != i->local_port) continue;
if (protocol != i->protocol) continue;
@ -538,7 +538,7 @@ void natpmp::on_reply(error_code const& e
if (m == nullptr)
{
#ifndef TORRENT_DISABLE_LOGGING
snprintf(msg + num_chars, sizeof(msg) - num_chars, " not found in map table");
snprintf(msg + num_chars, sizeof(msg) - aux::numeric_cast<std::size_t>(num_chars), " not found in map table");
log("%s", msg);
#endif
return;
@ -645,7 +645,7 @@ void natpmp::update_expiration_timer()
}
}
void natpmp::mapping_expired(error_code const& e, int i)
void natpmp::mapping_expired(error_code const& e, int const i)
{
TORRENT_ASSERT(is_single_thread());
COMPLETE_ASYNC("natpmp::mapping_expired");

View File

@ -340,12 +340,12 @@ namespace libtorrent
if (bool(m_finished) != state->is_finished)
recalculate_connect_candidates(state);
int round_robin = random(std::uint32_t(m_peers.size()-1));
int round_robin = aux::numeric_cast<int>(random(std::uint32_t(m_peers.size() - 1)));
int low_watermark = max_peerlist_size * 95 / 100;
if (low_watermark == max_peerlist_size) --low_watermark;
for (int iterations = (std::min)(int(m_peers.size()), 300);
for (int iterations = std::min(int(m_peers.size()), 300);
iterations > 0; --iterations)
{
if (int(m_peers.size()) < low_watermark)
@ -430,20 +430,20 @@ namespace libtorrent
// failcount is a 5 bit value
if (p->failcount == 31) return;
const bool was_conn_cand = is_connect_candidate(*p);
bool const was_conn_cand = is_connect_candidate(*p);
++p->failcount;
if (was_conn_cand && !is_connect_candidate(*p))
update_connect_candidates(-1);
}
void peer_list::set_failcount(torrent_peer* p, int f)
void peer_list::set_failcount(torrent_peer* p, int const f)
{
TORRENT_ASSERT(is_single_thread());
INVARIANT_CHECK;
TORRENT_ASSERT(p->in_use);
const bool was_conn_cand = is_connect_candidate(*p);
p->failcount = f;
bool const was_conn_cand = is_connect_candidate(*p);
p->failcount = aux::numeric_cast<std::uint32_t>(f);
if (was_conn_cand != is_connect_candidate(*p))
{
update_connect_candidates(was_conn_cand ? -1 : 1);
@ -489,7 +489,7 @@ namespace libtorrent
// TODO: 2 it would be nice if there was a way to iterate over these
// torrent_peer objects in the order they are allocated in the pool
// instead. It would probably be more efficient
for (int iterations = (std::min)(int(m_peers.size()), 300);
for (int iterations = std::min(int(m_peers.size()), 300);
iterations > 0; --iterations)
{
++state->loop_counter;
@ -543,7 +543,7 @@ namespace libtorrent
peers.resize(candidate_count - 1);
// insert this candidate sorted into peers
std::vector<torrent_peer*>::iterator i = std::lower_bound(peers.begin(), peers.end()
auto const i = std::lower_bound(peers.begin(), peers.end()
, &pe, std::bind(&peer_list::compare_peer, this, _1, _2, std::cref(external), external_port));
peers.insert(i, &pe);

View File

@ -38,6 +38,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket_io.hpp" // for read_*_endpoint()
#include "libtorrent/hasher.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
#include "libtorrent/torrent.hpp" // for default_piece_priority
namespace libtorrent
{
@ -165,13 +167,15 @@ namespace libtorrent
if (file_priority)
{
int const num_files = file_priority.list_size();
ret.file_priorities.resize(num_files, 4);
ret.file_priorities.resize(aux::numeric_cast<std::size_t>(num_files)
, default_piece_priority);
for (int i = 0; i < num_files; ++i)
{
ret.file_priorities[i] = std::uint8_t(
file_priority.list_int_value_at(i, 1));
std::size_t const idx = std::size_t(i);
ret.file_priorities[idx] = aux::clamp(
file_priority.list_int_value_at(i, default_piece_priority), std::int64_t(0), std::int64_t(7)) & 0xff;
// this is suspicious, leave seed mode
if (ret.file_priorities[i] == 0)
if (ret.file_priorities[idx] == 0)
{
ret.flags &= ~add_torrent_params::flag_seed_mode;
}
@ -239,9 +243,9 @@ namespace libtorrent
bdecode_node mt = rd.dict_find_string("merkle tree");
if (mt)
{
ret.merkle_tree.resize(mt.string_length() / 20);
std::memcpy(&ret.merkle_tree[0], mt.string_ptr()
, int(ret.merkle_tree.size()) * 20);
ret.merkle_tree.resize(aux::numeric_cast<std::size_t>(mt.string_length() / 20));
std::memcpy(ret.merkle_tree.data(), mt.string_ptr()
, ret.merkle_tree.size() * 20);
}
// some sanity checking. Maybe we shouldn't be in seed mode anymore
@ -266,10 +270,10 @@ namespace libtorrent
if (bdecode_node piece_priority = rd.dict_find_string("piece_priority"))
{
char const* prio_str = piece_priority.string_ptr();
ret.piece_priorities.resize(piece_priority.string_length());
for (int i = 0; i < int(ret.piece_priorities.size()); ++i)
ret.piece_priorities.resize(aux::numeric_cast<std::size_t>(piece_priority.string_length()));
for (std::size_t i = 0; i < ret.piece_priorities.size(); ++i)
{
ret.piece_priorities[i] = prio_str[i];
ret.piece_priorities[i] = aux::clamp(int(prio_str[i]), 0, 7) & 0xff;
}
}

View File

@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/resolve_links.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
namespace libtorrent
{
@ -45,7 +46,7 @@ resolve_links::resolve_links(std::shared_ptr<torrent_info> ti)
int piece_size = ti->piece_length();
file_storage const& fs = ti->files();
m_file_sizes.reserve(fs.num_files());
m_file_sizes.reserve(aux::numeric_cast<std::size_t>(fs.num_files()));
for (file_index_t i(0); i < fs.end_file(); ++i)
{
// don't match pad-files, and don't match files that aren't aligned to
@ -71,7 +72,7 @@ void resolve_links::match(std::shared_ptr<const torrent_info> const& ti
int piece_size = ti->piece_length();
file_storage const& fs = ti->files();
m_file_sizes.reserve(fs.num_files());
m_file_sizes.reserve(aux::numeric_cast<std::size_t>(fs.num_files()));
for (file_index_t i(0); i < fs.end_file(); ++i)
{
// for every file in the other torrent, see if we have one that match

View File

@ -56,7 +56,7 @@ namespace libtorrent
if (i >= m_stat_cache.end_index())
m_stat_cache.resize(static_cast<int>(i) + 1, not_in_cache);
int error_index = add_error(ec);
int const error_index = add_error(ec);
m_stat_cache[i].file_size = file_error - error_index;
}

View File

@ -54,6 +54,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/torrent_info.hpp"
@ -124,7 +125,6 @@ namespace libtorrent
}
return ret;
}
constexpr int default_piece_priority = 4;
} // anonymous namespace

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/error_code.hpp"
#include "libtorrent/ConvertUTF.h"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/aux_/numeric_cast.hpp"
#ifdef __clang__
@ -92,7 +93,7 @@ namespace libtorrent
, std::back_inserter(wide));
return static_cast<utf8_errors::error_code_enum>(ret);
}
wide.resize(dst_start - wide.c_str());
wide.resize(aux::numeric_cast<std::size_t>(dst_start - wide.c_str()));
return static_cast<utf8_errors::error_code_enum>(ret);
}
};
@ -121,7 +122,7 @@ namespace libtorrent
, std::back_inserter(wide));
return static_cast<utf8_errors::error_code_enum>(ret);
}
wide.resize(dst_start - wide.c_str());
wide.resize(aux::numeric_cast<std::size_t>(dst_start - wide.c_str()));
return static_cast<utf8_errors::error_code_enum>(ret);
}
};
@ -156,7 +157,7 @@ namespace libtorrent
, reinterpret_cast<UTF8**>(&dst_start)
, reinterpret_cast<UTF8*>(dst_start + utf8.size())
, lenientConversion);
utf8.resize(dst_start - &utf8[0]);
utf8.resize(aux::numeric_cast<std::size_t>(dst_start - &utf8[0]));
return static_cast<utf8_errors::error_code_enum>(ret);
}
};
@ -176,7 +177,7 @@ namespace libtorrent
, reinterpret_cast<UTF8**>(&dst_start)
, reinterpret_cast<UTF8*>(dst_start + utf8.size())
, lenientConversion);
utf8.resize(dst_start - &utf8[0]);
utf8.resize(aux::numeric_cast<std::size_t>(dst_start - &utf8[0]));
return static_cast<utf8_errors::error_code_enum>(ret);
}
};
@ -274,4 +275,3 @@ namespace libtorrent
#endif
#endif

View File

@ -82,8 +82,8 @@ void test_rules_invariant(std::vector<ip_range<T>> const& r, ip_filter const& f)
for (iterator i(r.begin()), j(std::next(r.begin()))
, end(r.end()); j != end; ++j, ++i)
{
TEST_EQUAL(f.access(i->last), int(i->flags));
TEST_EQUAL(f.access(j->first), int(j->flags));
TEST_EQUAL(f.access(i->last), i->flags);
TEST_EQUAL(f.access(j->first), j->flags);
TEST_CHECK(detail::plus_one(i->last.to_bytes()) == j->first.to_bytes());
}
}