2015-08-26 15:18:01 +00:00
|
|
|
#include "stdafx.h"
|
2015-10-17 17:47:18 +00:00
|
|
|
#include "Utilities/Log.h"
|
2015-08-26 15:18:01 +00: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
|
|
|
|
{
|
2017-02-10 19:56:16 +00:00
|
|
|
void* reserve_memory(std::size_t size)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-08-15 10:18:47 +00:00
|
|
|
return verify("reserve_memory" HERE, VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS));
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2016-08-15 10:18:47 +00:00
|
|
|
return verify("reserve_memory" HERE, ::mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0));
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:56:16 +00:00
|
|
|
void commit_page_memory(void* pointer, std::size_t size)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-08-14 19:41:01 +00:00
|
|
|
verify(HERE), VirtualAlloc(pointer, size, MEM_COMMIT, PAGE_READWRITE);
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2017-02-10 19:56:16 +00:00
|
|
|
verify(HERE), ::mprotect((void*)((u64)pointer & -4096), ::align(size, 4096), PROT_READ | PROT_WRITE) != -1;
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:56:16 +00:00
|
|
|
void free_reserved_memory(void* pointer, std::size_t size)
|
2015-08-26 15:18:01 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-08-14 19:41:01 +00:00
|
|
|
verify(HERE), VirtualFree(pointer, 0, MEM_DECOMMIT);
|
2015-08-26 15:18:01 +00:00
|
|
|
#else
|
2017-02-10 19:56:16 +00:00
|
|
|
verify(HERE), ::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
|
2015-08-26 15:18:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|