From 6ba64a1bb3575e0499c85a9119d147ac83969474 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 27 Dec 2017 12:47:38 +0100 Subject: [PATCH] reserve space for m_peers_to_disconnect up front, in order to make queing up a peer for disconnection noexcept --- src/torrent.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/torrent.cpp b/src/torrent.cpp index 420b29af6..1c2fbcdda 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5413,6 +5413,8 @@ namespace libtorrent { if (p->associated_torrent().lock().get() == this) { std::weak_ptr weak_t = shared_from_this(); + TORRENT_ASSERT_VAL(m_peers_to_disconnect.capacity() > m_peers_to_disconnect.size() + , m_peers_to_disconnect.capacity()); m_peers_to_disconnect.push_back(p); m_deferred_disconnect.post(m_ses.get_io_service(), aux::make_handler([=]() { @@ -5492,9 +5494,10 @@ namespace libtorrent { TORRENT_ASSERT(is_single_thread()); INVARIANT_CHECK; - std::vector> peers; - m_peers_to_disconnect.swap(peers); - for (auto const& p : peers) +#if TORRENT_USE_ASSERTS + auto const num = m_peers_to_disconnect.size(); +#endif + for (auto const& p : m_peers_to_disconnect) { TORRENT_ASSERT(p); TORRENT_ASSERT(p->associated_torrent().lock().get() == this); @@ -5502,6 +5505,8 @@ namespace libtorrent { remove_connection(p.get()); m_ses.close_connection(p.get()); } + TORRENT_ASSERT_VAL(m_peers_to_disconnect.size() == num, m_peers_to_disconnect.size() - num); + m_peers_to_disconnect.clear(); if (m_graceful_pause_mode && m_connections.empty()) { @@ -5992,6 +5997,12 @@ namespace libtorrent { TORRENT_ASSERT(!c->m_in_constructor); // add the newly connected peer to this torrent's peer list TORRENT_ASSERT(m_iterating_connections == 0); + + // we don't want to have to allocate memory to disconnect this peer, so + // make sure there's enough memory allocated in the deferred_disconnect + // list up-front + m_peers_to_disconnect.reserve(m_connections.size() + 1); + sorted_insert(m_connections, c.get()); update_want_peers(); update_want_tick(); @@ -6586,6 +6597,12 @@ namespace libtorrent { // add the newly connected peer to this torrent's peer list TORRENT_ASSERT(m_iterating_connections == 0); + + // we don't want to have to allocate memory to disconnect this peer, so + // make sure there's enough memory allocated in the deferred_disconnect + // list up-front + m_peers_to_disconnect.reserve(m_connections.size() + 1); + sorted_insert(m_connections, c.get()); m_ses.insert_peer(c); need_peer_list(); @@ -6872,6 +6889,8 @@ namespace libtorrent { } peers_erased(st.erased); + m_peers_to_disconnect.reserve(m_connections.size() + 1); + TORRENT_ASSERT(sorted_find(m_connections, p) == m_connections.end()); TORRENT_ASSERT(m_iterating_connections == 0); sorted_insert(m_connections, p);