2015-08-26 17:18:01 +02:00
|
|
|
#include "stdafx.h"
|
2015-10-17 19:47:18 +02:00
|
|
|
#include "Utilities/Log.h"
|
2015-08-26 17:18:01 +02:00
|
|
|
#include "VirtualMemory.h"
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace memory_helper
|
|
|
|
{
|
|
|
|
void* reserve_memory(size_t size)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2015-10-23 16:42:34 +02:00
|
|
|
void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
|
2016-05-13 16:01:48 +02:00
|
|
|
ENSURES(ret != NULL);
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2015-10-23 16:42:34 +02:00
|
|
|
void* ret = mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
2016-05-13 16:01:48 +02:00
|
|
|
ENSURES(ret != 0);
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
2015-10-23 16:42:34 +02:00
|
|
|
return ret;
|
2015-08-26 17:18:01 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 18:34:08 +02:00
|
|
|
void commit_page_memory(void* pointer, size_t size)
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-27 18:34:08 +02:00
|
|
|
VERIFY(VirtualAlloc(pointer, size, MEM_COMMIT, PAGE_READWRITE) != NULL);
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2016-06-27 18:34:08 +02:00
|
|
|
VERIFY(mprotect((void*)((u64)pointer & -4096), size, PROT_READ | PROT_WRITE) != -1);
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-08 12:42:41 +01:00
|
|
|
void free_reserved_memory(void* pointer, size_t size)
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-27 18:34:08 +02:00
|
|
|
VERIFY(VirtualFree(pointer, 0, MEM_DECOMMIT) != 0);
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2016-06-27 18:34:08 +02:00
|
|
|
VERIFY(mprotect(pointer, size, PROT_NONE) != -1);
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-26 22:46:04 +02:00
|
|
|
}
|