Changeset 34
- Timestamp:
- Nov 5, 2011, 12:19:24 PM (12 years ago)
- Location:
- trunk/Utilities/VirtualHeapPtr
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Utilities/VirtualHeapPtr
- Property svn:ignore
-
old new 4 4 *.suo 5 5 *.user 6 *.opensdf
-
- Property svn:ignore
-
trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.cpp
r33 r34 6 6 7 7 #include "stdafx.h" 8 #include <psapi.h> 9 10 #pragma comment(lib, "psapi.lib") 11 12 //////////////////////////////////////////////////////////// 13 // CGlobalVirtualAllocator 14 15 class CGlobalVirtualAllocator 16 { 17 private: 18 SIZE_T m_nGranularity; 19 20 public: 21 // CGlobalVirtualAllocator 22 CGlobalVirtualAllocator() throw() : 23 m_nGranularity(4 << 10) // 4L 24 { 25 PERFORMANCE_INFORMATION Information; 26 ZeroMemory(&Information, sizeof Information); 27 ATLVERIFY(GetPerformanceInfo(&Information, sizeof Information)); 28 m_nGranularity = Information.PageSize; 29 ATLASSERT(m_nGranularity); 30 ATLASSERT(!(m_nGranularity & (m_nGranularity - 1))); 31 } 32 SIZE_T GetGranularity() const throw() 33 { 34 return m_nGranularity; 35 } 36 }; 37 38 CGlobalVirtualAllocator g_GlobalVirtualAllocator; 39 40 //////////////////////////////////////////////////////////// 41 // CVirtualAllocator 42 43 class CVirtualAllocator 44 { 45 public: 46 // CVirtualAllocator 47 static SIZE_T Align(SIZE_T nDataSize) throw() 48 { 49 const SIZE_T nGranularity = g_GlobalVirtualAllocator.GetGranularity(); 50 ATLASSERT(nGranularity); 51 ATLASSERT(!(nGranularity & (nGranularity - 1))); 52 return (nDataSize + nGranularity - 1) & ~(nGranularity - 1); 53 } 54 static VOID* Reallocate(_In_opt_ VOID* pvData, _In_ SIZE_T nDataSize) 55 { 56 MEMORY_BASIC_INFORMATION DataInformation; 57 if(pvData) 58 { 59 ATLVERIFY(VirtualQuery(pvData, &DataInformation, sizeof DataInformation)); 60 if(nDataSize <= DataInformation.RegionSize) 61 return pvData; 62 } 63 VOID* pvNewData = NULL; 64 if(nDataSize) 65 { 66 pvNewData = VirtualAlloc(NULL, Align(nDataSize), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); 67 ATLENSURE_THROW(pvNewData, AtlHresultFromLastError()); 68 _ATLTRY 69 { 70 ATLENSURE_THROW(VirtualLock(pvNewData, nDataSize), AtlHresultFromLastError()); 71 if(pvNewData && pvData) 72 { 73 ATLASSERT(DataInformation.AllocationProtect == PAGE_READWRITE); 74 SIZE_T nCopyDataSize = nDataSize; 75 if(nCopyDataSize > DataInformation.RegionSize) 76 nCopyDataSize = DataInformation.RegionSize; 77 Checked::memcpy_s(pvNewData, nDataSize, pvData, nCopyDataSize); 78 } 79 } 80 _ATLCATCHALL() 81 { 82 Free(pvData); 83 _ATLRETHROW; 84 } 85 } 86 if(pvData) 87 Free(pvData); 88 return pvNewData; 89 } 90 static VOID* Allocate(_In_ SIZE_T nDataSize) 91 { 92 if(!nDataSize) 93 return NULL; 94 VOID* pvData = VirtualAlloc(NULL, Align(nDataSize), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); 95 ATLENSURE_THROW(pvData, AtlHresultFromLastError()); 96 _ATLTRY 97 { 98 ATLENSURE_THROW(VirtualLock(pvData, nDataSize), AtlHresultFromLastError()); 99 } 100 _ATLCATCHALL() 101 { 102 Free(pvData); 103 _ATLRETHROW; 104 } 105 return pvData; 106 } 107 static VOID Free(_In_opt_ VOID* pvData) throw() 108 { 109 if(!pvData) 110 return; 111 //VirtualUnlock(pvData, 112 ATLVERIFY(VirtualFree(pvData, 0, MEM_RELEASE)); 113 } 114 }; 115 116 //////////////////////////////////////////////////////////// 117 // CVirtualHeapPtr 118 119 template <typename T> 120 class CVirtualHeapPtr : 121 public CHeapPtr<T, CVirtualAllocator> 122 { 123 public: 124 // CVirtualHeapPtr 125 CVirtualHeapPtr() throw() 126 { 127 } 128 explicit CVirtualHeapPtr(_In_ T* pData) throw() : 129 CHeapPtr<T, CVirtualAllocator>(pData) 130 { 131 } 132 VOID SetProtection(DWORD nProtection) 133 { 134 if(!m_pData) 135 return; 136 MEMORY_BASIC_INFORMATION DataInformation; 137 ATLENSURE_THROW(VirtualQuery(m_pData, &DataInformation, sizeof DataInformation), AtlHresultFromLastError()); 138 DWORD nCurrentProtection; 139 ATLENSURE_THROW(VirtualProtect(m_pData, DataInformation.RegionSize, nProtection, &nCurrentProtection), AtlHresultFromLastError()); 140 } 141 }; 8 #include "VirtualHeapPtr.h" 142 9 143 10 //////////////////////////////////////////////////////////// … … 146 13 int _tmain(int argc, _TCHAR* argv[]) 147 14 { 148 CVirtualHeapPtr<BYTE> p; 15 _tprintf(_T("g_GlobalVirtualAllocator.GetPageSize() %d\n\n"), g_GlobalVirtualAllocator.GetPageSize()); 16 CDebugHeapPtr<BYTE> p; 149 17 p.Allocate(1); 150 18 p[0] = 0x01; … … 155 23 p[0] = 0x03; 156 24 _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); 157 if(FALSE) 25 #pragma region PAGE_READONLY 26 //if(FALSE) 158 27 { 159 28 p.SetProtection(PAGE_READONLY); … … 165 34 _ATLCATCHALL() 166 35 { 167 _tprintf(_T("Oopsie in line %d \n"), __LINE__);36 _tprintf(_T("Oopsie in line %d (Failed to write to PAGE_READONLY memory)\n"), __LINE__); 168 37 } 169 38 p.SetProtection(PAGE_READWRITE); … … 171 40 _tprintf(_T("p[0] 0x%02X, p[1] 0x%02X\n"), p[0], p[1]); 172 41 } 42 #pragma endregion 43 volatile BYTE n; 173 44 p.SetProtection(PAGE_READWRITE | PAGE_GUARD); 174 volatile BYTE n;45 #pragma region PAGE_GUARD 175 46 _ATLTRY 176 47 { … … 179 50 _ATLCATCHALL() 180 51 { 181 _tprintf(_T("Oopsie in line %d \n"), __LINE__);52 _tprintf(_T("Oopsie in line %d (First PAGE_GUARD access)\n"), __LINE__); 182 53 } 183 54 _ATLTRY … … 187 58 _ATLCATCHALL() 188 59 { 189 _tprintf(_T("Oopsie in line %d \n"), __LINE__);60 _tprintf(_T("Oopsie in line %d (Second PAGE_GUARD access)\n"), __LINE__); 190 61 } 62 #pragma endregion 63 #pragma region Invalid Index 64 _ATLTRY 65 { 66 n = p[-1]; 67 } 68 _ATLCATCHALL() 69 { 70 _tprintf(_T("Oopsie in line %d (Reading with a too small index)\n"), __LINE__); 71 } 72 _ATLTRY 73 { 74 p[-1] = n; 75 } 76 _ATLCATCHALL() 77 { 78 _tprintf(_T("Oopsie in line %d (Writing with a too small index)\n"), __LINE__); 79 } 80 _ATLTRY 81 { 82 n = p[2]; 83 } 84 _ATLCATCHALL() 85 { 86 _tprintf(_T("Oopsie in line %d (Reading with a too large index)\n"), __LINE__); 87 } 88 _ATLTRY 89 { 90 p[2] = n; 91 } 92 _ATLCATCHALL() 93 { 94 _tprintf(_T("Oopsie in line %d (Writing with a too large index)\n"), __LINE__); 95 } 96 #pragma endregion 191 97 return 0; 192 98 } -
trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.vcxproj
r33 r34 83 83 <ClInclude Include="stdafx.h" /> 84 84 <ClInclude Include="targetver.h" /> 85 <ClInclude Include="VirtualHeapPtr.h" /> 85 86 </ItemGroup> 86 87 <ItemGroup> -
trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.vcxproj.filters
r33 r34 25 25 <Filter>Header Files</Filter> 26 26 </ClInclude> 27 <ClInclude Include="VirtualHeapPtr.h"> 28 <Filter>Header Files</Filter> 29 </ClInclude> 27 30 </ItemGroup> 28 31 <ItemGroup>
Note: See TracChangeset
for help on using the changeset viewer.