1 /* 2 3 Copyright (c) 2003-2018, Arvid Norberg 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 10 * Redistributions of source code must retain the above copyright 11 notice, this list of conditions and the following disclaimer. 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the distribution. 15 * Neither the name of the author nor the names of its 16 contributors may be used to endorse or promote products derived 17 from this software without specific prior written permission. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 POSSIBILITY OF SUCH DAMAGE. 30 31 */ 32 33 #ifndef TORRENT_PEER_ALLOCATOR_HPP_INCLUDED 34 #define TORRENT_PEER_ALLOCATOR_HPP_INCLUDED 35 36 #include "libtorrent/config.hpp" 37 #include "libtorrent/torrent_peer.hpp" 38 #include "libtorrent/aux_/pool.hpp" 39 40 namespace libtorrent { 41 42 struct TORRENT_EXTRA_EXPORT torrent_peer_allocator_interface 43 { 44 enum peer_type_t 45 { 46 ipv4_peer_type, 47 ipv6_peer_type, 48 i2p_peer_type 49 }; 50 51 virtual torrent_peer* allocate_peer_entry(int type) = 0; 52 virtual void free_peer_entry(torrent_peer* p) = 0; 53 protected: ~torrent_peer_allocator_interfacelibtorrent::torrent_peer_allocator_interface54 ~torrent_peer_allocator_interface() {} 55 }; 56 57 struct TORRENT_EXTRA_EXPORT torrent_peer_allocator final 58 : torrent_peer_allocator_interface 59 { 60 #if TORRENT_USE_ASSERTS ~torrent_peer_allocatorlibtorrent::torrent_peer_allocator61 ~torrent_peer_allocator() { 62 m_in_use = false; 63 } 64 #endif 65 66 torrent_peer* allocate_peer_entry(int type) override; 67 void free_peer_entry(torrent_peer* p) override; 68 total_byteslibtorrent::torrent_peer_allocator69 std::uint64_t total_bytes() const { return m_total_bytes; } total_allocationslibtorrent::torrent_peer_allocator70 std::uint64_t total_allocations() const { return m_total_allocations; } live_byteslibtorrent::torrent_peer_allocator71 int live_bytes() const { return m_live_bytes; } live_allocationslibtorrent::torrent_peer_allocator72 int live_allocations() const { return m_live_allocations; } 73 74 private: 75 76 // this is a shared pool where torrent_peer objects 77 // are allocated. It's a pool since we're likely 78 // to have tens of thousands of peers, and a pool 79 // saves significant overhead 80 81 aux::pool m_ipv4_peer_pool{sizeof(libtorrent::ipv4_peer), 500}; 82 aux::pool m_ipv6_peer_pool{sizeof(libtorrent::ipv6_peer), 500}; 83 #if TORRENT_USE_I2P 84 aux::pool m_i2p_peer_pool{sizeof(libtorrent::i2p_peer), 500}; 85 #endif 86 87 // the total number of bytes allocated (cumulative) 88 std::uint64_t m_total_bytes = 0; 89 // the total number of allocations (cumulative) 90 std::uint64_t m_total_allocations = 0; 91 // the number of currently live bytes 92 int m_live_bytes = 0; 93 // the number of currently live allocations 94 int m_live_allocations = 0; 95 #if TORRENT_USE_ASSERTS 96 bool m_in_use = true; 97 #endif 98 }; 99 } 100 101 #endif 102 103