1 #ifndef __ATLMEM_H__ 2 #define __ATLMEM_H__ 3 4 #pragma once 5 #include "atlcore.h" 6 7 8 namespace ATL 9 { 10 11 //__interface __declspec(uuid("654F7EF5-CFDF-4df9-A450-6C6A13C622C0")) 12 class IAtlMemMgr 13 { 14 public: 15 virtual ~IAtlMemMgr() {}; 16 17 virtual _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) void* Allocate( 18 _In_ size_t SizeBytes 19 ) = 0; 20 21 virtual void Free( 22 _Inout_opt_ void* Buffer 23 ) = 0; 24 25 virtual _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) void* Reallocate( 26 _Inout_updates_bytes_opt_(SizeBytes) void* Buffer, 27 _In_ size_t SizeBytes 28 ) = 0; 29 30 virtual size_t GetSize( 31 _In_ void* Buffer 32 ) = 0; 33 }; 34 35 class CWin32Heap : public IAtlMemMgr 36 { 37 public: 38 HANDLE m_hHeap; 39 40 public: 41 CWin32Heap() : 42 m_hHeap(NULL) 43 { 44 } 45 46 CWin32Heap(_In_ HANDLE hHeap) : 47 m_hHeap(hHeap) 48 { 49 ATLASSERT(hHeap != NULL); 50 } 51 52 virtual ~CWin32Heap() 53 { 54 } 55 56 57 // IAtlMemMgr 58 _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) virtual void* Allocate( 59 _In_ size_t SizeBytes 60 ) 61 { 62 return ::HeapAlloc(m_hHeap, 0, SizeBytes); 63 } 64 65 virtual void Free( 66 _In_opt_ void* Buffer 67 ) 68 { 69 if (Buffer) 70 { 71 if (!::HeapFree(m_hHeap, 0, Buffer)) 72 ATLASSERT(FALSE); 73 } 74 } 75 76 _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) virtual void* Reallocate( 77 _In_opt_ void* Buffer, 78 _In_ size_t SizeBytes 79 ) 80 { 81 if (SizeBytes == 0) 82 { 83 Free(Buffer); 84 return NULL; 85 } 86 87 if (Buffer == NULL) 88 { 89 return Allocate(SizeBytes); 90 } 91 92 return ::HeapReAlloc(m_hHeap, 0, Buffer, SizeBytes); 93 } 94 95 virtual size_t GetSize( 96 _Inout_ void* Buffer 97 ) 98 { 99 return ::HeapSize(m_hHeap, 0, Buffer); 100 } 101 }; 102 103 } 104 105 #endif 106