fixing sign-conversion warnings, part 4, minor refactor (#1549)

fixing sign-conversion warnings, part 4, minor refactor
This commit is contained in:
Alden Torres 2017-01-14 18:53:25 -05:00 committed by Arvid Norberg
parent 2e7f43ba21
commit df6cb6a48d
12 changed files with 65 additions and 46 deletions

View File

@ -39,22 +39,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include <malloc.h>
#define TORRENT_ALLOCA(v, t, n) ::libtorrent::span<t> v; { \
t* TORRENT_ALLOCA_tmp = static_cast<t*>(_alloca(sizeof(t) * (n))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, n); }
t* TORRENT_ALLOCA_tmp = static_cast<t*>(_alloca(sizeof(t) * (std::size_t(n)))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, std::size_t(n)); }
#elif defined TORRENT_BSD
#include <stdlib.h>
#define TORRENT_ALLOCA(v, t, n) ::libtorrent::span<t> v; { \
t* TORRENT_ALLOCA_tmp = static_cast<t*>(alloca(sizeof(t) * (n))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, n); }
t* TORRENT_ALLOCA_tmp = static_cast<t*>(alloca(sizeof(t) * (std::size_t(n)))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, std::size_t(n)); }
#else
#include <alloca.h>
#define TORRENT_ALLOCA(v, t, n) ::libtorrent::span<t> v; { \
t* TORRENT_ALLOCA_tmp = static_cast<t*>(alloca(sizeof(t) * (n))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, n); }
t* TORRENT_ALLOCA_tmp = static_cast<t*>(alloca(sizeof(t) * (std::size_t(n)))); \
v = ::libtorrent::span<t>(TORRENT_ALLOCA_tmp, std::size_t(n)); }
#endif

View File

@ -103,6 +103,20 @@ namespace libtorrent { namespace aux {
TORRENT_ASSERT(s <= std::size_t((std::numeric_limits<underlying_index>::max)()));
this->base::resize(s, v);
}
template <typename U = underlying_index, typename Cond
= typename std::enable_if<std::is_signed<U>::value>::type>
void reserve(underlying_index s)
{
TORRENT_ASSERT(s >= 0);
this->base::reserve(std::size_t(s));
}
void reserve(std::size_t s)
{
TORRENT_ASSERT(s <= std::size_t((std::numeric_limits<underlying_index>::max)()));
this->base::reserve(s);
}
};
template <typename Iter>

View File

