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
|
|
|
|
|
2017-03-13 10:40:54 +01:00
|
|
|
namespace utils
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
2017-03-19 13:50:56 +01:00
|
|
|
// Convert memory protection (internal)
|
|
|
|
static auto operator +(protection prot)
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-03-19 13:50:56 +01:00
|
|
|
DWORD _prot = PAGE_NOACCESS;
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
case protection::rw: _prot = PAGE_READWRITE; break;
|
|
|
|
case protection::ro: _prot = PAGE_READONLY; break;
|
|
|
|
case protection::no: break;
|
|
|
|
case protection::wx: _prot = PAGE_EXECUTE_READWRITE; break;
|
|
|
|
case protection::rx: _prot = PAGE_EXECUTE_READ; break;
|
|
|
|
}
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2017-03-19 13:50:56 +01:00
|
|
|
int _prot = PROT_NONE;
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
case protection::rw: _prot = PROT_READ | PROT_WRITE; break;
|
|
|
|
case protection::ro: _prot = PROT_READ; break;
|
|
|
|
case protection::no: break;
|
|
|
|
case protection::wx: _prot = PROT_READ | PROT_WRITE | PROT_EXEC; break;
|
|
|
|
case protection::rx: _prot = PROT_READ | PROT_EXEC; break;
|
|
|
|
}
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
2017-03-19 13:50:56 +01:00
|
|
|
|
|
|
|
return _prot;
|
2015-08-26 17:18:01 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 13:50:56 +01:00
|
|
|
void* memory_reserve(std::size_t size, void* use_addr)
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-03-19 13:50:56 +01:00
|
|
|
return verify("reserve_memory" HERE, ::VirtualAlloc(use_addr, size, MEM_RESERVE, PAGE_NOACCESS));
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2017-03-19 13:50:56 +01:00
|
|
|
return verify("reserve_memory" HERE, ::mmap(use_addr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE | (use_addr ? MAP_FIXED : 0), -1, 0));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_commit(void* pointer, std::size_t size, protection prot)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
verify(HERE), ::VirtualAlloc(pointer, size, MEM_COMMIT, +prot);
|
|
|
|
#else
|
|
|
|
verify(HERE), ::mprotect((void*)((u64)pointer & -4096), ::align(size, 4096), +prot) != -1;
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-13 10:40:54 +01:00
|
|
|
void memory_decommit(void* pointer, std::size_t size)
|
2015-08-26 17:18:01 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-03-13 10:40:54 +01:00
|
|
|
verify(HERE), ::VirtualFree(pointer, 0, MEM_DECOMMIT);
|
2015-08-26 17:18:01 +02:00
|
|
|
#else
|
2017-02-10 20:56:16 +01:00
|
|
|
verify(HERE), ::mmap(pointer, size, PROT_NONE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
|
2017-03-13 10:40:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_protect(void* pointer, std::size_t size, protection prot)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD old;
|
2017-03-19 13:50:56 +01:00
|
|
|
verify(HERE), ::VirtualProtect(pointer, size, +prot, &old);
|
2017-03-13 10:40:54 +01:00
|
|
|
#else
|
2017-03-19 13:50:56 +01:00
|
|
|
verify(HERE), ::mprotect((void*)((u64)pointer & -4096), ::align(size, 4096), +prot) != -1;
|
2015-08-26 17:18:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-26 22:46:04 +02:00
|
|
|
}
|