-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapMem.cpp
104 lines (98 loc) · 3.25 KB
/
HeapMem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Heap memory allocation management
// Class to manage a large heap allocation and automatically deallocate,
// without raising exceptions on failure
#include <Windows.h>
#include <sstream>
#include "SysErrorMessage.h"
#include "HeapMem.h"
/// <summary>
/// Allocate a block of virtual memory from the current process heap.
/// Deallocates any previous allocated memory.
/// </summary>
/// <param name="nBytes">Input: number of bytes to allocate</param>
/// <param name="sErrorInfo">Output: information about any failure</param>
/// <returns>true if successful</returns>
bool HeapMem::Alloc(size_t nBytes, std::wstring& sErrorInfo)
{
// Init output variable
sErrorInfo.clear();
// Deallocate any previously allocated memory
if (!Dealloc(sErrorInfo))
return false;
// Get the process heap for the current process
HANDLE hProcessHeap = ProcessHeap(sErrorInfo);
if (nullptr == hProcessHeap)
return false;
// Allocate heap memory
m_pMem = HeapAlloc(hProcessHeap, 0, nBytes);
if (nullptr != m_pMem)
{
m_nSize = nBytes;
}
else
{
std::wstringstream strErrorInfo;
strErrorInfo << L"HeapAlloc failed to allocate " << nBytes << L" bytes";
sErrorInfo = strErrorInfo.str();
return false;
}
return true;
}
/// <summary>
/// Deallocate any previously allocated memory
/// </summary>
/// <param name="sErrorInfo">Output: information about any failure</param>
/// <returns>true if successful</returns>
bool HeapMem::Dealloc(std::wstring& sErrorInfo)
{
// Init output variable
sErrorInfo.clear();
if (nullptr != m_pMem)
{
// Get the process heap for the current process
HANDLE hProcessHeap = ProcessHeap(sErrorInfo);
if (nullptr == hProcessHeap)
return false;
// Deallocate the memory, clear pointer
BOOL ret = HeapFree(hProcessHeap, 0, m_pMem);
m_pMem = nullptr;
m_nSize = 0;
if (!ret)
{
DWORD dwLastErr = GetLastError();
std::wstringstream strErrorInfo;
strErrorInfo << L"HeapFree failed: " << SysErrorMessageWithCode(dwLastErr);
sErrorInfo = strErrorInfo.str();
return false;
}
}
return true;
}
/// <summary>
/// Deallocate any previously allocated memory. (No string output in case of failure.)
/// </summary>
/// <returns>true if successful</returns>
bool HeapMem::Dealloc()
{
std::wstring sUnused;
return Dealloc(sUnused);
}
/// <summary>
/// Returns a handle to the process heap, handling any error conditions
/// Note that this is not a handle to a kernel/executive object; do not call CloseHandle on it.
/// </summary>
/// <param name="sErrorInfo">Output: information about any failure</param>
/// <returns>Handle to process heap if successful; nullptr if unsuccessful</returns>
HANDLE HeapMem::ProcessHeap(std::wstring& sErrorInfo)
{
// API call to get a handle to the current process heap.
HANDLE hProcessHeap = GetProcessHeap();
if (nullptr == hProcessHeap)
{
DWORD dwLastErr = GetLastError();
std::wstringstream strErrorInfo;
strErrorInfo << L"GetProcessHeap failed: " << SysErrorMessageWithCode(dwLastErr);
sErrorInfo = strErrorInfo.str();
}
return hProcessHeap;
}