diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 696fcead7..414f55d6d 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -194,6 +194,8 @@ nobase_include_HEADERS = \ aux_/win_util.hpp \ aux_/non_owning_handle.hpp \ aux_/storage_utils.hpp \ + aux_/numeric_cast.hpp \ + aux_/unique_ptr.hpp \ \ extensions/smart_ban.hpp \ extensions/ut_metadata.hpp \ diff --git a/include/libtorrent/aux_/numeric_cast.hpp b/include/libtorrent/aux_/numeric_cast.hpp new file mode 100644 index 000000000..bb367e25a --- /dev/null +++ b/include/libtorrent/aux_/numeric_cast.hpp @@ -0,0 +1,57 @@ +/* + +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_NUMERIC_CAST_HPP +#define TORRENT_NUMERIC_CAST_HPP + +#include + +#include "libtorrent/assert.hpp" + +namespace libtorrent { namespace aux { + + template ::value && std::is_integral::value>::type> + T numeric_cast(In v) + { + T r = static_cast(v); + TORRENT_ASSERT(v == static_cast(r)); + TORRENT_ASSERT(std::is_unsigned::value || std::is_signed::value + || std::int64_t(v) >= 0); + TORRENT_ASSERT(std::is_signed::value || std::is_unsigned::value + || std::size_t(v) <= std::size_t((std::numeric_limits::max)())); + return r; + } + +}} + +#endif diff --git a/include/libtorrent/aux_/unique_ptr.hpp b/include/libtorrent/aux_/unique_ptr.hpp new file mode 100644 index 000000000..e68960f63 --- /dev/null +++ b/include/libtorrent/aux_/unique_ptr.hpp @@ -0,0 +1,62 @@ +/* + +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_UNIQUE_PTR_HPP +#define TORRENT_UNIQUE_PTR_HPP + +#include +#include + +#include "libtorrent/units.hpp" +#include "libtorrent/assert.hpp" + +namespace libtorrent { namespace aux { + + template + struct unique_ptr; + + template + struct unique_ptr : std::unique_ptr + { + using base = std::unique_ptr; + using underlying_index = typename underlying_index_t::type; + + auto operator[](IndexType idx) const -> decltype(this->base::operator[](underlying_index())) + { + TORRENT_ASSERT(idx >= IndexType(0)); + return this->base::operator[](std::size_t(static_cast(idx))); + } + }; + +}} + +#endif diff --git a/include/libtorrent/aux_/vector.hpp b/include/libtorrent/aux_/vector.hpp index 6d706faa4..5dbd81116 100644 --- a/include/libtorrent/aux_/vector.hpp +++ b/include/libtorrent/aux_/vector.hpp @@ -113,20 +113,6 @@ namespace libtorrent { namespace aux { } }; - // TODO: find a better place for this function - template ::value && std::is_integral::value>::type> - T numeric_cast(In v) - { - T r = static_cast(v); - TORRENT_ASSERT(v == static_cast(r)); - TORRENT_ASSERT(std::is_unsigned::value || std::is_signed::value - || std::int64_t(v) >= 0); - TORRENT_ASSERT(std::is_signed::value || std::is_unsigned::value - || std::size_t(v) <= std::size_t((std::numeric_limits::max)())); - return r; - } - }} #endif diff --git a/include/libtorrent/block_cache.hpp b/include/libtorrent/block_cache.hpp index fe9f0b084..ea0a5caa6 100644 --- a/include/libtorrent/block_cache.hpp +++ b/include/libtorrent/block_cache.hpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_buffer_pool.hpp" #include "libtorrent/file.hpp" // for iovec_t #include "libtorrent/disk_io_job.hpp" +#include "libtorrent/aux_/unique_ptr.hpp" #if TORRENT_USE_ASSERTS #include "libtorrent/aux_/vector.hpp" #endif @@ -206,7 +207,7 @@ namespace libtorrent // the pointers to the block data. If this is a ghost // cache entry, there won't be any data here - std::unique_ptr blocks; + aux::unique_ptr blocks; // the last time a block was written to this piece // plus the minimum amount of time the block is guaranteed diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index e75677d5f..596d19265 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -200,7 +200,7 @@ namespace libtorrent // job is still holding a reference to. The end of // the range of blocks a hash jobs holds references // to is always the last block in the piece. - std::uint32_t offset; + std::int32_t offset; // number of bytes 'buffer' points to. Used for read & write std::uint16_t buffer_size; diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index 13ee27156..7771ca45b 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/span.hpp" #include "libtorrent/string_view.hpp" #include "libtorrent/aux_/vector.hpp" +#include "libtorrent/aux_/numeric_cast.hpp" #include // for vsnprintf #include diff --git a/src/bdecode.cpp b/src/bdecode.cpp index d63679952..607c9d858 100644 --- a/src/bdecode.cpp +++ b/src/bdecode.cpp @@ -32,7 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bdecode.hpp" #include "libtorrent/alloca.hpp" -#include "libtorrent/aux_/vector.hpp" // for numeric_cast +#include "libtorrent/aux_/numeric_cast.hpp" #include #include // for memset #include // for snprintf diff --git a/src/block_cache.cpp b/src/block_cache.cpp index aee881d61..0f088df57 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -1248,7 +1248,7 @@ void block_cache::move_to_ghost(cached_piece_entry* pe) int block_cache::pad_job(disk_io_job const* j, int blocks_in_piece , int read_ahead) const { - int block_offset = j->d.io.offset & (block_size()-1); + int block_offset = j->d.io.offset & (block_size() - 1); int start = j->d.io.offset / block_size(); int end = block_offset > 0 && (read_ahead > block_size() - block_offset) ? start + 2 : start + 1; @@ -1707,7 +1707,7 @@ int block_cache::copy_from_piece(cached_piece_entry* const pe // copy from the cache and update the last use timestamp int block = j->d.io.offset / block_size(); - int block_offset = j->d.io.offset & (block_size()-1); + int block_offset = j->d.io.offset & (block_size() - 1); int buffer_offset = 0; int size = j->d.io.buffer_size; int const blocks_to_read = block_offset > 0 && (size > block_size() - block_offset) ? 2 : 1; @@ -1747,7 +1747,7 @@ int block_cache::copy_from_piece(cached_piece_entry* const pe int const blocks_per_piece = (j->storage->files()->piece_length() + block_size() - 1) / block_size(); j->d.io.ref.storage = j->storage->storage_index(); j->d.io.ref.cookie = static_cast(pe->piece) * blocks_per_piece + start_block; - j->buffer.disk_block = bl.buf + (j->d.io.offset & (block_size()-1)); + j->buffer.disk_block = bl.buf + (j->d.io.offset & (block_size() - 1)); j->storage->inc_refcount(); ++m_send_buffer_blocks; diff --git a/src/disk_io_job.cpp b/src/disk_io_job.cpp index 57868172e..a355968c0 100644 --- a/src/disk_io_job.cpp +++ b/src/disk_io_job.cpp @@ -123,14 +123,14 @@ namespace libtorrent { if (action != write) return false; - int block_offset = int(d.io.offset) & (block_size - 1); + int block_offset = d.io.offset & (block_size - 1); int size = d.io.buffer_size; - int start = int(d.io.offset) / block_size; + int start = 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) { - cached_block_entry const& b = pe->blocks[std::size_t(i)]; + cached_block_entry const& b = pe->blocks[i]; if (b.dirty || b.pending) return false; } diff --git a/src/lsd.cpp b/src/lsd.cpp index 09184250c..061266576 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -42,7 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket_io.hpp" // for print_address #include "libtorrent/debug.hpp" #include "libtorrent/hex.hpp" // to_hex, from_hex -#include "libtorrent/aux_/vector.hpp" // for numeric_cast +#include "libtorrent/aux_/numeric_cast.hpp" using namespace std::placeholders;