factor out page size function and make aligned allocator configurable to use posix_memalign and memalign

This commit is contained in:
Arvid Norberg 2009-12-09 01:55:30 +00:00
parent cb3958c91b
commit df2f2570c2
4 changed files with 32 additions and 16 deletions

View File

@ -39,6 +39,8 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
TORRENT_EXPORT int page_size();
struct TORRENT_EXPORT page_aligned_allocator
{
typedef std::size_t size_type;

View File

@ -37,14 +37,39 @@ POSSIBILITY OF SUCH DAMAGE.
#include <Windows.h>
#else
#include <stdlib.h>
#include <unistd.h> // _SC_PAGESIZE
#endif
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)
{
#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);
#elif defined TORRENT_BEOS
// we could potentially use create_area() here, but

View File

@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/alloca.hpp"
#include "libtorrent/allocator.hpp" // page_size
#include "libtorrent/escape_string.hpp" // for string conversion
#include <boost/scoped_ptr.hpp>
@ -901,13 +902,7 @@ namespace libtorrent
{
if (m_page_size != 0) return;
#ifdef TORRENT_WINDOWS
SYSTEM_INFO si;
GetSystemInfo(&si);
m_page_size = si.dwPageSize;
#else
m_page_size = sysconf(_SC_PAGESIZE);
#endif
m_page_size = page_size();
}
#endif

View File

@ -65,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/disk_buffer_holder.hpp"
#include "libtorrent/alloca.hpp"
#include "libtorrent/allocator.hpp" // page_size
#include <cstdio>
@ -339,20 +340,13 @@ namespace libtorrent
storage(file_storage const& fs, file_storage const* mapped, std::string const& path, file_pool& fp)
: m_files(fs)
, m_pool(fp)
, m_page_size(4096)
, m_page_size(page_size())
, m_allocate_files(false)
{
if (mapped) m_mapped_files.reset(new file_storage(*mapped));
TORRENT_ASSERT(m_files.begin() != m_files.end());
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();