some refactor and cleanup (#1065)

This commit is contained in:
Alden Torres 2016-09-05 22:25:20 -04:00 committed by Arvid Norberg
parent 994a84cfe8
commit b313de3925
16 changed files with 48 additions and 82 deletions

View File

@ -33,13 +33,10 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_ALLOCATING_HANDLER_HPP_INCLUDED
#define TORRENT_ALLOCATING_HANDLER_HPP_INCLUDED
#include <boost/config.hpp>
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/aligned_storage.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
namespace libtorrent { namespace aux
@ -125,4 +122,3 @@ namespace libtorrent { namespace aux
}
#endif

View File

@ -34,9 +34,8 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_BANDWIDTH_MANAGER_HPP_INCLUDED
#include <memory>
#include <vector>
#include "libtorrent/socket.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/bandwidth_limit.hpp"
@ -76,8 +75,7 @@ struct TORRENT_EXTRA_EXPORT bandwidth_manager
private:
// these are the consumers that want bandwidth
typedef std::vector<bw_request> queue_t;
queue_t m_queue;
std::vector<bw_request> m_queue;
// the number of bytes all the requests in queue are for
std::int64_t m_queued_bytes;
@ -91,4 +89,3 @@ private:
}
#endif

View File

@ -63,7 +63,7 @@ struct TORRENT_EXTRA_EXPORT bw_request
// from the most limiting one
int assign_bandwidth();
enum { max_bandwidth_channels = 10 };
constexpr static int max_bandwidth_channels = 10;
// we don't actually support more than 10 channels per peer
bandwidth_channel* channel[max_bandwidth_channels];
};
@ -71,4 +71,3 @@ struct TORRENT_EXTRA_EXPORT bw_request
}
#endif

View File

@ -30,10 +30,11 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED
#define TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED
#include "libtorrent/export.hpp"
namespace libtorrent
{
struct TORRENT_EXTRA_EXPORT bandwidth_socket
@ -45,4 +46,3 @@ namespace libtorrent
}
#endif // TORRENT_BANDWIDTH_SOCKET_HPP_INCLUDED

View File

@ -355,7 +355,7 @@ namespace libtorrent
void reclaim_block(block_cache_reference const& ref);
// returns a range of all pieces. This migh be a very
// returns a range of all pieces. This might be a very
// long list, use carefully
std::pair<iterator, iterator> all_pieces() const;
int num_pieces() const { return int(m_pieces.size()); }
@ -536,7 +536,7 @@ namespace libtorrent
int m_pinned_blocks;
#if TORRENT_USE_ASSERTS
std::vector<std::pair<std::string, void const*> > m_deleted_storages;
std::vector<std::pair<std::string, void const*>> m_deleted_storages;
#endif
};

View File

@ -38,7 +38,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/address.hpp"
namespace libtorrent { namespace dht
@ -77,4 +76,3 @@ node_id TORRENT_EXTRA_EXPORT generate_prefix_mask(int bits);
} } // namespace libtorrent::dht
#endif // NODE_ID_HPP

View File

@ -93,4 +93,3 @@ private:
}
#endif

View File

@ -1306,4 +1306,3 @@ namespace libtorrent
}
#endif // TORRENT_TORRENT_HANDLE_HPP_INCLUDED

View File

