rpcs3/Utilities/VirtualMemory.cpp

46 lines
961 B
C++
Raw Normal View History

#include "stdafx.h"
2015-10-17 17:47:18 +00:00
#include "Utilities/Log.h"
#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
void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
2016-05-13 14:01:48 +00:00
ENSURES(ret != NULL);
#else
void* ret = mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
2016-05-13 14:01:48 +00:00
ENSURES(ret != 0);
#endif
return ret;
}
void commit_page_memory(void* pointer, size_t page_size)
{
#ifdef _WIN32
2016-05-13 14:01:48 +00:00
VERIFY(VirtualAlloc((u8*)pointer, page_size, MEM_COMMIT, PAGE_READWRITE) != NULL);
#else
2016-05-13 14:01:48 +00:00
VERIFY(mprotect((u8*)pointer, page_size, PROT_READ | PROT_WRITE) != -1);
#endif
}
void free_reserved_memory(void* pointer, size_t size)
{
#ifdef _WIN32
2016-05-13 14:01:48 +00:00
VERIFY(VirtualFree(pointer, 0, MEM_RELEASE) != 0);
#else
2016-05-13 14:01:48 +00:00
VERIFY(munmap(pointer, size) == 0);
#endif
}
}