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

📄 mem.c

📁 一个用新方法实现的堆管理器
💻 C
字号:
//#include "includes.h"
#include "cxheap.h"

DS_HCB tabHcb[CFG_CxHeapCnt];

DS_HCB* CxHeapCreate(U16 heapSize, U16 pageSizeIn2Pow, U8 *pHeapMem)
{	
	U32	i;
	U32	rem;
	U8 *p;
	DS_HCB *pHCB;
	if (pageSizeIn2Pow>CAP_MaxHeapPageIn2Pow || pageSizeIn2Pow<CAP_MinHeapPageIn2Pow) 
		return 0;
	for (i=0;i<CFG_CxHeapCnt;i++)
	{
		CxHeapExitCritical();
		CxHeapEnterCritical();
		if (tabHcb[i].isInuse!=0) continue;		
		tabHcb[i].isInuse=1;
		CxHeapExitCritical();
		break;
	}
	pHCB=&(tabHcb[i]);
	#if CFG_CxHeapMT!=0
		CxHeapInitMutex(pHCB);
	#endif
	pHCB->pHeap=pHeapMem;
	pHCB->pageSizeIn2Pow=pageSizeIn2Pow;
	pHCB->pageSize=1;	//initialize pageSize to 1
	pHCB->fstFreePage=0;
	pHCB->allocCnt=0;
	pHCB->allocPageCnt=0;
	i=pageSizeIn2Pow;
	while (i)
	{
		--i;
		pHCB->pageSize<<=1;
	}	//calcuate the pageSize
	pHCB->pageCnt=heapSize>>pageSizeIn2Pow;	//calc the page count roughly.
	rem=heapSize-(pHCB->pageCnt<<pageSizeIn2Pow);	//calc the remain unused bytes of the heap
	while (rem< pHCB->pageCnt)
	{
		//every page uses a extra byte to express the state.
		--pHCB->pageCnt;
		rem+=pHCB->pageSize;
	}
	//the remain bytes are reserved for page state tags.
	pHCB->pHpSt= pHCB->pHeap+ (pHCB->pageCnt<<pageSizeIn2Pow);
	p= pHCB->pHpSt;
	//Clear page state tags.
	for (i=0;i< pHCB->pageCnt;i++)
	{
		*p++=0;
	}
	return pHCB;
}

U8 *CxHeapAllocBytes(U32 byteCnt, DS_HCB *pHCB)
{
	return CxHeapAlloc( (byteCnt+pHCB->pageSize-1)>>pHCB->pageSizeIn2Pow,pHCB);
}

U8 *CxHeapAlloc(U8 pageCnt, DS_HCB *pHCB)
{
	//Maximum of 255 pages can be alloced once.
	U8	*p;
	U8 freeCnt,i;
	#if CFG_ArgChkLv>0
	if (pHCB==0) return 0;
	if (pHCB->isInuse==0) return 0;
	#endif
	#if CFG_ArgChkLv>1
		if (pageCnt<1 || pageCnt> pHCB->pageCnt) return 0;
	#endif
	#if CFG_CxHeapMT!=0
	CxHeapLock(pHCB);
	#endif
	p=pHCB->pHpSt+pHCB->fstFreePage;
	freeCnt=0;
	for (i=pHCB->fstFreePage;i<pHCB->pageCnt;)
	{
		if (*p!=0)
		{
			//found a used series of pages.
			freeCnt=0;	//Not enough free pages found, retry.
			i+=*p;
			p+= *p;			
		}
		else
		{
			freeCnt++;
			p++;
			i++;
			if ( freeCnt==pageCnt)
			{
				//found a free series of pages!
				*(p-freeCnt)=pageCnt;
				pHCB->allocCnt++;
				pHCB->allocPageCnt+=pageCnt;
				//if the first page of this newly alloced part of heap is
				//the first free page then the first free page becomes
				//after this part.Otherwise there is a fragment of heap
				//before this newly alloced part of heap.
				if (pHCB->fstFreePage==i-pageCnt) pHCB->fstFreePage=i;
				#if CFG_CxHeapMT!=0
				CxHeapUnlock(pHCB);
				#endif				
				return (pHCB->pHeap+((i-pageCnt)<<pHCB->pageSizeIn2Pow));
			}							
		}
	}
	return 0;
}
U8	CxHeapFree(U8 *pHeap, DS_HCB *pHCB)
{
	U8	pageNum;
#if CFG_ArgChkLv>0
	if (pHCB==0 ) return 0xff;
	if (pHCB->isInuse==0) return 0xfe;
#endif
#if CFG_ArgChkLv>1
	if (pHeap<pHCB->pHeap ) return 0xfd;
	if ( pHeap>= pHCB->pHeap+(pHCB->pageCnt<<pHCB->pageSizeIn2Pow)-pHCB->pageSize) return 0xfc;
#endif
	#if CFG_CxHeapMT!=0
	CxHeapLock(pHCB);
	#endif
	pageNum=(pHeap-pHCB->pHeap)>>pHCB->pageSizeIn2Pow;
	*(pHCB->pHpSt+pageNum)=0;
	if (pHCB->fstFreePage>pageNum) pHCB->fstFreePage=pageNum;
	pHCB->allocCnt--;
	pHCB->allocPageCnt--;
	#if CFG_CxHeapMT!=0
	CxHeapUnlock(pHCB);
	#endif	
	return 0;
}

⌨️ 快捷键说明

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