diff --git a/io_win32.cpp b/io_win32.cpp index 856e697a..05f3ab91 100644 --- a/io_win32.cpp +++ b/io_win32.cpp @@ -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 (_get_osfhandle (src))), + fflags, + mflags) +{ }; + + /////////////////////////////////////////////////////////////////////////////// size_t mapped_file::size (void) const diff --git a/io_win32.hpp b/io_win32.hpp index bd3e6c7c..c103f6d0 100644 --- a/io_win32.hpp +++ b/io_win32.hpp @@ -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 #include + +/////////////////////////////////////////////////////////////////////////////// +// 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;