factor out page size function and make aligned allocator configurable to use posix_memalign and memalign
This commit is contained in:
parent
cb3958c91b
commit
df2f2570c2
|
@ -39,6 +39,8 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
|
|
||||||
|
TORRENT_EXPORT int page_size();
|
||||||
|
|
||||||
struct TORRENT_EXPORT page_aligned_allocator
|
struct TORRENT_EXPORT page_aligned_allocator
|
||||||
{
|
{
|
||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
|
|
|
@ -37,14 +37,39 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#else
|
#else
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h> // _SC_PAGESIZE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
|
int page_size()
|
||||||
|
{
|
||||||
|
static int s = 0;
|
||||||
|
if (s != 0) return s;
|
||||||
|
|
||||||
|
#ifdef TORRENT_WINDOWS
|
||||||
|
SYSTEM_INFO si;
|
||||||
|
GetSystemInfo(&si);
|
||||||
|
s = si.dwPageSize;
|
||||||
|
#else
|
||||||
|
s = 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(const size_type bytes)
|
char* page_aligned_allocator::malloc(const size_type bytes)
|
||||||
{
|
{
|
||||||
#if defined TORRENT_WINDOWS
|
#if TORRENT_USE_POSIX_MEMALIGN
|
||||||
|
void* ret;
|
||||||
|
if (posix_memalign(&ret, page_size(), bytes) != 0) ret = 0;
|
||||||
|
return ret;
|
||||||
|
#elif TORRENT_USE_MEMALIGN
|
||||||
|
return memalign(page_size(), bytes);
|
||||||
|
#elif defined TORRENT_WINDOWS
|
||||||
return (char*)VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
return (char*)VirtualAlloc(0, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||||
#elif defined TORRENT_BEOS
|
#elif defined TORRENT_BEOS
|
||||||
// we could potentially use create_area() here, but
|
// we could potentially use create_area() here, but
|
||||||
|
|
|
@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/pch.hpp"
|
#include "libtorrent/pch.hpp"
|
||||||
#include "libtorrent/config.hpp"
|
#include "libtorrent/config.hpp"
|
||||||
#include "libtorrent/alloca.hpp"
|
#include "libtorrent/alloca.hpp"
|
||||||
|
#include "libtorrent/allocator.hpp" // page_size
|
||||||
#include "libtorrent/escape_string.hpp" // for string conversion
|
#include "libtorrent/escape_string.hpp" // for string conversion
|
||||||
|
|
||||||
#include <boost/scoped_ptr.hpp>
|
#include <boost/scoped_ptr.hpp>
|
||||||
|
@ -901,13 +902,7 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
if (m_page_size != 0) return;
|
if (m_page_size != 0) return;
|
||||||
|
|
||||||
#ifdef TORRENT_WINDOWS
|
m_page_size = page_size();
|
||||||
SYSTEM_INFO si;
|
|
||||||
GetSystemInfo(&si);
|
|
||||||
m_page_size = si.dwPageSize;
|
|
||||||
#else
|
|
||||||
m_page_size = sysconf(_SC_PAGESIZE);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -65,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/aux_/session_impl.hpp"
|
#include "libtorrent/aux_/session_impl.hpp"
|
||||||
#include "libtorrent/disk_buffer_holder.hpp"
|
#include "libtorrent/disk_buffer_holder.hpp"
|
||||||
#include "libtorrent/alloca.hpp"
|
#include "libtorrent/alloca.hpp"
|
||||||
|
#include "libtorrent/allocator.hpp" // page_size
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
|
@ -339,20 +340,13 @@ namespace libtorrent
|
||||||
storage(file_storage const& fs, file_storage const* mapped, std::string const& path, file_pool& fp)
|
storage(file_storage const& fs, file_storage const* mapped, std::string const& path, file_pool& fp)
|
||||||
: m_files(fs)
|
: m_files(fs)
|
||||||
, m_pool(fp)
|
, m_pool(fp)
|
||||||
, m_page_size(4096)
|
, m_page_size(page_size())
|
||||||
, m_allocate_files(false)
|
, m_allocate_files(false)
|
||||||
{
|
{
|
||||||
if (mapped) m_mapped_files.reset(new file_storage(*mapped));
|
if (mapped) m_mapped_files.reset(new file_storage(*mapped));
|
||||||
|
|
||||||
TORRENT_ASSERT(m_files.begin() != m_files.end());
|
TORRENT_ASSERT(m_files.begin() != m_files.end());
|
||||||
m_save_path = complete(path);
|
m_save_path = complete(path);
|
||||||
#ifdef TORRENT_WINDOWS
|
|
||||||
SYSTEM_INFO si;
|
|
||||||
GetSystemInfo(&si);
|
|
||||||
m_page_size = si.dwPageSize;
|
|
||||||
#else
|
|
||||||
m_page_size = sysconf(_SC_PAGESIZE);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool has_any_file();
|
bool has_any_file();
|
||||||
|
|
Loading…
Reference in New Issue