1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-23 03:02:53 +01:00
rpcs3/Utilities/VirtualMemory.cpp

43 lines
983 B
C++
Raw Normal View History

#include "stdafx.h"
2015-10-17 19:47:18 +02: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
2016-08-14 21:41:01 +02:00
return verify(VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS), "reserve_memory" HERE);
#else
2016-08-14 21:41:01 +02:00
return verify(::mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0), "reserve_memory" HERE);
#endif
}
2016-06-27 18:34:08 +02:00
void commit_page_memory(void* pointer, size_t size)
{
#ifdef _WIN32
2016-08-14 21:41:01 +02:00
verify(HERE), VirtualAlloc(pointer, size, MEM_COMMIT, PAGE_READWRITE);
#else
2016-08-14 21:41:01 +02:00
verify(HERE), ::mprotect((void*)((u64)pointer & -4096), size, PROT_READ | PROT_WRITE) != -1;
#endif
}
void free_reserved_memory(void* pointer, size_t size)
{
#ifdef _WIN32
2016-08-14 21:41:01 +02:00
verify(HERE), VirtualFree(pointer, 0, MEM_DECOMMIT);
#else
2016-08-14 21:41:01 +02:00
verify(HERE), ::mprotect(pointer, size, PROT_NONE) != -1;
#endif
}
}