Use VirtualAlloc() instead of mmap() on Windows

This commit is contained in:
Josh Holtrop 2017-01-14 09:48:54 -05:00
parent e65f269766
commit f7e077d8ef

View File

@ -1,9 +1,13 @@
#include "System.h" #include "System.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h>
#include <stdint.h> #include <stdint.h>
#include <fcntl.h> #include <fcntl.h>
#ifdef PLATFORM_WINDOWS
#include "windows.h"
#else
#include <sys/mman.h>
#endif
unsigned long System::page_size; unsigned long System::page_size;
unsigned int System::page_size_log; unsigned int System::page_size_log;
@ -63,18 +67,30 @@ bool System::init(char * argv[])
void * System::alloc_pages(int num) void * System::alloc_pages(int num)
{ {
#ifdef PLATFORM_WINDOWS
return VirtualAlloc(
NULL,
num << System::page_size_log,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
#else
return mmap( return mmap(
NULL, NULL,
num * System::page_size, num << System::page_size_log,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, MAP_PRIVATE | MAP_ANONYMOUS,
-1, -1,
0); 0);
#endif
} }
void System::free_pages(void * addr, int num) void System::free_pages(void * addr, int num)
{ {
#ifdef PLATFORM_WINDOWS
(void)VirtualFree(addr, 0, MEM_RELEASE);
#else
(void)munmap(addr, num << System::page_size_log); (void)munmap(addr, num << System::page_size_log);
#endif
} }
std::string System::executable_path() std::string System::executable_path()