[Common] Cleanup MemoryManagement.cpp
This commit is contained in:
parent
03b032c939
commit
ec03eb642a
|
@ -2,38 +2,38 @@
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "MemoryManagement.h"
|
#include "MemoryManagement.h"
|
||||||
|
|
||||||
static bool TranslateFromMemProtect ( MEM_PROTECTION memProtection, int & OsMemProtection)
|
static bool TranslateFromMemProtect(MEM_PROTECTION memProtection, int & OsMemProtection)
|
||||||
{
|
{
|
||||||
switch (memProtection)
|
switch (memProtection)
|
||||||
{
|
{
|
||||||
case MEM_NOACCESS: OsMemProtection = PAGE_NOACCESS; break;
|
case MEM_NOACCESS: OsMemProtection = PAGE_NOACCESS; break;
|
||||||
case MEM_READONLY: OsMemProtection = PAGE_READONLY; break;
|
case MEM_READONLY: OsMemProtection = PAGE_READONLY; break;
|
||||||
case MEM_READWRITE: OsMemProtection = PAGE_READWRITE; break;
|
case MEM_READWRITE: OsMemProtection = PAGE_READWRITE; break;
|
||||||
case MEM_EXECUTE_READWRITE: OsMemProtection = PAGE_EXECUTE_READWRITE; break;
|
case MEM_EXECUTE_READWRITE: OsMemProtection = PAGE_EXECUTE_READWRITE; break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* AllocateAddressSpace(size_t size)
|
void* AllocateAddressSpace(size_t size)
|
||||||
{
|
{
|
||||||
return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS);
|
return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FreeAddressSpace(void* addr, size_t size)
|
bool FreeAddressSpace(void* addr, size_t size)
|
||||||
{
|
{
|
||||||
return VirtualFree(addr, 0, MEM_RELEASE) != 0;
|
return VirtualFree(addr, 0, MEM_RELEASE) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* CommitMemory(void* addr, size_t size, MEM_PROTECTION memProtection)
|
void* CommitMemory(void* addr, size_t size, MEM_PROTECTION memProtection)
|
||||||
{
|
{
|
||||||
int OsMemProtection;
|
int OsMemProtection;
|
||||||
if (!TranslateFromMemProtect(memProtection,OsMemProtection))
|
if (!TranslateFromMemProtect(memProtection, OsMemProtection))
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return VirtualAlloc(addr, size, MEM_COMMIT, OsMemProtection);
|
return VirtualAlloc(addr, size, MEM_COMMIT, OsMemProtection);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DecommitMemory(void* addr, size_t size)
|
bool DecommitMemory(void* addr, size_t size)
|
||||||
|
@ -43,16 +43,16 @@ bool DecommitMemory(void* addr, size_t size)
|
||||||
|
|
||||||
bool ProtectMemory(void* addr, size_t size, MEM_PROTECTION memProtection, MEM_PROTECTION * OldProtect)
|
bool ProtectMemory(void* addr, size_t size, MEM_PROTECTION memProtection, MEM_PROTECTION * OldProtect)
|
||||||
{
|
{
|
||||||
int OsMemProtection;
|
int OsMemProtection;
|
||||||
if (!TranslateFromMemProtect(memProtection,OsMemProtection))
|
if (!TranslateFromMemProtect(memProtection, OsMemProtection))
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD OldOsProtect;
|
DWORD OldOsProtect;
|
||||||
BOOL res = VirtualProtect(addr, size, OsMemProtection, &OldOsProtect);
|
BOOL res = VirtualProtect(addr, size, OsMemProtection, &OldOsProtect);
|
||||||
if (OldProtect != NULL)
|
if (OldProtect != NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
return res != 0;
|
return res != 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue