From 01c1cf72c4cb7276a0d95dbbda00b84f8d101420 Mon Sep 17 00:00:00 2001 From: zilmar Date: Wed, 13 Jan 2016 20:39:23 +1100 Subject: [PATCH] [Common] Add MemoryManagement.cpp --- Source/Common/Common.vcproj | 8 ++++ Source/Common/Common.vcxproj | 2 + Source/Common/Common.vcxproj.filters | 6 +++ Source/Common/MemoryManagement.cpp | 58 ++++++++++++++++++++++++++++ Source/Common/MemoryManagement.h | 15 +++++++ 5 files changed, 89 insertions(+) create mode 100644 Source/Common/MemoryManagement.cpp create mode 100644 Source/Common/MemoryManagement.h diff --git a/Source/Common/Common.vcproj b/Source/Common/Common.vcproj index 29dab1788..ff469900a 100644 --- a/Source/Common/Common.vcproj +++ b/Source/Common/Common.vcproj @@ -145,6 +145,10 @@ RelativePath=".\md5.cpp" > + + @@ -214,6 +218,10 @@ RelativePath=".\md5.h" > + + diff --git a/Source/Common/Common.vcxproj b/Source/Common/Common.vcxproj index 2e55005e8..a6b548f6e 100644 --- a/Source/Common/Common.vcxproj +++ b/Source/Common/Common.vcxproj @@ -37,6 +37,7 @@ + @@ -54,6 +55,7 @@ + diff --git a/Source/Common/Common.vcxproj.filters b/Source/Common/Common.vcxproj.filters index 8f34ba6bc..7367aeb86 100644 --- a/Source/Common/Common.vcxproj.filters +++ b/Source/Common/Common.vcxproj.filters @@ -50,6 +50,9 @@ Source Files + + Source Files + @@ -100,5 +103,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Source/Common/MemoryManagement.cpp b/Source/Common/MemoryManagement.cpp new file mode 100644 index 000000000..a967a79c4 --- /dev/null +++ b/Source/Common/MemoryManagement.cpp @@ -0,0 +1,58 @@ +#include "stdafx.h" +#include +#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; +} diff --git a/Source/Common/MemoryManagement.h b/Source/Common/MemoryManagement.h new file mode 100644 index 000000000..9afac9e05 --- /dev/null +++ b/Source/Common/MemoryManagement.h @@ -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);