1、Introduction What is memory pool? I think I must answer the question firstly, because it is important to beginner. In short, memory pool is a memory block which you got from system and use some unit of it to replace the system call malloc/free and new/delete. The advantage of the technology is reuse
2、 existing memory block so that reduce the times of system call. Its a hard work to give the definition. If you still cant understand the concept, please google it.Background Why to use memory pool? I have two reason:1To shorten the time that program allocate memory. Now I show an example to prove th
3、e technology have good efficiency: Collapse#include #include class CTestClass char m_chBuf4096;int main()DWORD count = GetTickCount(); for(unsigned int i=0; i#include char buf4100; /Simple Memory Pool class CTestClasschar m_chBuf4096;public: void *operator new(unsigned int uiSize)return (void *)buf;
4、void operator delete(void *p);int main()DWORD count = GetTickCount();for(unsigned int i=0; ipPrev = NULL;pCurUnit-pNext = m_pFreeMemBlock; /Insert the new unit at head.if(NULL != m_pFreeMemBlock)m_pFreeMemBlock-pPrev = pCurUnit;m_pFreeMemBlock = pCurUnit; The chief task of function CMemPool() is cre
5、ate a static double linked list. There are some skills of pointer which may be hard to beginner. I just explain a statement “struct _Unit *pCurUnit = (struct _Unit *)( (char *)m_pMemBlock + i*(ulUnitSize+sizeof(struct _Unit) )” ; the result is pCurUnit point to start address a memory unit, and the v
6、ariable “i” in this statement represent which unit. If you didnt master pointer, please refer to textbook. Collapse/*=CMemPool():Destructor of this class. Its task is to free memory block./=*/CMemPool:CMemPool()free(m_pMemBlock);The function CMemPool() is easy to be understood. I will explain nothin
7、g.Collapse/*=Alloc:To allocate a memory unit. If memory pool cant provide proper memory unit,It will call system function.Parameters:inulSizeMemory unit size.inbUseMemPoolWhether use memory pool.Return Values:Return a pointer to a memory unit./=*/void* CMemPool:Alloc(unsigned long ulSize, bool bUseM
8、emPool)if( ulSize m_ulUnitSize | false = bUseMemPool | NULL = m_pMemBlock | NULL = m_pFreeMemBlock)return malloc(ulSize);/Now FreeList isnt emptystruct _Unit *pCurUnit = m_pFreeMemBlock;m_pFreeMemBlock = pCurUnit-pNext; /Get a unit from free linkedlist.if(NULL != m_pFreeMemBlock)m_pFreeMemBlock-pPre
9、v = NULL;pCurUnit-pNext = m_pAllocatedMemBlock;if(NULL != m_pAllocatedMemBlock)m_pAllocatedMemBlock-pPrev = pCurUnit; m_pAllocatedMemBlock = pCurUnit;return (void *)(char *)pCurUnit + sizeof(struct _Unit) );The chief task of function Alloc() is to move a unit from “Free linked list” to “Allocated li
10、nked list” and will return the address of ninth byte of a unit. Collapse/*=Free:To free a memory unit. If the pointer of parameter point to a memory unit,then insert it to “Free linked list“. Otherwise, call system function “free“.Parameters:inpIt point to a memory unit and prepare to free it.Return
11、 Values:none/=*/void CMemPool:Free( void* p )if(m_pMemBlockpNext;if(NULL != m_pAllocatedMemBlock)m_pAllocatedMemBlock-pPrev = NULL;pCurUnit-pNext = m_pFreeMemBlock;if(NULL != m_pFreeMemBlock)m_pFreeMemBlock-pPrev = pCurUnit;m_pFreeMemBlock = pCurUnit;elsefree(p);The chief task of function Free() is
12、to move a unit from “Allocated linked list” to “Free linked list”. Using the memory pool How to use the memory pool? In the one hand, you must define an object of CMemPool for instance g_MemPool at proper place. In the other hand, you need override new/delete operator of some class which prepare to
13、use memory pool like previous example. Test The following picture is the result of my demo code in my computer.The following chart is made according to previous result.Its obvious that the memory pool can improve the effect of a program which use system call new/delete or malloc/free frequently. Thats off. Thank you.LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)About the AuthorJude Deng _Occupation: Software DeveloperLocation: ChinaMember