26 static bool mmap_to_page_protection(
int prot, DWORD& ret, DWORD& acc) {
27 if (prot == PROT_NONE) {
30 }
else if (prot == PROT_READ) {
33 }
else if (prot == PROT_EXEC) {
35 acc = FILE_MAP_EXECUTE;
36 }
else if (prot == (PROT_READ | PROT_EXEC)) {
37 ret = PAGE_EXECUTE_READ;
38 acc = FILE_MAP_READ | FILE_MAP_EXECUTE;
39 }
else if (prot == (PROT_READ | PROT_WRITE)) {
41 acc = FILE_MAP_READ | FILE_MAP_WRITE;
42 }
else if (prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) {
43 ret = PAGE_EXECUTE_READWRITE;
44 acc = FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_EXECUTE;
51 static size_t alignToAllocationGranularity(
size_t s) {
52 static size_t granularity = [] {
53 static SYSTEM_INFO inf;
55 return inf.dwAllocationGranularity;
57 return (s + granularity - 1) / granularity * granularity;
61 int madvise(
const void* ,
size_t ,
int ) {
68 int mlock(
const void*
addr,
size_t len) {
73 if (addr ==
nullptr && len == 0) {
76 if (!VirtualLock((
void*)addr, len)) {
83 constexpr
uint32_t kMMapLengthMagic = 0xFACEB00C;
84 struct MemMapDebugTrailer {
90 void* mmap(
void* addr,
size_t length,
int prot,
int flags,
int fd, off_t off) {
94 if ((flags & (MAP_ANONYMOUS | MAP_SHARED)) == (MAP_ANONYMOUS | MAP_SHARED)) {
98 if ((flags & MAP_PRIVATE) == MAP_PRIVATE && fd != -1) {
102 if (!(flags & MAP_ANONYMOUS) && fd == -1) {
108 if (!mmap_to_page_protection(prot, newProt, accessFlags)) {
113 if (!(flags & MAP_ANONYMOUS) || (flags & MAP_SHARED)) {
114 HANDLE
h = INVALID_HANDLE_VALUE;
115 if (!(flags & MAP_ANONYMOUS)) {
116 h = (HANDLE)_get_osfhandle(fd);
119 HANDLE fmh = CreateFileMapping(
123 (DWORD)((length >> 32) & 0xFFFFFFFF),
124 (DWORD)(length & 0xFFFFFFFF),
126 if (fmh ==
nullptr) {
129 ret = MapViewOfFileEx(
133 (DWORD)(off & 0xFFFFFFFF),
136 if (ret ==
nullptr) {
141 auto baseLength = length;
146 length +=
sizeof(MemMapDebugTrailer);
151 length = alignToAllocationGranularity(length);
152 ret = VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, newProt);
153 if (ret ==
nullptr) {
158 auto deb = (MemMapDebugTrailer*)((
char*)ret + baseLength);
159 deb->length = baseLength;
160 deb->magic = kMMapLengthMagic;
169 int mprotect(
void* addr,
size_t size,
int prot) {
172 if (!mmap_to_page_protection(prot, newProt, access)) {
177 BOOL res = VirtualProtect(addr, size, newProt, &oldProt);
184 int munlock(
const void* addr,
size_t length) {
186 if (addr ==
nullptr && length == 0) {
189 if (!VirtualUnlock((
void*)addr, length)) {
195 int munmap(
void* addr,
size_t length) {
197 if (!UnmapViewOfFile(addr)) {
202 MEMORY_BASIC_INFORMATION inf;
203 VirtualQuery(addr, &inf,
sizeof(inf));
204 assert(inf.AllocationBase == addr);
206 auto deb = (MemMapDebugTrailer*)((
char*)addr + length);
207 assert(deb->length == length);
208 assert(deb->magic == kMMapLengthMagic);
210 if (!VirtualFree(addr, 0, MEM_RELEASE)) {
constexpr auto size(C const &c) -> decltype(c.size())
ThreadPoolListHook * addr