Skip to content

Commit 46cf711

Browse files
committed
WIP mmap changes for Windows
1 parent 2f26cea commit 46cf711

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

include/dwarfs/mmap.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class mmap : public mmif {
4444

4545
private:
4646
boost::iostreams::mapped_file mutable mf_;
47-
#ifndef _WIN32
48-
off_t const page_size_;
49-
#endif
47+
uint64_t const page_size_;
5048
};
5149
} // namespace dwarfs

src/dwarfs/mmap.cpp

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,41 @@
2222
#include <cassert>
2323
#include <cerrno>
2424

25-
#ifndef _WIN32
25+
#ifdef _WIN32
26+
#include <folly/portability/Windows.h>
27+
#else
2628
#include <sys/mman.h>
29+
#include <unistd.h>
2730
#endif
2831

2932
#include "dwarfs/error.h"
3033
#include "dwarfs/mmap.h"
3134

3235
namespace dwarfs {
3336

37+
namespace {
38+
39+
uint64_t get_page_size() {
40+
#ifdef _WIN32
41+
::SYSTEM_INFO info;
42+
::GetSystemInfo(&info);
43+
return info.dwPageSize;
44+
#else
45+
return ::sysconf(_SC_PAGESIZE);
46+
#endif
47+
}
48+
49+
} // namespace
50+
3451
std::error_code
3552
mmap::lock(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
3653
std::error_code ec;
3754

38-
#ifndef _WIN32
55+
#ifdef _WIN32
56+
if (::VirtualLock(mf_.const_data() + offset, size) == 0) {
57+
ec.assign(::GetLastError(), std::system_category());
58+
}
59+
#else
3960
if (::mlock(mf_.const_data() + offset, size) != 0) {
4061
ec.assign(errno, std::generic_category());
4162
}
@@ -48,13 +69,17 @@ std::error_code
4869
mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
4970
std::error_code ec;
5071

51-
#ifndef _WIN32
5272
auto misalign = offset % page_size_;
5373

5474
offset -= misalign;
5575
size += misalign;
5676
size -= size % page_size_;
5777

78+
#ifdef _WIN32
79+
if (::VirtualFree(mf_.data() + offset, size, MEM_DECOMMIT) == 0) {
80+
ec.assign(::GetLastError(), std::system_category());
81+
}
82+
#else
5883
if (::madvise(mf_.data() + offset, size, MADV_DONTNEED) != 0) {
5984
ec.assign(errno, std::generic_category());
6085
}
@@ -66,9 +91,13 @@ mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
6691
std::error_code mmap::release_until(off_t offset [[maybe_unused]]) {
6792
std::error_code ec;
6893

69-
#ifndef _WIN32
7094
offset -= offset % page_size_;
7195

96+
#ifdef _WIN32
97+
if (::VirtualFree(mf_.data(), offset, MEM_DECOMMIT) == 0) {
98+
ec.assign(::GetLastError(), std::system_category());
99+
}
100+
#else
72101
if (::madvise(mf_.data(), offset, MADV_DONTNEED) != 0) {
73102
ec.assign(errno, std::generic_category());
74103
}
@@ -83,19 +112,13 @@ size_t mmap::size() const { return mf_.size(); }
83112

84113
mmap::mmap(const std::string& path)
85114
: mf_(path, boost::iostreams::mapped_file::readonly)
86-
#ifndef _WIN32
87-
, page_size_(::sysconf(_SC_PAGESIZE))
88-
#endif
89-
{
115+
, page_size_(get_page_size()) {
90116
assert(mf_.is_open());
91117
}
92118

93119
mmap::mmap(const std::string& path, size_t size)
94120
: mf_(path, boost::iostreams::mapped_file::readonly, size)
95-
#ifndef _WIN32
96-
, page_size_(::sysconf(_SC_PAGESIZE))
97-
#endif
98-
{
121+
, page_size_(get_page_size()) {
99122
assert(mf_.is_open());
100123
}
101124

0 commit comments

Comments
 (0)