⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.cpp

📁 pppoe client
💻 CPP
字号:

//********************************************************************
//	日期:	2004/08/24 - 24:8:2004   18:21
//	名前:	tiamo
//	描述:	memory
//			you should call init*** in your driver entry,and shut*** in your driver uload
//*********************************************************************

#include "Stdafx.h"

#ifdef DBG

// memory block list head
LIST_ENTRY g_ltMemoryBlocksHead;

// memory lock
NDIS_SPIN_LOCK g_lockMemory;

// alloc them to a 'mem' section
#pragma alloc_text("MEMORY",InitializeMemorySystem)
#pragma alloc_text("MEMORY",ShutdownMemorySystem)
#pragma alloc_text("MEMORY",DebugAllocMemory)
#pragma alloc_text("MEMORY",DebugFreeMemory)
#pragma alloc_text("MEMORY",DumpLeak)

// init memory system
VOID InitializeMemorySystem()
{
	NdisInitializeListHead(&g_ltMemoryBlocksHead);

	NdisAllocateSpinLock(&g_lockMemory);
}

// shut down memory system
VOID ShutdownMemorySystem()
{
	DumpLeak();

	NdisFreeSpinLock(&g_lockMemory);
}

// allocate memory,before user's memory,we allocate additional bytes to save our info,which will be used by DumpLeak
NDIS_STATUS DebugAllocMemory(PVOID *pVirtualAddress,ULONG ulSize,ULONG ulSig,ULONG ulFile,ULONG ulLine)
{
	NDIS_STATUS status = NdisAllocateMemoryWithTag(pVirtualAddress,ulSize + sizeof(MEMORY_BLOCK_HEADER),ulSig);

	if(status == NDIS_STATUS_SUCCESS)
	{
		PMEMORY_BLOCK_HEADER pHeader = static_cast<PMEMORY_BLOCK_HEADER>(*pVirtualAddress);
		pHeader->m_ulMemorySig = MEMORY_BLOCK_SIG;
		pHeader->m_ulFileName = ulFile;
		pHeader->m_ulLine = ulLine;
		pHeader->m_ulSig = ulSig;
		pHeader->m_ulSize = ulSize;

		NdisInterlockedInsertHeadList(&g_ltMemoryBlocksHead,&pHeader->m_ltMemoryBlockAnchor,&g_lockMemory);

		*pVirtualAddress = pHeader + 1;
	}

	return status;
}

// free memory
VOID DebugFreeMemory(PVOID pVirtualAddress,ULONG ulSize)
{
	if(pVirtualAddress)
	{
		PMEMORY_BLOCK_HEADER pHeader = static_cast<PMEMORY_BLOCK_HEADER>(pVirtualAddress);

		pHeader --;

		ASSERT(pHeader->m_ulSize == ulSize && pHeader->m_ulMemorySig == MEMORY_BLOCK_SIG);
		
		NdisAcquireSpinLock(&g_lockMemory);

		RemoveEntryList(&pHeader->m_ltMemoryBlockAnchor);

		NdisReleaseSpinLock(&g_lockMemory);

		pHeader->m_ulMemorySig = 0;

		NdisFreeMemory(pHeader,ulSize + sizeof(MEMORY_BLOCK_HEADER),0);
	}
}

// dump leak
VOID DumpLeak()
{
	NdisAcquireSpinLock(&g_lockMemory);

	while(!IsListEmpty(&g_ltMemoryBlocksHead))
	{
		PLIST_ENTRY pEntry = RemoveHeadList(&g_ltMemoryBlocksHead);

		ASSERT(pEntry);

		PMEMORY_BLOCK_HEADER pHead = CONTAINING_RECORD(pEntry,MEMORY_BLOCK_HEADER,m_ltMemoryBlockAnchor);

		ASSERT(pHead);

		ASSERT(pHead->m_ulMemorySig == MEMORY_BLOCK_SIG);

		DebugError(("Allocated memory at 0x%x,file %c%c%c%c,line %d,sig %d,size %d,not free!\n",pHead + 1,
					pHead->m_ucFileName[0],pHead->m_ucFileName[1],pHead->m_ucFileName[2],pHead->m_ucFileName[3],
					pHead->m_ulLine,pHead->m_ulSig,pHead->m_ulSize));

		NdisFreeMemory(pHead,pHead->m_ulSize + sizeof(MEMORY_BLOCK_HEADER),0);
	}

	NdisReleaseSpinLock(&g_lockMemory);
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -