23 #include "precompiled.h"
42 return PAGE_READWRITE;
46 return PAGE_READWRITE;
48 return PAGE_EXECUTE_READ;
51 return PAGE_EXECUTE_READWRITE;
53 return PAGE_EXECUTE_READWRITE;
71 const BOOL ok = VirtualProtect(addr, len, newProtect, &oldProtect);
78 static Status mmap_mem(
void* start,
size_t len,
int prot,
int flags,
int fd,
void** pp)
92 if(!want_commit && start != 0 && flags &
MAP_FIXED)
94 MEMORY_BASIC_INFORMATION mbi;
95 if(!VirtualQuery(start, &mbi,
sizeof(mbi)))
97 if(mbi.State == MEM_COMMIT)
107 const DWORD allocationType = want_commit? MEM_COMMIT : MEM_RESERVE;
109 void* p = VirtualAlloc(start, len, allocationType, protect);
112 debug_printf(L
"wmman: VirtualAlloc(%p, 0x%I64X) failed\n", start, len);
140 protect = PAGE_READWRITE;
141 access = FILE_MAP_WRITE;
145 protect = PAGE_WRITECOPY;
146 access = FILE_MAP_COPY;
151 protect = PAGE_READONLY;
152 access = FILE_MAP_READ;
166 if(hFile == INVALID_HANDLE_VALUE)
180 const HANDLE hMap = CreateFileMapping(hFile, 0, protect, 0, 0, 0);
183 void* p = MapViewOfFileEx(hMap, access,
u64_hi(ofs),
u64_lo(ofs), (SIZE_T)len, start);
185 ENSURE(!(flags & MAP_FIXED) || (p == start));
202 void*
mmap(
void* start,
size_t len,
int prot,
int flags,
int fd,
off_t ofs)
209 status =
mmap_mem(start, len, prot, flags, fd, &p);
211 status =
mmap_file(start, len, prot, flags, fd, ofs, &p);
226 BOOL ok = UnmapViewOfFile(start);
229 ok = VirtualFree(start, 0, MEM_RELEASE);
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static Status mmap_file(void *start, size_t len, int prot, int flags, int fd, off_t ofs, void **pp)
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
int mprotect(void *addr, size_t len, int prot)
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
u32 u64_lo(u64 x)
return upper 32-bits
const Status INVALID_HANDLE
static Status DecodeFlags(int prot, int flags, DWORD &protect, DWORD &access)
int ErrnoFromStatus(Status status)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
void * mmap(void *start, size_t len, int prot, int flags, int fd, off_t ofs)
const Status INVALID_PARAM
i64 Status
Error handling system.
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
#define WARN_IF_FALSE(expression)
const Status INVALID_FLAG
unsigned MemoryProtectionFromPosix(int prot)
#define WARN_RETURN(status)
static Status mmap_mem(void *start, size_t len, int prot, int flags, int fd, void **pp)
HANDLE HANDLE_from_intptr(intptr_t i)
int munmap(void *start, size_t len)
#define cassert(expr)
Compile-time assertion.
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
#define RETURN_STATUS_IF_ERR(expression)