2014-07-06 21:18:00 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2014-07-06 21:18:00 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/torrent_peer_allocator.hpp"
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
torrent_peer* torrent_peer_allocator::allocate_peer_entry(int type)
|
|
|
|
{
|
2017-11-08 11:44:56 +01:00
|
|
|
TORRENT_ASSERT(m_in_use);
|
2016-06-20 17:32:06 +02:00
|
|
|
torrent_peer* p = nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case torrent_peer_allocator_interface::ipv4_peer_type:
|
2015-08-14 05:52:25 +02:00
|
|
|
p = static_cast<torrent_peer*>(m_ipv4_peer_pool.malloc());
|
2016-06-20 17:32:06 +02:00
|
|
|
if (p == nullptr) return nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_ipv4_peer_pool.set_next_size(500);
|
|
|
|
m_total_bytes += sizeof(libtorrent::ipv4_peer);
|
|
|
|
m_live_bytes += sizeof(libtorrent::ipv4_peer);
|
|
|
|
++m_live_allocations;
|
|
|
|
++m_total_allocations;
|
|
|
|
break;
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
case torrent_peer_allocator_interface::ipv6_peer_type:
|
2015-08-14 05:52:25 +02:00
|
|
|
p = static_cast<torrent_peer*>(m_ipv6_peer_pool.malloc());
|
2016-06-20 17:32:06 +02:00
|
|
|
if (p == nullptr) return nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_ipv6_peer_pool.set_next_size(500);
|
|
|
|
m_total_bytes += sizeof(libtorrent::ipv6_peer);
|
|
|
|
m_live_bytes += sizeof(libtorrent::ipv6_peer);
|
|
|
|
++m_live_allocations;
|
|
|
|
++m_total_allocations;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
case torrent_peer_allocator_interface::i2p_peer_type:
|
2015-08-14 05:52:25 +02:00
|
|
|
p = static_cast<torrent_peer*>(m_i2p_peer_pool.malloc());
|
2016-06-20 17:32:06 +02:00
|
|
|
if (p == nullptr) return nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_i2p_peer_pool.set_next_size(500);
|
|
|
|
m_total_bytes += sizeof(libtorrent::i2p_peer);
|
|
|
|
m_live_bytes += sizeof(libtorrent::i2p_peer);
|
|
|
|
++m_live_allocations;
|
|
|
|
++m_total_allocations;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrent_peer_allocator::free_peer_entry(torrent_peer* p)
|
|
|
|
{
|
2017-11-08 11:44:56 +01:00
|
|
|
TORRENT_ASSERT(m_in_use);
|
|
|
|
TORRENT_ASSERT(p->in_use);
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (p->is_v6_addr)
|
|
|
|
{
|
2015-08-14 05:52:25 +02:00
|
|
|
TORRENT_ASSERT(m_ipv6_peer_pool.is_from(static_cast<libtorrent::ipv6_peer*>(p)));
|
2014-07-06 21:18:00 +02:00
|
|
|
static_cast<libtorrent::ipv6_peer*>(p)->~ipv6_peer();
|
|
|
|
m_ipv6_peer_pool.free(p);
|
2016-11-21 07:49:56 +01:00
|
|
|
TORRENT_ASSERT(m_live_bytes >= int(sizeof(ipv6_peer)));
|
|
|
|
m_live_bytes -= int(sizeof(ipv6_peer));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_live_allocations > 0);
|
|
|
|
--m_live_allocations;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
if (p->is_i2p_addr)
|
|
|
|
{
|
2015-08-14 05:52:25 +02:00
|
|
|
TORRENT_ASSERT(m_i2p_peer_pool.is_from(static_cast<libtorrent::i2p_peer*>(p)));
|
2014-07-06 21:18:00 +02:00
|
|
|
static_cast<libtorrent::i2p_peer*>(p)->~i2p_peer();
|
|
|
|
m_i2p_peer_pool.free(p);
|
2016-11-21 07:49:56 +01:00
|
|
|
TORRENT_ASSERT(m_live_bytes >= int(sizeof(i2p_peer)));
|
|
|
|
m_live_bytes -= int(sizeof(i2p_peer));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_live_allocations > 0);
|
|
|
|
--m_live_allocations;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-14 05:52:25 +02:00
|
|
|
TORRENT_ASSERT(m_ipv4_peer_pool.is_from(static_cast<libtorrent::ipv4_peer*>(p)));
|
2014-07-06 21:18:00 +02:00
|
|
|
static_cast<libtorrent::ipv4_peer*>(p)->~ipv4_peer();
|
|
|
|
m_ipv4_peer_pool.free(p);
|
2016-11-21 07:49:56 +01:00
|
|
|
TORRENT_ASSERT(m_live_bytes >= int(sizeof(ipv4_peer)));
|
|
|
|
m_live_bytes -= int(sizeof(ipv4_peer));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_live_allocations > 0);
|
|
|
|
--m_live_allocations;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|