http://stxxl.sourceforge.net
<dementiev@mpi-sb.mpg.de>
<beckmann@cs.uni-frankfurt.de>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/bits/io/mmap_file.h>
#if STXXL_HAVE_MMAP_FILE
#include <stxxl/bits/io/iostats.h>
#include <stxxl/bits/common/error_handling.h>
#include "ufs_platform.h"
#include <sys/mman.h>
STXXL_BEGIN_NAMESPACE
void mmap_file::serve(const request* req) throw (io_error)
{
scoped_mutex_lock fd_lock(fd_mutex);
assert(req->get_file() == this);
offset_type offset = req->get_offset();
void* buffer = req->get_buffer();
size_type bytes = req->get_size();
request::request_type type = req->get_type();
stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
int prot = (type == request::READ) ? PROT_READ : PROT_WRITE;
void* mem = mmap(NULL, bytes, prot, MAP_SHARED, file_des, offset);
if (mem == MAP_FAILED)
{
STXXL_THROW_ERRNO(io_error,
" mmap() failed." <<
" path=" << filename <<
" bytes=" << bytes <<
" Page size: " << sysconf(_SC_PAGESIZE) <<
" offset modulo page size " << (offset % sysconf(_SC_PAGESIZE)));
}
else if (mem == 0)
{
STXXL_THROW_ERRNO(io_error, "mmap() returned NULL");
}
else
{
if (type == request::READ)
{
memcpy(buffer, mem, bytes);
}
else
{
memcpy(mem, buffer, bytes);
}
STXXL_THROW_ERRNO_NE_0(munmap(mem, bytes), io_error,
"munmap() failed");
}
}
const char* mmap_file::io_type() const
{
return "mmap";
}
STXXL_END_NAMESPACE
#endif