@ -31,7 +31,10 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/bandwidth_manager.hpp"
#include "libtorrent/time.hpp"
#if TORRENT_USE_ASSERTS
#include <climits>
#endif
namespace libtorrent
{
@ -47,7 +50,7 @@ namespace libtorrent
{
m_abort = true;
queue_t tm;
std::vector<bw_request> tm;
tm.swap(m_queue);
m_queued_bytes = 0;
@ -62,10 +65,9 @@ namespace libtorrent
#if TORRENT_USE_ASSERTS
bool bandwidth_manager::is_queued(bandwidth_socket const* peer) const
{
for (queue_t::const_iterator i = m_queue.begin()
, end(m_queue.end()); i != end; ++i)
for (auto const& r : m_queue)
{
if (i->peer.get() == peer) return true;
if (r.peer.get() == peer) return true;
}
return false;
}
@ -125,10 +127,9 @@ namespace libtorrent
void bandwidth_manager::check_invariant() const
{
std::int64_t queued = 0;
for (queue_t::const_iterator i = m_queue.begin()
, end(m_queue.end()); i != end; ++i)
for (auto const& r : m_queue)
{
queued += i->request_size - i->assigned;
queued += r.request_size - r.assigned;
}
TORRENT_ASSERT(queued == m_queued_bytes);
}
@ -148,10 +149,9 @@ namespace libtorrent
std::vector<bandwidth_channel*> channels;
queue_t tm;
std::vector<bw_request> tm;
for (queue_t::iterator i = m_queue.begin();
i != m_queue.end();)
for (auto i = m_queue.begin(); i != m_queue.end();)
{
if (i->peer->is_disconnecting())
{
@ -178,26 +178,23 @@ namespace libtorrent
++i;
}
for (queue_t::iterator i = m_queue.begin()
, end(m_queue.end()); i != end; ++i)
for (auto const& r : m_queue)
{
for (int j = 0; j < bw_request::max_bandwidth_channels && i->channel[j]; ++j)
for (int j = 0; j < bw_request::max_bandwidth_channels && r.channel[j]; ++j)
{
bandwidth_channel* bwc = i->channel[j];
bandwidth_channel* bwc = r.channel[j];
if (bwc->tmp == 0) channels.push_back(bwc);
TORRENT_ASSERT(INT_MAX - bwc->tmp > i->priority);
bwc->tmp += i->priority;
TORRENT_ASSERT(INT_MAX - bwc->tmp > r.priority);
bwc->tmp += r.priority;
}
}
for (std::vector<bandwidth_channel*>::iterator i = channels.begin()
, end(channels.end()); i != end; ++i)
for (auto const& ch : channels)
{
(*i)->update_quota(int(dt_milliseconds));
ch->update_quota(int(dt_milliseconds));
}
for (queue_t::iterator i = m_queue.begin();
i != m_queue.end();)
for (auto i = m_queue.begin(); i != m_queue.end();)
{
int a = i->assign_bandwidth();
if (i->assigned == i->request_size
@ -223,4 +220,3 @@ namespace libtorrent
}
}
}

View File

@ -1222,10 +1222,9 @@ void block_cache::clear(tailqueue<disk_io_job>& jobs)
// at the end
std::vector<char*> bufs;
for (iterator p = m_pieces.begin()
, end(m_pieces.end()); p != end; ++p)
for (auto const& p : m_pieces)
{
cached_piece_entry& pe = const_cast<cached_piece_entry&>(*p);
cached_piece_entry& pe = const_cast<cached_piece_entry&>(p);
#if TORRENT_USE_ASSERTS
for (tailqueue_iterator<disk_io_job> i = pe.jobs.iterate(); i.get(); i.next())
TORRENT_PIECE_ASSERT((static_cast<disk_io_job const*>(i.get()))->piece == pe.piece, &pe);
@ -1664,9 +1663,8 @@ void block_cache::check_invariant() const
}
boost::unordered_set<char*> buffers;
for (iterator i = m_pieces.begin(), end(m_pieces.end()); i != end; ++i)
for (auto const& p :m_pieces)
{
cached_piece_entry const& p = *i;
TORRENT_PIECE_ASSERT(p.blocks, &p);
TORRENT_PIECE_ASSERT(p.storage, &p);
@ -1887,7 +1885,7 @@ cached_piece_entry* block_cache::find_piece(piece_manager* st, int piece)
cached_piece_entry model;
model.storage = st->shared_from_this();
model.piece = piece;
iterator i = m_pieces.find(model);
auto i = m_pieces.find(model);
TORRENT_ASSERT(i == m_pieces.end() || (i->storage.get() == st && i->piece == piece));
if (i == m_pieces.end()) return nullptr;
TORRENT_PIECE_ASSERT(i->in_use, &*i);
@ -1904,4 +1902,3 @@ cached_piece_entry* block_cache::find_piece(piece_manager* st, int piece)
}
}

View File

@ -180,8 +180,7 @@ namespace libtorrent
#if TORRENT_USE_ASSERTS
// by now, all pieces should have been evicted
std::pair<block_cache::iterator, block_cache::iterator> pieces
= m_disk_cache.all_pieces();
auto pieces = m_disk_cache.all_pieces();
TORRENT_ASSERT(pieces.first == pieces.second);
#endif
@ -837,7 +836,7 @@ namespace libtorrent
}
else
{
std::pair<block_cache::iterator, block_cache::iterator> range = m_disk_cache.all_pieces();
auto range = m_disk_cache.all_pieces();
while (range.first != range.second)
{
// TODO: it would be nice to optimize this by having the cache
@ -1635,8 +1634,8 @@ namespace libtorrent
#if TORRENT_USE_ASSERTS && defined TORRENT_EXPENSIVE_INVARIANT_CHECKS
std::unique_lock<std::mutex> l2_(m_cache_mutex);
std::pair<block_cache::iterator, block_cache::iterator> range = m_disk_cache.all_pieces();
for (block_cache::iterator i = range.first; i != range.second; ++i)
auto range = m_disk_cache.all_pieces();
for (auto i = range.first; i != range.second; ++i)
{
cached_piece_entry const& p = *i;
int bs = m_disk_cache.block_size();
@ -2784,10 +2783,8 @@ namespace libtorrent
{
ret->pieces.reserve(m_disk_cache.num_pieces());
std::pair<block_cache::iterator, block_cache::iterator> range
= m_disk_cache.all_pieces();
for (block_cache::iterator i = range.first; i != range.second; ++i)
auto range = m_disk_cache.all_pieces();
for (auto i = range.first; i != range.second; ++i)
{
if (i->cache_state == cached_piece_entry::read_lru2_ghost
|| i->cache_state == cached_piece_entry::read_lru1_ghost)
@ -3308,8 +3305,7 @@ namespace libtorrent
#if TORRENT_USE_ASSERTS
// by now, all pieces should have been evicted
std::pair<block_cache::iterator, block_cache::iterator> pieces
= m_disk_cache.all_pieces();
auto pieces = m_disk_cache.all_pieces();
TORRENT_ASSERT(pieces.first == pieces.second);
#endif

View File

@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/kademlia/node_entry.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/broadcast_socket.hpp" // for is_local et.al
#include "libtorrent/socket_io.hpp" // for hash_address
#include "libtorrent/random.hpp" // for random
#include "libtorrent/hasher.hpp" // for hasher
#include "libtorrent/crc32c.hpp" // for crc32c
@ -153,13 +152,14 @@ void make_id_secret(node_id& in)
hasher h(reinterpret_cast<char const*>(&secret), 4);
h.update(reinterpret_cast<char const*>(&rand), 4);
sha1_hash const secret_hash = h.final();
memcpy(&in[20-4], &secret_hash[0], 4);
memcpy(&in[20-8], &rand, 4);
std::memcpy(&in[20 - 4], &secret_hash[0], 4);
std::memcpy(&in[20 - 8], &rand, 4);
}
node_id generate_random_id()
{
char r[20];
// TODO: use here aux::random_bytes?
for (int i = 0; i < 20; ++i) r[i] = random(0xff);
return hasher(r, 20).final();
}
@ -176,9 +176,9 @@ bool verify_secret_id(node_id const& nid)
if (secret == 0) return false;
hasher h(reinterpret_cast<char*>(&secret), 4);
h.update(reinterpret_cast<char const*>(&nid[20-8]), 4);
h.update(reinterpret_cast<char const*>(&nid[20 - 8]), 4);
sha1_hash secret_hash = h.final();
return memcmp(&nid[20-4], &secret_hash[0], 4) == 0;
return std::memcmp(&nid[20 - 4], &secret_hash[0], 4) == 0;
}
// verifies whether a node-id matches the IP it's used from
@ -211,10 +211,9 @@ node_id generate_prefix_mask(int bits)
TORRENT_ASSERT(bits <= 160);
node_id mask(nullptr);
int b = 0;
for (; b < bits - 7; b += 8) mask[b/8] |= 0xff;
if (bits < 160) mask[b/8] |= (0xff << (8 - (bits&7))) & 0xff;
for (; b < bits - 7; b += 8) mask[b / 8] |= 0xff;
if (bits < 160) mask[b / 8] |= (0xff << (8 - (bits & 7))) & 0xff;
return mask;
}
} } // namespace libtorrent::dht

