WIP mmap changes for Windows

This commit is contained in:
Marcus Holland-Moritz 2023-06-21 18:32:25 +02:00
parent 2f26ceafb4
commit 46cf71139f
2 changed files with 36 additions and 15 deletions

View File

@ -44,8 +44,6 @@ class mmap : public mmif {
private: private:
boost::iostreams::mapped_file mutable mf_; boost::iostreams::mapped_file mutable mf_;
#ifndef _WIN32 uint64_t const page_size_;
off_t const page_size_;
#endif
}; };
} // namespace dwarfs } // namespace dwarfs

View File

@ -22,8 +22,11 @@
#include <cassert> #include <cassert>
#include <cerrno> #include <cerrno>
#ifndef _WIN32 #ifdef _WIN32
#include <folly/portability/Windows.h>
#else
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h>
#endif #endif
#include "dwarfs/error.h" #include "dwarfs/error.h"
@ -31,11 +34,29 @@
namespace dwarfs { namespace dwarfs {
namespace {
uint64_t get_page_size() {
#ifdef _WIN32
::SYSTEM_INFO info;
::GetSystemInfo(&info);
return info.dwPageSize;
#else
return ::sysconf(_SC_PAGESIZE);
#endif
}
} // namespace
std::error_code std::error_code
mmap::lock(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) { mmap::lock(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
std::error_code ec; std::error_code ec;
#ifndef _WIN32 #ifdef _WIN32
if (::VirtualLock(mf_.const_data() + offset, size) == 0) {
ec.assign(::GetLastError(), std::system_category());
}
#else
if (::mlock(mf_.const_data() + offset, size) != 0) { if (::mlock(mf_.const_data() + offset, size) != 0) {
ec.assign(errno, std::generic_category()); ec.assign(errno, std::generic_category());
} }
@ -48,13 +69,17 @@ std::error_code
mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) { mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
std::error_code ec; std::error_code ec;
#ifndef _WIN32
auto misalign = offset % page_size_; auto misalign = offset % page_size_;
offset -= misalign; offset -= misalign;
size += misalign; size += misalign;
size -= size % page_size_; size -= size % page_size_;
#ifdef _WIN32
if (::VirtualFree(mf_.data() + offset, size, MEM_DECOMMIT) == 0) {
ec.assign(::GetLastError(), std::system_category());
}
#else
if (::madvise(mf_.data() + offset, size, MADV_DONTNEED) != 0) { if (::madvise(mf_.data() + offset, size, MADV_DONTNEED) != 0) {
ec.assign(errno, std::generic_category()); ec.assign(errno, std::generic_category());
} }
@ -66,9 +91,13 @@ mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
std::error_code mmap::release_until(off_t offset [[maybe_unused]]) { std::error_code mmap::release_until(off_t offset [[maybe_unused]]) {
std::error_code ec; std::error_code ec;
#ifndef _WIN32
offset -= offset % page_size_; offset -= offset % page_size_;
#ifdef _WIN32
if (::VirtualFree(mf_.data(), offset, MEM_DECOMMIT) == 0) {
ec.assign(::GetLastError(), std::system_category());
}
#else
if (::madvise(mf_.data(), offset, MADV_DONTNEED) != 0) { if (::madvise(mf_.data(), offset, MADV_DONTNEED) != 0) {
ec.assign(errno, std::generic_category()); ec.assign(errno, std::generic_category());
} }
@ -83,19 +112,13 @@ size_t mmap::size() const { return mf_.size(); }
mmap::mmap(const std::string& path) mmap::mmap(const std::string& path)
: mf_(path, boost::iostreams::mapped_file::readonly) : mf_(path, boost::iostreams::mapped_file::readonly)
#ifndef _WIN32 , page_size_(get_page_size()) {
, page_size_(::sysconf(_SC_PAGESIZE))
#endif
{
assert(mf_.is_open()); assert(mf_.is_open());
} }
mmap::mmap(const std::string& path, size_t size) mmap::mmap(const std::string& path, size_t size)
: mf_(path, boost::iostreams::mapped_file::readonly, size) : mf_(path, boost::iostreams::mapped_file::readonly, size)
#ifndef _WIN32 , page_size_(get_page_size()) {
, page_size_(::sysconf(_SC_PAGESIZE))
#endif
{
assert(mf_.is_open()); assert(mf_.is_open());
} }