io: add win32 fd constructor to mapped_file

This commit is contained in:
Danny Robson 2016-08-25 12:04:55 +10:00
parent 41a8efa200
commit 98c09df7f9
2 changed files with 66 additions and 30 deletions

View File

@ -59,39 +59,21 @@ DWORD
mflags_to_protect (int mflags) {
DWORD res = 0;
if (mflags & util::detail::win32::PROT_READ) res |= PAGE_READONLY;
if (mflags & util::detail::win32::PROT_WRITE) res |= PAGE_READWRITE;
if (mflags & util::detail::win32::PROT_EXEC) res |= PAGE_EXECUTE;
if (mflags & PROT_READ) res |= PAGE_READONLY;
if (mflags & PROT_WRITE) res |= PAGE_READWRITE;
if (mflags & PROT_EXEC) res |= PAGE_EXECUTE;
return res;
}
///////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const boost::filesystem::path &path,
mapped_file::mapped_file (::util::win32::handle &&src,
int fflags,
int mflags):
m_file (std::forward<::util::win32::handle> (src)),
m_data (nullptr, UnmapViewOfFile)
{
// Cache the ASCII path to avoid memory scoping issues.
std::string path_str = path.string ();
// Get hold of the file we're attempting to map. Emulate some level of POXIS mmap.
m_file.reset (
CreateFile (
path_str.c_str (),
fflags_to_generic (fflags),
fflags == O_RDONLY ? FILE_SHARE_READ : 0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
nullptr
)
);
if (m_file == INVALID_HANDLE_VALUE)
win32_error::throw_code ();
// I would rather perform checks on filesize after mapping, but mapping
// requires a check for empty files before we perform the mapping to
// detect errors it throws in that specific situation.
@ -135,6 +117,38 @@ mapped_file::mapped_file (const boost::filesystem::path &path,
}
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const boost::filesystem::path &path,
int fflags,
int mflags):
mapped_file (
::util::win32::handle (
::CreateFile (
path.string ().c_str (),
fflags_to_generic (fflags),
fflags == O_RDONLY ? FILE_SHARE_READ : 0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
nullptr
)
),
fflags,
mflags
)
{ ; }
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const util::fd &src,
int fflags,
int mflags):
mapped_file (util::win32::handle (reinterpret_cast<HANDLE> (_get_osfhandle (src))),
fflags,
mflags)
{ };
///////////////////////////////////////////////////////////////////////////////
size_t
mapped_file::size (void) const

View File

@ -17,6 +17,7 @@
#ifndef __UTIL_IO_WIN32_HPP
#define __UTIL_IO_WIN32_HPP
#include "./io.hpp"
#include "./win32/handle.hpp"
#include "./view.hpp"
@ -27,20 +28,41 @@
#include <sys/types.h>
#include <fcntl.h>
///////////////////////////////////////////////////////////////////////////////
// compatibility definitions
enum : int {
PROT_NONE = 0,
PROT_READ = 1 << 0,
PROT_EXEC = 1 << 1,
PROT_WRITE = 1 << 2
};
//-----------------------------------------------------------------------------
enum : int {
MAP_SHARED,
MAP_PRIVATE,
MAP_ANONYMOUS
};
///////////////////////////////////////////////////////////////////////////////
// implementation definitions
namespace util {
namespace detail { namespace win32 {
enum {
PROT_NONE = 0,
PROT_READ = 1 << 0,
PROT_EXEC = 1 << 1,
PROT_WRITE = 1 << 2
};
class mapped_file {
public:
mapped_file (::util::win32::handle &&,
int fflags = O_RDONLY,
int mflags = PROT_READ);
mapped_file (const boost::filesystem::path &path,
int fflags = O_RDONLY,
int mflags = PROT_READ);
mapped_file (const util::fd&,
int fflag = O_RDONLY,
int mflags = PROT_READ);
mapped_file (const mapped_file&) = delete;
mapped_file& operator= (const mapped_file&) = delete;