diff --git a/Jamfile b/Jamfile index 9b259de8d..f15f35680 100755 --- a/Jamfile +++ b/Jamfile @@ -160,6 +160,9 @@ rule building ( properties * ) feature need-librt : no yes : composite propagated link-incompatible ; +feature pool-allocators : off static shared : composite propagated link-incompatible ; +feature.compose off : TORRENT_DISABLE_POOL_ALLOCATOR ; + feature geoip : off static shared : composite propagated link-incompatible ; feature.compose off : TORRENT_DISABLE_GEO_IP ; diff --git a/docs/building.rst b/docs/building.rst index cbcf1dd31..dd8e4f876 100644 --- a/docs/building.rst +++ b/docs/building.rst @@ -264,6 +264,12 @@ Build features: | | shipped public domain SHA-1 implementation is | | | used. | +------------------------+----------------------------------------------------+ +| ``pool-allocators`` | * ``on`` - default, uses pool allocators for send | +| | buffers. | +| | * ``off`` - uses ``malloc()`` and ``free()`` | +| | instead. Might be useful to debug buffer issues | +| | with tools like electric fence or libgmalloc. | ++------------------------+----------------------------------------------------+ | ``link`` | * ``static`` - builds libtorrent as a static | | | library (.a / .lib) | | | * ``shared`` - builds libtorrent as a shared | @@ -540,6 +546,8 @@ defines you can use to control the build. | | will be built and the correct code will be | | | chosen at run-time. | +---------------------------------------+-------------------------------------------------+ +| ``TORRENT_DISABLE_POOL_ALLOCATOR`` | Disables use of ``boost::pool<>``. | ++---------------------------------------+-------------------------------------------------+ | ``TORRENT_LINKING_SHARED`` | If this is defined when including the | | | libtorrent headers, the classes and functions | | | will be tagged with ``__declspec(dllimport)`` | diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 2bc4c9cef..9894a7b94 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -343,9 +343,11 @@ namespace libtorrent void on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih); +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR // this pool is used to allocate and recycle send // buffers from. boost::pool<> m_send_buffers; +#endif boost::mutex m_send_buffer_mutex; // the file pool that all storages in this session's diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 8da1a5d2e..ff974b210 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -42,11 +42,13 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include #include #include "libtorrent/config.hpp" +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR +#include +#endif namespace libtorrent { @@ -259,9 +261,11 @@ namespace libtorrent bool m_coalesce_reads; bool m_use_read_cache; +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR // memory pool for read and write operations // and disk cache boost::pool<> m_pool; +#endif // number of bytes per block. The BitTorrent // protocol defines the block size to 16 KiB. diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index d2306c870..3afd7a5c3 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -55,7 +55,9 @@ namespace libtorrent , m_coalesce_writes(true) , m_coalesce_reads(true) , m_use_read_cache(true) +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR , m_pool(block_size) +#endif , m_block_size(block_size) , m_ios(ios) , m_disk_io_thread(boost::ref(*this)) @@ -493,7 +495,9 @@ namespace libtorrent { if (p.blocks[k]) { +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR TORRENT_ASSERT(m_pool.is_from(p.blocks[k])); +#endif ++blocks; } } @@ -515,7 +519,9 @@ namespace libtorrent { if (p.blocks[k]) { +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR TORRENT_ASSERT(m_pool.is_from(p.blocks[k])); +#endif ++blocks; } } @@ -690,7 +696,11 @@ namespace libtorrent #ifdef TORRENT_STATS ++m_allocations; #endif +#ifdef TORRENT_DISABLE_POOL_ALLOCATOR + return (char*)malloc(m_block_size); +#else return (char*)m_pool.ordered_malloc(); +#endif } void disk_io_thread::free_buffer(char* buf, mutex_t::scoped_lock& l) @@ -699,7 +709,11 @@ namespace libtorrent #ifdef TORRENT_STATS --m_allocations; #endif +#ifdef TORRENT_DISABLE_POOL_ALLOCATOR + free(buf); +#else m_pool.ordered_free(buf); +#endif } void disk_io_thread::operator()() @@ -738,7 +752,9 @@ namespace libtorrent std::string const& error_string = j.storage->error(); if (!error_string.empty()) { +#ifndef NDEBUG std::cout << "ERROR: " << error_string << std::endl; +#endif j.str = error_string; j.storage->clear_error(); ret = -1; @@ -891,7 +907,9 @@ namespace libtorrent ++i; } } +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR m_pool.release_memory(); +#endif l.unlock(); ret = j.storage->release_files_impl(); if (ret != 0) @@ -925,7 +943,9 @@ namespace libtorrent } } m_pieces.erase(i, m_pieces.end()); +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR m_pool.release_memory(); +#endif l.unlock(); ret = j.storage->delete_files_impl(); if (ret != 0) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 1e7af69f8..51a8e6314 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -135,8 +135,11 @@ namespace aux { , fs::path const& logpath #endif ) - : m_send_buffers(send_buffer_size) - , m_files(40) + : +#ifndef TORRENT_DISABLE_POOL_ALLOCATOR + m_send_buffers(send_buffer_size), +#endif + m_files(40) , m_io_service() , m_disk_thread(m_io_service) , m_half_open(m_io_service) @@ -2220,8 +2223,13 @@ namespace aux { m_buffer_usage_logger << log_time() << " protocol_buffer: " << (m_buffer_allocations * send_buffer_size) << std::endl; #endif +#ifdef TORRENT_DISABLE_POOL_ALLOCATOR + int num_bytes = num_buffers * send_buffer_size; + return std::make_pair((char*)malloc(num_bytes), num_bytes); +#else return std::make_pair((char*)m_send_buffers.ordered_malloc(num_buffers) , num_buffers * send_buffer_size); +#endif } void session_impl::free_buffer(char* buf, int size) @@ -2238,7 +2246,11 @@ namespace aux { m_buffer_usage_logger << log_time() << " protocol_buffer: " << (m_buffer_allocations * send_buffer_size) << std::endl; #endif +#ifdef TORRENT_DISABLE_POOL_ALLOCATOR + free(buf); +#else m_send_buffers.ordered_free(buf, num_buffers); +#endif } #ifndef NDEBUG