diff --git a/include/libtorrent/aux_/unique_ptr.hpp b/include/libtorrent/aux_/unique_ptr.hpp index e68960f63..438b751ed 100644 --- a/include/libtorrent/aux_/unique_ptr.hpp +++ b/include/libtorrent/aux_/unique_ptr.hpp @@ -50,6 +50,9 @@ namespace libtorrent { namespace aux { using base = std::unique_ptr; using underlying_index = typename underlying_index_t::type; + unique_ptr() {} + explicit unique_ptr(T arr[]) : base(arr) {} + auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index())) { TORRENT_ASSERT(idx >= IndexType(0)); diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index 387a79a79..2c5a36232 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -35,12 +35,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/config.hpp" +#include "libtorrent/aux_/unique_ptr.hpp" #include "libtorrent/aux_/byteswap.hpp" #include "libtorrent/aux_/ffs.hpp" #include // for memset and memcpy #include // uint32_t -#include // for unique_ptr namespace libtorrent { @@ -253,7 +253,7 @@ namespace libtorrent // the first element is not part of the bitfield, it's the // number of bits. - std::unique_ptr m_buf; + aux::unique_ptr m_buf; }; template diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index bcaf0ec98..42564d803 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -142,7 +142,7 @@ namespace libtorrent #endif { #ifndef TORRENT_NO_DEPRECATE - memset(num_fence_jobs, 0, sizeof(num_fence_jobs)); + std::memset(num_fence_jobs, 0, sizeof(num_fence_jobs)); #endif } diff --git a/include/libtorrent/units.hpp b/include/libtorrent/units.hpp index 63fc435b8..d9a0fc2e6 100644 --- a/include/libtorrent/units.hpp +++ b/include/libtorrent/units.hpp @@ -141,11 +141,11 @@ namespace std { using type = libtorrent::aux::strong_typedef; public: - static constexpr type min() - { return type(std::numeric_limits::min()); } + static constexpr type (min)() + { return type((std::numeric_limits::min)()); } - static constexpr type max() - { return type(std::numeric_limits::max()); } + static constexpr type (max)() + { return type((std::numeric_limits::max)()); } }; template diff --git a/src/bitfield.cpp b/src/bitfield.cpp index 5bc5f34de..2a537d046 100644 --- a/src/bitfield.cpp +++ b/src/bitfield.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/bitfield.hpp" +#include "libtorrent/aux_/numeric_cast.hpp" #include "libtorrent/aux_/cpuid.hpp" #ifdef _MSC_VER @@ -112,7 +113,7 @@ namespace libtorrent // from: // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers - static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; + static const std::uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; std::uint32_t c = v - ((v >> 1) & B[0]); c = ((c >> S[1]) & B[1]) + (c & B[1]); @@ -169,22 +170,23 @@ namespace libtorrent int const cur_size_words = num_words(); if (cur_size_words != new_size_words) { - std::unique_ptr b(new std::uint32_t[new_size_words + 1]); + aux::unique_ptr b(new std::uint32_t[new_size_words + 1]); #ifdef BOOST_NO_EXCEPTIONS if (b == nullptr) std::terminate(); #endif - b[0] = bits; - if (m_buf) std::memcpy(&b[1], buf(), std::min(new_size_words, cur_size_words) * 4); + b[0] = aux::numeric_cast(bits); + if (m_buf) std::memcpy(&b[1], buf() + , aux::numeric_cast(std::min(new_size_words, cur_size_words) * 4)); if (new_size_words > cur_size_words) { std::memset(&b[1 + cur_size_words], 0 - , (new_size_words - cur_size_words) * 4); + , aux::numeric_cast((new_size_words - cur_size_words) * 4)); } m_buf = std::move(b); } else { - m_buf[0] = bits; + m_buf[0] = aux::numeric_cast(bits); } clear_trailing_bits(); @@ -193,22 +195,22 @@ namespace libtorrent int bitfield::find_first_set() const { - std::size_t const num = num_words(); + int const num = num_words(); if (num == 0) return -1; - int const count = aux::count_leading_zeros({&m_buf[1], num}); - return count != int(num) * 32 ? count : -1; + int const count = aux::count_leading_zeros({&m_buf[1], std::size_t(num)}); + return count != num * 32 ? count : -1; } int bitfield::find_last_clear() const { - std::size_t const num = num_words(); + int const num = num_words(); if (num == 0) return - 1; int const size = this->size(); std::uint32_t const mask = 0xffffffff << (32 - (size & 31)); std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask); int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32)); return last != 0 - ? (int(num) - 1) * 32 + ext - : size - (aux::count_trailing_ones({&m_buf[1], num - 1}) + ext); + ? (num - 1) * 32 + ext + : size - (aux::count_trailing_ones({&m_buf[1], std::size_t(num - 1)}) + ext); } } diff --git a/src/bloom_filter.cpp b/src/bloom_filter.cpp index bcc896687..21da0f0c0 100644 --- a/src/bloom_filter.cpp +++ b/src/bloom_filter.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/bloom_filter.hpp" +#include "libtorrent/aux_/numeric_cast.hpp" namespace libtorrent { @@ -38,8 +39,8 @@ namespace libtorrent { std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8); std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8); - idx1 %= len * 8; - idx2 %= len * 8; + idx1 %= aux::numeric_cast(len * 8); + idx2 %= aux::numeric_cast(len * 8); return (bits[idx1 / 8] & (1 << (idx1 & 7))) != 0 && (bits[idx2 / 8] & (1 << (idx2 & 7))) != 0; } @@ -48,8 +49,8 @@ namespace libtorrent { std::uint32_t idx1 = std::uint32_t(k[0]) | (std::uint32_t(k[1]) << 8); std::uint32_t idx2 = std::uint32_t(k[2]) | (std::uint32_t(k[3]) << 8); - idx1 %= len * 8; - idx2 %= len * 8; + idx1 %= aux::numeric_cast(len * 8); + idx2 %= aux::numeric_cast(len * 8); bits[idx1 / 8] |= (1 << (idx1 & 7)); bits[idx2 / 8] |= (1 << (idx2 & 7)); }