1 // Heap.cpp : 定义控制台应用程序的入口点。 2 // 3
4 #include "stdafx.h"
5 #include <Windows.h>
6 #include <iostream>
7 #include <new>
8 using namespace std; 9
10 int* p = new int(10); 11
12
13 class CSomeClass 14 { 15 protected: 16 static size_t s_Counts; 17 static HANDLE s_hHeap; 18
19
20 public: 21 CSomeClass(); 22 CSomeClass(int value); 23 virtual ~CSomeClass(); 24
25 //重载的该运算符为该类的成员函数 26 //所以 这里的size_t参数分配的字节数不必显示指定,该参数为sizeof(该类)
27 void* operator new(size_t); 28 //void* operator new(size_t, const char* fileName, int line);
29 void* operator new[](size_t); 30
31 void operator delete(void* p); 32 void operator delete[](void* p); 33 //void operator delete(void* p, const char* fileName, int line);
34
35
36 int m_value; 37
38 }; 39
40 size_t CSomeClass::s_Counts = 0; 41 HANDLE CSomeClass::s_hHeap = NULL; 42 CSomeClass::CSomeClass(){} 43
44 CSomeClass::CSomeClass(int value) : m_value(value){} 45
46 CSomeClass::~CSomeClass(){} 47
48 void* CSomeClass::operator new(size_t size) 49 //void* CSomeClass::operator new(size_t size, const char* fileName, int line)
50 { 51 //cout<<endl<<fileName<<", line "<<line<<endl; 52
53 //如果是第一个实例,则创建堆
54 if (NULL == s_hHeap) 55 { 56 s_hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0); 57 if (NULL == s_hHeap) 58 { 59 return NULL; 60 } 61 } 62
63 void* p = HeapAlloc(s_hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size); 64 if (NULL != p) 65 { 66 ++s_Counts; 67 } 68
69 return p; 70 } 71
72 void* CSomeClass::operator new[](size_t size) 73 { 74 //size大小为 sizeof(CSomeClass) * 个数 + 额外信息的大小 75
76 //注意,这里这种做法可能会有问题,只有当额外信息的大小 小于 对象大小时才合理!!! 77
78 //nums为对象的个数
79 size_t nums = size / sizeof(CSomeClass); 80
81 //sizeOfExtra的大小为存储额外信息的空间大小
82 size_t sizeOfExtra = size % sizeof(CSomeClass); 83
84 if (NULL == s_hHeap) 85 { 86 s_hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0); 87 if (NULL == s_hHeap) 88 { 89 return NULL; 90 } 91 } 92
93 void* p = HeapAlloc(s_hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size); 94 if (NULL != p) 95 { 96 s_Counts += nums; 97 } 98
99 return p; 100 } 101
102
103 void CSomeClass::operator delete(void* p) 104 //void CSomeClass::operator delete(void* p, const char* fileName, int line)
105 { 106 if (NULL == p || NULL == s_hHeap) 107 { 108 return; 109 } 110
111 BOOL bRet = HeapFree(s_hHeap, 0, p); 112 if (FALSE != bRet) 113 { 114 if (--s_Counts == 0) 115 { 116 HeapDestroy(s_hHeap); 117 //CloseHandle(s_hHeap); //保留这一行 有异常 invalid handle
118 s_hHeap = NULL; 119 } 120 } 121 } 122
123 void CSomeClass::operator delete[](void* p) 124 { 125 if (NULL == p || NULL == s_hHeap) 126 { 127 return; 128 } 129
130 size_t size = HeapSize(s_hHeap, 0, p); 131 size_t nums = size / sizeof(CSomeClass); 132 size_t sizeOfExtra = size % sizeof(CSomeClass); 133
134 BOOL bRet = HeapFree(s_hHeap, 0, p); 135 if (FALSE != bRet) 136 { 137 if (0 == (s_Counts -= nums)) 138 { 139 HeapDestroy(s_hHeap); 140 s_hHeap = NULL; 141 } 142 } 143 } 144
145
146 #ifdef _DEBUG 147 //#define new new(__FILE__, __LINE__) 148 //#define delete delete(__FILE__, __LINE__)
149 #endif
150
151
152
153 int __cdecl _tmain(int argc, _TCHAR* argv[], _TCHAR* env[]) 154 { 155 HANDLE hHeaps[25] = {NULL}; 156 DWORD dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[0]), hHeaps); 157
158 CSomeClass* pAddr2 = new CSomeClass[10](); 159 dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[0]), hHeaps); 160 delete[] pAddr2; 161 dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[0]), hHeaps); 162
163 #pragma region 测试1
164 int size = sizeof(CSomeClass); 165 cout<<"sizeof(CSomeClass) is "<<size<<endl; 166
167 CSomeClass* pObj = new CSomeClass(size * 2); 168 pObj->m_value = 100; 169 CSomeClass* pObj2 = new CSomeClass(); 170 pObj2->m_value = 200; 171
172 delete pObj; 173 //delete pObj2;
174 #pragma endregion
175
176
177 CSomeClass* pArr = new CSomeClass[10]; 178
179
180 //UINT HeapCompact(HANDLE hHeap, DWORD fdwFlags) 181 //BOOL HeapLock(HANDLE hHeap); 182 //BOOL HeapUnLock(HANDLE hHeap); 183 //BOOL HeapWalk(HANDLE hHEap, PROCESS_HEAP_ENTRY pHeapEntry)
184 /*
185 BOOL bRet = FALSE; 186 PROCESS_HEAP_ENTRY phe; 187 phe.lpData = NULL; 188
189 HeapLock(hHeap); 190 while(TRUE == (bRet = HeapWalk(hHeap, 0, &phe)) 191 { 192 //do something 193 phe.lpData = NULL; 194 } 195
196 HeapUnLock(hHeap); 197
198 */
199
200
201
202
203 return 0; 204 } 205
206
207 /*
208 int _tmain(int argc, _TCHAR* argv[]) 209 { 210 HANDLE hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0); 211 if (NULL == hHeap) 212 { 213 cerr<<"Create heap error \n"; 214 return -1; 215 } 216
217 //BOOL bRet = HeapSetInformation(hHeap, HeapEnableTerminationOnCorruption, NULL, 0); 218
219 ULONG HeapInformationValue = 2; 220 BOOL bRet = HeapSetInformation( 221 hHeap, HeapCompatibilityInformation, &HeapInformationValue, sizeof(HeapInformationValue)); 222
223
224 size_t size = 100; 225 PVOID pAddr = HeapAlloc(hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size); 226 if(NULL == pAddr) 227 { 228 HeapFree(hHeap, 0, pAddr); //释放申请的内存快 229 HeapDestroy(hHeap); //销毁堆 230 CloseHandle(hHeap); //关闭句柄 231 cerr<<"HeapAlloc error \n"; 232 return -2; 233 } 234
235 *((int*)(pAddr)) = 100; 236 cout<<"int pAddr is "<<*((int*)(pAddr))<<endl; 237
238 cout<<"alloc size of heap is "<<HeapSize(hHeap, 0, pAddr)<<endl; 239
240 PVOID pAddr2 = HeapReAlloc(hHeap, HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY | HEAP_REALLOC_IN_PLACE_ONLY, 241 pAddr, size * 20); 242 if (NULL == pAddr2) 243 { 244 HeapFree(hHeap, 0, pAddr); 245 HeapDestroy(hHeap); 246 CloseHandle(hHeap); 247 cerr<<"HeapReAlloc error \n"; 248
249 return -3; 250 } 251 cout<<"int pAddr2 is "<<*((int*)(pAddr2) + 1)<<endl; 252
253 cout<<"Realloc size of heap is "<<HeapSize(hHeap, 0, pAddr2)<<endl; 254
255 HeapFree(hHeap, 0, pAddr2); 256 HeapDestroy(hHeap); 257 CloseHandle(hHeap); 258 cout<<"Heap opera is finish "<<endl; 259
260
261 return 0; 262 } 263
264 */