View File

@ -49,7 +49,7 @@ resolve_links::resolve_links(std::shared_ptr<torrent_info> ti)
for (int i = 0; i < fs.num_files(); ++i)
{
// don't match pad-files, and don't match files that aren't aligned to
// ieces. Files are matched by comparing piece hashes, so pieces must
// pieces. Files are matched by comparing piece hashes, so pieces must
// be aligned and the same size
if (fs.pad_file_at(i)) continue;
if ((fs.file_offset(i) % piece_size) != 0) continue;
@ -84,8 +84,7 @@ void resolve_links::match(std::shared_ptr<const torrent_info> const& ti
std::int64_t file_size = fs.file_size(i);
typedef boost::unordered_multimap<std::int64_t, int>::iterator iterator;
iterator iter = m_file_sizes.find(file_size);
auto iter = m_file_sizes.find(file_size);
// we don't have a file whose size matches, look at the next one
if (iter == m_file_sizes.end()) continue;

View File

@ -4590,7 +4590,7 @@ namespace aux {
add_torrent_params params = p;
std::shared_ptr<torrent> torrent_ptr;
bool added;
boost::tie(torrent_ptr, added) = add_torrent_impl(params, ec);
std::tie(torrent_ptr, added) = add_torrent_impl(params, ec);
torrent_handle const handle(torrent_ptr);
m_alerts.emplace_alert<add_torrent_alert>(handle, params, ec);

View File

@ -740,4 +740,3 @@ namespace libtorrent
}
}

View File

@ -32,12 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <functional>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <vector>
#include <cstdlib>
#include <cstdio> // for snprintf
@ -61,10 +56,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
enum
{
request_size_overhead = 5000
};
constexpr int request_size_overhead = 5000;
struct disk_interface;