【Windows核心编程】重载类成员函数new / new[] / delete / delete[]

时间:2022-04-27 08:45:20
 // Heap.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <new>
using namespace std; int* p = new int(); class CSomeClass
{
protected:
static size_t s_Counts;
static HANDLE s_hHeap; public:
CSomeClass();
CSomeClass(int value);
virtual ~CSomeClass(); //重载的该运算符为该类的成员函数
//所以 这里的size_t参数分配的字节数不必显示指定,该参数为sizeof(该类)
void* operator new(size_t);
//void* operator new(size_t, const char* fileName, int line);
void* operator new[](size_t); void operator delete(void* p);
void operator delete[](void* p);
//void operator delete(void* p, const char* fileName, int line); int m_value; }; size_t CSomeClass::s_Counts = ;
HANDLE CSomeClass::s_hHeap = NULL;
CSomeClass::CSomeClass(){} CSomeClass::CSomeClass(int value) : m_value(value){} CSomeClass::~CSomeClass(){} void* CSomeClass::operator new(size_t size)
//void* CSomeClass::operator new(size_t size, const char* fileName, int line)
{
//cout<<endl<<fileName<<", line "<<line<<endl; //如果是第一个实例,则创建堆
if (NULL == s_hHeap)
{
s_hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, , );
if (NULL == s_hHeap)
{
return NULL;
}
} void* p = HeapAlloc(s_hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size);
if (NULL != p)
{
++s_Counts;
} return p;
} void* CSomeClass::operator new[](size_t size)
{
//size大小为 sizeof(CSomeClass) * 个数 + 额外信息的大小 //注意,这里这种做法可能会有问题,只有当额外信息的大小 小于 对象大小时才合理!!! //nums为对象的个数
size_t nums = size / sizeof(CSomeClass); //sizeOfExtra的大小为存储额外信息的空间大小
size_t sizeOfExtra = size % sizeof(CSomeClass); if (NULL == s_hHeap)
{
s_hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, , );
if (NULL == s_hHeap)
{
return NULL;
}
} void* p = HeapAlloc(s_hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size);
if (NULL != p)
{
s_Counts += nums;
} return p;
} void CSomeClass::operator delete(void* p)
//void CSomeClass::operator delete(void* p, const char* fileName, int line)
{
if (NULL == p || NULL == s_hHeap)
{
return;
} BOOL bRet = HeapFree(s_hHeap, , p);
if (FALSE != bRet)
{
if (--s_Counts == )
{
HeapDestroy(s_hHeap);
//CloseHandle(s_hHeap); //保留这一行 有异常 invalid handle
s_hHeap = NULL;
}
}
} void CSomeClass::operator delete[](void* p)
{
if (NULL == p || NULL == s_hHeap)
{
return;
} size_t size = HeapSize(s_hHeap, , p);
size_t nums = size / sizeof(CSomeClass);
size_t sizeOfExtra = size % sizeof(CSomeClass); BOOL bRet = HeapFree(s_hHeap, , p);
if (FALSE != bRet)
{
if ( == (s_Counts -= nums))
{
HeapDestroy(s_hHeap);
s_hHeap = NULL;
}
}
} #ifdef _DEBUG
//#define new new(__FILE__, __LINE__)
//#define delete delete(__FILE__, __LINE__)
#endif int __cdecl _tmain(int argc, _TCHAR* argv[], _TCHAR* env[])
{
HANDLE hHeaps[] = {NULL};
DWORD dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[]), hHeaps); CSomeClass* pAddr2 = new CSomeClass[]();
dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[]), hHeaps);
delete[] pAddr2;
dwHeaps = GetProcessHeaps(sizeof(hHeaps) / sizeof(hHeaps[]), hHeaps); #pragma region 测试1
int size = sizeof(CSomeClass);
cout<<"sizeof(CSomeClass) is "<<size<<endl; CSomeClass* pObj = new CSomeClass(size * );
pObj->m_value = ;
CSomeClass* pObj2 = new CSomeClass();
pObj2->m_value = ; delete pObj;
//delete pObj2;
#pragma endregion CSomeClass* pArr = new CSomeClass[]; //UINT HeapCompact(HANDLE hHeap, DWORD fdwFlags)
//BOOL HeapLock(HANDLE hHeap);
//BOOL HeapUnLock(HANDLE hHeap);
//BOOL HeapWalk(HANDLE hHEap, PROCESS_HEAP_ENTRY pHeapEntry)
/*
BOOL bRet = FALSE;
PROCESS_HEAP_ENTRY phe;
phe.lpData = NULL; HeapLock(hHeap);
while(TRUE == (bRet = HeapWalk(hHeap, 0, &phe))
{
//do something
phe.lpData = NULL;
} HeapUnLock(hHeap); */ return ;
} /*
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0);
if (NULL == hHeap)
{
cerr<<"Create heap error \n";
return -1;
} //BOOL bRet = HeapSetInformation(hHeap, HeapEnableTerminationOnCorruption, NULL, 0); ULONG HeapInformationValue = 2;
BOOL bRet = HeapSetInformation(
hHeap, HeapCompatibilityInformation, &HeapInformationValue, sizeof(HeapInformationValue)); size_t size = 100;
PVOID pAddr = HeapAlloc(hHeap, HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, size);
if(NULL == pAddr)
{
HeapFree(hHeap, 0, pAddr); //释放申请的内存快
HeapDestroy(hHeap); //销毁堆
CloseHandle(hHeap); //关闭句柄
cerr<<"HeapAlloc error \n";
return -2;
} *((int*)(pAddr)) = 100;
cout<<"int pAddr is "<<*((int*)(pAddr))<<endl; cout<<"alloc size of heap is "<<HeapSize(hHeap, 0, pAddr)<<endl; PVOID pAddr2 = HeapReAlloc(hHeap, HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY | HEAP_REALLOC_IN_PLACE_ONLY,
pAddr, size * 20);
if (NULL == pAddr2)
{
HeapFree(hHeap, 0, pAddr);
HeapDestroy(hHeap);
CloseHandle(hHeap);
cerr<<"HeapReAlloc error \n"; return -3;
}
cout<<"int pAddr2 is "<<*((int*)(pAddr2) + 1)<<endl; cout<<"Realloc size of heap is "<<HeapSize(hHeap, 0, pAddr2)<<endl; HeapFree(hHeap, 0, pAddr2);
HeapDestroy(hHeap);
CloseHandle(hHeap);
cout<<"Heap opera is finish "<<endl; return 0;
} */