[Common] Add MemoryManagement.cpp
This commit is contained in:
parent
58f3502bd5
commit
01c1cf72c4
|
@ -145,6 +145,10 @@
|
||||||
RelativePath=".\md5.cpp"
|
RelativePath=".\md5.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\MemoryManagement.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\MemTest.cpp"
|
RelativePath=".\MemTest.cpp"
|
||||||
>
|
>
|
||||||
|
@ -214,6 +218,10 @@
|
||||||
RelativePath=".\md5.h"
|
RelativePath=".\md5.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\MemoryManagement.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\MemTest.h"
|
RelativePath=".\MemTest.h"
|
||||||
>
|
>
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
<ClCompile Include="IniFileClass.cpp" />
|
<ClCompile Include="IniFileClass.cpp" />
|
||||||
<ClCompile Include="LogClass.cpp" />
|
<ClCompile Include="LogClass.cpp" />
|
||||||
<ClCompile Include="md5.cpp" />
|
<ClCompile Include="md5.cpp" />
|
||||||
|
<ClCompile Include="MemoryManagement.cpp" />
|
||||||
<ClCompile Include="MemTest.cpp" />
|
<ClCompile Include="MemTest.cpp" />
|
||||||
<ClCompile Include="path.cpp" />
|
<ClCompile Include="path.cpp" />
|
||||||
<ClCompile Include="Platform.cpp" />
|
<ClCompile Include="Platform.cpp" />
|
||||||
|
@ -54,6 +55,7 @@
|
||||||
<ClInclude Include="IniFileClass.h" />
|
<ClInclude Include="IniFileClass.h" />
|
||||||
<ClInclude Include="LogClass.h" />
|
<ClInclude Include="LogClass.h" />
|
||||||
<ClInclude Include="md5.h" />
|
<ClInclude Include="md5.h" />
|
||||||
|
<ClInclude Include="MemoryManagement.h" />
|
||||||
<ClInclude Include="MemTest.h" />
|
<ClInclude Include="MemTest.h" />
|
||||||
<ClInclude Include="path.h" />
|
<ClInclude Include="path.h" />
|
||||||
<ClInclude Include="Platform.h" />
|
<ClInclude Include="Platform.h" />
|
||||||
|
|
|
@ -50,6 +50,9 @@
|
||||||
<ClCompile Include="Platform.cpp">
|
<ClCompile Include="Platform.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="MemoryManagement.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="stdafx.h">
|
<ClInclude Include="stdafx.h">
|
||||||
|
@ -100,5 +103,8 @@
|
||||||
<ClInclude Include="Platform.h">
|
<ClInclude Include="Platform.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="MemoryManagement.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include "stdafx.h"
|
||||||
|
#include <windows.h>
|
||||||
|
#include "MemoryManagement.h"
|
||||||
|
|
||||||
|
static bool TranslateFromMemProtect ( MEM_PROTECTION memProtection, int & OsMemProtection)
|
||||||
|
{
|
||||||
|
switch (memProtection)
|
||||||
|
{
|
||||||
|
case MEM_NOACCESS: OsMemProtection = PAGE_NOACCESS; break;
|
||||||
|
case MEM_READONLY: OsMemProtection = PAGE_READONLY; break;
|
||||||
|
case MEM_READWRITE: OsMemProtection = PAGE_READWRITE; break;
|
||||||
|
case MEM_EXECUTE_READWRITE: OsMemProtection = PAGE_EXECUTE_READWRITE; break;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* AllocateAddressSpace(size_t size)
|
||||||
|
{
|
||||||
|
return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FreeAddressSpace(void* addr, size_t size)
|
||||||
|
{
|
||||||
|
return VirtualFree(addr, 0, MEM_RELEASE) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* CommitMemory(void* addr, size_t size, MEM_PROTECTION memProtection)
|
||||||
|
{
|
||||||
|
int OsMemProtection;
|
||||||
|
if (!TranslateFromMemProtect(memProtection,OsMemProtection))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return VirtualAlloc(addr, size, MEM_COMMIT, OsMemProtection);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DecommitMemory(void* addr, size_t size)
|
||||||
|
{
|
||||||
|
return VirtualFree((void*)addr, size, MEM_DECOMMIT) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ProtectMemory(void* addr, size_t size, MEM_PROTECTION memProtection, MEM_PROTECTION * OldProtect)
|
||||||
|
{
|
||||||
|
int OsMemProtection;
|
||||||
|
if (!TranslateFromMemProtect(memProtection,OsMemProtection))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD OldOsProtect;
|
||||||
|
BOOL res = VirtualProtect(addr, size, OsMemProtection, &OldOsProtect);
|
||||||
|
if (OldProtect != NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return res != 0;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum MEM_PROTECTION
|
||||||
|
{
|
||||||
|
MEM_NOACCESS,
|
||||||
|
MEM_READONLY,
|
||||||
|
MEM_READWRITE,
|
||||||
|
MEM_EXECUTE_READWRITE,
|
||||||
|
};
|
||||||
|
|
||||||
|
void* AllocateAddressSpace(size_t size);
|
||||||
|
bool FreeAddressSpace(void* addr, size_t size);
|
||||||
|
void* CommitMemory(void* addr, size_t size, MEM_PROTECTION memProtection);
|
||||||
|
bool DecommitMemory(void* addr, size_t size);
|
||||||
|
bool ProtectMemory(void* addr, size_t size, MEM_PROTECTION memProtection, MEM_PROTECTION * OldProtect = NULL);
|
Loading…
Reference in New Issue