don't use page aligned disk buffers

This commit is contained in:
arvidn 2018-11-23 02:08:57 +01:00 committed by Arvid Norberg
parent 3406f12ab3
commit cdf66409a4
11 changed files with 6 additions and 287 deletions

View File

@ -16,7 +16,6 @@ set(sources
web_connection_base web_connection_base
alert alert
alert_manager alert_manager
allocator
announce_entry announce_entry
assert assert
bandwidth_limit bandwidth_limit

View File

@ -632,7 +632,6 @@ lib iphlpapi : : <name>iphlpapi <link>shared ;
SOURCES = SOURCES =
alert alert
alert_manager alert_manager
allocator
announce_entry announce_entry
assert assert
bandwidth_limit bandwidth_limit

View File

@ -7,7 +7,6 @@ nobase_include_HEADERS = \
alert_manager.hpp \ alert_manager.hpp \
alert_types.hpp \ alert_types.hpp \
alloca.hpp \ alloca.hpp \
allocator.hpp \
announce_entry.hpp \ announce_entry.hpp \
assert.hpp \ assert.hpp \
bandwidth_limit.hpp \ bandwidth_limit.hpp \

View File

@ -1,59 +0,0 @@
/*
Copyright (c) 2009-2018, Arvid Norberg
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.
*/
#ifndef TORRENT_ALLOCATOR_HPP_INCLUDED
#define TORRENT_ALLOCATOR_HPP_INCLUDED
#include <cstddef>
#include "libtorrent/config.hpp"
namespace libtorrent
{
TORRENT_EXTRA_EXPORT int page_size();
struct TORRENT_EXTRA_EXPORT page_aligned_allocator
{
typedef int size_type;
typedef std::ptrdiff_t difference_type;
static char* malloc(size_type bytes);
static void free(char* block);
#ifdef TORRENT_DEBUG_BUFFERS
static bool in_use(char const* block);
#endif
};
}
#endif

View File

@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/utility.hpp> #include <boost/utility.hpp>
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR #ifndef TORRENT_DISABLE_POOL_ALLOCATOR
#include "libtorrent/allocator.hpp" // for page_aligned_allocator
#include <boost/pool/pool.hpp> #include <boost/pool/pool.hpp>
#endif #endif
@ -158,7 +157,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR #ifndef TORRENT_DISABLE_POOL_ALLOCATOR
// if this is true, all buffers are allocated // if this is true, all buffers are allocated
// from m_pool. If this is false, all buffers // from m_pool. If this is false, all buffers
// are allocated using page_aligned_allocator. // are allocated using malloc.
// if the settings change to prefer the other // if the settings change to prefer the other
// allocator, this bool will not switch over // allocator, this bool will not switch over
// to match the settings until all buffers have // to match the settings until all buffers have
@ -174,7 +173,7 @@ namespace libtorrent
// memory pool for read and write operations // memory pool for read and write operations
// and disk cache // and disk cache
boost::pool<page_aligned_allocator> m_pool; boost::pool<> m_pool;
#endif #endif
// this is specifically exempt from release_asserts // this is specifically exempt from release_asserts

View File

@ -35,7 +35,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/storage.hpp" #include "libtorrent/storage.hpp"
#include "libtorrent/allocator.hpp"
#include "libtorrent/io_service.hpp" #include "libtorrent/io_service.hpp"
#include "libtorrent/sliding_average.hpp" #include "libtorrent/sliding_average.hpp"
#include "libtorrent/disk_io_job.hpp" #include "libtorrent/disk_io_job.hpp"

View File

@ -57,7 +57,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/disk_buffer_holder.hpp" #include "libtorrent/disk_buffer_holder.hpp"
#include "libtorrent/thread.hpp" #include "libtorrent/thread.hpp"
#include "libtorrent/storage_defs.hpp" #include "libtorrent/storage_defs.hpp"
#include "libtorrent/allocator.hpp"
#include "libtorrent/file_pool.hpp" // pool_file_status #include "libtorrent/file_pool.hpp" // pool_file_status
#include "libtorrent/part_file.hpp" #include "libtorrent/part_file.hpp"
#include "libtorrent/stat_cache.hpp" #include "libtorrent/stat_cache.hpp"

View File

@ -41,7 +41,6 @@ libtorrent_rasterbar_la_SOURCES = \
web_connection_base.cpp \ web_connection_base.cpp \
alert.cpp \ alert.cpp \
alert_manager.cpp \ alert_manager.cpp \
allocator.cpp \
announce_entry.cpp \ announce_entry.cpp \
assert.cpp \ assert.cpp \
bandwidth_limit.cpp \ bandwidth_limit.cpp \

View File

@ -1,211 +0,0 @@
/*
Copyright (c) 2009-2018, Arvid Norberg
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/allocator.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/assert.hpp" // for print_backtrace
#include <boost/cstdint.hpp>
#if defined TORRENT_BEOS
#include <kernel/OS.h>
#include <stdlib.h> // malloc/free
#elif !defined TORRENT_WINDOWS
#include <stdlib.h> // posix_memalign/free
#include <unistd.h> // _SC_PAGESIZE
#endif
#if TORRENT_USE_MEMALIGN || TORRENT_USE_POSIX_MEMALIGN || defined TORRENT_WINDOWS
#include <malloc.h> // memalign and _aligned_malloc
#include <stdlib.h> // _aligned_malloc on mingw
#endif
#ifdef TORRENT_WINDOWS
// windows.h must be included after stdlib.h under mingw
#include <windows.h>
#endif
#ifdef TORRENT_MINGW
#define _aligned_malloc __mingw_aligned_malloc
#define _aligned_free __mingw_aligned_free
#endif
#ifdef TORRENT_DEBUG_BUFFERS
#ifndef TORRENT_WINDOWS
#include <sys/mman.h>
#endif
struct alloc_header
{
boost::int64_t size;
int magic;
char stack[3072];
};
#endif
namespace libtorrent
{
int page_size()
{
static int s = 0;
if (s != 0) return s;
#ifdef TORRENT_BUILD_SIMULATOR
s = 4096;
#elif defined TORRENT_WINDOWS
SYSTEM_INFO si;
GetSystemInfo(&si);
s = si.dwPageSize;
#elif defined TORRENT_BEOS
s = B_PAGE_SIZE;
#else
s = int(sysconf(_SC_PAGESIZE));
#endif
// assume the page size is 4 kiB if we
// fail to query it
if (s <= 0) s = 4096;
return s;
}
char* page_aligned_allocator::malloc(page_aligned_allocator::size_type bytes)
{
TORRENT_ASSERT(bytes > 0);
// just sanity check (this needs to be pretty high
// for cases where the cache size is several gigabytes)
TORRENT_ASSERT(bytes < 0x30000000);
TORRENT_ASSERT(int(bytes) >= page_size());
#ifdef TORRENT_DEBUG_BUFFERS
const int page = page_size();
const int num_pages = (bytes + (page-1)) / page + 2;
const int orig_bytes = bytes;
bytes = num_pages * page;
#endif
void* ret;
#if TORRENT_USE_POSIX_MEMALIGN
if (posix_memalign(&ret, page_size(), bytes)
!= 0) ret = NULL;
#elif TORRENT_USE_MEMALIGN
ret = memalign(page_size(), bytes);
#elif defined TORRENT_WINDOWS
ret = _aligned_malloc(bytes, page_size());
#elif defined TORRENT_BEOS
area_id id = create_area("", &ret, B_ANY_ADDRESS
, (bytes + page_size() - 1) & (page_size()-1), B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
if (id < B_OK) return NULL;
#else
ret = valloc(size_t(bytes));
#endif
if (ret == NULL) return NULL;
#ifdef TORRENT_DEBUG_BUFFERS
// make the two surrounding pages non-readable and -writable
alloc_header* h = static_cast<alloc_header*>(ret);
h->size = orig_bytes;
h->magic = 0x1337;
print_backtrace(h->stack, sizeof(h->stack));
#ifdef TORRENT_WINDOWS
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL)
#define PROT_READ PAGE_READONLY
#endif
mprotect(ret, page, PROT_READ);
mprotect(static_cast<char*>(ret) + (num_pages-1) * page, page, PROT_READ);
#ifdef TORRENT_WINDOWS
#undef mprotect
#undef PROT_READ
#endif
// fprintf(stderr, "malloc: %p head: %p tail: %p size: %d\n", ret + page, ret, ret + page + bytes, int(bytes));
return static_cast<char*>(ret) + page;
#else
return static_cast<char*>(ret);
#endif // TORRENT_DEBUG_BUFFERS
}
void page_aligned_allocator::free(char* block)
{
if (block == 0) return;
#ifdef TORRENT_DEBUG_BUFFERS
#ifdef TORRENT_WINDOWS
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, NULL)
#define PROT_READ PAGE_READONLY
#define PROT_WRITE PAGE_READWRITE
#endif
const int page = page_size();
// make the two surrounding pages non-readable and -writable
mprotect(block - page, page, PROT_READ | PROT_WRITE);
alloc_header* h = reinterpret_cast<alloc_header*>(block - page);
const int num_pages = (h->size + (page-1)) / page + 2;
TORRENT_ASSERT(h->magic == 0x1337);
mprotect(block + (num_pages-2) * page, page, PROT_READ | PROT_WRITE);
// fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size));
h->magic = 0;
block -= page;
#ifdef TORRENT_WINDOWS
#undef mprotect
#undef PROT_READ
#undef PROT_WRITE
#endif
print_backtrace(h->stack, sizeof(h->stack));
#endif // TORRENT_DEBUG_BUFFERS
#ifdef TORRENT_WINDOWS
_aligned_free(block);
#elif defined TORRENT_BEOS
area_id id = area_for(block);
if (id < B_OK) return;
delete_area(id);
#else
::free(block);
#endif // TORRENT_WINDOWS
}
#ifdef TORRENT_DEBUG_BUFFERS
bool page_aligned_allocator::in_use(char const* block)
{
const int page = page_size();
alloc_header const* h = reinterpret_cast<alloc_header const*>(block - page);
return h->magic == 0x1337;
}
#endif
}

View File

@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/disk_buffer_pool.hpp" #include "libtorrent/disk_buffer_pool.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#include "libtorrent/allocator.hpp"
#include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/aux_/session_settings.hpp"
#include "libtorrent/io_service.hpp" #include "libtorrent/io_service.hpp"
#include "libtorrent/alert.hpp" #include "libtorrent/alert.hpp"
@ -185,8 +184,6 @@ namespace libtorrent
#if defined TORRENT_DEBUG #if defined TORRENT_DEBUG
return m_buffers_in_use.count(buffer) == 1; return m_buffers_in_use.count(buffer) == 1;
#elif defined TORRENT_DEBUG_BUFFERS
return page_aligned_allocator::in_use(buffer);
#elif defined TORRENT_DISABLE_POOL_ALLOCATOR #elif defined TORRENT_DISABLE_POOL_ALLOCATOR
return true; return true;
#else #else
@ -290,7 +287,7 @@ namespace libtorrent
{ {
#if defined TORRENT_DISABLE_POOL_ALLOCATOR #if defined TORRENT_DISABLE_POOL_ALLOCATOR
ret = page_aligned_allocator::malloc(m_block_size); ret = static_cast<char*>(std::malloc(m_block_size));
#else #else
if (m_using_pool_allocator) if (m_using_pool_allocator)
@ -306,7 +303,7 @@ namespace libtorrent
} }
else else
{ {
ret = page_aligned_allocator::malloc(m_block_size); ret = static_cast<char*>(std::malloc(m_block_size));
} }
#endif #endif
if (ret == NULL) if (ret == NULL)
@ -549,13 +546,13 @@ namespace libtorrent
{ {
#if defined TORRENT_DISABLE_POOL_ALLOCATOR #if defined TORRENT_DISABLE_POOL_ALLOCATOR
page_aligned_allocator::free(buf); std::free(buf);
#else #else
if (m_using_pool_allocator) if (m_using_pool_allocator)
m_pool.free(buf); m_pool.free(buf);
else else
page_aligned_allocator::free(buf); std::free(buf);
#endif // TORRENT_DISABLE_POOL_ALLOCATOR #endif // TORRENT_DISABLE_POOL_ALLOCATOR
} }

View File

@ -64,7 +64,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/alloca.hpp" #include "libtorrent/alloca.hpp"
#include "libtorrent/allocator.hpp" // page_size
#include "libtorrent/file.hpp" #include "libtorrent/file.hpp"
#include <cstring> #include <cstring>
#include <vector> #include <vector>