more use of span and std::shared_ptr (#1018)

more use of span and std::shared_ptr
This commit is contained in:
Alden Torres 2016-08-21 13:37:11 -04:00 committed by Arvid Norberg
parent df5f2cb31a
commit 00510d2b06
7 changed files with 23 additions and 37 deletions

View File

@ -35,15 +35,13 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/high_resolution_timer.hpp>
#if defined TORRENT_BUILD_SIMULATOR
#include "simulator/simulator.hpp"
#endif
#else
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/high_resolution_timer.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif // SIMULATOR
namespace libtorrent
{
@ -55,4 +53,3 @@ namespace libtorrent
}
#endif // TORRENT_DEADLINE_TIMER_HPP_INCLUDED

View File

@ -39,10 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <string>
#include <cstdint>
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/smart_ptr.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <memory>
namespace libtorrent
{
@ -85,7 +82,7 @@ namespace libtorrent
int download_priority;
};
struct TORRENT_EXTRA_EXPORT peer_class : boost::enable_shared_from_this<peer_class>
struct TORRENT_EXTRA_EXPORT peer_class : std::enable_shared_from_this<peer_class>
{
friend struct peer_class_pool;
@ -137,7 +134,7 @@ namespace libtorrent
// state for peer classes (a peer can belong to multiple classes)
// this can control
std::vector<boost::shared_ptr<peer_class> > m_peer_classes;
std::vector<std::shared_ptr<peer_class>> m_peer_classes;
// indices in m_peer_classes that are no longer used
std::vector<peer_class_t> m_free_list;
@ -145,4 +142,3 @@ namespace libtorrent
}
#endif // TORRENT_PEER_CLASS_HPP_INCLUDED

View File

@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/buffer.hpp>
#include <libtorrent/disk_buffer_holder.hpp>
#include <libtorrent/sliding_average.hpp>
#include <boost/asio/buffer.hpp>
namespace libtorrent {
@ -59,7 +58,7 @@ struct TORRENT_EXTRA_EXPORT receive_buffer
int capacity() const { return int(m_recv_buffer.size()); }
int watermark() const { return m_watermark.mean(); }
boost::asio::mutable_buffer reserve(int size);
span<char> reserve(int size);
void grow(int limit);
// tell the buffer we just received more bytes at the end of it. This will

View File

@ -85,11 +85,11 @@ namespace libtorrent
{
TORRENT_ASSERT(m_peer_classes.size() < 0x10000);
ret = peer_class_t(m_peer_classes.size());
m_peer_classes.push_back(boost::shared_ptr<peer_class>());
m_peer_classes.push_back(std::shared_ptr<peer_class>());
}
TORRENT_ASSERT(m_peer_classes[ret].get() == nullptr);
m_peer_classes[ret] = boost::make_shared<peer_class>(label);
m_peer_classes[ret] = std::make_shared<peer_class>(label);
return ret;
}
@ -123,6 +123,4 @@ namespace libtorrent
if (c >= m_peer_classes.size()) return nullptr;
return m_peer_classes[c].get();
}
}

View File

@ -36,14 +36,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/peer_connection.hpp"
#include "libtorrent/identify_client.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/io.hpp"
#include "libtorrent/file.hpp"
#include "libtorrent/version.hpp"
#include "libtorrent/extensions.hpp"
#include "libtorrent/aux_/session_interface.hpp"
#include "libtorrent/peer_list.hpp"
@ -5652,7 +5650,7 @@ namespace libtorrent
if (max_receive == 0) return;
boost::asio::mutable_buffer const vec = m_recv_buffer.reserve(max_receive);
span<char> const vec = m_recv_buffer.reserve(max_receive);
TORRENT_ASSERT((m_channel_state[download_channel] & peer_info::bw_network) == 0);
m_channel_state[download_channel] |= peer_info::bw_network;
#ifndef TORRENT_DISABLE_LOGGING
@ -5663,7 +5661,7 @@ namespace libtorrent
// utp sockets aren't thread safe...
ADD_OUTSTANDING_ASYNC("peer_connection::on_receive_data");
m_socket->async_read_some(
boost::asio::mutable_buffers_1(vec), make_read_handler(
boost::asio::mutable_buffers_1(vec.data(), vec.size()), make_read_handler(
std::bind(&peer_connection::on_receive_data, self(), _1, _2)));
}
@ -5859,8 +5857,9 @@ namespace libtorrent
if (buffer_size > quota_left) buffer_size = quota_left;
if (buffer_size > 0)
{
boost::asio::mutable_buffer const vec = m_recv_buffer.reserve(buffer_size);
size_t bytes = m_socket->read_some(boost::asio::mutable_buffers_1(vec), ec);
span<char> const vec = m_recv_buffer.reserve(buffer_size);
size_t bytes = m_socket->read_some(
boost::asio::mutable_buffers_1(vec.data(), vec.size()), ec);
// this is weird. You would imagine read_some() would do this
if (bytes == 0 && !ec) ec = boost::asio::error::eof;

View File

@ -40,7 +40,7 @@ int receive_buffer::max_receive() const
return int(m_recv_buffer.size() - m_recv_end);
}
boost::asio::mutable_buffer receive_buffer::reserve(int size)
span<char> receive_buffer::reserve(int size)
{
INVARIANT_CHECK;
TORRENT_ASSERT(size > 0);
@ -61,7 +61,7 @@ boost::asio::mutable_buffer receive_buffer::reserve(int size)
m_watermark = sliding_average<20>();
}
return boost::asio::buffer(&m_recv_buffer[0] + m_recv_end, size);
return span<char>(m_recv_buffer).subspan(m_recv_end, size);
}
void receive_buffer::grow(int const limit)

View File

@ -55,8 +55,7 @@ TORRENT_TEST(recv_buffer_pos_at_end_false)
b.cut(0, 1000);
// allocate some space to receive into
boost::asio::mutable_buffer vec
= b.reserve(1000);
b.reserve(1000);
b.received(1000);
b.advance_pos(999);
@ -69,7 +68,7 @@ TORRENT_TEST(recv_buffer_pos_at_end_true)
receive_buffer b;
b.cut(0, 1000);
b.reserve(1000);
boost::asio::mutable_buffer vec = b.reserve(1000);
b.reserve(1000);
b.received(1000);
b.advance_pos(1000);
TEST_EQUAL(b.pos_at_end(), true);
@ -81,7 +80,7 @@ TORRENT_TEST(recv_buffer_packet_finished)
// packet_size = 10
b.cut(0, 10);
b.reserve(1000);
boost::asio::mutable_buffer vec = b.reserve(1000);
b.reserve(1000);
b.received(1000);
for (int i = 0; i < 10; ++i)
@ -160,12 +159,10 @@ TORRENT_TEST(recv_buffer_reserve)
auto range2 = b.reserve(50);
using namespace boost::asio;
TEST_EQUAL(b.capacity(), capacity);
TEST_EQUAL(buffer_cast<char*>(range1) + 20, buffer_cast<char*>(range2));
TEST_CHECK(buffer_size(range1) >= 20);
TEST_CHECK(buffer_size(range2) >= 50);
TEST_EQUAL(range1.begin() + 20, range2.begin());
TEST_CHECK(range1.size() >= 20);
TEST_CHECK(range2.size() >= 50);
}
TORRENT_TEST(receive_buffer_normalize)