fixing sign-conversion warnings, part 11, created aux::array and refactor (#1649)

fixing sign-conversion warnings, part 11, created aux::array and refactor
This commit is contained in:
Alden Torres 2017-02-04 22:05:53 -05:00 committed by Arvid Norberg
parent cce5714ee1
commit de6f2a9d88
15 changed files with 192 additions and 110 deletions

View File

@ -198,6 +198,7 @@ nobase_include_HEADERS = \
aux_/alloca.hpp \ aux_/alloca.hpp \
aux_/throw.hpp \ aux_/throw.hpp \
aux_/typed_span.hpp \ aux_/typed_span.hpp \
aux_/array.hpp \
\ \
extensions/smart_ban.hpp \ extensions/smart_ban.hpp \
extensions/ut_metadata.hpp \ extensions/ut_metadata.hpp \

View File

@ -0,0 +1,78 @@
/*
Copyright (c) 2017, Arvid Norberg, Alden Torres
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_ARRAY_HPP
#define TORRENT_ARRAY_HPP
#include <array>
#include "libtorrent/units.hpp"
#include "libtorrent/assert.hpp"
namespace libtorrent { namespace aux {
template <typename T, std::size_t Size, typename IndexType = int>
struct array : std::array<T, Size>
{
using base = std::array<T, Size>;
using underlying_index = typename underlying_index_t<IndexType>::type;
array() = default;
explicit array(std::array<T, Size>&& arr) : base(arr) {}
auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index()))
{
TORRENT_ASSERT(idx >= IndexType(0));
TORRENT_ASSERT(idx < end_index());
return this->base::operator[](std::size_t(static_cast<underlying_index>(idx)));
}
auto operator[](IndexType idx) -> decltype(this->base::operator[](underlying_index()))
{
TORRENT_ASSERT(idx >= IndexType(0));
TORRENT_ASSERT(idx < end_index());
return this->base::operator[](std::size_t(static_cast<underlying_index>(idx)));
}
#if !TORRENT_USE_ASSERTS
constexpr
#endif
IndexType end_index() const
{
TORRENT_ASSERT(this->size() <= std::size_t(std::numeric_limits<underlying_index>::max()));
return IndexType(static_cast<underlying_index>(this->size()));
}
};
}}
#endif

View File

@ -50,7 +50,7 @@ namespace libtorrent { namespace aux {
using base = std::unique_ptr<T[]>; using base = std::unique_ptr<T[]>;
using underlying_index = typename underlying_index_t<IndexType>::type; using underlying_index = typename underlying_index_t<IndexType>::type;
unique_ptr() {} unique_ptr() = default;
explicit unique_ptr(T* arr) : base(arr) {} explicit unique_ptr(T* arr) : base(arr) {}
auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index())) auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index()))

View File

@ -34,7 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_PEER_CLASS_SET_HPP_INCLUDED #define TORRENT_PEER_CLASS_SET_HPP_INCLUDED
#include "libtorrent/peer_class.hpp" #include "libtorrent/peer_class.hpp"
#include <array> #include "libtorrent/aux_/array.hpp"
namespace libtorrent { namespace libtorrent {
@ -50,7 +50,7 @@ namespace libtorrent {
peer_class_t class_at(int i) const peer_class_t class_at(int i) const
{ {
TORRENT_ASSERT(i >= 0 && i < int(m_size)); TORRENT_ASSERT(i >= 0 && i < int(m_size));
return m_class[std::size_t(i)]; return m_class[i];
} }
private: private:
@ -62,7 +62,7 @@ namespace libtorrent {
// class IDs. Each ID refers to a an entry in m_ses.m_peer_classes which // class IDs. Each ID refers to a an entry in m_ses.m_peer_classes which
// holds the metadata about the class. Classes affect bandwidth limits // holds the metadata about the class. Classes affect bandwidth limits
// among other things // among other things
std::array<peer_class_t, 15> m_class; aux::array<peer_class_t, 15> m_class;
}; };
} }

View File

@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_POLICY_HPP_INCLUDED #define TORRENT_POLICY_HPP_INCLUDED
#include <algorithm> #include <algorithm>
#include <deque>
#include "libtorrent/string_util.hpp" // for allocate_string_copy #include "libtorrent/string_util.hpp" // for allocate_string_copy
#include "libtorrent/request_blocks.hpp" // for source_rank #include "libtorrent/request_blocks.hpp" // for source_rank
@ -47,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#include "libtorrent/peer_connection_interface.hpp" #include "libtorrent/peer_connection_interface.hpp"
#include "libtorrent/aux_/deque.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -167,7 +167,7 @@ namespace libtorrent
int num_peers() const { return int(m_peers.size()); } int num_peers() const { return int(m_peers.size()); }
using peers_t = std::deque<torrent_peer*>; using peers_t = aux::deque<torrent_peer*>;
using iterator = peers_t::iterator; using iterator = peers_t::iterator;
using const_iterator = peers_t::const_iterator; using const_iterator = peers_t::const_iterator;
iterator begin() { return m_peers.begin(); } iterator begin() { return m_peers.begin(); }

View File

@ -34,11 +34,11 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_PERFORMANCE_COUNTERS_HPP_INCLUDED #define TORRENT_PERFORMANCE_COUNTERS_HPP_INCLUDED
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/aux_/array.hpp"
#include <cstdint> #include <cstdint>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <array>
namespace libtorrent namespace libtorrent
{ {
@ -449,15 +449,14 @@ namespace libtorrent
// of the counters per thread and collect them at convenient // of the counters per thread and collect them at convenient
// synchronization points // synchronization points
#ifdef ATOMIC_LLONG_LOCK_FREE #ifdef ATOMIC_LLONG_LOCK_FREE
std::array<std::atomic<std::int64_t>, num_counters> m_stats_counter; aux::array<std::atomic<std::int64_t>, num_counters> m_stats_counter;
#else #else
// if the atomic type is't lock-free, use a single lock instead, for // if the atomic type is't lock-free, use a single lock instead, for
// the whole array // the whole array
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
std::array<std::int64_t, num_counters> m_stats_counter; aux::array<std::int64_t, num_counters> m_stats_counter;
#endif #endif
}; };
} }
#endif #endif

View File

@ -2166,10 +2166,11 @@ namespace libtorrent
if (should_log(peer_log_alert::outgoing_message)) if (should_log(peer_log_alert::outgoing_message))
{ {
std::string bitfield_string; std::string bitfield_string;
bitfield_string.resize(num_pieces); std::size_t const n_pieces = aux::numeric_cast<std::size_t>(num_pieces);
for (int k = 0; k < num_pieces; ++k) bitfield_string.resize(n_pieces);
for (std::size_t k = 0; k < n_pieces; ++k)
{ {
if (msg[5 + k / 8] & (0x80 >> (k % 8))) bitfield_string[k] = '1'; if (msg[5 + int(k) / 8] & (0x80 >> (k % 8))) bitfield_string[k] = '1';
else bitfield_string[k] = '0'; else bitfield_string[k] = '0';
} }
peer_log(peer_log_alert::outgoing_message, "BITFIELD" peer_log(peer_log_alert::outgoing_message, "BITFIELD"
@ -2877,7 +2878,7 @@ namespace libtorrent
{ {
// select a crypto method // select a crypto method
int allowed_encryption = m_settings.get_int(settings_pack::allowed_enc_level); int allowed_encryption = m_settings.get_int(settings_pack::allowed_enc_level);
std::uint32_t crypto_select = crypto_field & allowed_encryption; std::uint32_t crypto_select = crypto_field & std::uint32_t(allowed_encryption);
// when prefer_rc4 is set, keep the most significant bit // when prefer_rc4 is set, keep the most significant bit
// otherwise keep the least significant one // otherwise keep the least significant one
@ -2907,14 +2908,14 @@ namespace libtorrent
} }
// write the pe4 step // write the pe4 step
write_pe4_sync(crypto_select); write_pe4_sync(aux::numeric_cast<int>(crypto_select));
} }
else // is_outgoing() else // is_outgoing()
{ {
// check if crypto select is valid // check if crypto select is valid
int allowed_encryption = m_settings.get_int(settings_pack::allowed_enc_level); int allowed_encryption = m_settings.get_int(settings_pack::allowed_enc_level);
crypto_field &= allowed_encryption; crypto_field &= std::uint32_t(allowed_encryption);
if (crypto_field == 0) if (crypto_field == 0)
{ {
// we don't allow any of the offered encryption levels // we don't allow any of the offered encryption levels

View File

@ -71,7 +71,7 @@ namespace libtorrent { namespace aux
__cpuid((int*)info, type); __cpuid((int*)info, type);
#elif defined __GNUC__ #elif defined __GNUC__
__get_cpuid(type, &info[0], &info[1], &info[2], &info[3]); __get_cpuid(std::uint32_t(type), &info[0], &info[1], &info[2], &info[3]);
#else #else
TORRENT_UNUSED(type); TORRENT_UNUSED(type);
// for non-x86 and non-amd64, just return zeroes // for non-x86 and non-amd64, just return zeroes

View File

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#include "libtorrent/units.hpp" #include "libtorrent/units.hpp"
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/aux_/array.hpp"
#include <functional> #include <functional>
@ -3386,7 +3387,7 @@ namespace libtorrent
disk_io_job* j = m_completed_jobs.get_all(); disk_io_job* j = m_completed_jobs.get_all();
l.unlock(); l.unlock();
std::array<disk_io_job*, 64> to_delete; aux::array<disk_io_job*, 64> to_delete;
int cnt = 0; int cnt = 0;
while (j) while (j)

View File

@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/escape_string.hpp" #include "libtorrent/aux_/escape_string.hpp"
#include "libtorrent/aux_/max_path.hpp" // for TORRENT_MAX_PATH #include "libtorrent/aux_/max_path.hpp" // for TORRENT_MAX_PATH
#include "libtorrent/string_util.hpp" // for to_string #include "libtorrent/string_util.hpp" // for to_string
#include "libtorrent/aux_/array.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -245,7 +246,7 @@ namespace libtorrent
// first, strip the file:// part. // first, strip the file:// part.
// On windows, we have // On windows, we have
// to strip the first / as well // to strip the first / as well
int num_to_strip = 7; std::size_t num_to_strip = 7;
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (url[7] == '/' || url[7] == '\\') ++num_to_strip; if (url[7] == '/' || url[7] == '\\') ++num_to_strip;
#endif #endif
@ -279,8 +280,8 @@ namespace libtorrent
'4', '5', '6', '7', '8', '9', '+', '/' '4', '5', '6', '7', '8', '9', '+', '/'
}; };
std::array<std::uint8_t, 3> inbuf; aux::array<std::uint8_t, 3> inbuf;
std::array<std::uint8_t, 4> outbuf; aux::array<std::uint8_t, 4> outbuf;
std::string ret; std::string ret;
for (std::string::const_iterator i = s.begin(); i != s.end();) for (std::string::const_iterator i = s.begin(); i != s.end();)
@ -335,10 +336,10 @@ namespace libtorrent
}; };
char const *base32_table = 0 != (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical; char const *base32_table = 0 != (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical;
static std::array<int const, 6> input_output_mapping = {{0, 2, 4, 5, 7, 8}}; static aux::array<int, 6> const input_output_mapping{{{0, 2, 4, 5, 7, 8}}};
std::array<std::uint8_t, 5> inbuf; aux::array<std::uint8_t, 5> inbuf;
std::array<std::uint8_t, 8> outbuf; aux::array<std::uint8_t, 8> outbuf;
std::string ret; std::string ret;
for (std::string::const_iterator i = s.begin(); i != s.end();) for (std::string::const_iterator i = s.begin(); i != s.end();)
@ -383,8 +384,8 @@ namespace libtorrent
std::string base32decode(std::string const& s) std::string base32decode(std::string const& s)
{ {
std::array<std::uint8_t, 8> inbuf; aux::array<std::uint8_t, 8> inbuf;
std::array<std::uint8_t, 5> outbuf; aux::array<std::uint8_t, 5> outbuf;
std::string ret; std::string ret;
for (std::string::const_iterator i = s.begin(); i != s.end();) for (std::string::const_iterator i = s.begin(); i != s.end();)
@ -400,9 +401,9 @@ namespace libtorrent
{ {
char const in = char(std::toupper(*i++)); char const in = char(std::toupper(*i++));
if (in >= 'A' && in <= 'Z') if (in >= 'A' && in <= 'Z')
inbuf[j] = in - 'A'; inbuf[j] = (in - 'A') & 0xff;
else if (in >= '2' && in <= '7') else if (in >= '2' && in <= '7')
inbuf[j] = in - '2' + ('Z' - 'A') + 1; inbuf[j] = (in - '2' + ('Z' - 'A') + 1) & 0xff;
else if (in == '=') else if (in == '=')
{ {
inbuf[j] = 0; inbuf[j] = 0;

View File

@ -429,9 +429,9 @@ namespace libtorrent
} }
s->file_size = ret.st_size; s->file_size = ret.st_size;
s->atime = ret.st_atime; s->atime = std::uint64_t(ret.st_atime);
s->mtime = ret.st_mtime; s->mtime = std::uint64_t(ret.st_mtime);
s->ctime = ret.st_ctime; s->ctime = std::uint64_t(ret.st_ctime);
s->mode = (S_ISREG(ret.st_mode) ? file_status::regular_file : 0) s->mode = (S_ISREG(ret.st_mode) ? file_status::regular_file : 0)
| (S_ISDIR(ret.st_mode) ? file_status::directory : 0) | (S_ISDIR(ret.st_mode) ? file_status::directory : 0)
@ -708,7 +708,7 @@ namespace libtorrent
) ++p; ) ++p;
if (p - start > 0) if (p - start > 0)
{ {
ret.append(start, p - start); ret.append(start, aux::numeric_cast<std::size_t>(p - start));
if (only_first_part) return ret; if (only_first_part) return ret;
ret.append(1, '\0'); ret.append(1, '\0');
} }
@ -731,12 +731,13 @@ namespace libtorrent
{ {
for (int i = int(f.size()) - 1; i >= 0; --i) for (int i = int(f.size()) - 1; i >= 0; --i)
{ {
if (f[i] == '/') break; std::size_t const idx = std::size_t(i);
if (f[idx] == '/') break;
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (f[i] == '\\') break; if (f[idx] == '\\') break;
#endif #endif
if (f[i] != '.') continue; if (f[idx] != '.') continue;
return f.substr(i); return f.substr(idx);
} }
return ""; return "";
} }
@ -750,21 +751,22 @@ namespace libtorrent
char const* ext = std::strrchr(f.c_str(), '.'); char const* ext = std::strrchr(f.c_str(), '.');
// if we don't have an extension, just return f // if we don't have an extension, just return f
if (ext == nullptr || ext == &f[0] || (slash != nullptr && ext < slash)) return f; if (ext == nullptr || ext == &f[0] || (slash != nullptr && ext < slash)) return f;
return f.substr(0, ext - &f[0]); return f.substr(0, aux::numeric_cast<std::size_t>(ext - &f[0]));
} }
void replace_extension(std::string& f, std::string const& ext) void replace_extension(std::string& f, std::string const& ext)
{ {
for (int i = int(f.size()) - 1; i >= 0; --i) for (int i = int(f.size()) - 1; i >= 0; --i)
{ {
if (f[i] == '/') break; std::size_t const idx = std::size_t(i);
if (f[idx] == '/') break;
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
if (f[i] == '\\') break; if (f[idx] == '\\') break;
#endif #endif
if (f[i] != '.') continue; if (f[idx] != '.') continue;
f.resize(i); f.resize(idx);
break; break;
} }
f += '.'; f += '.';
@ -813,10 +815,10 @@ namespace libtorrent
int len = int(f.size()) - 1; int len = int(f.size()) - 1;
// if the last character is / or \ ignore it // if the last character is / or \ ignore it
if (f[len] == '/' || f[len] == '\\') --len; if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\') --len;
while (len >= 0) while (len >= 0)
{ {
if (f[len] == '/' || f[len] == '\\') if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\')
break; break;
--len; --len;
} }
@ -835,16 +837,16 @@ namespace libtorrent
int len = int(f.size()); int len = int(f.size());
// if the last character is / or \ ignore it // if the last character is / or \ ignore it
if (f[len-1] == '/' || f[len-1] == '\\') --len; if (f[std::size_t(len - 1)] == '/' || f[std::size_t(len - 1)] == '\\') --len;
while (len > 0) while (len > 0)
{ {
--len; --len;
if (f[len] == '/' || f[len] == '\\') if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\')
break; break;
} }
if (f[len] == '/' || f[len] == '\\') ++len; if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\') ++len;
return std::string(f.c_str(), len); return std::string(f.c_str(), std::size_t(len));
} }
char const* filename_cstr(char const* f) char const* filename_cstr(char const* f)
@ -884,10 +886,10 @@ namespace libtorrent
|| *sep == '\\' || *sep == '\\'
#endif #endif
) )
return std::string(sep + 1, len); return std::string(sep + 1, std::size_t(len));
++len; ++len;
} }
return std::string(first, len); return std::string(first, std::size_t(len));
} }
return std::string(sep + 1); return std::string(sep + 1);
@ -930,12 +932,12 @@ namespace libtorrent
bool const need_sep = lhs[lhs.size() - 1] != '/'; bool const need_sep = lhs[lhs.size() - 1] != '/';
#endif #endif
std::string ret; std::string ret;
int target_size = int(lhs.size() + rhs.size() + 2); std::size_t target_size = lhs.size() + rhs.size() + 2;
ret.resize(target_size); ret.resize(target_size);
target_size = std::snprintf(&ret[0], target_size, "%*s%s%*s" target_size = aux::numeric_cast<std::size_t>(std::snprintf(&ret[0], target_size, "%*s%s%*s"
, int(lhs.size()), lhs.data() , int(lhs.size()), lhs.data()
, (need_sep ? TORRENT_SEPARATOR : "") , (need_sep ? TORRENT_SEPARATOR : "")
, int(rhs.size()), rhs.data()); , int(rhs.size()), rhs.data()));
ret.resize(target_size); ret.resize(target_size);
return ret; return ret;
} }
@ -1628,7 +1630,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
bool coalesce_read_buffers(span<iovec_t const>& bufs bool coalesce_read_buffers(span<iovec_t const>& bufs
, iovec_t& tmp) , iovec_t& tmp)
{ {
int const buf_size = bufs_size(bufs); std::size_t const buf_size = aux::numeric_cast<std::size_t>(bufs_size(bufs));
char* buf = static_cast<char*>(std::malloc(buf_size)); char* buf = static_cast<char*>(std::malloc(buf_size));
if (!buf) return false; if (!buf) return false;
tmp.iov_base = buf; tmp.iov_base = buf;
@ -1647,7 +1649,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
bool coalesce_write_buffers(span<iovec_t const>& bufs bool coalesce_write_buffers(span<iovec_t const>& bufs
, iovec_t& tmp) , iovec_t& tmp)
{ {
int const buf_size = bufs_size(bufs); std::size_t const buf_size = aux::numeric_cast<std::size_t>(bufs_size(bufs));
char* buf = static_cast<char*>(std::malloc(buf_size)); char* buf = static_cast<char*>(std::malloc(buf_size));
if (!buf) return false; if (!buf) return false;
gather_copy(bufs, buf); gather_copy(bufs, buf);

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/bloom_filter.hpp> #include <libtorrent/bloom_filter.hpp>
#include <libtorrent/session_settings.hpp> #include <libtorrent/session_settings.hpp>
#include <libtorrent/random.hpp> #include <libtorrent/random.hpp>
#include <libtorrent/aux_/vector.hpp>
namespace libtorrent { namespace libtorrent {
namespace dht { namespace dht {
@ -176,7 +177,7 @@ namespace
struct infohashes_sample struct infohashes_sample
{ {
std::vector<sha1_hash> samples; aux::vector<sha1_hash> samples;
time_point created = min_time(); time_point created = min_time();
int count() const { return int(samples.size()); } int count() const { return int(samples.size()); }
@ -478,7 +479,7 @@ namespace
refresh_infohashes_sample(); refresh_infohashes_sample();
std::vector<sha1_hash> const& samples = m_infohashes_sample.samples; aux::vector<sha1_hash> const& samples = m_infohashes_sample.samples;
item["samples"] = span<char const>( item["samples"] = span<char const>(
reinterpret_cast<char const*>(samples.data()), samples.size() * 20); reinterpret_cast<char const*>(samples.data()), samples.size() * 20);
@ -582,7 +583,7 @@ namespace
&& m_infohashes_sample.count() >= max_count) && m_infohashes_sample.count() >= max_count)
return; return;
std::vector<sha1_hash>& samples = m_infohashes_sample.samples; aux::vector<sha1_hash>& samples = m_infohashes_sample.samples;
samples.clear(); samples.clear();
samples.reserve(count); samples.reserve(count);

View File

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket_io.hpp" // for print_endpoint #include "libtorrent/socket_io.hpp" // for print_endpoint
#include "libtorrent/invariant_check.hpp" #include "libtorrent/invariant_check.hpp"
#include "libtorrent/address.hpp" #include "libtorrent/address.hpp"
#include "libtorrent/aux_/array.hpp"
using namespace std::placeholders; using namespace std::placeholders;
@ -126,8 +127,8 @@ int routing_table::bucket_limit(int bucket) const
{ {
if (!m_settings.extended_routing_table) return m_bucket_size; if (!m_settings.extended_routing_table) return m_bucket_size;
static const int size_exceptions[] = {16, 8, 4, 2}; static const aux::array<int, 4> size_exceptions{{{16, 8, 4, 2}}};
if (bucket < int(sizeof(size_exceptions) / sizeof(size_exceptions[0]))) if (bucket < size_exceptions.end_index())
return m_bucket_size * size_exceptions[bucket]; return m_bucket_size * size_exceptions[bucket];
return m_bucket_size; return m_bucket_size;
} }
@ -692,7 +693,7 @@ ip_ok:
// the data someone is looking for, make sure there is an affinity // the data someone is looking for, make sure there is an affinity
// towards having a good spread of node IDs in each bucket // towards having a good spread of node IDs in each bucket
std::uint32_t mask = bucket_size_limit - 1; int mask = bucket_size_limit - 1;
int mask_shift = 0; int mask_shift = 0;
TORRENT_ASSERT_VAL(mask > 0, mask); TORRENT_ASSERT_VAL(mask > 0, mask);
while ((mask & 0x80) == 0) while ((mask & 0x80) == 0)
@ -738,7 +739,7 @@ ip_ok:
// have a unique prefix // have a unique prefix
// find node entries with duplicate prefixes in O(1) // find node entries with duplicate prefixes in O(1)
std::vector<bucket_t::iterator> prefix(int(1 << (8 - mask_shift)), b.end()); aux::vector<bucket_t::iterator> prefix(aux::numeric_cast<std::size_t>(int(1 << (8 - mask_shift))), b.end());
TORRENT_ASSERT(int(prefix.size()) >= bucket_size_limit); TORRENT_ASSERT(int(prefix.size()) >= bucket_size_limit);
// the begin iterator from this object is used as a placeholder // the begin iterator from this object is used as a placeholder
@ -1061,7 +1062,7 @@ void routing_table::find_node(node_id const& target
int const bucket_index = int(std::distance(m_buckets.begin(), i)); int const bucket_index = int(std::distance(m_buckets.begin(), i));
int const bucket_size_limit = bucket_limit(bucket_index); int const bucket_size_limit = bucket_limit(bucket_index);
l.reserve(bucket_size_limit); l.reserve(aux::numeric_cast<std::size_t>(bucket_size_limit));
table_t::iterator j = i; table_t::iterator j = i;
@ -1088,7 +1089,7 @@ void routing_table::find_node(node_id const& target
, [&target](node_entry const& lhs, node_entry const& rhs) , [&target](node_entry const& lhs, node_entry const& rhs)
{ return compare_ref(lhs.id, rhs.id, target); }); { return compare_ref(lhs.id, rhs.id, target); });
l.resize(count); l.resize(aux::numeric_cast<std::size_t>(count));
return; return;
} }
unsorted_start_idx = int(l.size()); unsorted_start_idx = int(l.size());
@ -1127,7 +1128,7 @@ void routing_table::find_node(node_id const& target
, [&target](node_entry const& lhs, node_entry const& rhs) , [&target](node_entry const& lhs, node_entry const& rhs)
{ return compare_ref(lhs.id, rhs.id, target); }); { return compare_ref(lhs.id, rhs.id, target); });
l.resize(count); l.resize(aux::numeric_cast<std::size_t>(count));
return; return;
} }
unsorted_start_idx = int(l.size()); unsorted_start_idx = int(l.size());

View File

@ -50,7 +50,7 @@ namespace libtorrent {
counters::counters(counters const& c) counters::counters(counters const& c)
{ {
#ifdef ATOMIC_LLONG_LOCK_FREE #ifdef ATOMIC_LLONG_LOCK_FREE
for (std::size_t i = 0; i < m_stats_counter.size(); ++i) for (int i = 0; i < m_stats_counter.end_index(); ++i)
m_stats_counter[i].store( m_stats_counter[i].store(
c.m_stats_counter[i].load(std::memory_order_relaxed) c.m_stats_counter[i].load(std::memory_order_relaxed)
, std::memory_order_relaxed); , std::memory_order_relaxed);
@ -63,7 +63,7 @@ namespace libtorrent {
counters& counters::operator=(counters const& c) counters& counters::operator=(counters const& c)
{ {
#ifdef ATOMIC_LLONG_LOCK_FREE #ifdef ATOMIC_LLONG_LOCK_FREE
for (std::size_t i = 0; i < m_stats_counter.size(); ++i) for (int i = 0; i < m_stats_counter.end_index(); ++i)
m_stats_counter[i].store( m_stats_counter[i].store(
c.m_stats_counter[i].load(std::memory_order_relaxed) c.m_stats_counter[i].load(std::memory_order_relaxed)
, std::memory_order_relaxed); , std::memory_order_relaxed);
@ -90,7 +90,7 @@ namespace libtorrent {
// the argument specifies which counter to // the argument specifies which counter to
// increment or decrement // increment or decrement
std::int64_t counters::inc_stats_counter(int c, std::int64_t value) std::int64_t counters::inc_stats_counter(int const c, std::int64_t const value)
{ {
// if c >= num_stats_counters, it means it's not // if c >= num_stats_counters, it means it's not
// a monotonically increasing counter, but a gauge // a monotonically increasing counter, but a gauge
@ -110,9 +110,9 @@ namespace libtorrent {
#endif #endif
} }
// ratio is a vaue between 0 and 100 representing the percentage the value // ratio is a value between 0 and 100 representing the percentage the value
// is blended in at. // is blended in at.
void counters::blend_stats_counter(int c, std::int64_t value, int ratio) void counters::blend_stats_counter(int const c, std::int64_t const value, int const ratio)
{ {
TORRENT_ASSERT(c >= num_stats_counters); TORRENT_ASSERT(c >= num_stats_counters);
TORRENT_ASSERT(c < num_counters); TORRENT_ASSERT(c < num_counters);
@ -121,21 +121,21 @@ namespace libtorrent {
#ifdef ATOMIC_LLONG_LOCK_FREE #ifdef ATOMIC_LLONG_LOCK_FREE
std::int64_t current = m_stats_counter[c].load(std::memory_order_relaxed); std::int64_t current = m_stats_counter[c].load(std::memory_order_relaxed);
std::int64_t new_value = (current * (100-ratio) + value * ratio) / 100; std::int64_t new_value = (current * (100 - ratio) + value * ratio) / 100;
while (!m_stats_counter[c].compare_exchange_weak(current, new_value while (!m_stats_counter[c].compare_exchange_weak(current, new_value
, std::memory_order_relaxed)) , std::memory_order_relaxed))
{ {
new_value = (current * (100-ratio) + value * ratio) / 100; new_value = (current * (100 - ratio) + value * ratio) / 100;
} }
#else #else
std::lock_guard<std::mutex> l(m_mutex); std::lock_guard<std::mutex> l(m_mutex);
std::int64_t current = m_stats_counter[c]; std::int64_t current = m_stats_counter[c];
m_stats_counter[c] = (current * (100-ratio) + value * ratio) / 100; m_stats_counter[c] = (current * (100 - ratio) + value * ratio) / 100;
#endif #endif
} }
void counters::set_value(int c, std::int64_t value) void counters::set_value(int const c, std::int64_t const value)
{ {
TORRENT_ASSERT(c >= 0); TORRENT_ASSERT(c >= 0);
TORRENT_ASSERT(c < num_counters); TORRENT_ASSERT(c < num_counters);

View File

@ -35,9 +35,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#include "libtorrent/settings_pack.hpp" #include "libtorrent/settings_pack.hpp"
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/aux_/array.hpp"
#include <algorithm> #include <algorithm>
#include <array>
namespace { namespace {
@ -99,8 +99,8 @@ namespace libtorrent
using aux::session_impl; using aux::session_impl;
std::array<str_setting_entry_t, settings_pack::num_string_settings> str_settings = aux::array<str_setting_entry_t, settings_pack::num_string_settings> const str_settings
{{ ({{
SET(user_agent, "libtorrent/" LIBTORRENT_VERSION, &session_impl::update_user_agent), SET(user_agent, "libtorrent/" LIBTORRENT_VERSION, &session_impl::update_user_agent),
SET(announce_ip, nullptr, nullptr), SET(announce_ip, nullptr, nullptr),
SET(mmap_cache, nullptr, nullptr), SET(mmap_cache, nullptr, nullptr),
@ -117,10 +117,10 @@ namespace libtorrent
SET(i2p_hostname, "", &session_impl::update_i2p_bridge), SET(i2p_hostname, "", &session_impl::update_i2p_bridge),
SET(peer_fingerprint, "-LT1200-", &session_impl::update_peer_fingerprint), SET(peer_fingerprint, "-LT1200-", &session_impl::update_peer_fingerprint),
SET(dht_bootstrap_nodes, "dht.libtorrent.org:25401", &session_impl::update_dht_bootstrap_nodes) SET(dht_bootstrap_nodes, "dht.libtorrent.org:25401", &session_impl::update_dht_bootstrap_nodes)
}}; }});
std::array<bool_setting_entry_t, settings_pack::num_bool_settings> bool_settings = aux::array<bool_setting_entry_t, settings_pack::num_bool_settings> const bool_settings
{{ ({{
SET(allow_multiple_connections_per_ip, false, nullptr), SET(allow_multiple_connections_per_ip, false, nullptr),
DEPRECATED_SET(ignore_limits_on_local_network, true, &session_impl::update_ignore_rate_limits_on_local_network), DEPRECATED_SET(ignore_limits_on_local_network, true, &session_impl::update_ignore_rate_limits_on_local_network),
SET(send_redundant_have, true, nullptr), SET(send_redundant_have, true, nullptr),
@ -189,10 +189,10 @@ namespace libtorrent
SET(proxy_peer_connections, true, nullptr), SET(proxy_peer_connections, true, nullptr),
SET(auto_sequential, true, &session_impl::update_auto_sequential), SET(auto_sequential, true, &session_impl::update_auto_sequential),
SET(proxy_tracker_connections, true, nullptr), SET(proxy_tracker_connections, true, nullptr),
}}; }});
std::array<int_setting_entry_t, settings_pack::num_int_settings> int_settings = aux::array<int_setting_entry_t, settings_pack::num_int_settings> const int_settings
{{ ({{
SET(tracker_completion_timeout, 30, nullptr), SET(tracker_completion_timeout, 30, nullptr),
SET(tracker_receive_timeout, 10, nullptr), SET(tracker_receive_timeout, 10, nullptr),
SET(stop_tracker_timeout, 5, nullptr), SET(stop_tracker_timeout, 5, nullptr),
@ -321,7 +321,7 @@ namespace libtorrent
SET(proxy_port, 0, &session_impl::update_proxy), SET(proxy_port, 0, &session_impl::update_proxy),
SET(i2p_port, 0, &session_impl::update_i2p_bridge), SET(i2p_port, 0, &session_impl::update_i2p_bridge),
SET(cache_size_volatile, 256, nullptr) SET(cache_size_volatile, 256, nullptr)
}}; }});
#undef SET #undef SET
#undef SET_DEPRECATED #undef SET_DEPRECATED
@ -330,20 +330,20 @@ namespace libtorrent
int setting_by_name(std::string const& key) int setting_by_name(std::string const& key)
{ {
for (std::size_t k = 0; k < str_settings.size(); ++k) for (int k = 0; k < str_settings.end_index(); ++k)
{ {
if (key != str_settings[k].name) continue; if (key != str_settings[k].name) continue;
return settings_pack::string_type_base + int(k); return settings_pack::string_type_base + k;
} }
for (std::size_t k = 0; k < int_settings.size(); ++k) for (int k = 0; k < int_settings.end_index(); ++k)
{ {
if (key != int_settings[k].name) continue; if (key != int_settings[k].name) continue;
return settings_pack::int_type_base + int(k); return settings_pack::int_type_base + k;
} }
for (std::size_t k = 0; k < bool_settings.size(); ++k) for (int k = 0; k < bool_settings.end_index(); ++k)
{ {
if (key != bool_settings[k].name) continue; if (key != bool_settings[k].name) continue;
return settings_pack::bool_type_base + int(k); return settings_pack::bool_type_base + k;
} }
return -1; return -1;
} }
@ -379,27 +379,27 @@ namespace libtorrent
case bdecode_node::int_t: case bdecode_node::int_t:
{ {
bool found = false; bool found = false;
for (std::size_t k = 0; k < int_settings.size(); ++k) for (int k = 0; k < int_settings.end_index(); ++k)
{ {
if (key != int_settings[k].name) continue; if (key != int_settings[k].name) continue;
pack.set_int(settings_pack::int_type_base + int(k), int(val.int_value())); pack.set_int(settings_pack::int_type_base + k, int(val.int_value()));
found = true; found = true;
break; break;
} }
if (found) continue; if (found) continue;
for (std::size_t k = 0; k < bool_settings.size(); ++k) for (int k = 0; k < bool_settings.end_index(); ++k)
{ {
if (key != bool_settings[k].name) continue; if (key != bool_settings[k].name) continue;
pack.set_bool(settings_pack::bool_type_base + int(k), val.int_value() != 0); pack.set_bool(settings_pack::bool_type_base + k, val.int_value() != 0);
break; break;
} }
} }
break; break;
case bdecode_node::string_t: case bdecode_node::string_t:
for (std::size_t k = 0; k < str_settings.size(); ++k) for (int k = 0; k < str_settings.end_index(); ++k)
{ {
if (key != str_settings[k].name) continue; if (key != str_settings[k].name) continue;
pack.set_str(settings_pack::string_type_base + int(k), val.string_value().to_string()); pack.set_str(settings_pack::string_type_base + k, val.string_value().to_string());
break; break;
} }
break; break;
@ -416,20 +416,20 @@ namespace libtorrent
for (int i = 0; i < settings_pack::num_string_settings; ++i) for (int i = 0; i < settings_pack::num_string_settings; ++i)
{ {
char const* cmp = str_settings[i].default_value == nullptr ? "" : str_settings[i].default_value; char const* cmp = str_settings[i].default_value == nullptr ? "" : str_settings[i].default_value;
if (cmp == s.m_strings[i]) continue; if (cmp == s.m_strings[std::size_t(i)]) continue;
sett[str_settings[i].name] = s.m_strings[i]; sett[str_settings[i].name] = s.m_strings[std::size_t(i)];
} }
for (int i = 0; i < settings_pack::num_int_settings; ++i) for (int i = 0; i < settings_pack::num_int_settings; ++i)
{ {
if (int_settings[i].default_value == s.m_ints[i]) continue; if (int_settings[i].default_value == s.m_ints[std::size_t(i)]) continue;
sett[int_settings[i].name] = s.m_ints[i]; sett[int_settings[i].name] = s.m_ints[std::size_t(i)];
} }
for (int i = 0; i < settings_pack::num_bool_settings; ++i) for (int i = 0; i < settings_pack::num_bool_settings; ++i)
{ {
if (bool_settings[i].default_value == s.m_bools[i]) continue; if (bool_settings[i].default_value == s.m_bools[std::size_t(i)]) continue;
sett[bool_settings[i].name] = s.m_bools[i]; sett[bool_settings[i].name] = s.m_bools[std::size_t(i)];
} }
} }
@ -674,27 +674,24 @@ namespace libtorrent
case string_type_base: case string_type_base:
{ {
std::pair<std::uint16_t, std::string> v(name, std::string()); std::pair<std::uint16_t, std::string> v(name, std::string());
std::vector<std::pair<std::uint16_t, std::string> >::iterator i auto const i = std::lower_bound(m_strings.begin(), m_strings.end()
= std::lower_bound(m_strings.begin(), m_strings.end(), v , v, &compare_first<std::string>);
, &compare_first<std::string>);
if (i != m_strings.end() && i->first == name) m_strings.erase(i); if (i != m_strings.end() && i->first == name) m_strings.erase(i);
break; break;
} }
case int_type_base: case int_type_base:
{ {
std::pair<std::uint16_t, int> v(name, 0); std::pair<std::uint16_t, int> v(name, 0);
std::vector<std::pair<std::uint16_t, int> >::iterator i auto const i = std::lower_bound(m_ints.begin(), m_ints.end()
= std::lower_bound(m_ints.begin(), m_ints.end(), v , v, &compare_first<int>);
, &compare_first<int>);
if (i != m_ints.end() && i->first == name) m_ints.erase(i); if (i != m_ints.end() && i->first == name) m_ints.erase(i);
break; break;
} }
case bool_type_base: case bool_type_base:
{ {
std::pair<std::uint16_t, bool> v(name, false); std::pair<std::uint16_t, bool> v(name, false);
std::vector<std::pair<std::uint16_t, bool> >::iterator i auto const i = std::lower_bound(m_bools.begin(), m_bools.end()
= std::lower_bound(m_bools.begin(), m_bools.end(), v , v, &compare_first<bool>);
, &compare_first<bool>);
if (i != m_bools.end() && i->first == name) m_bools.erase(i); if (i != m_bools.end() && i->first == name) m_bools.erase(i);
break; break;
} }