diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 304df702a..bff16eda3 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -198,6 +198,7 @@ nobase_include_HEADERS = \ aux_/alloca.hpp \ aux_/throw.hpp \ aux_/typed_span.hpp \ + aux_/array.hpp \ \ extensions/smart_ban.hpp \ extensions/ut_metadata.hpp \ diff --git a/include/libtorrent/aux_/array.hpp b/include/libtorrent/aux_/array.hpp new file mode 100644 index 000000000..739e8039c --- /dev/null +++ b/include/libtorrent/aux_/array.hpp @@ -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 + +#include "libtorrent/units.hpp" +#include "libtorrent/assert.hpp" + +namespace libtorrent { namespace aux { + + template + struct array : std::array + { + using base = std::array; + using underlying_index = typename underlying_index_t::type; + + array() = default; + explicit array(std::array&& 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(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(idx))); + } + +#if !TORRENT_USE_ASSERTS + constexpr +#endif + IndexType end_index() const + { + TORRENT_ASSERT(this->size() <= std::size_t(std::numeric_limits::max())); + return IndexType(static_cast(this->size())); + } + }; + +}} + +#endif diff --git a/include/libtorrent/aux_/unique_ptr.hpp b/include/libtorrent/aux_/unique_ptr.hpp index 9e7c428c2..ddf7c5112 100644 --- a/include/libtorrent/aux_/unique_ptr.hpp +++ b/include/libtorrent/aux_/unique_ptr.hpp @@ -50,7 +50,7 @@ namespace libtorrent { namespace aux { using base = std::unique_ptr; using underlying_index = typename underlying_index_t::type; - unique_ptr() {} + unique_ptr() = default; explicit unique_ptr(T* arr) : base(arr) {} auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index())) diff --git a/include/libtorrent/peer_class_set.hpp b/include/libtorrent/peer_class_set.hpp index d48e423a6..ad2a17a45 100644 --- a/include/libtorrent/peer_class_set.hpp +++ b/include/libtorrent/peer_class_set.hpp @@ -34,7 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_PEER_CLASS_SET_HPP_INCLUDED #include "libtorrent/peer_class.hpp" -#include +#include "libtorrent/aux_/array.hpp" namespace libtorrent { @@ -50,7 +50,7 @@ namespace libtorrent { peer_class_t class_at(int i) const { TORRENT_ASSERT(i >= 0 && i < int(m_size)); - return m_class[std::size_t(i)]; + return m_class[i]; } private: @@ -62,7 +62,7 @@ namespace libtorrent { // 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 // among other things - std::array m_class; + aux::array m_class; }; } diff --git a/include/libtorrent/peer_list.hpp b/include/libtorrent/peer_list.hpp index 2f0d60281..b8213eaca 100644 --- a/include/libtorrent/peer_list.hpp +++ b/include/libtorrent/peer_list.hpp @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_POLICY_HPP_INCLUDED #include -#include #include "libtorrent/string_util.hpp" // for allocate_string_copy #include "libtorrent/request_blocks.hpp" // for source_rank @@ -47,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/debug.hpp" #include "libtorrent/peer_connection_interface.hpp" +#include "libtorrent/aux_/deque.hpp" namespace libtorrent { @@ -167,7 +167,7 @@ namespace libtorrent int num_peers() const { return int(m_peers.size()); } - using peers_t = std::deque; + using peers_t = aux::deque; using iterator = peers_t::iterator; using const_iterator = peers_t::const_iterator; iterator begin() { return m_peers.begin(); } diff --git a/include/libtorrent/performance_counters.hpp b/include/libtorrent/performance_counters.hpp index a8a6c8159..1ad8316cb 100644 --- a/include/libtorrent/performance_counters.hpp +++ b/include/libtorrent/performance_counters.hpp @@ -34,11 +34,11 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_PERFORMANCE_COUNTERS_HPP_INCLUDED #include "libtorrent/config.hpp" +#include "libtorrent/aux_/array.hpp" #include #include #include -#include namespace libtorrent { @@ -449,15 +449,14 @@ namespace libtorrent // of the counters per thread and collect them at convenient // synchronization points #ifdef ATOMIC_LLONG_LOCK_FREE - std::array, num_counters> m_stats_counter; + aux::array, num_counters> m_stats_counter; #else // if the atomic type is't lock-free, use a single lock instead, for // the whole array mutable std::mutex m_mutex; - std::array m_stats_counter; + aux::array m_stats_counter; #endif }; } #endif - diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 3161c6750..e2eb498c3 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -2166,10 +2166,11 @@ namespace libtorrent if (should_log(peer_log_alert::outgoing_message)) { std::string bitfield_string; - bitfield_string.resize(num_pieces); - for (int k = 0; k < num_pieces; ++k) + std::size_t const n_pieces = aux::numeric_cast(num_pieces); + 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'; } peer_log(peer_log_alert::outgoing_message, "BITFIELD" @@ -2877,7 +2878,7 @@ namespace libtorrent { // select a crypto method 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 // otherwise keep the least significant one @@ -2907,14 +2908,14 @@ namespace libtorrent } // write the pe4 step - write_pe4_sync(crypto_select); + write_pe4_sync(aux::numeric_cast(crypto_select)); } else // is_outgoing() { // check if crypto select is valid 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) { // we don't allow any of the offered encryption levels diff --git a/src/cpuid.cpp b/src/cpuid.cpp index 6de1d1836..dd0dce2c9 100644 --- a/src/cpuid.cpp +++ b/src/cpuid.cpp @@ -71,7 +71,7 @@ namespace libtorrent { namespace aux __cpuid((int*)info, type); #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 TORRENT_UNUSED(type); // for non-x86 and non-amd64, just return zeroes diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 503bea6ce..e56e95112 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" #include "libtorrent/units.hpp" #include "libtorrent/hasher.hpp" +#include "libtorrent/aux_/array.hpp" #include @@ -3386,7 +3387,7 @@ namespace libtorrent disk_io_job* j = m_completed_jobs.get_all(); l.unlock(); - std::array to_delete; + aux::array to_delete; int cnt = 0; while (j) diff --git a/src/escape_string.cpp b/src/escape_string.cpp index c88f75378..e2bdf6544 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/escape_string.hpp" #include "libtorrent/aux_/max_path.hpp" // for TORRENT_MAX_PATH #include "libtorrent/string_util.hpp" // for to_string +#include "libtorrent/aux_/array.hpp" namespace libtorrent { @@ -245,7 +246,7 @@ namespace libtorrent // first, strip the file:// part. // On windows, we have // to strip the first / as well - int num_to_strip = 7; + std::size_t num_to_strip = 7; #ifdef TORRENT_WINDOWS if (url[7] == '/' || url[7] == '\\') ++num_to_strip; #endif @@ -279,8 +280,8 @@ namespace libtorrent '4', '5', '6', '7', '8', '9', '+', '/' }; - std::array inbuf; - std::array outbuf; + aux::array inbuf; + aux::array outbuf; std::string ret; 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; - static std::array input_output_mapping = {{0, 2, 4, 5, 7, 8}}; + static aux::array const input_output_mapping{{{0, 2, 4, 5, 7, 8}}}; - std::array inbuf; - std::array outbuf; + aux::array inbuf; + aux::array outbuf; std::string ret; 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::array inbuf; - std::array outbuf; + aux::array inbuf; + aux::array outbuf; std::string ret; for (std::string::const_iterator i = s.begin(); i != s.end();) @@ -400,9 +401,9 @@ namespace libtorrent { char const in = char(std::toupper(*i++)); if (in >= 'A' && in <= 'Z') - inbuf[j] = in - 'A'; + inbuf[j] = (in - 'A') & 0xff; else if (in >= '2' && in <= '7') - inbuf[j] = in - '2' + ('Z' - 'A') + 1; + inbuf[j] = (in - '2' + ('Z' - 'A') + 1) & 0xff; else if (in == '=') { inbuf[j] = 0; diff --git a/src/file.cpp b/src/file.cpp index 499a8845a..9a9f5e0dd 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -429,9 +429,9 @@ namespace libtorrent } s->file_size = ret.st_size; - s->atime = ret.st_atime; - s->mtime = ret.st_mtime; - s->ctime = ret.st_ctime; + s->atime = std::uint64_t(ret.st_atime); + s->mtime = std::uint64_t(ret.st_mtime); + s->ctime = std::uint64_t(ret.st_ctime); s->mode = (S_ISREG(ret.st_mode) ? file_status::regular_file : 0) | (S_ISDIR(ret.st_mode) ? file_status::directory : 0) @@ -708,7 +708,7 @@ namespace libtorrent ) ++p; if (p - start > 0) { - ret.append(start, p - start); + ret.append(start, aux::numeric_cast(p - start)); if (only_first_part) return ret; ret.append(1, '\0'); } @@ -731,12 +731,13 @@ namespace libtorrent { 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 - if (f[i] == '\\') break; + if (f[idx] == '\\') break; #endif - if (f[i] != '.') continue; - return f.substr(i); + if (f[idx] != '.') continue; + return f.substr(idx); } return ""; } @@ -750,21 +751,22 @@ namespace libtorrent char const* ext = std::strrchr(f.c_str(), '.'); // if we don't have an extension, just 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(ext - &f[0])); } void replace_extension(std::string& f, std::string const& ext) { 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 - if (f[i] == '\\') break; + if (f[idx] == '\\') break; #endif - if (f[i] != '.') continue; + if (f[idx] != '.') continue; - f.resize(i); + f.resize(idx); break; } f += '.'; @@ -813,10 +815,10 @@ namespace libtorrent int len = int(f.size()) - 1; // 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) { - if (f[len] == '/' || f[len] == '\\') + if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\') break; --len; } @@ -835,16 +837,16 @@ namespace libtorrent int len = int(f.size()); // 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) { --len; - if (f[len] == '/' || f[len] == '\\') + if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\') break; } - if (f[len] == '/' || f[len] == '\\') ++len; - return std::string(f.c_str(), len); + if (f[std::size_t(len)] == '/' || f[std::size_t(len)] == '\\') ++len; + return std::string(f.c_str(), std::size_t(len)); } char const* filename_cstr(char const* f) @@ -884,10 +886,10 @@ namespace libtorrent || *sep == '\\' #endif ) - return std::string(sep + 1, len); + return std::string(sep + 1, std::size_t(len)); ++len; } - return std::string(first, len); + return std::string(first, std::size_t(len)); } return std::string(sep + 1); @@ -930,12 +932,12 @@ namespace libtorrent bool const need_sep = lhs[lhs.size() - 1] != '/'; #endif 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); - target_size = std::snprintf(&ret[0], target_size, "%*s%s%*s" + target_size = aux::numeric_cast(std::snprintf(&ret[0], target_size, "%*s%s%*s" , int(lhs.size()), lhs.data() , (need_sep ? TORRENT_SEPARATOR : "") - , int(rhs.size()), rhs.data()); + , int(rhs.size()), rhs.data())); ret.resize(target_size); return ret; } @@ -1628,7 +1630,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { bool coalesce_read_buffers(span& bufs , iovec_t& tmp) { - int const buf_size = bufs_size(bufs); + std::size_t const buf_size = aux::numeric_cast(bufs_size(bufs)); char* buf = static_cast(std::malloc(buf_size)); if (!buf) return false; tmp.iov_base = buf; @@ -1647,7 +1649,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { bool coalesce_write_buffers(span& bufs , iovec_t& tmp) { - int const buf_size = bufs_size(bufs); + std::size_t const buf_size = aux::numeric_cast(bufs_size(bufs)); char* buf = static_cast(std::malloc(buf_size)); if (!buf) return false; gather_copy(bufs, buf); diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index 92dd48d01..f7571bcbf 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace libtorrent { namespace dht { @@ -176,7 +177,7 @@ namespace struct infohashes_sample { - std::vector samples; + aux::vector samples; time_point created = min_time(); int count() const { return int(samples.size()); } @@ -478,7 +479,7 @@ namespace refresh_infohashes_sample(); - std::vector const& samples = m_infohashes_sample.samples; + aux::vector const& samples = m_infohashes_sample.samples; item["samples"] = span( reinterpret_cast(samples.data()), samples.size() * 20); @@ -582,7 +583,7 @@ namespace && m_infohashes_sample.count() >= max_count) return; - std::vector& samples = m_infohashes_sample.samples; + aux::vector& samples = m_infohashes_sample.samples; samples.clear(); samples.reserve(count); diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index c8cae2abb..0151a25f0 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket_io.hpp" // for print_endpoint #include "libtorrent/invariant_check.hpp" #include "libtorrent/address.hpp" +#include "libtorrent/aux_/array.hpp" 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; - static const int size_exceptions[] = {16, 8, 4, 2}; - if (bucket < int(sizeof(size_exceptions) / sizeof(size_exceptions[0]))) + static const aux::array size_exceptions{{{16, 8, 4, 2}}}; + if (bucket < size_exceptions.end_index()) return m_bucket_size * size_exceptions[bucket]; return m_bucket_size; } @@ -692,7 +693,7 @@ ip_ok: // the data someone is looking for, make sure there is an affinity // 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; TORRENT_ASSERT_VAL(mask > 0, mask); while ((mask & 0x80) == 0) @@ -738,7 +739,7 @@ ip_ok: // have a unique prefix // find node entries with duplicate prefixes in O(1) - std::vector prefix(int(1 << (8 - mask_shift)), b.end()); + aux::vector prefix(aux::numeric_cast(int(1 << (8 - mask_shift))), b.end()); TORRENT_ASSERT(int(prefix.size()) >= bucket_size_limit); // 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_size_limit = bucket_limit(bucket_index); - l.reserve(bucket_size_limit); + l.reserve(aux::numeric_cast(bucket_size_limit)); 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) { return compare_ref(lhs.id, rhs.id, target); }); - l.resize(count); + l.resize(aux::numeric_cast(count)); return; } 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) { return compare_ref(lhs.id, rhs.id, target); }); - l.resize(count); + l.resize(aux::numeric_cast(count)); return; } unsorted_start_idx = int(l.size()); diff --git a/src/performance_counters.cpp b/src/performance_counters.cpp index 0cf6dfb9a..1b2a6d01b 100644 --- a/src/performance_counters.cpp +++ b/src/performance_counters.cpp @@ -50,7 +50,7 @@ namespace libtorrent { counters::counters(counters const& c) { #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( c.m_stats_counter[i].load(std::memory_order_relaxed) , std::memory_order_relaxed); @@ -63,7 +63,7 @@ namespace libtorrent { counters& counters::operator=(counters const& c) { #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( c.m_stats_counter[i].load(std::memory_order_relaxed) , std::memory_order_relaxed); @@ -90,7 +90,7 @@ namespace libtorrent { // the argument specifies which counter to // 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 // a monotonically increasing counter, but a gauge @@ -110,9 +110,9 @@ namespace libtorrent { #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. - 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_counters); @@ -121,21 +121,21 @@ namespace libtorrent { #ifdef ATOMIC_LLONG_LOCK_FREE 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 , std::memory_order_relaxed)) { - new_value = (current * (100-ratio) + value * ratio) / 100; + new_value = (current * (100 - ratio) + value * ratio) / 100; } #else std::lock_guard l(m_mutex); 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 } - 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 < num_counters); diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index fa5b926fe..316617807 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -35,9 +35,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/settings_pack.hpp" #include "libtorrent/aux_/session_impl.hpp" +#include "libtorrent/aux_/array.hpp" #include -#include namespace { @@ -99,8 +99,8 @@ namespace libtorrent using aux::session_impl; - std::array str_settings = - {{ + aux::array const str_settings + ({{ SET(user_agent, "libtorrent/" LIBTORRENT_VERSION, &session_impl::update_user_agent), SET(announce_ip, nullptr, nullptr), SET(mmap_cache, nullptr, nullptr), @@ -117,10 +117,10 @@ namespace libtorrent SET(i2p_hostname, "", &session_impl::update_i2p_bridge), SET(peer_fingerprint, "-LT1200-", &session_impl::update_peer_fingerprint), SET(dht_bootstrap_nodes, "dht.libtorrent.org:25401", &session_impl::update_dht_bootstrap_nodes) - }}; + }}); - std::array bool_settings = - {{ + aux::array const bool_settings + ({{ 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), SET(send_redundant_have, true, nullptr), @@ -189,10 +189,10 @@ namespace libtorrent SET(proxy_peer_connections, true, nullptr), SET(auto_sequential, true, &session_impl::update_auto_sequential), SET(proxy_tracker_connections, true, nullptr), - }}; + }}); - std::array int_settings = - {{ + aux::array const int_settings + ({{ SET(tracker_completion_timeout, 30, nullptr), SET(tracker_receive_timeout, 10, nullptr), SET(stop_tracker_timeout, 5, nullptr), @@ -321,7 +321,7 @@ namespace libtorrent SET(proxy_port, 0, &session_impl::update_proxy), SET(i2p_port, 0, &session_impl::update_i2p_bridge), SET(cache_size_volatile, 256, nullptr) - }}; + }}); #undef SET #undef SET_DEPRECATED @@ -330,20 +330,20 @@ namespace libtorrent 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; - 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; - 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; - return settings_pack::bool_type_base + int(k); + return settings_pack::bool_type_base + k; } return -1; } @@ -379,27 +379,27 @@ namespace libtorrent case bdecode_node::int_t: { 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; - 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; break; } 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; - 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; 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; - 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; @@ -416,20 +416,20 @@ namespace libtorrent 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; - if (cmp == s.m_strings[i]) continue; - sett[str_settings[i].name] = s.m_strings[i]; + if (cmp == s.m_strings[std::size_t(i)]) continue; + sett[str_settings[i].name] = s.m_strings[std::size_t(i)]; } for (int i = 0; i < settings_pack::num_int_settings; ++i) { - if (int_settings[i].default_value == s.m_ints[i]) continue; - sett[int_settings[i].name] = s.m_ints[i]; + if (int_settings[i].default_value == s.m_ints[std::size_t(i)]) continue; + sett[int_settings[i].name] = s.m_ints[std::size_t(i)]; } for (int i = 0; i < settings_pack::num_bool_settings; ++i) { - if (bool_settings[i].default_value == s.m_bools[i]) continue; - sett[bool_settings[i].name] = s.m_bools[i]; + if (bool_settings[i].default_value == s.m_bools[std::size_t(i)]) continue; + sett[bool_settings[i].name] = s.m_bools[std::size_t(i)]; } } @@ -674,27 +674,24 @@ namespace libtorrent case string_type_base: { std::pair v(name, std::string()); - std::vector >::iterator i - = std::lower_bound(m_strings.begin(), m_strings.end(), v - , &compare_first); + auto const i = std::lower_bound(m_strings.begin(), m_strings.end() + , v, &compare_first); if (i != m_strings.end() && i->first == name) m_strings.erase(i); break; } case int_type_base: { std::pair v(name, 0); - std::vector >::iterator i - = std::lower_bound(m_ints.begin(), m_ints.end(), v - , &compare_first); + auto const i = std::lower_bound(m_ints.begin(), m_ints.end() + , v, &compare_first); if (i != m_ints.end() && i->first == name) m_ints.erase(i); break; } case bool_type_base: { std::pair v(name, false); - std::vector >::iterator i - = std::lower_bound(m_bools.begin(), m_bools.end(), v - , &compare_first); + auto const i = std::lower_bound(m_bools.begin(), m_bools.end() + , v, &compare_first); if (i != m_bools.end() && i->first == name) m_bools.erase(i); break; }