@ -48,6 +48,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/disk_buffer_pool.hpp"
#include "libtorrent/file.hpp" // for iovec_t
#include "libtorrent/disk_io_job.hpp"
#if TORRENT_USE_ASSERTS
#include "libtorrent/aux_/vector.hpp"
#endif
namespace libtorrent
{
@ -95,7 +98,7 @@ namespace libtorrent
#endif // TORRENT_DISABLE_LOGGING
#if TORRENT_USE_ASSERTS
void print_piece_log(std::vector<piece_log_t> const& piece_log);
void print_piece_log(aux::vector<piece_log_t> const& piece_log);
void assert_print_piece(cached_piece_entry const* pe);
#endif
@ -315,7 +318,7 @@ namespace libtorrent
// this is a debug facility to keep a log
// of which operations have been run on this piece
std::vector<piece_log_t> piece_log;
aux::vector<piece_log_t> piece_log;
bool in_storage = false;
bool in_use = true;
@ -332,7 +335,7 @@ namespace libtorrent
struct hash_value
{
std::size_t operator()(cached_piece_entry const& p) const
{ return reinterpret_cast<std::size_t>(p.storage.get()) + static_cast<int>(p.piece); }
{ return reinterpret_cast<std::size_t>(p.storage.get()) + std::size_t(static_cast<int>(p.piece)); }
};
using cache_t = std::unordered_set<cached_piece_entry, hash_value>;

View File

@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket_type.hpp"
#include "libtorrent/session_settings.hpp"
#include "libtorrent/i2p_stream.hpp"
#include "libtorrent/aux_/vector.hpp"
namespace libtorrent
{
@ -149,7 +150,7 @@ private:
std::string m_url;
std::string m_user_agent;
std::vector<tcp::endpoint> m_endpoints;
aux::vector<tcp::endpoint> m_endpoints;
// if the current connection attempt fails, we'll connect to the
// endpoint with this index (in m_endpoints) next

View File

@ -238,7 +238,7 @@ static_assert(sizeof(job_action_name)/sizeof(job_action_name[0])
#if TORRENT_USE_ASSERTS
void print_piece_log(std::vector<piece_log_t> const& piece_log)
void print_piece_log(aux::vector<piece_log_t> const& piece_log)
{
for (int i = 0; i < int(piece_log.size()); ++i)
{

View File

@ -105,7 +105,7 @@ namespace libtorrent
std::unique_lock<std::mutex> l(m_pool_mutex);
if (m_exceeded_max_size)
ret = m_in_use - std::min(m_low_watermark, int(m_max_use - m_observers.size() * 2));
ret = m_in_use - std::min(m_low_watermark, m_max_use - int(m_observers.size()) * 2);
if (m_in_use + num_needed > m_max_use)
ret = std::max(ret, m_in_use + num_needed - m_max_use);
@ -196,7 +196,7 @@ namespace libtorrent
for (auto& i : iov)
{
i.iov_base = allocate_buffer_impl(l, "pending read");
i.iov_len = block_size();
i.iov_len = std::size_t(block_size());
if (i.iov_base == nullptr)
{
// uh oh. We failed to allocate the buffer!

View File

@ -123,13 +123,16 @@ namespace libtorrent
{
if (action != write) return false;
int block_offset = d.io.offset & (block_size - 1);
int block_offset = int(d.io.offset) & (block_size - 1);
int size = d.io.buffer_size;
int start = d.io.offset / block_size;
int start = int(d.io.offset) / block_size;
int end = block_offset > 0 && (size > block_size - block_offset) ? start + 2 : start + 1;
for (int i = start; i < end; ++i)
if (pe->blocks[i].dirty || pe->blocks[i].pending) return false;
{
cached_block_entry const& b = pe->blocks[std::size_t(i)];
if (b.dirty || b.pending) return false;
}
// if all our blocks are not pending and not dirty, it means they
// were successfully written to disk. This job is complete

View File

@ -676,7 +676,7 @@ namespace libtorrent
bool binary_string = false;
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
{
if (!is_print(static_cast<unsigned char>(*i)))
if (!is_print(*i))
{
binary_string = true;
break;
@ -698,7 +698,7 @@ namespace libtorrent
out += "list\n";
for (list_type::const_iterator i = list().begin(); i != list().end(); ++i)
{
i->to_string_impl(out, indent+1);
i->to_string_impl(out, indent + 1);
}
} break;
case dictionary_t:
@ -709,13 +709,13 @@ namespace libtorrent
bool binary_string = false;
for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k)
{
if (!is_print(static_cast<unsigned char>(*k)))
if (!is_print(*k))
{
binary_string = true;
break;
}
}
for (int j = 0; j < indent+1; ++j) out += " ";
for (int j = 0; j < indent + 1; ++j) out += " ";
out += "[";
if (binary_string) out += aux::to_hex(i->first);
else out += i->first;
@ -725,7 +725,7 @@ namespace libtorrent
&& i->second.type() != entry::int_t)
out += "\n";
else out += " ";
i->second.to_string_impl(out, indent+2);
i->second.to_string_impl(out, indent + 2);
}
} break;
case preformatted_t:

View File

@ -104,22 +104,20 @@ namespace libtorrent { namespace
{
#if !defined TORRENT_BUILD_SIMULATOR
address_v4 inaddr_to_address(in_addr const* ina, int len = 4)
address_v4 inaddr_to_address(in_addr const* ina, int const len = 4)
{
typedef boost::asio::ip::address_v4::bytes_type bytes_t;
bytes_t b;
std::memset(&b[0], 0, b.size());
if (len > 0) std::memcpy(&b[0], ina, (std::min)(len, int(b.size())));
boost::asio::ip::address_v4::bytes_type b;
b.fill(0);
if (len > 0) std::memcpy(b.data(), ina, std::min(std::size_t(len), b.size()));
return address_v4(b);
}
#if TORRENT_USE_IPV6
address_v6 inaddr6_to_address(in6_addr const* ina6, int len = 16)
address_v6 inaddr6_to_address(in6_addr const* ina6, int const len = 16)
{
typedef boost::asio::ip::address_v6::bytes_type bytes_t;
bytes_t b;
std::memset(&b[0], 0, b.size());
if (len > 0) std::memcpy(&b[0], ina6, (std::min)(len, int(b.size())));
boost::asio::ip::address_v6::bytes_type b;
b.fill(0);
if (len > 0) std::memcpy(b.data(), ina6, std::min(std::size_t(len), b.size()));
return address_v6(b);
}
#endif
@ -137,13 +135,13 @@ namespace libtorrent { namespace
{
if (sin->sa_family == AF_INET || assume_family == AF_INET)
return inaddr_to_address(&reinterpret_cast<sockaddr_in const*>(sin)->sin_addr
, sockaddr_len(sin) - offsetof(sockaddr, sa_data));
, sockaddr_len(sin) - int(offsetof(sockaddr, sa_data)));
#if TORRENT_USE_IPV6
else if (sin->sa_family == AF_INET6 || assume_family == AF_INET6)
{
auto saddr = reinterpret_cast<sockaddr_in6 const*>(sin);
auto ret = inaddr6_to_address(&saddr->sin6_addr
, sockaddr_len(sin) - offsetof(sockaddr, sa_data));
, sockaddr_len(sin) - int(offsetof(sockaddr, sa_data)));
ret.scope_id(saddr->sin6_scope_id);
return ret;
}
@ -358,7 +356,7 @@ namespace libtorrent
b1 = a1.to_v6().to_bytes();
b2 = a2.to_v6().to_bytes();
m = mask.to_v6().to_bytes();
for (int i = 0; i < int(b1.size()); ++i)
for (std::size_t i = 0; i < b1.size(); ++i)
{
b1[i] &= m[i];
b2[i] &= m[i];

View File

@ -214,7 +214,7 @@ namespace libtorrent
// if needed
unsigned long destlen = 4096;
int ret = 0;
unsigned long srclen = size - header_len;
unsigned long srclen = std::uint32_t(size - header_len);
in += header_len;
do
@ -242,7 +242,7 @@ namespace libtorrent
destlen *= 2;
if (destlen > std::uint32_t(maximum_size))
destlen = maximum_size;
destlen = std::uint32_t(maximum_size);
}
} while (ret == 1);

View File

@ -167,9 +167,9 @@ void http_connection::get(std::string const& url, time_duration timeout, int pri
char* end = request + sizeof(request);
char* ptr = request;
#define APPEND_FMT(fmt) ptr += std::snprintf(ptr, end - ptr, fmt)
#define APPEND_FMT1(fmt, arg) ptr += std::snprintf(ptr, end - ptr, fmt, arg)
#define APPEND_FMT2(fmt, arg1, arg2) ptr += std::snprintf(ptr, end - ptr, fmt, arg1, arg2)
#define APPEND_FMT(fmt) ptr += std::snprintf(ptr, std::size_t(end - ptr), fmt)
#define APPEND_FMT1(fmt, arg) ptr += std::snprintf(ptr, std::size_t(end - ptr), fmt, arg)
#define APPEND_FMT2(fmt, arg1, arg2) ptr += std::snprintf(ptr, std::size_t(end - ptr), fmt, arg1, arg2)
// exclude ssl here, because SSL assumes CONNECT support in the
// proxy and is handled at the lower layer
@ -396,7 +396,7 @@ void http_connection::start(std::string const& hostname, int port
{
m_hostname = hostname;
m_port = std::uint16_t(port);
m_endpoints.push_back(tcp::endpoint(address(), std::uint16_t(port)));
m_endpoints.push_back({address(), m_port});
connect();
}
else
@ -689,8 +689,8 @@ void http_connection::on_write(error_code const& e)
}
}
ADD_OUTSTANDING_ASYNC("http_connection::on_read");
m_sock.async_read_some(boost::asio::buffer(&m_recvbuffer[0] + m_read_pos
, amount_to_read)
m_sock.async_read_some(boost::asio::buffer(m_recvbuffer.data() + m_read_pos
, std::size_t(amount_to_read))
, std::bind(&http_connection::on_read
, shared_from_this(), _1, _2));
}
@ -744,7 +744,7 @@ void http_connection::on_read(error_code const& e
{
span<char const> rcv_buf(m_recvbuffer);
bool error = false;
m_parser.incoming(rcv_buf.first(m_read_pos), error);
m_parser.incoming(rcv_buf.first(std::size_t(m_read_pos)), error);
if (error)
{
// HTTP parse error

View File

@ -99,7 +99,7 @@ namespace libtorrent
{
if (handle_error(e, h)) return;
int const read_pos = int(m_buffer.size());
std::size_t const read_pos = m_buffer.size();
// look for \n\n and \r\n\r\n
// both of which means end of http response header
bool found_end = false;
@ -121,7 +121,7 @@ namespace libtorrent
if (found_end)
{
m_buffer.push_back(0);
char* status = std::strchr(&m_buffer[0], ' ');
char* status = std::strchr(m_buffer.data(), ' ');
if (status == nullptr)
{
h(boost::asio::error::operation_not_supported);
@ -147,7 +147,7 @@ namespace libtorrent
// read another byte from the socket
m_buffer.resize(read_pos + 1);
async_read(m_sock, boost::asio::buffer(&m_buffer[0] + read_pos, 1)
async_read(m_sock, boost::asio::buffer(m_buffer.data() + read_pos, 1)
, std::bind(&http_stream::handshake2, this, _1, std::move(h)));
}