fixing sign-conversion warnings, part 8, created aux::unique_ptr, refactor (#1616)

fixing sign-conversion warnings, part 8, created aux::unique_ptr, refactor
This commit is contained in:
Alden Torres 2017-01-26 08:31:20 -05:00 committed by Arvid Norberg
parent 070e85090b
commit 2bc7a60f33
11 changed files with 133 additions and 24 deletions

View File

@ -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 \

View File

@ -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 <type_traits>
#include "libtorrent/assert.hpp"
namespace libtorrent { namespace aux {
template <class T, class In, typename Cond = typename std::enable_if<
std::is_integral<T>::value && std::is_integral<In>::value>::type>
T numeric_cast(In v)
{
T r = static_cast<T>(v);
TORRENT_ASSERT(v == static_cast<In>(r));
TORRENT_ASSERT(std::is_unsigned<In>::value || std::is_signed<T>::value
|| std::int64_t(v) >= 0);
TORRENT_ASSERT(std::is_signed<In>::value || std::is_unsigned<T>::value
|| std::size_t(v) <= std::size_t((std::numeric_limits<T>::max)()));
return r;
}
}}
#endif

View File

@ -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 <memory>
#include <type_traits>
#include "libtorrent/units.hpp"
#include "libtorrent/assert.hpp"
namespace libtorrent { namespace aux {
template <typename T, typename IndexType = int>
struct unique_ptr;
template <typename T, typename IndexType>
struct unique_ptr<T[], IndexType> : std::unique_ptr<T[]>
{
using base = std::unique_ptr<T[]>;
using underlying_index = typename underlying_index_t<IndexType>::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<underlying_index>(idx)));
}
};
}}
#endif

View File

@ -113,20 +113,6 @@ namespace libtorrent { namespace aux {
}
};
// TODO: find a better place for this function
template <class T, class In, typename Cond = typename std::enable_if<
std::is_integral<T>::value && std::is_integral<In>::value>::type>
T numeric_cast(In v)
{
T r = static_cast<T>(v);
TORRENT_ASSERT(v == static_cast<In>(r));
TORRENT_ASSERT(std::is_unsigned<In>::value || std::is_signed<T>::value
|| std::int64_t(v) >= 0);
TORRENT_ASSERT(std::is_signed<In>::value || std::is_unsigned<T>::value
|| std::size_t(v) <= std::size_t((std::numeric_limits<T>::max)()));
return r;
}
}}
#endif

View File

@ -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<cached_block_entry[]> blocks;
aux::unique_ptr<cached_block_entry[]> blocks;
// the last time a block was written to this piece
// plus the minimum amount of time the block is guaranteed

View File

@ -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;

View File

@ -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 <cstdio> // for vsnprintf
#include <cstring>

View File

@ -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 <limits>
#include <cstring> // for memset
#include <cstdio> // for snprintf

View File

@ -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<int>(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;

View File

@ -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;
}

View File

@ -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;